-
Notifications
You must be signed in to change notification settings - Fork 866
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry conditions to the strategy if none is configured (#5648)
* Add retry conditions to the strategy if none is configured * Update core/retries/src/main/java/software/amazon/awssdk/retries/internal/BaseRetryStrategy.java * Fix a typo in the name of the class
- Loading branch information
Showing
5 changed files
with
209 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"type": "bugfix", | ||
"description": "This change adds the default retry conditions in the client builder if none are configured to behave similarly to retry policies that are configured behind the scenes without the users having to do that themselves. This will prevent customers using directly the retry strategies builders from `DefaultRetryStrategy` to end up with a no-op strategy." | ||
} |
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
162 changes: 162 additions & 0 deletions
162
...ftware/amazon/awssdk/protocol/tests/retry/RetryStrategyBackfillsEmptyRetryConditions.java
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.protocol.tests.retry; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import com.github.tomakehurst.wiremock.stubbing.Scenario; | ||
import java.net.URI; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.retries.DefaultRetryStrategy; | ||
import software.amazon.awssdk.services.protocoljsonrpc.ProtocolJsonRpcClient; | ||
import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesRequest; | ||
import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesResponse; | ||
import software.amazon.awssdk.services.protocoljsonrpc.model.ProtocolJsonRpcException; | ||
|
||
public class RetryStrategyBackfillsEmptyRetryConditions { | ||
|
||
private static final String PATH = "/"; | ||
private static final String JSON_BODY = "{\"StringMember\":\"foo\"}"; | ||
|
||
@Rule | ||
public WireMockRule wireMock = new WireMockRule(0); | ||
|
||
private ProtocolJsonRpcClient client; | ||
|
||
@Before | ||
public void setupClient() { | ||
client = ProtocolJsonRpcClient.builder() | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", | ||
"skid"))) | ||
.region(Region.US_EAST_1) | ||
.overrideConfiguration(o -> o.retryStrategy(DefaultRetryStrategy.standardStrategyBuilder().build())) | ||
.endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void shouldRetryOn500() { | ||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at 500") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willSetStateTo("first attempt") | ||
.willReturn(aResponse() | ||
.withStatus(500))); | ||
|
||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at 500") | ||
.whenScenarioStateIs("first attempt") | ||
.willSetStateTo("second attempt") | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(JSON_BODY))); | ||
|
||
AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); | ||
assertThat(allTypesResponse).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void shouldRetryOnRetryableAwsErrorCode() { | ||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at PriorRequestNotComplete") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willSetStateTo("first attempt") | ||
.willReturn(aResponse() | ||
.withStatus(400) | ||
.withHeader("x-amzn-ErrorType", "PriorRequestNotComplete") | ||
.withBody("\"{\"__type\":\"PriorRequestNotComplete\",\"message\":\"Blah " | ||
+ "error\"}\""))); | ||
|
||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at PriorRequestNotComplete") | ||
.whenScenarioStateIs("first attempt") | ||
.willSetStateTo("second attempt") | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(JSON_BODY))); | ||
|
||
AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); | ||
assertThat(allTypesResponse).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void shouldRetryOnAwsThrottlingErrorCode() { | ||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at SlowDown") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willSetStateTo("first attempt") | ||
.willReturn(aResponse() | ||
.withStatus(400) | ||
.withHeader("x-amzn-ErrorType", "SlowDown") | ||
.withBody("\"{\"__type\":\"SlowDown\",\"message\":\"Blah " | ||
+ "error\"}\""))); | ||
|
||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at SlowDown") | ||
.whenScenarioStateIs("first attempt") | ||
.willSetStateTo("second attempt") | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(JSON_BODY))); | ||
|
||
AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); | ||
assertThat(allTypesResponse).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void retryStrategyNone_shouldNotRetry() { | ||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at 500") | ||
.whenScenarioStateIs(Scenario.STARTED) | ||
.willSetStateTo("first attempt") | ||
.willReturn(aResponse() | ||
.withStatus(500))); | ||
|
||
stubFor(post(urlEqualTo(PATH)) | ||
.inScenario("retry at 500") | ||
.whenScenarioStateIs("first attempt") | ||
.willSetStateTo("second attempt") | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(JSON_BODY))); | ||
|
||
ProtocolJsonRpcClient clientWithNoRetry = | ||
ProtocolJsonRpcClient.builder() | ||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", | ||
"skid"))) | ||
.region(Region.US_EAST_1) | ||
.endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
// When max attempts is 1 (i.e., just the first attempt is allowed but no retries), | ||
// the back filling should not happen. | ||
.overrideConfiguration(c -> c.retryStrategy(DefaultRetryStrategy.standardStrategyBuilder() | ||
.maxAttempts(1) | ||
.build())) | ||
.build(); | ||
|
||
assertThatThrownBy(() -> clientWithNoRetry.allTypes(AllTypesRequest.builder().build())).isInstanceOf(ProtocolJsonRpcException.class); | ||
} | ||
} |