kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 72031067a18f08fd7a2ce71120374e5d5b691d13a738cd4e9fb566d6a4dbbb1d |
|---|---|
| Author | Dan Jatnieks <danj@datastax.com> · 2016-01-26 19:21 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-01-28 11:30 +01:00 |
| Parents | a6b44c4c2439 |
7 files changed
modified changelog/README.md +1 −0
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | - [improvement] JAVA-1035: Expose host broadcast_address and listen_address if available. |
| 7 | 7 | - [new feature] JAVA-1037: Allow named parameters in simple statements. |
| 8 | 8 | - [improvement] JAVA-1033: Allow per-statement read timeout. |
| 9 | +- [improvement] JAVA-1042: Include DSE version and workload in Host data. | |
| 9 | 10 | |
| 10 | 11 | Merged from 2.1 branch: |
| 11 | 12 |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +24 −0
| @@ -503,6 +503,14 @@ | ||
| 503 | 503 | : null; |
| 504 | 504 | host.setListenAddress(listenAddress); |
| 505 | 505 | |
| 506 | + if (row.getColumnDefinitions().contains("workload")) { | |
| 507 | + String dseWorkload = row.getString("workload"); | |
| 508 | + host.setDseWorkload(dseWorkload); | |
| 509 | + } | |
| 510 | + if (row.getColumnDefinitions().contains("dse_version")) { | |
| 511 | + String dseVersion = row.getString("dse_version"); | |
| 512 | + host.setDseVersion(dseVersion); | |
| 513 | + } | |
| 506 | 514 | } |
| 507 | 515 | |
| 508 | 516 | private static void updateLocationInfo(Host host, String datacenter, String rack, boolean isInitialConnection, Cluster.Manager cluster) { |
| @@ -565,7 +573,10 @@ | ||
| 565 | 573 | List<String> racks = new ArrayList<String>(); |
| 566 | 574 | List<String> cassandraVersions = new ArrayList<String>(); |
| 567 | 575 | List<InetAddress> broadcastAddresses = new ArrayList<InetAddress>(); |
| 576 | + List<InetAddress> listenAddresses = new ArrayList<InetAddress>(); | |
| 568 | 577 | List<Set<String>> allTokens = new ArrayList<Set<String>>(); |
| 578 | + List<String> dseVersions = new ArrayList<String>(); | |
| 579 | + List<String> dseWorkloads = new ArrayList<String>(); | |
| 569 | 580 | |
| 570 | 581 | for (Row row : peersFuture.get()) { |
| 571 | 582 | InetSocketAddress rpcAddress = rpcAddressForPeerHost(row, connection.address, cluster, logMissingRpcAddresses); |
| @@ -579,6 +590,12 @@ | ||
| 579 | 590 | broadcastAddresses.add(row.getInet("peer")); |
| 580 | 591 | if (metadataEnabled) |
| 581 | 592 | allTokens.add(row.getSet("tokens", String.class)); |
| 593 | + InetAddress listenAddress = row.getColumnDefinitions().contains("listen_address") ? row.getInet("listen_address") : null; | |
| 594 | + listenAddresses.add(listenAddress); | |
| 595 | + String dseWorkload = row.getColumnDefinitions().contains("workload") ? row.getString("workload") : null; | |
| 596 | + dseWorkloads.add(dseWorkload); | |
| 597 | + String dseVersion = row.getColumnDefinitions().contains("dse_version") ? row.getString("dse_version") : null; | |
| 598 | + dseVersions.add(dseVersion); | |
| 582 | 599 | } |
| 583 | 600 | |
| 584 | 601 | for (int i = 0; i < foundHosts.size(); i++) { |
| @@ -603,6 +620,13 @@ | ||
| 603 | 620 | host.setVersion(cassandraVersions.get(i)); |
| 604 | 621 | if (broadcastAddresses.get(i) != null) |
| 605 | 622 | host.setBroadcastAddress(broadcastAddresses.get(i)); |
| 623 | + if (listenAddresses.get(i) != null) | |
| 624 | + host.setListenAddress(listenAddresses.get(i)); | |
| 625 | + | |
| 626 | + if (dseVersions.get(i) != null) | |
| 627 | + host.setDseVersion(dseVersions.get(i)); | |
| 628 | + if (dseWorkloads.get(i) != null) | |
| 629 | + host.setDseWorkload(dseWorkloads.get(i)); | |
| 606 | 630 | |
| 607 | 631 | if (metadataEnabled && partitioner != null && !allTokens.get(i).isEmpty()) |
| 608 | 632 | tokenMap.put(host, allTokens.get(i)); |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +54 −8
| @@ -74,6 +74,9 @@ | ||
| 74 | 74 | |
| 75 | 75 | private volatile Set<Token> tokens; |
| 76 | 76 | |
| 77 | + private volatile String dseWorkload; | |
| 78 | + private volatile VersionNumber dseVersion; | |
| 79 | + | |
| 77 | 80 | // ClusterMetadata keeps one Host object per inet address and we rely on this (more precisely, |
| 78 | 81 | // we rely on the fact that we can use Object equality as a valid equality), so don't use |
| 79 | 82 | // that constructor but ClusterMetadata.getHost instead. |
| @@ -113,6 +116,22 @@ | ||
| 113 | 116 | this.listenAddress = listenAddress; |
| 114 | 117 | } |
| 115 | 118 | |
| 119 | + void setDseVersion(String dseVersion) { | |
| 120 | + VersionNumber versionNumber = null; | |
| 121 | + try { | |
| 122 | + if (dseVersion != null) { | |
| 123 | + versionNumber = VersionNumber.parse(dseVersion); | |
| 124 | + } | |
| 125 | + } catch (IllegalArgumentException e) { | |
| 126 | + logger.warn("Error parsing DSE version {}. This shouldn't have happened", dseVersion); | |
| 127 | + } | |
| 128 | + this.dseVersion = versionNumber; | |
| 129 | + } | |
| 130 | + | |
| 131 | + void setDseWorkload(String dseWorkload) { | |
| 132 | + this.dseWorkload = dseWorkload; | |
| 133 | + } | |
| 134 | + | |
| 116 | 135 | /** |
| 117 | 136 | * Returns the address that the driver will use to connect to the node. |
| 118 | 137 | * <p/> |
| @@ -138,14 +157,14 @@ | ||
| 138 | 157 | * </ol> |
| 139 | 158 | * |
| 140 | 159 | * @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> | |
| 160 | + * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html">The cassandra.yaml configuration file</a> | |
| 142 | 161 | */ |
| 143 | 162 | public InetSocketAddress getSocketAddress() { |
| 144 | 163 | return address; |
| 145 | 164 | } |
| 146 | 165 | |
| 147 | 166 | /** |
| 148 | - * Returns the node broadcast address, i.e. the IP by which it should be contacted by other peers in the cluster. | |
| 167 | + * Returns the node broadcast address, that is, the IP by which it should be contacted by other peers in the cluster. | |
| 149 | 168 | * <p/> |
| 150 | 169 | * This corresponds to the {@code broadcast_address} cassandra.yaml file setting and |
| 151 | 170 | * is by default the same as {@link #getListenAddress()}, unless specified |
| @@ -157,14 +176,14 @@ | ||
| 157 | 176 | * case this method will return {@code null}. |
| 158 | 177 | * |
| 159 | 178 | * @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> | |
| 179 | + * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html">The cassandra.yaml configuration file</a> | |
| 161 | 180 | */ |
| 162 | 181 | public InetAddress getBroadcastAddress() { |
| 163 | 182 | return broadcastAddress; |
| 164 | 183 | } |
| 165 | 184 | |
| 166 | 185 | /** |
| 167 | - * Returns the node listen address, i.e. the IP the node uses to contact other peers in the cluster. | |
| 186 | + * Returns the node listen address, that is, the IP the node uses to contact other peers in the cluster. | |
| 168 | 187 | * <p/> |
| 169 | 188 | * This corresponds to the {@code listen_address} cassandra.yaml file setting. |
| 170 | 189 | * <em>This is NOT the address clients should use to contact this node</em>. |
| @@ -174,7 +193,7 @@ | ||
| 174 | 193 | * case this method will return {@code null}. |
| 175 | 194 | * |
| 176 | 195 | * @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> | |
| 196 | + * @see <a href="https://docs.datastax.com/en/cassandra/2.1/cassandra/configuration/configCassandra_yaml_r.html">The cassandra.yaml configuration file</a> | |
| 178 | 197 | */ |
| 179 | 198 | public InetAddress getListenAddress() { |
| 180 | 199 | return listenAddress; |
| @@ -199,7 +218,7 @@ | ||
| 199 | 218 | * <p/> |
| 200 | 219 | * The returned rack name is the one as known by Cassandra. |
| 201 | 220 | * It is also possible for this information to be unavailable. In that case |
| 202 | - * this method returns {@code null}, and the caller should always aware of this | |
| 221 | + * this method returns {@code null}, and the caller should always be aware of this | |
| 203 | 222 | * possibility. |
| 204 | 223 | * |
| 205 | 224 | * @return the Cassandra rack name or null if the rack is unavailable |
| @@ -211,8 +230,9 @@ | ||
| 211 | 230 | /** |
| 212 | 231 | * The Cassandra version the host is running. |
| 213 | 232 | * <p/> |
| 214 | - * As for other host information fetch from Cassandra above, the returned | |
| 215 | - * version can theoretically be null if the information is unavailable. | |
| 233 | + * It is also possible for this information to be unavailable. In that case | |
| 234 | + * this method returns {@code null}, and the caller should always be aware of this | |
| 235 | + * possibility. | |
| 216 | 236 | * |
| 217 | 237 | * @return the Cassandra version the host is running. |
| 218 | 238 | */ |
| @@ -220,6 +240,32 @@ | ||
| 220 | 240 | return cassandraVersion; |
| 221 | 241 | } |
| 222 | 242 | |
| 243 | + /** | |
| 244 | + * The DSE version the host is running. | |
| 245 | + * <p/> | |
| 246 | + * It is also possible for this information to be unavailable. In that case | |
| 247 | + * this method returns {@code null}, and the caller should always be aware of this | |
| 248 | + * possibility. | |
| 249 | + * | |
| 250 | + * @return the DSE version the host is running. | |
| 251 | + */ | |
| 252 | + public VersionNumber getDseVersion() { | |
| 253 | + return dseVersion; | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * The DSE Workload the host is running. | |
| 258 | + * <p/> | |
| 259 | + * It is also possible for this information to be unavailable. In that case | |
| 260 | + * this method returns {@code null}, and the caller should always be aware of this | |
| 261 | + * possibility. | |
| 262 | + * | |
| 263 | + * @return the DSE workload the host is running. | |
| 264 | + */ | |
| 265 | + public String getDseWorkload() { | |
| 266 | + return dseWorkload; | |
| 267 | + } | |
| 268 | + | |
| 223 | 269 | /** |
| 224 | 270 | * Returns the tokens that this host owns. |
| 225 | 271 | * |
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +41 −0
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 21 | 21 | import org.assertj.core.api.AbstractAssert; |
| 22 | 22 | |
| 23 | +import java.net.InetAddress; | |
| 23 | 24 | import java.util.concurrent.CountDownLatch; |
| 24 | 25 | import java.util.concurrent.TimeUnit; |
| 25 | 26 | |
| @@ -128,4 +129,44 @@ | ||
| 128 | 129 | fail(actual + " did not go down within " + duration + " " + unit); |
| 129 | 130 | return this; |
| 130 | 131 | } |
| 132 | + | |
| 133 | + public HostAssert hasWorkload(String workload) { | |
| 134 | + assertThat(actual.getDseWorkload()).isNotNull().isEqualTo(workload); | |
| 135 | + return this; | |
| 136 | + } | |
| 137 | + | |
| 138 | + public HostAssert hasNoWorkload() { | |
| 139 | + assertThat(actual.getDseWorkload()).isNull(); | |
| 140 | + return this; | |
| 141 | + } | |
| 142 | + | |
| 143 | + public HostAssert hasDseVersion(VersionNumber versionNumber) { | |
| 144 | + assertThat(actual.getDseVersion()).isNotNull().isEqualTo(versionNumber); | |
| 145 | + return this; | |
| 146 | + } | |
| 147 | + | |
| 148 | + public HostAssert hasNoDseVersion() { | |
| 149 | + assertThat(actual.getDseVersion()).isNull(); | |
| 150 | + return this; | |
| 151 | + } | |
| 152 | + | |
| 153 | + public HostAssert hasListenAddress(InetAddress address) { | |
| 154 | + assertThat(actual.getListenAddress()).isNotNull().isEqualTo(address); | |
| 155 | + return this; | |
| 156 | + } | |
| 157 | + | |
| 158 | + public HostAssert hasNoListenAddress() { | |
| 159 | + assertThat(actual.getListenAddress()).isNull(); | |
| 160 | + return this; | |
| 161 | + } | |
| 162 | + | |
| 163 | + public HostAssert hasBroadcastAddress(InetAddress address) { | |
| 164 | + assertThat(actual.getBroadcastAddress()).isNotNull().isEqualTo(address); | |
| 165 | + return this; | |
| 166 | + } | |
| 167 | + | |
| 168 | + public HostAssert hasNoBroadcastAddress() { | |
| 169 | + assertThat(actual.getBroadcastAddress()).isNull(); | |
| 170 | + return this; | |
| 171 | + } | |
| 131 | 172 | } |
added driver-core/src/test/java/com/datastax/driver/core/HostMetadataIntegrationTest.java +288 −0
| @@ -0,0 +1,288 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import org.apache.log4j.Level; | |
| 19 | +import org.apache.log4j.Logger; | |
| 20 | +import org.testng.annotations.Test; | |
| 21 | + | |
| 22 | +import java.net.InetAddress; | |
| 23 | + | |
| 24 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 25 | +import static com.datastax.driver.core.CCMAccess.Workload.solr; | |
| 26 | +import static com.datastax.driver.core.CCMAccess.Workload.spark; | |
| 27 | +import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; | |
| 28 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 29 | + | |
| 30 | +public class HostMetadataIntegrationTest { | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Validates that when given a DSE cluster that the workload and dse versions are properly | |
| 34 | + * parsed and set on the respective {@link Host} instances. | |
| 35 | + * <p/> | |
| 36 | + * This test is disabled as running DSE with workloads is intensive and has unreliable results | |
| 37 | + * when running CCM with multiple workloads. | |
| 38 | + * <p/> | |
| 39 | + * FIXME: this test is currently disabled because nodes with specific workload | |
| 40 | + * do not seem to come up within the allocated time. | |
| 41 | + * | |
| 42 | + * @test_category host:metadata | |
| 43 | + * @jira_ticket JAVA1042 | |
| 44 | + * @see HostMetadataIntegrationTest#should_parse_dse_workload_and_version_if_available() | |
| 45 | + */ | |
| 46 | + @Test(groups = "long", enabled = false) | |
| 47 | + public void test_mixed_dse_workload() { | |
| 48 | + CCMBridge.Builder builder = CCMBridge.builder() | |
| 49 | + .withNodes(3) | |
| 50 | + .withDSE() | |
| 51 | + .withVersion("4.8.3") | |
| 52 | + .withWorkload(2, solr) | |
| 53 | + .withWorkload(3, spark); | |
| 54 | + CCMAccess ccm = CCMCache.get(builder); | |
| 55 | + | |
| 56 | + VersionNumber version = VersionNumber.parse("4.8.3"); | |
| 57 | + | |
| 58 | + Cluster cluster = Cluster.builder() | |
| 59 | + .addContactPoints(ccm.addressOfNode(1).getAddress()) | |
| 60 | + .withPort(ccm.getBinaryPort()) | |
| 61 | + .withQueryOptions(nonDebouncingQueryOptions()) | |
| 62 | + .build(); | |
| 63 | + try { | |
| 64 | + cluster.connect(); | |
| 65 | + | |
| 66 | + assertThat(cluster).host(1).hasWorkload("Cassandra").hasDseVersion(version); | |
| 67 | + assertThat(cluster).host(2).hasWorkload("Search").hasDseVersion(version); | |
| 68 | + assertThat(cluster).host(3).hasWorkload("Analytics").hasDseVersion(version); | |
| 69 | + } finally { | |
| 70 | + cluster.close(); | |
| 71 | + ccm.close(); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Validates that {@link Host#getDseVersion()} and {@link Host#getDseWorkload()} return values defined in | |
| 77 | + * the <code>dse_version</code> and <code>workload</code> columns if they are present in <code>system.local</code> | |
| 78 | + * or <code>system.peers</code> otherwise they return null. | |
| 79 | + * | |
| 80 | + * @test_category host:metadata | |
| 81 | + * @jira_ticket JAVA-1042 | |
| 82 | + */ | |
| 83 | + @Test(groups = "short'") | |
| 84 | + public void should_parse_dse_workload_and_version_if_available() { | |
| 85 | + // given: A 5 node cluster with all nodes having a workload and dse_version except node 2. | |
| 86 | + ScassandraCluster scassandraCluster = ScassandraCluster.builder() | |
| 87 | + .withIpPrefix(TestUtils.IP_PREFIX) | |
| 88 | + .withNodes(5) | |
| 89 | + .forcePeerInfo(1, 1, "workload", "Analytics") | |
| 90 | + .forcePeerInfo(1, 1, "dse_version", "4.8.4") | |
| 91 | + .forcePeerInfo(1, 3, "workload", "Solr") | |
| 92 | + .forcePeerInfo(1, 3, "dse_version", "4.8.4") | |
| 93 | + .forcePeerInfo(1, 4, "workload", "Cassandra") | |
| 94 | + .forcePeerInfo(1, 4, "dse_version", "4.8.4") | |
| 95 | + .forcePeerInfo(1, 5, "workload", "AmazingNewFeature") | |
| 96 | + .forcePeerInfo(1, 5, "dse_version", "5.0.0") | |
| 97 | + .build(); | |
| 98 | + | |
| 99 | + Cluster cluster = Cluster.builder() | |
| 100 | + .addContactPoints(scassandraCluster.address(1).getAddress()) | |
| 101 | + .withPort(scassandraCluster.getBinaryPort()) | |
| 102 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 103 | + .build(); | |
| 104 | + | |
| 105 | + try { | |
| 106 | + scassandraCluster.init(); | |
| 107 | + // when: initializing a cluster instance. | |
| 108 | + cluster.init(); | |
| 109 | + | |
| 110 | + // then: All nodes except node 2 should have a workload and dse version. | |
| 111 | + assertThat(cluster).host(1).hasWorkload("Analytics").hasDseVersion(VersionNumber.parse("4.8.4")); | |
| 112 | + assertThat(cluster).host(2).hasNoWorkload().hasNoDseVersion(); | |
| 113 | + assertThat(cluster).host(3).hasWorkload("Solr").hasDseVersion(VersionNumber.parse("4.8.4")); | |
| 114 | + assertThat(cluster).host(4).hasWorkload("Cassandra").hasDseVersion(VersionNumber.parse("4.8.4")); | |
| 115 | + assertThat(cluster).host(5).hasWorkload("AmazingNewFeature").hasDseVersion(VersionNumber.parse("5.0.0")); | |
| 116 | + } finally { | |
| 117 | + cluster.close(); | |
| 118 | + scassandraCluster.stop(); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Validates that {@link Host#getDseVersion()} and {@link Host#getDseWorkload()} return null if | |
| 124 | + * the <code>dse_version</code> and <code>workload</code> columns are not present in <code>system.local</code> | |
| 125 | + * for the control host. | |
| 126 | + * | |
| 127 | + * @test_category host:metadata | |
| 128 | + * @jira_ticket JAVA-1042 | |
| 129 | + */ | |
| 130 | + @Test(groups = "short") | |
| 131 | + public void should_not_parse_dse_workload_and_version_if_not_present_in_local_table() { | |
| 132 | + // given: A cluster with node 1 (control host) not having a workload or dse_version set, and node 2 with those | |
| 133 | + // columns set. | |
| 134 | + ScassandraCluster scassandraCluster = ScassandraCluster.builder() | |
| 135 | + .withIpPrefix(TestUtils.IP_PREFIX) | |
| 136 | + .withNodes(2) | |
| 137 | + .forcePeerInfo(1, 2, "workload", "Analytics") | |
| 138 | + .forcePeerInfo(1, 2, "dse_version", "4.8.4") | |
| 139 | + .build(); | |
| 140 | + | |
| 141 | + Cluster cluster = Cluster.builder() | |
| 142 | + .addContactPoints(scassandraCluster.address(1).getAddress()) | |
| 143 | + .withPort(scassandraCluster.getBinaryPort()) | |
| 144 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 145 | + .build(); | |
| 146 | + | |
| 147 | + try { | |
| 148 | + scassandraCluster.init(); | |
| 149 | + // when: initializing a cluster instance. | |
| 150 | + cluster.init(); | |
| 151 | + | |
| 152 | + // then: | |
| 153 | + // - node 1 should have no workload or dse version. | |
| 154 | + // - node 2 should have a workload and a dse version. | |
| 155 | + assertThat(cluster).host(1).hasNoWorkload().hasNoDseVersion(); | |
| 156 | + assertThat(cluster).host(2).hasWorkload("Analytics").hasDseVersion(VersionNumber.parse("4.8.4")); | |
| 157 | + } finally { | |
| 158 | + cluster.close(); | |
| 159 | + scassandraCluster.stop(); | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Validates that if <code>dse_version</code> column is a non-version value in <code>system.local</code> or | |
| 165 | + * <code>system.peers</code> that a warning is logged and {@link Host#getDseVersion()} returns null. | |
| 166 | + * | |
| 167 | + * @test_category host:metadata | |
| 168 | + * @jira_ticket JAVA-1042 | |
| 169 | + */ | |
| 170 | + @Test(groups = "short") | |
| 171 | + public void should_log_warning_when_invalid_version_used_for_dse_version() { | |
| 172 | + // given: A cluster with a node that has an invalid version for dse_version in system.local. | |
| 173 | + ScassandraCluster scassandraCluster = ScassandraCluster.builder() | |
| 174 | + .withIpPrefix(TestUtils.IP_PREFIX) | |
| 175 | + .withNodes(1) | |
| 176 | + .forcePeerInfo(1, 1, "workload", "Analytics") | |
| 177 | + .forcePeerInfo(1, 1, "dse_version", "Invalid Version!") | |
| 178 | + .build(); | |
| 179 | + | |
| 180 | + Cluster cluster = Cluster.builder() | |
| 181 | + .addContactPoints(scassandraCluster.address(1).getAddress()) | |
| 182 | + .withPort(scassandraCluster.getBinaryPort()) | |
| 183 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 184 | + .build(); | |
| 185 | + | |
| 186 | + MemoryAppender logs = new MemoryAppender(); | |
| 187 | + Logger logger = Logger.getLogger(Host.class); | |
| 188 | + Level originalLoggerLevel = logger.getLevel(); | |
| 189 | + logger.setLevel(Level.WARN); | |
| 190 | + logger.addAppender(logs); | |
| 191 | + | |
| 192 | + try { | |
| 193 | + scassandraCluster.init(); | |
| 194 | + // when: initializing a cluster instance. | |
| 195 | + cluster.init(); | |
| 196 | + | |
| 197 | + // then: dse version for that host should not be set and a warning shall be logged. | |
| 198 | + assertThat(logs.get()) | |
| 199 | + .contains("Error parsing DSE version Invalid Version!. This shouldn't have happened"); | |
| 200 | + assertThat(cluster).host(1).hasNoDseVersion().hasWorkload("Analytics"); | |
| 201 | + } finally { | |
| 202 | + logger.removeAppender(logs); | |
| 203 | + logger.setLevel(originalLoggerLevel); | |
| 204 | + cluster.close(); | |
| 205 | + scassandraCluster.stop(); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + /** | |
| 210 | + * Validates that {@link Host#getBroadcastAddress()} is set for all hosts (control host or not) in the default | |
| 211 | + * case of a cluster, as the broadcast address is derived from <code>broadcast_address</code> in | |
| 212 | + * <code>system.local</code> and <code>peer</code> in <code>system.peers</code>. | |
| 213 | + * | |
| 214 | + * @test_category host:metadata | |
| 215 | + * @jira_ticket JAVA-1035 | |
| 216 | + */ | |
| 217 | + @Test(groups = "short") | |
| 218 | + public void should_set_broadcast_address_for_all_nodes() { | |
| 219 | + // given: A cluster with 3 nodes. | |
| 220 | + ScassandraCluster scassandraCluster = ScassandraCluster.builder() | |
| 221 | + .withIpPrefix(TestUtils.IP_PREFIX) | |
| 222 | + .withNodes(3) | |
| 223 | + .build(); | |
| 224 | + | |
| 225 | + Cluster cluster = Cluster.builder() | |
| 226 | + .addContactPoints(scassandraCluster.address(1).getAddress()) | |
| 227 | + .withPort(scassandraCluster.getBinaryPort()) | |
| 228 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 229 | + .build(); | |
| 230 | + | |
| 231 | + try { | |
| 232 | + scassandraCluster.init(); | |
| 233 | + // when: initializing a cluster instance. | |
| 234 | + cluster.init(); | |
| 235 | + | |
| 236 | + // then: broadcast address should be set for each host: | |
| 237 | + // - broadcast_address is used for system.local and is always present, so control host should have it. | |
| 238 | + // - peer column is used for system.peers to resolve broadcast address and is always present. | |
| 239 | + for (int i = 1; i <= scassandraCluster.nodes().size(); i++) { | |
| 240 | + assertThat(cluster).host(i).hasBroadcastAddress(TestUtils.addressOfNode(i)); | |
| 241 | + } | |
| 242 | + } finally { | |
| 243 | + cluster.close(); | |
| 244 | + scassandraCluster.stop(); | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Validates that {@link Host#getListenAddress()} is set for a host if and only if the <code>listen_address</code> | |
| 250 | + * column is present and set in the <code>system.peers</code> table or if the host is the control host. | |
| 251 | + * | |
| 252 | + * @test_category host:metadata | |
| 253 | + * @jira_ticket JAVA-1035 | |
| 254 | + */ | |
| 255 | + @Test(groups = "short") | |
| 256 | + public void should_set_listen_address_if_available() { | |
| 257 | + // given: A Cluster with 3 nodes and node 2 being configured with a listen address in its peers table. | |
| 258 | + InetAddress listenAddress = TestUtils.addressOfNode(10); | |
| 259 | + ScassandraCluster scassandraCluster = ScassandraCluster.builder() | |
| 260 | + .withIpPrefix(TestUtils.IP_PREFIX) | |
| 261 | + .withNodes(3) | |
| 262 | + .forcePeerInfo(1, 2, "listen_address", listenAddress) | |
| 263 | + .build(); | |
| 264 | + | |
| 265 | + Cluster cluster = Cluster.builder() | |
| 266 | + .addContactPoints(scassandraCluster.address(1).getAddress()) | |
| 267 | + .withPort(scassandraCluster.getBinaryPort()) | |
| 268 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 269 | + .build(); | |
| 270 | + | |
| 271 | + try { | |
| 272 | + // when: initializing a cluster instance. | |
| 273 | + scassandraCluster.init(); | |
| 274 | + cluster.init(); | |
| 275 | + | |
| 276 | + // then: listen address should be set appropriate for each host: | |
| 277 | + // - Control Host will always have a listen address (listen_address should always be present). | |
| 278 | + assertThat(cluster).host(1).hasListenAddress(TestUtils.addressOfNode(1)); | |
| 279 | + // - Host 2 should have a listen address since we provided one. | |
| 280 | + assertThat(cluster).host(2).hasListenAddress(listenAddress); | |
| 281 | + // - Host 3 should have no listen address as it wasn't provided. | |
| 282 | + assertThat(cluster).host(3).hasNoListenAddress(); | |
| 283 | + } finally { | |
| 284 | + cluster.close(); | |
| 285 | + scassandraCluster.stop(); | |
| 286 | + } | |
| 287 | + } | |
| 288 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/ScassandraCluster.java +33 −9
| @@ -287,31 +287,42 @@ | ||
| 287 | 287 | if (node == peer) { // prime system.local. |
| 288 | 288 | metadata = SELECT_LOCAL; |
| 289 | 289 | query = "SELECT * FROM system.local WHERE key='local'"; |
| 290 | - row = ImmutableMap.<String, Object>builder() | |
| 290 | + ImmutableMap.Builder<String, Object> rowBuilder = ImmutableMap.<String, Object>builder() | |
| 291 | 291 | .put("key", "local") |
| 292 | 292 | .put("bootstrapped", "COMPLETED") |
| 293 | 293 | .put("broadcast_address", address) |
| 294 | 294 | .put("cluster_name", "scassandra") |
| 295 | 295 | .put("cql_version", "3.2.0") |
| 296 | 296 | .put("data_center", datacenter(dc)) |
| 297 | - .put("listen_address", address) | |
| 297 | + .put("listen_address", getPeerInfo(dc, n + 1, "listen_address", address)) | |
| 298 | 298 | .put("partitioner", "org.apache.cassandra.dht.Murmur3Partitioner") |
| 299 | 299 | .put("rack", "rack1") |
| 300 | 300 | .put("release_version", getPeerInfo(dc, n + 1, "release_version", "2.1.8")) |
| 301 | - .put("tokens", ImmutableSet.of(tokens.get(n))) | |
| 302 | - .build(); | |
| 301 | + .put("tokens", ImmutableSet.of(tokens.get(n))); | |
| 302 | + | |
| 303 | + // These columns might not always be present, we don't have to specify them in the scassandra | |
| 304 | + // column metadata as it will default them to text columns. | |
| 305 | + rowBuilder = addInfoIfExists(rowBuilder, dc, n + 1, "dse_version"); | |
| 306 | + rowBuilder = addInfoIfExists(rowBuilder, dc, n + 1, "workload"); | |
| 307 | + | |
| 308 | + row = rowBuilder.build(); | |
| 303 | 309 | } else { // prime system.peers. |
| 304 | 310 | query = "SELECT * FROM system.peers WHERE peer='" + address + "'"; |
| 305 | 311 | metadata = SELECT_PEERS; |
| 306 | - row = ImmutableMap.<String, Object>builder() | |
| 312 | + ImmutableMap.Builder<String, Object> rowBuilder = ImmutableMap.<String, Object>builder() | |
| 307 | 313 | .put("peer", address) |
| 308 | 314 | .put("rpc_address", address) |
| 309 | 315 | .put("data_center", datacenter(dc)) |
| 310 | 316 | .put("rack", "rack1") |
| 311 | 317 | .put("release_version", getPeerInfo(dc, n + 1, "release_version", "2.1.8")) |
| 312 | - .put("tokens", ImmutableSet.of(Long.toString(tokens.get(n)))) | |
| 313 | - .build(); | |
| 314 | - rows.add(row); | |
| 318 | + .put("tokens", ImmutableSet.of(Long.toString(tokens.get(n)))); | |
| 319 | + | |
| 320 | + rowBuilder = addInfoIfExists(rowBuilder, dc, n + 1, "listen_address"); | |
| 321 | + rowBuilder = addInfoIfExists(rowBuilder, dc, n + 1, "dse_version"); | |
| 322 | + rowBuilder = addInfoIfExists(rowBuilder, dc, n + 1, "workload"); | |
| 323 | + | |
| 324 | + row = rowBuilder.build(); | |
| 325 | + rows.add(rowBuilder.build()); | |
| 315 | 326 | } |
| 316 | 327 | client.prime(PrimingRequest.queryBuilder() |
| 317 | 328 | .withQuery(query) |
| @@ -353,6 +364,18 @@ | ||
| 353 | 364 | .build()); |
| 354 | 365 | } |
| 355 | 366 | |
| 367 | + private ImmutableMap.Builder<String, Object> addInfoIfExists(ImmutableMap.Builder<String, Object> builder, int dc, int node, String property) { | |
| 368 | + Map<Integer, Map<String, Object>> forDc = forcedPeerInfos.get(dc); | |
| 369 | + if (forDc == null) | |
| 370 | + return builder; | |
| 371 | + | |
| 372 | + Map<String, Object> forNode = forDc.get(node); | |
| 373 | + if (forNode == null) | |
| 374 | + return builder; | |
| 375 | + | |
| 376 | + return (forNode.containsKey(property)) ? builder.put(property, forNode.get(property)) : builder; | |
| 377 | + } | |
| 378 | + | |
| 356 | 379 | private Object getPeerInfo(int dc, int node, String property, Object defaultValue) { |
| 357 | 380 | Map<Integer, Map<String, Object>> forDc = forcedPeerInfos.get(dc); |
| 358 | 381 | if (forDc == null) |
| @@ -373,7 +396,8 @@ | ||
| 373 | 396 | column("data_center", TEXT), |
| 374 | 397 | column("rack", TEXT), |
| 375 | 398 | column("release_version", TEXT), |
| 376 | - column("tokens", set(TEXT)) | |
| 399 | + column("tokens", set(TEXT)), | |
| 400 | + column("listen_address", INET) | |
| 377 | 401 | }; |
| 378 | 402 | |
| 379 | 403 | static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = { |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +1 −1
| @@ -569,7 +569,7 @@ | ||
| 569 | 569 | */ |
| 570 | 570 | public static InetAddress addressOfNode(int i) { |
| 571 | 571 | try { |
| 572 | - return InetAddress.getByName(IP_PREFIX + 1); | |
| 572 | + return InetAddress.getByName(IP_PREFIX + i); | |
| 573 | 573 | } catch (UnknownHostException e) { |
| 574 | 574 | // should never happen |
| 575 | 575 | throw Throwables.propagate(e); |