Package org.apache.helix.model

Examples of org.apache.helix.model.Resource


    }
    MessageGenerationOutput output = new MessageGenerationOutput();

    for (String resourceName : resourceMap.keySet())
    {
      Resource resource = resourceMap.get(resourceName);
      int bucketSize = resource.getBucketSize();

      StateModelDefinition stateModelDef = cache.getStateModelDef(resource.getStateModelDefRef());

      for (Partition partition : resource.getPartitions())
      {
        Map<String, String> instanceStateMap = bestPossibleStateOutput.getInstanceStateMap(
            resourceName, partition);

        for (String instanceName : instanceStateMap.keySet())
        {
          String desiredState = instanceStateMap.get(instanceName);

          String currentState = currentStateOutput.getCurrentState(resourceName, partition,
              instanceName);
          if (currentState == null)
          {
            currentState = stateModelDef.getInitialState();
          }

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

          String pendingState = currentStateOutput.getPendingState(resourceName, partition,
              instanceName);

          String nextState = stateModelDef.getNextStateForTransition(currentState, desiredState);
          if (nextState == null)
          {
            logger.error("Unable to find a next state for partition: "
                + partition.getPartitionName() + " from stateModelDefinition"
                + stateModelDef.getClass() + " from:" + currentState + " to:" + desiredState);
            continue;
          }

          if (pendingState != null)
          {
            if (nextState.equalsIgnoreCase(pendingState))
            {
              logger.debug("Message already exists for " + instanceName + " to transit "
                  + partition.getPartitionName() + " from " + currentState + " to " + nextState);
            } else if (currentState.equalsIgnoreCase(pendingState))
            {
              logger.info("Message hasn't been removed for " + instanceName + " to transit"
                  + partition.getPartitionName() + " to " + pendingState + ", desiredState: "
                  + desiredState);
            } else
            {
              logger.info("IdealState changed before state transition completes for "
                  + partition.getPartitionName() + " on " + instanceName + ", pendingState: "
                  + pendingState + ", currentState: " + currentState + ", nextState: " + nextState);
            }
          } else
          {
            Message message = createMessage(manager, resourceName, partition.getPartitionName(),
                instanceName, currentState, nextState, sessionIdMap.get(instanceName),
                stateModelDef.getId(), resource.getStateModelFactoryname(), bucketSize);
            IdealState idealState = cache.getIdealState(resourceName);
            // Set timeout of needed
            String stateTransition = currentState + "-" + nextState + "_"
                + Message.Attributes.TIMEOUT;
            if (idealState != null
View Full Code Here


  }

  protected Map<String, Resource> getResourceMap()
  {
    Map<String, Resource> resourceMap = new HashMap<String, Resource>();
    Resource testResource = new Resource("testResourceName");
    testResource.setStateModelDefRef("MasterSlave");
    testResource.addPartition("testResourceName_0");
    testResource.addPartition("testResourceName_1");
    testResource.addPartition("testResourceName_2");
    testResource.addPartition("testResourceName_3");
    testResource.addPartition("testResourceName_4");
    resourceMap.put("testResourceName", testResource);

    return resourceMap;
  }
View Full Code Here

    }

    HelixDataAccessor dataAccessor = manager.getHelixDataAccessor();
    List<Message> messagesToSend = new ArrayList<Message>();
    for (String resourceName : resourceMap.keySet()) {
      Resource resource = resourceMap.get(resourceName);
      for (Partition partition : resource.getPartitions()) {
        List<Message> messages = messageOutput.getMessages(resourceName, partition);
        messagesToSend.addAll(messages);
      }
    }
View Full Code Here

    Iterator<Message> iter = messages.iterator();
    while (iter.hasNext()) {
      Message message = iter.next();
      String resourceName = message.getResourceName();
      Resource resource = resourceMap.get(resourceName);

      String instanceName = message.getTgtName();
      LiveInstance liveInstance = liveInstanceMap.get(instanceName);
      String participantVersion = null;
      if (liveInstance != null) {
        participantVersion = liveInstance.getHelixVersion();
      }

      if (resource == null || !resource.getBatchMessageMode() || participantVersion == null
          || !properties.isFeatureSupported("batch_message", participantVersion)) {
        outputMessages.add(message);
        continue;
      }
View Full Code Here

        onlineOffline));
  }

  protected Map<String, Resource> getResourceMap() {
    Map<String, Resource> resourceMap = new HashMap<String, Resource>();
    Resource testResource = new Resource("testResourceName");
    testResource.setStateModelDefRef("MasterSlave");
    testResource.addPartition("testResourceName_0");
    testResource.addPartition("testResourceName_1");
    testResource.addPartition("testResourceName_2");
    testResource.addPartition("testResourceName_3");
    testResource.addPartition("testResourceName_4");
    resourceMap.put("testResourceName", testResource);

    return resourceMap;
  }
View Full Code Here

      sessionIdMap.put(liveInstance.getInstanceName(), liveInstance.getSessionId());
    }
    MessageGenerationOutput output = new MessageGenerationOutput();

    for (String resourceName : resourceMap.keySet()) {
      Resource resource = resourceMap.get(resourceName);
      int bucketSize = resource.getBucketSize();

      StateModelDefinition stateModelDef = cache.getStateModelDef(resource.getStateModelDefRef());

      for (Partition partition : resource.getPartitions()) {
        Map<String, String> instanceStateMap =
            bestPossibleStateOutput.getInstanceStateMap(resourceName, partition);

        // 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<String, List<Message>> messageMap = new HashMap<String, List<Message>>();

        for (String instanceName : instanceStateMap.keySet()) {
          String desiredState = instanceStateMap.get(instanceName);

          String currentState =
              currentStateOutput.getCurrentState(resourceName, partition, instanceName);
          if (currentState == null) {
            currentState = stateModelDef.getInitialState();
          }

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

          String pendingState =
              currentStateOutput.getPendingState(resourceName, partition, instanceName);

          String nextState = stateModelDef.getNextStateForTransition(currentState, desiredState);
          if (nextState == null) {
            logger.error("Unable to find a next state for partition: "
                + partition.getPartitionName() + " from stateModelDefinition"
                + stateModelDef.getClass() + " from:" + currentState + " to:" + desiredState);
            continue;
          }

          if (pendingState != null) {
            if (nextState.equalsIgnoreCase(pendingState)) {
              logger.debug("Message already exists for " + instanceName + " to transit "
                  + partition.getPartitionName() + " from " + currentState + " to " + nextState);
            } else if (currentState.equalsIgnoreCase(pendingState)) {
              logger.info("Message hasn't been removed for " + instanceName + " to transit"
                  + partition.getPartitionName() + " to " + pendingState + ", desiredState: "
                  + desiredState);
            } else {
              logger.info("IdealState changed before state transition completes for "
                  + partition.getPartitionName() + " on " + instanceName + ", pendingState: "
                  + pendingState + ", currentState: " + currentState + ", nextState: " + nextState);
            }
          } else {
            Message message =
                createMessage(manager, resourceName, partition.getPartitionName(), instanceName,
                    currentState, nextState, sessionIdMap.get(instanceName), stateModelDef.getId(),
                    resource.getStateModelFactoryname(), bucketSize);
            IdealState idealState = cache.getIdealState(resourceName);
            if (idealState != null
                && idealState.getStateModelDefRef().equalsIgnoreCase(
                    DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE)) {
              if (idealState.getRecord().getMapField(partition.getPartitionName()) != null) {
View Full Code Here

    BestPossibleStateOutput output = new BestPossibleStateOutput();

    for (String resourceName : resourceMap.keySet()) {
      logger.debug("Processing resource:" + resourceName);

      Resource resource = resourceMap.get(resourceName);
      // Ideal state may be gone. In that case we need to get the state model name
      // from the current state
      IdealState idealState = cache.getIdealState(resourceName);

      if (idealState == null) {
        // if ideal state is deleted, use an empty one
        logger.info("resource:" + resourceName + " does not exist anymore");
        idealState = new IdealState(resourceName);
      }

      Rebalancer rebalancer = null;
      MappingCalculator mappingCalculator = null;
      if (idealState.getRebalanceMode() == RebalanceMode.USER_DEFINED
          && idealState.getRebalancerClassName() != null) {
        String rebalancerClassName = idealState.getRebalancerClassName();
        logger
            .info("resource " + resourceName + " use idealStateRebalancer " + rebalancerClassName);
        try {
          rebalancer =
              (Rebalancer) (HelixUtil.loadClass(getClass(), rebalancerClassName).newInstance());
          mappingCalculator = new SemiAutoRebalancer();
        } catch (Exception e) {
          logger.warn("Exception while invoking custom rebalancer class:" + rebalancerClassName, e);
        }
      }
      if (rebalancer == null) {
        if (idealState.getRebalanceMode() == RebalanceMode.FULL_AUTO) {
          rebalancer = new AutoRebalancer();
          mappingCalculator = new AutoRebalancer();
        } else if (idealState.getRebalanceMode() == RebalanceMode.SEMI_AUTO) {
          rebalancer = new SemiAutoRebalancer();
          mappingCalculator = new SemiAutoRebalancer();
        } else {
          rebalancer = new CustomRebalancer();
          mappingCalculator = new CustomRebalancer();
        }
        idealState =
            rebalancer.computeNewIdealState(resourceName, idealState, currentStateOutput, cache);
      }

      // Use the internal MappingCalculator interface to compute the final assignment
      // The next release will support rebalancers that compute the mapping from start to finish
      if (mappingCalculator != null) {
        ResourceAssignment partitionStateAssignment =
            mappingCalculator.computeBestPossiblePartitionState(cache, idealState, resource,
                currentStateOutput);
        for (Partition partition : resource.getPartitions()) {
          Map<String, String> newStateMap = partitionStateAssignment.getInstanceStateMap(partition);
          output.setState(resourceName, partition, newStateMap);
        }
      }
    }
View Full Code Here

        Set<String> partitionSet = idealState.getPartitionSet();
        String resourceName = idealState.getResourceName();

        for (String partition : partitionSet) {
          addPartition(partition, resourceName, resourceMap);
          Resource resource = resourceMap.get(resourceName);
          resource.setStateModelDefRef(idealState.getStateModelDefRef());
          resource.setStateModelFactoryName(idealState.getStateModelFactoryName());
          resource.setBucketSize(idealState.getBucketSize());
          resource.setBatchMessageMode(idealState.getBatchMessageMode());
        }
      }
    }

    // It's important to get partitions from CurrentState as well since the
    // idealState might be removed.
    Map<String, LiveInstance> availableInstances = cache.getLiveInstances();

    if (availableInstances != null && availableInstances.size() > 0) {
      for (LiveInstance instance : availableInstances.values()) {
        String instanceName = instance.getInstanceName();
        String clientSessionId = instance.getSessionId();

        Map<String, CurrentState> currentStateMap =
            cache.getCurrentState(instanceName, clientSessionId);
        if (currentStateMap == null || currentStateMap.size() == 0) {
          continue;
        }
        for (CurrentState currentState : currentStateMap.values()) {

          String resourceName = currentState.getResourceName();
          Map<String, String> resourceStateMap = currentState.getPartitionStateMap();

          // don't overwrite ideal state settings
          if (!resourceMap.containsKey(resourceName)) {
            addResource(resourceName, resourceMap);
            Resource resource = resourceMap.get(resourceName);
            resource.setStateModelDefRef(currentState.getStateModelDefRef());
            resource.setStateModelFactoryName(currentState.getStateModelFactoryName());
            resource.setBucketSize(currentState.getBucketSize());
            resource.setBatchMessageMode(currentState.getBatchMessageMode());
          }

          if (currentState.getStateModelDefRef() == null) {
            LOG.error("state model def is null." + "resource:" + currentState.getResourceName()
                + ", partitions: " + currentState.getPartitionStateMap().keySet() + ", states: "
View Full Code Here

  private void addResource(String resource, Map<String, Resource> resourceMap) {
    if (resource == null || resourceMap == null) {
      return;
    }
    if (!resourceMap.containsKey(resource)) {
      resourceMap.put(resource, new Resource(resource));
    }
  }
View Full Code Here

  private void addPartition(String partition, String resourceName, Map<String, Resource> resourceMap) {
    if (resourceName == null || partition == null || resourceMap == null) {
      return;
    }
    if (!resourceMap.containsKey(resourceName)) {
      resourceMap.put(resourceName, new Resource(resourceName));
    }
    Resource resource = resourceMap.get(resourceName);
    resource.addPartition(partition);

  }
View Full Code Here

TOP

Related Classes of org.apache.helix.model.Resource

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.