Elffuss BraidHub

kikocis/java-driver public

Merge pull request #570 from datastax/java1035

JAVA-1035: Expose host broadcast_address and listen_address if available.
Changesetdfc873429762d13be9b15eebe91698caa59c36040cfe3201c216632a6998aa4b
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-01-15 19:17 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-01-15 19:17 +01:00
Parentsec7bd869f099, 5c9db3fa0bee

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -3,6 +3,7 @@
33 ### 3.0.0 (in progress)
44
55 - [bug] JAVA-1034: fix metadata parser for collections of custom types.
6+- [improvement] JAVA-1035: Expose host broadcast_address and listen_address if available.
67
78 Merged from 2.1 branch:
89
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +53 −26
@@ -368,32 +368,39 @@
368368 }
369369 }
370370
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()))) {
376383 // Some DSE versions were inserting a line for the local node in peers (with mostly null values). This has been fixed, but if we
377384 // detect that's the case, ignore it as it's not really a big deal.
378385 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);
379386 return null;
380- } else if (addr == null) {
387+ } else if (rpcAddress == null) {
381388 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);
383390 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;
387394 }
388- return cluster.translateAddress(addr);
395+ return cluster.translateAddress(rpcAddress);
389396 }
390397
391398 private Row fetchNodeInfo(Host host, Connection c) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException {
392399 boolean isConnectedHost = c.address.equals(host.getSocketAddress());
393- if (isConnectedHost || host.listenAddress != null) {
400+ if (isConnectedHost || host.getBroadcastAddress() != null) {
394401 DefaultResultSetFuture future = isConnectedHost
395402 ? 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() + '\''));
397404 c.write(future);
398405 return future.get().one();
399406 }
@@ -402,7 +409,7 @@
402409 DefaultResultSetFuture future = new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS));
403410 c.write(future);
404411 for (Row row : future.get()) {
405- InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster, true);
412+ InetSocketAddress addr = rpcAddressForPeerHost(row, c.address, cluster, true);
406413 if (addr != null && addr.equals(host.getSocketAddress()))
407414 return row;
408415 }
@@ -434,7 +441,7 @@
434441 }
435442 // Ignore hosts with a null rpc_address, as this is most likely a phantom row in system.peers (JAVA-428).
436443 // 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
438445 } else if (!c.address.equals(host.getSocketAddress()) && row.getInet("rpc_address") == null) {
439446 logger.warn("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", host.getAddress(), c.address, host.getAddress());
440447 return false;
@@ -472,12 +479,30 @@
472479 updateLocationInfo(host, row.getString("data_center"), row.getString("rack"), isInitialConnection, cluster);
473480
474481 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")
478503 : null;
504+ host.setListenAddress(listenAddress);
479505
480- host.setVersionAndListenAdress(version, listenAddress);
481506 }
482507
483508 private static void updateLocationInfo(Host host, String datacenter, String rack, boolean isInitialConnection, Cluster.Manager cluster) {
@@ -539,19 +564,19 @@
539564 List<String> dcs = new ArrayList<String>();
540565 List<String> racks = new ArrayList<String>();
541566 List<String> cassandraVersions = new ArrayList<String>();
542- List<InetAddress> listenAddresses = new ArrayList<InetAddress>();
567+ List<InetAddress> broadcastAddresses = new ArrayList<InetAddress>();
543568 List<Set<String>> allTokens = new ArrayList<Set<String>>();
544569
545570 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)
548573 continue;
549574
550- foundHosts.add(addr);
575+ foundHosts.add(rpcAddress);
551576 dcs.add(row.getString("data_center"));
552577 racks.add(row.getString("rack"));
553578 cassandraVersions.add(row.getString("release_version"));
554- listenAddresses.add(row.getInet("peer"));
579+ broadcastAddresses.add(row.getInet("peer"));
555580 if (metadataEnabled)
556581 allTokens.add(row.getSet("tokens", String.class));
557582 }
@@ -575,7 +600,9 @@
575600 if (dcs.get(i) != null || racks.get(i) != null)
576601 updateLocationInfo(host, dcs.get(i), racks.get(i), isInitialConnection, cluster);
577602 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));
579606
580607 if (metadataEnabled && partitioner != null && !allTokens.get(i).isEmpty())
581608 tokenMap.put(host, allTokens.get(i));
@@ -630,7 +657,7 @@
630657
631658 for (Row row : peersFuture.get()) {
632659
633- InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, true);
660+ InetSocketAddress addr = rpcAddressForPeerHost(row, connection.address, cluster, true);
634661 if (addr == null || row.isNull("schema_version"))
635662 continue;
636663
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +78 −17
@@ -15,6 +15,7 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import com.datastax.driver.core.policies.AddressTranslator;
1819 import com.google.common.collect.ImmutableList;
1920 import com.google.common.util.concurrent.ListenableFuture;
2021 import org.slf4j.Logger;
@@ -37,8 +38,20 @@
3738
3839 static final Logger statesLogger = LoggerFactory.getLogger(Host.class.getName() + ".STATES");
3940
41+ // The address we'll use to connect to the node
4042 private final InetSocketAddress address;
4143
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+
4255 enum State {ADDED, DOWN, UP}
4356
4457 volatile State state;
@@ -59,12 +72,6 @@
5972 private volatile String rack;
6073 private volatile VersionNumber cassandraVersion;
6174
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-
6875 private volatile Set<Token> tokens;
6976
7077 // ClusterMetadata keeps one Host object per inet address and we rely on this (more precisely,
@@ -86,39 +93,93 @@
8693 this.rack = rack;
8794 }
8895
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;
9598 try {
96- this.cassandraVersion = VersionNumber.parse(cassandraVersion);
99+ if (cassandraVersion != null) {
100+ versionNumber = VersionNumber.parse(cassandraVersion);
101+ }
97102 } catch (IllegalArgumentException e) {
98103 logger.warn("Error parsing Cassandra version {}. This shouldn't have happened", cassandraVersion);
99104 }
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;
100114 }
101115
102116 /**
103- * Returns the node address.
117+ * Returns the address that the driver will use to connect to the node.
104118 * <p/>
105119 * This is a shortcut for {@code getSocketAddress().getAddress()}.
106120 *
107- * @return the node {@link InetAddress}.
121+ * @return the address.
122+ * @see #getSocketAddress()
108123 */
109124 public InetAddress getAddress() {
110125 return address.getAddress();
111126 }
112127
113128 /**
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>
115139 *
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>
117142 */
118143 public InetSocketAddress getSocketAddress() {
119144 return address;
120145 }
121146
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+
122183 /**
123184 * Returns the name of the datacenter this host is part of.
124185 * <p/>
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +9 −9
@@ -288,9 +288,9 @@
288288 // Check against the info in the system tables, which is a bit weak since it's exactly how the metadata is
289289 // constructed in the first place, but there's not much else we can do.
290290 // 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();
294294 Set<String> tokenStrings = row.getSet("tokens", String.class);
295295 assertThat(tokenStrings).hasSize(numTokens);
296296 Iterable<Token> tokensFromSystemTable = Iterables.transform(tokenStrings, new Function<String, Token>() {
@@ -323,12 +323,12 @@
323323 assertOnlyOneWrapped(ranges);
324324
325325 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+ })
332332 );
333333
334334 assertOnlyOneWrapped(splitRanges);