fix(ecs): reconcile persisted services at boot (#4501)

* fix(ecs): reconcile persisted services at boot

EcsService is ApplicationScoped, so Quarkus only creates it on the first
ECS request, and creating it is what loads the ECS stores and starts the
reconciler. After a restart every persisted service stayed without tasks,
load balancer targets or Cloud Map instances until something called the
ECS API.

EmulatorLifecycle now calls EcsService.restorePersistedRuntime() after
storage is loaded and the load balancers are restored, the way it already
restores RDS, ElastiCache and ELBv2.

* test(ecs): assert boot starts the ECS reconciler through CDI
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-27 22:28:13 -05:00
1 parent 2895262064
commit 52225e2053
6 files changed
+127 -1

No files matched your search

@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.appsync.graphql.SchemaCreationWorker;
import io.github.hectorvent.floci.services.docdb.container.DocDbContainerManager;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbRuntime;
import io.github.hectorvent.floci.services.ec2.Ec2MetadataServer;
import io.github.hectorvent.floci.services.ecs.EcsService;
import io.github.hectorvent.floci.services.ecs.container.EcsTaskRoleCredentialsServer;
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
import io.github.hectorvent.floci.services.elasticache.ElastiCacheMemcachedService;
@@ -99,6 +100,7 @@ class EmulatorLifecycleTest {
@Mock private EmulatorConfig.TimestreamInfluxDbServiceConfig timestreamInfluxDbServiceConfig;
@Mock private io.github.hectorvent.floci.services.elbv2.ElbV2Service elbV2Service;
@Mock private io.github.hectorvent.floci.services.elb.ElbClassicService elbClassicService;
@Mock private EcsService ecsService;
@Mock private InitializationHooksRunner initializationHooksRunner;
@Mock private SqsEventSourcePoller sqsPoller;
@Mock private KinesisEventSourcePoller kinesisPoller;
@@ -127,6 +129,7 @@ class EmulatorLifecycleTest {
Mockito.lenient().when(servicesConfig.ecs()).thenReturn(ecsServiceConfig);
Mockito.lenient().when(ecsServiceConfig.taskRoleCredentials()).thenReturn(ecsTaskRoleCredentialsConfig);
Mockito.lenient().when(ecsTaskRoleCredentialsConfig.enabled()).thenReturn(false);
Mockito.lenient().when(ecsServiceConfig.enabled()).thenReturn(false);
Mockito.lenient().when(ec2ServiceConfig.enabled()).thenReturn(false);
Mockito.lenient().when(servicesConfig.elbv2()).thenReturn(elbv2ServiceConfig);
Mockito.lenient().when(elbv2ServiceConfig.enabled()).thenReturn(false);
@@ -152,7 +155,7 @@ class EmulatorLifecycleTest {
memoryDbContainerManager, memoryDbProxyManager,
docDbContainerManager, neptuneContainerManager, neptuneProxyManager,
rabbitMqManager, flinkContainerManager, rdsService, timestreamInfluxDbService,
elbV2Service, elbClassicService,
elbV2Service, elbClassicService, ecsService,
initializationHooksRunner, sqsPoller, kinesisPoller, dynamodbStreamsPoller,
pipesService, ec2MetadataServer, ecsTaskRoleCredentialsServer, ecrRegistryManager, flociUiManager, initLifecycleState,
schemaCreationWorker, stepFunctionsService, containerTeardowns, persistentPathValidator,
@@ -256,6 +259,36 @@ class EmulatorLifecycleTest {
Mockito.verify(elbV2Service, Mockito.never()).restorePersistedRuntime();
}
@Test
void restoresPersistedEcsServicesAtBootAfterStorageAndLoadBalancers() {
// EcsService is created lazily, and creating it is what loads its stores and starts the
// reconciler. Without this call a restart leaves every persisted service without tasks
// until the first ECS request.
stubStorageConfig();
when(elbv2ServiceConfig.enabled()).thenReturn(true);
when(ecsServiceConfig.enabled()).thenReturn(true);
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
InOrder inOrder = Mockito.inOrder(storageFactory, elbV2Service, ecsService);
inOrder.verify(storageFactory).loadAll();
inOrder.verify(elbV2Service).restorePersistedRuntime();
inOrder.verify(ecsService).restorePersistedRuntime();
}
@Test
void leavesEcsUntouchedAtBootWhenEcsIsDisabled() {
stubStorageConfig();
when(initializationHooksRunner.hasHooks(InitializationHook.START)).thenReturn(false);
when(initializationHooksRunner.hasHooks(InitializationHook.READY)).thenReturn(false);
emulatorLifecycle.onStart(Mockito.mock(StartupEvent.class));
verify(ecsService, never()).restorePersistedRuntime();
}
@Test
@DisplayName("Should restore ElastiCache persisted runtime after loading storage when elasticache is enabled")
void shouldRestoreElastiCachePersistedRuntimeAfterStorageLoad() {
@@ -0,0 +1,42 @@
package io.github.hectorvent.floci.services.ecs;
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableBean;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import jakarta.enterprise.context.ApplicationScoped;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* EcsService is created lazily, and creating it is what loads the ECS stores and schedules the
* reconciler. Boot has to create it, or persisted services stay without tasks after a restart
* until the first ECS request. The profile of its own gives this test a fresh application that
* no other test has sent an ECS request to.
*/
@QuarkusTest
@TestProfile(EcsBootReconcilerIntegrationTest.FreshBootProfile.class)
class EcsBootReconcilerIntegrationTest {
public static final class FreshBootProfile implements QuarkusTestProfile {
}
@Test
void bootStartsTheReconcilerWithoutAnEcsRequest() {
Map<InjectableBean<?>, Object> created = Arc.container().getActiveContext(ApplicationScoped.class)
.getState().getContextualInstances();
EcsService ecsService = created.entrySet().stream()
.filter(entry -> entry.getKey().getBeanClass() == EcsService.class)
.map(entry -> (EcsService) entry.getValue())
.findFirst()
.orElse(null);
assertNotNull(ecsService, "EcsService must be created at boot, not on the first ECS request");
assertFalse(ecsService.isReconcilerShutdown());
}
}
@@ -106,6 +106,28 @@ class EcsServicePersistenceTest {
"deleted attribute must not reappear after restart");
}
@Test
void persistedServiceGetsItsTasksBackAfterRestartWithoutAnEcsRequest() {
SharedStorageFactory storage = new SharedStorageFactory();
EcsService first = serviceWithStorage(storage);
first.createCluster("app-cluster", Map.of(), REGION);
TaskDefinition td = first.registerTaskDefinition("web", List.of(container("app", "nginx:latest")),
NetworkMode.awsvpc, "256", "512", null, null, List.of("FARGATE"), null, REGION);
first.createService("app-cluster", "web-svc", td.getTaskDefinitionArn(),
2, LaunchType.FARGATE, List.of(), awsvpcConfiguration(), Map.of(), REGION);
// Task state is memory-only, so the restarted process starts with none. What EmulatorLifecycle
// does at boot is restore the persisted runtime, then the reconciler ticks.
EcsService restarted = serviceWithStorage(storage);
restarted.restorePersistedRuntime();
assertTrue(restarted.listTasks("app-cluster", null, null, "web-svc", REGION).isEmpty());
restarted.reconcile();
assertEquals(2, restarted.listTasks("app-cluster", null, null, "web-svc", REGION).size());
}
private static ContainerDefinition container(String name, String image) {
ContainerDefinition cd = new ContainerDefinition();
cd.setName(name);