kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b4f646b0b2ad4996ffae73e20240ec5a1fc22e39597b01edb9d70c5bd876db47 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-10 16:54 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-10 16:54 +02:00 |
| Parents | f0e51e4b50e6, 5232d23164bf |
5 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | - [improvement] Defunct connection on request timeout (JAVA-425) |
| 24 | 24 | - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426) |
| 25 | 25 | - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) |
| 26 | +- [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496) | |
| 26 | 27 | |
| 27 | 28 | |
| 28 | 29 | 2.1.1: |
| @@ -93,6 +94,7 @@ | ||
| 93 | 94 | - [improvement] Defunct connection on request timeout (JAVA-425) |
| 94 | 95 | - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426) |
| 95 | 96 | - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) |
| 97 | +- [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496) | |
| 96 | 98 | |
| 97 | 99 | |
| 98 | 100 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −1
| @@ -1187,6 +1187,13 @@ | ||
| 1187 | 1187 | metadata.add(address); |
| 1188 | 1188 | } |
| 1189 | 1189 | |
| 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 | + | |
| 1190 | 1197 | try { |
| 1191 | 1198 | try { |
| 1192 | 1199 | controlConnection.connect(); |
| @@ -1203,9 +1210,11 @@ | ||
| 1203 | 1210 | } |
| 1204 | 1211 | } |
| 1205 | 1212 | |
| 1213 | + // metadata now also contains other hosts discovered by the connection, update our copy | |
| 1214 | + hosts.addAll(metadata.allHosts()); | |
| 1215 | + | |
| 1206 | 1216 | // Now that the control connection is ready, we have all the information we need about the nodes (datacenter, |
| 1207 | 1217 | // rack...) to initialize the load balancing policy |
| 1208 | - Collection<Host> hosts = metadata.allHosts(); | |
| 1209 | 1218 | loadBalancingPolicy().init(Cluster.this, hosts); |
| 1210 | 1219 | |
| 1211 | 1220 | isFullyInit = true; |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +3 −1
| @@ -26,6 +26,7 @@ | ||
| 26 | 26 | |
| 27 | 27 | import com.google.common.base.Joiner; |
| 28 | 28 | import com.google.common.base.Strings; |
| 29 | +import com.google.common.annotations.VisibleForTesting; | |
| 29 | 30 | import com.google.common.collect.AbstractIterator; |
| 30 | 31 | import org.slf4j.Logger; |
| 31 | 32 | import org.slf4j.LoggerFactory; |
| @@ -61,7 +62,8 @@ | ||
| 61 | 62 | private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcLiveHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>(); |
| 62 | 63 | private final AtomicInteger index = new AtomicInteger(); |
| 63 | 64 | |
| 64 | - private volatile String localDc; | |
| 65 | + @VisibleForTesting | |
| 66 | + volatile String localDc; | |
| 65 | 67 | |
| 66 | 68 | private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcSuspectedHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>(); |
| 67 | 69 |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +4 −0
| @@ -225,6 +225,10 @@ | ||
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | + public static String ipOfNode(int nodeNumber) { | |
| 229 | + return IP_PREFIX + Integer.toString(nodeNumber); | |
| 230 | + } | |
| 231 | + | |
| 228 | 232 | // One cluster for the whole test class |
| 229 | 233 | public static abstract class PerClassSingleNodeCluster { |
| 230 | 234 |
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 | +} |