This repository was archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Server streaming retries take 2 #463
Merged
garrettjonesgoogle
merged 13 commits into
googleapis:master
from
igorbernstein2:ssc-7b-retries-2
Feb 7, 2018
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5d5ea55
wip: new server streaming retry approach
igorbernstein2 0955f59
wip
igorbernstein2 145ebf3
code cleanup
igorbernstein2 a1d71f2
re-add watchdog
igorbernstein2 29175f5
fix bug
igorbernstein2 c4bfea3
add tests for ServerStreamingAttempCallable
igorbernstein2 4386290
better docs
igorbernstein2 bceb775
add simple tests for settings
igorbernstein2 1dc0207
fix docs
igorbernstein2 59d9c31
address feedback
igorbernstein2 1ece226
oops moved the wrong check
igorbernstein2 ae5cd81
undo wrong change
igorbernstein2 88ca64a
doc tweaks
igorbernstein2 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
65 changes: 65 additions & 0 deletions
65
gax/src/main/java/com/google/api/gax/retrying/ServerStreamingAttemptException.java
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2018, Google LLC All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.retrying; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
|
|
||
| /** | ||
| * A wrapper exception thrown by {@code ServerStreamingAttemptCallable} to communicate additional | ||
| * context to the {@link StreamingRetryAlgorithm} and to pass the original cancellation stack trace | ||
| * to {@code RetryingServerStreamingCallable}. | ||
| * | ||
| * <p>For internal use only - public for technical reasons. | ||
| */ | ||
| @InternalApi("For internal use only") | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
| public class ServerStreamingAttemptException extends RuntimeException { | ||
| private final boolean canResume; | ||
| private final boolean seenResponses; | ||
|
|
||
| public ServerStreamingAttemptException( | ||
| Throwable cause, boolean canResume, boolean seenResponses) { | ||
| super(cause); | ||
| this.canResume = canResume; | ||
| this.seenResponses = seenResponses; | ||
| } | ||
|
|
||
| /** If the {@link StreamResumptionStrategy} supports resuming after this error. */ | ||
| public boolean canResume() { | ||
| return canResume; | ||
| } | ||
|
|
||
| /** | ||
| * If the current RPC attempt has seen any streamed messages. This is used as a signal by {@link | ||
| * StreamingRetryAlgorithm} to reset timers. | ||
| */ | ||
| public boolean hasSeenResponses() { | ||
| return seenResponses; | ||
| } | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
gax/src/main/java/com/google/api/gax/retrying/SimpleStreamResumptionStrategy.java
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright 2018, Google LLC All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.retrying; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
| import com.google.common.base.Preconditions; | ||
|
|
||
| /** | ||
| * Simplest implementation of a {@link StreamResumptionStrategy} which returns the initial request | ||
| * for unstarted streams. | ||
| */ | ||
| @BetaApi("The surface for streaming is not stable yet and may change in the future.") | ||
| public final class SimpleStreamResumptionStrategy<RequestT, ResponseT> | ||
| implements StreamResumptionStrategy<RequestT, ResponseT> { | ||
| private boolean seenFirstResponse; | ||
|
|
||
| @Override | ||
| public StreamResumptionStrategy<RequestT, ResponseT> createNew() { | ||
| return new SimpleStreamResumptionStrategy<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onProgress(ResponseT response) { | ||
| seenFirstResponse = true; | ||
| } | ||
|
|
||
| @Override | ||
| public RequestT getResumeRequest(RequestT originalRequest) { | ||
| Preconditions.checkState(!seenFirstResponse, "Tried to resume an unresumeable stream."); | ||
| return originalRequest; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canResume() { | ||
| return !seenFirstResponse; | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
gax/src/main/java/com/google/api/gax/retrying/StreamResumptionStrategy.java
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright 2018, Google LLC All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.retrying; | ||
|
|
||
| import com.google.api.core.BetaApi; | ||
|
|
||
| /** | ||
| * This is part of the server streaming retry api. Its implementers are responsible for tracking the | ||
| * progress of the stream and calculating a request to resume it in case of an error. | ||
| * | ||
| * <p>Implementations don't have to be threadsafe because all of the calls will be serialized. | ||
| */ | ||
| @BetaApi("The surface for streaming is not stable yet and may change in the future.") | ||
| public interface StreamResumptionStrategy<RequestT, ResponseT> { | ||
|
|
||
| /** Creates a new instance of this StreamResumptionStrategy without accumulated state */ | ||
| StreamResumptionStrategy<RequestT, ResponseT> createNew(); | ||
|
|
||
| /** | ||
| * Called by the {@code ServerStreamingAttemptCallable} when a response has been successfully | ||
| * received. | ||
| */ | ||
| void onProgress(ResponseT response); | ||
|
|
||
| /** | ||
| * Called when a stream needs to be restarted, the implementation should generate a request that | ||
| * will yield a new stream whose first response would come right after the last response received | ||
| * by onProgress. | ||
| * | ||
| * @return A request that can be used to resume the stream. | ||
| */ | ||
| RequestT getResumeRequest(RequestT originalRequest); | ||
|
|
||
| /** If a resume request can be created. */ | ||
| boolean canResume(); | ||
| } |
100 changes: 100 additions & 0 deletions
100
gax/src/main/java/com/google/api/gax/retrying/StreamingRetryAlgorithm.java
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 |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /* | ||
| * Copyright 2018, Google LLC All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * * Redistributions in binary form must reproduce the above | ||
| * copyright notice, this list of conditions and the following disclaimer | ||
| * in the documentation and/or other materials provided with the | ||
| * distribution. | ||
| * * Neither the name of Google LLC nor the names of its | ||
| * contributors may be used to endorse or promote products derived from | ||
| * this software without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.google.api.gax.retrying; | ||
|
|
||
| import com.google.api.core.InternalApi; | ||
| import java.util.concurrent.CancellationException; | ||
|
|
||
| /** | ||
| * The streaming retry algorithm, which makes decision based either on the thrown exception and the | ||
| * execution time settings of the previous attempt. This extends {@link RetryAlgorithm} to take | ||
| * additional information (provided by {@code ServerStreamingAttemptCallable}) into account. | ||
| * | ||
| * <p>This class is thread-safe. | ||
| * | ||
| * <p>Internal use only - public for technical reasons. | ||
| */ | ||
| @InternalApi("For internal use only") | ||
| public final class StreamingRetryAlgorithm<ResponseT> extends RetryAlgorithm<ResponseT> { | ||
| public StreamingRetryAlgorithm( | ||
| ResultRetryAlgorithm<ResponseT> resultAlgorithm, TimedRetryAlgorithm timedAlgorithm) { | ||
| super(resultAlgorithm, timedAlgorithm); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * <p>The attempt settings will be reset if the stream attempt produced any messages. | ||
| */ | ||
| @Override | ||
| public TimedAttemptSettings createNextAttempt( | ||
| Throwable prevThrowable, ResponseT prevResponse, TimedAttemptSettings prevSettings) { | ||
|
|
||
| if (prevThrowable instanceof ServerStreamingAttemptException) { | ||
| ServerStreamingAttemptException wrapper = (ServerStreamingAttemptException) prevThrowable; | ||
| prevThrowable = prevThrowable.getCause(); | ||
|
|
||
| // If we have made progress in the last attempt, then reset the delays | ||
| if (wrapper.hasSeenResponses()) { | ||
| prevSettings = | ||
| createFirstAttempt() | ||
| .toBuilder() | ||
| .setFirstAttemptStartTimeNanos(prevSettings.getFirstAttemptStartTimeNanos()) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
| return super.createNextAttempt(prevThrowable, prevResponse, prevSettings); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * <p>Ensures retries are only scheduled if the {@link StreamResumptionStrategy} in the {@code | ||
| * ServerStreamingAttemptCallable} supports it. | ||
| */ | ||
| @Override | ||
| public boolean shouldRetry( | ||
| Throwable prevThrowable, ResponseT prevResponse, TimedAttemptSettings nextAttemptSettings) | ||
| throws CancellationException { | ||
|
|
||
| // Unwrap | ||
| if (prevThrowable instanceof ServerStreamingAttemptException) { | ||
| ServerStreamingAttemptException wrapper = (ServerStreamingAttemptException) prevThrowable; | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
| prevThrowable = prevThrowable.getCause(); | ||
|
|
||
| if (!wrapper.canResume()) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return super.shouldRetry(prevThrowable, prevResponse, nextAttemptSettings); | ||
| } | ||
| } | ||
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
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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.