use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project java-driver by datastax.
the class RefreshConnectedHostTest method should_refresh_single_connected_host.
/**
* Tests {@link PoolingOptions#refreshConnectedHost(Host)} through a custom load balancing policy.
*/
@Test(groups = "long")
public void should_refresh_single_connected_host() {
// This will make the driver use at most 2 hosts, the others will be ignored
LimitingLoadBalancingPolicy loadBalancingPolicy = new LimitingLoadBalancingPolicy(new RoundRobinPolicy(), 2, 1);
PoolingOptions poolingOptions = Mockito.spy(new PoolingOptions());
Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0)).withPort(ccm().getBinaryPort()).withPoolingOptions(poolingOptions).withLoadBalancingPolicy(loadBalancingPolicy).withReconnectionPolicy(new ConstantReconnectionPolicy(1000)).withQueryOptions(TestUtils.nonDebouncingQueryOptions()).build());
Session session = cluster.connect();
assertThat(cluster).usesControlHost(1);
assertThat(cluster).host(1).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
// Wait for the node to be up, because apparently on Jenkins it's still only ADDED when we reach this line
// Waiting for NEW_NODE_DELAY_SECONDS+1 allows the driver to create a connection pool and mark the node up
assertThat(cluster).host(2).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS + 1, SECONDS).isAtDistance(HostDistance.LOCAL);
// Add and bring host 3 up, its presence should be acknowledged but it should be ignored
ccm().add(3);
ccm().start(3);
ccm().waitForUp(3);
assertThat(cluster).host(1).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
assertThat(cluster).host(2).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
// Ensure that the host is added to the Cluster.
assertThat(cluster).host(3).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS + 1, SECONDS).isAtDistance(HostDistance.IGNORED);
assertThat(session).hasNoPoolFor(3);
// Kill host 2, host 3 should take its place
ccm().stop(2);
TestUtils.waitForUp(TestUtils.ipOfNode(3), cluster);
assertThat(cluster).host(1).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
assertThat(cluster).host(2).hasState(State.DOWN);
assertThat(cluster).host(3).hasState(State.UP).isAtDistance(HostDistance.LOCAL);
assertThat(session).hasPoolFor(3);
// This is when refreshConnectedHost should have been invoked, it triggers pool creation when
// we switch the node from IGNORED to UP:
Mockito.verify(poolingOptions).refreshConnectedHost(TestUtils.findHost(cluster, 3));
}
use of com.datastax.driver.core.policies.ConstantReconnectionPolicy 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.ConstantReconnectionPolicy in project java-driver by datastax.
the class ReconnectionTest method should_use_connection_from_reconnection_in_pool.
/**
* The connection established by a successful reconnection attempt should be reused in one of the
* connection pools (JAVA-505).
*/
@CCMConfig(dirtiesContext = true, createCluster = false)
@Test(groups = "long")
public void should_use_connection_from_reconnection_in_pool() {
TogglabePolicy loadBalancingPolicy = new TogglabePolicy(new RoundRobinPolicy());
// Spy SocketOptions.getKeepAlive to count how many connections were instantiated.
SocketOptions socketOptions = spy(new SocketOptions());
Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0)).withPort(ccm().getBinaryPort()).withReconnectionPolicy(new ConstantReconnectionPolicy(5000)).withLoadBalancingPolicy(loadBalancingPolicy).withSocketOptions(socketOptions).withProtocolVersion(ccm().getProtocolVersion()).build());
// Create two sessions to have multiple pools
cluster.connect();
cluster.connect();
int corePoolSize = TestUtils.numberOfLocalCoreConnections(cluster);
// Right after init, 1 connection has been opened by the control connection, and the core size for each pool.
verify(socketOptions, times(1 + corePoolSize * 2)).getKeepAlive();
// Tweak the LBP so that the control connection never reconnects. This makes it easier
// to reason about the number of connection attempts.
loadBalancingPolicy.returnEmptyQueryPlan = true;
// Stop the node and cancel the reconnection attempts to it
ccm().stop(1);
ccm().waitForDown(1);
assertThat(cluster).host(1).goesDownWithin(20, SECONDS);
Host host1 = TestUtils.findHost(cluster, 1);
host1.getReconnectionAttemptFuture().cancel(false);
ccm().start(1);
ccm().waitForUp(1);
// Reset the spy and count the number of connections attempts for 1 reconnect
reset(socketOptions);
host1.tryReconnectOnce();
assertThat(cluster).host(1).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS * 2, SECONDS);
// Expect 1 connection from the reconnection attempt 3 for the pools (we need 4
// but the one from the reconnection attempt gets reused).
verify(socketOptions, times(corePoolSize * 2)).getKeepAlive();
}
use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project java-driver by datastax.
the class ReconnectionTest method should_reconnect_after_full_connectivity_loss.
@CCMConfig(dirtiesContext = true, numberOfNodes = 2, createCluster = false)
@Test(groups = "long")
public void should_reconnect_after_full_connectivity_loss() throws InterruptedException {
Cluster cluster = register(Cluster.builder().addContactPoints(getContactPoints().get(0)).withPort(ccm().getBinaryPort()).withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis)).build());
cluster.connect();
assertThat(cluster).usesControlHost(1);
// Stop all nodes. We won't get notifications anymore, so the only mechanism to
// reconnect is the background reconnection attempts.
ccm().stop(2);
ccm().stop(1);
ccm().waitForDown(2);
ccm().start(2);
ccm().waitForUp(2);
assertThat(cluster).host(2).comesUpWithin(Cluster.NEW_NODE_DELAY_SECONDS * 2, SECONDS);
// Give the control connection a few moments to reconnect
TimeUnit.MILLISECONDS.sleep(reconnectionDelayMillis * 2);
assertThat(cluster).usesControlHost(2);
}
use of com.datastax.driver.core.policies.ConstantReconnectionPolicy in project java-driver by datastax.
the class ClusterInitTest method should_not_schedule_reconnections_before_init_complete.
/**
* Validates that if hosts are unreachable during Cluster initialization, no background reconnection to them
* is scheduled before the initialization is complete.
*
* @test_category connection
* @jira_ticket JAVA-954
* @expected_result No reconnection scheduled.
*/
@Test(groups = "short", expectedExceptions = NoHostAvailableException.class)
public void should_not_schedule_reconnections_before_init_complete() {
// Both contact points time out so we're sure we'll try both of them and init will never complete.
List<FakeHost> hosts = Lists.newArrayList(new FakeHost(TestUtils.ipOfNode(0), 9042, THROWING_CONNECT_TIMEOUTS), new FakeHost(TestUtils.ipOfNode(1), 9042, THROWING_CONNECT_TIMEOUTS));
// Use a low reconnection interval and keep the default connect timeout (5 seconds). So if a reconnection was scheduled,
// we would see a call to the reconnection policy.
CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(100));
Cluster cluster = Cluster.builder().addContactPoints(hosts.get(0).address, hosts.get(1).address).withReconnectionPolicy(reconnectionPolicy).build();
try {
cluster.init();
} finally {
// We expect a nextDelay invocation from the ConvictionPolicy for each host, but that will
// not trigger a reconnection.
assertThat(reconnectionPolicy.count.get()).isEqualTo(2);
for (FakeHost fakeHost : hosts) {
fakeHost.stop();
}
cluster.close();
}
// We don't test that reconnections are scheduled if init succeeds, but that's covered in
// should_handle_failing_or_missing_contact_points
}
Aggregations