use of org.apache.helix.model.Partition in project helix by apache.
the class JobRebalancer method toResourceAssignment.
private ResourceAssignment toResourceAssignment(String jobResource, Map<Integer, PartitionAssignment> paMap) {
// Construct a ResourceAssignment object from the map of partition assignments.
ResourceAssignment ra = new ResourceAssignment(jobResource);
for (Map.Entry<Integer, PartitionAssignment> e : paMap.entrySet()) {
PartitionAssignment pa = e.getValue();
ra.addReplicaMap(new Partition(pName(jobResource, e.getKey())), ImmutableMap.of(pa._instance, pa._state));
}
return ra;
}
use of org.apache.helix.model.Partition in project helix by apache.
the class TaskRebalancer method buildEmptyAssignment.
protected ResourceAssignment buildEmptyAssignment(String name, CurrentStateOutput currStateOutput) {
ResourceAssignment assignment = new ResourceAssignment(name);
Set<Partition> partitions = currStateOutput.getCurrentStateMappedPartitions(name);
for (Partition partition : partitions) {
Map<String, String> currentStateMap = currStateOutput.getCurrentStateMap(name, partition);
Map<String, String> replicaMap = Maps.newHashMap();
for (String instanceName : currentStateMap.keySet()) {
replicaMap.put(instanceName, HelixDefinedState.DROPPED.toString());
}
assignment.addReplicaMap(partition, replicaMap);
}
return assignment;
}
use of org.apache.helix.model.Partition in project helix by apache.
the class ClusterExternalViewVerifier method verify.
@Override
public boolean verify() throws Exception {
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(_accessor);
List<String> liveInstances = new ArrayList<String>();
liveInstances.addAll(cache.getLiveInstances().keySet());
boolean success = verifyLiveNodes(liveInstances);
if (!success) {
LOG.info("liveNodes not match, expect: " + _expectSortedLiveNodes + ", actual: " + liveInstances);
return false;
}
BestPossibleStateOutput bestPossbileStates = calculateBestPossibleState(cache);
Map<String, ExternalView> externalViews = _accessor.getChildValuesMap(_keyBuilder.externalViews());
for (String resourceName : externalViews.keySet()) {
ExternalView externalView = externalViews.get(resourceName);
Map<Partition, Map<String, String>> bestPossbileState = bestPossbileStates.getResourceMap(resourceName);
success = verifyExternalView(externalView, bestPossbileState);
if (!success) {
LOG.info("external-view for resource: " + resourceName + " not match");
return false;
}
}
return true;
}
use of org.apache.helix.model.Partition in project helix by apache.
the class CurrentStateOutput method getPartitionCountWithState.
private Map<String, Integer> getPartitionCountWithState(String resourceStateModel, String state, Map<String, Map<Partition, Map<String, Object>>> stateMap) {
Map<String, Integer> currentPartitionCount = new HashMap<>();
for (String resource : stateMap.keySet()) {
String stateModel = _resourceStateModelMap.get(resource);
if ((stateModel != null && stateModel.equals(resourceStateModel)) || (stateModel == null && resourceStateModel == null)) {
for (Partition partition : stateMap.get(resource).keySet()) {
Map<String, Object> partitionMessage = stateMap.get(resource).get(partition);
for (Map.Entry<String, Object> participantMap : partitionMessage.entrySet()) {
String participant = participantMap.getKey();
if (!currentPartitionCount.containsKey(participant)) {
currentPartitionCount.put(participant, 0);
}
String currState = participantMap.getValue().toString();
if (participantMap.getValue() instanceof Message) {
currState = ((Message) participantMap.getValue()).getToState();
}
if ((currState != null && currState.equals(state)) || (currState == null && state == null)) {
currentPartitionCount.put(participant, currentPartitionCount.get(participant) + 1);
}
}
}
}
}
return currentPartitionCount;
}
use of org.apache.helix.model.Partition in project helix by apache.
the class PersistAssignmentStage method process.
@Override
public void process(ClusterEvent event) throws Exception {
ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
ClusterConfig clusterConfig = cache.getClusterConfig();
if (!clusterConfig.isPersistBestPossibleAssignment() && !clusterConfig.isPersistIntermediateAssignment()) {
return;
}
BestPossibleStateOutput bestPossibleAssignment = event.getAttribute(AttributeName.BEST_POSSIBLE_STATE.name());
HelixManager helixManager = event.getAttribute(AttributeName.helixmanager.name());
HelixDataAccessor accessor = helixManager.getHelixDataAccessor();
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
for (String resourceId : bestPossibleAssignment.resourceSet()) {
Resource resource = resourceMap.get(resourceId);
if (resource != null) {
final IdealState idealState = cache.getIdealState(resourceId);
if (idealState == null) {
LOG.warn("IdealState not found for resource " + resourceId);
continue;
}
IdealState.RebalanceMode mode = idealState.getRebalanceMode();
if (!mode.equals(IdealState.RebalanceMode.SEMI_AUTO) && !mode.equals(IdealState.RebalanceMode.FULL_AUTO)) {
// do not persist assignment for resource in neither semi or full auto.
continue;
}
boolean needPersist = false;
if (mode.equals(IdealState.RebalanceMode.FULL_AUTO)) {
// persist preference list in ful-auto mode.
Map<String, List<String>> newLists = bestPossibleAssignment.getPreferenceLists(resourceId);
if (newLists != null && hasPreferenceListChanged(newLists, idealState)) {
idealState.setPreferenceLists(newLists);
needPersist = true;
}
}
PartitionStateMap partitionStateMap = bestPossibleAssignment.getPartitionStateMap(resourceId);
if (clusterConfig.isPersistIntermediateAssignment()) {
IntermediateStateOutput intermediateAssignment = event.getAttribute(AttributeName.INTERMEDIATE_STATE.name());
partitionStateMap = intermediateAssignment.getPartitionStateMap(resourceId);
}
// TODO: temporary solution for Espresso/Dbus backcompatible, should remove this.
Map<Partition, Map<String, String>> assignmentToPersist = convertAssignmentPersisted(resource, idealState, partitionStateMap.getStateMap());
if (assignmentToPersist != null && hasInstanceMapChanged(assignmentToPersist, idealState)) {
for (Partition partition : assignmentToPersist.keySet()) {
Map<String, String> instanceMap = assignmentToPersist.get(partition);
idealState.setInstanceStateMap(partition.getPartitionName(), instanceMap);
}
needPersist = true;
}
if (needPersist) {
// Update instead of set to ensure any intermediate changes that the controller does not update are kept.
accessor.updateProperty(keyBuilder.idealStates(resourceId), new DataUpdater<ZNRecord>() {
@Override
public ZNRecord update(ZNRecord current) {
if (current != null) {
// Overwrite MapFields and ListFields items with the same key.
// Note that default merge will keep old values in the maps or lists unchanged, which is not desired.
current.getMapFields().clear();
current.getMapFields().putAll(idealState.getRecord().getMapFields());
current.getListFields().putAll(idealState.getRecord().getListFields());
}
return current;
}
}, idealState);
}
}
}
}
Aggregations