generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: [Parallel] Update context setting logic #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...c/main/java/software/amazon/lambda/durable/examples/parallel/ParallelWithWaitExample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples.parallel; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import software.amazon.lambda.durable.DurableContext; | ||
| import software.amazon.lambda.durable.DurableFuture; | ||
| import software.amazon.lambda.durable.DurableHandler; | ||
| import software.amazon.lambda.durable.ParallelConfig; | ||
|
|
||
| /** | ||
| * Example demonstrating parallel branches where some branches include wait operations. | ||
| * | ||
| * <p>This models a notification fan-out pattern where different channels have different delivery delays: | ||
| * | ||
| * <ul> | ||
| * <li>Email — sent immediately | ||
| * <li>SMS — waits for a rate-limit window before sending | ||
| * <li>Push notification — waits for a quiet-hours window before sending | ||
| * </ul> | ||
| * | ||
| * <p>All three branches run concurrently. Branches with waits suspend without consuming compute resources and resume | ||
| * automatically once the wait elapses. The parallel operation completes once all branches finish. | ||
| */ | ||
| public class ParallelWithWaitExample | ||
| extends DurableHandler<ParallelWithWaitExample.Input, ParallelWithWaitExample.Output> { | ||
|
|
||
| public record Input(String userId, String message) {} | ||
|
|
||
| public record Output(List<String> deliveries) {} | ||
|
|
||
| @Override | ||
| public Output handleRequest(Input input, DurableContext context) { | ||
| var logger = context.getLogger(); | ||
| logger.info("Sending notifications to user {}", input.userId()); | ||
|
|
||
| var config = ParallelConfig.builder().build(); | ||
| var futures = new ArrayList<DurableFuture<String>>(3); | ||
|
|
||
| try (var parallel = context.parallel("notify", config)) { | ||
|
|
||
| // Branch 1: email — no wait, deliver immediately | ||
| futures.add(parallel.branch("email", String.class, ctx -> { | ||
| ctx.wait("email-rate-limit-delay", Duration.ofSeconds(10)); | ||
| return ctx.step("send-email", String.class, stepCtx -> "email:" + input.message()); | ||
| })); | ||
|
|
||
| // Branch 2: SMS — wait for rate-limit window, then send | ||
| futures.add(parallel.branch("sms", String.class, ctx -> { | ||
| ctx.wait("sms-rate-limit-delay", Duration.ofSeconds(10)); | ||
| return ctx.step("send-sms", String.class, stepCtx -> "sms:" + input.message()); | ||
| })); | ||
|
|
||
| // Branch 3: push notification — wait for quiet-hours window, then send | ||
| futures.add(parallel.branch("push", String.class, ctx -> { | ||
| ctx.wait("push-quiet-delay", Duration.ofSeconds(10)); | ||
| return ctx.step("send-push", String.class, stepCtx -> "push:" + input.message()); | ||
| })); | ||
| } | ||
|
|
||
| var deliveries = futures.stream().map(DurableFuture::get).toList(); | ||
| logger.info("All {} notifications delivered", deliveries.size()); | ||
| return new Output(deliveries); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...st/java/software/amazon/lambda/durable/examples/parallel/ParallelWithWaitExampleTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples.parallel; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.lambda.durable.model.ExecutionStatus; | ||
| import software.amazon.lambda.durable.testing.LocalDurableTestRunner; | ||
|
|
||
| class ParallelWithWaitExampleTest { | ||
| @Test | ||
| void completesAfterManuallyAdvancingWaits() { | ||
| var handler = new ParallelWithWaitExample(); | ||
| var runner = LocalDurableTestRunner.create(ParallelWithWaitExample.Input.class, handler); | ||
|
|
||
| var input = new ParallelWithWaitExample.Input("user-456", "world"); | ||
|
|
||
| // First run suspends on wait branches | ||
| var first = runner.run(input); | ||
| assertEquals(ExecutionStatus.PENDING, first.getStatus()); | ||
|
|
||
| // Advance waits and re-run to completion | ||
| runner.advanceTime(); | ||
| var result = runner.runUntilComplete(input); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
|
|
||
| var output = result.getResult(ParallelWithWaitExample.Output.class); | ||
| assertEquals(List.of("email:world", "sms:world", "push:world"), output.deliveries()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
sdk/src/test/java/software/amazon/lambda/durable/context/BaseContextImplTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.context; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.awssdk.services.lambda.model.CheckpointUpdatedExecutionState; | ||
| import software.amazon.awssdk.services.lambda.model.Operation; | ||
| import software.amazon.awssdk.services.lambda.model.OperationStatus; | ||
| import software.amazon.awssdk.services.lambda.model.OperationType; | ||
| import software.amazon.lambda.durable.DurableConfig; | ||
| import software.amazon.lambda.durable.TestUtils; | ||
| import software.amazon.lambda.durable.execution.ExecutionManager; | ||
| import software.amazon.lambda.durable.execution.ThreadContext; | ||
| import software.amazon.lambda.durable.execution.ThreadType; | ||
| import software.amazon.lambda.durable.model.DurableExecutionInput; | ||
|
|
||
| class BaseContextImplTest { | ||
|
|
||
| private static final String INVOCATION_ID = "20dae574-53da-37a1-bfd5-b0e2e6ec715d"; | ||
| private static final String EXECUTION_NAME = "349beff4-a89d-4bc8-a56f-af7a8af67a5f"; | ||
| private static final Operation EXECUTION_OP = Operation.builder() | ||
| .id(INVOCATION_ID) | ||
| .type(OperationType.EXECUTION) | ||
| .status(OperationStatus.STARTED) | ||
| .build(); | ||
|
|
||
| @BeforeEach | ||
| void clearThreadContext() { | ||
| // currentThreadContext is a static ThreadLocal on ExecutionManager — clear it | ||
| // before each test to prevent bleed-through from other tests on the same thread. | ||
| createExecutionManager().setCurrentThreadContext(null); | ||
| } | ||
|
|
||
| private ExecutionManager createExecutionManager() { | ||
| var client = TestUtils.createMockClient(); | ||
| var initialState = CheckpointUpdatedExecutionState.builder() | ||
| .operations(new ArrayList<>(List.of(EXECUTION_OP))) | ||
| .build(); | ||
| return new ExecutionManager( | ||
| new DurableExecutionInput( | ||
| "arn:aws:lambda:us-east-1:123456789012:function:test:$LATEST/durable-execution/" | ||
| + EXECUTION_NAME + "/" + INVOCATION_ID, | ||
| "test-token", | ||
| initialState), | ||
| DurableConfig.builder().withDurableExecutionClient(client).build()); | ||
| } | ||
|
|
||
| @Test | ||
| void defaultConstructor_setsCurrentThreadContext() { | ||
| var executionManager = createExecutionManager(); | ||
| // Precondition: no thread context set yet | ||
| assertNull(executionManager.getCurrentThreadContext()); | ||
|
|
||
| // Creating a root context with the default constructor should set the thread context | ||
| DurableContextImpl.createRootContext( | ||
| executionManager, DurableConfig.builder().build(), null); | ||
|
|
||
| var threadContext = executionManager.getCurrentThreadContext(); | ||
| assertNotNull(threadContext); | ||
| assertEquals(ThreadType.CONTEXT, threadContext.threadType()); | ||
| assertNull(threadContext.threadId()); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorWithSetCurrentThreadContextTrue_setsCurrentThreadContext() { | ||
| var executionManager = createExecutionManager(); | ||
|
|
||
| // createRootContext sets thread context to root (threadId=null) | ||
| var rootContext = DurableContextImpl.createRootContext( | ||
| executionManager, DurableConfig.builder().build(), null); | ||
| assertEquals( | ||
| ThreadType.CONTEXT, executionManager.getCurrentThreadContext().threadType()); | ||
| assertNull(executionManager.getCurrentThreadContext().threadId()); | ||
|
|
||
| // createChildContext (setCurrentThreadContext=true) should overwrite with child's context | ||
| rootContext.createChildContext("child-id", "child-name"); | ||
|
|
||
| var threadContext = executionManager.getCurrentThreadContext(); | ||
| assertNotNull(threadContext); | ||
| assertEquals(ThreadType.CONTEXT, threadContext.threadType()); | ||
| assertEquals("child-id", threadContext.threadId()); | ||
| } | ||
|
|
||
| @Test | ||
| void constructorWithSetCurrentThreadContextFalse_doesNotOverwriteThreadContext() { | ||
| var executionManager = createExecutionManager(); | ||
|
|
||
| // Create root context first (it will set thread context to null/root) | ||
| var rootContext = DurableContextImpl.createRootContext( | ||
| executionManager, DurableConfig.builder().build(), null); | ||
|
|
||
| // Now set a sentinel — simulating a caller thread that already has context established | ||
| var sentinel = new ThreadContext("original-context", ThreadType.CONTEXT); | ||
| executionManager.setCurrentThreadContext(sentinel); | ||
|
|
||
| // createChildContextWithoutSettingThreadContext should NOT overwrite the sentinel | ||
| rootContext.createChildContextWithoutSettingThreadContext("child-id", "child-name"); | ||
|
|
||
| // Thread context should still be the sentinel, not the child's context | ||
| var threadContext = executionManager.getCurrentThreadContext(); | ||
| assertNotNull(threadContext); | ||
| assertEquals("original-context", threadContext.threadId()); | ||
| } | ||
|
|
||
| @Test | ||
| void createChildContextWithoutSettingThreadContext_returnsValidChildContext() { | ||
| var executionManager = createExecutionManager(); | ||
| executionManager.setCurrentThreadContext(new ThreadContext(null, ThreadType.CONTEXT)); | ||
| var rootContext = DurableContextImpl.createRootContext( | ||
| executionManager, DurableConfig.builder().build(), null); | ||
|
|
||
| var childContext = rootContext.createChildContextWithoutSettingThreadContext("child-id", "child-name"); | ||
|
|
||
| assertNotNull(childContext); | ||
| assertEquals("child-id", childContext.getContextId()); | ||
| assertEquals("child-name", childContext.getContextName()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a kind of temporary hack for now