generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
feat(map): Implement Map Operation #202
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
39 changes: 39 additions & 0 deletions
39
examples/src/main/java/software/amazon/lambda/durable/examples/SimpleMapExample.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,39 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples; | ||
|
|
||
| import java.util.List; | ||
| import software.amazon.lambda.durable.DurableContext; | ||
| import software.amazon.lambda.durable.DurableHandler; | ||
|
|
||
| /** | ||
| * Example demonstrating the map operation with the Durable Execution SDK. | ||
| * | ||
| * <p>This handler processes a list of names concurrently using {@code map()}, where each item runs in its own child | ||
| * context with full checkpoint-and-replay support. | ||
| * | ||
| * <ol> | ||
| * <li>Create a list of names from the input | ||
| * <li>Map over each name concurrently, applying a greeting transformation via a durable step | ||
| * <li>Collect and join the results | ||
| * </ol> | ||
| */ | ||
| public class SimpleMapExample extends DurableHandler<GreetingRequest, String> { | ||
|
|
||
| @Override | ||
| public String handleRequest(GreetingRequest input, DurableContext context) { | ||
| var name = input.getName(); | ||
| context.getLogger().info("Starting map example for {}", name); | ||
|
|
||
| var names = List.of(name, name.toUpperCase(), name.toLowerCase()); | ||
|
|
||
| // Map over each name concurrently — each iteration runs in its own child context | ||
| var result = context.map("greet-all", names, String.class, (ctx, item, index) -> { | ||
| return ctx.step("greet-" + index, String.class, stepCtx -> "Hello, " + item + "!"); | ||
| }); | ||
|
|
||
| context.getLogger().info("Map completed: allSucceeded={}, size={}", result.allSucceeded(), result.size()); | ||
|
|
||
| return String.join(" | ", result.results()); | ||
| } | ||
| } |
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
48 changes: 48 additions & 0 deletions
48
examples/src/test/java/software/amazon/lambda/durable/examples/SimpleMapExampleTest.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,48 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.lambda.durable.model.ExecutionStatus; | ||
| import software.amazon.lambda.durable.testing.LocalDurableTestRunner; | ||
|
|
||
| class SimpleMapExampleTest { | ||
|
|
||
| @Test | ||
| void testSimpleMapExample() { | ||
| var handler = new SimpleMapExample(); | ||
| var runner = LocalDurableTestRunner.create(GreetingRequest.class, handler); | ||
|
|
||
| var result = runner.runUntilComplete(new GreetingRequest("Alice")); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
| assertEquals("Hello, Alice! | Hello, ALICE! | Hello, alice!", result.getResult(String.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithDefaultName() { | ||
| var handler = new SimpleMapExample(); | ||
| var runner = LocalDurableTestRunner.create(GreetingRequest.class, handler); | ||
|
|
||
| var result = runner.runUntilComplete(new GreetingRequest()); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
| assertEquals("Hello, World! | Hello, WORLD! | Hello, world!", result.getResult(String.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void testReplay() { | ||
| var handler = new SimpleMapExample(); | ||
| var runner = LocalDurableTestRunner.create(GreetingRequest.class, handler); | ||
|
|
||
| var input = new GreetingRequest("Bob"); | ||
| var result1 = runner.runUntilComplete(input); | ||
| assertEquals("Hello, Bob! | Hello, BOB! | Hello, bob!", result1.getResult(String.class)); | ||
|
|
||
| // Replay — should use cached results | ||
| var result2 = runner.runUntilComplete(input); | ||
| assertEquals(result1.getResult(String.class), result2.getResult(String.class)); | ||
| } | ||
| } |
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
52 changes: 52 additions & 0 deletions
52
sdk-integration-tests/src/test/java/software/amazon/lambda/durable/MapIntegrationTest.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,52 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable; | ||
|
|
||
| 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 MapIntegrationTest { | ||
|
|
||
| @Test | ||
| void testSimpleMap() { | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, context) -> { | ||
| var items = List.of("a", "b", "c"); | ||
| var result = context.map("process-items", items, String.class, (ctx, item, index) -> { | ||
| return item.toUpperCase(); | ||
| }); | ||
|
|
||
| assertTrue(result.allSucceeded()); | ||
| assertEquals(3, result.size()); | ||
| assertEquals("A", result.getResult(0)); | ||
| assertEquals("B", result.getResult(1)); | ||
| assertEquals("C", result.getResult(2)); | ||
|
|
||
| return String.join(",", result.results()); | ||
| }); | ||
|
|
||
| var result = runner.runUntilComplete("test"); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
| assertEquals("A,B,C", result.getResult(String.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void testMapWithStepsInsideBranches() { | ||
| var runner = LocalDurableTestRunner.create(String.class, (input, context) -> { | ||
| var items = List.of("hello", "world"); | ||
| var result = context.map("map-with-steps", items, String.class, (ctx, item, index) -> { | ||
| return ctx.step("process-" + index, String.class, stepCtx -> item.toUpperCase()); | ||
| }); | ||
|
|
||
| assertTrue(result.allSucceeded()); | ||
| return String.join(" ", result.results()); | ||
| }); | ||
|
|
||
| var result = runner.runUntilComplete("test"); | ||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
| assertEquals("HELLO WORLD", result.getResult(String.class)); | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
sdk/src/main/java/software/amazon/lambda/durable/CompletedDurableFuture.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,23 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable; | ||
|
|
||
| /** | ||
| * A {@link DurableFuture} that is already completed with a value. | ||
| * | ||
| * <p>Used for short-circuit cases (e.g., empty collection in map) where no checkpoint or async execution is needed. | ||
| * | ||
| * @param <T> the result type | ||
| */ | ||
| class CompletedDurableFuture<T> implements DurableFuture<T> { | ||
| private final T value; | ||
|
|
||
| CompletedDurableFuture(T value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| @Override | ||
| public T get() { | ||
| return value; | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
sdk/src/main/java/software/amazon/lambda/durable/CompletionConfig.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,51 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable; | ||
|
|
||
| /** | ||
| * Controls when a concurrent operation (map or parallel) completes. | ||
| * | ||
| * <p>Provides factory methods for common completion strategies and fine-grained control via {@code minSuccessful}, | ||
| * {@code toleratedFailureCount}, and {@code toleratedFailurePercentage}. | ||
| */ | ||
| public class CompletionConfig { | ||
| private final Integer minSuccessful; | ||
| private final Integer toleratedFailureCount; | ||
| private final Double toleratedFailurePercentage; | ||
|
|
||
| private CompletionConfig(Integer minSuccessful, Integer toleratedFailureCount, Double toleratedFailurePercentage) { | ||
| this.minSuccessful = minSuccessful; | ||
| this.toleratedFailureCount = toleratedFailureCount; | ||
| this.toleratedFailurePercentage = toleratedFailurePercentage; | ||
| } | ||
|
|
||
| /** All items must succeed. Zero failures tolerated. */ | ||
| public static CompletionConfig allSuccessful() { | ||
| return new CompletionConfig(null, 0, null); | ||
| } | ||
|
|
||
| /** All items run regardless of failures. Failures captured per-item. */ | ||
| public static CompletionConfig allCompleted() { | ||
| return new CompletionConfig(null, null, null); | ||
| } | ||
|
|
||
| /** Complete as soon as the first item succeeds. */ | ||
| public static CompletionConfig firstSuccessful() { | ||
| return new CompletionConfig(1, null, null); | ||
| } | ||
|
|
||
| /** @return minimum number of successful items required, or null if not set */ | ||
| public Integer minSuccessful() { | ||
| return minSuccessful; | ||
| } | ||
|
|
||
| /** @return maximum number of failures tolerated, or null if unlimited */ | ||
| public Integer toleratedFailureCount() { | ||
| return toleratedFailureCount; | ||
| } | ||
|
|
||
| /** @return maximum percentage of failures tolerated (0.0 to 1.0), or null if not set */ | ||
| public Double toleratedFailurePercentage() { | ||
| return toleratedFailurePercentage; | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.