Repository navigation
Expand file tree
/
Copy pathPluginParallelBranchHooks.java
More file actions
78 lines (70 loc) · 3.49 KB
/
Copy pathPluginParallelBranchHooks.java
File metadata and controls
78 lines (70 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package plugin;
import java.util.ArrayList;
import java.util.List;
import software.amazon.lambda.durable.DurableConfig;
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;
import software.amazon.lambda.durable.plugin.DurableExecutionPlugin;
import software.amazon.lambda.durable.plugin.InvocationInfo;
import software.amazon.lambda.durable.plugin.UserFunctionEndInfo;
import software.amazon.lambda.durable.plugin.UserFunctionStartInfo;
/**
* 10-12: Plugin user-function hooks fire per parallel branch with parent linkage.
*
* <p>A parallel operation named "parallel" with two branches (max-concurrency 1, so they run sequentially in index
* order); each branch returns a constant directly. The plugin, filtering to parallel-branch operations, logs fn-start
* and fn-end (with outcome) from the real user-function hooks, carrying the branch operation id and the parallel parent
* id. These hooks run on the branch's own thread, so start-before-end order per branch is deterministic.
*/
public class PluginParallelBranchHooks extends DurableHandler<Object, List<String>> {
@Override
protected DurableConfig createConfiguration() {
return DurableConfig.builder().withPlugins(new BranchHooksPlugin()).build();
}
@Override
public List<String> handleRequest(Object input, DurableContext context) {
var config = ParallelConfig.builder().maxConcurrency(1).build();
var futures = new ArrayList<DurableFuture<String>>();
ParallelDurableFuture parallel = context.parallel("parallel", config);
try (parallel) {
futures.add(parallel.branch("branch-0", String.class, branch -> "task-1"));
futures.add(parallel.branch("branch-1", String.class, branch -> "task-2"));
}
return futures.stream().map(DurableFuture::get).toList();
}
private static final class BranchHooksPlugin implements DurableExecutionPlugin {
private volatile String executionArn;
@Override
public void onInvocationStart(InvocationInfo info) {
this.executionArn = info.durableExecutionArn();
}
@Override
public void onUserFunctionStart(UserFunctionStartInfo info) {
if (!PluginSupport.isBranch(info.subType())) {
return;
}
System.out.println(String.format(
"{\"plugin\": \"CONFPLUGIN\", \"hook\": \"fn-start\", \"op\": \"%s\", \"parent\": \"%s\"%s}",
info.id(), PluginSupport.parentOrNone(info.parentId()), PluginSupport.arnField(executionArn)));
}
@Override
public void onUserFunctionEnd(UserFunctionEndInfo info) {
if (!PluginSupport.isBranch(info.subType())) {
return;
}
String outcome = info.outcome().name();
System.out.println(String.format(
"{\"plugin\": \"CONFPLUGIN\", \"hook\": \"fn-end\", \"op\": \"%s\", \"parent\": \"%s\", "
+ "\"outcome\": \"%s\"%s}",
info.id(),
PluginSupport.parentOrNone(info.parentId()),
outcome,
PluginSupport.arnField(executionArn)));
}
}
}