Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package static_typing;

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.ParallelDurableFuture;
import software.amazon.lambda.durable.config.ParallelConfig;

/**
* 12-2: A parallel branch can complete before later branches are registered.
*
* <p>The first branch is awaited while the parallel operation is still open. The second branch is registered only after
* that result is available, proving that registration starts branches before the operation is sealed.
*/
public class ParallelEarlyStart extends DurableHandler<Object, List<String>> {

@Override
public List<String> handleRequest(Object input, DurableContext context) {
var config = ParallelConfig.builder().maxConcurrency(1).build();
ParallelDurableFuture parallel = context.parallel("early-start", config);
DurableFuture<String> second;
String firstResult;

try (parallel) {
var first = parallel.branch("first", String.class, branch -> "ready");
firstResult = first.get();
second = parallel.branch("second", String.class, branch -> firstResult + "-second");
}

return List.of(firstResult, second.get());
}
}
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 static_typing;

import java.util.List;
import java.util.Map;
import software.amazon.lambda.durable.DurableContext;
import software.amazon.lambda.durable.DurableFuture;
import software.amazon.lambda.durable.DurableHandler;
import software.amazon.lambda.durable.ParallelDurableFuture;
import software.amazon.lambda.durable.TypeToken;
import software.amazon.lambda.durable.config.ParallelConfig;

/**
* 12-1: Parallel exposes independently typed handles for heterogeneous branch results.
*
* <p>Each branch declares its own concrete result type, including a parameterized map, and the values are retrieved
* from those typed handles after the parallel operation is sealed.
*/
public class ParallelTypedBranches extends DurableHandler<Object, List<Object>> {

@Override
public List<Object> handleRequest(Object input, DurableContext context) {
var config = ParallelConfig.builder().maxConcurrency(1).build();
ParallelDurableFuture parallel = context.parallel("typed-branches", config);
DurableFuture<String> inventory;
DurableFuture<Integer> payment;
DurableFuture<Map<String, String>> quote;

try (parallel) {
inventory = parallel.branch("inventory", String.class, branch -> "reserved");
payment = parallel.branch("payment", Integer.class, branch -> 200);
quote = parallel.branch(
"quote", new TypeToken<Map<String, String>>() {}, branch -> Map.of("currency", "USD"));
}

return List.of(inventory.get(), payment.get(), quote.get());
}
}
1 change: 0 additions & 1 deletion conformance-tests/template_parallel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,3 @@ Resources:
RetentionPeriodInDays: 7
ExecutionTimeout: 300


81 changes: 81 additions & 0 deletions conformance-tests/template_static_typing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Durable Execution Conformance Test Examples - Java (Static Typing)

Parameters:
Architecture:
Type: String
Default: arm64
Description: Lambda Function Architecture
AllowedValues:
- x86_64
- arm64
JavaVersion:
Type: String
Default: 'java21'
Description: Java runtime version

Globals:
Function:
Timeout: 60
MemorySize: 512
Runtime:
Ref: JavaVersion
Architectures:
- Ref: Architecture

Resources:
DurableFunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Policies:
- PolicyName: DurableExecutionPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- lambda:CheckpointDurableExecution
- lambda:GetDurableExecutionState
Resource: '*'

ParallelTypedBranches:
Type: AWS::Serverless::Function
TestingMetadata:
TestDescription: ["12-1"]
Properties:
CodeUri: .
Handler: static_typing.ParallelTypedBranches
Description: Parallel with independently typed heterogeneous branch handles
Role:
Fn::GetAtt:
- DurableFunctionRole
- Arn
DurableConfig:
RetentionPeriodInDays: 7
ExecutionTimeout: 300

ParallelEarlyStart:
Type: AWS::Serverless::Function
TestingMetadata:
TestDescription: ["12-2"]
Properties:
CodeUri: .
Handler: static_typing.ParallelEarlyStart
Description: Parallel branch completes before later branches are registered
Role:
Fn::GetAtt:
- DurableFunctionRole
- Arn
DurableConfig:
RetentionPeriodInDays: 7
ExecutionTimeout: 300
Loading