Search in sources :

Example 1 with ReconnectionPolicy

use of com.datastax.driver.core.policies.ReconnectionPolicy in project spring-boot by spring-projects.

the class CassandraAutoConfiguration method cluster.

@Bean
@ConditionalOnMissingBean
public Cluster cluster() {
    CassandraProperties properties = this.properties;
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withPort(properties.getPort());
    if (properties.getUsername() != null) {
        builder.withCredentials(properties.getUsername(), properties.getPassword());
    }
    if (properties.getCompression() != null) {
        builder.withCompression(properties.getCompression());
    }
    if (properties.getLoadBalancingPolicy() != null) {
        LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
        builder.withLoadBalancingPolicy(policy);
    }
    builder.withQueryOptions(getQueryOptions());
    if (properties.getReconnectionPolicy() != null) {
        ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
        builder.withReconnectionPolicy(policy);
    }
    if (properties.getRetryPolicy() != null) {
        RetryPolicy policy = instantiate(properties.getRetryPolicy());
        builder.withRetryPolicy(policy);
    }
    builder.withSocketOptions(getSocketOptions());
    if (properties.isSsl()) {
        builder.withSSL();
    }
    String points = properties.getContactPoints();
    builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
    customize(builder);
    return builder.build();
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with ReconnectionPolicy

use of com.datastax.driver.core.policies.ReconnectionPolicy in project tutorials by eugenp.

the class CassandraConfiguration method cluster.

@Bean
public Cluster cluster(CassandraProperties properties) {
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withProtocolVersion(protocolVersion).withPort(getPort(properties));
    if (properties.getUsername() != null) {
        builder.withCredentials(properties.getUsername(), properties.getPassword());
    }
    if (properties.getCompression() != null) {
        builder.withCompression(properties.getCompression());
    }
    if (properties.getLoadBalancingPolicy() != null) {
        LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
        builder.withLoadBalancingPolicy(policy);
    }
    builder.withQueryOptions(getQueryOptions(properties));
    if (properties.getReconnectionPolicy() != null) {
        ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
        builder.withReconnectionPolicy(policy);
    }
    if (properties.getRetryPolicy() != null) {
        RetryPolicy policy = instantiate(properties.getRetryPolicy());
        builder.withRetryPolicy(policy);
    }
    builder.withSocketOptions(getSocketOptions(properties));
    if (properties.isSsl()) {
        builder.withSSL();
    }
    String points = properties.getContactPoints();
    builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
    Cluster cluster = builder.build();
    cluster.getConfiguration().getCodecRegistry().register(LocalDateCodec.instance).register(CustomZonedDateTimeCodec.instance);
    if (metricRegistry != null) {
        cluster.init();
        metricRegistry.registerAll(cluster.getMetrics().getRegistry());
    }
    return cluster;
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) Bean(org.springframework.context.annotation.Bean)

Example 3 with ReconnectionPolicy

use of com.datastax.driver.core.policies.ReconnectionPolicy 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 4 with ReconnectionPolicy

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

the class ReconnectionPolicyTest method constantReconnectionPolicyTest.

/*
     * Test the ConstantReconnectionPolicy.
     */
@Test(groups = "long")
@CCMConfig(clusterProvider = "constant")
public void constantReconnectionPolicyTest() throws Throwable {
    // Ensure that ConstantReconnectionPolicy is what we should be testing
    if (!(cluster().getConfiguration().getPolicies().getReconnectionPolicy() instanceof ConstantReconnectionPolicy)) {
        fail("Set policy does not match retrieved policy.");
    }
    // Test basic getters
    ConstantReconnectionPolicy reconnectionPolicy = (ConstantReconnectionPolicy) cluster().getConfiguration().getPolicies().getReconnectionPolicy();
    assertTrue(reconnectionPolicy.getConstantDelayMs() == 10 * 1000);
    // Test erroneous instantiations
    try {
        new ConstantReconnectionPolicy(-1);
        fail();
    } catch (IllegalArgumentException e) {
    // ok
    }
    // Test nextDelays()
    ReconnectionPolicy.ReconnectionSchedule schedule = new ConstantReconnectionPolicy(10 * 1000).newSchedule();
    assertTrue(schedule.nextDelayMs() == 10000);
    assertTrue(schedule.nextDelayMs() == 10000);
    assertTrue(schedule.nextDelayMs() == 10000);
    assertTrue(schedule.nextDelayMs() == 10000);
    assertTrue(schedule.nextDelayMs() == 10000);
    // Run integration test
    // matches the above test
    long restartTime = 32;
    // 2nd cycle start time
    long retryTime = 40;
    // time until next reconnection attempt
    long breakTime = 10;
// 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) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.testng.annotations.Test)

Example 5 with ReconnectionPolicy

use of com.datastax.driver.core.policies.ReconnectionPolicy in project eventapis by kloiasoft.

the class CassandraSession method cluster.

/*    private Cluster cluster() {
            Cluster.Builder builder = Cluster.builder();
            Arrays.stream(eventStoreConfig.getContactPoints().split(";")).forEach(s -> {
                String[] hostPort = s.split(":");
                builder.addContactPointsWithPorts(new InetSocketAddress(hostPort[0], Integer.parseInt(hostPort[1])));
            });
            builder.withPoolingOptions(eventStoreConfig.getPoolingOptions());

            Cluster cluster = builder.build();
            cluster.register(QueryLogger.builder().build());
            return cluster.init();
        }*/
private Cluster cluster() {
    EventStoreConfig properties = eventStoreConfig;
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withPort(properties.getPort());
    if (properties.getUsername() != null) {
        builder.withCredentials(properties.getUsername(), properties.getPassword());
    }
    if (properties.getCompression() != null) {
        builder.withCompression(properties.getCompression());
    }
    if (properties.getLoadBalancingPolicy() != null) {
        LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
        builder.withLoadBalancingPolicy(policy);
    }
    builder.withQueryOptions(getQueryOptions());
    if (properties.getReconnectionPolicy() != null) {
        ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
        builder.withReconnectionPolicy(policy);
    }
    if (properties.getRetryPolicy() != null) {
        RetryPolicy policy = instantiate(properties.getRetryPolicy());
        builder.withRetryPolicy(policy);
    }
    builder.withSocketOptions(getSocketOptions());
    if (properties.isSsl()) {
        builder.withSSL();
    }
    String points = properties.getContactPoints();
    builder.addContactPointsWithPorts(Arrays.stream(StringUtils.split(points, ",")).map(s -> {
        String[] split = s.split(":");
        String host = split[0];
        int port = properties.getPort();
        try {
            port = Integer.parseInt(split[1]);
        } catch (Exception e) {
            log.trace(e.getMessage());
        }
        return new InetSocketAddress(host, port);
    }).collect(Collectors.toList()));
    return builder.build();
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) InetSocketAddress(java.net.InetSocketAddress) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy)

Aggregations

ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)11 Cluster (com.datastax.driver.core.Cluster)7 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)4 RetryPolicy (com.datastax.driver.core.policies.RetryPolicy)4 Test (org.junit.Test)4 IntegrationTest (tech.sirwellington.alchemy.annotations.testing.IntegrationTest)4 Bean (org.springframework.context.annotation.Bean)3 ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)2 ExponentialReconnectionPolicy (com.datastax.driver.core.policies.ExponentialReconnectionPolicy)2 Test (org.testng.annotations.Test)2 Session (com.datastax.driver.core.Session)1 TupleType (com.datastax.driver.core.TupleType)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 ReconnectionSchedule (com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule)1 ZonedDateTimeCodec (com.datastax.driver.extras.codecs.jdk8.ZonedDateTimeCodec)1 InetSocketAddress (java.net.InetSocketAddress)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1