-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDurableContext.java
More file actions
443 lines (383 loc) · 21 KB
/
DurableContext.java
File metadata and controls
443 lines (383 loc) · 21 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package software.amazon.lambda.durable;
import java.time.Duration;
import java.util.Collection;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import software.amazon.lambda.durable.context.BaseContext;
import software.amazon.lambda.durable.model.MapResult;
import software.amazon.lambda.durable.model.WaitForConditionResult;
public interface DurableContext extends BaseContext {
/**
* Executes a durable step with the given name and blocks until it completes.
*
* <p>On first execution, runs {@code func} and checkpoints the result. On replay, returns the cached result without
* re-executing.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the result class for deserialization
* @param func the function to execute, receiving a {@link StepContext}
* @return the step result
*/
<T> T step(String name, Class<T> resultType, Function<StepContext, T> func);
/**
* Executes a durable step with the given name and configuration, blocking until it completes.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the result class for deserialization
* @param func the function to execute, receiving a {@link StepContext}
* @param config the step configuration (retry strategy, semantics, custom SerDes)
* @return the step result
*/
<T> T step(String name, Class<T> resultType, Function<StepContext, T> func, StepConfig config);
/**
* Executes a durable step using a {@link TypeToken} for generic result types, blocking until it completes.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic types
* @param func the function to execute, receiving a {@link StepContext}
* @return the step result
*/
<T> T step(String name, TypeToken<T> resultType, Function<StepContext, T> func);
/**
* Executes a durable step using a {@link TypeToken} and configuration, blocking until it completes.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic types
* @param func the function to execute, receiving a {@link StepContext}
* @param config the step configuration (retry strategy, semantics, custom SerDes)
* @return the step result
*/
<T> T step(String name, TypeToken<T> resultType, Function<StepContext, T> func, StepConfig config);
/**
* Asynchronously executes a durable step, returning a {@link DurableFuture} that can be composed or blocked on.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the result class for deserialization
* @param func the function to execute, receiving a {@link StepContext}
* @return a future representing the step result
*/
<T> DurableFuture<T> stepAsync(String name, Class<T> resultType, Function<StepContext, T> func);
/**
* Asynchronously executes a durable step using custom configuration.
*
* <p>This is the core stepAsync implementation. All other step/stepAsync overloads delegate here.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param func the function to execute, receiving a {@link StepContext}
* @param config the step configuration (retry strategy, semantics, custom SerDes)
* @return a future representing the step result
*/
<T> DurableFuture<T> stepAsync(String name, Class<T> resultType, Function<StepContext, T> func, StepConfig config);
/**
* Asynchronously executes a durable step using a {@link TypeToken} for generic result types.
*
* <p>This is the core stepAsync implementation. All other step/stepAsync overloads delegate here.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic types
* @param func the function to execute, receiving a {@link StepContext}
* @return a future representing the step result
*/
<T> DurableFuture<T> stepAsync(String name, TypeToken<T> resultType, Function<StepContext, T> func);
/**
* Asynchronously executes a durable step using a {@link TypeToken} and custom configuration.
*
* <p>This is the core stepAsync implementation. All other step/stepAsync overloads delegate here.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic types
* @param func the function to execute, receiving a {@link StepContext}
* @param config the step configuration (retry strategy, semantics, custom SerDes)
* @return a future representing the step result
*/
<T> DurableFuture<T> stepAsync(
String name, TypeToken<T> resultType, Function<StepContext, T> func, StepConfig config);
@Deprecated
<T> T step(String name, Class<T> resultType, Supplier<T> func);
@Deprecated
<T> T step(String name, Class<T> resultType, Supplier<T> func, StepConfig config);
@Deprecated
<T> T step(String name, TypeToken<T> resultType, Supplier<T> func);
@Deprecated
<T> T step(String name, TypeToken<T> resultType, Supplier<T> func, StepConfig config);
@Deprecated
<T> DurableFuture<T> stepAsync(String name, Class<T> resultType, Supplier<T> func);
@Deprecated
<T> DurableFuture<T> stepAsync(String name, Class<T> resultType, Supplier<T> func, StepConfig config);
@Deprecated
<T> DurableFuture<T> stepAsync(String name, TypeToken<T> resultType, Supplier<T> func);
@Deprecated
<T> DurableFuture<T> stepAsync(String name, TypeToken<T> resultType, Supplier<T> func, StepConfig config);
/**
* Suspends execution for the specified duration without consuming compute resources.
*
* <p>On first execution, checkpoints a wait operation and suspends the Lambda. On replay after the duration has
* elapsed, returns immediately.
*
* @param name the unique operation name within this context
* @param duration the duration to wait
* @return always {@code null}
*/
Void wait(String name, Duration duration);
/**
* Asynchronously suspends execution for the specified duration.
*
* @param name the unique operation name within this context
* @param duration the duration to wait
* @return a future that completes when the wait duration has elapsed
*/
DurableFuture<Void> waitAsync(String name, Duration duration);
/**
* Invokes another Lambda function by name and blocks until the result is available.
*
* <p>On first execution, checkpoints a chained invoke operation that triggers the target function. On replay,
* returns the cached result without re-invoking.
*
* @param <T> the result type
* @param <U> the payload type
* @param name the unique operation name within this context
* @param functionName the ARN or name of the Lambda function to invoke
* @param payload the input payload to send to the target function
* @param resultType the result class for deserialization
* @return the invocation result
*/
<T, U> T invoke(String name, String functionName, U payload, Class<T> resultType);
/** Invokes another Lambda function with custom configuration, blocking until the result is available. */
<T, U> T invoke(String name, String functionName, U payload, Class<T> resultType, InvokeConfig config);
/** Invokes another Lambda function using a {@link TypeToken} for generic result types, blocking until complete. */
<T, U> T invoke(String name, String functionName, U payload, TypeToken<T> resultType);
/** Invokes another Lambda function using a {@link TypeToken} and custom configuration, blocking until complete. */
<T, U> T invoke(String name, String functionName, U payload, TypeToken<T> resultType, InvokeConfig config);
/** Invokes another Lambda function using a {@link TypeToken} and custom configuration, blocking until complete. */
<T, U> DurableFuture<T> invokeAsync(
String name, String functionName, U payload, Class<T> resultType, InvokeConfig config);
/** Asynchronously invokes another Lambda function, returning a {@link DurableFuture}. */
<T, U> DurableFuture<T> invokeAsync(String name, String functionName, U payload, Class<T> resultType);
/** Asynchronously invokes another Lambda function using a {@link TypeToken} for generic result types. */
<T, U> DurableFuture<T> invokeAsync(String name, String functionName, U payload, TypeToken<T> resultType);
/**
* Asynchronously invokes another Lambda function using a {@link TypeToken} and custom configuration.
*
* <p>This is the core invokeAsync implementation. All other invoke/invokeAsync overloads delegate here.
*
* @param <T> the result type
* @param <U> the payload type
* @param name the unique operation name within this context
* @param functionName the ARN or name of the Lambda function to invoke
* @param payload the input payload to send to the target function
* @param resultType the type token for deserialization of generic result types
* @param config the invoke configuration (custom SerDes for result and payload)
* @return a future representing the invocation result
*/
<T, U> DurableFuture<T> invokeAsync(
String name, String functionName, U payload, TypeToken<T> resultType, InvokeConfig config);
/** Creates a callback with custom configuration. */
<T> DurableCallbackFuture<T> createCallback(String name, Class<T> resultType, CallbackConfig config);
/** Creates a callback using a {@link TypeToken} for generic result types. */
<T> DurableCallbackFuture<T> createCallback(String name, TypeToken<T> resultType);
/** Creates a callback with default configuration. */
<T> DurableCallbackFuture<T> createCallback(String name, Class<T> resultType);
/**
* Creates a callback operation that suspends execution until an external system completes it.
*
* <p>This is the core createCallback implementation. Returns a {@link DurableCallbackFuture} containing a callback
* ID that external systems use to report completion via the Lambda Durable API.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic result types
* @param config the callback configuration (custom SerDes)
* @return a future containing the callback ID and eventual result
*/
<T> DurableCallbackFuture<T> createCallback(String name, TypeToken<T> resultType, CallbackConfig config);
/**
* Runs a function in a child context, blocking until it completes.
*
* <p>Child contexts provide isolated operation ID namespaces, allowing nested workflows to be composed without ID
* collisions. On replay, the child context's operations are replayed independently.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the result class for deserialization
* @param func the function to execute, receiving a child {@link DurableContext}
* @return the child context result
*/
<T> T runInChildContext(String name, Class<T> resultType, Function<DurableContext, T> func);
/**
* Runs a function in a child context using a {@link TypeToken} for generic result types, blocking until complete.
*/
<T> T runInChildContext(String name, TypeToken<T> resultType, Function<DurableContext, T> func);
/** Asynchronously runs a function in a child context, returning a {@link DurableFuture}. */
<T> DurableFuture<T> runInChildContextAsync(String name, Class<T> resultType, Function<DurableContext, T> func);
/** Asynchronously runs a function in a child context using a {@link TypeToken} for generic result types. */
<T> DurableFuture<T> runInChildContextAsync(String name, TypeToken<T> resultType, Function<DurableContext, T> func);
<I, O> MapResult<O> map(String name, Collection<I> items, Class<O> resultType, MapFunction<I, O> function);
<I, O> MapResult<O> map(
String name, Collection<I> items, Class<O> resultType, MapFunction<I, O> function, MapConfig config);
<I, O> MapResult<O> map(String name, Collection<I> items, TypeToken<O> resultType, MapFunction<I, O> function);
<I, O> MapResult<O> map(
String name, Collection<I> items, TypeToken<O> resultType, MapFunction<I, O> function, MapConfig config);
<I, O> DurableFuture<MapResult<O>> mapAsync(
String name, Collection<I> items, Class<O> resultType, MapFunction<I, O> function);
<I, O> DurableFuture<MapResult<O>> mapAsync(
String name, Collection<I> items, Class<O> resultType, MapFunction<I, O> function, MapConfig config);
<I, O> DurableFuture<MapResult<O>> mapAsync(
String name, Collection<I> items, TypeToken<O> resultType, MapFunction<I, O> function);
<I, O> DurableFuture<MapResult<O>> mapAsync(
String name, Collection<I> items, TypeToken<O> resultType, MapFunction<I, O> function, MapConfig config);
/**
* Creates a {@link ParallelContext} for executing multiple branches concurrently.
*
* @param config the parallel execution configuration
* @return a new ParallelContext for registering and executing branches
*/
ParallelContext parallel(String name, ParallelConfig config);
/**
* Executes a submitter function and waits for an external callback, blocking until the callback completes.
*
* <p>Combines a step (to run the submitter) and a callback (to receive the external result) in a child context. The
* submitter receives a callback ID that external systems use to report completion.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the result class for deserialization
* @param func the submitter function, receiving the callback ID and a {@link StepContext}
* @return the callback result
*/
<T> T waitForCallback(String name, Class<T> resultType, BiConsumer<String, StepContext> func);
/** Executes a submitter and waits for an external callback using a {@link TypeToken}, blocking until complete. */
<T> T waitForCallback(String name, TypeToken<T> resultType, BiConsumer<String, StepContext> func);
/** Executes a submitter and waits for an external callback with custom configuration, blocking until complete. */
<T> T waitForCallback(
String name,
Class<T> resultType,
BiConsumer<String, StepContext> func,
WaitForCallbackConfig waitForCallbackConfig);
/** Executes a submitter and waits for an external callback using a {@link TypeToken} and custom configuration. */
<T> T waitForCallback(
String name,
TypeToken<T> resultType,
BiConsumer<String, StepContext> func,
WaitForCallbackConfig waitForCallbackConfig);
/** Asynchronously executes a submitter and waits for an external callback. */
<T> DurableFuture<T> waitForCallbackAsync(String name, Class<T> resultType, BiConsumer<String, StepContext> func);
/** Asynchronously executes a submitter and waits for an external callback using a {@link TypeToken}. */
<T> DurableFuture<T> waitForCallbackAsync(
String name, TypeToken<T> resultType, BiConsumer<String, StepContext> func);
/** Asynchronously executes a submitter and waits for an external callback with custom configuration. */
<T> DurableFuture<T> waitForCallbackAsync(
String name,
Class<T> resultType,
BiConsumer<String, StepContext> func,
WaitForCallbackConfig waitForCallbackConfig);
/**
* Asynchronously executes a submitter and waits for an external callback using a {@link TypeToken} and custom
* configuration.
*
* <p>This is the core waitForCallbackAsync implementation. All other waitForCallback/waitForCallbackAsync overloads
* delegate here. Internally creates a child context containing a callback operation and a step that runs the
* submitter function.
*
* @param <T> the result type
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic result types
* @param func the submitter function, receiving the callback ID and a {@link StepContext}
* @param waitForCallbackConfig the configuration for both the callback and submitter step
* @return a future representing the callback result
*/
<T> DurableFuture<T> waitForCallbackAsync(
String name,
TypeToken<T> resultType,
BiConsumer<String, StepContext> func,
WaitForCallbackConfig waitForCallbackConfig);
/**
* Polls a condition function until it signals done, blocking until complete.
*
* @param <T> the type of state being polled
* @param name the unique operation name within this context
* @param resultType the result class for deserialization
* @param checkFunc the function that evaluates the condition and returns a {@link WaitForConditionResult}
* @param initialState the initial state passed to the first check invocation
* @return the final state value when the condition is met
*/
<T> T waitForCondition(
String name,
Class<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState);
/** Polls a condition function until it signals done, using a custom configuration, blocking until complete. */
<T> T waitForCondition(
String name,
Class<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState,
WaitForConditionConfig<T> config);
/** Polls a condition function until it signals done, using a {@link TypeToken}, blocking until complete. */
<T> T waitForCondition(
String name,
TypeToken<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState);
/**
* Polls a condition function until it signals done, using a {@link TypeToken} and custom configuration, blocking
* until complete.
*/
<T> T waitForCondition(
String name,
TypeToken<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState,
WaitForConditionConfig<T> config);
/** Asynchronously polls a condition function until it signals done. */
<T> DurableFuture<T> waitForConditionAsync(
String name,
Class<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState);
/** Asynchronously polls a condition function until it signals done, using custom configuration. */
<T> DurableFuture<T> waitForConditionAsync(
String name,
Class<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState,
WaitForConditionConfig<T> config);
/** Asynchronously polls a condition function until it signals done, using a {@link TypeToken}. */
<T> DurableFuture<T> waitForConditionAsync(
String name,
TypeToken<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState);
/**
* Asynchronously polls a condition function until it signals done, using a {@link TypeToken} and custom
* configuration.
*
* <p>This is the core waitForConditionAsync implementation. All other waitForCondition/waitForConditionAsync
* overloads delegate here.
*
* @param <T> the type of state being polled
* @param name the unique operation name within this context
* @param resultType the type token for deserialization of generic types
* @param checkFunc the function that evaluates the condition and returns a {@link WaitForConditionResult}
* @param initialState the initial state passed to the first check invocation
* @param config the waitForCondition configuration (wait strategy, custom SerDes)
* @return a future representing the final state value
*/
<T> DurableFuture<T> waitForConditionAsync(
String name,
TypeToken<T> resultType,
BiFunction<T, StepContext, WaitForConditionResult<T>> checkFunc,
T initialState,
WaitForConditionConfig<T> config);
}