Elffuss BraidHub

kikocis/java-driver public

Shuffle the replicas in TokenAwarePolicy.newQueryPlan (JAVA-504).
Changesetd7be58e9b86745efc572886ab15ecbc50c28e265866b30b37f9581df56ea75cb
Authorolim7t <omichallat+github@gmail.com> · 2014-10-16 10:52 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-16 10:52 +02:00
Parentsfd5942948e60

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -17,6 +17,7 @@
1717 - [bug] Fix NPE when a connection fails during pool construction (JAVA-503)
1818 - [improvement] Log datacenter name in DCAware policy's init when it is explicitly provided
1919 (JAVA-423)
20+- [improvement] Shuffle the replicas in TokenAwarePolicy.newQueryPlan (JAVA-504)
2021
2122
2223 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/policies/TokenAwarePolicy.java +30 −7
@@ -16,9 +16,9 @@
1616 package com.datastax.driver.core.policies;
1717
1818 import java.nio.ByteBuffer;
19-import java.util.Collection;
20-import java.util.Iterator;
21-import java.util.Set;
19+import java.util.*;
20+
21+import com.google.common.collect.Lists;
2222
2323 import com.google.common.collect.AbstractIterator;
2424
@@ -49,17 +49,32 @@
4949 public class TokenAwarePolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy {
5050
5151 private final LoadBalancingPolicy childPolicy;
52+ private final boolean shuffleReplicas;
5253 private Metadata clusterMetadata;
5354
5455 /**
55- * Creates a new {@code TokenAware} policy that wraps the provided child
56- * load balancing policy.
56+ * Creates a new {@code TokenAware} policy.
5757 *
5858 * @param childPolicy the load balancing policy to wrap with token
5959 * awareness.
60+ * @param shuffleReplicas whether to shuffle the replicas returned
61+ * by {@code getRoutingKey}.
6062 */
61- public TokenAwarePolicy(LoadBalancingPolicy childPolicy) {
63+ public TokenAwarePolicy(LoadBalancingPolicy childPolicy, boolean shuffleReplicas) {
6264 this.childPolicy = childPolicy;
65+ this.shuffleReplicas = shuffleReplicas;
66+ }
67+
68+ /**
69+ * Creates a new {@code TokenAware} policy with no shuffling of replicas.
70+ *
71+ * @param childPolicy the load balancing policy to wrap with token
72+ * awareness.
73+ *
74+ * @see #TokenAwarePolicy(LoadBalancingPolicy, boolean)
75+ */
76+ public TokenAwarePolicy(LoadBalancingPolicy childPolicy) {
77+ this(childPolicy, false);
6378 }
6479
6580 @Override
@@ -110,9 +125,17 @@
110125 if (replicas.isEmpty())
111126 return childPolicy.newQueryPlan(loggedKeyspace, statement);
112127
128+ final Iterator<Host> iter;
129+ if (shuffleReplicas) {
130+ List<Host> l = Lists.newArrayList(replicas);
131+ Collections.shuffle(l);
132+ iter = l.iterator();
133+ } else {
134+ iter = replicas.iterator();
135+ }
136+
113137 return new AbstractIterator<Host>() {
114138
115- private final Iterator<Host> iter = replicas.iterator();
116139 private Iterator<Host> childIterator;
117140
118141 @Override
added driver-core/src/test/java/com/datastax/driver/core/policies/TokenAwarePolicyTest.java +78 −0
@@ -0,0 +1,78 @@
1+package com.datastax.driver.core.policies;
2+
3+import java.nio.ByteBuffer;
4+import java.util.ArrayList;
5+import java.util.List;
6+import java.util.Set;
7+
8+import static org.assertj.core.api.Assertions.assertThat;
9+import com.google.common.collect.Lists;
10+import org.testng.annotations.Test;
11+
12+import com.datastax.driver.core.*;
13+import com.datastax.driver.core.utils.Bytes;
14+
15+public class TokenAwarePolicyTest {
16+ @Test(groups = "long")
17+ public void should_shuffle_replicas_when_requested() {
18+ testShuffleReplicas(new TokenAwarePolicy(new RoundRobinPolicy(), true),
19+ true);
20+ }
21+
22+ @Test(groups = "long")
23+ public void should_not_shuffle_replicas_when_not_requested() {
24+ testShuffleReplicas(new TokenAwarePolicy(new RoundRobinPolicy(), false),
25+ false);
26+ }
27+
28+ private void testShuffleReplicas(TokenAwarePolicy loadBalancingPolicy, boolean expectShuffled) {
29+ CCMBridge ccm = null;
30+ Cluster cluster = null;
31+ try {
32+ ccm = CCMBridge.create("test", 3);
33+ cluster = Cluster.builder()
34+ .addContactPoint(CCMBridge.ipOfNode(1))
35+ .withLoadBalancingPolicy(loadBalancingPolicy)
36+ .build();
37+
38+ String keyspace = "ks";
39+ ByteBuffer routingKey = Bytes.fromHexString("0xCAFEBABE");
40+
41+ Session session = cluster.connect();
42+ // Use a replication factor of 2, each routing key on this keyspace will corresponding to 2 hosts out of 3
43+ session.execute(String.format("CREATE KEYSPACE %s "
44+ + "WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2}",
45+ keyspace));
46+
47+ // Actual query does not matter, only the keyspace and routing key will be used
48+ SimpleStatement statement = new SimpleStatement("foo");
49+ statement.setKeyspace(keyspace);
50+ statement.setRoutingKey(routingKey);
51+
52+ // Identify the two replicas for our routing key
53+ List<Host> replicas = Lists.newArrayList(cluster.getMetadata().getReplicas(keyspace, routingKey));
54+ assertThat(replicas).hasSize(2);
55+
56+ // Run a few query plans, make sure that these two replicas come first, shuffled or not depending on
57+ // what we expect
58+ boolean shuffledAtLeastOnce = false;
59+ for (int i = 0; i < 10; i++) {
60+ List<Host> queryPlan = Lists.newArrayList(loadBalancingPolicy.newQueryPlan(null, statement));
61+ assertThat(queryPlan).hasSize(3);
62+
63+ List<Host> firstTwo = queryPlan.subList(0, 2);
64+ assertThat(firstTwo).containsAll(replicas); // order does not matter
65+
66+ if (!firstTwo.equals(replicas))
67+ shuffledAtLeastOnce = true;
68+ }
69+ assertThat(shuffledAtLeastOnce).isEqualTo(expectShuffled);
70+
71+ } finally {
72+ if (cluster != null)
73+ cluster.close();
74+ if (ccm != null)
75+ ccm.remove();
76+ }
77+ }
78+}