Search in sources :

Example 16 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolReconnectTest method should_reconnect_when_channel_starts_graceful_shutdown.

@Test
public void should_reconnect_when_channel_starts_graceful_shutdown() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    DriverChannel channel3 = newMockDriverChannel(3);
    CompletableFuture<DriverChannel> channel3Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).pending(node, channel3Future).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    // Simulate graceful shutdown on channel2
    ((ChannelPromise) channel2.closeStartedFuture()).setSuccess();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    factoryHelper.waitForCall(node);
    channel3Future.complete(channel3);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel3));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) ChannelPromise(io.netty.channel.ChannelPromise) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 17 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolReconnectTest method should_let_current_attempt_complete_when_reconnecting_now.

@Test
public void should_let_current_attempt_complete_when_reconnecting_now() throws ExecutionException, InterruptedException {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(1);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).pending(node, channel2Future).build();
    InOrder inOrder = inOrder(eventBus);
    // Initial connection
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 1);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(1)).fire(ChannelEvent.channelOpened(node));
    // Kill channel1, reconnection begins and starts initializing channel2, but the initialization
    // is still pending (channel2Future not completed)
    ((ChannelPromise) channel1.closeStartedFuture()).setSuccess();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelClosed(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    factoryHelper.waitForCalls(node, 1);
    // Force a reconnection, should not try to create a new channel since we have a pending one
    pool.reconnectNow();
    TimeUnit.MILLISECONDS.sleep(200);
    factoryHelper.verifyNoMoreCalls();
    inOrder.verify(eventBus, never()).fire(any());
    // Complete the initialization of channel2, reconnection succeeds
    channel2Future.complete(channel2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel2));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) ChannelPromise(io.netty.channel.ChannelPromise) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 18 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolResizeTest method should_shrink_outside_of_reconnection.

@Test
public void should_shrink_outside_of_reconnection() throws Exception {
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE)).thenReturn(4);
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    DriverChannel channel3 = newMockDriverChannel(3);
    DriverChannel channel4 = newMockDriverChannel(4);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).success(node, channel4).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.REMOTE, context, "test");
    factoryHelper.waitForCalls(node, 4);
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(4)).fire(ChannelEvent.channelOpened(node));
    pool.resize(NodeDistance.LOCAL);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelClosed(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel3, channel4));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) InOrder(org.mockito.InOrder) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 19 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolResizeTest method should_resize_during_reconnection_if_config_changes.

@Test
public void should_resize_during_reconnection_if_config_changes() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    CompletableFuture<DriverChannel> channel2Future = new CompletableFuture<>();
    DriverChannel channel3 = newMockDriverChannel(3);
    CompletableFuture<DriverChannel> channel3Future = new CompletableFuture<>();
    DriverChannel channel4 = newMockDriverChannel(4);
    CompletableFuture<DriverChannel> channel4Future = new CompletableFuture<>();
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).failure(node, "mock channel init failure").pending(node, channel2Future).pending(node, channel3Future).pending(node, channel4Future).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1);
    // A reconnection should have been scheduled to add the missing channel, don't complete yet
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    // Simulate a configuration change
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(4);
    eventBus.fire(ConfigChangeEvent.INSTANCE);
    TimeUnit.MILLISECONDS.sleep(200);
    // Complete the channel for the first reconnection, bringing the count to 2
    channel2Future.complete(channel2);
    factoryHelper.waitForCall(node);
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.channelOpened(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2));
    // A second attempt should have been scheduled since we're now still under the target size
    verify(reconnectionSchedule, VERIFY_TIMEOUT.times(2)).nextDelay();
    // Same reconnection is still running, no additional events
    inOrder.verify(eventBus, never()).fire(ChannelEvent.reconnectionStopped(node));
    inOrder.verify(eventBus, never()).fire(ChannelEvent.reconnectionStarted(node));
    // Two more channels get opened, bringing us to the target count
    factoryHelper.waitForCalls(node, 2);
    channel3Future.complete(channel3);
    channel4Future.complete(channel4);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) CompletableFuture(java.util.concurrent.CompletableFuture) InOrder(org.mockito.InOrder) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Example 20 with DriverChannel

use of com.datastax.oss.driver.internal.core.channel.DriverChannel in project java-driver by datastax.

the class ChannelPoolResizeTest method should_grow_outside_of_reconnection.

@Test
public void should_grow_outside_of_reconnection() throws Exception {
    when(reconnectionSchedule.nextDelay()).thenReturn(Duration.ofNanos(1));
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE)).thenReturn(2);
    when(defaultProfile.getInt(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE)).thenReturn(4);
    DriverChannel channel1 = newMockDriverChannel(1);
    DriverChannel channel2 = newMockDriverChannel(2);
    DriverChannel channel3 = newMockDriverChannel(3);
    DriverChannel channel4 = newMockDriverChannel(4);
    MockChannelFactoryHelper factoryHelper = MockChannelFactoryHelper.builder(channelFactory).success(node, channel1).success(node, channel2).success(node, channel3).success(node, channel4).build();
    InOrder inOrder = inOrder(eventBus);
    CompletionStage<ChannelPool> poolFuture = ChannelPool.init(node, null, NodeDistance.LOCAL, context, "test");
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    assertThatStage(poolFuture).isSuccess();
    ChannelPool pool = poolFuture.toCompletableFuture().get();
    assertThat(pool.channels).containsOnly(channel1, channel2);
    pool.resize(NodeDistance.REMOTE);
    // The resizing should have triggered a reconnection
    verify(reconnectionSchedule, VERIFY_TIMEOUT).nextDelay();
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStarted(node));
    factoryHelper.waitForCalls(node, 2);
    inOrder.verify(eventBus, VERIFY_TIMEOUT.times(2)).fire(ChannelEvent.channelOpened(node));
    inOrder.verify(eventBus, VERIFY_TIMEOUT).fire(ChannelEvent.reconnectionStopped(node));
    await().untilAsserted(() -> assertThat(pool.channels).containsOnly(channel1, channel2, channel3, channel4));
    factoryHelper.verifyNoMoreCalls();
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) InOrder(org.mockito.InOrder) MockChannelFactoryHelper(com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper) Test(org.junit.Test)

Aggregations

DriverChannel (com.datastax.oss.driver.internal.core.channel.DriverChannel)54 Test (org.junit.Test)35 MockChannelFactoryHelper (com.datastax.oss.driver.internal.core.channel.MockChannelFactoryHelper)30 CompletableFuture (java.util.concurrent.CompletableFuture)17 InOrder (org.mockito.InOrder)13 Node (com.datastax.oss.driver.api.core.metadata.Node)9 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)6 DriverChannelOptions (com.datastax.oss.driver.internal.core.channel.DriverChannelOptions)6 ChannelPromise (io.netty.channel.ChannelPromise)5 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)4 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)4 InetSocketAddress (java.net.InetSocketAddress)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 NodeUnavailableException (com.datastax.oss.driver.api.core.NodeUnavailableException)3 AdminResult (com.datastax.oss.driver.internal.core.adminrequest.AdminResult)3 EventCallback (com.datastax.oss.driver.internal.core.channel.EventCallback)3 Before (org.junit.Before)3 Version (com.datastax.oss.driver.api.core.Version)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)2 AdminRow (com.datastax.oss.driver.internal.core.adminrequest.AdminRow)2