com.datastax.driver.core.policies.LoadBalancingPolicy Java Examples

The following examples show how to use com.datastax.driver.core.policies.LoadBalancingPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraClient.java    From debezium-incubator with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
CassandraClient(CassandraConnectorConfig config, LoadBalancingPolicy lbPolicy) throws GeneralSecurityException, IOException {
    Cluster.Builder builder = Cluster.builder()
            .addContactPoints(config.cassandraHosts())
            .withPort(config.cassandraPort())
            .withProtocolVersion(ProtocolVersion.V4)
            .withLoadBalancingPolicy(lbPolicy)
            // See https://docs.datastax.com/en/developer/java-driver/3.5/manual/metrics/#metrics-4-compatibility.
            .withoutJMXReporting();

    if (config.cassandraUsername() != null && config.cassandraPassword() != null) {
        builder.withCredentials(config.cassandraUsername(), config.cassandraPassword());
    }

    if (config.cassandraSslEnabled()) {
        SslContext sslContext = createSslContext(config.cassandraSslConfigPath());
        SSLOptions sslOptions = new RemoteEndpointAwareNettySSLOptions(sslContext);
        builder.withSSL(sslOptions);
    }

    cluster = builder.build();
    session = cluster.connect();

    registerClusterMetrics(cluster.getClusterName());
}
 
Example #2
Source File: LatencyAwarePolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: DCAwareRoundRobinPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    DCAwareRoundRobinPolicy.Builder builder = DCAwareRoundRobinPolicy.builder();

    if (allowRemoteDCsForLocalConsistencyLevel == Boolean.TRUE) {
        builder.allowRemoteDCsForLocalConsistencyLevel();
    }

    if (localDC != null) {
        builder.withLocalDc(localDC);
    }

    if (usedHostsPerRemoteDC != null) {
        builder.withUsedHostsPerRemoteDc(usedHostsPerRemoteDC);
    }

    return builder.build();
}
 
Example #4
Source File: LatencyAwarePolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void buildsPolicyWithAllParams() throws Exception {
    final LatencyAwarePolicyFactory factory = new LatencyAwarePolicyFactory();
    factory.setSubPolicy(subPolicyFactory);
    factory.setExclusionThreshold(1.0d);
    factory.setMinimumMeasurements(2);
    factory.setRetryPeriod(Duration.minutes(3));
    factory.setScale(Duration.milliseconds(100));
    factory.setUpdateRate(Duration.seconds(5));

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isSameAs(resultingPolicy);
    verify(subPolicyFactory).build();

    InOrder inOrder = inOrder(policyBuilder);
    inOrder.verify(policyBuilder).withExclusionThreshold(1.0d);
    inOrder.verify(policyBuilder).withMininumMeasurements(2);
    inOrder.verify(policyBuilder).withRetryPeriod(3L, TimeUnit.MINUTES);
    inOrder.verify(policyBuilder).withScale(100L, TimeUnit.MILLISECONDS);
    inOrder.verify(policyBuilder).withUpdateRate(5L, TimeUnit.SECONDS);
    inOrder.verify(policyBuilder).build();
}
 
Example #5
Source File: LatencyAwarePolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void buildsPolicyWithNoParams() throws Exception {
    final LatencyAwarePolicyFactory factory = new LatencyAwarePolicyFactory();
    factory.setSubPolicy(subPolicyFactory);

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isSameAs(resultingPolicy);
    verify(subPolicyFactory).build();

    verify(policyBuilder, never()).withExclusionThreshold(anyDouble());
    verify(policyBuilder, never()).withMininumMeasurements(anyInt());
    verify(policyBuilder, never()).withRetryPeriod(anyLong(), any(TimeUnit.class));
    verify(policyBuilder, never()).withScale(anyLong(), any(TimeUnit.class));
    verify(policyBuilder, never()).withUpdateRate(anyLong(), any(TimeUnit.class));
    verify(policyBuilder).build();
}
 
Example #6
Source File: DCAwareRoundRobinPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicyWithAllParams() throws Exception {
    final DCAwareRoundRobinPolicyFactory factory = new DCAwareRoundRobinPolicyFactory();
    factory.setLocalDC("dc1");
    factory.setUsedHostsPerRemoteDC(1);
    factory.setAllowRemoteDCsForLocalConsistencyLevel(true);

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isExactlyInstanceOf(DCAwareRoundRobinPolicy.class);
}
 
Example #7
Source File: MapConfiguredCqlClientFactory.java    From storm-cassandra-cql with Apache License 2.0 5 votes vote down vote up
private void configureLoadBalancingPolicy() {
    final String dataCenterNameConfiguration = (String) configuration.get(TRIDENT_CASSANDRA_LOCAL_DATA_CENTER_NAME);
    if (StringUtils.isNotEmpty(dataCenterNameConfiguration)) {
        final LoadBalancingPolicy loadBalancingPolicy = DCAwareRoundRobinPolicy.builder().withLocalDc(dataCenterNameConfiguration).build();
        builder = builder.withLoadBalancingPolicy(loadBalancingPolicy);
    }
}
 
Example #8
Source File: CqlSession.java    From ob1k with Apache License 2.0 5 votes vote down vote up
public CqlSession(final String nodes, final int port, final String keyspace, final SocketOptions socketOptions,
                  final RetryPolicy retryPolicy, final QueryOptions queryOptions,
                  final LoadBalancingPolicy loadBalancingPolicy, final int maxConnectionsPerHost,
                  final MetricFactory metricFactory) {

  // this is temp. to reuse current hosts properties:
  final Iterable<String> nodesIter = Splitter.on(",").split(nodes);
  final String[] nodesArr = Iterables.toArray(
    StreamSupport.stream(nodesIter.spliterator(), false).map(input -> {
    if (input == null) return null;

    final int idx = input.lastIndexOf(":");
    return input.substring(0, idx);
  }).collect(Collectors.toList()), String.class);


  /*PoolingOptions poolingOptions = new PoolingOptions();
  poolingOptions.setMaxConnectionsPerHost(HostDistance.LOCAL, maxConnectionsPerHost);
  poolingOptions.setMaxConnectionsPerHost(HostDistance.REMOTE, maxConnectionsPerHost);*/


  final Cluster cluster = Cluster.builder().
          withPort(port).
          withSocketOptions(socketOptions).
          withQueryOptions(queryOptions).
          withLoadBalancingPolicy(loadBalancingPolicy).
          //  withPoolingOptions(poolingOptions).
                  addContactPoints(nodesArr).build();
  //cluster.init();
  this.session = cluster.connect(keyspace);
  this.retryPolicy = Preconditions.checkNotNull(retryPolicy);
  this.metricFactory = Preconditions.checkNotNull(metricFactory);
}
 
Example #9
Source File: CqlConfigHelper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static Cluster getInputCluster(String[] hosts, Configuration conf)
{
    int port = getInputNativePort(conf);
    Optional<AuthProvider> authProvider = getAuthProvider(conf);
    Optional<SSLOptions> sslOptions = getSSLOptions(conf);
    Optional<Integer> protocolVersion = getProtocolVersion(conf);
    LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(conf, hosts);
    SocketOptions socketOptions = getReadSocketOptions(conf);
    QueryOptions queryOptions = getReadQueryOptions(conf);
    PoolingOptions poolingOptions = getReadPoolingOptions(conf);
    
    Cluster.Builder builder = Cluster.builder()
                                     .addContactPoints(hosts)
                                     .withPort(port)
                                     .withCompression(ProtocolOptions.Compression.NONE);

    if (authProvider.isPresent())
        builder.withAuthProvider(authProvider.get());
    if (sslOptions.isPresent())
        builder.withSSL(sslOptions.get());

    if (protocolVersion.isPresent()) {
        builder.withProtocolVersion(protocolVersion.get());
    }
    builder.withLoadBalancingPolicy(loadBalancingPolicy)
           .withSocketOptions(socketOptions)
           .withQueryOptions(queryOptions)
           .withPoolingOptions(poolingOptions);

    return builder.build();
}
 
Example #10
Source File: CassandraSessionFactory.java    From jmeter-cassandra with Apache License 2.0 5 votes vote down vote up
public static synchronized Session createSession(String sessionKey, Set<InetAddress> host, String keyspace, String username, String password, LoadBalancingPolicy loadBalancingPolicy) {

    instance = getInstance();
    Session session = instance.sessions.get(sessionKey);
      if (session == null) {

          Cluster.Builder cb = Cluster.builder()
                  .addContactPoints(host)
                  .withReconnectionPolicy(new ConstantReconnectionPolicy(10000)) ;

          if (loadBalancingPolicy != null ) {
              cb = cb.withLoadBalancingPolicy(loadBalancingPolicy);
          }

          if ( username != null && ! username.isEmpty()) {
              cb = cb.withCredentials(username, password);
          }

          Cluster cluster = cb.build();


          if (keyspace != null && !keyspace.isEmpty())
        session = cluster.connect(keyspace);
      else
        session = cluster.connect();

        instance.sessions.put(sessionKey, session);
    }
    return session;
  }
 
Example #11
Source File: RoundRobinPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicy() throws Exception {
    final RoundRobinPolicyFactory factory = new RoundRobinPolicyFactory();

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isExactlyInstanceOf(RoundRobinPolicy.class);
}
 
Example #12
Source File: RemoteMetadataFactory.java    From cassandra-exporter with Apache License 2.0 5 votes vote down vote up
@Override
public InetAddress localBroadcastAddress() {
    final LoadBalancingPolicy loadBalancingPolicy = cluster.getConfiguration().getPolicies().getLoadBalancingPolicy();

    // if the LoadBalancingPolicy is correctly configured, this should return just the local host
    final Host host = cluster.getMetadata().getAllHosts().stream()
            .filter(h -> loadBalancingPolicy.distance(h) == HostDistance.LOCAL)
            .findFirst()
            .orElseThrow(() -> new IllegalStateException("No Cassandra node with LOCAL distance found."));

    return host.getBroadcastAddress();
}
 
Example #13
Source File: DCAwareRoundRobinPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicyWithNoParams() throws Exception {
    final DCAwareRoundRobinPolicyFactory factory = new DCAwareRoundRobinPolicyFactory();

    final LoadBalancingPolicy policy = factory.build();

    assertThat(policy).isExactlyInstanceOf(DCAwareRoundRobinPolicy.class);
}
 
Example #14
Source File: ErrorAwarePolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    ErrorAwarePolicy.Builder builder = ErrorAwarePolicy.builder(subPolicy.build());

    if (maxErrorsPerMinute != null) {
        builder.withMaxErrorsPerMinute(maxErrorsPerMinute);
    }

    if (retryPeriod != null) {
        builder.withRetryPeriod(retryPeriod.getQuantity(), retryPeriod.getUnit());
    }

    return builder.build();
}
 
Example #15
Source File: DatasourceSerializationTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Serialization test.
 */
@Test
public void serializationTest() {
    DataSource src = new DataSource();

    Credentials cred = new CassandraAdminCredentials();
    String[] points = new String[]{"127.0.0.1", "10.0.0.2", "10.0.0.3"};
    LoadBalancingPolicy plc = new MyLoadBalancingPolicy();

    src.setCredentials(cred);
    src.setContactPoints(points);
    src.setReadConsistency("ONE");
    src.setWriteConsistency("QUORUM");
    src.setLoadBalancingPolicy(plc);

    JavaSerializer serializer = new JavaSerializer();

    ByteBuffer buff = serializer.serialize(src);
    DataSource _src = (DataSource)serializer.deserialize(buff);

    Credentials _cred = (Credentials)getFieldValue(_src, "creds");
    List<InetAddress> _points = (List<InetAddress>)getFieldValue(_src, "contactPoints");
    ConsistencyLevel _readCons = (ConsistencyLevel)getFieldValue(_src, "readConsistency");
    ConsistencyLevel _writeCons = (ConsistencyLevel)getFieldValue(_src, "writeConsistency");
    LoadBalancingPolicy _plc = (LoadBalancingPolicy)getFieldValue(_src, "loadBalancingPlc");

    assertTrue("Incorrectly serialized/deserialized credentials for Cassandra DataSource",
        cred.getPassword().equals(_cred.getPassword()) && cred.getUser().equals(_cred.getUser()));

    assertTrue("Incorrectly serialized/deserialized contact points for Cassandra DataSource",
        "/127.0.0.1".equals(_points.get(0).toString()) &&
        "/10.0.0.2".equals(_points.get(1).toString()) &&
        "/10.0.0.3".equals(_points.get(2).toString()));

    assertTrue("Incorrectly serialized/deserialized consistency levels for Cassandra DataSource",
        ConsistencyLevel.ONE == _readCons && ConsistencyLevel.QUORUM == _writeCons);

    assertTrue("Incorrectly serialized/deserialized load balancing policy for Cassandra DataSource",
        _plc instanceof MyLoadBalancingPolicy);
}
 
Example #16
Source File: DataSource.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    fetchSize = (Integer)in.readObject();
    readConsistency = (ConsistencyLevel)in.readObject();
    writeConsistency = (ConsistencyLevel)in.readObject();
    user = U.readString(in);
    pwd = U.readString(in);
    port = (Integer)in.readObject();
    contactPoints = (List<InetAddress>)in.readObject();
    contactPointsWithPorts = (List<InetSocketAddress>)in.readObject();
    maxSchemaAgreementWaitSeconds = (Integer)in.readObject();
    protoVer = (Integer)in.readObject();
    compression = U.readString(in);
    useSSL = (Boolean)in.readObject();
    collectMetrix = (Boolean)in.readObject();
    jmxReporting = (Boolean)in.readObject();
    creds = (Credentials)in.readObject();
    loadBalancingPlc = (LoadBalancingPolicy)readObject(in);
    reconnectionPlc = (ReconnectionPolicy)readObject(in);
    addrTranslator = (AddressTranslator)readObject(in);
    speculativeExecutionPlc = (SpeculativeExecutionPolicy)readObject(in);
    authProvider = (AuthProvider)readObject(in);
    sslOptions = (SSLOptions)readObject(in);
    poolingOptions = (PoolingOptions)readObject(in);
    sockOptions = (SocketOptions)readObject(in);
    nettyOptions = (NettyOptions)readObject(in);
}
 
Example #17
Source File: TokenAwarePolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    return (shuffleReplicas == null)
            ? new TokenAwarePolicy(subPolicy.build())
            : new TokenAwarePolicy(subPolicy.build(), shuffleReplicas);
}
 
Example #18
Source File: RoundRobinPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    return new RoundRobinPolicy();
}
 
Example #19
Source File: WhiteListPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public LoadBalancingPolicy build() {
    return new WhiteListPolicy(subPolicy.build(), whiteList);
}
 
Example #20
Source File: CqlConfigHelper.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private static LoadBalancingPolicy getReadLoadBalancingPolicy(Configuration conf, final String[] stickHosts)
{
    return new LimitedLocalNodeFirstLocalBalancingPolicy(stickHosts);
}
 
Example #21
Source File: DataSource.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Sets load balancing policy.
 *
 * @param plc Load balancing policy.
 */
public void setLoadBalancingPolicy(LoadBalancingPolicy plc) {
    loadBalancingPlc = plc;

    invalidate();
}
 
Example #22
Source File: CassandraClusterConfig.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public LoadBalancingPolicy getLoadBalancingPolicy() {
	return loadBalancingPolicy;
}
 
Example #23
Source File: CassandraClusterConfig.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) {
	this.loadBalancingPolicy = loadBalancingPolicy;
}
 
Example #24
Source File: ConnectionManager.java    From Rhombus with MIT License 4 votes vote down vote up
public LoadBalancingPolicy getLoadBalancingPolicy() {
	return loadBalancingPolicy;
}
 
Example #25
Source File: ConnectionManager.java    From Rhombus with MIT License 4 votes vote down vote up
public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) {
	this.loadBalancingPolicy = loadBalancingPolicy;
}
 
Example #26
Source File: DataStaxClusterImpl.java    From usergrid with Apache License 2.0 4 votes vote down vote up
public synchronized Cluster buildCluster(){

        ConsistencyLevel defaultConsistencyLevel;
        try {
            defaultConsistencyLevel = cassandraConfig.getDataStaxReadCl();
        } catch (IllegalArgumentException e){

            logger.error("Unable to parse provided consistency level in property: {}, defaulting to: {}",
                CassandraFig.READ_CL,
                ConsistencyLevel.LOCAL_QUORUM);

            defaultConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
        }


        LoadBalancingPolicy loadBalancingPolicy;
        if( !cassandraConfig.getLocalDataCenter().isEmpty() ){

            loadBalancingPolicy = new DCAwareRoundRobinPolicy.Builder()
                .withLocalDc( cassandraConfig.getLocalDataCenter() ).build();
        }else{
            loadBalancingPolicy = new DCAwareRoundRobinPolicy.Builder().build();
        }

        final PoolingOptions poolingOptions = new PoolingOptions()
            .setCoreConnectionsPerHost(HostDistance.LOCAL, cassandraConfig.getConnections())
            .setMaxConnectionsPerHost(HostDistance.LOCAL, cassandraConfig.getConnections())
            .setIdleTimeoutSeconds( cassandraConfig.getPoolTimeout() / 1000 )
            .setPoolTimeoutMillis( cassandraConfig.getPoolTimeout());

        // purposely add a couple seconds to the driver's lower level socket timeouts vs. cassandra timeouts
        final SocketOptions socketOptions = new SocketOptions()
            .setConnectTimeoutMillis( cassandraConfig.getTimeout())
            .setReadTimeoutMillis( cassandraConfig.getTimeout())
            .setKeepAlive(true);

        final QueryOptions queryOptions = new QueryOptions()
            .setConsistencyLevel(defaultConsistencyLevel)
            .setMetadataEnabled(true); // choose whether to have the driver store metadata such as schema info

        Cluster.Builder datastaxCluster = Cluster.builder()
            .withClusterName(cassandraConfig.getClusterName())
            .addContactPoints(cassandraConfig.getHosts().split(","))
            .withMaxSchemaAgreementWaitSeconds(45)
            .withCompression(ProtocolOptions.Compression.LZ4)
            .withLoadBalancingPolicy(loadBalancingPolicy)
            .withPoolingOptions(poolingOptions)
            .withQueryOptions(queryOptions)
            .withSocketOptions(socketOptions)
            .withReconnectionPolicy(Policies.defaultReconnectionPolicy())
            // client side timestamp generation is IMPORTANT; otherwise successive writes are left up to the server
            // to determine the ts and bad network delays, clock sync, etc. can result in bad behaviors
            .withTimestampGenerator(new AtomicMonotonicTimestampGenerator())
            .withProtocolVersion(getProtocolVersion(cassandraConfig.getVersion()));

        // only add auth credentials if they were provided
        if ( !cassandraConfig.getUsername().isEmpty() && !cassandraConfig.getPassword().isEmpty() ){
            datastaxCluster.withCredentials(
                cassandraConfig.getUsername(),
                cassandraConfig.getPassword()
            );
        }


        return datastaxCluster.build();

    }
 
Example #27
Source File: ConnectionStateManager.java    From attic-apex-malhar with Apache License 2.0 2 votes vote down vote up
/**
 * Used to define how the nodes in the cluster will be contacted for executing a mutation.
 * The following is the default behaviour if not set.
 * 1. Use a TokenAware approach i.e. the row key is used to decide the right node to execute the mutation
 *    on the target cassandra node. i.e. One of the R-1 replicas is used as the coordinator node.
 *    This effectively balances the traffic onto all nodes of the cassandra cluster for the given
 *    Operator instance. Of course this assumes the keys are evenly distributed in the cluster
 *    which is normally the case
 * 2. Overlay TokenAware with DC aware approach - The above token aware approach is further segmented to use only
 *    the local DC for the mutation executions. Cassandras multi-DC execution will take care of the cross DC
 *    replication thus achieving the lowest possible latencies for the given mutation of writes.
 *
 * Using this effectively removes the need for an extra implementation of the Partitioning logic of the Operator
 * Nor would we need any extra logic ( for most use cases ) for dynamic partitioning implementations as the
 * underlying driver normalizes the traffic pattern anyways.
 * @param loadBalancingPolicy
 * @return The builder instance as initially created updated with this value
   */
public ConnectionBuilder withLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy)
{
  this.loadBalancingPolicy = loadBalancingPolicy;
  return this;
}
 
Example #28
Source File: LoadBalancingPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 votes vote down vote up
LoadBalancingPolicy build();