use of org.apache.helix.model.Partition in project helix by apache.
the class CurrentStateComputationStage method updateCurrentStates.
// update current states in CurrentStateOutput
private void updateCurrentStates(LiveInstance instance, Collection<CurrentState> currentStates, CurrentStateOutput currentStateOutput, Map<String, Resource> resourceMap) {
String instanceName = instance.getInstanceName();
String instanceSessionId = instance.getSessionId();
for (CurrentState currentState : currentStates) {
if (!instanceSessionId.equals(currentState.getSessionId())) {
continue;
}
String resourceName = currentState.getResourceName();
String stateModelDefName = currentState.getStateModelDefRef();
Resource resource = resourceMap.get(resourceName);
if (resource == null) {
continue;
}
if (stateModelDefName != null) {
currentStateOutput.setResourceStateModelDef(resourceName, stateModelDefName);
}
currentStateOutput.setBucketSize(resourceName, currentState.getBucketSize());
Map<String, String> partitionStateMap = currentState.getPartitionStateMap();
for (String partitionName : partitionStateMap.keySet()) {
Partition partition = resource.getPartition(partitionName);
if (partition != null) {
currentStateOutput.setCurrentState(resourceName, partition, instanceName, currentState.getState(partitionName));
currentStateOutput.setRequestedState(resourceName, partition, instanceName, currentState.getRequestedState(partitionName));
currentStateOutput.setInfo(resourceName, partition, instanceName, currentState.getInfo(partitionName));
currentStateOutput.setEndTime(resourceName, partition, instanceName, currentState.getEndTime(partitionName));
}
}
}
}
use of org.apache.helix.model.Partition in project helix by apache.
the class CurrentStateComputationStage method updatePendingMessages.
// update all pending messages to CurrentStateOutput.
private void updatePendingMessages(LiveInstance instance, Collection<Message> pendingMessages, CurrentStateOutput currentStateOutput, Map<String, Resource> resourceMap) {
String instanceName = instance.getInstanceName();
String instanceSessionId = instance.getSessionId();
// update all pending messages
for (Message message : pendingMessages) {
if (!MessageType.STATE_TRANSITION.name().equalsIgnoreCase(message.getMsgType()) && !MessageType.STATE_TRANSITION_CANCELLATION.name().equalsIgnoreCase(message.getMsgType())) {
continue;
}
if (!instanceSessionId.equals(message.getTgtSessionId())) {
continue;
}
String resourceName = message.getResourceName();
Resource resource = resourceMap.get(resourceName);
if (resource == null) {
continue;
}
if (!message.getBatchMessageMode()) {
String partitionName = message.getPartitionName();
Partition partition = resource.getPartition(partitionName);
if (partition != null) {
setMessageState(currentStateOutput, resourceName, partition, instanceName, message);
} else {
// log
}
} else {
List<String> partitionNames = message.getPartitionNames();
if (!partitionNames.isEmpty()) {
for (String partitionName : partitionNames) {
Partition partition = resource.getPartition(partitionName);
if (partition != null) {
setMessageState(currentStateOutput, resourceName, partition, instanceName, message);
} else {
// log
}
}
}
}
}
}
use of org.apache.helix.model.Partition in project helix by apache.
the class CurrentStateComputationStage method updateMissingTopStateStatus.
private void updateMissingTopStateStatus(ClusterDataCache cache, ClusterStatusMonitor clusterStatusMonitor, Map<String, Resource> resourceMap, CurrentStateOutput currentStateOutput) {
Map<String, Map<String, Long>> missingTopStateMap = cache.getMissingTopStateMap();
long durationThreshold = Long.MAX_VALUE;
if (cache.getClusterConfig() != null) {
durationThreshold = cache.getClusterConfig().getMissTopStateDurationThreshold();
}
for (Resource resource : resourceMap.values()) {
StateModelDefinition stateModelDef = cache.getStateModelDef(resource.getStateModelDefRef());
if (stateModelDef == null || resource.getStateModelDefRef().equalsIgnoreCase(TASK_STATE_MODEL_NAME)) {
// Resource does not have valid statemodel or it is task state model
continue;
}
for (Partition partition : resource.getPartitions()) {
Map<String, String> stateMap = currentStateOutput.getCurrentStateMap(resource.getResourceName(), partition);
// Missing top state need to record
if (!stateMap.values().contains(stateModelDef.getTopState()) && (!missingTopStateMap.containsKey(resource.getResourceName()) || !missingTopStateMap.get(resource.getResourceName()).containsKey(partition.getPartitionName()))) {
reportNewTopStateMissing(cache, stateMap, missingTopStateMap, resource, partition, stateModelDef.getTopState());
}
// The first time participant started or controller switched will be ignored
if (missingTopStateMap.containsKey(resource.getResourceName()) && missingTopStateMap.get(resource.getResourceName()).containsKey(partition.getPartitionName()) && stateMap.values().contains(stateModelDef.getTopState())) {
reportTopStateComesBack(cache, stateMap, missingTopStateMap, resource, partition, clusterStatusMonitor, durationThreshold, stateModelDef.getTopState());
}
}
}
// Check whether it is already passed threshold
for (String resourceName : missingTopStateMap.keySet()) {
for (String partitionName : missingTopStateMap.get(resourceName).keySet()) {
long startTime = missingTopStateMap.get(resourceName).get(partitionName);
if (startTime > 0 && System.currentTimeMillis() - startTime > durationThreshold) {
missingTopStateMap.get(resourceName).put(partitionName, TRANSITION_FAILED);
if (clusterStatusMonitor != null) {
clusterStatusMonitor.updateMissingTopStateDurationStats(resourceName, 0L, false);
}
}
}
}
if (clusterStatusMonitor != null) {
clusterStatusMonitor.resetMaxMissingTopStateGauge();
}
}
use of org.apache.helix.model.Partition in project helix by apache.
the class AbstractRebalancer method computeBestPossiblePartitionState.
/**
* Compute the best state for all partitions.
* This is the default implementation, subclasses should re-implement
* this method if its logic to generate bestpossible map for each partition is different from the default one here.
*
* @param cache
* @param idealState
* @param resource
* @param currentStateOutput
* Provides the current state and pending state transitions for all partitions
* @return
*/
@Override
public ResourceAssignment computeBestPossiblePartitionState(ClusterDataCache cache, IdealState idealState, Resource resource, CurrentStateOutput currentStateOutput) {
if (LOG.isDebugEnabled()) {
LOG.debug("Processing resource:" + resource.getResourceName());
}
String stateModelDefName = idealState.getStateModelDefRef();
StateModelDefinition stateModelDef = cache.getStateModelDef(stateModelDefName);
ResourceAssignment partitionMapping = new ResourceAssignment(resource.getResourceName());
for (Partition partition : resource.getPartitions()) {
Map<String, String> currentStateMap = currentStateOutput.getCurrentStateMap(resource.getResourceName(), partition);
Set<String> disabledInstancesForPartition = cache.getDisabledInstancesForPartition(resource.getResourceName(), partition.toString());
List<String> preferenceList = getPreferenceList(partition, idealState, Collections.unmodifiableSet(cache.getLiveInstances().keySet()));
Map<String, String> bestStateForPartition = computeBestPossibleStateForPartition(cache.getLiveInstances().keySet(), stateModelDef, preferenceList, currentStateMap, disabledInstancesForPartition, idealState);
partitionMapping.addReplicaMap(partition, bestStateForPartition);
}
return partitionMapping;
}
use of org.apache.helix.model.Partition in project helix by apache.
the class CustomRebalancer method computeBestPossiblePartitionState.
@Override
public ResourceAssignment computeBestPossiblePartitionState(ClusterDataCache cache, IdealState idealState, Resource resource, CurrentStateOutput currentStateOutput) {
// Looking for cached BestPossible mapping for this resource, if it is already there, do not recompute it again.
// The cached mapping will be cleared in ClusterDataCache if there is anything changed in cluster state that can
// cause the potential changes in BestPossible state.
ResourceAssignment partitionMapping = cache.getCachedResourceAssignment(resource.getResourceName());
if (partitionMapping != null) {
return partitionMapping;
}
LOG.info("Computing BestPossibleMapping for " + resource.getResourceName());
String stateModelDefName = idealState.getStateModelDefRef();
StateModelDefinition stateModelDef = cache.getStateModelDef(stateModelDefName);
if (LOG.isDebugEnabled()) {
LOG.debug("Processing resource:" + resource.getResourceName());
}
partitionMapping = new ResourceAssignment(resource.getResourceName());
for (Partition partition : resource.getPartitions()) {
Map<String, String> currentStateMap = currentStateOutput.getCurrentStateMap(resource.getResourceName(), partition);
Set<String> disabledInstancesForPartition = cache.getDisabledInstancesForPartition(resource.getResourceName(), partition.toString());
Map<String, String> idealStateMap = idealState.getInstanceStateMap(partition.getPartitionName());
Map<String, String> bestStateForPartition = computeCustomizedBestStateForPartition(cache, stateModelDef, idealStateMap, currentStateMap, disabledInstancesForPartition, idealState.isEnabled());
partitionMapping.addReplicaMap(partition, bestStateForPartition);
}
cache.setCachedResourceAssignment(resource.getResourceName(), partitionMapping);
return partitionMapping;
}
Aggregations