|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package io.helidon.microprofile.faulttolerance; |
| 18 | + |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import java.util.concurrent.CompletionStage; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.Executor; |
| 23 | +import java.util.concurrent.Future; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.TimeoutException; |
| 26 | +import java.util.function.BiConsumer; |
| 27 | +import java.util.function.BiFunction; |
| 28 | +import java.util.function.Consumer; |
| 29 | +import java.util.function.Function; |
| 30 | +import java.util.function.Supplier; |
| 31 | + |
| 32 | +/** |
| 33 | + * A wrapper {@link CompletableFuture} which also records the associated {@link FaultToleranceCommand} so |
| 34 | + * {@link CommandScheduler} can retrieve that command. If the delegate returns a result of type |
| 35 | + * {@code Future} then this implementation further unwraps the delegate's value to return the actual |
| 36 | + * value. |
| 37 | + * |
| 38 | + * @param <T> type of result conveyed |
| 39 | + */ |
| 40 | +class CommandCompletableFuture<T> extends CompletableFuture<T> { |
| 41 | + |
| 42 | + static <U> CommandCompletableFuture<U> create(CompletableFuture<U> delegate, |
| 43 | + Supplier<FaultToleranceCommand> commandSupplier) { |
| 44 | + return new CommandCompletableFuture<>(delegate, commandSupplier); |
| 45 | + } |
| 46 | + |
| 47 | + private final CompletableFuture<T> delegate; |
| 48 | + private final Supplier<FaultToleranceCommand> commandSupplier; |
| 49 | + |
| 50 | + private CommandCompletableFuture(CompletableFuture<T> delegate, |
| 51 | + Supplier<FaultToleranceCommand> commandSupplier) { |
| 52 | + this.delegate = delegate; |
| 53 | + this.commandSupplier = commandSupplier; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean isDone() { |
| 58 | + return delegate.isDone(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public T get() throws InterruptedException, ExecutionException { |
| 63 | + try { |
| 64 | + return getResult(-1L, null); |
| 65 | + } catch (TimeoutException e) { |
| 66 | + throw new RuntimeException(e); // should never be thrown |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 72 | + return getResult(timeout, unit); |
| 73 | + } |
| 74 | + |
| 75 | + @SuppressWarnings("unchecked") |
| 76 | + T getResult(long timeout, TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException { |
| 77 | + Object result = timeout < 0 ? delegate.get() : delegate.get(timeout, unit); |
| 78 | + if (result instanceof CompletionStage<?>) { |
| 79 | + result = ((CompletionStage<T>) result).toCompletableFuture(); |
| 80 | + } |
| 81 | + if (result instanceof Future<?>) { |
| 82 | + final Future<T> future = (Future<T>) result; |
| 83 | + return timeout < 0 ? future.get() : future.get(timeout, unit); |
| 84 | + } |
| 85 | + return (T) result; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public T join() { |
| 90 | + return delegate.join(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public T getNow(T valueIfAbsent) { |
| 95 | + return delegate.getNow(valueIfAbsent); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean complete(T value) { |
| 100 | + |
| 101 | + return delegate.complete(value); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean completeExceptionally(Throwable ex) { |
| 106 | + return delegate.completeExceptionally(ex); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public <U> CompletableFuture<U> thenApply(Function<? super T, ? extends U> fn) { |
| 111 | + return delegate.thenApply(fn); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn) { |
| 116 | + return delegate.thenApplyAsync(fn); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public <U> CompletableFuture<U> thenApplyAsync(Function<? super T, ? extends U> fn, Executor executor) { |
| 121 | + return delegate.thenApplyAsync(fn, executor); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public CompletableFuture<Void> thenAccept(Consumer<? super T> action) { |
| 126 | + return delegate.thenAccept(action); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action) { |
| 131 | + return delegate.thenAcceptAsync(action); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action, Executor executor) { |
| 136 | + return delegate.thenAcceptAsync(action, executor); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public CompletableFuture<Void> thenRun(Runnable action) { |
| 141 | + return delegate.thenRun(action); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public CompletableFuture<Void> thenRunAsync(Runnable action) { |
| 146 | + return delegate.thenRunAsync(action); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public CompletableFuture<Void> thenRunAsync(Runnable action, Executor executor) { |
| 151 | + return delegate.thenRunAsync(action, executor); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public <U, V> CompletableFuture<V> thenCombine(CompletionStage<? extends U> other, |
| 156 | + BiFunction<? super T, ? super U, ? extends V> fn) { |
| 157 | + return delegate.thenCombine(other, fn); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, |
| 162 | + BiFunction<? super T, ? super U, ? extends V> fn) { |
| 163 | + return delegate.thenCombineAsync(other, fn); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public <U, V> CompletableFuture<V> thenCombineAsync(CompletionStage<? extends U> other, |
| 168 | + BiFunction<? super T, ? super U, ? extends V> fn, |
| 169 | + Executor executor) { |
| 170 | + return delegate.thenCombineAsync(other, fn, executor); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public <U> CompletableFuture<Void> thenAcceptBoth(CompletionStage<? extends U> other, |
| 175 | + BiConsumer<? super T, ? super U> action) { |
| 176 | + return delegate.thenAcceptBoth(other, action); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, |
| 181 | + BiConsumer<? super T, ? super U> action) { |
| 182 | + return delegate.thenAcceptBothAsync(other, action); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public <U> CompletableFuture<Void> thenAcceptBothAsync(CompletionStage<? extends U> other, |
| 187 | + BiConsumer<? super T, ? super U> action, Executor executor) { |
| 188 | + return delegate.thenAcceptBothAsync(other, action, executor); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action) { |
| 193 | + return delegate.runAfterBoth(other, action); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action) { |
| 198 | + return delegate.runAfterBothAsync(other, action); |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action, Executor executor) { |
| 203 | + return delegate.runAfterBothAsync(other, action, executor); |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public <U> CompletableFuture<U> applyToEither(CompletionStage<? extends T> other, Function<? super T, U> fn) { |
| 208 | + return delegate.applyToEither(other, fn); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn) { |
| 213 | + return delegate.applyToEitherAsync(other, fn); |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public <U> CompletableFuture<U> applyToEitherAsync(CompletionStage<? extends T> other, Function<? super T, U> fn, |
| 218 | + Executor executor) { |
| 219 | + return delegate.applyToEitherAsync(other, fn, executor); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public CompletableFuture<Void> acceptEither(CompletionStage<? extends T> other, Consumer<? super T> action) { |
| 224 | + return delegate.acceptEither(other, action); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action) { |
| 229 | + return delegate.acceptEitherAsync(other, action); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public CompletableFuture<Void> acceptEitherAsync(CompletionStage<? extends T> other, Consumer<? super T> action, |
| 234 | + Executor executor) { |
| 235 | + return delegate.acceptEitherAsync(other, action, executor); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action) { |
| 240 | + return delegate.runAfterEither(other, action); |
| 241 | + } |
| 242 | + |
| 243 | + @Override |
| 244 | + public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action) { |
| 245 | + return delegate.runAfterEitherAsync(other, action); |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action, Executor executor) { |
| 250 | + return delegate.runAfterEitherAsync(other, action, executor); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn) { |
| 255 | + return delegate.thenCompose(fn); |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn) { |
| 260 | + return delegate.thenComposeAsync(fn); |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + public <U> CompletableFuture<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn, |
| 265 | + Executor executor) { |
| 266 | + return delegate.thenComposeAsync(fn, executor); |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public CompletableFuture<T> whenComplete(BiConsumer<? super T, ? super Throwable> action) { |
| 271 | + return delegate.whenComplete(action); |
| 272 | + } |
| 273 | + |
| 274 | + @Override |
| 275 | + public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action) { |
| 276 | + return delegate.whenCompleteAsync(action); |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor) { |
| 281 | + return delegate.whenCompleteAsync(action, executor); |
| 282 | + } |
| 283 | + |
| 284 | + @Override |
| 285 | + public <U> CompletableFuture<U> handle(BiFunction<? super T, Throwable, ? extends U> fn) { |
| 286 | + return delegate.handle(fn); |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn) { |
| 291 | + return delegate.handleAsync(fn); |
| 292 | + } |
| 293 | + |
| 294 | + @Override |
| 295 | + public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor) { |
| 296 | + return delegate.handleAsync(fn, executor); |
| 297 | + } |
| 298 | + |
| 299 | + @Override |
| 300 | + public CompletableFuture<T> toCompletableFuture() { |
| 301 | + return this; |
| 302 | + } |
| 303 | + |
| 304 | + @Override |
| 305 | + public CompletableFuture<T> exceptionally(Function<Throwable, ? extends T> fn) { |
| 306 | + return delegate.exceptionally(fn); |
| 307 | + } |
| 308 | + |
| 309 | + @Override |
| 310 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 311 | + FaultToleranceCommand command = commandSupplier.get(); |
| 312 | + BulkheadHelper bulkheadHelper = command.getBulkheadHelper(); |
| 313 | + if (bulkheadHelper != null && !bulkheadHelper.isInvocationRunning(command)) { |
| 314 | + return delegate.cancel(true); // overridden |
| 315 | + } |
| 316 | + return delegate.cancel(mayInterruptIfRunning); |
| 317 | + } |
| 318 | + |
| 319 | + @Override |
| 320 | + public boolean isCancelled() { |
| 321 | + return delegate.isCancelled(); |
| 322 | + } |
| 323 | + |
| 324 | + @Override |
| 325 | + public boolean isCompletedExceptionally() { |
| 326 | + return delegate.isCompletedExceptionally(); |
| 327 | + } |
| 328 | + |
| 329 | + @Override |
| 330 | + public void obtrudeValue(T value) { |
| 331 | + delegate.obtrudeValue(value); |
| 332 | + } |
| 333 | + |
| 334 | + @Override |
| 335 | + public void obtrudeException(Throwable ex) { |
| 336 | + delegate.obtrudeException(ex); |
| 337 | + } |
| 338 | + |
| 339 | + @Override |
| 340 | + public int getNumberOfDependents() { |
| 341 | + return delegate.getNumberOfDependents(); |
| 342 | + } |
| 343 | + |
| 344 | + @Override |
| 345 | + public String toString() { |
| 346 | + return String.format("%s@%h around %s", getClass().getName(), this, delegate.toString()); |
| 347 | + } |
| 348 | +} |
0 commit comments