fix(ecs): quiesce task producers before container teardown (#8214)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-15 14:46:26 -04:00
1 parent 07ef865dfa
commit 42ead66324
11 files changed
+296 -25

No files matched your search

@@ -25,6 +25,46 @@ class ContainerTeardownsTest {
private static final Logger LOG = Logger.getLogger(ContainerTeardownsTest.class);
@Test
void preparesEveryServiceBeforeTheFirstDrain() {
assertPreparesBeforeDraining(false);
}
@Test
void failedPreparationStillPreparesAndDrainsEveryService() {
assertPreparesBeforeDraining(true);
}
private static void assertPreparesBeforeDraining(boolean failPreparation) {
List<String> phases = new ArrayList<>();
ContainerTeardown first = teardown("first", phases, failPreparation);
ContainerTeardown second = teardown("second", phases, false);
@SuppressWarnings("unchecked")
Instance<ContainerTeardown> teardowns = mock(Instance.class);
when(teardowns.iterator()).thenAnswer(ignored -> List.of(first, second).iterator());
ContainerTeardowns.stopAll(teardowns, LOG);
assertEquals(List.of("prepare-first", "prepare-second", "stop-first", "stop-second"), phases);
}
private static ContainerTeardown teardown(String name, List<String> phases, boolean failPreparation) {
return new ContainerTeardown() {
@Override
public void beginTeardown() {
phases.add("prepare-" + name);
if (failPreparation) {
throw new IllegalStateException("injected preparation failure");
}
}
@Override
public void stopManagedContainers() {
phases.add("stop-" + name);
}
};
}
@Test
void stopAllRunsATeardownWhoseContainersOthersUseAfterTheirs() {
List<String> stopped = new ArrayList<>();
@@ -192,6 +192,27 @@ class EmulatorLifecycleTest {
.thenReturn(Collections.emptyIterator());
}
@Test
void preparesContainerOwnersBeforeRetiringSharedCredentials() {
ContainerTeardown first = Mockito.mock(ContainerTeardown.class);
ContainerTeardown second = Mockito.mock(ContainerTeardown.class);
when(containerTeardowns.iterator()).thenReturn(List.of(first, second).iterator());
when(ecsTaskRoleCredentialsConfig.enabled()).thenReturn(true);
emulatorLifecycle.onStop(new ShutdownEvent());
InOrder order = Mockito.inOrder(first, second, storageFactory, ecsTaskRoleCredentialsServer);
order.verify(storageFactory).flushAll();
order.verify(first).beginTeardown();
order.verify(second).beginTeardown();
order.verify(ecsTaskRoleCredentialsServer).stop();
order.verify(first).stopManagedContainers();
order.verify(second).stopManagedContainers();
order.verify(storageFactory).shutdownAll();
verify(first).beginTeardown();
verify(second).beginTeardown();
}
private void stubStorageConfig() {
when(config.storage()).thenReturn(storageConfig);
when(storageConfig.mode()).thenReturn("in-memory");
@@ -13,12 +13,14 @@ import io.floci.aws.services.ecs.model.LaunchType;
import io.floci.aws.services.ecs.model.TaskStatus;
import io.floci.aws.testing.InMemoryStorageFactory;
import io.floci.aws.testing.MutableClock;
import io.floci.aws.testing.ThreadStates;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
@@ -397,6 +399,148 @@ class EcsGracefulStopConcurrencyServiceTest {
assertEquals("RUNNING", lastStatus(service, replacement));
}
@Test
void teardownPreparationPausesReconciliationUntilReset() throws Exception {
EcsService service = newService();
createService(service, "draining", 1);
tick(service);
EcsTask original = onlyRunningTask(service, "draining");
service.beginTeardown();
assertTrue(service.isReconcilerShutdown(), "preparation stops the scheduler before other owners drain");
service.updateService(CLUSTER, "draining", null, 2, null, REGION);
tick(service);
service.reconcileServices();
verify(containerManager, times(1)).startTask(any(), any(), any(), anyString());
service.stopManagedContainers();
verify(containerManager).stopTask(handleOf(original));
service.clear();
service.afterReset();
tick(service);
assertEquals(2, runningTasks(service, "draining").size(), "reset resumes the desired service count");
}
@Test
void preparationInterruptsTheScheduledPullBeforeWaitingForItsPass() throws Exception {
EcsService service = newService(true);
CountDownLatch pullEntered = new CountDownLatch(1);
CountDownLatch releasePull = new CountDownLatch(1);
CountDownLatch interrupted = new CountDownLatch(1);
doAnswer(invocation -> {
pullEntered.countDown();
try {
releasePull.await();
} catch (InterruptedException e) {
interrupted.countDown();
Thread.currentThread().interrupt();
throw new IllegalStateException("image pull interrupted", e);
}
throw new IllegalStateException("test released the pull without interruption");
}).when(containerManager).startTask(any(), any(), any(), anyString());
createService(service, "scheduled-pull", 1);
FutureTask<Void> preparation = new FutureTask<>(service::beginTeardown, null);
Thread thread = new Thread(preparation, "test-ecs-scheduled-pull-teardown");
thread.setDaemon(true);
try {
assertTrue(pullEntered.await(10, TimeUnit.SECONDS), "the real scheduler must enter its pull");
thread.start();
preparation.get(TICK_BUDGET.toMillis(), TimeUnit.MILLISECONDS);
assertEquals(0, interrupted.getCount(), "preparation interrupts the scheduler's inline pull");
List<EcsTask> tasks = allTasks(service, "scheduled-pull");
assertEquals(1, tasks.size());
assertEquals("STOPPED", tasks.getFirst().getLastStatus());
assertEquals("TaskFailedToStart", tasks.getFirst().getStopCode());
verify(containerManager, never()).stopTask(any());
} finally {
releasePull.countDown();
if (thread.getState() != Thread.State.NEW) {
preparation.get(TICK_BUDGET.toMillis(), TimeUnit.MILLISECONDS);
}
}
}
@Test
void preparationReturnsWhileTheScheduledPullIgnoresInterruption() throws Exception {
EcsService service = newService(true);
CountDownLatch pullEntered = new CountDownLatch(1);
CountDownLatch interrupted = new CountDownLatch(1);
CountDownLatch releasePull = new CountDownLatch(1);
CountDownLatch pullFinished = new CountDownLatch(1);
doAnswer(invocation -> {
pullEntered.countDown();
try {
boolean released = false;
while (!released) {
try {
released = releasePull.await(10, TimeUnit.SECONDS);
assertTrue(released, "test must release the uncooperative pull");
} catch (InterruptedException e) {
interrupted.countDown();
}
}
throw new IllegalStateException("test released the uncooperative pull");
} finally {
pullFinished.countDown();
}
}).when(containerManager).startTask(any(), any(), any(), anyString());
createService(service, "uncooperative-pull", 1);
FutureTask<Void> preparation = new FutureTask<>(service::beginTeardown, null);
Thread thread = new Thread(preparation, "test-ecs-uncooperative-pull-teardown");
thread.setDaemon(true);
try {
assertTrue(pullEntered.await(10, TimeUnit.SECONDS), "the real scheduler must enter its pull");
thread.start();
assertTrue(interrupted.await(5, TimeUnit.SECONDS), "preparation must interrupt the pull");
preparation.get(TICK_BUDGET.toMillis(), TimeUnit.MILLISECONDS);
assertEquals(1, pullFinished.getCount(), "preparation returns while the pull is still blocked");
assertTrue(service.isReconcilerShutdown());
} finally {
releasePull.countDown();
assertTrue(pullFinished.await(5, TimeUnit.SECONDS), "the test pull must finish");
if (thread.getState() != Thread.State.NEW) {
preparation.get(TICK_BUDGET.toMillis(), TimeUnit.MILLISECONDS);
}
}
}
@Test
void teardownPreparationWaitsForTaskLaunchBeforeStoppingContainers() throws Exception {
EcsService service = newService();
createService(service, "launching", 1);
CountDownLatch launchEntered = new CountDownLatch(1);
CountDownLatch releaseLaunch = new CountDownLatch(1);
doAnswer(invocation -> {
EcsTask task = invocation.getArgument(0);
launchEntered.countDown();
assertTrue(releaseLaunch.await(30, TimeUnit.SECONDS), "test must release the in-flight launch");
Container container = new Container();
container.setName("app");
container.setRuntimeId("launching-container");
task.setContainers(List.of(container));
return new EcsTaskHandle(task.getTaskArn(), Map.of("app", container.getRuntimeId()), Map.of());
}).when(containerManager).startTask(any(), any(), any(), anyString());
FutureTask<Void> launch = startTick(service);
FutureTask<Void> preparation = new FutureTask<>(service::beginTeardown, null);
Thread preparationThread = new Thread(preparation, "test-ecs-teardown-preparation");
preparationThread.setDaemon(true);
try {
assertTrue(launchEntered.await(5, TimeUnit.SECONDS), "the task launch must be in flight");
preparationThread.start();
Thread.State state = ThreadStates.awaitState(preparationThread,
EnumSet.of(Thread.State.TIMED_WAITING, Thread.State.TERMINATED), TICK_BUDGET,
"teardown preparation");
assertEquals(Thread.State.TIMED_WAITING, state, "preparation must wait for the admitted task launch");
} finally {
releaseLaunch.countDown();
}
launch.get(TICK_BUDGET.toMillis(), TimeUnit.MILLISECONDS);
preparation.get(TICK_BUDGET.toMillis(), TimeUnit.MILLISECONDS);
EcsTask launched = onlyRunningTask(service, "launching");
service.stopManagedContainers();
verify(containerManager).stopTask(handleOf(launched));
}
// ── helpers ──────────────────────────────────────────────────────────────
/** One full reconcile tick, on a thread of its own, so a tick stuck behind a stop fails rather than hangs. */
@@ -418,13 +562,21 @@ class EcsGracefulStopConcurrencyServiceTest {
}
private EcsService newService() {
return newService(false);
}
private EcsService newService(boolean scheduleReconciliation) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().ecs().mock()).thenReturn(false);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
EcsService service = new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
config, lbRegistrar, new InMemoryStorageFactory("000000000000"), eventPublisher,
new EcsExecSessionRegistry(), null, null, clock);
service.initializeStorage();
if (scheduleReconciliation) {
service.init();
} else {
service.initializeStorage();
}
service.createCluster(CLUSTER, REGION);
services.add(service);
return service;
@@ -73,8 +73,8 @@ class EcsReconciliationConcurrencyServiceTest {
assertTrue(entered.await(5, TimeUnit.SECONDS), "first pass must reach service storage");
secondThread.start();
await().atMost(Duration.ofSeconds(5)).until(() ->
secondThread.getState() == Thread.State.BLOCKED || second.isDone());
assertEquals(Thread.State.BLOCKED, secondThread.getState(),
secondThread.getState() == Thread.State.WAITING || second.isDone());
assertEquals(Thread.State.WAITING, secondThread.getState(),
"a direct pass must wait for the running pass instead of reconciling the same deficit");
} finally {
release.countDown();
@@ -21,6 +21,8 @@ import io.floci.aws.testing.InMemoryStorageFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.time.Duration;
import java.util.ArrayList;
@@ -315,8 +317,9 @@ class EcsRunTaskAsyncLaunchServiceTest {
verify(containerManager, never()).startTask(any(), any(), any(), anyString());
}
@Test
void teardownInterruptsAPullAndAfterResetLaunchesAgain() throws Exception {
@ParameterizedTest
@ValueSource(booleans = {false, true})
void teardownInterruptsAPullAndAfterResetLaunchesAgain(boolean prepareOnly) throws Exception {
doAnswer(invocation -> {
startEntered.countDown();
try {
@@ -333,7 +336,11 @@ class EcsRunTaskAsyncLaunchServiceTest {
EcsTask response = run(service, "teardown");
assertTrue(startEntered.await(5, TimeUnit.SECONDS));
assertTimeoutPreemptively(LAUNCH_BUDGET, service::stopManagedContainers);
assertTimeoutPreemptively(LAUNCH_BUDGET,
prepareOnly ? service::beginTeardown : service::stopManagedContainers);
if (prepareOnly) {
verify(containerManager, never()).stopTask(any());
}
EcsTask stopped = describe(service, response);
assertEquals("STOPPED", stopped.getLastStatus());