Java Code Examples for org.apache.helix.model.Message#getTgtName()

The following examples show how to use org.apache.helix.model.Message#getTgtName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSessionExpiryInTransition.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context) {
  MockParticipantManager manager = (MockParticipantManager) context.getManager();

  String instance = message.getTgtName();
  String partition = message.getPartitionName();
  if (instance.equals("localhost_12918") && partition.equals("TestDB0_1") // TestDB0_1 is SLAVE
                                                                          // on localhost_12918
      && !_done.getAndSet(true)) {
    try {
      ZkTestHelper.expireSession(manager.getZkClient());
    } catch (Exception e) {
      LOG.error("Exception expire zk-session", e);
    }
  }
}
 
Example 2
Source File: DistClusterControllerStateModel.java    From helix with Apache License 2.0 6 votes vote down vote up
@Override
public void onBecomeLeaderFromStandby(Message message, NotificationContext context)
    throws Exception {
  String clusterName = message.getPartitionName();
  String controllerName = message.getTgtName();

  logger.info(controllerName + " becoming leader from standby for " + clusterName);

  if (_controller == null) {
    _controller =
        HelixManagerFactory.getZKHelixManager(clusterName, controllerName,
            InstanceType.CONTROLLER, _zkAddr);
    _controller.setEnabledControlPipelineTypes(_enabledPipelineTypes);
    _controller.connect();
    _controller.startTimerTasks();
    logStateTransition("STANDBY", "LEADER", clusterName, controllerName);
  } else {
    logger.error("controller already exists:" + _controller.getInstanceName() + " for "
        + clusterName);
  }

}
 
Example 3
Source File: TestEnablePartitionDuringDisable.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context) {
  HelixManager manager = context.getManager();
  String clusterName = manager.getClusterName();

  String instance = message.getTgtName();
  String partitionName = message.getPartitionName();
  String fromState = message.getFromState();
  String toState = message.getToState();
  if (instance.equals("localhost_12919") && partitionName.equals("TestDB0_0")) {
    if (fromState.equals("SLAVE") && toState.equals("OFFLINE")) {
      slaveToOfflineCnt++;

      try {
        String command = "--zkSvr " + ZK_ADDR + " --enablePartition true " + clusterName
            + " localhost_12919 TestDB0 TestDB0_0";

        ClusterSetup.processCommandLineArgs(command.split("\\s+"));
      } catch (Exception e) {
        LOG.error("Exception in cluster setup", e);
      }

    } else if (slaveToOfflineCnt > 0 && fromState.equals("OFFLINE")
        && toState.equals("SLAVE")) {
      offlineToSlave++;
    }
  }
}
 
Example 4
Source File: DistClusterControllerStateModel.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void onBecomeStandbyFromLeader(Message message, NotificationContext context) {
  String clusterName = message.getPartitionName();
  String controllerName = message.getTgtName();

  logger.info(controllerName + " becoming standby from leader for " + clusterName);

  if (_controller != null) {
    reset();
    logStateTransition("LEADER", "STANDBY", clusterName, controllerName);
  } else {
    logger.error("No controller exists for " + clusterName);
  }
}
 
Example 5
Source File: TestParticipantManager.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context)
    throws InterruptedException {
  String instance = message.getTgtName();
  String partition = message.getPartitionName();
  if (instance.equals("localhost_12918") && partition.equals("TestDB0_0")
      && !_done.getAndSet(true)) {
    _startCountdown.countDown();
    // this await will be interrupted since we cancel the task during handleNewSession
    _endCountdown.await();
  }
}
 
Example 6
Source File: InstanceMessagesCache.java    From helix with Apache License 2.0 5 votes vote down vote up
private void cacheRelayMessage(Message relayMessage, Message hostMessage) {
  String instanceName = relayMessage.getTgtName();
  if (!_relayMessageCache.containsKey(instanceName)) {
    _relayMessageCache.put(instanceName, Maps.<String, Message> newHashMap());
  }
  if (!_relayMessageCache.get(instanceName).containsKey(relayMessage.getId())) {
    // Only log if the message doesn't already exist in the cache
    LOG.info("Add relay message to relay cache " + relayMessage.getMsgId() + ", hosted message "
        + hostMessage.getMsgId());
  }
  _relayMessageCache.get(instanceName).put(relayMessage.getId(), relayMessage);
  _relayHostMessageCache.put(relayMessage.getMsgId(), hostMessage);
}
 
Example 7
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "OFFLINE", from = "ERROR")
public void onBecomeOfflineFromError(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  LOGGER.info(instanceName + " becomes OFFLINE from ERROR for " + partitionName);
}
 
Example 8
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "OFFLINE", from = "SLAVE")
public void onBecomeOfflineFromSlave(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  LOGGER.info(instanceName + " becomes OFFLINE from SLAVE for " + partitionName);
}
 
Example 9
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "MASTER", from = "SLAVE")
public void onBecomeMasterFromSlave(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  LOGGER.info(instanceName + " becomes MASTER from SLAVE for " + partitionName);
}
 
Example 10
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "SLAVE", from = "MASTER")
public void onBecomeSlaveFromMaster(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  LOGGER.info(instanceName + " becomes SLAVE from MASTER for " + partitionName);
}
 
Example 11
Source File: TestMessageThrottle2.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "SLAVE", from = "OFFLINE")
public void onBecomeSlaveFromOffline(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  LOGGER.info(instanceName + " becomes SLAVE from OFFLINE for " + partitionName);
}
 
Example 12
Source File: TaskStateModel.java    From helix with Apache License 2.0 4 votes vote down vote up
private void startTask(Message msg, String taskPartition) {
  JobConfig cfg = TaskUtil.getJobConfig(_manager, msg.getResourceName());
  TaskConfig taskConfig = null;
  String command = cfg.getCommand();

  // Get a task-specific command if specified
  JobContext ctx = TaskUtil.getJobContext(_manager, msg.getResourceName());
  int pId = Integer.parseInt(taskPartition.substring(taskPartition.lastIndexOf('_') + 1));
  if (ctx.getTaskIdForPartition(pId) != null) {
    taskConfig = cfg.getTaskConfig(ctx.getTaskIdForPartition(pId));
    if (taskConfig != null) {
      if (taskConfig.getCommand() != null) {
        command = taskConfig.getCommand();
      }
    }
  }

  // Report a target if that was used to assign the partition
  String target = ctx.getTargetForPartition(pId);
  if (taskConfig == null && target != null) {
    taskConfig = TaskConfig.Builder.from(target);
  }

  // Populate a task callback context
  TaskCallbackContext callbackContext = new TaskCallbackContext();
  callbackContext.setManager(_manager);
  callbackContext.setJobConfig(cfg);
  callbackContext.setTaskConfig(taskConfig);

  // Create a task instance with this command
  if (command == null || _taskFactoryRegistry == null
      || !_taskFactoryRegistry.containsKey(command)) {
    throw new IllegalStateException("No callback implemented(or not registered) for task " + command);
  }
  TaskFactory taskFactory = _taskFactoryRegistry.get(command);
  Task task = taskFactory.createNewTask(callbackContext);

  if (task instanceof UserContentStore) {
    ((UserContentStore) task).init(_manager, cfg.getWorkflow(), msg.getResourceName(), taskPartition);
  }

  // Submit the task for execution
  _taskRunner =
      new TaskRunner(task, msg.getResourceName(), taskPartition, msg.getTgtName(), _manager,
          msg.getTgtSessionId());
  _taskExecutor.submit(_taskRunner);
  _taskRunner.waitTillStarted();

  // Set up a timer to cancel the task when its time out expires.

  timeout_task = _timeoutTaskExecutor.schedule(new TimerTask() {
    @Override
    public void run() {
      if (_taskRunner != null) {
        _taskRunner.timeout();
      }
    }
  }, cfg.getTimeoutPerTask(), TimeUnit.MILLISECONDS);
}
 
Example 13
Source File: DummyParticipant.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "SLAVE", from = "OFFLINE")
public void onBecomeSlaveFromOffline(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  System.out.println(instanceName + " becomes SLAVE from OFFLINE for " + partitionName);
}
 
Example 14
Source File: InstanceMessagesCache.java    From helix with Apache License 2.0 4 votes vote down vote up
private void checkRelayHost(Message relayMessage, Map<String, LiveInstance> liveInstanceMap,
    Map<String, Map<String, Map<String, CurrentState>>> currentStateMap, Message hostedMessage) {

  long currentTime = System.currentTimeMillis();

  String sessionId = hostedMessage.getTgtSessionId();
  String relayInstance = hostedMessage.getTgtName();
  String resourceName = hostedMessage.getResourceName();
  String partitionName = hostedMessage.getPartitionName();

  if (!liveInstanceMap.containsKey(relayInstance)) {
    // If the p2p forwarding host is no longer live, we should not remove the relay message immediately
    // since we do not know whether the relay message was forwarded before the instance went offline.
    setMessageRelayTime(relayMessage, currentTime);
    return;
  }
  String instanceSessionId = liveInstanceMap.get(relayInstance).getEphemeralOwner();
  if (!instanceSessionId.equals(sessionId)) {
    LOG.info("Relay instance sessionId {} does not match sessionId {} in hosted message {}, "
            + "set relay message {} to be expired.", instanceSessionId, sessionId,
        relayMessage.getId(), hostedMessage.getMsgId());
    setMessageRelayTime(relayMessage, currentTime);
    return;
  }

  Map<String, Map<String, CurrentState>> instanceCurrentStateMap =
      currentStateMap.get(relayInstance);
  if (instanceCurrentStateMap == null || !instanceCurrentStateMap.containsKey(sessionId)) {
    LOG.warn(
        "CurrentStateMap null for {}, session {}, set relay messages {} to be expired. Hosted message {}.",
        relayInstance, sessionId, relayMessage.getId(), hostedMessage.getId());
    setMessageRelayTime(relayMessage, currentTime);
    return;
  }

  Map<String, CurrentState> sessionCurrentStateMap = instanceCurrentStateMap.get(sessionId);
  CurrentState currentState = sessionCurrentStateMap.get(resourceName);

  if (currentState == null) {
    LOG.info("No currentState found for {} on {}, set relay message {} to be expired.",
        resourceName, relayInstance, relayMessage.getId());
    setMessageRelayTime(relayMessage, currentTime);
    return;
  }

  String partitionState = currentState.getState(partitionName);
  String targetState = hostedMessage.getToState();
  String fromState = hostedMessage.getFromState();

  // The relay host partition state has been changed after relay message was created.
  if (!fromState.equals(partitionState)) {
    // If the partition on the relay host turned to ERROR while transited from top state,
    // we can remove the cached relay message right away since participant won't forward the relay message anyway.
    if (HelixDefinedState.ERROR.name().equals(partitionState) && fromState
        .equals(currentState.getPreviousState(partitionName))) {
      LOG.info("Partition {} got to ERROR from the top state, "
              + "expiring relay message {} immediately. Hosted message {}.", partitionName,
          relayMessage.getId(), hostedMessage.getId());
      relayMessage.setExpired(true);
      return;
    }

    // If the partition completed the transition, set the relay time to be the actual time when state transition completed.
    if (targetState.equals(partitionState) && fromState
        .equals(currentState.getPreviousState(partitionName))) {
      // The relay host already completed the state transition.
      long completeTime = currentState.getEndTime(partitionName);
      if (completeTime > relayMessage.getCreateTimeStamp()) {
        setMessageRelayTime(relayMessage, completeTime);
        LOG.error("Target state for partition {} matches the hosted message's target state, "
            + "set relay message {} to be expired.", partitionName, relayMessage.getId());
        return;
      }
    }

    // For all other situations, set relay time to be current time.
    setMessageRelayTime(relayMessage, currentTime);
    // the state has been changed after it completed the required state transition (maybe another state-transition happened).
    LOG.info("Current state {} for partition {} does not match hosted message's from state, "
            + "set relay message {} to be expired.", partitionState, partitionName,
        relayMessage.getId());
  }
}
 
Example 15
Source File: DefaultSchedulerMessageHandlerFactory.java    From helix with Apache License 2.0 4 votes vote down vote up
void handleMessageUsingScheduledTaskQueue(Criteria recipientCriteria, Message messageTemplate,
    String controllerMsgId) {
  HelixDataAccessor accessor = _manager.getHelixDataAccessor();
  Builder keyBuilder = accessor.keyBuilder();

  String clusterName = recipientCriteria.getClusterName();
  if (clusterName != null && !clusterName.equals(_manager.getClusterName())) {
    throw new HelixException(String.format(
        "ScheduledTaskQueue cannot send message to another cluster. Local cluster name %s, remote cluster name %s.",
        _manager.getClusterName(), clusterName));
  }

  Map<String, String> sendSummary = new HashMap<String, String>();
  sendSummary.put("MessageCount", "0");
  Map<InstanceType, List<Message>> messages =
      _manager.getMessagingService().generateMessage(recipientCriteria, messageTemplate);

  // Calculate tasks, and put them into the idealState of the SCHEDULER_TASK_QUEUE resource.
  // List field are the destination node, while the Message parameters are stored in the
  // mapFields
  // task throttling can be done on SCHEDULER_TASK_QUEUE resource
  if (messages.size() > 0) {
    String taskQueueName = _message.getRecord().getSimpleField(SCHEDULER_TASK_QUEUE);
    if (taskQueueName == null) {
      throw new HelixException("SchedulerTaskMessage need to have " + SCHEDULER_TASK_QUEUE
          + " specified.");
    }
    IdealState newAddedScheduledTasks = new IdealState(taskQueueName);
    newAddedScheduledTasks.setBucketSize(TASKQUEUE_BUCKET_NUM);
    newAddedScheduledTasks.setStateModelDefRef(SCHEDULER_TASK_QUEUE);

    synchronized (_manager) {
      int existingTopPartitionId = 0;
      IdealState currentTaskQueue =
          _manager.getHelixDataAccessor().getProperty(
              accessor.keyBuilder().idealStates(newAddedScheduledTasks.getId()));
      if (currentTaskQueue != null) {
        existingTopPartitionId = findTopPartitionId(currentTaskQueue) + 1;
      }

      List<Message> taskMessages = (List<Message>) (messages.values().toArray()[0]);
      for (Message task : taskMessages) {
        String partitionId = taskQueueName + "_" + existingTopPartitionId;
        existingTopPartitionId++;
        String instanceName = task.getTgtName();
        newAddedScheduledTasks.setPartitionState(partitionId, instanceName, "COMPLETED");
        task.getRecord().setSimpleField(instanceName, "COMPLETED");
        task.getRecord().setSimpleField(CONTROLLER_MSG_ID, controllerMsgId);

        List<String> priorityList = new LinkedList<String>();
        priorityList.add(instanceName);
        newAddedScheduledTasks.getRecord().setListField(partitionId, priorityList);
        newAddedScheduledTasks.getRecord().setMapField(partitionId,
            task.getRecord().getSimpleFields());
        _logger.info("Scheduling for controllerMsg " + controllerMsgId + " , sending task "
            + partitionId + " " + task.getMsgId() + " to " + instanceName);

        if (_logger.isDebugEnabled()) {
          _logger.debug(task.getRecord().getSimpleFields().toString());
        }
      }
      _manager.getHelixDataAccessor().updateProperty(
          accessor.keyBuilder().idealStates(newAddedScheduledTasks.getId()),
          newAddedScheduledTasks);
      sendSummary.put("MessageCount", "" + taskMessages.size());
    }
  }
  // Record the number of messages sent into scheduler message status updates

  ZNRecord statusUpdate =
      accessor.getProperty(
          keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(),
              _message.getMsgId())).getRecord();

  statusUpdate.getMapFields().put("SentMessageCount", sendSummary);
  accessor.updateProperty(keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.name(),
      _message.getMsgId()), new StatusUpdate(statusUpdate));
}
 
Example 16
Source File: DummyParticipant.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "OFFLINE", from = "ERROR")
public void onBecomeOfflineFromError(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  System.out.println(instanceName + " becomes OFFLINE from ERROR for " + partitionName);
}
 
Example 17
Source File: DummyParticipant.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "DROPPED", from = "OFFLINE")
public void onBecomeDroppedFromOffline(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  System.out.println(instanceName + " becomes DROPPED from OFFLINE for " + partitionName);
}
 
Example 18
Source File: DummyParticipant.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "OFFLINE", from = "SLAVE")
public void onBecomeOfflineFromSlave(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  System.out.println(instanceName + " becomes OFFLINE from SLAVE for " + partitionName);
}
 
Example 19
Source File: DummyParticipant.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "SLAVE", from = "MASTER")
public void onBecomeSlaveFromMaster(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  System.out.println(instanceName + " becomes SLAVE from MASTER for " + partitionName);
}
 
Example 20
Source File: DummyParticipant.java    From helix with Apache License 2.0 4 votes vote down vote up
@Transition(to = "MASTER", from = "SLAVE")
public void onBecomeMasterFromSlave(Message message, NotificationContext context) {
  String partitionName = message.getPartitionName();
  String instanceName = message.getTgtName();
  System.out.println(instanceName + " becomes MASTER from SLAVE for " + partitionName);
}