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();
}
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;
}
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);
}
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);
}
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();
}
Aggregations