Elffuss BraidHub

kikocis/java-driver public

Do not use a host when no rpc_address found for it (JAVA-428).
Changeset2108bc7bd494f8682ac75ab119de2fd61be913164763dbc8e2cf28dd42a3fa60
Authorolim7t <omichallat+github@gmail.com> · 2014-08-26 15:39 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-08-26 20:51 +02:00
Parentsbe70c4c6faff

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +29 −17
@@ -1481,19 +1481,22 @@
14811481 // TODO: this is a bit wasteful, we should consider passing it to onAdd/onUp so
14821482 // we use it for the first HostConnectionPool created
14831483 connection.closeAsync();
1484- logger.debug("Successful reconnection to {}, setting host UP", host);
14851484 // Make sure we have up-to-date infos on that host before adding it (so we typically
14861485 // catch that an upgraded node uses a new cassandra version).
1487- controlConnection.refreshNodeInfo(host);
1488- try {
1489- if (isHostAddition)
1490- onAdd(host);
1491- else
1492- onUp(host);
1493- } catch (InterruptedException e) {
1494- Thread.currentThread().interrupt();
1495- } catch (Exception e) {
1496- logger.error("Unexpected error while setting node up", e);
1486+ if (controlConnection.refreshNodeInfo(host)) {
1487+ logger.debug("Successful reconnection to {}, setting host UP", host);
1488+ try {
1489+ if (isHostAddition)
1490+ onAdd(host);
1491+ else
1492+ onUp(host);
1493+ } catch (InterruptedException e) {
1494+ Thread.currentThread().interrupt();
1495+ } catch (Exception e) {
1496+ logger.error("Unexpected error while setting node up", e);
1497+ }
1498+ } else {
1499+ logger.debug("Not enough info for {}, ignoring host", host);
14971500 }
14981501 }
14991502
@@ -1816,8 +1819,11 @@
18161819 public void runMayThrow() throws InterruptedException, ExecutionException {
18171820 // Make sure we have up-to-date infos on that host before adding it (so we typically
18181821 // catch that an upgraded node uses a new cassandra version).
1819- controlConnection.refreshNodeInfo(newHost);
1820- onAdd(newHost);
1822+ if (controlConnection.refreshNodeInfo(newHost)) {
1823+ onAdd(newHost);
1824+ } else {
1825+ logger.debug("Not enough info for {}, ignoring host", newHost);
1826+ }
18211827 }
18221828 }, 1, TimeUnit.SECONDS);
18231829 }
@@ -1855,8 +1861,11 @@
18551861 public void runMayThrow() throws InterruptedException, ExecutionException {
18561862 // Make sure we have up-to-date infos on that host before adding it (so we typically
18571863 // catch that an upgraded node uses a new cassandra version).
1858- controlConnection.refreshNodeInfo(h);
1859- onAdd(h);
1864+ if (controlConnection.refreshNodeInfo(h)) {
1865+ onAdd(h);
1866+ } else {
1867+ logger.debug("Not enough info for {}, ignoring host", h);
1868+ }
18601869 }
18611870 }, 1, TimeUnit.SECONDS);
18621871 } else {
@@ -1865,8 +1874,11 @@
18651874 public void runMayThrow() throws InterruptedException, ExecutionException {
18661875 // Make sure we have up-to-date infos on that host before adding it (so we typically
18671876 // catch that an upgraded node uses a new cassandra version).
1868- controlConnection.refreshNodeInfo(hostUp);
1869- onUp(hostUp);
1877+ if (controlConnection.refreshNodeInfo(hostUp)) {
1878+ onUp(hostUp);
1879+ } else {
1880+ logger.debug("Not enough info for {}, ignoring host", hostUp);
1881+ }
18701882 }
18711883 });
18721884 }
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +22 −15
@@ -225,7 +225,7 @@
225225
226226 // We need to refresh the node list first so we know about the cassandra version of
227227 // the node we're connecting to.
228- refreshNodeListAndTokenMap(connection, cluster, isInitialConnection);
228+ refreshNodeListAndTokenMap(connection, cluster, isInitialConnection, true);
229229
230230 // Note that refreshing the schema will trigger refreshNodeListAndTokenMap since table == null
231231 // We want that because the token map was not properly initialized by the first call above, since it requires the list of keyspaces
@@ -309,7 +309,7 @@
309309 // If the table is null, we either rebuild all from scratch or have an updated keyspace. In both case, rebuild the token map
310310 // since some replication on some keyspace may have changed
311311 if (table == null)
312- refreshNodeListAndTokenMap(connection, cluster, false);
312+ refreshNodeListAndTokenMap(connection, cluster, false, false);
313313 }
314314
315315 public void refreshNodeListAndTokenMap() {
@@ -320,7 +320,7 @@
320320
321321 logger.debug("[Control connection] Refreshing node list and token map");
322322 try {
323- refreshNodeListAndTokenMap(c, cluster, false);
323+ refreshNodeListAndTokenMap(c, cluster, false, true);
324324 } catch (ConnectionException e) {
325325 logger.debug("[Control connection] Connection error while refreshing node list and token map ({})", e.getMessage());
326326 signalError();
@@ -338,7 +338,7 @@
338338 }
339339 }
340340
341- private static InetSocketAddress addressToUseForPeerHost(Row peersRow, InetSocketAddress connectedHost, Cluster.Manager cluster) {
341+ private static InetSocketAddress addressToUseForPeerHost(Row peersRow, InetSocketAddress connectedHost, Cluster.Manager cluster, boolean logMissingRpcAddresses) {
342342 InetAddress peer = peersRow.getInet("peer");
343343 InetAddress addr = peersRow.getInet("rpc_address");
344344
@@ -348,8 +348,9 @@
348348 logger.debug("System.peers on node {} has a line for itself. This is not normal but is a known problem of some DSE version. Ignoring the entry.", connectedHost);
349349 return null;
350350 } else if (addr == null) {
351- logger.error("No rpc_address found for host {} in {}'s peers system table. That should not happen but using address {} instead", peer, connectedHost, peer);
352- addr = peer;
351+ if (logMissingRpcAddresses)
352+ logger.error("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", peer, connectedHost, peer);
353+ return null;
353354 } else if (addr.equals(bindAllAddress)) {
354355 logger.warn("Found host with 0.0.0.0 as rpc_address, using listen_address ({}) to contact it instead. If this is incorrect you should avoid the use of 0.0.0.0 server side.", peer);
355356 addr = peer;
@@ -372,7 +373,7 @@
372373 DefaultResultSetFuture future = new DefaultResultSetFuture(null, new Requests.Query(SELECT_PEERS));
373374 c.write(future);
374375 for (Row row : future.get()) {
375- InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster);
376+ InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster, true);
376377 if (addr != null && addr.equals(host.getSocketAddress()))
377378 return row;
378379 }
@@ -394,22 +395,28 @@
394395 return null;
395396 }
396397
397- public void refreshNodeInfo(Host host) {
398+ /**
399+ * @return whether we have enough information to bring the node back up
400+ */
401+ public boolean refreshNodeInfo(Host host) {
398402
399403 Connection c = connectionRef.get();
400404 // At startup, when we add the initial nodes, this will be null, which is ok
401405 if (c == null)
402- return;
406+ return true;
403407
404408 logger.debug("[Control connection] Refreshing node info on {}", host);
405409 Row row = fetchNodeInfo(host, c);
406- // It's possible our peers selection returns nothing, but that's fine, this method is best effort really.
407410 if (row == null) {
408- logger.debug("[control connection] Asked to refresh node info for {} but host not found in {} system table (this can happen)", host.getSocketAddress(), c.address);
409- return;
411+ logger.error("No row found for host {} in {}'s peers system table. {} will be ignored.", host.getAddress(), c.address, host.getAddress());
412+ return false;
413+ } else if (row.getInet("rpc_address") == null) {
414+ logger.error("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", host.getAddress(), c.address, host.getAddress());
415+ return false;
410416 }
411417
412418 updateInfo(host, row, cluster);
419+ return true;
413420 }
414421
415422 // row can come either from the 'local' table or the 'peers' one
@@ -440,7 +447,7 @@
440447 cluster.loadBalancingPolicy().onAdd(host);
441448 }
442449
443- private static void refreshNodeListAndTokenMap(Connection connection, Cluster.Manager cluster, boolean isInitialConnection) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException {
450+ private static void refreshNodeListAndTokenMap(Connection connection, Cluster.Manager cluster, boolean isInitialConnection, boolean logMissingRpcAddresses) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException {
444451 logger.debug("[Control connection] Refreshing node list and token map");
445452
446453 // Make sure we're up to date on nodes and tokens
@@ -485,7 +492,7 @@
485492 List<Set<String>> allTokens = new ArrayList<Set<String>>();
486493
487494 for (Row row : peersFuture.get()) {
488- InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster);
495+ InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, logMissingRpcAddresses);
489496 if (addr == null)
490497 continue;
491498
@@ -546,7 +553,7 @@
546553
547554 for (Row row : peersFuture.get()) {
548555
549- InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster);
556+ InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, true);
550557 if (addr == null || row.isNull("schema_version"))
551558 continue;
552559
added driver-core/src/test/java/com/datastax/driver/core/MissingRpcAddressTest.java +67 −0
@@ -0,0 +1,67 @@
1+package com.datastax.driver.core;
2+
3+import java.net.InetAddress;
4+import java.net.InetSocketAddress;
5+
6+import com.google.common.collect.Lists;
7+import org.testng.annotations.Test;
8+
9+import static org.testng.Assert.assertEquals;
10+import static org.testng.Assert.assertNull;
11+
12+import com.datastax.driver.core.policies.Policies;
13+import com.datastax.driver.core.policies.WhiteListPolicy;
14+
15+/**
16+ * Tests the behavior of the driver when some hosts have no rpc_address in the control host's system tables (JAVA-428).
17+ *
18+ * This can happen because of gossip bugs. We want to ignore these hosts because this is most likely indicative of an error state.
19+ */
20+public class MissingRpcAddressTest {
21+
22+ @Test(groups = "short")
23+ public void testMissingRpcAddressAtStartup() throws Exception {
24+ CCMBridge ccm = CCMBridge.create("ccm", 2);
25+ deleteNode2RpcAddressFromNode1();
26+
27+ Cluster cluster = null;
28+ try {
29+ // Use only one contact point to make sure that the control connection is on node1
30+ cluster = Cluster.builder()
31+ .addContactPoint(CCMBridge.IP_PREFIX + "1")
32+ .build();
33+ cluster.connect();
34+
35+ // Since node2's RPC address is unknown on our control host, it should have been ignored
36+ assertEquals(cluster.getMetrics().getConnectedToHosts().getValue().intValue(), 1);
37+ assertNull(cluster.getMetadata().getHost(socketAddress(2)));
38+ } finally {
39+ if (cluster != null)
40+ cluster.close();
41+ ccm.remove();
42+ }
43+ }
44+
45+ // Artificially modify the system tables to simulate the missing rpc_address.
46+ private void deleteNode2RpcAddressFromNode1() throws Exception {
47+ Cluster cluster = null;
48+ try {
49+ cluster = Cluster.builder()
50+ .addContactPoint(CCMBridge.IP_PREFIX + "1")
51+ // ensure we will only connect to node1
52+ .withLoadBalancingPolicy(new WhiteListPolicy(Policies.defaultLoadBalancingPolicy(),
53+ Lists.newArrayList(socketAddress(1))))
54+ .build();
55+ Session session = cluster.connect();
56+ session.execute("DELETE rpc_address FROM system.peers WHERE peer = ?", InetAddress.getByName(CCMBridge.IP_PREFIX + "2"));
57+ session.close();
58+ } finally {
59+ if (cluster != null)
60+ cluster.close();
61+ }
62+ }
63+
64+ private static InetSocketAddress socketAddress(int node) {
65+ return new InetSocketAddress(CCMBridge.IP_PREFIX + Integer.toString(node), 9042);
66+ }
67+}