diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index 742ed0073e..632cf53fb3 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -439,7 +439,7 @@ These services spawn Docker containers. They require access to the Docker socket |---|---|---| | `FLOCI_SERVICES_ECR_ENABLED` | `true` | Enable the ECR service | | `FLOCI_SERVICES_ECR_REGISTRY_IMAGE` | `registry:2` | Docker image for the ECR registry sidecar | -| `FLOCI_SERVICES_ECR_REGISTRY_CONTAINER_NAME` | `floci-ecr-registry` | Name of the ECR registry sidecar container | +| `FLOCI_SERVICES_ECR_REGISTRY_CONTAINER_NAME` | `floci-ecr-registry` | Name of the ECR registry sidecar container. The naming helper normalises it to `floci-aws-ecr-registry` | | `FLOCI_SERVICES_ECR_REGISTRY_BASE_PORT` | `5100` | First private loopback port for the backing registry | | `FLOCI_SERVICES_ECR_REGISTRY_MAX_PORT` | `5199` | Last private loopback port for the backing registry | | `FLOCI_SERVICES_ECR_TLS_ENABLED` | `false` | Enable TLS for the ECR registry | diff --git a/docs/configuration/ports.md b/docs/configuration/ports.md index 38f105986c..d25989b261 100644 --- a/docs/configuration/ports.md +++ b/docs/configuration/ports.md @@ -39,7 +39,7 @@ Floci tells the Docker daemon to start a sidecar/service container and bind its ``` host:9400 ←── opensearch container:9200 (Docker binds 9400 directly on the host) ↑ - Floci reaches it via Docker network: floci-opensearch-{name}:9200 + Floci reaches it via Docker network: floci-aws-opensearch-{name}:9200 ``` No `docker-compose.yml` `ports:` mapping is needed — the port is already on the host. @@ -76,7 +76,7 @@ redis-cli -h localhost -p 6379 When you create an EKS cluster in real mode, Floci asks the Docker daemon to start a k3s container and bind its API server port (6443) to the next available host port in `6500–6599`. The port is bound directly on the host by Docker — no `docker-compose.yml` mapping is needed. -The `endpoint` field returned by `DescribeCluster` points to `https://localhost:` when running outside a container, or `https://floci-eks-:6443` when Floci is running inside Docker. +The `endpoint` field returned by `DescribeCluster` points to `https://localhost:` when running outside a container, or `https://floci-aws-eks-:6443` when Floci is running inside Docker. ```bash aws eks create-cluster \ @@ -123,7 +123,7 @@ Configure the range with `FLOCI_SERVICES_LAMBDA_RUNTIME_API_BASE_PORT` and `FLOC When you create an OpenSearch domain in real mode, Floci asks the Docker daemon to start an `opensearchproject/opensearch` container and bind its REST port (9200) to the next available host port in `9400–9499`. The port is bound directly on the host by Docker — no `docker-compose.yml` mapping is needed. -The `endpoint` field returned by `DescribeDomain` points to `http://localhost:` when running outside a container, or `http://floci-opensearch-:9200` when Floci is running inside Docker. +The `endpoint` field returned by `DescribeDomain` points to `http://localhost:` when running outside a container, or `http://floci-aws-opensearch-:9200` when Floci is running inside Docker. ```bash aws opensearch create-domain \ diff --git a/docs/configuration/storage.md b/docs/configuration/storage.md index 88cb62d636..c97cf08367 100644 --- a/docs/configuration/storage.md +++ b/docs/configuration/storage.md @@ -67,7 +67,7 @@ When not set for a service, it inherits `FLOCI_STORAGE_MODE`. Only override when !!! note "RDS storage mode" `FLOCI_STORAGE_SERVICES_RDS_MODE` controls Floci's own metadata persistence for RDS, not the DB container volumes. In all modes, each DB instance or cluster gets a named Docker volume - (`floci-rds-{volumeId}`). In `memory` mode the volume is automatically removed when the + (`floci-aws-rds-{volumeId}`). In `memory` mode the volume is automatically removed when the instance is deleted. In other modes the volume is retained unless `FLOCI_STORAGE_PRUNE_VOLUMES_ON_DELETE=true`. @@ -142,12 +142,32 @@ Each resource gets a `volumeId` (a 6-character hex string, e.g. `a1b2c3`) genera time and stored in the resource model. The container name and volume name both use this suffix: ``` -floci-rds-a1b2c3 # RDS instance container and volume -floci-opensearch-b4c5d6 # OpenSearch domain container and volume -floci-msk-e7f8a9 # MSK cluster container and volume -floci-ecr-registry-data # ECR shared registry volume (singleton) +floci-aws-rds-a1b2c3 # RDS instance container and volume +floci-aws-opensearch-b4c5d6 # OpenSearch domain container and volume +floci-aws-msk-e7f8a9 # MSK cluster container and volume +floci-aws-ecr-registry-data # ECR shared registry volume (singleton) ``` +### Upgrading from a version that used the `floci-` prefix + +Floci once named containers and volumes `floci--`. Every Floci emulator shared that +prefix, so two of them on one Docker daemon could collide, and `docker volume prune --filter +label=floci=true` reached all of them at once. Each emulator now owns a prefix of its own, and this +one uses `floci-aws-`. + +**Existing data is not moved and not lost.** A resource created before the change keeps its +`floci-` volume, and Floci records that name so it keeps resolving to it across future upgrades. +Its container is recreated under the new prefix, which is safe because containers hold no data. +Volumes with no record to consult, such as the ECR registry and EFS file systems, are found by +checking for the old name first. + +Two consequences worth knowing: + +- **Scripts that name containers must be updated**, for example `docker exec floci-rds-`, and + so must anything resolving a container by DNS on the Docker network. +- **Downgrading does not migrate back.** A resource created after the upgrade records a + `floci-aws-` name that an older build will not look for. + Volumes are labelled `floci=true` so you can manage them with standard Docker commands: ```bash diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelper.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelper.java index ea30ac716c..33f0eceeae 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelper.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelper.java @@ -26,6 +26,25 @@ public final class ContainerStorageHelper { static final String CLOUD = "aws"; + /** + * The base name this emulator owns, without a trailing separator. Each Floci emulator takes + * its own cloud token so several can share one Docker daemon without colliding on names or + * on {@code docker volume prune} filters. + */ + public static final String NAME_PREFIX = "floci-" + CLOUD; + + /** {@link #NAME_PREFIX} with its separator, the literal prefix of every name produced here. */ + static final String CONTAINER_PREFIX = NAME_PREFIX + "-"; + + /** + * The prefix this emulator used before it took the shared {@code floci--} convention. + * + *

Frozen forever. It exists only so resources created by earlier versions, above all + * data-bearing Docker volumes, keep resolving to the names their data actually lives under. + * Never change it, and never use it to name a newly created resource. + */ + static final String LEGACY_PREFIX = "floci-"; + private ContainerStorageHelper() {} /** @@ -38,18 +57,59 @@ public final class ContainerStorageHelper { } public static String resourceName(EmulatorConfig config, String service, String volumeId, String fallbackId) { - return dockerName(config, "floci-" + service + "-" + (volumeId != null ? volumeId : fallbackId)); + return dockerName(config, service + "-" + (volumeId != null ? volumeId : fallbackId)); } + /** + * Prefixes {@code baseName} with {@code floci-aws-} and the configured resource namespace, + * which lands after the cloud token: {@code floci-aws--}. + * + *

A name that already carries a prefix, current or legacy, is normalised rather than + * prefixed a second time. That is what lets a container be named after the legacy volume it + * mounts: pre-migration volumes keep their old name forever, while their containers, which + * are disposable, always take the current one. + */ public static String dockerName(EmulatorConfig config, String baseName) { + return applyPrefix(CONTAINER_PREFIX, config, baseName); + } + + /** + * {@link #resourceName(EmulatorConfig, String, String, String)} as it behaved before the + * {@code floci-aws-} migration. Only for resolving resources an earlier version created; + * never for naming a new one. + */ + public static String legacyResourceName( + EmulatorConfig config, String service, String volumeId, String fallbackId) { + return legacyDockerName(config, service + "-" + (volumeId != null ? volumeId : fallbackId)); + } + + /** + * {@link #dockerName(EmulatorConfig, String)} as it behaved before the {@code floci-aws-} + * migration. Only for resolving resources an earlier version created; never for naming a + * new one. + */ + public static String legacyDockerName(EmulatorConfig config, String baseName) { + return applyPrefix(LEGACY_PREFIX, config, baseName); + } + + private static String applyPrefix(String prefix, EmulatorConfig config, String baseName) { + String base = stripPrefix(baseName); String namespace = resourceNamespace(config); - if (namespace.isBlank()) { - return baseName; + if (namespace.isBlank() || base.startsWith(namespace + "-")) { + return prefix + base; } - if (baseName.startsWith("floci-")) { - return "floci-" + namespace + "-" + baseName.substring("floci-".length()); + return prefix + namespace + "-" + base; + } + + /** Removes whichever prefix {@code baseName} carries, so prefixing is idempotent. */ + private static String stripPrefix(String baseName) { + if (baseName.startsWith(CONTAINER_PREFIX)) { + return baseName.substring(CONTAINER_PREFIX.length()); } - return "floci-" + namespace + "-" + baseName; + if (baseName.startsWith(LEGACY_PREFIX)) { + return baseName.substring(LEGACY_PREFIX.length()); + } + return baseName; } /** @@ -176,19 +236,22 @@ public final class ContainerStorageHelper { } /** - * Ensures the named volume exists and mounts it to {@code internalMount} in the container. - * Must only be called when {@link #isNamedVolumeMode} returns {@code true}. + * Removes a stale container by name, including one an earlier version left under the legacy + * prefix. + * + *

A container surviving from before the {@code floci-aws-} migration still holds its data + * volume's lock, so a start that only cleared the current name would fail to acquire it. Both + * names are cleared, and neither being present is fine. */ - public static void applyStorage( - ContainerBuilder.Builder builder, - ContainerLifecycleManager lifecycleManager, + public static void removeStaleContainer( EmulatorConfig config, - String service, - String volumeId, - String fallbackId, - String internalMount) { - String volumeName = resourceName(config, service, volumeId, fallbackId); - applyNamedVolume(builder, lifecycleManager, volumeName, internalMount); + ContainerLifecycleManager lifecycleManager, + String containerName) { + lifecycleManager.removeIfExists(containerName); + String legacyName = legacyDockerName(config, containerName); + if (!legacyName.equals(containerName)) { + lifecycleManager.removeIfExists(legacyName); + } } /** Mounts a persisted, exact Docker volume name without applying namespace rules again. */ diff --git a/src/main/java/io/github/hectorvent/floci/services/amazonmq/AmazonMqService.java b/src/main/java/io/github/hectorvent/floci/services/amazonmq/AmazonMqService.java index 6432c3a7fe..44e3436905 100644 --- a/src/main/java/io/github/hectorvent/floci/services/amazonmq/AmazonMqService.java +++ b/src/main/java/io/github/hectorvent/floci/services/amazonmq/AmazonMqService.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.amazonmq; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -128,6 +129,10 @@ public class AmazonMqService implements ResourceProvider { engineVersion, deploymentMode, params.hostInstanceType()); broker.setAccountId(accountId); broker.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF))); + // Stamp the volume name now, with the current prefix, so it is persisted rather than + // recomputed later. Only records predating this field fall back to the legacy name. + broker.setDockerVolumeName(ContainerStorageHelper.resourceName( + config, "amazonmq", broker.getVolumeId(), broker.getBrokerId())); broker.setPubliclyAccessible(params.publiclyAccessible()); broker.setAutoMinorVersionUpgrade(params.autoMinorVersionUpgrade()); if (params.users() != null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManager.java b/src/main/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManager.java index 0dffb2bea5..0d8aba002f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManager.java @@ -82,8 +82,8 @@ public class RabbitMqManager { } /** Deterministic container name for a broker, stable across emulator restarts. */ - private static String containerName(String brokerId) { - return "floci-amazonmq-" + brokerId; + private String containerName(String brokerId) { + return ContainerStorageHelper.dockerName(config, "amazonmq-" + brokerId); } public void startContainer(Broker broker) { @@ -93,7 +93,7 @@ public class RabbitMqManager { broker.getBrokerName(), image); // Remove any stale container with the same name (e.g. leftover from a crash). - lifecycleManager.removeIfExists(containerName); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName); ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image) .withName(containerName) @@ -135,9 +135,8 @@ public class RabbitMqManager { ContainerInfo info; try { if (ContainerStorageHelper.isNamedVolumeMode(config)) { - ContainerStorageHelper.applyStorage(specBuilder, lifecycleManager, config, - "amazonmq", broker.getVolumeId(), broker.getBrokerId(), - "/var/lib/rabbitmq"); + ContainerStorageHelper.applyNamedVolume(specBuilder, lifecycleManager, + resolveVolumeName(broker), "/var/lib/rabbitmq"); } else { String hostDataPath = ContainerStorageHelper.hostResourcePath(config, "amazonmq", broker.getBrokerId()) .toAbsolutePath().toString(); @@ -234,7 +233,7 @@ public class RabbitMqManager { // restart (it is intentionally not persisted; see Broker). Fall back to // the deterministic container name so an explicit DeleteBroker still // removes a container left running from a previous run. - lifecycleManager.removeIfExists(containerName(broker.getBrokerId())); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName(broker.getBrokerId())); } } @@ -280,7 +279,21 @@ public class RabbitMqManager { } public void removeBrokerStorage(Broker broker) { - ContainerStorageHelper.removeStorage(config, lifecycleManager, - "amazonmq", broker.getVolumeId(), broker.getBrokerId()); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, resolveVolumeName(broker)); } + + /** + * The broker's Docker volume name, backfilled once for records written before the field existed: + * those predate the {@code floci-aws-} migration, so their data is in the legacy-named volume + * and must keep resolving there. Never use the live helper here, which would strand that data + * under a freshly created volume. + */ + private String resolveVolumeName(Broker broker) { + if (broker.getDockerVolumeName() == null || broker.getDockerVolumeName().isBlank()) { + broker.setDockerVolumeName(ContainerStorageHelper.legacyResourceName( + config, "amazonmq", broker.getVolumeId(), broker.getBrokerId())); + } + return broker.getDockerVolumeName(); + } + } diff --git a/src/main/java/io/github/hectorvent/floci/services/amazonmq/model/Broker.java b/src/main/java/io/github/hectorvent/floci/services/amazonmq/model/Broker.java index 70e7214628..0d1fa1f2b9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/amazonmq/model/Broker.java +++ b/src/main/java/io/github/hectorvent/floci/services/amazonmq/model/Broker.java @@ -77,6 +77,13 @@ public class Broker { // removeBrokerStorage can locate the named volume after a restart). private String volumeId; + /** + * The Docker volume name. Stamped at creation with the current prefix; null on records + * written before this field existed, which are backfilled with the frozen legacy name so + * their data stays reachable. + */ + private String dockerVolumeName; + public Broker() {} public Broker(String brokerId, String brokerArn, String brokerName, @@ -143,4 +150,8 @@ public class Broker { public String getVolumeId() { return volumeId; } public void setVolumeId(String volumeId) { this.volumeId = volumeId; } + + public String getDockerVolumeName() { return dockerVolumeName; } + + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/batch/BatchDockerRunner.java b/src/main/java/io/github/hectorvent/floci/services/batch/BatchDockerRunner.java index 4fb502e960..70eb1ebf26 100644 --- a/src/main/java/io/github/hectorvent/floci/services/batch/BatchDockerRunner.java +++ b/src/main/java/io/github/hectorvent/floci/services/batch/BatchDockerRunner.java @@ -61,7 +61,7 @@ public class BatchDockerRunner implements ContainerTeardown { public BatchRunResult run(BatchJob job, int attemptNumber) { String logStreamName = logStreamer.generateLogStreamName( job.getJobDefinitionName() + "/default/" + job.getJobId()); - String containerName = ContainerStorageHelper.dockerName(config, "floci-batch-" + job.getJobId() + "-" + attemptNumber); + String containerName = ContainerStorageHelper.dockerName(config, "batch-" + job.getJobId() + "-" + attemptNumber); return runContainer(job, job.getJobId(), containerName, logStreamName, "batch:" + job.getJobName() + ":" + job.getJobId(), job.getContainerImage(), "Job definition container image is missing", @@ -73,7 +73,7 @@ public class BatchDockerRunner implements ContainerTeardown { String logStreamName = logStreamer.generateLogStreamName( job.getJobDefinitionName() + "/default/" + job.getJobId() + "/" + node.getNodeIndex()); String containerName = ContainerStorageHelper.dockerName(config, - "floci-batch-" + job.getJobId() + "-" + attemptNumber + "-node" + node.getNodeIndex()); + "batch-" + job.getJobId() + "-" + attemptNumber + "-node" + node.getNodeIndex()); String inFlightKey = job.getJobId() + "#node" + node.getNodeIndex(); return runContainer(job, inFlightKey, containerName, logStreamName, "batch:" + job.getJobName() + ":" + job.getJobId() + ":node" + node.getNodeIndex(), diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java index a7312d7221..5c8595f692 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/net/VpcNetworkManager.java @@ -858,7 +858,7 @@ public class VpcNetworkManager { String networkName(String region, String vpcId) { return ContainerStorageHelper.dockerName(config, - "floci-vpc-" + config.port() + "-" + region + "-" + vpcId); + "vpc-" + config.port() + "-" + region + "-" + vpcId); } private SubnetBinding subnetBinding(String region, String subnetId) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java index 2716993df1..acdb072161 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManager.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.docker.PortAllocator; import io.github.hectorvent.floci.services.ec2.model.Instance; import io.github.hectorvent.floci.services.ec2.model.IpPermission; @@ -156,8 +157,13 @@ public class Ec2PortForwardManager { int appPort = entry.getKey(); int hostPort = entry.getValue(); portAllocator.markReserved(hostPort); - String name = forwardContainerName(instance.getInstanceId(), appPort); - if (lifecycleManager.findByName(name).isEmpty()) { + // A surviving sidecar is adopted by name, so check the pre-migration name too: + // otherwise a forward created before the rename is recreated while the old container + // still holds its host port, and the new one cannot bind. + String name = forwardContainerName(config, instance.getInstanceId(), appPort); + if (lifecycleManager.findByName(name).isEmpty() + && lifecycleManager.findByName(legacyForwardContainerName( + config, instance.getInstanceId(), appPort)).isEmpty()) { LOG.infov("Recreating missing port-forward sidecar for EC2 instance {0} app port {1}", instance.getInstanceId(), appPort); if (!publishOn(instance, appPort, hostPort)) { @@ -193,8 +199,8 @@ public class Ec2PortForwardManager { return false; } - String name = forwardContainerName(instanceId, appPort); - lifecycleManager.removeIfExists(name); + String name = forwardContainerName(config, instanceId, appPort); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, name); ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(config.services().ec2().socatImage()) .withName(name) @@ -224,8 +230,8 @@ public class Ec2PortForwardManager { } void unpublish(Instance instance, int appPort) { - String name = forwardContainerName(instance.getInstanceId(), appPort); - lifecycleManager.removeIfExists(name); + String name = forwardContainerName(config, instance.getInstanceId(), appPort); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, name); Integer hostPort = instance.getPublishedPorts().remove(appPort); if (hostPort != null) { portAllocator.release(hostPort); @@ -247,8 +253,18 @@ public class Ec2PortForwardManager { } } - static String forwardContainerName(String instanceId, int appPort) { - return "floci-ec2-fwd-" + instanceId + "-" + appPort; + /** The unprefixed name token for a forward sidecar. */ + static String forwardContainerToken(String instanceId, int appPort) { + return "ec2-fwd-" + instanceId + "-" + appPort; + } + + static String forwardContainerName(EmulatorConfig config, String instanceId, int appPort) { + return ContainerStorageHelper.dockerName(config, forwardContainerToken(instanceId, appPort)); + } + + /** The name a pre-migration version gave the sidecar; only for finding or clearing survivors. */ + static String legacyForwardContainerName(EmulatorConfig config, String instanceId, int appPort) { + return ContainerStorageHelper.legacyDockerName(config, forwardContainerToken(instanceId, appPort)); } private NetworkTarget resolveInstanceTarget(Instance instance) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java index 5c69c22752..7e391d0d39 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManager.java @@ -51,13 +51,19 @@ public class EcrRegistryManager { private static final int CONTAINER_INTERNAL_PORT = 5000; private static final int REPOSITORY_DELETION_TIMEOUT_SECONDS = 10; private static final int REPOSITORY_DELETION_KILL_GRACE_SECONDS = 1; - private static final String NAMED_VOLUME = "floci-ecr-registry-data"; + private static final String NAMED_VOLUME = "ecr-registry-data"; private static final String REPOSITORIES_PATH = "/var/lib/registry/docker/registry/v2/repositories/"; /** Matches an AWS-shaped ECR image URI: {@code .dkr.ecr..amazonaws.com/[:tag]}. */ private static final java.util.regex.Pattern AWS_ECR_URI = java.util.regex.Pattern.compile("^([0-9]{12})\\.dkr\\.ecr\\.([a-z0-9-]+)\\.amazonaws\\.com/(.+)$"); + /** + * The container name actually in use. Differs from {@link #registryContainerName()} only when + * a legacy-named container surviving a pre-migration version was adopted. + */ + private volatile String activeContainerName; + private final ContainerBuilder containerBuilder; private final ContainerLifecycleManager lifecycleManager; private final ContainerLogStreamer logStreamer; @@ -196,7 +202,10 @@ public class EcrRegistryManager { * the container name plus the container-internal port (not the published host port). */ public String internalEndpoint() { - return "http://" + registryContainerName() + ":" + CONTAINER_INTERNAL_PORT; + // In-network clients reach the registry by container name, so this must be the name the + // container actually has: a legacy-named survivor keeps resolving through its own name. + String name = activeContainerName != null ? activeContainerName : registryContainerName(); + return "http://" + name + ":" + CONTAINER_INTERNAL_PORT; } /** Returns a {@link RegistryHttpClient} bound to the current registry endpoint. */ @@ -266,17 +275,27 @@ public class EcrRegistryManager { } String name = registryContainerName(); - // Check for existing container to adopt + // Check for existing container to adopt. The registry survives shutdown by design and is + // adopted BY NAME, so look up the pre-migration name too: otherwise an upgraded emulator + // orphans the old container while it still holds the registry host port and its data. var existing = lifecycleManager.findByName(name); + String adoptedName = name; + if (existing.isEmpty()) { + adoptedName = legacyRegistryContainerName(); + existing = lifecycleManager.findByName(adoptedName); + } if (existing.isPresent()) { if (hasLoopbackBinding(existing.get())) { + this.activeContainerName = adoptedName; adoptExisting(existing.get()); runReconcileOnce(); return; } - LOG.infov("Recreating ECR backing registry {0} with a loopback-only port binding", name); + // Recreated under the current name; the data volume is resolved by its own probe. + LOG.infov("Recreating ECR backing registry {0} with a loopback-only port binding", adoptedName); lifecycleManager.stopAndRemove(existing.get().getId(), null); } + this.activeContainerName = name; // Allocate port int chosenPort = portAllocator.allocate( @@ -332,9 +351,27 @@ public class EcrRegistryManager { return ContainerStorageHelper.dockerName(config, config.services().ecr().registryContainerName()); } + /** The name a pre-migration version gave this container; only for finding a survivor. */ + private String legacyRegistryContainerName() { + return ContainerStorageHelper.legacyDockerName(config, config.services().ecr().registryContainerName()); + } + + /** + * The registry's data volume. It is a singleton with no persisted-name record, so probe: one + * created before the {@code floci-aws-} migration keeps its legacy name, and every image + * pushed into it, forever. Only when no legacy volume exists is the current name used. + */ + private String registryVolumeName() { + String legacyName = ContainerStorageHelper.legacyDockerName(config, NAMED_VOLUME); + if (lifecycleManager.volumeExists(legacyName)) { + return legacyName; + } + return ContainerStorageHelper.dockerName(config, NAMED_VOLUME); + } + private void addPersistenceMounts(ContainerBuilder.Builder specBuilder, List env) { if (ContainerStorageHelper.isNamedVolumeMode(config)) { - String volumeName = ContainerStorageHelper.dockerName(config, NAMED_VOLUME); + String volumeName = registryVolumeName(); lifecycleManager.ensureVolume(volumeName); specBuilder.withNamedVolume(volumeName, "/var/lib/registry"); return; @@ -565,8 +602,7 @@ public class EcrRegistryManager { if (!shouldPruneStorage() || !ContainerStorageHelper.isNamedVolumeMode(config)) { return; } - ContainerStorageHelper.removeNamedVolume( - config, lifecycleManager, ContainerStorageHelper.dockerName(config, NAMED_VOLUME)); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, registryVolumeName()); } private boolean shouldPruneStorage() { diff --git a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java index d6c923072b..33f9c1c8c9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java @@ -236,7 +236,7 @@ public class EcsContainerManager { if (firelensRouter != null) { firelensConfig = firelensConfig(task, taskDef, firelensRouter, firelensLogOptions); firelensExternalConfig = s3ExternalConfig(firelensRouter); - firelensVolumeName = ContainerStorageHelper.dockerName(config, "floci-ecs-firelens-" + taskId); + firelensVolumeName = ContainerStorageHelper.dockerName(config, "ecs-firelens-" + taskId); lifecycleManager.ensureVolume(firelensVolumeName); firelensSocketAddress = unixSocketAddress(firelensVolumeName); } @@ -249,7 +249,7 @@ public class EcsContainerManager { try { for (ContainerDefinition def : launchOrder) { awaitDependencies(def, containerIds); - String containerName = ContainerStorageHelper.dockerName(config, "floci-ecs-" + taskId + "-" + def.getName()); + String containerName = ContainerStorageHelper.dockerName(config, "ecs-" + taskId + "-" + def.getName()); // RunTask containerOverrides matched by container name: command replaces // the task-def command; environment is merged over the task-def environment. @@ -1767,7 +1767,7 @@ public class EcsContainerManager { /** * Materialises an EFS-configured task volume as a shared local Docker named volume scoped to - * both the file system and the mount's effective root (see {@link #efsVolumeName}), then + * both the file system and the mount's effective root (see {@link #efsVolumeToken}), then * initialises the volume root's POSIX ownership to emulate the EFS access point's * RootDirectory.CreationInfo (no-op unless {@code floci.storage.efs} configures owner/permissions) * and applies the configured PosixUser emulation (uid[:gid] and/or supplementary group) so a @@ -1799,15 +1799,31 @@ public class EcsContainerManager { * AWS an access point's own root directory takes precedence over any {@code rootDirectory} * on the volume. */ - static String efsVolumeName(String fileSystemId, String accessPointId, String rootDirectory) { + static String efsVolumeToken(String fileSystemId, String accessPointId, String rootDirectory) { if (accessPointId != null && !accessPointId.isBlank()) { - return "floci-efs-" + fileSystemId + "-" + sha256Hex("accessPoint:" + accessPointId); + return "efs-" + fileSystemId + "-" + sha256Hex("accessPoint:" + accessPointId); } String normalizedRoot = normalizeRootDirectory(rootDirectory); if ("/".equals(normalizedRoot)) { - return "floci-efs-" + fileSystemId; + return "efs-" + fileSystemId; } - return "floci-efs-" + fileSystemId + "-" + sha256Hex(normalizedRoot); + return "efs-" + fileSystemId + "-" + sha256Hex(normalizedRoot); + } + + /** + * The volume name for {@link #efsVolumeToken}. EFS volumes hold user data but carry no + * persisted-name record, so probe: one created before the {@code floci-aws-} migration keeps + * its legacy name, and its data, forever. Only when no legacy volume exists is the current + * name used. The probe stays indefinitely; it is what makes upgrades across several versions + * safe. + */ + private String efsVolumeName(String fileSystemId, String accessPointId, String rootDirectory) { + String token = efsVolumeToken(fileSystemId, accessPointId, rootDirectory); + String legacyName = ContainerStorageHelper.legacyDockerName(config, token); + if (lifecycleManager.volumeExists(legacyName)) { + return legacyName; + } + return ContainerStorageHelper.dockerName(config, token); } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index ff1fa74dc1..4a838ea09c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -292,7 +292,7 @@ public class EksClusterManager { cluster.setHostPort(hostPort); // Remove any stale container - lifecycleManager.removeIfExists(containerName); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName); // k3s v1.34+ removed support for --kube-apiserver-arg=storage-backend and // --kube-apiserver-arg=etcd-servers. k3s now manages kine (embedded SQLite) @@ -791,9 +791,23 @@ public class EksClusterManager { */ private String resolveRestoredDockerName(Cluster cluster) { String qualified = accountQualifiedName(cluster); - String legacy = ContainerStorageHelper.resourceName(config, "eks", null, cluster.getName()); - if (legacy.equals(qualified)) { - return legacy; // default account: the names never diverged + + // A cluster created after account qualification but before the floci-aws- rename lives + // under the qualified name with the old prefix. Nothing else about it changed, so adopt + // it outright rather than putting it through the ownership check below. + String prefixLegacyQualified = ContainerStorageHelper.legacyDockerName(config, qualified); + if (!prefixLegacyQualified.equals(qualified) + && lifecycleManager.findByName(prefixLegacyQualified).isPresent()) { + LOG.infov("EKS cluster {0} keeps its pre-rename Docker name {1}", + cluster.getName(), prefixLegacyQualified); + return prefixLegacyQualified; + } + + // Clusters predating account qualification used the unqualified name, which also predates + // the rename, so it is resolved with the frozen legacy prefix. + String legacy = ContainerStorageHelper.legacyResourceName(config, "eks", null, cluster.getName()); + if (legacy.equals(ContainerStorageHelper.legacyDockerName(config, qualified))) { + return qualified; // default account: the names never diverged } var legacySurvivor = lifecycleManager.findByName(legacy); if (legacySurvivor.isPresent()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java b/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java index 0139355f31..db37bfe137 100644 --- a/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/floci/duck/FlociDuckManager.java @@ -32,7 +32,7 @@ import java.util.Optional; public class FlociDuckManager { private static final Logger LOG = Logger.getLogger(FlociDuckManager.class); - private static final String CONTAINER_NAME = "floci-duck"; + private static final String CONTAINER_NAME = "duck"; private static final int DUCK_PORT = 3000; private static final int HEALTH_POLL_MAX_MS = 30_000; private static final int HEALTH_POLL_INTERVAL_MS = 500; @@ -112,7 +112,7 @@ public class FlociDuckManager { LOG.infov("Starting floci-duck container using image {0}", image); String containerName = ContainerStorageHelper.dockerName(config, CONTAINER_NAME); - lifecycleManager.removeIfExists(containerName); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName); ContainerSpec spec = containerBuilder.newContainer(image) .withName(containerName) diff --git a/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java b/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java index a45cd898d3..5a1db31442 100644 --- a/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/floci/ui/FlociUiManager.java @@ -180,7 +180,14 @@ public class FlociUiManager { this.profile = resolveProfile(image); String name = ContainerStorageHelper.dockerName(config, config.services().ui().containerName()); + // The UI container survives shutdown by design and is adopted BY NAME, so look up the + // pre-migration name too. Otherwise an upgraded emulator orphans the old container + // while it still holds the fixed UI host port, and the new one cannot bind. Optional existing = lifecycleManager.findByName(name); + if (existing.isEmpty()) { + existing = lifecycleManager.findByName(ContainerStorageHelper.legacyDockerName( + config, config.services().ui().containerName())); + } if (existing.isPresent() && !replaceIfNotRunning(existing.get()) && !replaceIfEndpointDrifted(existing.get())) { diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java index d2fdd2dbc6..e8093f331e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.kinesisanalytics; import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; @@ -186,6 +187,10 @@ public class KinesisAnalyticsV2Service { FlinkApplication app = new FlinkApplication(applicationName, arn, runtimeEnvironment, serviceExecutionRole, mode); app.setAccountId(accountId); + // Stamp the savepoints volume name now, with the current prefix, so it is persisted + // rather than recomputed later. Only records predating this field fall back to the + // legacy name. + app.setDockerVolumeName(containerManager.savepointsVolumeName(app)); app.setApplicationDescription(applicationDescription); // AWS-faithful: a freshly created application is READY (not RUNNING); no container yet. app.setApplicationStatus(ApplicationStatus.READY); diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java index 6af369f407..e725012ff6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManager.java @@ -178,9 +178,8 @@ public class FlinkContainerManager { // Stop/StartApplication cycle — stopCluster() removes the JobManager container, but this // volume is only removed on DeleteApplication (removeSavepointsVolume), mirroring how other // Docker-backed services keep persistent data outside the container lifecycle. - ContainerStorageHelper.applyStorage(jmSpec, lifecycleManager, config, "kinesisanalytics", - runtimeKey(app) + "-savepoints", runtimeKey(app) + "-savepoints", - SAVEPOINTS_MOUNT); + ContainerStorageHelper.applyNamedVolume(jmSpec, lifecycleManager, + resolveVolumeName(app), SAVEPOINTS_MOUNT); if (!containerDetector.isRunningInContainer()) { jmSpec.withDynamicPort(JOBMANAGER_REST_PORT); } else { @@ -580,8 +579,35 @@ public class FlinkContainerManager { /** Removes the persistent savepoints volume. Called only on DeleteApplication, never on a plain * StopApplication (stopCluster), so snapshots survive a stop/restart cycle. */ public void removeSavepointsVolume(FlinkApplication app) { - ContainerStorageHelper.removeStorage(config, lifecycleManager, "kinesisanalytics", - runtimeKey(app) + "-savepoints", runtimeKey(app) + "-savepoints"); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, resolveVolumeName(app)); + } + + /** + * The savepoints volume name a newly created application is stamped with: the current + * prefix over the account-and-region scoped runtime key, so the name is persisted rather + * than recomputed later. + */ + public String savepointsVolumeName(FlinkApplication app) { + return ContainerStorageHelper.dockerName(config, savepointsVolumeToken(app)); + } + + /** The savepoints volume token; the name is this with a prefix applied. */ + private String savepointsVolumeToken(FlinkApplication app) { + return "kinesisanalytics-" + runtimeKey(app) + "-savepoints"; + } + + /** + * The savepoints volume name, backfilled once for applications created before the field + * existed: those predate the {@code floci-aws-} migration, so their savepoints are in the + * legacy-named volume and must keep resolving there. Never use the live helper here, which + * would strand them under a freshly created volume. + */ + private String resolveVolumeName(FlinkApplication app) { + if (app.getDockerVolumeName() == null || app.getDockerVolumeName().isBlank()) { + app.setDockerVolumeName( + ContainerStorageHelper.legacyDockerName(config, savepointsVolumeToken(app))); + } + return app.getDockerVolumeName(); } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java index 7480b4518f..9e6a4cef11 100644 --- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java +++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/model/FlinkApplication.java @@ -24,6 +24,17 @@ public class FlinkApplication { @JsonProperty("ApplicationName") private String applicationName; + /** + * The savepoints Docker volume name. Stamped at creation with the current prefix; null on + * records written before this field existed, which are backfilled with the frozen legacy + * name so their existing savepoints stay reachable. + */ + private String dockerVolumeName; + + public String getDockerVolumeName() { return dockerVolumeName; } + + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } + @JsonProperty("ApplicationARN") private String applicationArn; diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java index 87d230edad..24860dc925 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java @@ -71,8 +71,12 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { private static final String TASK_DIR = "/var/task"; private static final String RUNTIME_DIR = "/var/runtime"; - /** Default base prefix for the containers and code volumes Lambda spawns. */ - static final String DEFAULT_NAME_PREFIX = "floci"; + /** + * Default base prefix for the containers and code volumes Lambda spawns: the prefix this + * emulator owns, so it cannot collide with a sibling Floci emulator on the same daemon. + * A user-configured prefix still replaces it wholesale. + */ + static final String DEFAULT_NAME_PREFIX = ContainerStorageHelper.NAME_PREFIX; /** A prefix must be a legal Docker name on its own: names must start alphanumeric. */ private static final java.util.regex.Pattern SAFE_NAME_PREFIX = java.util.regex.Pattern.compile("^[A-Za-z0-9][A-Za-z0-9_.-]*$"); @@ -80,7 +84,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { /** * The base name prefix for Lambda containers and code volumes: * {@code floci.services.lambda.container-name-prefix} when set and Docker-safe, - * otherwise the default {@code floci}. + * otherwise {@link #DEFAULT_NAME_PREFIX}. */ static String resolveContainerNamePrefix(EmulatorConfig config) { String configured = config.services().lambda().containerNamePrefix() @@ -1133,13 +1137,33 @@ public class ContainerLauncher implements LambdaRuntimeLauncher { return namePrefix + "-code-" + fname + "-" + h; } - static String efsVolumeName(String accessPointArn) { + /** + * The unprefixed name token for an access point's volume. Includes a hash of the whole ARN so + * two access points sharing a resource id in different accounts or Regions stay distinct. + */ + static String efsVolumeToken(String accessPointArn) { int separator = Math.max(accessPointArn.lastIndexOf('/'), accessPointArn.lastIndexOf(':')); String resourceId = separator >= 0 ? accessPointArn.substring(separator + 1) : accessPointArn; if (resourceId.isBlank()) { throw new IllegalArgumentException("File system access point ARN must include a resource id"); } - return "floci-efs-" + resourceId + "-" + sha256Hex(accessPointArn); + return "efs-" + resourceId + "-" + sha256Hex(accessPointArn); + } + + /** + * The Docker volume backing an access point. EFS volumes hold user data but carry no + * persisted-name record, so probe: one created before the {@code floci-aws-} migration keeps + * its legacy name, and its data, forever. Only when no legacy volume exists is the current + * name used. The probe stays indefinitely; it is what makes upgrades across several versions + * safe. + */ + private String efsVolumeName(String accessPointArn) { + String token = efsVolumeToken(accessPointArn); + String legacyName = ContainerStorageHelper.legacyDockerName(config, token); + if (lifecycleManager.volumeExists(legacyName)) { + return legacyName; + } + return ContainerStorageHelper.dockerName(config, token); } private static String sha256Hex(String value) { diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java index 191320d6e1..0d6aebdd38 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.msk; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Pagination; @@ -134,6 +135,10 @@ public class MskService implements ResourceProvider { cluster.setAccountId(accountId); cluster.setResourceRegion(regionResolver.getRegion()); cluster.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF))); + // Stamp the volume name now, with the current prefix, so it is persisted rather than + // recomputed later. Only records predating this field fall back to the legacy name. + cluster.setDockerVolumeName(ContainerStorageHelper.resourceName( + config, "msk", cluster.getVolumeId(), cluster.getClusterName())); if (request.getNumberOfBrokerNodes() != null) { cluster.setNumberOfBrokerNodes(request.getNumberOfBrokerNodes()); @@ -345,6 +350,10 @@ public class MskService implements ResourceProvider { cluster.setAccountId(accountId); cluster.setResourceRegion(regionResolver.getRegion()); cluster.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF))); + // Stamp the volume name now, with the current prefix, so it is persisted rather than + // recomputed later. Only records predating this field fall back to the legacy name. + cluster.setDockerVolumeName(ContainerStorageHelper.resourceName( + config, "msk", cluster.getVolumeId(), cluster.getClusterName())); // Provisioned-only members must not surface on a serverless cluster. cluster.setNumberOfBrokerNodes(0); diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java b/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java index 605e8e0e29..15640291ab 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/RedpandaManager.java @@ -114,7 +114,7 @@ public class RedpandaManager { String containerName = ContainerStorageHelper.resourceName(config, "msk", cluster.getVolumeId(), cluster.getClusterName()); // Cleanup stale container - lifecycleManager.removeIfExists(containerName); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName); // Redpanda embeds broker addresses in Kafka protocol responses (Metadata, // FindCoordinator, ...) and clients reconnect directly to whatever address @@ -163,9 +163,8 @@ public class RedpandaManager { // Handle persistence mounting if (ContainerStorageHelper.isNamedVolumeMode(config)) { - ContainerStorageHelper.applyStorage(specBuilder, lifecycleManager, config, - "msk", cluster.getVolumeId(), cluster.getClusterName(), - "/var/lib/redpanda/data"); + ContainerStorageHelper.applyNamedVolume(specBuilder, lifecycleManager, + resolveVolumeName(cluster), "/var/lib/redpanda/data"); } else { // Legacy host-path mode: host-persistent-path is an absolute path String hostDataPath = legacyCompatibleHostPath(cluster).toAbsolutePath().toString(); @@ -280,8 +279,7 @@ public class RedpandaManager { } public void removeClusterStorage(MskCluster cluster) { - ContainerStorageHelper.removeStorage(config, lifecycleManager, - "msk", cluster.getVolumeId(), cluster.getClusterName()); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, resolveVolumeName(cluster)); } private String clusterIdentityKey(MskCluster cluster) { @@ -302,4 +300,19 @@ public class RedpandaManager { ? legacyPath : scopedPath; } + + /** + * The cluster's Docker volume name, backfilled once for records written before the field existed: + * those predate the {@code floci-aws-} migration, so their data is in the legacy-named volume + * and must keep resolving there. Never use the live helper here, which would strand that data + * under a freshly created volume. + */ + private String resolveVolumeName(MskCluster cluster) { + if (cluster.getDockerVolumeName() == null || cluster.getDockerVolumeName().isBlank()) { + cluster.setDockerVolumeName(ContainerStorageHelper.legacyResourceName( + config, "msk", cluster.getVolumeId(), cluster.getClusterName())); + } + return cluster.getDockerVolumeName(); + } + } diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java b/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java index 79dc645081..b183eaf00e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java @@ -105,6 +105,13 @@ public class MskCluster { @JsonProperty("volumeId") private String volumeId; + /** + * The Docker volume name. Stamped at creation with the current prefix; null on records + * written before this field existed, which are backfilled with the frozen legacy name so + * their data stays reachable. + */ + private String dockerVolumeName; + public MskCluster() {} public MskCluster(String clusterArn, String clusterName, String kafkaVersion) { @@ -189,4 +196,8 @@ public class MskCluster { public String getVolumeId() { return volumeId; } public void setVolumeId(String volumeId) { this.volumeId = volumeId; } + + public String getDockerVolumeName() { return dockerVolumeName; } + + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java index 51dec997d9..1709a0dfef 100644 --- a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManager.java @@ -377,14 +377,14 @@ public class MwaaEnvironmentManager { /** Routed through {@link ContainerStorageHelper} so multiple Floci instances sharing one Docker * daemon (via {@code FLOCI_DOCKER_RESOURCE_NAMESPACE}) don't collide, same as every other * Docker-backed service (EKS, RDS, ...). {@code config} may be {@code null} — the helper treats - * that as "no namespace configured" and returns the base name unchanged. */ + * that as "no namespace configured" and applies only the {@code floci-aws-} prefix. */ static String dbContainerName(EmulatorConfig config, Environment environment) { - return ContainerStorageHelper.dockerName(config, "floci-mwaa-" + environmentIdentity(environment) + "-db"); + return ContainerStorageHelper.dockerName(config, "mwaa-" + environmentIdentity(environment) + "-db"); } static String airflowContainerName(EmulatorConfig config, Environment environment) { return ContainerStorageHelper.dockerName(config, - "floci-mwaa-" + environmentIdentity(environment) + "-airflow"); + "mwaa-" + environmentIdentity(environment) + "-airflow"); } private static String environmentIdentity(Environment environment) { diff --git a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java index ef2d125184..c20d68582a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainManager.java @@ -103,7 +103,7 @@ public class OpenSearchDomainManager { LOG.infov("Starting OpenSearch container for domain: {0} (version={1}, image={2})", domain.getDomainName(), domain.getEngineVersion(), image); - lifecycleManager.removeIfExists(containerName); + ContainerStorageHelper.removeStaleContainer(config, lifecycleManager, containerName); // A restart of an existing domain has just removed the old container, so its // reservation is stale; hand the port back before allocating a fresh one. @@ -143,9 +143,8 @@ public class OpenSearchDomainManager { applyEngineEnv(specBuilder, domain.getEngineVersion()); if (ContainerStorageHelper.isNamedVolumeMode(config)) { - ContainerStorageHelper.applyStorage(specBuilder, lifecycleManager, config, - "opensearch", domain.getVolumeId(), domain.getDomainName(), - "/usr/share/opensearch/data"); + ContainerStorageHelper.applyNamedVolume(specBuilder, lifecycleManager, + resolveVolumeName(domain), "/usr/share/opensearch/data"); } else { // Legacy host-path mode: host-persistent-path is an absolute path Path dataPath = ContainerStorageHelper.hostResourcePath(config, "opensearch", domain.getDomainName()); @@ -218,8 +217,21 @@ public class OpenSearchDomainManager { } public void removeDomainStorage(Domain domain) { - ContainerStorageHelper.removeStorage(config, lifecycleManager, - "opensearch", domain.getVolumeId(), domain.getDomainName()); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, resolveVolumeName(domain)); + } + + /** + * The domain's Docker volume name, backfilled once for records written before the field + * existed: those predate the {@code floci-aws-} migration, so their data is in the + * legacy-named volume and must keep resolving there. Never use the live helper here, which + * would strand that data under a freshly created volume. + */ + private String resolveVolumeName(Domain domain) { + if (domain.getDockerVolumeName() == null || domain.getDockerVolumeName().isBlank()) { + domain.setDockerVolumeName(ContainerStorageHelper.legacyResourceName( + config, "opensearch", domain.getVolumeId(), domain.getDomainName())); + } + return domain.getDockerVolumeName(); } private String resolveImage(String engineVersion) { diff --git a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java index e39fcae6d1..e3ab611d4c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java +++ b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.opensearch; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -130,6 +131,10 @@ public class OpenSearchService implements ResourceProvider { domain.setEndpoint(""); domain.setCreatedAt(Instant.now()); domain.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF))); + // Stamp the volume name now, with the current prefix, so it is persisted rather than + // recomputed later. Only records predating this field fall back to the legacy name. + domain.setDockerVolumeName(ContainerStorageHelper.resourceName( + config, "opensearch", domain.getVolumeId(), domain.getDomainName())); if (clusterConfig != null) { domain.setClusterConfig(clusterConfig); diff --git a/src/main/java/io/github/hectorvent/floci/services/opensearch/model/Domain.java b/src/main/java/io/github/hectorvent/floci/services/opensearch/model/Domain.java index 9bf40913e5..0579c2f10d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/opensearch/model/Domain.java +++ b/src/main/java/io/github/hectorvent/floci/services/opensearch/model/Domain.java @@ -74,6 +74,14 @@ public class Domain { @JsonProperty("VolumeId") private String volumeId; + /** + * The domain's Docker volume name. Stamped at creation with the current prefix; null on + * records written before this field existed, which are backfilled with the frozen legacy + * name so their data stays reachable. + */ + @JsonProperty("DockerVolumeName") + private String dockerVolumeName; + @JsonProperty("CreatedAt") @JsonFormat(shape = JsonFormat.Shape.NUMBER) private Instant createdAt; @@ -232,6 +240,14 @@ public class Domain { this.volumeId = volumeId; } + public String getDockerVolumeName() { + return dockerVolumeName; + } + + public void setDockerVolumeName(String dockerVolumeName) { + this.dockerVolumeName = dockerVolumeName; + } + public Instant getCreatedAt() { return createdAt; } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java index 4fd139b6ac..7424bbbcce 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java @@ -695,7 +695,9 @@ public class RdsService implements Resettable, ResourceProvider { // bogus volume name that a later non-mock restore could try to reference. instanceDockerVolumeName = cluster.getDockerVolumeName() != null ? cluster.getDockerVolumeName() - : volumeName(cluster.getVolumeId(), + // No persisted name means a record written before that field, so its + // data is under the legacy-prefixed volume. + : legacyVolumeName(cluster.getVolumeId(), resolvedClusterStorageResourceId(cluster)); } instanceStorageResourceId = resolvedClusterStorageResourceId(cluster); @@ -8329,10 +8331,31 @@ public class RdsService implements Resettable, ResourceProvider { return subnets.stream().map(Subnet::getSubnetId).toList(); } + /** Volume name for a newly created resource: always the current {@code floci-aws-} prefix. */ private String volumeName(String volumeId, String fallbackId) { return ContainerStorageHelper.resourceName(config, "rds", volumeId, fallbackId); } + /** + * Volume name a pre-migration version would have produced, using the frozen legacy prefix. + * + *

Only for backfilling {@code dockerVolumeName} on records persisted before that field, or + * before the {@code floci-aws-} migration: their data lives in the legacy-named volume. Never + * use the live helper for a backfill, which would silently orphan that data under a freshly + * created volume. This backfill stays in the code indefinitely; it is what makes upgrading + * across several versions safe. + */ + private String legacyVolumeName(String volumeId, String fallbackId) { + return ContainerStorageHelper.legacyResourceName(config, "rds", volumeId, fallbackId); + } + + /** {@link #newVolumeName} in its pre-migration shape, for the same backfill reason. */ + private String legacyNewVolumeName(String volumeId, String storageResourceId) { + String qualifiedVolumeId = volumeId == null || volumeId.isBlank() + ? null : storageResourceId + "-" + volumeId; + return legacyVolumeName(qualifiedVolumeId, storageResourceId); + } + private String newVolumeName(String volumeId, String storageResourceId) { String qualifiedVolumeId = volumeId == null || volumeId.isBlank() ? null : storageResourceId + "-" + volumeId; @@ -8352,10 +8375,10 @@ public class RdsService implements Resettable, ResourceProvider { } if (instance.getContainerStorageResourceId() != null && !instance.getContainerStorageResourceId().isBlank()) { - return newVolumeName( + return legacyNewVolumeName( instance.getVolumeId(), instance.getContainerStorageResourceId()); } - return volumeName(instance.getVolumeId(), instance.getDbInstanceIdentifier()); + return legacyVolumeName(instance.getVolumeId(), instance.getDbInstanceIdentifier()); } private String resolvedClusterStorageResourceId(DbCluster cluster) { @@ -8371,10 +8394,10 @@ public class RdsService implements Resettable, ResourceProvider { } if (cluster.getContainerStorageResourceId() != null && !cluster.getContainerStorageResourceId().isBlank()) { - return newVolumeName( + return legacyNewVolumeName( cluster.getVolumeId(), cluster.getContainerStorageResourceId()); } - return volumeName(cluster.getVolumeId(), cluster.getDbClusterIdentifier()); + return legacyVolumeName(cluster.getVolumeId(), cluster.getDbClusterIdentifier()); } private static String firstNonBlank(String value, String fallback) { diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java index 99518b223c..bbb160fee4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManager.java @@ -160,7 +160,11 @@ public class RdsContainerManager implements RdsBackendGate, Resettable { if (isDockerReachable()) { throw e; } - discardNeverCreatedIdentity(effectiveRuntimeId, dockerVolumeName); + // The retained identity names the container, which is the normalised form of the + // volume name: a pre-migration volume keeps its legacy name while its container takes + // the current prefix, so the two are no longer the same string. + discardNeverCreatedIdentity(effectiveRuntimeId, + ContainerStorageHelper.dockerName(config, dockerVolumeName)); if (!dockerUnavailableLogged) { dockerUnavailableLogged = true; LOG.warnv("No Docker daemon is reachable from Floci ({0}). RDS metadata operations " @@ -222,7 +226,10 @@ public class RdsContainerManager implements RdsBackendGate, Resettable { String exactVolumeName = requireSafeStorageComponent( dockerVolumeName, "Docker volume name"); int enginePort = engine.defaultPort(); - String containerName = exactVolumeName; + // The volume keeps whatever name it was persisted under, including a legacy one, because + // it holds the data. The container is disposable, so it always takes the current prefix; + // dockerName normalises an already-prefixed input, which is what makes that possible. + String containerName = ContainerStorageHelper.dockerName(config, exactVolumeName); String storageKey = storageKey(storageResourceId, exactVolumeName); String containerKey = "container:" + containerName; if (!claimedRuntimes.add(effectiveRuntimeId)) { @@ -245,8 +252,15 @@ public class RdsContainerManager implements RdsBackendGate, Resettable { String cleanupContainerId = containerName; RdsContainerHandle handle = null; try { - // Remove any stale container with the same name only after storage ownership is secured. + // Remove any stale container with the same name only after storage ownership is + // secured. The legacy-named twin must go too: a container left by a pre-migration + // version still holds this volume's engine lock, and failing to clear it must abort + // the start rather than surface later as a corrupt-looking database. lifecycleManager.removeIfExistsStrict(containerName); + String legacyContainerName = ContainerStorageHelper.legacyDockerName(config, exactVolumeName); + if (!legacyContainerName.equals(containerName)) { + lifecycleManager.removeIfExistsStrict(legacyContainerName); + } cleanupContainerId = null; // Build environment variables diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java index 8e67a8b4b7..601ef5bd39 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java @@ -13,6 +13,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.C import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo; import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.services.redshift.model.Cluster; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; @@ -162,8 +163,8 @@ public class RedshiftContainerManager { return accountId + "/" + clusterIdentifier; } - private static String containerName(String accountId, String clusterIdentifier) { - return "floci-redshift-" + accountId + "-" + clusterIdentifier; + private String containerName(String accountId, String clusterIdentifier) { + return ContainerStorageHelper.dockerName(config, "redshift-" + accountId + "-" + clusterIdentifier); } public void takeSnapshot(String accountId, String clusterIdentifier, String username, String dbname, Path outputFile) { diff --git a/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerEndpointManager.java b/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerEndpointManager.java index 647e65346b..bc53f72521 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerEndpointManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerEndpointManager.java @@ -82,7 +82,7 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable { if (image == null) { throw new IllegalArgumentException("PrimaryContainer.Image is required"); } - String name = ContainerStorageHelper.dockerName(config, "floci-sagemaker-endpoint-" + endpoint.endpointName); + String name = ContainerStorageHelper.dockerName(config, "sagemaker-endpoint-" + endpoint.endpointName); lifecycleManager.removeIfExists(name); ContainerBuilder.Builder builder = containerBuilder.newContainer(image) .withName(name) diff --git a/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerTrainingRunner.java b/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerTrainingRunner.java index b7b696fee7..3eea09ffec 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerTrainingRunner.java +++ b/src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerTrainingRunner.java @@ -88,7 +88,7 @@ public class SageMakerTrainingRunner implements ContainerTeardown, Resettable { Closeable logs = null; try { String image = SageMakerEndpointManager.string(job.algorithmSpecification.get("TrainingImage")); - String name = ContainerStorageHelper.dockerName(config, "floci-sagemaker-training-" + job.trainingJobName); + String name = ContainerStorageHelper.dockerName(config, "sagemaker-training-" + job.trainingJobName); lifecycleManager.removeIfExists(name); ContainerBuilder.Builder builder = containerBuilder.newContainer(image) .withName(name) diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java index 056e354b6d..f749249df7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java +++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbService.java @@ -177,6 +177,7 @@ public class TimestreamInfluxDbService implements Resettable { String accountId = regionResolver.getAccountId(); DbInstance instance = new DbInstance(); instance.setId(newId()); + instance.setDockerVolumeName(containerManager.volumeName(instance.getId())); instance.setName(name); instance.setArn(arn(region, "db-instance", instance.getId())); instance.setAccountId(accountId); @@ -214,7 +215,7 @@ public class TimestreamInfluxDbService implements Resettable { } else { instance.setStatus("CREATING"); instances.put(key(region, instance.getId()), instance); - provision(DB_INSTANCE, accountId, region, instance.getId(), setup, engineEnvironment(group), null, + provision(DB_INSTANCE, accountId, region, instance.getId(), resolveVolumeName(instance), setup, engineEnvironment(group), null, "FAILED"); } return new Reported<>(instance, "CREATING"); @@ -275,7 +276,7 @@ public class TimestreamInfluxDbService implements Resettable { if (parameterGroupChanged && hasDataPlane(instance.getContainerId())) { instance.setStatus(status); instances.put(key(region, instance.getId()), instance); - provision(DB_INSTANCE, instance.getAccountId(), region, instance.getId(), null, engineEnvironment(group), + provision(DB_INSTANCE, instance.getAccountId(), region, instance.getId(), resolveVolumeName(instance), null, engineEnvironment(group), null, "FAILED"); } else { instance.setStatus(AVAILABLE); @@ -306,7 +307,7 @@ public class TimestreamInfluxDbService implements Resettable { + " belongs to DB cluster " + instance.getDbClusterId() + "; nodes can't be removed individually, delete the DB cluster instead."); } - removeDataPlane(instance.getId(), instance.getContainerId()); + removeDataPlane(instance.getId(), instance.getContainerId(), resolveVolumeName(instance)); instances.delete(key(region, instance.getId())); return new Reported<>(instance, "DELETING"); } @@ -351,6 +352,7 @@ public class TimestreamInfluxDbService implements Resettable { String accountId = regionResolver.getAccountId(); DbCluster cluster = new DbCluster(); cluster.setId(newId()); + cluster.setDockerVolumeName(containerManager.volumeName(cluster.getId())); cluster.setName(name); cluster.setArn(arn(region, "db-cluster", cluster.getId())); cluster.setAccountId(accountId); @@ -400,7 +402,7 @@ public class TimestreamInfluxDbService implements Resettable { instances.put(key(region, member.getId()), member); } if (containerBacked) { - provision(DB_CLUSTER, accountId, region, cluster.getId(), setup, engineEnvironment(group), null, "FAILED"); + provision(DB_CLUSTER, accountId, region, cluster.getId(), resolveVolumeName(cluster), setup, engineEnvironment(group), null, "FAILED"); } return new Reported<>(cluster, "CREATING"); } @@ -463,7 +465,7 @@ public class TimestreamInfluxDbService implements Resettable { instances.put(key(region, member.getId()), member); } if (recreate) { - provision(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(), null, engineEnvironment(group), + provision(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(), resolveVolumeName(cluster), null, engineEnvironment(group), null, "FAILED"); } return new Reported<>(cluster, status); @@ -493,7 +495,7 @@ public class TimestreamInfluxDbService implements Resettable { public synchronized Reported deleteDbCluster(JsonNode request, String region) { DbCluster cluster = requireCluster(region, clusterIdentifier(request)); TimestreamInfluxDbValidation.optionalBoolean(request, "retainAutomatedBackups"); - removeDataPlane(cluster.getId(), cluster.getContainerId()); + removeDataPlane(cluster.getId(), cluster.getContainerId(), resolveVolumeName(cluster)); for (DbInstance member : clusterMembers(region, cluster.getId())) { instances.delete(key(region, member.getId())); } @@ -670,6 +672,7 @@ public class TimestreamInfluxDbService implements Resettable { requireUniqueClusterName(region, name); DbCluster cluster = new DbCluster(); cluster.setId(newId()); + cluster.setDockerVolumeName(containerManager.volumeName(cluster.getId())); cluster.setName(name); cluster.setArn(arn(region, "db-cluster", cluster.getId())); cluster.setAccountId(backup.getAccountId()); @@ -706,7 +709,7 @@ public class TimestreamInfluxDbService implements Resettable { instances.put(key(region, member.getId()), member); } if (restoreData) { - provision(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(), temporarySetup(), + provision(DB_CLUSTER, cluster.getAccountId(), region, cluster.getId(), resolveVolumeName(cluster), temporarySetup(), engineEnvironment(group), backup.getId(), "RESTORE_FAILED"); } return new RestoreResult(cluster.getId(), DB_CLUSTER, cluster.getEngineType(), cluster.getDeploymentType()); @@ -715,6 +718,7 @@ public class TimestreamInfluxDbService implements Resettable { requireUniqueInstanceName(region, name); DbInstance instance = new DbInstance(); instance.setId(newId()); + instance.setDockerVolumeName(containerManager.volumeName(instance.getId())); instance.setName(name); instance.setArn(arn(region, "db-instance", instance.getId())); instance.setAccountId(backup.getAccountId()); @@ -749,7 +753,7 @@ public class TimestreamInfluxDbService implements Resettable { if (restoreData) { DbParameterGroup group = parameterGroups.get(key(region, orDefault(backup.getDbParameterGroupId(), ""))) .orElse(null); - provision(DB_INSTANCE, instance.getAccountId(), region, instance.getId(), temporarySetup(), + provision(DB_INSTANCE, instance.getAccountId(), region, instance.getId(), resolveVolumeName(instance), temporarySetup(), engineEnvironment(group), backup.getId(), "RESTORE_FAILED"); } return new RestoreResult(instance.getId(), DB_INSTANCE, instance.getEngineType(), instance.getDeploymentType()); @@ -791,7 +795,7 @@ public class TimestreamInfluxDbService implements Resettable { if (instance.getContainerId() != null && instance.getDbClusterId() == null) { DbParameterGroup group = parameterGroup(instance.getAccountId(), instance.getRegion(), instance.getDbParameterGroupIdentifier()); - provision(DB_INSTANCE, instance.getAccountId(), instance.getRegion(), instance.getId(), null, + provision(DB_INSTANCE, instance.getAccountId(), instance.getRegion(), instance.getId(), resolveVolumeName(instance), null, engineEnvironment(group), null, "FAILED"); } } @@ -799,7 +803,7 @@ public class TimestreamInfluxDbService implements Resettable { if (cluster.getContainerId() != null) { DbParameterGroup group = parameterGroup(cluster.getAccountId(), cluster.getRegion(), cluster.getDbParameterGroupIdentifier()); - provision(DB_CLUSTER, cluster.getAccountId(), cluster.getRegion(), cluster.getId(), null, + provision(DB_CLUSTER, cluster.getAccountId(), cluster.getRegion(), cluster.getId(), resolveVolumeName(cluster), null, engineEnvironment(group), null, "FAILED"); } } @@ -821,26 +825,26 @@ public class TimestreamInfluxDbService implements Resettable { } private void provision(String resourceType, String accountId, String region, String resourceId, - InfluxDbSetup setup, Map engineEnvironment, String backupId, - String failureStatus) { + String dataVolumeName, InfluxDbSetup setup, Map engineEnvironment, + String backupId, String failureStatus) { executor.execute(() -> { InfluxDbEndpoint endpoint = null; try { if (!containerManager.isDockerReachable()) { LOG.warnv("No Docker daemon is reachable; {0} {1} is available without an InfluxDB container", resourceType, resourceId); - applyDataPlane(resourceType, accountId, region, resourceId, null, AVAILABLE); + applyDataPlane(resourceType, accountId, region, resourceId, dataVolumeName, null, AVAILABLE); return; } - endpoint = containerManager.start(resourceId, accountId, region, setup, engineEnvironment); + endpoint = containerManager.start(resourceId, dataVolumeName, accountId, region, setup, engineEnvironment); containerManager.waitUntilReady(endpoint); if (backupId != null) { containerManager.restore(endpoint.containerId(), backupId); } - applyDataPlane(resourceType, accountId, region, resourceId, endpoint, AVAILABLE); + applyDataPlane(resourceType, accountId, region, resourceId, dataVolumeName, endpoint, AVAILABLE); } catch (RuntimeException e) { LOG.errorv(e, "Failed to provision the InfluxDB container for {0} {1}", resourceType, resourceId); - applyDataPlane(resourceType, accountId, region, resourceId, endpoint, failureStatus); + applyDataPlane(resourceType, accountId, region, resourceId, dataVolumeName, endpoint, failureStatus); } }); } @@ -851,10 +855,10 @@ public class TimestreamInfluxDbService implements Resettable { try { containerManager.restart(endpoint.containerId()); containerManager.waitUntilReady(endpoint); - applyDataPlane(resourceType, accountId, region, resourceId, endpoint, AVAILABLE); + applyDataPlane(resourceType, accountId, region, resourceId, null, endpoint, AVAILABLE); } catch (RuntimeException e) { LOG.errorv(e, "Failed to reboot the InfluxDB container for {0} {1}", resourceType, resourceId); - applyDataPlane(resourceType, accountId, region, resourceId, endpoint, "REBOOT_FAILED"); + applyDataPlane(resourceType, accountId, region, resourceId, null, endpoint, "REBOOT_FAILED"); } }); } @@ -884,13 +888,18 @@ public class TimestreamInfluxDbService implements Resettable { }); } + /** + * {@code dataVolumeName} is the volume the creating flow mounted, so an orphaned container + * (its record vanished meanwhile) takes its fresh volumes with it; reboots and restores pass + * null because a record deleted under them already removed its own volumes. + */ private synchronized void applyDataPlane(String resourceType, String accountId, String region, String resourceId, - InfluxDbEndpoint endpoint, String status) { + String dataVolumeName, InfluxDbEndpoint endpoint, String status) { String host = endpoint != null ? endpoint.host() : LOCAL_ENDPOINT; if (DB_INSTANCE.equals(resourceType)) { Optional stored = instances.getForAccount(accountId, key(region, resourceId)); if (stored.isEmpty()) { - discardOrphan(resourceId, endpoint); + discardOrphan(resourceId, dataVolumeName, endpoint); return; } DbInstance instance = stored.get(); @@ -905,7 +914,7 @@ public class TimestreamInfluxDbService implements Resettable { } Optional stored = clusters.getForAccount(accountId, key(region, resourceId)); if (stored.isEmpty()) { - discardOrphan(resourceId, endpoint); + discardOrphan(resourceId, dataVolumeName, endpoint); return; } DbCluster cluster = stored.get(); @@ -927,19 +936,41 @@ public class TimestreamInfluxDbService implements Resettable { } } - private void discardOrphan(String resourceId, InfluxDbEndpoint endpoint) { + private void discardOrphan(String resourceId, String dataVolumeName, InfluxDbEndpoint endpoint) { if (endpoint != null) { - removeDataPlane(resourceId, endpoint.containerId()); + removeDataPlane(resourceId, endpoint.containerId(), dataVolumeName); } } - private void removeDataPlane(String resourceId, String containerId) { + /** + * The record's data volume name, backfilled once for records written before the field + * existed: those predate the {@code floci-aws-} migration, so their data is in the + * legacy-named volume and must keep resolving there. Never use the live helper here, which + * would strand that data under a freshly created volume. + */ + private String resolveVolumeName(DbInstance instance) { + if (instance.getDockerVolumeName() == null || instance.getDockerVolumeName().isBlank()) { + instance.setDockerVolumeName(containerManager.legacyVolumeName(instance.getId())); + } + return instance.getDockerVolumeName(); + } + + private String resolveVolumeName(DbCluster cluster) { + if (cluster.getDockerVolumeName() == null || cluster.getDockerVolumeName().isBlank()) { + cluster.setDockerVolumeName(containerManager.legacyVolumeName(cluster.getId())); + } + return cluster.getDockerVolumeName(); + } + + private void removeDataPlane(String resourceId, String containerId, String dataVolumeName) { if (mock) { return; } try { containerManager.stop(resourceId, containerId); - containerManager.removeStorage(resourceId); + if (dataVolumeName != null) { + containerManager.removeStorage(dataVolumeName); + } } catch (RuntimeException e) { LOG.warnv("Failed to remove the InfluxDB container for {0}: {1}", resourceId, e.getMessage()); } @@ -1006,10 +1037,10 @@ public class TimestreamInfluxDbService implements Resettable { executor.execute(() -> { try { containerManager.restore(endpoint.containerId(), backupId); - applyDataPlane(resourceType, accountId, region, resourceId, endpoint, AVAILABLE); + applyDataPlane(resourceType, accountId, region, resourceId, null, endpoint, AVAILABLE); } catch (RuntimeException e) { LOG.errorv(e, "Failed to restore backup {0} into {1} {2}", backupId, resourceType, resourceId); - applyDataPlane(resourceType, accountId, region, resourceId, endpoint, "RESTORE_FAILED"); + applyDataPlane(resourceType, accountId, region, resourceId, null, endpoint, "RESTORE_FAILED"); } }); } diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java index c58caa4de8..0e7b0c707c 100644 --- a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManager.java @@ -94,7 +94,23 @@ public class TimestreamInfluxDbContainerManager implements ContainerTeardown { } } - public InfluxDbEndpoint start(String resourceId, String accountId, String region, + /** + * The data volume name a newly created resource is stamped with: the current prefix, so the + * name is persisted on the record rather than recomputed later. + */ + public String volumeName(String resourceId) { + return ContainerStorageHelper.resourceName(config, SERVICE, null, resourceId); + } + + /** + * The data volume name as it was before the {@code floci-aws-} migration, for records that + * predate the persisted field: their data lives under this name and must keep resolving there. + */ + public String legacyVolumeName(String resourceId) { + return ContainerStorageHelper.legacyResourceName(config, SERVICE, null, resourceId); + } + + public InfluxDbEndpoint start(String resourceId, String dataVolumeName, String accountId, String region, InfluxDbSetup setup, Map engineEnvironment) { EmulatorConfig.TimestreamInfluxDbServiceConfig serviceConfig = config.services().timestreamInfluxdb(); String containerName = containerName(resourceId); @@ -125,7 +141,7 @@ public class TimestreamInfluxDbContainerManager implements ContainerTeardown { ContainerInfo info; try { - applyVolumes(builder, resourceId); + applyVolumes(builder, resourceId, dataVolumeName); info = lifecycleManager.createAndStart(builder.build()); } catch (RuntimeException e) { lifecycleManager.removeIfExists(containerName); @@ -193,10 +209,11 @@ public class TimestreamInfluxDbContainerManager implements ContainerTeardown { } } - public void removeStorage(String resourceId) { + /** Removes the persisted data volume and its {@code -config} sibling according to storage policy. */ + public void removeStorage(String dataVolumeName) { if (ContainerStorageHelper.isNamedVolumeMode(config)) { - ContainerStorageHelper.removeStorage(config, lifecycleManager, SERVICE, null, resourceId); - ContainerStorageHelper.removeStorage(config, lifecycleManager, SERVICE, null, resourceId + "-config"); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, dataVolumeName); + ContainerStorageHelper.removeNamedVolume(config, lifecycleManager, dataVolumeName + "-config"); } } @@ -304,11 +321,10 @@ public class TimestreamInfluxDbContainerManager implements ContainerTeardown { } } - private void applyVolumes(ContainerBuilder.Builder builder, String resourceId) { + private void applyVolumes(ContainerBuilder.Builder builder, String resourceId, String dataVolumeName) { if (ContainerStorageHelper.isNamedVolumeMode(config)) { - ContainerStorageHelper.applyStorage(builder, lifecycleManager, config, SERVICE, null, resourceId, DATA_PATH); - ContainerStorageHelper.applyStorage(builder, lifecycleManager, config, SERVICE, null, - resourceId + "-config", CONFIG_PATH); + ContainerStorageHelper.applyNamedVolume(builder, lifecycleManager, dataVolumeName, DATA_PATH); + ContainerStorageHelper.applyNamedVolume(builder, lifecycleManager, dataVolumeName + "-config", CONFIG_PATH); return; } Path base = ContainerStorageHelper.hostResourcePath(config, SERVICE, resourceId); diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java index aec824ef53..f9a42ae624 100644 --- a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbCluster.java @@ -41,6 +41,13 @@ public class DbCluster { private String region; private String containerId; + /** + * The data Docker volume name (the config volume is this plus {@code -config}). Stamped at + * creation with the current prefix; null on records written before this field existed, + * which are backfilled with the frozen legacy name so their data stays reachable. + */ + private String dockerVolumeName; + public DbCluster() { } @@ -50,6 +57,8 @@ public class DbCluster { public void setRegion(String region) { this.region = region; } public String getContainerId() { return containerId; } public void setContainerId(String containerId) { this.containerId = containerId; } + public String getDockerVolumeName() { return dockerVolumeName; } + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } public String getId() { return id; } public void setId(String id) { this.id = id; } diff --git a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java index 5c82286a9e..8e472fd06a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java +++ b/src/main/java/io/github/hectorvent/floci/services/timestreaminfluxdb/model/DbInstance.java @@ -43,6 +43,13 @@ public class DbInstance { private String region; private String containerId; + /** + * The data Docker volume name (the config volume is this plus {@code -config}). Stamped at + * creation with the current prefix; null on records written before this field existed, + * which are backfilled with the frozen legacy name so their data stays reachable. + */ + private String dockerVolumeName; + public DbInstance() { } @@ -52,6 +59,8 @@ public class DbInstance { public void setRegion(String region) { this.region = region; } public String getContainerId() { return containerId; } public void setContainerId(String containerId) { this.containerId = containerId; } + public String getDockerVolumeName() { return dockerVolumeName; } + public void setDockerVolumeName(String dockerVolumeName) { this.dockerVolumeName = dockerVolumeName; } public String getId() { return id; } public void setId(String id) { this.id = id; } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelperTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelperTest.java index ab360522f8..7002e977c0 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelperTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/ContainerStorageHelperTest.java @@ -14,21 +14,49 @@ import static org.mockito.Mockito.when; class ContainerStorageHelperTest { @Test - void resourceNamesStayUnchangedWithoutNamespace() { - assertEquals("floci-rds-db1", ContainerStorageHelper.resourceName("rds", null, "db1")); - assertEquals("floci-rds-vol1", ContainerStorageHelper.resourceName(config(""), "rds", "vol1", "db1")); - assertEquals("floci-opensearch-domain1", ContainerStorageHelper.resourceName(config(""), "opensearch", null, "domain1")); - assertEquals("floci-ec2-i-123", ContainerStorageHelper.dockerName(config(""), "floci-ec2-i-123")); + void resourceNamesCarryThisEmulatorsPrefix() { + assertEquals("floci-aws-rds-db1", ContainerStorageHelper.resourceName("rds", null, "db1")); + assertEquals("floci-aws-rds-vol1", ContainerStorageHelper.resourceName(config(""), "rds", "vol1", "db1")); + assertEquals("floci-aws-opensearch-domain1", + ContainerStorageHelper.resourceName(config(""), "opensearch", null, "domain1")); + assertEquals("floci-aws-ec2-i-123", ContainerStorageHelper.dockerName(config(""), "ec2-i-123")); + } + + @Test + void alreadyPrefixedNamesAreNormalisedNotPrefixedTwice() { + // A persisted name is fed straight back in when a container is named after the volume it + // mounts, so prefixing has to be idempotent in both directions. + assertEquals("floci-aws-ec2-i-123", ContainerStorageHelper.dockerName(config(""), "floci-ec2-i-123")); + assertEquals("floci-aws-ec2-i-123", ContainerStorageHelper.dockerName(config(""), "floci-aws-ec2-i-123")); + assertEquals("floci-aws-ui", ContainerStorageHelper.dockerName(config(""), "floci-ui")); + } + + @Test + void legacyNamesKeepTheFrozenPreMigrationShape() { + // Frozen forever: this is the name a pre-migration volume's data actually lives under. + assertEquals("floci-rds-vol1", + ContainerStorageHelper.legacyResourceName(config(""), "rds", "vol1", "db1")); + assertEquals("floci-rds-db1", + ContainerStorageHelper.legacyResourceName(config(""), "rds", null, "db1")); + assertEquals("floci-run-one-rds-vol1", + ContainerStorageHelper.legacyResourceName(config(" run/one "), "rds", "vol1", "db1")); + // Round-trips back from a current name, which is how a container derives its legacy twin. + assertEquals("floci-rds-vol1", + ContainerStorageHelper.legacyDockerName(config(""), "floci-aws-rds-vol1")); } @Test void resourceNamesIncludeSanitizedNamespaceWhenConfigured() { EmulatorConfig config = config(" run/one "); - assertEquals("floci-run-one-rds-db1", ContainerStorageHelper.resourceName(config, "rds", null, "db1")); - assertEquals("floci-run-one-rds-vol1", ContainerStorageHelper.resourceName(config, "rds", "vol1", "db1")); - assertEquals("floci-run-one-ec2-i-123", ContainerStorageHelper.dockerName(config, "floci-ec2-i-123")); - assertEquals("floci-run-one-ui", ContainerStorageHelper.dockerName(config, "floci-ui")); + // The namespace lands after the cloud token, never before it. + assertEquals("floci-aws-run-one-rds-db1", ContainerStorageHelper.resourceName(config, "rds", null, "db1")); + assertEquals("floci-aws-run-one-rds-vol1", ContainerStorageHelper.resourceName(config, "rds", "vol1", "db1")); + assertEquals("floci-aws-run-one-ec2-i-123", ContainerStorageHelper.dockerName(config, "floci-ec2-i-123")); + assertEquals("floci-aws-run-one-ui", ContainerStorageHelper.dockerName(config, "floci-ui")); + // Re-normalising either shape is a no-op. + assertEquals("floci-aws-run-one-ec2-i-123", + ContainerStorageHelper.dockerName(config, "floci-aws-run-one-ec2-i-123")); } @Test @@ -43,7 +71,7 @@ class ContainerStorageHelperTest { EmulatorConfig config = config(".."); assertEquals(Path.of("/tmp/floci/rds/db1"), ContainerStorageHelper.hostResourcePath(config, "rds", "db1")); - assertEquals("floci-rds-db1", ContainerStorageHelper.resourceName(config, "rds", null, "db1")); + assertEquals("floci-aws-rds-db1", ContainerStorageHelper.resourceName(config, "rds", null, "db1")); assertEquals( Map.of("floci", "true", "floci_emulator", "floci-aws"), ContainerStorageHelper.defaultLabels(config)); @@ -68,9 +96,10 @@ class ContainerStorageHelperTest { ContainerStorageHelper.prefixedDockerName(config(""), "acme", "my-fn-abc123")); assertEquals("acme-run-one-my-fn-abc123", ContainerStorageHelper.prefixedDockerName(config(" run/one "), "acme", "my-fn-abc123")); - // The default prefix through this path matches what dockerName produces. - assertEquals(ContainerStorageHelper.dockerName(config("run-one"), "floci-my-fn-abc123"), - ContainerStorageHelper.prefixedDockerName(config("run-one"), "floci", "my-fn-abc123")); + // This emulator's own prefix through this path matches what dockerName produces. + assertEquals(ContainerStorageHelper.dockerName(config("run-one"), "my-fn-abc123"), + ContainerStorageHelper.prefixedDockerName( + config("run-one"), ContainerStorageHelper.NAME_PREFIX, "my-fn-abc123")); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManagerTest.java index 37483a5d0b..83ad8388de 100644 --- a/src/test/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/amazonmq/container/RabbitMqManagerTest.java @@ -217,8 +217,10 @@ class RabbitMqManagerTest { assertThrows(RuntimeException.class, () -> manager.startContainer(broker("b-1"))); - // Once for the stale-container sweep before create, once for the rollback. - verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-amazonmq-b-1"); + // Once for the stale-container sweep before create, once for the rollback; the sweep also + // clears the pre-migration name so a survivor cannot keep the broker's ports. + verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-aws-amazonmq-b-1"); + verify(lifecycleManager).removeIfExists("floci-amazonmq-b-1"); verify(portAllocator).release(5672); verify(portAllocator).release(15672); } @@ -242,6 +244,7 @@ class RabbitMqManagerTest { manager.stopContainer(broker); + verify(lifecycleManager).removeIfExists("floci-aws-amazonmq-b-2"); verify(lifecycleManager).removeIfExists("floci-amazonmq-b-2"); verifyNoMoreInteractions(lifecycleManager); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java index 4793196bfe..58712111e5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/portforward/Ec2PortForwardManagerTest.java @@ -139,8 +139,15 @@ class Ec2PortForwardManagerTest { @Test void forwardContainerNameIsDeterministic() { + assertEquals("floci-aws-ec2-fwd-i-123-80", + Ec2PortForwardManager.forwardContainerName(null, "i-123", 80)); + } + + @Test + void legacyForwardContainerNameKeepsThePreMigrationShape() { + // Frozen: it is how a sidecar created before the rename is found and cleared. assertEquals("floci-ec2-fwd-i-123-80", - Ec2PortForwardManager.forwardContainerName("i-123", 80)); + Ec2PortForwardManager.legacyForwardContainerName(null, "i-123", 80)); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java index 2078c6ccfa..b51b25c824 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecr/registry/EcrRegistryManagerTest.java @@ -57,7 +57,7 @@ class EcrRegistryManagerTest { private static final int BASE_PORT = 6100; private static final int MAX_PORT = 6101; // pool of exactly two ports - private static final String REGISTRY_NAME = "floci-test-ecr-registry"; + private static final String REGISTRY_NAME = "floci-aws-test-ecr-registry"; private static final String AWS_ECR_IMAGE = "123456789012.dkr.ecr.us-east-1.amazonaws.com/backend-user:1"; private PortAllocator portAllocator; @@ -343,7 +343,7 @@ class EcrRegistryManagerTest { when(containerDetector.isRunningInContainer()).thenReturn(true); when(docker.resourceNamespace()).thenReturn(Optional.of("run/one")); - assertEquals("http://floci-run-one-test-ecr-registry:5000", manager.httpClient().baseUrl()); + assertEquals("http://floci-aws-run-one-test-ecr-registry:5000", manager.httpClient().baseUrl()); } @Test @@ -532,7 +532,7 @@ class EcrRegistryManagerTest { manager.pruneStorage(); verify(lifecycleManager).stopAndRemove(Mockito.eq("container-id"), Mockito.isNull()); - verify(lifecycleManager).removeVolume("floci-ecr-registry-data"); + verify(lifecycleManager).removeVolume("floci-aws-ecr-registry-data"); } @Test @@ -557,7 +557,7 @@ class EcrRegistryManagerTest { manager.shutdown(); verify(lifecycleManager).stopAndRemove(Mockito.eq("container-id"), Mockito.isNull()); - verify(lifecycleManager).removeVolume("floci-ecr-registry-data"); + verify(lifecycleManager).removeVolume("floci-aws-ecr-registry-data"); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java index 50f6711a88..e83b078086 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsIsolationDockerIntegrationTest.java @@ -145,7 +145,7 @@ class EcsContainerManagerEfsIsolationDockerIntegrationTest { EcsTask task = new EcsTask(); task.setTaskArn("arn:aws:ecs:us-east-1:000000000000:task/efs-isolation/" + suffix); - volumesToCleanUp.add(EcsContainerManager.efsVolumeName(fileSystemId, accessPointId, rootDirectory)); + volumesToCleanUp.add("floci-aws-" + EcsContainerManager.efsVolumeToken(fileSystemId, accessPointId, rootDirectory)); EcsTaskHandle handle = containerManager.startTask(task, taskDef, List.of(), "us-east-1"); String dockerId = handle.getContainerIds().get("app"); diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsVolumeNamingTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsVolumeNamingTest.java index 451f7b8625..55b26ff159 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsVolumeNamingTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerEfsVolumeNamingTest.java @@ -9,8 +9,8 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Unit tests for {@link EcsContainerManager#efsVolumeName}, the naming logic that scopes an - * EFS-backed task volume's shared Docker named volume to both {@code fileSystemId} and its + * Unit tests for {@link EcsContainerManager#efsVolumeToken}, the naming logic that scopes an + * EFS-backed task volume's shared Docker named volume (before the emulator prefix) to both {@code fileSystemId} and its * effective root (#2563). The name must be stable for a given (fileSystemId, rootDirectory, * accessPointId) combination (so identical mounts keep sharing one volume), distinct across * different roots on the same file system (so mounts do not collide on the same tree), and @@ -22,16 +22,16 @@ class EcsContainerManagerEfsVolumeNamingTest { @Test void defaultsToPlainFileSystemNameWhenNoRootOrAccessPointIsSet() { - assertEquals("floci-efs-fs-abc", EcsContainerManager.efsVolumeName("fs-abc", null, null)); - assertEquals("floci-efs-fs-abc", EcsContainerManager.efsVolumeName("fs-abc", null, "")); - assertEquals("floci-efs-fs-abc", EcsContainerManager.efsVolumeName("fs-abc", null, "/")); + assertEquals("efs-fs-abc", EcsContainerManager.efsVolumeToken("fs-abc", null, null)); + assertEquals("efs-fs-abc", EcsContainerManager.efsVolumeToken("fs-abc", null, "")); + assertEquals("efs-fs-abc", EcsContainerManager.efsVolumeToken("fs-abc", null, "/")); } @Test void differentRootDirectoriesOnSameFileSystemYieldDifferentVolumes() { - String rootA = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a"); - String rootB = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-b"); - String rootNone = EcsContainerManager.efsVolumeName("fs-shared", null, null); + String rootA = EcsContainerManager.efsVolumeToken("fs-shared", null, "/team-a"); + String rootB = EcsContainerManager.efsVolumeToken("fs-shared", null, "/team-b"); + String rootNone = EcsContainerManager.efsVolumeToken("fs-shared", null, null); assertNotEquals(rootA, rootB, "different rootDirectory values must isolate the volume"); assertNotEquals(rootA, rootNone, "a non-root rootDirectory must not collapse to the plain name"); @@ -41,16 +41,16 @@ class EcsContainerManagerEfsVolumeNamingTest { @Test void sameRootDirectoryOnSameFileSystemStillShares() { - String first = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a"); - String second = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a"); + String first = EcsContainerManager.efsVolumeToken("fs-shared", null, "/team-a"); + String second = EcsContainerManager.efsVolumeToken("fs-shared", null, "/team-a"); assertEquals(first, second, "identical (fileSystemId, rootDirectory) must keep sharing one volume"); } @Test void accessPointIdTakesPrecedenceOverRootDirectory() { - String viaAccessPoint = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", "/ignored"); - String viaRootOnly = EcsContainerManager.efsVolumeName("fs-shared", null, "/ignored"); + String viaAccessPoint = EcsContainerManager.efsVolumeToken("fs-shared", "fsap-111", "/ignored"); + String viaRootOnly = EcsContainerManager.efsVolumeToken("fs-shared", null, "/ignored"); assertNotEquals(viaAccessPoint, viaRootOnly, "an access point's own root must take precedence over rootDirectory, not merge with it"); @@ -58,16 +58,16 @@ class EcsContainerManagerEfsVolumeNamingTest { @Test void differentAccessPointsOnSameFileSystemYieldDifferentVolumes() { - String apOne = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", null); - String apTwo = EcsContainerManager.efsVolumeName("fs-shared", "fsap-222", null); + String apOne = EcsContainerManager.efsVolumeToken("fs-shared", "fsap-111", null); + String apTwo = EcsContainerManager.efsVolumeToken("fs-shared", "fsap-222", null); assertNotEquals(apOne, apTwo, "different accessPointId values must isolate the volume"); } @Test void sameAccessPointStillShares() { - String first = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", null); - String second = EcsContainerManager.efsVolumeName("fs-shared", "fsap-111", "/whatever-is-ignored"); + String first = EcsContainerManager.efsVolumeToken("fs-shared", "fsap-111", null); + String second = EcsContainerManager.efsVolumeToken("fs-shared", "fsap-111", "/whatever-is-ignored"); assertEquals(first, second, "the same accessPointId must keep sharing one volume regardless of rootDirectory"); @@ -75,13 +75,13 @@ class EcsContainerManagerEfsVolumeNamingTest { @Test void equivalentRootDirectorySpellingsShareTheSameVolume() { - String canonical = EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a"); + String canonical = EcsContainerManager.efsVolumeToken("fs-shared", null, "/team-a"); - assertEquals(canonical, EcsContainerManager.efsVolumeName("fs-shared", null, "/team-a/"), + assertEquals(canonical, EcsContainerManager.efsVolumeToken("fs-shared", null, "/team-a/"), "a trailing slash must not create a distinct volume for the same directory"); - assertEquals(canonical, EcsContainerManager.efsVolumeName("fs-shared", null, "//team-a"), + assertEquals(canonical, EcsContainerManager.efsVolumeToken("fs-shared", null, "//team-a"), "a repeated leading slash must not create a distinct volume for the same directory"); - assertEquals(canonical, EcsContainerManager.efsVolumeName("fs-shared", null, "team-a"), + assertEquals(canonical, EcsContainerManager.efsVolumeToken("fs-shared", null, "team-a"), "a missing leading slash must not create a distinct volume for the same directory"); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesFromTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesFromTest.java index 0f840c6dbc..706369af09 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesFromTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesFromTest.java @@ -130,7 +130,7 @@ class EcsContainerManagerVolumesFromTest { when(lifecycleManager.getDockerClient()).thenReturn(dockerClient); InspectVolumeCmd inspectVolumeCmd = mock(InspectVolumeCmd.class); InspectVolumeResponse volume = mock(InspectVolumeResponse.class); - when(dockerClient.inspectVolumeCmd("floci-ecs-firelens-volumesfrom1")).thenReturn(inspectVolumeCmd); + when(dockerClient.inspectVolumeCmd("floci-aws-ecs-firelens-volumesfrom1")).thenReturn(inspectVolumeCmd); when(inspectVolumeCmd.exec()).thenReturn(volume); when(volume.getMountpoint()).thenReturn("/var/lib/docker/volumes/floci-ecs-firelens-volumesfrom1/_data"); CopyArchiveToContainerCmd copyCmd = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF); @@ -160,8 +160,8 @@ class EcsContainerManagerVolumesFromTest { lifecycleOrder.verify(lifecycleManager).createAndStart(any(ContainerSpec.class)); verify(routerBuilder).withVolumesFrom("source-id", false); verify(appBuilder).withVolumesFrom("source-id", true); - verify(lifecycleManager).ensureVolume("floci-ecs-firelens-volumesfrom1"); - verify(routerBuilder).withNamedVolume("floci-ecs-firelens-volumesfrom1", "/var/run"); + verify(lifecycleManager).ensureVolume("floci-aws-ecs-firelens-volumesfrom1"); + verify(routerBuilder).withNamedVolume("floci-aws-ecs-firelens-volumesfrom1", "/var/run"); verify(copyCmd).withRemotePath("/fluent-bit/etc"); verify(routerBuilder, never()).withLoopbackPortBinding(anyInt(), anyInt()); verify(appBuilder, never()).withNetworkMode(anyString()); @@ -178,7 +178,7 @@ class EcsContainerManagerVolumesFromTest { verify(logStreamer, never()).attach(eq("app-id"), any(), any(), any(), any()); verify(logStreamer).attach(eq("source-id"), any(), any(), eq("us-east-1"), any()); verify(logStreamer).attach(eq("router-id"), any(), any(), eq("us-east-1"), any()); - assertEquals("floci-ecs-firelens-volumesfrom1", handle.getFirelensVolumeName()); + assertEquals("floci-aws-ecs-firelens-volumesfrom1", handle.getFirelensVolumeName()); assertEquals(List.of("source", "router", "app"), handle.getContainerIds().keySet().stream().toList()); assertEquals(List.of("app", "router", "source"), ecsTask.getContainers().stream().map(Container::getName).toList()); diff --git a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java index 94b944f5d1..0b2f5a1366 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManagerVolumesTest.java @@ -159,10 +159,12 @@ class EcsContainerManagerVolumesTest { manager.startTask(task, taskDef, List.of(), "us-east-1"); + // Each EFS volume -> a shared local Docker named volume under the current prefix (no + // legacy-named volume exists here), read-write or read-only per the mountPoint. verify(builder, times(1)).withNamedVolume( - EcsContainerManager.efsVolumeName("fs-0123456789abcdef0", null, "/dps"), "/mnt/efs", false); + "floci-aws-" + EcsContainerManager.efsVolumeToken("fs-0123456789abcdef0", null, "/dps"), "/mnt/efs", false); verify(builder, times(1)).withNamedVolume( - EcsContainerManager.efsVolumeName("fs-00000000000000001", null, null), "/mnt/shared", true); + "floci-aws-" + EcsContainerManager.efsVolumeToken("fs-00000000000000001", null, null), "/mnt/shared", true); // EFS volumes are never bind-mounted as host paths. verify(builder, never()).withBind(any(), any()); @@ -202,7 +204,7 @@ class EcsContainerManagerVolumesTest { configured.startTask(task, taskDef, List.of(), "us-east-1"); verify(lifecycleManager, times(1)).ensureSharedVolume( - EcsContainerManager.efsVolumeName("fs-abc", null, "/dps"), + "floci-aws-" + EcsContainerManager.efsVolumeToken("fs-abc", null, "/dps"), OptionalInt.of(1001), OptionalInt.of(1001), Optional.of("2775"), "busybox:stable"); } diff --git a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java index fd2f52f8f5..15b7efb902 100644 --- a/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/eks/EksClusterManagerTest.java @@ -472,7 +472,7 @@ class EksClusterManagerTest { manager.stopCluster(cluster); verify(lifecycleManager).stopAndRemove("cid-1", null); - verify(lifecycleManager).removeVolume("floci-eks-demo"); + verify(lifecycleManager).removeVolume("floci-aws-eks-demo"); } @Test @@ -501,7 +501,7 @@ class EksClusterManagerTest { Cluster cluster = cluster(); cluster.setAccountId("000000000000"); - assertEquals("floci-eks-demo", manager.clusterResourceName(cluster)); + assertEquals("floci-aws-eks-demo", manager.clusterResourceName(cluster)); } @Test @@ -539,7 +539,7 @@ class EksClusterManagerTest { cluster.setAccountId("999999999999"); manager.restoreCluster(cluster); - assertEquals("floci-eks-999999999999.demo", cluster.getDockerName()); + assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName()); assertEquals("cid-new", cluster.getContainerId()); verify(lifecycleManager, never()).adopt(anyString(), any()); verify(lifecycleManager, never()).removeIfExists("floci-eks-demo"); @@ -560,7 +560,7 @@ class EksClusterManagerTest { cluster.setAccountId("999999999999"); manager.restoreCluster(cluster); - assertEquals("floci-eks-999999999999.demo", cluster.getDockerName()); + assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName()); verify(lifecycleManager, never()).adopt(anyString(), any()); verify(lifecycleManager, never()).removeIfExists("floci-eks-demo"); } @@ -581,7 +581,7 @@ class EksClusterManagerTest { cluster.setAccountId("999999999999"); manager.restoreCluster(cluster); - assertEquals("floci-eks-999999999999.demo", cluster.getDockerName()); + assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName()); assertEquals("cid-new", cluster.getContainerId()); verify(inspectVolumeCmd).exec(); } @@ -595,8 +595,8 @@ class EksClusterManagerTest { cluster.setAccountId("999999999999"); manager.startCluster(cluster); - assertEquals("floci-eks-999999999999.demo", cluster.getDockerName()); - verify(lifecycleManager).removeIfExists("floci-eks-999999999999.demo"); + assertEquals("floci-aws-eks-999999999999.demo", cluster.getDockerName()); + verify(lifecycleManager).removeIfExists("floci-aws-eks-999999999999.demo"); } } @@ -1644,7 +1644,7 @@ class EksClusterManagerTest { cluster.setVersion("1.30"); assertThrows(RuntimeException.class, () -> manager.startCluster(cluster)); - verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-eks-fail-cluster"); + verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-aws-eks-fail-cluster"); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManagerTest.java index fe918bebea..729530f130 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/container/ElastiCacheContainerManagerTest.java @@ -41,7 +41,7 @@ class ElastiCacheContainerManagerTest { // Rollback must still fall back to the deterministic name so nothing is orphaned. manager.stopByGroupId("my-group"); - verify(lifecycleManager).removeIfExists("floci-valkey-my-group"); + verify(lifecycleManager).removeIfExists("floci-aws-valkey-my-group"); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java index 5084840a74..f5ac284d4d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/container/FlinkContainerManagerTest.java @@ -316,8 +316,8 @@ class FlinkContainerManagerTest { assertThrows(RuntimeException.class, () -> manager.startCluster(app)); - verify(lifecycleManager, times(2)).removeIfExists("floci-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-jm-failure"); - verify(lifecycleManager, atLeastOnce()).removeIfExists("floci-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-jm-failure-tm"); + verify(lifecycleManager, times(2)).removeIfExists("floci-aws-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-jm-failure"); + verify(lifecycleManager, atLeastOnce()).removeIfExists("floci-aws-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-jm-failure-tm"); assertNull(app.getContainerId()); assertNull(app.getTaskManagerContainerId()); } @@ -337,7 +337,7 @@ class FlinkContainerManagerTest { assertThrows(RuntimeException.class, () -> manager.startCluster(app)); verify(lifecycleManager).stopAndRemove("jm-id", null); - verify(lifecycleManager, atLeastOnce()).removeIfExists("floci-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-tm-failure-tm"); + verify(lifecycleManager, atLeastOnce()).removeIfExists("floci-aws-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-tm-failure-tm"); assertNull(app.getContainerId()); assertNull(app.getRestEndpoint()); assertNull(app.getTaskManagerContainerId()); @@ -356,8 +356,8 @@ class FlinkContainerManagerTest { assertThrows(RuntimeException.class, () -> manager.startCluster(app)); - verify(lifecycleManager, times(2)).removeIfExists("floci-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-log4j-copy-failure"); - verify(lifecycleManager, atLeastOnce()).removeIfExists("floci-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-log4j-copy-failure-tm"); + verify(lifecycleManager, times(2)).removeIfExists("floci-aws-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-log4j-copy-failure"); + verify(lifecycleManager, atLeastOnce()).removeIfExists("floci-aws-kinesisanalytics-arn-aws-kinesisanalytics-us-west-2-000000000000-application-log4j-copy-failure-tm"); verify(lifecycleManager, Mockito.never()).startCreated(any(), any()); assertNull(app.getContainerId()); } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCodeVolumeReuseTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCodeVolumeReuseTest.java index 7e6b391014..e65f50091a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCodeVolumeReuseTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherCodeVolumeReuseTest.java @@ -81,7 +81,7 @@ class ContainerLauncherCodeVolumeReuseTest { EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class); when(config.storage()).thenReturn(storage); when(storage.persistentPath()).thenReturn(tempDir.toString()); - // ensureCodeVolume resolves the configured container-name prefix (unset = "floci"). + // ensureCodeVolume resolves the configured container-name prefix (unset = "floci-aws"). when(config.services()).thenReturn(services); when(services.lambda()).thenReturn(lambda); when(lambda.containerNamePrefix()).thenReturn(Optional.empty()); @@ -202,8 +202,8 @@ class ContainerLauncherCodeVolumeReuseTest { void successfulPopulationPrunesOnlyOrphanInternalMarkers() throws Exception { LambdaFunction fn = fn("sha-v1-abcdef0123456789"); String vol = ContainerLauncher.codeVolumeName(fn); - String live = "floci-code-live"; - String orphan = "floci-code-orphan"; + String live = "floci-aws-code-live"; + String orphan = "floci-aws-code-orphan"; writeMarker(live); writeMarker(orphan); writeMarker("unrelated-marker"); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherNamePrefixTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherNamePrefixTest.java index 54995ed7a3..e169bb5a47 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherNamePrefixTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherNamePrefixTest.java @@ -23,9 +23,9 @@ class ContainerLauncherNamePrefixTest { private static final Pattern DOCKER_NAME = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9_.-]*$"); @Test - void fallsBackToFlociWhenPrefixUnsetOrBlank() { - assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config(null))); - assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config(" "))); + void fallsBackToThisEmulatorsPrefixWhenUnsetOrBlank() { + assertEquals("floci-aws", ContainerLauncher.resolveContainerNamePrefix(config(null))); + assertEquals("floci-aws", ContainerLauncher.resolveContainerNamePrefix(config(" "))); } @Test @@ -35,11 +35,11 @@ class ContainerLauncherNamePrefixTest { } @Test - void fallsBackToFlociWhenPrefixIsNotDockerSafe() { + void fallsBackToThisEmulatorsPrefixWhenNotDockerSafe() { // Docker names must start alphanumeric and allow only [A-Za-z0-9_.-] after that. - assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config("-leading-dash"))); - assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config("has space"))); - assertEquals("floci", ContainerLauncher.resolveContainerNamePrefix(config("has:colon"))); + assertEquals("floci-aws", ContainerLauncher.resolveContainerNamePrefix(config("-leading-dash"))); + assertEquals("floci-aws", ContainerLauncher.resolveContainerNamePrefix(config("has space"))); + assertEquals("floci-aws", ContainerLauncher.resolveContainerNamePrefix(config("has:colon"))); } @Test @@ -56,7 +56,7 @@ class ContainerLauncherNamePrefixTest { "must be a docker-volume-safe name, was: " + name); // The one-arg overload stays on the default prefix (used by pre-existing callers/tests). assertEquals(ContainerLauncher.codeVolumeName(fn), - ContainerLauncher.codeVolumeName("floci", fn)); + ContainerLauncher.codeVolumeName(ContainerLauncher.DEFAULT_NAME_PREFIX, fn)); } private static EmulatorConfig config(String prefix) { diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java index 0a892bb299..11a23fd2d9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java @@ -484,7 +484,7 @@ class ContainerLauncherTest { launcher.launch(fn); - String expectedVolumeName = "floci-efs-fsap-0123456789abcdef0-" + String expectedVolumeName = "floci-aws-efs-fsap-0123456789abcdef0-" + "9d6eafd2aec94d4518a004f005725b4b3c673c1506436bb7368cfd5450fc0810"; verify(lifecycleManager).ensureSharedVolume(expectedVolumeName, OptionalInt.empty(), OptionalInt.empty(), Optional.empty(), "busybox:stable"); diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java index 377b45c6a7..92eeb84619 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.lambda.launcher; +import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -39,8 +40,8 @@ class ContainerLauncherVolumeNamingTest { String name = ContainerLauncher.codeVolumeName( fnWithSha("my-fn", "AbC123+/xyz=deadbeefcafebabe0123456789")); - assertTrue(name.startsWith("floci-code-my-fn-"), - "should be floci-code-- shaped, was: " + name); + assertTrue(name.startsWith("floci-aws-code-my-fn-"), + "should be floci-aws-code-- shaped, was: " + name); assertTrue(DOCKER_VOLUME_NAME.matcher(name).matches(), "must be a docker-volume-safe name, was: " + name); } @@ -105,12 +106,15 @@ class ContainerLauncherVolumeNamingTest { String arn = "arn:aws:elasticfilesystem:us-east-1:000000000000:" + "access-point/fsap-0123456789abcdef0"; - String volumeName = ContainerLauncher.efsVolumeName(arn); + String token = ContainerLauncher.efsVolumeToken(arn); - assertEquals("floci-efs-fsap-0123456789abcdef0-" + assertEquals("efs-fsap-0123456789abcdef0-" + "9d6eafd2aec94d4518a004f005725b4b3c673c1506436bb7368cfd5450fc0810", - volumeName); - assertEquals(volumeName, ContainerLauncher.efsVolumeName(arn)); + token); + assertEquals(token, ContainerLauncher.efsVolumeToken(arn)); + String volumeName = ContainerStorageHelper.dockerName(null, token); + assertTrue(volumeName.startsWith("floci-aws-efs-"), + "a new EFS volume takes this emulator's prefix, was: " + volumeName); assertTrue(DOCKER_VOLUME_NAME.matcher(volumeName).matches(), "EFS volume name must be Docker-safe, was: " + volumeName); } @@ -122,8 +126,8 @@ class ContainerLauncherVolumeNamingTest { String westArn = "arn:aws:elasticfilesystem:us-west-2:111111111111:" + "access-point/fsap-0123456789abcdef0"; - assertNotEquals(ContainerLauncher.efsVolumeName(eastArn), - ContainerLauncher.efsVolumeName(westArn), + assertNotEquals(ContainerLauncher.efsVolumeToken(eastArn), + ContainerLauncher.efsVolumeToken(westArn), "the complete ARN must participate in volume identity"); } diff --git a/src/test/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManagerTest.java index 9f62e42a81..9634da0fb2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/memorydb/container/MemoryDbContainerManagerTest.java @@ -45,7 +45,7 @@ class MemoryDbContainerManagerTest { // Rollback must still fall back to the deterministic name so nothing is orphaned. manager.stopByClusterName("my-cluster"); - verify(lifecycleManager).removeIfExists("floci-memorydb-my-cluster"); + verify(lifecycleManager).removeIfExists("floci-aws-memorydb-my-cluster"); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java index 543febc8ec..419b54e970 100644 --- a/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java @@ -39,6 +39,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; @@ -113,6 +114,51 @@ class RedpandaManagerTest { } } + @Test + void aPersistedVolumeNameIsMountedVerbatim() { + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerInfo("container-1", Map.of(KAFKA_PORT, new EndpointInfo("h", KAFKA_PORT)))); + when(config.storage().hostPersistentPath()).thenReturn(""); + MskCluster cluster = newCluster(); + cluster.setDockerVolumeName("floci-aws-msk-abc123"); + + manager.startContainer(cluster); + + verify(lifecycleManager).ensureVolume("floci-aws-msk-abc123"); + assertEquals("floci-aws-msk-abc123", cluster.getDockerVolumeName(), + "a persisted volume name must never be rewritten"); + } + + @Test + void aPreUpgradeClusterMountsItsLegacyVolumeAndKeepsThatName() { + // No persisted name means a record written before the field existed, so the cluster's + // data lives in the legacy-prefixed volume. Mounting the current name would silently + // start an empty broker and strand every topic. + when(lifecycleManager.createAndStart(any())).thenReturn( + new ContainerInfo("container-2", Map.of(KAFKA_PORT, new EndpointInfo("h", KAFKA_PORT)))); + when(config.storage().hostPersistentPath()).thenReturn(""); + MskCluster cluster = newCluster(); + cluster.setDockerVolumeName(null); + + manager.startContainer(cluster); + + verify(lifecycleManager).ensureVolume("floci-msk-abc123"); + verify(lifecycleManager, never()).ensureVolume("floci-aws-msk-abc123"); + assertEquals("floci-msk-abc123", cluster.getDockerVolumeName(), + "the legacy name must be backfilled onto the record, not recomputed each start"); + } + + @Test + void deletingAPreUpgradeClusterRemovesItsLegacyVolume() { + when(config.storage().mode()).thenReturn("memory"); + MskCluster cluster = newCluster(); + cluster.setDockerVolumeName(null); + + manager.removeClusterStorage(cluster); + + verify(lifecycleManager).removeVolume("floci-msk-abc123"); + } + private MskCluster newCluster() { MskCluster cluster = new MskCluster("arn:aws:kafka:us-east-1:000000000000:cluster/test-cluster/abc", "test-cluster", "3.6.0"); cluster.setVolumeId("abc123"); @@ -199,7 +245,7 @@ class RedpandaManagerTest { int flagIndex = spec.cmd().indexOf("--advertise-kafka-addr"); assertTrue(flagIndex >= 0, "cmd should contain --advertise-kafka-addr"); - assertEquals("floci-msk-abc123:9092", spec.cmd().get(flagIndex + 1)); + assertEquals("floci-aws-msk-abc123:9092", spec.cmd().get(flagIndex + 1)); assertFalse(spec.portBindings().containsKey(KAFKA_PORT), "container mode should not publish ports to host"); assertTrue(spec.exposedPorts().contains(KAFKA_PORT)); diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java index 70a8168187..68ace876b4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaEnvironmentManagerTest.java @@ -317,9 +317,9 @@ class MwaaEnvironmentManagerTest { environment.setAccountId("000000000000"); environment.setArn("arn:aws:airflow:us-east-1:000000000000:environment/my-env"); - assertEquals("floci-ns1-mwaa-000000000000.us-east-1.my-env-db", + assertEquals("floci-aws-ns1-mwaa-000000000000.us-east-1.my-env-db", MwaaEnvironmentManager.dbContainerName(namespacedConfig, environment)); - assertEquals("floci-ns1-mwaa-000000000000.us-east-1.my-env-airflow", + assertEquals("floci-aws-ns1-mwaa-000000000000.us-east-1.my-env-airflow", MwaaEnvironmentManager.airflowContainerName(namespacedConfig, environment)); } diff --git a/src/test/java/io/github/hectorvent/floci/services/neptune/container/NeptuneContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/neptune/container/NeptuneContainerManagerTest.java index dc5b156947..758203ea74 100644 --- a/src/test/java/io/github/hectorvent/floci/services/neptune/container/NeptuneContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/neptune/container/NeptuneContainerManagerTest.java @@ -46,7 +46,7 @@ class NeptuneContainerManagerTest { // Rollback must still fall back to the deterministic name so nothing is orphaned. manager.stopByClusterId("my-cluster"); - verify(lifecycleManager).removeIfExists("floci-neptune-my-cluster"); + verify(lifecycleManager).removeIfExists("floci-aws-neptune-my-cluster"); } @Test @@ -169,7 +169,7 @@ class NeptuneContainerManagerTest { "io.floci.resource-id", "cluster1", "io.floci.account", "111111111111", "io.floci.region", "eu-west-1")); - verify(builder).withName("floci-neptune-111111111111-eu-west-1-cluster1"); + verify(builder).withName("floci-aws-neptune-111111111111-eu-west-1-cluster1"); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java index 7f5d9a7175..6f4d92ee96 100644 --- a/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/rds/container/RdsContainerManagerTest.java @@ -356,7 +356,7 @@ class RdsContainerManagerTest { var spec = org.mockito.ArgumentCaptor.forClass(ContainerSpec.class); verify(lifecycleManager).create(spec.capture()); assertEquals(dbPath.toString(), spec.getValue().binds().getFirst().getPath()); - assertEquals("floci-rds-db1", spec.getValue().name()); + assertEquals("floci-aws-rds-db1", spec.getValue().name()); verify(logStreamer).attachForAccount( "222222222222", "container-id", "/aws/rds/instance/db1/error", "log-stream", "us-west-2", "rds:" + runtimeId); @@ -912,7 +912,7 @@ class RdsContainerManagerTest { runtimeId, "legacy", "legacy", "floci-rds-legacy-volume", DatabaseEngine.MYSQL, "mysql:8.0", "root", "password", "db")); - assertEquals("floci-rds-legacy-volume", + assertEquals("floci-aws-rds-legacy-volume", manager.getActiveHandle(runtimeId).getContainerId()); verify(lifecycleManager, never()).create(any()); assertThrows(IllegalStateException.class, () -> manager.start( @@ -1095,7 +1095,44 @@ class RdsContainerManagerTest { var spec = org.mockito.ArgumentCaptor.forClass(ContainerSpec.class); verify(lifecycleManager).removeIfExistsStrict("floci-rds-volume-a"); verify(lifecycleManager).create(spec.capture()); - assertEquals("floci-rds-volume-a", spec.getValue().name()); + assertEquals("floci-aws-rds-volume-a", spec.getValue().name()); + } + + @Test + void aLegacyVolumeIsMountedAsIsWhileItsContainerTakesTheCurrentName() { + // The central asymmetry of the prefix migration. The volume holds the data, so it keeps + // whatever name it was persisted under; the container is disposable, so it is always + // normalised. Mounting the current name here would start an empty database. + EmulatorConfig config = config(Path.of("data")); + ContainerDetector containerDetector = mock(ContainerDetector.class); + ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class); + stubStarts(lifecycleManager, new ContainerLifecycleManager.ContainerInfo( + "container-id", Map.of(3306, new ContainerLifecycleManager.EndpointInfo("db1", 3306)))); + ContainerLogStreamer logStreamer = mock(ContainerLogStreamer.class); + lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream"); + + RdsContainerManager manager = new RdsContainerManager( + new ContainerBuilder(config, mock(DockerHostResolver.class), mock(EmbeddedDnsServer.class)), + lifecycleManager, + logStreamer, + containerDetector, + config, + new RegionResolver("us-east-1", "000000000000"), + mock(ServiceConfigAccess.class)); + + manager.start(null, "db1", "db1", "floci-rds-db1", + DatabaseEngine.MYSQL, "mysql:8.0", "root", "password", "db"); + + var spec = org.mockito.ArgumentCaptor.forClass(ContainerSpec.class); + verify(lifecycleManager).create(spec.capture()); + assertEquals("floci-aws-rds-db1", spec.getValue().name(), + "the container is renamed to the current prefix"); + verify(lifecycleManager).ensureVolume("floci-rds-db1"); + verify(lifecycleManager, org.mockito.Mockito.never()).ensureVolume("floci-aws-rds-db1"); + // A pre-migration container under the old name holds the volume's engine lock, so it is + // cleared too before the new one starts. + verify(lifecycleManager).removeIfExistsStrict("floci-rds-db1"); + verify(lifecycleManager).removeIfExistsStrict("floci-aws-rds-db1"); } @Test @@ -1124,7 +1161,7 @@ class RdsContainerManagerTest { var spec = org.mockito.ArgumentCaptor.forClass(ContainerSpec.class); verify(lifecycleManager).removeIfExistsStrict("floci-rds-db1"); verify(lifecycleManager).create(spec.capture()); - assertEquals("floci-rds-db1", spec.getValue().name()); + assertEquals("floci-aws-rds-db1", spec.getValue().name()); assertEquals("db1", handle.getRuntimeId()); manager.stop(handle); verify(lifecycleManager).stopAndRemoveStrict( diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java index c83d56fa5e..cec992c121 100644 --- a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManagerTest.java @@ -309,7 +309,7 @@ class RedshiftContainerManagerTest { manager.stop(ACCOUNT_ID, "test-cluster"); assertTrue(manager.getContainer(ACCOUNT_ID, "test-cluster").isEmpty()); - verify(lifecycleManager).removeIfExists("floci-redshift-" + ACCOUNT_ID + "-test-cluster"); + verify(lifecycleManager).removeIfExists("floci-aws-redshift-" + ACCOUNT_ID + "-test-cluster"); } @Test @@ -333,7 +333,7 @@ class RedshiftContainerManagerTest { @Test void adoptOrStartAdoptsExistingContainerInsteadOfRecreatingIt() { - String containerName = "floci-redshift-" + ACCOUNT_ID + "-test-cluster"; + String containerName = "floci-aws-redshift-" + ACCOUNT_ID + "-test-cluster"; Container existing = mock(Container.class); when(existing.getId()).thenReturn("cont-existing"); when(lifecycleManager.findByName(containerName)).thenReturn(Optional.of(existing)); @@ -352,7 +352,7 @@ class RedshiftContainerManagerTest { @Test void adoptOrStartFallsBackToStartWhenNoExistingContainer() { - String containerName = "floci-redshift-" + ACCOUNT_ID + "-test-cluster"; + String containerName = "floci-aws-redshift-" + ACCOUNT_ID + "-test-cluster"; when(lifecycleManager.findByName(containerName)).thenReturn(Optional.empty()); ContainerBuilder.Builder specBuilder = mock(ContainerBuilder.Builder.class, org.mockito.Mockito.RETURNS_SELF); when(containerBuilder.newContainer(anyString())).thenReturn(specBuilder); @@ -523,7 +523,7 @@ class RedshiftContainerManagerTest { @Test void adoptOrStartAttachesLogStreamerAndToleratesFailure() throws Exception { - String containerName = "floci-redshift-" + ACCOUNT_ID + "-test-cluster"; + String containerName = "floci-aws-redshift-" + ACCOUNT_ID + "-test-cluster"; Container existing = mock(Container.class); when(existing.getId()).thenReturn("cont-adopt-stream"); when(lifecycleManager.findByName(containerName)).thenReturn(Optional.of(existing)); diff --git a/src/test/java/io/github/hectorvent/floci/services/sagemaker/SageMakerDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/sagemaker/SageMakerDockerIntegrationTest.java index 83fad625b7..eccce01533 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sagemaker/SageMakerDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sagemaker/SageMakerDockerIntegrationTest.java @@ -122,7 +122,7 @@ class SageMakerDockerIntegrationTest { """.formatted(job, bucket)).then().statusCode(200); // Wait for the container to actually be running, blocked on an empty pipe, so the stop // races a real running container rather than one still being staged. - awaitContainerRunning("floci-sagemaker-training-" + job); + awaitContainerRunning("floci-aws-sagemaker-training-" + job); post("SageMaker.StopTrainingJob", "{\"TrainingJobName\":\"%s\"}".formatted(job)).then().statusCode(200); post("SageMaker.DescribeTrainingJob", "{\"TrainingJobName\":\"%s\"}".formatted(job)) .then().statusCode(200).body("TrainingJobStatus", equalTo("Stopped")); @@ -154,7 +154,7 @@ class SageMakerDockerIntegrationTest { // rather than polling for the container means a container that is created and discarded // between two polls is still seen: events are queued and replayed from the watch's start. try (ContainerDestroyWatch discarded = - new ContainerDestroyWatch(dockerClient, "floci-sagemaker-endpoint-" + endpoint)) { + new ContainerDestroyWatch(dockerClient, "floci-aws-sagemaker-endpoint-" + endpoint)) { post("SageMaker.CreateEndpoint", "{\"EndpointName\":\"%s\",\"EndpointConfigName\":\"%s\"}".formatted(endpoint, cfg)) .then().statusCode(200); post("SageMaker.DeleteEndpoint", "{\"EndpointName\":\"%s\"}".formatted(endpoint)).then().statusCode(200); diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java index 200947cb44..d3e5d80047 100644 --- a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/TimestreamInfluxDbServiceTest.java @@ -41,6 +41,9 @@ class TimestreamInfluxDbServiceTest { private static final String ACCOUNT = "000000000000"; private static final String REGION = "us-east-1"; + private static final String CURRENT_PREFIX = "floci-aws-timestream-influxdb-"; + private static final String LEGACY_PREFIX = "floci-timestream-influxdb-"; + private final AccountAwareStorageBackend instances = AccountAwareStorageBackend.inMemory(ACCOUNT); private final ObjectMapper objectMapper = new ObjectMapper(); private TimestreamInfluxDbContainerManager containerManager; @@ -50,7 +53,9 @@ class TimestreamInfluxDbServiceTest { void setUp() { containerManager = mock(TimestreamInfluxDbContainerManager.class); when(containerManager.isDockerReachable()).thenReturn(true); - when(containerManager.start(anyString(), anyString(), anyString(), any(), anyMap())) + when(containerManager.volumeName(anyString())).thenAnswer(inv -> CURRENT_PREFIX + inv.getArgument(0)); + when(containerManager.legacyVolumeName(anyString())).thenAnswer(inv -> LEGACY_PREFIX + inv.getArgument(0)); + when(containerManager.start(anyString(), anyString(), anyString(), anyString(), any(), anyMap())) .thenReturn(new InfluxDbEndpoint("container-1", "localhost", 18086)); SecretsManagerService secrets = mock(SecretsManagerService.class); Secret secret = mock(Secret.class); @@ -58,7 +63,7 @@ class TimestreamInfluxDbServiceTest { when(secrets.createSecret(anyString(), anyString(), isNull(), anyString(), isNull(), isNull(), eq(REGION))) .thenReturn(secret); service = new TimestreamInfluxDbService( - AccountAwareStorageBackend.inMemory(ACCOUNT), + instances, AccountAwareStorageBackend.inMemory(ACCOUNT), AccountAwareStorageBackend.inMemory(ACCOUNT), AccountAwareStorageBackend.inMemory(ACCOUNT), @@ -74,7 +79,8 @@ class TimestreamInfluxDbServiceTest { """), REGION).resource(); ArgumentCaptor setup = ArgumentCaptor.forClass(InfluxDbSetup.class); - verify(containerManager).start(eq(created.getId()), eq(ACCOUNT), eq(REGION), setup.capture(), eq(Map.of())); + verify(containerManager).start(eq(created.getId()), eq(CURRENT_PREFIX + created.getId()), eq(ACCOUNT), eq(REGION), + setup.capture(), eq(Map.of())); assertEquals(new InfluxDbSetup("operator", "password123", "acme", "metrics"), setup.getValue()); DbInstance stored = service.getDbInstance(json("{\"identifier\":\"" + created.getId() + "\"}"), REGION); @@ -102,7 +108,7 @@ class TimestreamInfluxDbServiceTest { DbInstance stored = service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION); assertEquals("AVAILABLE", stored.getStatus()); assertNull(stored.getContainerId()); - verify(containerManager, never()).start(anyString(), anyString(), anyString(), any(), anyMap()); + verify(containerManager, never()).start(anyString(), anyString(), anyString(), anyString(), any(), anyMap()); } @Test @@ -114,7 +120,7 @@ class TimestreamInfluxDbServiceTest { createInstance("tuned-db", groupId); - verify(containerManager).start(anyString(), eq(ACCOUNT), eq(REGION), any(), + verify(containerManager).start(anyString(), anyString(), eq(ACCOUNT), eq(REGION), any(), eq(Map.of("INFLUXD_LOG_LEVEL", "debug", "INFLUXD_HTTP_IDLE_TIMEOUT", "5m"))); } @@ -128,7 +134,8 @@ class TimestreamInfluxDbServiceTest { service.updateDbInstance(json("{\"identifier\":\"" + id + "\",\"dbParameterGroupIdentifier\":\"" + groupId + "\"}"), REGION); - verify(containerManager).start(eq(id), eq(ACCOUNT), eq(REGION), isNull(), eq(Map.of("INFLUXD_UI_DISABLED", "true"))); + verify(containerManager).start(eq(id), eq(CURRENT_PREFIX + id), eq(ACCOUNT), eq(REGION), isNull(), + eq(Map.of("INFLUXD_UI_DISABLED", "true"))); assertEquals("AVAILABLE", service.getDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION).getStatus()); } @@ -142,7 +149,7 @@ class TimestreamInfluxDbServiceTest { service.deleteDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION); verify(containerManager).stop(id, "container-1"); - verify(containerManager).removeStorage(id); + verify(containerManager).removeStorage(CURRENT_PREFIX + id); } @Test @@ -166,7 +173,7 @@ class TimestreamInfluxDbServiceTest { assertEquals("COMPLETED", stored.getStatus()); assertTrue(stored.isDataCaptured()); - when(containerManager.start(anyString(), anyString(), anyString(), any(), anyMap())) + when(containerManager.start(anyString(), anyString(), anyString(), anyString(), any(), anyMap())) .thenReturn(new InfluxDbEndpoint("container-2", "localhost", 18087)); RestoreResult result = service.restoreFromDbBackup( json("{\"name\":\"restored\",\"dbBackupId\":\"" + backup.getId() + "\"}"), REGION); @@ -202,7 +209,7 @@ class TimestreamInfluxDbServiceTest { "vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]} """), REGION).resource().getId(); - verify(containerManager).start(eq(clusterId), eq(ACCOUNT), eq(REGION), any(), anyMap()); + verify(containerManager).start(eq(clusterId), eq(CURRENT_PREFIX + clusterId), eq(ACCOUNT), eq(REGION), any(), anyMap()); DbCluster cluster = service.getDbCluster(json("{\"dbClusterId\":\"" + clusterId + "\"}"), REGION); assertEquals("AVAILABLE", cluster.getStatus()); List members = service.listDbInstancesForCluster( @@ -224,7 +231,7 @@ class TimestreamInfluxDbServiceTest { "vpcSubnetIds":["subnet-abc"],"vpcSecurityGroupIds":["sg-abc"]} """.formatted(groupId)), REGION).resource().getId(); - verify(containerManager, never()).start(anyString(), anyString(), anyString(), any(), anyMap()); + verify(containerManager, never()).start(anyString(), anyString(), anyString(), anyString(), any(), anyMap()); DbCluster cluster = service.getDbCluster(json("{\"dbClusterId\":\"" + clusterId + "\"}"), REGION); assertEquals("AVAILABLE", cluster.getStatus()); assertEquals(8181, cluster.getPort()); @@ -290,6 +297,24 @@ class TimestreamInfluxDbServiceTest { assertEquals("ValidationException", error.getErrorCode()); } + /** + * The upgrade path: a record persisted before the volume name was stamped is backfilled with + * the legacy name, so deleting it removes the volumes its data lives in, not freshly named + * empty ones. + */ + @Test + void recordsWithoutAPersistedVolumeNameKeepTheirLegacyVolumes() throws Exception { + String id = createInstance("legacy-db", null).getId(); + DbInstance stored = instances.getForAccount(ACCOUNT, REGION + ":" + id).orElseThrow(); + stored.setDockerVolumeName(null); + instances.putForAccount(ACCOUNT, REGION + ":" + id, stored); + + service.deleteDbInstance(json("{\"identifier\":\"" + id + "\"}"), REGION); + + verify(containerManager).removeStorage(LEGACY_PREFIX + id); + assertEquals(LEGACY_PREFIX + id, stored.getDockerVolumeName()); + } + private DbInstance createInstance(String name, String parameterGroupId) throws Exception { String groupField = parameterGroupId == null ? "" : ",\"dbParameterGroupIdentifier\":\"" + parameterGroupId + "\""; return service.createDbInstance(json(""" diff --git a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java index d7fa1fdc5e..d2fadf9828 100644 --- a/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/timestreaminfluxdb/container/TimestreamInfluxDbContainerManagerTest.java @@ -33,6 +33,7 @@ class TimestreamInfluxDbContainerManagerTest { private ContainerBuilder.Builder builder; private PortAllocator portAllocator; private TimestreamInfluxDbContainerManager manager; + private EmulatorConfig.StorageConfig storage; @BeforeEach void setUp() { @@ -47,7 +48,7 @@ class TimestreamInfluxDbContainerManagerTest { EmulatorConfig config = mock(EmulatorConfig.class); EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class); EmulatorConfig.TimestreamInfluxDbServiceConfig influx = mock(EmulatorConfig.TimestreamInfluxDbServiceConfig.class); - EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class); + storage = mock(EmulatorConfig.StorageConfig.class); when(config.services()).thenReturn(services); when(config.storage()).thenReturn(storage); when(services.timestreamInfluxdb()).thenReturn(influx); @@ -65,7 +66,7 @@ class TimestreamInfluxDbContainerManagerTest { when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id", Map.of(8086, new EndpointInfo("localhost", 8090)))); - InfluxDbEndpoint endpoint = manager.start("abc1234567", "000000000000", "us-east-1", + InfluxDbEndpoint endpoint = manager.start("abc1234567", "floci-aws-timestream-influxdb-abc1234567", "000000000000", "us-east-1", new InfluxDbSetup("admin", "password123", "acme", "metrics"), Map.of("INFLUXD_LOG_LEVEL", "debug")); assertEquals(new InfluxDbEndpoint("container-id", "localhost", 8090), endpoint); @@ -76,8 +77,8 @@ class TimestreamInfluxDbContainerManagerTest { verify(builder).withEnv("DOCKER_INFLUXDB_INIT_BUCKET", "metrics"); verify(builder).withEnv("INFLUXD_LOG_LEVEL", "debug"); verify(builder).withPortBinding(8086, 8090); - verify(builder).withNamedVolume("floci-timestream-influxdb-abc1234567", "/var/lib/influxdb2"); - verify(builder).withNamedVolume("floci-timestream-influxdb-abc1234567-config", "/etc/influxdb2"); + verify(builder).withNamedVolume("floci-aws-timestream-influxdb-abc1234567", "/var/lib/influxdb2"); + verify(builder).withNamedVolume("floci-aws-timestream-influxdb-abc1234567-config", "/etc/influxdb2"); verify(builder).withLabels(Map.of( "io.floci", "aws", "io.floci.service", "timestream-influxdb", @@ -91,7 +92,7 @@ class TimestreamInfluxDbContainerManagerTest { when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id", Map.of(8086, new EndpointInfo("localhost", 8090)))); - manager.start("abc1234567", "000000000000", "us-east-1", null, Map.of()); + manager.start("abc1234567", "floci-aws-timestream-influxdb-abc1234567", "000000000000", "us-east-1", null, Map.of()); verify(builder, never()).withEnv("DOCKER_INFLUXDB_INIT_MODE", "setup"); } @@ -100,18 +101,47 @@ class TimestreamInfluxDbContainerManagerTest { void failedStartRemovesTheContainerAndReleasesThePort() { when(lifecycleManager.createAndStart(any())).thenThrow(new RuntimeException("port already allocated")); - assertThrows(RuntimeException.class, () -> manager.start("abc1234567", "000000000000", "us-east-1", + assertThrows(RuntimeException.class, () -> manager.start("abc1234567", "floci-aws-timestream-influxdb-abc1234567", "000000000000", "us-east-1", new InfluxDbSetup("admin", "password123", "acme", "metrics"), Map.of())); verify(portAllocator).release(8090); - verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-timestream-influxdb-abc1234567"); + verify(lifecycleManager, Mockito.times(2)).removeIfExists("floci-aws-timestream-influxdb-abc1234567"); + } + + @Test + void volumeNamesCarryTheCurrentAndTheLegacyPrefix() { + assertEquals("floci-aws-timestream-influxdb-abc1234567", manager.volumeName("abc1234567")); + assertEquals("floci-timestream-influxdb-abc1234567", manager.legacyVolumeName("abc1234567")); + } + + /** A record from before the migration keeps mounting the legacy-named volumes it was created with. */ + @Test + void startMountsThePersistedVolumeNameRatherThanRecomputingIt() { + when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id", + Map.of(8086, new EndpointInfo("localhost", 8090)))); + + manager.start("abc1234567", "floci-timestream-influxdb-abc1234567", "000000000000", "us-east-1", null, Map.of()); + + verify(builder).withNamedVolume("floci-timestream-influxdb-abc1234567", "/var/lib/influxdb2"); + verify(builder).withNamedVolume("floci-timestream-influxdb-abc1234567-config", "/etc/influxdb2"); + verify(builder).withName("floci-aws-timestream-influxdb-abc1234567"); + } + + @Test + void removeStorageRemovesTheDataAndConfigVolumesByTheirPersistedName() { + when(storage.mode()).thenReturn("memory"); + + manager.removeStorage("floci-timestream-influxdb-abc1234567"); + + verify(lifecycleManager).removeVolume("floci-timestream-influxdb-abc1234567"); + verify(lifecycleManager).removeVolume("floci-timestream-influxdb-abc1234567-config"); } @Test void stopReleasesThePublishedPort() { when(lifecycleManager.createAndStart(any())).thenReturn(new ContainerInfo("container-id", Map.of(8086, new EndpointInfo("localhost", 8090)))); - manager.start("abc1234567", "000000000000", "us-east-1", null, Map.of()); + manager.start("abc1234567", "floci-aws-timestream-influxdb-abc1234567", "000000000000", "us-east-1", null, Map.of()); manager.stop("abc1234567", "container-id");