generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: [Parallel] send operation success when parallel failed #228
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...bda/durable/examples/ParallelExample.java → ...le/examples/parallel/ParallelExample.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
78 changes: 78 additions & 0 deletions
78
...ava/software/amazon/lambda/durable/examples/parallel/ParallelFailureToleranceExample.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,78 @@ | ||
| // 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.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; | ||
| import software.amazon.lambda.durable.StepConfig; | ||
| import software.amazon.lambda.durable.retry.RetryStrategies; | ||
|
|
||
| /** | ||
| * Example demonstrating parallel execution with failure tolerance. | ||
| * | ||
| * <p>When {@code toleratedFailureCount} is set, the parallel operation completes successfully even if some branches | ||
| * fail — as long as the number of failures does not exceed the threshold. Failed branches produce {@code null} results | ||
| * that callers can filter out. | ||
| * | ||
| * <p>Use this pattern when partial success is acceptable, for example: sending notifications to multiple channels where | ||
| * some channels may be unavailable. | ||
| */ | ||
| public class ParallelFailureToleranceExample | ||
| extends DurableHandler<ParallelFailureToleranceExample.Input, ParallelFailureToleranceExample.Output> { | ||
|
|
||
| public record Input(List<String> services, int toleratedFailures) {} | ||
|
|
||
| public record Output(List<String> succeeded, List<String> failed) {} | ||
|
|
||
| @Override | ||
| public Output handleRequest(Input input, DurableContext context) { | ||
| var logger = context.getLogger(); | ||
| logger.info("Starting parallel execution with toleratedFailureCount={}", input.toleratedFailures()); | ||
|
|
||
| var config = ParallelConfig.builder() | ||
| .toleratedFailureCount(input.toleratedFailures()) | ||
| .build(); | ||
|
|
||
| var futures = new ArrayList<DurableFuture<String>>(input.services().size()); | ||
|
|
||
| try (var parallel = context.parallel("call-services", config)) { | ||
| for (var service : input.services()) { | ||
| var future = parallel.branch("call-" + service, String.class, branchCtx -> { | ||
| return branchCtx.step( | ||
| "invoke-" + service, | ||
| String.class, | ||
| stepCtx -> { | ||
| if (service.startsWith("bad-")) { | ||
| throw new RuntimeException("Service unavailable: " + service); | ||
| } | ||
| return "ok:" + service; | ||
| }, | ||
| StepConfig.builder() | ||
| .retryStrategy(RetryStrategies.Presets.NO_RETRY) | ||
| .build()); | ||
| }); | ||
| futures.add(future); | ||
| } | ||
| } | ||
|
|
||
| var succeeded = new ArrayList<String>(); | ||
| var failed = new ArrayList<String>(); | ||
|
|
||
| for (int i = 0; i < futures.size(); i++) { | ||
| try { | ||
| var result = futures.get(i).get(); | ||
| succeeded.add(result); | ||
| } catch (Exception e) { | ||
| failed.add(input.services().get(i)); | ||
| logger.info("Branch failed for service {}: {}", input.services().get(i), e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| logger.info("Completed: {} succeeded, {} failed", succeeded.size(), failed.size()); | ||
| return new Output(succeeded, failed); | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
...durable/examples/ParallelExampleTest.java → ...xamples/parallel/ParallelExampleTest.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
63 changes: 63 additions & 0 deletions
63
...software/amazon/lambda/durable/examples/parallel/ParallelFailureToleranceExampleTest.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,63 @@ | ||
| // 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 ParallelFailureToleranceExampleTest { | ||
|
|
||
| @Test | ||
| void succeedsWhenFailuresAreWithinTolerance() { | ||
| var handler = new ParallelFailureToleranceExample(); | ||
| var runner = LocalDurableTestRunner.create(ParallelFailureToleranceExample.Input.class, handler); | ||
|
|
||
| // 2 good services, 1 bad — toleratedFailureCount=1 so the parallel op still succeeds | ||
| var input = new ParallelFailureToleranceExample.Input(List.of("svc-a", "bad-svc-b", "svc-c"), 1); | ||
| var result = runner.runUntilComplete(input); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
|
|
||
| var output = result.getResult(ParallelFailureToleranceExample.Output.class); | ||
| assertEquals(2, output.succeeded().size()); | ||
| assertEquals(1, output.failed().size()); | ||
| assertTrue(output.succeeded().contains("ok:svc-a")); | ||
| assertTrue(output.succeeded().contains("ok:svc-c")); | ||
| assertTrue(output.failed().contains("bad-svc-b")); | ||
| } | ||
|
|
||
| @Test | ||
| void succeedsWhenAllBranchesSucceed() { | ||
| var handler = new ParallelFailureToleranceExample(); | ||
| var runner = LocalDurableTestRunner.create(ParallelFailureToleranceExample.Input.class, handler); | ||
|
|
||
| var input = new ParallelFailureToleranceExample.Input(List.of("svc-a", "svc-b", "svc-c"), 2); | ||
| var result = runner.runUntilComplete(input); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
|
|
||
| var output = result.getResult(ParallelFailureToleranceExample.Output.class); | ||
| assertEquals(3, output.succeeded().size()); | ||
| assertTrue(output.failed().isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void failsWhenFailuresExceedTolerance() { | ||
| var handler = new ParallelFailureToleranceExample(); | ||
| var runner = LocalDurableTestRunner.create(ParallelFailureToleranceExample.Input.class, handler); | ||
|
|
||
| // 2 bad services, toleratedFailureCount=1 — second failure exceeds tolerance | ||
| var input = new ParallelFailureToleranceExample.Input(List.of("svc-a", "bad-svc-b", "bad-svc-c"), 1); | ||
| var result = runner.runUntilComplete(input); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
|
|
||
| var output = result.getResult(ParallelFailureToleranceExample.Output.class); | ||
| assertEquals(2, output.failed().size()); | ||
| assertEquals(1, output.succeeded().size()); | ||
| } | ||
| } |
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
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.
Let's add a wait operation here to make sure replay works
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.
#231