|
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 | 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 @@ public Integer apply(InetAddress input) { |
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(); |
|
0 commit comments