kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5c9db3fa0bee561efbe03b9876c0c09682466a1d03059ae0a3aea8d05a0d5114 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-12-04 17:07 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-01-15 19:16 +01:00 |
| Parents | ec7bd869f099 |
4 files changed
modified changelog/README.md +1 −0
| @@ -3,6 +3,7 @@ | ||
| 3 | 3 | ### 3.0.0 (in progress) |
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-1034: fix metadata parser for collections of custom types. |
| 6 | +- [improvement] JAVA-1035: Expose host broadcast_address and listen_address if available. | |
| 6 | 7 | |
| 7 | 8 | Merged from 2.1 branch: |
| 8 | 9 |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +53 −26
| @@ -368,32 +368,39 @@ | ||
| 368 | 368 | } |
| 369 | 369 | } |
| 370 | 370 | |
| 371 | - private static InetSocketAddress addressToUseForPeerHost(Row peersRow, InetSocketAddress connectedHost, Cluster.Manager cluster, boolean logMissingRpcAddresses) { | |
| 372 | - InetAddress peer = peersRow.getInet("peer"); | |
| 373 | - InetAddress addr = peersRow.getInet("rpc_address"); | |
| 374 | - | |
| 375 | - if (peer.equals(connectedHost.getAddress()) || (addr != null && addr.equals(connectedHost.getAddress()))) { | |
| 371 | + private static InetSocketAddress rpcAddressForPeerHost(Row peersRow, InetSocketAddress connectedHost, Cluster.Manager cluster, boolean logMissingRpcAddresses) { | |
| 372 | + | |
| 373 | + // after CASSANDRA-9436, system.peers contains the following inet columns: | |
| 374 | + // - peer: this is actually broadcast_address | |
| 375 | + // - rpc_address: the address we are looking for (this corresponds to broadcast_rpc_address in the peer's cassandra yaml file; | |
| 376 | + // if this setting if unset, it defaults to the value for rpc_address or rpc_interface) | |
| 377 | + // - preferred_ip: used by Ec2MultiRegionSnitch and GossipingPropertyFileSnitch, possibly others; contents unclear | |
| 378 | + | |
| 379 | + InetAddress broadcastAddress = peersRow.getInet("peer"); | |
| 380 | + InetAddress rpcAddress = peersRow.getInet("rpc_address"); | |
| 381 | + | |
| 382 | + if (broadcastAddress.equals(connectedHost.getAddress()) || (rpcAddress != null && rpcAddress.equals(connectedHost.getAddress()))) { | |
| 376 | 383 | // Some DSE versions were inserting a line for the local node in peers (with mostly null values). This has been fixed, but if we |
| 377 | 384 | // detect that's the case, ignore it as it's not really a big deal. |
| 378 | 385 | 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); |
| 379 | 386 | return null; |
| 380 | - } else if (addr == null) { | |
| 387 | + } else if (rpcAddress == null) { | |
| 381 | 388 | if (logMissingRpcAddresses) |
| 382 | - logger.warn("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", peer, connectedHost, peer); | |
| 389 | + logger.warn("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", broadcastAddress, connectedHost, broadcastAddress); | |
| 383 | 390 | return null; |
| 384 | - } else if (addr.equals(bindAllAddress)) { | |
| 385 | - 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); | |
| 386 | - addr = peer; | |
| 391 | + } else if (rpcAddress.equals(bindAllAddress)) { | |
| 392 | + logger.warn("Found host with 0.0.0.0 as rpc_address, using broadcast_address ({}) to contact it instead. If this is incorrect you should avoid the use of 0.0.0.0 server side.", broadcastAddress); | |
| 393 | + rpcAddress = broadcastAddress; | |
| 387 | 394 | } |
| 388 | - return cluster.translateAddress(addr); | |
| 395 | + return cluster.translateAddress(rpcAddress); | |
| 389 | 396 | } |
| 390 | 397 | |
| 391 | 398 | private Row fetchNodeInfo(Host host, Connection c) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { |
| 392 | 399 | boolean isConnectedHost = c.address.equals(host.getSocketAddress()); |
| 393 | - if (isConnectedHost || host.listenAddress != null) { | |
| 400 | + if (isConnectedHost || host.getBroadcastAddress() != null) { | |
| 394 | 401 | DefaultResultSetFuture future = isConnectedHost |
| 395 | 402 | ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_LOCAL)) |
| 396 | - : new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS + " WHERE peer='" + host.listenAddress.getHostAddress() + '\'')); | |
| 403 | + : new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS + " WHERE peer='" + host.getBroadcastAddress().getHostAddress() + '\'')); | |
| 397 | 404 | c.write(future); |
| 398 | 405 | return future.get().one(); |
| 399 | 406 | } |
| @@ -402,7 +409,7 @@ | ||
| 402 | 409 | DefaultResultSetFuture future = new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS)); |
| 403 | 410 | c.write(future); |
| 404 | 411 | for (Row row : future.get()) { |
| 405 | - InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster, true); | |
| 412 | + InetSocketAddress addr = rpcAddressForPeerHost(row, c.address, cluster, true); | |
| 406 | 413 | if (addr != null && addr.equals(host.getSocketAddress())) |
| 407 | 414 | return row; |
| 408 | 415 | } |
| @@ -434,7 +441,7 @@ | ||
| 434 | 441 | } |
| 435 | 442 | // Ignore hosts with a null rpc_address, as this is most likely a phantom row in system.peers (JAVA-428). |
| 436 | 443 | // Don't test this for the control host since we're already connected to it anyway, and we read the info from system.local |
| 437 | - // which doesn't have an rpc_address column (JAVA-546). | |
| 444 | + // which didn't have an rpc_address column (JAVA-546) until CASSANDRA-9436 | |
| 438 | 445 | } else if (!c.address.equals(host.getSocketAddress()) && row.getInet("rpc_address") == null) { |
| 439 | 446 | logger.warn("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", host.getAddress(), c.address, host.getAddress()); |
| 440 | 447 | return false; |
| @@ -472,12 +479,30 @@ | ||
| 472 | 479 | updateLocationInfo(host, row.getString("data_center"), row.getString("rack"), isInitialConnection, cluster); |
| 473 | 480 | |
| 474 | 481 | String version = row.getString("release_version"); |
| 475 | - // We don't know if it's a 'local' or a 'peers' row, and only 'peers' rows have the 'peer' field. | |
| 476 | - InetAddress listenAddress = row.getColumnDefinitions().contains("peer") | |
| 477 | - ? row.getInet("peer") | |
| 482 | + host.setVersion(version); | |
| 483 | + | |
| 484 | + // Before CASSANDRA-9436 local row did not contain any info about the host addresses. | |
| 485 | + // After CASSANDRA-9436 (2.0.16, 2.1.6, 2.2.0 rc1) local row contains two new columns: | |
| 486 | + // - broadcast_address | |
| 487 | + // - rpc_address | |
| 488 | + // After CASSANDRA-9603 (2.0.17, 2.1.8, 2.2.0 rc2) local row contains one more column: | |
| 489 | + // - listen_address | |
| 490 | + | |
| 491 | + InetAddress broadcastAddress = null; | |
| 492 | + if (row.getColumnDefinitions().contains("peer")) { // system.peers | |
| 493 | + broadcastAddress = row.getInet("peer"); | |
| 494 | + } else if (row.getColumnDefinitions().contains("broadcast_address")) { // system.local | |
| 495 | + broadcastAddress = row.getInet("broadcast_address"); | |
| 496 | + } | |
| 497 | + host.setBroadcastAddress(broadcastAddress); | |
| 498 | + | |
| 499 | + // in system.local only for C* versions >= 2.0.17, 2.1.8, 2.2.0 rc2, | |
| 500 | + // not yet in system.peers as of C* 3.2 | |
| 501 | + InetAddress listenAddress = row.getColumnDefinitions().contains("listen_address") | |
| 502 | + ? row.getInet("listen_address") | |
| 478 | 503 | : null; |
| 504 | + host.setListenAddress(listenAddress); | |
| 479 | 505 | |
| 480 | - host.setVersionAndListenAdress(version, listenAddress); | |
| 481 | 506 | } |
| 482 | 507 | |
| 483 | 508 | private static void updateLocationInfo(Host host, String datacenter, String rack, boolean isInitialConnection, Cluster.Manager cluster) { |
| @@ -539,19 +564,19 @@ | ||
| 539 | 564 | List<String> dcs = new ArrayList<String>(); |
| 540 | 565 | List<String> racks = new ArrayList<String>(); |
| 541 | 566 | List<String> cassandraVersions = new ArrayList<String>(); |
| 542 | - List<InetAddress> listenAddresses = new ArrayList<InetAddress>(); | |
| 567 | + List<InetAddress> broadcastAddresses = new ArrayList<InetAddress>(); | |
| 543 | 568 | List<Set<String>> allTokens = new ArrayList<Set<String>>(); |
| 544 | 569 | |
| 545 | 570 | for (Row row : peersFuture.get()) { |
| 546 | - InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, logMissingRpcAddresses); | |
| 547 | - if (addr == null) | |
| 571 | + InetSocketAddress rpcAddress = rpcAddressForPeerHost(row, connection.address, cluster, logMissingRpcAddresses); | |
| 572 | + if (rpcAddress == null) | |
| 548 | 573 | continue; |
| 549 | 574 | |
| 550 | - foundHosts.add(addr); | |
| 575 | + foundHosts.add(rpcAddress); | |
| 551 | 576 | dcs.add(row.getString("data_center")); |
| 552 | 577 | racks.add(row.getString("rack")); |
| 553 | 578 | cassandraVersions.add(row.getString("release_version")); |
| 554 | - listenAddresses.add(row.getInet("peer")); | |
| 579 | + broadcastAddresses.add(row.getInet("peer")); | |
| 555 | 580 | if (metadataEnabled) |
| 556 | 581 | allTokens.add(row.getSet("tokens", String.class)); |
| 557 | 582 | } |
| @@ -575,7 +600,9 @@ | ||
| 575 | 600 | if (dcs.get(i) != null || racks.get(i) != null) |
| 576 | 601 | updateLocationInfo(host, dcs.get(i), racks.get(i), isInitialConnection, cluster); |
| 577 | 602 | if (cassandraVersions.get(i) != null) |
| 578 | - host.setVersionAndListenAdress(cassandraVersions.get(i), listenAddresses.get(i)); | |
| 603 | + host.setVersion(cassandraVersions.get(i)); | |
| 604 | + if (broadcastAddresses.get(i) != null) | |
| 605 | + host.setBroadcastAddress(broadcastAddresses.get(i)); | |
| 579 | 606 | |
| 580 | 607 | if (metadataEnabled && partitioner != null && !allTokens.get(i).isEmpty()) |
| 581 | 608 | tokenMap.put(host, allTokens.get(i)); |
| @@ -630,7 +657,7 @@ | ||
| 630 | 657 | |
| 631 | 658 | for (Row row : peersFuture.get()) { |
| 632 | 659 | |
| 633 | - InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, true); | |
| 660 | + InetSocketAddress addr = rpcAddressForPeerHost(row, connection.address, cluster, true); | |
| 634 | 661 | if (addr == null || row.isNull("schema_version")) |
| 635 | 662 | continue; |
| 636 | 663 |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +78 −17
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import com.datastax.driver.core.policies.AddressTranslator; | |
| 18 | 19 | import com.google.common.collect.ImmutableList; |
| 19 | 20 | import com.google.common.util.concurrent.ListenableFuture; |
| 20 | 21 | import org.slf4j.Logger; |
| @@ -37,8 +38,20 @@ | ||
| 37 | 38 | |
| 38 | 39 | static final Logger statesLogger = LoggerFactory.getLogger(Host.class.getName() + ".STATES"); |
| 39 | 40 | |
| 41 | + // The address we'll use to connect to the node | |
| 40 | 42 | private final InetSocketAddress address; |
| 41 | 43 | |
| 44 | + // The broadcast_address as known by Cassandra. | |
| 45 | + // We use that internally because | |
| 46 | + // that's the 'peer' in the 'System.peers' table and avoids querying the full peers table in | |
| 47 | + // ControlConnection.refreshNodeInfo. | |
| 48 | + private volatile InetAddress broadcastAddress; | |
| 49 | + | |
| 50 | + // The listen_address as known by Cassandra. | |
| 51 | + // This is usually the same as broadcast_address unless | |
| 52 | + // specified otherwise in cassandra.yaml file. | |
| 53 | + private volatile InetAddress listenAddress; | |
| 54 | + | |
| 42 | 55 | enum State {ADDED, DOWN, UP} |
| 43 | 56 | |
| 44 | 57 | volatile State state; |
| @@ -59,12 +72,6 @@ | ||
| 59 | 72 | private volatile String rack; |
| 60 | 73 | private volatile VersionNumber cassandraVersion; |
| 61 | 74 | |
| 62 | - // The listen_address (really, the broadcast one) as know by Cassandra. We use that internally because | |
| 63 | - // that's the 'peer' in the 'System.peers' table and avoids querying the full peers table in | |
| 64 | - // ControlConnection.refreshNodeInfo. We don't want to expose however because we don't always have the info | |
| 65 | - // (partly because the 'System.local' doesn't have it for some weird reason for instance). | |
| 66 | - volatile InetAddress listenAddress; | |
| 67 | - | |
| 68 | 75 | private volatile Set<Token> tokens; |
| 69 | 76 | |
| 70 | 77 | // ClusterMetadata keeps one Host object per inet address and we rely on this (more precisely, |
| @@ -86,39 +93,93 @@ | ||
| 86 | 93 | this.rack = rack; |
| 87 | 94 | } |
| 88 | 95 | |
| 89 | - void setVersionAndListenAdress(String cassandraVersion, InetAddress listenAddress) { | |
| 90 | - if (listenAddress != null) | |
| 91 | - this.listenAddress = listenAddress; | |
| 92 | - | |
| 93 | - if (cassandraVersion == null) | |
| 94 | - return; | |
| 96 | + void setVersion(String cassandraVersion) { | |
| 97 | + VersionNumber versionNumber = null; | |
| 95 | 98 | try { |
| 96 | - this.cassandraVersion = VersionNumber.parse(cassandraVersion); | |
| 99 | + if (cassandraVersion != null) { | |
| 100 | + versionNumber = VersionNumber.parse(cassandraVersion); | |
| 101 | + } | |
| 97 | 102 | } catch (IllegalArgumentException e) { |
| 98 | 103 | logger.warn("Error parsing Cassandra version {}. This shouldn't have happened", cassandraVersion); |
| 99 | 104 | } |
| 105 | + this.cassandraVersion = versionNumber; | |
| 106 | + } | |
| 107 | + | |
| 108 | + void setBroadcastAddress(InetAddress broadcastAddress) { | |
| 109 | + this.broadcastAddress = broadcastAddress; | |
| 110 | + } | |
| 111 | + | |
| 112 | + void setListenAddress(InetAddress listenAddress) { | |
| 113 | + this.listenAddress = listenAddress; | |
| 100 | 114 | } |
| 101 | 115 | |
| 102 | 116 | /** |
| 103 | - * Returns the node address. | |
| 117 | + * Returns the address that the driver will use to connect to the node. | |
| 104 | 118 | * <p/> |
| 105 | 119 | * This is a shortcut for {@code getSocketAddress().getAddress()}. |
| 106 | 120 | * |
| 107 | - * @return the node {@link InetAddress}. | |
| 121 | + * @return the address. | |
| 122 | + * @see #getSocketAddress() | |
| 108 | 123 | */ |
| 109 | 124 | public InetAddress getAddress() { |
| 110 | 125 | return address.getAddress(); |
| 111 | 126 | } |
| 112 | 127 | |
| 113 | 128 | /** |
| 114 | - * Returns the node socket address. | |
| 129 | + * Returns the address and port that the driver will use to connect to the node. | |
| 130 | + * <p/> | |
| 131 | + * This is the node's broadcast RPC address, possibly translated if an {@link AddressTranslator} has been configured | |
| 132 | + * for this cluster. | |
| 133 | + * <p/> | |
| 134 | + * The broadcast RPC address is inferred from the following cassandra.yaml file settings: | |
| 135 | + * <ol> | |
| 136 | + * <li>{@code rpc_address}, {@code rpc_interface} or {@code broadcast_rpc_address}</li> | |
| 137 | + * <li>{@code native_transport_port}</li> | |
| 138 | + * </ol> | |
| 115 | 139 | * |
| 116 | - * @return the node {@link InetSocketAddress}. | |
| 140 | + * @return the address and port. | |
| 141 | + * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html>The cassandra.yaml configuration file</a> | |
| 117 | 142 | */ |
| 118 | 143 | public InetSocketAddress getSocketAddress() { |
| 119 | 144 | return address; |
| 120 | 145 | } |
| 121 | 146 | |
| 147 | + /** | |
| 148 | + * Returns the node broadcast address, i.e. the IP by which it should be contacted by other peers in the cluster. | |
| 149 | + * <p/> | |
| 150 | + * This corresponds to the {@code broadcast_address} cassandra.yaml file setting and | |
| 151 | + * is by default the same as {@link #getListenAddress()}, unless specified | |
| 152 | + * otherwise in cassandra.yaml. | |
| 153 | + * <em>This is NOT the address clients should use to contact this node</em>. | |
| 154 | + * <p/> | |
| 155 | + * Note that, depending on the Cassandra version the node is currently in, | |
| 156 | + * it might not be possible to determine the node's broadcast address, in which | |
| 157 | + * case this method will return {@code null}. | |
| 158 | + * | |
| 159 | + * @return the node broadcast address. | |
| 160 | + * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html>The cassandra.yaml configuration file</a> | |
| 161 | + */ | |
| 162 | + public InetAddress getBroadcastAddress() { | |
| 163 | + return broadcastAddress; | |
| 164 | + } | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * Returns the node listen address, i.e. the IP the node uses to contact other peers in the cluster. | |
| 168 | + * <p/> | |
| 169 | + * This corresponds to the {@code listen_address} cassandra.yaml file setting. | |
| 170 | + * <em>This is NOT the address clients should use to contact this node</em>. | |
| 171 | + * <p/> | |
| 172 | + * Note that, depending on the Cassandra version the node is currently in, | |
| 173 | + * it might not be possible to determine the node's listen address, in which | |
| 174 | + * case this method will return {@code null}. | |
| 175 | + * | |
| 176 | + * @return the node listen address. | |
| 177 | + * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html>The cassandra.yaml configuration file</a> | |
| 178 | + */ | |
| 179 | + public InetAddress getListenAddress() { | |
| 180 | + return listenAddress; | |
| 181 | + } | |
| 182 | + | |
| 122 | 183 | /** |
| 123 | 184 | * Returns the name of the datacenter this host is part of. |
| 124 | 185 | * <p/> |
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +9 −9
| @@ -288,9 +288,9 @@ | ||
| 288 | 288 | // Check against the info in the system tables, which is a bit weak since it's exactly how the metadata is |
| 289 | 289 | // constructed in the first place, but there's not much else we can do. |
| 290 | 290 | // Note that this relies on all queries going to node 1, which is why we use a WhiteList LBP in setup(). |
| 291 | - Row row = (host.listenAddress == null) | |
| 292 | - ? session.execute("select tokens from system.local").one() | |
| 293 | - : session.execute("select tokens from system.peers where peer = '" + host.listenAddress.getHostAddress() + "'").one(); | |
| 291 | + Row row = session.execute("select tokens from system.peers where peer = '" + host.getBroadcastAddress().getHostAddress() + "'").one(); | |
| 292 | + if (row == null) | |
| 293 | + row = session.execute("select tokens from system.local").one(); | |
| 294 | 294 | Set<String> tokenStrings = row.getSet("tokens", String.class); |
| 295 | 295 | assertThat(tokenStrings).hasSize(numTokens); |
| 296 | 296 | Iterable<Token> tokensFromSystemTable = Iterables.transform(tokenStrings, new Function<String, Token>() { |
| @@ -323,12 +323,12 @@ | ||
| 323 | 323 | assertOnlyOneWrapped(ranges); |
| 324 | 324 | |
| 325 | 325 | Iterable<TokenRange> splitRanges = Iterables.concat(Iterables.transform(ranges, |
| 326 | - new Function<TokenRange, Iterable<TokenRange>>() { | |
| 327 | - @Override | |
| 328 | - public Iterable<TokenRange> apply(TokenRange input) { | |
| 329 | - return input.splitEvenly(10); | |
| 330 | - } | |
| 331 | - }) | |
| 326 | + new Function<TokenRange, Iterable<TokenRange>>() { | |
| 327 | + @Override | |
| 328 | + public Iterable<TokenRange> apply(TokenRange input) { | |
| 329 | + return input.splitEvenly(10); | |
| 330 | + } | |
| 331 | + }) | |
| 332 | 332 | ); |
| 333 | 333 | |
| 334 | 334 | assertOnlyOneWrapped(splitRanges); |