コード例 #1
0
  /**
   * TODO: This code is duplicate in multiple places. Can we do it in to one place in the beginning
   * and compute the stateConstraint instance once and re use at other places. Each IdealState must
   * have a constraint object associated with it
   *
   * @param stateModelDefinition
   * @param rebalancerConfig if rebalancerConfig == null, we can't evaluate R thus no constraints
   * @param cluster
   * @return
   */
  private Map<State, Bounds> computeStateConstraints(
      StateModelDefinition stateModelDefinition, IdealState idealState, Cluster cluster) {
    Map<State, Bounds> stateConstraints = new HashMap<State, Bounds>();

    List<State> statePriorityList = stateModelDefinition.getTypedStatesPriorityList();
    for (State state : statePriorityList) {
      String numInstancesPerState = stateModelDefinition.getNumParticipantsPerState(state);
      int max = -1;
      if ("N".equals(numInstancesPerState)) {
        max = cluster.getLiveParticipantMap().size();
      } else if ("R".equals(numInstancesPerState)) {
        // idealState is null when resource has been dropped,
        // R can't be evaluated and ignore state constraints
        if (idealState != null) {
          String replicas = idealState.getReplicas();
          if (replicas.equals(StateModelToken.ANY_LIVEINSTANCE.toString())) {
            max = cluster.getLiveParticipantMap().size();
          } else {
            max = Integer.parseInt(replicas);
          }
        }
      } else {
        try {
          max = Integer.parseInt(numInstancesPerState);
        } catch (Exception e) {
          // use -1
        }
      }

      if (max > -1) {
        // if state has no constraint, will not put in map
        stateConstraints.put(state, new Bounds(0, max));
      }
    }

    return stateConstraints;
  }