|
6 | 6 | import com.amazonaws.services.lambda.runtime.RequestHandler; |
7 | 7 | import java.nio.charset.StandardCharsets; |
8 | 8 | import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.concurrent.CompletionException; |
9 | 10 | import java.util.function.BiFunction; |
10 | 11 | import org.slf4j.Logger; |
11 | 12 | import org.slf4j.LoggerFactory; |
@@ -65,26 +66,43 @@ public static <I, O> DurableExecutionOutput execute( |
65 | 66 | // Execute the handlerFuture in ExecutionManager. If it completes successfully, the output of user function |
66 | 67 | // will be returned. Otherwise, it will complete exceptionally with a SuspendExecutionException or a |
67 | 68 | // failure. |
68 | | - return executionManager |
69 | | - .runUntilCompleteOrSuspend(handlerFuture) |
70 | | - .handle((result, ex) -> { |
71 | | - if (ex != null) { |
72 | | - // an exception thrown from handlerFuture or suspension/termination occurred |
73 | | - Throwable cause = ExceptionHelper.unwrapCompletableFuture(ex); |
74 | | - if (cause instanceof SuspendExecutionException) { |
75 | | - return DurableExecutionOutput.pending(); |
| 69 | + try { |
| 70 | + return executionManager |
| 71 | + .runUntilCompleteOrSuspend(handlerFuture) |
| 72 | + .handle((result, ex) -> { |
| 73 | + if (ex != null) { |
| 74 | + // an exception thrown from handlerFuture or suspension/termination occurred |
| 75 | + Throwable cause = ExceptionHelper.unwrapCompletableFuture(ex); |
| 76 | + |
| 77 | + // return PENDING if it's SuspendExecutionException |
| 78 | + if (cause instanceof SuspendExecutionException) { |
| 79 | + return DurableExecutionOutput.pending(); |
| 80 | + } |
| 81 | + |
| 82 | + // let the backend retry the invocation if the exception is retryable |
| 83 | + if (cause |
| 84 | + instanceof |
| 85 | + UnrecoverableDurableExecutionException |
| 86 | + unrecoverableDurableExecutionException |
| 87 | + && unrecoverableDurableExecutionException.isRetryable()) { |
| 88 | + throw unrecoverableDurableExecutionException; |
| 89 | + } |
| 90 | + |
| 91 | + // fail the execution otherwise |
| 92 | + logger.debug("Execution failed: {}", cause.getMessage()); |
| 93 | + return DurableExecutionOutput.failure(buildErrorObject(cause, config.getSerDes())); |
76 | 94 | } |
77 | | - |
78 | | - logger.debug("Execution failed: {}", cause.getMessage()); |
79 | | - return DurableExecutionOutput.failure(buildErrorObject(cause, config.getSerDes())); |
80 | | - } |
81 | | - // user handler complete successfully |
82 | | - var outputPayload = config.getSerDes().serialize(result); |
83 | | - |
84 | | - logger.debug("Execution completed"); |
85 | | - return DurableExecutionOutput.success(handleLargePayload(executionManager, outputPayload)); |
86 | | - }) |
87 | | - .join(); |
| 95 | + // user handler complete successfully |
| 96 | + logger.debug("Execution completed"); |
| 97 | + var outputPayload = config.getSerDes().serialize(result); |
| 98 | + return DurableExecutionOutput.success(handleLargePayload(executionManager, outputPayload)); |
| 99 | + }) |
| 100 | + .join(); |
| 101 | + } catch (CompletionException e) { |
| 102 | + // unwrap the CompletionException and rethrow the wrapped exception |
| 103 | + ExceptionHelper.sneakyThrow(ExceptionHelper.unwrapCompletableFuture(e)); |
| 104 | + return null; |
| 105 | + } |
88 | 106 | } |
89 | 107 | } |
90 | 108 |
|
|
0 commit comments