本文整理汇总了Java中org.apache.helix.model.IdealState.getStateModelDefRef方法的典型用法代码示例。如果您正苦于以下问题:Java IdealState.getStateModelDefRef方法的具体用法?Java IdealState.getStateModelDefRef怎么用?Java IdealState.getStateModelDefRef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.helix.model.IdealState
的用法示例。
在下文中一共展示了IdealState.getStateModelDefRef方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addResource
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
@Override
public void addResource(String clusterName, String resourceName,
IdealState idealstate) {
String stateModelRef = idealstate.getStateModelDefRef();
String stateModelDefPath = PropertyPathBuilder.stateModelDef(clusterName, stateModelRef);
if (!_zkClient.exists(stateModelDefPath)) {
throw new HelixException(
"State model " + stateModelRef + " not found in the cluster STATEMODELDEFS path");
}
String idealStatePath = PropertyPathBuilder.idealState(clusterName);
String resourceIdealStatePath = idealStatePath + "/" + resourceName;
if (_zkClient.exists(resourceIdealStatePath)) {
throw new HelixException("Skip the operation. Resource ideal state directory already exists:"
+ resourceIdealStatePath);
}
ZKUtil.createChildren(_zkClient, idealStatePath, idealstate.getRecord());
}
示例2: computeBestPossiblePartitionState
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
/**
* 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;
}
示例3: computeIdealPartitionState
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
private Map<String, Map<String, String>> computeIdealPartitionState(ClusterDataCache cache,
IdealState idealState) {
String stateModelDefName = idealState.getStateModelDefRef();
StateModelDefinition stateModelDef = cache.getStateModelDef(stateModelDefName);
Map<String, Map<String, String>> idealPartitionState =
new HashMap<String, Map<String, String>>();
Set<String> liveEnabledInstances = new HashSet<String>(cache.getLiveInstances().keySet());
liveEnabledInstances.removeAll(cache.getDisabledInstances());
for (String partition : idealState.getPartitionSet()) {
List<String> preferenceList = AbstractRebalancer
.getPreferenceList(new Partition(partition), idealState, liveEnabledInstances);
Map<String, String> idealMapping =
HelixUtil.computeIdealMapping(preferenceList, stateModelDef, liveEnabledInstances);
idealPartitionState.put(partition, idealMapping);
}
return idealPartitionState;
}
示例4: computeBestPossiblePartitionState
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
/**
* 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());
}
Set<String> allNodes = cache.getEnabledInstances();
Set<String> liveNodes = cache.getLiveInstances().keySet();
ClusterConfig clusterConfig = cache.getClusterConfig();
long delayTime = getRebalanceDelay(idealState, clusterConfig);
Set<String> activeNodes = getActiveInstances(allNodes, idealState, liveNodes,
cache.getInstanceOfflineTimeMap(), cache.getLiveInstances().keySet(),
cache.getInstanceConfigMap(), delayTime, clusterConfig);
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, activeNodes);
Map<String, String> bestStateForPartition =
computeBestPossibleStateForPartition(liveNodes, stateModelDef, preferenceList, currentStateMap,
disabledInstancesForPartition, idealState);
partitionMapping.addReplicaMap(partition, bestStateForPartition);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Best possible mapping for resource " + resource.getResourceName() + ": "
+ partitionMapping);
}
return partitionMapping;
}
示例5: computeBestPossiblePartitionState
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
@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;
}
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;
}
示例6: rebalance
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
void rebalance(String clusterName, String resourceName, int replica, String keyPrefix,
List<String> instanceNames, String groupId) {
// ensure we get the same idealState with the same set of instances
Collections.sort(instanceNames);
IdealState idealState = getResourceIdealState(clusterName, resourceName);
if (idealState == null) {
throw new HelixException("Resource: " + resourceName + " has NOT been added yet");
}
if (groupId != null && groupId.length() > 0) {
idealState.setInstanceGroupTag(groupId);
}
idealState.setReplicas(Integer.toString(replica));
int partitions = idealState.getNumPartitions();
String stateModelName = idealState.getStateModelDefRef();
StateModelDefinition stateModDef = getStateModelDef(clusterName, stateModelName);
if (stateModDef == null) {
throw new HelixException("cannot find state model: " + stateModelName);
}
// StateModelDefinition def = new StateModelDefinition(stateModDef);
List<String> statePriorityList = stateModDef.getStatesPriorityList();
String masterStateValue = null;
String slaveStateValue = null;
replica--;
for (String state : statePriorityList) {
String count = stateModDef.getNumInstancesPerState(state);
if (count.equals("1")) {
if (masterStateValue != null) {
throw new HelixException("Invalid or unsupported state model definition");
}
masterStateValue = state;
} else if (count.equalsIgnoreCase("R")) {
if (slaveStateValue != null) {
throw new HelixException("Invalid or unsupported state model definition");
}
slaveStateValue = state;
} else if (count.equalsIgnoreCase("N")) {
if (!(masterStateValue == null && slaveStateValue == null)) {
throw new HelixException("Invalid or unsupported state model definition");
}
replica = instanceNames.size() - 1;
masterStateValue = slaveStateValue = state;
}
}
if (masterStateValue == null && slaveStateValue == null) {
throw new HelixException("Invalid or unsupported state model definition");
}
if (masterStateValue == null) {
masterStateValue = slaveStateValue;
}
if (idealState.getRebalanceMode() != RebalanceMode.FULL_AUTO
&& idealState.getRebalanceMode() != RebalanceMode.USER_DEFINED) {
ZNRecord newIdealState = DefaultIdealStateCalculator
.calculateIdealState(instanceNames, partitions, replica, keyPrefix, masterStateValue,
slaveStateValue);
// for now keep mapField in SEMI_AUTO mode and remove listField in CUSTOMIZED mode
if (idealState.getRebalanceMode() == RebalanceMode.SEMI_AUTO) {
idealState.getRecord().setListFields(newIdealState.getListFields());
// TODO: need consider to remove this.
idealState.getRecord().setMapFields(newIdealState.getMapFields());
}
if (idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED) {
idealState.getRecord().setMapFields(newIdealState.getMapFields());
}
} else {
for (int i = 0; i < partitions; i++) {
String partitionName = keyPrefix + "_" + i;
idealState.getRecord().setMapField(partitionName, new HashMap<String, String>());
idealState.getRecord().setListField(partitionName, new ArrayList<String>());
}
}
setResourceIdealState(clusterName, resourceName, idealState);
}
示例7: convertAssignmentPersisted
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
/**
* TODO: This is a temporary hacky for back-compatible support of Espresso and Databus, we should
* get rid of this conversion as soon as possible. --- Lei, 2016/9/9.
*/
private Map<Partition, Map<String, String>> convertAssignmentPersisted(Resource resource,
IdealState idealState, Map<Partition, Map<String, String>> assignments) {
String stateModelDef = idealState.getStateModelDefRef();
/** Only convert for MasterSlave resources */
if (!stateModelDef.equals(BuiltInStateModelDefinitions.MasterSlave.name()) || idealState
.getRebalanceMode().equals(IdealState.RebalanceMode.FULL_AUTO)) {
return assignments;
}
Map<Partition, Map<String, String>> assignmentToPersist =
new HashMap<Partition, Map<String, String>>();
for (Partition partition : resource.getPartitions()) {
Map<String, String> instanceMap = new HashMap<String, String>();
Map<String, String> assignment = assignments.get(partition);
if (assignment != null) {
instanceMap.putAll(assignment);
}
List<String> preferenceList = idealState.getPreferenceList(partition.getPartitionName());
if (preferenceList == null) {
preferenceList = Collections.emptyList();
}
Set<String> nodeList = new HashSet<String>(preferenceList);
nodeList.addAll(assignment.keySet());
boolean hasMaster = false;
for (String ins : nodeList) {
String state = instanceMap.get(ins);
if (state == null || (!state.equals(MasterSlaveSMD.States.SLAVE.name()) && !state
.equals(MasterSlaveSMD.States.MASTER.name()))) {
instanceMap.put(ins, MasterSlaveSMD.States.SLAVE.name());
}
if (state != null && state.equals(MasterSlaveSMD.States.MASTER.name())) {
hasMaster = true;
}
}
// if no master, just pick the first node in the preference list as the master.
if (!hasMaster && preferenceList.size() > 0) {
instanceMap.put(preferenceList.get(0), MasterSlaveSMD.States.MASTER.name());
}
assignmentToPersist.put(partition, instanceMap);
}
return assignmentToPersist;
}
示例8: process
import org.apache.helix.model.IdealState; //导入方法依赖的package包/类
@Override
public void process(ClusterEvent event) throws Exception {
ClusterDataCache cache = event.getAttribute(AttributeName.ClusterDataCache.name());
if (cache == null) {
throw new StageException("Missing attributes in event:" + event + ". Requires DataCache");
}
Map<String, Resource> resourceMap = event.getAttribute(AttributeName.RESOURCES.name());
if (resourceMap == null) {
throw new StageException("Resources must be computed prior to validation!");
}
Map<String, IdealState> idealStateMap = cache.getIdealStates();
Map<String, Map<String, String>> idealStateRuleMap = cache.getIdealStateRules();
for (String resourceName : idealStateMap.keySet()) {
// check every ideal state against the ideal state rules
// the pipeline should not process any resources that have an unsupported ideal state
IdealState idealState = idealStateMap.get(resourceName);
if (!idealStateRuleMap.isEmpty()) {
boolean hasMatchingRule = false;
for (String ruleName : idealStateRuleMap.keySet()) {
Map<String, String> rule = idealStateRuleMap.get(ruleName);
boolean matches = idealStateMatchesRule(idealState, rule);
hasMatchingRule = hasMatchingRule || matches;
if (matches) {
break;
}
}
if (!hasMatchingRule) {
LOG.warn("Resource " + resourceName + " does not have a valid ideal state!");
resourceMap.remove(resourceName);
}
}
// check that every resource to process has a live state model definition
String stateModelDefRef = idealState.getStateModelDefRef();
StateModelDefinition stateModelDef = cache.getStateModelDef(stateModelDefRef);
if (stateModelDef == null) {
LOG.warn("Resource " + resourceName + " uses state model " + stateModelDefRef
+ ", but it is not on the cluster!");
resourceMap.remove(resourceName);
}
}
}