Collections.sort(instanceNames);
IdealState idealState = getResourceIdealState(clusterName, resourceName);
if (idealState == null)
{
throw new HelixException("Resource: " + resourceName + " has NOT been added yet");
}
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;