"Replicas must be less than number of storage nodes");
}
Collections.sort(instanceNames);
ZNRecord result = new ZNRecord(dbName);
List<Integer> partitionList = new ArrayList<Integer>(partitions);
for (int i = 0; i < partitions; i++)
{
partitionList.add(new Integer(i));
}
Random rand = new Random(randomSeed);
// Shuffle the partition list
Collections.shuffle(partitionList, rand);
for (int i = 0; i < partitionList.size(); i++)
{
int partitionId = partitionList.get(i);
Map<String, String> partitionAssignment = new TreeMap<String, String>();
int masterNode = i % instanceNames.size();
// the first in the list is the node that contains the master
partitionAssignment.put(instanceNames.get(masterNode), masterValue);
// for the jth replica, we put it on (masterNode + j) % nodes-th
// node
for (int j = 1; j <= replicas; j++)
{
int index = (masterNode + j * partitionList.size()) % instanceNames.size();
while(partitionAssignment.keySet().contains(instanceNames.get(index)))
{
index = (index +1) % instanceNames.size();
}
partitionAssignment
.put(instanceNames.get(index),
slaveValue);
}
String partitionName = dbName + "_" + partitionId;
result.setMapField(partitionName, partitionAssignment);
}
result.setSimpleField(IdealStateProperty.NUM_PARTITIONS.toString(), String.valueOf(partitions));
return result;
}