kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 746d27b05c5b90f5bf431c46a3ff23cc7b4b9242aaa5f7fc6be10b46361c87e8 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-14 10:24 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-14 19:24 +02:00 |
| Parents | 2a1d4419b6b0 |
13 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [bug] Ensure control connection does not trigger concurrent reconnects (JAVA-497) |
| 13 | 13 | - [improvement] Keep trying to reconnect on authentication errors (JAVA-472) |
| 14 | 14 | - [improvement] Expose close method on load balancing policy (JAVA-463) |
| 15 | +- [improvement] Allow load balancing policy to trigger refresh for a single host (JAVA-459) | |
| 15 | 16 | |
| 16 | 17 | |
| 17 | 18 | 2.0.6: |
modified driver-core/pom.xml +7 −0
| @@ -78,6 +78,13 @@ | ||
| 78 | 78 | <scope>test</scope> |
| 79 | 79 | </dependency> |
| 80 | 80 | |
| 81 | + <dependency> | |
| 82 | + <groupId>org.mockito</groupId> | |
| 83 | + <artifactId>mockito-all</artifactId> | |
| 84 | + <version>1.10.8</version> | |
| 85 | + <scope>test</scope> | |
| 86 | + </dependency> | |
| 87 | + | |
| 81 | 88 | </dependencies> |
| 82 | 89 | |
| 83 | 90 | <profiles> |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +10 −0
| @@ -1967,6 +1967,16 @@ | ||
| 1967 | 1967 | s.updateCreatedPools(executor); |
| 1968 | 1968 | } |
| 1969 | 1969 | |
| 1970 | + void refreshConnectedHost(Host host) { | |
| 1971 | + // Deal with the control connection if it was using this host | |
| 1972 | + Host ccHost = controlConnection.connectedHost(); | |
| 1973 | + if (ccHost == null || ccHost.equals(host) && loadBalancingPolicy().distance(ccHost) != HostDistance.LOCAL) | |
| 1974 | + controlConnection.reconnect(); | |
| 1975 | + | |
| 1976 | + for (SessionManager s : sessions) | |
| 1977 | + s.updateCreatedPools(host, executor); | |
| 1978 | + } | |
| 1979 | + | |
| 1970 | 1980 | private class ClusterCloseFuture extends CloseFuture.Forwarding { |
| 1971 | 1981 | |
| 1972 | 1982 | ClusterCloseFuture(List<CloseFuture> futures) { |
modified driver-core/src/main/java/com/datastax/driver/core/Host.java +9 −0
| @@ -175,6 +175,15 @@ | ||
| 175 | 175 | return initialReconnectionAttempt.get(); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | + @Override | |
| 179 | + public boolean equals(Object other) { | |
| 180 | + if (other instanceof Host) { | |
| 181 | + Host that = (Host)other; | |
| 182 | + return this.address.equals(that.address); | |
| 183 | + } | |
| 184 | + return false; | |
| 185 | + } | |
| 186 | + | |
| 178 | 187 | @Override |
| 179 | 188 | public int hashCode() { |
| 180 | 189 | return address.hashCode(); |
modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +11 −0
| @@ -226,6 +226,17 @@ | ||
| 226 | 226 | manager.refreshConnectedHosts(); |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | + /** | |
| 230 | + * Requests the driver to re-evaluate the {@link HostDistance} for a given node. | |
| 231 | + * | |
| 232 | + * @param host the host to refresh. | |
| 233 | + * | |
| 234 | + * @see #refreshConnectedHosts() | |
| 235 | + */ | |
| 236 | + public void refreshConnectedHost(Host host) { | |
| 237 | + manager.refreshConnectedHost(host); | |
| 238 | + } | |
| 239 | + | |
| 229 | 240 | private static void checkRequestsPerConnectionRange(int value, String description, HostDistance distance) { |
| 230 | 241 | if (value < 0 || value > StreamIdGenerator.MAX_STREAM_PER_CONNECTION) |
| 231 | 242 | throw new IllegalArgumentException(String.format("%s for %s hosts must be in the range (0, %d)", |
added driver-core/src/main/java/com/datastax/driver/core/PoolingOptionsTest.java +94 −0
| @@ -0,0 +1,94 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import org.mockito.Mockito; | |
| 4 | + | |
| 5 | +import org.testng.annotations.Test; | |
| 6 | + | |
| 7 | +import com.datastax.driver.core.Host.State; | |
| 8 | +import com.datastax.driver.core.policies.ConstantReconnectionPolicy; | |
| 9 | +import com.datastax.driver.core.policies.LimitingLoadBalancingPolicy; | |
| 10 | +import com.datastax.driver.core.policies.RoundRobinPolicy; | |
| 11 | + | |
| 12 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 13 | + | |
| 14 | +public class PoolingOptionsTest { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * Tests {@link PoolingOptions#refreshConnectedHost(Host)} through a custom load balancing policy. | |
| 18 | + */ | |
| 19 | + @Test(groups = "long") | |
| 20 | + public void should_refresh_single_connected_host() { | |
| 21 | + CCMBridge ccm = null; | |
| 22 | + Cluster cluster = null; | |
| 23 | + try { | |
| 24 | + // This will make the driver use at most 2 hosts, the others will be ignored | |
| 25 | + LimitingLoadBalancingPolicy loadBalancingPolicy = new LimitingLoadBalancingPolicy(new RoundRobinPolicy(), 2, 1); | |
| 26 | + | |
| 27 | + // Setup a 3-host cluster, start only two hosts so that we know in advance which ones the policy will use | |
| 28 | + ccm = CCMBridge.create("test"); | |
| 29 | + ccm.populate(3); | |
| 30 | + ccm.start(1); | |
| 31 | + ccm.start(2); | |
| 32 | + ccm.waitForUp(1); | |
| 33 | + ccm.waitForUp(2); | |
| 34 | + | |
| 35 | + PoolingOptions poolingOptions = Mockito.spy(new PoolingOptions()); | |
| 36 | + cluster = Cluster.builder() | |
| 37 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 38 | + .withPoolingOptions(poolingOptions) | |
| 39 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 40 | + .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)) | |
| 41 | + .build(); | |
| 42 | + | |
| 43 | + Session session = cluster.connect(); | |
| 44 | + | |
| 45 | + assertThat(cluster).usesControlHost(1); | |
| 46 | + assertThat(cluster).host(1) | |
| 47 | + .hasState(State.UP) | |
| 48 | + .isAtDistance(HostDistance.LOCAL); | |
| 49 | + assertThat(cluster).host(2) | |
| 50 | + .hasState(State.UP) | |
| 51 | + .isAtDistance(HostDistance.LOCAL); | |
| 52 | + | |
| 53 | + // Bring host 3 up, its presence should be acknowledged but it should be ignored | |
| 54 | + ccm.start(3); | |
| 55 | + ccm.waitForUp(3); | |
| 56 | + | |
| 57 | + assertThat(cluster).host(1) | |
| 58 | + .hasState(State.UP) | |
| 59 | + .isAtDistance(HostDistance.LOCAL); | |
| 60 | + assertThat(cluster).host(2) | |
| 61 | + .hasState(State.UP) | |
| 62 | + .isAtDistance(HostDistance.LOCAL); | |
| 63 | + assertThat(cluster).host(3) | |
| 64 | + .hasState(State.ADDED) | |
| 65 | + .isAtDistance(HostDistance.IGNORED); | |
| 66 | + assertThat(session).hasNoPoolFor(3); | |
| 67 | + | |
| 68 | + // Kill host 2, host 3 should take its place | |
| 69 | + ccm.stop(2); | |
| 70 | + TestUtils.waitFor(CCMBridge.ipOfNode(3), cluster); | |
| 71 | + | |
| 72 | + assertThat(cluster).host(1) | |
| 73 | + .hasState(State.UP) | |
| 74 | + .isAtDistance(HostDistance.LOCAL); | |
| 75 | + assertThat(cluster).host(2) | |
| 76 | + .hasState(State.DOWN); | |
| 77 | + assertThat(cluster).host(3) | |
| 78 | + .hasState(State.UP) | |
| 79 | + .isAtDistance(HostDistance.LOCAL); | |
| 80 | + assertThat(session).hasPoolFor(3); | |
| 81 | + | |
| 82 | + // This is when refreshConnectedHost should have been invoked, it triggers pool creation when | |
| 83 | + // we switch the node from IGNORED to UP: | |
| 84 | + Mockito.verify(poolingOptions) | |
| 85 | + .refreshConnectedHost(ClusterAssert.findHost(cluster, 3)); | |
| 86 | + | |
| 87 | + } finally { | |
| 88 | + if (cluster != null) | |
| 89 | + cluster.close(); | |
| 90 | + if (ccm != null) | |
| 91 | + ccm.remove(); | |
| 92 | + } | |
| 93 | + } | |
| 94 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +23 −0
| @@ -338,6 +338,29 @@ | ||
| 338 | 338 | } |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | + void updateCreatedPools(Host h, ListeningExecutorService executor) { | |
| 342 | + HostDistance dist = loadBalancingPolicy().distance(h); | |
| 343 | + HostConnectionPool pool = pools.get(h); | |
| 344 | + | |
| 345 | + try { | |
| 346 | + if (pool == null) { | |
| 347 | + if (dist != HostDistance.IGNORED && h.isUp()) | |
| 348 | + maybeAddPool(h, executor).get(); | |
| 349 | + } else if (dist != pool.hostDistance) { | |
| 350 | + if (dist == HostDistance.IGNORED) { | |
| 351 | + removePool(h).get(); | |
| 352 | + } else { | |
| 353 | + pool.hostDistance = dist; | |
| 354 | + pool.ensureCoreConnections(); | |
| 355 | + } | |
| 356 | + } | |
| 357 | + } catch (InterruptedException e) { | |
| 358 | + Thread.currentThread().interrupt(); | |
| 359 | + } catch (ExecutionException e) { | |
| 360 | + logger.error("Unexpected error while refreshing connection pools", e.getCause()); | |
| 361 | + } | |
| 362 | + } | |
| 363 | + | |
| 341 | 364 | void onDown(Host host) throws InterruptedException, ExecutionException { |
| 342 | 365 | // Note that with well behaved balancing policy (that ignore dead nodes), the removePool call is not necessary |
| 343 | 366 | // since updateCreatedPools should take care of it. But better protect against non well behaving policies. |
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +4 −0
| @@ -7,4 +7,8 @@ | ||
| 7 | 7 | public static ClusterAssert assertThat(Cluster cluster) { |
| 8 | 8 | return new ClusterAssert(cluster); |
| 9 | 9 | } |
| 10 | + | |
| 11 | + public static SessionAssert assertThat(Session session) { | |
| 12 | + return new SessionAssert(session); | |
| 13 | + } | |
| 10 | 14 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ClusterAssert.java +10 −2
| @@ -16,9 +16,17 @@ | ||
| 16 | 16 | return this; |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | + public HostAssert host(int hostNumber) { | |
| 20 | + // TODO at some point this won't work anymore if we have assertions that wait for a node to | |
| 21 | + // join the cluster, e.g. assertThat(cluster).node(3).comesUp(). | |
| 22 | + Host host = findHost(actual, hostNumber); | |
| 23 | + | |
| 24 | + return new HostAssert(host, actual); | |
| 25 | + } | |
| 26 | + | |
| 19 | 27 | /** Utility method to find the {@code Host} object corresponding to a node in a cluster. */ |
| 20 | - public static Host findHost(Cluster cluster, int node) { | |
| 21 | - String address = CCMBridge.ipOfNode(node); | |
| 28 | + public static Host findHost(Cluster cluster, int hostNumber) { | |
| 29 | + String address = CCMBridge.ipOfNode(hostNumber); | |
| 22 | 30 | for (Host host : cluster.getMetadata().getAllHosts()) { |
| 23 | 31 | if (host.getAddress().getHostAddress().equals(address)) |
| 24 | 32 | return host; |
added driver-core/src/test/java/com/datastax/driver/core/HostAssert.java +28 −0
| @@ -0,0 +1,28 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 4 | + | |
| 5 | +import com.datastax.driver.core.policies.LoadBalancingPolicy; | |
| 6 | + | |
| 7 | +import org.assertj.core.api.AbstractAssert; | |
| 8 | + | |
| 9 | +public class HostAssert extends AbstractAssert<HostAssert, Host> { | |
| 10 | + | |
| 11 | + private final Cluster cluster; | |
| 12 | + | |
| 13 | + protected HostAssert(Host host, Cluster cluster) { | |
| 14 | + super(host, HostAssert.class); | |
| 15 | + this.cluster = cluster; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public HostAssert hasState(Host.State expected) { | |
| 19 | + assertThat(actual.state).isEqualTo(expected); | |
| 20 | + return this; | |
| 21 | + } | |
| 22 | + | |
| 23 | + public HostAssert isAtDistance(HostDistance expected) { | |
| 24 | + LoadBalancingPolicy loadBalancingPolicy = cluster.manager.loadBalancingPolicy(); | |
| 25 | + assertThat(loadBalancingPolicy.distance(actual)).isEqualTo(expected); | |
| 26 | + return this; | |
| 27 | + } | |
| 28 | +} |
added driver-core/src/test/java/com/datastax/driver/core/SessionAssert.java +25 −0
| @@ -0,0 +1,25 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import org.assertj.core.api.AbstractAssert; | |
| 4 | + | |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 6 | + | |
| 7 | +public class SessionAssert extends AbstractAssert<SessionAssert, SessionManager> { | |
| 8 | + | |
| 9 | + protected SessionAssert(Session actual) { | |
| 10 | + // We are cheating a bit by casting, but this is the only implementation anyway | |
| 11 | + super((SessionManager)actual, SessionAssert.class); | |
| 12 | + } | |
| 13 | + | |
| 14 | + public SessionAssert hasPoolFor(int hostNumber) { | |
| 15 | + Host host = ClusterAssert.findHost(actual.cluster, hostNumber); | |
| 16 | + assertThat(actual.pools.containsKey(host)).isTrue(); | |
| 17 | + return this; | |
| 18 | + } | |
| 19 | + | |
| 20 | + public SessionAssert hasNoPoolFor(int hostNumber) { | |
| 21 | + Host host = ClusterAssert.findHost(actual.cluster, hostNumber); | |
| 22 | + assertThat(actual.pools.containsKey(host)).isFalse(); | |
| 23 | + return this; | |
| 24 | + } | |
| 25 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/policies/DelegatingLoadBalancingPolicy.java +1 −1
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | * NB: this is currently only used in tests, but could be provided as a convenience in the production code. |
| 15 | 15 | */ |
| 16 | 16 | public abstract class DelegatingLoadBalancingPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { |
| 17 | - private final LoadBalancingPolicy delegate; | |
| 17 | + protected final LoadBalancingPolicy delegate; | |
| 18 | 18 | |
| 19 | 19 | public DelegatingLoadBalancingPolicy(LoadBalancingPolicy delegate) { |
| 20 | 20 | this.delegate = delegate; |
added driver-core/src/test/java/com/datastax/driver/core/policies/LimitingLoadBalancingPolicy.java +142 −0
| @@ -0,0 +1,142 @@ | ||
| 1 | +package com.datastax.driver.core.policies; | |
| 2 | + | |
| 3 | +import java.util.*; | |
| 4 | +import java.util.concurrent.locks.Lock; | |
| 5 | +import java.util.concurrent.locks.ReentrantLock; | |
| 6 | + | |
| 7 | +import com.google.common.collect.Sets; | |
| 8 | + | |
| 9 | +import com.datastax.driver.core.Cluster; | |
| 10 | +import com.datastax.driver.core.Host; | |
| 11 | +import com.datastax.driver.core.HostDistance; | |
| 12 | +import com.datastax.driver.core.Statement; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Sample load balancing policy that limits the number of nodes the driver connects to. | |
| 16 | + * | |
| 17 | + * If more nodes are available, they are marked as IGNORED. When one of the "chosen" nodes | |
| 18 | + * goes down, the policy picks one of the ignored nodes to replace it. | |
| 19 | + * <p> | |
| 20 | + * This kind of policy can be used to alleviate the load on a cluster that has a lot of | |
| 21 | + * clients. | |
| 22 | + * <p> | |
| 23 | + * For simplicity, this policy does not distinguish LOCAL and REMOTE nodes. | |
| 24 | + */ | |
| 25 | +public class LimitingLoadBalancingPolicy extends DelegatingLoadBalancingPolicy { | |
| 26 | + private final int maxHosts; | |
| 27 | + private final int threshold; | |
| 28 | + private final Set<Host> liveHosts = Sets.newConcurrentHashSet(); | |
| 29 | + private final Set<Host> chosenHosts = Sets.newConcurrentHashSet(); | |
| 30 | + private final Lock updateLock = new ReentrantLock(); | |
| 31 | + | |
| 32 | + private volatile Cluster cluster; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * @param delegate the underlying policy that will be fed with the chosen nodes | |
| 36 | + * @param maxHosts the maximum number of chosen nodes | |
| 37 | + * @param threshold how many chosen nodes we accept to lose before we start picking new ones | |
| 38 | + */ | |
| 39 | + public LimitingLoadBalancingPolicy(LoadBalancingPolicy delegate, int maxHosts, int threshold) { | |
| 40 | + super(delegate); | |
| 41 | + this.maxHosts = maxHosts; | |
| 42 | + this.threshold = threshold; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public void init(Cluster cluster, Collection<Host> hosts) { | |
| 47 | + this.cluster = cluster; | |
| 48 | + for (Host host : hosts) | |
| 49 | + if (host.isUp()) | |
| 50 | + this.liveHosts.add(host); | |
| 51 | + this.delegate.init(cluster, Collections.<Host>emptyList()); | |
| 52 | + updateChosenHosts(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + private void updateChosenHosts() { | |
| 56 | + if (chosenHosts.size() > maxHosts - threshold || liveHosts.size() == 0) | |
| 57 | + return; | |
| 58 | + | |
| 59 | + // We lock to prevent two events from triggering this simultaneously. | |
| 60 | + updateLock.lock(); | |
| 61 | + try { | |
| 62 | + int missing = maxHosts - chosenHosts.size(); | |
| 63 | + if (missing < threshold || liveHosts.size() == 0) | |
| 64 | + return; | |
| 65 | + Set<Host> newlyChosen = new HashSet<Host>(); | |
| 66 | + | |
| 67 | + for (Host host : liveHosts) { | |
| 68 | + // Note that this picks hosts whatever their distance is. | |
| 69 | + // We can't reliably call childPolicy.distance() here, because the childPolicy | |
| 70 | + // might require hosts to be already added to compute their distance properly | |
| 71 | + // (this is the case for DCAware policy). | |
| 72 | + newlyChosen.add(host); | |
| 73 | + missing -= 1; | |
| 74 | + if (missing == 0) | |
| 75 | + break; | |
| 76 | + } | |
| 77 | + | |
| 78 | + chosenHosts.addAll(newlyChosen); | |
| 79 | + liveHosts.removeAll(newlyChosen); | |
| 80 | + for (Host host : newlyChosen) { | |
| 81 | + delegate.onAdd(host); | |
| 82 | + | |
| 83 | + // delegate should have updated the distance, inform the driver so that it can | |
| 84 | + // recreate the pool. | |
| 85 | + cluster.getConfiguration().getPoolingOptions().refreshConnectedHost(host); | |
| 86 | + } | |
| 87 | + } finally { | |
| 88 | + updateLock.unlock(); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + | |
| 92 | + @Override | |
| 93 | + public HostDistance distance(Host host) { | |
| 94 | + if (chosenHosts.contains(host)) | |
| 95 | + return delegate.distance(host); | |
| 96 | + else | |
| 97 | + return HostDistance.IGNORED; | |
| 98 | + } | |
| 99 | + | |
| 100 | + @Override | |
| 101 | + public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | |
| 102 | + // Since we only add chosen nodes to the child policy, its query plan will only contain chosen nodes | |
| 103 | + return delegate.newQueryPlan(loggedKeyspace, statement); | |
| 104 | + } | |
| 105 | + | |
| 106 | + @Override | |
| 107 | + public void onAdd(Host host) { | |
| 108 | + liveHosts.add(host); | |
| 109 | + // update in case we didn't have enough chosen hosts before the addition | |
| 110 | + updateChosenHosts(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + @Override | |
| 114 | + public void onUp(Host host) { | |
| 115 | + onAdd(host); | |
| 116 | + } | |
| 117 | + | |
| 118 | + @Override | |
| 119 | + public void onSuspected(Host host) { | |
| 120 | + // If a node gets suspected, it means the driver was connected to it, so it was a chosen node. | |
| 121 | + // Keep it as chosen for now, but notify the child policy which might reorder the query plan accordingly. | |
| 122 | + delegate.onSuspected(host); | |
| 123 | + } | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + public void onDown(Host host) { | |
| 127 | + delegate.onDown(host); | |
| 128 | + | |
| 129 | + liveHosts.remove(host); | |
| 130 | + chosenHosts.remove(host); | |
| 131 | + updateChosenHosts(); | |
| 132 | + } | |
| 133 | + | |
| 134 | + @Override | |
| 135 | + public void onRemove(Host host) { | |
| 136 | + delegate.onRemove(host); | |
| 137 | + | |
| 138 | + liveHosts.remove(host); | |
| 139 | + chosenHosts.remove(host); | |
| 140 | + updateChosenHosts(); | |
| 141 | + } | |
| 142 | +} |