kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 23c646c58e549459eaa2131d7b04276c6ad82b47f192039d2b346c2e09c88464 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-10 20:59 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-10 20:59 +01:00 |
| Parents | 064e9c2c7850, a0603f86c121 |
6 files changed
modified changelog/README.md +1 −0
| @@ -38,6 +38,7 @@ | ||
| 38 | 38 | - [improvement] JAVA-444: Add Java process information to UUIDs.makeNode() hash. |
| 39 | 39 | - [improvement] JAVA-977: Preserve original cause when BuiltStatement value can't be serialized. |
| 40 | 40 | - [bug] JAVA-1094: Backport TypeCodec parse and format fixes from 3.0. |
| 41 | +- [improvement] JAVA-852: Ignore peers with null entries during discovery. | |
| 41 | 42 | |
| 42 | 43 | Merged from 2.0 branch: |
| 43 | 44 |
modified driver-core/pom.xml +18 −12
| @@ -15,7 +15,8 @@ | ||
| 15 | 15 | limitations under the License. |
| 16 | 16 | |
| 17 | 17 | --> |
| 18 | -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| 18 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| 19 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| 19 | 20 | <modelVersion>4.0.0</modelVersion> |
| 20 | 21 | <parent> |
| 21 | 22 | <groupId>com.datastax.cassandra</groupId> |
| @@ -25,7 +26,9 @@ | ||
| 25 | 26 | <artifactId>cassandra-driver-core</artifactId> |
| 26 | 27 | <packaging>bundle</packaging> |
| 27 | 28 | <name>DataStax Java Driver for Apache Cassandra - Core</name> |
| 28 | - <description>A driver for Apache Cassandra 1.2+ that works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's binary protocol.</description> | |
| 29 | + <description>A driver for Apache Cassandra 1.2+ that works exclusively with the Cassandra Query Language version 3 | |
| 30 | + (CQL3) and Cassandra's binary protocol. | |
| 31 | + </description> | |
| 29 | 32 | <url>https://github.com/datastax/java-driver</url> |
| 30 | 33 | |
| 31 | 34 | <properties> |
| @@ -67,17 +70,17 @@ | ||
| 67 | 70 | <!-- Each of them is only a mandatory runtime dependency if you want to use the compression it offers --> |
| 68 | 71 | |
| 69 | 72 | <dependency> |
| 70 | - <groupId>org.xerial.snappy</groupId> | |
| 71 | - <artifactId>snappy-java</artifactId> | |
| 72 | - <version>${snappy.version}</version> | |
| 73 | - <optional>true</optional> | |
| 73 | + <groupId>org.xerial.snappy</groupId> | |
| 74 | + <artifactId>snappy-java</artifactId> | |
| 75 | + <version>${snappy.version}</version> | |
| 76 | + <optional>true</optional> | |
| 74 | 77 | </dependency> |
| 75 | 78 | |
| 76 | 79 | <dependency> |
| 77 | - <groupId>net.jpountz.lz4</groupId> | |
| 78 | - <artifactId>lz4</artifactId> | |
| 79 | - <version>${lz4.version}</version> | |
| 80 | - <optional>true</optional> | |
| 80 | + <groupId>net.jpountz.lz4</groupId> | |
| 81 | + <artifactId>lz4</artifactId> | |
| 82 | + <version>${lz4.version}</version> | |
| 83 | + <optional>true</optional> | |
| 81 | 84 | </dependency> |
| 82 | 85 | |
| 83 | 86 | <!-- End of compression libraries --> |
| @@ -221,7 +224,9 @@ | ||
| 221 | 224 | <executions> |
| 222 | 225 | <execution> |
| 223 | 226 | <phase>package</phase> |
| 224 | - <goals><goal>shade</goal></goals> | |
| 227 | + <goals> | |
| 228 | + <goal>shade</goal> | |
| 229 | + </goals> | |
| 225 | 230 | <configuration> |
| 226 | 231 | <shadedArtifactAttached>true</shadedArtifactAttached> |
| 227 | 232 | <artifactSet> |
| @@ -287,7 +292,7 @@ | ||
| 287 | 292 | </goals> |
| 288 | 293 | </pluginExecutionFilter> |
| 289 | 294 | <action> |
| 290 | - <ignore /> | |
| 295 | + <ignore/> | |
| 291 | 296 | </action> |
| 292 | 297 | </pluginExecution> |
| 293 | 298 | </pluginExecutions> |
| @@ -375,6 +380,7 @@ | ||
| 375 | 380 | <includes> |
| 376 | 381 | <include>**/SSL*Test.java</include> |
| 377 | 382 | <include>**/UUIDsPID*.java</include> |
| 383 | + <include>**/ControlConnectionTest.java</include> | |
| 378 | 384 | </includes> |
| 379 | 385 | </configuration> |
| 380 | 386 | </plugin> |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +63 −17
| @@ -38,6 +38,8 @@ | ||
| 38 | 38 | |
| 39 | 39 | private static final Logger logger = LoggerFactory.getLogger(ControlConnection.class); |
| 40 | 40 | |
| 41 | + private static final boolean EXTENDED_PEER_CHECK = SystemProperties.getBoolean("com.datastax.driver.EXTENDED_PEER_CHECK", true); | |
| 42 | + | |
| 41 | 43 | private static final InetAddress bindAllAddress; |
| 42 | 44 | |
| 43 | 45 | static { |
| @@ -280,7 +282,7 @@ | ||
| 280 | 282 | // We need to refresh the node list again; |
| 281 | 283 | // We want that because the token map was not properly initialized by the first call above, |
| 282 | 284 | // since it requires the list of keyspaces to be loaded. |
| 283 | - refreshNodeListAndTokenMap(connection, cluster, false, true); | |
| 285 | + refreshNodeListAndTokenMap(connection, cluster, false, false); | |
| 284 | 286 | |
| 285 | 287 | return connection; |
| 286 | 288 | } catch (BusyConnectionException e) { |
| @@ -421,19 +423,18 @@ | ||
| 421 | 423 | } |
| 422 | 424 | } |
| 423 | 425 | |
| 424 | - private static InetSocketAddress addressToUseForPeerHost(Row peersRow, InetSocketAddress connectedHost, Cluster.Manager cluster, boolean logMissingRpcAddresses) { | |
| 426 | + private static InetSocketAddress addressToUseForPeerHost(Row peersRow, InetSocketAddress connectedHost, Cluster.Manager cluster) { | |
| 425 | 427 | InetAddress peer = peersRow.getInet("peer"); |
| 426 | 428 | InetAddress addr = peersRow.getInet("rpc_address"); |
| 427 | 429 | |
| 428 | - if (peer.equals(connectedHost.getAddress()) || (addr != null && addr.equals(connectedHost.getAddress()))) { | |
| 430 | + // We've already called isValid on the row, which checks this | |
| 431 | + assert addr != null; | |
| 432 | + | |
| 433 | + if (peer.equals(connectedHost.getAddress()) || addr.equals(connectedHost.getAddress())) { | |
| 429 | 434 | // Some DSE versions were inserting a line for the local node in peers (with mostly null values). This has been fixed, but if we |
| 430 | 435 | // detect that's the case, ignore it as it's not really a big deal. |
| 431 | 436 | 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); |
| 432 | 437 | return null; |
| 433 | - } else if (addr == null) { | |
| 434 | - if (logMissingRpcAddresses) | |
| 435 | - logger.warn("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", peer, connectedHost, peer); | |
| 436 | - return null; | |
| 437 | 438 | } else if (addr.equals(bindAllAddress)) { |
| 438 | 439 | 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); |
| 439 | 440 | addr = peer; |
| @@ -462,7 +463,7 @@ | ||
| 462 | 463 | DefaultResultSetFuture future = new DefaultResultSetFuture(null, cluster.protocolVersion(), new Requests.Query(SELECT_PEERS)); |
| 463 | 464 | c.write(future); |
| 464 | 465 | for (Row row : future.get()) { |
| 465 | - InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster, true); | |
| 466 | + InetSocketAddress addr = addressToUseForPeerHost(row, c.address, cluster); | |
| 466 | 467 | if (addr != null && addr.equals(host.getSocketAddress())) |
| 467 | 468 | return row; |
| 468 | 469 | } |
| @@ -492,11 +493,13 @@ | ||
| 492 | 493 | logger.warn("No row found for host {} in {}'s peers system table. {} will be ignored.", host.getAddress(), c.address, host.getAddress()); |
| 493 | 494 | return false; |
| 494 | 495 | } |
| 495 | - // Ignore hosts with a null rpc_address, as this is most likely a phantom row in system.peers (JAVA-428). | |
| 496 | - // Don't test this for the control host since we're already connected to it anyway, and we read the info from system.local | |
| 497 | - // which doesn't have an rpc_address column (JAVA-546). | |
| 498 | - } else if (!c.address.equals(host.getSocketAddress()) && row.getInet("rpc_address") == null) { | |
| 499 | - logger.warn("No rpc_address found for host {} in {}'s peers system table. {} will be ignored.", host.getAddress(), c.address, host.getAddress()); | |
| 496 | + } | |
| 497 | + | |
| 498 | + // Ignore rows with invalid values, as this is most likely a phantom row in system.peers (JAVA-428, | |
| 499 | + // JAVA-852). | |
| 500 | + // Skip the control host since we're already connected to it anyway, and we read the info from system.local, | |
| 501 | + // which doesn't have an rpc_address column (JAVA-546). | |
| 502 | + if (!c.address.equals(host.getSocketAddress()) && !isValidPeer(row, true)) { | |
| 500 | 503 | return false; |
| 501 | 504 | } |
| 502 | 505 | |
| @@ -554,7 +557,7 @@ | ||
| 554 | 557 | cluster.loadBalancingPolicy().onAdd(host); |
| 555 | 558 | } |
| 556 | 559 | |
| 557 | - private static void refreshNodeListAndTokenMap(Connection connection, Cluster.Manager cluster, boolean isInitialConnection, boolean logMissingRpcAddresses) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { | |
| 560 | + private static void refreshNodeListAndTokenMap(Connection connection, Cluster.Manager cluster, boolean isInitialConnection, boolean logInvalidPeers) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { | |
| 558 | 561 | logger.debug("[Control connection] Refreshing node list and token map"); |
| 559 | 562 | |
| 560 | 563 | boolean metadataEnabled = cluster.configuration.getQueryOptions().isMetadataEnabled(); |
| @@ -603,10 +606,10 @@ | ||
| 603 | 606 | List<Set<String>> allTokens = new ArrayList<Set<String>>(); |
| 604 | 607 | |
| 605 | 608 | for (Row row : peersFuture.get()) { |
| 606 | - InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, logMissingRpcAddresses); | |
| 607 | - if (addr == null) | |
| 609 | + if (!isValidPeer(row, logInvalidPeers)) | |
| 608 | 610 | continue; |
| 609 | 611 | |
| 612 | + InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster); | |
| 610 | 613 | foundHosts.add(addr); |
| 611 | 614 | dcs.add(row.getString("data_center")); |
| 612 | 615 | racks.add(row.getString("rack")); |
| @@ -654,6 +657,46 @@ | ||
| 654 | 657 | cluster.metadata.rebuildTokenMap(partitioner, tokenMap); |
| 655 | 658 | } |
| 656 | 659 | |
| 660 | + private static boolean isValidPeer(Row peerRow, boolean logIfInvalid) { | |
| 661 | + boolean isValid = peerRow.getColumnDefinitions().contains("rpc_address") | |
| 662 | + && !peerRow.isNull("rpc_address"); | |
| 663 | + if (EXTENDED_PEER_CHECK) { | |
| 664 | + isValid &= peerRow.getColumnDefinitions().contains("host_id") | |
| 665 | + && !peerRow.isNull("host_id") | |
| 666 | + && peerRow.getColumnDefinitions().contains("data_center") | |
| 667 | + && !peerRow.isNull("data_center") | |
| 668 | + && peerRow.getColumnDefinitions().contains("rack") | |
| 669 | + && !peerRow.isNull("rack") | |
| 670 | + && peerRow.getColumnDefinitions().contains("tokens") | |
| 671 | + && !peerRow.isNull("tokens"); | |
| 672 | + } | |
| 673 | + if (!isValid && logIfInvalid) | |
| 674 | + logger.warn("Found invalid row in system.peers: {}. " + | |
| 675 | + "This is likely a gossip or snitch issue, this host will be ignored.", formatInvalidPeer(peerRow)); | |
| 676 | + return isValid; | |
| 677 | + } | |
| 678 | + | |
| 679 | + // Custom formatting to avoid spamming the logs if 'tokens' is present and contains a gazillion tokens | |
| 680 | + private static String formatInvalidPeer(Row peerRow) { | |
| 681 | + StringBuilder sb = new StringBuilder("[peer=" + peerRow.getInet("peer")); | |
| 682 | + formatMissingOrNullColumn(peerRow, "rpc_address", sb); | |
| 683 | + if (EXTENDED_PEER_CHECK) { | |
| 684 | + formatMissingOrNullColumn(peerRow, "host_id", sb); | |
| 685 | + formatMissingOrNullColumn(peerRow, "data_center", sb); | |
| 686 | + formatMissingOrNullColumn(peerRow, "rack", sb); | |
| 687 | + formatMissingOrNullColumn(peerRow, "tokens", sb); | |
| 688 | + } | |
| 689 | + sb.append("]"); | |
| 690 | + return sb.toString(); | |
| 691 | + } | |
| 692 | + | |
| 693 | + private static void formatMissingOrNullColumn(Row peerRow, String columnName, StringBuilder sb) { | |
| 694 | + if (!peerRow.getColumnDefinitions().contains(columnName)) | |
| 695 | + sb.append(", missing ").append(columnName); | |
| 696 | + else if (peerRow.isNull(columnName)) | |
| 697 | + sb.append(", ").append(columnName).append("=null"); | |
| 698 | + } | |
| 699 | + | |
| 657 | 700 | boolean waitForSchemaAgreement() throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException { |
| 658 | 701 | long start = System.nanoTime(); |
| 659 | 702 | long elapsed = 0; |
| @@ -690,7 +733,10 @@ | ||
| 690 | 733 | |
| 691 | 734 | for (Row row : peersFuture.get()) { |
| 692 | 735 | |
| 693 | - InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster, true); | |
| 736 | + if (!isValidPeer(row, false)) | |
| 737 | + continue; | |
| 738 | + | |
| 739 | + InetSocketAddress addr = addressToUseForPeerHost(row, connection.address, cluster); | |
| 694 | 740 | if (addr == null || row.isNull("schema_version")) |
| 695 | 741 | continue; |
| 696 | 742 |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +3 −2
| @@ -35,6 +35,7 @@ | ||
| 35 | 35 | import java.util.Collections; |
| 36 | 36 | import java.util.List; |
| 37 | 37 | import java.util.Map; |
| 38 | +import java.util.UUID; | |
| 38 | 39 | import java.util.concurrent.TimeUnit; |
| 39 | 40 | import java.util.concurrent.TimeoutException; |
| 40 | 41 | |
| @@ -140,8 +141,7 @@ | ||
| 140 | 141 | } |
| 141 | 142 | assertThat(cluster).host(failingHost.address).isReconnectingFromDown(); |
| 142 | 143 | } |
| 143 | - assertThat(cluster).host(missingHostAddress).isNull(); | |
| 144 | - | |
| 144 | + assertThat(TestUtils.findHost(cluster, missingHostAddress)).isNull(); | |
| 145 | 145 | } finally { |
| 146 | 146 | if (cluster != null) |
| 147 | 147 | cluster.close(); |
| @@ -275,6 +275,7 @@ | ||
| 275 | 275 | .put("rack", "rack1") |
| 276 | 276 | .put("release_version", "2.0.1") |
| 277 | 277 | .put("tokens", ImmutableSet.of(Long.toString(Long.MIN_VALUE + i++))) |
| 278 | + .put("host_id", UUID.randomUUID()) | |
| 278 | 279 | .build()); |
| 279 | 280 | } |
| 280 | 281 |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +124 −0
| @@ -25,9 +25,11 @@ | ||
| 25 | 25 | import com.google.common.collect.ImmutableMap; |
| 26 | 26 | import com.google.common.collect.ImmutableSet; |
| 27 | 27 | import com.google.common.collect.Maps; |
| 28 | +import org.apache.log4j.Level; | |
| 28 | 29 | import org.scassandra.http.client.PrimingRequest; |
| 29 | 30 | import org.slf4j.Logger; |
| 30 | 31 | import org.slf4j.LoggerFactory; |
| 32 | +import org.testng.annotations.DataProvider; | |
| 31 | 33 | import org.testng.annotations.Test; |
| 32 | 34 | |
| 33 | 35 | import java.net.InetAddress; |
| @@ -36,6 +38,7 @@ | ||
| 36 | 38 | import java.util.Collection; |
| 37 | 39 | import java.util.Iterator; |
| 38 | 40 | import java.util.Map; |
| 41 | +import java.util.UUID; | |
| 39 | 42 | import java.util.concurrent.TimeUnit; |
| 40 | 43 | import java.util.concurrent.atomic.AtomicInteger; |
| 41 | 44 | |
| @@ -219,7 +222,17 @@ | ||
| 219 | 222 | } finally { |
| 220 | 223 | scassandras.stop(); |
| 221 | 224 | } |
| 225 | + } | |
| 222 | 226 | |
| 227 | + @DataProvider | |
| 228 | + public Object[][] disallowedNullColumnsInPeerData() { | |
| 229 | + return new Object[][]{ | |
| 230 | + {"host_id"}, | |
| 231 | + {"data_center"}, | |
| 232 | + {"rack"}, | |
| 233 | + {"tokens"}, | |
| 234 | + {"host_id,data_center,rack,tokens"} | |
| 235 | + }; | |
| 223 | 236 | } |
| 224 | 237 | |
| 225 | 238 | /** |
| @@ -270,6 +283,7 @@ | ||
| 270 | 283 | .put("data_center", datacenter(1)) |
| 271 | 284 | .put("rack", "rack1") |
| 272 | 285 | .put("release_version", "2.1.8") |
| 286 | + .put("host_id", UUID.randomUUID()) | |
| 273 | 287 | .put("tokens", ImmutableSet.of(Long.toString(scassandras.getTokensForDC(1).get(1)))) |
| 274 | 288 | .build(); |
| 275 | 289 | |
| @@ -309,7 +323,117 @@ | ||
| 309 | 323 | cluster.close(); |
| 310 | 324 | scassandras.stop(); |
| 311 | 325 | } |
| 326 | + } | |
| 327 | + | |
| 328 | + /** | |
| 329 | + * Validates that if the com.datastax.driver.EXTENDED_PEER_CHECK system property is set to false that a peer | |
| 330 | + * with null values for host_id, data_center, rack, tokens is not ignored. | |
| 331 | + * | |
| 332 | + * @test_category host:metadata | |
| 333 | + * @jira_ticket JAVA-852 | |
| 334 | + * @since 2.1.10 | |
| 335 | + */ | |
| 336 | + @Test(groups = "isolated", dataProvider = "disallowedNullColumnsInPeerData") | |
| 337 | + @CCMConfig(createCcm = false) | |
| 338 | + public void should_use_peer_if_extended_peer_check_is_disabled(String columns) { | |
| 339 | + System.setProperty("com.datastax.driver.EXTENDED_PEER_CHECK", "false"); | |
| 340 | + run_with_null_peer_info(columns, true); | |
| 341 | + } | |
| 342 | + | |
| 343 | + /** | |
| 344 | + * Validates that if the com.datastax.driver.EXTENDED_PEER_CHECK system property is set to true that a peer | |
| 345 | + * with null values for host_id, data_center, rack, or tokens is ignored. | |
| 346 | + * | |
| 347 | + * @test_category host:metadata | |
| 348 | + * @jira_ticket JAVA-852 | |
| 349 | + * @since 2.1.10 | |
| 350 | + */ | |
| 351 | + @Test(groups = "isolated", dataProvider = "disallowedNullColumnsInPeerData") | |
| 352 | + @CCMConfig(createCcm = false) | |
| 353 | + public void should_ignore_peer_if_extended_peer_check_is_enabled(String columns) { | |
| 354 | + System.setProperty("com.datastax.driver.EXTENDED_PEER_CHECK", "true"); | |
| 355 | + run_with_null_peer_info(columns, false); | |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * Validates that a peer with null values for host_id, data_center, rack, or tokens is ignored. | |
| 360 | + * | |
| 361 | + * @test_category host:metadata | |
| 362 | + * @jira_ticket JAVA-852 | |
| 363 | + * @since 2.1.10 | |
| 364 | + */ | |
| 365 | + @Test(groups = "short", dataProvider = "disallowedNullColumnsInPeerData") | |
| 366 | + @CCMConfig(createCcm = false) | |
| 367 | + public void should_ignore_and_warn_peers_with_null_entries_by_default(String columns) { | |
| 368 | + run_with_null_peer_info(columns, false); | |
| 369 | + } | |
| 370 | + | |
| 371 | + private void run_with_null_peer_info(String columns, boolean expectPeer2) { | |
| 372 | + // given: A cluster with peer 2 having a null rack. | |
| 373 | + ScassandraCluster.ScassandraClusterBuilder builder = ScassandraCluster.builder() | |
| 374 | + .withNodes(3); | |
| 375 | + | |
| 376 | + StringBuilder columnDataBuilder = new StringBuilder(); | |
| 377 | + for (String column : columns.split(",")) { | |
| 378 | + builder = builder.forcePeerInfo(1, 2, column, null); | |
| 379 | + columnDataBuilder.append(String.format("%s=null, ", column)); | |
| 380 | + } | |
| 381 | + | |
| 382 | + String columnData = columnDataBuilder.toString(); | |
| 383 | + if (columnData.endsWith(", ")) { | |
| 384 | + columnData = columnData.substring(0, columnData.length() - 2); | |
| 385 | + } | |
| 386 | + | |
| 387 | + ScassandraCluster scassandraCluster = builder.build(); | |
| 388 | + | |
| 389 | + Cluster cluster = Cluster.builder() | |
| 390 | + .addContactPoints(scassandraCluster.address(1).getAddress()) | |
| 391 | + .withPort(scassandraCluster.getBinaryPort()) | |
| 392 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 393 | + .build(); | |
| 394 | + | |
| 395 | + // Capture logs to ensure appropriate warnings are logged. | |
| 396 | + org.apache.log4j.Logger cLogger = org.apache.log4j.Logger.getLogger("com.datastax.driver.core"); | |
| 397 | + Level originalLevel = cLogger.getLevel(); | |
| 398 | + if (originalLevel != null && !originalLevel.isGreaterOrEqual(Level.WARN)) { | |
| 399 | + cLogger.setLevel(Level.WARN); | |
| 400 | + } | |
| 401 | + MemoryAppender logs = new MemoryAppender(); | |
| 402 | + cLogger.addAppender(logs); | |
| 403 | + | |
| 404 | + try { | |
| 405 | + scassandraCluster.init(); | |
| 312 | 406 | |
| 407 | + // when: Initializing a cluster instance and grabbing metadata. | |
| 408 | + cluster.init(); | |
| 409 | + | |
| 410 | + InetAddress node2Address = scassandraCluster.address(2).getAddress(); | |
| 411 | + String expectedError = String.format("Found invalid row in system.peers: [peer=%s, %s]. " + | |
| 412 | + "This is likely a gossip or snitch issue, this host will be ignored.", node2Address, columnData); | |
| 413 | + String log = logs.get(); | |
| 414 | + // then: A peer with a null rack should not show up in host metadata, unless allowed via system property. | |
| 415 | + if (expectPeer2) { | |
| 416 | + assertThat(cluster.getMetadata().getAllHosts()) | |
| 417 | + .hasSize(3) | |
| 418 | + .extractingResultOf("getAddress") | |
| 419 | + .contains(node2Address); | |
| 420 | + | |
| 421 | + assertThat(log).doesNotContain(expectedError); | |
| 422 | + } else { | |
| 423 | + assertThat(cluster.getMetadata().getAllHosts()) | |
| 424 | + .hasSize(2) | |
| 425 | + .extractingResultOf("getAddress") | |
| 426 | + .doesNotContain(node2Address); | |
| 427 | + | |
| 428 | + assertThat(log) | |
| 429 | + .containsOnlyOnce(expectedError); | |
| 430 | + } | |
| 431 | + } finally { | |
| 432 | + cLogger.removeAppender(logs); | |
| 433 | + cLogger.setLevel(originalLevel); | |
| 434 | + cluster.close(); | |
| 435 | + scassandraCluster.stop(); | |
| 436 | + } | |
| 313 | 437 | } |
| 314 | 438 | |
| 315 | 439 | static class QueryPlanCountingPolicy extends DelegatingLoadBalancingPolicy { |
modified driver-core/src/test/java/com/datastax/driver/core/ScassandraCluster.java +34 −28
| @@ -15,11 +15,8 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.beust.jcommander.internal.Lists; | |
| 19 | -import com.beust.jcommander.internal.Maps; | |
| 20 | -import com.google.common.collect.ImmutableList; | |
| 21 | -import com.google.common.collect.ImmutableMap; | |
| 22 | -import com.google.common.collect.ImmutableSet; | |
| 18 | +import com.datastax.driver.core.utils.UUIDs; | |
| 19 | +import com.google.common.collect.*; | |
| 23 | 20 | import org.scassandra.Scassandra; |
| 24 | 21 | import org.scassandra.ScassandraFactory; |
| 25 | 22 | import org.scassandra.http.client.PrimingClient; |
| @@ -263,7 +260,7 @@ | ||
| 263 | 260 | // Offset DCs by dc * 100 to ensure unique tokens. |
| 264 | 261 | int offset = (dc - 1) * 100; |
| 265 | 262 | int dcNodeCount = nodes(dc).size(); |
| 266 | - List<Long> tokens = Lists.newArrayList(dcNodeCount); | |
| 263 | + List<Long> tokens = Lists.newArrayListWithExpectedSize(dcNodeCount); | |
| 267 | 264 | for (int i = 0; i < dcNodeCount; i++) { |
| 268 | 265 | tokens.add((i * ((long) Math.pow(2, 64) / dcNodeCount) + offset)); |
| 269 | 266 | } |
| @@ -287,30 +284,30 @@ | ||
| 287 | 284 | if (node == peer) { // prime system.local. |
| 288 | 285 | metadata = SELECT_LOCAL; |
| 289 | 286 | query = "SELECT * FROM system.local WHERE key='local'"; |
| 290 | - row = ImmutableMap.<String, Object>builder() | |
| 291 | - .put("key", "local") | |
| 292 | - .put("bootstrapped", "COMPLETED") | |
| 293 | - .put("broadcast_address", address) | |
| 294 | - .put("cluster_name", "scassandra") | |
| 295 | - .put("cql_version", "3.2.0") | |
| 296 | - .put("data_center", datacenter(dc)) | |
| 297 | - .put("listen_address", address) | |
| 298 | - .put("partitioner", "org.apache.cassandra.dht.Murmur3Partitioner") | |
| 299 | - .put("rack", "rack1") | |
| 300 | - .put("release_version", getPeerInfo(dc, n + 1, "release_version", "2.1.8")) | |
| 301 | - .put("tokens", ImmutableSet.of(tokens.get(n))) | |
| 302 | - .build(); | |
| 287 | + | |
| 288 | + row = Maps.newHashMap(); | |
| 289 | + addPeerInfo(row, dc, n + 1, "key", "local"); | |
| 290 | + addPeerInfo(row, dc, n + 1, "bootstrapped", "COMPLETED"); | |
| 291 | + addPeerInfo(row, dc, n + 1, "broadcast_address", address); | |
| 292 | + addPeerInfo(row, dc, n + 1, "cluster_name", "scassandra"); | |
| 293 | + addPeerInfo(row, dc, n + 1, "cql_version", "3.2.0"); | |
| 294 | + addPeerInfo(row, dc, n + 1, "data_center", datacenter(dc)); | |
| 295 | + addPeerInfo(row, dc, n + 1, "listen_address", address); | |
| 296 | + addPeerInfo(row, dc, n + 1, "partitioner", "org.apache.cassandra.dht.Murmur3Partitioner"); | |
| 297 | + addPeerInfo(row, dc, n + 1, "rack", getPeerInfo(dc, n + 1, "rack", "rack1")); | |
| 298 | + addPeerInfo(row, dc, n + 1, "release_version", getPeerInfo(dc, n + 1, "release_version", "2.1.8")); | |
| 299 | + addPeerInfo(row, dc, n + 1, "tokens", ImmutableSet.of(tokens.get(n))); | |
| 303 | 300 | } else { // prime system.peers. |
| 304 | 301 | query = "SELECT * FROM system.peers WHERE peer='" + address + "'"; |
| 305 | 302 | metadata = SELECT_PEERS; |
| 306 | - row = ImmutableMap.<String, Object>builder() | |
| 307 | - .put("peer", address) | |
| 308 | - .put("rpc_address", address) | |
| 309 | - .put("data_center", datacenter(dc)) | |
| 310 | - .put("rack", "rack1") | |
| 311 | - .put("release_version", getPeerInfo(dc, n + 1, "release_version", "2.1.8")) | |
| 312 | - .put("tokens", ImmutableSet.of(Long.toString(tokens.get(n)))) | |
| 313 | - .build(); | |
| 303 | + row = Maps.newHashMap(); | |
| 304 | + addPeerInfo(row, dc, n + 1, "peer", address); | |
| 305 | + addPeerInfo(row, dc, n + 1, "rpc_address", address); | |
| 306 | + addPeerInfo(row, dc, n + 1, "data_center", datacenter(dc)); | |
| 307 | + addPeerInfo(row, dc, n + 1, "rack", getPeerInfo(dc, n + 1, "rack", "rack1")); | |
| 308 | + addPeerInfo(row, dc, n + 1, "release_version", getPeerInfo(dc, n + 1, "release_version", "2.1.8")); | |
| 309 | + addPeerInfo(row, dc, n + 1, "tokens", ImmutableSet.of(Long.toString(tokens.get(n)))); | |
| 310 | + addPeerInfo(row, dc, n + 1, "host_id", UUIDs.random()); | |
| 314 | 311 | rows.add(row); |
| 315 | 312 | } |
| 316 | 313 | client.prime(PrimingRequest.queryBuilder() |
| @@ -353,6 +350,14 @@ | ||
| 353 | 350 | .build()); |
| 354 | 351 | } |
| 355 | 352 | |
| 353 | + private Map<String, Object> addPeerInfo(Map<String, Object> input, int dc, int node, String property, Object defaultValue) { | |
| 354 | + Object peerInfo = getPeerInfo(dc, node, property, defaultValue); | |
| 355 | + if(peerInfo != null) { | |
| 356 | + input.put(property, peerInfo); | |
| 357 | + } | |
| 358 | + return input; | |
| 359 | + } | |
| 360 | + | |
| 356 | 361 | private Object getPeerInfo(int dc, int node, String property, Object defaultValue) { |
| 357 | 362 | Map<Integer, Map<String, Object>> forDc = forcedPeerInfos.get(dc); |
| 358 | 363 | if (forDc == null) |
| @@ -373,7 +378,8 @@ | ||
| 373 | 378 | column("data_center", TEXT), |
| 374 | 379 | column("rack", TEXT), |
| 375 | 380 | column("release_version", TEXT), |
| 376 | - column("tokens", set(TEXT)) | |
| 381 | + column("tokens", set(TEXT)), | |
| 382 | + column("host_id", UUID) | |
| 377 | 383 | }; |
| 378 | 384 | |
| 379 | 385 | public static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = { |