Search in sources :

Example 41 with ConstantReconnectionPolicy

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

the class ReconnectionTest method should_keep_reconnecting_on_authentication_error.

@CCMConfig(dirtiesContext = true, config = "authenticator:PasswordAuthenticator", jvmArgs = "-Dcassandra.superuser_setup_delay_ms=0", createCluster = false)
@Test(groups = "long")
public void should_keep_reconnecting_on_authentication_error() throws InterruptedException {
    // user being created.
    if (ccm().getCassandraVersion().getMajor() < 2) {
        Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
    }
    CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis));
    CountingAuthProvider authProvider = new CountingAuthProvider("cassandra", "cassandra");
    Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0)).withPort(ccm().getBinaryPort()).withAuthProvider(authProvider).withReconnectionPolicy(reconnectionPolicy).build());
    cluster.init();
    assertThat(cluster).usesControlHost(1);
    // Stop the server, set wrong credentials and restart
    ccm().stop(1);
    ccm().waitForDown(1);
    authProvider.setPassword("wrongPassword");
    ccm().start(1);
    ccm().waitForUp(1);
    // Wait a few iterations to ensure that our authProvider has returned the wrong credentials at least twice
    // NB: authentication errors show up in the logs
    int initialCount = authProvider.count.get();
    long initialMetricCount = cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount();
    // make sure we don't wait indefinitely
    int iterations = 0, maxIterations = 12;
    do {
        iterations += 1;
        TimeUnit.SECONDS.sleep(5);
    } while (iterations < maxIterations && authProvider.count.get() <= initialCount + 1);
    assertThat(iterations).isLessThan(maxIterations);
    // Number of authentication errors should have increased.
    assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isGreaterThan(initialMetricCount);
    // Fix the credentials
    authProvider.setPassword("cassandra");
    // The driver should eventually reconnect to the node
    assertThat(cluster).host(1).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS * 2, SECONDS);
}
Also used : ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.testng.annotations.Test)

Example 42 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project cassandra-driver-mapping by valchkou.

the class SchemaSyncTest method init.

@BeforeClass
public static void init() {
    String node = "127.0.0.1";
    Builder builder = Cluster.builder();
    builder.addContactPoint(node);
    builder.withLoadBalancingPolicy(LatencyAwarePolicy.builder(new RoundRobinPolicy()).build());
    builder.withReconnectionPolicy(new ConstantReconnectionPolicy(1000L));
    cluster = builder.build();
    session = cluster.connect();
    Cache<String, PreparedStatement> cache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MILLISECONDS).maximumSize(1).concurrencyLevel(1).build();
    MappingSession.setStatementCache(cache);
}
Also used : Builder(com.datastax.driver.core.Cluster.Builder) CacheBuilder(com.google.common.cache.CacheBuilder) PreparedStatement(com.datastax.driver.core.PreparedStatement) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) BeforeClass(org.junit.BeforeClass)

Example 43 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project cassandra-driver-mapping by valchkou.

the class MappingSessionAsyncTest method init.

@BeforeClass
public static void init() {
    String node = "127.0.0.1";
    Builder builder = Cluster.builder();
    builder.addContactPoint(node);
    builder.withLoadBalancingPolicy(LatencyAwarePolicy.builder(new RoundRobinPolicy()).build());
    builder.withReconnectionPolicy(new ConstantReconnectionPolicy(1000L));
    cluster = builder.build();
    session = cluster.connect();
    Cache<String, PreparedStatement> cache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MILLISECONDS).maximumSize(1).concurrencyLevel(1).build();
    MappingSession.setStatementCache(cache);
}
Also used : Builder(com.datastax.driver.core.Cluster.Builder) CacheBuilder(com.google.common.cache.CacheBuilder) PreparedStatement(com.datastax.driver.core.PreparedStatement) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) BeforeClass(org.junit.BeforeClass)

Example 44 with ConstantReconnectionPolicy

use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project cassandra-driver-mapping by valchkou.

the class MappingSessionTest method init.

@BeforeClass
public static void init() {
    String node = "127.0.0.1";
    Builder builder = Cluster.builder();
    builder.addContactPoint(node);
    builder.withLoadBalancingPolicy(LatencyAwarePolicy.builder(new RoundRobinPolicy()).build());
    builder.withReconnectionPolicy(new ConstantReconnectionPolicy(1000L));
    cluster = builder.build();
    session = cluster.connect();
    Cache<String, PreparedStatement> cache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MILLISECONDS).maximumSize(1).concurrencyLevel(1).build();
    MappingSession.setStatementCache(cache);
}
Also used : QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) Builder(com.datastax.driver.core.Cluster.Builder) CacheBuilder(com.google.common.cache.CacheBuilder) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy)

Aggregations

ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)44 Cluster (com.datastax.driver.core.Cluster)29 ResultSet (com.datastax.driver.core.ResultSet)28 Session (com.datastax.driver.core.Session)28 Row (com.datastax.driver.core.Row)26 Test (org.testng.annotations.Test)10 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)7 Builder (com.datastax.driver.core.Cluster.Builder)3 CacheBuilder (com.google.common.cache.CacheBuilder)3 IOException (java.io.IOException)3 PreparedStatement (com.datastax.driver.core.PreparedStatement)2 BeforeClass (org.junit.BeforeClass)2 Configuration (com.datastax.driver.core.Configuration)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 QueryOptions (com.datastax.driver.core.QueryOptions)1 SocketOptions (com.datastax.driver.core.SocketOptions)1 ExponentialReconnectionPolicy (com.datastax.driver.core.policies.ExponentialReconnectionPolicy)1 LimitingLoadBalancingPolicy (com.datastax.driver.core.policies.LimitingLoadBalancingPolicy)1 Policies (com.datastax.driver.core.policies.Policies)1 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)1