fix(stepfunctions): persist execution history across restarts (#3291)

* fix(stepfunctions): persist execution history across restarts

* fix(stepfunctions): preserve persisted execution history

* fix(stepfunctions): preserve running execution snapshot during restart test

Keep the in-flight worker alive until the persisted snapshot is reloaded, then
wait for cleanup so the test cannot rewrite or retain its temporary state.

* fix(stepfunctions): checkpoint persisted execution history
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-09 10:27:19 -04:00
1 parent d3530d90c9
commit 35159c5aaa
5 files changed
+311 -19

No files matched your search

@@ -37,11 +37,13 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.function.IntConsumer;
@ApplicationScoped
public class StepFunctionsService implements Resettable, ResourceProvider {
private static final Logger LOG = Logger.getLogger(StepFunctionsService.class);
private static final int HISTORY_PERSIST_CHECKPOINT = 100;
private final StorageBackend<String, StateMachine> stateMachineStore;
// Account-aware: the startup sweep has no request context and must reach every account.
@@ -577,9 +579,11 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
exec.setName(execName);
exec.setInput(input);
exec.setStatus("RUNNING");
executionStore.put(arn, exec);
history = new ExecutionHistory();
history = new ExecutionHistory(eventCount -> {
if (eventCount % HISTORY_PERSIST_CHECKPOINT == 0) {
executionStore.put(arn, exec);
}
});
var startEvent = new HistoryEvent();
startEvent.setId(1L);
startEvent.setPreviousEventId(0L);
@@ -587,7 +591,9 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
startEvent.setDetails(Map.of("input", input != null ? input : "{}",
"roleArn", sm.getRoleArn() != null ? sm.getRoleArn() : "",
"inputDetails", Map.of("truncated", false)));
exec.setHistory(history);
history.add(startEvent);
executionStore.put(arn, exec);
historyCache.put(arn, history);
}
@@ -794,18 +800,23 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
if (cause != null) {
details.put("cause", cause);
}
// An execution can outlive its history: the executions are stored, the histories are held in
// memory only, so a restart in persistent mode brings a RUNNING execution back with nothing
// behind it. The abort still gets recorded, against a history that starts here.
historyCache.computeIfAbsent(arn, key -> new ExecutionHistory())
.sealWith("ExecutionAborted", details);
// A restart can leave a RUNNING execution without its worker and token future. Preserve all
// persisted events, then append the terminal event that explains the deterministic recovery.
ExecutionHistory history = historyCache.computeIfAbsent(arn,
key -> new ExecutionHistory(exec.getHistory(), () -> { }, false));
exec.setHistory(history);
history.sealWith("ExecutionAborted", details);
return true;
}
public List<HistoryEvent> getExecutionHistory(String arn) {
describeExecution(arn);
ExecutionHistory history = historyCache.get(arn);
return history != null ? history : Collections.emptyList();
Execution exec = describeExecution(arn);
return historyCache.computeIfAbsent(arn,
key -> new ExecutionHistory(exec.getHistory(), () -> { }, isTerminal(exec.getStatus())));
}
private static boolean isTerminal(String status) {
return !"RUNNING".equals(status);
}
/**
@@ -821,14 +832,37 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
private static final long serialVersionUID = 1L;
private final IntConsumer onAppend;
private boolean sealed;
ExecutionHistory() {
this(eventCount -> { });
}
ExecutionHistory(Runnable onAppend) {
this(eventCount -> onAppend.run());
}
ExecutionHistory(IntConsumer onAppend) {
this.onAppend = onAppend;
}
ExecutionHistory(List<HistoryEvent> events, Runnable onAppend, boolean sealed) {
super(events != null ? events : List.of());
this.onAppend = eventCount -> onAppend.run();
this.sealed = sealed;
}
@Override
public synchronized boolean add(HistoryEvent event) {
if (sealed) {
return false;
}
return super.add(event);
boolean added = super.add(event);
if (added) {
onAppend.accept(size());
}
return added;
}
/** Appends the terminal event, numbered from the end of the history, and takes no more. */
@@ -3,6 +3,9 @@ package io.github.hectorvent.floci.services.stepfunctions.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.ArrayList;
import java.util.List;
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class Execution {
@@ -16,6 +19,7 @@ public class Execution {
private Double stopDate;
private String error;
private String cause;
private List<HistoryEvent> history = new ArrayList<>();
public Execution() {
this.startDate = System.currentTimeMillis() / 1000.0;
@@ -50,4 +54,9 @@ public class Execution {
public String getCause() { return cause; }
public void setCause(String cause) { this.cause = cause; }
public List<HistoryEvent> getHistory() { return history; }
public void setHistory(List<HistoryEvent> history) {
this.history = history != null ? history : new ArrayList<>();
}
}