Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.0' into 2.1
Changesetb4f646b0b2ad4996ffae73e20240ec5a1fc22e39597b01edb9d70c5bd876db47
Authorolim7t <omichallat+github@gmail.com> · 2014-10-10 16:54 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-10 16:54 +02:00
Parentsf0e51e4b50e6, 5232d23164bf

Browse the files at this changeset

5 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -23,6 +23,7 @@
2323 - [improvement] Defunct connection on request timeout (JAVA-425)
2424 - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426)
2525 - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471)
26+- [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496)
2627
2728
2829 2.1.1:
@@ -93,6 +94,7 @@
9394 - [improvement] Defunct connection on request timeout (JAVA-425)
9495 - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426)
9596 - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471)
97+- [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496)
9698
9799
98100 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −1
@@ -1187,6 +1187,13 @@
11871187 metadata.add(address);
11881188 }
11891189
1190+ // Make a copy of the hosts - which only contain the contact points at this stage - before we initialize the
1191+ // control connection. We use a set with guaranteed insertion order, so that the contact points will remain
1192+ // first, before any other host discovered by the control connection.
1193+ // This is important if the load balancing policy is the DCAware one, because it needs to see the contact points
1194+ // first for the local DC detection to work properly.
1195+ Set<Host> hosts = Sets.newLinkedHashSet(metadata.allHosts());
1196+
11901197 try {
11911198 try {
11921199 controlConnection.connect();
@@ -1203,9 +1210,11 @@
12031210 }
12041211 }
12051212
1213+ // metadata now also contains other hosts discovered by the connection, update our copy
1214+ hosts.addAll(metadata.allHosts());
1215+
12061216 // Now that the control connection is ready, we have all the information we need about the nodes (datacenter,
12071217 // rack...) to initialize the load balancing policy
1208- Collection<Host> hosts = metadata.allHosts();
12091218 loadBalancingPolicy().init(Cluster.this, hosts);
12101219
12111220 isFullyInit = true;
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +3 −1
@@ -26,6 +26,7 @@
2626
2727 import com.google.common.base.Joiner;
2828 import com.google.common.base.Strings;
29+import com.google.common.annotations.VisibleForTesting;
2930 import com.google.common.collect.AbstractIterator;
3031 import org.slf4j.Logger;
3132 import org.slf4j.LoggerFactory;
@@ -61,7 +62,8 @@
6162 private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcLiveHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>();
6263 private final AtomicInteger index = new AtomicInteger();
6364
64- private volatile String localDc;
65+ @VisibleForTesting
66+ volatile String localDc;
6567
6668 private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcSuspectedHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>();
6769
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +4 −0
@@ -225,6 +225,10 @@
225225 }
226226 }
227227
228+ public static String ipOfNode(int nodeNumber) {
229+ return IP_PREFIX + Integer.toString(nodeNumber);
230+ }
231+
228232 // One cluster for the whole test class
229233 public static abstract class PerClassSingleNodeCluster {
230234
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+}