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

The following examples show how to use org.apache.helix.model.Message#getToState() . 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: MessageGenerationPhase.java    From helix with Apache License 2.0 5 votes vote down vote up
private Message generateCancellationMessageForPendingMessage(final String desiredState, final String currentState,
    final String nextState, final Message pendingMessage, final HelixManager manager,
    final Resource resource, final Partition partition, final Map<String, String> sessionIdMap,
    final String instanceName, final StateModelDefinition stateModelDef,
    final Message cancellationMessage, final boolean isCancellationEnabled) {

  Message message = null;

  if (pendingMessage != null) {
    String pendingState = pendingMessage.getToState();
    if (nextState.equalsIgnoreCase(pendingState)) {
      LogUtil.logInfo(logger, _eventId,
          "Message already exists for " + instanceName + " to transit " + resource
              .getResourceName() + "." + partition.getPartitionName() + " from "
              + currentState + " to " + nextState + ", isRelay: " + pendingMessage.isRelayMessage());
    } else if (currentState.equalsIgnoreCase(pendingState)) {
      LogUtil.logInfo(logger, _eventId,
          "Message hasn't been removed for " + instanceName + " to transit " + resource
              .getResourceName() + "." + partition.getPartitionName() + " to "
              + pendingState + ", desiredState: " + desiredState + ", isRelay: " + pendingMessage.isRelayMessage());
    } else {
      LogUtil.logInfo(logger, _eventId,
          "IdealState changed before state transition completes for " + resource
              .getResourceName() + "." + partition.getPartitionName() + " on "
              + instanceName + ", pendingState: " + pendingState + ", currentState: "
              + currentState + ", nextState: " + nextState + ", isRelay: " + pendingMessage.isRelayMessage());

      message = createStateTransitionCancellationMessage(manager, resource,
          partition.getPartitionName(), instanceName, sessionIdMap.get(instanceName),
          stateModelDef.getId(), pendingMessage.getFromState(), pendingState, nextState,
          cancellationMessage, isCancellationEnabled, currentState);
    }
  }
  return message;
}
 
Example 2
Source File: HelixTask.java    From helix with Apache License 2.0 5 votes vote down vote up
private void reportMessageStat(HelixManager manager, Message message, HelixTaskResult taskResult) {
  // report stat
  if (!message.getMsgType().equals(MessageType.STATE_TRANSITION.name())) {
    return;
  }
  long now = new Date().getTime();
  long msgReadTime = message.getReadTimeStamp();
  long msgExecutionStartTime = message.getExecuteStartTimeStamp();
  if (msgReadTime != 0 && msgExecutionStartTime != 0) {
    long totalDelay = now - msgReadTime;
    long executionDelay = now - msgExecutionStartTime;
    long msgLatency = msgReadTime - message.getCreateTimeStamp();
    if (totalDelay >= 0 && executionDelay >= 0) {
      String fromState = message.getFromState();
      String toState = message.getToState();
      String transition = fromState + "--" + toState;

      StateTransitionContext cxt =
          new StateTransitionContext(manager.getClusterName(), manager.getInstanceName(),
              message.getResourceName(), transition);

      StateTransitionDataPoint data =
          new StateTransitionDataPoint(totalDelay, executionDelay, msgLatency,
              taskResult.isSuccess());
      _executor.getParticipantMonitor().reportTransitionStat(cxt, data);
    }
  } else {
    logger.warn(
        "message read time and start execution time not recorded. State transition delay time is not available, message read time {}, Execute start time {}.",
        msgReadTime, msgExecutionStartTime);
  }
}
 
Example 3
Source File: TestResetPartitionState.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context) {
  // System.err.println("doReset() invoked");
  super.doTransition(message, context);
  String fromState = message.getFromState();
  String toState = message.getToState();
  if (fromState.equals("ERROR") && toState.equals("OFFLINE")) {
    _errToOfflineInvoked++;
  }
}
 
Example 4
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 5
Source File: TestP2PSingleTopState.java    From helix with Apache License 2.0 5 votes vote down vote up
public void doTransition(Message message, NotificationContext context) {
  String to = message.getToState();
  String resource = message.getResourceName();
  String partition = message.getPartitionName();
  String key = resource + "." + partition;

  if (to.equals(MasterSlaveSMD.States.MASTER.name())) {
    Map<String, String> mapFields = new HashMap<>(ExternalViews.get(key));
    if (mapFields.values().contains(MasterSlaveSMD.States.MASTER.name())) {
      Map<String, String> newMapFile = new HashMap<>(mapFields);
      newMapFile.put(_instanceName, to);
      duplicatedPartitionsSnapshot.put(key, newMapFile);
    }

    totalToMaster.incrementAndGet();
    if (message.isRelayMessage()) {
      totalRelayMessage.incrementAndGet();
    }
  }

  ExternalViews.putIfAbsent(key, new ConcurrentHashMap<>());
  if (to.equalsIgnoreCase("DROPPED")) {
    ExternalViews.get(key).remove(_instanceName);
  } else {
    ExternalViews.get(key).put(_instanceName, to);
  }
}
 
Example 6
Source File: ErrTransition.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context) {
  String fromState = message.getFromState();
  String toState = message.getToState();
  String partition = message.getPartitionName();

  String key = (fromState + "-" + toState).toUpperCase();
  if (_errPartitions.containsKey(key) && _errPartitions.get(key).contains(partition)) {
    String errMsg =
        "IGNORABLE: test throw exception in msgId: " + message.getId() + " for " + partition
            + " transit from " + fromState + " to " + toState;
    throw new RuntimeException(errMsg);
  }
}
 
Example 7
Source File: TestResetPartitionState.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public void doTransition(Message message, NotificationContext context) {
  super.doTransition(message, context);
  String fromState = message.getFromState();
  String toState = message.getToState();
  if (fromState.equals("ERROR") && toState.equals("OFFLINE")) {
    // System.err.println("doReset() invoked");
    _errToOfflineInvoked.incrementAndGet();
  }
}
 
Example 8
Source File: HelixStateTransitionHandler.java    From helix with Apache License 2.0 4 votes vote down vote up
private void invoke(HelixManager manager, NotificationContext context, HelixTaskResult taskResult,
    Message message) throws IllegalAccessException, InvocationTargetException,
    InterruptedException, HelixRollbackException {
  _statusUpdateUtil.logInfo(message, HelixStateTransitionHandler.class,
      "Message handling invoking", manager);

  // by default, we invoke state transition function in state model
  Method methodToInvoke = null;
  String fromState = message.getFromState();
  String toState = message.getToState();
  methodToInvoke = _transitionMethodFinder.getMethodForTransition(_stateModel.getClass(),
      fromState, toState, new Class[] {
          Message.class, NotificationContext.class
      });
  if (methodToInvoke != null) {
    logger.info(String.format(
        "Instance %s, partition %s received state transition from %s to %s on session %s, message id: %s",
        message.getTgtName(), message.getPartitionName(), message.getFromState(),
        message.getToState(), message.getTgtSessionId(), message.getMsgId()));

    if (_cancelled) {
      throw new HelixRollbackException(String.format(
          "Instance %s, partition %s state transition from %s to %s on session %s has been cancelled, message id: %s",
          message.getTgtName(), message.getPartitionName(), message.getFromState(),
          message.getToState(), message.getTgtSessionId(), message.getMsgId()));
    }

    Object result = methodToInvoke.invoke(_stateModel, new Object[] {
        message, context
    });
    taskResult.setSuccess(true);
    String resultStr;
    if (result == null || result instanceof Void) {
      resultStr = "";
    } else {
      resultStr = result.toString();
    }
    taskResult.setInfo(resultStr);
  } else {
    String errorMessage = "Unable to find method for transition from " + fromState + " to "
        + toState + " in " + _stateModel.getClass();
    logger.error(errorMessage);
    taskResult.setSuccess(false);
    taskResult.setInfo(errorMessage);
    _statusUpdateUtil.logError(message, HelixStateTransitionHandler.class, errorMessage, manager);
  }
}
 
Example 9
Source File: InstanceMessagesCache.java    From helix with Apache License 2.0 4 votes vote down vote up
private void checkTargetHost(String targetHost, Message relayMessage, Map<String, LiveInstance> liveInstanceMap,
    Map<String, Map<String, Map<String, CurrentState>>> currentStateMap) {

  long currentTime = System.currentTimeMillis();
  String resourceName = relayMessage.getResourceName();
  String partitionName = relayMessage.getPartitionName();
  String sessionId = relayMessage.getTgtSessionId();

  if (!liveInstanceMap.containsKey(targetHost)) {
    LOG.info("Target host is not alive anymore, expiring relay message {} immediately.",
        relayMessage.getId());
    relayMessage.setExpired(true);
    return;
  }

  String instanceSessionId = liveInstanceMap.get(targetHost).getEphemeralOwner();

  // Target host's session has been changed, remove relay message
  if (!instanceSessionId.equals(sessionId)) {
    LOG.info("Instance SessionId does not match, expiring relay message {} immediately.",
        relayMessage.getId());
    relayMessage.setExpired(true);
    return;
  }

  Map<String, Map<String, CurrentState>> instanceCurrentStateMap =
      currentStateMap.get(targetHost);
  if (instanceCurrentStateMap == null || !instanceCurrentStateMap.containsKey(sessionId)) {
    // This should happen only when a new session is being established.
    // We should not do anything here, once new session is established in participant side,
    // the relay message will be deleted from cache in controller's next pipeline.
    LOG.warn("CurrentStateMap null for {}, session {}, pending relay message {}", targetHost,
        sessionId, relayMessage.getId());
    return;
  }

  Map<String, CurrentState> sessionCurrentStateMap = instanceCurrentStateMap.get(sessionId);
  CurrentState currentState = sessionCurrentStateMap.get(resourceName);
  // TODO: we should add transaction id for each state transition, we can immediately delete the relay message once we have transaction id record in each currentState.
  if (currentState == null) {
    setMessageRelayTime(relayMessage, currentTime);
    LOG.warn("CurrentState is null for {} on {}, set relay time {} for message {}", resourceName,
        targetHost, relayMessage.getRelayTime(), relayMessage.getId());
    return;
  }

  // if the target partition already completed the state transition,
  // or the current state on the target partition has been changed,
  // Do not remove the message immediately to avoid race-condition,
  // for example, controller may decide to move master to another instance at this time,
  // if it does not aware of a pending relay message, it may end up with two masters.
  // so we only set the relay message to be expired.
  // TODO: we should add transaction id for each state transition, we can immediately delete the relay message once we have transaction id record in each currentState.
  String partitionCurrentState = currentState.getState(partitionName);
  String targetState = relayMessage.getToState();
  String fromState = relayMessage.getFromState();
  if (targetState.equals(partitionCurrentState) || !fromState.equals(partitionCurrentState)) {
    setMessageRelayTime(relayMessage, currentTime);
    LOG.debug("{}'s currentState {} on {} has changed, set relay message {} to be expired.",
        partitionName, partitionCurrentState, targetHost, relayMessage.getId());
  }
}
 
Example 10
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 11
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 ++;
      }
    }
  }
}