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

@ -138,19 +138,9 @@ class CommitLogManifestReader extends InputReader<Key<CommitLogManifest>> {
try { try {
return retrier.callWithRetry( return retrier.callWithRetry(
() -> queryIterator.next(), () -> queryIterator.next(),
new Retrier.FailureReporter() { (thrown, failures, maxAttempts) -> {
@Override
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {
checkNotNull(currentCursor, "Can't retry because cursor is null. Giving up."); checkNotNull(currentCursor, "Can't retry because cursor is null. Giving up.");
queryIterator = query().startAt(currentCursor).keys().iterator(); queryIterator = query().startAt(currentCursor).keys().iterator();
}
@Override
public void afterFinalFailure(Throwable thrown, int failures) {
logger.severefmt(
"Max retry attempts reached trying to read item %d/%d. Giving up.",
loaded, total);
}
}, },
DatastoreTimeoutException.class); DatastoreTimeoutException.class);
} finally { } finally {

View file

@ -14,6 +14,7 @@
package google.registry.reporting.billing; package google.registry.reporting.billing;
import static com.google.common.base.Throwables.getRootCause;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsFilename;
@ -78,6 +79,7 @@ class BillingEmailUtils {
/** Sends an e-mail to all expected recipients with an attached overall invoice from GCS. */ /** Sends an e-mail to all expected recipients with an attached overall invoice from GCS. */
void emailOverallInvoice() { void emailOverallInvoice() {
try {
retrier.callWithRetry( retrier.callWithRetry(
() -> { () -> {
String invoiceFile = String invoiceFile =
@ -91,7 +93,8 @@ class BillingEmailUtils {
for (String recipient : invoiceEmailRecipients) { for (String recipient : invoiceEmailRecipients) {
msg.addRecipient(RecipientType.TO, new InternetAddress(recipient)); msg.addRecipient(RecipientType.TO, new InternetAddress(recipient));
} }
msg.setSubject(String.format("Domain Registry invoice data %s", yearMonth.toString())); msg.setSubject(
String.format("Domain Registry invoice data %s", yearMonth.toString()));
Multipart multipart = new MimeMultipart(); Multipart multipart = new MimeMultipart();
BodyPart textPart = new MimeBodyPart(); BodyPart textPart = new MimeBodyPart();
textPart.setText( textPart.setText(
@ -108,22 +111,22 @@ class BillingEmailUtils {
emailService.sendMessage(msg); emailService.sendMessage(msg);
} }
}, },
new Retrier.FailureReporter() {
@Override
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {}
@Override
public void afterFinalFailure(Throwable thrown, int failures) {
sendAlertEmail(
String.format("Emailing invoice failed due to %s", thrown.getMessage()));
}
},
IOException.class, IOException.class,
MessagingException.class); MessagingException.class);
} catch (Throwable e) {
logger.severe(e, "Emailing invoice failed");
// Strip one layer, because callWithRetry wraps in a RuntimeException
sendAlertEmail(
String.format(
"Emailing invoice failed due to %s",
getRootCause(e).getMessage()));
throw e;
}
} }
/** Sends an e-mail to the provided alert e-mail address indicating a billing failure. */ /** Sends an e-mail to the provided alert e-mail address indicating a billing failure. */
void sendAlertEmail(String body) { void sendAlertEmail(String body) {
try {
retrier.callWithRetry( retrier.callWithRetry(
() -> { () -> {
Message msg = emailService.createMessage(); Message msg = emailService.createMessage();
@ -134,15 +137,10 @@ class BillingEmailUtils {
emailService.sendMessage(msg); emailService.sendMessage(msg);
return null; return null;
}, },
new Retrier.FailureReporter() {
@Override
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {}
@Override
public void afterFinalFailure(Throwable thrown, int failures) {
logger.severe(thrown, "The alert e-mail system failed.");
}
},
MessagingException.class); MessagingException.class);
} catch (Throwable e) {
logger.severe(e, "The alert e-mail system failed.");
throw e;
}
} }
} }

View file

@ -14,6 +14,7 @@
package google.registry.reporting.billing; package google.registry.reporting.billing;
import static com.google.common.base.Throwables.getRootCause;
import static google.registry.request.Action.Method.POST; import static google.registry.request.Action.Method.POST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK; import static javax.servlet.http.HttpServletResponse.SC_OK;
@ -104,6 +105,7 @@ public final class CopyDetailReportsAction implements Runnable {
continue; continue;
} }
// Attempt to copy each detail report to its associated registrar's drive folder. // Attempt to copy each detail report to its associated registrar's drive folder.
try {
retrier.callWithRetry( retrier.callWithRetry(
() -> { () -> {
try (InputStream input = try (InputStream input =
@ -119,19 +121,14 @@ public final class CopyDetailReportsAction implements Runnable {
registrarId, driveFolderId, billingBucket, detailReportName); registrarId, driveFolderId, billingBucket, detailReportName);
} }
}, },
new Retrier.FailureReporter() { IOException.class);
@Override } catch (Throwable e) {
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {}
@Override
public void afterFinalFailure(Throwable thrown, int failures) {
emailUtils.sendAlertEmail( emailUtils.sendAlertEmail(
String.format( String.format(
"Warning: CopyDetailReportsAction failed.\nEncountered: %s on file: %s", "Warning: CopyDetailReportsAction failed.\nEncountered: %s on file: %s",
thrown.getMessage(), detailReportName)); getRootCause(e).getMessage(), detailReportName));
throw e;
} }
},
IOException.class);
} }
response.setStatus(SC_OK); response.setStatus(SC_OK);
response.setContentType(MediaType.PLAIN_TEXT_UTF_8); response.setContentType(MediaType.PLAIN_TEXT_UTF_8);

View file

@ -14,6 +14,7 @@
package google.registry.reporting.icann; package google.registry.reporting.icann;
import static com.google.common.base.Throwables.getRootCause;
import static google.registry.request.Action.Method.POST; import static google.registry.request.Action.Method.POST;
import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR; import static javax.servlet.http.HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
import static javax.servlet.http.HttpServletResponse.SC_OK; import static javax.servlet.http.HttpServletResponse.SC_OK;
@ -73,6 +74,7 @@ public final class IcannReportingStagingAction implements Runnable {
@Override @Override
public void run() { public void run() {
try {
retrier.callWithRetry( retrier.callWithRetry(
() -> { () -> {
ImmutableList.Builder<String> manifestedFilesBuilder = new ImmutableList.Builder<>(); ImmutableList.Builder<String> manifestedFilesBuilder = new ImmutableList.Builder<>();
@ -94,29 +96,29 @@ public final class IcannReportingStagingAction implements Runnable {
response.setPayload("Completed staging action."); response.setPayload("Completed staging action.");
logger.infofmt("Enqueueing report upload :"); logger.infofmt("Enqueueing report upload :");
TaskOptions uploadTask = TaskOptions.Builder.withUrl(IcannReportingUploadAction.PATH) TaskOptions uploadTask =
TaskOptions.Builder.withUrl(IcannReportingUploadAction.PATH)
.method(Method.POST) .method(Method.POST)
.countdownMillis(Duration.standardMinutes(2).getMillis()) .countdownMillis(Duration.standardMinutes(2).getMillis())
.param(IcannReportingModule.PARAM_SUBDIR, subdir); .param(IcannReportingModule.PARAM_SUBDIR, subdir);
QueueFactory.getQueue(CRON_QUEUE).add(uploadTask); QueueFactory.getQueue(CRON_QUEUE).add(uploadTask);
return null; return null;
}, },
new Retrier.FailureReporter() { BigqueryJobFailureException.class);
@Override } catch (Throwable e) {
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {}
@Override
public void afterFinalFailure(Throwable thrown, int failures) {
emailUtils.emailResults( emailUtils.emailResults(
"ICANN Monthly report staging summary [FAILURE]", "ICANN Monthly report staging summary [FAILURE]",
String.format( String.format(
"Staging failed due to %s, check logs for more details.", thrown.toString())); "Staging failed due to %s, check logs for more details.",
logger.severe(thrown, "Staging action failed."); getRootCause(e).toString()));
logger.severe(e, "Staging action failed.");
response.setStatus(SC_INTERNAL_SERVER_ERROR); response.setStatus(SC_INTERNAL_SERVER_ERROR);
response.setContentType(MediaType.PLAIN_TEXT_UTF_8); response.setContentType(MediaType.PLAIN_TEXT_UTF_8);
response.setPayload(String.format("Staging failed due to %s", thrown.toString())); response.setPayload(
String.format(
"Staging failed due to %s",
getRootCause(e).toString()));
throw e;
} }
},
BigqueryJobFailureException.class);
} }
} }

View file

@ -49,16 +49,6 @@ public class Retrier implements Serializable {
* <p>Not called at all if the retrier succeeded on its first attempt. * <p>Not called at all if the retrier succeeded on its first attempt.
*/ */
void beforeRetry(Throwable thrown, int failures, int maxAttempts); void beforeRetry(Throwable thrown, int failures, int maxAttempts);
/**
* Called after a a non-retriable error.
*
* <p>Called either after the final failure, or if the Throwable thrown isn't "a retriable
* error". The retrier throws right after calling this function.
*
* <p>Not called at all if the retrier succeeds.
*/
void afterFinalFailure(Throwable thrown, int failures);
} }
@Inject @Inject
@ -89,7 +79,6 @@ public class Retrier implements Serializable {
return callable.call(); return callable.call();
} catch (Throwable e) { } catch (Throwable e) {
if (++failures == attempts || !isRetryable.test(e)) { if (++failures == attempts || !isRetryable.test(e)) {
failureReporter.afterFinalFailure(e, failures);
throwIfUnchecked(e); throwIfUnchecked(e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -180,13 +169,6 @@ public class Retrier implements Serializable {
} }
private static final FailureReporter LOGGING_FAILURE_REPORTER = private static final FailureReporter LOGGING_FAILURE_REPORTER =
new FailureReporter() { (thrown, failures, maxAttempts) ->
@Override logger.infofmt(thrown, "Retrying transient error, attempt %d/%d", failures, maxAttempts);
public void beforeRetry(Throwable thrown, int failures, int maxAttempts) {
logger.infofmt(thrown, "Retrying transient error, attempt %d", failures);
}
@Override
public void afterFinalFailure(Throwable thrown, int failures) {}
};
} }

View file

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