Skip to content

Commit

Permalink
Fix bug in DefaultSession
Browse files Browse the repository at this point in the history
If a node's distance was set to IGNORED but it was already IGNORED, the
session would open a new pool.
  • Loading branch information
olim7t committed Dec 3, 2017
1 parent 4c6b5ac commit 43e855a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Expand Up @@ -326,7 +326,7 @@ private void processDistanceEvent(DistanceEvent event) {
NodeDistance newDistance = event.distance;
if (pending.containsKey(node)) {
pendingDistanceEvents.put(node, event);
} else if (newDistance == NodeDistance.IGNORED && pools.containsKey(node)) {
} else if (newDistance == NodeDistance.IGNORED) {
ChannelPool pool = pools.remove(node);
if (pool != null) {
LOG.debug("[{}] {} became IGNORED, destroying pool", logPrefix, node);
Expand Down
Expand Up @@ -365,6 +365,38 @@ public void should_remove_pool_if_node_becomes_ignored() {
assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3);
}

@Test
public void should_do_nothing_if_node_becomes_ignored_but_was_already_ignored() {
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 = DefaultSession.init(context, KEYSPACE, "test");

factoryHelper.waitForCall(node1, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node2, KEYSPACE, NodeDistance.LOCAL);
factoryHelper.waitForCall(node3, KEYSPACE, NodeDistance.LOCAL);
waitForPendingAdminTasks();
assertThat(initFuture).isSuccess();

eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node2));
Mockito.verify(pool2, timeout(100)).closeAsync();

Session session = CompletableFutures.getCompleted(initFuture.toCompletableFuture());
assertThat(((DefaultSession) session).getPools()).containsValues(pool1, pool3);

// Fire the same event again, nothing should happen
eventBus.fire(new DistanceEvent(NodeDistance.IGNORED, node2));
waitForPendingAdminTasks();
factoryHelper.verifyNoMoreCalls();
}

@Test
public void should_recreate_pool_if_node_becomes_not_ignored() {
Mockito.when(node2.getDistance()).thenReturn(NodeDistance.IGNORED);
Expand Down

0 comments on commit 43e855a

Please sign in to comment.