Elffuss BraidHub

kikocis/java-driver public

Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496).
Changeset5232d23164bf325c60382922ca7253b1d7e68a79c82c524909160bcd1d5ad3a5
Authorolim7t <omichallat+github@gmail.com> · 2014-10-09 21:08 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-10 10:46 +02:00
Parentse1cdb53aa3c6

Browse the files at this changeset

5 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -8,6 +8,7 @@
88 - [improvement] Defunct connection on request timeout (JAVA-425)
99 - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426)
1010 - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471)
11+- [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496)
1112
1213
1314 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +11 −2
@@ -1143,19 +1143,28 @@
11431143 // We don't want to signal -- call onAdd() -- because nothing is ready
11441144 // yet (loadbalancing policy, control connection, ...). All we want is
11451145 // create the Host object so we can initialize the control connection.
1146- Host host = metadata.add(address);
1146+ metadata.add(address);
11471147 }
11481148
11491149 try {
11501150 while (true) {
11511151 try {
1152+ // Make a copy of the hosts - which only contain the contact points at this stage - before we initialize the
1153+ // control connection. We use a set with guaranteed insertion order, so that the contact points will remain
1154+ // first, before any other host discovered by the control connection.
1155+ // This is important if the load balancing policy is the DCAware one, because it needs to see the contact points
1156+ // first for the local DC detection to work properly.
1157+ Set<Host> hosts = Sets.newLinkedHashSet(metadata.allHosts());
1158+
11521159 controlConnection.connect();
11531160 if (connectionFactory.protocolVersion < 0)
11541161 connectionFactory.protocolVersion = 2;
11551162
1163+ // metadata now also contains other hosts discovered by the connection, update our copy
1164+ hosts.addAll(metadata.allHosts());
1165+
11561166 // Now that the control connection is ready, we have all the information we need about the nodes (datacenter,
11571167 // rack...) to initialize the load balancing policy
1158- Collection<Host> hosts = metadata.allHosts();
11591168 loadBalancingPolicy().init(Cluster.this, hosts);
11601169
11611170 isFullyInit = true;
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +3 −1
@@ -24,6 +24,7 @@
2424 import java.util.concurrent.TimeoutException;
2525 import java.util.concurrent.atomic.AtomicInteger;
2626
27+import com.google.common.annotations.VisibleForTesting;
2728 import com.google.common.collect.AbstractIterator;
2829 import org.slf4j.Logger;
2930 import org.slf4j.LoggerFactory;
@@ -59,7 +60,8 @@
5960 private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcLiveHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>();
6061 private final AtomicInteger index = new AtomicInteger();
6162
62- private volatile String localDc;
63+ @VisibleForTesting
64+ volatile String localDc;
6365
6466 private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcSuspectedHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>();
6567
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +4 −0
@@ -226,6 +226,10 @@
226226 }
227227 }
228228
229+ public static String ipOfNode(int nodeNumber) {
230+ return IP_PREFIX + Integer.toString(nodeNumber);
231+ }
232+
229233 // One cluster for the whole test class
230234 public static abstract class PerClassSingleNodeCluster {
231235
added driver-core/src/test/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicyTest.java +52 −0
@@ -0,0 +1,52 @@
1+package com.datastax.driver.core.policies;
2+
3+import org.testng.annotations.Test;
4+
5+import static org.testng.Assert.assertEquals;
6+import static org.testng.Assert.fail;
7+
8+import com.datastax.driver.core.CCMBridge;
9+import com.datastax.driver.core.Cluster;
10+import com.datastax.driver.core.Host;
11+
12+public class DCAwareRoundRobinPolicyTest {
13+
14+ @Test(groups = "short")
15+ public void should_pick_local_dc_from_contact_points() {
16+ CCMBridge ccm = null;
17+ Cluster cluster = null;
18+ DCAwareRoundRobinPolicy policy;
19+
20+ try {
21+ // Create two-DC cluster with one host in each DC
22+ ccm = CCMBridge.create("test", 1, 1);
23+ policy = new DCAwareRoundRobinPolicy();
24+
25+ // Pass host1 as contact point
26+ cluster = Cluster.builder()
27+ .addContactPoint(CCMBridge.ipOfNode(1))
28+ .withLoadBalancingPolicy(policy)
29+ .build();
30+ cluster.init();
31+
32+ // Policy's localDC should be the one from the contact point
33+ Host host1 = findHost(cluster, CCMBridge.ipOfNode(1));
34+ assertEquals(policy.localDc, host1.getDatacenter());
35+
36+ } finally {
37+ if (cluster != null)
38+ cluster.close();
39+ if (ccm != null)
40+ ccm.remove();
41+ }
42+ }
43+
44+ private static Host findHost(Cluster cluster, String ip) {
45+ for (Host host : cluster.getMetadata().getAllHosts()) {
46+ if (host.getAddress().getHostAddress().equals(ip))
47+ return host;
48+ }
49+ fail(ip + " not found in cluster metadata");
50+ return null;
51+ }
52+}