diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 296b70a895..6013a50bb1 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -177,6 +177,7 @@ public interface EmulatorConfig { BatchStorageConfig batch(); CodePipelineStorageConfig codepipeline(); S3VectorsStorageConfig s3vectors(); + EcsStorageConfig ecs(); } interface SsmStorageConfig { @@ -329,6 +330,13 @@ public interface EmulatorConfig { long flushIntervalMs(); } + interface EcsStorageConfig { + Optional mode(); + + @WithDefault("5000") + long flushIntervalMs(); + } + interface WalConfig { @WithDefault("30000") long compactionIntervalMs(); diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index 974f077fa8..84f7084651 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -206,7 +206,8 @@ public class ResolvedServiceCatalog { protocols(ServiceProtocol.QUERY), Set.of(), Set.of("ec2"), Set.of(), Set.of()), descriptor("ecs", "ecs", config.services().ecs().enabled(), true, - null, null, 5000L, null, ServiceProtocol.JSON, + "ecs", storageMode(config.storage().services().ecs().mode(), config.storage().mode()), + config.storage().services().ecs().flushIntervalMs(), null, ServiceProtocol.JSON, protocols(ServiceProtocol.JSON), Set.of("AmazonEC2ContainerServiceV20141113."), Set.of("ecs"), Set.of(), Set.of()), descriptor("appconfig", "appconfig", config.services().appconfig().enabled(), true, diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java index 35ff575324..cf3f20ad73 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/EcsService.java @@ -1,8 +1,11 @@ package io.github.hectorvent.floci.services.ecs; +import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.StorageBackedMap; +import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager; import io.github.hectorvent.floci.services.ecs.container.EcsTaskHandle; import io.github.hectorvent.floci.services.ecs.model.Attribute; @@ -37,6 +40,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.function.Function; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executors; @@ -53,27 +57,28 @@ public class EcsService { private final RegionResolver regionResolver; private final EcsContainerManager containerManager; private final EcsLoadBalancerRegistrar lbRegistrar; + private final StorageFactory storageFactory; private final boolean dockerMode; private final String baseUrl; private final ScheduledExecutorService reconciler = Executors.newSingleThreadScheduledExecutor( r -> { Thread t = new Thread(r, "ecs-reconciler"); t.setDaemon(true); return t; }); // region::clusterName → EcsCluster - private final Map clusters = new ConcurrentHashMap<>(); + private Map clusters = new ConcurrentHashMap<>(); // family:revision → TaskDefinition - private final Map taskDefinitions = new ConcurrentHashMap<>(); + private Map taskDefinitions = new ConcurrentHashMap<>(); // family → latest revision number - private final Map latestRevisions = new ConcurrentHashMap<>(); + private Map latestRevisions = new ConcurrentHashMap<>(); // taskArn → EcsTask private final Map tasks = new ConcurrentHashMap<>(); // taskArn → EcsTaskHandle (running containers) private final Map taskHandles = new ConcurrentHashMap<>(); // region::clusterName/serviceName → EcsServiceModel - private final Map services = new ConcurrentHashMap<>(); + private Map services = new ConcurrentHashMap<>(); // region::clusterArn/instanceArn → ContainerInstance private final Map containerInstances = new ConcurrentHashMap<>(); // name → CapacityProvider (excludes built-ins FARGATE, FARGATE_SPOT) - private final Map capacityProviders = new ConcurrentHashMap<>(); + private Map capacityProviders = new ConcurrentHashMap<>(); // taskSetArn → TaskSet private final Map taskSets = new ConcurrentHashMap<>(); // serviceDeploymentArn → ServiceDeployment @@ -81,25 +86,77 @@ public class EcsService { // serviceRevisionArn → ServiceRevision private final Map serviceRevisions = new ConcurrentHashMap<>(); // targetArn → List - private final Map> attributes = new ConcurrentHashMap<>(); + private Map> attributes = new ConcurrentHashMap<>(); // name → value (account-level settings) - private final Map accountSettings = new ConcurrentHashMap<>(); + private Map accountSettings = new ConcurrentHashMap<>(); @Inject public EcsService(RegionResolver regionResolver, EcsContainerManager containerManager, - EmulatorConfig config, EcsLoadBalancerRegistrar lbRegistrar) { + EmulatorConfig config, EcsLoadBalancerRegistrar lbRegistrar, + StorageFactory storageFactory) { this.regionResolver = regionResolver; this.containerManager = containerManager; this.dockerMode = !config.services().ecs().mock(); this.baseUrl = config.effectiveBaseUrl(); this.lbRegistrar = lbRegistrar; + this.storageFactory = storageFactory; } @PostConstruct void init() { + initializeStorage(); reconciler.scheduleAtFixedRate(this::reconcile, 5, 5, TimeUnit.SECONDS); } + void initializeStorage() { + if (storageFactory == null) { + return; // keeps non-CDI unit tests working + } + this.clusters = storageBacked("ecs-clusters.json", + new TypeReference>() {}); + this.taskDefinitions = storageBacked("ecs-task-definitions.json", + new TypeReference>() {}); + this.latestRevisions = storageBacked("ecs-latest-revisions.json", + new TypeReference>() {}); + this.services = storageBacked("ecs-services.json", + new TypeReference>() {}); + this.capacityProviders = storageBacked("ecs-capacity-providers.json", + new TypeReference>() {}); + this.attributes = storageBacked("ecs-attributes.json", + new TypeReference>>() {}); + this.accountSettings = storageBacked("ecs-account-settings.json", + new TypeReference>() {}); + } + + private Map storageBacked(String fileName, TypeReference> typeReference) { + return new StorageBackedMap<>(storageFactory.create("ecs", fileName, typeReference)); + } + + /** + * Re-persists a durable entity whose fields were mutated in place. {@link StorageBackedMap} + * only flags the backend dirty on {@code put}/{@code remove}, so an in-place mutation of a + * value obtained via {@code get}/{@code values()} must be written back to be captured by the + * periodic flush. The entry is located by ARN so callers don't need to recompute its key. + */ + private static void persistByArn(Map map, V value, Function arnFn) { + String arn = arnFn.apply(value); + if (arn == null) { + return; + } + for (Map.Entry entry : map.entrySet()) { + if (arn.equals(arnFn.apply(entry.getValue()))) { + map.put(entry.getKey(), value); + return; + } + } + } + + private void persistCluster(String region, EcsCluster cluster) { + if (cluster != null) { + clusters.put(clusterKey(region, cluster.getClusterName()), cluster); + } + } + @PreDestroy void shutdown() { reconciler.shutdownNow(); @@ -171,12 +228,14 @@ public class EcsService { if (settings != null) { cluster.setSettings(settings); } + persistCluster(region, cluster); return cluster; } public EcsCluster updateClusterSettings(String clusterRef, List settings, String region) { EcsCluster cluster = resolveClusterOrThrow(clusterRef, region); cluster.setSettings(settings); + persistCluster(region, cluster); return cluster; } @@ -185,6 +244,7 @@ public class EcsService { EcsCluster cluster = resolveClusterOrThrow(clusterRef, region); cluster.setCapacityProviders(providers); cluster.setDefaultCapacityProviderStrategy(defaultStrategy); + persistCluster(region, cluster); return cluster; } @@ -329,6 +389,7 @@ public class EcsService { public TaskDefinition deregisterTaskDefinition(String taskDefinitionRef, String region) { TaskDefinition td = resolveTaskDefinitionOrThrow(taskDefinitionRef, region); td.setStatus("INACTIVE"); + taskDefinitions.put(td.getFamily() + ":" + td.getRevision(), td); return td; } @@ -429,6 +490,7 @@ public class EcsService { launched.add(task); } + persistCluster(region, cluster); return launched; } @@ -465,6 +527,7 @@ public class EcsService { EcsCluster cluster = resolveClusterByArn(task.getClusterArn()); if (cluster != null && cluster.getRunningTasksCount() > 0) { cluster.setRunningTasksCount(cluster.getRunningTasksCount() - 1); + persistCluster(region, cluster); } LOG.infov("Stopped ECS task: {0}", task.getTaskArn()); @@ -604,6 +667,7 @@ public class EcsService { services.put(key, svc); cluster.setActiveServicesCount(cluster.getActiveServicesCount() + 1); + persistCluster(region, cluster); recordServiceDeployment(svc, taskDefinition, region); LOG.infov("Created ECS service: {0} in cluster {1}", serviceName, cluster.getClusterName()); return svc; @@ -639,6 +703,7 @@ public class EcsService { svc.setTaskDefinition(taskDefinition); recordServiceDeployment(svc, taskDefinition, region); } + services.put(key, svc); return svc; } @@ -662,6 +727,8 @@ public class EcsService { svc.setStatus("INACTIVE"); svc.setDesiredCount(0); cluster.setActiveServicesCount(Math.max(0, cluster.getActiveServicesCount() - 1)); + services.put(key, svc); + persistCluster(region, cluster); // Stop tasks before removing the service from the map, so the per-task // ELBv2 deregistration hook can still resolve the service's loadBalancers. tasks.values().stream() @@ -765,6 +832,7 @@ public class EcsService { else if (resource instanceof EcsServiceModel s) { s.getTags().putAll(tags); } else if (resource instanceof ContainerInstance ci) { ci.getTags().putAll(tags); } else if (resource instanceof CapacityProvider cp) { cp.getTags().putAll(tags); } + persistTaggedResource(resource); } private void removeTagsFromResource(Object resource, List tagKeys) { @@ -774,6 +842,19 @@ public class EcsService { else if (resource instanceof EcsServiceModel s) { tagKeys.forEach(s.getTags()::remove); } else if (resource instanceof ContainerInstance ci) { tagKeys.forEach(ci.getTags()::remove); } else if (resource instanceof CapacityProvider cp) { tagKeys.forEach(cp.getTags()::remove); } + persistTaggedResource(resource); + } + + /** Writes back a tag mutation to the owning durable map. Tasks and container instances are + * transient (not persisted), so they need no write-back. */ + private void persistTaggedResource(Object resource) { + switch (resource) { + case EcsCluster c -> persistByArn(clusters, c, EcsCluster::getClusterArn); + case TaskDefinition td -> persistByArn(taskDefinitions, td, TaskDefinition::getTaskDefinitionArn); + case EcsServiceModel s -> persistByArn(services, s, EcsServiceModel::getServiceArn); + case CapacityProvider cp -> persistByArn(capacityProviders, cp, CapacityProvider::getCapacityProviderArn); + default -> { /* EcsTask / ContainerInstance are transient */ } + } } private Map getTagsFromResource(Object resource) { @@ -821,6 +902,7 @@ public class EcsService { List existing = attributes.computeIfAbsent(targetId, k -> new ArrayList<>()); existing.removeIf(a -> a.name().equals(attr.name())); existing.add(attr); + attributes.put(targetId, existing); stored.add(attr); } return stored; @@ -839,6 +921,11 @@ public class EcsService { } return false; }); + if (existing.isEmpty()) { + attributes.remove(targetId); + } else { + attributes.put(targetId, existing); + } } } return deleted; @@ -876,6 +963,7 @@ public class EcsService { String key = containerInstanceKey(cluster.getClusterArn(), instanceArn); containerInstances.put(key, instance); cluster.setRegisteredContainerInstancesCount(cluster.getRegisteredContainerInstancesCount() + 1); + persistCluster(region, cluster); LOG.infov("Registered container instance: {0} in cluster {1}", instanceArn, cluster.getClusterName()); return instance; } @@ -901,6 +989,7 @@ public class EcsService { instance.setStatus("INACTIVE"); cluster.setRegisteredContainerInstancesCount( Math.max(0, cluster.getRegisteredContainerInstancesCount() - 1)); + persistCluster(region, cluster); return instance; } @@ -968,6 +1057,7 @@ public class EcsService { public CapacityProvider updateCapacityProvider(String name, Map asgProvider) { CapacityProvider cp = resolveCapacityProviderOrThrow(name); cp.setAutoScalingGroupProvider(asgProvider); + capacityProviders.put(cp.getName(), cp); return cp; } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index d769a32071..e2b80da20f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -98,6 +98,8 @@ floci: flush-interval-ms: 5000 s3vectors: flush-interval-ms: 5000 + ecs: + flush-interval-ms: 5000 rds: # Override storage mode for RDS (memory = no Docker volumes; hybrid/persistent = named volume per instance) # mode: memory diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServicePersistenceTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServicePersistenceTest.java new file mode 100644 index 0000000000..7e0a3c615e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/EcsServicePersistenceTest.java @@ -0,0 +1,136 @@ +package io.github.hectorvent.floci.services.ecs; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.ecs.container.EcsContainerManager; +import io.github.hectorvent.floci.services.ecs.model.Attribute; +import io.github.hectorvent.floci.services.ecs.model.ContainerDefinition; +import io.github.hectorvent.floci.services.ecs.model.EcsCluster; +import io.github.hectorvent.floci.services.ecs.model.EcsServiceModel; +import io.github.hectorvent.floci.services.ecs.model.LaunchType; +import io.github.hectorvent.floci.services.ecs.model.NetworkMode; +import io.github.hectorvent.floci.services.ecs.model.TaskDefinition; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Verifies that ECS durable resources survive a restart by backing the service maps with a + * {@link StorageFactory} whose backends are shared between two service instances (the second + * instance simulates a process restart reloading from the same persistent store). + */ +class EcsServicePersistenceTest { + + private static final String REGION = "us-west-2"; + + @Test + void durableResourcesAndTagsSurviveRestart() { + SharedStorageFactory storage = new SharedStorageFactory(); + + EcsService first = serviceWithStorage(storage); + EcsCluster cluster = first.createCluster("app-cluster", Map.of("owner", "platform"), REGION); + TaskDefinition td = first.registerTaskDefinition("web", List.of(container("app", "nginx:latest")), + NetworkMode.awsvpc, "256", "512", null, null, List.of("FARGATE"), + Map.of("tier", "web"), REGION); + EcsServiceModel svc = first.createService("app-cluster", "web-svc", td.getTaskDefinitionArn(), + 0, LaunchType.FARGATE, List.of(), null, Map.of("team", "payments"), REGION); + first.putAttributes("app-cluster", + List.of(new Attribute("stack", "prod", "container-instance", "ci-1")), REGION); + first.putAccountSetting("containerInsights", "enabled"); + + // Simulate restart: a fresh instance reloading from the same shared store. + EcsService reloaded = serviceWithStorage(storage); + + EcsCluster reloadedCluster = reloaded.describeClusters(List.of("app-cluster"), REGION).getFirst(); + assertEquals(cluster.getClusterArn(), reloadedCluster.getClusterArn()); + assertEquals("platform", reloadedCluster.getTags().get("owner")); + + TaskDefinition reloadedTd = reloaded.describeTaskDefinition(td.getTaskDefinitionArn(), REGION); + assertEquals("ACTIVE", reloadedTd.getStatus()); + assertEquals("web", reloadedTd.getTags().get("tier")); + + EcsServiceModel reloadedSvc = reloaded.describeServices("app-cluster", + List.of(svc.getServiceArn()), REGION).getFirst(); + assertEquals("payments", reloadedSvc.getTags().get("team")); + + assertEquals("prod", reloaded.listAttributes("app-cluster", null, "stack", null, REGION) + .getFirst().value()); + assertEquals("enabled", reloaded.listAccountSettings("containerInsights", null) + .getFirst().getValue()); + + // latestRevisions persisted: registering the same family again yields the next revision. + assertEquals(2, reloaded.registerTaskDefinition("web", List.of(container("app", "nginx:latest")), + NetworkMode.awsvpc, "256", "512", null, null, List.of("FARGATE"), null, REGION).getRevision()); + } + + @Test + void tagAndAttributeMutationsArePersistedAfterRestart() { + SharedStorageFactory storage = new SharedStorageFactory(); + + EcsService first = serviceWithStorage(storage); + EcsCluster cluster = first.createCluster("c1", Map.of(), REGION); + first.tagResource(cluster.getClusterArn(), Map.of("env", "test", "owner", "qa")); + first.untagResource(cluster.getClusterArn(), List.of("owner")); + first.putAttributes("c1", List.of(new Attribute("a", "1", "container-instance", "t1")), REGION); + first.deleteAttributes("c1", List.of(new Attribute("a", "1", "container-instance", "t1")), REGION); + + EcsService reloaded = serviceWithStorage(storage); + + Map tags = reloaded.listTagsForResource(cluster.getClusterArn()); + assertEquals("test", tags.get("env")); + assertFalse(tags.containsKey("owner"), "untagged key must not reappear after restart"); + assertTrue(reloaded.listAttributes("c1", null, "a", null, REGION).isEmpty(), + "deleted attribute must not reappear after restart"); + } + + private static ContainerDefinition container(String name, String image) { + ContainerDefinition cd = new ContainerDefinition(); + cd.setName(name); + cd.setImage(image); + return cd; + } + + private static EcsService serviceWithStorage(StorageFactory storage) { + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().ecs().mock()).thenReturn(true); + when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566"); + + EcsService service = new EcsService( + new RegionResolver(REGION, "000000000000"), + mock(EcsContainerManager.class), + config, + mock(EcsLoadBalancerRegistrar.class), + storage); + service.initializeStorage(); + return service; + } + + private static final class SharedStorageFactory extends StorageFactory { + private final Map> stores = new HashMap<>(); + + private SharedStorageFactory() { + super(null, null); + } + + @Override + @SuppressWarnings("unchecked") + public StorageBackend create(String serviceName, + String fileName, + TypeReference> typeReference) { + return (StorageBackend) stores.computeIfAbsent(fileName, ignored -> new InMemoryStorage<>()); + } + } +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 7124d4b133..f587203c38 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -42,6 +42,8 @@ floci: flush-interval-ms: 60000 s3vectors: flush-interval-ms: 60000 + ecs: + flush-interval-ms: 60000 cloudfront: auth: