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

The following examples show how to use org.apache.helix.model.Message#getMsgId() . 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: StatusUpdateUtil.java    From helix with Apache License 2.0 6 votes vote down vote up
/**
 * Create a ZNRecord for a message, which stores the content of the message (stored in
 * simple fields) into the ZNRecord mapFields. In this way, the message update can be
 * merged with the previous status update record in the zookeeper. See ZNRecord.merge()
 * for more details.
 */
ZNRecord createMessageLogRecord(Message message) {
  ZNRecord result = new ZNRecord(getStatusUpdateRecordName(message));
  String mapFieldKey = "MESSAGE " + message.getMsgId();
  result.setMapField(mapFieldKey, new TreeMap<String, String>());

  // Store all the simple fields of the message in the new ZNRecord's map
  // field.
  for (String simpleFieldKey : message.getRecord().getSimpleFields().keySet()) {
    result.getMapField(mapFieldKey).put(simpleFieldKey,
        message.getRecord().getSimpleField(simpleFieldKey));
  }
  if (message.getResultMap() != null) {
    result.setMapField("MessageResult", message.getResultMap());
  }
  return result;
}
 
Example 2
Source File: DefaultControllerMessageHandlerFactory.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
  String type = message.getMsgType();

  if (!type.equals(getMessageType())) {
    throw new HelixException("Unexpected msg type for message " + message.getMsgId() + " type:"
        + message.getMsgType());
  }

  return new DefaultControllerMessageHandler(message, context);
}
 
Example 3
Source File: DefaultParticipantErrorMessageHandlerFactory.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
  String type = message.getMsgType();

  if (!type.equals(getMessageType())) {
    throw new HelixException("Unexpected msg type for message " + message.getMsgId() + " type:"
        + message.getMsgType());
  }

  return new DefaultParticipantErrorMessageHandler(message, context, _manager);
}
 
Example 4
Source File: DefaultSchedulerMessageHandlerFactory.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
  String type = message.getMsgType();

  if (!type.equals(getMessageType())) {
    throw new HelixException("Unexpected msg type for message " + message.getMsgId() + " type:"
        + message.getMsgType());
  }

  return new DefaultSchedulerMessageHandler(message, context, _manager);
}
 
Example 5
Source File: DefaultMessagingService.java    From helix with Apache License 2.0 5 votes vote down vote up
private List<Message> generateMessagesForController(Message message) {
  List<Message> messages = new ArrayList<Message>();
  String id = (message.getMsgId() == null) ? UUID.randomUUID().toString() : message.getMsgId();
  Message newMessage = new Message(message.getRecord(), id);
  newMessage.setMsgId(id);
  newMessage.setSrcName(_manager.getInstanceName());
  newMessage.setTgtName(InstanceType.CONTROLLER.name());
  messages.add(newMessage);
  return messages;
}
 
Example 6
Source File: HelixStateMachineEngine.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public MessageHandler createHandler(Message message, NotificationContext context) {
  String type = message.getMsgType();

  if (!type.equals(MessageType.STATE_TRANSITION.name()) && !type
      .equals(MessageType.STATE_TRANSITION_CANCELLATION.name())) {
    throw new HelixException("Expect state-transition message type, but was "
        + message.getMsgType() + ", msgId: " + message.getMsgId());
  }

  String partitionKey = message.getPartitionName();
  String stateModelName = message.getStateModelDef();
  String resourceName = message.getResourceName();
  String sessionId = message.getTgtSessionId();
  int bucketSize = message.getBucketSize();

  if (stateModelName == null) {
    logger
        .error("Fail to create msg-handler because message does not contain stateModelDef. msgId: "
            + message.getId());
    return null;
  }

  String factoryName = message.getStateModelFactoryName();
  if (factoryName == null) {
    factoryName = HelixConstants.DEFAULT_STATE_MODEL_FACTORY;
  }

  StateModelFactory<? extends StateModel> stateModelFactory =
      getStateModelFactory(stateModelName, factoryName);
  if (stateModelFactory == null) {
    logger.warn("Fail to create msg-handler because cannot find stateModelFactory for model: "
        + stateModelName + " using factoryName: " + factoryName + " for resource: "
        + resourceName);
    return null;
  }

  // check if the state model definition exists and cache it
  if (!_stateModelDefs.containsKey(stateModelName)) {
    HelixDataAccessor accessor = _manager.getHelixDataAccessor();
    Builder keyBuilder = accessor.keyBuilder();
    StateModelDefinition stateModelDef =
        accessor.getProperty(keyBuilder.stateModelDef(stateModelName));
    if (stateModelDef == null) {
      throw new HelixException("fail to create msg-handler because stateModelDef for "
          + stateModelName + " does NOT exist");
    }
    _stateModelDefs.put(stateModelName, stateModelDef);
  }

  if (!message.getBatchMessageMode()) {
    String initState = _stateModelDefs.get(message.getStateModelDef()).getInitialState();
    StateModel stateModel = stateModelFactory.getStateModel(resourceName, partitionKey);
    if (stateModel == null) {
      stateModel = stateModelFactory.createAndAddStateModel(resourceName, partitionKey);
      if (stateModelName.equals(TaskConstants.STATE_MODEL_NAME)
          && message.getToState().equals(TaskPartitionState.DROPPED.name())) {
        // If stateModel is null, that means there was a reboot of the Participant. Then the
        // purpose of this first message must be to drop the task. We manually set the current
        // state to be the same state of fromState (which Controller inferred from JobContext) to
        // allow the Participant to successfully process this dropping transition
        stateModel.updateState(message.getFromState());
      } else {
        stateModel.updateState(initState);
      }
    }
    if (message.getMsgType().equals(MessageType.STATE_TRANSITION_CANCELLATION.name())) {
      return new HelixStateTransitionCancellationHandler(stateModel, message, context);
    } else {
      // create currentStateDelta for this partition
      // TODO: move currentStateDelta to StateTransitionMsgHandler
      CurrentState currentStateDelta = new CurrentState(resourceName);
      currentStateDelta.setSessionId(sessionId);
      currentStateDelta.setStateModelDefRef(stateModelName);
      currentStateDelta.setStateModelFactoryName(factoryName);
      currentStateDelta.setBucketSize(bucketSize);

      currentStateDelta.setState(partitionKey,
          (stateModel.getCurrentState() == null) ? initState : stateModel.getCurrentState());

      return new HelixStateTransitionHandler(stateModelFactory, stateModel, message, context,
          currentStateDelta);
    }
  } else {
    BatchMessageWrapper wrapper = stateModelFactory.getBatchMessageWrapper(resourceName);
    if (wrapper == null) {
      wrapper = stateModelFactory.createAndAddBatchMessageWrapper(resourceName);
    }

    // get executor-service for the message
    TaskExecutor executor = (TaskExecutor) context.get(MapKey.TASK_EXECUTOR.toString());
    if (executor == null) {
      logger.error(
          "fail to get executor-service for batch message: " + message.getId() + ". msgType: "
              + message.getMsgType() + ", resource: " + message.getResourceName());
      return null;
    }
    return new BatchMessageHandler(message, context, this, wrapper, executor);
  }
}
 
Example 7
Source File: StatusUpdateUtil.java    From helix with Apache License 2.0 4 votes vote down vote up
private String getStatusUpdateKey(Message message) {
  if (message.getMsgType().equalsIgnoreCase(MessageType.STATE_TRANSITION.name())) {
    return message.getPartitionName();
  }
  return message.getMsgId();
}
 
Example 8
Source File: StatusUpdateUtil.java    From helix with Apache License 2.0 4 votes vote down vote up
String getStatusUpdateRecordName(Message message) {
  if (message.getMsgType().equalsIgnoreCase(MessageType.STATE_TRANSITION.name())) {
    return message.getTgtSessionId() + "__" + message.getResourceName();
  }
  return message.getMsgId();
}
 
Example 9
Source File: MockHelixTaskExecutor.java    From helix with Apache License 2.0 4 votes vote down vote up
void checkDuplicatedMessages(List<Message> messages) {
  HelixDataAccessor accessor = manager.getHelixDataAccessor();
  PropertyKey.Builder keyBuilder = accessor.keyBuilder();
  PropertyKey path = keyBuilder.currentStates(manager.getInstanceName(), manager.getSessionId());
  Map<String, CurrentState> currentStateMap = accessor.getChildValuesMap(path, true);

  Set<String> seenPartitions = new HashSet<>();
  for (Message message : messages) {
    if (message.getMsgType().equals(Message.MessageType.STATE_TRANSITION.name())) {
      String resource = message.getResourceName();
      String partition = message.getPartitionName();

      //System.err.println(message.getMsgId());
      String key = resource + "-" + partition;
      if (seenPartitions.contains(key)) {
        //System.err.println("Duplicated message received for " + resource + ":" + partition);
        duplicatedMessages++;
      }
      seenPartitions.add(key);

      String toState = message.getToState();
      String state = null;
      if (currentStateMap.containsKey(resource)) {
        CurrentState currentState = currentStateMap.get(resource);
        state = currentState.getState(partition);
      }

      if (toState.equals(state) && message.getMsgState() == Message.MessageState.NEW) {
        //            logger.error(
        //                "Extra message: " + message.getMsgId() + ", Partition is already in target state "
        //                    + toState + " for " + resource + ":" + partition);
        extraStateTransition++;
      }

      String messageTarget =
          getMessageTarget(message.getResourceName(), message.getPartitionName());

      if (message.getMsgState() == Message.MessageState.NEW &&
          _messageTaskMap.containsKey(messageTarget)) {
        String taskId = _messageTaskMap.get(messageTarget);
        MessageTaskInfo messageTaskInfo = _taskMap.get(taskId);
        Message existingMsg = messageTaskInfo.getTask().getMessage();
        if (existingMsg.getMsgId() != message.getMsgId())
          //            logger.error("Duplicated message In Progress: " + message.getMsgId()
          //                    + ", state transition in progress with message " + existingMsg.getMsgId()
          //                    + " to " + toState + " for " + resource + ":" + partition);
          duplicatedMessagesInProgress ++;
      }
    }
  }
}
 
Example 10
Source File: SchedulerTasksResource.java    From helix with Apache License 2.0 4 votes vote down vote up
@Override
public Representation post(Representation entity) {
  try {
    String clusterName = (String) getRequest().getAttributes().get("clusterName");
    Form form = new Form(entity);
    ZkClient zkClient =
        (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);

    String msgTemplateString =
        ClusterRepresentationUtil.getFormJsonParameterString(form, MESSAGETEMPLATE);
    if (msgTemplateString == null) {
      throw new HelixException("SchedulerTasksResource need to have MessageTemplate specified.");
    }
    Map<String, String> messageTemplate =
        ClusterRepresentationUtil.getFormJsonParameters(form, MESSAGETEMPLATE);

    String criteriaString = ClusterRepresentationUtil.getFormJsonParameterString(form, CRITERIA);
    if (criteriaString == null) {
      throw new HelixException("SchedulerTasksResource need to have Criteria specified.");
    }
    HelixDataAccessor accessor =
        ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
    if (leader == null) {
      throw new HelixException("There is no leader for the cluster " + clusterName);
    }

    Message schedulerMessage =
        new Message(MessageType.SCHEDULER_MSG, UUID.randomUUID().toString());
    schedulerMessage.getRecord().getSimpleFields().put(CRITERIA, criteriaString);

    schedulerMessage.getRecord().getMapFields().put(MESSAGETEMPLATE, messageTemplate);

    schedulerMessage.setTgtSessionId(leader.getEphemeralOwner());
    schedulerMessage.setTgtName("CONTROLLER");
    schedulerMessage.setSrcInstanceType(InstanceType.CONTROLLER);
    String taskQueueName =
        ClusterRepresentationUtil.getFormJsonParameterString(form, TASKQUEUENAME);
    if (taskQueueName != null && taskQueueName.length() > 0) {
      schedulerMessage.getRecord().setSimpleField(
          DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, taskQueueName);
    }
    accessor.setProperty(accessor.keyBuilder().controllerMessage(schedulerMessage.getMsgId()),
        schedulerMessage);

    Map<String, String> resultMap = new HashMap<String, String>();
    resultMap.put("StatusUpdatePath", PropertyPathBuilder.controllerStatusUpdate(
        clusterName, MessageType.SCHEDULER_MSG.name(), schedulerMessage.getMsgId()));
    resultMap.put("MessageType", Message.MessageType.SCHEDULER_MSG.name());
    resultMap.put("MsgId", schedulerMessage.getMsgId());

    // Assemble the rest URL for task status update
    String ipAddress = InetAddress.getLocalHost().getCanonicalHostName();
    String url =
        "http://" + ipAddress + ":" + getContext().getAttributes().get(RestAdminApplication.PORT)
            + "/clusters/" + clusterName + "/Controller/statusUpdates/SCHEDULER_MSG/"
            + schedulerMessage.getMsgId();
    resultMap.put("statusUpdateUrl", url);

    getResponse().setEntity(ClusterRepresentationUtil.ObjectToJson(resultMap),
        MediaType.APPLICATION_JSON);
    getResponse().setStatus(Status.SUCCESS_OK);
  } catch (Exception e) {
    getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
        MediaType.APPLICATION_JSON);
    getResponse().setStatus(Status.SUCCESS_OK);
    LOG.error("", e);
  }
  return null;
}