forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce necessary executors and implement sleepAsync #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
stIncMale
wants to merge
3
commits into
introduceRetryPolicy
Choose a base branch
from
sleepAsync
base: introduceRetryPolicy
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,656
−229
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -16,10 +16,14 @@ | |
| package com.mongodb.internal.async.function; | ||
|
|
||
| import com.mongodb.annotations.NotThreadSafe; | ||
| import com.mongodb.internal.async.MutableValue; | ||
| import com.mongodb.internal.async.SingleResultCallback; | ||
| import com.mongodb.lang.Nullable; | ||
| import com.mongodb.internal.async.function.RetryPolicy.Decision.RetryAttemptInfo; | ||
| import com.mongodb.internal.thread.AsyncClientExecutor; | ||
|
|
||
| import static com.mongodb.assertions.Assertions.assertNotNull; | ||
| import java.time.Duration; | ||
|
|
||
| import static com.mongodb.internal.async.AsyncRunnable.beginAsync; | ||
|
|
||
| /** | ||
| * A decorator that implements automatic retrying of failed executions of an {@link AsyncCallbackSupplier}. | ||
|
|
@@ -34,53 +38,45 @@ | |
| */ | ||
| @NotThreadSafe | ||
| public final class RetryingAsyncCallbackSupplier<R> implements AsyncCallbackSupplier<R> { | ||
| private final AsyncClientExecutor clientExecutor; | ||
| private final RetryControl<?> control; | ||
| private final AsyncCallbackSupplier<R> asyncFunction; | ||
|
|
||
| /** | ||
| * @param clientExecutor For {@linkplain AsyncClientExecutor#sleepAsync(Duration, SingleResultCallback) delaying} attempts | ||
| * according to {@link RetryAttemptInfo#getBackoff()}. | ||
| * @param control The {@link RetryControl} to control the new {@link RetryingAsyncCallbackSupplier}. | ||
| * @param asyncFunction The retryable {@link AsyncCallbackSupplier} to be decorated. | ||
| */ | ||
| public RetryingAsyncCallbackSupplier(final RetryControl<?> control, final AsyncCallbackSupplier<R> asyncFunction) { | ||
| public RetryingAsyncCallbackSupplier( | ||
| final AsyncClientExecutor clientExecutor, | ||
| final RetryControl<?> control, | ||
| final AsyncCallbackSupplier<R> asyncFunction) { | ||
| this.clientExecutor = clientExecutor; | ||
| this.control = control; | ||
| this.asyncFunction = asyncFunction; | ||
| } | ||
|
|
||
| @Override | ||
| public void get(final SingleResultCallback<R> callback) { | ||
| // `asyncFunction` and `callback` are the only externally provided pieces of code for which we do not need to care about | ||
| // them throwing exceptions. If they do, that violates their contract and there is nothing we should do about it. | ||
| asyncFunction.get(new RetryingCallback(callback)); | ||
| } | ||
|
|
||
| /** | ||
| * This callback is allowed to be completed more than once. | ||
| */ | ||
| @NotThreadSafe | ||
| private class RetryingCallback implements SingleResultCallback<R> { | ||
| private final SingleResultCallback<R> wrapped; | ||
|
|
||
| RetryingCallback(final SingleResultCallback<R> callback) { | ||
| wrapped = callback; | ||
| } | ||
|
|
||
| @Override | ||
| public void onResult(@Nullable final R attemptSuccessfulResult, @Nullable final Throwable attemptFailedResult) { | ||
| if (attemptFailedResult != null) { | ||
| MutableValue<MutableValue<R>> asyncFunctionSuccessfulResult = new MutableValue<>(); | ||
| beginAsync().thenRunWhileLoop(() -> asyncFunctionSuccessfulResult.getNullable() == null, iterationCallback -> { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| beginAsync().<R>thenSupply(asyncFunctionCallback -> { | ||
| asyncFunction.get(asyncFunctionCallback); | ||
| }).thenConsume((attemptSuccessfulResult, onAttemptSuccessCallback) -> { | ||
| // `attemptSuccessfulResult` may be `null`, so we have to wrap it in `MutableValue` for the while check to notice it | ||
| asyncFunctionSuccessfulResult.set(new MutableValue<>(attemptSuccessfulResult)); | ||
| onAttemptSuccessCallback.complete(onAttemptSuccessCallback); | ||
| }).onErrorIf(e -> true, (attemptFailedResult, onAttemptFailureCallback) -> { | ||
| if (attemptFailedResult instanceof Error) { | ||
| wrapped.onResult(null, attemptFailedResult); | ||
| return; | ||
| } | ||
| try { | ||
| assertNotNull(control.advanceOrThrow(attemptFailedResult)); | ||
| } catch (Throwable retryingSupplierFailedResult) { | ||
| wrapped.onResult(null, retryingSupplierFailedResult); | ||
| return; | ||
| onAttemptFailureCallback.completeExceptionally(attemptFailedResult); | ||
| } else { | ||
| RetryAttemptInfo retryAttemptInfo = control.advanceOrThrow(attemptFailedResult); | ||
| clientExecutor.sleepAsync(retryAttemptInfo.getBackoff(), onAttemptFailureCallback); | ||
| } | ||
| asyncFunction.get(this); | ||
| } else { | ||
| wrapped.onResult(attemptSuccessfulResult, null); | ||
| } | ||
| } | ||
| }).finish(iterationCallback); | ||
| }).<R>thenSupply(c -> { | ||
| c.complete(asyncFunctionSuccessfulResult.get().getNullable()); | ||
| }).finish(callback); | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -16,10 +16,16 @@ | |
| package com.mongodb.internal.async.function; | ||
|
|
||
| import com.mongodb.annotations.NotThreadSafe; | ||
| import com.mongodb.internal.async.MutableValue; | ||
| import com.mongodb.internal.async.function.RetryPolicy.Decision.RetryAttemptInfo; | ||
| import com.mongodb.internal.thread.AsyncClientExecutor; | ||
| import com.mongodb.lang.Nullable; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static com.mongodb.assertions.Assertions.assertNotNull; | ||
| import static com.mongodb.internal.thread.InterruptionUtil.interruptAndCreateMongoInterruptedException; | ||
| import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
|
|
||
| /** | ||
| * A decorator that implements automatic retrying of failed executions of a {@link Supplier}. | ||
|
|
@@ -37,23 +43,37 @@ public final class RetryingSyncSupplier<R> implements Supplier<R> { | |
| private final Supplier<R> syncFunction; | ||
|
|
||
| /** | ||
| * See {@link RetryingAsyncCallbackSupplier#RetryingAsyncCallbackSupplier(RetryControl, AsyncCallbackSupplier)}. | ||
| * See {@link RetryingAsyncCallbackSupplier#RetryingAsyncCallbackSupplier(AsyncClientExecutor, RetryControl, AsyncCallbackSupplier)}. | ||
| */ | ||
| public RetryingSyncSupplier(final RetryControl<?> control, final Supplier<R> syncFunction) { | ||
| this.control = control; | ||
| this.syncFunction = syncFunction; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public R get() { | ||
| while (true) { | ||
| MutableValue<MutableValue<R>> asyncFunctionSuccessfulResult = new MutableValue<>(); | ||
| while (asyncFunctionSuccessfulResult.getNullable() == null) { | ||
| try { | ||
| return syncFunction.get(); | ||
| R attemptSuccessfulResult = syncFunction.get(); | ||
| // `attemptSuccessfulResult` may be `null`, so we have to wrap it in `MutableValue` for the while check to notice it | ||
| asyncFunctionSuccessfulResult.set(new MutableValue<>(attemptSuccessfulResult)); | ||
| } catch (Error attemptFailedResult) { | ||
| throw attemptFailedResult; | ||
| } catch (Throwable attemptFailedResult) { | ||
| assertNotNull(control.advanceOrThrow(attemptFailedResult)); | ||
| RetryAttemptInfo retryAttemptInfo = control.advanceOrThrow(attemptFailedResult); | ||
| sleep(retryAttemptInfo.getBackoff()); | ||
| } | ||
| } | ||
| return asyncFunctionSuccessfulResult.get().getNullable(); | ||
| } | ||
|
|
||
| private void sleep(final Duration duration) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try { | ||
| NANOSECONDS.sleep(duration.toNanos()); | ||
| } catch (InterruptedException e) { | ||
| throw interruptAndCreateMongoInterruptedException(null, e); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VAKOTODOReplaceAsyncClientExecutor.unimplementedwithAsyncClientExecutor.NO_OP.