in helix-core/src/main/java/org/apache/helix/tools/commandtools/YAMLClusterSetup.java [179:256]
private static StateModelDefinition getStateModelDef(StateModelConfig stateModel,
ConstraintsConfig constraints) {
// Use a builder to define the state model
StateModelDefinition.Builder builder = new StateModelDefinition.Builder(stateModel.name);
if (stateModel.states == null || stateModel.states.size() == 0) {
throw new HelixException("List of states are required in a state model!");
}
Set<String> stateSet = new HashSet<String>(stateModel.states);
if (stateModel.initialState == null) {
throw new HelixException("Initial state is required in a state model!");
} else if (!stateSet.contains(stateModel.initialState)) {
throw new HelixException("Initial state is not a valid state");
}
builder.initialState(stateModel.initialState);
// Build a helper for state priorities
Map<String, Integer> statePriorities = new HashMap<String, Integer>();
if (constraints != null && constraints.state != null && constraints.state.priorityList != null) {
int statePriority = 0;
for (String state : constraints.state.priorityList) {
if (!stateSet.contains(state)) {
throw new HelixException("State " + state
+ " in the state priority list is not in the state list!");
}
statePriorities.put(state, statePriority);
statePriority++;
}
}
// Add states, set state priorities
for (String state : stateModel.states) {
if (statePriorities.containsKey(state)) {
builder.addState(state, statePriorities.get(state));
} else {
builder.addState(state);
}
}
// Set state counts
for (Map<String, String> counts : constraints.state.counts) {
String state = counts.get("name");
if (!stateSet.contains(state)) {
throw new HelixException("State " + state + " has a count, but not in the state list!");
}
builder.dynamicUpperBound(state, counts.get("count"));
}
// Build a helper for transition priorities
Map<String, Integer> transitionPriorities = new HashMap<String, Integer>();
if (constraints != null && constraints.transition != null
&& constraints.transition.priorityList != null) {
int transitionPriority = 0;
for (String transition : constraints.transition.priorityList) {
transitionPriorities.put(transition, transitionPriority);
transitionPriority++;
}
}
// Add the transitions
if (stateModel.transitions == null || stateModel.transitions.size() == 0) {
throw new HelixException("Transitions are required!");
}
for (Map<String, String> transitions : stateModel.transitions) {
String name = transitions.get("name");
String from = transitions.get("from");
String to = transitions.get("to");
if (name == null || from == null || to == null) {
throw new HelixException("All transitions must have a name, a from state, and a to state");
}
if (transitionPriorities.containsKey(name)) {
builder.addTransition(from, to, transitionPriorities.get(name));
} else {
builder.addTransition(from, to);
}
}
return builder.build();
}