-
Notifications
You must be signed in to change notification settings - Fork 31
Implement updated standard retry behavior #733
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
Alan4506
merged 13 commits into
smithy-lang:develop
from
Alan4506:retries-2.1-sep-update-v2
Aug 7, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0f1fb38
Implement updated standard retry behavior
Alan4506 a900e8c
Address review feedback
Alan4506 7b6c346
Apply DynamoDB retry defaults via a service-level plugin
Alan4506 457c163
chore: Merge upstream develop
Alan4506 6241089
Finalize updated standard retries behavior
Alan4506 46bef5b
docs: Fix long-poll trait namespace
Alan4506 fcf40e3
Remove unused code blocks
Alan4506 6cd08fa
Update changelog entry for smithy-core
Alan4506 d3cdf3a
Update ClientGenerator to set long-polling context separately to prev…
Alan4506 6f54733
Scope ClientGenerator context changes to long-polling operations
Alan4506 d3d6c8d
Update docstring
Alan4506 95e343d
Address reviewer feedback
Alan4506 1bf3cad
Address review feedback on retry nits
Alan4506 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
142 changes: 142 additions & 0 deletions
142
.../src/main/java/software/amazon/smithy/python/aws/codegen/AwsDynamoDbRetryIntegration.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,142 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.python.aws.codegen; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
| import software.amazon.smithy.aws.traits.ServiceTrait; | ||
| import software.amazon.smithy.codegen.core.Symbol; | ||
| import software.amazon.smithy.codegen.core.SymbolReference; | ||
| import software.amazon.smithy.python.codegen.GenerationContext; | ||
| import software.amazon.smithy.python.codegen.SmithyPythonDependency; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
| import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin; | ||
|
|
||
| /** | ||
| * Generates DynamoDB's retry defaults as a service-scoped client plugin on | ||
| * DynamoDB / DynamoDB Streams clients. | ||
| */ | ||
| public final class AwsDynamoDbRetryIntegration implements PythonIntegration { | ||
|
|
||
| private static final Set<String> DYNAMODB_SDK_IDS = Set.of("DynamoDB", "DynamoDB Streams"); | ||
|
|
||
| public static final String DYNAMODB_RETRY_MODULE = """ | ||
| _DYNAMODB_DEFAULT_MAX_ATTEMPTS = 4 | ||
| _DYNAMODB_DEFAULT_BACKOFF_SCALE = 0.025 | ||
| _DYNAMODB_DEFAULT_MAX_BACKOFF = 20 | ||
|
|
||
|
|
||
| class _RetryConfig(Protocol): | ||
| retry_strategy: $1T | $2T | None | ||
|
|
||
|
|
||
| def dynamodb_retry_plugin(config: _RetryConfig) -> None: | ||
| \"\"\"Apply DynamoDB's standard-mode retry defaults for any option left unset.\"\"\" | ||
| retry_strategy = config.retry_strategy | ||
| if retry_strategy is not None and not isinstance( | ||
| retry_strategy, $2T | ||
| ): | ||
| return | ||
|
|
||
| if isinstance(retry_strategy, $2T): | ||
| # Explicit options take precedence over separately resolved fields. | ||
| retry_mode = retry_strategy.retry_mode | ||
| max_attempts = retry_strategy.max_attempts | ||
| else: | ||
| # Read independently resolved AsyncConfig fields when available. A legacy | ||
| # Config has no scalar retry fields, so None represents an unset value. | ||
| retry_mode = getattr(config, "retry_mode", None) or "standard" | ||
| max_attempts = getattr(config, "max_attempts", None) | ||
| source_of = getattr(config, "source_of", None) | ||
| if ( | ||
| source_of is not None | ||
| and source_of("max_attempts") == $4T.DEFAULT | ||
| ): | ||
| max_attempts = None | ||
|
|
||
| if retry_mode != "standard": | ||
| return | ||
|
|
||
| config.retry_strategy = $3T( | ||
| max_attempts=( | ||
| max_attempts | ||
| if max_attempts is not None | ||
| else _DYNAMODB_DEFAULT_MAX_ATTEMPTS | ||
| ), | ||
| backoff_strategy=$5T( | ||
| backoff_scale_value=_DYNAMODB_DEFAULT_BACKOFF_SCALE, | ||
| max_backoff=_DYNAMODB_DEFAULT_MAX_BACKOFF, | ||
| jitter_type=$6T.FULL, | ||
| ), | ||
| ) | ||
| """; | ||
|
|
||
| @Override | ||
| public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) { | ||
| final String pluginFile = "retries"; | ||
| final String moduleName = context.settings().moduleName(); | ||
|
|
||
| final SymbolReference dynamodbRetryPlugin = SymbolReference.builder() | ||
| .symbol(Symbol.builder() | ||
| .namespace(String.format("%s.%s", moduleName, pluginFile), ".") | ||
| .definitionFile(String.format("./src/%s/%s.py", moduleName, pluginFile)) | ||
| .name("dynamodb_retry_plugin") | ||
| .build()) | ||
| .build(); | ||
| final Symbol retryStrategy = Symbol.builder() | ||
| .namespace("smithy_core.aio.interfaces.retries", ".") | ||
| .name("RetryStrategy") | ||
| .build(); | ||
| final Symbol retryStrategyOptions = Symbol.builder() | ||
| .namespace("smithy_core.retries", ".") | ||
| .name("RetryStrategyOptions") | ||
| .build(); | ||
| final Symbol standardRetryStrategy = Symbol.builder() | ||
| .namespace("smithy_core.aio.retries", ".") | ||
| .name("StandardRetryStrategy") | ||
| .build(); | ||
| final Symbol configSource = Symbol.builder() | ||
| .namespace("smithy_aws_core.config", ".") | ||
| .name("ConfigSource") | ||
| .build(); | ||
| final Symbol exponentialBackoffStrategy = Symbol.builder() | ||
| .namespace("smithy_core.retries", ".") | ||
| .name("ExponentialRetryBackoffStrategy") | ||
| .build(); | ||
| final Symbol exponentialBackoffJitterType = Symbol.builder() | ||
| .namespace("smithy_core.retries", ".") | ||
| .name("ExponentialBackoffJitterType") | ||
| .build(); | ||
|
|
||
| return List.of( | ||
| RuntimeClientPlugin.builder() | ||
| .servicePredicate((model, service) -> service.getTrait(ServiceTrait.class) | ||
| .map(trait -> DYNAMODB_SDK_IDS.contains(trait.getSdkId())) | ||
| .orElse(false)) | ||
| .pythonPlugin(dynamodbRetryPlugin) | ||
| .writeAdditionalFiles((c) -> { | ||
| String filename = "src/%s/%s.py".formatted(moduleName, pluginFile); | ||
| c.writerDelegator() | ||
| .useFileWriter( | ||
| filename, | ||
| moduleName + ".", | ||
| writer -> { | ||
| writer.addDependency(SmithyPythonDependency.SMITHY_CORE); | ||
| writer.addDependency(AwsPythonDependency.SMITHY_AWS_CORE); | ||
| writer.addStdlibImport("typing", "Protocol"); | ||
| writer.write( | ||
| DYNAMODB_RETRY_MODULE, | ||
| retryStrategy, | ||
| retryStrategyOptions, | ||
| standardRetryStrategy, | ||
| configSource, | ||
| exponentialBackoffStrategy, | ||
| exponentialBackoffJitterType); | ||
| }); | ||
| return List.of(filename); | ||
| }) | ||
| .build()); | ||
| } | ||
| } | ||
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
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
4 changes: 4 additions & 0 deletions
4
...e/.changes/next-release/smithy-aws-core-enhancement-881860b5bda049d1b000983dd2a3bd0e.json
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,4 @@ | ||
| { | ||
| "type": "enhancement", | ||
| "description": "Added support for the `x-amz-retry-after` response header." | ||
| } |
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
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
Oops, something went wrong.
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.
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.
nit: can we explicitly set
max_backoffto a constant that equals 20? in case the underlying default changes, we want to make that explicit here.