/
AsyncWaiterExecutor.java
140 lines (126 loc) · 6.22 KB
/
AsyncWaiterExecutor.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
/*
* 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.waiters;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.waiters.WaiterAcceptor;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.core.waiters.WaiterState;
import software.amazon.awssdk.utils.Either;
import software.amazon.awssdk.utils.Validate;
/**
* Executes async waiter operations
*
* @param <T> the type of the response
*/
@SdkInternalApi
@ThreadSafe
public final class AsyncWaiterExecutor<T> {
private final ScheduledExecutorService executorService;
private final WaiterExecutorHelper<T> executorHelper;
public AsyncWaiterExecutor(WaiterConfiguration configuration,
List<WaiterAcceptor<? super T>> waiterAcceptors,
ScheduledExecutorService executorService) {
Validate.paramNotNull(waiterAcceptors, "waiterAcceptors");
this.executorService = Validate.paramNotNull(executorService, "executorService");
this.executorHelper = new WaiterExecutorHelper<>(waiterAcceptors, configuration);
}
/**
* Execute the provided async polling function
*/
CompletableFuture<WaiterResponse<T>> execute(Supplier<CompletableFuture<T>> asyncPollingFunction) {
CompletableFuture<WaiterResponse<T>> future = new CompletableFuture<>();
doExecute(asyncPollingFunction, future, 0, System.currentTimeMillis());
return future;
}
private void doExecute(Supplier<CompletableFuture<T>> asyncPollingFunction,
CompletableFuture<WaiterResponse<T>> future,
int attemptNumber,
long startTime) {
runAsyncPollingFunction(asyncPollingFunction, future, ++attemptNumber, startTime);
}
private void runAsyncPollingFunction(Supplier<CompletableFuture<T>> asyncPollingFunction,
CompletableFuture<WaiterResponse<T>> future,
int attemptNumber,
long startTime) {
asyncPollingFunction.get().whenComplete((response, exception) -> {
try {
Either<T, Throwable> responseOrException;
if (exception == null) {
responseOrException = Either.left(response);
} else {
if (exception instanceof CompletionException) {
responseOrException = Either.right(exception.getCause());
} else {
responseOrException = Either.right(exception);
}
}
Optional<WaiterAcceptor<? super T>> optionalWaiterAcceptor =
executorHelper.firstWaiterAcceptorIfMatched(responseOrException);
if (optionalWaiterAcceptor.isPresent()) {
WaiterAcceptor<? super T> acceptor = optionalWaiterAcceptor.get();
WaiterState state = acceptor.waiterState();
switch (state) {
case SUCCESS:
future.complete(executorHelper.createWaiterResponse(responseOrException, attemptNumber));
break;
case RETRY:
maybeRetry(asyncPollingFunction, future, attemptNumber, startTime);
break;
case FAILURE:
future.completeExceptionally(executorHelper.waiterFailureException(acceptor));
break;
default:
future.completeExceptionally(new UnsupportedOperationException());
}
} else {
Optional<Throwable> t = responseOrException.right();
if (t.isPresent() && t.get() instanceof Error) {
future.completeExceptionally(t.get());
} else {
future.completeExceptionally(executorHelper.noneMatchException(responseOrException));
}
}
} catch (Throwable t) {
Throwable cause = t instanceof CompletionException ? t.getCause() : t;
if (cause instanceof Error) {
future.completeExceptionally(cause);
} else {
future.completeExceptionally(SdkClientException.create("Encountered unexpected exception.", cause));
}
}
});
}
private void maybeRetry(Supplier<CompletableFuture<T>> asyncPollingFunction,
CompletableFuture<WaiterResponse<T>> future,
int attemptNumber,
long startTime) {
Either<Long, SdkClientException> nextDelayOrUnretryableException =
executorHelper.nextDelayOrUnretryableException(attemptNumber, startTime);
nextDelayOrUnretryableException.apply(
nextDelay -> executorService.schedule(() -> doExecute(asyncPollingFunction, future, attemptNumber, startTime),
nextDelay,
TimeUnit.MILLISECONDS),
future::completeExceptionally);
}
}