-
Notifications
You must be signed in to change notification settings - Fork 912
Fix CompletableFuture hangs when RetryStrategy/MetricsCollector raise errors #6125
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
alextwoods
merged 4 commits into
master
from
feature/master/alexwoo/handle-retrypolicy-exception
May 23, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,6 @@ | ||
{ | ||
"type": "bugfix", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "Fix CompletableFuture hanging when RetryStrategy/MetricsCollector raise errors" | ||
} |
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
129 changes: 129 additions & 0 deletions
129
...test/java/software/amazon/awssdk/core/client/AsyncClientMetricCollectorExceptionTest.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,129 @@ | ||
/* | ||
* 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.core.client; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.when; | ||
import static software.amazon.awssdk.core.internal.util.AsyncResponseHandlerTestUtils.noOpResponseHandler; | ||
import static utils.HttpTestUtils.testAsyncClientBuilder; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.mockito.stubbing.Answer; | ||
import software.amazon.awssdk.core.Response; | ||
import software.amazon.awssdk.core.SdkResponse; | ||
import software.amazon.awssdk.core.async.EmptyPublisher; | ||
import software.amazon.awssdk.core.http.ExecutionContext; | ||
import software.amazon.awssdk.core.http.NoopTestRequest; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain; | ||
import software.amazon.awssdk.core.interceptor.InterceptorContext; | ||
import software.amazon.awssdk.core.internal.http.AmazonAsyncHttpClient; | ||
import software.amazon.awssdk.core.metrics.CoreMetric; | ||
import software.amazon.awssdk.core.protocol.VoidSdkResponse; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.http.SdkHttpFullResponse; | ||
import software.amazon.awssdk.http.SdkHttpResponse; | ||
import software.amazon.awssdk.http.async.AsyncExecuteRequest; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler; | ||
import software.amazon.awssdk.metrics.MetricCollector; | ||
import software.amazon.awssdk.retries.DefaultRetryStrategy; | ||
import utils.ValidSdkObjects; | ||
|
||
/** | ||
* Tests to verify that exceptions thrown by the MetricCollector are reported through the returned future. | ||
* {@link java.util.concurrent.CompletableFuture}. | ||
* | ||
* @see AsyncClientHandlerExceptionTest | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class AsyncClientMetricCollectorExceptionTest { | ||
|
||
public static final String MESSAGE = "test exception"; | ||
|
||
@Mock | ||
private MetricCollector metricCollector; | ||
|
||
@Mock | ||
private SdkAsyncHttpClient asyncHttpClient; | ||
|
||
@Test | ||
public void exceptionInReportMetricReportedInFuture() { | ||
when(metricCollector.createChild(any())).thenReturn(metricCollector); | ||
Exception exception = new RuntimeException(MESSAGE); | ||
doThrow(exception).when(metricCollector).reportMetric(eq(CoreMetric.API_CALL_DURATION), any(Duration.class)); | ||
|
||
CompletableFuture<SdkResponse> responseFuture = makeRequest(); | ||
|
||
assertThatThrownBy(() -> responseFuture.get(1, TimeUnit.SECONDS)).hasRootCause(exception); | ||
} | ||
|
||
private CompletableFuture<SdkResponse> makeRequest() { | ||
when(asyncHttpClient.execute(any(AsyncExecuteRequest.class))).thenAnswer((Answer<CompletableFuture<Void>>) invocationOnMock -> { | ||
SdkAsyncHttpResponseHandler handler = invocationOnMock.getArgument(0, AsyncExecuteRequest.class).responseHandler(); | ||
handler.onHeaders(SdkHttpFullResponse.builder() | ||
.statusCode(200) | ||
.build()); | ||
handler.onStream(new EmptyPublisher<>()); | ||
return CompletableFuture.completedFuture(null); | ||
}); | ||
|
||
AmazonAsyncHttpClient asyncClient = testAsyncClientBuilder() | ||
.retryStrategy(DefaultRetryStrategy.doNotRetry()) | ||
.asyncHttpClient(asyncHttpClient) | ||
.build(); | ||
|
||
SdkHttpFullRequest httpFullRequest = ValidSdkObjects.sdkHttpFullRequest().build(); | ||
NoopTestRequest sdkRequest = NoopTestRequest.builder().build(); | ||
InterceptorContext interceptorContext = InterceptorContext | ||
.builder() | ||
.request(sdkRequest) | ||
.httpRequest(httpFullRequest) | ||
.build(); | ||
|
||
Response<SdkResponse> response = | ||
Response.<SdkResponse>builder() | ||
.isSuccess(true) | ||
.response(VoidSdkResponse.builder().build()) | ||
.httpResponse(SdkHttpResponse.builder().statusCode(200).build()) | ||
.build(); | ||
|
||
return asyncClient | ||
.requestExecutionBuilder() | ||
.originalRequest(sdkRequest) | ||
.request(httpFullRequest) | ||
.executionContext( | ||
ExecutionContext | ||
.builder() | ||
.executionAttributes(new ExecutionAttributes()) | ||
.interceptorContext(interceptorContext) | ||
.metricCollector(metricCollector) | ||
.interceptorChain(new ExecutionInterceptorChain(Collections.emptyList())) | ||
.build() | ||
) | ||
.execute(noOpResponseHandler(response)); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...c/test/java/software/amazon/awssdk/core/client/AsyncClientRetryStrategyExceptionTest.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,110 @@ | ||
/* | ||
* 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.core.client; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static software.amazon.awssdk.core.internal.util.AsyncResponseHandlerTestUtils.noOpResponseHandler; | ||
import static utils.HttpTestUtils.testAsyncClientBuilder; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import software.amazon.awssdk.core.SdkResponse; | ||
import software.amazon.awssdk.core.http.ExecutionContext; | ||
import software.amazon.awssdk.core.http.NoopTestRequest; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain; | ||
import software.amazon.awssdk.core.interceptor.InterceptorContext; | ||
import software.amazon.awssdk.core.internal.http.AmazonAsyncHttpClient; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.metrics.MetricCollector; | ||
import software.amazon.awssdk.retries.api.AcquireInitialTokenResponse; | ||
import software.amazon.awssdk.retries.api.RetryStrategy; | ||
import software.amazon.awssdk.retries.api.RetryToken; | ||
import utils.ValidSdkObjects; | ||
|
||
/** | ||
* Tests to verify that exceptions thrown by the RetryStrategy are reported through the returned future. | ||
* {@link java.util.concurrent.CompletableFuture}. | ||
* | ||
* @see AsyncClientHandlerExceptionTest | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class AsyncClientRetryStrategyExceptionTest { | ||
|
||
public static final String MESSAGE = "test exception"; | ||
|
||
@Mock | ||
private RetryStrategy retryStrategy; | ||
|
||
@Test | ||
public void exceptionInInitialTokenReportedInFuture() { | ||
Exception exception = new RuntimeException(MESSAGE); | ||
when(retryStrategy.acquireInitialToken(any())).thenThrow(exception); | ||
|
||
CompletableFuture<SdkResponse> responseFuture = makeRequest(); | ||
|
||
assertThatThrownBy(() -> responseFuture.get(1, TimeUnit.SECONDS)).hasRootCause(exception); | ||
} | ||
|
||
@Test | ||
public void exceptionInRefreshTokenReportedInFuture() { | ||
when(retryStrategy.acquireInitialToken(any())).thenReturn( | ||
AcquireInitialTokenResponse.create(new RetryToken() { | ||
}, Duration.ZERO) | ||
); | ||
Exception exception = new RuntimeException(MESSAGE); | ||
when(retryStrategy.refreshRetryToken(any())).thenThrow(exception); | ||
|
||
CompletableFuture<SdkResponse> responseFuture = makeRequest(); | ||
|
||
assertThatThrownBy(() -> responseFuture.get(1, TimeUnit.SECONDS)).hasRootCause(exception); | ||
} | ||
|
||
private CompletableFuture<SdkResponse> makeRequest() { | ||
AmazonAsyncHttpClient asyncClient = testAsyncClientBuilder().retryStrategy(retryStrategy).build(); | ||
|
||
SdkHttpFullRequest httpFullRequest = ValidSdkObjects.sdkHttpFullRequest().build(); | ||
NoopTestRequest sdkRequest = NoopTestRequest.builder().build(); | ||
InterceptorContext interceptorContext = InterceptorContext | ||
.builder() | ||
.request(sdkRequest) | ||
.httpRequest(httpFullRequest) | ||
.build(); | ||
|
||
return asyncClient | ||
.requestExecutionBuilder() | ||
.originalRequest(sdkRequest) | ||
.request(httpFullRequest) | ||
.executionContext( | ||
ExecutionContext | ||
.builder() | ||
.executionAttributes(new ExecutionAttributes()) | ||
.interceptorContext(interceptorContext) | ||
.metricCollector(MetricCollector.create("test")) | ||
.interceptorChain(new ExecutionInterceptorChain(Collections.emptyList())) | ||
.build() | ||
) | ||
.execute(noOpResponseHandler()); | ||
} | ||
} |
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.
whenComplete already handles
t != null
exception , why do we needexceptionally
here?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.
This is to handle an exception raised in the whenComplete (ie by the metrics collector)- this is called on the new future returned from whenComplete and not on the original future.