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);
}
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();
}
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
}
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));
}
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));
}
Aggregations