kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2a1d4419b6b0eac655aa3f1d2460cd638ee5ebd924c379910ce40b30d9876e14 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-13 18:51 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-13 18:57 +02:00 |
| Parents | ebf55fe1e06d |
12 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | - [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496) |
| 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 | +- [improvement] Expose close method on load balancing policy (JAVA-463) | |
| 14 | 15 | |
| 15 | 16 | |
| 16 | 17 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +5 −0
| @@ -1251,6 +1251,11 @@ | ||
| 1251 | 1251 | if (metrics != null) |
| 1252 | 1252 | metrics.shutdown(); |
| 1253 | 1253 | |
| 1254 | + // And the load balancing policy | |
| 1255 | + LoadBalancingPolicy loadBalancingPolicy = loadBalancingPolicy(); | |
| 1256 | + if (loadBalancingPolicy instanceof CloseableLoadBalancingPolicy) | |
| 1257 | + ((CloseableLoadBalancingPolicy)loadBalancingPolicy).close(); | |
| 1258 | + | |
| 1254 | 1259 | // Then we shutdown all connections |
| 1255 | 1260 | List<CloseFuture> futures = new ArrayList<CloseFuture>(sessions.size() + 1); |
| 1256 | 1261 | futures.add(controlConnection.closeAsync()); |
added driver-core/src/main/java/com/datastax/driver/core/policies/CloseableLoadBalancingPolicy.java +31 −0
| @@ -0,0 +1,31 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.policies; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * A load balancing policy that wants to be notified at cluster shutdown. | |
| 20 | + * | |
| 21 | + * The only reason that this is separate from {@link LoadBalancingPolicy} is to avoid breaking binary compatibility at the | |
| 22 | + * time this was introduced (2.0.7 / 2.1.3). It might be merged with the parent interface in a future major version. | |
| 23 | + */ | |
| 24 | +public interface CloseableLoadBalancingPolicy extends LoadBalancingPolicy { | |
| 25 | + /** | |
| 26 | + * Gets invoked at cluster shutdown. | |
| 27 | + * | |
| 28 | + * This gives the policy the opportunity to perform some cleanup, for instance stop threads that it might have started. | |
| 29 | + */ | |
| 30 | + void close(); | |
| 31 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/policies/DCAwareRoundRobinPolicy.java +6 −1
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | * incurs a slight overhead so the {@code LoadBalancingPolicy.RoundRobin} |
| 52 | 52 | * policy could be preferred to this policy in that case. |
| 53 | 53 | */ |
| 54 | -public class DCAwareRoundRobinPolicy implements LoadBalancingPolicy { | |
| 54 | +public class DCAwareRoundRobinPolicy implements LoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 55 | 55 | |
| 56 | 56 | private static final Logger logger = LoggerFactory.getLogger(DCAwareRoundRobinPolicy.class); |
| 57 | 57 | |
| @@ -415,4 +415,9 @@ | ||
| 415 | 415 | public void onRemove(Host host) { |
| 416 | 416 | onDown(host); |
| 417 | 417 | } |
| 418 | + | |
| 419 | + @Override | |
| 420 | + public void close() { | |
| 421 | + // nothing to do | |
| 422 | + } | |
| 418 | 423 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/LatencyAwarePolicy.java +7 −1
| @@ -57,7 +57,7 @@ | ||
| 57 | 57 | * |
| 58 | 58 | * @since 1.0.4 |
| 59 | 59 | */ |
| 60 | -public class LatencyAwarePolicy implements ChainableLoadBalancingPolicy { | |
| 60 | +public class LatencyAwarePolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 61 | 61 | |
| 62 | 62 | private static final Logger logger = LoggerFactory.getLogger(LatencyAwarePolicy.class); |
| 63 | 63 | |
| @@ -688,4 +688,10 @@ | ||
| 688 | 688 | return new LatencyAwarePolicy(childPolicy, exclusionThreshold, scale, retryPeriod, updateRate, minMeasure); |
| 689 | 689 | } |
| 690 | 690 | } |
| 691 | + | |
| 692 | + @Override | |
| 693 | + public void close() { | |
| 694 | + if (childPolicy instanceof CloseableLoadBalancingPolicy) | |
| 695 | + ((CloseableLoadBalancingPolicy)childPolicy).close(); | |
| 696 | + } | |
| 691 | 697 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/RoundRobinPolicy.java +6 −1
| @@ -48,7 +48,7 @@ | ||
| 48 | 48 | * datacenter this will be inefficient and you will want to use the |
| 49 | 49 | * {@link DCAwareRoundRobinPolicy} load balancing policy instead. |
| 50 | 50 | */ |
| 51 | -public class RoundRobinPolicy implements LoadBalancingPolicy { | |
| 51 | +public class RoundRobinPolicy implements LoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 52 | 52 | |
| 53 | 53 | private static final Logger logger = LoggerFactory.getLogger(RoundRobinPolicy.class); |
| 54 | 54 | |
| @@ -198,4 +198,9 @@ | ||
| 198 | 198 | public void onRemove(Host host) { |
| 199 | 199 | onDown(host); |
| 200 | 200 | } |
| 201 | + | |
| 202 | + @Override | |
| 203 | + public void close() { | |
| 204 | + // nothing to do | |
| 205 | + } | |
| 201 | 206 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/TokenAwarePolicy.java +7 −1
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | * token aware policy, replicas from remote data centers may only be |
| 47 | 47 | * returned after all the host of the local data center. |
| 48 | 48 | */ |
| 49 | -public class TokenAwarePolicy implements ChainableLoadBalancingPolicy { | |
| 49 | +public class TokenAwarePolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 50 | 50 | |
| 51 | 51 | private final LoadBalancingPolicy childPolicy; |
| 52 | 52 | private Metadata clusterMetadata; |
| @@ -161,4 +161,10 @@ | ||
| 161 | 161 | public void onRemove(Host host) { |
| 162 | 162 | childPolicy.onRemove(host); |
| 163 | 163 | } |
| 164 | + | |
| 165 | + @Override | |
| 166 | + public void close() { | |
| 167 | + if (childPolicy instanceof CloseableLoadBalancingPolicy) | |
| 168 | + ((CloseableLoadBalancingPolicy)childPolicy).close(); | |
| 169 | + } | |
| 164 | 170 | } |
modified driver-core/src/main/java/com/datastax/driver/core/policies/WhiteListPolicy.java +7 −1
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | * data-center then you should use DCAwareRoundRobinPolicy and *not* this policy |
| 48 | 48 | * in particular. |
| 49 | 49 | */ |
| 50 | -public class WhiteListPolicy implements ChainableLoadBalancingPolicy { | |
| 50 | +public class WhiteListPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 51 | 51 | private final LoadBalancingPolicy childPolicy; |
| 52 | 52 | private final Set<InetSocketAddress> whiteList; |
| 53 | 53 | |
| @@ -153,4 +153,10 @@ | ||
| 153 | 153 | if (whiteList.contains(host.getSocketAddress())) |
| 154 | 154 | childPolicy.onRemove(host); |
| 155 | 155 | } |
| 156 | + | |
| 157 | + @Override | |
| 158 | + public void close() { | |
| 159 | + if (childPolicy instanceof CloseableLoadBalancingPolicy) | |
| 160 | + ((CloseableLoadBalancingPolicy)childPolicy).close(); | |
| 161 | + } | |
| 156 | 162 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +8 −40
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | package com.datastax.driver.core; |
| 2 | 2 | |
| 3 | -import java.util.Collection; | |
| 4 | 3 | import java.util.Iterator; |
| 5 | 4 | import java.util.concurrent.TimeUnit; |
| 6 | 5 | import java.util.concurrent.atomic.AtomicInteger; |
| @@ -9,6 +8,7 @@ | ||
| 9 | 8 | |
| 10 | 9 | import static org.assertj.core.api.Assertions.assertThat; |
| 11 | 10 | |
| 11 | +import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy; | |
| 12 | 12 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 13 | 13 | import com.datastax.driver.core.policies.Policies; |
| 14 | 14 | import com.datastax.driver.core.policies.ReconnectionPolicy; |
| @@ -22,9 +22,7 @@ | ||
| 22 | 22 | // Custom load balancing policy that counts the number of calls to newQueryPlan(). |
| 23 | 23 | // Since we don't open any session from our Cluster, only the control connection reattempts are calling this |
| 24 | 24 | // method, therefore the invocation count is equal to the number of attempts. |
| 25 | - final AtomicInteger reconnectionAttempts = new AtomicInteger(); | |
| 26 | - LoadBalancingPolicy loadBalancingPolicy = new QueryPlanCountingPolicy(Policies.defaultLoadBalancingPolicy(), | |
| 27 | - reconnectionAttempts); | |
| 25 | + QueryPlanCountingPolicy loadBalancingPolicy = new QueryPlanCountingPolicy(Policies.defaultLoadBalancingPolicy()); | |
| 28 | 26 | |
| 29 | 27 | // Custom reconnection policy with a very large delay (longer than the test duration), to make sure we count |
| 30 | 28 | // only the first reconnection attempt of each reconnection handler. |
| @@ -60,50 +58,20 @@ | ||
| 60 | 58 | if (ccm != null) |
| 61 | 59 | ccm.remove(); |
| 62 | 60 | } |
| 63 | - assertThat(reconnectionAttempts.get()).isEqualTo(1); | |
| 61 | + assertThat(loadBalancingPolicy.counter.get()).isEqualTo(1); | |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | - static class QueryPlanCountingPolicy implements LoadBalancingPolicy { | |
| 64 | + static class QueryPlanCountingPolicy extends DelegatingLoadBalancingPolicy { | |
| 67 | 65 | |
| 68 | - private final LoadBalancingPolicy childPolicy; | |
| 69 | - private final AtomicInteger counter; | |
| 66 | + final AtomicInteger counter = new AtomicInteger(); | |
| 70 | 67 | |
| 71 | - public QueryPlanCountingPolicy(LoadBalancingPolicy childPolicy, AtomicInteger counter) { | |
| 72 | - this.childPolicy = childPolicy; | |
| 73 | - this.counter = counter; | |
| 68 | + public QueryPlanCountingPolicy(LoadBalancingPolicy delegate) { | |
| 69 | + super(delegate); | |
| 74 | 70 | } |
| 75 | 71 | |
| 76 | 72 | public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { |
| 77 | 73 | counter.incrementAndGet(); |
| 78 | - return childPolicy.newQueryPlan(loggedKeyspace, statement); | |
| 79 | - } | |
| 80 | - | |
| 81 | - public void init(Cluster cluster, Collection<Host> hosts) { | |
| 82 | - childPolicy.init(cluster, hosts); | |
| 83 | - } | |
| 84 | - | |
| 85 | - public HostDistance distance(Host host) { | |
| 86 | - return childPolicy.distance(host); | |
| 87 | - } | |
| 88 | - | |
| 89 | - public void onAdd(Host host) { | |
| 90 | - childPolicy.onAdd(host); | |
| 91 | - } | |
| 92 | - | |
| 93 | - public void onUp(Host host) { | |
| 94 | - childPolicy.onUp(host); | |
| 95 | - } | |
| 96 | - | |
| 97 | - public void onSuspected(Host host) { | |
| 98 | - childPolicy.onSuspected(host); | |
| 99 | - } | |
| 100 | - | |
| 101 | - public void onDown(Host host) { | |
| 102 | - childPolicy.onDown(host); | |
| 103 | - } | |
| 104 | - | |
| 105 | - public void onRemove(Host host) { | |
| 106 | - childPolicy.onRemove(host); | |
| 74 | + return super.newQueryPlan(loggedKeyspace, statement); | |
| 107 | 75 | } |
| 108 | 76 | } |
| 109 | 77 | } |
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyBootstrapTest.java +12 −25
| @@ -17,13 +17,14 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.io.PrintWriter; |
| 19 | 19 | import java.io.StringWriter; |
| 20 | -import java.util.Collection; | |
| 21 | -import java.util.Iterator; | |
| 22 | 20 | |
| 23 | -import org.testng.annotations.*; | |
| 21 | +import org.testng.annotations.AfterClass; | |
| 22 | +import org.testng.annotations.BeforeClass; | |
| 23 | +import org.testng.annotations.Test; | |
| 24 | 24 | |
| 25 | 25 | import static org.testng.Assert.assertEquals; |
| 26 | 26 | |
| 27 | +import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy; | |
| 27 | 28 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 28 | 29 | import com.datastax.driver.core.policies.RoundRobinPolicy; |
| 29 | 30 | |
| @@ -52,9 +53,8 @@ | ||
| 52 | 53 | c.discard(); |
| 53 | 54 | } |
| 54 | 55 | |
| 55 | - static class CountingPolicy implements LoadBalancingPolicy { | |
| 56 | + static class CountingPolicy extends DelegatingLoadBalancingPolicy { | |
| 56 | 57 | |
| 57 | - private final LoadBalancingPolicy delegate; | |
| 58 | 58 | int adds; |
| 59 | 59 | int suspecteds; |
| 60 | 60 | int removes; |
| @@ -64,51 +64,38 @@ | ||
| 64 | 64 | final StringWriter history = new StringWriter(); |
| 65 | 65 | private final PrintWriter out = new PrintWriter(history); |
| 66 | 66 | |
| 67 | - CountingPolicy(LoadBalancingPolicy delegate) { | |
| 68 | - this.delegate = delegate; | |
| 67 | + public CountingPolicy(LoadBalancingPolicy delegate) { | |
| 68 | + super(delegate); | |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | public void onAdd(Host host) { |
| 72 | 72 | out.printf("add %s%n", host); |
| 73 | 73 | adds++; |
| 74 | - delegate.onAdd(host); | |
| 74 | + super.onAdd(host); | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | public void onSuspected(Host host) { |
| 78 | 78 | out.printf("suspect %s%n", host); |
| 79 | 79 | suspecteds++; |
| 80 | - delegate.onSuspected(host); | |
| 80 | + super.onSuspected(host); | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | public void onUp(Host host) { |
| 84 | 84 | out.printf("up %s%n", host); |
| 85 | 85 | ups++; |
| 86 | - delegate.onUp(host); | |
| 86 | + super.onUp(host); | |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | public void onDown(Host host) { |
| 90 | 90 | out.printf("down %s%n", host); |
| 91 | 91 | downs++; |
| 92 | - delegate.onDown(host); | |
| 92 | + super.onDown(host); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public void onRemove(Host host) { |
| 96 | 96 | out.printf("remove %s%n", host); |
| 97 | 97 | removes++; |
| 98 | - delegate.onRemove(host); | |
| 99 | - } | |
| 100 | - | |
| 101 | - public void init(Cluster cluster, Collection<Host> hosts) { | |
| 102 | - delegate.init(cluster, hosts); | |
| 103 | - } | |
| 104 | - | |
| 105 | - public HostDistance distance(Host host) { | |
| 106 | - return delegate.distance(host); | |
| 107 | - } | |
| 108 | - | |
| 109 | - @Override | |
| 110 | - public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | |
| 111 | - return delegate.newQueryPlan(loggedKeyspace, statement); | |
| 98 | + super.onRemove(host); | |
| 112 | 99 | } |
| 113 | 100 | } |
| 114 | 101 | } |
added driver-core/src/test/java/com/datastax/driver/core/policies/CloseableLoadBalancingPolicyTest.java +45 −0
| @@ -0,0 +1,45 @@ | ||
| 1 | +package com.datastax.driver.core.policies; | |
| 2 | + | |
| 3 | +import org.testng.annotations.Test; | |
| 4 | + | |
| 5 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 6 | + | |
| 7 | +import com.datastax.driver.core.CCMBridge; | |
| 8 | +import com.datastax.driver.core.Cluster; | |
| 9 | + | |
| 10 | +public class CloseableLoadBalancingPolicyTest { | |
| 11 | + @Test(groups = "short") | |
| 12 | + public void should_be_invoked_at_shutdown() { | |
| 13 | + CloseMonitoringPolicy policy = new CloseMonitoringPolicy(Policies.defaultLoadBalancingPolicy()); | |
| 14 | + CCMBridge ccm = null; | |
| 15 | + Cluster cluster = null; | |
| 16 | + try { | |
| 17 | + ccm = CCMBridge.create("test", 1); | |
| 18 | + cluster = Cluster.builder() | |
| 19 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 20 | + .withLoadBalancingPolicy(policy) | |
| 21 | + .build(); | |
| 22 | + cluster.connect(); | |
| 23 | + } finally { | |
| 24 | + if (cluster != null) | |
| 25 | + cluster.close(); | |
| 26 | + if (ccm != null) | |
| 27 | + ccm.remove(); | |
| 28 | + } | |
| 29 | + assertThat(policy.wasClosed).isTrue(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + static class CloseMonitoringPolicy extends DelegatingLoadBalancingPolicy { | |
| 33 | + | |
| 34 | + volatile boolean wasClosed = false; | |
| 35 | + | |
| 36 | + public CloseMonitoringPolicy(LoadBalancingPolicy delegate) { | |
| 37 | + super(delegate); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + public void close() { | |
| 42 | + wasClosed = true; | |
| 43 | + } | |
| 44 | + } | |
| 45 | +} |
added driver-core/src/test/java/com/datastax/driver/core/policies/DelegatingLoadBalancingPolicy.java +65 −0
| @@ -0,0 +1,65 @@ | ||
| 1 | +package com.datastax.driver.core.policies; | |
| 2 | + | |
| 3 | +import java.util.Collection; | |
| 4 | +import java.util.Iterator; | |
| 5 | + | |
| 6 | +import com.datastax.driver.core.Cluster; | |
| 7 | +import com.datastax.driver.core.Host; | |
| 8 | +import com.datastax.driver.core.HostDistance; | |
| 9 | +import com.datastax.driver.core.Statement; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Base class for tests that want to wrap a policy to add some instrumentation. | |
| 13 | + * | |
| 14 | + * NB: this is currently only used in tests, but could be provided as a convenience in the production code. | |
| 15 | + */ | |
| 16 | +public abstract class DelegatingLoadBalancingPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy { | |
| 17 | + private final LoadBalancingPolicy delegate; | |
| 18 | + | |
| 19 | + public DelegatingLoadBalancingPolicy(LoadBalancingPolicy delegate) { | |
| 20 | + this.delegate = delegate; | |
| 21 | + } | |
| 22 | + | |
| 23 | + public void init(Cluster cluster, Collection<Host> hosts) { | |
| 24 | + delegate.init(cluster, hosts); | |
| 25 | + } | |
| 26 | + | |
| 27 | + public HostDistance distance(Host host) { | |
| 28 | + return delegate.distance(host); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) { | |
| 32 | + return delegate.newQueryPlan(loggedKeyspace, statement); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public void onAdd(Host host) { | |
| 36 | + delegate.onAdd(host); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public void onUp(Host host) { | |
| 40 | + delegate.onUp(host); | |
| 41 | + } | |
| 42 | + | |
| 43 | + public void onSuspected(Host host) { | |
| 44 | + delegate.onSuspected(host); | |
| 45 | + } | |
| 46 | + | |
| 47 | + public void onDown(Host host) { | |
| 48 | + delegate.onDown(host); | |
| 49 | + } | |
| 50 | + | |
| 51 | + public void onRemove(Host host) { | |
| 52 | + delegate.onRemove(host); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public LoadBalancingPolicy getChildPolicy() { | |
| 57 | + return delegate; | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public void close() { | |
| 62 | + if (delegate instanceof CloseableLoadBalancingPolicy) | |
| 63 | + ((CloseableLoadBalancingPolicy)delegate).close(); | |
| 64 | + } | |
| 65 | +} | |
| No newline at the end of the file |