kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3ee15697d42eabb6aac17ce5146dbabad7c83a7362418a513285a20af18bed5c |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-09-14 18:02 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-11-30 19:27 +01:00 |
| Parents | 00d2baed8f37 |
21 files changed
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +4 −0
| @@ -23,6 +23,10 @@ | ||
| 23 | 23 | return new ClusterAssert(cluster); |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | + public static HostAssert assertThat(Host host) { | |
| 27 | + return new HostAssert(host); | |
| 28 | + } | |
| 29 | + | |
| 26 | 30 | public static SessionAssert assertThat(Session session) { |
| 27 | 31 | return new SessionAssert(session); |
| 28 | 32 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +5 −0
| @@ -47,6 +47,11 @@ | ||
| 47 | 47 | return this; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | + public HostAssert controlHost() { | |
| 51 | + Host host = TestUtils.findOrWaitForControlConnection(actual, 10, TimeUnit.SECONDS); | |
| 52 | + return new HostAssert(host, actual); | |
| 53 | + } | |
| 54 | + | |
| 50 | 55 | public HostAssert host(int hostNumber) { |
| 51 | 56 | // Wait for the node to be added if it's not already known. |
| 52 | 57 | // In 2.2+ C* does not send an added event until the node is ready so we wait a long time. |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterInitTest.java +1 −1
| @@ -239,7 +239,7 @@ | ||
| 239 | 239 | primingClient.prime( |
| 240 | 240 | PrimingRequest.queryBuilder() |
| 241 | 241 | .withQuery("SELECT * FROM system.peers") |
| 242 | - .withColumnTypes(SCassandraCluster.SELECT_PEERS_COLUMN_TYPES) | |
| 242 | + .withColumnTypes(ScassandraCluster.SELECT_PEERS) | |
| 243 | 243 | .withRows(rows) |
| 244 | 244 | .build()); |
| 245 | 245 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +11 −2
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.net.InetAddress; |
| 19 | +import java.util.Collection; | |
| 19 | 20 | import java.util.Iterator; |
| 20 | 21 | import java.util.Map; |
| 21 | 22 | import java.util.concurrent.TimeUnit; |
| @@ -23,11 +24,14 @@ | ||
| 23 | 24 | |
| 24 | 25 | import com.google.common.base.Function; |
| 25 | 26 | import com.google.common.collect.HashMultiset; |
| 27 | +import com.google.common.collect.Iterables; | |
| 26 | 28 | import com.google.common.collect.Maps; |
| 27 | 29 | import org.slf4j.Logger; |
| 28 | 30 | import org.slf4j.LoggerFactory; |
| 29 | 31 | import org.testng.annotations.Test; |
| 30 | 32 | |
| 33 | +import static com.google.common.collect.Lists.newArrayList; | |
| 34 | + | |
| 31 | 35 | import static com.datastax.driver.core.Assertions.assertThat; |
| 32 | 36 | import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; |
| 33 | 37 | import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; |
| @@ -152,13 +156,18 @@ | ||
| 152 | 156 | public void should_randomize_contact_points_when_determining_control_connection() { |
| 153 | 157 | int hostCount = 5; |
| 154 | 158 | int iterations = 100; |
| 155 | - SCassandraCluster scassandras = new SCassandraCluster(hostCount); | |
| 159 | + ScassandraCluster scassandras = ScassandraCluster.builder().withNodes(hostCount).build(); | |
| 160 | + scassandras.init(); | |
| 156 | 161 | |
| 157 | 162 | try { |
| 163 | + Collection<String> contactPoints = newArrayList(); | |
| 164 | + for(int i = 1; i <= hostCount; i++) { | |
| 165 | + contactPoints.add(scassandras.address(i)); | |
| 166 | + } | |
| 158 | 167 | final HashMultiset<InetAddress> occurrencesByHost = HashMultiset.create(hostCount); |
| 159 | 168 | for(int i = 0; i < iterations; i++) { |
| 160 | 169 | Cluster cluster = Cluster.builder() |
| 161 | - .addContactPoints(scassandras.addresses()) | |
| 170 | + .addContactPoints(contactPoints.toArray(new String[hostCount])) | |
| 162 | 171 | .withNettyOptions(nonQuietClusterCloseOptions) |
| 163 | 172 | .build(); |
| 164 | 173 |
added driver-core/src/test/java/com/datastax/driver/core/DataProviders.java +72 −0
| @@ -0,0 +1,72 @@ | ||
| 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 java.util.Arrays; | |
| 19 | +import java.util.Iterator; | |
| 20 | + | |
| 21 | +import com.google.common.base.Predicate; | |
| 22 | +import com.google.common.collect.Iterables; | |
| 23 | +import org.testng.annotations.DataProvider; | |
| 24 | + | |
| 25 | +public class DataProviders { | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * @return A DataProvider that provides all non-serial consistency levels | |
| 29 | + */ | |
| 30 | + @DataProvider(name = "consistencyLevels") | |
| 31 | + public static Iterator<Object[]> consistencyLevels() { | |
| 32 | + final Iterator<ConsistencyLevel> consistencyLevels = Iterables.filter(Arrays.asList(ConsistencyLevel.values()), new Predicate<ConsistencyLevel>() { | |
| 33 | + @Override | |
| 34 | + public boolean apply(ConsistencyLevel input) { | |
| 35 | + // filter out serial CLs. | |
| 36 | + return !input.equals(ConsistencyLevel.SERIAL) && !input.equals(ConsistencyLevel.LOCAL_SERIAL); | |
| 37 | + } | |
| 38 | + }).iterator(); | |
| 39 | + | |
| 40 | + return new Iterator<Object[]> () { | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public boolean hasNext() { | |
| 44 | + return consistencyLevels.hasNext(); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Override | |
| 48 | + public Object[] next() { | |
| 49 | + return new Object[] { | |
| 50 | + consistencyLevels.next() | |
| 51 | + }; | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Override | |
| 55 | + public void remove() { | |
| 56 | + throw new UnsupportedOperationException("This shouldn't happen.."); | |
| 57 | + } | |
| 58 | + }; | |
| 59 | + } | |
| 60 | + | |
| 61 | + /** | |
| 62 | + * @return A DataProvider that provides all serial consistency levels | |
| 63 | + */ | |
| 64 | + @DataProvider(name = "serialConsistencyLevels") | |
| 65 | + public static Object[][] serialConsistencyLevels() { | |
| 66 | + return new Object[][] { | |
| 67 | + { ConsistencyLevel.SERIAL }, | |
| 68 | + { ConsistencyLevel.LOCAL_SERIAL } | |
| 69 | + }; | |
| 70 | + } | |
| 71 | + | |
| 72 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +9 −0
| @@ -31,6 +31,10 @@ | ||
| 31 | 31 | |
| 32 | 32 | private final Cluster cluster; |
| 33 | 33 | |
| 34 | + protected HostAssert(Host host) { | |
| 35 | + this(host, null); | |
| 36 | + } | |
| 37 | + | |
| 34 | 38 | protected HostAssert(Host host, Cluster cluster) { |
| 35 | 39 | super(host, HostAssert.class); |
| 36 | 40 | this.cluster = cluster; |
| @@ -63,6 +67,11 @@ | ||
| 63 | 67 | return this; |
| 64 | 68 | } |
| 65 | 69 | |
| 70 | + public HostAssert isInDatacenter(String datacenter) { | |
| 71 | + assertThat(actual.getDatacenter()).isEqualTo(datacenter); | |
| 72 | + return this; | |
| 73 | + } | |
| 74 | + | |
| 66 | 75 | public HostAssert isNotReconnectingFromDown() { |
| 67 | 76 | assertThat(actual.getReconnectionAttemptFuture() != null && !actual.getReconnectionAttemptFuture().isDone()) |
| 68 | 77 | .isFalse(); |
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolMultiTest.java +6 −5
| @@ -32,13 +32,14 @@ | ||
| 32 | 32 | |
| 33 | 33 | public class HostConnectionPoolMultiTest { |
| 34 | 34 | |
| 35 | - private SCassandraCluster scassandra; | |
| 35 | + private ScassandraCluster scassandra; | |
| 36 | 36 | |
| 37 | 37 | private Cluster cluster; |
| 38 | 38 | |
| 39 | 39 | @BeforeMethod(groups={"short", "long"}) |
| 40 | 40 | private void setUp() { |
| 41 | - scassandra = new SCassandraCluster(2); | |
| 41 | + scassandra = ScassandraCluster.builder().withNodes(2).build(); | |
| 42 | + scassandra.init(); | |
| 42 | 43 | } |
| 43 | 44 | |
| 44 | 45 | @AfterMethod(groups={"short", "long"}) |
| @@ -72,7 +73,7 @@ | ||
| 72 | 73 | @Test(groups="short") |
| 73 | 74 | public void should_mark_host_down_if_all_connections_fail_on_init() { |
| 74 | 75 | // Prevent any connections on node 2. |
| 75 | - scassandra.instance(2).currentClient().disableListener(); | |
| 76 | + scassandra.node(2).currentClient().disableListener(); | |
| 76 | 77 | createCluster(8,8); |
| 77 | 78 | |
| 78 | 79 | // Node 2 should be in a down state while node 1 stays up. |
| @@ -80,7 +81,7 @@ | ||
| 80 | 81 | assertThat(cluster).host(1).isUp(); |
| 81 | 82 | |
| 82 | 83 | // Node 2 should come up as soon as it is able to reconnect. |
| 83 | - scassandra.instance(2).currentClient().enableListener(); | |
| 84 | + scassandra.node(2).currentClient().enableListener(); | |
| 84 | 85 | assertThat(cluster).host(2).comesUpWithin(2, SECONDS); |
| 85 | 86 | } |
| 86 | 87 | |
| @@ -104,7 +105,7 @@ | ||
| 104 | 105 | InetSocketAddress controlSocket = (InetSocketAddress)controlConnection.channel.localAddress(); |
| 105 | 106 | |
| 106 | 107 | // Close the control connection. |
| 107 | - scassandra.instance(1).currentClient() | |
| 108 | + scassandra.node(1).currentClient() | |
| 108 | 109 | .closeConnection(CLOSE, controlSocket); |
| 109 | 110 | |
| 110 | 111 | // Sleep reconnect interval * 2 to allow time to reconnect. |
deleted driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyTest.java +0 −456
| @@ -1,456 +0,0 @@ | ||
| 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 java.net.InetAddress; | |
| 19 | -import java.net.InetSocketAddress; | |
| 20 | -import java.util.Arrays; | |
| 21 | -import java.util.List; | |
| 22 | -import org.testng.annotations.Test; | |
| 23 | - | |
| 24 | -import com.datastax.driver.core.exceptions.*; | |
| 25 | -import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; | |
| 26 | -import com.datastax.driver.core.policies.RoundRobinPolicy; | |
| 27 | -import com.datastax.driver.core.policies.TokenAwarePolicy; | |
| 28 | -import com.datastax.driver.core.policies.WhiteListPolicy; | |
| 29 | - | |
| 30 | -import static com.datastax.driver.core.TestUtils.*; | |
| 31 | -import static org.testng.Assert.*; | |
| 32 | - | |
| 33 | -public class LoadBalancingPolicyTest extends AbstractPoliciesTest { | |
| 34 | - | |
| 35 | - @Test(groups = "long") | |
| 36 | - public void roundRobinTest() throws Throwable { | |
| 37 | - | |
| 38 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new RoundRobinPolicy()); | |
| 39 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, builder); | |
| 40 | - try { | |
| 41 | - | |
| 42 | - createSchema(c.session); | |
| 43 | - init(c, 12); | |
| 44 | - query(c, 12); | |
| 45 | - | |
| 46 | - assertQueried(CCMBridge.IP_PREFIX + '1', 6); | |
| 47 | - assertQueried(CCMBridge.IP_PREFIX + '2', 6); | |
| 48 | - | |
| 49 | - resetCoordinators(); | |
| 50 | - c.cassandraCluster.bootstrapNode(3); | |
| 51 | - waitFor(CCMBridge.IP_PREFIX + '3', c.cluster); | |
| 52 | - Thread.sleep(50000); | |
| 53 | - | |
| 54 | - query(c, 12); | |
| 55 | - | |
| 56 | - assertQueried(CCMBridge.IP_PREFIX + '1', 4); | |
| 57 | - assertQueried(CCMBridge.IP_PREFIX + '2', 4); | |
| 58 | - assertQueried(CCMBridge.IP_PREFIX + '3', 4); | |
| 59 | - | |
| 60 | - resetCoordinators(); | |
| 61 | - c.cassandraCluster.decommissionNode(1); | |
| 62 | - waitForDecommission(CCMBridge.IP_PREFIX + '1', c.cluster); | |
| 63 | - | |
| 64 | - query(c, 12); | |
| 65 | - | |
| 66 | - assertQueried(CCMBridge.IP_PREFIX + '2', 6); | |
| 67 | - assertQueried(CCMBridge.IP_PREFIX + '3', 6); | |
| 68 | - | |
| 69 | - } catch (Throwable e) { | |
| 70 | - c.errorOut(); | |
| 71 | - throw e; | |
| 72 | - } finally { | |
| 73 | - resetCoordinators(); | |
| 74 | - c.discard(); | |
| 75 | - } | |
| 76 | - } | |
| 77 | - | |
| 78 | - @Test(groups = "long") | |
| 79 | - public void whiteListPolicyTest() throws Throwable { | |
| 80 | - | |
| 81 | - List<InetSocketAddress> whiteList = Arrays.asList(new InetSocketAddress(InetAddress.getByName(CCMBridge.IP_PREFIX + '2'), 9042)); | |
| 82 | - | |
| 83 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new WhiteListPolicy(new RoundRobinPolicy(), whiteList)); | |
| 84 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(3, builder); | |
| 85 | - try { | |
| 86 | - | |
| 87 | - createSchema(c.session); | |
| 88 | - init(c, 12); | |
| 89 | - query(c, 12); | |
| 90 | - | |
| 91 | - assertQueried(CCMBridge.IP_PREFIX + "1", 0); | |
| 92 | - assertQueried(CCMBridge.IP_PREFIX + "2", 12); | |
| 93 | - assertQueried(CCMBridge.IP_PREFIX + "3", 0); | |
| 94 | - | |
| 95 | - resetCoordinators(); | |
| 96 | - c.cassandraCluster.decommissionNode(2); | |
| 97 | - waitForDecommission(CCMBridge.IP_PREFIX + "2", c.cluster); | |
| 98 | - | |
| 99 | - try { | |
| 100 | - query(c, 12); | |
| 101 | - fail("Should work, we've only whitelisted node 2 and it's been removed"); | |
| 102 | - } catch (NoHostAvailableException e) { | |
| 103 | - // That's what we expected | |
| 104 | - } | |
| 105 | - | |
| 106 | - } catch (Throwable e) { | |
| 107 | - c.errorOut(); | |
| 108 | - throw e; | |
| 109 | - } finally { | |
| 110 | - resetCoordinators(); | |
| 111 | - c.discard(); | |
| 112 | - } | |
| 113 | - } | |
| 114 | - | |
| 115 | - @Test(groups = "long") | |
| 116 | - public void roundRobinWith2DCsTest() throws Throwable { | |
| 117 | - | |
| 118 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new RoundRobinPolicy()); | |
| 119 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, 2, builder); | |
| 120 | - try { | |
| 121 | - | |
| 122 | - createSchema(c.session); | |
| 123 | - init(c, 12); | |
| 124 | - query(c, 12); | |
| 125 | - | |
| 126 | - assertQueried(CCMBridge.IP_PREFIX + '1', 3); | |
| 127 | - assertQueried(CCMBridge.IP_PREFIX + '2', 3); | |
| 128 | - assertQueried(CCMBridge.IP_PREFIX + '3', 3); | |
| 129 | - assertQueried(CCMBridge.IP_PREFIX + '4', 3); | |
| 130 | - | |
| 131 | - resetCoordinators(); | |
| 132 | - c.cassandraCluster.bootstrapNode(5, "dc2"); | |
| 133 | - c.cassandraCluster.decommissionNode(1); | |
| 134 | - waitFor(CCMBridge.IP_PREFIX + '5', c.cluster); | |
| 135 | - waitForDecommission(CCMBridge.IP_PREFIX + '1', c.cluster); | |
| 136 | - Thread.sleep(50000); | |
| 137 | - | |
| 138 | - query(c, 12); | |
| 139 | - | |
| 140 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 141 | - assertQueried(CCMBridge.IP_PREFIX + '2', 3); | |
| 142 | - assertQueried(CCMBridge.IP_PREFIX + '3', 3); | |
| 143 | - assertQueried(CCMBridge.IP_PREFIX + '4', 3); | |
| 144 | - assertQueried(CCMBridge.IP_PREFIX + '5', 3); | |
| 145 | - | |
| 146 | - } catch (Throwable e) { | |
| 147 | - c.errorOut(); | |
| 148 | - throw e; | |
| 149 | - } finally { | |
| 150 | - resetCoordinators(); | |
| 151 | - c.discard(); | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 155 | - @Test(groups = "long") | |
| 156 | - public void DCAwareRoundRobinTest() throws Throwable { | |
| 157 | - | |
| 158 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2")); | |
| 159 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, 2, builder); | |
| 160 | - try { | |
| 161 | - | |
| 162 | - createMultiDCSchema(c.session); | |
| 163 | - init(c, 12); | |
| 164 | - query(c, 12); | |
| 165 | - | |
| 166 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 167 | - assertQueried(CCMBridge.IP_PREFIX + '2', 0); | |
| 168 | - assertQueried(CCMBridge.IP_PREFIX + '3', 6); | |
| 169 | - assertQueried(CCMBridge.IP_PREFIX + '4', 6); | |
| 170 | - | |
| 171 | - } catch (Throwable e) { | |
| 172 | - c.errorOut(); | |
| 173 | - throw e; | |
| 174 | - } finally { | |
| 175 | - resetCoordinators(); | |
| 176 | - c.discard(); | |
| 177 | - } | |
| 178 | - } | |
| 179 | - | |
| 180 | - @Test(groups = "long") | |
| 181 | - public void DCAwareRoundRobinTest2() throws Throwable { | |
| 182 | - | |
| 183 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc1")); | |
| 184 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, 2, builder); | |
| 185 | - try { | |
| 186 | - | |
| 187 | - createMultiDCSchema(c.session); | |
| 188 | - init(c, 12); | |
| 189 | - query(c, 12); | |
| 190 | - | |
| 191 | - assertQueried(CCMBridge.IP_PREFIX + '1', 6); | |
| 192 | - assertQueried(CCMBridge.IP_PREFIX + '2', 6); | |
| 193 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 194 | - assertQueried(CCMBridge.IP_PREFIX + '4', 0); | |
| 195 | - | |
| 196 | - } catch (Throwable e) { | |
| 197 | - c.errorOut(); | |
| 198 | - throw e; | |
| 199 | - } finally { | |
| 200 | - resetCoordinators(); | |
| 201 | - c.discard(); | |
| 202 | - } | |
| 203 | - } | |
| 204 | - | |
| 205 | - @Test(groups = "long") | |
| 206 | - public void dcAwareRoundRobinTestWithOneRemoteHost() throws Throwable { | |
| 207 | - | |
| 208 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new DCAwareRoundRobinPolicy("dc2", 1)); | |
| 209 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, 2, builder); | |
| 210 | - try { | |
| 211 | - | |
| 212 | - createMultiDCSchema(c.session); | |
| 213 | - init(c, 12); | |
| 214 | - query(c, 12); | |
| 215 | - | |
| 216 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 217 | - assertQueried(CCMBridge.IP_PREFIX + '2', 0); | |
| 218 | - assertQueried(CCMBridge.IP_PREFIX + '3', 6); | |
| 219 | - assertQueried(CCMBridge.IP_PREFIX + '4', 6); | |
| 220 | - assertQueried(CCMBridge.IP_PREFIX + '5', 0); | |
| 221 | - | |
| 222 | - resetCoordinators(); | |
| 223 | - c.cassandraCluster.bootstrapNode(5, "dc3"); | |
| 224 | - waitFor(CCMBridge.IP_PREFIX + '5', c.cluster); | |
| 225 | - | |
| 226 | - query(c, 12); | |
| 227 | - | |
| 228 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 229 | - assertQueried(CCMBridge.IP_PREFIX + '2', 0); | |
| 230 | - assertQueried(CCMBridge.IP_PREFIX + '3', 6); | |
| 231 | - assertQueried(CCMBridge.IP_PREFIX + '4', 6); | |
| 232 | - assertQueried(CCMBridge.IP_PREFIX + '5', 0); | |
| 233 | - | |
| 234 | - resetCoordinators(); | |
| 235 | - c.cassandraCluster.decommissionNode(3); | |
| 236 | - c.cassandraCluster.decommissionNode(4); | |
| 237 | - waitForDecommission(CCMBridge.IP_PREFIX + '3', c.cluster); | |
| 238 | - waitForDecommission(CCMBridge.IP_PREFIX + '4', c.cluster); | |
| 239 | - | |
| 240 | - // TODO: Fix this now non-deterministic test | |
| 241 | - //query(c, 12); | |
| 242 | - // | |
| 243 | - //assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 244 | - //assertQueried(CCMBridge.IP_PREFIX + '2', 0); | |
| 245 | - //assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 246 | - //assertQueried(CCMBridge.IP_PREFIX + '4', 0); | |
| 247 | - //assertQueried(CCMBridge.IP_PREFIX + '5', 12); | |
| 248 | - // | |
| 249 | - //resetCoordinators(); | |
| 250 | - c.cassandraCluster.decommissionNode(5); | |
| 251 | - waitForDecommission(CCMBridge.IP_PREFIX + '5', c.cluster, 120); | |
| 252 | - | |
| 253 | - query(c, 12); | |
| 254 | - | |
| 255 | - assertOneNodeQueried(12, CCMBridge.IP_PREFIX + '1', CCMBridge.IP_PREFIX + '2', CCMBridge.IP_PREFIX + '3'); | |
| 256 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 257 | - assertQueried(CCMBridge.IP_PREFIX + '4', 0); | |
| 258 | - assertQueried(CCMBridge.IP_PREFIX + '5', 0); | |
| 259 | - | |
| 260 | - resetCoordinators(); | |
| 261 | - c.cassandraCluster.decommissionNode(1); | |
| 262 | - waitForDecommission(CCMBridge.IP_PREFIX + '1', c.cluster); | |
| 263 | - | |
| 264 | - query(c, 12); | |
| 265 | - | |
| 266 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 267 | - assertQueried(CCMBridge.IP_PREFIX + '2', 12); | |
| 268 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 269 | - assertQueried(CCMBridge.IP_PREFIX + '4', 0); | |
| 270 | - assertQueried(CCMBridge.IP_PREFIX + '5', 0); | |
| 271 | - | |
| 272 | - resetCoordinators(); | |
| 273 | - c.cassandraCluster.forceStop(2); | |
| 274 | - waitForDown(CCMBridge.IP_PREFIX + '2', c.cluster); | |
| 275 | - | |
| 276 | - try { | |
| 277 | - query(c, 12); | |
| 278 | - fail(); | |
| 279 | - } catch (NoHostAvailableException e) { | |
| 280 | - // No more nodes so ... | |
| 281 | - } | |
| 282 | - | |
| 283 | - } catch (Throwable e) { | |
| 284 | - c.errorOut(); | |
| 285 | - throw e; | |
| 286 | - } finally { | |
| 287 | - resetCoordinators(); | |
| 288 | - c.discard(); | |
| 289 | - } | |
| 290 | - } | |
| 291 | - | |
| 292 | - @Test(groups = "long") | |
| 293 | - public void tokenAwareTest() throws Throwable { | |
| 294 | - tokenAwareTest(false); | |
| 295 | - } | |
| 296 | - | |
| 297 | - @Test(groups = "long") | |
| 298 | - public void tokenAwarePreparedTest() throws Throwable { | |
| 299 | - tokenAwareTest(true); | |
| 300 | - } | |
| 301 | - | |
| 302 | - /** | |
| 303 | - * Check for JAVA-123 bug. Doesn't really test token awareness, but rather | |
| 304 | - * that we do not destroy the keys. | |
| 305 | - */ | |
| 306 | - @Test(groups = "long") | |
| 307 | - public void tokenAwareCompositeKeyTest() throws Throwable { | |
| 308 | - | |
| 309 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())); | |
| 310 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, builder); | |
| 311 | - | |
| 312 | - Session session = c.session; | |
| 313 | - | |
| 314 | - try { | |
| 315 | - String COMPOSITE_TABLE = "composite"; | |
| 316 | - session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, SIMPLE_KEYSPACE, 2)); | |
| 317 | - session.execute("USE " + SIMPLE_KEYSPACE); | |
| 318 | - session.execute(String.format("CREATE TABLE %s (k1 int, k2 int, i int, PRIMARY KEY ((k1, k2)))", COMPOSITE_TABLE)); | |
| 319 | - | |
| 320 | - PreparedStatement ps = session.prepare("INSERT INTO " + COMPOSITE_TABLE + "(k1, k2, i) VALUES (?, ?, ?)"); | |
| 321 | - session.execute(ps.bind(1, 2, 3)); | |
| 322 | - | |
| 323 | - ResultSet rs = session.execute("SELECT * FROM " + COMPOSITE_TABLE + " WHERE k1 = 1 AND k2 = 2"); | |
| 324 | - assertTrue(!rs.isExhausted()); | |
| 325 | - Row r = rs.one(); | |
| 326 | - assertTrue(rs.isExhausted()); | |
| 327 | - | |
| 328 | - assertEquals(r.getInt("i"), 3); | |
| 329 | - } catch (Throwable e) { | |
| 330 | - c.errorOut(); | |
| 331 | - throw e; | |
| 332 | - } finally { | |
| 333 | - c.discard(); | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - public void tokenAwareTest(boolean usePrepared) throws Throwable { | |
| 338 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())); | |
| 339 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(3, builder); | |
| 340 | - try { | |
| 341 | - | |
| 342 | - createSchema(c.session); | |
| 343 | - init(c, 12); | |
| 344 | - query(c, 12); | |
| 345 | - | |
| 346 | - // Not the best test ever, we should use OPP and check we do it the | |
| 347 | - // right nodes. But since M3P is hard-coded for now, let just check | |
| 348 | - // we just hit only one node. | |
| 349 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 350 | - assertQueried(CCMBridge.IP_PREFIX + '2', 12); | |
| 351 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 352 | - | |
| 353 | - resetCoordinators(); | |
| 354 | - query(c, 12); | |
| 355 | - | |
| 356 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 357 | - assertQueried(CCMBridge.IP_PREFIX + '2', 12); | |
| 358 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 359 | - | |
| 360 | - resetCoordinators(); | |
| 361 | - c.cassandraCluster.stop(2); | |
| 362 | - waitForDownWithWait(CCMBridge.IP_PREFIX + '2', c.cluster, 10); | |
| 363 | - | |
| 364 | - try { | |
| 365 | - query(c, 12, usePrepared); | |
| 366 | - fail(); | |
| 367 | - } catch (ReadTimeoutException e) { | |
| 368 | - assertEquals(1, e.getRequiredAcknowledgements()); | |
| 369 | - assertEquals(0, e.getReceivedAcknowledgements()); | |
| 370 | - } catch (UnavailableException e) { | |
| 371 | - assertEquals(1, e.getRequiredReplicas()); | |
| 372 | - assertEquals(0, e.getAliveReplicas()); | |
| 373 | - } | |
| 374 | - | |
| 375 | - resetCoordinators(); | |
| 376 | - c.cassandraCluster.start(2); | |
| 377 | - waitFor(CCMBridge.IP_PREFIX + '2', c.cluster); | |
| 378 | - | |
| 379 | - // FIXME: remove sleep once waitFor() is fixed | |
| 380 | - Thread.sleep(2000); | |
| 381 | - | |
| 382 | - query(c, 12); | |
| 383 | - | |
| 384 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 385 | - assertQueried(CCMBridge.IP_PREFIX + '2', 12); | |
| 386 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 387 | - | |
| 388 | - resetCoordinators(); | |
| 389 | - c.cassandraCluster.decommissionNode(2); | |
| 390 | - waitForDecommission(CCMBridge.IP_PREFIX + '2', c.cluster); | |
| 391 | - | |
| 392 | - query(c, 12); | |
| 393 | - | |
| 394 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 395 | - assertQueried(CCMBridge.IP_PREFIX + '2', 0); | |
| 396 | - assertQueried(CCMBridge.IP_PREFIX + '3', 12); | |
| 397 | - | |
| 398 | - } catch (Throwable e) { | |
| 399 | - c.errorOut(); | |
| 400 | - throw e; | |
| 401 | - } finally { | |
| 402 | - resetCoordinators(); | |
| 403 | - c.discard(); | |
| 404 | - } | |
| 405 | - } | |
| 406 | - | |
| 407 | - @Test(groups = "long") | |
| 408 | - public void tokenAwareWithRF2Test() throws Throwable { | |
| 409 | - Cluster.Builder builder = Cluster.builder().withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy(), false)); | |
| 410 | - CCMBridge.CCMCluster c = CCMBridge.buildCluster(2, builder); | |
| 411 | - try { | |
| 412 | - | |
| 413 | - createSchema(c.session, 2); | |
| 414 | - init(c, 12); | |
| 415 | - query(c, 12); | |
| 416 | - | |
| 417 | - // Not the best test ever, we should use OPP and check we do hit the | |
| 418 | - // right nodes. But since M3P is hard-coded for now, let just check | |
| 419 | - // we just hit only one node. | |
| 420 | - assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 421 | - assertQueried(CCMBridge.IP_PREFIX + '2', 12); | |
| 422 | - assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 423 | - | |
| 424 | - // TODO: Better testing infrastructure | |
| 425 | - // https://datastax-oss.atlassian.net/browse/JAVA-245 | |
| 426 | - //resetCoordinators(); | |
| 427 | - //c.cassandraCluster.bootstrapNode(3); | |
| 428 | - //waitFor(CCMBridge.IP_PREFIX + '3', c.cluster); | |
| 429 | - // | |
| 430 | - //query(c, 12); | |
| 431 | - // | |
| 432 | - //// We should still be hitting only one node | |
| 433 | - //assertQueried(CCMBridge.IP_PREFIX + '1', 0); | |
| 434 | - //assertQueried(CCMBridge.IP_PREFIX + '2', 12); | |
| 435 | - //assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 436 | - // | |
| 437 | - //resetCoordinators(); | |
| 438 | - //c.cassandraCluster.stop(2); | |
| 439 | - //waitForDown(CCMBridge.IP_PREFIX + '2', c.cluster); | |
| 440 | - // | |
| 441 | - //query(c, 12); | |
| 442 | - // | |
| 443 | - //// Still only one node since RF=2 | |
| 444 | - //assertQueried(CCMBridge.IP_PREFIX + '1', 12); | |
| 445 | - //assertQueried(CCMBridge.IP_PREFIX + '2', 0); | |
| 446 | - //assertQueried(CCMBridge.IP_PREFIX + '3', 0); | |
| 447 | - | |
| 448 | - } catch (Throwable e) { | |
| 449 | - c.errorOut(); | |
| 450 | - throw e; | |
| 451 | - } finally { | |
| 452 | - resetCoordinators(); | |
| 453 | - c.discard(); | |
| 454 | - } | |
| 455 | - } | |
| 456 | -} |
modified driver-core/src/test/java/com/datastax/driver/core/QueryOptionsTest.java +16 −21
| @@ -19,13 +19,15 @@ | ||
| 19 | 19 | import java.util.concurrent.TimeUnit; |
| 20 | 20 | |
| 21 | 21 | import com.google.common.collect.Lists; |
| 22 | +import org.scassandra.Scassandra; | |
| 22 | 23 | import org.scassandra.http.client.PreparedStatementPreparation; |
| 23 | 24 | import org.testng.annotations.*; |
| 24 | 25 | |
| 25 | 26 | import static com.datastax.driver.core.Assertions.assertThat; |
| 27 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 26 | 28 | |
| 27 | 29 | public class QueryOptionsTest { |
| 28 | - SCassandraCluster scassandra; | |
| 30 | + ScassandraCluster scassandra; | |
| 29 | 31 | |
| 30 | 32 | QueryOptions queryOptions; |
| 31 | 33 | |
| @@ -36,19 +38,18 @@ | ||
| 36 | 38 | Host host1, host2, host3; |
| 37 | 39 | |
| 38 | 40 | |
| 39 | - @BeforeClass(groups = "short") | |
| 40 | - public void beforeClass() { | |
| 41 | - scassandra = new SCassandraCluster(CCMBridge.IP_PREFIX, 3); | |
| 42 | - } | |
| 43 | - | |
| 44 | 41 | @BeforeMethod(groups = "short") |
| 45 | 42 | public void beforeMethod() { |
| 43 | + scassandra = ScassandraCluster.builder().withNodes(3).build(); | |
| 44 | + scassandra.init(); | |
| 45 | + | |
| 46 | 46 | queryOptions = new QueryOptions(); |
| 47 | 47 | loadBalancingPolicy = new SortingLoadBalancingPolicy(); |
| 48 | 48 | cluster = Cluster.builder() |
| 49 | 49 | .addContactPoint(CCMBridge.ipOfNode(2)) |
| 50 | 50 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 51 | 51 | .withQueryOptions(queryOptions) |
| 52 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 52 | 53 | .build(); |
| 53 | 54 | |
| 54 | 55 | session = cluster.connect(); |
| @@ -59,7 +60,7 @@ | ||
| 59 | 60 | |
| 60 | 61 | // Make sure there are no prepares |
| 61 | 62 | for (int host : Lists.newArrayList(1, 2, 3)) { |
| 62 | - assertThat(scassandra.retrievePreparedStatementPreparations(host)).hasSize(0); | |
| 63 | + assertThat(scassandra.node(host).activityClient().retrievePreparedStatementPreparations()).hasSize(0); | |
| 63 | 64 | } |
| 64 | 65 | } |
| 65 | 66 | |
| @@ -70,9 +71,9 @@ | ||
| 70 | 71 | assertThat(cluster.manager.preparedQueries).containsValue(statement); |
| 71 | 72 | |
| 72 | 73 | // Ensure prepared properly based on expectation. |
| 73 | - List<PreparedStatementPreparation> preparationOne = scassandra.retrievePreparedStatementPreparations(1); | |
| 74 | - List<PreparedStatementPreparation> preparationTwo = scassandra.retrievePreparedStatementPreparations(2); | |
| 75 | - List<PreparedStatementPreparation> preparationThree = scassandra.retrievePreparedStatementPreparations(3); | |
| 74 | + List<PreparedStatementPreparation> preparationOne = scassandra.node(1).activityClient().retrievePreparedStatementPreparations(); | |
| 75 | + List<PreparedStatementPreparation> preparationTwo = scassandra.node(2).activityClient().retrievePreparedStatementPreparations(); | |
| 76 | + List<PreparedStatementPreparation> preparationThree = scassandra.node(3).activityClient().retrievePreparedStatementPreparations(); | |
| 76 | 77 | |
| 77 | 78 | assertThat(preparationOne).hasSize(1); |
| 78 | 79 | assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query); |
| @@ -142,19 +143,19 @@ | ||
| 142 | 143 | String query = "select sansa_stark from the_known_world"; |
| 143 | 144 | session.prepare(query); |
| 144 | 145 | |
| 145 | - List<PreparedStatementPreparation> preparationOne = scassandra.retrievePreparedStatementPreparations(1); | |
| 146 | + List<PreparedStatementPreparation> preparationOne = scassandra.node(1).activityClient().retrievePreparedStatementPreparations(); | |
| 146 | 147 | |
| 147 | 148 | assertThat(preparationOne).hasSize(1); |
| 148 | 149 | assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query); |
| 149 | 150 | |
| 150 | - scassandra.clearAllRecordedActivity(); | |
| 151 | - scassandra.stop(1); | |
| 151 | + scassandra.node(1).activityClient().clearAllRecordedActivity(); | |
| 152 | + scassandra.node(1).stop(); | |
| 152 | 153 | assertThat(cluster).host(1).goesDownWithin(10, TimeUnit.SECONDS); |
| 153 | 154 | |
| 154 | - scassandra.start(1); | |
| 155 | + scassandra.node(1).start(); | |
| 155 | 156 | assertThat(cluster).host(1).comesUpWithin(60, TimeUnit.SECONDS); |
| 156 | 157 | |
| 157 | - preparationOne = scassandra.retrievePreparedStatementPreparations(1); | |
| 158 | + preparationOne = scassandra.node(1).activityClient().retrievePreparedStatementPreparations(); | |
| 158 | 159 | if (expectReprepare) { |
| 159 | 160 | assertThat(preparationOne).hasSize(1); |
| 160 | 161 | assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query); |
| @@ -218,12 +219,6 @@ | ||
| 218 | 219 | if (cluster != null) |
| 219 | 220 | cluster.close(); |
| 220 | 221 | |
| 221 | - scassandra.clearAllPrimes(); | |
| 222 | - scassandra.clearAllRecordedActivity(); | |
| 223 | - } | |
| 224 | - | |
| 225 | - @AfterClass(groups = "short") | |
| 226 | - public void afterClass() { | |
| 227 | 222 | if (scassandra != null) |
| 228 | 223 | scassandra.stop(); |
| 229 | 224 | } |
added driver-core/src/test/java/com/datastax/driver/core/QueryTracker.java +109 −0
| @@ -0,0 +1,109 @@ | ||
| 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 java.net.InetAddress; | |
| 19 | +import java.util.List; | |
| 20 | +import java.util.Map; | |
| 21 | +import java.util.concurrent.ExecutionException; | |
| 22 | +import java.util.concurrent.TimeUnit; | |
| 23 | + | |
| 24 | +import com.google.common.util.concurrent.Futures; | |
| 25 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 26 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 27 | +import org.assertj.core.util.Maps; | |
| 28 | + | |
| 29 | +import static com.google.common.collect.Lists.newArrayList; | |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 31 | +import static org.testng.Assert.fail; | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * A convenience utility for executing queries against a {@link Session} and tracking | |
| 35 | + * which hosts were queried. | |
| 36 | + */ | |
| 37 | +public class QueryTracker { | |
| 38 | + static final String QUERY = "select * from test.foo"; | |
| 39 | + | |
| 40 | + Map<InetAddress, Integer> coordinators = Maps.newConcurrentHashMap(); | |
| 41 | + | |
| 42 | + public void query(Session session, int times) { | |
| 43 | + query(session, times, ConsistencyLevel.ONE); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public void query(Session session, int times, ConsistencyLevel cl) { | |
| 47 | + query(session, times, cl, null); | |
| 48 | + } | |
| 49 | + | |
| 50 | + public void query(Session session, int times, ConsistencyLevel cl, Class<? extends Exception> expectedException) { | |
| 51 | + Statement statement = new SimpleStatement(QUERY); | |
| 52 | + if(cl != null) { | |
| 53 | + statement.setConsistencyLevel(cl); | |
| 54 | + } | |
| 55 | + | |
| 56 | + query(session, times, statement, expectedException); | |
| 57 | + } | |
| 58 | + | |
| 59 | + public void query(Session session, int times, Statement statement) { | |
| 60 | + query(session, times, statement, null); | |
| 61 | + } | |
| 62 | + | |
| 63 | + public void query(Session session, int times, Statement statement, Class<? extends Exception> expectedException) { | |
| 64 | + List<ListenableFuture<ResultSet>> futures = newArrayList(); | |
| 65 | + | |
| 66 | + for(int i = 0; i < times; i++) { | |
| 67 | + futures.add(session.executeAsync(statement)); | |
| 68 | + } | |
| 69 | + | |
| 70 | + try { | |
| 71 | + List<ResultSet> results = Uninterruptibles.getUninterruptibly(Futures.allAsList(futures), 1, TimeUnit.MINUTES); | |
| 72 | + for(ResultSet result : results) { | |
| 73 | + InetAddress coordinator = result.getExecutionInfo().getQueriedHost().getAddress(); | |
| 74 | + Integer n = coordinators.get(coordinator); | |
| 75 | + coordinators.put(coordinator, n == null ? 1 : n + 1); | |
| 76 | + } | |
| 77 | + } catch (ExecutionException ex) { | |
| 78 | + Throwable cause = ex.getCause(); | |
| 79 | + if(expectedException == null) { | |
| 80 | + fail("Queries failed", ex); | |
| 81 | + } else { | |
| 82 | + assertThat(cause).isInstanceOf(expectedException); | |
| 83 | + } | |
| 84 | + } catch (Exception e) { | |
| 85 | + fail("Queries failed", e); | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + public int queryCount(ScassandraCluster sCluster, int dc, int node) { | |
| 90 | + try { | |
| 91 | + String host = sCluster.address(dc, node); | |
| 92 | + Integer queried = coordinators.get(InetAddress.getByName(host)); | |
| 93 | + return queried != null ? queried : 0; | |
| 94 | + } catch (Exception e) { | |
| 95 | + throw new RuntimeException(e); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + public void assertQueried(ScassandraCluster sCluster, int dc, int node, int n) { | |
| 100 | + int queryCount = queryCount(sCluster, dc, node); | |
| 101 | + assertThat(queryCount) | |
| 102 | + .as("Expected node %d:%d to be queried %d times but was %d", dc, node, n, queryCount) | |
| 103 | + .isEqualTo(n); | |
| 104 | + } | |
| 105 | + | |
| 106 | + public void reset() { | |
| 107 | + this.coordinators.clear(); | |
| 108 | + } | |
| 109 | +} |
deleted driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +0 −178
| @@ -1,178 +0,0 @@ | ||
| 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 java.net.InetAddress; | |
| 19 | -import java.net.UnknownHostException; | |
| 20 | -import java.util.List; | |
| 21 | -import java.util.Map; | |
| 22 | - | |
| 23 | -import com.google.common.collect.ImmutableMap; | |
| 24 | -import com.google.common.collect.ImmutableSet; | |
| 25 | -import com.google.common.collect.Lists; | |
| 26 | -import org.scassandra.Scassandra; | |
| 27 | -import org.scassandra.ScassandraFactory; | |
| 28 | -import org.scassandra.http.client.*; | |
| 29 | - | |
| 30 | -/** | |
| 31 | - * Launches multiple SCassandra instances, and mocks the appropriate request to make the driver think | |
| 32 | - * that they belong to the same cluster. | |
| 33 | - * | |
| 34 | - * Note: this uses the same addresses as {@link CCMBridge}, so make sure IP aliases have been set up, | |
| 35 | - * and a CCM cluster is not running at the same time. | |
| 36 | - */ | |
| 37 | -public class SCassandraCluster { | |
| 38 | - private static final int BINARY_PORT = 9042; | |
| 39 | - private static final int ADMIN_PORT = 9052; | |
| 40 | - | |
| 41 | - private final List<Scassandra> scassandras; | |
| 42 | - private final List<InetAddress> addresses; | |
| 43 | - private final List<PrimingClient> primingClients; | |
| 44 | - private final List<ActivityClient> activityClients; | |
| 45 | - | |
| 46 | - public SCassandraCluster(int nodeCount) { | |
| 47 | - this(CCMBridge.IP_PREFIX, nodeCount); | |
| 48 | - } | |
| 49 | - | |
| 50 | - public SCassandraCluster(String ipPrefix, int nodeCount) { | |
| 51 | - scassandras = Lists.newArrayListWithCapacity(nodeCount); | |
| 52 | - addresses = Lists.newArrayListWithCapacity(nodeCount); | |
| 53 | - primingClients = Lists.newArrayListWithCapacity(nodeCount); | |
| 54 | - activityClients = Lists.newArrayListWithCapacity(nodeCount); | |
| 55 | - | |
| 56 | - for (int i = 1; i <= nodeCount; i++) { | |
| 57 | - String ip = ipPrefix + i; | |
| 58 | - try { | |
| 59 | - addresses.add(InetAddress.getByName(ip)); | |
| 60 | - } catch (UnknownHostException e) { | |
| 61 | - throw new AssertionError(e); | |
| 62 | - } | |
| 63 | - Scassandra scassandra = ScassandraFactory.createServer(ip, BINARY_PORT, ip, ADMIN_PORT); | |
| 64 | - scassandra.start(); | |
| 65 | - scassandras.add(scassandra); | |
| 66 | - | |
| 67 | - // Currently Scassandra#primingClient() uses localhost by default, so build manually: | |
| 68 | - primingClients.add(PrimingClient.builder() | |
| 69 | - .withHost(ip).withPort(scassandra.getAdminPort()) | |
| 70 | - .build()); | |
| 71 | - activityClients.add(ActivityClient.builder() | |
| 72 | - .withHost(ip).withPort(scassandra.getAdminPort()) | |
| 73 | - .build() | |
| 74 | - ); | |
| 75 | - } | |
| 76 | - primePeers(); | |
| 77 | - } | |
| 78 | - | |
| 79 | - public List<InetAddress> addresses() { | |
| 80 | - return addresses; | |
| 81 | - } | |
| 82 | - | |
| 83 | - public void stop() { | |
| 84 | - for (Scassandra scassandra : scassandras) | |
| 85 | - scassandra.stop(); | |
| 86 | - } | |
| 87 | - | |
| 88 | - public void start(int node) { | |
| 89 | - int i = node - 1; | |
| 90 | - scassandras.get(i).start(); | |
| 91 | - primePeers(primingClients.get(i), scassandras.get(i)); | |
| 92 | - } | |
| 93 | - | |
| 94 | - public void stop(int node) { | |
| 95 | - scassandras.get(node - 1).stop(); | |
| 96 | - } | |
| 97 | - | |
| 98 | - public SCassandraCluster prime(int node, PrimingRequest request) { | |
| 99 | - primingClients.get(node - 1).prime(request); | |
| 100 | - return this; | |
| 101 | - } | |
| 102 | - | |
| 103 | - public List<Query> retrieveQueries(int node) { | |
| 104 | - return activityClients.get(node - 1).retrieveQueries(); | |
| 105 | - } | |
| 106 | - | |
| 107 | - public List<PreparedStatementExecution> retrievePreparedStatementExecutions(int node){ | |
| 108 | - return activityClients.get(node - 1).retrievePreparedStatementExecutions(); | |
| 109 | - } | |
| 110 | - | |
| 111 | - public List<PreparedStatementPreparation> retrievePreparedStatementPreparations(int node) { | |
| 112 | - return activityClients.get(node - 1).retrievePreparedStatementPreparations(); | |
| 113 | - } | |
| 114 | - | |
| 115 | - public void clearAllPrimes() { | |
| 116 | - for (PrimingClient primingClient : primingClients) | |
| 117 | - primingClient.clearAllPrimes(); | |
| 118 | - primePeers(); | |
| 119 | - } | |
| 120 | - | |
| 121 | - public void clearAllRecordedActivity() { | |
| 122 | - for (ActivityClient activityClient : activityClients) | |
| 123 | - activityClient.clearAllRecordedActivity(); | |
| 124 | - } | |
| 125 | - | |
| 126 | - public Scassandra instance(int node) { | |
| 127 | - return scassandras.get(node -1); | |
| 128 | - } | |
| 129 | - | |
| 130 | - private void primePeers() { | |
| 131 | - for (int i = 0; i < scassandras.size(); i++) | |
| 132 | - primePeers(primingClients.get(i), scassandras.get(i)); | |
| 133 | - } | |
| 134 | - | |
| 135 | - @SuppressWarnings("unchecked") | |
| 136 | - private void primePeers(PrimingClient primingClient, Scassandra toIgnore) { | |
| 137 | - List<Map<String, ?>> rows = Lists.newArrayListWithCapacity(scassandras.size()); | |
| 138 | - for (int i = 0; i < scassandras.size(); i++) { | |
| 139 | - if (scassandras.get(i).equals(toIgnore)) | |
| 140 | - continue; | |
| 141 | - InetAddress address = addresses.get(i); | |
| 142 | - Map<String, ?> row = ImmutableMap.<String, Object>builder() | |
| 143 | - .put("peer", address) | |
| 144 | - .put("rpc_address", address) | |
| 145 | - .put("data_center", "datacenter1") | |
| 146 | - .put("rack", "rack1") | |
| 147 | - .put("release_version", "2.0.1") | |
| 148 | - .put("tokens", ImmutableSet.of(Long.toString(Long.MIN_VALUE + i))) | |
| 149 | - .build(); | |
| 150 | - | |
| 151 | - rows.add(row); | |
| 152 | - | |
| 153 | - String query = "SELECT * FROM system.peers WHERE peer='" + address.toString().substring(1) + "'"; | |
| 154 | - primingClient.prime( | |
| 155 | - PrimingRequest.queryBuilder() | |
| 156 | - .withQuery(query) | |
| 157 | - .withColumnTypes(SELECT_PEERS_COLUMN_TYPES) | |
| 158 | - .withRows(row) | |
| 159 | - .build()); | |
| 160 | - } | |
| 161 | - primingClient.prime( | |
| 162 | - PrimingRequest.queryBuilder() | |
| 163 | - .withQuery("SELECT * FROM system.peers") | |
| 164 | - .withColumnTypes(SELECT_PEERS_COLUMN_TYPES) | |
| 165 | - .withRows(rows) | |
| 166 | - .build()); | |
| 167 | - } | |
| 168 | - | |
| 169 | - static final ImmutableMap<String, ColumnTypes> SELECT_PEERS_COLUMN_TYPES = | |
| 170 | - ImmutableMap.<String, ColumnTypes>builder() | |
| 171 | - .put("peer", ColumnTypes.Inet) | |
| 172 | - .put("rpc_address", ColumnTypes.Inet) | |
| 173 | - .put("data_center", ColumnTypes.Text) | |
| 174 | - .put("rack", ColumnTypes.Text) | |
| 175 | - .put("release_version", ColumnTypes.Text) | |
| 176 | - .put("tokens", ColumnTypes.TextSet) | |
| 177 | - .build(); | |
| 178 | -} |
added driver-core/src/test/java/com/datastax/driver/core/ScassandraCluster.java +485 −0
| @@ -0,0 +1,485 @@ | ||
| 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 java.util.List; | |
| 19 | +import java.util.Map; | |
| 20 | +import java.util.TreeSet; | |
| 21 | +import java.util.concurrent.TimeUnit; | |
| 22 | + | |
| 23 | +import com.beust.jcommander.internal.Lists; | |
| 24 | +import com.google.common.collect.ImmutableList; | |
| 25 | +import com.google.common.collect.ImmutableMap; | |
| 26 | +import com.google.common.collect.ImmutableSet; | |
| 27 | +import org.scassandra.Scassandra; | |
| 28 | +import org.scassandra.ScassandraFactory; | |
| 29 | +import org.scassandra.http.client.PrimingClient; | |
| 30 | +import org.scassandra.http.client.PrimingRequest; | |
| 31 | +import org.slf4j.Logger; | |
| 32 | +import org.slf4j.LoggerFactory; | |
| 33 | + | |
| 34 | +import static org.scassandra.cql.MapType.map; | |
| 35 | +import static org.scassandra.cql.PrimitiveType.BIG_INT; | |
| 36 | +import static org.scassandra.cql.PrimitiveType.BOOLEAN; | |
| 37 | +import static org.scassandra.cql.PrimitiveType.DOUBLE; | |
| 38 | +import static org.scassandra.cql.PrimitiveType.INET; | |
| 39 | +import static org.scassandra.cql.PrimitiveType.INT; | |
| 40 | +import static org.scassandra.cql.PrimitiveType.TEXT; | |
| 41 | +import static org.scassandra.cql.PrimitiveType.UUID; | |
| 42 | +import static org.scassandra.cql.SetType.set; | |
| 43 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 44 | +import static org.scassandra.http.client.types.ColumnMetadata.column; | |
| 45 | + | |
| 46 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 47 | + | |
| 48 | +public class ScassandraCluster { | |
| 49 | + | |
| 50 | + private static final Logger logger = LoggerFactory.getLogger(ScassandraCluster.class); | |
| 51 | + | |
| 52 | + private final String ipPrefix; | |
| 53 | + | |
| 54 | + private final List<Scassandra> instances; | |
| 55 | + | |
| 56 | + private final Map<Integer,List<Scassandra>> dcNodeMap; | |
| 57 | + | |
| 58 | + private final List<Map<String,? extends Object>> keyspaceRows; | |
| 59 | + | |
| 60 | + ScassandraCluster(Integer[] nodes, String ipPrefix, int binaryPort, int adminPort, List<Map<String,? extends Object>> keyspaceRows) { | |
| 61 | + this.ipPrefix = ipPrefix; | |
| 62 | + | |
| 63 | + int node = 1; | |
| 64 | + ImmutableList.Builder<Scassandra> instanceListBuilder = ImmutableList.builder(); | |
| 65 | + ImmutableMap.Builder<Integer, List<Scassandra>> dcNodeMapBuilder = ImmutableMap.builder(); | |
| 66 | + for(int dc=0; dc < nodes.length; dc++) { | |
| 67 | + ImmutableList.Builder<Scassandra> dcNodeListBuilder = ImmutableList.builder(); | |
| 68 | + for(int n=0; n < nodes[dc]; n++) { | |
| 69 | + String ip = ipPrefix + node++; | |
| 70 | + Scassandra instance = ScassandraFactory.createServer(ip, binaryPort, ip, adminPort); | |
| 71 | + instanceListBuilder = instanceListBuilder.add(instance); | |
| 72 | + dcNodeListBuilder = dcNodeListBuilder.add(instance); | |
| 73 | + } | |
| 74 | + dcNodeMapBuilder.put(dc+1, dcNodeListBuilder.build()); | |
| 75 | + } | |
| 76 | + instances = instanceListBuilder.build(); | |
| 77 | + dcNodeMap = dcNodeMapBuilder.build(); | |
| 78 | + this.keyspaceRows = keyspaceRows; | |
| 79 | + } | |
| 80 | + | |
| 81 | + public Scassandra node(int node) { | |
| 82 | + return instances.get(node-1); | |
| 83 | + } | |
| 84 | + | |
| 85 | + public List<Scassandra> nodes() { | |
| 86 | + return instances; | |
| 87 | + } | |
| 88 | + | |
| 89 | + public String address(int node) { | |
| 90 | + return ipPrefix + node; | |
| 91 | + } | |
| 92 | + | |
| 93 | + public Scassandra node(int dc, int node) { | |
| 94 | + return dcNodeMap.get(dc).get(node - 1); | |
| 95 | + } | |
| 96 | + | |
| 97 | + public List<Scassandra> nodes(int dc) { | |
| 98 | + return dcNodeMap.get(dc); | |
| 99 | + } | |
| 100 | + | |
| 101 | + public int ipSuffix(int dc, int node) { | |
| 102 | + // TODO: Scassandra should be updated to include address to avoid O(n) lookup. | |
| 103 | + int nodeCount = 0; | |
| 104 | + for(Integer dcNum : new TreeSet<Integer>(dcNodeMap.keySet())) { | |
| 105 | + List<Scassandra> nodesInDc = dcNodeMap.get(dc); | |
| 106 | + for(int n = 0; n < nodesInDc.size(); n++) { | |
| 107 | + nodeCount++; | |
| 108 | + if(dcNum == dc && n+1 == node) { | |
| 109 | + return nodeCount; | |
| 110 | + } | |
| 111 | + } | |
| 112 | + } | |
| 113 | + return -1; | |
| 114 | + } | |
| 115 | + | |
| 116 | + public String address(int dc, int node) { | |
| 117 | + // TODO: Scassandra should be updated to include address to avoid O(n) lookup. | |
| 118 | + int ipSuffix = ipSuffix(dc, node); | |
| 119 | + return ipSuffix == -1 ? null : ipPrefix + ipSuffix; | |
| 120 | + } | |
| 121 | + | |
| 122 | + public Host host(Cluster cluster, int dc, int node) { | |
| 123 | + String address = address(dc, node); | |
| 124 | + for(Host host : cluster.getMetadata().getAllHosts()) { | |
| 125 | + if(host.getAddress().getHostAddress().equals(address)) { | |
| 126 | + return host; | |
| 127 | + } | |
| 128 | + } | |
| 129 | + return null; | |
| 130 | + } | |
| 131 | + | |
| 132 | + public static String datacenter(int dc) { | |
| 133 | + return "DC" + dc; | |
| 134 | + } | |
| 135 | + | |
| 136 | + public void init() { | |
| 137 | + for(Map.Entry<Integer, List<Scassandra>> dc : dcNodeMap.entrySet()) { | |
| 138 | + for(Scassandra node : dc.getValue()) { | |
| 139 | + node.start(); | |
| 140 | + primeMetadata(node); | |
| 141 | + } | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + public void stop() { | |
| 146 | + logger.debug("Stopping ScassandraCluster."); | |
| 147 | + for(Scassandra node : instances) { | |
| 148 | + node.stop(); | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * First stops each node in {@code dc} and then asserts that each node's {@link Host} | |
| 154 | + * is marked down for the given {@link Cluster} instance within 10 seconds. | |
| 155 | + * | |
| 156 | + * If any of the nodes are the control host, this node is stopped last, to reduce | |
| 157 | + * likelihood of control connection choosing a host that will be shut down. | |
| 158 | + * | |
| 159 | + * @param cluster cluster to wait for down statuses on. | |
| 160 | + * @param dc DC to stop. | |
| 161 | + */ | |
| 162 | + public void stopDC(Cluster cluster, int dc) { | |
| 163 | + logger.debug("Stopping all nodes in {}.", datacenter(dc)); | |
| 164 | + // If any node is the control host, stop it last. | |
| 165 | + int controlHost = -1; | |
| 166 | + for(int i = 1; i <= nodes(dc).size(); i++) { | |
| 167 | + int id = ipSuffix(dc, i); | |
| 168 | + Host host = TestUtils.findHost(cluster, id); | |
| 169 | + if (cluster.manager.controlConnection.connectedHost() == host) { | |
| 170 | + logger.debug("Node {} identified as control host. Stopping last.", id); | |
| 171 | + controlHost = id; | |
| 172 | + continue; | |
| 173 | + } | |
| 174 | + stop(cluster, id); | |
| 175 | + } | |
| 176 | + | |
| 177 | + if(controlHost != -1) { | |
| 178 | + stop(cluster, controlHost); | |
| 179 | + } | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Stops a node by id and then asserts that its {@link Host} is marked down | |
| 184 | + * for the given {@link Cluster} instance within 10 seconds. | |
| 185 | + * | |
| 186 | + * @param cluster cluster to wait for down status on. | |
| 187 | + * @param node Node to stop. | |
| 188 | + */ | |
| 189 | + public void stop(Cluster cluster, int node) { | |
| 190 | + logger.debug("Stopping node {}.", node); | |
| 191 | + Scassandra scassandra = node(node); | |
| 192 | + scassandra.stop(); | |
| 193 | + assertThat(cluster).host(node).goesDownWithin(10, TimeUnit.SECONDS); | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * Stops a node by dc and id and then asserts that its {@link Host} is marked down | |
| 198 | + * for the given {@link Cluster} instance within 10 seconds. | |
| 199 | + * | |
| 200 | + * @param cluster cluster to wait for down status on. | |
| 201 | + * @param dc Data center node is in. | |
| 202 | + * @param node Node to stop. | |
| 203 | + */ | |
| 204 | + public void stop(Cluster cluster, int dc, int node) { | |
| 205 | + logger.debug("Stopping node {} in {}.", node, datacenter(dc)); | |
| 206 | + stop(cluster, ipSuffix(dc, node)); | |
| 207 | + } | |
| 208 | + | |
| 209 | + /** | |
| 210 | + * First starts each node in {@code dc} and then asserts that each node's {@link Host} | |
| 211 | + * is marked up for the given {@link Cluster} instance within 10 seconds. | |
| 212 | + * | |
| 213 | + * @param cluster cluster to wait for up statuses on. | |
| 214 | + * @param dc DC to start. | |
| 215 | + */ | |
| 216 | + public void startDC(Cluster cluster, int dc) { | |
| 217 | + logger.debug("Starting all nodes in {}.", datacenter(dc)); | |
| 218 | + for(int i = 1; i <= nodes(dc).size(); i++) { | |
| 219 | + int id = ipSuffix(dc, i); | |
| 220 | + start(cluster, id); | |
| 221 | + } | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Starts a node by id and then asserts that its {@link Host} is marked up | |
| 226 | + * for the given {@link Cluster} instance within 10 seconds. | |
| 227 | + * | |
| 228 | + * @param cluster cluster to wait for up status on. | |
| 229 | + * @param node Node to start. | |
| 230 | + */ | |
| 231 | + public void start(Cluster cluster, int node) { | |
| 232 | + logger.debug("Starting node {}.", node); | |
| 233 | + Scassandra scassandra = node(node); | |
| 234 | + scassandra.start(); | |
| 235 | + assertThat(cluster).host(node).comesUpWithin(10, TimeUnit.SECONDS); | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Starts a node by dc and id and then asserts that its {@link Host} is marked up | |
| 240 | + * for the given {@link Cluster} instance within 10 seconds. | |
| 241 | + * | |
| 242 | + * @param cluster cluster to wait for up status on. | |
| 243 | + * @param dc Data center node is in. | |
| 244 | + * @param node Node to start. | |
| 245 | + */ | |
| 246 | + public void start(Cluster cluster, int dc, int node) { | |
| 247 | + logger.debug("Starting node {} in {}.", node, datacenter(dc)); | |
| 248 | + start(cluster, ipSuffix(dc, node)); | |
| 249 | + } | |
| 250 | + | |
| 251 | + private List<Long> getTokensForDC(int dc) { | |
| 252 | + // Offset DCs by dc * 100 to ensure unique tokens. | |
| 253 | + int offset = (dc-1) * 100; | |
| 254 | + int dcNodeCount = nodes(dc).size(); | |
| 255 | + List<Long> tokens = Lists.newArrayList(dcNodeCount); | |
| 256 | + for(int i = 0; i < dcNodeCount; i++) { | |
| 257 | + tokens.add((i * ((long)Math.pow(2, 64)/dcNodeCount) + offset)); | |
| 258 | + } | |
| 259 | + return tokens; | |
| 260 | + } | |
| 261 | + | |
| 262 | + private void primeMetadata(Scassandra node) { | |
| 263 | + PrimingClient client = node.primingClient(); | |
| 264 | + int nodeCount = 0; | |
| 265 | + | |
| 266 | + ImmutableList.Builder<Map<String,?>> rows = ImmutableList.builder(); | |
| 267 | + for(Integer dc : new TreeSet<Integer>(dcNodeMap.keySet())) { | |
| 268 | + List<Scassandra> nodesInDc = dcNodeMap.get(dc); | |
| 269 | + List<Long> tokens = getTokensForDC(dc); | |
| 270 | + for(int n = 0; n < nodesInDc.size(); n++) { | |
| 271 | + String address = ipPrefix + ++nodeCount; | |
| 272 | + Scassandra peer = nodesInDc.get(n); | |
| 273 | + String query; | |
| 274 | + Map<String,Object> row; | |
| 275 | + org.scassandra.http.client.types.ColumnMetadata[] metadata; | |
| 276 | + if(node == peer) { // prime system.local. | |
| 277 | + metadata = SELECT_LOCAL; | |
| 278 | + query = "SELECT * FROM system.local WHERE key='local'"; | |
| 279 | + row = ImmutableMap.<String, Object>builder() | |
| 280 | + .put("key", "local") | |
| 281 | + .put("bootstrapped", "COMPLETED") | |
| 282 | + .put("broadcast_address", address) | |
| 283 | + .put("cluster_name", "scassandra") | |
| 284 | + .put("cql_version", "3.2.0") | |
| 285 | + .put("data_center", datacenter(dc)) | |
| 286 | + .put("listen_address", address) | |
| 287 | + .put("partitioner", "org.apache.cassandra.dht.Murmur3Partitioner") | |
| 288 | + .put("rack", "rack1") | |
| 289 | + .put("release_version", "2.1.8") | |
| 290 | + .put("tokens", ImmutableSet.of(tokens.get(n))) | |
| 291 | + .build(); | |
| 292 | + } else { // prime system.peers. | |
| 293 | + query = "SELECT * FROM system.peers WHERE peer='" + address + "'"; | |
| 294 | + metadata = SELECT_PEERS; | |
| 295 | + row = ImmutableMap.<String, Object>builder() | |
| 296 | + .put("peer", address) | |
| 297 | + .put("rpc_address", address) | |
| 298 | + .put("data_center", datacenter(dc)) | |
| 299 | + .put("rack", "rack1") | |
| 300 | + .put("release_version", "2.1.8") | |
| 301 | + .put("tokens", ImmutableSet.of(Long.toString(tokens.get(n)))) | |
| 302 | + .build(); | |
| 303 | + rows.add(row); | |
| 304 | + } | |
| 305 | + client.prime(PrimingRequest.queryBuilder() | |
| 306 | + .withQuery(query) | |
| 307 | + .withThen(then() | |
| 308 | + .withColumnTypes(metadata) | |
| 309 | + .withRows(row) | |
| 310 | + .build()) | |
| 311 | + .build()); | |
| 312 | + } | |
| 313 | + } | |
| 314 | + | |
| 315 | + client.prime(PrimingRequest.queryBuilder() | |
| 316 | + .withQuery("SELECT * FROM system.peers") | |
| 317 | + .withThen(then() | |
| 318 | + .withColumnTypes(SELECT_PEERS) | |
| 319 | + .withRows(rows.build()) | |
| 320 | + .build()) | |
| 321 | + .build()); | |
| 322 | + | |
| 323 | + // Needed to ensure cluster_name matches what we expect on connection. | |
| 324 | + Map<String,Object> clusterNameRow = ImmutableMap.<String,Object>builder() | |
| 325 | + .put("cluster_name", "scassandra") | |
| 326 | + .build(); | |
| 327 | + client.prime(PrimingRequest.queryBuilder() | |
| 328 | + .withQuery("select cluster_name from system.local") | |
| 329 | + .withThen(then() | |
| 330 | + .withColumnTypes(SELECT_CLUSTER_NAME) | |
| 331 | + .withRows(clusterNameRow) | |
| 332 | + .build()) | |
| 333 | + .build()); | |
| 334 | + | |
| 335 | + | |
| 336 | + // Prime keyspaces | |
| 337 | + client.prime(PrimingRequest.queryBuilder() | |
| 338 | + .withQuery("SELECT * FROM system.schema_keyspaces") | |
| 339 | + .withThen(then() | |
| 340 | + .withColumnTypes(SELECT_SCHEMA_KEYSPACES) | |
| 341 | + .withRows(keyspaceRows) | |
| 342 | + .build()) | |
| 343 | + .build()); | |
| 344 | + } | |
| 345 | + | |
| 346 | + static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_PEERS = { | |
| 347 | + column("peer", INET), | |
| 348 | + column("rpc_address", INET), | |
| 349 | + column("data_center", TEXT), | |
| 350 | + column("rack", TEXT), | |
| 351 | + column("release_version", TEXT), | |
| 352 | + column("tokens", set(TEXT)) | |
| 353 | + }; | |
| 354 | + | |
| 355 | + static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_LOCAL = { | |
| 356 | + column("key", TEXT), | |
| 357 | + column("bootstrapped", TEXT), | |
| 358 | + column("broadcast_address", INET), | |
| 359 | + column("cluster_name", TEXT), | |
| 360 | + column("cql_version", TEXT), | |
| 361 | + column("data_center", TEXT), | |
| 362 | + column("listen_address", INET), | |
| 363 | + column("partitioner", TEXT), | |
| 364 | + column("rack", TEXT), | |
| 365 | + column("release_version", TEXT), | |
| 366 | + column("tokens", set(TEXT)), | |
| 367 | + }; | |
| 368 | + | |
| 369 | + static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_CLUSTER_NAME = { | |
| 370 | + column("cluster_name", TEXT) | |
| 371 | + }; | |
| 372 | + | |
| 373 | + static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_SCHEMA_KEYSPACES = { | |
| 374 | + column("durable_writes", BOOLEAN), | |
| 375 | + column("keyspace_name", TEXT), | |
| 376 | + column("strategy_class", TEXT), | |
| 377 | + column("strategy_options", TEXT) | |
| 378 | + }; | |
| 379 | + | |
| 380 | + static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_SCHEMA_COLUMN_FAMILIES = { | |
| 381 | + column("bloom_filter_fp_chance", DOUBLE), | |
| 382 | + column("caching", TEXT), | |
| 383 | + column("cf_id", UUID), | |
| 384 | + column("column_aliases", TEXT), | |
| 385 | + column("columnfamily_name", TEXT), | |
| 386 | + column("comment", TEXT), | |
| 387 | + column("compaction_strategy_class", TEXT), | |
| 388 | + column("compaction_strategy_options", TEXT), | |
| 389 | + column("comparator", TEXT), | |
| 390 | + column("compression_parameters", TEXT), | |
| 391 | + column("default_time_to_live", INT), | |
| 392 | + column("default_validator", TEXT), | |
| 393 | + column("dropped_columns", map(TEXT, BIG_INT)), | |
| 394 | + column("gc_grace_seconds", INT), | |
| 395 | + column("index_interval", INT), | |
| 396 | + column("is_dense", BOOLEAN), | |
| 397 | + column("key_aliases", TEXT), | |
| 398 | + column("key_validator", TEXT), | |
| 399 | + column("keyspace_name", TEXT), | |
| 400 | + column("local_read_repair_chance", DOUBLE), | |
| 401 | + column("max_compaction_threshold", INT), | |
| 402 | + column("max_index_interval", INT), | |
| 403 | + column("memtable_flush_period_in_ms", INT), | |
| 404 | + column("min_compaction_threshold", INT), | |
| 405 | + column("min_index_interval", INT), | |
| 406 | + column("read_repair_chance", DOUBLE), | |
| 407 | + column("speculative_retry", TEXT), | |
| 408 | + column("subcomparator", TEXT), | |
| 409 | + column("type", TEXT), | |
| 410 | + column("value_alias", TEXT) | |
| 411 | + }; | |
| 412 | + | |
| 413 | + static final org.scassandra.http.client.types.ColumnMetadata[] SELECT_SCHEMA_COLUMNS = { | |
| 414 | + column("column_name", TEXT), | |
| 415 | + column("columnfamily_name", TEXT), | |
| 416 | + column("component_index", INT), | |
| 417 | + column("index_name", TEXT), | |
| 418 | + column("index_options", TEXT), | |
| 419 | + column("index_type", TEXT), | |
| 420 | + column("keyspace_name", TEXT), | |
| 421 | + column("type", TEXT), | |
| 422 | + column("validator", TEXT), | |
| 423 | + }; | |
| 424 | + | |
| 425 | + public static ScassandraClusterBuilder builder() { | |
| 426 | + return new ScassandraClusterBuilder(); | |
| 427 | + } | |
| 428 | + | |
| 429 | + public static class ScassandraClusterBuilder { | |
| 430 | + | |
| 431 | + private Integer nodes[] = {1}; | |
| 432 | + private String ipPrefix = CCMBridge.IP_PREFIX; | |
| 433 | + private final List<Map<String,? extends Object>> keyspaceRows = Lists.newArrayList(); | |
| 434 | + | |
| 435 | + | |
| 436 | + public ScassandraClusterBuilder withNodes(Integer ... nodes) { | |
| 437 | + this.nodes = nodes; | |
| 438 | + return this; | |
| 439 | + } | |
| 440 | + | |
| 441 | + public ScassandraClusterBuilder withIpPrefix(String ipPrefix) { | |
| 442 | + this.ipPrefix = ipPrefix; | |
| 443 | + return this; | |
| 444 | + } | |
| 445 | + | |
| 446 | + public ScassandraClusterBuilder withSimpleKeyspace(String name, int replicationFactor) { | |
| 447 | + Map<String,Object> simpleKeyspaceRow = ImmutableMap.<String,Object>builder() | |
| 448 | + .put("durable_writes", false) | |
| 449 | + .put("keyspace_name", name) | |
| 450 | + .put("strategy_class", "SimpleStrategy") | |
| 451 | + .put("strategy_options", "{\"replication_factor\":\"" + replicationFactor + "\"}") | |
| 452 | + .build(); | |
| 453 | + | |
| 454 | + keyspaceRows.add(simpleKeyspaceRow); | |
| 455 | + return this; | |
| 456 | + } | |
| 457 | + | |
| 458 | + public ScassandraClusterBuilder withNetworkTopologyKeyspace(String name, Map<Integer,Integer> replicationFactors) { | |
| 459 | + StringBuilder strategyOptionsBuilder = new StringBuilder("{"); | |
| 460 | + for(Map.Entry<Integer,Integer> dc : replicationFactors.entrySet()) { | |
| 461 | + strategyOptionsBuilder.append("\""); | |
| 462 | + strategyOptionsBuilder.append(datacenter(dc.getKey())); | |
| 463 | + strategyOptionsBuilder.append("\":\""); | |
| 464 | + strategyOptionsBuilder.append(dc.getValue()); | |
| 465 | + strategyOptionsBuilder.append("\","); | |
| 466 | + } | |
| 467 | + | |
| 468 | + String strategyOptions = strategyOptionsBuilder.substring(0, strategyOptionsBuilder.length() - 1) + "}"; | |
| 469 | + | |
| 470 | + Map<String,Object> ntsKeyspaceRow = ImmutableMap.<String,Object>builder() | |
| 471 | + .put("durable_writes", false) | |
| 472 | + .put("keyspace_name", name) | |
| 473 | + .put("strategy_class", "NetworkTopologyStrategy") | |
| 474 | + .put("strategy_options", strategyOptions) | |
| 475 | + .build(); | |
| 476 | + | |
| 477 | + keyspaceRows.add(ntsKeyspaceRow); | |
| 478 | + return this; | |
| 479 | + } | |
| 480 | + | |
| 481 | + public ScassandraCluster build() { | |
| 482 | + return new ScassandraCluster(nodes, ipPrefix, 9042, 9052, keyspaceRows); | |
| 483 | + } | |
| 484 | + } | |
| 485 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/SpeculativeExecutionTest.java +34 −31
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import com.datastax.driver.core.policies.SpeculativeExecutionPolicy; |
| 21 | 21 | import com.google.common.collect.ImmutableList; |
| 22 | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | +import org.scassandra.Scassandra; | |
| 23 | 24 | import org.scassandra.http.client.PrimingRequest; |
| 24 | 25 | import org.testng.annotations.*; |
| 25 | 26 | |
| @@ -27,12 +28,14 @@ | ||
| 27 | 28 | import com.datastax.driver.core.policies.RetryPolicy; |
| 28 | 29 | |
| 29 | 30 | import static com.datastax.driver.core.Assertions.assertThat; |
| 31 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 32 | + | |
| 30 | 33 | import static org.mockito.Mockito.mock; |
| 31 | 34 | import static org.mockito.Mockito.times; |
| 32 | 35 | import static org.mockito.Mockito.verify; |
| 33 | 36 | |
| 34 | 37 | public class SpeculativeExecutionTest { |
| 35 | - SCassandraCluster scassandras; | |
| 38 | + ScassandraCluster scassandras; | |
| 36 | 39 | |
| 37 | 40 | Cluster cluster = null; |
| 38 | 41 | SortingLoadBalancingPolicy loadBalancingPolicy; |
| @@ -40,22 +43,21 @@ | ||
| 40 | 43 | Host host1, host2, host3; |
| 41 | 44 | Session session; |
| 42 | 45 | |
| 43 | - @BeforeClass(groups = "short") | |
| 44 | - public void beforeClass() { | |
| 45 | - scassandras = new SCassandraCluster(CCMBridge.IP_PREFIX, 3); | |
| 46 | - } | |
| 47 | - | |
| 48 | 46 | @BeforeMethod(groups = "short") |
| 49 | 47 | public void beforeMethod() { |
| 48 | + scassandras = ScassandraCluster.builder().withNodes(3).build(); | |
| 49 | + scassandras.init(); | |
| 50 | + | |
| 50 | 51 | int speculativeExecutionDelay = 200; |
| 51 | 52 | |
| 52 | 53 | loadBalancingPolicy = new SortingLoadBalancingPolicy(); |
| 53 | 54 | cluster = Cluster.builder() |
| 54 | - .addContactPoint(CCMBridge.ipOfNode(2)) | |
| 55 | + .addContactPoint(scassandras.address(2)) | |
| 55 | 56 | .withLoadBalancingPolicy(loadBalancingPolicy) |
| 56 | 57 | .withSpeculativeExecutionPolicy(new ConstantSpeculativeExecutionPolicy(speculativeExecutionDelay, 1)) |
| 57 | 58 | .withQueryOptions(new QueryOptions().setDefaultIdempotence(true)) |
| 58 | 59 | .withRetryPolicy(new CustomRetryPolicy()) |
| 60 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 59 | 61 | .build(); |
| 60 | 62 | |
| 61 | 63 | session = cluster.connect(); |
| @@ -67,9 +69,17 @@ | ||
| 67 | 69 | errors = cluster.getMetrics().getErrorMetrics(); |
| 68 | 70 | } |
| 69 | 71 | |
| 72 | + @AfterMethod(groups = "short") | |
| 73 | + public void afterMethod() { | |
| 74 | + if (cluster != null) | |
| 75 | + cluster.close(); | |
| 76 | + if (scassandras != null) | |
| 77 | + scassandras.stop(); | |
| 78 | + } | |
| 79 | + | |
| 70 | 80 | @Test(groups = "short") |
| 71 | 81 | public void should_not_start_speculative_execution_if_first_execution_completes_successfully() { |
| 72 | - scassandras.prime(1, PrimingRequest.queryBuilder() | |
| 82 | + scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 73 | 83 | .withQuery("mock query") |
| 74 | 84 | .withRows(row("result", "result1")) |
| 75 | 85 | .build() |
| @@ -88,12 +98,14 @@ | ||
| 88 | 98 | @Test(groups = "short") |
| 89 | 99 | public void should_not_start_speculative_execution_if_first_execution_retries_but_is_still_fast_enough() { |
| 90 | 100 | // will retry once on this node: |
| 91 | - scassandras.prime(1, PrimingRequest.queryBuilder() | |
| 101 | + scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 92 | 102 | .withQuery("mock query") |
| 93 | 103 | .withConsistency(PrimingRequest.Consistency.TWO) |
| 94 | 104 | .withResult(PrimingRequest.Result.read_request_timeout) |
| 95 | 105 | .build() |
| 96 | - ).prime(1, PrimingRequest.queryBuilder() | |
| 106 | + ); | |
| 107 | + | |
| 108 | + scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 97 | 109 | .withQuery("mock query") |
| 98 | 110 | .withConsistency(PrimingRequest.Consistency.ONE) |
| 99 | 111 | .withRows(row("result", "result1")) |
| @@ -116,12 +128,14 @@ | ||
| 116 | 128 | |
| 117 | 129 | @Test(groups = "short") |
| 118 | 130 | public void should_start_speculative_execution_if_first_execution_takes_too_long() { |
| 119 | - scassandras.prime(1, PrimingRequest.queryBuilder() | |
| 131 | + scassandras.node(1).primingClient().prime(PrimingRequest.queryBuilder() | |
| 120 | 132 | .withQuery("mock query") |
| 121 | 133 | .withFixedDelay(400) |
| 122 | 134 | .withRows(row("result", "result1")) |
| 123 | 135 | .build() |
| 124 | - ).prime(2, PrimingRequest.queryBuilder() | |
| 136 | + ); | |
| 137 | + | |
| 138 | + scassandras.node(2).primingClient().prime(PrimingRequest.queryBuilder() | |
| 125 | 139 | .withQuery("mock query") |
| 126 | 140 | .withRows(row("result", "result2")) |
| 127 | 141 | .build() |
| @@ -141,23 +155,25 @@ | ||
| 141 | 155 | // Rely on read timeouts to trigger errors that cause an execution to move to the next node |
| 142 | 156 | cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(1000); |
| 143 | 157 | |
| 144 | - scassandras | |
| 158 | + scassandras.node(1).primingClient() | |
| 145 | 159 | // execution1 starts with host1, which will time out at t=1000 |
| 146 | - .prime(1, PrimingRequest.queryBuilder() | |
| 160 | + .prime(PrimingRequest.queryBuilder() | |
| 147 | 161 | .withQuery("mock query") |
| 148 | 162 | .withFixedDelay(2000) |
| 149 | 163 | .withRows(row("result", "result1")) |
| 150 | - .build()) | |
| 164 | + .build()); | |
| 151 | 165 | // at t=1000, execution1 moves to host3, which eventually succeeds at t=1500 |
| 152 | - .prime(3, PrimingRequest.queryBuilder() | |
| 166 | + scassandras.node(3).primingClient() | |
| 167 | + .prime(PrimingRequest.queryBuilder() | |
| 153 | 168 | .withQuery("mock query") |
| 154 | 169 | .withFixedDelay(500) |
| 155 | 170 | .withRows(row("result", "result3")) |
| 156 | - .build()) | |
| 171 | + .build()); | |
| 157 | 172 | // meanwhile, execution2 starts at t=200, using host2 which times out at t=1200 |
| 158 | 173 | // at that time, the query plan is empty so execution2 fails |
| 159 | 174 | // The goal of this test is to check that execution2 does not fail the query, since execution1 is still running |
| 160 | - .prime(2, PrimingRequest.queryBuilder() | |
| 175 | + scassandras.node(2).primingClient() | |
| 176 | + .prime(PrimingRequest.queryBuilder() | |
| 161 | 177 | .withQuery("mock query") |
| 162 | 178 | .withFixedDelay(2000) |
| 163 | 179 | .withRows(row("result", "result2")) |
| @@ -202,19 +218,6 @@ | ||
| 202 | 218 | } |
| 203 | 219 | } |
| 204 | 220 | |
| 205 | - @AfterMethod(groups = "short") | |
| 206 | - public void afterMethod() { | |
| 207 | - scassandras.clearAllPrimes(); | |
| 208 | - if (cluster != null) | |
| 209 | - cluster.close(); | |
| 210 | - } | |
| 211 | - | |
| 212 | - @AfterClass(groups = "short") | |
| 213 | - public void afterClass() { | |
| 214 | - if (scassandras != null) | |
| 215 | - scassandras.stop(); | |
| 216 | - } | |
| 217 | - | |
| 218 | 221 | /** |
| 219 | 222 | * Custom retry policy that retries at ONE on read timeout. |
| 220 | 223 | * This deals with the fact that Scassandra only allows read timeouts with 0 replicas. |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +14 −0
| @@ -27,6 +27,7 @@ | ||
| 27 | 27 | import java.util.concurrent.TimeUnit; |
| 28 | 28 | |
| 29 | 29 | import com.google.common.util.concurrent.Futures; |
| 30 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 30 | 31 | import io.netty.channel.EventLoopGroup; |
| 31 | 32 | import org.scassandra.Scassandra; |
| 32 | 33 | import org.scassandra.ScassandraFactory; |
| @@ -432,6 +433,19 @@ | ||
| 432 | 433 | return null; |
| 433 | 434 | } |
| 434 | 435 | |
| 436 | + public static Host findOrWaitForControlConnection(final Cluster cluster, long duration, TimeUnit unit) { | |
| 437 | + ControlConnection controlConnection = cluster.manager.controlConnection; | |
| 438 | + long durationNs = TimeUnit.NANOSECONDS.convert(duration, unit); | |
| 439 | + long start = System.nanoTime(); | |
| 440 | + while(System.nanoTime() - start < durationNs) { | |
| 441 | + if(controlConnection.isOpen()) { | |
| 442 | + return controlConnection.connectedHost(); | |
| 443 | + } | |
| 444 | + Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS); | |
| 445 | + } | |
| 446 | + return null; | |
| 447 | + } | |
| 448 | + | |
| 435 | 449 | public static HostConnectionPool poolOf(Session session, int hostNumber) { |
| 436 | 450 | SessionManager sessionManager = (SessionManager)session; |
| 437 | 451 | return sessionManager.pools.get(findHost(session.getCluster(), hostNumber)); |
modified driver-core/src/test/java/com/datastax/driver/core/exceptions/ExceptionsScassandraTest.java +16 −10
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | |
| 21 | 21 | import com.google.common.collect.ImmutableList; |
| 22 | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | +import org.scassandra.Scassandra; | |
| 23 | 24 | import org.scassandra.http.client.PrimingRequest; |
| 24 | 25 | import org.testng.annotations.*; |
| 25 | 26 | |
| @@ -36,7 +37,7 @@ | ||
| 36 | 37 | |
| 37 | 38 | public class ExceptionsScassandraTest { |
| 38 | 39 | |
| 39 | - protected SCassandraCluster scassandras; | |
| 40 | + protected ScassandraCluster scassandras; | |
| 40 | 41 | protected Cluster cluster; |
| 41 | 42 | protected Metrics.Errors errors; |
| 42 | 43 | protected Host host1; |
| @@ -44,7 +45,8 @@ | ||
| 44 | 45 | |
| 45 | 46 | @BeforeClass(groups = "short") |
| 46 | 47 | public void beforeClass() { |
| 47 | - scassandras = new SCassandraCluster(CCMBridge.IP_PREFIX, 1); | |
| 48 | + scassandras = ScassandraCluster.builder().withNodes(1).build(); | |
| 49 | + scassandras.init(); | |
| 48 | 50 | } |
| 49 | 51 | |
| 50 | 52 | @BeforeMethod(groups = "short") |
| @@ -56,8 +58,11 @@ | ||
| 56 | 58 | session = cluster.connect(); |
| 57 | 59 | host1 = TestUtils.findHost(cluster, 1); |
| 58 | 60 | errors = cluster.getMetrics().getErrorMetrics(); |
| 59 | - scassandras.clearAllPrimes(); | |
| 60 | - scassandras.clearAllRecordedActivity(); | |
| 61 | + | |
| 62 | + for(Scassandra node : scassandras.nodes()) { | |
| 63 | + node.primingClient().clearAllPrimes(); | |
| 64 | + node.activityClient().clearAllRecordedActivity(); | |
| 65 | + } | |
| 61 | 66 | } |
| 62 | 67 | |
| 63 | 68 | @Test(groups = "short") |
| @@ -110,11 +115,10 @@ | ||
| 110 | 115 | } |
| 111 | 116 | |
| 112 | 117 | protected void simulateError(int hostNumber, PrimingRequest.Result result) { |
| 113 | - scassandras | |
| 114 | - .prime(hostNumber, PrimingRequest.queryBuilder() | |
| 115 | - .withQuery("mock query") | |
| 116 | - .withResult(result) | |
| 117 | - .build()); | |
| 118 | + scassandras.node(hostNumber).primingClient().prime(PrimingRequest.queryBuilder() | |
| 119 | + .withQuery("mock query") | |
| 120 | + .withResult(result) | |
| 121 | + .build()); | |
| 118 | 122 | } |
| 119 | 123 | |
| 120 | 124 | private static List<Map<String, ?>> row(String key, String value) { |
| @@ -131,7 +135,9 @@ | ||
| 131 | 135 | |
| 132 | 136 | @AfterMethod(groups = "short") |
| 133 | 137 | public void afterMethod() { |
| 134 | - scassandras.clearAllPrimes(); | |
| 138 | + for(Scassandra node : scassandras.nodes()) { | |
| 139 | + node.primingClient().clearAllPrimes(); | |
| 140 | + } | |
| 135 | 141 | if (cluster != null) |
| 136 | 142 | cluster.close(); |
| 137 | 143 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/AbstractRetryPolicyIntegrationTest.java +18 −21
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | import com.google.common.collect.ImmutableList; |
| 22 | 22 | import com.google.common.collect.ImmutableMap; |
| 23 | 23 | import org.mockito.Mockito; |
| 24 | +import org.scassandra.Scassandra; | |
| 24 | 25 | import org.scassandra.http.client.PrimingRequest; |
| 25 | 26 | import org.testng.annotations.AfterClass; |
| 26 | 27 | import org.testng.annotations.AfterMethod; |
| @@ -35,6 +36,8 @@ | ||
| 35 | 36 | |
| 36 | 37 | import com.datastax.driver.core.*; |
| 37 | 38 | |
| 39 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 40 | + | |
| 38 | 41 | /** |
| 39 | 42 | * Base class for retry policy integration tests. |
| 40 | 43 | * |
| @@ -46,7 +49,7 @@ | ||
| 46 | 49 | * tests in child classes. |
| 47 | 50 | */ |
| 48 | 51 | public class AbstractRetryPolicyIntegrationTest { |
| 49 | - protected SCassandraCluster scassandras; | |
| 52 | + protected ScassandraCluster scassandras; | |
| 50 | 53 | protected Cluster cluster = null; |
| 51 | 54 | protected Metrics.Errors errors; |
| 52 | 55 | protected Host host1, host2, host3; |
| @@ -58,17 +61,16 @@ | ||
| 58 | 61 | this.retryPolicy = Mockito.spy(retryPolicy); |
| 59 | 62 | } |
| 60 | 63 | |
| 61 | - @BeforeClass(groups = "short") | |
| 62 | - public void beforeClass() { | |
| 63 | - scassandras = new SCassandraCluster(CCMBridge.IP_PREFIX, 3); | |
| 64 | - } | |
| 65 | - | |
| 66 | 64 | @BeforeMethod(groups = "short") |
| 67 | 65 | public void beforeMethod() { |
| 66 | + scassandras = ScassandraCluster.builder().withNodes(3).build(); | |
| 67 | + scassandras.init(); | |
| 68 | + | |
| 68 | 69 | cluster = Cluster.builder() |
| 69 | 70 | .addContactPoint(CCMBridge.ipOfNode(1)) |
| 70 | 71 | .withRetryPolicy(retryPolicy) |
| 71 | 72 | .withLoadBalancingPolicy(new SortingLoadBalancingPolicy()) |
| 73 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 72 | 74 | .build(); |
| 73 | 75 | |
| 74 | 76 | session = cluster.connect(); |
| @@ -80,21 +82,21 @@ | ||
| 80 | 82 | errors = cluster.getMetrics().getErrorMetrics(); |
| 81 | 83 | |
| 82 | 84 | Mockito.reset(retryPolicy); |
| 83 | - scassandras.clearAllPrimes(); | |
| 84 | - scassandras.clearAllRecordedActivity(); | |
| 85 | + | |
| 86 | + for(Scassandra node : scassandras.nodes()) { | |
| 87 | + node.activityClient().clearAllRecordedActivity(); | |
| 88 | + } | |
| 85 | 89 | } |
| 86 | 90 | |
| 87 | 91 | protected void simulateError(int hostNumber, PrimingRequest.Result result) { |
| 88 | - scassandras | |
| 89 | - .prime(hostNumber, PrimingRequest.queryBuilder() | |
| 90 | - .withQuery("mock query") | |
| 91 | - .withResult(result) | |
| 92 | - .build()); | |
| 92 | + scassandras.node(hostNumber).primingClient().prime(PrimingRequest.queryBuilder() | |
| 93 | + .withQuery("mock query") | |
| 94 | + .withResult(result) | |
| 95 | + .build()); | |
| 93 | 96 | } |
| 94 | 97 | |
| 95 | 98 | protected void simulateNormalResponse(int hostNumber) { |
| 96 | - scassandras | |
| 97 | - .prime(hostNumber, PrimingRequest.queryBuilder() | |
| 99 | + scassandras.node(hostNumber).primingClient().prime(PrimingRequest.queryBuilder() | |
| 98 | 100 | .withQuery("mock query") |
| 99 | 101 | .withRows(row("result", "result1")) |
| 100 | 102 | .build()); |
| @@ -129,18 +131,13 @@ | ||
| 129 | 131 | } |
| 130 | 132 | |
| 131 | 133 | protected void assertQueried(int hostNumber, int times) { |
| 132 | - assertThat(scassandras.retrieveQueries(hostNumber)).hasSize(times); | |
| 134 | + assertThat(scassandras.node(hostNumber).activityClient().retrieveQueries()).hasSize(times); | |
| 133 | 135 | } |
| 134 | 136 | |
| 135 | 137 | @AfterMethod(groups = "short") |
| 136 | 138 | public void afterMethod() { |
| 137 | - scassandras.clearAllPrimes(); | |
| 138 | 139 | if (cluster != null) |
| 139 | 140 | cluster.close(); |
| 140 | - } | |
| 141 | - | |
| 142 | - @AfterClass(groups = "short") | |
| 143 | - public void afterClass() { | |
| 144 | 141 | if (scassandras != null) |
| 145 | 142 | scassandras.stop(); |
| 146 | 143 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicyTest.java +451 −105
| @@ -16,183 +16,529 @@ | ||
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | 18 | import java.util.Collection; |
| 19 | -import java.util.Set; | |
| 20 | -import java.util.concurrent.CopyOnWriteArraySet; | |
| 21 | 19 | |
| 20 | +import com.google.common.collect.Lists; | |
| 22 | 21 | import org.apache.log4j.Level; |
| 23 | 22 | import org.apache.log4j.Logger; |
| 24 | -import org.testng.annotations.*; | |
| 25 | - | |
| 26 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | -import static org.testng.Assert.assertEquals; | |
| 28 | - | |
| 29 | -import com.datastax.driver.core.*; | |
| 23 | +import org.mockito.ArgumentCaptor; | |
| 24 | +import org.mockito.Captor; | |
| 25 | +import org.mockito.Mockito; | |
| 26 | +import org.testng.annotations.AfterMethod; | |
| 27 | +import org.testng.annotations.BeforeMethod; | |
| 28 | +import org.testng.annotations.Test; | |
| 29 | + | |
| 30 | +import static com.google.common.collect.Lists.newArrayList; | |
| 31 | +import static org.mockito.Matchers.any; | |
| 32 | +import static org.mockito.Mockito.spy; | |
| 33 | +import static org.mockito.MockitoAnnotations.initMocks; | |
| 34 | + | |
| 35 | +import com.datastax.driver.core.Cluster; | |
| 36 | +import com.datastax.driver.core.ConsistencyLevel; | |
| 37 | +import com.datastax.driver.core.DataProviders; | |
| 38 | +import com.datastax.driver.core.Host; | |
| 39 | +import com.datastax.driver.core.MemoryAppender; | |
| 40 | +import com.datastax.driver.core.QueryTracker; | |
| 41 | +import com.datastax.driver.core.ScassandraCluster; | |
| 42 | +import com.datastax.driver.core.Session; | |
| 43 | +import com.datastax.driver.core.SocketOptions; | |
| 44 | +import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 45 | + | |
| 46 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 47 | +import static com.datastax.driver.core.ScassandraCluster.datacenter; | |
| 48 | +import static com.datastax.driver.core.TestUtils.findHost; | |
| 49 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 30 | 50 | |
| 31 | 51 | public class DCAwareRoundRobinPolicyTest { |
| 52 | + | |
| 32 | 53 | Logger policyLogger = Logger.getLogger(DCAwareRoundRobinPolicy.class); |
| 33 | 54 | MemoryAppender logs; |
| 34 | - CCMBridge ccm; | |
| 35 | - | |
| 36 | - @BeforeClass(groups = "short") | |
| 37 | - public void createCcm() { | |
| 38 | - // Two-DC cluster with one host in each DC | |
| 39 | - ccm = CCMBridge.builder("test").withNodes(1, 1).build(); | |
| 40 | - } | |
| 55 | + QueryTracker queryTracker; | |
| 41 | 56 | |
| 42 | - @AfterClass(groups = "short") | |
| 43 | - public void deleteCcm() { | |
| 44 | - if (ccm != null) | |
| 45 | - ccm.remove(); | |
| 46 | - } | |
| 57 | + @Captor | |
| 58 | + ArgumentCaptor<Collection<Host>> initHostsCaptor; | |
| 47 | 59 | |
| 48 | 60 | @BeforeMethod(groups = "short") |
| 49 | - public void startRecordingLogs() { | |
| 61 | + public void setUp() { | |
| 62 | + initMocks(this); | |
| 50 | 63 | policyLogger.setLevel(Level.WARN); |
| 51 | 64 | logs = new MemoryAppender(); |
| 52 | 65 | policyLogger.addAppender(logs); |
| 66 | + queryTracker = new QueryTracker(); | |
| 53 | 67 | } |
| 54 | 68 | |
| 55 | 69 | @AfterMethod(groups = "short") |
| 56 | - public void stopRecordingLogs() { | |
| 70 | + public void tearDown() { | |
| 57 | 71 | policyLogger.setLevel(null); |
| 58 | 72 | policyLogger.removeAppender(logs); |
| 59 | 73 | } |
| 60 | 74 | |
| 75 | + private Cluster.Builder builder() { | |
| 76 | + return Cluster.builder() | |
| 77 | + // Close cluster immediately to speed up tests. | |
| 78 | + .withNettyOptions(nonQuietClusterCloseOptions); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * Ensures that {@link DCAwareRoundRobinPolicy} will round robin within hosts in the explicitly specific local DC | |
| 83 | + * via {@link DCAwareRoundRobinPolicy.Builder#withLocalDc(String)} | |
| 84 | + * | |
| 85 | + * @test_category load_balancing:dc_aware | |
| 86 | + */ | |
| 61 | 87 | @Test(groups = "short") |
| 62 | - public void should_use_local_dc_from_contact_points_when_not_explicitly_specified() { | |
| 63 | - Cluster cluster = null; | |
| 64 | - CountingDCAwarePolicy policy = new CountingDCAwarePolicy(); | |
| 88 | + public void should_round_robin_within_local_dc() { | |
| 89 | + // given: a 10 node 2 DC cluster. | |
| 90 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5, 5).build(); | |
| 91 | + Cluster cluster = builder() | |
| 92 | + .addContactPoint(sCluster.address(1, 1)) | |
| 93 | + .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(datacenter(1)).build()) | |
| 94 | + .build(); | |
| 95 | + try { | |
| 96 | + sCluster.init(); | |
| 97 | + | |
| 98 | + Session session = cluster.connect(); | |
| 99 | + // when: a query is executed 50 times. | |
| 100 | + queryTracker.query(session, 50); | |
| 101 | + | |
| 102 | + // then: each node in local DC should get an equal (10) number of requests. | |
| 103 | + // then: no node in the remote DC should get a request. | |
| 104 | + for(int i = 1; i <= 5; i++) { | |
| 105 | + queryTracker.assertQueried(sCluster, 1, i, 10); | |
| 106 | + queryTracker.assertQueried(sCluster, 2, i, 0); | |
| 107 | + } | |
| 108 | + } finally { | |
| 109 | + cluster.close(); | |
| 110 | + sCluster.stop(); | |
| 111 | + } | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Ensures that {@link DCAwareRoundRobinPolicy} does not use remote hosts if replicas in the local DC are UP. | |
| 116 | + * | |
| 117 | + * @test_category load_balancing:dc_aware | |
| 118 | + */ | |
| 119 | + @Test(groups="short") | |
| 120 | + public void should_not_use_remote_hosts_if_some_nodes_are_up_in_local_dc() { | |
| 121 | + // given: a 10 node 2 DC cluster with DC policy with 2 remote hosts. | |
| 122 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5, 5).build(); | |
| 123 | + Cluster cluster = builder() | |
| 124 | + .addContactPoint(sCluster.address(1, 1)) | |
| 125 | + .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withLocalDc(datacenter(1)).withUsedHostsPerRemoteDc(2).build()) | |
| 126 | + .build(); | |
| 65 | 127 | |
| 66 | 128 | try { |
| 67 | - // Pass host1 as contact point | |
| 68 | - cluster = Cluster.builder() | |
| 69 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 70 | - .withLoadBalancingPolicy(policy) | |
| 71 | - .build(); | |
| 72 | - cluster.init(); | |
| 129 | + sCluster.init(); | |
| 130 | + | |
| 131 | + Session session = cluster.connect(); | |
| 132 | + | |
| 133 | + // when: a query is executed 50 times and some hosts are down in the local DC. | |
| 134 | + sCluster.stop(cluster, 1, 5); | |
| 135 | + sCluster.stop(cluster, 1, 3); | |
| 136 | + sCluster.stop(cluster, 1, 1); | |
| 137 | + assertThat(cluster).controlHost().isNotNull(); | |
| 138 | + queryTracker.query(session, 50); | |
| 139 | + | |
| 140 | + // then: all requests should be distributed to the remaining up nodes in local DC. | |
| 141 | + queryTracker.assertQueried(sCluster, 1, 2, 25); | |
| 142 | + queryTracker.assertQueried(sCluster, 1, 4, 25); | |
| 143 | + | |
| 144 | + // then: no nodes in the remote DC should have been queried. | |
| 145 | + for(int i = 1; i <= 5; i++) { | |
| 146 | + queryTracker.assertQueried(sCluster, 2, i, 0); | |
| 147 | + } | |
| 148 | + } finally { | |
| 149 | + cluster.close(); | |
| 150 | + sCluster.stop(); | |
| 151 | + } | |
| 152 | + } | |
| 73 | 153 | |
| 74 | - Host host1 = TestUtils.findHost(cluster, 1); | |
| 154 | + /** | |
| 155 | + * Ensures that {@link DCAwareRoundRobinPolicy} will round robin on remote hosts but only if | |
| 156 | + * no local replicas are available and only within the number of hosts configured by | |
| 157 | + * {@link DCAwareRoundRobinPolicy.Builder#withUsedHostsPerRemoteDc(int)} | |
| 158 | + * | |
| 159 | + * @test_category load_balancing:dc_aware | |
| 160 | + */ | |
| 161 | + @Test(groups="short") | |
| 162 | + public void should_round_robin_on_remote_hosts_when_no_up_nodes_in_local_dc() { | |
| 163 | + // given: a 10 node 2 DC cluster with DC policy with 2 remote hosts. | |
| 164 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5, 5).build(); | |
| 165 | + Cluster cluster = builder() | |
| 166 | + .addContactPoint(sCluster.address(1, 1)) | |
| 167 | + .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withUsedHostsPerRemoteDc(2).build()) | |
| 168 | + .build(); | |
| 169 | + | |
| 170 | + try { | |
| 171 | + sCluster.init(); | |
| 172 | + | |
| 173 | + Session session = cluster.connect(); | |
| 75 | 174 | |
| 76 | - // Policy's localDC should be the one from the contact point | |
| 77 | - assertThat(policy.getLocalDc()).isEqualTo(host1.getDatacenter()); | |
| 175 | + sCluster.stopDC(cluster, 1); | |
| 78 | 176 | |
| 79 | - assertThat(policy.initHosts).containsExactly(host1); | |
| 177 | + // Wait for control connection to be re-established, needed as | |
| 178 | + // control connection attempts increment LBP counter. | |
| 179 | + assertThat(cluster).controlHost().isNotNull(); | |
| 80 | 180 | |
| 81 | - assertThat(logs.get()) | |
| 82 | - .doesNotContain("Some contact points don't match local data center"); | |
| 181 | + // when: a query is executed 50 times and all hosts are down in local DC. | |
| 182 | + queryTracker.query(session, 50); | |
| 83 | 183 | |
| 184 | + // then: only usedHostsPerRemoteDc nodes in the remote DC should get requests. | |
| 185 | + Collection<Integer> queryCounts = newArrayList(); | |
| 186 | + for (int i = 1; i <= 5; i++) { | |
| 187 | + queryCounts.add(queryTracker.queryCount(sCluster, 2, i)); | |
| 188 | + } | |
| 189 | + assertThat(queryCounts).containsOnly(0, 0, 0, 25, 25); | |
| 84 | 190 | } finally { |
| 85 | - if (cluster != null) | |
| 86 | - cluster.close(); | |
| 191 | + cluster.close(); | |
| 192 | + sCluster.stop(); | |
| 87 | 193 | } |
| 88 | 194 | } |
| 89 | 195 | |
| 90 | - @Test(groups = "short") | |
| 91 | - public void should_warn_if_contact_points_have_different_dcs_when_not_explicitly_specified() { | |
| 92 | - Cluster cluster = null; | |
| 93 | - CountingDCAwarePolicy policy = new CountingDCAwarePolicy(); | |
| 196 | + /** | |
| 197 | + * Ensures that {@link DCAwareRoundRobinPolicy} will by default only use remote hosts for non DC local | |
| 198 | + * Consistency Levels. In the case that a DC local Consistency Level is provided a | |
| 199 | + * {@link NoHostAvailableException} is raised. | |
| 200 | + * | |
| 201 | + * @test_category load_balancing:dc_aware | |
| 202 | + */ | |
| 203 | + @Test(groups="short", dataProvider="consistencyLevels", dataProviderClass = DataProviders.class) | |
| 204 | + public void should_only_use_remote_hosts_when_using_non_dc_local_cl(ConsistencyLevel cl) { | |
| 205 | + // given: a 4 node 2 DC Cluster with a LB policy that specifies to not allow remote dcs for | |
| 206 | + // a local consistency level. | |
| 207 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2, 2).build(); | |
| 208 | + Cluster cluster = builder() | |
| 209 | + .addContactPoint(sCluster.address(1, 1)) | |
| 210 | + .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder().withUsedHostsPerRemoteDc(2).build()) | |
| 211 | + .build(); | |
| 94 | 212 | |
| 95 | 213 | try { |
| 96 | - // Pass both hosts as contact points, they have different DCs | |
| 97 | - cluster = Cluster.builder() | |
| 98 | - .addContactPoints(CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2)) | |
| 99 | - .withLoadBalancingPolicy(policy) | |
| 100 | - .build(); | |
| 101 | - cluster.init(); | |
| 214 | + sCluster.init(); | |
| 215 | + | |
| 216 | + Session session = cluster.connect(); | |
| 217 | + | |
| 218 | + sCluster.stopDC(cluster, 1); | |
| 219 | + | |
| 220 | + // Wait for control connection to be re-established, needed as | |
| 221 | + // control connection attempts increment LBP counter. | |
| 222 | + assertThat(cluster).controlHost().isNotNull(); | |
| 223 | + | |
| 224 | + // when: a query is executed 50 times and all hosts are down in local DC. | |
| 225 | + // then: expect a NHAE for a local CL since no local replicas available. | |
| 226 | + Class<? extends Exception> expectedException = cl.isDCLocal() ? NoHostAvailableException.class : null; | |
| 227 | + queryTracker.query(session, 50, cl, expectedException); | |
| 228 | + | |
| 229 | + int expectedQueryCount = cl.isDCLocal() ? 0 : 25; | |
| 230 | + for (int i = 1; i <= 2; i++) { | |
| 231 | + queryTracker.assertQueried(sCluster, 1, i, 0); | |
| 232 | + // then: Remote hosts should only be queried for non local CLs. | |
| 233 | + queryTracker.assertQueried(sCluster, 2, i, expectedQueryCount); | |
| 234 | + } | |
| 235 | + } finally { | |
| 236 | + cluster.close(); | |
| 237 | + sCluster.stop(); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Ensures that {@link DCAwareRoundRobinPolicy} will use remote hosts for non DC local | |
| 243 | + * Consistency Levels if {@link DCAwareRoundRobinPolicy.Builder#allowRemoteDCsForLocalConsistencyLevel} is used. | |
| 244 | + * In the case that a DC local Consistency Level is provided a {@link NoHostAvailableException} is raised. | |
| 245 | + * | |
| 246 | + * @test_category load_balancing:dc_aware | |
| 247 | + */ | |
| 248 | + @Test(groups="short", dataProvider="consistencyLevels", dataProviderClass = DataProviders.class) | |
| 249 | + public void should_use_remote_hosts_for_local_cl_when_allowed(ConsistencyLevel cl) { | |
| 250 | + // given: a 4 node 2 DC Cluster with a LB policy that specifies to allow remote dcs for | |
| 251 | + // a local consistency level. | |
| 252 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2, 2).build(); | |
| 253 | + Cluster cluster = builder() | |
| 254 | + .addContactPoint(sCluster.address(1, 1)) | |
| 255 | + .withLoadBalancingPolicy(DCAwareRoundRobinPolicy.builder() | |
| 256 | + .allowRemoteDCsForLocalConsistencyLevel() | |
| 257 | + .withUsedHostsPerRemoteDc(2).build()) | |
| 258 | + .build(); | |
| 102 | 259 | |
| 103 | - Host host1 = TestUtils.findHost(cluster, 1); | |
| 104 | - Host host2 = TestUtils.findHost(cluster, 2); | |
| 260 | + try { | |
| 261 | + sCluster.init(); | |
| 262 | + | |
| 263 | + Session session = cluster.connect(); | |
| 264 | + | |
| 265 | + sCluster.stopDC(cluster, 1); | |
| 105 | 266 | |
| 106 | - assertThat(policy.initHosts).containsOnly(host1, host2); | |
| 267 | + // Wait for control connection to be re-established, needed as | |
| 268 | + // control connection attempts increment LBP counter. | |
| 269 | + assertThat(cluster).controlHost().isNotNull(); | |
| 107 | 270 | |
| 108 | - assertThat(logs.get()) | |
| 109 | - .contains("Some contact points don't match local data center"); | |
| 271 | + // when: a query is executed 50 times and all hosts are down in local DC. | |
| 272 | + queryTracker.query(session, 50, cl, null); | |
| 110 | 273 | |
| 274 | + for (int i = 1; i <= 2; i++) { | |
| 275 | + queryTracker.assertQueried(sCluster, 1, i, 0); | |
| 276 | + // then: Remote hosts should be queried. | |
| 277 | + queryTracker.assertQueried(sCluster, 2, i, 25); | |
| 278 | + } | |
| 111 | 279 | } finally { |
| 112 | - if (cluster != null) | |
| 113 | - cluster.close(); | |
| 280 | + cluster.close(); | |
| 281 | + sCluster.stop(); | |
| 114 | 282 | } |
| 115 | 283 | } |
| 116 | 284 | |
| 285 | + /** | |
| 286 | + * Ensures that when {@link DCAwareRoundRobinPolicy} is wrapped with a {@link HostFilterPolicy} that | |
| 287 | + * blacklists a data center that nodes in that datacenter are never queried. | |
| 288 | + * | |
| 289 | + * @test_category load_balancing:dc_aware | |
| 290 | + */ | |
| 117 | 291 | @Test(groups = "short") |
| 118 | - public void should_use_provided_local_dc_and_not_warn_if_contact_points_match() { | |
| 119 | - Cluster cluster = null; | |
| 120 | - String providedLocalDc = "dc1"; | |
| 121 | - CountingDCAwarePolicy policy = new CountingDCAwarePolicy(providedLocalDc); | |
| 292 | + public void should_not_send_requests_to_blacklisted_dc_using_host_filter_policy() { | |
| 293 | + // given: a 6 node 3 DC cluster with a DCAwareRoundRobinPolicy that is filtering hosts in DC2. | |
| 294 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2,2,2).build(); | |
| 295 | + LoadBalancingPolicy loadBalancingPolicy = HostFilterPolicy.fromDCBlackList( | |
| 296 | + DCAwareRoundRobinPolicy.builder().withUsedHostsPerRemoteDc(2).build(), | |
| 297 | + Lists.newArrayList(datacenter(2))); | |
| 298 | + Cluster cluster = builder() | |
| 299 | + .addContactPoint(sCluster.address(1, 1)) | |
| 300 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 301 | + .build(); | |
| 122 | 302 | |
| 123 | 303 | try { |
| 124 | - cluster = Cluster.builder() | |
| 125 | - .addContactPoints(CCMBridge.ipOfNode(1)) | |
| 126 | - .withLoadBalancingPolicy(policy) | |
| 127 | - .build(); | |
| 128 | - cluster.init(); | |
| 304 | + sCluster.init(); | |
| 305 | + | |
| 306 | + Session session = cluster.connect(); | |
| 307 | + | |
| 308 | + // when: A query is made and nodes for the local dc are available. | |
| 309 | + queryTracker.query(session, 50); | |
| 310 | + | |
| 311 | + // then: only nodes in the local DC should have been queried. | |
| 312 | + queryTracker.assertQueried(sCluster, 1, 1, 25); | |
| 313 | + queryTracker.assertQueried(sCluster, 1, 2, 25); | |
| 314 | + queryTracker.assertQueried(sCluster, 2, 1, 0); | |
| 315 | + queryTracker.assertQueried(sCluster, 2, 2, 0); | |
| 316 | + queryTracker.assertQueried(sCluster, 3, 1, 0); | |
| 317 | + queryTracker.assertQueried(sCluster, 3, 1, 0); | |
| 318 | + | |
| 319 | + // when: A query is made and all nodes in the local dc are down. | |
| 320 | + sCluster.stopDC(cluster, 1); | |
| 321 | + assertThat(cluster).controlHost().isNotNull(); | |
| 322 | + queryTracker.reset(); | |
| 323 | + queryTracker.query(session, 50); | |
| 324 | + | |
| 325 | + // then: Only nodes in DC3 should have been queried, since DC2 is blacklisted and DC1 is down. | |
| 326 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 327 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 328 | + queryTracker.assertQueried(sCluster, 2, 1, 0); | |
| 329 | + queryTracker.assertQueried(sCluster, 2, 2, 0); | |
| 330 | + queryTracker.assertQueried(sCluster, 3, 1, 25); | |
| 331 | + queryTracker.assertQueried(sCluster, 3, 2, 25); | |
| 332 | + } finally { | |
| 333 | + cluster.close(); | |
| 334 | + sCluster.stop(); | |
| 335 | + } | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * Ensures that when {@link DCAwareRoundRobinPolicy} is wrapped with a {@link HostFilterPolicy} that | |
| 340 | + * white lists data centers that only nodes in those data centers are queried. | |
| 341 | + * | |
| 342 | + * @test_category load_balancing:dc_aware | |
| 343 | + */ | |
| 344 | + @Test(groups = "short") | |
| 345 | + public void should_send_requests_to_whitelisted_dcs_using_host_filter_policy() { | |
| 346 | + // given: a 6 node 3 DC cluster with a DCAwareRoundRobinPolicy that is whitelisting hosts in DC1 and DC2. | |
| 347 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2,2,2).build(); | |
| 348 | + LoadBalancingPolicy loadBalancingPolicy = HostFilterPolicy.fromDCWhiteList( | |
| 349 | + DCAwareRoundRobinPolicy.builder().withUsedHostsPerRemoteDc(2).build(), | |
| 350 | + Lists.newArrayList(datacenter(1), datacenter(2))); | |
| 351 | + Cluster cluster = builder() | |
| 352 | + .addContactPoint(sCluster.address(1, 1)) | |
| 353 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 354 | + .build(); | |
| 355 | + | |
| 356 | + try { | |
| 357 | + sCluster.init(); | |
| 358 | + | |
| 359 | + Session session = cluster.connect(); | |
| 360 | + | |
| 361 | + // when: A query is made and nodes for the local dc are available. | |
| 362 | + queryTracker.query(session, 50); | |
| 363 | + | |
| 364 | + // then: only nodes in the local DC should have been queried. | |
| 365 | + queryTracker.assertQueried(sCluster, 1, 1, 25); | |
| 366 | + queryTracker.assertQueried(sCluster, 1, 2, 25); | |
| 367 | + queryTracker.assertQueried(sCluster, 2, 1, 0); | |
| 368 | + queryTracker.assertQueried(sCluster, 2, 2, 0); | |
| 369 | + queryTracker.assertQueried(sCluster, 3, 1, 0); | |
| 370 | + queryTracker.assertQueried(sCluster, 3, 1, 0); | |
| 371 | + | |
| 372 | + // when: A query is made and all nodes in the local dc are down. | |
| 373 | + sCluster.stopDC(cluster, 1); | |
| 374 | + assertThat(cluster).controlHost().isNotNull(); | |
| 375 | + queryTracker.reset(); | |
| 376 | + queryTracker.query(session, 50); | |
| 377 | + | |
| 378 | + // then: Only nodes in DC2 should have been queried, since DC3 is not in the whitelist and DC1 is down. | |
| 379 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 380 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 381 | + queryTracker.assertQueried(sCluster, 2, 1, 25); | |
| 382 | + queryTracker.assertQueried(sCluster, 2, 2, 25); | |
| 383 | + queryTracker.assertQueried(sCluster, 3, 1, 0); | |
| 384 | + queryTracker.assertQueried(sCluster, 3, 1, 0); | |
| 385 | + } finally { | |
| 386 | + cluster.close(); | |
| 387 | + sCluster.stop(); | |
| 388 | + } | |
| 389 | + } | |
| 129 | 390 | |
| 130 | - Host host1 = TestUtils.findHost(cluster, 1); | |
| 391 | + /** | |
| 392 | + * Ensures that {@link DCAwareRoundRobinPolicy} will determine it's local DC based on the data center of the | |
| 393 | + * contact point(s). | |
| 394 | + * | |
| 395 | + * @test_category load_balancing:dc_aware | |
| 396 | + */ | |
| 397 | + @Test(groups="short") | |
| 398 | + public void should_use_local_dc_from_contact_points_when_not_explicitly_specified() { | |
| 399 | + // given: a 4 node 2 DC cluster without a local DC specified. | |
| 400 | + DCAwareRoundRobinPolicy policy = spy(DCAwareRoundRobinPolicy.builder().build()); | |
| 401 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2, 2).build(); | |
| 402 | + Cluster cluster = builder() | |
| 403 | + .addContactPoint(sCluster.address(1, 1)) | |
| 404 | + .withLoadBalancingPolicy(policy) | |
| 405 | + .build(); | |
| 131 | 406 | |
| 132 | - assertEquals(policy.getLocalDc(), providedLocalDc); | |
| 407 | + try { | |
| 408 | + sCluster.init(); | |
| 133 | 409 | |
| 134 | - assertThat(policy.initHosts).containsExactly(host1); | |
| 410 | + Host host1 = findHost(cluster, 1); | |
| 135 | 411 | |
| 136 | - assertThat(logs.get()) | |
| 137 | - .doesNotContain("Some contact points don't match local data center"); | |
| 412 | + // when: the cluster is initialized. | |
| 413 | + cluster.init(); | |
| 138 | 414 | |
| 415 | + // then: should have been initialized with only the host given as the contact point. | |
| 416 | + Mockito.verify(policy).init(any(Cluster.class), initHostsCaptor.capture()); | |
| 417 | + assertThat(initHostsCaptor.getValue()).containsExactly(host1); | |
| 418 | + // then: the local dc should match the contact points' datacenter. | |
| 419 | + assertThat(policy.localDc).isEqualTo(host1.getDatacenter()); | |
| 420 | + // then: should not indicate that contact points don't match the local datacenter. | |
| 421 | + assertThat(logs.get()).doesNotContain("Some contact points don't match local datacenter"); | |
| 139 | 422 | } finally { |
| 140 | - if (cluster != null) | |
| 141 | - cluster.close(); | |
| 423 | + cluster.close(); | |
| 424 | + sCluster.stop(); | |
| 142 | 425 | } |
| 143 | 426 | } |
| 144 | 427 | |
| 428 | + /** | |
| 429 | + * Ensures that {@link DCAwareRoundRobinPolicy} will determine it's local DC based on the data center of the | |
| 430 | + * contact point(s) and if contact points in different DCs are detected that a log message is generated | |
| 431 | + * indicating some contact points don't match the local data center. | |
| 432 | + * | |
| 433 | + * @test_category load_balancing:dc_aware | |
| 434 | + */ | |
| 145 | 435 | @Test(groups = "short") |
| 146 | - public void should_use_provided_local_dc_and_warn_if_contact_points_dont_match() { | |
| 147 | - Cluster cluster = null; | |
| 148 | - // Provide non-existent DC to make sure none of the contact points matches | |
| 149 | - String providedLocalDc = "dc3"; | |
| 150 | - CountingDCAwarePolicy policy = new CountingDCAwarePolicy(providedLocalDc); | |
| 436 | + public void should_warn_if_contact_points_have_different_dcs_when_not_explicitly_specified() { | |
| 437 | + // given: a 4 node 2 DC cluster with a Cluster instance with contact points in different DCs | |
| 438 | + // and no contact point specified. | |
| 439 | + DCAwareRoundRobinPolicy policy = spy(DCAwareRoundRobinPolicy.builder().build()); | |
| 440 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2, 2).build(); | |
| 441 | + Cluster cluster = builder() | |
| 442 | + .addContactPoints(sCluster.address(1, 1), sCluster.address(2, 1)) | |
| 443 | + .withLoadBalancingPolicy(policy) | |
| 444 | + .build(); | |
| 151 | 445 | |
| 152 | 446 | try { |
| 153 | - cluster = Cluster.builder() | |
| 154 | - .addContactPoints(CCMBridge.ipOfNode(1), CCMBridge.ipOfNode(2)) | |
| 155 | - .withLoadBalancingPolicy(policy) | |
| 156 | - .build(); | |
| 447 | + sCluster.init(); | |
| 448 | + | |
| 449 | + Host host1 = findHost(cluster, 1); | |
| 450 | + Host host3 = findHost(cluster, 3); | |
| 451 | + | |
| 452 | + // when: the cluster is initialized. | |
| 157 | 453 | cluster.init(); |
| 158 | 454 | |
| 159 | - Host host1 = TestUtils.findHost(cluster, 1); | |
| 160 | - Host host2 = TestUtils.findHost(cluster, 2); | |
| 455 | + // then: should have been initialized with only two hosts given as the contact point. | |
| 456 | + Mockito.verify(policy).init(any(Cluster.class), initHostsCaptor.capture()); | |
| 457 | + assertThat(initHostsCaptor.getValue()).containsOnly(host1, host3); | |
| 458 | + // then: should indicate that some contact points don't match the local datacenter. | |
| 459 | + assertThat(logs.get()).contains("Some contact points don't match local data center"); | |
| 460 | + } finally { | |
| 461 | + cluster.close(); | |
| 462 | + sCluster.stop(); | |
| 463 | + } | |
| 464 | + } | |
| 465 | + | |
| 466 | + /** | |
| 467 | + * Ensures that {@link DCAwareRoundRobinPolicy} will not log a warning if all contact points match | |
| 468 | + * the data center provided in {@link DCAwareRoundRobinPolicy.Builder#withLocalDc(String)} and that | |
| 469 | + * the explicitly provided local data center is used. | |
| 470 | + * | |
| 471 | + * @test_category load_balancing:dc_aware | |
| 472 | + */ | |
| 473 | + @Test(groups = "short") | |
| 474 | + public void should_use_provided_local_dc_and_not_warn_if_contact_points_match() { | |
| 475 | + // given: a 4 node 2 DC cluster with a Cluster instance with contact points in different DCs | |
| 476 | + // and a local DC that doesn't match any contact points. | |
| 477 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2, 2).build(); | |
| 478 | + DCAwareRoundRobinPolicy policy = spy(DCAwareRoundRobinPolicy.builder().withLocalDc(datacenter(1)).build()); | |
| 479 | + Cluster cluster = builder() | |
| 480 | + .addContactPoints(sCluster.address(1, 1)) | |
| 481 | + .withLoadBalancingPolicy(policy) | |
| 482 | + .build(); | |
| 161 | 483 | |
| 162 | - assertEquals(policy.getLocalDc(), providedLocalDc); | |
| 484 | + try { | |
| 485 | + sCluster.init(); | |
| 163 | 486 | |
| 164 | - assertThat(policy.initHosts).containsOnly(host1, host2); | |
| 487 | + Host host1 = findHost(cluster, 1); | |
| 165 | 488 | |
| 166 | - assertThat(logs.get()) | |
| 167 | - .contains("Some contact points don't match local data center"); | |
| 489 | + // when: the cluster is initialized. | |
| 490 | + cluster.init(); | |
| 168 | 491 | |
| 492 | + // then: should have been initialized with only two hosts given as the contact point. | |
| 493 | + Mockito.verify(policy).init(any(Cluster.class), initHostsCaptor.capture()); | |
| 494 | + assertThat(initHostsCaptor.getValue()).containsOnly(host1); | |
| 495 | + // then: the data center should appropriately be set to the one specified. | |
| 496 | + assertThat(policy.localDc).isEqualTo(host1.getDatacenter()); | |
| 497 | + // then: should not indicate that contact points don't match the local datacenter. | |
| 498 | + assertThat(logs.get()).doesNotContain("Some contact points don't match local data center"); | |
| 169 | 499 | } finally { |
| 170 | - if (cluster != null) | |
| 171 | - cluster.close(); | |
| 500 | + cluster.close(); | |
| 501 | + sCluster.stop(); | |
| 172 | 502 | } |
| 173 | 503 | } |
| 174 | 504 | |
| 175 | 505 | /** |
| 176 | - * Wraps the policy under test to spy the calls to init. | |
| 506 | + * Ensures that {@link DCAwareRoundRobinPolicy} will log a warning if some contact points don't match | |
| 507 | + * the data center provided in {@link DCAwareRoundRobinPolicy.Builder#withLocalDc(String)} and that | |
| 508 | + * the explicitly provided local data center is used. | |
| 509 | + * | |
| 510 | + * @test_category load_balancing:dc_aware | |
| 177 | 511 | */ |
| 178 | - static class CountingDCAwarePolicy extends DelegatingLoadBalancingPolicy { | |
| 179 | - Set<Host> initHosts = new CopyOnWriteArraySet<Host>(); | |
| 180 | - | |
| 181 | - CountingDCAwarePolicy() { | |
| 182 | - super(new DCAwareRoundRobinPolicy()); | |
| 183 | - } | |
| 512 | + @Test(groups = "short") | |
| 513 | + public void should_use_provided_local_dc_and_warn_if_contact_points_dont_match() { | |
| 514 | + // given: a 4 node 2 DC cluster with a Cluster instance with contact points in different DCs | |
| 515 | + // and a local DC that doesn't match any contact points. | |
| 516 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2, 2).build(); | |
| 517 | + DCAwareRoundRobinPolicy policy = spy(DCAwareRoundRobinPolicy.builder().withLocalDc(datacenter(3)).build()); | |
| 518 | + Cluster cluster = builder() | |
| 519 | + .addContactPoints(sCluster.address(1, 1), sCluster.address(2, 1)) | |
| 520 | + .withLoadBalancingPolicy(policy) | |
| 521 | + .build(); | |
| 184 | 522 | |
| 185 | - CountingDCAwarePolicy(String localDc) { | |
| 186 | - super(new DCAwareRoundRobinPolicy(localDc)); | |
| 187 | - } | |
| 523 | + try { | |
| 524 | + sCluster.init(); | |
| 188 | 525 | |
| 189 | - String getLocalDc() { | |
| 190 | - return ((DCAwareRoundRobinPolicy) delegate).localDc; | |
| 191 | - } | |
| 526 | + Host host1 = findHost(cluster, 1); | |
| 527 | + Host host3 = findHost(cluster, 3); | |
| 192 | 528 | |
| 193 | - @Override public void init(Cluster cluster, Collection<Host> hosts) { | |
| 194 | - initHosts.addAll(hosts); | |
| 195 | - super.init(cluster, hosts); | |
| 529 | + // when: the cluster is initialized. | |
| 530 | + cluster.init(); | |
| 531 | + | |
| 532 | + // then: should have been initialized with only two hosts given as the contact point. | |
| 533 | + Mockito.verify(policy).init(any(Cluster.class), initHostsCaptor.capture()); | |
| 534 | + assertThat(initHostsCaptor.getValue()).containsOnly(host1, host3); | |
| 535 | + // then: the data center should appropriately be set to the one specified. | |
| 536 | + assertThat(policy.localDc).isEqualTo(datacenter(3)); | |
| 537 | + // then: should indicate that some contact points don't match the local datacenter. | |
| 538 | + assertThat(logs.get()).contains("Some contact points don't match local data center"); | |
| 539 | + } finally { | |
| 540 | + cluster.close(); | |
| 541 | + sCluster.stop(); | |
| 196 | 542 | } |
| 197 | 543 | } |
| 198 | 544 | } |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DefaultRetryPolicyIntegrationTest.java +5 −1
| @@ -20,6 +20,8 @@ | ||
| 20 | 20 | import com.datastax.driver.core.Metrics; |
| 21 | 21 | import com.datastax.driver.core.Session; |
| 22 | 22 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 23 | + | |
| 24 | +import org.scassandra.Scassandra; | |
| 23 | 25 | import org.testng.annotations.Test; |
| 24 | 26 | |
| 25 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -117,7 +119,9 @@ | ||
| 117 | 119 | try { |
| 118 | 120 | Session whiteListedSession = whiteListedCluster.connect(); |
| 119 | 121 | // Clear all activity as result of connect. |
| 120 | - scassandras.clearAllRecordedActivity(); | |
| 122 | + for(Scassandra node : scassandras.nodes()) { | |
| 123 | + node.activityClient().clearAllRecordedActivity(); | |
| 124 | + } | |
| 121 | 125 | |
| 122 | 126 | simulateError(1, unavailable); |
| 123 | 127 |
added driver-core/src/test/java/com/datastax/driver/core/policies/RoundRobinPolicyTest.java +169 −0
| @@ -0,0 +1,169 @@ | ||
| 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.policies; | |
| 17 | + | |
| 18 | +import org.apache.log4j.Level; | |
| 19 | +import org.apache.log4j.Logger; | |
| 20 | +import org.testng.annotations.AfterMethod; | |
| 21 | +import org.testng.annotations.BeforeMethod; | |
| 22 | +import org.testng.annotations.Test; | |
| 23 | + | |
| 24 | +import com.datastax.driver.core.Cluster; | |
| 25 | +import com.datastax.driver.core.ConsistencyLevel; | |
| 26 | +import com.datastax.driver.core.DataProviders; | |
| 27 | +import com.datastax.driver.core.MemoryAppender; | |
| 28 | +import com.datastax.driver.core.QueryTracker; | |
| 29 | +import com.datastax.driver.core.ScassandraCluster; | |
| 30 | +import com.datastax.driver.core.Session; | |
| 31 | + | |
| 32 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 33 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 34 | + | |
| 35 | +public class RoundRobinPolicyTest { | |
| 36 | + | |
| 37 | + Logger policyLogger = Logger.getLogger(RoundRobinPolicy.class); | |
| 38 | + MemoryAppender logs; | |
| 39 | + QueryTracker queryTracker; | |
| 40 | + | |
| 41 | + @BeforeMethod(groups = "short") | |
| 42 | + public void setUp() { | |
| 43 | + queryTracker = new QueryTracker(); | |
| 44 | + policyLogger.setLevel(Level.WARN); | |
| 45 | + logs = new MemoryAppender(); | |
| 46 | + policyLogger.addAppender(logs); | |
| 47 | + } | |
| 48 | + | |
| 49 | + @AfterMethod(groups = "short") | |
| 50 | + public void tearDown() { | |
| 51 | + policyLogger.setLevel(null); | |
| 52 | + policyLogger.removeAppender(logs); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Ensures that when used {@link RoundRobinPolicy} properly round robins requests within | |
| 57 | + * nodes in a single datacenter. | |
| 58 | + * | |
| 59 | + * @test_category load_balancing:round_robin | |
| 60 | + */ | |
| 61 | + @Test(groups="short") | |
| 62 | + public void should_round_robin_within_single_datacenter() { | |
| 63 | + // given: a 5 node cluster using RoundRobinPolicy. | |
| 64 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5).build(); | |
| 65 | + | |
| 66 | + Cluster cluster = Cluster.builder() | |
| 67 | + .addContactPoint(sCluster.address(1)) | |
| 68 | + .withLoadBalancingPolicy(new RoundRobinPolicy()) | |
| 69 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 70 | + .build(); | |
| 71 | + | |
| 72 | + try { | |
| 73 | + sCluster.init(); | |
| 74 | + | |
| 75 | + Session session = cluster.connect(); | |
| 76 | + // when: a query is executed 50 times. | |
| 77 | + queryTracker.query(session, 50); | |
| 78 | + | |
| 79 | + // then: all nodes should be queried equally. | |
| 80 | + for(int i = 1; i <= 5; i++) { | |
| 81 | + queryTracker.assertQueried(sCluster, 1, i, 10); | |
| 82 | + } | |
| 83 | + } finally { | |
| 84 | + cluster.close(); | |
| 85 | + sCluster.stop(); | |
| 86 | + } | |
| 87 | + | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Ensures that when used {@link RoundRobinPolicy} properly round robins requests to nodes irrespective | |
| 92 | + * of cluster topology by ensuring nodes in different data centers are queried equally to others. | |
| 93 | + * | |
| 94 | + * @test_category load_balancing:round_robin | |
| 95 | + */ | |
| 96 | + @Test(groups="short") | |
| 97 | + public void should_round_robin_irrespective_of_topology() { | |
| 98 | + // given: a 10 node, 5 DC cluster using RoundRobinPolicy. | |
| 99 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(2,2,2,2,2).build(); | |
| 100 | + | |
| 101 | + Cluster cluster = Cluster.builder() | |
| 102 | + .addContactPoint(sCluster.address(1)) | |
| 103 | + .withLoadBalancingPolicy(new RoundRobinPolicy()) | |
| 104 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 105 | + .build(); | |
| 106 | + | |
| 107 | + try { | |
| 108 | + sCluster.init(); | |
| 109 | + | |
| 110 | + Session session = cluster.connect(); | |
| 111 | + // when: a query is executed 50 times. | |
| 112 | + queryTracker.query(session, 50); | |
| 113 | + | |
| 114 | + // then: all nodes should be queried equally. | |
| 115 | + for(int dc = 1; dc <= 5; dc++) { | |
| 116 | + for (int i = 1; i <= 2; i++) { | |
| 117 | + queryTracker.assertQueried(sCluster, dc, i, 5); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + } finally { | |
| 121 | + cluster.close(); | |
| 122 | + sCluster.stop(); | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Ensures that when used {@link RoundRobinPolicy} generates a warning if a consistency level is used | |
| 128 | + * that is data center local (i.e. LOCAL_QUORUM) and nodes from multiple data centers are in the cluster | |
| 129 | + * and that warning is only generated once. Also validates that is a non-Dc local consistency level is | |
| 130 | + * used (i.e. ONE) that no such warning is generated. | |
| 131 | + * | |
| 132 | + * @test_category load_balancing:round_robin | |
| 133 | + */ | |
| 134 | + @Test(groups="short", dataProvider="consistencyLevels", dataProviderClass=DataProviders.class) | |
| 135 | + public void should_warn_if_using_dc_local_consistency_level(ConsistencyLevel cl) { | |
| 136 | + // given: a 2 node, 2 DC cluster using RoundRobinPolicy. | |
| 137 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(1,1).build(); | |
| 138 | + | |
| 139 | + Cluster cluster = Cluster.builder() | |
| 140 | + .addContactPoint(sCluster.address(1)) | |
| 141 | + .withLoadBalancingPolicy(new RoundRobinPolicy()) | |
| 142 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 143 | + .build(); | |
| 144 | + | |
| 145 | + String expectedLogMessage = "Detected request at Consistency Level " + cl + " but the non-DC aware RoundRobinPolicy is in use."; | |
| 146 | + | |
| 147 | + try { | |
| 148 | + sCluster.init(); | |
| 149 | + | |
| 150 | + Session session = cluster.connect(); | |
| 151 | + // when: a query is executed 50 times. | |
| 152 | + queryTracker.query(session, 50, cl); | |
| 153 | + | |
| 154 | + // then: all nodes should be queried equally. | |
| 155 | + queryTracker.assertQueried(sCluster, 1, 1, 25); | |
| 156 | + queryTracker.assertQueried(sCluster, 2, 1, 25); | |
| 157 | + | |
| 158 | + // Should get a warning if using a local DC cl. | |
| 159 | + if(cl.isDCLocal()) { | |
| 160 | + assertThat(logs.get()).containsOnlyOnce(expectedLogMessage); | |
| 161 | + } else { | |
| 162 | + assertThat(logs.get()).doesNotContain(expectedLogMessage); | |
| 163 | + } | |
| 164 | + } finally { | |
| 165 | + cluster.close(); | |
| 166 | + sCluster.stop(); | |
| 167 | + } | |
| 168 | + } | |
| 169 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +325 −50
| @@ -18,77 +18,352 @@ | ||
| 18 | 18 | import java.nio.ByteBuffer; |
| 19 | 19 | import java.util.List; |
| 20 | 20 | |
| 21 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 21 | +import com.google.common.collect.ImmutableMap; | |
| 22 | 22 | import com.google.common.collect.Lists; |
| 23 | +import org.testng.annotations.BeforeMethod; | |
| 24 | +import org.testng.annotations.DataProvider; | |
| 23 | 25 | import org.testng.annotations.Test; |
| 24 | 26 | |
| 25 | -import com.datastax.driver.core.*; | |
| 26 | -import com.datastax.driver.core.utils.Bytes; | |
| 27 | +import com.datastax.driver.core.BoundStatement; | |
| 28 | +import com.datastax.driver.core.CCMBridge; | |
| 29 | +import com.datastax.driver.core.Cluster; | |
| 30 | +import com.datastax.driver.core.DataType; | |
| 31 | +import com.datastax.driver.core.Host; | |
| 32 | +import com.datastax.driver.core.PreparedStatement; | |
| 33 | +import com.datastax.driver.core.QueryTracker; | |
| 34 | +import com.datastax.driver.core.ResultSet; | |
| 35 | +import com.datastax.driver.core.Row; | |
| 36 | +import com.datastax.driver.core.ScassandraCluster; | |
| 37 | +import com.datastax.driver.core.Session; | |
| 38 | +import com.datastax.driver.core.SimpleStatement; | |
| 39 | +import com.datastax.driver.core.TestUtils; | |
| 27 | 40 | |
| 28 | -import static com.datastax.driver.core.TestUtils.nonDebouncingQueryOptions; | |
| 41 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 42 | +import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT; | |
| 43 | +import static com.datastax.driver.core.TestUtils.SIMPLE_KEYSPACE; | |
| 44 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 29 | 45 | |
| 30 | 46 | public class TokenAwarePolicyTest { |
| 31 | - @Test(groups = "long") | |
| 32 | - public void should_shuffle_replicas_when_requested() { | |
| 33 | - testShuffleReplicas(new TokenAwarePolicy(new RoundRobinPolicy(), true), | |
| 34 | - true); | |
| 47 | + | |
| 48 | + QueryTracker queryTracker; | |
| 49 | + | |
| 50 | + @BeforeMethod(groups = "short") | |
| 51 | + public void setUp() { | |
| 52 | + queryTracker = new QueryTracker(); | |
| 35 | 53 | } |
| 36 | 54 | |
| 37 | - @Test(groups = "long") | |
| 38 | - public void should_not_shuffle_replicas_when_not_requested() { | |
| 39 | - testShuffleReplicas(new TokenAwarePolicy(new RoundRobinPolicy(), false), | |
| 40 | - false); | |
| 55 | + @DataProvider(name="shuffleProvider") | |
| 56 | + public Object[][] shuffleProvider () { | |
| 57 | + return new Object[][] { | |
| 58 | + { true }, | |
| 59 | + { false }, | |
| 60 | + { null } | |
| 61 | + }; | |
| 41 | 62 | } |
| 42 | 63 | |
| 43 | - private void testShuffleReplicas(TokenAwarePolicy loadBalancingPolicy, boolean expectShuffled) { | |
| 44 | - CCMBridge ccm = null; | |
| 45 | - Cluster cluster = null; | |
| 64 | + /** | |
| 65 | + * Ensures that {@link TokenAwarePolicy} will shuffle discovered replicas depending on the value of shuffleReplicas | |
| 66 | + * used when constructing with {@link TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy, boolean)} and that if not | |
| 67 | + * provided replicas are shuffled by default when using {@link TokenAwarePolicy#TokenAwarePolicy(LoadBalancingPolicy, boolean)}. | |
| 68 | + * | |
| 69 | + * @test_category load_balancing:token_aware | |
| 70 | + */ | |
| 71 | + @Test(groups="short", dataProvider="shuffleProvider") | |
| 72 | + public void should_shuffle_replicas_based_on_configuration(Boolean shuffleReplicas) { | |
| 73 | + // given: an 8 node cluster using TokenAwarePolicy and some shuffle replica configuration with a keyspace with replication factor of 3. | |
| 74 | + ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 75 | + .withNodes(8) | |
| 76 | + .withSimpleKeyspace("keyspace", 3) | |
| 77 | + .build(); | |
| 78 | + | |
| 79 | + LoadBalancingPolicy loadBalancingPolicy; | |
| 80 | + if(shuffleReplicas == null) { | |
| 81 | + loadBalancingPolicy = new TokenAwarePolicy(new RoundRobinPolicy()); | |
| 82 | + shuffleReplicas = true; | |
| 83 | + } else { | |
| 84 | + loadBalancingPolicy = new TokenAwarePolicy(new RoundRobinPolicy(), shuffleReplicas); | |
| 85 | + } | |
| 86 | + | |
| 87 | + Cluster cluster = Cluster.builder().addContactPoints(sCluster.address(1)) | |
| 88 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 89 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 90 | + .build(); | |
| 91 | + | |
| 46 | 92 | try { |
| 47 | - ccm = CCMBridge.builder("test").withNodes(3).build(); | |
| 48 | - cluster = Cluster.builder() | |
| 49 | - .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 50 | - .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 51 | - .withQueryOptions(nonDebouncingQueryOptions()) | |
| 52 | - .build(); | |
| 53 | - | |
| 54 | - String keyspace = "ks"; | |
| 55 | - ByteBuffer routingKey = Bytes.fromHexString("0xCAFEBABE"); | |
| 93 | + sCluster.init(); | |
| 56 | 94 | |
| 57 | - Session session = cluster.connect(); | |
| 58 | - // Use a replication factor of 2, each routing key on this keyspace will corresponding to 2 hosts out of 3 | |
| 59 | - session.execute(String.format("CREATE KEYSPACE %s " | |
| 60 | - + "WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}", | |
| 61 | - keyspace)); | |
| 62 | - | |
| 63 | - // Actual query does not matter, only the keyspace and routing key will be used | |
| 64 | - SimpleStatement statement = new SimpleStatement("foo"); | |
| 65 | - statement.setKeyspace(keyspace); | |
| 66 | - statement.setRoutingKey(routingKey); | |
| 95 | + // given: A routing key that falls in the token range of node 6. | |
| 96 | + | |
| 97 | + // Encodes into murmur hash '4874351301193663061' which should belong be owned by node 6 with replicas 7 and 8. | |
| 98 | + ByteBuffer routingKey = DataType.text().serialize("This is some sample text"); | |
| 67 | 99 | |
| 68 | - // Identify the two replicas for our routing key | |
| 69 | - List<Host> replicas = Lists.newArrayList(cluster.getMetadata().getReplicas(keyspace, routingKey)); | |
| 70 | - assertThat(replicas).hasSize(2); | |
| 100 | + // then: The replicas resolved from the cluster metadata must match node 6 and its replicas. | |
| 101 | + List<Host> replicas = Lists.newArrayList(cluster.getMetadata().getReplicas("keyspace", routingKey)); | |
| 102 | + assertThat(replicas).containsExactly( | |
| 103 | + sCluster.host(cluster, 1, 6), | |
| 104 | + sCluster.host(cluster, 1, 7), | |
| 105 | + sCluster.host(cluster, 1, 8)); | |
| 106 | + | |
| 107 | + // then: generating a query plan on a statement using that routing key should properly prioritize node 6 and its replicas. | |
| 108 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5"); | |
| 109 | + statement.setRoutingKey(routingKey); | |
| 110 | + statement.setKeyspace("keyspace"); | |
| 71 | 111 | |
| 72 | - // Run a few query plans, make sure that these two replicas come first, shuffled or not depending on | |
| 73 | - // what we expect | |
| 74 | 112 | boolean shuffledAtLeastOnce = false; |
| 75 | - for (int i = 0; i < 10; i++) { | |
| 113 | + for(int i = 0; i < 1024; i++) { | |
| 76 | 114 | List<Host> queryPlan = Lists.newArrayList(loadBalancingPolicy.newQueryPlan(null, statement)); |
| 77 | - assertThat(queryPlan).hasSize(3); | |
| 115 | + assertThat(queryPlan).containsOnlyElementsOf(cluster.getMetadata().getAllHosts()); | |
| 116 | + | |
| 117 | + List<Host> firstThree = queryPlan.subList(0, 3); | |
| 118 | + // then: if shuffle replicas was used or using default, the first three hosts returned should be 6,7,8 in any order. | |
| 119 | + // if shuffle replicas was not used, the first three hosts returned should be 6,7,8 in that order. | |
| 120 | + if(shuffleReplicas) { | |
| 121 | + assertThat(firstThree).containsOnlyElementsOf(replicas); | |
| 122 | + if(!firstThree.equals(replicas)) { | |
| 123 | + shuffledAtLeastOnce = true; | |
| 124 | + } | |
| 125 | + } else { | |
| 126 | + assertThat(firstThree).containsExactlyElementsOf(replicas); | |
| 127 | + } | |
| 128 | + } | |
| 129 | + | |
| 130 | + // then: given 1024 query plans, the replicas should be shuffled at least once. | |
| 131 | + assertThat(shuffledAtLeastOnce).isEqualTo(shuffleReplicas); | |
| 132 | + } finally { | |
| 133 | + cluster.close(); | |
| 134 | + sCluster.stop(); | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Ensures that {@link TokenAwarePolicy} will properly prioritize replicas if a provided | |
| 140 | + * {@link SimpleStatement} is using an explicitly set keyspace and routing key and the | |
| 141 | + * keyspace provided is using SimpleStrategy with a replication factor of 1. | |
| 142 | + * | |
| 143 | + * @test_category load_balancing:token_aware | |
| 144 | + */ | |
| 145 | + @Test(groups="short") | |
| 146 | + public void should_choose_proper_host_based_on_routing_key() { | |
| 147 | + // given: A 3 node cluster using TokenAwarePolicy with a replication factor of 1. | |
| 148 | + ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 149 | + .withNodes(3) | |
| 150 | + .withSimpleKeyspace("keyspace", 1) | |
| 151 | + .build(); | |
| 152 | + Cluster cluster = Cluster.builder().addContactPoints(sCluster.address(1)) | |
| 153 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 154 | + .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) | |
| 155 | + .build(); | |
| 156 | + | |
| 157 | + // when: A query is made with a routing key | |
| 158 | + try { | |
| 159 | + sCluster.init(); | |
| 160 | + | |
| 161 | + Session session = cluster.connect(); | |
| 162 | + | |
| 163 | + // Encodes into murmur hash '4557949199137838892' which should belong be owned by node 3. | |
| 164 | + ByteBuffer routingKey = DataType.text().serialize("should_choose_proper_host_based_on_routing_key"); | |
| 165 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5") | |
| 166 | + .setRoutingKey(routingKey) | |
| 167 | + .setKeyspace("keyspace"); | |
| 168 | + | |
| 169 | + queryTracker.query(session, 10, statement); | |
| 170 | + | |
| 171 | + // then: The host having that token should be queried. | |
| 172 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 173 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 174 | + queryTracker.assertQueried(sCluster, 1, 3, 10); | |
| 175 | + } finally { | |
| 176 | + cluster.close(); | |
| 177 | + sCluster.stop(); | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + /** | |
| 182 | + * Ensures that {@link TokenAwarePolicy} will properly prioritize replicas in the local datacenter | |
| 183 | + * if a provided {@link SimpleStatement} is using an explicitly set keyspace and routing key and | |
| 184 | + * the keyspace provided is using NetworkTopologyStrategy with an RF of 1:1. | |
| 185 | + * | |
| 186 | + * @test_category load_balancing:token_aware | |
| 187 | + */ | |
| 188 | + @Test(groups="short") | |
| 189 | + public void should_choose_host_in_local_dc_when_using_network_topology_strategy_and_dc_aware() { | |
| 190 | + // given: A 6 node, 2 DC cluster with RF 1:1, using TokenAwarePolicy wrapping DCAwareRoundRobinPolicy with remote hosts. | |
| 191 | + ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 192 | + .withNodes(3, 3) | |
| 193 | + .withNetworkTopologyKeyspace("keyspace", ImmutableMap.of(1, 1, 2, 1)) | |
| 194 | + .build(); | |
| 195 | + Cluster cluster = Cluster.builder().addContactPoints(sCluster.address(2,1)) | |
| 196 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 197 | + .withLoadBalancingPolicy(new TokenAwarePolicy(DCAwareRoundRobinPolicy.builder() | |
| 198 | + .withLocalDc(ScassandraCluster.datacenter(2)) | |
| 199 | + .withUsedHostsPerRemoteDc(3) | |
| 200 | + .build())) | |
| 201 | + .build(); | |
| 202 | + | |
| 203 | + // when: A query is made with a routing key | |
| 204 | + try { | |
| 205 | + sCluster.init(); | |
| 206 | + | |
| 207 | + Session session = cluster.connect(); | |
| 208 | + | |
| 209 | + // Encodes into murmur hash '-8124212968526248339' which should belong to 1:1 in DC1 and 2:1 in DC2. | |
| 210 | + ByteBuffer routingKey = DataType.text().serialize("should_choose_host_in_local_dc_when_using_network_topology_strategy_and_dc_aware"); | |
| 211 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5") | |
| 212 | + .setRoutingKey(routingKey) | |
| 213 | + .setKeyspace("keyspace"); | |
| 214 | + | |
| 215 | + queryTracker.query(session, 10, statement); | |
| 216 | + | |
| 217 | + // then: The local replica (2:1) should be queried and never the remote one. | |
| 218 | + queryTracker.assertQueried(sCluster, 2, 1, 10); | |
| 219 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 220 | + } finally { | |
| 221 | + cluster.close(); | |
| 222 | + sCluster.stop(); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + | |
| 226 | + /** | |
| 227 | + * Ensures that {@link TokenAwarePolicy} will properly handle unavailability of replicas | |
| 228 | + * matching with routing keys by falling back on its child policy and that when those | |
| 229 | + * replicas become available the policy uses those replicas once again. | |
| 230 | + * | |
| 231 | + * @test_category load_balancing:token_aware | |
| 232 | + */ | |
| 233 | + @Test(groups="short") | |
| 234 | + public void should_use_other_nodes_when_replicas_having_token_are_down() { | |
| 235 | + // given: A 4 node cluster using TokenAwarePolicy with a replication factor of 2. | |
| 236 | + ScassandraCluster sCluster = ScassandraCluster.builder() | |
| 237 | + .withNodes(4) | |
| 238 | + .withSimpleKeyspace("keyspace", 2) | |
| 239 | + .build(); | |
| 240 | + Cluster cluster = Cluster.builder().addContactPoints(sCluster.address(2)) | |
| 241 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 242 | + // Don't shuffle replicas just to keep test deterministic. | |
| 243 | + .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy(), false)) | |
| 244 | + .build(); | |
| 245 | + | |
| 246 | + try { | |
| 247 | + sCluster.init(); | |
| 248 | + | |
| 249 | + Session session = cluster.connect(); | |
| 250 | + | |
| 251 | + // when: A query is made with a routing key and both hosts having that key's token are down. | |
| 252 | + // Encodes into murmur hash '6444339665561646341' which should belong to node 4. | |
| 253 | + ByteBuffer routingKey = DataType.text().serialize("should_use_other_nodes_when_replicas_having_token_are_down"); | |
| 254 | + SimpleStatement statement = new SimpleStatement("select * from table where k=5") | |
| 255 | + .setRoutingKey(routingKey) | |
| 256 | + .setKeyspace("keyspace"); | |
| 257 | + | |
| 258 | + queryTracker.query(session, 10, statement); | |
| 259 | + | |
| 260 | + // then: The node that is the primary for that key's hash is chosen. | |
| 261 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 262 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 263 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 264 | + queryTracker.assertQueried(sCluster, 1, 4, 10); | |
| 265 | + | |
| 266 | + // when: The primary node owning that key goes down and a query is made. | |
| 267 | + queryTracker.reset(); | |
| 268 | + sCluster.stop(cluster, 4); | |
| 269 | + queryTracker.query(session, 10, statement); | |
| 270 | + | |
| 271 | + // then: The next replica having that data should be chosen (node 1). | |
| 272 | + queryTracker.assertQueried(sCluster, 1, 1, 10); | |
| 273 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 274 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 275 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 276 | + | |
| 277 | + // when: All nodes having that token are down and a query is made. | |
| 278 | + queryTracker.reset(); | |
| 279 | + sCluster.stop(cluster, 1); | |
| 280 | + queryTracker.query(session, 10, statement); | |
| 281 | + | |
| 282 | + // then: The remaining nodes which are non-replicas of that token should be used | |
| 283 | + // delegating to the child policy (RoundRobin). | |
| 284 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 285 | + queryTracker.assertQueried(sCluster, 1, 2, 5); | |
| 286 | + queryTracker.assertQueried(sCluster, 1, 3, 5); | |
| 287 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 288 | + | |
| 289 | + // when: A replica having that key becomes up and a query is made. | |
| 290 | + queryTracker.reset(); | |
| 291 | + sCluster.start(cluster, 1); | |
| 292 | + queryTracker.query(session, 10, statement); | |
| 293 | + | |
| 294 | + // then: The newly up replica should be queried. | |
| 295 | + queryTracker.assertQueried(sCluster, 1, 1, 10); | |
| 296 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 297 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 298 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 299 | + | |
| 300 | + // when: The primary replicas becomes up and a query is made. | |
| 301 | + queryTracker.reset(); | |
| 302 | + sCluster.start(cluster, 4); | |
| 303 | + queryTracker.query(session, 10, statement); | |
| 304 | + | |
| 305 | + // then: The primary replica which is now up should be queried. | |
| 306 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 307 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 308 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 309 | + queryTracker.assertQueried(sCluster, 1, 4, 10); | |
| 310 | + } finally { | |
| 311 | + cluster.close(); | |
| 312 | + sCluster.stop(); | |
| 313 | + } | |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * Ensures that {@link TokenAwarePolicy} will properly handle a routing key for a {@link PreparedStatement} | |
| 318 | + * whose table uses multiple columns for its partition key. | |
| 319 | + * | |
| 320 | + * @test_category load_balancing:token_aware | |
| 321 | + * @jira_ticket JAVA-123 (to ensure routing key buffers are not destroyed). | |
| 322 | + */ | |
| 323 | + @Test(groups = "long") | |
| 324 | + public void should_properly_generate_and_use_routing_key_for_composite_partition_key() { | |
| 325 | + // given: a 3 node cluster with a keyspace with RF 1. | |
| 326 | + CCMBridge ccm = CCMBridge.builder("TokenAwarePolicyTest").withNodes(3).notStarted().build(); | |
| 327 | + | |
| 328 | + Cluster cluster = Cluster.builder() | |
| 329 | + .withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())) | |
| 330 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 331 | + .build(); | |
| 332 | + | |
| 333 | + try { | |
| 334 | + ccm.start(); | |
| 335 | + Session session = cluster.connect(); | |
| 336 | + | |
| 337 | + String table = "composite"; | |
| 338 | + session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, SIMPLE_KEYSPACE, 1)); | |
| 339 | + session.execute("USE " + SIMPLE_KEYSPACE); | |
| 340 | + session.execute(String.format("CREATE TABLE %s (k1 int, k2 int, i int, PRIMARY KEY ((k1, k2)))", table)); | |
| 341 | + | |
| 342 | + // (1,2) resolves to token '4881097376275569167' which belongs to node 1 so all queries should go to that node. | |
| 343 | + PreparedStatement insertPs = session.prepare("INSERT INTO " + table + "(k1, k2, i) VALUES (?, ?, ?)"); | |
| 344 | + BoundStatement insertBs = insertPs.bind(1, 2, 3); | |
| 345 | + | |
| 346 | + PreparedStatement selectPs = session.prepare("SELECT * FROM " + table + " WHERE k1=? and k2=?"); | |
| 347 | + BoundStatement selectBs = selectPs.bind(1,2); | |
| 348 | + | |
| 349 | + // when: executing a prepared statement with a composite partition key. | |
| 350 | + // then: should query the correct node (1) in for both insert and select queries. | |
| 351 | + for(int i = 0; i < 10; i++) { | |
| 352 | + ResultSet rs = session.execute(insertBs); | |
| 353 | + assertThat(rs.getExecutionInfo().getQueriedHost()).isEqualTo(TestUtils.findHost(cluster, 1)); | |
| 78 | 354 | |
| 79 | - List<Host> firstTwo = queryPlan.subList(0, 2); | |
| 80 | - assertThat(firstTwo).containsAll(replicas); // order does not matter | |
| 355 | + rs = session.execute(selectBs); | |
| 356 | + assertThat(rs.getExecutionInfo().getQueriedHost()).isEqualTo(TestUtils.findHost(cluster, 1)); | |
| 357 | + assertThat(rs.isExhausted()).isFalse(); | |
| 358 | + Row r = rs.one(); | |
| 359 | + assertThat(rs.isExhausted()).isTrue(); | |
| 81 | 360 | |
| 82 | - if (!firstTwo.equals(replicas)) | |
| 83 | - shuffledAtLeastOnce = true; | |
| 361 | + assertThat(r.getInt("i")).isEqualTo(3); | |
| 84 | 362 | } |
| 85 | - assertThat(shuffledAtLeastOnce).isEqualTo(expectShuffled); | |
| 86 | 363 | |
| 87 | 364 | } finally { |
| 88 | - if (cluster != null) | |
| 89 | - cluster.close(); | |
| 90 | - if (ccm != null) | |
| 91 | - ccm.remove(); | |
| 365 | + cluster.close(); | |
| 366 | + ccm.remove(); | |
| 92 | 367 | } |
| 93 | 368 | } |
| 94 | 369 | } |
added driver-core/src/test/java/com/datastax/driver/core/policies/WhiteListPolicyTest.java +134 −0
| @@ -0,0 +1,134 @@ | ||
| 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.policies; | |
| 17 | + | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | +import java.util.List; | |
| 21 | + | |
| 22 | +import com.google.common.collect.Lists; | |
| 23 | +import org.testng.annotations.BeforeMethod; | |
| 24 | +import org.testng.annotations.Test; | |
| 25 | + | |
| 26 | +import com.datastax.driver.core.Cluster; | |
| 27 | +import com.datastax.driver.core.ConsistencyLevel; | |
| 28 | +import com.datastax.driver.core.QueryTracker; | |
| 29 | +import com.datastax.driver.core.ScassandraCluster; | |
| 30 | +import com.datastax.driver.core.Session; | |
| 31 | +import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 32 | + | |
| 33 | +import static com.datastax.driver.core.TestUtils.nonQuietClusterCloseOptions; | |
| 34 | + | |
| 35 | +public class WhiteListPolicyTest { | |
| 36 | + | |
| 37 | + QueryTracker queryTracker; | |
| 38 | + | |
| 39 | + @BeforeMethod(groups = "short") | |
| 40 | + public void setUp() { | |
| 41 | + queryTracker = new QueryTracker(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Provides basic validation of {@link WhiteListPolicy}. | |
| 46 | + * <p/> | |
| 47 | + * Ensures that: | |
| 48 | + * <ol> | |
| 49 | + * <li>Only addresses provided in the whitelist are every used for querying.</li> | |
| 50 | + * <li>If no nodes present in the whitelist are available, queries fail with a {@link NoHostAvailableException}</li> | |
| 51 | + * </ol> | |
| 52 | + * | |
| 53 | + * @test_category load_balancing:white_list | |
| 54 | + */ | |
| 55 | + @Test(groups="short") | |
| 56 | + public void should_only_query_hosts_in_white_list() throws Exception { | |
| 57 | + // given: a 5 node cluster with a WhiteListPolicy targeting nodes 3 and 5. | |
| 58 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5).build(); | |
| 59 | + List<InetSocketAddress> whiteList = Lists.newArrayList( | |
| 60 | + new InetSocketAddress(InetAddress.getByName(sCluster.address(3)), 9042), | |
| 61 | + new InetSocketAddress(InetAddress.getByName(sCluster.address(5)), 9042)); | |
| 62 | + | |
| 63 | + Cluster cluster = Cluster.builder() | |
| 64 | + .addContactPoint(sCluster.address(5)) | |
| 65 | + .withLoadBalancingPolicy(new WhiteListPolicy(new RoundRobinPolicy(), whiteList)) | |
| 66 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 67 | + .build(); | |
| 68 | + | |
| 69 | + try { | |
| 70 | + sCluster.init(); | |
| 71 | + | |
| 72 | + Session session = cluster.connect(); | |
| 73 | + // when: a query is executed 50 times. | |
| 74 | + queryTracker.query(session, 50); | |
| 75 | + | |
| 76 | + // then: only nodes 3 and 5 should have been queried. | |
| 77 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 78 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 79 | + queryTracker.assertQueried(sCluster, 1, 3, 25); | |
| 80 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 81 | + queryTracker.assertQueried(sCluster, 1, 5, 25); | |
| 82 | + | |
| 83 | + queryTracker.reset(); | |
| 84 | + | |
| 85 | + // when: the only nodes in the whitelist are stopped. | |
| 86 | + sCluster.stop(cluster, 3); | |
| 87 | + sCluster.stop(cluster, 5); | |
| 88 | + | |
| 89 | + // then: all queries should raise a NHAE. | |
| 90 | + queryTracker.query(session, 50, ConsistencyLevel.ONE, NoHostAvailableException.class); | |
| 91 | + | |
| 92 | + queryTracker.assertQueried(sCluster, 1, 1, 0); | |
| 93 | + queryTracker.assertQueried(sCluster, 1, 2, 0); | |
| 94 | + queryTracker.assertQueried(sCluster, 1, 3, 0); | |
| 95 | + queryTracker.assertQueried(sCluster, 1, 4, 0); | |
| 96 | + queryTracker.assertQueried(sCluster, 1, 5, 0); | |
| 97 | + } finally { | |
| 98 | + cluster.close(); | |
| 99 | + sCluster.stop(); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Validates that a {@link Cluster} cannot be initiated if using a {@link WhiteListPolicy} and | |
| 105 | + * none of the specified contact point addresses are present in the white list. | |
| 106 | + * | |
| 107 | + * @test_category load_balancing:white_list | |
| 108 | + */ | |
| 109 | + @Test(groups="short", expectedExceptions = {IllegalArgumentException.class}) | |
| 110 | + public void should_require_contact_point_in_white_list() throws Exception { | |
| 111 | + // given: a 5 node cluster with a WhiteListPolicy targeting node2. | |
| 112 | + ScassandraCluster sCluster = ScassandraCluster.builder().withNodes(5).build(); | |
| 113 | + | |
| 114 | + // when: using a Cluster instance with none of the contact points in the declared | |
| 115 | + // WhiteListPolicy. | |
| 116 | + List<InetSocketAddress> whiteList = Lists.newArrayList( | |
| 117 | + new InetSocketAddress(InetAddress.getByName(sCluster.address(2)), 9042)); | |
| 118 | + Cluster cluster = Cluster.builder() | |
| 119 | + .addContactPoint(sCluster.address(3)) | |
| 120 | + .withLoadBalancingPolicy(new WhiteListPolicy(new RoundRobinPolicy(), whiteList)) | |
| 121 | + .withNettyOptions(nonQuietClusterCloseOptions) | |
| 122 | + .build(); | |
| 123 | + | |
| 124 | + try { | |
| 125 | + sCluster.init(); | |
| 126 | + // then: The cluster instance should fail to initialize as none of the contact | |
| 127 | + // points is present in the white list. | |
| 128 | + cluster.init(); | |
| 129 | + } finally { | |
| 130 | + cluster.close(); | |
| 131 | + sCluster.stop(); | |
| 132 | + } | |
| 133 | + } | |
| 134 | +} |