Search in sources :

Example 1 with RebalanceStrategy

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;
}
Also used : ClusterDataCache(org.apache.helix.controller.stages.ClusterDataCache) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InstanceConfig(org.apache.helix.model.InstanceConfig) RebalanceStrategy(org.apache.helix.controller.rebalancer.strategy.RebalanceStrategy) StateModelDefinition(org.apache.helix.model.StateModelDefinition) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with RebalanceStrategy

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;
}
Also used : AutoRebalanceStrategy(org.apache.helix.controller.rebalancer.strategy.AutoRebalanceStrategy) HelixException(org.apache.helix.HelixException) AutoRebalanceStrategy(org.apache.helix.controller.rebalancer.strategy.AutoRebalanceStrategy) RebalanceStrategy(org.apache.helix.controller.rebalancer.strategy.RebalanceStrategy)

Aggregations

RebalanceStrategy (org.apache.helix.controller.rebalancer.strategy.RebalanceStrategy)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 HelixException (org.apache.helix.HelixException)1 AutoRebalanceStrategy (org.apache.helix.controller.rebalancer.strategy.AutoRebalanceStrategy)1 ClusterDataCache (org.apache.helix.controller.stages.ClusterDataCache)1 InstanceConfig (org.apache.helix.model.InstanceConfig)1 StateModelDefinition (org.apache.helix.model.StateModelDefinition)1