Search in sources :

Example 1 with ExponentialReconnectionPolicy

use of com.datastax.driver.core.policies.ExponentialReconnectionPolicy in project java-driver by datastax.

the class ReconnectionPolicyTest method exponentialReconnectionPolicyTest.

/*
     * Test the ExponentialReconnectionPolicy.
     */
@Test(groups = "long")
@CCMConfig(clusterProvider = "exponential")
public void exponentialReconnectionPolicyTest() throws Throwable {
    // Ensure that ExponentialReconnectionPolicy is what we should be testing
    if (!(cluster().getConfiguration().getPolicies().getReconnectionPolicy() instanceof ExponentialReconnectionPolicy)) {
        fail("Set policy does not match retrieved policy.");
    }
    // Test basic getters
    ExponentialReconnectionPolicy reconnectionPolicy = (ExponentialReconnectionPolicy) cluster().getConfiguration().getPolicies().getReconnectionPolicy();
    assertTrue(reconnectionPolicy.getBaseDelayMs() == 2 * 1000);
    assertTrue(reconnectionPolicy.getMaxDelayMs() == 5 * 60 * 1000);
    // Test erroneous instantiations
    try {
        new ExponentialReconnectionPolicy(-1, 1);
        fail();
    } catch (IllegalArgumentException e) {
    // ok
    }
    try {
        new ExponentialReconnectionPolicy(1, -1);
        fail();
    } catch (IllegalArgumentException e) {
    // ok
    }
    try {
        new ExponentialReconnectionPolicy(-1, -1);
        fail();
    } catch (IllegalArgumentException e) {
    // ok
    }
    try {
        new ExponentialReconnectionPolicy(2, 1);
        fail();
    } catch (IllegalArgumentException e) {
    // ok
    }
    // Test nextDelays()
    ReconnectionPolicy.ReconnectionSchedule schedule = new ExponentialReconnectionPolicy(2 * 1000, 5 * 60 * 1000).newSchedule();
    assertTrue(schedule.nextDelayMs() == 2000);
    assertTrue(schedule.nextDelayMs() == 4000);
    assertTrue(schedule.nextDelayMs() == 8000);
    assertTrue(schedule.nextDelayMs() == 16000);
    assertTrue(schedule.nextDelayMs() == 32000);
    for (int i = 0; i < 64; ++i) schedule.nextDelayMs();
    assertTrue(schedule.nextDelayMs() == reconnectionPolicy.getMaxDelayMs());
    // Run integration test
    // 16: 3 full cycles + 2 seconds
    long restartTime = 2 + 4 + 8 + 2;
    // 4th cycle start time
    long retryTime = 30;
    // time until next reconnection attempt
    long breakTime = 62;
// TODO: Try to sort out variance
// reconnectionPolicyTest(restartTime, retryTime, breakTime);
}
Also used : ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) Test(org.testng.annotations.Test)

Example 2 with ExponentialReconnectionPolicy

use of com.datastax.driver.core.policies.ExponentialReconnectionPolicy in project presto by prestodb.

the class CassandraClientModule method createCassandraSession.

@Singleton
@Provides
public static CassandraSession createCassandraSession(CassandraConnectorId connectorId, CassandraClientConfig config, JsonCodec<List<ExtraColumnMetadata>> extraColumnMetadataCodec) {
    requireNonNull(config, "config is null");
    requireNonNull(extraColumnMetadataCodec, "extraColumnMetadataCodec is null");
    Cluster.Builder clusterBuilder = Cluster.builder().withProtocolVersion(config.getProtocolVersion());
    List<String> contactPoints = requireNonNull(config.getContactPoints(), "contactPoints is null");
    checkArgument(!contactPoints.isEmpty(), "empty contactPoints");
    clusterBuilder.withPort(config.getNativeProtocolPort());
    clusterBuilder.withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 10000));
    clusterBuilder.withRetryPolicy(config.getRetryPolicy().getPolicy());
    LoadBalancingPolicy loadPolicy = new RoundRobinPolicy();
    if (config.isUseDCAware()) {
        requireNonNull(config.getDcAwareLocalDC(), "DCAwarePolicy localDC is null");
        DCAwareRoundRobinPolicy.Builder builder = DCAwareRoundRobinPolicy.builder().withLocalDc(config.getDcAwareLocalDC());
        if (config.getDcAwareUsedHostsPerRemoteDc() > 0) {
            builder.withUsedHostsPerRemoteDc(config.getDcAwareUsedHostsPerRemoteDc());
            if (config.isDcAwareAllowRemoteDCsForLocal()) {
                builder.allowRemoteDCsForLocalConsistencyLevel();
            }
        }
        loadPolicy = builder.build();
    }
    if (config.isUseTokenAware()) {
        loadPolicy = new TokenAwarePolicy(loadPolicy, config.isTokenAwareShuffleReplicas());
    }
    if (config.isUseWhiteList()) {
        checkArgument(!config.getWhiteListAddresses().isEmpty(), "empty WhiteListAddresses");
        List<InetSocketAddress> whiteList = new ArrayList<>();
        for (String point : config.getWhiteListAddresses()) {
            whiteList.add(new InetSocketAddress(point, config.getNativeProtocolPort()));
        }
        loadPolicy = new WhiteListPolicy(loadPolicy, whiteList);
    }
    clusterBuilder.withLoadBalancingPolicy(loadPolicy);
    SocketOptions socketOptions = new SocketOptions();
    socketOptions.setReadTimeoutMillis(toIntExact(config.getClientReadTimeout().toMillis()));
    socketOptions.setConnectTimeoutMillis(toIntExact(config.getClientConnectTimeout().toMillis()));
    if (config.getClientSoLinger() != null) {
        socketOptions.setSoLinger(config.getClientSoLinger());
    }
    if (config.isTlsEnabled()) {
        SslContextProvider sslContextProvider = new SslContextProvider(config.getKeystorePath(), config.getKeystorePassword(), config.getTruststorePath(), config.getTruststorePassword());
        sslContextProvider.buildSslContext().ifPresent(context -> clusterBuilder.withSSL(JdkSSLOptions.builder().withSSLContext(context).build()));
    }
    clusterBuilder.withSocketOptions(socketOptions);
    if (config.getUsername() != null && config.getPassword() != null) {
        clusterBuilder.withCredentials(config.getUsername(), config.getPassword());
    }
    QueryOptions options = new QueryOptions();
    options.setFetchSize(config.getFetchSize());
    options.setConsistencyLevel(config.getConsistencyLevel());
    clusterBuilder.withQueryOptions(options);
    if (config.getSpeculativeExecutionLimit() > 1) {
        clusterBuilder.withSpeculativeExecutionPolicy(new ConstantSpeculativeExecutionPolicy(// delay before a new execution is launched
        config.getSpeculativeExecutionDelay().toMillis(), // maximum number of executions
        config.getSpeculativeExecutionLimit()));
    }
    return new NativeCassandraSession(connectorId.toString(), extraColumnMetadataCodec, new ReopeningCluster(() -> {
        contactPoints.forEach(clusterBuilder::addContactPoint);
        return clusterBuilder.build();
    }), config.getNoHostAvailableRetryTimeout());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketOptions(com.datastax.driver.core.SocketOptions) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) QueryOptions(com.datastax.driver.core.QueryOptions) ConstantSpeculativeExecutionPolicy(com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) WhiteListPolicy(com.datastax.driver.core.policies.WhiteListPolicy) SslContextProvider(com.facebook.presto.cassandra.util.SslContextProvider) ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 3 with ExponentialReconnectionPolicy

use of com.datastax.driver.core.policies.ExponentialReconnectionPolicy in project aroma-data-operations by RedRoma.

the class ModuleCassandraDevCluster method provideReconnectPolicy.

@Provides
ReconnectionPolicy provideReconnectPolicy() {
    long baseAttempt = TimeUnit.SECONDS.toMillis(5);
    long maxTimeWaiting = TimeUnit.MINUTES.toMillis(1);
    ExponentialReconnectionPolicy policy = new ExponentialReconnectionPolicy(baseAttempt, maxTimeWaiting);
    return policy;
}
Also used : ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) Provides(com.google.inject.Provides)

Example 4 with ExponentialReconnectionPolicy

use of com.datastax.driver.core.policies.ExponentialReconnectionPolicy in project storm by apache.

the class ClusterFactory method make.

/**
 * Creates a new Cluster based on the specified configuration.
 * @param topoConf the storm configuration.
 * @return a new a new {@link com.datastax.driver.core.Cluster} instance.
 */
@Override
protected Cluster make(Map<String, Object> topoConf) {
    CassandraConf cassandraConf = new CassandraConf(topoConf);
    Cluster.Builder cluster = Cluster.builder().withoutJMXReporting().withoutMetrics().addContactPoints(cassandraConf.getNodes()).withPort(cassandraConf.getPort()).withRetryPolicy(cassandraConf.getRetryPolicy()).withReconnectionPolicy(new ExponentialReconnectionPolicy(cassandraConf.getReconnectionPolicyBaseMs(), cassandraConf.getReconnectionPolicyMaxMs())).withLoadBalancingPolicy(cassandraConf.getLoadBalancingPolicy());
    cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis((int) cassandraConf.getSocketReadTimeoutMillis());
    cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis((int) cassandraConf.getSocketConnectTimeoutMillis());
    final String username = cassandraConf.getUsername();
    final String password = cassandraConf.getPassword();
    if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
        cluster.withAuthProvider(new PlainTextAuthProvider(username, password));
    }
    QueryOptions options = new QueryOptions().setConsistencyLevel(cassandraConf.getConsistencyLevel());
    cluster.withQueryOptions(options);
    PoolingOptions poolOps = new PoolingOptions();
    poolOps.setMaxQueueSize(cassandraConf.getPoolMaxQueueSize());
    poolOps.setHeartbeatIntervalSeconds(cassandraConf.getHeartbeatIntervalSeconds());
    poolOps.setIdleTimeoutSeconds(cassandraConf.getIdleTimeoutSeconds());
    poolOps.setMaxRequestsPerConnection(HostDistance.LOCAL, cassandraConf.getMaxRequestPerConnectionLocal());
    poolOps.setMaxRequestsPerConnection(HostDistance.REMOTE, cassandraConf.getMaxRequestPerConnectionRemote());
    cluster.withPoolingOptions(poolOps);
    return cluster.build();
}
Also used : PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) PlainTextAuthProvider(com.datastax.driver.core.PlainTextAuthProvider) ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) QueryOptions(com.datastax.driver.core.QueryOptions)

Example 5 with ExponentialReconnectionPolicy

use of com.datastax.driver.core.policies.ExponentialReconnectionPolicy in project ats-framework by Axway.

the class CassandraDbProvider method connect.

/**
 * Currently we connect just once and then reuse the connection.
 * We do not bother with closing the connection.
 *
 * It is normal to use one Session per DB. The Session is thread safe.
 */
private void connect() {
    if (cluster == null) {
        log.info("Connecting to Cassandra server on " + this.dbHost + " at port " + this.dbPort);
        // allow fetching as much data as present in the DB
        QueryOptions queryOptions = new QueryOptions();
        queryOptions.setFetchSize(Integer.MAX_VALUE);
        queryOptions.setConsistencyLevel(ConsistencyLevel.ONE);
        cluster = Cluster.builder().addContactPoint(this.dbHost).withPort(this.dbPort).withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 30000)).withQueryOptions(queryOptions).withCredentials(this.dbUser, this.dbPassword).build();
    }
    if (session == null) {
        log.info("Connecting to Cassandra DB with name " + this.dbName);
        session = cluster.connect(dbName);
    }
}
Also used : ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) QueryOptions(com.datastax.driver.core.QueryOptions) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy)

Aggregations

ExponentialReconnectionPolicy (com.datastax.driver.core.policies.ExponentialReconnectionPolicy)5 QueryOptions (com.datastax.driver.core.QueryOptions)3 Cluster (com.datastax.driver.core.Cluster)2 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)2 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)2 Provides (com.google.inject.Provides)2 PlainTextAuthProvider (com.datastax.driver.core.PlainTextAuthProvider)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 SocketOptions (com.datastax.driver.core.SocketOptions)1 ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)1 ConstantSpeculativeExecutionPolicy (com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy)1 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)1 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)1 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)1 WhiteListPolicy (com.datastax.driver.core.policies.WhiteListPolicy)1 SslContextProvider (com.facebook.presto.cassandra.util.SslContextProvider)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Singleton (javax.inject.Singleton)1 Test (org.testng.annotations.Test)1