Elffuss BraidHub

kikocis/java-driver public

Merge pull request #273 from datastax/java595

Fix cluster name mismatch check at startup (JAVA-595).
Changeset2545f7762c197c2867782c3ac476483894e41935db717a60c65056c61b88ba9a
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 15:23 -08:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 15:23 -08:00
Parents18e92eaa4205, e6290790ad2e

Browse the files at this changeset

9 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 - [improvement] Use List<?> instead of List<Object> in QueryBuilder API
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/Connection.java +1 −3
@@ -183,9 +183,7 @@
183183
184184 private UnsupportedProtocolVersionException unsupportedProtocolVersionException(int triedVersion) {
185185 logger.debug("Got unsupported protocol version error from {} for version {}", address, triedVersion);
186- UnsupportedProtocolVersionException exc = new UnsupportedProtocolVersionException(address, triedVersion);
187- defunct(new TransportException(address, "Cannot initialize transport", exc));
188- return exc;
186+ return new UnsupportedProtocolVersionException(address, triedVersion);
189187 }
190188
191189 private void authenticateV1(Authenticator authenticator) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException {
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/CCMBridge.java +8 −0
@@ -98,6 +98,14 @@
9898 return bridge;
9999 }
100100
101+ public static CCMBridge createWithCustomVersion(String name, int nbNodes, String cassandraVersion) {
102+ checkArgument(!"current".equals(name.toLowerCase()),
103+ "cluster can't be called \"current\"");
104+ CCMBridge bridge = new CCMBridge();
105+ bridge.execute("ccm create %s -n %d -s -i %s -b -v %s ", name, nbNodes, IP_PREFIX, cassandraVersion);
106+ return bridge;
107+ }
108+
101109 public static CCMBridge create(String name, int nbNodesDC1, int nbNodesDC2) {
102110 checkArgument(!"current".equals(name.toLowerCase()),
103111 "cluster can't be called \"current\"");
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() {
added driver-core/src/test/java/com/datastax/driver/core/RecommissionedNodeTest.java +163 −0
@@ -0,0 +1,163 @@
1+package com.datastax.driver.core;
2+
3+import java.util.concurrent.TimeUnit;
4+
5+import org.slf4j.Logger;
6+import org.slf4j.LoggerFactory;
7+import org.testng.annotations.AfterMethod;
8+import org.testng.annotations.Test;
9+
10+import com.datastax.driver.core.utils.CassandraVersion;
11+
12+import static com.datastax.driver.core.Assertions.*;
13+import static com.datastax.driver.core.Host.State.DOWN;
14+import static com.datastax.driver.core.Host.State.UP;
15+
16+/**
17+ * Due to C* gossip bugs, system.peers may report nodes that are gone from the cluster.
18+ *
19+ * This class tests scenarios where these nodes have been recommissioned to another cluster and
20+ * come back up. The driver must detect that they are not part of the cluster anymore, and ignore them.
21+ */
22+public class RecommissionedNodeTest {
23+ private static final Logger logger = LoggerFactory.getLogger(RecommissionedNodeTest.class);
24+
25+ CCMBridge mainCcm, otherCcm;
26+ Cluster mainCluster;
27+
28+ @Test(groups = "long")
29+ public void should_ignore_recommissioned_node_on_reconnection_attempt() throws Exception {
30+ mainCcm = CCMBridge.create("main", 3);
31+ // node1 will be our "recommissioned" node, for now we just stop it so that it stays in the peers table.
32+ mainCcm.stop(1);
33+ mainCcm.waitForDown(1);
34+
35+ // Now start the driver that will connect to node2 and node3, and consider node1 down
36+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
37+ mainCluster.connect();
38+ waitForCountUpHosts(mainCluster, 2);
39+ // From that point, reconnections to node1 have been scheduled.
40+
41+ // Start another ccm that will reuse node1's address
42+ otherCcm = CCMBridge.create("other", 1);
43+ otherCcm.waitForUp(1);
44+
45+ // Give the driver the time to notice the node is back up and try to connect to it.
46+ TimeUnit.SECONDS.sleep(32);
47+
48+ assertThat(countUpHosts(mainCluster)).isEqualTo(2);
49+ }
50+
51+ @Test(groups = "long")
52+ public void should_ignore_recommissioned_node_on_control_connection_reconnect() throws Exception {
53+ mainCcm = CCMBridge.create("main", 2);
54+ mainCcm.stop(1);
55+ mainCcm.waitForDown(1);
56+
57+ // Start the driver, the control connection will be on node2
58+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
59+ mainCluster.connect();
60+ waitForCountUpHosts(mainCluster, 1);
61+
62+ // Start another ccm that will reuse node1's address
63+ otherCcm = CCMBridge.create("other", 1);
64+ otherCcm.waitForUp(1);
65+
66+ // Stop node2, the control connection gets defunct
67+ mainCcm.stop(2);
68+ TimeUnit.SECONDS.sleep(32);
69+
70+ // The driver should not try to reconnect the control connection to node1
71+ assertThat(mainCluster).hasClosedControlConnection();
72+ }
73+
74+ @Test(groups = "long")
75+ public void should_ignore_recommissioned_node_on_session_init() throws Exception {
76+ // Simulate the bug before starting the cluster
77+ mainCcm = CCMBridge.create("main", 2);
78+ mainCcm.stop(1);
79+ mainCcm.waitForDown(1);
80+
81+ otherCcm = CCMBridge.create("other", 1);
82+ otherCcm.waitForUp(1);
83+
84+ // Start the driver, it should only connect to node 2
85+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
86+
87+ // When we first initialize the Cluster, all hosts are marked UP
88+ assertThat(mainCluster).host(2).hasState(UP);
89+ assertThat(mainCluster).host(1).hasState(UP);
90+
91+ // Create a session. This will try to open a pool to node 1 and find out that the cluster name doesn't match.
92+ mainCluster.connect();
93+
94+ // Node 1 should now be DOWN with no reconnection attempt
95+ assertThat(mainCluster).host(1)
96+ .hasState(DOWN)
97+ .isNotReconnectingFromDown()
98+ .isNotReconnectingFromSuspected();
99+ }
100+
101+ @Test(groups = "long")
102+ @CassandraVersion(major=2.0)
103+ public void should_ignore_node_that_does_not_support_protocol_version_on_session_init() throws Exception {
104+ // Simulate the bug before starting the cluster
105+ mainCcm = CCMBridge.create("main", 2);
106+ mainCcm.stop(1);
107+ mainCcm.waitForDown(1);
108+
109+ otherCcm = CCMBridge.createWithCustomVersion("main", 1, "1.2.19");
110+ otherCcm.waitForUp(1);
111+
112+ // Start the driver, it should only connect to node 2
113+ mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
114+
115+ // Create a session. This will try to open a pool to node 1 and find that it doesn't support protocol version.
116+ mainCluster.connect();
117+
118+ // Node 1 should now be DOWN with no reconnection attempt
119+ assertThat(mainCluster).host(1)
120+ .hasState(DOWN)
121+ .isNotReconnectingFromDown()
122+ .isNotReconnectingFromSuspected();
123+ }
124+
125+ @AfterMethod(groups = "long")
126+ public void teardown() {
127+ if (mainCluster != null)
128+ mainCluster.close();
129+
130+ if (mainCcm != null)
131+ mainCcm.remove();
132+ if (otherCcm != null)
133+ otherCcm.remove();
134+ }
135+
136+ private static int countUpHosts(Cluster cluster) {
137+ int ups = 0;
138+ for (Host host : cluster.getMetadata().getAllHosts()) {
139+ if (host.isUp())
140+ ups += 1;
141+ }
142+ return ups;
143+ }
144+
145+ private static void waitForCountUpHosts(Cluster cluster, int expectedCount) throws InterruptedException {
146+ int maxRetries = 30;
147+ int interval = 10;
148+
149+ for (int i = 0; i <= maxRetries; i++) {
150+ int actualCount = countUpHosts(cluster);
151+ if (actualCount == expectedCount)
152+ return;
153+
154+ if (i == maxRetries)
155+ fail(String.format("Up host count didn't reach %d after %d seconds",
156+ expectedCount, i * interval));
157+ else
158+ logger.debug("Counted {} up hosts after {} seconds", actualCount, i * interval);
159+
160+ TimeUnit.SECONDS.sleep(interval);
161+ }
162+ }
163+}
No newline at the end of the file
deleted driver-core/src/test/java/com/datastax/driver/core/RecommissionnedNodeTest.java +0 −113
@@ -1,113 +0,0 @@
1-package com.datastax.driver.core;
2-
3-import java.util.concurrent.TimeUnit;
4-
5-import org.slf4j.Logger;
6-import org.slf4j.LoggerFactory;
7-import org.testng.annotations.AfterMethod;
8-import org.testng.annotations.Test;
9-import static org.testng.Assert.*;
10-
11-/**
12- * Due to C* gossip bugs, system.peers may report nodes that are gone from the cluster.
13- *
14- * This class tests scenarios where these nodes have been recommissionned to another cluster and
15- * come back up. The driver must detect that they are not part of the cluster anymore, and ignore them.
16- */
17-public class RecommissionnedNodeTest {
18- private static final Logger logger = LoggerFactory.getLogger(RecommissionnedNodeTest.class);
19-
20- CCMBridge mainCcm, otherCcm;
21- Cluster mainCluster;
22-
23- /**
24- * Tests the case when trying to reconnect to the foreign node results from a scheduled reconnection attempt.
25- */
26- @Test(groups = "long")
27- public void testReconnectionToRegularNode() throws Exception {
28- mainCcm = CCMBridge.create("main", 3);
29- // node1 will be our "recommissionned" node, for now we just stop it so that it stays in the peers table.
30- mainCcm.stop(1);
31- mainCcm.waitForDown(1);
32-
33- // 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.connect();
36- waitForCountUpHosts(mainCluster, 2);
37- // From that point, reconnections to node1 have been scheduled.
38-
39- // Start another ccm that will reuse node1's address
40- otherCcm = CCMBridge.create("other", 1);
41- otherCcm.waitForUp(1);
42-
43- // Give the driver the time to notice the node is back up and try to connect to it.
44- TimeUnit.SECONDS.sleep(32);
45-
46- assertEquals(countUpHosts(mainCluster), 2);
47- }
48-
49- /**
50- * Tests the case when trying to reconnect to the foreign node results from a scheduled reconnection attempt.
51- */
52- @Test(groups = "long")
53- public void testControlConnection() throws Exception {
54- mainCcm = CCMBridge.create("main", 2);
55- mainCcm.stop(1);
56- mainCcm.waitForDown(1);
57-
58- // Start the driver, the control connection will be on node2
59- Cluster mainCluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + "2").build();
60- mainCluster.connect();
61- waitForCountUpHosts(mainCluster, 1);
62-
63- // Start another ccm that will reuse node1's address
64- otherCcm = CCMBridge.create("other", 1);
65- otherCcm.waitForUp(1);
66-
67- // Stop node2, the control connection gets defunct
68- mainCcm.stop(2);
69- TimeUnit.SECONDS.sleep(32);
70-
71- // The driver should not try to reconnect the control connection to node1
72- assertFalse(mainCluster.manager.controlConnection.isOpen());
73- }
74-
75- @AfterMethod(groups = "long")
76- public void teardown() {
77- if (mainCluster != null)
78- mainCluster.close();
79-
80- if (mainCcm != null)
81- mainCcm.remove();
82- if (otherCcm != null)
83- otherCcm.remove();
84- }
85-
86- private static int countUpHosts(Cluster cluster) {
87- int ups = 0;
88- for (Host host : cluster.getMetadata().getAllHosts()) {
89- if (host.isUp())
90- ups += 1;
91- }
92- return ups;
93- }
94-
95- private static void waitForCountUpHosts(Cluster cluster, int expectedCount) throws InterruptedException {
96- int maxRetries = 30;
97- int interval = 10;
98-
99- for (int i = 0; i <= maxRetries; i++) {
100- int actualCount = countUpHosts(cluster);
101- if (actualCount == expectedCount)
102- return;
103-
104- if (i == maxRetries)
105- fail(String.format("Up host count didn't reach %d after %d seconds",
106- expectedCount, i * interval));
107- else
108- logger.debug("Counted {} up hosts after {} seconds", actualCount, i * interval);
109-
110- TimeUnit.SECONDS.sleep(interval);
111- }
112- }
113-}
No newline at the end of the file