-
Notifications
You must be signed in to change notification settings - Fork 55
feat: implement basic retry support in runtime #328
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13f6fa6
feat: implement basic retry support in runtime
ianbotsf 90d5cc4
addressing PR feedback: SdkRetryPolicy → AwsDefaultRetryPolicy
ianbotsf f2e65f3
addressing PR feedback: adding handling of HTTP status code to retry …
ianbotsf 4416e3f
linting
ianbotsf a18cca0
addressing PR feedback: fixing incorrect status code mappings
ianbotsf fc2fccf
Merge branch 'main' into retries
ianbotsf 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
55 changes: 55 additions & 0 deletions
55
...me/protocols/http/common/src/aws/sdk/kotlin/runtime/http/retries/AwsDefaultRetryPolicy.kt
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,55 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0. | ||
| */ | ||
|
|
||
| package aws.sdk.kotlin.runtime.http.retries | ||
|
|
||
| import aws.sdk.kotlin.runtime.AwsServiceException | ||
| import aws.smithy.kotlin.runtime.ServiceErrorMetadata | ||
| import aws.smithy.kotlin.runtime.http.response.HttpResponse | ||
| import aws.smithy.kotlin.runtime.retries.RetryDirective | ||
| import aws.smithy.kotlin.runtime.retries.RetryErrorType.* | ||
| import aws.smithy.kotlin.runtime.retries.impl.StandardRetryPolicy | ||
|
|
||
| public object AwsDefaultRetryPolicy : StandardRetryPolicy() { | ||
| internal val knownErrorTypes = mapOf( | ||
| "BandwidthLimitExceeded" to Throttling, | ||
| "EC2ThrottledException" to Throttling, | ||
| "IDPCommunicationError" to Timeout, | ||
| "LimitExceededException" to Throttling, | ||
| "PriorRequestNotComplete" to Throttling, | ||
| "ProvisionedThroughputExceededException" to Throttling, | ||
| "RequestLimitExceeded" to Throttling, | ||
| "RequestThrottled" to Throttling, | ||
| "RequestThrottledException" to Throttling, | ||
| "RequestTimeout" to Timeout, | ||
| "RequestTimeoutException" to Timeout, | ||
| "SlowDown" to Throttling, | ||
| "ThrottledException" to Throttling, | ||
| "Throttling" to Throttling, | ||
| "ThrottlingException" to Throttling, | ||
| "TooManyRequestsException" to Throttling, | ||
| "TransactionInProgressException" to Throttling, | ||
| ) | ||
|
|
||
| internal val knownStatusCodes = mapOf( | ||
| 500 to Timeout, | ||
| 502 to Timeout, | ||
| 503 to Timeout, | ||
| 504 to Timeout, | ||
| ) | ||
|
|
||
| override fun evaluateOtherExceptions(ex: Throwable): RetryDirective? = when (ex) { | ||
| is AwsServiceException -> evaluateAwsServiceException(ex) | ||
| else -> null | ||
| } | ||
|
|
||
| private fun evaluateAwsServiceException(ex: AwsServiceException): RetryDirective? = with(ex.sdkErrorMetadata) { | ||
| (knownErrorTypes[errorCode] ?: knownStatusCodes[statusCode]) | ||
| ?.let { RetryDirective.RetryError(it) } | ||
| } | ||
|
|
||
| private val ServiceErrorMetadata.statusCode: Int? | ||
| get() = (protocolResponse as? HttpResponse)?.status?.value | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
...otocols/http/common/test/aws/sdk/kotlin/runtime/http/retries/AwsDefaultRetryPolicyTest.kt
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,36 @@ | ||
| package aws.sdk.kotlin.runtime.http.retries | ||
|
|
||
| import aws.sdk.kotlin.runtime.AwsErrorMetadata | ||
| import aws.sdk.kotlin.runtime.AwsServiceException | ||
| import aws.smithy.kotlin.runtime.ServiceErrorMetadata | ||
| import aws.smithy.kotlin.runtime.http.Headers | ||
| import aws.smithy.kotlin.runtime.http.HttpBody | ||
| import aws.smithy.kotlin.runtime.http.HttpStatusCode | ||
| import aws.smithy.kotlin.runtime.http.response.HttpResponse | ||
| import aws.smithy.kotlin.runtime.retries.RetryDirective | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
|
|
||
| class AwsDefaultRetryPolicyTest { | ||
| @Test | ||
| fun testErrorsByErrorCode() { | ||
| AwsDefaultRetryPolicy.knownErrorTypes.forEach { (errorCode, errorType) -> | ||
| val ex = AwsServiceException() | ||
| ex.sdkErrorMetadata.attributes[AwsErrorMetadata.ErrorCode] = errorCode | ||
| val result = AwsDefaultRetryPolicy.evaluate(Result.failure(ex)) | ||
| assertEquals(RetryDirective.RetryError(errorType), result) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testErrorsByStatusCode() { | ||
| AwsDefaultRetryPolicy.knownStatusCodes.forEach { (statusCode, errorType) -> | ||
| val modeledStatusCode = HttpStatusCode.fromValue(statusCode) | ||
| val response = HttpResponse(modeledStatusCode, Headers.Empty, HttpBody.Empty) | ||
| val ex = AwsServiceException() | ||
| ex.sdkErrorMetadata.attributes[ServiceErrorMetadata.ProtocolResponse] = response | ||
| val result = AwsDefaultRetryPolicy.evaluate(Result.failure(ex)) | ||
| assertEquals(RetryDirective.RetryError(errorType), result) | ||
| } | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # SDK-specific Retry Design | ||
|
|
||
| * **Type**: Design | ||
| * **Author(s)**: Ian Botsford | ||
|
|
||
| # Abstract | ||
|
|
||
| The AWS SDK for Kotlin uses a specialization of the generalized | ||
| [**smithy-kotlin** Retry Design](https://github.com/awslabs/smithy-kotlin/blob/main/docs/design/retries.md). This | ||
| document covers those specializations (but does not re-hash the generalized design). | ||
|
|
||
| # SDK implementation | ||
|
|
||
| The SDK uses the following customizations/specializations over the generalized | ||
| [**smithy-kotlin** Retry Design](https://github.com/awslabs/smithy-kotlin/blob/main/docs/design/retries.md): | ||
|
|
||
| ## Retry policy | ||
|
|
||
| The generalized `StandardRetryPolicy` is subclassed to provide support for information only available in AWS-specific | ||
| exception types: | ||
|
|
||
| ```kotlin | ||
| object AwsDefaultRetryPolicy : StandardRetryPolicy() { | ||
| internal val knownErrorTypes = mapOf( | ||
| "BandwidthLimitExceeded" to Throttling, | ||
| "RequestTimeoutException" to Timeout, | ||
| "TooManyRequestsException" to Throttling, | ||
| … | ||
| ) | ||
|
|
||
| override fun evaluateOtherExceptions(ex: Throwable): RetryDirective? = when (ex) { | ||
| is AwsServiceException -> evaluateAwsServiceException(ex) | ||
| else -> null | ||
| } | ||
|
|
||
| private fun evaluateAwsServiceException(ex: AwsServiceException): RetryDirective? = with(ex.sdkErrorMetadata) { | ||
| knownErrorTypes[errorCode]?.let { RetryDirective.RetryError(it) } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| This policy utilizes the error code provided in the exception to derive a retry directive based on a known list. This | ||
| list may grow/change over time. | ||
|
|
||
| # Revision history | ||
|
|
||
| * 9/27/2021 - Created |
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.
correctness
Aren't these
Transienterrors?Uh oh!
There was an error while loading. Please reload this page.
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.
Also don't we need to define error codes forRequestTimeoutandRequestTimeoutExceptionasTransienterror codes?Nevermind, saw your other comment. We apparently call these
TimeoutThe known status codes still looks incorrect though
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.
Right you are, they should be
Timeout.