Skip to content

Commit 993bc55

Browse files
authored
[Refactor]: separate context impl and interface (#223)
* separate context impl and interface * remove ExecutionContext
1 parent a3eca09 commit 993bc55

36 files changed

Lines changed: 1153 additions & 886 deletions

docs/spec/waitForCondition.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface WaitForConditionWaitStrategy<T> {
7676
```
7777

7878
- `state`: the current state returned by the check function
79-
- `attempt`: 1-based attempt number (first check is attempt 1)
79+
- `attempt`: 0-based attempt number (first check is attempt 0)
8080
- Returns a `WaitForConditionDecision` indicating whether to continue or stop
8181

8282
### WaitForConditionDecision

sdk-testing/src/main/java/software/amazon/lambda/durable/testing/TestOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public ErrorObject getError() {
8585
return details != null ? details.error() : null;
8686
}
8787

88-
/** Returns the current retry attempt number (1-based), defaulting to 1 if not available. */
88+
/** Returns the current retry attempt number (0-based), defaulting to 0 if not available. */
8989
public int getAttempt() {
9090
var details = operation.stepDetails();
91-
return details != null && details.attempt() != null ? details.attempt() : 1;
91+
return details != null && details.attempt() != null ? details.attempt() : 0;
9292
}
9393
}

sdk/src/main/java/software/amazon/lambda/durable/BaseContext.java

Lines changed: 17 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,134 +3,49 @@
33
package software.amazon.lambda.durable;
44

55
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;
106
import software.amazon.lambda.durable.logging.DurableLogger;
117

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 {
549
/**
5510
* Gets a logger with additional information of the current execution context.
5611
*
5712
* @return a DurableLogger instance
5813
*/
59-
public abstract DurableLogger getLogger();
14+
DurableLogger getLogger();
6015

6116
/**
6217
* Returns the AWS Lambda runtime context.
6318
*
6419
* @return the Lambda context
6520
*/
66-
public Context getLambdaContext() {
67-
return lambdaContext;
68-
}
21+
Context getLambdaContext();
6922

7023
/**
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
7625
*
77-
* @return the execution context
26+
* @return the execution arn
7827
*/
79-
public ExecutionContext getExecutionContext() {
80-
return executionContext;
81-
}
28+
String getExecutionArn();
8229

8330
/**
8431
* Returns the configuration for durable execution behavior.
8532
*
8633
* @return the durable configuration
8734
*/
88-
public DurableConfig getDurableConfig() {
89-
return durableConfig;
90-
}
35+
DurableConfig getDurableConfig();
9136

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();
10242

103-
public ExecutionManager getExecutionManager() {
104-
return executionManager;
105-
}
43+
/** Gets the context name for this context. Null for root context. */
44+
String getContextName();
10645

10746
/** 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();
11848

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();
13651
}

0 commit comments

Comments
 (0)