mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(codeartifact): retain unhealthy sidecars during active requests (#8692)
* fix(codeartifact): retain unhealthy sidecars during active requests * fix(codeartifact): drain retired sidecar cleanup after failures * fix(codeartifact): keep replacement sidecar names distinct * fix(codeartifact): keep repository admission independent of startup * fix(codeartifact): reclaim untracked repository sidecars * fix(codeartifact): share run-labelled container cleanup
This commit is contained in:
1 parent
b2f550e6be
commit
07f6dea386
22 files changed
+832
-33
No files matched your search
@@ -2,9 +2,11 @@ package io.floci.aws.core.common.docker;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
@@ -46,6 +48,68 @@ public class PerKeyContainerPool {
|
||||
* nothing left able to ever stop it again.
|
||||
*/
|
||||
private final AtomicLong generation = new AtomicLong();
|
||||
private final ConcurrentHashMap<String, Integer> activeUses = new ConcurrentHashMap<>();
|
||||
private final ConcurrentHashMap<String, List<StartedContainer>> retiredContainers = new ConcurrentHashMap<>();
|
||||
|
||||
/** Protects every container observed during an admitted use, including an unhealthy predecessor. */
|
||||
public void beginUse(String key) {
|
||||
activeUses.merge(key, 1, Integer::sum);
|
||||
}
|
||||
|
||||
/** Releases a use and removes retired containers once the last admitted use has finished. */
|
||||
public void endUse(String key) {
|
||||
Object lock = startLocks.computeIfAbsent(key, k -> new Object());
|
||||
synchronized (lock) {
|
||||
AtomicBoolean drained = new AtomicBoolean();
|
||||
activeUses.compute(key, (k, active) -> {
|
||||
if (active == null) {
|
||||
throw new IllegalStateException("No active container use for key " + key);
|
||||
}
|
||||
drained.set(active == 1);
|
||||
return active == 1 ? null : active - 1;
|
||||
});
|
||||
if (drained.get()) {
|
||||
stopRetired(key, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void stopRetired(String key, boolean strict) {
|
||||
List<StartedContainer> retired = retiredContainers.get(key);
|
||||
if (retired == null) {
|
||||
return;
|
||||
}
|
||||
RuntimeException failure = null;
|
||||
for (StartedContainer container : List.copyOf(retired)) {
|
||||
try {
|
||||
lifecycleManager.stopAndRemoveStrict(container.containerId(), null);
|
||||
retired.remove(container);
|
||||
} catch (RuntimeException e) {
|
||||
if (strict) {
|
||||
failure = accumulateFailure(failure, e);
|
||||
} else {
|
||||
LOG.warnv(e, "Could not remove retired sidecar container {0} for key {1}; cleanup can retry",
|
||||
container.containerId(), key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (retired.isEmpty()) {
|
||||
retiredContainers.remove(key, retired);
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private static RuntimeException accumulateFailure(RuntimeException failure, RuntimeException next) {
|
||||
if (failure == null) {
|
||||
return next;
|
||||
}
|
||||
if (failure != next) {
|
||||
failure.addSuppressed(next);
|
||||
}
|
||||
return failure;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param healthPath path (e.g. {@code /-/ping}) appended to a container's base URL to probe
|
||||
@@ -75,7 +139,21 @@ public class PerKeyContainerPool {
|
||||
}
|
||||
if (existing != null) {
|
||||
LOG.warnv("Sidecar container for key {0} is no longer healthy; restarting it", key);
|
||||
lifecycleManager.stopAndRemove(existing.containerId(), null);
|
||||
StartedContainer predecessor = existing;
|
||||
AtomicBoolean retained = new AtomicBoolean();
|
||||
// Admission and detachment share a short map operation, never startup or Docker I/O.
|
||||
// A use admitted after detachment cannot discover the predecessor in containers.
|
||||
activeUses.compute(key, (k, active) -> {
|
||||
retained.set(active != null);
|
||||
if (active != null) {
|
||||
retiredContainers.computeIfAbsent(key, unused -> new ArrayList<>()).add(predecessor);
|
||||
}
|
||||
containers.remove(key, predecessor);
|
||||
return active;
|
||||
});
|
||||
if (!retained.get()) {
|
||||
lifecycleManager.stopAndRemove(existing.containerId(), null);
|
||||
}
|
||||
}
|
||||
long startedGeneration = generation.get();
|
||||
StartedContainer started = starter.start();
|
||||
@@ -112,7 +190,7 @@ public class PerKeyContainerPool {
|
||||
* brand-new one just to find out it has nothing.
|
||||
*/
|
||||
public boolean isTracked(String key) {
|
||||
return containers.containsKey(key);
|
||||
return containers.containsKey(key) || retiredContainers.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,14 +216,27 @@ public class PerKeyContainerPool {
|
||||
}
|
||||
Object lock = startLocks.computeIfAbsent(key, k -> new Object());
|
||||
synchronized (lock) {
|
||||
RuntimeException failure = null;
|
||||
try {
|
||||
stopRetired(key, strict);
|
||||
} catch (RuntimeException e) {
|
||||
failure = e;
|
||||
}
|
||||
StartedContainer container = containers.get(key);
|
||||
if (container != null) {
|
||||
if (strict) {
|
||||
lifecycleManager.stopAndRemoveStrict(container.containerId(), null);
|
||||
} else {
|
||||
lifecycleManager.stopAndRemove(container.containerId(), null);
|
||||
try {
|
||||
if (strict) {
|
||||
lifecycleManager.stopAndRemoveStrict(container.containerId(), null);
|
||||
} else {
|
||||
lifecycleManager.stopAndRemove(container.containerId(), null);
|
||||
}
|
||||
containers.remove(key, container);
|
||||
} catch (RuntimeException e) {
|
||||
failure = accumulateFailure(failure, e);
|
||||
}
|
||||
containers.remove(key, container);
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,6 +244,16 @@ public class PerKeyContainerPool {
|
||||
/** Stops and removes every container this pool has started, then forgets them all. */
|
||||
public void stopAll() {
|
||||
generation.incrementAndGet();
|
||||
for (String key : List.copyOf(retiredContainers.keySet())) {
|
||||
Object lock = startLocks.computeIfAbsent(key, k -> new Object());
|
||||
synchronized (lock) {
|
||||
try {
|
||||
stopRetired(key, false);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.debugv(e, "Failed to stop retired sidecar containers for key {0}", key);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (containers.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -171,7 +272,10 @@ public class PerKeyContainerPool {
|
||||
public void stopAllStrictExcept(Set<String> protectedKeys) {
|
||||
generation.incrementAndGet();
|
||||
RuntimeException failure = null;
|
||||
for (String key : List.copyOf(containers.keySet())) {
|
||||
Set<String> keys = ConcurrentHashMap.newKeySet();
|
||||
keys.addAll(containers.keySet());
|
||||
keys.addAll(retiredContainers.keySet());
|
||||
for (String key : keys) {
|
||||
if (protectedKeys.contains(key)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -9,10 +9,14 @@ import io.floci.aws.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.floci.aws.core.common.docker.ContainerSpec;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.common.docker.PerKeyContainerPool;
|
||||
import io.floci.aws.core.common.docker.RunLabelledContainers;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Starts and stops one BaGetter container per CodeArtifact repository backing the {@code nuget}
|
||||
* format, the same per-repository shape as Verdaccio (npm) and pypiserver (pypi) rather than
|
||||
@@ -47,13 +51,15 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final SidecarRepositoryLease lease;
|
||||
private final RunLabelledContainers runContainers;
|
||||
|
||||
@Inject
|
||||
public BagetterSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
EmulatorConfig config) {
|
||||
EmulatorConfig config, @Named("codeartifact") RunLabelledContainers runContainers) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.runContainers = runContainers;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
this.lease = new SidecarRepositoryLease("BaGetter", pool);
|
||||
}
|
||||
@@ -101,8 +107,9 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
|
||||
private PerKeyContainerPool.StartedContainer startContainer(String nugetRepositoryId) {
|
||||
String image = config.services().codeartifact().nugetImage();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-bagetter-" + nugetRepositoryId);
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
runContainers.removeLeftovers();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-bagetter-" + nugetRepositoryId
|
||||
+ "-" + UUID.randomUUID());
|
||||
|
||||
// Loopback-only: this container authenticates nothing on its own (ApiKey left empty
|
||||
// below), so the Bearer/Basic check in CodeArtifactNuGetController is the only thing
|
||||
@@ -116,6 +123,8 @@ public class BagetterSidecarManager implements ContainerTeardown {
|
||||
// an explicit path.
|
||||
ContainerSpec spec = containerBuilder.newContainer(image)
|
||||
.withName(containerName)
|
||||
.withLabels(ContainerStorageHelper.resourceIdentityLabels("codeartifact", nugetRepositoryId, null, null))
|
||||
.withLabels(runContainers.labels())
|
||||
.withEnv("ApiKey", "")
|
||||
// Already the image's own shipped default; set explicitly so a future image
|
||||
// default change can't quietly start resolving packages from the real nuget.org,
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.floci.aws.services.codeartifact;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.docker.ContainerLifecycleManager;
|
||||
import io.floci.aws.core.common.docker.RunLabelledContainers;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.inject.Produces;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import jakarta.inject.Singleton;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/** One process identity shared by all repository formats, including their admitted predecessors. */
|
||||
@ApplicationScoped
|
||||
public class CodeArtifactContainerRuns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CodeArtifactContainerRuns.class);
|
||||
private final RunLabelledContainers containers;
|
||||
|
||||
@Inject
|
||||
public CodeArtifactContainerRuns(ContainerLifecycleManager lifecycleManager, EmulatorConfig config) {
|
||||
containers = new RunLabelledContainers(lifecycleManager, config, "codeartifact", "io.floci.codeartifact.run", LOG);
|
||||
}
|
||||
|
||||
@Produces
|
||||
@Singleton
|
||||
@Named("codeartifact")
|
||||
public RunLabelledContainers containers() {
|
||||
return containers;
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,14 @@ import io.floci.aws.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.floci.aws.core.common.docker.ContainerSpec;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.common.docker.PerKeyContainerPool;
|
||||
import io.floci.aws.core.common.docker.RunLabelledContainers;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@@ -59,6 +62,7 @@ public class KellnrSidecarManager implements ContainerTeardown {
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final SidecarRepositoryLease lease;
|
||||
private final RunLabelledContainers runContainers;
|
||||
// One random admin password per repository's own container, regenerated every time
|
||||
// startContainer() runs for that key (including an internal pool restart of an unhealthy
|
||||
// container under the same key): KellnrSidecarClient reads this fresh on every admin login
|
||||
@@ -67,10 +71,11 @@ public class KellnrSidecarManager implements ContainerTeardown {
|
||||
|
||||
@Inject
|
||||
public KellnrSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
EmulatorConfig config) {
|
||||
EmulatorConfig config, @Named("codeartifact") RunLabelledContainers runContainers) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.runContainers = runContainers;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
this.lease = new SidecarRepositoryLease("Kellnr", pool);
|
||||
}
|
||||
@@ -146,8 +151,9 @@ public class KellnrSidecarManager implements ContainerTeardown {
|
||||
|
||||
private PerKeyContainerPool.StartedContainer startContainer(String cargoRepositoryId) {
|
||||
String image = config.services().codeartifact().cargoImage();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-kellnr-" + cargoRepositoryId);
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
runContainers.removeLeftovers();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-kellnr-" + cargoRepositoryId
|
||||
+ "-" + UUID.randomUUID());
|
||||
|
||||
// Set before the container is created, not after: KellnrSidecarClient's own adminLogin
|
||||
// reads this map directly, so the new password must already be visible to it the moment
|
||||
@@ -165,6 +171,8 @@ public class KellnrSidecarManager implements ContainerTeardown {
|
||||
// live), see this class's own javadoc for why that default is never left in place.
|
||||
ContainerSpec spec = containerBuilder.newContainer(image)
|
||||
.withName(containerName)
|
||||
.withLabels(ContainerStorageHelper.resourceIdentityLabels("codeartifact", cargoRepositoryId, null, null))
|
||||
.withLabels(runContainers.labels())
|
||||
.withEnv("KELLNR_ORIGIN__HOSTNAME", "localhost")
|
||||
.withEnv("KELLNR_SETUP__ADMIN_PWD", adminPassword)
|
||||
.withLoopbackPortBinding(KELLNR_PORT, 0)
|
||||
|
||||
@@ -9,11 +9,14 @@ import io.floci.aws.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.floci.aws.core.common.docker.ContainerSpec;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.common.docker.PerKeyContainerPool;
|
||||
import io.floci.aws.core.common.docker.RunLabelledContainers;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Starts and stops one pypiserver container per CodeArtifact repository backing the {@code pypi}
|
||||
@@ -40,13 +43,15 @@ public class PypiserverSidecarManager implements ContainerTeardown {
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final SidecarRepositoryLease lease;
|
||||
private final RunLabelledContainers runContainers;
|
||||
|
||||
@Inject
|
||||
public PypiserverSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
EmulatorConfig config) {
|
||||
EmulatorConfig config, @Named("codeartifact") RunLabelledContainers runContainers) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.runContainers = runContainers;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
this.lease = new SidecarRepositoryLease("pypiserver", pool);
|
||||
}
|
||||
@@ -93,8 +98,9 @@ public class PypiserverSidecarManager implements ContainerTeardown {
|
||||
|
||||
private PerKeyContainerPool.StartedContainer startContainer(String pypiRepositoryId) {
|
||||
String image = config.services().codeartifact().pypiImage();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-pypiserver-" + pypiRepositoryId);
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
runContainers.removeLeftovers();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-pypiserver-" + pypiRepositoryId
|
||||
+ "-" + UUID.randomUUID());
|
||||
|
||||
// Loopback-only: this container authenticates nothing on its own (auth disabled below), so
|
||||
// the Bearer check in CodeArtifactPypiController is the only thing standing between a
|
||||
@@ -102,6 +108,8 @@ public class PypiserverSidecarManager implements ContainerTeardown {
|
||||
// can reach the host bypass that check entirely, the same reasoning as Verdaccio's binding.
|
||||
ContainerSpec spec = containerBuilder.newContainer(image)
|
||||
.withName(containerName)
|
||||
.withLabels(ContainerStorageHelper.resourceIdentityLabels("codeartifact", pypiRepositoryId, null, null))
|
||||
.withLabels(runContainers.labels())
|
||||
.withLoopbackPortBinding(PYPISERVER_PORT, 0)
|
||||
.withDockerNetwork(config.services().dockerNetwork())
|
||||
.withEmbeddedDns()
|
||||
|
||||
@@ -41,6 +41,7 @@ final class SidecarRepositoryLease {
|
||||
throw new IllegalStateException(sidecarName + " sidecar is being released for repository "
|
||||
+ repositoryId);
|
||||
}
|
||||
pool.beginUse(repositoryId);
|
||||
use.active++;
|
||||
}
|
||||
}
|
||||
@@ -58,6 +59,7 @@ final class SidecarRepositoryLease {
|
||||
use.active--;
|
||||
}
|
||||
try {
|
||||
pool.endUse(repositoryId);
|
||||
stopIfUnused(repositoryId, use);
|
||||
} catch (RuntimeException e) {
|
||||
LOG.warnv(e, "Could not stop {0} sidecar for repository {1} after its last use; "
|
||||
|
||||
@@ -10,11 +10,14 @@ import io.floci.aws.core.common.docker.ContainerSpec;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.common.docker.PerKeyContainerPool;
|
||||
import io.floci.aws.core.common.docker.RetryingTarCopier;
|
||||
import io.floci.aws.core.common.docker.RunLabelledContainers;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Named;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Starts and stops one Verdaccio container per CodeArtifact npm repository, unlike Reposilite's one
|
||||
@@ -70,13 +73,15 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
private final EmulatorConfig config;
|
||||
private final PerKeyContainerPool pool;
|
||||
private final SidecarRepositoryLease lease;
|
||||
private final RunLabelledContainers runContainers;
|
||||
|
||||
@Inject
|
||||
public VerdaccioSidecarManager(ContainerBuilder containerBuilder, ContainerLifecycleManager lifecycleManager,
|
||||
EmulatorConfig config) {
|
||||
EmulatorConfig config, @Named("codeartifact") RunLabelledContainers runContainers) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.config = config;
|
||||
this.runContainers = runContainers;
|
||||
this.pool = new PerKeyContainerPool(lifecycleManager, HEALTH_PATH);
|
||||
this.lease = new SidecarRepositoryLease("Verdaccio", pool);
|
||||
}
|
||||
@@ -124,8 +129,9 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
|
||||
private PerKeyContainerPool.StartedContainer startContainer(String npmRepositoryId, String publicUrl) {
|
||||
String image = config.services().codeartifact().npmImage();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-verdaccio-" + npmRepositoryId);
|
||||
lifecycleManager.removeIfExists(containerName);
|
||||
runContainers.removeLeftovers();
|
||||
String containerName = ContainerStorageHelper.dockerName(config, "floci-verdaccio-" + npmRepositoryId
|
||||
+ "-" + UUID.randomUUID());
|
||||
|
||||
// Loopback-only: this container has no auth of its own (open $all access, matching the
|
||||
// Maven proxy's own deliberate choice not to resolve upstreams), so the Bearer check in
|
||||
@@ -134,6 +140,8 @@ public class VerdaccioSidecarManager implements ContainerTeardown {
|
||||
// bypass that check entirely.
|
||||
ContainerSpec spec = containerBuilder.newContainer(image)
|
||||
.withName(containerName)
|
||||
.withLabels(ContainerStorageHelper.resourceIdentityLabels("codeartifact", npmRepositoryId, null, null))
|
||||
.withLabels(runContainers.labels())
|
||||
.withEnv("VERDACCIO_PUBLIC_URL", publicUrl)
|
||||
.withLoopbackPortBinding(VERDACCIO_PORT, 0)
|
||||
.withDockerNetwork(config.services().dockerNetwork())
|
||||
|
||||
Reference in new issue
Block a user