static Cluster buildCluster(Cassandra3Storage cassandra) { Cluster.Builder builder = Cluster.builder(); List<InetSocketAddress> contactPoints = parseContactPoints(cassandra); int defaultPort = findConnectPort(contactPoints); builder.addContactPointsWithPorts(contactPoints); builder.withPort(defaultPort); // This ends up protocolOptions.port if (cassandra.username != null && cassandra.password != null) { builder.withCredentials(cassandra.username, cassandra.password); } builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE); builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder( cassandra.localDc != null ? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build() : new RoundRobinPolicy() // This can select remote, but LatencyAwarePolicy will prefer local ).build())); builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost( HostDistance.LOCAL, cassandra.maxConnections )); return builder.build(); }
static Cluster buildCluster(CassandraStorage cassandra) { Cluster.Builder builder = Cluster.builder(); List<InetSocketAddress> contactPoints = parseContactPoints(cassandra); int defaultPort = findConnectPort(contactPoints); builder.addContactPointsWithPorts(contactPoints); builder.withPort(defaultPort); // This ends up protocolOptions.port if (cassandra.username != null && cassandra.password != null) { builder.withCredentials(cassandra.username, cassandra.password); } builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE); builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder( cassandra.localDc != null ? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build() : new RoundRobinPolicy() // This can select remote, but LatencyAwarePolicy will prefer local ).build())); builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost( HostDistance.LOCAL, cassandra.maxConnections )); return builder.build(); }
@Override public LoadBalancingPolicy build() { LatencyAwarePolicy.Builder builder = LatencyAwarePolicy.builder(subPolicy.build()); if (exclusionThreshold != null) { builder.withExclusionThreshold(exclusionThreshold); } if (minimumMeasurements != null) { builder.withMininumMeasurements(minimumMeasurements); } if (retryPeriod != null) { builder.withRetryPeriod(retryPeriod.getQuantity(), retryPeriod.getUnit()); } if (scale != null) { builder.withScale(scale.getQuantity(), scale.getUnit()); } if (updateRate != null) { builder.withUpdateRate(updateRate.getQuantity(), updateRate.getUnit()); } return builder.build(); }
RoundRobinPolicy toRoundRobinPolicy(Cassandra3Storage storage) { return (RoundRobinPolicy) ((LatencyAwarePolicy) ((TokenAwarePolicy) buildCluster(storage) .getConfiguration() .getPolicies() .getLoadBalancingPolicy()) .getChildPolicy()).getChildPolicy(); }
DCAwareRoundRobinPolicy toDCAwareRoundRobinPolicy(Cassandra3Storage storage) { return (DCAwareRoundRobinPolicy) ((LatencyAwarePolicy) ((TokenAwarePolicy) buildCluster(storage) .getConfiguration() .getPolicies() .getLoadBalancingPolicy()) .getChildPolicy()).getChildPolicy(); }
RoundRobinPolicy toRoundRobinPolicy(CassandraStorage storage) { return (RoundRobinPolicy) ((LatencyAwarePolicy) ((TokenAwarePolicy) buildCluster(storage) .getConfiguration() .getPolicies() .getLoadBalancingPolicy()) .getChildPolicy()).getChildPolicy(); }
DCAwareRoundRobinPolicy toDCAwareRoundRobinPolicy(CassandraStorage storage) { return (DCAwareRoundRobinPolicy) ((LatencyAwarePolicy) ((TokenAwarePolicy) buildCluster(storage) .getConfiguration() .getPolicies() .getLoadBalancingPolicy()) .getChildPolicy()).getChildPolicy(); }
@Test public void testLoadBalancingPolicyParsing() throws Exception { String lbPolicyStr = "RoundRobinPolicy()"; System.out.println(lbPolicyStr); assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof RoundRobinPolicy); System.out.println("===================="); lbPolicyStr = "TokenAwarePolicy(RoundRobinPolicy())"; System.out.println(lbPolicyStr); assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof TokenAwarePolicy); System.out.println("===================="); lbPolicyStr = "DCAwareRoundRobinPolicy(\"dc1\")"; System.out.println(lbPolicyStr); assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof DCAwareRoundRobinPolicy); System.out.println("===================="); lbPolicyStr = "TokenAwarePolicy(DCAwareRoundRobinPolicy(\"dc1\"))"; System.out.println(lbPolicyStr); assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof TokenAwarePolicy); System.out.println("===================="); lbPolicyStr = "TokenAwarePolicy"; System.out.println(lbPolicyStr); assertTrue(Utils.parseLbPolicy(lbPolicyStr)==null); System.out.println("===================="); lbPolicyStr = "LatencyAwarePolicy(TokenAwarePolicy(RoundRobinPolicy()),(double) 10.5,(long) 1,(long) 10,(long)1,10)"; System.out.println(lbPolicyStr); assertTrue(Utils.parseLbPolicy(lbPolicyStr) instanceof LatencyAwarePolicy); System.out.println("===================="); }
@Before public void setUp() throws Exception { when(subPolicyFactory.build()).thenReturn(subPolicy); mockStatic(LatencyAwarePolicy.class); when(LatencyAwarePolicy.builder(subPolicy)).thenReturn(policyBuilder); when(policyBuilder.build()).thenReturn(resultingPolicy); }
private static Cluster build(List<String> addresses, int port) { Cluster.Builder builder = Cluster.builder(); for (String address : addresses) { builder.addContactPoint(address); } builder .withPort(port) .withCompression(Compression.LZ4) .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE) .withLoadBalancingPolicy(LatencyAwarePolicy.builder(new RoundRobinPolicy()).build()); Cluster cluster = builder.build(); try { // Attempt to init the cluster to make sure it's usable. I'd prefer to remove this and leave it on the // client to retry when the connect method throws an exception. cluster.init(); return cluster; } catch(NoHostAvailableException e) { LOGGER.warn("Unable to connect to Cassandra, will retry contact points next time", cluster, e); cluster = builder.build(); cluster.init(); } return cluster; }