This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use context retry settings for streaming calls
- Loading branch information
Showing
8 changed files
with
522 additions
and
12 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
gax/src/main/java/com/google/api/gax/retrying/ContextAwareStreamingRetryAlgorithm.java
This file contains 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,110 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* 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 ContextAwareStreamingRetryAlgorithm<ResponseT> | ||
extends ContextAwareRetryAlgorithm<ResponseT> { | ||
public ContextAwareStreamingRetryAlgorithm( | ||
ContextAwareResultRetryAlgorithm<ResponseT> resultAlgorithm, | ||
ContextAwareTimedRetryAlgorithm timedAlgorithm) { | ||
super(resultAlgorithm, timedAlgorithm); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* <p>The attempt settings will be reset if the stream attempt produced any messages. | ||
*/ | ||
public TimedAttemptSettings createNextAttempt( | ||
RetryingContext context, | ||
Throwable prevThrowable, | ||
ResponseT prevResponse, | ||
TimedAttemptSettings prevSettings) { | ||
|
||
if (prevThrowable instanceof ServerStreamingAttemptException) { | ||
ServerStreamingAttemptException attemptException = | ||
(ServerStreamingAttemptException) prevThrowable; | ||
prevThrowable = prevThrowable.getCause(); | ||
|
||
// If we have made progress in the last attempt, then reset the delays | ||
if (attemptException.hasSeenResponses()) { | ||
prevSettings = | ||
createFirstAttempt(context) | ||
.toBuilder() | ||
.setFirstAttemptStartTimeNanos(prevSettings.getFirstAttemptStartTimeNanos()) | ||
.setOverallAttemptCount(prevSettings.getOverallAttemptCount()) | ||
.build(); | ||
} | ||
} | ||
|
||
return super.createNextAttempt(context, prevThrowable, prevResponse, prevSettings); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* <p>Ensures retries are only scheduled if the {@link StreamResumptionStrategy} in the {@code | ||
* ServerStreamingAttemptCallable} supports it. | ||
*/ | ||
@Override | ||
public boolean shouldRetry( | ||
RetryingContext context, | ||
Throwable prevThrowable, | ||
ResponseT prevResponse, | ||
TimedAttemptSettings nextAttemptSettings) | ||
throws CancellationException { | ||
|
||
// Unwrap | ||
if (prevThrowable instanceof ServerStreamingAttemptException) { | ||
ServerStreamingAttemptException attemptException = | ||
(ServerStreamingAttemptException) prevThrowable; | ||
prevThrowable = prevThrowable.getCause(); | ||
|
||
if (!attemptException.canResume()) { | ||
return false; | ||
} | ||
} | ||
|
||
return super.shouldRetry(context, prevThrowable, prevResponse, nextAttemptSettings); | ||
} | ||
} |
This file contains 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 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 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.