kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2b3a37f80c2d31bf3866e971f5e6039ea5e55fc18b11f8f3d3f69d4763a0d84c |
|---|---|
| Author | wy96f <wy96fyw@gmail.com> · 2015-12-05 16:44 +08:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-02-23 18:41 +01:00 |
| Parents | e35903a94cbb |
4 files changed
modified changelog/README.md +1 −0
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | - [improvement] JAVA-630: Don't process DOWN events for nodes that have active connections. |
| 17 | 17 | - [improvement] JAVA-851: Improve UUIDs javadoc with regard to user-provided timestamps. |
| 18 | 18 | - [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. |
| 19 | +- [improvement] JAVA-1038: Fetch node info by rpc_address if its broadcast_address is not in system.peers. | |
| 19 | 20 | |
| 20 | 21 | Merged from 2.0 branch: |
| 21 | 22 |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +8 −1
| @@ -448,7 +448,14 @@ | ||
| 448 | 448 | ? new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_LOCAL)) |
| 449 | 449 | : new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS + " WHERE peer='" + host.listenAddress.getHostAddress() + '\'')); |
| 450 | 450 | c.write(future); |
| 451 | - return future.get().one(); | |
| 451 | + Row row = future.get().one(); | |
| 452 | + if (row != null) { | |
| 453 | + return row; | |
| 454 | + } else { | |
| 455 | + logger.debug("Could not find peer with broadcast address {}, " + | |
| 456 | + "falling back to a full system.peers scan to fetch info for {} " + | |
| 457 | + "(this can happen if the broadcast address changed)", host.listenAddress, host); | |
| 458 | + } | |
| 452 | 459 | } |
| 453 | 460 | |
| 454 | 461 | // 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 +97 −0
| @@ -22,13 +22,17 @@ | ||
| 22 | 22 | import com.datastax.driver.core.utils.CassandraVersion; |
| 23 | 23 | import com.google.common.base.Function; |
| 24 | 24 | import com.google.common.collect.HashMultiset; |
| 25 | +import com.google.common.collect.ImmutableMap; | |
| 26 | +import com.google.common.collect.ImmutableSet; | |
| 25 | 27 | import com.google.common.collect.Maps; |
| 28 | +import org.scassandra.http.client.PrimingRequest; | |
| 26 | 29 | import org.slf4j.Logger; |
| 27 | 30 | import org.slf4j.LoggerFactory; |
| 28 | 31 | import org.testng.annotations.Test; |
| 29 | 32 | |
| 30 | 33 | import java.net.InetAddress; |
| 31 | 34 | import java.net.InetSocketAddress; |
| 35 | +import java.net.UnknownHostException; | |
| 32 | 36 | import java.util.Collection; |
| 33 | 37 | import java.util.Iterator; |
| 34 | 38 | import java.util.Map; |
| @@ -37,9 +41,12 @@ | ||
| 37 | 41 | |
| 38 | 42 | import static com.datastax.driver.core.Assertions.assertThat; |
| 39 | 43 | 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; | |
| 40 | 46 | import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; |
| 41 | 47 | import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; |
| 42 | 48 | import static com.google.common.collect.Lists.newArrayList; |
| 49 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 43 | 50 | |
| 44 | 51 | @CreateCCM(PER_METHOD) |
| 45 | 52 | @CCMConfig(dirtiesContext = true, createCluster = false) |
| @@ -215,6 +222,96 @@ | ||
| 215 | 222 | |
| 216 | 223 | } |
| 217 | 224 | |
| 225 | + /** | |
| 226 | + * Ensures that when a node changes its broadcast address (for example, after | |
| 227 | + * a shutdown and startup on EC2 and its public IP has changed), | |
| 228 | + * the driver will be able to detect that change and recognize the host | |
| 229 | + * in the system.peers table in spite of that change. | |
| 230 | + * | |
| 231 | + * @jira_ticket JAVA-1038 | |
| 232 | + * @expected_result The driver should be able to detect that a host has changed its broadcast address | |
| 233 | + * and update its metadata accordingly. | |
| 234 | + * @test_category control_connection | |
| 235 | + * @since 2.1.10 | |
| 236 | + */ | |
| 237 | + @SuppressWarnings("unchecked") | |
| 238 | + @Test(groups = "short") | |
| 239 | + @CCMConfig(createCcm = false) | |
| 240 | + public void should_fetch_whole_peers_table_if_broadcast_address_changed() throws UnknownHostException { | |
| 241 | + ScassandraCluster scassandras = ScassandraCluster.builder().withNodes(2).build(); | |
| 242 | + scassandras.init(); | |
| 243 | + | |
| 244 | + InetSocketAddress node2RpcAddress = scassandras.address(2); | |
| 245 | + | |
| 246 | + Cluster cluster = Cluster.builder() | |
| 247 | + .addContactPoints(scassandras.address(1).getAddress()) | |
| 248 | + .withPort(scassandras.getBinaryPort()) | |
| 249 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 250 | + .build(); | |
| 251 | + | |
| 252 | + try { | |
| 253 | + | |
| 254 | + cluster.init(); | |
| 255 | + | |
| 256 | + Host host2 = cluster.getMetadata().getHost(node2RpcAddress); | |
| 257 | + assertThat(host2).isNotNull(); | |
| 258 | + | |
| 259 | + InetAddress node2OldBroadcastAddress = host2.listenAddress; | |
| 260 | + InetAddress node2NewBroadcastAddress = InetAddress.getByName("1.2.3.4"); | |
| 261 | + | |
| 262 | + // host 2 has the old broadcast_address (which is identical to its rpc_broadcast_address) | |
| 263 | + assertThat(host2.getAddress()) | |
| 264 | + .isEqualTo(node2OldBroadcastAddress); | |
| 265 | + | |
| 266 | + // simulate a change in host 2 public IP | |
| 267 | + Map<String, ?> rows = ImmutableMap.<String, Object>builder() | |
| 268 | + .put("peer", node2NewBroadcastAddress) // new broadcast address for host 2 | |
| 269 | + .put("rpc_address", host2.getAddress()) // rpc_broadcast_address remains unchanged | |
| 270 | + .put("data_center", datacenter(1)) | |
| 271 | + .put("rack", "rack1") | |
| 272 | + .put("release_version", "2.1.8") | |
| 273 | + .put("tokens", ImmutableSet.of(Long.toString(scassandras.getTokensForDC(1).get(1)))) | |
| 274 | + .build(); | |
| 275 | + | |
| 276 | + scassandras.node(1).primingClient().clearAllPrimes(); | |
| 277 | + | |
| 278 | + // the driver will attempt to locate host2 in system.peers by its old broadcast address, and that will fail | |
| 279 | + scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 280 | + .withQuery("SELECT * FROM system.peers WHERE peer='" + node2OldBroadcastAddress + "'") | |
| 281 | + .withThen(then() | |
| 282 | + .withColumnTypes(SELECT_PEERS) | |
| 283 | + .build()) | |
| 284 | + .build()); | |
| 285 | + | |
| 286 | + // the driver will then attempt to fetch the whole system.peers | |
| 287 | + scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 288 | + .withQuery("SELECT * FROM system.peers") | |
| 289 | + .withThen(then() | |
| 290 | + .withColumnTypes(SELECT_PEERS) | |
| 291 | + .withRows(rows) | |
| 292 | + .build()) | |
| 293 | + .build()); | |
| 294 | + | |
| 295 | + assertThat(cluster.manager.controlConnection.refreshNodeInfo(host2)).isTrue(); | |
| 296 | + | |
| 297 | + host2 = cluster.getMetadata().getHost(node2RpcAddress); | |
| 298 | + | |
| 299 | + // host2 should now have a new broadcast address | |
| 300 | + assertThat(host2).isNotNull(); | |
| 301 | + assertThat(host2.listenAddress) | |
| 302 | + .isEqualTo(node2NewBroadcastAddress); | |
| 303 | + | |
| 304 | + // host 2 should keep its old rpc broadcast address | |
| 305 | + assertThat(host2.getSocketAddress()) | |
| 306 | + .isEqualTo(node2RpcAddress); | |
| 307 | + | |
| 308 | + } finally { | |
| 309 | + cluster.close(); | |
| 310 | + scassandras.stop(); | |
| 311 | + } | |
| 312 | + | |
| 313 | + } | |
| 314 | + | |
| 218 | 315 | static class QueryPlanCountingPolicy extends DelegatingLoadBalancingPolicy { |
| 219 | 316 | |
| 220 | 317 | final AtomicInteger counter = new AtomicInteger(); |
modified driver-core/src/test/java/com/datastax/driver/core/ScassandraCluster.java +3 −3
| @@ -259,7 +259,7 @@ | ||
| 259 | 259 | start(cluster, ipSuffix(dc, node)); |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | - private List<Long> getTokensForDC(int dc) { | |
| 262 | + public List<Long> getTokensForDC(int dc) { | |
| 263 | 263 | // Offset DCs by dc * 100 to ensure unique tokens. |
| 264 | 264 | int offset = (dc - 1) * 100; |
| 265 | 265 | int dcNodeCount = nodes(dc).size(); |
| @@ -367,7 +367,7 @@ | ||
| 367 | 367 | : defaultValue; |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | - static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_PEERS = { | |
| 370 | + public static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_PEERS = { | |
| 371 | 371 | column("peer", INET), |
| 372 | 372 | column("rpc_address", INET), |
| 373 | 373 | column("data_center", TEXT), |
| @@ -376,7 +376,7 @@ | ||
| 376 | 376 | column("tokens", set(TEXT)) |
| 377 | 377 | }; |
| 378 | 378 | |
| 379 | - static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = { | |
| 379 | + public static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = { | |
| 380 | 380 | column("key", TEXT), |
| 381 | 381 | column("bootstrapped", TEXT), |
| 382 | 382 | column("broadcast_address", INET), |