use of org.apache.helix.controller.rebalancer.strategy.RebalanceStrategy in project helix by apache.
the class HelixUtil method getIdealAssignmentForFullAuto.
/**
* This method provides the ideal state mapping with corresponding rebalance strategy
* @param clusterConfig The cluster config
* @param instanceConfigs List of instance configs
* @param liveInstances List of live instance names
* @param idealState The ideal state of current resource. If input is null, will be
* treated as newly created resource.
* @param partitions The list of partition names
* @param strategyClassName The rebalance strategy. e.g. AutoRebalanceStrategy
* @return A map of ideal state assignment as partition -> instance -> state
*/
public static Map<String, Map<String, String>> getIdealAssignmentForFullAuto(ClusterConfig clusterConfig, List<InstanceConfig> instanceConfigs, List<String> liveInstances, IdealState idealState, List<String> partitions, String strategyClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
List<String> allNodes = new ArrayList<>();
Map<String, InstanceConfig> instanceConfigMap = new HashMap<>();
for (InstanceConfig instanceConfig : instanceConfigs) {
allNodes.add(instanceConfig.getInstanceName());
instanceConfigMap.put(instanceConfig.getInstanceName(), instanceConfig);
}
ClusterDataCache cache = new ClusterDataCache();
cache.setClusterConfig(clusterConfig);
cache.setInstanceConfigMap(instanceConfigMap);
StateModelDefinition stateModelDefinition = BuiltInStateModelDefinitions.valueOf(idealState.getStateModelDefRef()).getStateModelDefinition();
RebalanceStrategy strategy = RebalanceStrategy.class.cast(loadClass(HelixUtil.class, strategyClassName).newInstance());
strategy.init(idealState.getResourceName(), partitions, stateModelDefinition.getStateCountMap(liveInstances.size(), Integer.parseInt(idealState.getReplicas())), idealState.getMaxPartitionsPerInstance());
Map<String, List<String>> preferenceLists = strategy.computePartitionAssignment(allNodes, liveInstances, new HashMap<String, Map<String, String>>(), cache).getListFields();
Map<String, Map<String, String>> idealStateMapping = new HashMap<>();
Set<String> liveInstanceSet = new HashSet<>(liveInstances);
for (String partitionName : preferenceLists.keySet()) {
idealStateMapping.put(partitionName, computeIdealMapping(preferenceLists.get(partitionName), stateModelDefinition, liveInstanceSet));
}
return idealStateMapping;
}
use of org.apache.helix.controller.rebalancer.strategy.RebalanceStrategy in project helix by apache.
the class AbstractRebalancer method getRebalanceStrategy.
protected RebalanceStrategy getRebalanceStrategy(String rebalanceStrategyName, List<String> partitions, String resourceName, LinkedHashMap<String, Integer> stateCountMap, int maxPartition) {
RebalanceStrategy rebalanceStrategy;
if (rebalanceStrategyName == null || rebalanceStrategyName.equalsIgnoreCase(RebalanceStrategy.DEFAULT_REBALANCE_STRATEGY)) {
rebalanceStrategy = new AutoRebalanceStrategy(resourceName, partitions, stateCountMap, maxPartition);
} else {
try {
rebalanceStrategy = RebalanceStrategy.class.cast(HelixUtil.loadClass(getClass(), rebalanceStrategyName).newInstance());
rebalanceStrategy.init(resourceName, partitions, stateCountMap, maxPartition);
} catch (ClassNotFoundException ex) {
throw new HelixException("Exception while invoking custom rebalance strategy class: " + rebalanceStrategyName, ex);
} catch (InstantiationException ex) {
throw new HelixException("Exception while invoking custom rebalance strategy class: " + rebalanceStrategyName, ex);
} catch (IllegalAccessException ex) {
throw new HelixException("Exception while invoking custom rebalance strategy class: " + rebalanceStrategyName, ex);
}
}
return rebalanceStrategy;
}
Aggregations