kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 8a71bb6176455afee2c6f0878303c72931ea8bc0f507ccd1bd64b8cad5aa95c3 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-07-09 16:54 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-08-04 10:16 +02:00 |
| Parents | d4861195664d |
7 files changed
modified changelog/README.md +1 −0
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | - [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733) |
| 26 | 26 | - [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765) |
| 27 | 27 | - [improvement] implement UPDATE ... IF EXISTS in QueryBuilder (JAVA-827) |
| 28 | +- [improvement] Randomize contact points list to prevent hotspots (JAVA-618) | |
| 28 | 29 | |
| 29 | 30 | Merged from 2.0.10_fixes branch: |
| 30 | 31 |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +4 −1
| @@ -74,7 +74,10 @@ | ||
| 74 | 74 | return; |
| 75 | 75 | |
| 76 | 76 | // NB: at this stage, allHosts() only contains the initial contact points |
| 77 | - setNewConnection(reconnectInternal(cluster.metadata.allHosts().iterator(), true)); | |
| 77 | + List<Host> hosts = new ArrayList<Host>(cluster.metadata.allHosts()); | |
| 78 | + // shuffle so that multiple clients with the same contact points don't all pick the same control host | |
| 79 | + Collections.shuffle(hosts); | |
| 80 | + setNewConnection(reconnectInternal(hosts.iterator(), true)); | |
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | public CloseFuture closeAsync() { |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +3 −3
| @@ -19,9 +19,6 @@ | ||
| 19 | 19 | import java.util.concurrent.ConcurrentHashMap; |
| 20 | 20 | import java.util.concurrent.ConcurrentMap; |
| 21 | 21 | import java.util.concurrent.CopyOnWriteArrayList; |
| 22 | -import java.util.concurrent.ExecutionException; | |
| 23 | -import java.util.concurrent.TimeUnit; | |
| 24 | -import java.util.concurrent.TimeoutException; | |
| 25 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 26 | 23 | |
| 27 | 24 | import com.google.common.annotations.VisibleForTesting; |
| @@ -210,6 +207,9 @@ | ||
| 210 | 207 | String nonLocalHosts = Joiner.on(",").join(notInLocalDC); |
| 211 | 208 | logger.warn("Some contact points don't match local data center. Local DC = {}. Non-conforming contact points: {}", localDc, nonLocalHosts); |
| 212 | 209 | } |
| 210 | + | |
| 211 | + this.index.set(new Random().nextInt(Math.max(hosts.size(), 1))); | |
| 212 | + | |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | private String dc(Host host) { |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +66 −0
| @@ -15,10 +15,18 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 18 | 19 | import java.util.Iterator; |
| 20 | +import java.util.Map; | |
| 19 | 21 | import java.util.concurrent.TimeUnit; |
| 20 | 22 | import java.util.concurrent.atomic.AtomicInteger; |
| 21 | 23 | |
| 24 | +import com.google.common.base.Function; | |
| 25 | +import com.google.common.collect.Collections2; | |
| 26 | +import com.google.common.collect.HashMultiset; | |
| 27 | +import com.google.common.collect.Maps; | |
| 28 | +import org.slf4j.Logger; | |
| 29 | +import org.slf4j.LoggerFactory; | |
| 22 | 30 | import org.testng.annotations.Test; |
| 23 | 31 | |
| 24 | 32 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -29,6 +37,9 @@ | ||
| 29 | 37 | import com.datastax.driver.core.policies.ReconnectionPolicy; |
| 30 | 38 | |
| 31 | 39 | public class ControlConnectionTest { |
| 40 | + | |
| 41 | + static final Logger logger = LoggerFactory.getLogger(ControlConnectionTest.class); | |
| 42 | + | |
| 32 | 43 | @Test(groups = "short") |
| 33 | 44 | public void should_prevent_simultaneous_reconnection_attempts() throws InterruptedException { |
| 34 | 45 | CCMBridge ccm = null; |
| @@ -122,6 +133,61 @@ | ||
| 122 | 133 | } |
| 123 | 134 | } |
| 124 | 135 | |
| 136 | + /** | |
| 137 | + * <p> | |
| 138 | + * Ensures that contact points are randomized when determining the initial control connection | |
| 139 | + * by default. Initializes a cluster with 5 contact points 100 times and ensures that all 5 | |
| 140 | + * were used. | |
| 141 | + * </p> | |
| 142 | + * | |
| 143 | + * @jira_ticket JAVA-618 | |
| 144 | + * @expected_result All 5 hosts were chosen within 100 attempts. There is a very small possibility | |
| 145 | + * that this may not be the case and this is not actually an error. | |
| 146 | + * @test_category control_connection | |
| 147 | + * @since 2.0.11, 2.1.8, 2.2.0 | |
| 148 | + */ | |
| 149 | + @Test(groups = "short") | |
| 150 | + public void should_randomize_contact_points_when_determining_control_connection() { | |
| 151 | + int hostCount = 5; | |
| 152 | + int iterations = 100; | |
| 153 | + SCassandraCluster scassandras = new SCassandraCluster(hostCount); | |
| 154 | + | |
| 155 | + try { | |
| 156 | + final HashMultiset<InetAddress> occurrencesByHost = HashMultiset.create(hostCount); | |
| 157 | + for(int i = 0; i < iterations; i++) { | |
| 158 | + Cluster cluster = Cluster.builder() | |
| 159 | + .addContactPoints(scassandras.addresses()) | |
| 160 | + .build(); | |
| 161 | + | |
| 162 | + try { | |
| 163 | + cluster.init(); | |
| 164 | + occurrencesByHost.add(cluster.manager.controlConnection.connectedHost().getAddress()); | |
| 165 | + } finally { | |
| 166 | + cluster.close(); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + if(logger.isDebugEnabled()) { | |
| 170 | + Map<InetAddress, Integer> hostCounts = Maps.toMap(occurrencesByHost.elementSet(), new Function<InetAddress, Integer>() { | |
| 171 | + @Override | |
| 172 | + public Integer apply(InetAddress input) { | |
| 173 | + return occurrencesByHost.count(input); | |
| 174 | + } | |
| 175 | + }); | |
| 176 | + logger.debug("Control Connection Use Counts by Host: {}", hostCounts); | |
| 177 | + } | |
| 178 | + // There is an incredibly low chance that a host may not be used based on randomness. | |
| 179 | + // This probability is very low however. | |
| 180 | + assertThat(occurrencesByHost.elementSet().size()) | |
| 181 | + .as("Not all hosts were used as contact points. There is a very small chance" | |
| 182 | + + " of this happening based on randomness, investigate whether or not this" | |
| 183 | + + " is a bug.") | |
| 184 | + .isEqualTo(hostCount); | |
| 185 | + } finally { | |
| 186 | + scassandras.stop(); | |
| 187 | + } | |
| 188 | + | |
| 189 | + } | |
| 190 | + | |
| 125 | 191 | static class QueryPlanCountingPolicy extends DelegatingLoadBalancingPolicy { |
| 126 | 192 | |
| 127 | 193 | final AtomicInteger counter = new AtomicInteger(); |
modified driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +4 −0
| @@ -43,6 +43,10 @@ | ||
| 43 | 43 | private final List<PrimingClient> primingClients; |
| 44 | 44 | private final List<ActivityClient> activityClients; |
| 45 | 45 | |
| 46 | + public SCassandraCluster(int nodeCount) { | |
| 47 | + this(CCMBridge.IP_PREFIX, nodeCount); | |
| 48 | + } | |
| 49 | + | |
| 46 | 50 | public SCassandraCluster(String ipPrefix, int nodeCount) { |
| 47 | 51 | scassandras = Lists.newArrayListWithCapacity(nodeCount); |
| 48 | 52 | addresses = Lists.newArrayListWithCapacity(nodeCount); |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicyTest.java +0 −1
| @@ -191,7 +191,6 @@ | ||
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | @Override public void init(Cluster cluster, Collection<Host> hosts) { |
| 194 | - System.out.println("init " + hosts); | |
| 195 | 194 | initHosts.addAll(hosts); |
| 196 | 195 | super.init(cluster, hosts); |
| 197 | 196 | } |
modified upgrade_guide/README.md +12 −0
| @@ -5,6 +5,9 @@ | ||
| 5 | 5 | |
| 6 | 6 | ### 2.0.11 |
| 7 | 7 | |
| 8 | +2.0.11 preserves binary compatibility with previous versions. There are a few | |
| 9 | +changes in the driver's behavior: | |
| 10 | + | |
| 8 | 11 | 1. The `DefaultRetryPolicy`'s behaviour has changed in the case of an Unavailable |
| 9 | 12 | exception received from a request. The new behaviour will cause the driver to |
| 10 | 13 | process a Retry on a different node at most once, otherwise an exception will |
| @@ -13,11 +16,20 @@ | ||
| 13 | 16 | because of a network partition) but can still answer to the client normally. |
| 14 | 17 | In this case, trying another node has a chance of success. |
| 15 | 18 | The previous behaviour was to always throw an exception. |
| 19 | + | |
| 16 | 20 | 2. A `BuiltStatement` is now considered non-idempotent whenever a `fcall()` |
| 17 | 21 | or `raw()` is used to build a value to be inserted in the database. |
| 18 | 22 | If you know that the CQL functions or expressions are safe, use |
| 19 | 23 | `setIdempotent(true)` on the statement. |
| 20 | 24 | |
| 25 | +3. The list of contact points provided at startup is now shuffled before trying | |
| 26 | + to open the control connection, so that multiple clients with the same contact | |
| 27 | + points don't all pick the same control host. As a result, you can't assume that | |
| 28 | + the driver will try contact points in a deterministic order. In particular, if | |
| 29 | + you use the `DCAwareRoundRobinPolicy` without specifying a primary datacenter | |
| 30 | + name, make sure that you only provide local hosts as contact points. | |
| 31 | + | |
| 32 | + | |
| 21 | 33 | ### 2.0.x to 2.0.10 |
| 22 | 34 | |
| 23 | 35 | We try to avoid breaking changes within a branch (2.0.x to 2.0.y), but |