public static ClusterView generateStaticConfigClusterView(String[] nodesInfo,
List<DBParam> dbParams, int replica)
{
// create mock cluster view
ClusterView view = new ClusterView();
// add nodes
List<ZNRecord> nodeConfigList = new ArrayList<ZNRecord>();
List<String> instanceNames = new ArrayList<String>();
Arrays.sort(nodesInfo, new Comparator<String>() {
@Override
public int compare(String str1, String str2)
{
return str1.compareTo(str2);
}
});
// set CONFIGS
for (String nodeInfo : nodesInfo)
{
int lastPos = nodeInfo.lastIndexOf(":");
if (lastPos == -1)
{
throw new IllegalArgumentException("nodeInfo should be in format of host:port, " + nodeInfo);
}
String host = nodeInfo.substring(0, lastPos);
String port = nodeInfo.substring(lastPos + 1);
String nodeId = host + "_" + port;
ZNRecord nodeConfig = new ZNRecord(nodeId);
nodeConfig.setSimpleField(InstanceConfigProperty.HELIX_ENABLED.toString(),
Boolean.toString(true));
nodeConfig.setSimpleField(InstanceConfigProperty.HELIX_HOST.toString(), host);
nodeConfig.setSimpleField(InstanceConfigProperty.HELIX_PORT.toString(), port);
instanceNames.add(nodeId);
nodeConfigList.add(nodeConfig);
}
view.setClusterPropertyList(PropertyType.CONFIGS, nodeConfigList);
// set IDEALSTATES
// compute ideal states for each db
List<ZNRecord> idealStates = new ArrayList<ZNRecord>();
for (DBParam dbParam : dbParams)
{
ZNRecord result = IdealStateCalculatorByShuffling.calculateIdealState(instanceNames,
dbParam.partitions, replica, dbParam.name);
idealStates.add(result);
}
view.setClusterPropertyList(PropertyType.IDEALSTATES, idealStates);
// calculate messages for transition using naive algorithm
Map<String, List<ZNRecord>> msgListForInstance = new HashMap<String, List<ZNRecord>>();
List<ZNRecord> idealStatesArray = view.getPropertyList(PropertyType.IDEALSTATES);
for (ZNRecord idealStateRecord : idealStatesArray)
{
// IdealState idealState = new IdealState(idealStateRecord);
List<Message> messages = computeMessagesForSimpleTransition(idealStateRecord);
for (Message message : messages)
{
// logger.info("Sending message to " + message.getTgtName() +
// " transition "
// + message.getStateUnitKey() + " from:" +
// message.getFromState() +
// " to:"
// + message.getToState());
// client.addMessage(message, message.getTgtName());
String instance = message.getTgtName();
List<ZNRecord> msgList = msgListForInstance.get(instance);
if (msgList == null)
{
msgList = new ArrayList<ZNRecord>();
msgListForInstance.put(instance, msgList);
}
// msgList.add(message);
addMessageInOrder(msgList, message);
}
}
// set INSTANCES
// put message lists into cluster view
List<ClusterView.MemberInstance> insList = new ArrayList<ClusterView.MemberInstance>();
for (Map.Entry<String, List<ZNRecord>> entry : msgListForInstance.entrySet())
{
String instance = entry.getKey();
List<ZNRecord> msgList = entry.getValue();
ClusterView.MemberInstance ins = view.getMemberInstance(instance, true);
ins.setInstanceProperty(PropertyType.MESSAGES, msgList);
// ins.setInstanceProperty(InstancePropertyType.CURRENTSTATES,
// null);
// ins.setInstanceProperty(InstancePropertyType.ERRORS, null);
// ins.setInstanceProperty(InstancePropertyType.STATUSUPDATES,
// null);
insList.add(ins);
}
// sort it
ClusterView.MemberInstance[] insArray = new ClusterView.MemberInstance[insList.size()];
insArray = insList.toArray(insArray);
Arrays.sort(insArray, new Comparator<ClusterView.MemberInstance>() {
@Override
public int compare(ClusterView.MemberInstance ins1, ClusterView.MemberInstance ins2)
{
return ins1.getInstanceName().compareTo(ins2.getInstanceName());
}
});
insList = Arrays.asList(insArray);
view.setInstances(insList);
return view;
}