kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a6ca982a760c32a3ad7d4265faff056c68353cac7f89e663ebd3952396e9682f |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-12 15:42 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-12 18:59 +02:00 |
| Parents | 5232d23164bf |
3 files changed
modified driver-core/pom.xml +7 −0
| @@ -71,6 +71,13 @@ | ||
| 71 | 71 | <scope>test</scope> |
| 72 | 72 | </dependency> |
| 73 | 73 | |
| 74 | + <dependency> | |
| 75 | + <groupId>org.assertj</groupId> | |
| 76 | + <artifactId>assertj-core</artifactId> | |
| 77 | + <version>1.7.0</version> | |
| 78 | + <scope>test</scope> | |
| 79 | + </dependency> | |
| 80 | + | |
| 74 | 81 | </dependencies> |
| 75 | 82 | |
| 76 | 83 | <profiles> |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractReconnectionHandler.java +11 −0
| @@ -138,6 +138,12 @@ | ||
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | private void reschedule(long nextDelay) { |
| 141 | + // If we got cancelled during the failed reconnection attempt that lead here, don't reschedule | |
| 142 | + if (localFuture.isCancelled()) { | |
| 143 | + currentAttempt.compareAndSet(localFuture, null); | |
| 144 | + return; | |
| 145 | + } | |
| 146 | + | |
| 141 | 147 | readyForNext = false; |
| 142 | 148 | ScheduledFuture<?> newFuture = executor.schedule(this, nextDelay, TimeUnit.MILLISECONDS); |
| 143 | 149 | assert localFuture != null; |
| @@ -145,6 +151,11 @@ | ||
| 145 | 151 | if (!currentAttempt.compareAndSet(localFuture, newFuture)) { |
| 146 | 152 | newFuture.cancel(false); |
| 147 | 153 | } |
| 154 | + // Check one last time is the previous future was cancelled (there is still a race if a client | |
| 155 | + // held on a reference to that future long after retrieving it from the AtomicReference) | |
| 156 | + if (localFuture.isCancelled()) { | |
| 157 | + newFuture.cancel(false); | |
| 158 | + } | |
| 148 | 159 | localFuture = newFuture; |
| 149 | 160 | readyForNext = true; |
| 150 | 161 | } |
added driver-core/src/test/java/com/datastax/driver/core/AbstractReconnectionHandlerTest.java +289 −0
| @@ -0,0 +1,289 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.net.InetSocketAddress; | |
| 4 | +import java.util.concurrent.*; | |
| 5 | +import java.util.concurrent.atomic.AtomicReference; | |
| 6 | + | |
| 7 | +import com.google.common.util.concurrent.Runnables; | |
| 8 | +import org.slf4j.Logger; | |
| 9 | +import org.slf4j.LoggerFactory; | |
| 10 | +import org.testng.annotations.AfterMethod; | |
| 11 | +import org.testng.annotations.BeforeMethod; | |
| 12 | +import org.testng.annotations.Test; | |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 14 | +import static org.testng.Assert.fail; | |
| 15 | + | |
| 16 | +import com.datastax.driver.core.AbstractReconnectionHandlerTest.MockReconnectionWork.ReconnectBehavior; | |
| 17 | +import com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule; | |
| 18 | + | |
| 19 | +public class AbstractReconnectionHandlerTest { | |
| 20 | + private static final Logger logger = LoggerFactory.getLogger(AbstractReconnectionHandlerTest.class); | |
| 21 | + | |
| 22 | + ScheduledExecutorService executor; | |
| 23 | + MockReconnectionSchedule schedule; | |
| 24 | + MockReconnectionWork work; | |
| 25 | + final AtomicReference<ScheduledFuture<?>> future = new AtomicReference<ScheduledFuture<?>>(); | |
| 26 | + AbstractReconnectionHandler handler; | |
| 27 | + | |
| 28 | + @BeforeMethod(groups = "unit") | |
| 29 | + public void setup() { | |
| 30 | + executor = Executors.newScheduledThreadPool(2); | |
| 31 | + schedule = new MockReconnectionSchedule(); | |
| 32 | + work = new MockReconnectionWork(); | |
| 33 | + future.set(null); | |
| 34 | + handler = new AbstractReconnectionHandler(executor, schedule, future) { | |
| 35 | + @Override | |
| 36 | + protected Connection tryReconnect() throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { | |
| 37 | + return work.tryReconnect(); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + protected void onReconnection(Connection connection) { | |
| 42 | + work.onReconnection(connection); | |
| 43 | + } | |
| 44 | + }; | |
| 45 | + } | |
| 46 | + | |
| 47 | + @AfterMethod(groups = "unit") | |
| 48 | + public void tearDown() { | |
| 49 | + if (future.get() != null) | |
| 50 | + future.get().cancel(false); | |
| 51 | + executor.shutdownNow(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Test(groups = "unit") | |
| 55 | + public void should_complete_if_first_reconnection_succeeds() { | |
| 56 | + handler.start(); | |
| 57 | + | |
| 58 | + assertThat(future.get()).isNotNull(); | |
| 59 | + assertThat(future.get().isDone()).isFalse(); | |
| 60 | + | |
| 61 | + schedule.tick(); | |
| 62 | + work.nextReconnect = ReconnectBehavior.SUCCEED; | |
| 63 | + work.tick(); | |
| 64 | + | |
| 65 | + waitForCompletion(); | |
| 66 | + | |
| 67 | + assertThat(work.success).isTrue(); | |
| 68 | + assertThat(work.tries).isEqualTo(1); | |
| 69 | + assertThat(future.get()).isNull(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + @Test(groups = "unit") | |
| 73 | + public void should_retry_until_success() { | |
| 74 | + handler.start(); | |
| 75 | + | |
| 76 | + int simulatedErrors = 10; | |
| 77 | + for (int i = 0; i < simulatedErrors; i++) { | |
| 78 | + schedule.tick(); | |
| 79 | + work.nextReconnect = ReconnectBehavior.THROW_EXCEPTION; | |
| 80 | + work.tick(); | |
| 81 | + assertThat(work.success).isFalse(); | |
| 82 | + assertThat(future.get().isDone()).isFalse(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + schedule.tick(); | |
| 86 | + work.nextReconnect = ReconnectBehavior.SUCCEED; | |
| 87 | + work.tick(); | |
| 88 | + | |
| 89 | + waitForCompletion(); | |
| 90 | + | |
| 91 | + assertThat(work.success).isTrue(); | |
| 92 | + assertThat(work.tries).isEqualTo(simulatedErrors + 1); | |
| 93 | + assertThat(future.get()).isNull(); | |
| 94 | + } | |
| 95 | + | |
| 96 | + @Test(groups = "unit") | |
| 97 | + public void should_stop_if_cancelled_between_attempts() { | |
| 98 | + handler.start(); | |
| 99 | + | |
| 100 | + schedule.delay = 10 * 1000; // give ourselves time to cancel | |
| 101 | + schedule.tick(); | |
| 102 | + | |
| 103 | + future.get().cancel(false); | |
| 104 | + | |
| 105 | + waitForCompletion(); | |
| 106 | + | |
| 107 | + assertThat(work.success).isFalse(); | |
| 108 | + assertThat(work.tries).isEqualTo(0); | |
| 109 | + // This is not a feature but is interesting to take note of (meaning that a non-null future | |
| 110 | + // does not necessarily mean that a handler is running): | |
| 111 | + assertThat(future.get()).isNotNull(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + @Test(groups = "unit") | |
| 115 | + public void should_complete_if_cancelled_during_successful_reconnect() throws InterruptedException { | |
| 116 | + handler.start(); | |
| 117 | + | |
| 118 | + schedule.tick(); | |
| 119 | + work.nextReconnect = ReconnectBehavior.SUCCEED; | |
| 120 | + | |
| 121 | + // short pause to make sure we are in the middle of the handler's run method (it checks | |
| 122 | + // if the future is cancelled at the beginning) | |
| 123 | + TimeUnit.MILLISECONDS.sleep(100); | |
| 124 | + // don't force interruption because that's what the production code does | |
| 125 | + future.get().cancel(false); | |
| 126 | + | |
| 127 | + work.tick(); | |
| 128 | + | |
| 129 | + waitForCompletion(); | |
| 130 | + | |
| 131 | + assertThat(work.success).isTrue(); | |
| 132 | + assertThat(work.tries).isEqualTo(1); | |
| 133 | + } | |
| 134 | + | |
| 135 | + @Test(groups = "unit") | |
| 136 | + public void should_stop_if_cancelled_during_failed_reconnect() throws InterruptedException { | |
| 137 | + handler.start(); | |
| 138 | + | |
| 139 | + schedule.tick(); | |
| 140 | + work.nextReconnect = ReconnectBehavior.THROW_EXCEPTION; | |
| 141 | + | |
| 142 | + // short pause to make sure we are in the middle of the handler's run method (it checks | |
| 143 | + // if the future is cancelled at the beginning) | |
| 144 | + TimeUnit.MILLISECONDS.sleep(100); | |
| 145 | + // don't force interruption because that's what the production code does | |
| 146 | + future.get().cancel(false); | |
| 147 | + | |
| 148 | + work.tick(); | |
| 149 | + | |
| 150 | + // Need to | |
| 151 | + schedule.tick(); | |
| 152 | + | |
| 153 | + waitForCompletion(); | |
| 154 | + | |
| 155 | + assertThat(work.success).isFalse(); | |
| 156 | + assertThat(work.tries).isEqualTo(1); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * This is how the class currently works, but wouldn't it be better to stop and let the other task run? | |
| 161 | + */ | |
| 162 | + @Test(groups = "unit") | |
| 163 | + public void should_cancel_previous_task() { | |
| 164 | + // Schedule a dummy task just to have a future | |
| 165 | + ScheduledFuture<?> previousFuture = executor.schedule(Runnables.doNothing(), 1, TimeUnit.HOURS); | |
| 166 | + future.set(previousFuture); | |
| 167 | + | |
| 168 | + handler.start(); | |
| 169 | + | |
| 170 | + assertThat(previousFuture.isCancelled()).isTrue(); | |
| 171 | + | |
| 172 | + schedule.tick(); | |
| 173 | + work.nextReconnect = ReconnectBehavior.SUCCEED; | |
| 174 | + work.tick(); | |
| 175 | + | |
| 176 | + waitForCompletion(); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * A reconnection schedule that allows manually setting the delay. | |
| 181 | + * | |
| 182 | + * To make testing easier, nextDelay blocks until tick() is called from the main thread. | |
| 183 | + */ | |
| 184 | + static class MockReconnectionSchedule implements ReconnectionSchedule { | |
| 185 | + volatile long delay; | |
| 186 | + private final CyclicBarrier barrier = new CyclicBarrier(2); | |
| 187 | + | |
| 188 | + // Hack to work around the fact that the first call to nextDelayMs is synchronous | |
| 189 | + private volatile boolean firstDelay = true; | |
| 190 | + private volatile boolean firstTick = true; | |
| 191 | + | |
| 192 | + @Override | |
| 193 | + public long nextDelayMs() { | |
| 194 | + if (firstDelay) | |
| 195 | + firstDelay = false; | |
| 196 | + else { | |
| 197 | + logger.debug("in schedule, waiting for tick from main thread"); | |
| 198 | + try { | |
| 199 | + barrier.await(10, TimeUnit.SECONDS); | |
| 200 | + logger.debug("in schedule, got tick from main thread, proceeding"); | |
| 201 | + } catch (Exception e) { | |
| 202 | + fail("Error while waiting for tick", e); | |
| 203 | + } | |
| 204 | + } | |
| 205 | + logger.debug("in schedule, returning {}", delay); | |
| 206 | + return delay; | |
| 207 | + } | |
| 208 | + | |
| 209 | + public void tick() { | |
| 210 | + if (firstTick) | |
| 211 | + firstTick = false; | |
| 212 | + else { | |
| 213 | + logger.debug("send tick to schedule"); | |
| 214 | + try { | |
| 215 | + barrier.await(10, TimeUnit.SECONDS); | |
| 216 | + } catch (Exception e) { | |
| 217 | + fail("Error while sending tick, no thread was waiting", e); | |
| 218 | + } | |
| 219 | + barrier.reset(); | |
| 220 | + } | |
| 221 | + } | |
| 222 | + } | |
| 223 | + | |
| 224 | + /** | |
| 225 | + * Simulates the work done by the overridable methods of the handler. | |
| 226 | + * | |
| 227 | + * Allows choosing whether the next reconnect will succeed or throw an exception. | |
| 228 | + * To make testing easier, tryReconnect blocks until tick() is called from the main thread. | |
| 229 | + */ | |
| 230 | + static class MockReconnectionWork { | |
| 231 | + enum ReconnectBehavior { | |
| 232 | + SUCCEED, THROW_EXCEPTION | |
| 233 | + }; | |
| 234 | + | |
| 235 | + private final CyclicBarrier barrier = new CyclicBarrier(2); | |
| 236 | + | |
| 237 | + volatile ReconnectBehavior nextReconnect; | |
| 238 | + | |
| 239 | + volatile int tries = 0; | |
| 240 | + volatile boolean success = false; | |
| 241 | + | |
| 242 | + protected Connection tryReconnect() throws ConnectionException { | |
| 243 | + tries += 1; | |
| 244 | + logger.debug("in reconnection work, wait for tick from main thread"); | |
| 245 | + try { | |
| 246 | + barrier.await(10, TimeUnit.SECONDS); | |
| 247 | + logger.debug("in reconnection work, got tick from main thread, proceeding"); | |
| 248 | + } catch (Exception e) { | |
| 249 | + fail("Error while waiting for tick", e); | |
| 250 | + } | |
| 251 | + switch (nextReconnect) { | |
| 252 | + case SUCCEED: | |
| 253 | + logger.debug("simulate reconnection success"); | |
| 254 | + return null; | |
| 255 | + case THROW_EXCEPTION: | |
| 256 | + logger.debug("simulate reconnection error"); | |
| 257 | + throw new ConnectionException(new InetSocketAddress(8888), | |
| 258 | + "Simulated exception from mock reconnection"); | |
| 259 | + default: | |
| 260 | + throw new AssertionError(); | |
| 261 | + } | |
| 262 | + } | |
| 263 | + | |
| 264 | + public void tick() { | |
| 265 | + logger.debug("send tick to reconnection work"); | |
| 266 | + try { | |
| 267 | + barrier.await(10, TimeUnit.SECONDS); | |
| 268 | + } catch (Exception e) { | |
| 269 | + fail("Error while sending tick, no thread was waiting", e); | |
| 270 | + } | |
| 271 | + barrier.reset(); | |
| 272 | + } | |
| 273 | + | |
| 274 | + protected void onReconnection(Connection connection) { | |
| 275 | + success = true; | |
| 276 | + } | |
| 277 | + } | |
| 278 | + | |
| 279 | + private void waitForCompletion() { | |
| 280 | + executor.shutdown(); | |
| 281 | + try { | |
| 282 | + boolean shutdown = executor.awaitTermination(10, TimeUnit.SECONDS); | |
| 283 | + if (!shutdown) | |
| 284 | + fail("executor ran for longer than expected"); | |
| 285 | + } catch (InterruptedException e) { | |
| 286 | + fail("Interrupted while waiting for executor to shutdown"); | |
| 287 | + } | |
| 288 | + } | |
| 289 | +} |