HostSelectionWithFallback { int calculateReplicationFactor(List<HostToken> allHostTokens) { return HostUtils.calculateReplicationFactorForDC(allHostTokens, null, localRack); } HostSelectionWithFallback(ConnectionPoolConfiguration config, ConnectionPoolMonitor monitor); Connection<CL> getConnection(BaseOperation<CL, ?> op, int duration, TimeUnit unit); Connection<CL> getConnectionUsingRetryPolicy(BaseOperation<CL, ?> op, int duration, TimeUnit unit, RetryPolicy retry); Collection<Connection<CL>> getConnectionsToRing(TokenRackMapper tokenRackMapper, int duration, TimeUnit unit); void initWithHosts(Map<Host, HostConnectionPool<CL>> hPools); void addHost(Host host, HostConnectionPool<CL> hostPool); void removeHost(Host host); TokenPoolTopology createTokenPoolTopology(List<HostToken> allHostTokens); TokenPoolTopology getTokenPoolTopology(); Long getTokenForKey(String key); @Override String toString();  }
@Test public void testReplicationFactorOf3WithDupes() { cpConfig.setLoadBalancingStrategy(LoadBalancingStrategy.TokenAware); cpConfig.withTokenSupplier(getTokenMapSupplier()); HostSelectionWithFallback<Integer> selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); List<HostToken> hostTokens = Arrays.asList( new HostToken(1383429731L, new HostBuilder().setHostname("host-1").setPort(-1).setRack(rack1).createHost()), new HostToken(2815085496L, new HostBuilder().setHostname("host-2").setPort(-1).setRack(rack1).createHost()), new HostToken(4246741261L, new HostBuilder().setHostname("host-3").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-4").setPort(-1).setRack(rack1).createHost()), new HostToken(2815085496L, new HostBuilder().setHostname("host-5").setPort(-1).setRack(rack1).createHost()), new HostToken(4246741261L, new HostBuilder().setHostname("host-6").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-7").setPort(-1).setRack(rack1).createHost()), new HostToken(2815085496L, new HostBuilder().setHostname("host-8").setPort(-1).setRack(rack1).createHost()), new HostToken(4246741261L, new HostBuilder().setHostname("host-9").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-7").setPort(-1).setRack(rack1).createHost()), new HostToken(2815085496L, new HostBuilder().setHostname("host-8").setPort(-1).setRack(rack1).createHost()), new HostToken(4246741261L, new HostBuilder().setHostname("host-9").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-7").setPort(-1).setRack(rack1).createHost()), new HostToken(2815085496L, new HostBuilder().setHostname("host-8").setPort(-1).setRack(rack1).createHost()), new HostToken(4246741261L, new HostBuilder().setHostname("host-9").setPort(-1).setRack(rack1).createHost()) ); int rf = selection.calculateReplicationFactor(hostTokens); Assert.assertEquals(3, rf); }
@Test public void testReplicationFactorOf3() { cpConfig.setLocalRack("us-east-1c"); cpConfig.setLoadBalancingStrategy(LoadBalancingStrategy.TokenAware); cpConfig.withTokenSupplier(getTokenMapSupplier()); HostSelectionWithFallback<Integer> selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); List<HostToken> hostTokens = Arrays.asList( new HostToken(1111L, new HostBuilder().setHostname("host-1").setPort(-1).setRack(rack1).createHost()), new HostToken(1111L, new HostBuilder().setHostname("host-2").setPort(-1).setRack(rack1).createHost()), new HostToken(1111L, new HostBuilder().setHostname("host-3").setPort(-1).setRack(rack1).createHost()), new HostToken(2222L, new HostBuilder().setHostname("host-4").setPort(-1).setRack(rack1).createHost()), new HostToken(2222L, new HostBuilder().setHostname("host-5").setPort(-1).setRack(rack1).createHost()), new HostToken(2222L, new HostBuilder().setHostname("host-6").setPort(-1).setRack(rack1).createHost()) ); int rf = selection.calculateReplicationFactor(hostTokens); Assert.assertEquals(3, rf); }
@Test public void testReplicationFactorOf2() { cpConfig.setLocalRack(rack1); cpConfig.setLoadBalancingStrategy(LoadBalancingStrategy.TokenAware); cpConfig.withTokenSupplier(getTokenMapSupplier()); HostSelectionWithFallback<Integer> selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); List<HostToken> hostTokens = Arrays.asList( new HostToken(1111L, new HostBuilder().setHostname("host-1").setPort(-1).setRack(rack1).createHost()), new HostToken(1111L, new HostBuilder().setHostname("host-2").setPort(-1).setRack(rack1).createHost()), new HostToken(2222L, new HostBuilder().setHostname("host-4").setPort(-1).setRack(rack1).createHost()), new HostToken(2222L, new HostBuilder().setHostname("host-5").setPort(-1).setRack(rack1).createHost()) ); int rf = selection.calculateReplicationFactor(hostTokens); Assert.assertEquals(2, rf); }
@Test public void testReplicationFactorOf1() { cpConfig.setLoadBalancingStrategy(LoadBalancingStrategy.TokenAware); cpConfig.withTokenSupplier(getTokenMapSupplier()); HostSelectionWithFallback<Integer> selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); List<HostToken> hostTokens = Arrays.asList( new HostToken(1111L, h1), new HostToken(2222L, h2), new HostToken(3333L, h3), new HostToken(4444L, h4) ); int rf = selection.calculateReplicationFactor(hostTokens); Assert.assertEquals(1, rf); }
@Test(expected = RuntimeException.class) public void testIllegalReplicationFactor() { cpConfig.setLocalRack("us-east-1c"); cpConfig.setLoadBalancingStrategy(LoadBalancingStrategy.TokenAware); cpConfig.withTokenSupplier(getTokenMapSupplier()); HostSelectionWithFallback<Integer> selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); List<HostToken> hostTokens = Arrays.asList( new HostToken(1111L, new HostBuilder().setHostname("host-1").setPort(-1).setRack(rack1).createHost()), new HostToken(1111L, new HostBuilder().setHostname("host-2").setPort(-1).setRack(rack1).createHost()), new HostToken(2222L, new HostBuilder().setHostname("host-4").setPort(-1).setRack(rack1).createHost()) ); selection.calculateReplicationFactor(hostTokens); }
@Test public void testReplicationFactorForMultiRegionCluster() { cpConfig.setLocalRack("us-east-1d"); cpConfig.setLoadBalancingStrategy(LoadBalancingStrategy.TokenAware); cpConfig.withTokenSupplier(getTokenMapSupplier()); HostSelectionWithFallback<Integer> selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); List<HostToken> hostTokens = Arrays.asList( new HostToken(3530913378L, new HostBuilder().setHostname("host-1").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-1").setPort(-1).setRack(rack1).createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-2").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-2").setPort(-1).setRack(rack1).createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-3").setPort(-1).setRack(rack1).createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-3").setPort(-1).setRack(rack1).createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-4").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-4").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-5").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-5").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-6").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-6").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-7").setPort(-1).setRack("remoteRack2").createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-7").setPort(-1).setRack("remoteRack2").createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-8").setPort(-1).setRack("remoteRack2").createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-8").setPort(-1).setRack("remoteRack1").createHost()), new HostToken(3530913378L, new HostBuilder().setHostname("host-9").setPort(-1).setRack("remoteRack2").createHost()), new HostToken(1383429731L, new HostBuilder().setHostname("host-9").setPort(-1).setRack("remoteRack2").createHost()) ); int rf = selection.calculateReplicationFactor(hostTokens); Assert.assertEquals(3, rf); cpConfig.setLocalRack(null); selection = new HostSelectionWithFallback<Integer>(cpConfig, cpMonitor); rf = selection.calculateReplicationFactor(hostTokens); Assert.assertEquals(3, rf); }