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

The following examples show how to use com.datastax.driver.core.policies.ConstantReconnectionPolicy. 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: CassandraClusterXmlConfigTest.java    From spring-cloud-connectors with Apache License 2.0 6 votes vote down vote up
@Test
public void cassandraSessionWithConfiguration() throws Exception {
	ApplicationContext testContext = getTestApplicationContext(
			"cloud-cassandra-with-config.xml", createService("my-service"));
	Cluster cluster = testContext.getBean("cassandra-full-config",
			getConnectorType());

	assertNotNull(cluster.getConfiguration().getSocketOptions());
	assertEquals(15000,
			cluster.getConfiguration().getSocketOptions().getConnectTimeoutMillis());
	assertTrue(DefaultRetryPolicy.class.isAssignableFrom(
			cluster.getConfiguration().getPolicies().getRetryPolicy().getClass()));
	assertTrue(RoundRobinPolicy.class.isAssignableFrom(cluster.getConfiguration()
			.getPolicies().getLoadBalancingPolicy().getClass()));
	assertTrue(ConstantReconnectionPolicy.class.isAssignableFrom(cluster
			.getConfiguration().getPolicies().getReconnectionPolicy().getClass()));
}
 
Example #2
Source File: UtilsUnitTest.java    From cassandra-jdbc-wrapper with Apache License 2.0 5 votes vote down vote up
@Test
public void testReconnectionPolicyParsing() throws Exception
{
	String retryPolicyStr = "ConstantReconnectionPolicy((long)10)";
	System.out.println(retryPolicyStr);
	assertTrue(Utils.parseReconnectionPolicy(retryPolicyStr) instanceof ConstantReconnectionPolicy);
	System.out.println("====================");
	retryPolicyStr = "ExponentialReconnectionPolicy((long)10,(Long)100)";
	System.out.println(retryPolicyStr);
	assertTrue(Utils.parseReconnectionPolicy(retryPolicyStr) instanceof ExponentialReconnectionPolicy);
	System.out.println("====================");

}
 
Example #3
Source File: ConstantReconnectionPolicyFactoryTest.java    From dropwizard-cassandra with Apache License 2.0 5 votes vote down vote up
@Test
public void buildsPolicyWithDelayInMillis() throws Exception {
    final ConstantReconnectionPolicyFactory factory = new ConstantReconnectionPolicyFactory();
    factory.setDelay(Duration.seconds(5));

    final ConstantReconnectionPolicy policy = (ConstantReconnectionPolicy) factory.build();

    assertThat(policy.getConstantDelayMs()).isEqualTo(5000L);
}
 
Example #4
Source File: CentralModule.java    From glowroot with Apache License 2.0 5 votes vote down vote up
private static Cluster createCluster(CentralConfiguration centralConfig,
        TimestampGenerator defaultTimestampGenerator) {
    Cluster.Builder builder = Cluster.builder()
            .addContactPoints(
                    centralConfig.cassandraContactPoint().toArray(new String[0]))
            .withPort(centralConfig.cassandraPort())
            // aggressive reconnect policy seems ok since not many clients
            .withReconnectionPolicy(new ConstantReconnectionPolicy(1000))
            // let driver know that only idempotent queries are used so it will retry on timeout
            .withQueryOptions(new QueryOptions()
                    .setDefaultIdempotence(true)
                    .setConsistencyLevel(centralConfig.cassandraReadConsistencyLevel()))
            .withPoolingOptions(new PoolingOptions()
                    .setMaxRequestsPerConnection(HostDistance.LOCAL,
                            centralConfig.cassandraMaxConcurrentQueries())
                    .setMaxRequestsPerConnection(HostDistance.REMOTE,
                            centralConfig.cassandraMaxConcurrentQueries())
                    // using 2x "max requests per connection", so that thread-based
                    // throttling can allow up to 3x "max requests per connection", which is
                    // split 50% writes / 25% reads / 25% rollups, which will keep the pool
                    // saturated with writes alone
                    .setMaxQueueSize(centralConfig.cassandraMaxConcurrentQueries() * 2)
                    .setPoolTimeoutMillis(centralConfig.cassandraPoolTimeoutMillis()))
            .withTimestampGenerator(defaultTimestampGenerator);
    String cassandraUsername = centralConfig.cassandraUsername();
    if (!cassandraUsername.isEmpty()) {
        // empty password is strange but valid
        builder.withCredentials(cassandraUsername, centralConfig.cassandraPassword());
    }
    if (centralConfig.cassandraSSL()) {
    	builder.withSSL();
    }
    return builder.build();
}
 
Example #5
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 #6
Source File: CassandraClusterCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateClusterWithConfig() throws Exception {

	CassandraServiceInfo info = new CassandraServiceInfo("local",
			Collections.singletonList("127.0.0.1"), 9142);

	CassandraClusterConfig config = new CassandraClusterConfig();
	config.setCompression(ProtocolOptions.Compression.NONE);
	config.setPoolingOptions(new PoolingOptions().setPoolTimeoutMillis(1234));
	config.setQueryOptions(new QueryOptions());
	config.setProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED);
	config.setLoadBalancingPolicy(new RoundRobinPolicy());
	config.setReconnectionPolicy(new ConstantReconnectionPolicy(1));
	config.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
	config.setSocketOptions(new SocketOptions());

	Cluster cluster = creator.create(info, config);

	Configuration configuration = cluster.getConfiguration();

	assertThat(configuration.getProtocolOptions().getCompression(),
			is(config.getCompression()));
	assertThat(configuration.getQueryOptions(), is(config.getQueryOptions()));
	assertThat(configuration.getSocketOptions(), is(config.getSocketOptions()));

	Policies policies = configuration.getPolicies();
	assertThat(policies.getLoadBalancingPolicy(),
			is(config.getLoadBalancingPolicy()));
	assertThat(policies.getReconnectionPolicy(), is(config.getReconnectionPolicy()));
	assertThat(policies.getRetryPolicy(), is(config.getRetryPolicy()));
}
 
Example #7
Source File: BackplaneConfiguration.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void initialize() {
    String cassandraHosts = env.getProperty("ea.cassandra.hosts","localhost:9042");
    String cassandraClusterName = env.getProperty("ea.cassandra.cluster","ElasticActorsCluster");
    String cassandraKeyspaceName = env.getProperty("ea.cassandra.keyspace","\"ElasticActors\"");
    Integer cassandraPort = env.getProperty("ea.cassandra.port", Integer.class, 9042);

    Set<String> hostSet = StringUtils.commaDelimitedListToSet(cassandraHosts);

    String[] contactPoints = new String[hostSet.size()];
    int i=0;
    for (String host : hostSet) {
        if(host.contains(":")) {
            contactPoints[i] = host.substring(0,host.indexOf(":"));
        } else {
            contactPoints[i] = host;
        }
        i+=1;
    }

    PoolingOptions poolingOptions = new PoolingOptions();
    poolingOptions.setHeartbeatIntervalSeconds(60);
    poolingOptions.setConnectionsPerHost(HostDistance.LOCAL, 2, env.getProperty("ea.cassandra.maxActive",Integer.class,Runtime.getRuntime().availableProcessors() * 3));
    poolingOptions.setPoolTimeoutMillis(2000);

    Cluster cassandraCluster =
            Cluster.builder().withClusterName(cassandraClusterName)
                    .addContactPoints(contactPoints)
                    .withPort(cassandraPort)
            .withLoadBalancingPolicy(new RoundRobinPolicy())
            .withRetryPolicy(new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE))
            .withPoolingOptions(poolingOptions)
            .withReconnectionPolicy(new ConstantReconnectionPolicy(env.getProperty("ea.cassandra.retryDownedHostsDelayInSeconds",Integer.class,1) * 1000))
            .withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.QUORUM)).build();

    this.cassandraSession = cassandraCluster.connect(cassandraKeyspaceName);

    
}
 
Example #8
Source File: ConstantReconnectionPolicyFactory.java    From dropwizard-cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public ReconnectionPolicy build() {
    return new ConstantReconnectionPolicy(delay.toMilliseconds());
}