kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2108bc7bd494f8682ac75ab119de2fd61be913164763dbc8e2cf28dd42a3fa60 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-08-26 15:39 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-08-26 20:51 +02:00 |
| Parents | be70c4c6faff |
3 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +29 −17
| @@ -1481,19 +1481,22 @@ | ||
| 1481 | 1481 | // TODO: this is a bit wasteful, we should consider passing it to onAdd/onUp so |
| 1482 | 1482 | // we use it for the first HostConnectionPool created |
| 1483 | 1483 | connection.closeAsync(); |
| 1484 | - logger.debug("Successful reconnection to {}, setting host UP", host); | |
| 1485 | 1484 | // Make sure we have up-to-date infos on that host before adding it (so we typically |
| 1486 | 1485 | // 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); | |
| 1497 | 1500 | } |
| 1498 | 1501 | } |
| 1499 | 1502 | |
| @@ -1816,8 +1819,11 @@ | ||
| 1816 | 1819 | public void runMayThrow() throws InterruptedException, ExecutionException { |
| 1817 | 1820 | // Make sure we have up-to-date infos on that host before adding it (so we typically |
| 1818 | 1821 | // 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 | + } | |
| 1821 | 1827 | } |
| 1822 | 1828 | }, 1, TimeUnit.SECONDS); |
| 1823 | 1829 | } |
| @@ -1855,8 +1861,11 @@ | ||
| 1855 | 1861 | public void runMayThrow() throws InterruptedException, ExecutionException { |
| 1856 | 1862 | // Make sure we have up-to-date infos on that host before adding it (so we typically |
| 1857 | 1863 | // 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 | + } | |
| 1860 | 1869 | } |
| 1861 | 1870 | }, 1, TimeUnit.SECONDS); |
| 1862 | 1871 | } else { |
| @@ -1865,8 +1874,11 @@ | ||
| 1865 | 1874 | public void runMayThrow() throws InterruptedException, ExecutionException { |
| 1866 | 1875 | // Make sure we have up-to-date infos on that host before adding it (so we typically |
| 1867 | 1876 | // 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 | + } | |
| 1870 | 1882 | } |
| 1871 | 1883 | }); |
| 1872 | 1884 | } |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +22 −15
| @@ -225,7 +225,7 @@ | ||
| 225 | 225 | |
| 226 | 226 | // We need to refresh the node list first so we know about the cassandra version of |
| 227 | 227 | // the node we're connecting to. |
| 228 | - refreshNodeListAndTokenMap(connection, cluster, isInitialConnection); | |
| 228 | + refreshNodeListAndTokenMap(connection, cluster, isInitialConnection, true); | |
| 229 | 229 | |
| 230 | 230 | // Note that refreshing the schema will trigger refreshNodeListAndTokenMap since table == null |
| 231 | 231 | // 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 @@ | ||
| 309 | 309 | // If the table is null, we either rebuild all from scratch or have an updated keyspace. In both case, rebuild the token map |
| 310 | 310 | // since some replication on some keyspace may have changed |
| 311 | 311 | if (table == null) |
| 312 | - refreshNodeListAndTokenMap(connection, cluster, false); | |
| 312 | + refreshNodeListAndTokenMap(connection, cluster, false, false); | |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | public void refreshNodeListAndTokenMap() { |
| @@ -320,7 +320,7 @@ | ||
| 320 | 320 | |
| 321 | 321 | logger.debug("[Control connection] Refreshing node list and token map"); |
| 322 | 322 | try { |
| 323 | - refreshNodeListAndTokenMap(c, cluster, false); | |
| 323 | + refreshNodeListAndTokenMap(c, cluster, false, true); | |
| 324 | 324 | } catch (ConnectionException e) { |
| 325 | 325 | logger.debug("[Control connection] Connection error while refreshing node list and token map ({})", e.getMessage()); |
| 326 | 326 | signalError(); |
| @@ -338,7 +338,7 @@ | ||
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | 340 | |
| 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) { | |
| 342 | 342 | InetAddress peer = peersRow.getInet("peer"); |
| 343 | 343 | InetAddress addr = peersRow.getInet("rpc_address"); |
| 344 | 344 | |
| @@ -348,8 +348,9 @@ | ||
| 348 | 348 | 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); |
| 349 | 349 | return null; |
| 350 | 350 | } 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; | |
| 353 | 354 | } else if (addr.equals(bindAllAddress)) { |
| 354 | 355 | 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); |
| 355 | 356 | addr = peer; |
| @@ -372,7 +373,7 @@ | ||
| 372 | 373 | DefaultResultSetFuture future = new DefaultResultSetFuture(null, new Requests.Query(SELECT_PEERS)); |
| 373 | 374 | c.write(future); |
| 374 | 375 | for (Row row : future.get()) { |
| 375 | - InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster); | |
| 376 | + InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster, true); | |
| 376 | 377 | if (addr != null && addr.equals(host.getSocketAddress())) |
| 377 | 378 | return row; |
| 378 | 379 | } |
| @@ -394,22 +395,28 @@ | ||
| 394 | 395 | return null; |
| 395 | 396 | } |
| 396 | 397 | |
| 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) { | |
| 398 | 402 | |
| 399 | 403 | Connection c = connectionRef.get(); |
| 400 | 404 | // At startup, when we add the initial nodes, this will be null, which is ok |
| 401 | 405 | if (c == null) |
| 402 | - return; | |
| 406 | + return true; | |
| 403 | 407 | |
| 404 | 408 | logger.debug("[Control connection] Refreshing node info on {}", host); |
| 405 | 409 | Row row = fetchNodeInfo(host, c); |
| 406 | - // It's possible our peers selection returns nothing, but that's fine, this method is best effort really. | |
| 407 | 410 | 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; | |
| 410 | 416 | } |
| 411 | 417 | |
| 412 | 418 | updateInfo(host, row, cluster); |
| 419 | + return true; | |
| 413 | 420 | } |
| 414 | 421 | |
| 415 | 422 | // row can come either from the 'local' table or the 'peers' one |
| @@ -440,7 +447,7 @@ | ||
| 440 | 447 | cluster.loadBalancingPolicy().onAdd(host); |
| 441 | 448 | } |
| 442 | 449 | |
| 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 { | |
| 444 | 451 | logger.debug("[Control connection] Refreshing node list and token map"); |
| 445 | 452 | |
| 446 | 453 | // Make sure we're up to date on nodes and tokens |
| @@ -485,7 +492,7 @@ | ||
| 485 | 492 | List<Set<String>> allTokens = new ArrayList<Set<String>>(); |
| 486 | 493 | |
| 487 | 494 | for (Row row : peersFuture.get()) { |
| 488 | - InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster); | |
| 495 | + InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, logMissingRpcAddresses); | |
| 489 | 496 | if (addr == null) |
| 490 | 497 | continue; |
| 491 | 498 | |
| @@ -546,7 +553,7 @@ | ||
| 546 | 553 | |
| 547 | 554 | for (Row row : peersFuture.get()) { |
| 548 | 555 | |
| 549 | - InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster); | |
| 556 | + InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, true); | |
| 550 | 557 | if (addr == null || row.isNull("schema_version")) |
| 551 | 558 | continue; |
| 552 | 559 |
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 | +} |