Elffuss BraidHub

kikocis/java-driver public

Fix cluster name mismatch check at startup (JAVA-595).
Changeset36b017dc4f86c09db01c18f69ed661976aee6359047496c6fa6bbce6f1ddffd0
Authorolim7t <omichallat+github@gmail.com> · 2015-01-27 19:11 +01:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-05 14:33 -06:00
Parents12fbb1df3cfc

Browse the files at this changeset

6 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -8,6 +8,7 @@
88 - [improvement] Add connection heartbeat (JAVA-533)
99 - [improvement] Reduce level of logs on missing rpc_address (JAVA-568)
1010 - [improvement] Expose node token and range information (JAVA-312)
11+- [bug] Fix cluster name mismatch check at startup (JAVA-595)
1112 - [bug] Fix guava dependency when using OSGI (JAVA-620)
1213 - [bug] Fix handling of DROP events when ks name is case-sensitive (JAVA-678)
1314
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +12 −12
@@ -1351,8 +1351,8 @@
13511351
13521352 void logClusterNameMismatch(Host host, String expectedClusterName, String actualClusterName) {
13531353 logger.warn("Detected added or restarted Cassandra host {} but ignoring it since its cluster name '{}' does not match the one "
1354- + "currently known ({})",
1355- host, actualClusterName, expectedClusterName);
1354+ + "currently known ({})",
1355+ host, actualClusterName, expectedClusterName);
13561356 }
13571357
13581358 public ListenableFuture<?> triggerOnUp(final Host host) {
@@ -1469,15 +1469,15 @@
14691469 }
14701470 }
14711471
1472- public ListenableFuture<?> triggerOnDown(final Host host) {
1473- return triggerOnDown(host, false);
1472+ public ListenableFuture<?> triggerOnDown(final Host host, boolean startReconnection) {
1473+ return triggerOnDown(host, false, startReconnection);
14741474 }
14751475
1476- public ListenableFuture<?> triggerOnDown(final Host host, final boolean isHostAddition) {
1476+ public ListenableFuture<?> triggerOnDown(final Host host, final boolean isHostAddition, final boolean startReconnection) {
14771477 return executor.submit(new ExceptionCatchingRunnable() {
14781478 @Override
14791479 public void runMayThrow() throws InterruptedException, ExecutionException {
1480- onDown(host, isHostAddition, false);
1480+ onDown(host, isHostAddition, false, startReconnection);
14811481 }
14821482 });
14831483 }
@@ -1492,7 +1492,7 @@
14921492 // connected to one in the first place, but if we ever do, simply hand it
14931493 // off to onDown
14941494 if (loadBalancingPolicy().distance(host) == HostDistance.IGNORED) {
1495- triggerOnDown(host);
1495+ triggerOnDown(host, true);
14961496 return;
14971497 }
14981498
@@ -1532,7 +1532,7 @@
15321532 success = false;
15331533 }
15341534 if (!success)
1535- onDown(host, false, true);
1535+ onDown(host, false, true, true);
15361536 }
15371537 }));
15381538
@@ -1548,7 +1548,7 @@
15481548 }
15491549
15501550 // Use triggerOnDown unless you're sure you want to run this on the current thread.
1551- private void onDown(final Host host, final boolean isHostAddition, final boolean isSuspectedVerification) throws InterruptedException, ExecutionException {
1551+ private void onDown(final Host host, final boolean isHostAddition, final boolean isSuspectedVerification, boolean startReconnection) throws InterruptedException, ExecutionException {
15521552 logger.debug("Host {} is DOWN", host);
15531553
15541554 if (isClosed())
@@ -1601,7 +1601,7 @@
16011601 }
16021602
16031603 // Don't start a reconnection if we ignore the node anyway (JAVA-314)
1604- if (distance == HostDistance.IGNORED)
1604+ if (distance == HostDistance.IGNORED || !startReconnection)
16051605 return;
16061606
16071607 // Note: we basically waste the first successful reconnection, but it's probably not a big deal
@@ -1861,7 +1861,7 @@
18611861 boolean isDown = host.signalConnectionFailure(exception);
18621862 if (isDown) {
18631863 if (isHostAddition || !markSuspected) {
1864- triggerOnDown(host, isHostAddition);
1864+ triggerOnDown(host, isHostAddition, true);
18651865 } else {
18661866 // Note that we do want to call onSuspected on the current thread, as the whole point is
18671867 // that by the time this method return, the host initialReconnectionAttempt will have been
@@ -2114,7 +2114,7 @@
21142114 // right away, so we favor the detection to make the Host.isUp method more reliable.
21152115 Host hostDown = metadata.getHost(stAddr);
21162116 if (hostDown != null)
2117- triggerOnDown(hostDown);
2117+ triggerOnDown(hostDown, true);
21182118 break;
21192119 }
21202120 break;
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +8 −0
@@ -308,6 +308,14 @@
308308 return true;
309309 }
310310 }
311+ } catch (UnsupportedProtocolVersionException e) {
312+ cluster.manager.logUnsupportedVersionProtocol(host);
313+ cluster.manager.triggerOnDown(host, false);
314+ return false;
315+ } catch (ClusterNameMismatchException e) {
316+ cluster.manager.logClusterNameMismatch(host, e.expectedClusterName, e.actualClusterName);
317+ cluster.manager.triggerOnDown(host, false);
318+ return false;
311319 } catch (Exception e) {
312320 logger.error("Error creating pool to " + host, e);
313321 return false;
modified driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +5 −0
@@ -21,6 +21,11 @@
2121 return this;
2222 }
2323
24+ public ClusterAssert hasClosedControlConnection() {
25+ assertThat(actual.manager.controlConnection.isOpen()).isFalse();
26+ return this;
27+ }
28+
2429 public HostAssert host(int hostNumber) {
2530 // TODO at some point this won't work anymore if we have assertions that wait for a node to
2631 // join the cluster, e.g. assertThat(cluster).node(3).comesUp().
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +13 −0
@@ -32,6 +32,19 @@
3232 return this;
3333 }
3434
35+ public HostAssert isNotReconnectingFromDown() {
36+ assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone())
37+ .isFalse();
38+ return this;
39+ }
40+
41+ public HostAssert isNotReconnectingFromSuspected() {
42+ assertThat(actual.getInitialReconnectionAttemptFuture() != null && !actual.getInitialReconnectionAttemptFuture().isDone())
43+ .isFalse();
44+ return this;
45+
46+ }
47+
3548 public HostAssert comesUpWithin(long duration, TimeUnit unit) {
3649 final CountDownLatch upSignal = new CountDownLatch(1);
3750 StateListener upListener = new StateListenerBase() {
modified driver-core/src/test/java/com/datastax/driver/core/RecommissionnedNodeTest.java +38 −13
@@ -6,7 +6,11 @@
66 import org.slf4j.LoggerFactory;
77 import org.testng.annotations.AfterMethod;
88 import org.testng.annotations.Test;
9-import static org.testng.Assert.*;
9+
10+import static com.datastax.driver.core.Assertions.*;
11+import static com.datastax.driver.core.Host.State.ADDED;
12+import static com.datastax.driver.core.Host.State.DOWN;
13+import static com.datastax.driver.core.Host.State.UP;
1014
1115 /**
1216 * Due to C* gossip bugs, system.peers may report nodes that are gone from the cluster.
@@ -20,18 +24,15 @@
2024 CCMBridge mainCcm, otherCcm;
2125 Cluster mainCluster;
2226
23- /**
24- * Tests the case when trying to reconnect to the foreign node results from a scheduled reconnection attempt.
25- */
2627 @Test(groups = "long")
27- public void testReconnectionToRegularNode() throws Exception {
28+ public void should_ignore_recommissionned_node_on_reconnection_attempt() throws Exception {
2829 mainCcm = CCMBridge.create("main", 3);
2930 // node1 will be our "recommissionned" node, for now we just stop it so that it stays in the peers table.
3031 mainCcm.stop(1);
3132 mainCcm.waitForDown(1);
3233
3334 // Now start the driver that will connect to node2 and node3, and consider node1 down
34- Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
35+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
3536 mainCluster.connect();
3637 waitForCountUpHosts(mainCluster, 2);
3738 // From that point, reconnections to node1 have been scheduled.
@@ -43,20 +44,17 @@
4344 // Give the driver the time to notice the node is back up and try to connect to it.
4445 TimeUnit.SECONDS.sleep(32);
4546
46- assertEquals(countUpHosts(mainCluster), 2);
47+ assertThat(countUpHosts(mainCluster)).isEqualTo(2);
4748 }
4849
49- /**
50- * Tests the case when trying to reconnect to the foreign node results from a scheduled reconnection attempt.
51- */
5250 @Test(groups = "long")
53- public void testControlConnection() throws Exception {
51+ public void should_ignore_recommissionned_node_on_control_connection_reconnect() throws Exception {
5452 mainCcm = CCMBridge.create("main", 2);
5553 mainCcm.stop(1);
5654 mainCcm.waitForDown(1);
5755
5856 // Start the driver, the control connection will be on node2
59- Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
57+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
6058 mainCluster.connect();
6159 waitForCountUpHosts(mainCluster, 1);
6260
@@ -69,7 +67,34 @@
6967 TimeUnit.SECONDS.sleep(32);
7068
7169 // The driver should not try to reconnect the control connection to node1
72- assertFalse(mainCluster.manager.controlConnection.isOpen());
70+ assertThat(mainCluster).hasClosedControlConnection();
71+ }
72+
73+ @Test(groups = "long")
74+ public void should_ignore_recommissionned_node_on_session_init() throws Exception {
75+ // Simulate the bug before starting the cluster
76+ mainCcm = CCMBridge.create("main", 2);
77+ mainCcm.stop(1);
78+ mainCcm.waitForDown(1);
79+
80+ otherCcm = CCMBridge.create("other", 1);
81+ otherCcm.waitForUp(1);
82+
83+ // Start the driver, it should only connect to node 2
84+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
85+
86+ // When we first initialize the Cluster, all hosts are marked UP
87+ assertThat(mainCluster).host(2).hasState(UP);
88+ assertThat(mainCluster).host(1).hasState(UP);
89+
90+ // Create a session. This will try to open a pool to node 1 and find out that the cluster name doesn't match.
91+ mainCluster.connect();
92+
93+ // Node 1 should now be DOWN with no reconnection attempt
94+ assertThat(mainCluster).host(1)
95+ .hasState(DOWN)
96+ .isNotReconnectingFromDown()
97+ .isNotReconnectingFromSuspected();
7398 }
7499
75100 @AfterMethod(groups = "long")