/** * Builds the policy configured by this builder. * * @return the policy. */ public DCAwareRoundRobinPolicy build() { if (usedHostsPerRemoteDc == 0 && allowRemoteDCsForLocalConsistencyLevel) { logger.warn( "Setting allowRemoteDCsForLocalConsistencyLevel has no effect if usedHostsPerRemoteDc = 0. " + "This setting will be ignored"); } return new DCAwareRoundRobinPolicy( localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, true); } }
.withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("US_EAST"))
Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2"));
/** * Builds the policy configured by this builder. * * @return the policy. */ public DCAwareRoundRobinPolicy build() { if (usedHostsPerRemoteDc == 0 && allowRemoteDCsForLocalConsistencyLevel) { logger.warn("Setting allowRemoteDCsForLocalConsistencyLevel has no effect if usedHostsPerRemoteDc = 0. " + "This setting will be ignored"); } return new DCAwareRoundRobinPolicy(localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, true); } }
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1") .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy("mydc", 0))) .build();
public static LoadBalancingPolicy defaultLoadBalancingPolicy() { return new TokenAwarePolicy(new DCAwareRoundRobinPolicy()); }
LoadBalancingPolicy policy = LatencyAwarePolicy .builder(new TokenAwarePolicy(new DCAwareRoundRobinPolicy())) .build();
/** * Builds the policy configured by this builder. * * @return the policy. */ public DCAwareRoundRobinPolicy build() { if (usedHostsPerRemoteDc == 0 && allowRemoteDCsForLocalConsistencyLevel) { logger.warn("Setting allowRemoteDCsForLocalConsistencyLevel has no effect if usedHostsPerRemoteDc = 0. " + "This setting will be ignored"); } return new DCAwareRoundRobinPolicy(localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, true); } }
/** * Builds the policy configured by this builder. * * @return the policy. */ public DCAwareRoundRobinPolicy build() { if (usedHostsPerRemoteDc == 0 && allowRemoteDCsForLocalConsistencyLevel) { logger.warn("Setting allowRemoteDCsForLocalConsistencyLevel has no effect if usedHostsPerRemoteDc = 0. " + "This setting will be ignored"); } return new DCAwareRoundRobinPolicy(localDc, usedHostsPerRemoteDc, allowRemoteDCsForLocalConsistencyLevel, true); } }
/** * The default load balancing policy. * <p> * The default load balancing policy is {@link DCAwareRoundRobinPolicy} with token * awareness (so {@code new TokenAwarePolicy(new DCAwareRoundRobinPolicy())}). * * @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(new DCAwareRoundRobinPolicy()); }
Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new WhiteListPolicy(new DCAwareRoundRobinPolicy("dc2"), whiteList));
Cluster.Builder builder = Cluster.builder(); Cluster cluster=builder.withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy("us-east"))).addContactPoints("127.0.0.1").build();
private LoadBalancingPolicy getLoadBalancingPolicy() { String policy = System.getProperty(LOAD_BALANCING); if (policy == null || policy.equals("RoundRobin")) { return new RoundRobinPolicy(); } if (policy.equals("DCAwareRoundRobin")) { String dataCenter = System.getProperty(DATA_CENTER); if (dataCenter == null) { LOG.warn(policy + " was specified for " + LOAD_BALANCING + " but " + DATA_CENTER + " is undefined." + "Reverting to RoundRobin load balancing policy."); return new RoundRobinPolicy(); } else { return new DCAwareRoundRobinPolicy(dataCenter); } } LOG.warn(policy + " is not a supported load balancing policy. Reverting to RoundRobin load balancing policy."); return new RoundRobinPolicy(); }
loadBalancingPolicy = new DCAwareRoundRobinPolicy(localDc, usedHostsPerRemoteDc);
@Override protected void initLoadBalancingPolicy(JsonObject loadBalancing) { // Recall super super.initLoadBalancingPolicy(loadBalancing); // If LB policy not set, try env vars if (loadBalancingPolicy == null) { String localDC = container.env().get(ENV_VAR_LOCAL_DC); if (!Strings.isNullOrEmpty(localDC)) { logger.debug("Using environment config for Local DC of " + localDC); loadBalancingPolicy = new DCAwareRoundRobinPolicy(localDC); } else { logger.debug("No environment configuration found for local DC"); } } }
private CassandraFactory(String hostName, String port, String dcName, String username, String password, Integer connectTimeoutMillis, Integer readTimeoutMillis){ LoadBalancingPolicy loadBalancingPolicy = new DCAwareRoundRobinPolicy(dcName,2); PoolingOptions poolingOptions = new PoolingOptions(); poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL,10); poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, 50); SocketOptions socketOptions = new SocketOptions().setReadTimeoutMillis(readTimeoutMillis) .setConnectTimeoutMillis(connectTimeoutMillis); Cluster.Builder builder = Cluster.builder().addContactPoints(hostName) .withPoolingOptions(poolingOptions) .withReconnectionPolicy(new ConstantReconnectionPolicy(100L)) .withLoadBalancingPolicy(loadBalancingPolicy) .withSocketOptions(socketOptions); if(!username.isEmpty() && !password.isEmpty()) { builder.withCredentials(username, password); } cluster = builder.build(); Metadata metadata = cluster.getMetadata(); LOGGER.info("Connected to cluster: {}", metadata.getClusterName()); for ( Host host : metadata.getAllHosts() ) { LOGGER.info("Datacenter: {}; Host: {}; Rack: {}", new String[]{ host.getDatacenter(), host.getAddress().getHostAddress(), host.getRack() }); } }