in src/main/java/org/apache/cassandra/distributed/shared/AbstractBuilder.java [454:499]
private void setRacks()
{
if (nodeIdTopology == null)
nodeIdTopology = new HashMap<>();
boolean shouldCalculatePerRackCount = false;
boolean hasExplicitPerRackCount = false;
for (Rack rack : racks)
{
if (rack.rackNodeCount == -1)
shouldCalculatePerRackCount = true;
else
hasExplicitPerRackCount = true;
}
if (shouldCalculatePerRackCount && hasExplicitPerRackCount)
throw new IllegalStateException("Can't mix explicit and implicit per rack counts");
int nodeId = nodeIdTopology.isEmpty() ? 1 : Collections.max(nodeIdTopology.keySet()) + 1;
if (shouldCalculatePerRackCount)
{
if (nodeCount == 0)
throw new IllegalStateException("Node count must be set when not setting per rack counts");
int totalRacks = racks.size();
int nodesPerRack = (nodeCount + totalRacks - 1) / totalRacks;
for (Rack rack : racks)
for (int i = 1; i <= nodesPerRack; i++)
nodeIdTopology.put(nodeId++, NetworkTopology.dcAndRack(rack.dcName, rack.rackName));
}
else
{
for (Rack rack : racks)
for (int i = 1; i <= rack.rackNodeCount; i++)
nodeIdTopology.put(nodeId++, NetworkTopology.dcAndRack(rack.dcName, rack.rackName));
}
if (nodeCount != nodeIdTopology.size())
{
assert nodeIdTopology.size() > nodeCount : "withRacks should only ever increase the node count";
if (nodeCount == 0)
nodeCount = nodeIdTopology.size();
else if (logTopology())
System.out.printf("Network topology of %s requires more nodes, only starting %s out of %s configured nodes%n", nodeIdTopology, nodeCount, nodeIdTopology.size());
}
}