WeightedHosts { public static WeightedHosts create(Map<InetSocketAddress, CassandraClientPoolingContainer> pools) { Preconditions.checkArgument(!pools.isEmpty(), "pools should be non-empty"); return new WeightedHosts(buildHostsWeightedByActiveConnections(pools)); } private  WeightedHosts(NavigableMap<Integer, InetSocketAddress> hosts); static WeightedHosts create(Map<InetSocketAddress, CassandraClientPoolingContainer> pools); InetSocketAddress getRandomHost();  }
@Test public void testWeightedHostsWithUniformActivity() { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of( new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(10), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(10), new InetSocketAddress(2), createMockClientPoolingContainerWithUtilization(10)); NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts; int expectedWeight = result.firstEntry().getKey(); int prevKey = 0; for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) { int currWeight = entry.getKey() - prevKey; assertEquals(expectedWeight, currWeight); prevKey = entry.getKey(); } }
@Test public void testWeightedHostsWithLowActivityPool() { InetSocketAddress lowActivityHost = new InetSocketAddress(2); Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of( new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(10), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(10), lowActivityHost, createMockClientPoolingContainerWithUtilization(0)); NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts; int largestWeight = result.firstEntry().getKey(); InetSocketAddress hostWithLargestWeight = result.firstEntry().getValue(); int prevKey = 0; for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) { int currWeight = entry.getKey() - prevKey; prevKey = entry.getKey(); if (currWeight > largestWeight) { largestWeight = currWeight; hostWithLargestWeight = entry.getValue(); } } assertEquals(lowActivityHost, hostWithLargestWeight); }
@Test public void testWeightedHostsWithMaxActivityPool() { InetSocketAddress highActivityHost = new InetSocketAddress(2); Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of( new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(5), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(5), highActivityHost, createMockClientPoolingContainerWithUtilization(20)); NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts; int smallestWeight = result.firstEntry().getKey(); InetSocketAddress hostWithSmallestWeight = result.firstEntry().getValue(); int prevKey = 0; for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) { int currWeight = entry.getKey() - prevKey; prevKey = entry.getKey(); if (currWeight < smallestWeight) { smallestWeight = currWeight; hostWithSmallestWeight = entry.getValue(); } } assertEquals(highActivityHost, hostWithSmallestWeight); }
@Test public void testWeightedHostsWithNonZeroWeights() { Map<InetSocketAddress, CassandraClientPoolingContainer> pools = ImmutableMap.of( new InetSocketAddress(0), createMockClientPoolingContainerWithUtilization(5), new InetSocketAddress(1), createMockClientPoolingContainerWithUtilization(10), new InetSocketAddress(2), createMockClientPoolingContainerWithUtilization(15)); NavigableMap<Integer, InetSocketAddress> result = WeightedHosts.create(pools).hosts; int prevKey = 0; for (Map.Entry<Integer, InetSocketAddress> entry : result.entrySet()) { int currWeight = entry.getKey() - prevKey; assertThat(currWeight, Matchers.greaterThan(0)); prevKey = entry.getKey(); } }