Search in sources :

Example 1 with DistanceEvent

use of com.datastax.oss.driver.internal.core.metadata.DistanceEvent in project java-driver by datastax.

the class ControlConnectionTest method should_reconnect_if_node_becomes_ignored.

@Test
public void should_reconnect_if_node_becomes_ignored() {
    // Given
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).success(node2, channel2).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    // When
    mockQueryPlan(node2);
    eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node1));
    // Then
    // an immediate reconnection was started
    factoryHelper.waitForCall(node2);
    await().untilAsserted(() -> assertThat(controlConnection.channel()).isEqualTo(channel2));
    verify(reconnectionSchedule, never()).nextDelay();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node2));
    verify(metadataManager, VERIFY_TIMEOUT).refreshNodes();
    verify(loadBalancingPolicyWrapper, VERIFY_TIMEOUT).init();
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 2 with DistanceEvent

use of com.datastax.oss.driver.internal.core.metadata.DistanceEvent in project java-driver by datastax.

the class ControlConnectionTest method should_reconnect_if_node_became_ignored_during_reconnection_attempt.

@Test
public void should_reconnect_if_node_became_ignored_during_reconnection_attempt() {
    // Given
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    DriverChannel channel3 = newMockDriverChannel(3);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node1, channel1).pending(node2, channel2Future).success(node1, channel3).build();
    CompletionStage<Void> initFuture = controlConnection.init(false, false, false);
    factoryHelper.waitForCall(node1);
    assertThatStage(initFuture).isSuccess(v -> assertThat(controlConnection.channel()).isEqualTo(channel1));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node1));
    mockQueryPlan(node2, node1);
    // channel1 goes down, triggering a reconnection
    channel1.close();
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node1));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    // the reconnection to node2 is in progress
    factoryHelper.waitForCall(node2);
    // When
    // node2 becomes ignored
    eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node2));
    // the reconnection to node2 completes
    channel2Future.complete(channel2);
    // Then
    // The channel should get closed and we should try the next node
    verify(channel2, VERIFY_TIMEOUT).forceClose();
    factoryHelper.waitForCall(node1);
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 3 with DistanceEvent

use of com.datastax.oss.driver.internal.core.metadata.DistanceEvent in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_remove_pool_if_forced_down_while_recreating.

@Test
public void should_remove_pool_if_forced_down_while_recreating() {
    when(node2.getDistance()).thenReturn(NodeDistance.IGNORED);
    ChannelPool pool1 = mockPool(node1);
    ChannelPool pool2 = mockPool(node2);
    CompletableFuture<ChannelPool> pool2Future = new CompletableFuture<>();
    ChannelPool pool3 = mockPool(node3);
    MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).pending(node2, KEYSPACE, NodeDistance.LOCAL, pool2Future).build();
    CompletionStage<CqlSession> initFuture = newSession();
    factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
    assertThatStage(initFuture).isSuccess();
    Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3);
    eventBus.fire(new DistanceEvent(NodeDistance.LOCAL, node2));
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    // Forced down while pool init is in progress
    eventBus.fire(NodeStateEvent.changed(NodeState.UP, NodeState.FORCED_DOWN, node2));
    // Now pool init succeeds
    pool2Future.complete(pool2);
    // Pool should have been closed
    verify(pool2, VERIFY_TIMEOUT).closeAsync();
    await().untilAsserted(() -> assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3));
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CompletableFuture(java.util.concurrent.CompletableFuture) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test)

Example 4 with DistanceEvent

use of com.datastax.oss.driver.internal.core.metadata.DistanceEvent in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_recreate_pool_if_node_becomes_not_ignored.

@Test
public void should_recreate_pool_if_node_becomes_not_ignored() {
    when(node2.getDistance()).thenReturn(NodeDistance.IGNORED);
    ChannelPool pool1 = mockPool(node1);
    ChannelPool pool2 = mockPool(node2);
    ChannelPool pool3 = mockPool(node3);
    MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).success(node2, KEYSPACE, NodeDistance.LOCAL, pool2).build();
    CompletionStage<CqlSession> initFuture = newSession();
    factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
    assertThatStage(initFuture).isSuccess();
    Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3);
    eventBus.fire(new DistanceEvent(NodeDistance.LOCAL, node2));
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    await().untilAsserted(() -> assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool2, pool3));
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test)

Example 5 with DistanceEvent

use of com.datastax.oss.driver.internal.core.metadata.DistanceEvent in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_remove_pool_if_ignored_while_recreating.

@Test
public void should_remove_pool_if_ignored_while_recreating() {
    when(node2.getDistance()).thenReturn(NodeDistance.IGNORED);
    ChannelPool pool1 = mockPool(node1);
    ChannelPool pool2 = mockPool(node2);
    CompletableFuture<ChannelPool> pool2Future = new CompletableFuture<>();
    ChannelPool pool3 = mockPool(node3);
    MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).pending(node2, KEYSPACE, NodeDistance.LOCAL, pool2Future).build();
    CompletionStage<CqlSession> initFuture = newSession();
    factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
    assertThatStage(initFuture).isSuccess();
    Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3);
    eventBus.fire(new DistanceEvent(NodeDistance.LOCAL, node2));
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    // Distance changes to ignored while pool init is in progress
    eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node2));
    // Now pool init succeeds
    pool2Future.complete(pool2);
    // Pool should have been closed
    verify(pool2, VERIFY_TIMEOUT).closeAsync();
    await().untilAsserted(() -> assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3));
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CompletableFuture(java.util.concurrent.CompletableFuture) DistanceEvent(com.datastax.oss.driver.internal.core.metadata.DistanceEvent) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test)

Aggregations

DistanceEvent (com.datastax.oss.driver.internal.core.metadata.DistanceEvent)11 Test (org.junit.Test)11 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)9 Session (com.datastax.oss.driver.api.core.session.Session)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)2 MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)2