-
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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.python.codegen.GenerationContext; | ||
| import software.amazon.smithy.python.codegen.RuntimeTypes; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
| import software.amazon.smithy.python.codegen.sections.InitRetryStrategyResolverSection; | ||
| import software.amazon.smithy.python.codegen.writer.PythonWriter; | ||
| import software.amazon.smithy.utils.CodeInterceptor; | ||
| import software.amazon.smithy.utils.CodeSection; | ||
|
|
||
| /** | ||
| * Injects DynamoDB's default retry options (max attempts 4, 25ms non-throttling | ||
| * base backoff). | ||
| */ | ||
| public final class AwsDynamoDbRetryIntegration implements PythonIntegration { | ||
|
|
||
| private static final Set<String> DYNAMODB_SDK_IDS = Set.of("DynamoDB", "DynamoDB Streams"); | ||
|
|
||
| private static final double DYNAMODB_BASE_BACKOFF_SECONDS = 0.025; | ||
| private static final int DYNAMODB_MAX_ATTEMPTS = 4; | ||
|
|
||
| private static boolean isDynamoDb(GenerationContext context) { | ||
| return context.settings() | ||
| .service(context.model()) | ||
| .getTrait(ServiceTrait.class) | ||
| .map(trait -> DYNAMODB_SDK_IDS.contains(trait.getSdkId())) | ||
| .orElse(false); | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends CodeInterceptor<? extends CodeSection, PythonWriter>> interceptors( | ||
| GenerationContext context | ||
| ) { | ||
| if (!isDynamoDb(context)) { | ||
| return List.of(); | ||
| } | ||
| return List.of(new DynamoDbRetryStrategyResolverInterceptor()); | ||
| } | ||
|
|
||
| private static final class DynamoDbRetryStrategyResolverInterceptor | ||
| implements CodeInterceptor<InitRetryStrategyResolverSection, PythonWriter> { | ||
|
|
||
| @Override | ||
| public Class<InitRetryStrategyResolverSection> sectionType() { | ||
| return InitRetryStrategyResolverSection.class; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(PythonWriter writer, String previousText, InitRetryStrategyResolverSection section) { | ||
| writer.write( | ||
| "self._retry_strategy_resolver = $T(default_max_attempts=$L, default_backoff_scale=$L)", | ||
| RuntimeTypes.RETRY_STRATEGY_RESOLVER, | ||
| DYNAMODB_MAX_ATTEMPTS, | ||
| DYNAMODB_BASE_BACKOFF_SECONDS); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.python.codegen.integrations.PythonIntegration; | ||
|
|
||
| /** | ||
| * Marks the known long-polling operations so the generic client generator | ||
| * applies long-polling retry behavior to them. | ||
| * | ||
| * <p>These operations are hard-coded until the {@code aws.api#longPoll} trait | ||
| * ships in service models. Once it ships, this can check for the trait instead. | ||
| */ | ||
| 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) { | ||
| return service.getTrait(ServiceTrait.class) | ||
| .map(trait -> LONG_POLLING_OPERATIONS.get(trait.getSdkId())) | ||
| .map(operations -> operations.contains(operation.getId().getName())) | ||
| .orElse(false); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| import software.amazon.smithy.model.traits.StringTrait; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
| import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin; | ||
| import software.amazon.smithy.python.codegen.sections.InitRetryStrategyResolverSection; | ||
| import software.amazon.smithy.python.codegen.writer.PythonWriter; | ||
| import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
|
||
|
|
@@ -86,13 +87,13 @@ def __init__(self, config: $1T | None = None, plugins: list[$2T] | None = None): | |
| for plugin in client_plugins: | ||
| plugin(self._config) | ||
| self._retry_strategy_resolver = $5T() | ||
| $5C | ||
| """, | ||
| configSymbol, | ||
| pluginSymbol, | ||
| writer.consumer(w -> writeConstructorDocs(w, serviceSymbol.getName())), | ||
| writer.consumer(w -> writeDefaultPlugins(w, defaultPlugins)), | ||
| RuntimeTypes.RETRY_STRATEGY_RESOLVER); | ||
| writer.consumer(this::writeRetryStrategyResolverInit)); | ||
|
|
||
| var topDownIndex = TopDownIndex.of(model); | ||
| var eventStreamIndex = EventStreamIndex.of(model); | ||
|
|
@@ -113,6 +114,22 @@ private void writeDefaultPlugins(PythonWriter writer, Collection<SymbolReference | |
| } | ||
| } | ||
|
|
||
| private void writeRetryStrategyResolverInit(PythonWriter writer) { | ||
| // Wrap this in a section so AWS integrations can inject service-specific retry defaults. | ||
| writer.pushState(new InitRetryStrategyResolverSection()); | ||
| writer.write("self._retry_strategy_resolver = $T()", RuntimeTypes.RETRY_STRATEGY_RESOLVER); | ||
| writer.popState(); | ||
| } | ||
|
|
||
| private boolean isLongPollingOperation(OperationShape operation) { | ||
| for (PythonIntegration integration : context.integrations()) { | ||
| if (integration.isLongPollingOperation(model, service, operation)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private void writeConstructorDocs(PythonWriter writer, String clientName) { | ||
| writer.writeMultiLineDocs(() -> { | ||
| writer.write(""" | ||
|
|
@@ -210,6 +227,7 @@ private void writeSharedOperationInit( | |
| } | ||
|
|
||
| writer.putContext("operation", symbolProvider.toSymbol(operation)); | ||
| writer.putContext("isLongPolling", isLongPollingOperation(operation)); | ||
| writer.addStdlibImport("copy", "deepcopy"); | ||
|
|
||
| writer.write(""" | ||
|
|
@@ -240,7 +258,8 @@ private void writeSharedOperationInit( | |
| auth_scheme_resolver=config.auth_scheme_resolver, | ||
| supported_auth_schemes=config.auth_schemes, | ||
| endpoint_resolver=config.endpoint_resolver, | ||
| retry_strategy=retry_strategy, | ||
| retry_strategy=retry_strategy,${?isLongPolling} | ||
| is_long_polling=True,${/isLongPolling} | ||
|
Contributor
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. [blocking, change requested] This information is retry specific. Plumbing it through every operation call works fine in this instance, but this type of solution isn't scalable. This method signature would grow very quickly if we needed to update the pipeline method's signature for every branching decision that we make as part of the pipeline call. We should talk about the right way to plumb through the information. We already have the |
||
| ) | ||
| """, | ||
| writer.consumer(w -> writeDefaultPlugins(w, defaultPlugins)), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.python.codegen.sections; | ||
|
|
||
| import software.amazon.smithy.utils.CodeSection; | ||
| import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
|
||
| /** | ||
| * A section that controls constructing the client's {@code RetryStrategyResolver}. | ||
| */ | ||
| @SmithyInternalApi | ||
| public record InitRetryStrategyResolverSection() implements CodeSection {} |
| 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 in the `awsQuery` protocol." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "breaking", | ||
| "description": "Updated retry quota costs and added an `is_long_polling` argument to `acquire_initial_retry_token`." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "Added support for separate backoff for throttling errors, the `x-amz-retry-after` header, per-service retry defaults, and long-polling backoff." | ||
| } |
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.
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.
Here's what the SEP says:
I took a look at other smithy SDKs and almost all of them are hardcoding the table now. I also found the trait has since shipped in smithy-lang/smithy#3019, though as
smithy.api#longPollrather thanaws.api#longPoll.However, I checked the service models: none of the operations carry the trait yet.
Also, given that:
I don't mind punting long polling to a follow-up. I agree that it is extra effort on our part for no customer benefit. If it also makes sense to you, then I'll drop the changes for long polling in this PR. Let me know what you think!