Elffuss BraidHub

kikocis/java-driver public

Improve tryReconnectOnce Javadoc + fix test.

Signed-off-by: Andrew Tolbert <andrew.tolbert@datastax.com>
Changeset07db25ca83b141f158ba37f2c6adf83c51e4e0de9d8c23d8ca9f85d196baa3a8
Authorolim7t <omichallat+github@gmail.com> · 2015-02-02 11:23 +01:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-02-13 07:25 -06:00
Parentsac197a0a34d1

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/Host.java +9 −6
@@ -205,14 +205,17 @@
205205 }
206206
207207 /**
208- * Triggers an asynchronous one-time reconnection attempt to this host.
208+ * Triggers an asynchronous reconnection attempt to this host.
209209 * <p>
210- * If reconnection succeeds, the host will be marked {@code UP} and its connection pool(s)
211- * will be initialized (unless the load balancing policy returns {@link HostDistance#IGNORED}
212- * for it). If reconnection fails, no further attempts will be scheduled.
210+ * This method is intended for load balancing policies that mark hosts as {@link HostDistance#IGNORED IGNORED},
211+ * but still need a way to periodically check these hosts' states (UP / DOWN).
213212 * <p>
214- * This method has no effect if the node is already {@code UP}, or if a reconnection attempt
215- * is already in progress.
213+ * For a host that is at distance {@code IGNORED}, this method will try to reconnect exactly once: if
214+ * reconnection succeeds, the host is marked {@code UP}; otherwise, no further attempts will be scheduled.
215+ * It has no effect if the node is already {@code UP}, or if a reconnection attempt is already in progress.
216+ * <p>
217+ * Note that if the host is <em>not</em> a distance {@code IGNORED}, this method <em>will</em> trigger a periodic
218+ * reconnection attempt if the reconnection fails.
216219 */
217220 public void tryReconnectOnce() {
218221 this.manager.startSingleReconnectionAttempt(this);
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +41 −19
@@ -5,6 +5,8 @@
55 import java.util.Collections;
66 import java.util.Iterator;
77 import java.util.Map;
8+import java.util.concurrent.ConcurrentHashMap;
9+import java.util.concurrent.ConcurrentMap;
810 import java.util.concurrent.TimeUnit;
911 import java.util.concurrent.atomic.AtomicInteger;
1012
@@ -12,7 +14,9 @@
1214
1315 import com.google.common.base.Charsets;
1416 import com.google.common.collect.ImmutableMap;
17+import com.google.common.util.concurrent.ListenableFuture;
1518 import org.testng.annotations.Test;
19+
1620 import static org.assertj.core.api.Assertions.assertThat;
1721
1822 import com.datastax.driver.core.Host.State;
@@ -33,9 +37,9 @@
3337 ccm = CCMBridge.create("test", 2);
3438 int reconnectionDelay = 1000;
3539 cluster = Cluster.builder()
36- .addContactPoint(CCMBridge.ipOfNode(1))
37- .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelay))
38- .build();
40+ .addContactPoint(CCMBridge.ipOfNode(1))
41+ .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelay))
42+ .build();
3943 cluster.connect();
4044
4145 assertThat(cluster).usesControlHost(1);
@@ -78,11 +82,11 @@
7882 CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMs));
7983
8084 cluster = Cluster.builder()
81- .addContactPoint(CCMBridge.ipOfNode(1))
82- // Start with the correct auth so that we can initialize the server
83- .withAuthProvider(authProvider)
84- .withReconnectionPolicy(reconnectionPolicy)
85- .build();
85+ .addContactPoint(CCMBridge.ipOfNode(1))
86+ // Start with the correct auth so that we can initialize the server
87+ .withAuthProvider(authProvider)
88+ .withReconnectionPolicy(reconnectionPolicy)
89+ .build();
8690
8791 cluster.init();
8892 assertThat(cluster).usesControlHost(1);
@@ -128,9 +132,9 @@
128132 try {
129133 ccm = CCMBridge.create("test", 2);
130134 cluster = Cluster.builder()
131- .addContactPoint(CCMBridge.ipOfNode(1))
132- .withReconnectionPolicy(reconnectionPolicy)
133- .build();
135+ .addContactPoint(CCMBridge.ipOfNode(1))
136+ .withReconnectionPolicy(reconnectionPolicy)
137+ .build();
134138 cluster.connect();
135139
136140 // Stop a node and cancel the reconnection attempts to it
@@ -169,22 +173,25 @@
169173 try {
170174 ccm = CCMBridge.create("test", 1);
171175 cluster = Cluster.builder()
172- .addContactPoint(CCMBridge.ipOfNode(1))
173- .withLoadBalancingPolicy(loadBalancingPolicy)
174- .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis))
175- .build();
176+ .addContactPoint(CCMBridge.ipOfNode(1))
177+ .withLoadBalancingPolicy(loadBalancingPolicy)
178+ .withReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMillis))
179+ .build();
176180 cluster.connect();
177181
178182 // Tweak the LBP so that the control connection never reconnects, otherwise
179183 // it would interfere with the rest of the test (this is a bit of a hack)
180184 loadBalancingPolicy.returnEmptyQueryPlan = true;
181185
182- // Stop the node and cancel the reconnection attempts to it
186+ // Stop the node, ignore it and cancel reconnection attempts to it
183187 ccm.stop(1);
184188 ccm.waitForDown(1);
185189 assertThat(cluster).host(1).goesDownWithin(20, SECONDS);
186190 Host host1 = TestUtils.findHost(cluster, 1);
187- host1.getReconnectionAttemptFuture().cancel(false);
191+ loadBalancingPolicy.setDistance(TestUtils.findHost(cluster, 1), HostDistance.IGNORED);
192+ ListenableFuture<?> reconnectionAttemptFuture = host1.getReconnectionAttemptFuture();
193+ if (reconnectionAttemptFuture != null)
194+ reconnectionAttemptFuture.cancel(false);
188195
189196 // Trigger a one-time reconnection attempt (this will fail)
190197 host1.tryReconnectOnce();
@@ -262,7 +269,7 @@
262269 Map<String, String> getCredentials() {
263270 count.incrementAndGet();
264271 return ImmutableMap.of("username", username,
265- "password", password);
272+ "password", password);
266273 }
267274 }
268275 }
@@ -299,16 +306,31 @@
299306 }
300307
301308 /**
302- * A load balancing policy that can be "disabled" by having its query plan return no hosts.
309+ * A load balancing policy that:
310+ * - can be "disabled" by having its query plan return no hosts.
311+ * - can be instructed to return a specific distance for some hosts.
303312 */
304313 public static class TogglabePolicy extends DelegatingLoadBalancingPolicy {
305314
306315 volatile boolean returnEmptyQueryPlan;
316+ final ConcurrentMap<Host, HostDistance> distances = new ConcurrentHashMap<Host, HostDistance>();
307317
308318 public TogglabePolicy(LoadBalancingPolicy delegate) {
309319 super(delegate);
310320 }
311321
322+ @Override
323+ public HostDistance distance(Host host) {
324+ HostDistance distance = distances.get(host);
325+ return (distance != null)
326+ ? distance
327+ : super.distance(host);
328+ }
329+
330+ public void setDistance(Host host, HostDistance distance) {
331+ distances.put(host, distance);
332+ }
333+
312334 @Override
313335 public Iterator<Host> newQueryPlan(String loggedKeyspace, Statement statement) {
314336 if (returnEmptyQueryPlan)