MethodMetadata metadata = toMethodMetadata(codecManager, method); RetryPolicy retryPolicy = new RetryPolicy(config, exceptionClassifier);
ExceptionClassification exceptionClassification = retryPolicy.classifyException(throwable, metadata.isIdempotent()); return; if (invocationAttempts > retryPolicy.getMaxRetries()) { fail(format("Max retry attempts (%s) exceeded", retryPolicy.getMaxRetries())); return; if (duration.compareTo(retryPolicy.getMaxRetryTime()) >= 0) { fail(format("Max retry time (%s) exceeded", retryPolicy.getMaxRetryTime())); return; Duration backoffDelay = retryPolicy.getBackoffDelay(invocationAttempts); log.debug("Failed invocation of %s with attempt %s, will retry in %s (overloadedRejects: %s). Exception: %s", metadata.getName(),
@Test public void testRetryUserException() { ExceptionClassification overloaded = new ExceptionClassification(Optional.of(true), OVERLOADED); RetryPolicy policy = new RetryPolicy(new DriftClientConfig(), classifier -> { if (classifier instanceof TestingUserException) { return overloaded; } return NORMAL_EXCEPTION; }); assertSame(policy.classifyException(new DriftApplicationException(new TestingUserException()), true), overloaded); assertSame(policy.classifyException(new TestingUserException(), true), overloaded); }
private static void assertDelays(MockMethodInvoker invoker, RetryPolicy retryPolicy, int expectedRetries) { assertEquals(invoker.getDelays(), IntStream.range(0, expectedRetries) .mapToObj(i -> retryPolicy.getBackoffDelay(i + 1)) .collect(toImmutableList())); }
Duration connectDelay = retryPolicy.getBackoffDelay(connectionFailuresCount); log.debug("Failed connection to %s with attempt %s, will retry in %s", address.get(), connectionFailuresCount, connectDelay); schedule(connectDelay, () -> invoke(address.get()));
ExceptionClassification exceptionClassification = retryPolicy.classifyException(throwable, metadata.isIdempotent()); return; if (invocationAttempts > retryPolicy.getMaxRetries()) { fail(format("Max retry attempts (%s) exceeded", retryPolicy.getMaxRetries())); return; if (duration.compareTo(retryPolicy.getMaxRetryTime()) >= 0) { fail(format("Max retry time (%s) exceeded", retryPolicy.getMaxRetryTime())); return; Duration backoffDelay = retryPolicy.getBackoffDelay(invocationAttempts); log.debug("Failed invocation of %s with attempt %s, will retry in %s (overloadedRejects: %s). Exception: %s", metadata.getName(),
MethodMetadata metadata = toMethodMetadata(codecManager, method); RetryPolicy retryPolicy = new RetryPolicy(config, exceptionClassifier);
Duration connectDelay = retryPolicy.getBackoffDelay(connectionFailuresCount); log.debug("Failed connection to %s with attempt %s, will retry in %s", address.get(), connectionFailuresCount, connectDelay); schedule(connectDelay, () -> invoke(address.get()));
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); }
RetryPolicy retryPolicy = new RetryPolicy( new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier()
private static void testExhaustHosts(int expectedRetries, boolean overloaded) throws Exception RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier());
@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); }
private static void testExceptionFromAddressSelectorMarkDown(int expectedRetries) throws Exception RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier());
private static void testExceptionFromInvokerDelay(int expectedRetries, final boolean throwUnexpected) throws Exception RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier());
private static void testExceptionFromAddressSelectorSelectAddress(int expectedRetries) throws Exception RetryPolicy retryPolicy = new RetryPolicy(new DriftClientConfig().setMaxRetries(expectedRetries + 10), new TestingExceptionClassifier());
}); DriftMethodInvocation<?> methodInvocation = createDriftMethodInvocation( new RetryPolicy(new DriftClientConfig().setMaxRetries(100), new TestingExceptionClassifier()), new TestingMethodInvocationStat(), invoker,
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); }
RetryPolicy retryPolicy = new RetryPolicy( new DriftClientConfig() .setMaxRetries(maxRetries + 10)