Github user ifesdjeen commented on a diff in the pull request:

    https://github.com/apache/cassandra/pull/224#discussion_r197125691
  
    --- Diff: src/java/org/apache/cassandra/db/ConsistencyLevel.java ---
    @@ -190,50 +197,50 @@ public int 
countLocalEndpoints(Iterable<InetAddressAndPort> liveEndpoints)
              * the blockFor first ones).
              */
             if (isDCLocal)
    -            liveEndpoints.sort(DatabaseDescriptor.getLocalComparator());
    +            liveReplicas.sort(DatabaseDescriptor.getLocalComparator());
     
    -        return liveEndpoints.subList(0, Math.min(liveEndpoints.size(), 
blockFor(keyspace)));
    +        return liveReplicas.subList(0, Math.min(liveReplicas.size(), 
blockFor(keyspace)));
         }
     
    -    private List<InetAddressAndPort> filterForEachQuorum(Keyspace 
keyspace, List<InetAddressAndPort> liveEndpoints)
    +    private ReplicaList filterForEachQuorum(Keyspace keyspace, ReplicaList 
liveReplicas)
    --- End diff --
    
    Here, the sorted variant is not preserved (even though we return a sorted 
collection from `filterForQuery`).
    
    We could retain sorted invariant if we used `filter`, maybe something like:
    
    ```
        private ReplicaList filterForEachQuorum(Keyspace keyspace, ReplicaList 
liveReplicas)
        {
            NetworkTopologyStrategy strategy = (NetworkTopologyStrategy) 
keyspace.getReplicationStrategy();
            Map<String, Integer> dcsReplicas = new HashMap<>();
            for (String dc : strategy.getDatacenters())
            {
                // we put _up to_ dc replicas only
                dcsReplicas.put(dc, localQuorumFor(keyspace, dc));
            }
    
            return liveReplicas.filter((replica) -> {
                String dc = 
DatabaseDescriptor.getEndpointSnitch().getDatacenter(replica);
                int replicas = dcsReplicas.get(dc);
                if (replicas > 0)
                {
                    dcsReplicas.put(dc, --replicas);
                    return true;
                }
                return false;
            });
        }
    ```
    
    (also would avoid three iterations in favour of just two).


---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to