Java Code Examples for org.apache.helix.model.IdealState#getMinActiveReplicas()

The following examples show how to use org.apache.helix.model.IdealState#getMinActiveReplicas() . 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: IntermediateStateCalcStage.java    From helix with Apache License 2.0 4 votes vote down vote up
/**
 * For a partition, given its preferenceList, bestPossibleState, and currentState, determine which
 * type of rebalance is needed to model IdealState's states defined by the state model definition.
 * @return RebalanceType needed to bring the replicas to idea states
 *         RECOVERY_BALANCE - not all required states (replicas) are available through all
 *         replicas, or the partition is disabled
 *         NONE - current state matches the ideal state
 *         LOAD_BALANCE - although all replicas required exist, Helix needs to optimize the
 *         allocation
 */
private RebalanceType getRebalanceType(ResourceControllerDataProvider cache,
    Map<String, String> bestPossibleMap, List<String> preferenceList,
    StateModelDefinition stateModelDef, Map<String, String> currentStateMap,
    IdealState idealState, String partitionName) {
  if (preferenceList == null) {
    preferenceList = Collections.emptyList();
  }

  // If there is a minimum active replica number specified in IS, we should respect it.
  // TODO: We should implement the per replica level throttling with generated message
  // Issue: https://github.com/apache/helix/issues/343
  int replica = idealState.getMinActiveReplicas() == -1
      ? idealState.getReplicaCount(preferenceList.size())
      : idealState.getMinActiveReplicas();
  Set<String> activeList = new HashSet<>(preferenceList);
  activeList.retainAll(cache.getEnabledLiveInstances());

  // For each state, check that this partition currently has the required number of that state as
  // required by StateModelDefinition.
  LinkedHashMap<String, Integer> expectedStateCountMap =
      stateModelDef.getStateCountMap(activeList.size(), replica); // StateModelDefinition's counts
  // Current counts without disabled partitions or disabled instances
  Map<String, String> currentStateMapWithoutDisabled = new HashMap<>(currentStateMap);
  currentStateMapWithoutDisabled.keySet().removeAll(
      cache.getDisabledInstancesForPartition(idealState.getResourceName(), partitionName));
  Map<String, Integer> currentStateCounts =
      StateModelDefinition.getStateCounts(currentStateMapWithoutDisabled);

  // Go through each state and compare counts
  for (String state : expectedStateCountMap.keySet()) {
    Integer expectedCount = expectedStateCountMap.get(state);
    Integer currentCount = currentStateCounts.get(state);
    expectedCount = expectedCount == null ? 0 : expectedCount;
    currentCount = currentCount == null ? 0 : currentCount;

    // If counts do not match up, this partition requires recovery
    if (currentCount < expectedCount) {
      // Recovery is not needed in cases where this partition just started, was dropped, or is in
      // error
      if (!state.equals(HelixDefinedState.DROPPED.name())
          && !state.equals(HelixDefinedState.ERROR.name())
          && !state.equals(stateModelDef.getInitialState())) {
        return RebalanceType.RECOVERY_BALANCE;
      }
    }
  }
  // No recovery needed, all expected replicas exist
  // Check if this partition is actually in the BestPossibleState
  if (currentStateMap.equals(bestPossibleMap)) {
    return RebalanceType.NONE; // No further action required
  } else {
    return RebalanceType.LOAD_BALANCE; // Required state counts are satisfied, but in order to
    // achieve BestPossibleState, load balance may be required
    // to shift replicas around
  }
}
 
Example 2
Source File: ResourceAccessor.java    From helix with Apache License 2.0 4 votes vote down vote up
private Map<String, String> computePartitionHealth(String clusterId, String resourceName) {
  HelixAdmin admin = getHelixAdmin();
  IdealState idealState = admin.getResourceIdealState(clusterId, resourceName);
  ExternalView externalView = admin.getResourceExternalView(clusterId, resourceName);
  StateModelDefinition stateModelDef =
      admin.getStateModelDef(clusterId, idealState.getStateModelDefRef());
  String initialState = stateModelDef.getInitialState();
  List<String> statesPriorityList = stateModelDef.getStatesPriorityList();
  statesPriorityList = statesPriorityList.subList(0, statesPriorityList.indexOf(initialState)); // Trim
                                                                                                // stateList
                                                                                                // to
                                                                                                // initialState
                                                                                                // and
                                                                                                // above
  int minActiveReplicas = idealState.getMinActiveReplicas();

  // Start the logic that determines the health status of each partition
  Map<String, String> partitionHealthResult = new HashMap<>();
  Set<String> allPartitionNames = idealState.getPartitionSet();
  if (!allPartitionNames.isEmpty()) {
    for (String partitionName : allPartitionNames) {
      int replicaCount =
          idealState.getReplicaCount(idealState.getPreferenceList(partitionName).size());
      // Simplify expectedStateCountMap by assuming that all instances are available to reduce
      // computation load on this REST endpoint
      LinkedHashMap<String, Integer> expectedStateCountMap =
          stateModelDef.getStateCountMap(replicaCount, replicaCount);
      // Extract all states into Collections from ExternalView
      Map<String, String> stateMapInExternalView = externalView.getStateMap(partitionName);
      Collection<String> allReplicaStatesInExternalView =
          (stateMapInExternalView != null && !stateMapInExternalView.isEmpty())
              ? stateMapInExternalView.values()
              : Collections.<String> emptyList();
      int numActiveReplicasInExternalView = 0;
      HealthStatus status = HealthStatus.HEALTHY;

      // Go through all states that are "active" states (higher priority than InitialState)
      for (int statePriorityIndex = 0; statePriorityIndex < statesPriorityList
          .size(); statePriorityIndex++) {
        String currentState = statesPriorityList.get(statePriorityIndex);
        int currentStateCountInIdealState = expectedStateCountMap.get(currentState);
        int currentStateCountInExternalView =
            Collections.frequency(allReplicaStatesInExternalView, currentState);
        numActiveReplicasInExternalView += currentStateCountInExternalView;
        // Top state counts must match, if not, unhealthy
        if (statePriorityIndex == 0
            && currentStateCountInExternalView != currentStateCountInIdealState) {
          status = HealthStatus.UNHEALTHY;
          break;
        } else if (currentStateCountInExternalView < currentStateCountInIdealState) {
          // For non-top states, if count in ExternalView is less than count in IdealState,
          // partially healthy
          status = HealthStatus.PARTIAL_HEALTHY;
        }
      }
      if (numActiveReplicasInExternalView < minActiveReplicas) {
        // If this partition does not satisfy the number of minimum active replicas, unhealthy
        status = HealthStatus.UNHEALTHY;
      }
      partitionHealthResult.put(partitionName, status.name());
    }
  }
  return partitionHealthResult;
}
 
Example 3
Source File: DelayedRebalanceUtil.java    From helix with Apache License 2.0 3 votes vote down vote up
/**
 * Get the minimum active replica count threshold that allows delayed rebalance.
 * Prioritize of the input params:
 * 1. resourceConfig
 * 2. idealState
 * 3. replicaCount
 * The lower priority minimum active replica count will only be applied if the higher priority
 * items are missing.
 * TODO: Remove the idealState input once we have all the config information migrated to the
 * TODO: resource config by default.
 *
 * @param resourceConfig the resource config
 * @param idealState     the ideal state of the resource
 * @param replicaCount   the expected active replica count.
 * @return the expected minimum active replica count that is required
 */
public static int getMinActiveReplica(ResourceConfig resourceConfig, IdealState idealState,
    int replicaCount) {
  int minActiveReplicas = resourceConfig == null ? -1 : resourceConfig.getMinActiveReplica();
  if (minActiveReplicas < 0) {
    minActiveReplicas = idealState.getMinActiveReplicas();
  }
  if (minActiveReplicas < 0) {
    minActiveReplicas = replicaCount;
  }
  return minActiveReplicas;
}