mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(stepfunctions): emit task history events and chain previousEventId (#2703)
A Task state now emits Scheduled, Started, and Succeeded or Failed events. Service integrations get Task* events. Direct Lambda ARNs get LambdaFunction* events. Activity ARNs get Activity* events. taskScheduledEventDetails carries the resolved Parameters or Arguments as a JSON string. previousEventId now chains every event to the one before it. The first state's Entered event points to 0, matching real AWS. Details now include inputDetails and outputDetails where AWS has them. Event shapes were verified against real AWS. Fixes #2520
This commit is contained in:
1 parent
13664e7693
commit
5600d21f32
7 files changed
+759
-32
No files matched your search
@@ -353,6 +353,7 @@ public class AslExecutor {
|
||||
// Declared outside the try so the terminal handlers below can keep numbering the history
|
||||
// where the execution left it.
|
||||
AtomicLong eventId = new AtomicLong(history.size());
|
||||
var firstState = true;
|
||||
try {
|
||||
JsonNode definition = objectMapper.readTree(sm.getDefinition());
|
||||
JsonNode states = definition.path("States");
|
||||
@@ -372,8 +373,10 @@ public class AslExecutor {
|
||||
}
|
||||
|
||||
String type = stateDef.path("Type").asText();
|
||||
addEvent(history, eventId, stateEnteredEventType(type), null,
|
||||
Map.of("name", currentStateName, "input", currentInput.toString()));
|
||||
addEvent(history, eventId, stateEnteredEventType(type), firstState ? 0L : eventId.get(),
|
||||
Map.of("name", currentStateName, "input", currentInput.toString(),
|
||||
"inputDetails", Map.of("truncated", false)));
|
||||
firstState = false;
|
||||
|
||||
// Update per-state context fields
|
||||
updateStateContext(execContext, currentStateName);
|
||||
@@ -382,8 +385,9 @@ public class AslExecutor {
|
||||
try {
|
||||
var stateResult = executeStateWithRetry(currentStateName, type, stateDef, currentInput,
|
||||
history, eventId, sm, jsonata, topLevelQueryLanguage, execContext, variables);
|
||||
addEvent(history, eventId, stateExitedEventType(type), eventId.get() - 1,
|
||||
Map.of("name", currentStateName, "output", stateResult.output().toString()));
|
||||
addEvent(history, eventId, stateExitedEventType(type),
|
||||
Map.of("name", currentStateName, "output", stateResult.output().toString(),
|
||||
"outputDetails", Map.of("truncated", false)));
|
||||
|
||||
currentInput = stateResult.output();
|
||||
currentStateName = stateResult.nextState();
|
||||
@@ -403,8 +407,9 @@ public class AslExecutor {
|
||||
failure = catchClauseFailure;
|
||||
}
|
||||
if (caught != null) {
|
||||
addEvent(history, eventId, stateExitedEventType(type), eventId.get() - 1,
|
||||
Map.of("name", currentStateName, "output", caught.output().toString()));
|
||||
addEvent(history, eventId, stateExitedEventType(type),
|
||||
Map.of("name", currentStateName, "output", caught.output().toString(),
|
||||
"outputDetails", Map.of("truncated", false)));
|
||||
currentInput = caught.output();
|
||||
currentStateName = caught.nextState();
|
||||
continue;
|
||||
@@ -422,8 +427,8 @@ public class AslExecutor {
|
||||
exec.setOutput(currentInput.toString());
|
||||
exec.setStopDate(System.currentTimeMillis() / 1000.0);
|
||||
exec.setStatus("SUCCEEDED");
|
||||
addEvent(history, eventId, "ExecutionSucceeded", null,
|
||||
Map.of("output", currentInput.toString()));
|
||||
addEvent(history, eventId, "ExecutionSucceeded",
|
||||
Map.of("output", currentInput.toString(), "outputDetails", Map.of("truncated", false)));
|
||||
onUpdate.accept(exec, history);
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -598,30 +603,41 @@ public class AslExecutor {
|
||||
tokenFuture = sfnService.get().registerPendingToken(taskToken);
|
||||
}
|
||||
|
||||
JsonNode taskResult;
|
||||
JsonNode effectiveInput;
|
||||
if (jsonata) {
|
||||
var effectiveInput = input;
|
||||
effectiveInput = input;
|
||||
if (stateDef.has("Arguments")) {
|
||||
var statesVar = buildStatesVar(input, null, context);
|
||||
effectiveInput = jsonataEvaluator.resolveTemplate(
|
||||
stateDef.get("Arguments"), "Arguments", statesVar, variables);
|
||||
}
|
||||
taskResult = mockedSteps != null
|
||||
? mockedTaskResult(mockedSteps, stateName, attempt)
|
||||
: invokeResource(effectiveResource, effectiveInput, sm, taskToken);
|
||||
} else {
|
||||
var effectiveInput = applyInputPath(stateDef, input);
|
||||
effectiveInput = applyInputPath(stateDef, input);
|
||||
if (stateDef.has("Parameters")) {
|
||||
effectiveInput = resolveParameters(stateDef.get("Parameters"), effectiveInput, context);
|
||||
}
|
||||
}
|
||||
|
||||
var profile = taskEventProfile(resource, isActivity);
|
||||
addTaskScheduledEvent(history, eventId, profile, stateDef, effectiveInput, sm);
|
||||
addTaskStartedEvent(history, eventId, profile);
|
||||
|
||||
JsonNode taskResult;
|
||||
try {
|
||||
taskResult = mockedSteps != null
|
||||
? mockedTaskResult(mockedSteps, stateName, attempt)
|
||||
: invokeResource(effectiveResource, effectiveInput, sm, taskToken);
|
||||
if (tokenFuture != null) {
|
||||
taskResult = awaitToken(tokenFuture, stateDef);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
var failure = e instanceof FailStateException f ? f : null;
|
||||
addTaskFailedEvent(history, eventId, profile,
|
||||
failure != null && failure.error != null ? failure.error : "States.Runtime",
|
||||
failure != null ? failure.cause : e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (tokenFuture != null) {
|
||||
taskResult = awaitToken(tokenFuture, stateDef);
|
||||
}
|
||||
addTaskSucceededEvent(history, eventId, profile, taskResult);
|
||||
|
||||
if (jsonata) {
|
||||
JsonNode output = applyJsonataOutput(stateDef, input, taskResult, context, variables);
|
||||
@@ -3422,16 +3438,101 @@ public class AslExecutor {
|
||||
|
||||
// ──────────────────────────── History helpers ────────────────────────────
|
||||
|
||||
private void addEvent(List<HistoryEvent> history, AtomicLong counter, String type,
|
||||
Long prevId, Map<String, Object> details) {
|
||||
HistoryEvent event = new HistoryEvent();
|
||||
private void addEvent(List<HistoryEvent> history, AtomicLong counter, String type, Map<String, Object> details) {
|
||||
addEvent(history, counter, type, counter.get(), details);
|
||||
}
|
||||
|
||||
private void addEvent(List<HistoryEvent> history, AtomicLong counter, String type, long previousEventId,
|
||||
Map<String, Object> details) {
|
||||
var event = new HistoryEvent();
|
||||
event.setId(counter.incrementAndGet());
|
||||
event.setPreviousEventId(previousEventId);
|
||||
event.setType(type);
|
||||
event.setPreviousEventId(prevId);
|
||||
event.setDetails(details);
|
||||
history.add(event);
|
||||
}
|
||||
|
||||
private record TaskEventProfile(String prefix, String resourceType, String resource) {}
|
||||
|
||||
private TaskEventProfile taskEventProfile(String resource, boolean isActivity) {
|
||||
if (isActivity) {
|
||||
return new TaskEventProfile("Activity", null, resource);
|
||||
}
|
||||
if (resource.contains(":lambda:") && resource.contains(":function:")) {
|
||||
return new TaskEventProfile("LambdaFunction", null, resource);
|
||||
}
|
||||
if (resource.startsWith("arn:aws:states:::")) {
|
||||
var tail = resource.substring("arn:aws:states:::".length());
|
||||
var idx = tail.lastIndexOf(':');
|
||||
if (idx < 0) {
|
||||
return new TaskEventProfile("Task", tail, tail);
|
||||
}
|
||||
return new TaskEventProfile("Task", tail.substring(0, idx), tail.substring(idx + 1));
|
||||
}
|
||||
return new TaskEventProfile("Task", resource, resource);
|
||||
}
|
||||
|
||||
private void addTaskScheduledEvent(List<HistoryEvent> history, AtomicLong eventId, TaskEventProfile profile,
|
||||
JsonNode stateDef, JsonNode effectiveInput, StateMachine sm) {
|
||||
var details = new LinkedHashMap<String, Object>();
|
||||
if (profile.resourceType() != null) {
|
||||
details.put("resourceType", profile.resourceType());
|
||||
}
|
||||
details.put("resource", profile.resource());
|
||||
if ("Task".equals(profile.prefix())) {
|
||||
details.put("region", extractRegionFromArn(sm.getStateMachineArn()));
|
||||
details.put("parameters", effectiveInput.toString());
|
||||
} else {
|
||||
details.put("input", effectiveInput.toString());
|
||||
details.put("inputDetails", Map.of("truncated", false));
|
||||
}
|
||||
if (stateDef.path("TimeoutSeconds").isNumber()) {
|
||||
details.put("timeoutInSeconds", stateDef.path("TimeoutSeconds").asLong());
|
||||
}
|
||||
if (stateDef.path("HeartbeatSeconds").isNumber()) {
|
||||
details.put("heartbeatInSeconds", stateDef.path("HeartbeatSeconds").asLong());
|
||||
}
|
||||
addEvent(history, eventId, profile.prefix() + "Scheduled", details);
|
||||
}
|
||||
|
||||
private void addTaskStartedEvent(List<HistoryEvent> history, AtomicLong eventId, TaskEventProfile profile) {
|
||||
if ("Task".equals(profile.prefix())) {
|
||||
addEvent(history, eventId, profile.prefix() + "Started",
|
||||
Map.of("resourceType", profile.resourceType(), "resource", profile.resource()));
|
||||
} else {
|
||||
addEvent(history, eventId, profile.prefix() + "Started", null);
|
||||
}
|
||||
}
|
||||
|
||||
private void addTaskSucceededEvent(List<HistoryEvent> history, AtomicLong eventId, TaskEventProfile profile,
|
||||
JsonNode taskResult) {
|
||||
var output = taskResult.toString();
|
||||
if ("Task".equals(profile.prefix())) {
|
||||
addEvent(history, eventId, profile.prefix() + "Succeeded",
|
||||
Map.of("resourceType", profile.resourceType(), "resource", profile.resource(),
|
||||
"output", output, "outputDetails", Map.of("truncated", false)));
|
||||
} else {
|
||||
addEvent(history, eventId, profile.prefix() + "Succeeded",
|
||||
Map.of("output", output, "outputDetails", Map.of("truncated", false)));
|
||||
}
|
||||
}
|
||||
|
||||
private void addTaskFailedEvent(List<HistoryEvent> history, AtomicLong eventId, TaskEventProfile profile,
|
||||
String error, String cause) {
|
||||
var details = new LinkedHashMap<String, Object>();
|
||||
if ("Task".equals(profile.prefix())) {
|
||||
details.put("resourceType", profile.resourceType());
|
||||
details.put("resource", profile.resource());
|
||||
}
|
||||
if (error != null) {
|
||||
details.put("error", error);
|
||||
}
|
||||
if (cause != null) {
|
||||
details.put("cause", cause);
|
||||
}
|
||||
addEvent(history, eventId, profile.prefix() + "Failed", details);
|
||||
}
|
||||
|
||||
private void failExecution(Execution exec, List<HistoryEvent> history, AtomicLong eventId, FailStateException e) {
|
||||
failExecution(exec, history, eventId, e.error != null ? e.error : "States.Runtime",
|
||||
e.cause != null ? e.cause : "");
|
||||
@@ -3448,7 +3549,7 @@ public class AslExecutor {
|
||||
exec.setCause(cause);
|
||||
exec.setStopDate(System.currentTimeMillis() / 1000.0);
|
||||
exec.setStatus("FAILED");
|
||||
addEvent(history, eventId, "ExecutionFailed", null,
|
||||
addEvent(history, eventId, "ExecutionFailed",
|
||||
Map.of("error", error, "cause", cause));
|
||||
}
|
||||
|
||||
|
||||
+13
-2
@@ -17,8 +17,10 @@ import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ApplicationScoped
|
||||
public class StepFunctionsJsonHandler {
|
||||
@@ -374,13 +376,22 @@ public class StepFunctionsJsonHandler {
|
||||
item.put("timestamp", e.getTimestamp());
|
||||
item.put("type", e.getType());
|
||||
if (e.getPreviousEventId() != null) item.put("previousEventId", e.getPreviousEventId());
|
||||
if (includeExecutionData && e.getDetails() != null) {
|
||||
item.set(historyEventDetailsField(e.getType()), objectMapper.valueToTree(e.getDetails()));
|
||||
if (e.getDetails() != null) {
|
||||
var details = e.getDetails();
|
||||
if (!includeExecutionData) {
|
||||
var filtered = new LinkedHashMap<>(details);
|
||||
filtered.keySet().removeAll(EXECUTION_DATA_FIELDS);
|
||||
details = filtered;
|
||||
}
|
||||
item.set(historyEventDetailsField(e.getType()), objectMapper.valueToTree(details));
|
||||
}
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static final Set<String> EXECUTION_DATA_FIELDS =
|
||||
Set.of("input", "inputDetails", "output", "outputDetails");
|
||||
|
||||
static String historyEventDetailsField(String type) {
|
||||
if (type.endsWith("StateEntered")) {
|
||||
return "stateEnteredEventDetails";
|
||||
|
||||
+7
-2
@@ -527,9 +527,11 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
var history = new ArrayList<HistoryEvent>();
|
||||
var startEvent = new HistoryEvent();
|
||||
startEvent.setId(1L);
|
||||
startEvent.setPreviousEventId(0L);
|
||||
startEvent.setType("ExecutionStarted");
|
||||
startEvent.setDetails(Map.of("input", input != null ? input : "{}",
|
||||
"roleArn", sm.getRoleArn() != null ? sm.getRoleArn() : ""));
|
||||
"roleArn", sm.getRoleArn() != null ? sm.getRoleArn() : "",
|
||||
"inputDetails", Map.of("truncated", false)));
|
||||
history.add(startEvent);
|
||||
historyCache.put(arn, history);
|
||||
|
||||
@@ -579,9 +581,11 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
var history = new ArrayList<HistoryEvent>();
|
||||
var startEvent = new HistoryEvent();
|
||||
startEvent.setId(1L);
|
||||
startEvent.setPreviousEventId(0L);
|
||||
startEvent.setType("ExecutionStarted");
|
||||
startEvent.setDetails(Map.of("input", input != null ? input : "{}",
|
||||
"roleArn", sm.getRoleArn() != null ? sm.getRoleArn() : ""));
|
||||
"roleArn", sm.getRoleArn() != null ? sm.getRoleArn() : "",
|
||||
"inputDetails", Map.of("truncated", false)));
|
||||
history.add(startEvent);
|
||||
|
||||
aslExecutor.executeSync(sm, exec, history, (updatedExec, updatedHistory) -> {
|
||||
@@ -668,6 +672,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
List<HistoryEvent> history = historyCache.getOrDefault(arn, new ArrayList<>());
|
||||
HistoryEvent event = new HistoryEvent();
|
||||
event.setId(history.size() + 1L);
|
||||
event.setPreviousEventId((long) history.size());
|
||||
event.setType("ExecutionAborted");
|
||||
Map<String, Object> details = new HashMap<>();
|
||||
if (error != null) details.put("error", error);
|
||||
|
||||
Reference in new issue
Block a user