public LoadBalancingPolicy getLoadBalancingPolicy() { if (this.loadBalancingPolicyName.equals(TokenAwarePolicy.class.getSimpleName())) { return new TokenAwarePolicy(new RoundRobinPolicy()); } if (this.loadBalancingPolicyName.equals(DCAwareRoundRobinPolicy.class.getSimpleName())) { Builder builder = DCAwareRoundRobinPolicy.builder(); if (StringUtils.isNotBlank(datacenterName)) { builder = builder.withLocalDc(this.datacenterName); } return new TokenAwarePolicy(builder.build()); } throw new IllegalArgumentException("Unknown cassandra load balancing policy " + this.loadBalancingPolicyName); }
loadPolicy = new TokenAwarePolicy(loadPolicy, config.isTokenAwareShuffleReplicas());
m_writeLoadBalancingPolicy = new TokenAwarePolicy((m_clusterConfiguration.getLocalDCName() == null) ? new RoundRobinPolicy() : DCAwareRoundRobinPolicy.builder().withLocalDc(m_clusterConfiguration.getLocalDCName()).build(), TokenAwarePolicy.ReplicaOrdering.TOPOLOGICAL); TokenAwarePolicy readLoadBalancePolicy = new TokenAwarePolicy((m_clusterConfiguration.getLocalDCName() == null) ? new RoundRobinPolicy() : DCAwareRoundRobinPolicy.builder().withLocalDc(m_clusterConfiguration.getLocalDCName()).build(), TokenAwarePolicy.ReplicaOrdering.RANDOM);
/** * The default load balancing policy. * * <p>The default load balancing policy is {@link DCAwareRoundRobinPolicy} with token awareness * (so {@code new TokenAwarePolicy(new DCAwareRoundRobinPolicy())}). * * <p>Note that this policy shuffles the replicas when token awareness is used, see {@link * TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy, boolean)} for an explanation of the * tradeoffs. * * @return the default load balancing policy. */ public static LoadBalancingPolicy defaultLoadBalancingPolicy() { // Note: balancing policies are stateful, so we can't store that in a static or that would screw // thing // up if multiple Cluster instance are started in the same JVM. return new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder().build()); }
loadBalancingPolicy = new TokenAwarePolicy(loadBalancingPolicy);
@Test(groups = "unit") public void should_respect_topological_order() { // given TokenAwarePolicy policy = new TokenAwarePolicy(childPolicy, TOPOLOGICAL); policy.init(cluster, null); // when Iterator<Host> queryPlan = policy.newQueryPlan("keyspace", statement); // then assertThat(queryPlan).containsExactly(host1, host2, host4, host3); }
@Test(groups = "unit") public void should_respect_child_policy_order() { // given TokenAwarePolicy policy = new TokenAwarePolicy(childPolicy, NEUTRAL); policy.init(cluster, null); // when Iterator<Host> queryPlan = policy.newQueryPlan("keyspace", statement); // then assertThat(queryPlan).containsExactly(host2, host1, host4, host3); }
@Test(groups = "unit") public void should_create_random_order() { // given TokenAwarePolicy policy = new TokenAwarePolicy(childPolicy, RANDOM); policy.init(cluster, null); // when Iterator<Host> queryPlan = policy.newQueryPlan("keyspace", statement); // then assertThat(queryPlan).containsOnlyOnce(host1, host2, host3, host4).endsWith(host4, host3); }
@Override public LoadBalancingPolicy build() { return (shuffleReplicas == null) ? new TokenAwarePolicy(subPolicy.build()) : new TokenAwarePolicy(subPolicy.build(), shuffleReplicas); } }
.withPort(ccm.getBinaryPort()) .withNettyOptions(nonQuietClusterCloseOptions) .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) .build();
getConfiguration().getTokenAwareLoadBalancingUsedHostsPerRemoteDc() >= 0) { builder.withLoadBalancingPolicy( new TokenAwarePolicy( new DCAwareRoundRobinPolicy.Builder() .withLocalDc(getConfiguration() } else { builder.withLoadBalancingPolicy( new TokenAwarePolicy( new RoundRobinPolicy()
new TokenAwarePolicy(new SortingLoadBalancingPolicy(), ordering);
.withNettyOptions(nonQuietClusterCloseOptions) .withLoadBalancingPolicy( new TokenAwarePolicy( DCAwareRoundRobinPolicy.builder() .withLocalDc(ScassandraCluster.datacenter(2))
private LoadBalancingPolicy getLB() { switch (asConfig.getConnectionPoolType()) { case ROUND_ROBIN: return new RoundRobinPolicy(); case TOKEN_AWARE: return new TokenAwarePolicy(new RoundRobinPolicy()); case BAG: throw new RuntimeException("Unsupported connection pool type, use ROUND_ROBIN or TOKEN_AWARE"); default: return new RoundRobinPolicy(); } }
.withPort(sCluster.getBinaryPort()) .withNettyOptions(nonQuietClusterCloseOptions) .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) .build();
new TokenAwarePolicy(new SortingLoadBalancingPolicy(), NEUTRAL)) .build();
@Test(groups = "unit") public void should_respect_child_policy_order() { // given TokenAwarePolicy policy = new TokenAwarePolicy(childPolicy, NEUTRAL); policy.init(cluster, null); // when Iterator<Host> queryPlan = policy.newQueryPlan("keyspace", statement); // then assertThat(queryPlan).containsExactly(host2, host1, host4, host3); }
@Test(groups = "unit") public void should_respect_topological_order() { // given TokenAwarePolicy policy = new TokenAwarePolicy(childPolicy, TOPOLOGICAL); policy.init(cluster, null); // when Iterator<Host> queryPlan = policy.newQueryPlan("keyspace", statement); // then assertThat(queryPlan).containsExactly(host1, host2, host4, host3); }
@Test(groups = "unit") public void should_create_random_order() { // given TokenAwarePolicy policy = new TokenAwarePolicy(childPolicy, RANDOM); policy.init(cluster, null); // when Iterator<Host> queryPlan = policy.newQueryPlan("keyspace", statement); // then assertThat(queryPlan).containsOnlyOnce(host1, host2, host3, host4).endsWith(host4, host3); }
new TokenAwarePolicy(new SortingLoadBalancingPolicy(), NEUTRAL)) .build();