kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | d7be58e9b86745efc572886ab15ecbc50c28e265866b30b37f9581df56ea75cb |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-16 10:52 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-16 10:52 +02:00 |
| Parents | fd5942948e60 |
3 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | - [bug] Fix NPE when a connection fails during pool construction (JAVA-503) |
| 18 | 18 | - [improvement] Log datacenter name in DCAware policy's init when it is explicitly provided |
| 19 | 19 | (JAVA-423) |
| 20 | +- [improvement] Shuffle the replicas in TokenAwarePolicy.newQueryPlan (JAVA-504) | |
| 20 | 21 | |
| 21 | 22 | |
| 22 | 23 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/policies/TokenAwarePolicy.java +30 −7
| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | package com.datastax.driver.core.policies; |
| 17 | 17 | |
| 18 | 18 | 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; | |
| 22 | 22 | |
| 23 | 23 | import com.google.common.collect.AbstractIterator; |
| 24 | 24 | |
| @@ -49,17 +49,32 @@ | ||
| 49 | 49 | public class TokenAwarePolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { |
| 50 | 50 | |
| 51 | 51 | private final LoadBalancingPolicy childPolicy; |
| 52 | + private final boolean shuffleReplicas; | |
| 52 | 53 | private Metadata clusterMetadata; |
| 53 | 54 | |
| 54 | 55 | /** |
| 55 | - * Creates a new {@code TokenAware} policy that wraps the provided child | |
| 56 | - * load balancing policy. | |
| 56 | + * Creates a new {@code TokenAware} policy. | |
| 57 | 57 | * |
| 58 | 58 | * @param childPolicy the load balancing policy to wrap with token |
| 59 | 59 | * awareness. |
| 60 | + * @param shuffleReplicas whether to shuffle the replicas returned | |
| 61 | + * by {@code getRoutingKey}. | |
| 60 | 62 | */ |
| 61 | - public TokenAwarePolicy(LoadBalancingPolicy childPolicy) { | |
| 63 | + public TokenAwarePolicy(LoadBalancingPolicy childPolicy, boolean shuffleReplicas) { | |
| 62 | 64 | 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); | |
| 63 | 78 | } |
| 64 | 79 | |
| 65 | 80 | @Override |
| @@ -110,9 +125,17 @@ | ||
| 110 | 125 | if (replicas.isEmpty()) |
| 111 | 126 | return childPolicy.newQueryPlan(loggedKeyspace, statement); |
| 112 | 127 | |
| 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 | + | |
| 113 | 137 | return new AbstractIterator<Host>() { |
| 114 | 138 | |
| 115 | - private final Iterator<Host> iter = replicas.iterator(); | |
| 116 | 139 | private Iterator<Host> childIterator; |
| 117 | 140 | |
| 118 | 141 | @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 | +} |