Elffuss BraidHub

kikocis/java-driver public

Expose close method on load balancing policy (JAVA-463).
Changeset2a1d4419b6b0eac655aa3f1d2460cd638ee5ebd924c379910ce40b30d9876e14
Authorolim7t <omichallat+github@gmail.com> · 2014-10-13 18:51 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-13 18:57 +02:00
Parentsebf55fe1e06d

Browse the files at this changeset

12 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -11,6 +11,7 @@
1111 - [bug] Fix DCAwareRoundRobinPolicy datacenter auto-discovery (JAVA-496)
1212 - [bug] Ensure control connection does not trigger concurrent reconnects (JAVA-497)
1313 - [improvement] Keep trying to reconnect on authentication errors (JAVA-472)
14+- [improvement] Expose close method on load balancing policy (JAVA-463)
1415
1516
1617 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +5 −0
@@ -1251,6 +1251,11 @@
12511251 if (metrics != null)
12521252 metrics.shutdown();
12531253
1254+ // And the load balancing policy
1255+ LoadBalancingPolicy loadBalancingPolicy = loadBalancingPolicy();
1256+ if (loadBalancingPolicy instanceof CloseableLoadBalancingPolicy)
1257+ ((CloseableLoadBalancingPolicy)loadBalancingPolicy).close();
1258+
12541259 // Then we shutdown all connections
12551260 List<CloseFuture> futures = new ArrayList<CloseFuture>(sessions.size() + 1);
12561261 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 @@
5151 * incurs a slight overhead so the {@code LoadBalancingPolicy.RoundRobin}
5252 * policy could be preferred to this policy in that case.
5353 */
54-public class DCAwareRoundRobinPolicy implements LoadBalancingPolicy {
54+public class DCAwareRoundRobinPolicy implements LoadBalancingPolicy, CloseableLoadBalancingPolicy {
5555
5656 private static final Logger logger = LoggerFactory.getLogger(DCAwareRoundRobinPolicy.class);
5757
@@ -415,4 +415,9 @@
415415 public void onRemove(Host host) {
416416 onDown(host);
417417 }
418+
419+ @Override
420+ public void close() {
421+ // nothing to do
422+ }
418423 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/LatencyAwarePolicy.java +7 −1
@@ -57,7 +57,7 @@
5757 *
5858 * @since 1.0.4
5959 */
60-public class LatencyAwarePolicy implements ChainableLoadBalancingPolicy {
60+public class LatencyAwarePolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy {
6161
6262 private static final Logger logger = LoggerFactory.getLogger(LatencyAwarePolicy.class);
6363
@@ -688,4 +688,10 @@
688688 return new LatencyAwarePolicy(childPolicy, exclusionThreshold, scale, retryPeriod, updateRate, minMeasure);
689689 }
690690 }
691+
692+ @Override
693+ public void close() {
694+ if (childPolicy instanceof CloseableLoadBalancingPolicy)
695+ ((CloseableLoadBalancingPolicy)childPolicy).close();
696+ }
691697 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/RoundRobinPolicy.java +6 −1
@@ -48,7 +48,7 @@
4848 * datacenter this will be inefficient and you will want to use the
4949 * {@link DCAwareRoundRobinPolicy} load balancing policy instead.
5050 */
51-public class RoundRobinPolicy implements LoadBalancingPolicy {
51+public class RoundRobinPolicy implements LoadBalancingPolicy, CloseableLoadBalancingPolicy {
5252
5353 private static final Logger logger = LoggerFactory.getLogger(RoundRobinPolicy.class);
5454
@@ -198,4 +198,9 @@
198198 public void onRemove(Host host) {
199199 onDown(host);
200200 }
201+
202+ @Override
203+ public void close() {
204+ // nothing to do
205+ }
201206 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/TokenAwarePolicy.java +7 −1
@@ -46,7 +46,7 @@
4646 * token aware policy, replicas from remote data centers may only be
4747 * returned after all the host of the local data center.
4848 */
49-public class TokenAwarePolicy implements ChainableLoadBalancingPolicy {
49+public class TokenAwarePolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy {
5050
5151 private final LoadBalancingPolicy childPolicy;
5252 private Metadata clusterMetadata;
@@ -161,4 +161,10 @@
161161 public void onRemove(Host host) {
162162 childPolicy.onRemove(host);
163163 }
164+
165+ @Override
166+ public void close() {
167+ if (childPolicy instanceof CloseableLoadBalancingPolicy)
168+ ((CloseableLoadBalancingPolicy)childPolicy).close();
169+ }
164170 }
modified driver-core/src/main/java/com/datastax/driver/core/policies/WhiteListPolicy.java +7 −1
@@ -47,7 +47,7 @@
4747 * data-center then you should use DCAwareRoundRobinPolicy and *not* this policy
4848 * in particular.
4949 */
50-public class WhiteListPolicy implements ChainableLoadBalancingPolicy {
50+public class WhiteListPolicy implements ChainableLoadBalancingPolicy, CloseableLoadBalancingPolicy {
5151 private final LoadBalancingPolicy childPolicy;
5252 private final Set<InetSocketAddress> whiteList;
5353
@@ -153,4 +153,10 @@
153153 if (whiteList.contains(host.getSocketAddress()))
154154 childPolicy.onRemove(host);
155155 }
156+
157+ @Override
158+ public void close() {
159+ if (childPolicy instanceof CloseableLoadBalancingPolicy)
160+ ((CloseableLoadBalancingPolicy)childPolicy).close();
161+ }
156162 }
modified driver-core/src/test/java/com/datastax/driver/core/ControlConnectionTest.java +8 −40
@@ -1,6 +1,5 @@
11 package com.datastax.driver.core;
22
3-import java.util.Collection;
43 import java.util.Iterator;
54 import java.util.concurrent.TimeUnit;
65 import java.util.concurrent.atomic.AtomicInteger;
@@ -9,6 +8,7 @@
98
109 import static org.assertj.core.api.Assertions.assertThat;
1110
11+import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy;
1212 import com.datastax.driver.core.policies.LoadBalancingPolicy;
1313 import com.datastax.driver.core.policies.Policies;
1414 import com.datastax.driver.core.policies.ReconnectionPolicy;
@@ -22,9 +22,7 @@
2222 // Custom load balancing policy that counts the number of calls to newQueryPlan().
2323 // Since we don't open any session from our Cluster, only the control connection reattempts are calling this
2424 // 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());
2826
2927 // Custom reconnection policy with a very large delay (longer than the test duration), to make sure we count
3028 // only the first reconnection attempt of each reconnection handler.
@@ -60,50 +58,20 @@
6058 if (ccm != null)
6159 ccm.remove();
6260 }
63- assertThat(reconnectionAttempts.get()).isEqualTo(1);
61+ assertThat(loadBalancingPolicy.counter.get()).isEqualTo(1);
6462 }
6563
66- static class QueryPlanCountingPolicy implements LoadBalancingPolicy {
64+ static class QueryPlanCountingPolicy extends DelegatingLoadBalancingPolicy {
6765
68- private final LoadBalancingPolicy childPolicy;
69- private final AtomicInteger counter;
66+ final AtomicInteger counter = new AtomicInteger();
7067
71- public QueryPlanCountingPolicy(LoadBalancingPolicy childPolicy, AtomicInteger counter) {
72- this.childPolicy = childPolicy;
73- this.counter = counter;
68+ public QueryPlanCountingPolicy(LoadBalancingPolicy delegate) {
69+ super(delegate);
7470 }
7571
7672 public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) {
7773 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);
10775 }
10876 }
10977 }
modified driver-core/src/test/java/com/datastax/driver/core/LoadBalancingPolicyBootstrapTest.java +12 −25
@@ -17,13 +17,14 @@
1717
1818 import java.io.PrintWriter;
1919 import java.io.StringWriter;
20-import java.util.Collection;
21-import java.util.Iterator;
2220
23-import org.testng.annotations.*;
21+import org.testng.annotations.AfterClass;
22+import org.testng.annotations.BeforeClass;
23+import org.testng.annotations.Test;
2424
2525 import static org.testng.Assert.assertEquals;
2626
27+import com.datastax.driver.core.policies.DelegatingLoadBalancingPolicy;
2728 import com.datastax.driver.core.policies.LoadBalancingPolicy;
2829 import com.datastax.driver.core.policies.RoundRobinPolicy;
2930
@@ -52,9 +53,8 @@
5253 c.discard();
5354 }
5455
55- static class CountingPolicy implements LoadBalancingPolicy {
56+ static class CountingPolicy extends DelegatingLoadBalancingPolicy {
5657
57- private final LoadBalancingPolicy delegate;
5858 int adds;
5959 int suspecteds;
6060 int removes;
@@ -64,51 +64,38 @@
6464 final StringWriter history = new StringWriter();
6565 private final PrintWriter out = new PrintWriter(history);
6666
67- CountingPolicy(LoadBalancingPolicy delegate) {
68- this.delegate = delegate;
67+ public CountingPolicy(LoadBalancingPolicy delegate) {
68+ super(delegate);
6969 }
7070
7171 public void onAdd(Host host) {
7272 out.printf("add %s%n", host);
7373 adds++;
74- delegate.onAdd(host);
74+ super.onAdd(host);
7575 }
7676
7777 public void onSuspected(Host host) {
7878 out.printf("suspect %s%n", host);
7979 suspecteds++;
80- delegate.onSuspected(host);
80+ super.onSuspected(host);
8181 }
8282
8383 public void onUp(Host host) {
8484 out.printf("up %s%n", host);
8585 ups++;
86- delegate.onUp(host);
86+ super.onUp(host);
8787 }
8888
8989 public void onDown(Host host) {
9090 out.printf("down %s%n", host);
9191 downs++;
92- delegate.onDown(host);
92+ super.onDown(host);
9393 }
9494
9595 public void onRemove(Host host) {
9696 out.printf("remove %s%n", host);
9797 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);
11299 }
113100 }
114101 }
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