generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
Add concurrent waitForCondition example, and add examples to cloud tests #268
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
57 changes: 57 additions & 0 deletions
57
.../java/software/amazon/lambda/durable/examples/wait/ConcurrentWaitForConditionExample.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,57 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples.wait; | ||
|
|
||
| import java.util.stream.IntStream; | ||
| import software.amazon.lambda.durable.DurableContext; | ||
| import software.amazon.lambda.durable.DurableHandler; | ||
| import software.amazon.lambda.durable.config.MapConfig; | ||
| import software.amazon.lambda.durable.config.WaitForConditionConfig; | ||
| import software.amazon.lambda.durable.model.WaitForConditionResult; | ||
|
|
||
| /** | ||
| * Example demonstrating concurrent waitForCondition operations using map. | ||
| * | ||
| * <p>Runs many (totalOperations) waitForCondition operations concurrently (maxConcurrency). Each operation: | ||
| * | ||
| * <ol> | ||
| * <li>Uses attempt count as state (replay-safe). | ||
| * <li>Fails and retries until the attempt count reaches the given threshold, and then succeeds | ||
| * </ol> | ||
| */ | ||
| public class ConcurrentWaitForConditionExample extends DurableHandler<ConcurrentWaitForConditionExample.Input, String> { | ||
|
|
||
| public record Input(int threshold, int totalOperations, int maxConcurrency) {} | ||
|
|
||
| @Override | ||
| public String handleRequest(Input input, DurableContext context) { | ||
| var items = IntStream.range(0, input.totalOperations()).boxed().toList(); | ||
|
|
||
| var config = MapConfig.builder().maxConcurrency(input.maxConcurrency()).build(); | ||
|
|
||
| var result = context.map( | ||
| "concurrent-wait-for-conditions", | ||
| items, | ||
| String.class, | ||
| (item, index, ctx) -> { | ||
| var conditionConfig = WaitForConditionConfig.<Integer>builder() | ||
| .initialState(1) | ||
| .build(); | ||
| // Poll until the counter reaches the input threshold | ||
| var count = ctx.waitForCondition( | ||
| "condition-" + index, | ||
| Integer.class, | ||
| (callCount, stepCtx) -> { | ||
| if (callCount >= input.threshold()) { | ||
| return WaitForConditionResult.stopPolling(callCount); | ||
| } | ||
| return WaitForConditionResult.continuePolling(callCount + 1); | ||
| }, | ||
| conditionConfig); | ||
| return String.valueOf(count); | ||
| }, | ||
| config); | ||
|
|
||
| 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
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
29 changes: 29 additions & 0 deletions
29
...a/software/amazon/lambda/durable/examples/wait/ConcurrentWaitForConditionExampleTest.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,29 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| package software.amazon.lambda.durable.examples.wait; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.lambda.durable.model.ExecutionStatus; | ||
| import software.amazon.lambda.durable.testing.LocalDurableTestRunner; | ||
|
|
||
| class ConcurrentWaitForConditionExampleTest { | ||
|
|
||
| @Test | ||
| void testConcurrentWaitForConditionExample() { | ||
| var handler = new ConcurrentWaitForConditionExample(); | ||
| var runner = LocalDurableTestRunner.create(ConcurrentWaitForConditionExample.Input.class, handler); | ||
|
|
||
| var result = runner.runUntilComplete(new ConcurrentWaitForConditionExample.Input(3, 100, 50)); | ||
|
|
||
| assertEquals(ExecutionStatus.SUCCEEDED, result.getStatus()); | ||
|
|
||
| var allOperationsOutput = result.getResult(String.class); | ||
| var operationResults = allOperationsOutput.split(" \\| "); | ||
| assertEquals(100, operationResults.length); | ||
| for (var operationResult : operationResults) { | ||
| assertEquals("3", operationResult); | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -518,6 +518,56 @@ Resources: | |
| DockerContext: ../ | ||
| DockerTag: durable-examples | ||
|
|
||
| WaitForConditionExampleFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| PackageType: Image | ||
| FunctionName: !Join | ||
| - '' | ||
| - - 'wait-for-condition-example' | ||
| - !Ref FunctionNameSuffix | ||
| ImageConfig: | ||
| Command: ["software.amazon.lambda.durable.examples.wait.WaitForConditionExample::handleRequest"] | ||
| DurableConfig: | ||
| ExecutionTimeout: 300 | ||
| RetentionPeriodInDays: 7 | ||
| Policies: | ||
| - Statement: | ||
| - Effect: Allow | ||
| Action: | ||
| - lambda:CheckpointDurableExecutions | ||
| - lambda:GetDurableExecutionState | ||
| Resource: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:wait-for-condition-example${FunctionNameSuffix}" | ||
| Metadata: | ||
| Dockerfile: !Ref DockerFile | ||
| DockerContext: ../ | ||
| DockerTag: durable-examples | ||
|
|
||
| ConcurrentWaitForConditionExampleFunction: | ||
| Type: AWS::Serverless::Function | ||
| Properties: | ||
| PackageType: Image | ||
| FunctionName: !Join | ||
| - '' | ||
| - - 'concurrent-wait-for-condition-example' | ||
| - !Ref FunctionNameSuffix | ||
| ImageConfig: | ||
| Command: ["software.amazon.lambda.durable.examples.wait.ConcurrentWaitForConditionExample::handleRequest"] | ||
| DurableConfig: | ||
| ExecutionTimeout: 300 | ||
| RetentionPeriodInDays: 7 | ||
| Policies: | ||
| - Statement: | ||
| - Effect: Allow | ||
| Action: | ||
| - lambda:CheckpointDurableExecutions | ||
| - lambda:GetDurableExecutionState | ||
| Resource: !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:concurrent-wait-for-condition-example${FunctionNameSuffix}" | ||
| Metadata: | ||
| Dockerfile: !Ref DockerFile | ||
| DockerContext: ../ | ||
| DockerTag: durable-examples | ||
|
|
||
| Outputs: | ||
| NoopExampleFunction: | ||
| Description: Noop Example Function ARN | ||
|
|
@@ -679,3 +729,19 @@ Outputs: | |
| Description: Complex Map Example Function Name | ||
| Value: !Ref ComplexMapExampleFunction | ||
|
|
||
| WaitForConditionExampleFunction: | ||
| Description: Wait For Condition Example Function ARN | ||
| Value: !GetAtt WaitForConditionExampleFunction.Arn | ||
|
|
||
| WaitForConditionExampleFunctionName: | ||
| Description: Wait For Condition Example Function Name | ||
| Value: !Ref WaitForConditionExampleFunction | ||
|
|
||
| ConcurrentWaitForConditionExampleFunction: | ||
| Description: Concurrent Wait For Condition Example Function ARN | ||
| Value: !GetAtt ConcurrentWaitForConditionExampleFunction.Arn | ||
|
|
||
| ConcurrentWaitForConditionExampleFunctionName: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt if all these outputs are useful |
||
| Description: Concurrent Wait For Condition Example Function Name | ||
| Value: !Ref ConcurrentWaitForConditionExampleFunction | ||
|
|
||
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.
can we assert the duration of waitForCondition operations?
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 local runner, never mind. We can assert the duration only in our cloud tests.
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.
Will make an update to this PR
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.
Added duration check for each operation in the cloud test