mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ecs): stop siblings when an essential container exits (#8321)
This commit is contained in:
1 parent
d08c241d98
commit
4649d6c683
4 files changed
+580
-24
No files matched your search
@@ -154,10 +154,10 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
private static final long LAUNCHER_SHUTDOWN_SECONDS = 30;
|
||||
// Runs the service scheduler's Docker stops (see stopInBackground). A container teardown drains
|
||||
// and shuts it down, and afterReset() replaces it, as with the reconciler.
|
||||
private volatile ExecutorService taskStopper = newTaskStopper();
|
||||
private volatile ExecutorService taskStopper;
|
||||
// Runs RunTask's and StartTask's launches after the call has answered. A container teardown
|
||||
// interrupts and shuts it down, and afterReset() replaces it, as with the task stopper.
|
||||
private volatile ExecutorService taskLauncher = newTaskLauncher();
|
||||
private volatile ExecutorService taskLauncher;
|
||||
private final Object reconcilerLock = new Object();
|
||||
private final Object reconciliationPassLock = new Object();
|
||||
// False until a previous run's task containers are confirmed gone; no task starts before.
|
||||
@@ -297,6 +297,18 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
EcsExecSessionRegistry execSessions,
|
||||
EcsServiceDiscoveryRegistrar discoveryRegistrar,
|
||||
CloudWatchMetricsService cloudWatchMetricsService, Clock clock) {
|
||||
this(regionResolver, containerManager, config, lbRegistrar, storageFactory, eventPublisher,
|
||||
execSessions, discoveryRegistrar, cloudWatchMetricsService, clock, newTaskLauncher(), newTaskStopper());
|
||||
}
|
||||
|
||||
/** With explicit task executors for deterministic lifecycle tests. */
|
||||
EcsService(RegionResolver regionResolver, EcsContainerManager containerManager,
|
||||
EmulatorConfig config, EcsLoadBalancerRegistrar lbRegistrar,
|
||||
StorageFactory storageFactory, EcsEventPublisher eventPublisher,
|
||||
EcsExecSessionRegistry execSessions,
|
||||
EcsServiceDiscoveryRegistrar discoveryRegistrar,
|
||||
CloudWatchMetricsService cloudWatchMetricsService, Clock clock,
|
||||
ExecutorService taskLauncher, ExecutorService taskStopper) {
|
||||
this.regionResolver = regionResolver;
|
||||
this.containerManager = containerManager;
|
||||
this.dockerMode = !config.services().ecs().mock();
|
||||
@@ -312,6 +324,8 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
this.discoveryRegistrar = discoveryRegistrar;
|
||||
this.cloudWatchMetricsService = cloudWatchMetricsService;
|
||||
this.clock = clock;
|
||||
this.taskLauncher = taskLauncher;
|
||||
this.taskStopper = taskStopper;
|
||||
}
|
||||
|
||||
public EcsService(RegionResolver regionResolver, EcsContainerManager containerManager,
|
||||
@@ -2262,7 +2276,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
task.setStopCode(stopCode);
|
||||
Duration drain = deregisterTaskFromLoadBalancers(task, region);
|
||||
deregisterTaskFromServiceDiscovery(task, region);
|
||||
if (inBackground && !drain.isZero()) {
|
||||
if (inBackground && !drain.isZero() && !STOP_CODE_ESSENTIAL_CONTAINER_EXITED.equals(stopCode)) {
|
||||
// As on AWS, the task stays DEACTIVATING, serving its in-flight requests, until its
|
||||
// targets have drained; the first reconciliation tick after that stops it.
|
||||
task.setLastStatus(TaskStatus.DEACTIVATING.name());
|
||||
@@ -5425,20 +5439,49 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
|
||||
updateHealth(task, handle);
|
||||
|
||||
// Inspect every container; abort if any are still running. A container that exited on its
|
||||
// own has a finish time of its own, read here while the daemon still remembers it.
|
||||
// Keep inspecting after a live container: an exited essential sibling stops the task.
|
||||
Map<String, Integer> exitCodes = new LinkedHashMap<>();
|
||||
Map<String, Instant> finishedAt = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, String> entry : handle.getContainerIds().entrySet()) {
|
||||
Integer code = containerManager.getExitCodeIfStopped(entry.getValue());
|
||||
String name = entry.getKey();
|
||||
Integer code = handle.getRecordedExitCode(name);
|
||||
if (code == null) {
|
||||
code = containerManager.getExitCodeIfStopped(entry.getValue());
|
||||
}
|
||||
if (code == null) {
|
||||
continue;
|
||||
}
|
||||
handle.recordExitCode(name, code);
|
||||
exitCodes.put(name, code);
|
||||
Instant finished = handle.getFinishedAt().get(name);
|
||||
if (finished == null) {
|
||||
finished = containerManager.getFinishedAtIfStopped(entry.getValue());
|
||||
handle.recordFinishedAt(name, finished);
|
||||
}
|
||||
if (finished != null) {
|
||||
finishedAt.put(name, finished);
|
||||
}
|
||||
}
|
||||
|
||||
boolean containersChanged = updateStoppedContainers(task, exitCodes, finishedAt);
|
||||
if (exitCodes.size() != handle.getContainerIds().size()) {
|
||||
if (exitCodes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
exitCodes.put(entry.getKey(), code);
|
||||
Instant finished = containerManager.getFinishedAtIfStopped(entry.getValue());
|
||||
if (finished != null) {
|
||||
finishedAt.put(entry.getKey(), finished);
|
||||
String region = taskRegion(task);
|
||||
TaskDefinition definition = findTaskDefinition(task.getTaskDefinitionArn(), region);
|
||||
boolean essentialExited = definition != null && definition.getContainerDefinitions().stream()
|
||||
.anyMatch(container -> container.isEssential() && exitCodes.containsKey(container.getName()));
|
||||
if (essentialExited) {
|
||||
stopTaskLocked(task, "Essential container in task exited", STOP_CODE_ESSENTIAL_CONTAINER_EXITED,
|
||||
region, true);
|
||||
} else if (containersChanged) {
|
||||
task.bumpVersion();
|
||||
if (eventPublisher != null) {
|
||||
eventPublisher.emitTaskStateChange(task, region);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// All containers have exited. Atomically claim the handle to avoid
|
||||
@@ -5452,20 +5495,6 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
containerManager.cleanupStoppedTask(claimed);
|
||||
retainPendingCleanupHandle(taskArn, claimed);
|
||||
|
||||
if (task.getContainers() != null) {
|
||||
task.getContainers().forEach(c -> {
|
||||
c.setLastStatus("STOPPED");
|
||||
Integer code = exitCodes.get(c.getName());
|
||||
if (code != null) {
|
||||
c.setExitCode(code);
|
||||
}
|
||||
Instant finished = finishedAt.get(c.getName());
|
||||
if (finished != null) {
|
||||
c.setFinishedAt(finished);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
task.setLastStatus(TaskStatus.STOPPED.name());
|
||||
task.setDesiredStatus(TaskStatus.STOPPED.name());
|
||||
task.setStoppingAt(Instant.now());
|
||||
@@ -5488,6 +5517,31 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
|
||||
}
|
||||
}
|
||||
|
||||
/** Updates observed container exits even when another container keeps the task running. */
|
||||
private boolean updateStoppedContainers(EcsTask task, Map<String, Integer> exitCodes,
|
||||
Map<String, Instant> finishedAt) {
|
||||
boolean changed = false;
|
||||
if (task.getContainers() == null) {
|
||||
return false;
|
||||
}
|
||||
for (Container container : task.getContainers()) {
|
||||
Integer code = exitCodes.get(container.getName());
|
||||
if (code == null) {
|
||||
continue;
|
||||
}
|
||||
Instant finished = finishedAt.get(container.getName());
|
||||
changed |= !TaskStatus.STOPPED.name().equals(container.getLastStatus())
|
||||
|| !Objects.equals(code, container.getExitCode())
|
||||
|| (finished != null && !finished.equals(container.getFinishedAt()));
|
||||
container.setLastStatus(TaskStatus.STOPPED.name());
|
||||
container.setExitCode(code);
|
||||
if (finished != null) {
|
||||
container.setFinishedAt(finished);
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes a running task's health from its containers' health checks. The task is HEALTHY
|
||||
* only when every container that has a health check reports healthy, UNHEALTHY as soon as one
|
||||
|
||||
Reference in new issue
Block a user