Remove the reduntant 'afterFinalFailure' from Retrier

'afterFinalFailure' is called just before rethrowing a non-retrying error from
the retrier. This can happen either because the exception shouldn't be retried,
or because we exceeded the maximum number of retries.

The same thing can be done by catching that thrown error outside of the
retrier:

retrier.callWithRetry(
  callable,
  new FailureReporter() {
    @Override
    void afterFinalFailure(Throwable thrown, int failures) {
      // do something with thrown
    }
  },
  RetriableException.class);

is (almost) the same as:

try {
  retrier.callWithRetry(callable, RetriableException.class);
} catch (Throwable thrown) {
  // do something with thrown
  throw thrown;
}

("almost" because the retrier might wrap the Throwable in a RuntimeException,
so you might need to getCause or getRootCause. Also - there is the
"beforeRetry" I ignored for the example)

Removing "afterFinalFailure" also makes the FailureReporter in line with Java 8
functional interface - meaning we can more easily create it when we do need to
override "beforeRetry".

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=189972101
This commit is contained in:
guyben 2018-03-21 14:52:10 -07:00 committed by jianglai
parent 63785e5149
commit 552940a816
6 changed files with 135 additions and 177 deletions

View file

@ -61,23 +61,12 @@ public class RetrierTest {
static class TestReporter implements FailureReporter {
int numBeforeRetry = 0;
int numOnFinalFailure = 0;
@Override
public void beforeRetry(Throwable e, int failures, int maxAttempts) {
numBeforeRetry++;
assertThat(failures).isEqualTo(numBeforeRetry);
}
@Override
public void afterFinalFailure(Throwable e, int failures) {
numOnFinalFailure++;
}
void assertNumbers(int expectedBeforeRetry, int expectedOnFinalFailure) {
assertThat(numBeforeRetry).isEqualTo(expectedBeforeRetry);
assertThat(numOnFinalFailure).isEqualTo(expectedOnFinalFailure);
}
}
@Test
@ -114,7 +103,7 @@ public class RetrierTest {
try {
retrier.callWithRetry(new CountingThrower(3), reporter, CountingException.class);
} catch (CountingException expected) {
reporter.assertNumbers(2, 1);
assertThat(reporter.numBeforeRetry).isEqualTo(2);
throw expected;
}
});
@ -126,7 +115,7 @@ public class RetrierTest {
TestReporter reporter = new TestReporter();
assertThat(retrier.callWithRetry(new CountingThrower(2), reporter, CountingException.class))
.isEqualTo(2);
reporter.assertNumbers(2, 0);
assertThat(reporter.numBeforeRetry).isEqualTo(2);
}
@Test
@ -134,6 +123,6 @@ public class RetrierTest {
TestReporter reporter = new TestReporter();
assertThat(retrier.callWithRetry(new CountingThrower(0), reporter, CountingException.class))
.isEqualTo(0);
reporter.assertNumbers(0, 0);
assertThat(reporter.numBeforeRetry).isEqualTo(0);
}
}