|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.java.waiters; |
| 7 | + |
| 8 | +import java.time.Duration; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | +import software.amazon.smithy.java.client.core.RequestOverrideConfig; |
| 14 | +import software.amazon.smithy.java.core.error.ModeledException; |
| 15 | +import software.amazon.smithy.java.core.schema.SerializableStruct; |
| 16 | +import software.amazon.smithy.java.waiters.backoff.BackoffStrategy; |
| 17 | +import software.amazon.smithy.java.waiters.matching.Matcher; |
| 18 | + |
| 19 | +/** |
| 20 | + * Waiters are used to poll a resource until a desired state is reached, or until it is determined that the resource |
| 21 | + * has reached an undesirable terminal state. |
| 22 | + * |
| 23 | + * <p>Waiters will repeatedly poll for the state of a resource using a provided polling function. The state of the |
| 24 | + * resource is then evaluated using a number of {@code Acceptor}s. These acceptors are evaluated in a fixed order and |
| 25 | + * can transition the state of the waiter if they determine the resource state matches some condition. |
| 26 | + * |
| 27 | + * <p>{@code SUCCESS} and {@code FAILURE} states are terminal states for Waiters and will cause the waiter to complete, returning the |
| 28 | + * terminal status. The default waiter state {@code RETRY} causes the waiter to retry polling the resource state. Retries are |
| 29 | + * performed using an exponential backoff approach with jitter. |
| 30 | + * |
| 31 | + * <p>Example usage<pre>{@code |
| 32 | + * var waiter = Waiter.builder(client::getFoo) |
| 33 | + * .success(Matcher.output(o -> o.status().equals("DONE"))) |
| 34 | + * .failure(Matcher.output(o -> o.status().equals("STOPPED"))) |
| 35 | + * .build(); |
| 36 | + * waiter.wait(GetFooInput.builder().id("my-id").build(), 1000); |
| 37 | + * }</pre> |
| 38 | + * |
| 39 | + * @param <I> Input type of resource polling function. |
| 40 | + * @param <O> Output type of resource polling function. |
| 41 | + * @see <a href="https://smithy.io/2.0/additional-specs/waiters.html">Waiter Specification</a> |
| 42 | + */ |
| 43 | +public final class Waiter<I extends SerializableStruct, O extends SerializableStruct> implements WaiterSettings { |
| 44 | + private final Waitable<I, O> pollingFunction; |
| 45 | + private final List<Acceptor<I, O>> acceptors; |
| 46 | + private BackoffStrategy backoffStrategy; |
| 47 | + private RequestOverrideConfig overrideConfig; |
| 48 | + |
| 49 | + private Waiter(Builder<I, O> builder) { |
| 50 | + this.pollingFunction = builder.pollingFunction; |
| 51 | + this.acceptors = Collections.unmodifiableList(builder.acceptors); |
| 52 | + this.backoffStrategy = Objects.requireNonNullElse(builder.backoffStrategy, BackoffStrategy.getDefault()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Wait for the resource to reach a terminal state. |
| 57 | + * |
| 58 | + * @param input Input to use for polling function. |
| 59 | + * @param maxWaitTime maximum amount of time for waiter to wait. |
| 60 | + * @throws WaiterFailureException if the waiter reaches a FAILURE state |
| 61 | + */ |
| 62 | + public void wait(I input, Duration maxWaitTime) { |
| 63 | + wait(input, maxWaitTime.toMillis()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Wait for the resource to reach a terminal state. |
| 68 | + * |
| 69 | + * @param input Input to use for polling function. |
| 70 | + * @param maxWaitTimeMillis maximum wait time |
| 71 | + * @throws WaiterFailureException if the waiter reaches a FAILURE state |
| 72 | + */ |
| 73 | + public void wait(I input, long maxWaitTimeMillis) { |
| 74 | + int attemptNumber = 0; |
| 75 | + long startTime = System.currentTimeMillis(); |
| 76 | + |
| 77 | + while (true) { |
| 78 | + attemptNumber++; |
| 79 | + |
| 80 | + ModeledException exception = null; |
| 81 | + O output = null; |
| 82 | + // Execute call to get input and output types |
| 83 | + try { |
| 84 | + output = pollingFunction.poll(input, overrideConfig); |
| 85 | + } catch (ModeledException modeledException) { |
| 86 | + exception = modeledException; |
| 87 | + } catch (Exception exc) { |
| 88 | + throw WaiterFailureException.builder() |
| 89 | + .message("Waiter encountered unexpected, unmodeled exception while polling.") |
| 90 | + .attemptNumber(attemptNumber) |
| 91 | + .cause(exc) |
| 92 | + .totalTimeMillis(System.currentTimeMillis() - startTime) |
| 93 | + .build(); |
| 94 | + } |
| 95 | + |
| 96 | + WaiterState state; |
| 97 | + try { |
| 98 | + state = resolveState(input, output, exception); |
| 99 | + } catch (Exception exc) { |
| 100 | + throw WaiterFailureException.builder() |
| 101 | + .message("Waiter encountered unexpected exception.") |
| 102 | + .cause(exc) |
| 103 | + .attemptNumber(attemptNumber) |
| 104 | + .totalTimeMillis(System.currentTimeMillis() - startTime) |
| 105 | + .build(); |
| 106 | + } |
| 107 | + |
| 108 | + switch (state) { |
| 109 | + case SUCCESS: |
| 110 | + return; |
| 111 | + case RETRY: |
| 112 | + waitToRetry(attemptNumber, maxWaitTimeMillis, startTime); |
| 113 | + break; |
| 114 | + case FAILURE: |
| 115 | + throw WaiterFailureException.builder() |
| 116 | + .message("Waiter reached terminal, FAILURE state") |
| 117 | + .attemptNumber(attemptNumber) |
| 118 | + .totalTimeMillis(System.currentTimeMillis() - startTime) |
| 119 | + .build(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private WaiterState resolveState(I input, O output, ModeledException exception) { |
| 125 | + // Update state based on first matcher that matches |
| 126 | + for (Acceptor<I, O> acceptor : acceptors) { |
| 127 | + if (acceptor.matcher().matches(input, output, exception)) { |
| 128 | + return acceptor.state(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // If there was an unmatched exception return failure |
| 133 | + if (exception != null) { |
| 134 | + throw exception; |
| 135 | + } |
| 136 | + |
| 137 | + // Otherwise retry |
| 138 | + return WaiterState.RETRY; |
| 139 | + } |
| 140 | + |
| 141 | + private void waitToRetry(int attemptNumber, long maxWaitTimeMillis, long startTimeMillis) { |
| 142 | + long elapsedTimeMillis = System.currentTimeMillis() - startTimeMillis; |
| 143 | + long remainingTime = maxWaitTimeMillis - elapsedTimeMillis; |
| 144 | + |
| 145 | + if (remainingTime < 0) { |
| 146 | + throw WaiterFailureException.builder() |
| 147 | + .message("Waiter timed out after " + attemptNumber + " retry attempts.") |
| 148 | + .attemptNumber(attemptNumber) |
| 149 | + .totalTimeMillis(elapsedTimeMillis) |
| 150 | + .build(); |
| 151 | + } |
| 152 | + var delay = backoffStrategy.computeNextDelayInMills(attemptNumber, remainingTime); |
| 153 | + try { |
| 154 | + Thread.sleep(delay); |
| 155 | + } catch (InterruptedException e) { |
| 156 | + Thread.currentThread().interrupt(); |
| 157 | + throw WaiterFailureException.builder() |
| 158 | + .message("Waiter interrupted while waiting to retry.") |
| 159 | + .attemptNumber(attemptNumber) |
| 160 | + .totalTimeMillis(System.currentTimeMillis() - startTimeMillis) |
| 161 | + .build(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void backoffStrategy(BackoffStrategy backoffStrategy) { |
| 167 | + this.backoffStrategy = Objects.requireNonNull(backoffStrategy, "backoffStrategy cannot be null."); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void overrideConfig(RequestOverrideConfig overrideConfig) { |
| 172 | + this.overrideConfig = Objects.requireNonNull(overrideConfig, "overrideConfig cannot be null."); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Create a new {@link Builder}. |
| 177 | + * |
| 178 | + * @param pollingFunction Client call that will be used to poll for the resource state. |
| 179 | + * @return new {@link Builder} instance. |
| 180 | + * @param <I> Input shape type |
| 181 | + * @param <O> Output shape type |
| 182 | + */ |
| 183 | + public static <I extends SerializableStruct, |
| 184 | + O extends SerializableStruct> Builder<I, O> builder(Waitable<I, O> pollingFunction) { |
| 185 | + return new Builder<>(pollingFunction); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Static builder for {@link Waiter}. |
| 190 | + * |
| 191 | + * @param <I> Polling function input shape type |
| 192 | + * @param <O> Polling function output shape type |
| 193 | + */ |
| 194 | + public static final class Builder<I extends SerializableStruct, O extends SerializableStruct> { |
| 195 | + private final List<Acceptor<I, O>> acceptors = new ArrayList<>(); |
| 196 | + private final Waitable<I, O> pollingFunction; |
| 197 | + private BackoffStrategy backoffStrategy; |
| 198 | + |
| 199 | + private Builder(Waitable<I, O> pollingFunction) { |
| 200 | + this.pollingFunction = pollingFunction; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Add a matcher to the Waiter that will transition the waiter to a SUCCESS state if matched. |
| 205 | + * |
| 206 | + * @param matcher matcher to add |
| 207 | + * @return this builder |
| 208 | + */ |
| 209 | + public Builder<I, O> success(Matcher<I, O> matcher) { |
| 210 | + this.acceptors.add(new Acceptor<>(WaiterState.SUCCESS, matcher)); |
| 211 | + return this; |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * Add a matcher to the Waiter that will transition the waiter to a FAILURE state if matched. |
| 216 | + * |
| 217 | + * @param matcher matcher to add |
| 218 | + * @return this builder |
| 219 | + */ |
| 220 | + public Builder<I, O> failure(Matcher<I, O> matcher) { |
| 221 | + this.acceptors.add(new Acceptor<>(WaiterState.FAILURE, matcher)); |
| 222 | + return this; |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Add a matcher to the Waiter that will transition the waiter to a FAILURE state if matched. |
| 227 | + * |
| 228 | + * @param matcher acceptor to add |
| 229 | + * @return this builder |
| 230 | + */ |
| 231 | + public Builder<I, O> retry(Matcher<I, O> matcher) { |
| 232 | + this.acceptors.add(new Acceptor<>(WaiterState.RETRY, matcher)); |
| 233 | + return this; |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Backoff strategy to use when polling for resource state. |
| 238 | + * |
| 239 | + * @param backoffStrategy backoff strategy to use |
| 240 | + * @return this builder |
| 241 | + */ |
| 242 | + public Builder<I, O> backoffStrategy(BackoffStrategy backoffStrategy) { |
| 243 | + this.backoffStrategy = Objects.requireNonNull(backoffStrategy, "backoffStrategy cannot be null"); |
| 244 | + return this; |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Create an immutable {@link Waiter} instance. |
| 249 | + * |
| 250 | + * @return the built {@code Waiter} object. |
| 251 | + */ |
| 252 | + public Waiter<I, O> build() { |
| 253 | + return new Waiter<>(this); |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * Interface representing a function that can be polled for the state of a resource. |
| 259 | + */ |
| 260 | + @FunctionalInterface |
| 261 | + public interface Waitable<I extends SerializableStruct, O extends SerializableStruct> { |
| 262 | + O poll(I input, RequestOverrideConfig requestContext); |
| 263 | + } |
| 264 | +} |
0 commit comments