mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 01:43:02 -05:00
fix(stepfunctions): abort executions abandoned by a restart (#2969)
* test(stepfunctions): cover executions abandoned by a restart * fix(stepfunctions): abort executions abandoned by a restart An execution runs inside the Floci process, so a restart leaves it stored as RUNNING with no worker behind it: DescribeExecution reported RUNNING forever and GetExecutionHistory reported nothing, so a caller waiting for a final status hung until its own timeout with no diagnosis. EmulatorLifecycle.onStart now calls StepFunctionsService.abortAbandonedExecutions after storageFactory.loadAll(), which retires every still-RUNNING execution with status ABORTED, a stopDate, error ExecutionAbandoned and a cause naming the restart, and seals its history with a single ExecutionAborted event. ABORTED is the status AWS gives an execution ended from outside its own definition, and it is the only terminal status that keeps the execution inside RedriveExecution's reach. The sweep runs with no request context, so it scans every account and writes each execution back under the account that owns it. The terminal write is shared with StopExecution, which handled the same window for its own half already. * fix(stepfunctions): file the abort history under the storage key markAborted read the history key off the stored value. An execution whose executionArn field is null made computeIfAbsent throw out of the startup sweep, which EmulatorLifecycle.onStart calls unguarded, so the process did not boot and the sweep stopped mid list with earlier entries flipped in memory and never written back. A field that disagreed with the storage key filed the ExecutionAborted event where GetExecutionHistory does not look. The key is now a parameter: StopExecution passes the ARN it was called with, the sweep passes the key the entry came under. The tests bound the stopDate between two literal epoch-second instants instead of asserting it is non-null, so a wrong unit fails; assert the sweep stays silent when nothing is RUNNING, which is what the count guard is for; and pin the WARN record by level and count rather than by its whole sentence. The lifecycle test whose plain verify was subsumed by the ordered one is gone. The documentation no longer claims the ExecutionAborted event survives a further restart: histories live in memory, so the event is written on the boot that aborts the execution, while the status, error and cause persist. * test(stepfunctions): require the AWS shape of an abort with no reason The startup sweep is measured against us-east-1 with StopExecution called without --error and --cause: status ABORTED, DescribeExecution omitting both keys, and executionAbortedEventDetails present and empty. The persistence test pins the sweep, and the integration test pins the wire shape, where an omitted key is distinguishable from a null one. * fix(stepfunctions): abort abandoned executions the way AWS aborts ExecutionAbandoned is a code AWS never emits, and the sweep was the only producer of it. Measured against us-east-1 with StopExecution called without --error and --cause, the abort reports status ABORTED, DescribeExecution omits the error and cause keys, and the terminal event carries an empty executionAbortedEventDetails. The sweep now takes that same path; the WARN line with the count is where the reason lives. markAborted keeps both parameters: StopExecution passes the caller's error and cause through them. --------- Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
1 parent
618ca58424
commit
504203f8e8
6 files changed
+386
-14
No files matched your search
@@ -91,6 +91,7 @@ class EmulatorLifecycleTest {
|
||||
@Mock private PersistentPathValidator persistentPathValidator;
|
||||
@Mock private EmulatorConfig.TlsConfig tlsConfig;
|
||||
@Mock private io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker schemaCreationWorker;
|
||||
@Mock private io.github.hectorvent.floci.services.stepfunctions.StepFunctionsService stepFunctionsService;
|
||||
@Mock private jakarta.enterprise.inject.Instance<io.github.hectorvent.floci.core.common.ContainerTeardown> containerTeardowns;
|
||||
|
||||
private EmulatorLifecycle emulatorLifecycle;
|
||||
@@ -120,7 +121,7 @@ class EmulatorLifecycleTest {
|
||||
rabbitMqManager, flinkContainerManager, rdsService, elbV2Service, elbClassicService,
|
||||
initializationHooksRunner, sqsPoller, kinesisPoller, dynamodbStreamsPoller,
|
||||
pipesService, ec2MetadataServer, ecrRegistryManager, flociUiManager, initLifecycleState,
|
||||
schemaCreationWorker, containerTeardowns, persistentPathValidator);
|
||||
schemaCreationWorker, stepFunctionsService, containerTeardowns, persistentPathValidator);
|
||||
Mockito.lenient().when(containerTeardowns.iterator())
|
||||
.thenReturn(java.util.Collections.emptyIterator());
|
||||
}
|
||||
@@ -223,6 +224,20 @@ class EmulatorLifecycleTest {
|
||||
inOrder.verify(schemaCreationWorker).rehydrateSchemas();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should abort abandoned Step Functions executions after loading storage")
|
||||
void shouldAbortAbandonedStepFunctionsExecutionsAfterStorageLoad() {
|
||||
stubStorageConfig();
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
|
||||
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
|
||||
|
||||
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
|
||||
|
||||
var inOrder = Mockito.inOrder(storageFactory, stepFunctionsService);
|
||||
inOrder.verify(storageFactory).loadAll();
|
||||
inOrder.verify(stepFunctionsService).abortAbandonedExecutions();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should validate the persistent path after BOOT hooks and before loading storage")
|
||||
void shouldValidatePersistentPathBeforeStorageLoad() {
|
||||
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
|
||||
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
/** Verifies executions abandoned by a restart reach a terminal status when the emulator comes back. */
|
||||
class StepFunctionsServicePersistenceTest {
|
||||
|
||||
private static final String DEFAULT_ACCOUNT = "000000000000";
|
||||
private static final String OTHER_ACCOUNT = "222222222222";
|
||||
private static final String EXECUTION_ARN =
|
||||
"arn:aws:states:us-east-1:000000000000:execution:TestStateMachine:HelloWorld";
|
||||
private static final String OTHER_ACCOUNT_EXECUTION_ARN =
|
||||
"arn:aws:states:us-east-1:222222222222:execution:TestStateMachine:HelloWorld";
|
||||
private static final String STATE_MACHINE_ARN =
|
||||
"arn:aws:states:us-east-1:000000000000:stateMachine:TestStateMachine";
|
||||
private static final double EPOCH_SECONDS_2023_11_14 = 1700000000.0;
|
||||
private static final double EPOCH_SECONDS_2100_01_01 = 4102444800.0;
|
||||
|
||||
private final AslExecutor aslExecutor = Mockito.mock(AslExecutor.class);
|
||||
private final SfnMockLoader mockLoader = Mockito.mock(SfnMockLoader.class);
|
||||
private final RegionResolver regionResolver = Mockito.mock(RegionResolver.class);
|
||||
|
||||
@Test
|
||||
void abandonedRunningExecutionIsAbortedOnRestart() {
|
||||
SharedStorageFactory storage = new SharedStorageFactory();
|
||||
AccountAwareStorageBackend<Execution> executions = executionStore(storage);
|
||||
StepFunctionsService beforeRestart = serviceWithStorage(storage);
|
||||
executions.putForAccount(DEFAULT_ACCOUNT, EXECUTION_ARN, running(EXECUTION_ARN));
|
||||
assertEquals("RUNNING", beforeRestart.describeExecution(EXECUTION_ARN).getStatus());
|
||||
|
||||
StepFunctionsService afterRestart = serviceWithStorage(storage);
|
||||
afterRestart.abortAbandonedExecutions();
|
||||
|
||||
// The shape AWS returns for an execution stopped with no error and no cause: the status is
|
||||
// the whole report, and error and cause are left off DescribeExecution and off the event.
|
||||
Execution reloaded = afterRestart.describeExecution(EXECUTION_ARN);
|
||||
assertEquals("ABORTED", reloaded.getStatus());
|
||||
assertNull(reloaded.getError());
|
||||
assertNull(reloaded.getCause());
|
||||
assertStopDateInEpochSeconds(reloaded.getStopDate());
|
||||
|
||||
List<HistoryEvent> history = afterRestart.getExecutionHistory(EXECUTION_ARN);
|
||||
assertEquals(1, history.size());
|
||||
HistoryEvent aborted = history.getFirst();
|
||||
assertEquals("ExecutionAborted", aborted.getType());
|
||||
assertEquals(1L, aborted.getId());
|
||||
assertEquals(Long.valueOf(0L), aborted.getPreviousEventId());
|
||||
assertEquals(Map.of(), aborted.getDetails());
|
||||
|
||||
verifyNoInteractions(aslExecutor, mockLoader, regionResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
void abandonedExecutionOfAnotherAccountStaysInItsOwnAccount() {
|
||||
SharedStorageFactory storage = new SharedStorageFactory();
|
||||
AccountAwareStorageBackend<Execution> executions = executionStore(storage);
|
||||
executions.putForAccount(OTHER_ACCOUNT, OTHER_ACCOUNT_EXECUTION_ARN,
|
||||
running(OTHER_ACCOUNT_EXECUTION_ARN));
|
||||
|
||||
List<LogRecord> logRecords = new ArrayList<>();
|
||||
Handler collector = collectorInto(logRecords);
|
||||
Logger serviceLogger = Logger.getLogger(StepFunctionsService.class.getName());
|
||||
serviceLogger.addHandler(collector);
|
||||
try {
|
||||
serviceWithStorage(storage).abortAbandonedExecutions();
|
||||
} finally {
|
||||
serviceLogger.removeHandler(collector);
|
||||
}
|
||||
|
||||
List<LogRecord> warnings = warningsIn(logRecords);
|
||||
assertEquals(1, warnings.size(),
|
||||
"expected one WARN record for the sweep, got: " + formattedAll(logRecords));
|
||||
assertTrue(formatted(warnings.getFirst()).contains("1"),
|
||||
"expected the WARN record to name how many executions were aborted, got: "
|
||||
+ formatted(warnings.getFirst()));
|
||||
|
||||
Optional<Execution> owned =
|
||||
executions.getForAccount(OTHER_ACCOUNT, OTHER_ACCOUNT_EXECUTION_ARN);
|
||||
assertTrue(owned.isPresent());
|
||||
assertEquals("ABORTED", owned.get().getStatus());
|
||||
assertNull(owned.get().getError());
|
||||
assertNull(owned.get().getCause());
|
||||
assertStopDateInEpochSeconds(owned.get().getStopDate());
|
||||
assertEquals(Optional.empty(),
|
||||
executions.getForAccount(DEFAULT_ACCOUNT, OTHER_ACCOUNT_EXECUTION_ARN));
|
||||
|
||||
verifyNoInteractions(aslExecutor, mockLoader, regionResolver);
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalExecutionsAreUntouchedAndTheSweepIsIdempotent() {
|
||||
SharedStorageFactory storage = new SharedStorageFactory();
|
||||
AccountAwareStorageBackend<Execution> executions = executionStore(storage);
|
||||
Execution succeeded = running("arn:aws:states:us-east-1:000000000000:execution:"
|
||||
+ "TestStateMachine:HelloWorldDone");
|
||||
succeeded.setStatus("SUCCEEDED");
|
||||
succeeded.setOutput("{\"greeting\":\"hello\"}");
|
||||
succeeded.setStopDate(1700000000.0);
|
||||
executions.putForAccount(DEFAULT_ACCOUNT, succeeded.getExecutionArn(), succeeded);
|
||||
executions.putForAccount(DEFAULT_ACCOUNT, EXECUTION_ARN, running(EXECUTION_ARN));
|
||||
|
||||
StepFunctionsService afterRestart = serviceWithStorage(storage);
|
||||
afterRestart.abortAbandonedExecutions();
|
||||
Double stopDateOfFirstSweep =
|
||||
afterRestart.describeExecution(EXECUTION_ARN).getStopDate();
|
||||
|
||||
// Nothing is RUNNING any more, so the second sweep retires nothing and says nothing: a WARN
|
||||
// on every clean boot is the noise the repo's logging rule forbids.
|
||||
List<LogRecord> logRecords = new ArrayList<>();
|
||||
Handler collector = collectorInto(logRecords);
|
||||
Logger serviceLogger = Logger.getLogger(StepFunctionsService.class.getName());
|
||||
serviceLogger.addHandler(collector);
|
||||
try {
|
||||
afterRestart.abortAbandonedExecutions();
|
||||
} finally {
|
||||
serviceLogger.removeHandler(collector);
|
||||
}
|
||||
assertEquals(List.of(), warningsIn(logRecords),
|
||||
"expected no WARN record from a sweep that found nothing RUNNING, got: "
|
||||
+ formattedAll(logRecords));
|
||||
|
||||
Execution untouched = afterRestart.describeExecution(succeeded.getExecutionArn());
|
||||
assertEquals("SUCCEEDED", untouched.getStatus());
|
||||
assertEquals("{\"greeting\":\"hello\"}", untouched.getOutput());
|
||||
assertEquals(Double.valueOf(1700000000.0), untouched.getStopDate());
|
||||
assertEquals(List.of(), afterRestart.getExecutionHistory(succeeded.getExecutionArn()));
|
||||
|
||||
Execution abandoned = afterRestart.describeExecution(EXECUTION_ARN);
|
||||
assertEquals("ABORTED", abandoned.getStatus());
|
||||
assertEquals(stopDateOfFirstSweep, abandoned.getStopDate());
|
||||
assertEquals(1, afterRestart.getExecutionHistory(EXECUTION_ARN).size());
|
||||
|
||||
verifyNoInteractions(aslExecutor, mockLoader, regionResolver);
|
||||
}
|
||||
|
||||
private static Handler collectorInto(List<LogRecord> records) {
|
||||
Handler collector = new Handler() {
|
||||
@Override
|
||||
public void publish(LogRecord record) {
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
};
|
||||
collector.setLevel(Level.ALL);
|
||||
return collector;
|
||||
}
|
||||
|
||||
private static List<LogRecord> warningsIn(List<LogRecord> records) {
|
||||
// The level is compared by value: under JBoss LogManager the record carries its own WARN
|
||||
// instance, not the java.util.logging constant.
|
||||
return records.stream()
|
||||
.filter(record -> record.getLevel().intValue() == Level.WARNING.intValue())
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static List<String> formattedAll(List<LogRecord> records) {
|
||||
return records.stream().map(StepFunctionsServicePersistenceTest::formatted).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* A stopDate DescribeExecution can report: epoch seconds, between 2023-11-14 and 2100-01-01.
|
||||
* Both bounds are literals, so a stopDate written in another unit fails here.
|
||||
*/
|
||||
private static void assertStopDateInEpochSeconds(Double stopDate) {
|
||||
assertNotNull(stopDate);
|
||||
assertTrue(stopDate > EPOCH_SECONDS_2023_11_14 && stopDate < EPOCH_SECONDS_2100_01_01,
|
||||
"stopDate is not a plausible epoch-seconds instant: " + stopDate);
|
||||
}
|
||||
|
||||
/** The record as a reader sees it, whether the handler receives it formatted or as a pattern. */
|
||||
private static String formatted(LogRecord record) {
|
||||
Object[] parameters = record.getParameters();
|
||||
if (parameters == null || parameters.length == 0) {
|
||||
return String.valueOf(record.getMessage());
|
||||
}
|
||||
return MessageFormat.format(record.getMessage(), parameters);
|
||||
}
|
||||
|
||||
private static Execution running(String executionArn) {
|
||||
Execution execution = new Execution();
|
||||
execution.setExecutionArn(executionArn);
|
||||
execution.setStateMachineArn(STATE_MACHINE_ARN);
|
||||
execution.setName("HelloWorld");
|
||||
execution.setInput("{}");
|
||||
execution.setStatus("RUNNING");
|
||||
return execution;
|
||||
}
|
||||
|
||||
private StepFunctionsService serviceWithStorage(StorageFactory storage) {
|
||||
return new StepFunctionsService(storage, regionResolver, aslExecutor,
|
||||
new ObjectMapper(), mockLoader);
|
||||
}
|
||||
|
||||
private static AccountAwareStorageBackend<Execution> executionStore(SharedStorageFactory storage) {
|
||||
return storage.create("stepfunctions", "sfn-executions.json",
|
||||
new TypeReference<Map<String, Execution>>() {});
|
||||
}
|
||||
|
||||
private static final class SharedStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private SharedStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(
|
||||
fileName, ignored -> AccountAwareStorageBackend.inMemory(DEFAULT_ACCOUNT));
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
@@ -15,6 +15,7 @@ import java.util.UUID;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
/**
|
||||
* Guards that {@code StopExecution} stops the execution instead of relabelling it: the status,
|
||||
@@ -94,6 +95,40 @@ class StepFunctionsStopExecutionIntegrationTest {
|
||||
assertEquals(stopDate, described.jsonPath().getDouble("stopDate"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Measured against us-east-1: an abort with no error and no cause leaves both keys out of
|
||||
* {@code DescribeExecution} and carries an empty {@code executionAbortedEventDetails}. This is
|
||||
* the terminal write the startup sweep of abandoned executions makes as well.
|
||||
*/
|
||||
@Test
|
||||
void abortWithNoReasonOmitsErrorAndCause() throws Exception {
|
||||
String executionArn = startWaitingExecution("stop-without-reason");
|
||||
Thread.sleep(300);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StopExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("""
|
||||
{"executionArn": "%s"}
|
||||
""".formatted(executionArn))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
Map<String, Object> described = describeExecution(executionArn).jsonPath().getMap("$");
|
||||
assertEquals("ABORTED", described.get("status"));
|
||||
// Absent, not null: a null value would leave the key in place and here it is gone.
|
||||
assertFalse(described.containsKey("error"), "DescribeExecution kept error: " + described);
|
||||
assertFalse(described.containsKey("cause"), "DescribeExecution kept cause: " + described);
|
||||
|
||||
List<Map<String, Object>> events =
|
||||
getExecutionHistory(executionArn).jsonPath().getList("events");
|
||||
Map<String, Object> aborted = events.get(events.size() - 1);
|
||||
assertEquals("ExecutionAborted", aborted.get("type"));
|
||||
assertEquals(Map.of(), aborted.get("executionAbortedEventDetails"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void abortedExecutionEndsItsHistoryAtExecutionAborted() throws Exception {
|
||||
String executionArn = startWaitingExecution("stop-ends-history");
|
||||
|
||||
Reference in new issue
Block a user