Elffuss BraidHub

kikocis/java-driver public

JAVA-1038: Fetch node info by rpc_address if its broadcast_address is not in system.peers.

(cherry-picked from 2.1)
Changesetafbf07e123a03155f7f909160bf3ed335a108189538f2a9be06d6bfb81d663e0
Authorwy96f <wy96fyw@gmail.com> · 2015-12-05 16:44 +08:00
Committerolim7t <omichallat+github@gmail.com> · 2016-06-16 19:44 -07:00
Parents5a43c6822d54

Browse the files at this changeset

4 files changed

modified changelog/README.md +52 −0
@@ -1,5 +1,57 @@
11 ## Changelog
22
3+### 3.1.0 (in progress)
4+
5+Merged from 3.0.x branch:
6+
7+- [bug] JAVA-1179: Request objects should be copied when executed.
8+- [improvement] JAVA-1182: Throw error when synchronous call made on I/O thread.
9+- [bug] JAVA-1184: Unwrap StatementWrappers when extracting column definitions.
10+- [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
11+- [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger.
12+- [improvement] JAVA-1151: Fail fast if HdrHistogram is not in the classpath.
13+- [improvement] JAVA-1154: Allow individual Statement to cancel the read timeout.
14+- [bug] JAVA-1074: Fix documentation around default timestamp generator.
15+- [improvement] JAVA-1109: Document SSLOptions changes in upgrade guide.
16+- [improvement] JAVA-1065: Add method to create token from partition key values.
17+- [improvement] JAVA-1136: Enable JDK signature check in module driver-extras.
18+- [improvement] JAVA-866: Support tuple notation in QueryBuilder.eq/in.
19+- [bug] JAVA-1140: Use same connection to check for schema agreement after a DDL query.
20+- [improvement] JAVA-1113: Support Cassandra 3.4 LIKE operator in QueryBuilder.
21+- [improvement] JAVA-1086: Support Cassandra 3.2 CAST function in QueryBuilder.
22+- [bug] JAVA-1095: Check protocol version for custom payload before sending the query.
23+- [improvement] JAVA-1133: Add OSGi headers to cassandra-driver-extras.
24+- [bug] JAVA-1137: Incorrect string returned by DataType.asFunctionParameterString() for collections and tuples.
25+- [bug] JAVA-1046: (Dynamic)CompositeTypes need to be parsed as string literal, not blob.
26+- [improvement] JAVA-1164: Clarify documentation on Host.listenAddress and broadcastAddress.
27+- [improvement] JAVA-1171: Add Host method to determine if DSE Graph is enabled.
28+- [improvement] JAVA-1069: Bootstrap driver-examples module.
29+- [documentation] JAVA-1150: Add example and FAQ entry about ByteBuffer/BLOB.
30+- [improvement] JAVA-1011: Expose PoolingOptions default values.
31+- [improvement] JAVA-630: Don't process DOWN events for nodes that have active connections.
32+- [improvement] JAVA-851: Improve UUIDs javadoc with regard to user-provided timestamps.
33+- [improvement] JAVA-979: Update javadoc for RegularStatement toString() and getQueryString() to indicate that consistency level and other parameters are not maintained in the query string.
34+- [bug] JAVA-1068: Unwrap StatementWrappers when hashing the paging state.
35+- [improvement] JAVA-1021: Improve error message when connect() is called with an invalid keyspace name.
36+- [improvement] JAVA-879: Mapper.map() accepts mapper-generated and user queries.
37+- [bug] JAVA-1100: Exception when connecting with shaded java driver in OSGI
38+- [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key.
39+- [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
40+- [improvement] JAVA-923: Position idempotent flag on object mapper queries.
41+- [bug] JAVA-1070: The Mapper should not prepare queries synchronously.
42+- [new feature] JAVA-982: Introduce new method ConsistencyLevel.isSerial().
43+- [bug] JAVA-764: Retry with the normal consistency level (not the serial one) when a write times out on the Paxos phase.
44+- [improvement] JAVA-852: Ignore peers with null entries during discovery.
45+- [bug] JAVA-1005: DowngradingConsistencyRetryPolicy does not work with EACH_QUORUM when 1 DC is down.
46+- [bug] JAVA-1002: Avoid deadlock when re-preparing a statement on other hosts.
47+- [bug] JAVA-1072: Ensure defunct connections are properly evicted from the pool.
48+- [bug] JAVA-1152: Fix NPE at ControlConnection.refreshNodeListAndTokenMap().
49+
50+Merged from 2.1 branch:
51+
52+- [improvement] JAVA-1038: Fetch node info by rpc_address if its broadcast_address is not in system.peers.
53+
54+
355 ### 3.0.2
456
557 Merged from 2.1 branch:
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +8 −1
@@ -402,7 +402,14 @@
402402 ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_LOCAL))
403403 : new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS + " WHERE peer='" + host.getBroadcastAddress().getHostAddress() + '\''));
404404 c.write(future);
405- return future.get().one();
405+ Row row = future.get().one();
406+ if (row != null) {
407+ return row;
408+ } else {
409+ logger.debug("Could not find peer with broadcast address {}, " +
410+ "falling back to a full system.peers scan to fetch info for {} " +
411+ "(this can happen if the broadcast address changed)", host.getBroadcastAddress(), host);
412+ }
406413 }
407414
408415 // We have to fetch the whole peers table and find the host we're looking for
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +99 −0
@@ -19,8 +19,11 @@
1919 import com.datastax.driver.core.utils.CassandraVersion;
2020 import com.google.common.base.Function;
2121 import com.google.common.collect.HashMultiset;
22+import com.google.common.collect.ImmutableMap;
23+import com.google.common.collect.ImmutableSet;
2224 import com.google.common.collect.Maps;
2325 import org.apache.log4j.Level;
26+import org.scassandra.http.client.PrimingRequest;
2427 import org.slf4j.Logger;
2528 import org.slf4j.LoggerFactory;
2629 import org.testng.annotations.DataProvider;
@@ -28,17 +31,22 @@
2831
2932 import java.net.InetAddress;
3033 import java.net.InetSocketAddress;
34+import java.net.UnknownHostException;
3135 import java.util.Collection;
3236 import java.util.Iterator;
3337 import java.util.Map;
38+import java.util.UUID;
3439 import java.util.concurrent.TimeUnit;
3540 import java.util.concurrent.atomic.AtomicInteger;
3641
3742 import static com.datastax.driver.core.Assertions.assertThat;
3843 import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
44+import static com.datastax.driver.core.ScassandraCluster.SELECT_PEERS;
45+import static com.datastax.driver.core.ScassandraCluster.datacenter;
3946 import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions;
4047 import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions;
4148 import static com.google.common.collect.Lists.newArrayList;
49+import static org.scassandra.http.client.PrimingRequest.then;
4250
4351 @CreateCCM(PER_METHOD)
4452 @CCMConfig(dirtiesContext = true, createCluster = false)
@@ -310,6 +318,97 @@
310318 }
311319 }
312320
321+ /**
322+ * Ensures that when a node changes its broadcast address (for example, after
323+ * a shutdown and startup on EC2 and its public IP has changed),
324+ * the driver will be able to detect that change and recognize the host
325+ * in the system.peers table in spite of that change.
326+ *
327+ * @jira_ticket JAVA-1038
328+ * @expected_result The driver should be able to detect that a host has changed its broadcast address
329+ * and update its metadata accordingly.
330+ * @test_category control_connection
331+ * @since 2.1.10
332+ */
333+ @SuppressWarnings("unchecked")
334+ @Test(groups = "short")
335+ @CCMConfig(createCcm = false)
336+ public void should_fetch_whole_peers_table_if_broadcast_address_changed() throws UnknownHostException {
337+ ScassandraCluster scassandras = ScassandraCluster.builder().withNodes(2).build();
338+ scassandras.init();
339+
340+ InetSocketAddress node2RpcAddress = scassandras.address(2);
341+
342+ Cluster cluster = Cluster.builder()
343+ .addContactPoints(scassandras.address(1).getAddress())
344+ .withPort(scassandras.getBinaryPort())
345+ .withNettyOptions(nonQuietClusterCloseOptions)
346+ .build();
347+
348+ try {
349+
350+ cluster.init();
351+
352+ Host host2 = cluster.getMetadata().getHost(node2RpcAddress);
353+ assertThat(host2).isNotNull();
354+
355+ InetAddress node2OldBroadcastAddress = host2.getBroadcastAddress();
356+ InetAddress node2NewBroadcastAddress = InetAddress.getByName("1.2.3.4");
357+
358+ // host 2 has the old broadcast_address (which is identical to its rpc_broadcast_address)
359+ assertThat(host2.getAddress())
360+ .isEqualTo(node2OldBroadcastAddress);
361+
362+ // simulate a change in host 2 public IP
363+ Map<String, ?> rows = ImmutableMap.<String, Object>builder()
364+ .put("peer", node2NewBroadcastAddress) // new broadcast address for host 2
365+ .put("rpc_address", host2.getAddress()) // rpc_broadcast_address remains unchanged
366+ .put("host_id", UUID.randomUUID())
367+ .put("data_center", datacenter(1))
368+ .put("rack", "rack1")
369+ .put("release_version", "2.1.8")
370+ .put("tokens", ImmutableSet.of(Long.toString(scassandras.getTokensForDC(1).get(1))))
371+ .build();
372+
373+ scassandras.node(1).primingClient().clearAllPrimes();
374+
375+ // the driver will attempt to locate host2 in system.peers by its old broadcast address, and that will fail
376+ scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder()
377+ .withQuery("SELECT * FROM system.peers WHERE peer='" + node2OldBroadcastAddress + "'")
378+ .withThen(then()
379+ .withColumnTypes(SELECT_PEERS)
380+ .build())
381+ .build());
382+
383+ // the driver will then attempt to fetch the whole system.peers
384+ scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder()
385+ .withQuery("SELECT * FROM system.peers")
386+ .withThen(then()
387+ .withColumnTypes(SELECT_PEERS)
388+ .withRows(rows)
389+ .build())
390+ .build());
391+
392+ assertThat(cluster.manager.controlConnection.refreshNodeInfo(host2)).isTrue();
393+
394+ host2 = cluster.getMetadata().getHost(node2RpcAddress);
395+
396+ // host2 should now have a new broadcast address
397+ assertThat(host2).isNotNull();
398+ assertThat(host2.getBroadcastAddress())
399+ .isEqualTo(node2NewBroadcastAddress);
400+
401+ // host 2 should keep its old rpc broadcast address
402+ assertThat(host2.getSocketAddress())
403+ .isEqualTo(node2RpcAddress);
404+
405+ } finally {
406+ cluster.close();
407+ scassandras.stop();
408+ }
409+
410+ }
411+
313412 static class QueryPlanCountingPolicy extends DelegatingLoadBalancingPolicy {
314413
315414 final AtomicInteger counter = new AtomicInteger();
modified driver-core/src/test/java/com/datastax/driver/core/ScassandraCluster.java +3 −3
@@ -258,7 +258,7 @@
258258 start(cluster, ipSuffix(dc, node));
259259 }
260260
261- private List<Long> getTokensForDC(int dc) {
261+ public List<Long> getTokensForDC(int dc) {
262262 // Offset DCs by dc * 100 to ensure unique tokens.
263263 int offset = (dc - 1) * 100;
264264 int dcNodeCount = nodes(dc).size();
@@ -400,7 +400,7 @@
400400 : defaultValue;
401401 }
402402
403- static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_PEERS = {
403+ public static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_PEERS = {
404404 column("peer", INET),
405405 column("rpc_address", INET),
406406 column("data_center", TEXT),
@@ -413,7 +413,7 @@
413413 column("schema_version", UUID)
414414 };
415415
416- static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = {
416+ public static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = {
417417 column("key", TEXT),
418418 column("bootstrapped", TEXT),
419419 column("broadcast_address", INET),