Skip to content
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

fix: first attempt should use the min of RPC timeout and total timeout #2641

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public TimedAttemptSettings createFirstAttempt() {
return TimedAttemptSettings.newBuilder()
.setGlobalSettings(globalSettings)
.setRetryDelay(Duration.ZERO)
.setRpcTimeout(globalSettings.getInitialRpcTimeout())
.setRpcTimeout(getInitialTimeout(globalSettings))
.setRandomizedRetryDelay(Duration.ZERO)
.setAttemptCount(0)
.setOverallAttemptCount(0)
Expand All @@ -93,12 +93,13 @@ public TimedAttemptSettings createFirstAttempt(RetryingContext context) {
}

RetrySettings retrySettings = context.getRetrySettings();

return TimedAttemptSettings.newBuilder()
// Use the given retrySettings rather than the settings this was created with.
// Attempts created using the TimedAttemptSettings built here will use these
// retrySettings, but a new call will not (unless overridden again).
.setGlobalSettings(retrySettings)
.setRpcTimeout(retrySettings.getInitialRpcTimeout())
.setRpcTimeout(getInitialTimeout(retrySettings))
.setRetryDelay(Duration.ZERO)
.setRandomizedRetryDelay(Duration.ZERO)
.setAttemptCount(0)
Expand Down Expand Up @@ -261,4 +262,18 @@ protected long nextRandomLong(long bound) {
? ThreadLocalRandom.current().nextLong(bound)
: bound;
}

/**
* Returns the timeout of the first attempt. The initial timeout will be min(initialRpcTimeout,
* totalTimeout) if totalTimeout is set.
*/
private Duration getInitialTimeout(RetrySettings retrySettings) {
// If the totalTimeout is zero (not set), then retries are capped by the max attempt
// number. The first attempt will use the initialRpcTimeout value for RPC timeout.
long totalTimeout = retrySettings.getTotalTimeout().toMillis();
return totalTimeout == 0
? retrySettings.getInitialRpcTimeout()
: Duration.ofMillis(
Math.min(retrySettings.getInitialRpcTimeout().toMillis(), totalTimeout));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public abstract class RetrySettings implements Serializable {
* Duration.ZERO} allows the RPC to continue indefinitely (until it hits a Connect Timeout or the
* connection has been terminated).
*
* <p>{@link #getTotalTimeout()} caps how long the logic should keep trying the RPC until it gives
* up completely. If {@link #getTotalTimeout()} is set, initialRpcTimeout should be <=
* totalTimeout.
*
* <p>If there are no configurations, Retries have the default initial RPC timeout value of {@code
* Duration.ZERO}. LRO polling does not use the Initial RPC Timeout value.
*/
Expand Down Expand Up @@ -283,6 +287,10 @@ public abstract static class Builder {
* Duration.ZERO} allows the RPC to continue indefinitely (until it hits a Connect Timeout or
* the connection has been terminated).
*
* <p>{@link #getTotalTimeout()} caps how long the logic should keep trying the RPC until it
* gives up completely. If {@link #getTotalTimeout()} is set, initialRpcTimeout should be <=
* totalTimeout.
*
* <p>If there are no configurations, Retries have the default initial RPC timeout value of
* {@code Duration.ZERO}. LRO polling does not use the Initial RPC Timeout value.
*/
Expand Down Expand Up @@ -382,6 +390,10 @@ public abstract static class Builder {
* Duration.ZERO} allows the RPC to continue indefinitely (until it hits a Connect Timeout or
* the connection has been terminated).
*
* <p>{@link #getTotalTimeout()} caps how long the logic should keep trying the RPC until it
* gives up completely. If {@link #getTotalTimeout()} is set, initialRpcTimeout should be <=
* totalTimeout.
*
* <p>If there are no configurations, Retries have the default initial RPC timeout value of
* {@code Duration.ZERO}. LRO polling does not use the Initial RPC Timeout value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,48 @@ public void testCreateFirstAttemptOverride() {
assertEquals(Duration.ZERO, attempt.getRetryDelay());
}

@Test
public void testCreateFirstAttemptHasCorrectTimeout() {
long rpcTimeout = 100;
long totalTimeout = 10;
RetrySettings retrySettings =
RetrySettings.newBuilder()
.setMaxAttempts(6)
.setInitialRetryDelay(Duration.ofMillis(1L))
.setRetryDelayMultiplier(2.0)
.setMaxRetryDelay(Duration.ofMillis(8L))
.setInitialRpcTimeout(Duration.ofMillis(rpcTimeout))
.setRpcTimeoutMultiplier(1.0)
.setMaxRpcTimeout(Duration.ofMillis(rpcTimeout))
.setTotalTimeout(Duration.ofMillis(totalTimeout))
.build();

ExponentialRetryAlgorithm algorithm = new ExponentialRetryAlgorithm(retrySettings, clock);

TimedAttemptSettings attempt = algorithm.createFirstAttempt();
assertEquals(Duration.ofMillis(totalTimeout), attempt.getRpcTimeout());

long overrideRpcTimeout = 100;
long overrideTotalTimeout = 20;
RetrySettings retrySettingsOverride =
retrySettings
.toBuilder()
.setInitialRpcTimeout(Duration.ofMillis(overrideRpcTimeout))
.setMaxRpcTimeout(Duration.ofMillis(overrideRpcTimeout))
.setTotalTimeout(Duration.ofMillis(overrideTotalTimeout))
.build();
RetryingContext retryingContext =
FakeCallContext.createDefault().withRetrySettings(retrySettingsOverride);
attempt = algorithm.createFirstAttempt(retryingContext);
assertEquals(Duration.ofMillis(overrideTotalTimeout), attempt.getRpcTimeout());

RetrySettings noTotalTimeout = retrySettings.toBuilder().setTotalTimeout(Duration.ZERO).build();

algorithm = new ExponentialRetryAlgorithm(noTotalTimeout, clock);
attempt = algorithm.createFirstAttempt();
assertEquals(attempt.getRpcTimeout(), retrySettings.getInitialRpcTimeout());
}

@Test
public void testCreateNextAttempt() {
TimedAttemptSettings firstAttempt = algorithm.createFirstAttempt();
Expand Down
Loading