mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 08:31:53 -05:00
feat(docker): own the floci-aws- container and volume prefix (#4273)
Every container and volume Floci creates was named floci-<service>-<id>. Every Floci emulator shared that prefix, so two of them on one Docker daemon collided on names, and docker volume prune --filter label=floci=true reached all of them at once. Each emulator now owns a prefix of its own; this one uses floci-aws-, and labels its resources floci_emulator=floci-aws. Existing data is neither moved nor lost. Resources with a record (RDS, MSK, OpenSearch, Amazon MQ, Kinesis Analytics savepoints, Timestream InfluxDB) persist the volume name they were created with; records from before the field existed are backfilled with the frozen legacy name so their data keeps resolving. Volumes with no record (the ECR registry, EFS-backed task and Lambda volumes) are found by probing the legacy name first. Containers are disposable and always take the current name; the UI console and the ECR registry, which survive shutdown and are adopted by name, are looked up under both prefixes so an upgrade does not orphan one that still holds its host port. Stale-container cleanup clears both names. Scripts that name containers (docker exec floci-rds-<id>) must be updated, and downgrading does not migrate back: a record written after the upgrade carries a floci-aws- name an older build will not look for. Documented in storage.md.
This commit is contained in:
1 parent
bfedf6c679
commit
836161e1ff
59 files changed
+874
-255
No files matched your search
+80
-17
@@ -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-<cloud>-} convention.
|
||||
*
|
||||
* <p>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-<namespace>-<base>}.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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. */
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+22
-9
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -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(),
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+24
-8
@@ -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) {
|
||||
|
||||
+43
-7
@@ -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 <account>.dkr.ecr.<region>.amazonaws.com/<repo>[: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<String> 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() {
|
||||
|
||||
+23
-7
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<Container> 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())) {
|
||||
|
||||
+5
@@ -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);
|
||||
|
||||
+31
-5
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+11
@@ -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;
|
||||
|
||||
|
||||
+29
-5
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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; }
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
+18
-6
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>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) {
|
||||
|
||||
+17
-3
@@ -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
|
||||
|
||||
+3
-2
@@ -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) {
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+58
-27
@@ -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<DbCluster> 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<String, String> engineEnvironment, String backupId,
|
||||
String failureStatus) {
|
||||
String dataVolumeName, InfluxDbSetup setup, Map<String, String> 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<DbInstance> 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<DbCluster> 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");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+25
-9
@@ -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<String, String> 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);
|
||||
|
||||
+9
@@ -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; }
|
||||
|
||||
+9
@@ -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; }
|
||||
|
||||
Reference in new issue
Block a user