public <T> T runInterruptible(Retryable<T> op) throws InterruptedException, RetryGiveupException { return run(op, true); }
public RetryExecutor withMaxRetryWait(int msec) { return new RetryExecutor(retryLimit, initialRetryWait, msec); }
/** * Build the common retry executor from some configuration params of plugin task. * @param task Plugin task. * @return RetryExecutor object */ private static RetryExecutor retryExecutorFrom(RetrySupportPluginTask task) { return retryExecutor() .withRetryLimit(task.getMaximumRetries()) .withInitialRetryWait(task.getInitialRetryIntervalMillis()) .withMaxRetryWait(task.getMaximumRetryIntervalMillis()); }
@Test(expected = AmazonClientException.class) public void reopenS3FileByReopener_on_retry_gave_up_should_throw_original_exception() throws Exception { String content = "value"; doThrow(new AmazonClientException("no")).doReturn(s3object("in/aa/a", content)).when(client).getObject(any(GetObjectRequest.class)); S3InputStreamReopener opener = new S3InputStreamReopener( client, new GetObjectRequest("my_bucket", "in/aa/a"), content.length(), retryExecutor() .withInitialRetryWait(0) .withRetryLimit(0)); opener.reopen(0, new RuntimeException()); }
private static RetryExecutor retryExecutor() { return RetryExecutor.retryExecutor() .withInitialRetryWait(0) .withMaxRetryWait(0); }
@Test public void listS3FileByPrefix_with_retry() { doThrow(new RuntimeException()).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }
@Test(expected = RetryGiveupException.class) @SuppressWarnings("unchecked") public void fail_after_exceeding_attempts_just_like_Retryable() throws Exception { retryExecutor() .withRetryLimit(3) .run(new DefaultRetryable(Deny.until(4))); }
return executor.runInterruptible(this);
@Test(expected = AmazonClientException.class) public void reopenS3FileByReopener_on_retry_always_throw_exception() throws Exception { // always failed call with 2 retries doThrow(new AmazonClientException("This exception is thrown when retrying.")).when(client).getObject(any(GetObjectRequest.class)); S3InputStreamReopener opener = new S3InputStreamReopener( client, new GetObjectRequest("my_bucket", "in/aa/a"), "value".length(), retryExecutor() .withInitialRetryWait(0) .withRetryLimit(2)); try (InputStream in = opener.reopen(0, new AmazonClientException("This exception can be ignored"))) { fail("Should throw exception."); } }
private static RetryExecutor retryExecutor() { return RetryExecutor.retryExecutor() .withInitialRetryWait(0) .withMaxRetryWait(0); }
@Test(expected = RuntimeException.class) @SuppressWarnings("unchecked") public void execute_should_unwrap_RetryGiveupException_but_rewrap_checked_exception_in_a_RuntimeException() { new DefaultRetryable(Deny.until(4).with(new Exception("A checked exception"))) .executeWith(retryExecutor().withRetryLimit(3)); }
@Test @SuppressWarnings("unchecked") public void guarantee_retry_attempts_just_like_Retryable() throws Exception { retryExecutor() .withRetryLimit(0) .run(new DefaultRetryable(Deny.until(0))); retryExecutor() .withRetryLimit(1) .run(new DefaultRetryable(Deny.until(1))); retryExecutor() .withRetryLimit(2) .run(new DefaultRetryable(Deny.until(1))); retryExecutor() .withRetryLimit(3) .run(new DefaultRetryable(Deny.until(2))); }
return executor.runInterruptible(this);
@Test public void reopenS3FileByReopener() throws Exception { String content = "value"; { // not retry doReturn(s3object("in/aa/a", content)).when(client).getObject(any(GetObjectRequest.class)); S3InputStreamReopener opener = new S3InputStreamReopener(client, new GetObjectRequest("my_bucket", "in/aa/a"), content.length()); try (InputStream in = opener.reopen(0, new RuntimeException())) { BufferedReader r = new BufferedReader(new InputStreamReader(in)); assertEquals("value", r.readLine()); } } { // retry once doThrow(new RuntimeException()).doReturn(s3object("in/aa/a", content)).when(client).getObject(any(GetObjectRequest.class)); S3InputStreamReopener opener = new S3InputStreamReopener( client, new GetObjectRequest("my_bucket", "in/aa/a"), content.length(), retryExecutor() .withInitialRetryWait(0) .withRetryLimit(1)); try (InputStream in = opener.reopen(0, new RuntimeException())) { BufferedReader r = new BufferedReader(new InputStreamReader(in)); assertEquals("value", r.readLine()); } } }
@Test(expected = IOException.class) public void executeAndPropagateAsIs_should_leave_original_exception_unwrapped() throws IOException { RetryExecutor retryExc = retryExecutor().withRetryLimit(3); // An explicit type parameter for operation return type is needed here, // Without one, javac (at least on 1.8) will fails to infer the X exception type parameter. new DefaultRetryable<Object>() { @Override public Object call() throws IOException { throw new IOException(); } }.executeWithCheckedException(retryExc, IOException.class); }
public <T> T run(Retryable<T> op) throws RetryGiveupException { try { return run(op, false); } catch (InterruptedException ex) { throw new RetryGiveupException("Unexpected interruption", ex); } }
public RetryExecutor withRetryLimit(int count) { return new RetryExecutor(count, initialRetryWait, maxRetryWait); }
@Test(expected = SomeException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception() { doThrow(new SomeException()).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(0)); }
public static RetryExecutor retryExecutor() { // TODO default configuration return new RetryExecutor(3, 500, 30 * 60 * 1000); }
@Test(expected = AmazonServiceException.class) public void listS3FileByPrefix_on_retry_gave_up_should_throw_the_original_exception_in_forbidden_code() { AmazonServiceException exception = new AmazonServiceException("Forbidden exception"); exception.setStatusCode(HttpStatus.SC_FORBIDDEN); exception.setErrorType(AmazonServiceException.ErrorType.Client); doThrow(exception).doReturn(new ObjectListing()) .when(client).listObjects(any(ListObjectsRequest.class)); FileList.Builder builder = new FileList.Builder(); dummyS3Plugin().listS3FilesByPrefix( builder, client, "some_bucket", "some_prefix", Optional.of("last_path"), true, retryExecutor().withRetryLimit(1)); }