// the leader assignment magic in the loop below will work.
for (Map.Entry<Integer, ArrayList<Integer>> e : partToHosts.entrySet()) {
Collections.sort(e.getValue());
}
JSONStringer stringer = new JSONStringer();
stringer.object();
stringer.key("hostcount").value(m_hostCount);
stringer.key("kfactor").value(getReplicationFactor());
stringer.key("sites_per_host").value(sitesPerHost);
stringer.key("partitions").array();
for (int part = 0; part < partitionCount; part++)
{
stringer.object();
stringer.key("partition_id").value(part);
// This two-line magic deterministically spreads the partition leaders
// evenly across the cluster at startup.
int index = part % (getReplicationFactor() + 1);
int master = partToHosts.get(part).get(index);
stringer.key("master").value(master);
stringer.key("replicas").array();
for (int host_pos : partToHosts.get(part)) {
stringer.value(host_pos);
}
stringer.endArray();
stringer.endObject();
}
stringer.endArray();
stringer.endObject();
JSONObject topo = new JSONObject(stringer.toString());
return topo;
}