public static ZNRecord calculateIdealState(
List<List<String>> instanceClusters,
List<Integer> instanceClusterWeights, int partitions, int replicas,
String dbName) throws Exception
{
ZNRecord result = new ZNRecord(dbName);
int numberOfClusters = instanceClusters.size();
List<List<String>> nodesInClusters = instanceClusters;
List<Integer> clusterWeights = instanceClusterWeights;
HashMap<String, Object> rushConfig = buildRushConfig(numberOfClusters,
nodesInClusters, replicas + 1, clusterWeights);
RUSHrHash rushHash = new RUSHrHash(rushConfig);
Random r = new Random(0);
for (int i = 0; i < partitions; i++)
{
int partitionId = i;
String partitionName = dbName + ".partition-" + partitionId;
ArrayList<HashMap> partitionAssignmentResult = rushHash
.findNode(i);
List<String> nodeNames = new ArrayList<String>();
for (HashMap<?, ?> p : partitionAssignmentResult)
{
for (Object key : p.keySet())
{
if (p.get(key) instanceof String)
{
nodeNames.add(p.get(key).toString());
}
}
}
Map<String, String> partitionAssignment = new TreeMap<String, String>();
for (int j = 0; j < nodeNames.size(); j++)
{
partitionAssignment.put(nodeNames.get(j), "SLAVE");
}
int master = r.nextInt(nodeNames.size());
//master = nodeNames.size()/2;
partitionAssignment.put(nodeNames.get(master), "MASTER");
result.setMapField(partitionName, partitionAssignment);
}
result.setSimpleField(IdealStateProperty.NUM_PARTITIONS.toString(), String.valueOf(partitions));
return result;
}