Search in sources :

Example 11 with Session

use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.

the class DefaultSessionPoolsTest method should_close_pool_if_recreated_while_closing.

@Test
public void should_close_pool_if_recreated_while_closing() {
    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();
    Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
    assertThat(((DefaultSession) 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);
    // but the session gets closed before pool init completes
    CompletionStage<Void> closeFuture = session.closeAsync();
    assertThatStage(closeFuture).isSuccess();
    // now pool init completes
    pool2Future.complete(pool2);
    // Pool should have been closed
    verify(pool2, VERIFY_TIMEOUT).forceCloseAsync();
}
Also used : ChannelPool(com.datastax.oss.driver.internal.core.pool.ChannelPool) CompletableFuture(java.util.concurrent.CompletableFuture) 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 12 with Session

use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.

the class ConnectIT method should_wait_for_contact_points_if_reconnection_enabled.

@Test
public void should_wait_for_contact_points_if_reconnection_enabled() throws Exception {
    // Given
    SIMULACRON_RULE.cluster().rejectConnections(0, RejectScope.STOP);
    // When
    DriverConfigLoader loader = SessionUtils.configLoaderBuilder().withBoolean(DefaultDriverOption.RECONNECT_ON_INIT, true).withClass(DefaultDriverOption.RECONNECTION_POLICY_CLASS, InitOnlyReconnectionPolicy.class).withDuration(DefaultDriverOption.RECONNECTION_BASE_DELAY, Duration.ofMillis(500)).build();
    CompletableFuture<? extends Session> sessionFuture = newSessionAsync(loader).toCompletableFuture();
    // wait a bit to ensure we have a couple of reconnections, otherwise we might race and allow
    // reconnections before the initial attempt
    TimeUnit.SECONDS.sleep(2);
    // Then
    assertThat(sessionFuture).isNotCompleted();
    // When
    SIMULACRON_RULE.cluster().acceptConnections();
    // Then this doesn't throw
    try (Session session = sessionFuture.get(30, TimeUnit.SECONDS)) {
        assertThat(session.getMetadata().getKeyspaces()).containsKey(CqlIdentifier.fromCql("test"));
    }
}
Also used : DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) Test(org.junit.Test)

Example 13 with Session

use of com.datastax.oss.driver.api.core.session.Session in project java-driver by datastax.

the class SessionLeakIT method should_never_warn_when_session_init_fails.

@Test
public void should_never_warn_when_session_init_fails() {
    SIMULACRON_RULE.cluster().prime(PrimeDsl.when("USE \"non_existent_keyspace\"").then(PrimeDsl.invalid("irrelevant")));
    int threshold = 4;
    // Set the config option explicitly, in case it gets overridden in the test application.conf:
    DriverConfigLoader configLoader = DriverConfigLoader.programmaticBuilder().withInt(DefaultDriverOption.SESSION_LEAK_THRESHOLD, threshold).build();
    // Go over the threshold, no warnings expected
    for (int i = 0; i < threshold + 1; i++) {
        try (Session session = SessionUtils.newSession(SIMULACRON_RULE, CqlIdentifier.fromCql("non_existent_keyspace"), configLoader)) {
            fail("Session %s should have failed to initialize", session.getName());
        } catch (InvalidKeyspaceException e) {
            assertThat(e.getMessage()).isEqualTo("Invalid keyspace non_existent_keyspace");
        }
    }
    verify(appender, never()).doAppend(any());
}
Also used : InvalidKeyspaceException(com.datastax.oss.driver.api.core.InvalidKeyspaceException) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Session(com.datastax.oss.driver.api.core.session.Session) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) Test(org.junit.Test)

Example 14 with Session

use of com.datastax.oss.driver.api.core.session.Session 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 15 with Session

use of com.datastax.oss.driver.api.core.session.Session 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

CqlSession (com.datastax.oss.driver.api.core.CqlSession)18 Session (com.datastax.oss.driver.api.core.session.Session)18 Test (org.junit.Test)18 ChannelPool (com.datastax.oss.driver.internal.core.pool.ChannelPool)13 DistanceEvent (com.datastax.oss.driver.internal.core.metadata.DistanceEvent)6 CompletableFuture (java.util.concurrent.CompletableFuture)4 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)3 DefaultSession (com.datastax.oss.driver.internal.core.session.DefaultSession)3 Node (com.datastax.oss.driver.api.core.metadata.Node)2 Metric (com.codahale.metrics.Metric)1 ReactiveRow (com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow)1 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 InvalidKeyspaceException (com.datastax.oss.driver.api.core.InvalidKeyspaceException)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)1 DriverContext (com.datastax.oss.driver.api.core.context.DriverContext)1 LoadBalancingPolicy (com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy)1 NodeDistance (com.datastax.oss.driver.api.core.loadbalancing.NodeDistance)1 DefaultNodeMetric (com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric)1 Metrics (com.datastax.oss.driver.api.core.metrics.Metrics)1