Buggy code:
public class DatacenterShardStrategy extends AbstractReplicationStrategy { private static final String DATACENTER_PROPERTY_FILENAME = "datacenters.properties" ; private AbstractRackAwareSnitch snitch ; private volatile Map < String , Map < String , Integer > > datacenters ; private static final Logger logger = LoggerFactory . getLogger ( DatacenterShardStrategy . class ) ; public DatacenterShardStrategy ( TokenMetadata tokenMetadata , IEndpointSnitch snitch ) throws ConfigurationException { } public synchronized void reloadConfiguration ( ) throws ConfigurationException { } public Set < InetAddress > calculateNaturalEndpoints ( Token searchToken , TokenMetadata tokenMetadata , String table ) { int totalReplicas = getReplicationFactor ( table ) ; Map < String , Integer > remainingReplicas = new HashMap < String , Integer > ( datacenters . get ( table ) ) ; Map < String , Set < String > > dcUsedRacks = new HashMap < String , Set < String > > ( ) ; <START_BUG> Set < InetAddress > endpoints = new HashSet < InetAddress > ( totalReplicas ) ; <END_BUG> for ( Iterator < Token > iter = TokenMetadata . ringIterator ( tokenMetadata . sortedTokens ( ) , searchToken ) ; ( ( endpoints . size ( ) ) < totalReplicas ) && ( iter . hasNext ( ) ) ; ) { Token token = iter . next ( ) ; InetAddress endpoint = tokenMetadata . getEndpoint ( token ) ; String datacenter = snitch . getDatacenter ( endpoint ) ; int remaining = ( remainingReplicas . containsKey ( datacenter ) ) ? remainingReplicas . get ( datacenter ) : 0 ; if ( remaining > 0 ) { Set < String > usedRacks = dcUsedRacks . get ( datacenter ) ; if ( usedRacks == null ) { usedRacks = new HashSet < String > ( ) ; dcUsedRacks . put ( datacenter , usedRacks ) ; } String rack = snitch . getRack ( endpoint ) ; if ( ! ( usedRacks . contains ( rack ) ) ) { endpoints . add ( endpoint ) ; usedRacks . add ( rack ) ; remainingReplicas . put ( datacenter , ( remaining - 1 ) ) ; } } } for ( Iterator < Token > iter = TokenMetadata . ringIterator ( tokenMetadata . sortedTokens ( ) , searchToken ) ; ( ( endpoints . size ( ) ) < totalReplicas ) && ( iter . hasNext ( ) ) ; ) { Token token = iter . next ( ) ; InetAddress endpoint = tokenMetadata . getEndpoint ( token ) ; if ( endpoints . contains ( endpoint ) ) continue ; String datacenter = snitch . getDatacenter ( endpoint ) ; int remaining = ( remainingReplicas . containsKey ( datacenter ) ) ? remainingReplicas . get ( datacenter ) : 0 ; if ( remaining > 0 ) { endpoints . add ( endpoint ) ; remainingReplicas . put ( datacenter , ( remaining - 1 ) ) ; } } for ( Map . Entry < String , Integer > entry : remainingReplicas . entrySet ( ) ) { if ( ( entry . getValue ( ) ) > 0 ) throw new IllegalStateException ( String . format ( "datacenter<seq2seq4repair_space>(%s)<seq2seq4repair_space>has<seq2seq4repair_space>no<seq2seq4repair_space>more<seq2seq4repair_space>endpoints,<seq2seq4repair_space>(%s)<seq2seq4repair_space>replicas<seq2seq4repair_space>still<seq2seq4repair_space>needed" , entry . getKey ( ) , entry . getValue ( ) ) ) ; } return endpoints ; } public int getReplicationFactor ( String table ) { } public int getReplicationFactor ( String dc , String table ) { } public Set < String > getDatacenters ( String table ) { } @ Override public AbstractWriteResponseHandler getWriteResponseHandler ( Collection < InetAddress > writeEndpoints , Multimap < InetAddress , InetAddress > hintedEndpoints , ConsistencyLevel consistency_level , String table ) { } @ Override public QuorumResponseHandler getQuorumResponseHandler ( IResponseResolver responseResolver , ConsistencyLevel consistencyLevel , String table ) { } }

Fix:
Set < InetAddress > endpoints = new LinkedHashSet < InetAddress > ( totalReplicas ) ;