-
Notifications
You must be signed in to change notification settings - Fork 791
/
AsyncRetryableStage.java
188 lines (159 loc) · 8.84 KB
/
AsyncRetryableStage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* 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.internal.http.pipeline.stages;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.io.IOException;
import java.time.Duration;
import java.util.OptionalDouble;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.SdkTestInternalApi;
import software.amazon.awssdk.core.Response;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.internal.http.HttpClientDependencies;
import software.amazon.awssdk.core.internal.http.RequestExecutionContext;
import software.amazon.awssdk.core.internal.http.TransformingAsyncResponseHandler;
import software.amazon.awssdk.core.internal.http.pipeline.RequestPipeline;
import software.amazon.awssdk.core.internal.http.pipeline.stages.utils.RetryableStageHelper;
import software.amazon.awssdk.core.internal.retry.RateLimitingTokenBucket;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.utils.CompletableFutureUtils;
/**
* Wrapper around the pipeline for a single request to provide retry, clockskew and request throttling functionality.
*/
@SdkInternalApi
public final class AsyncRetryableStage<OutputT> implements RequestPipeline<SdkHttpFullRequest,
CompletableFuture<Response<OutputT>>> {
private final TransformingAsyncResponseHandler<Response<OutputT>> responseHandler;
private final RequestPipeline<SdkHttpFullRequest, CompletableFuture<Response<OutputT>>> requestPipeline;
private final ScheduledExecutorService scheduledExecutor;
private final HttpClientDependencies dependencies;
private final RateLimitingTokenBucket rateLimitingTokenBucket;
public AsyncRetryableStage(TransformingAsyncResponseHandler<Response<OutputT>> responseHandler,
HttpClientDependencies dependencies,
RequestPipeline<SdkHttpFullRequest, CompletableFuture<Response<OutputT>>> requestPipeline) {
this.responseHandler = responseHandler;
this.dependencies = dependencies;
this.scheduledExecutor = dependencies.clientConfiguration().option(SdkClientOption.SCHEDULED_EXECUTOR_SERVICE);
this.rateLimitingTokenBucket = new RateLimitingTokenBucket();
this.requestPipeline = requestPipeline;
}
@SdkTestInternalApi
public AsyncRetryableStage(TransformingAsyncResponseHandler<Response<OutputT>> responseHandler,
HttpClientDependencies dependencies,
RequestPipeline<SdkHttpFullRequest, CompletableFuture<Response<OutputT>>> requestPipeline,
RateLimitingTokenBucket rateLimitingTokenBucket) {
this.responseHandler = responseHandler;
this.dependencies = dependencies;
this.scheduledExecutor = dependencies.clientConfiguration().option(SdkClientOption.SCHEDULED_EXECUTOR_SERVICE);
this.requestPipeline = requestPipeline;
this.rateLimitingTokenBucket = rateLimitingTokenBucket;
}
@Override
public CompletableFuture<Response<OutputT>> execute(SdkHttpFullRequest request,
RequestExecutionContext context) throws Exception {
return new RetryingExecutor(request, context).execute();
}
private class RetryingExecutor {
private final AsyncRequestBody originalRequestBody;
private final RequestExecutionContext context;
private final RetryableStageHelper retryableStageHelper;
private RetryingExecutor(SdkHttpFullRequest request, RequestExecutionContext context) {
this.originalRequestBody = context.requestProvider();
this.context = context;
this.retryableStageHelper = new RetryableStageHelper(request, context, rateLimitingTokenBucket, dependencies);
}
public CompletableFuture<Response<OutputT>> execute() throws Exception {
CompletableFuture<Response<OutputT>> future = new CompletableFuture<>();
maybeAttemptExecute(future);
return future;
}
public void maybeAttemptExecute(CompletableFuture<Response<OutputT>> future) {
retryableStageHelper.startingAttempt();
if (!retryableStageHelper.retryPolicyAllowsRetry()) {
future.completeExceptionally(retryableStageHelper.retryPolicyDisallowedRetryException());
return;
}
if (retryableStageHelper.getAttemptNumber() > 1) {
// We failed the last attempt, but will retry. The response handler wants to know when that happens.
responseHandler.onError(retryableStageHelper.getLastException());
// Reset the request provider to the original one before retries, in case it was modified downstream.
context.requestProvider(originalRequestBody);
}
Duration backoffDelay = retryableStageHelper.getBackoffDelay();
OptionalDouble tokenAcquireTimeSeconds = retryableStageHelper.getSendTokenNonBlocking();
if (!tokenAcquireTimeSeconds.isPresent()) {
String errorMessage = "Unable to acquire a send token immediately without waiting. This indicates that ADAPTIVE "
+ "retry mode is enabled, fast fail rate limiting is enabled, and that rate limiting is "
+ "engaged because of prior throttled requests. The request will not be executed.";
future.completeExceptionally(SdkClientException.create(errorMessage));
return;
}
long tokenAcquireTimeMillis = (long) (tokenAcquireTimeSeconds.getAsDouble() * 1000);
if (!backoffDelay.isZero()) {
retryableStageHelper.logBackingOff(backoffDelay);
}
long totalDelayMillis = backoffDelay.toMillis() + tokenAcquireTimeMillis;
if (totalDelayMillis > 0) {
scheduledExecutor.schedule(() -> attemptExecute(future), totalDelayMillis, MILLISECONDS);
} else {
attemptExecute(future);
}
}
private void attemptExecute(CompletableFuture<Response<OutputT>> future) {
CompletableFuture<Response<OutputT>> responseFuture;
try {
retryableStageHelper.logSendingRequest();
responseFuture = requestPipeline.execute(retryableStageHelper.requestToSend(), context);
// If the result future fails, go ahead and fail the response future.
CompletableFutureUtils.forwardExceptionTo(future, responseFuture);
} catch (SdkException | IOException e) {
maybeRetryExecute(future, e);
return;
} catch (Throwable e) {
future.completeExceptionally(e);
return;
}
responseFuture.whenComplete((response, exception) -> {
if (exception != null) {
if (exception instanceof Exception) {
maybeRetryExecute(future, (Exception) exception);
} else {
future.completeExceptionally(exception);
}
return;
}
retryableStageHelper.setLastResponse(response.httpResponse());
if (!response.isSuccess()) {
retryableStageHelper.adjustClockIfClockSkew(response);
maybeRetryExecute(future, response.exception());
return;
}
retryableStageHelper.updateClientSendingRateForSuccessResponse();
retryableStageHelper.attemptSucceeded();
future.complete(response);
});
}
private void maybeRetryExecute(CompletableFuture<Response<OutputT>> future, Exception exception) {
retryableStageHelper.setLastException(exception);
retryableStageHelper.updateClientSendingRateForErrorResponse();
maybeAttemptExecute(future);
}
}
}