/** * The key for a host is the open upper bound of the weight. Since the domain is intended to be contiguous, the * closed lower bound of that weight is the key of the previous entry. * <p> * The closed lower bound of the first entry is 0. * <p> * Every weight is guaranteed to be non-zero in size. That is, every key is guaranteed to be at least one larger * than the previous key. */ private static NavigableMap<Integer, InetSocketAddress> buildHostsWeightedByActiveConnections( Map<InetSocketAddress, CassandraClientPoolingContainer> pools) { Map<InetSocketAddress, Integer> openRequestsByHost = new HashMap<>(pools.size()); int totalOpenRequests = 0; for (Map.Entry<InetSocketAddress, CassandraClientPoolingContainer> poolEntry : pools.entrySet()) { int openRequests = Math.max(poolEntry.getValue().getOpenRequests(), 0); openRequestsByHost.put(poolEntry.getKey(), openRequests); totalOpenRequests += openRequests; } int lowerBoundInclusive = 0; NavigableMap<Integer, InetSocketAddress> weightedHosts = new TreeMap<>(); for (Map.Entry<InetSocketAddress, Integer> entry : openRequestsByHost.entrySet()) { // We want the weight to be inversely proportional to the number of open requests so that we pick // less-active hosts. We add 1 to make sure that all ranges are non-empty int weight = totalOpenRequests - entry.getValue() + 1; weightedHosts.put(lowerBoundInclusive + weight, entry.getKey()); lowerBoundInclusive += weight; } return weightedHosts; }
private static CassandraClientPoolingContainer createMockClientPoolingContainerWithUtilization(int utilization) { CassandraClientPoolingContainer mock = Mockito.mock(CassandraClientPoolingContainer.class); Mockito.when(mock.getOpenRequests()).thenReturn(utilization); return mock; } }
/** * The key for a host is the open upper bound of the weight. Since the domain is intended to be contiguous, the * closed lower bound of that weight is the key of the previous entry. * <p> * The closed lower bound of the first entry is 0. * <p> * Every weight is guaranteed to be non-zero in size. That is, every key is guaranteed to be at least one larger * than the previous key. */ private static NavigableMap<Integer, InetSocketAddress> buildHostsWeightedByActiveConnections( Map<InetSocketAddress, CassandraClientPoolingContainer> pools) { Map<InetSocketAddress, Integer> openRequestsByHost = new HashMap<>(pools.size()); int totalOpenRequests = 0; for (Map.Entry<InetSocketAddress, CassandraClientPoolingContainer> poolEntry : pools.entrySet()) { int openRequests = Math.max(poolEntry.getValue().getOpenRequests(), 0); openRequestsByHost.put(poolEntry.getKey(), openRequests); totalOpenRequests += openRequests; } int lowerBoundInclusive = 0; NavigableMap<Integer, InetSocketAddress> weightedHosts = new TreeMap<>(); for (Map.Entry<InetSocketAddress, Integer> entry : openRequestsByHost.entrySet()) { // We want the weight to be inversely proportional to the number of open requests so that we pick // less-active hosts. We add 1 to make sure that all ranges are non-empty int weight = totalOpenRequests - entry.getValue() + 1; weightedHosts.put(lowerBoundInclusive + weight, entry.getKey()); lowerBoundInclusive += weight; } return weightedHosts; }