Set<String> racks = new HashSet<String>();
// first pass: only collect replicas on unique racks
for (Iterator<Token> iter = TokenMetadata.ringIterator(dcTokens.sortedTokens(), searchToken, false);
dcEndpoints.size() < dcReplicas && iter.hasNext(); )
{
Token token = iter.next();
InetAddress endpoint = dcTokens.getEndpoint(token);
String rack = snitch.getRack(endpoint);
if (!racks.contains(rack))
{
dcEndpoints.add(endpoint);
racks.add(rack);
}
}
// second pass: if replica count has not been achieved from unique racks, add nodes from duplicate racks
for (Iterator<Token> iter = TokenMetadata.ringIterator(dcTokens.sortedTokens(), searchToken, false);
dcEndpoints.size() < dcReplicas && iter.hasNext(); )
{
Token token = iter.next();
InetAddress endpoint = dcTokens.getEndpoint(token);
if (!dcEndpoints.contains(endpoint))
dcEndpoints.add(endpoint);
}