kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5232d23164bf325c60382922ca7253b1d7e68a79c82c524909160bcd1d5ad3a5 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-09 21:08 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-10 10:46 +02:00 |
| Parents | e1cdb53aa3c6 |
5 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [improvement] Defunct connection on request timeout (JAVA-425) |
| 9 | 9 | - [improvement] Try next host when we get a SERVER_ERROR (JAVA-426) |
| 10 | 10 | - [bug] Handle race between query timeout and completion (JAVA-449, JAVA-460, JAVA-471) |
| 11 | +- [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496) | |
| 11 | 12 | |
| 12 | 13 | |
| 13 | 14 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +11 −2
| @@ -1143,19 +1143,28 @@ | ||
| 1143 | 1143 | // We don't want to signal -- call onAdd() -- because nothing is ready |
| 1144 | 1144 | // yet (loadbalancing policy, control connection, ...). All we want is |
| 1145 | 1145 | // create the Host object so we can initialize the control connection. |
| 1146 | - Host host = metadata.add(address); | |
| 1146 | + metadata.add(address); | |
| 1147 | 1147 | } |
| 1148 | 1148 | |
| 1149 | 1149 | try { |
| 1150 | 1150 | while (true) { |
| 1151 | 1151 | 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 | + | |
| 1152 | 1159 | controlConnection.connect(); |
| 1153 | 1160 | if (connectionFactory.protocolVersion < 0) |
| 1154 | 1161 | connectionFactory.protocolVersion = 2; |
| 1155 | 1162 | |
| 1163 | + // metadata now also contains other hosts discovered by the connection, update our copy | |
| 1164 | + hosts.addAll(metadata.allHosts()); | |
| 1165 | + | |
| 1156 | 1166 | // Now that the control connection is ready, we have all the information we need about the nodes (datacenter, |
| 1157 | 1167 | // rack...) to initialize the load balancing policy |
| 1158 | - Collection<Host> hosts = metadata.allHosts(); | |
| 1159 | 1168 | loadBalancingPolicy().init(Cluster.this, hosts); |
| 1160 | 1169 | |
| 1161 | 1170 | isFullyInit = true; |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +3 −1
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | import java.util.concurrent.TimeoutException; |
| 25 | 25 | import java.util.concurrent.atomic.AtomicInteger; |
| 26 | 26 | |
| 27 | +import com.google.common.annotations.VisibleForTesting; | |
| 27 | 28 | import com.google.common.collect.AbstractIterator; |
| 28 | 29 | import org.slf4j.Logger; |
| 29 | 30 | import org.slf4j.LoggerFactory; |
| @@ -59,7 +60,8 @@ | ||
| 59 | 60 | private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcLiveHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>(); |
| 60 | 61 | private final AtomicInteger index = new AtomicInteger(); |
| 61 | 62 | |
| 62 | - private volatile String localDc; | |
| 63 | + @VisibleForTesting | |
| 64 | + volatile String localDc; | |
| 63 | 65 | |
| 64 | 66 | private final ConcurrentMap<String, CopyOnWriteArrayList<Host>> perDcSuspectedHosts = new ConcurrentHashMap<String, CopyOnWriteArrayList<Host>>(); |
| 65 | 67 |
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +4 −0
| @@ -226,6 +226,10 @@ | ||
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | + public static String ipOfNode(int nodeNumber) { | |
| 230 | + return IP_PREFIX + Integer.toString(nodeNumber); | |
| 231 | + } | |
| 232 | + | |
| 229 | 233 | // One cluster for the whole test class |
| 230 | 234 | public static abstract class PerClassSingleNodeCluster { |
| 231 | 235 |
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 | +} |