Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.helix.model;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
The ideal states of all partitions in a resource
 
 public class IdealState extends HelixProperty {
  
Properties that are persisted and are queryable for an ideal state
 
   public enum IdealStateProperty {
     NUM_PARTITIONS,
     STATE_MODEL_DEF_REF,
     STATE_MODEL_FACTORY_NAME,
     REPLICAS,
     @Deprecated
     IDEAL_STATE_MODE,
     REBALANCE_MODE,
     REBALANCE_TIMER_PERIOD,
     MAX_PARTITIONS_PER_INSTANCE,
     INSTANCE_GROUP_TAG,
     REBALANCER_CLASS_NAME
   }
 
   public static final String QUERY_LIST = "PREFERENCE_LIST_QUERYS";

  
Deprecated.

 
   public enum IdealStateModeProperty {
     AUTO,
     CUSTOMIZED,
     AUTO_REBALANCE
   }

  
The mode used for rebalance. FULL_AUTO does both node location calculation and state assignment, SEMI_AUTO only does the latter, and CUSTOMIZED does neither. USER_DEFINED uses a Rebalancer implementation plugged in by the user.
 
   public enum RebalanceMode {
     FULL_AUTO,
     SEMI_AUTO,
     CUSTOMIZED,
    USER_DEFINED,
    NONE
  }
  private static final Logger logger = Logger.getLogger(IdealState.class.getName());

  
Instantiate an ideal state for a resource

Parameters:
resourceName the name of the resource
  public IdealState(String resourceName) {
    super(resourceName);
  }

  
Instantiate an ideal state for a resource

Parameters:
resourceId the id of the resource
  public IdealState(ResourceId resourceId) {
    super(resourceId.stringify());
  }

  
Instantiate an ideal state from a record

Parameters:
record ZNRecord corresponding to an ideal state
  public IdealState(ZNRecord record) {
    super(record);
  }

  
Get the associated resource

Returns:
the name of the resource
  public String getResourceName() {
    return .getId();
  }

  
Get the associated resource

Returns:
the id of the resource
  public ResourceId getResourceId() {
    return ResourceId.from(getResourceName());
  }

  
Get the rebalance mode of the ideal state

  public void setIdealStateMode(String mode) {
    RebalanceMode rebalanceMode = normalizeRebalanceMode(IdealStateModeProperty.valueOf(mode));
  }

  
Get the rebalance mode of the resource

Parameters:
rebalancerType
  public void setRebalanceMode(RebalanceMode rebalancerType) {
    IdealStateModeProperty idealStateMode = denormalizeRebalanceMode(rebalancerType);
  }

  
Get the maximum number of partitions an instance can serve

Returns:
the partition capacity of an instance for this resource, or Integer.MAX_VALUE
  public int getMaxPartitionsPerInstance() {
        .);
  }

  
Define a custom rebalancer that implements org.apache.helix.controller.rebalancer.HelixRebalancer

Parameters:
rebalancerClassName the name of the custom rebalancing class
  public void setRebalancerClassName(String rebalancerClassName) {
    
        .setSimpleField(..toString(), rebalancerClassName);
  }

  
Get the name of the user-defined rebalancer associated with this resource

Returns:
the rebalancer class name, or null if none is being used
  }

  
Set a reference to the user-defined rebalancer associated with this resource(if any)

Parameters:
rebalancerRef a reference to a user-defined rebalancer
  public void setRebalancerRef(RebalancerRef rebalancerRef) {
    if (rebalancerRef != null) {
      setRebalancerClassName(rebalancerRef.toString());
    } else {
      setRebalancerClassName(null);
    }
  }

  
Get a reference to the user-defined rebalancer associated with this resource(if any)

Returns:
RebalancerRef
    String className = getRebalancerClassName();
    if (className != null) {
      return RebalancerRef.from(getRebalancerClassName());
    }
    return null;
  }

  
Set the maximum number of partitions of this resource that an instance can serve

Parameters:
max the maximum number of partitions supported
  public void setMaxPartitionsPerInstance(int max) {
  }

  
Get the rebalancing mode on this resource

  }

  
Get the rebalancing mode on this resource

    RebalanceMode property =
            .);
    if (property == .) {
      property = normalizeRebalanceMode(getIdealStateMode());
      setRebalanceMode(property);
    }
    return property;
  }

  
Set the preferred instance placement and state for a partition replica

Parameters:
partitionName the replica to set
instanceName the assigned instance
state the replica state in this instance
  public void setPartitionState(String partitionNameString instanceNameString state) {
    Map<StringStringmapField = .getMapField(partitionName);
    if (mapField == null) {
      .setMapField(partitionNamenew TreeMap<StringString>());
    }
    .getMapField(partitionName).put(instanceNamestate);
  }

  
Set the preferred participant placement and state for a partition replica

Parameters:
partitionId the replica to set
participantId the assigned participant
state the replica state in this instance
  public void setPartitionState(PartitionId partitionIdParticipantId participantIdState state) {
    Map<StringStringmapField = .getMapField(partitionId.stringify());
    if (mapField == null) {
      .setMapField(partitionId.stringify(), new TreeMap<StringString>());
    }
    .getMapField(partitionId.stringify()).put(participantId.stringify(), state.toString());
  }

  
Get all of the partitions

Returns:
a set of partition names
  public Set<StringgetPartitionSet() {
        || getRebalanceMode() == .) {
      return .getListFields().keySet();
    } else if (getRebalanceMode() == .
        || getRebalanceMode() == .) {
      return .getMapFields().keySet();
    } else {
      .error("Invalid ideal state mode:" + getResourceName());
      return Collections.emptySet();
    }
  }

  
Get all of the partitions

Returns:
a set of partitions
    Set<PartitionIdpartitionSet = Sets.newHashSet();
    for (String partitionName : getPartitionSet()) {
      partitionSet.add(PartitionId.from(partitionName));
    }
    return partitionSet;
  }

  
Get the current mapping of a partition

Parameters:
partitionName the name of the partition
Returns:
the instances where the replicas live and the state of each
  public Map<StringStringgetInstanceStateMap(String partitionName) {
    return .getMapField(partitionName);
  }

  
Set the current mapping of a partition

Parameters:
partitionName the partition to set
instanceStateMap (participant name, state) pairs
  public void setInstanceStateMap(String partitionNameMap<StringStringinstanceStateMap) {
    .setMapField(partitionNameinstanceStateMap);
  }

  
Set the current mapping of a partition

Parameters:
partitionId the partition to set
participantStateMap (participant id, state) pairs
  public void setParticipantStateMap(PartitionId partitionId,
      Map<ParticipantIdStateparticipantStateMap) {
    Map<StringStringrawMap = new HashMap<StringString>();
    for (ParticipantId participantId : participantStateMap.keySet()) {
      rawMap.put(participantId.stringify(), participantStateMap.get(participantId).toString());
    }
    .setMapField(partitionId.stringify(), rawMap);
  }

  
Get the current mapping of a partition

Parameters:
partitionId the name of the partition
Returns:
the instances where the replicas live and the state of each
    Map<StringStringinstanceStateMap = getInstanceStateMap(partitionId.stringify());
    Map<ParticipantIdStateparticipantStateMap = Maps.newHashMap();
    if (instanceStateMap != null) {
      for (String participantId : instanceStateMap.keySet()) {
        participantStateMap.put(ParticipantId.from(participantId),
            State.from(instanceStateMap.get(participantId)));
      }
      return participantStateMap;
    }
    return null;
  }

  
Get the instances who host replicas of a partition

Parameters:
partitionName the partition to look up
Returns:
set of instance names
  public Set<StringgetInstanceSet(String partitionName) {
        || getRebalanceMode() == .) {
      List<StringprefList = .getListField(partitionName);
      if (prefList != null) {
        return new TreeSet<String>(prefList);
      } else {
        .warn(partitionName + " does NOT exist");
        return Collections.emptySet();
      }
    } else if (getRebalanceMode() == .
        || getRebalanceMode() == .) {
      Map<StringStringstateMap = .getMapField(partitionName);
      if (stateMap != null) {
        return new TreeSet<String>(stateMap.keySet());
      } else {
        .warn(partitionName + " does NOT exist");
        return Collections.emptySet();
      }
    } else {
      .error("Invalid ideal state mode: " + getResourceName());
      return Collections.emptySet();
    }
  }

  
Get the participants who host replicas of a partition

Parameters:
partitionId the partition to look up
Returns:
set of participant ids
  public Set<ParticipantIdgetParticipantSet(PartitionId partitionId) {
    Set<ParticipantIdparticipantSet = Sets.newHashSet();
    for (String participantName : getInstanceSet(partitionId.stringify())) {
      participantSet.add(ParticipantId.from(participantName));
    }
    return participantSet;
  }

  
Set the preference list of a partition

Parameters:
partitionName the name of the partition to set
preferenceList a list of participants that can serve replicas of the partition
  public void setPreferenceList(String partitionNameList<StringpreferenceList) {
    .setListField(partitionNamepreferenceList);
  }

  
Set the preference list of a partition

Parameters:
partitionId the id of the partition to set
preferenceList a list of participants that can serve replicas of the partition
  public void setPreferenceList(PartitionId partitionIdList<ParticipantIdpreferenceList) {
    List<StringrawPreferenceList = new ArrayList<String>();
    for (ParticipantId participantId : preferenceList) {
      rawPreferenceList.add(participantId.stringify());
    }
    .setListField(partitionId.stringify(), rawPreferenceList);
  }

  
Get the preference list of a partition

Parameters:
partitionName the name of the partition
Returns:
a list of instances that can serve replicas of the partition
  public List<StringgetPreferenceList(String partitionName) {
    List<StringinstanceStateList = .getListField(partitionName);
    if (instanceStateList != null) {
      return instanceStateList;
    }
    .warn("Resource key:" + partitionName + " does not have a pre-computed preference list.");
    return null;
  }

  
Get the preference list of a partition

Parameters:
partitionId the partition id
Returns:
an ordered list of participants that can serve replicas of the partition
  public List<ParticipantIdgetPreferenceList(PartitionId partitionId) {
    List<ParticipantIdpreferenceList = Lists.newArrayList();
    List<StringpreferenceStringList = getPreferenceList(partitionId.stringify());
    if (preferenceStringList != null) {
      for (String participantName : preferenceStringList) {
        preferenceList.add(ParticipantId.from(participantName));
      }
      return preferenceList;
    }
    return null;
  }

  
Get the state model associated with this resource

Returns:
an identifier of the state model
  public String getStateModelDefRef() {
  }

  
Get the state model associated with this resource

Returns:
an identifier of the state model
    return StateModelDefId.from(getStateModelDefRef());
  }

  
Set the state model associated with this resource

Parameters:
stateModel state model identifier
  public void setStateModelDefRef(String stateModel) {
  }

  
Set the state model associated with this resource

Parameters:
stateModel state model identifier
  public void setStateModelDefId(StateModelDefId stateModelDefId) {
    setStateModelDefRef(stateModelDefId.stringify());
  }

  
Set the number of partitions of this resource

Parameters:
numPartitions the number of partitions
  public void setNumPartitions(int numPartitions) {
  }

  
Get the number of partitions of this resource

Returns:
the number of partitions
  public int getNumPartitions() {
  }

  
Set the number of replicas for each partition of this resource. There are documented special values for the replica count, so this is a String.

Parameters:
replicas replica count (as a string)
  public void setReplicas(String replicas) {
  }

  
Get the number of replicas for each partition of this resource

Returns:
number of replicas (as a string)
  public String getReplicas() {
    // HACK: if replica doesn't exists, use the length of the first list field
    // instead
    // TODO: remove it when Dbus fixed the IdealState writer
    if (replica == null) {
      String firstPartition = null;
      switch (getRebalanceMode()) {
      case :
        if (.getListFields().size() == 0) {
          replica = "0";
        } else {
          firstPartition = new ArrayList<String>(.getListFields().keySet()).get(0);
          replica =
              Integer.toString(firstPartition == null ? 0 : .getListField(firstPartition)
                  .size());
        }
        
            .warn("could NOT find number of replicas in idealState. Use size of the first list instead. replica: "
                + replica + ", 1st partition: " + firstPartition);
        break;
      case :
        if (.getMapFields().size() == 0) {
          replica = "0";
        } else {
          firstPartition = new ArrayList<String>(.getMapFields().keySet()).get(0);
          replica =
              Integer.toString(firstPartition == null ? 0 : .getMapField(firstPartition)
                  .size());
        }
        
            .warn("could NOT find replicas in idealState. Use size of the first map instead. replica: "
                + replica + ", 1st partition: " + firstPartition);
        break;
      default:
        replica = "0";
        .warn("could NOT determine replicas. set to 0");
        break;
      }
    }
    return replica;
  }

  
Set the state model factory associated with this resource

Parameters:
name state model factory name
  public void setStateModelFactoryName(String name) {
  }

  
Set the state model factory associated with this resource

Parameters:
name state model factory id
  public void setStateModelFactoryId(StateModelFactoryId stateModelFactoryId) {
    if (stateModelFactoryId != null) {
      setStateModelFactoryName(stateModelFactoryId.stringify());
    }
  }

  
Get the state model factory associated with this resource

Returns:
state model factory name
  }

  
Get the state model factory associated with this resource

Returns:
state model factory id
    return StateModelFactoryId.from(getStateModelFactoryName());
  }

  
Set the frequency with which to rebalance

Returns:
the rebalancing timer period
  public int getRebalanceTimerPeriod() {
  }
  public boolean isValid() {
    if (getNumPartitions() < 0) {
      .error("idealState:" +  + " does not have number of partitions (was "
          + getNumPartitions() + ").");
      return false;
    }
    if (getStateModelDefRef() == null) {
      .error("idealStates:" +  + " does not have state model definition.");
      return false;
    }
      String replicaStr = getReplicas();
      if (replicaStr == null) {
        .error("invalid ideal-state. missing replicas in auto mode. record was: " + );
        return false;
      }
        int replica = Integer.parseInt(replicaStr);
        Set<StringpartitionSet = getPartitionSet();
        for (String partition : partitionSet) {
          List<StringpreferenceList = getPreferenceList(partition);
          if (preferenceList == null || preferenceList.size() != replica) {
            
                .error("invalid ideal-state. preference-list size not equals to replicas in auto mode. replica: "
                    + replica
                    + ", preference-list size: "
                    + preferenceList.size()
                    + ", record was: " + );
            return false;
          }
        }
      }
    }
    return true;
  }

  
Set a tag to check to enforce assignment to certain instances

Parameters:
groupTag the instance group tag
  public void setInstanceGroupTag(String groupTag) {
  }

  
Check for a tag that will restrict assignment to instances with a matching tag

Returns:
the group tag, or null if none is present
  public String getInstanceGroupTag() {
  }

  
Update the ideal state from a ResourceAssignment computed during a rebalance

Parameters:
assignment the new resource assignment
stateModelDef state model of the resource
  public void updateFromAssignment(ResourceAssignment assignmentStateModelDefinition stateModelDef) {
    // clear all preference lists and maps
    // assign a partition at a time
    for (PartitionId partition : assignment.getMappedPartitionIds()) {
      List<ParticipantIdpreferenceList = new ArrayList<ParticipantId>();
      Map<ParticipantIdStateparticipantStateMap = new HashMap<ParticipantIdState>();
      // invert the map to get in state order
      Map<ParticipantIdStatereplicaMap = assignment.getReplicaMap(partition);
      ListMultimap<StateParticipantIdinverseMap = ArrayListMultimap.create();
      Multimaps.invertFrom(Multimaps.forMap(replicaMap), inverseMap);
      // update the ideal state in order of state priorities
      for (State state : stateModelDef.getTypedStatesPriorityList()) {
        if (!state.equals(State.from(.))
            && !state.equals(State.from(.))) {
          List<ParticipantIdstateParticipants = inverseMap.get(state);
          for (ParticipantId participant : stateParticipants) {
            preferenceList.add(participant);
            participantStateMap.put(participantstate);
          }
        }
      }
      setPreferenceList(partitionpreferenceList);
      setParticipantStateMap(partitionparticipantStateMap);
    }
  }
    RebalanceMode property;
    switch (mode) {
    case :
      property = .;
      break;
    case :
      property = .;
      break;
    case :
      property = .;
      break;
    default:
      if (getRebalancerClassName() != null) {
        property = .;
      } else {
        property = .;
      }
      break;
    }
    return property;
  }
    IdealStateModeProperty property;
    switch (rebalancerType) {
    case :
      property = .;
      break;
    case :
      property = .;
      break;
    case :
      property = .;
      break;
    default:
      property = .;
      break;
    }
    return property;
  }

  
Parse a rebalance mode from a string. It can also understand IdealStateModeProperty values

Parameters:
mode string containing a RebalanceMode value
defaultMode the mode to use if the string is not valid
Returns:
converted RebalanceMode value
  public RebalanceMode rebalanceModeFromString(String modeRebalanceMode defaultMode) {
    RebalanceMode rebalanceMode = defaultMode;
    try {
      rebalanceMode = RebalanceMode.valueOf(mode);
    } catch (Exception rebalanceModeException) {
      try {
        IdealStateModeProperty oldMode = IdealStateModeProperty.valueOf(mode);
        rebalanceMode = normalizeRebalanceMode(oldMode);
      } catch (Exception e) {
        .error(e);
      }
    }
    return rebalanceMode;
  }

  
Get the non-Helix simple fields from this property and add them to a UserConfig

Parameters:
userConfig the user config to update
  public void updateUserConfig(UserConfig userConfig) {
    for (String simpleField : .getSimpleFields().keySet()) {
      Optional<IdealStatePropertyenumField =
          Enums.getIfPresent(IdealStateProperty.classsimpleField);
      if (!simpleField.contains(. + "") && !enumField.isPresent()) {
        userConfig.setSimpleField(simpleField.getSimpleField(simpleField));
      }
    }
  }

  
Convert a preference list of strings into a preference list of participants

Parameters:
rawPreferenceList the list of strings representing participant names
Returns:
converted list
  public static List<ParticipantIdpreferenceListFromStringList(List<StringrawPreferenceList) {
    if (rawPreferenceList == null) {
      return Collections.emptyList();
    }
    return Lists.transform(new ArrayList<String>(rawPreferenceList),
        new Function<StringParticipantId>() {
          @Override
          public ParticipantId apply(String participantName) {
            return ParticipantId.from(participantName);
          }
        });
  }

  
Convert preference lists of strings into preference lists of participants

Parameters:
rawPreferenceLists a map of partition name to a list of participant names
Returns:
converted lists as a map
      Map<StringList<String>> rawPreferenceLists) {
    if (rawPreferenceLists == null) {
      return Collections.emptyMap();
    }
    Map<PartitionIdList<ParticipantId>> preferenceLists =
        new HashMap<PartitionIdList<ParticipantId>>();
    for (String partitionId : rawPreferenceLists.keySet()) {
      preferenceLists.put(PartitionId.from(partitionId),
          preferenceListFromStringList(rawPreferenceLists.get(partitionId)));
    }
    return preferenceLists;
  }

  
Convert a preference list of participants into a preference list of strings

Parameters:
preferenceList the list of strings representing participant ids
Returns:
converted list
  public static List<StringstringListFromPreferenceList(List<ParticipantIdpreferenceList) {
    if (preferenceList == null) {
      return Collections.emptyList();
    }
    return Lists.transform(new ArrayList<ParticipantId>(preferenceList),
        new Function<ParticipantIdString>() {
          @Override
          public String apply(ParticipantId participantId) {
            return participantId.stringify();
          }
        });
  }

  
Convert preference lists of participants into preference lists of strings

Parameters:
preferenceLists a map of partition id to a list of participant ids
Returns:
converted lists as a map
      Map<PartitionIdList<ParticipantId>> preferenceLists) {
    if (preferenceLists == null) {
      return Collections.emptyMap();
    }
    Map<StringList<String>> rawPreferenceLists = new HashMap<StringList<String>>();
    for (PartitionId partitionId : preferenceLists.keySet()) {
      rawPreferenceLists.put(partitionId.stringify(),
          stringListFromPreferenceList(preferenceLists.get(partitionId)));
    }
    return rawPreferenceLists;
  }

  
Convert a partition mapping as strings into a participant state map

Parameters:
rawMap the map of participant name to state
Returns:
converted map
      Map<StringStringrawMap) {
    return ResourceAssignment.replicaMapFromStringMap(rawMap);
  }

  
Convert a full state mapping as strings into participant state maps

Parameters:
rawMaps the map of partition name to participant name and state
Returns:
converted maps
      Map<StringMap<StringString>> rawMaps) {
    return ResourceAssignment.replicaMapsFromStringMaps(rawMaps);
  }

  
Convert a partition mapping into a mapping of string names

Parameters:
participantStateMap the map of participant id to state
Returns:
converted map
      Map<ParticipantIdStateparticipantStateMap) {
    return ResourceAssignment.stringMapFromReplicaMap(participantStateMap);
  }

  
Convert a full state mapping into a mapping of string names

Parameters:
participantStateMaps the map of partition id to participant id and state
Returns:
converted maps
      Map<PartitionIdMap<ParticipantIdState>> participantStateMaps) {
    return ResourceAssignment.stringMapsFromReplicaMaps(participantStateMaps);
  }
New to GrepCode? Check out our FAQ X