Elffuss BraidHub

kikocis/java-driver public

Fix bug in AbstractReconnectionHandler + document concurrency policy.
Changeset4bdfd8d9a0cb9efa8b46d50b29dd5d922206ee22bc574f08cd1295dc59a6ead1
Authorolim7t <omichallat+github@gmail.com> · 2014-10-20 21:24 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-20 21:24 +02:00
Parentsac8f154e6fc9

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +22 −14
@@ -26,6 +26,16 @@
2626 import com.datastax.driver.core.exceptions.AuthenticationException;
2727 import com.datastax.driver.core.policies.ReconnectionPolicy;
2828
29+/**
30+ * Manages periodic reconnection attempts after a host has been marked down.
31+ * <p>
32+ * Concurrent attempts are handled via the {@link #currentAttempt} reference passed to the constructor.
33+ * For a given reference, only one handler will run at a given time. Additional handlers will cancel
34+ * themselves if they fight a previous handler running.
35+ * <p>
36+ * This class is designed for concurrency, but instances must not be shared: each thread creates and
37+ * starts its own private handler, all interactions happen through {@link #currentAttempt}.
38+ */
2939 abstract class AbstractReconnectionHandler implements Runnable {
3040
3141 private static final Logger logger = LoggerFactory.getLogger(AbstractReconnectionHandler.class);
@@ -41,7 +51,7 @@
4151
4252 private final long initialDelayMs;
4353
44- private volatile boolean isActive;
54+ private final CountDownLatch ready = new CountDownLatch(1);
4555
4656 public AbstractReconnectionHandler(ScheduledExecutorService executor, ReconnectionPolicy.ReconnectionSchedule schedule, AtomicReference<ListenableFuture<?>> currentAttempt) {
4757 this(executor, schedule, currentAttempt, -1);
@@ -79,14 +89,14 @@
7989 if (previous != null && !previous.isCancelled()) {
8090 logger.debug("Found another already active handler, cancelling");
8191 handlerFuture.cancel(false);
82- return;
92+ break;
8393 }
8494 if (currentAttempt.compareAndSet(previous, handlerFuture)) {
8595 logger.debug("Becoming the active handler");
8696 break;
8797 }
8898 }
89- isActive = true;
99+ ready.countDown();
90100 } catch (RejectedExecutionException e) {
91101 // The executor has been shutdown, fair enough, just ignore
92102 logger.debug("Aborting reconnection handling since the cluster is shutting down");
@@ -95,23 +105,21 @@
95105
96106 @Override
97107 public void run() {
108+ // Just make sure we don't start the first try too fast, in case we find out in start() that we need to cancel ourselves
109+ try {
110+ ready.await();
111+ } catch (InterruptedException e) {
112+ // This can happen at shutdown
113+ Thread.currentThread().interrupt();
114+ return;
115+ }
116+
98117 if (handlerFuture.isCancelled()) {
99118 logger.debug("Got cancelled, stopping");
100119 currentAttempt.compareAndSet(handlerFuture, null);
101120 return;
102121 }
103122
104- // Just make sure we don't start the first try too fast, in case we find out in start() that we need to cancel ourselves
105- while (!isActive && !Thread.currentThread().isInterrupted()) {
106- try {
107- TimeUnit.MILLISECONDS.sleep(5);
108- } catch (InterruptedException e) {
109- // This can happen at shutdown
110- Thread.currentThread().interrupt();
111- return;
112- }
113- }
114-
115123 try {
116124 onReconnection(tryReconnect());
117125 handlerFuture.markAsDone();