kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 4bdfd8d9a0cb9efa8b46d50b29dd5d922206ee22bc574f08cd1295dc59a6ead1 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-20 21:24 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-20 21:24 +02:00 |
| Parents | ac8f154e6fc9 |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +22 −14
| @@ -26,6 +26,16 @@ | ||
| 26 | 26 | import com.datastax.driver.core.exceptions.AuthenticationException; |
| 27 | 27 | import com.datastax.driver.core.policies.ReconnectionPolicy; |
| 28 | 28 | |
| 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 | + */ | |
| 29 | 39 | abstract class AbstractReconnectionHandler implements Runnable { |
| 30 | 40 | |
| 31 | 41 | private static final Logger logger = LoggerFactory.getLogger(AbstractReconnectionHandler.class); |
| @@ -41,7 +51,7 @@ | ||
| 41 | 51 | |
| 42 | 52 | private final long initialDelayMs; |
| 43 | 53 | |
| 44 | - private volatile boolean isActive; | |
| 54 | + private final CountDownLatch ready = new CountDownLatch(1); | |
| 45 | 55 | |
| 46 | 56 | public AbstractReconnectionHandler(ScheduledExecutorService executor, ReconnectionPolicy.ReconnectionSchedule schedule, AtomicReference<ListenableFuture<?>> currentAttempt) { |
| 47 | 57 | this(executor, schedule, currentAttempt, -1); |
| @@ -79,14 +89,14 @@ | ||
| 79 | 89 | if (previous != null && !previous.isCancelled()) { |
| 80 | 90 | logger.debug("Found another already active handler, cancelling"); |
| 81 | 91 | handlerFuture.cancel(false); |
| 82 | - return; | |
| 92 | + break; | |
| 83 | 93 | } |
| 84 | 94 | if (currentAttempt.compareAndSet(previous, handlerFuture)) { |
| 85 | 95 | logger.debug("Becoming the active handler"); |
| 86 | 96 | break; |
| 87 | 97 | } |
| 88 | 98 | } |
| 89 | - isActive = true; | |
| 99 | + ready.countDown(); | |
| 90 | 100 | } catch (RejectedExecutionException e) { |
| 91 | 101 | // The executor has been shutdown, fair enough, just ignore |
| 92 | 102 | logger.debug("Aborting reconnection handling since the cluster is shutting down"); |
| @@ -95,23 +105,21 @@ | ||
| 95 | 105 | |
| 96 | 106 | @Override |
| 97 | 107 | 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 | + | |
| 98 | 117 | if (handlerFuture.isCancelled()) { |
| 99 | 118 | logger.debug("Got cancelled, stopping"); |
| 100 | 119 | currentAttempt.compareAndSet(handlerFuture, null); |
| 101 | 120 | return; |
| 102 | 121 | } |
| 103 | 122 | |
| 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 | - | |
| 115 | 123 | try { |
| 116 | 124 | onReconnection(tryReconnect()); |
| 117 | 125 | handlerFuture.markAsDone(); |