FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: update DefaultRetryContext to trap and forward RejectedExceptionException to onFailure by BenWhitehead · Pull Request #3327 · googleapis/java-storage · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -142,17 +143,26 @@ public <T extends Throwable> void recordError(T t, OnSuccess onSuccess, OnFailur
BackoffDuration backoffDuration = (BackoffDuration) nextBackoff;

lastBackoffResult = nextBackoff;
pendingBackoff =
scheduledExecutorService.schedule(
() -> {
try {
onSuccess.onSuccess();
} finally {
clearPendingBackoff();
}
},
backoffDuration.getDuration().toNanos(),
TimeUnit.NANOSECONDS);
try {
pendingBackoff =
scheduledExecutorService.schedule(
() -> {
try {
onSuccess.onSuccess();
} finally {
clearPendingBackoff();
}
},
backoffDuration.getDuration().toNanos(),
TimeUnit.NANOSECONDS);
} catch (RejectedExecutionException e) {
InterruptedBackoffComment comment =
new InterruptedBackoffComment(
"Interrupted backoff -- unretryable error due to executor service shutdown");
comment.addSuppressed(e);
t.addSuppressed(comment);
onFailure.onFailure(t);
}
} else {
String msg =
String.format(
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.cloud.storage;

import static java.util.Objects.requireNonNull;

import com.google.api.client.util.Sleeper;
import com.google.api.core.ApiClock;
import com.google.api.core.ApiFuture;
Expand All @@ -40,6 +42,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.checkerframework.checker.nullness.qual.NonNull;

@InternalApi
@InternalExtensionOnly
Expand Down Expand Up @@ -131,6 +134,16 @@ static BackoffComment of(String message) {
}
}

final class InterruptedBackoffComment extends Throwable {
InterruptedBackoffComment(@NonNull String message) {
super(
requireNonNull(message, "message must be non null"),
/* cause= */ null,
/* enableSuppression= */ true,
/* writableStackTrace= */ false);
}
}

final class DirectScheduledExecutorService implements ScheduledExecutorService {
private static final DirectScheduledExecutorService INSTANCE =
new DirectScheduledExecutorService(Sleeper.DEFAULT, NanoClock.getDefaultClock());
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import static com.google.cloud.storage.TestUtils.assertAll;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.api.core.ApiClock;
import com.google.api.core.NanoClock;
Expand All @@ -31,6 +35,8 @@
import com.google.cloud.RetryHelper;
import com.google.cloud.RetryHelper.RetryHelperException;
import com.google.cloud.storage.Backoff.Jitterer;
import com.google.cloud.storage.RetryContext.BackoffComment;
import com.google.cloud.storage.RetryContext.InterruptedBackoffComment;
import com.google.cloud.storage.RetryContext.OnFailure;
import com.google.cloud.storage.RetryContext.OnSuccess;
import com.google.cloud.storage.Retrying.RetryingDependencies;
Expand All @@ -42,6 +48,7 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -423,6 +430,24 @@ public void resetAlsoResetsBackoffState() throws Exception {
});
}

@Test
public void rejectedExecutionException_funneledToOnFailureHandlerAsSuppressedException() {
ScheduledExecutorService exec = mock(ScheduledExecutorService.class);
RejectedExecutionException alreadyShutdown = new RejectedExecutionException("already shutdown");
when(exec.schedule(any(Runnable.class), anyLong(), any())).thenThrow(alreadyShutdown);
Throwable t1 = new RuntimeException("{err1}", new Throwable("{err1Cause}"));
RetryContext ctx =
RetryContext.of(exec, maxAttempts(2), Retrying.alwaysRetry(), Jitterer.noJitter());

AtomicReference<Throwable> err1 = new AtomicReference<>();
ctx.recordError(t1, failOnSuccess(), err1::set);
Throwable t = err1.get();
assertThat(t).isNotNull();
assertThat(t.getSuppressed()[0]).isInstanceOf(BackoffComment.class);
assertThat(t.getSuppressed()[1]).isInstanceOf(InterruptedBackoffComment.class);
assertThat(t.getSuppressed()[1].getSuppressed()[0]).isSameInstanceAs(alreadyShutdown);
}

private static ApiException apiException(Code code, String message) {
return ApiExceptionFactory.createException(message, null, GrpcStatusCode.of(code), false);
}
Expand Down

Back | FazBrowse Home | New Git URL