use of org.apache.helix.controller.rebalancer.Rebalancer in project helix by apache.
the class BestPossibleStateCalcStage method getRebalancer.
private Rebalancer getRebalancer(IdealState idealState, String resourceName, boolean isMaintenanceModeEnabled) {
Rebalancer customizedRebalancer = null;
String rebalancerClassName = idealState.getRebalancerClassName();
if (rebalancerClassName != null) {
logger.debug("resource " + resourceName + " use idealStateRebalancer " + rebalancerClassName);
try {
customizedRebalancer = Rebalancer.class.cast(HelixUtil.loadClass(getClass(), rebalancerClassName).newInstance());
} catch (Exception e) {
logger.error("Exception while invoking custom rebalancer class:" + rebalancerClassName, e);
}
}
Rebalancer rebalancer = null;
switch(idealState.getRebalanceMode()) {
case FULL_AUTO:
if (isMaintenanceModeEnabled) {
rebalancer = new MaintenanceRebalancer();
} else {
if (customizedRebalancer != null) {
rebalancer = customizedRebalancer;
} else {
rebalancer = new AutoRebalancer();
}
}
break;
case SEMI_AUTO:
rebalancer = new SemiAutoRebalancer();
break;
case CUSTOMIZED:
rebalancer = new CustomRebalancer();
break;
case USER_DEFINED:
case TASK:
rebalancer = customizedRebalancer;
break;
default:
logger.error("Fail to find the rebalancer, invalid rebalance mode " + idealState.getRebalanceMode());
break;
}
return rebalancer;
}
use of org.apache.helix.controller.rebalancer.Rebalancer in project helix by apache.
the class BestPossibleStateCalcStage method computeResourceBestPossibleState.
private boolean computeResourceBestPossibleState(ClusterEvent event, ClusterDataCache cache, CurrentStateOutput currentStateOutput, Resource resource, BestPossibleStateOutput output) {
// for each ideal state
// read the state model def
// for each resource
// get the preference list
// for each instanceName check if its alive then assign a state
String resourceName = resource.getResourceName();
logger.debug("Processing resource:" + resourceName);
// Ideal state may be gone. In that case we need to get the state model name
// from the current state
IdealState idealState = cache.getIdealState(resourceName);
if (idealState == null) {
// if ideal state is deleted, use an empty one
logger.info("resource:" + resourceName + " does not exist anymore");
idealState = new IdealState(resourceName);
idealState.setStateModelDefRef(resource.getStateModelDefRef());
}
Rebalancer rebalancer = getRebalancer(idealState, resourceName, cache.isMaintenanceModeEnabled());
MappingCalculator mappingCalculator = getMappingCalculator(rebalancer, resourceName);
if (rebalancer == null || mappingCalculator == null) {
logger.error("Error computing assignment for resource " + resourceName + ". no rebalancer found. rebalancer: " + rebalancer + " mappingCaculator: " + mappingCalculator);
}
if (rebalancer != null && mappingCalculator != null) {
if (rebalancer instanceof TaskRebalancer) {
TaskRebalancer taskRebalancer = TaskRebalancer.class.cast(rebalancer);
taskRebalancer.setClusterStatusMonitor((ClusterStatusMonitor) event.getAttribute(AttributeName.clusterStatusMonitor.name()));
}
ResourceAssignment partitionStateAssignment = null;
try {
HelixManager manager = event.getAttribute(AttributeName.helixmanager.name());
rebalancer.init(manager);
idealState = rebalancer.computeNewIdealState(resourceName, idealState, currentStateOutput, cache);
output.setPreferenceLists(resourceName, idealState.getPreferenceLists());
// Use the internal MappingCalculator interface to compute the final assignment
// The next release will support rebalancers that compute the mapping from start to finish
partitionStateAssignment = mappingCalculator.computeBestPossiblePartitionState(cache, idealState, resource, currentStateOutput);
for (Partition partition : resource.getPartitions()) {
Map<String, String> newStateMap = partitionStateAssignment.getReplicaMap(partition);
output.setState(resourceName, partition, newStateMap);
}
// Check if calculation is done successfully
return checkBestPossibleStateCalculation(idealState);
} catch (Exception e) {
logger.error("Error computing assignment for resource " + resourceName + ". Skipping.", e);
// TODO : remove this part after debugging NPE
StringBuilder sb = new StringBuilder();
sb.append(String.format("HelixManager is null : %s\n", event.getAttribute("helixmanager") == null));
sb.append(String.format("Rebalancer is null : %s\n", rebalancer == null));
sb.append(String.format("Calculated idealState is null : %s\n", idealState == null));
sb.append(String.format("MappingCaculator is null : %s\n", mappingCalculator == null));
sb.append(String.format("PartitionAssignment is null : %s\n", partitionStateAssignment == null));
sb.append(String.format("Output is null : %s\n", output == null));
logger.error(sb.toString());
}
}
// Exception or rebalancer is not found
return false;
}
Aggregations