-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added ability to specify timeout unit in RequestRetryOptions #17628
Merged
+96
−35
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
import com.azure.core.util.logging.ClientLogger; | ||
|
||
import com.azure.storage.common.implementation.StorageImplUtils; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Configuration options for {@link RequestRetryPolicy}. | ||
|
@@ -16,18 +17,17 @@ public final class RequestRetryOptions { | |
private final ClientLogger logger = new ClientLogger(RequestRetryOptions.class); | ||
|
||
private final int maxTries; | ||
private final int tryTimeout; | ||
private final long retryDelayInMs; | ||
private final long maxRetryDelayInMs; | ||
private final Duration tryTimeout; | ||
private final Duration retryDelay; | ||
private final Duration maxRetryDelay; | ||
private final RetryPolicyType retryPolicyType; | ||
private final String secondaryHost; | ||
|
||
/** | ||
* Configures how the {@link HttpPipeline} should retry requests. | ||
*/ | ||
public RequestRetryOptions() { | ||
this(RetryPolicyType.EXPONENTIAL, null, | ||
null, null, null, null); | ||
this(RetryPolicyType.EXPONENTIAL, null, (Integer) null, null, null, null); | ||
} | ||
|
||
/** | ||
|
@@ -36,8 +36,8 @@ public RequestRetryOptions() { | |
* @param retryPolicyType Optional. A {@link RetryPolicyType} specifying the type of retry pattern to use, default | ||
* value is {@link RetryPolicyType#EXPONENTIAL EXPONENTIAL}. | ||
* @param maxTries Optional. Maximum number of attempts an operation will be retried, default is {@code 4}. | ||
* @param tryTimeout Optional. Specified the maximum time allowed before a request is cancelled and assumed failed, | ||
* default is {@link Integer#MAX_VALUE}. | ||
* @param tryTimeoutInSeconds Optional. Specified the maximum time allowed before a request is cancelled and | ||
* assumed failed, default is {@link Integer#MAX_VALUE} s. | ||
* | ||
* <p>This value should be based on the bandwidth available to the host machine and proximity to the Storage | ||
* service, a good starting point may be 60 seconds per MB of anticipated payload size.</p> | ||
|
@@ -55,8 +55,39 @@ public RequestRetryOptions() { | |
* or non-null or {@code retryPolicyType} isn't {@link RetryPolicyType#EXPONENTIAL} | ||
* or {@link RetryPolicyType#FIXED}. | ||
*/ | ||
public RequestRetryOptions(RetryPolicyType retryPolicyType, Integer maxTries, Integer tryTimeout, | ||
public RequestRetryOptions(RetryPolicyType retryPolicyType, Integer maxTries, Integer tryTimeoutInSeconds, | ||
Long retryDelayInMs, Long maxRetryDelayInMs, String secondaryHost) { | ||
this(retryPolicyType, maxTries, Duration.ofSeconds(tryTimeoutInSeconds), Duration.ofMillis(retryDelayInMs), | ||
Duration.ofMillis(maxRetryDelayInMs), secondaryHost); | ||
} | ||
|
||
/** | ||
* Configures how the {@link HttpPipeline} should retry requests. | ||
* | ||
* @param retryPolicyType Optional. A {@link RetryPolicyType} specifying the type of retry pattern to use, default | ||
* value is {@link RetryPolicyType#EXPONENTIAL EXPONENTIAL}. | ||
* @param maxTries Optional. Maximum number of attempts an operation will be retried, default is {@code 4}. | ||
* @param tryTimeout Optional. Specified the maximum time allowed before a request is cancelled and | ||
* assumed failed, default is {@link Integer#MAX_VALUE}. | ||
* | ||
* <p>This value should be based on the bandwidth available to the host machine and proximity to the Storage | ||
* service, a good starting point may be 60 seconds per MB of anticipated payload size.</p> | ||
* @param retryDelay Optional. Specifies the amount of delay to use before retrying an operation, default value | ||
* is {@code 4ms} when {@code retryPolicyType} is {@link RetryPolicyType#EXPONENTIAL EXPONENTIAL} and {@code 30ms} | ||
* when {@code retryPolicyType} is {@link RetryPolicyType#FIXED FIXED}. | ||
* @param maxRetryDelay Optional. Specifies the maximum delay allowed before retrying an operation, default | ||
* value is {@code 120ms}. | ||
* @param secondaryHost Optional. Specified a secondary Storage account to retry requests against, default is none. | ||
* | ||
* <p>Before setting this understand the issues around reading stale and potentially-inconsistent data, view these | ||
* <a href=https://docs.microsoft.com/en-us/azure/storage/common/storage-designing-ha-apps-with-ragrs>Azure Docs</a> | ||
* for more information.</p> | ||
* @throws IllegalArgumentException If {@code getRetryDelayInMs} and {@code getMaxRetryDelayInMs} are not both null | ||
* or non-null or {@code retryPolicyType} isn't {@link RetryPolicyType#EXPONENTIAL} | ||
* or {@link RetryPolicyType#FIXED}. | ||
*/ | ||
public RequestRetryOptions(RetryPolicyType retryPolicyType, Integer maxTries, Duration tryTimeout, | ||
Duration retryDelay, Duration maxRetryDelay, String secondaryHost) { | ||
this.retryPolicyType = retryPolicyType == null ? RetryPolicyType.EXPONENTIAL : retryPolicyType; | ||
if (maxTries != null) { | ||
StorageImplUtils.assertInBounds("maxRetries", maxTries, 1, Integer.MAX_VALUE); | ||
|
@@ -66,40 +97,43 @@ public RequestRetryOptions(RetryPolicyType retryPolicyType, Integer maxTries, In | |
} | ||
|
||
if (tryTimeout != null) { | ||
StorageImplUtils.assertInBounds("tryTimeout", tryTimeout, 1, Integer.MAX_VALUE); | ||
StorageImplUtils.assertInBounds("'tryTimeout' in seconds", tryTimeout.getSeconds(), 1, | ||
Integer.MAX_VALUE); | ||
this.tryTimeout = tryTimeout; | ||
} else { | ||
/* | ||
Because this timeout applies to the whole operation, and calculating a meaningful timeout for read/write | ||
operations must consider the size of the payload, we can't set a meaningful default value for all requests | ||
and therefore default to no timeout. | ||
*/ | ||
this.tryTimeout = Integer.MAX_VALUE; | ||
this.tryTimeout = Duration.ofSeconds(Integer.MAX_VALUE); | ||
} | ||
|
||
if ((retryDelayInMs == null && maxRetryDelayInMs != null) | ||
|| (retryDelayInMs != null && maxRetryDelayInMs == null)) { | ||
if ((retryDelay == null && maxRetryDelay != null) | ||
|| (retryDelay != null && maxRetryDelay == null)) { | ||
throw logger.logExceptionAsError( | ||
new IllegalArgumentException("Both retryDelay and maxRetryDelay must be null or neither can be null")); | ||
} | ||
|
||
if (retryDelayInMs != null) { | ||
StorageImplUtils.assertInBounds("maxRetryDelayInMs", maxRetryDelayInMs, 1, Long.MAX_VALUE); | ||
StorageImplUtils.assertInBounds("retryDelayInMs", retryDelayInMs, 1, maxRetryDelayInMs); | ||
this.maxRetryDelayInMs = maxRetryDelayInMs; | ||
this.retryDelayInMs = retryDelayInMs; | ||
if (retryDelay != null) { | ||
StorageImplUtils.assertInBounds("'maxRetryDelay' in milliseconds", maxRetryDelay.toMillis(), 1, | ||
Long.MAX_VALUE); | ||
StorageImplUtils.assertInBounds("'retryDelay' in milliseconds", retryDelay.toMillis(), 1, | ||
maxRetryDelay.toMillis()); | ||
this.maxRetryDelay = maxRetryDelay; | ||
this.retryDelay = retryDelay; | ||
} else { | ||
switch (this.retryPolicyType) { | ||
case EXPONENTIAL: | ||
this.retryDelayInMs = TimeUnit.SECONDS.toMillis(4); | ||
this.retryDelay = Duration.ofSeconds(4); | ||
break; | ||
case FIXED: | ||
this.retryDelayInMs = TimeUnit.SECONDS.toMillis(30); | ||
this.retryDelay = Duration.ofSeconds(30); | ||
break; | ||
default: | ||
throw logger.logExceptionAsError(new IllegalArgumentException("Invalid 'RetryPolicyType'.")); | ||
} | ||
this.maxRetryDelayInMs = TimeUnit.SECONDS.toMillis(120); | ||
this.maxRetryDelay = Duration.ofSeconds(120); | ||
} | ||
this.secondaryHost = secondaryHost; | ||
} | ||
|
@@ -113,8 +147,17 @@ public int getMaxTries() { | |
|
||
/** | ||
* @return the maximum time, in seconds, allowed for a request until it is considered timed out. | ||
* @deprecated Please use {@link RequestRetryOptions#getTryTimeoutDuration()} | ||
*/ | ||
@Deprecated | ||
public int getTryTimeout() { | ||
return (int) this.tryTimeout.getSeconds(); | ||
} | ||
|
||
/** | ||
* @return the maximum time, in seconds, allowed for a request until it is considered timed out. | ||
*/ | ||
public Duration getTryTimeoutDuration() { | ||
return this.tryTimeout; | ||
} | ||
|
||
|
@@ -128,16 +171,34 @@ public String getSecondaryHost() { | |
|
||
/** | ||
* @return the delay in milliseconds between each retry attempt. | ||
* @deprecated Please use {@link RequestRetryOptions#getTryTimeoutDuration()} | ||
*/ | ||
@Deprecated | ||
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. Do we need to deprecate the getters? |
||
public long getRetryDelayInMs() { | ||
return retryDelayInMs; | ||
return retryDelay.toMillis(); | ||
} | ||
|
||
/** | ||
* @return the delay between each retry attempt. | ||
*/ | ||
public Duration getRetryDelay() { | ||
return retryDelay; | ||
} | ||
|
||
/** | ||
* @return the maximum delay in milliseconds allowed between each retry. | ||
* @deprecated Please use {@link RequestRetryOptions#getTryTimeoutDuration()} | ||
*/ | ||
@Deprecated | ||
public long getMaxRetryDelayInMs() { | ||
return maxRetryDelayInMs; | ||
return maxRetryDelay.toMillis(); | ||
} | ||
|
||
/** | ||
* @return the maximum delay allowed between each retry. | ||
*/ | ||
public Duration getMaxRetryDelay() { | ||
return maxRetryDelay; | ||
} | ||
|
||
/** | ||
|
@@ -150,18 +211,18 @@ long calculateDelayInMs(int tryCount) { | |
long delay; | ||
switch (this.retryPolicyType) { | ||
case EXPONENTIAL: | ||
delay = (powOfTwo(tryCount - 1) - 1L) * this.retryDelayInMs; | ||
delay = (powOfTwo(tryCount - 1) - 1L) * this.retryDelay.toMillis(); | ||
break; | ||
|
||
case FIXED: | ||
// The first try should have zero delay. Every other try has the fixed value | ||
delay = tryCount > 1 ? this.retryDelayInMs : 0; | ||
delay = tryCount > 1 ? this.retryDelay.toMillis() : 0; | ||
break; | ||
default: | ||
throw logger.logExceptionAsError(new IllegalArgumentException("Invalid retry policy type.")); | ||
} | ||
|
||
return Math.min(delay, this.maxRetryDelayInMs); | ||
return Math.min(delay, this.maxRetryDelay.toMillis()); | ||
} | ||
|
||
private long powOfTwo(int exponent) { | ||
|
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
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.
Any better ideas for this? :(
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.
I think this is consistent with what we did for like blockSizeLong, so I think it's fine.