Package org.apache.helix.api

Examples of org.apache.helix.api.State


      Set<Integer> donePartitions = new TreeSet<Integer>();
      for (int pId : pSet) {
        final String pName = pName(jobResource, pId);

        // Check for pending state transitions on this (partition, instance).
        State pendingState =
            currStateOutput.getPendingState(ResourceId.from(jobResource), PartitionId.from(pName),
                instance);
        if (pendingState != null) {
          // There is a pending state transition for this (partition, instance). Just copy forward
          // the state assignment from the previous ideal state.
          Map<ParticipantId, State> stateMap =
              prevAssignment.getReplicaMap(PartitionId.from(pName));
          if (stateMap != null) {
            State prevState = stateMap.get(instance);
            paMap.put(pId, new PartitionAssignment(instance.toString(), prevState.toString()));
            assignedPartitions.add(pId);
            LOG.debug(String
                .format(
                    "Task partition %s has a pending state transition on instance %s. Using the previous ideal state which was %s.",
                    pName, instance, prevState));
          }

          continue;
        }

        State currHelixState =
            currStateOutput.getCurrentState(ResourceId.from(jobResource), PartitionId.from(pName),
                instance);
        TaskPartitionState currState =
            (currHelixState != null) ? TaskPartitionState.valueOf(currHelixState.toString()) : null;
        if (currState != null) {
          jobCtx.setPartitionState(pId, currState);
        }

        // Process any requested state transitions.
        State requestedStateStr =
            currStateOutput.getRequestedState(ResourceId.from(jobResource),
                PartitionId.from(pName), instance);
        if (requestedStateStr != null && !requestedStateStr.toString().isEmpty()) {
          TaskPartitionState requestedState =
              TaskPartitionState.valueOf(requestedStateStr.toString());
          if (requestedState.equals(currState)) {
            LOG.warn(String.format(
                "Requested state %s is the same as the current state for instance %s.",
                requestedState, instance));
          }
View Full Code Here


    List<Message> selectedMessages = new ArrayList<Message>();
    Map<State, Bounds> bounds = new HashMap<State, Bounds>();

    // count currentState, if no currentState, count as in initialState
    for (ParticipantId liveParticipantId : liveParticipants.keySet()) {
      State state = initialState;
      if (currentStates.containsKey(liveParticipantId)) {
        state = currentStates.get(liveParticipantId);
      }

      if (!bounds.containsKey(state)) {
        bounds.put(state, new Bounds(0, 0));
      }
      bounds.get(state).increaseLowerBound();
      bounds.get(state).increaseUpperBound();
    }

    // count pendingStates
    for (ParticipantId participantId : pendingStates.keySet()) {
      State state = pendingStates.get(participantId);
      if (!bounds.containsKey(state)) {
        bounds.put(state, new Bounds(0, 0));
      }
      // TODO: add lower bound, need to refactor pendingState to include fromState also
      bounds.get(state).increaseUpperBound();
    }

    // group messages based on state transition priority
    Map<Integer, List<Message>> messagesGroupByStateTransitPriority =
        new TreeMap<Integer, List<Message>>();
    for (Message message : messages) {
      State fromState = message.getTypedFromState();
      State toState = message.getTypedToState();
      String transition = fromState.toString() + "-" + toState.toString();
      int priority = Integer.MAX_VALUE;

      if (stateTransitionPriorities.containsKey(transition)) {
        priority = stateTransitionPriorities.get(transition);
      }

      if (!messagesGroupByStateTransitPriority.containsKey(priority)) {
        messagesGroupByStateTransitPriority.put(priority, new ArrayList<Message>());
      }
      messagesGroupByStateTransitPriority.get(priority).add(message);
    }

    // select messages
    for (List<Message> messageList : messagesGroupByStateTransitPriority.values()) {
      for (Message message : messageList) {
        State fromState = message.getTypedFromState();
        State toState = message.getTypedToState();

        if (!bounds.containsKey(fromState)) {
          LOG.error("Message's fromState is not in currentState. message: " + message);
          continue;
        }
View Full Code Here

    @Override
    public void doTransition(Message message, NotificationContext context) {
      // System.err.println("doReset() invoked");
      super.doTransition(message, context);
      State fromState = message.getTypedFromState();
      State toState = message.getTypedToState();
      if (fromState.toString().equals("ERROR") && toState.toString().equals("OFFLINE")) {
        _errToOfflineInvoked++;
      }
    }
View Full Code Here

        continue;
      }
      int pId = partitions.get(0);
      if (includeSet.contains(pId)) {
        for (ParticipantId instance : instances) {
          State pending =
              currStateOutput.getPendingState(ResourceId.from(tgtIs.getResourceName()),
                  PartitionId.from(pName), instance);
          if (pending != null) {
            continue;
          }
          State s =
              currStateOutput.getCurrentState(ResourceId.from(tgtIs.getResourceName()),
                  PartitionId.from(pName), instance);
          String state = (s == null ? null : s.toString());
          if (tgtStates == null || tgtStates.contains(state)) {
            result.get(instance).add(pId);
          }
        }
      }
View Full Code Here

            .userConfig(userConfig);
    return resourceBuilder.build();
  }

  private static StateModelDefinition getLockUnlockModel() {
    final State LOCKED = State.from("LOCKED");
    final State RELEASED = State.from("RELEASED");
    final State DROPPED = State.from("DROPPED");
    StateModelDefId stateModelId = StateModelDefId.from("LockUnlock");
    StateModelDefinition.Builder stateModelBuilder =
        new StateModelDefinition.Builder(stateModelId).addState(LOCKED, 0).addState(RELEASED, 1)
            .addState(DROPPED, 2).addTransition(RELEASED, LOCKED, 0)
            .addTransition(LOCKED, RELEASED, 1).addTransition(RELEASED, DROPPED, 2)
View Full Code Here

    String timeout = message.getRecord().getSimpleField(CommandAttribute.TIMEOUT.getName());
    String pidFile = message.getRecord().getSimpleField(CommandAttribute.PID_FILE.getName());

    HelixManager manager = context.getManager();
    String clusterName = manager.getClusterName();
    State fromState = message.getTypedFromState();
    State toState = message.getTypedToState();

    // construct keys for command-config
    String cmdKey = buildKey(fromState.toString(), toState.toString(), CommandAttribute.COMMAND);
    String workingDirKey =
        buildKey(fromState.toString(), toState.toString(), CommandAttribute.WORKING_DIR);
    String timeoutKey =
        buildKey(fromState.toString(), toState.toString(), CommandAttribute.TIMEOUT);
    String pidFileKey =
        buildKey(fromState.toString(), toState.toString(), CommandAttribute.PID_FILE);
    List<String> cmdConfigKeys = Arrays.asList(cmdKey, workingDirKey, timeoutKey, pidFileKey);

    // read command from resource-scope configures
    if (cmd == null) {
      HelixConfigScope resourceScope =
View Full Code Here

    }

    @Override
    public void doTransition(Message message, NotificationContext context) {
      super.doTransition(message, context);
      State fromState = message.getTypedFromState();
      State toState = message.getTypedToState();
      if (fromState.toString().equals("ERROR") && toState.toString().equals("OFFLINE")) {
        // System.err.println("doReset() invoked");
        _errToOfflineInvoked.incrementAndGet();
      }
    }
View Full Code Here

    accessor.setProperty(keyBuilder.message("localhost_" + 3, message.getId()), message);

    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    ResourceCurrentState output2 = event.getAttribute(AttributeName.CURRENT_STATE.toString());
    State pendingState =
        output2.getPendingState(ResourceId.from("testResourceName"),
            PartitionId.from("testResourceName_1"), ParticipantId.from("localhost_3"));
    AssertJUnit.assertEquals(pendingState, State.from("SLAVE"));

    ZNRecord record1 = new ZNRecord("testResourceName");
    // Add a current state that matches sessionId and one that does not match
    CurrentState stateWithLiveSession = new CurrentState(record1);
    stateWithLiveSession.setSessionId(SessionId.from("session_3"));
    stateWithLiveSession.setStateModelDefRef("MasterSlave");
    stateWithLiveSession.setState(PartitionId.from("testResourceName_1"), State.from("OFFLINE"));
    ZNRecord record2 = new ZNRecord("testResourceName");
    CurrentState stateWithDeadSession = new CurrentState(record2);
    stateWithDeadSession.setSessionId(SessionId.from("session_dead"));
    stateWithDeadSession.setStateModelDefRef("MasterSlave");
    stateWithDeadSession.setState(PartitionId.from("testResourceName_1"), State.from("MASTER"));

    accessor.setProperty(keyBuilder.currentState("localhost_3", "session_3", "testResourceName"),
        stateWithLiveSession);
    accessor.setProperty(
        keyBuilder.currentState("localhost_3", "session_dead", "testResourceName"),
        stateWithDeadSession);
    runStage(event, new ReadClusterDataStage());
    runStage(event, stage);
    ResourceCurrentState output3 = event.getAttribute(AttributeName.CURRENT_STATE.toString());
    State currentState =
        output3.getCurrentState(ResourceId.from("testResourceName"),
            PartitionId.from("testResourceName_1"), ParticipantId.from("localhost_3"));
    AssertJUnit.assertEquals(currentState, State.from("OFFLINE"));

  }
View Full Code Here

      return;
    }

    if (taskResult.isSuccess()) {
      // String fromState = message.getFromState();
      State toState = _message.getTypedToState();
      _currentStateDelta.setState(partitionId, toState);

      if (toState.toString().equalsIgnoreCase(HelixDefinedState.DROPPED.toString())) {
        // for "OnOfflineToDROPPED" message, we need to remove the resource key record
        // from the current state of the instance because the resource key is dropped.
        // In the state model it will be stayed as "OFFLINE", which is OK.
        ZNRecordDelta delta =
            new ZNRecordDelta(_currentStateDelta.getRecord(), MergeOperation.SUBTRACT);
        // Don't subtract simple fields since they contain stateModelDefRef
        delta._record.getSimpleFields().clear();

        List<ZNRecordDelta> deltaList = new ArrayList<ZNRecordDelta>();
        deltaList.add(delta);
        _currentStateDelta.setDeltaList(deltaList);
        _stateModelFactory.removeStateModel(partitionId.stringify());
      } else {
        // if the partition is not to be dropped, update _stateModel to the TO_STATE
        _stateModel.updateState(toState.toString());
      }
    } else {
      if (exception instanceof HelixStateMismatchException) {
        // if fromState mismatch, set current state on zk to stateModel's current state
        logger.warn("Force CurrentState on Zk to be stateModel's CurrentState. partitionKey: "
View Full Code Here

    _statusUpdateUtil.logInfo(message, HelixStateTransitionHandler.class,
        "Message handling invoking", accessor);

    // by default, we invoke state transition function in state model
    Method methodToInvoke = null;
    State fromState = message.getTypedFromState();
    State toState = message.getTypedToState();
    methodToInvoke =
        _transitionMethodFinder.getMethodForTransition(_stateModel.getClass(),
            fromState.toString(), toState.toString(), new Class[] {
                Message.class, NotificationContext.class
            });
    if (methodToInvoke != null) {
      methodToInvoke.invoke(_stateModel, new Object[] {
          message, context
View Full Code Here

TOP

Related Classes of org.apache.helix.api.State

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.