fix(stepfunctions): fail a Parallel the moment any branch fails, whichever is listed first (#4713)

executeParallelState joined the branch futures in declaration order, so a
failure in a later branch was only seen once every earlier branch had
returned: a .sync Task listed before the failing branch kept the Parallel
running until its job ended on its own, and a Wait listed first until the
Wait ended. AWS fails the Parallel within about 0.1 s of the failing branch
either way (measured in #4646).

The branches now go through an ExecutorCompletionService, as the Map
iterations already do, and are polled as they complete under the same two
deadlines. The failed branch is the one whose future came off the queue, so
continueFrom and the aborted-state events of #4660 attach to the same chain.
The outputs are collected by branch index and still come back in declaration
order.

Closes #4646

Co-authored-by: mitsukado-shinagawa <mitsukado-shinagawa@users.noreply.github.com>
This commit is contained in:
mitsukado-shinagawaandmitsukado-shinagawa authored and GitHub committed 2026-09-30 07:00:42 -04:00
1 parent b0b191e0bc
commit 6ca104042b
4 files changed
+217 -11

No files matched your search

@@ -108,8 +108,10 @@ import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@@ -2623,6 +2625,11 @@ public class AslExecutor {
JsonNode branches = stateDef.path("Branches");
chain.publish("ParallelStateStarted", null);
var branchChains = new ArrayList<HistoryChain>();
// The branches are joined in the order they complete, not the order they are declared, so
// a failure in any branch ends the Parallel while the others are still running, as on AWS
// (measured: the Parallel fails within about 0.1 s of the failing branch, whichever branch
// is listed first). The outputs still land in declaration order.
CompletionService<JsonNode> completions = new ExecutorCompletionService<>(executor);
List<Future<JsonNode>> futures = new ArrayList<>();
for (JsonNode branch : branches) {
@@ -2644,7 +2651,7 @@ public class AslExecutor {
// A branch state that declares no QueryLanguage defaults to the state machine's, not to
// this Parallel's: the ASL specification calls the two independent, so what travels
// into the branch is topLevelQueryLanguage. https://states-language.net/spec.html
futures.add(executor.submit(() -> callUnderExecutionAccount(sm,
futures.add(completions.submit(() -> callUnderExecutionAccount(sm,
() -> executeBranch(startAt, branchStates, capturedInput, branchChain, sm,
topLevelQueryLanguage, branchContext, branchVariables))));
}
@@ -2657,20 +2664,22 @@ public class AslExecutor {
// own TimeoutSeconds, and the state machine's budget for the whole execution.
long joinDeadlineNanos = Math.min(stateDeadlineNanos, executionDeadlineNanos);
ArrayNode results = objectMapper.createArrayNode();
var joined = 0;
JsonNode[] outputs = new JsonNode[futures.size()];
// The branch whose future was last taken from the completion queue: the failed one when
// the join ends in ExecutionException.
int current = -1;
try {
for (Future<JsonNode> future : futures) {
for (int joined = 0; joined < futures.size(); joined++) {
long remainingNanos = joinDeadlineNanos - System.nanoTime();
if (remainingNanos <= 0) {
throw parallelJoinExpired(stateDeadlineNanos, timeoutSeconds);
}
try {
results.add(future.get(remainingNanos, TimeUnit.NANOSECONDS));
} catch (java.util.concurrent.TimeoutException e) {
Future<JsonNode> done = completions.poll(remainingNanos, TimeUnit.NANOSECONDS);
if (done == null) {
throw parallelJoinExpired(stateDeadlineNanos, timeoutSeconds);
}
joined++;
current = futures.indexOf(done);
outputs[current] = done.get();
}
} catch (InterruptedException e) {
abandon(branchChains, futures);
@@ -2678,7 +2687,7 @@ public class AslExecutor {
throw e;
} catch (ExecutionException e) {
List<String> abortedEventTypes = cutAfterFailure(branchChains, futures);
chain.continueFrom(branchChains.get(joined).lastEventId());
chain.continueFrom(branchChains.get(current).lastEventId());
if (e.getCause() instanceof FailStateException failure && !failure.isRuntimeError()) {
for (String abortedEventType : abortedEventTypes) {
chain.publishBeside(abortedEventType, null);
@@ -2705,6 +2714,10 @@ public class AslExecutor {
chain.continueAfter(branchChains);
chain.publishAside("ParallelStateSucceeded", null);
ArrayNode results = objectMapper.createArrayNode();
for (JsonNode output : outputs) {
results.add(output);
}
if (jsonata) {
JsonNode output = applyJsonataOutput(stateDef, input, results, context, variables);
return new StateResult(output, stateDef.path("Next").asText(null));