private static void testPropagateCancel(int expectedRetries, boolean interrupt) throws Exception { RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); TestFuture future = new TestFuture(); CountDownLatch settableFutureFetched = new CountDownLatch(1); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation( retryPolicy, stat, () -> { attempts.getAndIncrement(); if (attempts.get() > expectedRetries) { settableFutureFetched.countDown(); return future; } return immediateFailedFuture(createClassifiedException(true, NORMAL)); }); settableFutureFetched.await(); methodInvocation.cancel(interrupt); assertTrue(future.isCancelled()); assertEquals(future.checkWasInterrupted(), interrupt); assertEquals(attempts.get(), expectedRetries + 1); }
private static void testExceptionFromInvokerInvoke(int expectedRetries) throws Exception { RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation( retryPolicy, stat, () -> { attempts.getAndIncrement(); if (attempts.get() > expectedRetries) { throw UNEXPECTED_EXCEPTION; } return immediateFailedFuture(createClassifiedException(true, NORMAL)); }); try { methodInvocation.get(); fail("Expected exception"); } catch (ExecutionException e) { assertEquals(attempts.get(), expectedRetries + 1); assertUnexpectedException(e.getCause()); } }
@Test(timeOut = 60000) public void testMaxRetries() throws Exception { int maxRetries = 5; RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(maxRetries), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(retryPolicy, stat, () -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); }); try { methodInvocation.get(); fail("Expected exception"); } catch (ExecutionException e) { assertEquals(attempts.get(), maxRetries + 1); assertClassifiedException(e.getCause(), new ExceptionClassification(Optional.of(true), NORMAL), maxRetries); } stat.assertFailure(maxRetries); }
new MockMethodInvoker(() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); })
new MockMethodInvoker(() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); }), new TestingAddressSelector(100)
() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); });
int tries = attempts.getAndIncrement(); if (tries < expectedInvocationAttempts) { return immediateFailedFuture(createClassifiedException(true, NORMAL)); return immediateFailedFuture(createClassifiedException(false, DOWN)); }); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(
int currentAttempts = attempts.getAndIncrement(); if (currentAttempts < expectedRetries) { return immediateFailedFuture(createClassifiedException(true, NORMAL, wrapWithApplicationException)); return immediateFailedFuture(createClassifiedException(false, NORMAL, wrapWithApplicationException)); }); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(
attempts.getAndIncrement(); attemptedAddresses.add(request.getAddress()); return immediateFailedFuture(createClassifiedException(true, overloaded ? OVERLOADED : DOWN)); }); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(
() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); }, ticker);
new MockMethodInvoker(() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, DOWN)); }), new TestingAddressSelector(100)
private static void testBasicRetriesToSuccess(int expectedRetries, boolean wrapWithApplicationException) throws Exception { RetryPolicy retryPolicy = new RetryPolicy( new DriftClientConfig() .setMaxRetries(expectedRetries + 10) .setMinBackoffDelay(new Duration(1, SECONDS)) .setMaxBackoffDelay(new Duration(1, DAYS)) .setBackoffScaleFactor(2.0), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); MockMethodInvoker invoker = new MockMethodInvoker(() -> { int currentAttempts = attempts.getAndIncrement(); if (currentAttempts < expectedRetries) { return immediateFailedFuture(createClassifiedException(true, NORMAL, wrapWithApplicationException)); } return immediateFuture(SUCCESS); }); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation(retryPolicy, stat, invoker, new TestingAddressSelector(100), systemTicker()); assertEquals(methodInvocation.get(), SUCCESS); assertEquals(attempts.get(), expectedRetries + 1); stat.assertSuccess(expectedRetries); assertDelays(invoker, retryPolicy, expectedRetries); }
@Test(timeOut = 60000) public void testBasicRetriesToNoHosts() throws Exception { RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(10), new TestingExceptionClassifier()); TestingMethodInvocationStat stat = new TestingMethodInvocationStat(); AtomicInteger attempts = new AtomicInteger(); int expectedRetries = 3; DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation( retryPolicy, stat, new MockMethodInvoker(() -> { attempts.getAndIncrement(); return immediateFailedFuture(createClassifiedException(true, NORMAL)); }), new TestingAddressSelector(expectedRetries + 1), systemTicker()); try { methodInvocation.get(); fail("Expected exception"); } catch (ExecutionException e) { assertEquals(attempts.get(), expectedRetries + 1); assertClassifiedException(e.getCause(), new ExceptionClassification(Optional.of(true), NORMAL), expectedRetries); } stat.assertFailure(expectedRetries); }
public static Exception createClassifiedException(boolean retry, HostStatus hostStatus) { return createClassifiedException(retry, hostStatus, true); }