mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(stepfunctions): emit execution status change events to EventBridge (#5567)
* feat(stepfunctions): emit execution status change events to EventBridge STANDARD executions now publish "Step Functions Execution Status Change" events (source aws.states) to the default EventBridge bus on RUNNING, SUCCEEDED, FAILED, TIMED_OUT and ABORTED, so rules matching aws.states fire as they do on AWS. EXPRESS executions emit nothing, as on AWS. StepFunctionsEventPublisher follows the EcsEventPublisher pattern. RUNNING is published when StartExecution creates the execution, terminal statuses from the worker's completion callback, and ABORTED from StopExecution only, so a stopped execution emits exactly one ABORTED. An idempotent StartExecution and the startup sweep of executions abandoned by a restart emit nothing. * fix(stepfunctions): match the documented execution status change event Address review feedback on the aws.states events: - Always send stateMachineVersionArn and stateMachineAliasArn, null when unused, as AWS's documented events do. - Report redrive status as the documented examples do: FAILED and TIMED_OUT are REDRIVABLE with "Execution is <status> and is redrivable"; RUNNING, SUCCEEDED and ABORTED are NOT_REDRIVABLE with "Execution is <status> and cannot be redriven". - Name States.Timeout as the error of a TIMED_OUT event. - Apply the 248 KiB rule: input is left out when the escaped input and output together exceed it, output when the escaped output alone does, and inputDetails/outputDetails.included report it. * test(stepfunctions): check event absences without waiting on empty queues The integration test long-polled SQS for 2 s after each case to show nothing more arrived, so the class waited about 10 s by design. It now asserts only the events that should arrive. The absences that matter are checked in StepFunctionsServiceExecutionEventsTest: it captures the completion callback from a mocked AslExecutor and verifies that an EXPRESS execution publishes nothing and that the worker's callback after StopExecution adds no second ABORTED. Add StepFunctionsExecutions.waitForTerminalState to the shared testing package and use it here; moving the other Step Functions integration tests onto it is left for a separate PR.
This commit is contained in:
1 parent
ad1b225591
commit
fc5e1aea0a
12 files changed
+916
-7
No files matched your search
+168
@@ -0,0 +1,168 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Publishes AWS-shaped {@code aws.states} "Step Functions Execution Status Change" events to the
|
||||
* default EventBridge bus. AWS emits them for STANDARD workflows only, one per status the
|
||||
* execution enters: RUNNING, SUCCEEDED, FAILED, TIMED_OUT and ABORTED.
|
||||
*
|
||||
* <p>The event's account is left to the caller's request context, which is the execution's own
|
||||
* account on every path that emits: the StartExecution and StopExecution requests, and the worker
|
||||
* thread that runs under the execution's account. The region is taken from the execution ARN.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class StepFunctionsEventPublisher {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(StepFunctionsEventPublisher.class);
|
||||
static final String SOURCE = "aws.states";
|
||||
static final String DETAIL_TYPE = "Step Functions Execution Status Change";
|
||||
// AWS's documented EventBridge quota rule: when the escaped input and escaped output together
|
||||
// exceed 248 KiB the input is left out, and when the escaped output alone exceeds it the output
|
||||
// is left out too. inputDetails/outputDetails.included report which ones were.
|
||||
static final long MAX_PAYLOAD_BYTES = 248L * 1024;
|
||||
|
||||
private final EventBridgeService eventBridgeService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Inject
|
||||
public StepFunctionsEventPublisher(EventBridgeService eventBridgeService,
|
||||
RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
this.eventBridgeService = eventBridgeService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits one status change event carrying the execution's current fields. Never throws: a
|
||||
* failure to publish is logged and the execution carries on.
|
||||
*/
|
||||
public void emitStatusChange(Execution exec) {
|
||||
String executionArn = exec.getExecutionArn();
|
||||
try {
|
||||
ObjectNode detail = buildDetail(exec);
|
||||
ArrayNode resources = objectMapper.createArrayNode();
|
||||
if (executionArn != null) {
|
||||
resources.add(executionArn);
|
||||
}
|
||||
Map<String, Object> entry = new HashMap<>();
|
||||
entry.put("Source", SOURCE);
|
||||
entry.put("DetailType", DETAIL_TYPE);
|
||||
entry.put("Detail", objectMapper.writeValueAsString(detail));
|
||||
entry.put("Resources", resources);
|
||||
EventBridgeService.PutEventsResult result = eventBridgeService.putEvents(List.of(entry),
|
||||
AwsArnUtils.regionOrDefault(executionArn, regionResolver.getDefaultRegion()));
|
||||
if (result != null && result.failedCount() > 0) {
|
||||
LOG.warnv("EventBridge rejected Step Functions Execution Status Change for {0}: {1}",
|
||||
executionArn, result.entries());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("Failed to emit Step Functions Execution Status Change for {0}: {1}",
|
||||
executionArn, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS emits status change events for STANDARD executions only. An EXPRESS execution's ARN is
|
||||
* minted under the {@code express:} resource namespace, a STANDARD one under {@code execution:}.
|
||||
*/
|
||||
static boolean isStandard(Execution exec) {
|
||||
String arn = exec.getExecutionArn();
|
||||
if (arn == null || !AwsArnUtils.isArn(arn)) {
|
||||
return false;
|
||||
}
|
||||
return AwsArnUtils.parse(arn).resource().startsWith("execution:");
|
||||
}
|
||||
|
||||
// The fields are read under the Execution's monitor, the one the worker and StopExecution take
|
||||
// for their terminal writes, so the event never pairs one status with another's stopDate.
|
||||
// Every key of the documented event is always present, null when it does not apply.
|
||||
private ObjectNode buildDetail(Execution exec) throws JsonProcessingException {
|
||||
ObjectNode detail = objectMapper.createObjectNode();
|
||||
synchronized (exec) {
|
||||
String status = exec.getStatus();
|
||||
boolean succeeded = "SUCCEEDED".equals(status);
|
||||
detail.put("executionArn", exec.getExecutionArn());
|
||||
detail.put("stateMachineArn", exec.getStateMachineArn());
|
||||
detail.put("name", exec.getName());
|
||||
detail.put("status", status);
|
||||
detail.put("startDate", toEpochMillis(exec.getStartDate()));
|
||||
if (exec.getStopDate() != null && !"RUNNING".equals(status)) {
|
||||
detail.put("stopDate", toEpochMillis(exec.getStopDate()));
|
||||
} else {
|
||||
detail.putNull("stopDate");
|
||||
}
|
||||
|
||||
String output = succeeded ? exec.getOutput() : null;
|
||||
long outputSize = escapedSize(output);
|
||||
boolean outputIncluded = outputSize <= MAX_PAYLOAD_BYTES;
|
||||
boolean inputIncluded = escapedSize(exec.getInput()) + outputSize <= MAX_PAYLOAD_BYTES;
|
||||
detail.put("input", inputIncluded ? exec.getInput() : null);
|
||||
detail.putObject("inputDetails").put("included", inputIncluded);
|
||||
detail.put("output", outputIncluded ? output : null);
|
||||
if (succeeded) {
|
||||
detail.putObject("outputDetails").put("included", outputIncluded);
|
||||
} else {
|
||||
detail.putNull("outputDetails");
|
||||
}
|
||||
|
||||
detail.put("stateMachineVersionArn", exec.getStateMachineVersionArn());
|
||||
detail.put("stateMachineAliasArn", exec.getStateMachineAliasArn());
|
||||
detail.put("redriveCount", 0);
|
||||
detail.putNull("redriveDate");
|
||||
detail.put("redriveStatus", redriveStatus(status));
|
||||
detail.put("redriveStatusReason", redriveStatusReason(status));
|
||||
detail.put("error", "TIMED_OUT".equals(status) && exec.getError() == null
|
||||
? "States.Timeout" : exec.getError());
|
||||
detail.put("cause", exec.getCause());
|
||||
}
|
||||
return detail;
|
||||
}
|
||||
|
||||
/**
|
||||
* The size of {@code value} once escaped as a JSON string, the measure AWS applies its 248 KiB
|
||||
* rule to. A null value takes no room.
|
||||
*/
|
||||
private long escapedSize(String value) throws JsonProcessingException {
|
||||
if (value == null) {
|
||||
return 0;
|
||||
}
|
||||
// writeValueAsBytes quotes the string; the quotes are not part of the payload.
|
||||
return objectMapper.writeValueAsBytes(value).length - 2L;
|
||||
}
|
||||
|
||||
// Floci does not implement RedriveExecution, so these carry what AWS's documented events report
|
||||
// for an execution that was never redriven: FAILED and TIMED_OUT are redrivable, the rest are not.
|
||||
private static String redriveStatus(String status) {
|
||||
return "FAILED".equals(status) || "TIMED_OUT".equals(status) ? "REDRIVABLE" : "NOT_REDRIVABLE";
|
||||
}
|
||||
|
||||
private static String redriveStatusReason(String status) {
|
||||
if (status == null) {
|
||||
return null;
|
||||
}
|
||||
return "FAILED".equals(status) || "TIMED_OUT".equals(status)
|
||||
? "Execution is " + status + " and is redrivable"
|
||||
: "Execution is " + status + " and cannot be redriven";
|
||||
}
|
||||
|
||||
private static long toEpochMillis(double epochSeconds) {
|
||||
return Math.round(epochSeconds * 1000.0);
|
||||
}
|
||||
}
|
||||
+14
-2
@@ -71,6 +71,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
private final AslExecutor aslExecutor;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final SfnMockLoader mockLoader;
|
||||
private final StepFunctionsEventPublisher eventPublisher;
|
||||
|
||||
// Fields that are valid only in JSONPath mode. Validated against real AWS:
|
||||
// creating a JSONata state machine with any of these fields returns SCHEMA_VALIDATION_FAILED.
|
||||
@@ -123,7 +124,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
@Inject
|
||||
public StepFunctionsService(StorageFactory storageFactory, RegionResolver regionResolver,
|
||||
AslExecutor aslExecutor, ObjectMapper objectMapper,
|
||||
SfnMockLoader mockLoader) {
|
||||
SfnMockLoader mockLoader, StepFunctionsEventPublisher eventPublisher) {
|
||||
this.stateMachineStore = storageFactory.create("stepfunctions", "sfn-state-machines.json",
|
||||
new TypeReference<Map<String, StateMachine>>() {});
|
||||
this.executionStore = storageFactory.create("stepfunctions", "sfn-executions.json",
|
||||
@@ -141,6 +142,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
this.aslExecutor = aslExecutor;
|
||||
this.objectMapper = objectMapper;
|
||||
this.mockLoader = mockLoader;
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
@@ -781,12 +783,19 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
|
||||
LOG.infov("Started execution: {0}", arn);
|
||||
if (!express) {
|
||||
eventPublisher.emitStatusChange(exec);
|
||||
}
|
||||
|
||||
// The executor appends to this same history, so the cache entry put above is already the
|
||||
// finished history by the time this runs.
|
||||
// finished history by the time this runs. The worker also lands here after StopExecution
|
||||
// aborted it; that ABORTED event is StopExecution's to emit, so it is not emitted twice.
|
||||
aslExecutor.executeAsync(sm, exec, history, mockedTestCase, (updatedExec, updatedHistory) -> {
|
||||
executionStore.put(updatedExec.getExecutionArn(), updatedExec);
|
||||
LOG.infov("Execution {0} completed with status {1}", updatedExec.getExecutionArn(), updatedExec.getStatus());
|
||||
if (!express && !"ABORTED".equals(updatedExec.getStatus())) {
|
||||
eventPublisher.emitStatusChange(updatedExec);
|
||||
}
|
||||
});
|
||||
|
||||
return exec;
|
||||
@@ -962,6 +971,9 @@ public class StepFunctionsService implements Resettable, ResourceProvider {
|
||||
return;
|
||||
}
|
||||
executionStore.put(arn, exec);
|
||||
if (StepFunctionsEventPublisher.isStandard(exec)) {
|
||||
eventPublisher.emitStatusChange(exec);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user