Skip to content

Commit 09e65a5

Browse files
authored
feat: add executionContext object on context (#35)
1 parent 18ae802 commit 09e65a5

4 files changed

Lines changed: 99 additions & 1 deletion

File tree

sdk/src/main/java/com/amazonaws/lambda/durable/DurableContext.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class DurableContext {
2828
private final Context lambdaContext;
2929
private final AtomicInteger operationCounter;
3030
private final DurableLogger logger;
31+
private final ExecutionContext executionContext;
3132

3233
DurableContext(
3334
ExecutionManager executionManager,
@@ -39,6 +40,7 @@ public class DurableContext {
3940
this.serDes = serDes;
4041
this.lambdaContext = lambdaContext;
4142
this.operationCounter = new AtomicInteger(0);
43+
this.executionContext = new ExecutionContext(executionManager.getDurableExecutionArn());
4244

4345
var requestId = lambdaContext != null ? lambdaContext.getAwsRequestId() : null;
4446
this.logger = new DurableLogger(
@@ -221,6 +223,19 @@ public DurableLogger getLogger() {
221223
return logger;
222224
}
223225

226+
/**
227+
* Returns metadata about the current durable execution.
228+
*
229+
* <p>The execution context provides information that remains constant throughout the execution lifecycle, such as
230+
* the durable execution ARN. This is useful for tracking execution progress, correlating logs, and referencing
231+
* this execution in external systems.
232+
*
233+
* @return the execution context
234+
*/
235+
public ExecutionContext getExecutionContext() {
236+
return executionContext;
237+
}
238+
224239
// ========== createCallback methods ==========
225240

226241
public <T> DurableCallbackFuture<T> createCallback(String name, Class<T> resultType, CallbackConfig config) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package com.amazonaws.lambda.durable;
4+
5+
/**
6+
* Provides metadata about the current durable execution.
7+
*
8+
* <p>This context contains information about the execution environment that remains constant throughout the execution
9+
* lifecycle. Access it via {@link DurableContext#getExecutionContext()}.
10+
*/
11+
public class ExecutionContext {
12+
private final String durableExecutionArn;
13+
14+
ExecutionContext(String durableExecutionArn) {
15+
this.durableExecutionArn = durableExecutionArn;
16+
}
17+
18+
/**
19+
* Returns the ARN of the current durable execution.
20+
*
21+
* <p>The durable execution ARN uniquely identifies this execution instance and remains constant across all
22+
* invocations and replays. Use this ARN to:
23+
*
24+
* <ul>
25+
* <li>Track execution progress in external systems
26+
* <li>Correlate logs and metrics across invocations
27+
* <li>Reference this execution when calling Lambda APIs
28+
* </ul>
29+
*
30+
* <p>Example ARN format: {@code
31+
* arn:aws:lambda:us-east-1:123456789012:function:my-function:$LATEST/durable-execution/349beff4-a89d-4bc8-a56f-af7a8af67a5f/20dae574-53da-37a1-bfd5-b0e2e6ec715d}
32+
*
33+
* @return the durable execution ARN
34+
*/
35+
public String getDurableExecutionArn() {
36+
return durableExecutionArn;
37+
}
38+
}

sdk/src/test/java/com/amazonaws/lambda/durable/DurableContextTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ private DurableContext createTestContext(List<Operation> initialOperations) {
3232
var executor = Executors.newCachedThreadPool();
3333
var initialExecutionState = new InitialExecutionState(initialOperations, null);
3434
var executionManager = new ExecutionManager(
35-
"arn:aws:lambda:us-east-1:123456789012:function:test",
35+
"arn:aws:lambda:us-east-1:123456789012:function:test:$LATEST/durable-execution/"
36+
+ "349beff4-a89d-4bc8-a56f-af7a8af67a5f/20dae574-53da-37a1-bfd5-b0e2e6ec715d",
3637
"test-token",
3738
initialExecutionState,
3839
client,
@@ -49,6 +50,20 @@ void testContextCreation() {
4950
assertNull(context.getLambdaContext());
5051
}
5152

53+
@Test
54+
void testGetExecutionContext() {
55+
var context = createTestContext();
56+
57+
var executionContext = context.getExecutionContext();
58+
59+
assertNotNull(executionContext);
60+
assertNotNull(executionContext.getDurableExecutionArn());
61+
assertEquals(
62+
"arn:aws:lambda:us-east-1:123456789012:function:test:$LATEST/durable-execution/"
63+
+ "349beff4-a89d-4bc8-a56f-af7a8af67a5f/20dae574-53da-37a1-bfd5-b0e2e6ec715d",
64+
executionContext.getDurableExecutionArn());
65+
}
66+
5267
@Test
5368
void testStepExecution() {
5469
var context = createTestContext();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package com.amazonaws.lambda.durable;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
class ExecutionContextTest {
11+
12+
@Test
13+
void constructorSetsArn() {
14+
var arn = "arn:aws:lambda:us-east-1:123456789012:function:my-function:$LATEST/durable-execution/"
15+
+ "349beff4-a89d-4bc8-a56f-af7a8af67a5f/20dae574-53da-37a1-bfd5-b0e2e6ec715d";
16+
var context = new ExecutionContext(arn);
17+
18+
assertEquals(arn, context.getDurableExecutionArn());
19+
}
20+
21+
@Test
22+
void getDurableExecutionArnReturnsCorrectValue() {
23+
var arn = "arn:aws:lambda:eu-west-1:987654321098:function:test-fn:$LATEST/durable-execution/"
24+
+ "a1b2c3d4-e5f6-7890-abcd-ef1234567890/b2c3d4e5-f6a7-8901-bcde-f12345678901";
25+
var context = new ExecutionContext(arn);
26+
27+
assertNotNull(context.getDurableExecutionArn());
28+
assertEquals(arn, context.getDurableExecutionArn());
29+
}
30+
}

0 commit comments

Comments
 (0)