Package org.apache.helix.api

Examples of org.apache.helix.api.Cluster


    _cache = cache;

    ClusterId clusterId = ClusterId.from(manager.getClusterName());
    ClusterAccessor clusterAccessor = new ClusterAccessor(clusterId, accessor, _cache);

    Cluster cluster = clusterAccessor.readCluster();

    // Update the cluster status gauges
    ClusterStatusMonitor clusterStatusMonitor =
        (ClusterStatusMonitor) event.getAttribute("clusterStatusMonitor");
    if (clusterStatusMonitor != null) {
      Set<String> instanceSet = Sets.newHashSet();
      Set<String> liveInstanceSet = Sets.newHashSet();
      Set<String> disabledInstanceSet = Sets.newHashSet();
      Map<String, Set<String>> disabledPartitions = Maps.newHashMap();
      Map<String, Set<String>> tags = Maps.newHashMap();
      for (Participant participant : cluster.getParticipantMap().values()) {
        instanceSet.add(participant.getId().toString());
        if (participant.isAlive()) {
          liveInstanceSet.add(participant.getId().toString());
        }
        if (!participant.isEnabled()) {
          disabledInstanceSet.add(participant.getId().toString());
        }
        Set<String> partitionNames = Sets.newHashSet();
        for (PartitionId partitionId : participant.getDisabledPartitionIds()) {
          partitionNames.add(partitionId.toString());
        }
        disabledPartitions.put(participant.getId().toString(), partitionNames);
        tags.put(participant.getId().toString(), participant.getTags());
      }
      clusterStatusMonitor.setClusterInstanceStatus(liveInstanceSet, instanceSet,
          disabledInstanceSet, disabledPartitions, tags);
    }

    event.addAttribute("Cluster", cluster);
    event.addAttribute("ClusterDataCache", _cache);

    // read contexts (if any)
    Map<ContextId, ControllerContext> persistedContexts = null;
    if (cluster != null) {
      persistedContexts = cluster.getContextMap();
    } else {
      persistedContexts = Maps.newHashMap();
    }
    ControllerContextProvider contextProvider = new ControllerContextProvider(persistedContexts);
    event.addAttribute(AttributeName.CONTEXT_PROVIDER.toString(), contextProvider);

    // read ideal state rules (if any)
    ClusterConfiguration clusterConfiguration =
        accessor.getProperty(accessor.keyBuilder().clusterConfig());
    if (clusterConfiguration == null) {
      clusterConfiguration = new ClusterConfiguration(cluster.getId());
    }
    event.addAttribute(AttributeName.IDEAL_STATE_RULES.toString(),
        clusterConfiguration.getIdealStateRules());

    long endTime = System.currentTimeMillis();
View Full Code Here


* previous State [ResourceComputationStage]
*/
public class CurrentStateComputationStage extends AbstractBaseStage {
  @Override
  public void process(ClusterEvent event) throws Exception {
    Cluster cluster = event.getAttribute("Cluster");
    Map<ResourceId, ResourceConfig> resourceMap =
        event.getAttribute(AttributeName.RESOURCES.toString());

    if (cluster == null || resourceMap == null) {
      throw new StageException("Missing attributes in event:" + event
          + ". Requires Cluster|RESOURCE");
    }

    ResourceCurrentState currentStateOutput = new ResourceCurrentState();

    for (Participant liveParticipant : cluster.getLiveParticipantMap().values()) {
      ParticipantId participantId = liveParticipant.getId();

      // add pending messages
      Map<MessageId, Message> instanceMsgs = liveParticipant.getMessageMap();
      for (Message message : instanceMsgs.values()) {
View Full Code Here

    return new HashSet<ConstraintItem>(selectedItems.values());
  }

  @Override
  public void process(ClusterEvent event) throws Exception {
    Cluster cluster = event.getAttribute("Cluster");
    MessageOutput msgSelectionOutput =
        event.getAttribute(AttributeName.MESSAGES_SELECTED.toString());
    Map<ResourceId, ResourceConfig> resourceMap =
        event.getAttribute(AttributeName.RESOURCES.toString());
    BestPossibleStateOutput bestPossibleStateOutput =
        event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.toString());

    if (cluster == null || resourceMap == null || msgSelectionOutput == null
        || bestPossibleStateOutput == null) {
      throw new StageException("Missing attributes in event: " + event
          + ". Requires Cluster|RESOURCES|BEST_POSSIBLE_STATE|MESSAGES_SELECTED");
    }

    MessageOutput output = new MessageOutput();

    // TODO fix it
    ClusterConstraints constraint = cluster.getConstraint(ConstraintType.MESSAGE_CONSTRAINT);
    Map<String, Integer> throttleCounterMap = new HashMap<String, Integer>();

    if (constraint != null) {
      // go through all pending messages, they should be counted but not throttled
      for (ParticipantId participantId : cluster.getLiveParticipantMap().keySet()) {
        Participant liveParticipant = cluster.getLiveParticipantMap().get(participantId);
        throttle(throttleCounterMap, constraint, new ArrayList<Message>(liveParticipant
            .getMessageMap().values()), false);
      }
    }
View Full Code Here

  private static Logger LOG = Logger.getLogger(MessageGenerationStage.class);

  @Override
  public void process(ClusterEvent event) throws Exception {
    HelixManager manager = event.getAttribute("helixmanager");
    Cluster cluster = event.getAttribute("Cluster");
    Map<StateModelDefId, StateModelDefinition> stateModelDefMap = cluster.getStateModelMap();
    Map<ResourceId, ResourceConfig> resourceMap =
        event.getAttribute(AttributeName.RESOURCES.toString());
    ResourceCurrentState currentStateOutput =
        event.getAttribute(AttributeName.CURRENT_STATE.toString());
    BestPossibleStateOutput bestPossibleStateOutput =
        event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.toString());
    if (manager == null || cluster == null || resourceMap == null || currentStateOutput == null
        || bestPossibleStateOutput == null) {
      throw new StageException("Missing attributes in event:" + event
          + ". Requires HelixManager|Cluster|RESOURCES|CURRENT_STATE|BEST_POSSIBLE_STATE");
    }

    MessageOutput output = new MessageOutput();

    for (ResourceId resourceId : resourceMap.keySet()) {
      ResourceConfig resourceConfig = resourceMap.get(resourceId);
      int bucketSize = 0;
      bucketSize = resourceConfig.getIdealState().getBucketSize();

      IdealState idealState = resourceConfig.getIdealState();
      StateModelDefinition stateModelDef = stateModelDefMap.get(idealState.getStateModelDefId());

      ResourceAssignment resourceAssignment =
          bestPossibleStateOutput.getResourceAssignment(resourceId);
      for (PartitionId subUnitId : resourceAssignment.getMappedPartitionIds()) {
        Map<ParticipantId, State> instanceStateMap = resourceAssignment.getReplicaMap(subUnitId);

        // we should generate message based on the desired-state priority
        // so keep generated messages in a temp map keyed by state
        // desired-state->list of generated-messages
        Map<State, List<Message>> messageMap = new HashMap<State, List<Message>>();

        for (ParticipantId participantId : instanceStateMap.keySet()) {
          State desiredState = instanceStateMap.get(participantId);

          State currentState =
              currentStateOutput.getCurrentState(resourceId, subUnitId, participantId);
          if (currentState == null) {
            currentState = stateModelDef.getTypedInitialState();
          }

          if (desiredState.equals(currentState)) {
            continue;
          }

          State pendingState =
              currentStateOutput.getPendingState(resourceId, subUnitId, participantId);

          // TODO fix it
          State nextState = stateModelDef.getNextStateForTransition(currentState, desiredState);
          if (nextState == null) {
            LOG.error("Unable to find a next state for partition: " + subUnitId
                + " from stateModelDefinition" + stateModelDef.getClass() + " from:" + currentState
                + " to:" + desiredState);
            continue;
          }

          if (pendingState != null) {
            if (nextState.equals(pendingState)) {
              LOG.debug("Message already exists for " + participantId + " to transit " + subUnitId
                  + " from " + currentState + " to " + nextState);
            } else if (currentState.equals(pendingState)) {
              LOG.info("Message hasn't been removed for " + participantId + " to transit"
                  + subUnitId + " to " + pendingState + ", desiredState: " + desiredState);
            } else {
              LOG.info("IdealState changed before state transition completes for " + subUnitId
                  + " on " + participantId + ", pendingState: " + pendingState + ", currentState: "
                  + currentState + ", nextState: " + nextState);
            }
          } else {
            // TODO check if instance is alive
            SessionId sessionId =
                SessionId.from(cluster.getLiveParticipantMap().get(participantId).getLiveInstance()
                    .getSessionId());
            Message message =
                createMessage(manager, resourceId, subUnitId, participantId, currentState,
                    nextState, sessionId, StateModelDefId.from(stateModelDef.getId()),
                    idealState.getStateModelFactoryId(), bucketSize);
View Full Code Here

    LOG.info("START ExternalViewComputeStage.process()");

    HelixManager manager = event.getAttribute("helixmanager");
    Map<ResourceId, ResourceConfig> resourceMap =
        event.getAttribute(AttributeName.RESOURCES.toString());
    Cluster cluster = event.getAttribute("Cluster");
    ClusterDataCache cache = event.getAttribute("ClusterDataCache");

    if (manager == null || resourceMap == null || cluster == null || cache == null) {
      throw new StageException("Missing attributes in event:" + event
          + ". Requires ClusterManager|RESOURCES|Cluster|ClusterDataCache");
View Full Code Here

  private static final Logger LOG = Logger.getLogger(CompatibilityCheckStage.class.getName());

  @Override
  public void process(ClusterEvent event) throws Exception {
    HelixManager manager = event.getAttribute("helixmanager");
    Cluster cluster = event.getAttribute("Cluster");
    if (manager == null || cluster == null) {
      throw new StageException("Missing attributes in event:" + event
          + ". Requires HelixManager | Cluster");
    }

    HelixManagerProperties properties = manager.getProperties();
    // Map<String, LiveInstance> liveInstanceMap = cache.getLiveInstances();
    Map<ParticipantId, Participant> liveParticipants = cluster.getLiveParticipantMap();
    for (Participant liveParticipant : liveParticipants.values()) {
      String participantVersion = liveParticipant.getLiveInstance().getHelixVersion();
      if (!properties.isParticipantCompatible(participantVersion)) {
        String errorMsg =
            "incompatible participant. pipeline will not continue. " + "controller: "
View Full Code Here

    }
  }

  @Override
  public void process(ClusterEvent event) throws Exception {
    Cluster cluster = event.getAttribute("Cluster");
    Map<StateModelDefId, StateModelDefinition> stateModelDefMap = cluster.getStateModelMap();
    Map<ResourceId, ResourceConfig> resourceMap =
        event.getAttribute(AttributeName.RESOURCES.toString());
    ResourceCurrentState currentStateOutput =
        event.getAttribute(AttributeName.CURRENT_STATE.toString());
    BestPossibleStateOutput bestPossibleStateOutput =
        event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.toString());
    MessageOutput messageGenOutput = event.getAttribute(AttributeName.MESSAGES_ALL.toString());
    if (cluster == null || resourceMap == null || currentStateOutput == null
        || messageGenOutput == null || bestPossibleStateOutput == null) {
      throw new StageException("Missing attributes in event:" + event
          + ". Requires Cluster|RESOURCES|CURRENT_STATE|BEST_POSSIBLE_STATE|MESSAGES_ALL");
    }

    MessageOutput output = new MessageOutput();

    for (ResourceId resourceId : resourceMap.keySet()) {
      ResourceConfig resource = resourceMap.get(resourceId);
      StateModelDefinition stateModelDef =
          stateModelDefMap.get(resource.getIdealState().getStateModelDefId());

      if (stateModelDef == null) {
        LOG.info("resource: "
            + resourceId
            + " doesn't have state-model-def; e.g. we add a resource config but not add the resource in ideal-states");
        continue;
      }

      // TODO have a logical model for transition
      Map<String, Integer> stateTransitionPriorities = getStateTransitionPriorityMap(stateModelDef);
      Resource configResource = cluster.getResource(resourceId);

      // if configResource == null, the resource has been dropped
      Map<State, Bounds> stateConstraints =
          computeStateConstraints(stateModelDef,
              configResource == null ? null : configResource.getIdealState(), cluster);

      // TODO fix it
      for (PartitionId partitionId : bestPossibleStateOutput.getResourceAssignment(resourceId)
          .getMappedPartitionIds()) {
        List<Message> messages = messageGenOutput.getMessages(resourceId, partitionId);
        List<Message> selectedMessages =
            selectMessages(cluster.getLiveParticipantMap(),
                currentStateOutput.getCurrentStateMap(resourceId, partitionId),
                currentStateOutput.getPendingStateMap(resourceId, partitionId), messages,
                stateConstraints, stateTransitionPriorities, stateModelDef.getTypedInitialState());
        output.setMessages(resourceId, partitionId, selectedMessages);
      }
View Full Code Here

          idealStates.put(resource, new IdealState(resource));
        }
      }

      ClusterAccessor clusterAccessor = new ClusterAccessor(ClusterId.from(clusterName), accessor);
      Cluster cluster = clusterAccessor.readCluster();
      // calculate best possible state
      BestPossibleStateOutput bestPossOutput = ClusterStateVerifier.calcBestPossState(cluster);

      // set error states
      if (errStates != null) {
View Full Code Here

    // create a cluster
    boolean created = createCluster(clusterId, accessor, MODIFIER, 1);
    Assert.assertTrue(created);

    // read the cluster
    Cluster clusterSnapshot = accessor.readCluster();
    Assert.assertEquals(clusterSnapshot.getUserConfig().getIntField(MODIFIER, -1), 1);

    // create a cluster with the same id
    boolean created2 = createCluster(clusterId, accessor, MODIFIER, 2);
    Assert.assertFalse(created2); // should fail since cluster exists

    // remove a required property
    helixAccessor.removeProperty(helixAccessor.keyBuilder().liveInstances());

    // try again, should work this time
    created2 = createCluster(clusterId, accessor, MODIFIER, 2);
    Assert.assertTrue(created2);

    // read the cluster again
    clusterSnapshot = accessor.readCluster();
    Assert.assertEquals(clusterSnapshot.getUserConfig().getIntField(MODIFIER, -1), 2);

    accessor.dropCluster();
  }
View Full Code Here

   * Update the cluster configuration
   * @param clusterDelta change to the cluster configuration
   * @return updated ClusterConfig, or null if there was an error
   */
  public ClusterConfig updateCluster(ClusterConfig.Delta clusterDelta) {
    Cluster cluster = readCluster();
    if (cluster == null) {
      LOG.error("Cluster does not exist, cannot be updated");
      return null;
    }
    ClusterConfig config = clusterDelta.mergeInto(cluster.getConfig());
    boolean status = setBasicClusterConfig(config);
    return status ? config : null;
  }
View Full Code Here

TOP

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

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.