Search in sources :

Example 1 with Session

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

the class ReactiveRetryIT method should_retry_at_application_level.

@Test
public void should_retry_at_application_level() {
    // Given
    CqlSession session = spy(SESSION_RULE.session());
    BoundCluster cluster = SIMULACRON_RULE.cluster();
    cluster.node(0).prime(when(QUERY_STRING).then(unavailable(ConsistencyLevel.ONE, 1, 0)));
    cluster.node(1).prime(when(QUERY_STRING).then(unavailable(ConsistencyLevel.ONE, 1, 0)));
    cluster.node(2).prime(when(QUERY_STRING).then(rows().row("col1", "Yay!")));
    // When
    ReactiveRow row = Flowable.defer(() -> session.executeReactive(QUERY_STRING)).retry((retry, error) -> {
        assertThat(error).isInstanceOf(UnavailableException.class);
        UnavailableException ue = (UnavailableException) error;
        Node coordinator = ue.getCoordinator();
        if (retry == 1) {
            assertCoordinator(0, coordinator);
            return true;
        } else if (retry == 2) {
            assertCoordinator(1, coordinator);
            return true;
        } else {
            fail("Unexpected retry attempt");
            return false;
        }
    }).blockingLast();
    // Then
    assertThat(row.getString(0)).isEqualTo("Yay!");
    verify(session, times(3)).executeReactive(QUERY_STRING);
    assertUnavailableMetric(0, 1L);
    assertUnavailableMetric(1, 1L);
    assertUnavailableMetric(2, 0L);
}
Also used : Metrics(com.datastax.oss.driver.api.core.metrics.Metrics) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) DefaultNodeMetric(com.datastax.oss.driver.api.core.metrics.DefaultNodeMetric) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoadBalancingPolicy(com.datastax.oss.driver.api.core.loadbalancing.LoadBalancingPolicy) PrimeDsl.when(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.when) NodeDistance(com.datastax.oss.driver.api.core.loadbalancing.NodeDistance) SimulacronRule(com.datastax.oss.driver.api.testinfra.simulacron.SimulacronRule) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ClassRule(org.junit.ClassRule) DriverContext(com.datastax.oss.driver.api.core.context.DriverContext) SessionUtils(com.datastax.oss.driver.api.testinfra.session.SessionUtils) Metric(com.codahale.metrics.Metric) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) List(java.util.List) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Optional(java.util.Optional) Queue(java.util.Queue) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) Node(com.datastax.oss.driver.api.core.metadata.Node) ClusterSpec(com.datastax.oss.simulacron.common.cluster.ClusterSpec) Iterables(com.google.common.collect.Iterables) TestRule(org.junit.rules.TestRule) Mockito.spy(org.mockito.Mockito.spy) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) NoRetryPolicy(com.datastax.oss.driver.core.retry.PerProfileRetryPolicyIT.NoRetryPolicy) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) Flowable(io.reactivex.Flowable) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Before(org.junit.Before) Session(com.datastax.oss.driver.api.core.session.Session) Iterator(java.util.Iterator) Test(org.junit.Test) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) Request(com.datastax.oss.driver.api.core.session.Request) PrimeDsl.rows(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.rows) Mockito.verify(org.mockito.Mockito.verify) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) RuleChain(org.junit.rules.RuleChain) PrimeDsl.unavailable(com.datastax.oss.simulacron.common.stubbing.PrimeDsl.unavailable) ConsistencyLevel(com.datastax.oss.simulacron.common.codec.ConsistencyLevel) Nullable(edu.umd.cs.findbugs.annotations.Nullable) NodeComparator(com.datastax.oss.driver.api.testinfra.loadbalancing.NodeComparator) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) Node(com.datastax.oss.driver.api.core.metadata.Node) UnavailableException(com.datastax.oss.driver.api.core.servererrors.UnavailableException) ReactiveRow(com.datastax.dse.driver.api.core.cql.reactive.ReactiveRow) BoundCluster(com.datastax.oss.simulacron.server.BoundCluster) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 2 with Session

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

the class SessionLeakIT method should_warn_when_session_count_exceeds_threshold.

@Test
public void should_warn_when_session_count_exceeds_threshold() {
    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();
    Set<CqlSession> sessions = new HashSet<>();
    // Stay under the threshold, no warnings expected
    for (int i = 0; i < threshold; i++) {
        sessions.add(SessionUtils.newSession(SIMULACRON_RULE, configLoader));
    }
    verify(appender, never()).doAppend(any());
    // Go over the threshold, 1 warning for every new session
    sessions.add(SessionUtils.newSession(SIMULACRON_RULE, configLoader));
    verify(appender).doAppend(loggingEventCaptor.capture());
    assertThat(loggingEventCaptor.getValue().getFormattedMessage()).contains("You have too many session instances: 5 active, expected less than 4");
    reset(appender);
    sessions.add(SessionUtils.newSession(SIMULACRON_RULE, configLoader));
    verify(appender).doAppend(loggingEventCaptor.capture());
    assertThat(loggingEventCaptor.getValue().getFormattedMessage()).contains("You have too many session instances: 6 active, expected less than 4");
    // Go back under the threshold, no warnings expected
    sessions.forEach(Session::close);
    sessions.clear();
    reset(appender);
    CqlSession session = SessionUtils.newSession(SIMULACRON_RULE, configLoader);
    verify(appender, never()).doAppend(any());
    session.close();
}
Also used : DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) CqlSession(com.datastax.oss.driver.api.core.CqlSession) HashSet(java.util.HashSet) 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 3 with Session

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

the class ChannelSocketOptionsIT method should_report_socket_options.

@Test
public void should_report_socket_options() {
    Session session = SESSION_RULE.session();
    DriverExecutionProfile config = session.getContext().getConfig().getDefaultProfile();
    assertThat(config.getBoolean(SOCKET_TCP_NODELAY)).isTrue();
    assertThat(config.getBoolean(SOCKET_KEEP_ALIVE)).isFalse();
    assertThat(config.getBoolean(SOCKET_REUSE_ADDRESS)).isFalse();
    assertThat(config.getInt(SOCKET_LINGER_INTERVAL)).isEqualTo(10);
    assertThat(config.getInt(SOCKET_RECEIVE_BUFFER_SIZE)).isEqualTo(123456);
    assertThat(config.getInt(SOCKET_SEND_BUFFER_SIZE)).isEqualTo(123456);
    Node node = session.getMetadata().getNodes().values().iterator().next();
    if (session instanceof SessionWrapper) {
        session = ((SessionWrapper) session).getDelegate();
    }
    DriverChannel channel = ((DefaultSession) session).getChannel(node, "test");
    assertThat(channel).isNotNull();
    assertThat(channel.config()).isInstanceOf(SocketChannelConfig.class);
    SocketChannelConfig socketConfig = (SocketChannelConfig) channel.config();
    assertThat(socketConfig.isTcpNoDelay()).isTrue();
    assertThat(socketConfig.isKeepAlive()).isFalse();
    assertThat(socketConfig.isReuseAddress()).isFalse();
    assertThat(socketConfig.getSoLinger()).isEqualTo(10);
    RecvByteBufAllocator allocator = socketConfig.getRecvByteBufAllocator();
    assertThat(allocator).isInstanceOf(FixedRecvByteBufAllocator.class);
    assertThat(allocator.newHandle().guess()).isEqualTo(123456);
// cannot assert around SO_RCVBUF and SO_SNDBUF, such values are just hints
}
Also used : DriverChannel(com.datastax.oss.driver.internal.core.channel.DriverChannel) FixedRecvByteBufAllocator(io.netty.channel.FixedRecvByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) Node(com.datastax.oss.driver.api.core.metadata.Node) DefaultSession(com.datastax.oss.driver.internal.core.session.DefaultSession) 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) SessionWrapper(com.datastax.oss.driver.internal.core.session.SessionWrapper) Test(org.junit.Test)

Example 4 with Session

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

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

the class DefaultSessionPoolsTest method should_recreate_pool_if_node_is_forced_back_up.

@Test
public void should_recreate_pool_if_node_is_forced_back_up() {
    when(node2.getState()).thenReturn(NodeState.FORCED_DOWN);
    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(NodeStateEvent.changed(NodeState.FORCED_DOWN, NodeState.UP, 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) 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

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