-
Notifications
You must be signed in to change notification settings - Fork 32
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
Open
Alan4506
wants to merge
11
commits into
smithy-lang:develop
Choose a base branch
from
Alan4506:retries-2.1-sep-update-v2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 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 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
127 changes: 127 additions & 0 deletions
127
.../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,127 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
|
|
||
| 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 | ||
| ), | ||
| default_backoff_scale=_DYNAMODB_DEFAULT_BACKOFF_SCALE, | ||
| ) | ||
| """; | ||
|
|
||
| @Override | ||
| public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) { | ||
| final String pluginFile = "retry"; | ||
| 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(); | ||
|
|
||
| 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); | ||
| }); | ||
| return List.of(filename); | ||
| }) | ||
| .build()); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...re/src/main/java/software/amazon/smithy/python/aws/codegen/AwsLongPollingIntegration.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,45 @@ | ||
| /* | ||
| * 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.Map; | ||
| import java.util.Set; | ||
| import software.amazon.smithy.aws.traits.ServiceTrait; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.OperationShape; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.traits.LongPollTrait; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
|
|
||
| /** | ||
| * Marks long-polling operations so the generic client generator applies | ||
| * long-polling retry behavior to them. | ||
| * | ||
| * <p>An operation is long-polling if it carries the {@code smithy.api#longPoll} | ||
| * trait. Service models do not yet apply the trait, so the known operations are | ||
| * hard-coded as a fallback. Once the trait ships in the models, the fallback | ||
| * can be removed. | ||
| */ | ||
| public final class AwsLongPollingIntegration implements PythonIntegration { | ||
|
|
||
| private static final Map<String, Set<String>> LONG_POLLING_OPERATIONS = Map.of( | ||
| "SQS", | ||
| Set.of("ReceiveMessage"), | ||
| "SFN", | ||
| Set.of("GetActivityTask"), | ||
| "SWF", | ||
| Set.of("PollForActivityTask", "PollForDecisionTask")); | ||
|
|
||
| @Override | ||
| public boolean isLongPollingOperation(Model model, ServiceShape service, OperationShape operation) { | ||
| if (operation.hasTrait(LongPollTrait.class)) { | ||
| return true; | ||
| } | ||
| return service.getTrait(ServiceTrait.class) | ||
| .map(trait -> LONG_POLLING_OPERATIONS.get(trait.getSdkId())) | ||
| .map(operations -> operations.contains(operation.getId().getName())) | ||
| .orElse(false); | ||
| } | ||
| } | ||
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
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
...-core/.changes/next-release/smithy-aws-core-feature-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": "feature", | ||
| "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
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.
[Question, maybe blocking?]
Does it really make sense for us to do this now before the trait is ready? I'd rather not have to track the separate work of updating this generator to function based on the trait and just update it when the rest of the SDKs do.
If we are already committed to shipping this, it's fine, but it is extra effort on our part for no customer benefit.
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.
After a deeper look into other SDKs, I think it makes more sense to keep long polling in this PR, implemented as trait-first with a hardcoded fallback:
smithy.api#longPolltrait shipped in Smithy 1.69 (Add a long-poll trait smithy#3019), sohasTrait(LongPollTrait)compiles and works today. The service models don't carry the trait yet, so it currently falls through to the hardcoded table.