Search in sources :

Example 16 with ChannelPool

use of com.datastax.oss.driver.internal.core.pool.ChannelPool in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_remove_pool_if_ignored_while_init.

@Test
public void should_remove_pool_if_ignored_while_init() {
    CompletableFuture<ChannelPool> pool1Future = new CompletableFuture<>();
    CompletableFuture<ChannelPool> pool2Future = new CompletableFuture<>();
    CompletableFuture<ChannelPool> pool3Future = new CompletableFuture<>();
    ChannelPool pool1 = mockPool(node1);
    ChannelPool pool2 = mockPool(node2);
    ChannelPool pool3 = mockPool(node3);
    MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).pending(node1, KEYSPACE, NodeDistance.LOCAL, pool1Future).pending(node2, KEYSPACE, NodeDistance.LOCAL, pool2Future).pending(node3, KEYSPACE, NodeDistance.LOCAL, pool3Future).build();
    CompletionStage<CqlSession> initFuture = newSession();
    factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
    assertThatStage(initFuture).isNotDone();
    // Distance changes while init still pending
    eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node2));
    pool1Future.complete(pool1);
    pool2Future.complete(pool2);
    pool3Future.complete(pool3);
    verify(pool2, VERIFY_TIMEOUT).closeAsync();
    assertThatStage(initFuture).isSuccess(session -> 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) Test(org.junit.Test)

Example 17 with ChannelPool

use of com.datastax.oss.driver.internal.core.pool.ChannelPool in project java-driver by datastax.

the class DefaultSessionPoolsTest method mockPool.

private ChannelPool mockPool(Node node) {
    ChannelPool pool = mock(ChannelPool.class);
    when(pool.getNode()).thenReturn(node);
    when(pool.getInitialKeyspaceName()).thenReturn(KEYSPACE);
    when(pool.setKeyspace(any(CqlIdentifier.class))).thenReturn(CompletableFuture.completedFuture(null));
    CompletableFuture<Void> closeFuture = new CompletableFuture<>();
    when(pool.closeFuture()).thenReturn(closeFuture);
    when(pool.closeAsync()).then(i -> {
        closeFuture.complete(null);
        return closeFuture;
    });
    when(pool.forceCloseAsync()).then(i -> {
        closeFuture.complete(null);
        return closeFuture;
    });
    return pool;
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CompletableFuture(java.util.concurrent.CompletableFuture) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 18 with ChannelPool

use of com.datastax.oss.driver.internal.core.pool.ChannelPool in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_set_keyspace_on_pool_if_recreated_while_switching_keyspace.

@Test
public void should_set_keyspace_on_pool_if_recreated_while_switching_keyspace() {
    when(node2.getState()).thenReturn(NodeState.FORCED_DOWN);
    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();
    DefaultSession session = (DefaultSession) CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    assertThat(session.getPools()).containsValues(pool1, pool3);
    // node2 comes back up, start initializing a pool for it
    eventBus.fire(NodeStateEvent.changed(NodeState.FORCED_DOWN, NodeState.UP, node2));
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    // Keyspace gets changed on the session in the meantime, node2's pool will miss it
    CqlIdentifier newKeyspace = CqlIdentifier.fromInternal("newKeyspace");
    session.setKeyspace(newKeyspace);
    verify(pool1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(pool3, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    // now pool init completes
    pool2Future.complete(pool2);
    // Pool should have been closed
    verify(pool2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CompletableFuture(java.util.concurrent.CompletableFuture) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 19 with ChannelPool

use of com.datastax.oss.driver.internal.core.pool.ChannelPool in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_force_close_all_pools_when_force_closing.

@Test
public void should_force_close_all_pools_when_force_closing() {
    ChannelPool pool1 = mockPool(node1);
    ChannelPool pool2 = mockPool(node2);
    ChannelPool pool3 = mockPool(node3);
    MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node2, KEYSPACE, NodeDistance.LOCAL, pool2).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).build();
    CompletionStage<CqlSession> initFuture = newSession();
    factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
    assertThatStage(initFuture).isSuccess();
    Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    CompletionStage<Void> closeFuture = session.forceCloseAsync();
    assertThatStage(closeFuture).isSuccess();
    verify(pool1, VERIFY_TIMEOUT).forceCloseAsync();
    verify(pool2, VERIFY_TIMEOUT).forceCloseAsync();
    verify(pool3, VERIFY_TIMEOUT).forceCloseAsync();
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) 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 20 with ChannelPool

use of com.datastax.oss.driver.internal.core.pool.ChannelPool in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_set_keyspace_on_all_pools.

@Test
public void should_set_keyspace_on_all_pools() {
    ChannelPool pool1 = mockPool(node1);
    ChannelPool pool2 = mockPool(node2);
    ChannelPool pool3 = mockPool(node3);
    MockChannelPoolFactoryHelper factoryHelper = MockChannelPoolFactoryHelper.builder(channelPoolFactory).success(node1, KEYSPACE, NodeDistance.LOCAL, pool1).success(node2, KEYSPACE, NodeDistance.LOCAL, pool2).success(node3, KEYSPACE, NodeDistance.LOCAL, pool3).build();
    CompletionStage<CqlSession> initFuture = newSession();
    factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
    factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
    assertThatStage(initFuture).isSuccess();
    Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    CqlIdentifier newKeyspace = CqlIdentifier.fromInternal("newKeyspace");
    ((DefaultSession) session).setKeyspace(newKeyspace);
    verify(pool1, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(pool2, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
    verify(pool3, VERIFY_TIMEOUT).setKeyspace(newKeyspace);
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test)

Aggregations

ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)25 CqlSession (com.datastax.oss.driver.api.core.CqlSession)23 Test (org.junit.Test)23 Session (com.datastax.oss.driver.api.core.session.Session)13 CompletableFuture (java.util.concurrent.CompletableFuture)10 DistanceEvent (com.datastax.oss.driver.internal.core.metadata.DistanceEvent)9 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 Node (com.datastax.oss.driver.api.core.metadata.Node)3 TokenRange (com.datastax.oss.driver.api.core.metadata.token.TokenRange)2 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)2 SessionStateForNode (com.datastax.dse.driver.internal.core.insights.schema.SessionStateForNode)1 EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 DefaultNode (com.datastax.oss.driver.internal.core.metadata.DefaultNode)1 PoolManager (com.datastax.oss.driver.internal.core.session.PoolManager)1 InetSocketAddress (java.net.InetSocketAddress)1