|
3 | 3 | package software.amazon.lambda.durable; |
4 | 4 |
|
5 | 5 | import com.amazonaws.services.lambda.runtime.Context; |
6 | | -import software.amazon.lambda.durable.execution.ExecutionManager; |
7 | | -import software.amazon.lambda.durable.execution.SuspendExecutionException; |
8 | | -import software.amazon.lambda.durable.execution.ThreadContext; |
9 | | -import software.amazon.lambda.durable.execution.ThreadType; |
10 | 6 | import software.amazon.lambda.durable.logging.DurableLogger; |
11 | 7 |
|
12 | | -public abstract class BaseContext implements AutoCloseable { |
13 | | - private final ExecutionManager executionManager; |
14 | | - private final DurableConfig durableConfig; |
15 | | - private final Context lambdaContext; |
16 | | - private final ExecutionContext executionContext; |
17 | | - private final String contextId; |
18 | | - private final String contextName; |
19 | | - private final ThreadType threadType; |
20 | | - |
21 | | - private boolean isReplaying; |
22 | | - |
23 | | - /** |
24 | | - * Creates a new BaseContext instance. |
25 | | - * |
26 | | - * @param executionManager the execution manager for thread coordination and state management |
27 | | - * @param durableConfig the durable execution configuration |
28 | | - * @param lambdaContext the AWS Lambda runtime context |
29 | | - * @param contextId the context ID, null for root context, set for child contexts |
30 | | - * @param contextName the human-readable name for this context |
31 | | - * @param threadType the type of thread this context runs on |
32 | | - */ |
33 | | - protected BaseContext( |
34 | | - ExecutionManager executionManager, |
35 | | - DurableConfig durableConfig, |
36 | | - Context lambdaContext, |
37 | | - String contextId, |
38 | | - String contextName, |
39 | | - ThreadType threadType) { |
40 | | - this.executionManager = executionManager; |
41 | | - this.durableConfig = durableConfig; |
42 | | - this.lambdaContext = lambdaContext; |
43 | | - this.contextId = contextId; |
44 | | - this.contextName = contextName; |
45 | | - this.executionContext = new ExecutionContext(executionManager.getDurableExecutionArn()); |
46 | | - this.isReplaying = executionManager.hasOperationsForContext(contextId); |
47 | | - this.threadType = threadType; |
48 | | - |
49 | | - // write the thread id and type to thread local |
50 | | - executionManager.setCurrentThreadContext(new ThreadContext(contextId, threadType)); |
51 | | - } |
52 | | - |
53 | | - // =============== accessors ================ |
| 8 | +public interface BaseContext extends AutoCloseable { |
54 | 9 | /** |
55 | 10 | * Gets a logger with additional information of the current execution context. |
56 | 11 | * |
57 | 12 | * @return a DurableLogger instance |
58 | 13 | */ |
59 | | - public abstract DurableLogger getLogger(); |
| 14 | + DurableLogger getLogger(); |
60 | 15 |
|
61 | 16 | /** |
62 | 17 | * Returns the AWS Lambda runtime context. |
63 | 18 | * |
64 | 19 | * @return the Lambda context |
65 | 20 | */ |
66 | | - public Context getLambdaContext() { |
67 | | - return lambdaContext; |
68 | | - } |
| 21 | + Context getLambdaContext(); |
69 | 22 |
|
70 | 23 | /** |
71 | | - * Returns metadata about the current durable execution. |
72 | | - * |
73 | | - * <p>The execution context provides information that remains constant throughout the execution lifecycle, such as |
74 | | - * the durable execution ARN. This is useful for tracking execution progress, correlating logs, and referencing this |
75 | | - * execution in external systems. |
| 24 | + * Returns the current durable execution arn |
76 | 25 | * |
77 | | - * @return the execution context |
| 26 | + * @return the execution arn |
78 | 27 | */ |
79 | | - public ExecutionContext getExecutionContext() { |
80 | | - return executionContext; |
81 | | - } |
| 28 | + String getExecutionArn(); |
82 | 29 |
|
83 | 30 | /** |
84 | 31 | * Returns the configuration for durable execution behavior. |
85 | 32 | * |
86 | 33 | * @return the durable configuration |
87 | 34 | */ |
88 | | - public DurableConfig getDurableConfig() { |
89 | | - return durableConfig; |
90 | | - } |
| 35 | + DurableConfig getDurableConfig(); |
91 | 36 |
|
92 | | - // ============= internal utilities =============== |
93 | | - |
94 | | - /** Gets the context ID for this context. Null for root context, set for child contexts. */ |
95 | | - public String getContextId() { |
96 | | - return contextId; |
97 | | - } |
98 | | - |
99 | | - public String getContextName() { |
100 | | - return contextName; |
101 | | - } |
| 37 | + /** |
| 38 | + * Gets the context ID for this context. Null for root context, operationId of the context operation for child |
| 39 | + * contexts. |
| 40 | + */ |
| 41 | + String getContextId(); |
102 | 42 |
|
103 | | - public ExecutionManager getExecutionManager() { |
104 | | - return executionManager; |
105 | | - } |
| 43 | + /** Gets the context name for this context. Null for root context. */ |
| 44 | + String getContextName(); |
106 | 45 |
|
107 | 46 | /** Returns whether this context is currently in replay mode. */ |
108 | | - boolean isReplaying() { |
109 | | - return isReplaying; |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * Transitions this context from replay to execution mode. Called when the first un-cached operation is encountered. |
114 | | - */ |
115 | | - void setExecutionMode() { |
116 | | - this.isReplaying = false; |
117 | | - } |
| 47 | + boolean isReplaying(); |
118 | 48 |
|
119 | | - public void close() { |
120 | | - // this is called in the user thread, after the context's user code has completed |
121 | | - if (getContextId() != null) { |
122 | | - // if this is a child context or a step context, we need to |
123 | | - // deregister the context's thread from the execution manager |
124 | | - try { |
125 | | - executionManager.deregisterActiveThread(getContextId()); |
126 | | - } catch (SuspendExecutionException e) { |
127 | | - // Expected when this is the last active thread. Must catch here because: |
128 | | - // 1/ This runs in a worker thread detached from handlerFuture |
129 | | - // 2/ Uncaught exception would prevent stepAsync().get() from resume |
130 | | - // Suspension/Termination is already signaled via |
131 | | - // suspendExecutionFuture/terminateExecutionFuture |
132 | | - // before the throw. |
133 | | - } |
134 | | - } |
135 | | - } |
| 49 | + /** Closes this context. */ |
| 50 | + void close(); |
136 | 51 | } |
0 commit comments