fix(sagemaker): restore the worker pools in afterReset, not clear() (#3937)

* fix(sagemaker): restore the worker pools in afterReset, not clear()

A state reset runs every container teardown, and the SageMaker training
runner and endpoint manager shut their worker pools down there. Since
#3797 they create a new pool in clear(), but the reset controller skips
clear() as soon as the storage wipe or an earlier service's clear()
throws, so one failed reset still left both pools terminated and every
later CreateTrainingJob, CreateEndpoint and UpdateEndpoint returned 500
with RejectedExecutionException until Floci restarted.

Both beans now recreate the pool in afterReset(), which the controller
runs from a finally block for every service it prepared, whether or not
the wipe or a clear() succeeded. Shutdown still never runs it, so the
pools stay down there. clear() keeps only its stopRequested wipe, and
the ContainerTeardown Javadoc names afterReset() as the hook to use.

Refs #3796

* fix(ecs): restart the reconciler in afterReset, not clear()

EcsService restarted the reconciler its reset teardown had stopped in
clear(), the same one-way path the SageMaker pools had: a reset whose
storage wipe or earlier clear() threw skipped it and left ECS without a
reconciler until the emulator restarted. The restart now lives in
afterReset(), which the reset controller runs for every prepared service
regardless of that failure, matching the rule the ContainerTeardown
Javadoc states and what the SageMaker beans now do.

Refs #3796
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-19 18:30:57 -04:00
1 parent bc018afad0
commit 52941e90ef
7 files changed
+64 -34

No files matched your search

@@ -16,8 +16,10 @@ package io.github.hectorvent.floci.core.common;
*
* <p>Implementations must be idempotent; they may also be invoked from {@code @PreDestroy}
* as a fallback. An implementation that shuts down its own executor here must also implement
* {@link Resettable} and replace that executor in {@code clear()}: a reset runs this hook
* too, and {@code clear()} runs after it on a reset but never on shutdown.
* {@link Resettable} and replace that executor in {@code afterReset()}: a reset runs this hook
* too, and {@code afterReset()} runs at the end of a reset, even one whose storage wipe or
* {@code clear()} failed, but never on shutdown. {@code clear()} is not enough, since the
* controller skips it once an earlier step threw and the pool would stay terminated for good.
*/
public interface ContainerTeardown {
@@ -77,7 +77,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
private final EcsEventPublisher eventPublisher;
private final boolean dockerMode;
private final String baseUrl;
// Replaced by clear() after a state reset, whose container teardown shuts this scheduler down.
// Replaced by afterReset() after a state reset, whose container teardown shuts this scheduler down.
private volatile ScheduledExecutorService reconciler = newReconciler();
private final Object reconcilerLock = new Object();
@@ -207,7 +207,7 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
* state reset. Task state is transient (memory-only), so without this the containers
* outlive the process as orphans. The reconciler is shut down first, and any in-flight
* tick awaited, so it cannot restart drained tasks between this teardown and the final
* storage flush. A reset brings it back in {@link #clear()}. Handles are claimed
* storage flush. A reset brings it back in {@link #afterReset()}. Handles are claimed
* atomically to avoid racing an explicit StopTask.
*/
@Override
@@ -232,13 +232,20 @@ public class EcsService implements ContainerTeardown, ResourceProvider, Resettab
}
}
/**
* Runs after a state reset has drained the tasks and wiped the store, never on shutdown.
* The reset's teardown stopped the reconciler, so without a new one no service would be
* reconciled again until the emulator restarted.
*/
@Override
public void clear() {
// Nothing to wipe: the stores hold the ECS state and afterReset() restarts the reconciler.
}
/**
* Runs at the end of every state reset, never on shutdown. The reset's teardown stopped the
* reconciler, so without a new one no service would be reconciled again until the emulator
* restarted. This hook rather than {@code clear()} because the controller runs it even when
* the storage wipe or another service's {@code clear()} threw, and a failed reset must not
* leave ECS without a reconciler for good.
*/
@Override
public void afterReset() {
synchronized (reconcilerLock) {
if (reconciler.isShutdown()) {
ScheduledExecutorService replacement = newReconciler();
@@ -54,7 +54,7 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable {
private final EmulatorConfig config;
private final ContainerDetector containerDetector;
private final S3Service s3Service;
// Replaced by clear() after a state reset, whose container teardown shuts this pool down.
// Replaced by afterReset() after a state reset, whose container teardown shuts this pool down.
private volatile ExecutorService executor = Executors.newCachedThreadPool();
private final ConcurrentHashMap<String, String> containers = new ConcurrentHashMap<>();
private final HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build();
@@ -152,13 +152,20 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable {
executor.shutdownNow();
}
@Override
public void clear() {
// Nothing to wipe: the store holds the endpoints and afterReset() restores the pool.
}
/**
* Runs after a state reset has torn the containers down and wiped the store, never on
* shutdown. The teardown shut the worker pool down, so without a new one every later
* CreateEndpoint and UpdateEndpoint would be rejected until the emulator restarted.
* Runs at the end of every state reset, never on shutdown. The teardown shut the worker
* pool down, so without a new one every later CreateEndpoint and UpdateEndpoint would be
* rejected until the emulator restarted. This hook rather than {@code clear()} because the
* controller runs it even when the storage wipe or another service's {@code clear()} threw,
* and a failed reset must not leave the pool terminated for good.
*/
@Override
public synchronized void clear() {
public synchronized void afterReset() {
if (executor.isShutdown()) {
executor = Executors.newCachedThreadPool();
}
@@ -54,7 +54,7 @@ public class SageMakerTrainingRunner implements ContainerTeardown, Resettable {
private final ContainerDetector containerDetector;
private final S3Service s3Service;
private final ObjectMapper mapper;
// Replaced by clear() after a state reset, whose container teardown shuts this pool down.
// Replaced by afterReset() after a state reset, whose container teardown shuts this pool down.
private volatile ExecutorService executor = Executors.newCachedThreadPool();
private final ConcurrentHashMap<String, String> containers = new ConcurrentHashMap<>();
// Names a stop that stop() has already committed to the store as "Stopped": run()'s exit-code
@@ -170,14 +170,20 @@ public class SageMakerTrainingRunner implements ContainerTeardown, Resettable {
executor.shutdownNow();
}
/**
* Runs after a state reset has torn the containers down and wiped the store, never on
* shutdown. The teardown shut the worker pool down, so without a new one every later
* CreateTrainingJob would be rejected until the emulator restarted.
*/
@Override
public synchronized void clear() {
stopRequested.clear();
}
/**
* Runs at the end of every state reset, never on shutdown. The teardown shut the worker
* pool down, so without a new one every later CreateTrainingJob would be rejected until
* the emulator restarted. This hook rather than {@code clear()} because the controller
* runs it even when the storage wipe or another service's {@code clear()} threw, and a
* failed reset must not leave the pool terminated for good.
*/
@Override
public synchronized void afterReset() {
if (executor.isShutdown()) {
executor = Executors.newCachedThreadPool();
}
@@ -78,7 +78,7 @@ class EcsServiceTeardownTest {
}
@Test
void clearRestartsTheReconcilerThatAResetTeardownStopped() {
void afterResetRestartsTheReconcilerThatAResetTeardownStopped() {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().ecs().mock()).thenReturn(false); // docker mode
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
@@ -91,13 +91,15 @@ class EcsServiceTeardownTest {
null);
service.initializeStorage();
// A reset runs the teardown and then clear(); shutdown runs only the teardown.
// A reset runs the teardown, then clear(), and afterReset() last, even when the wipe or
// a clear() threw; shutdown runs only the teardown. afterReset() alone must bring the
// reconciler back.
service.stopManagedContainers();
assertTrue(service.isReconcilerShutdown());
service.clear();
service.afterReset();
assertFalse(service.isReconcilerShutdown());
service.clear();
service.afterReset();
assertFalse(service.isReconcilerShutdown());
service.stopManagedContainers();
@@ -18,22 +18,24 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
/**
* A state reset runs the container teardown, which shuts the worker pool down, and then
* {@code clear()}. Shutdown runs only the teardown.
* A state reset runs the container teardown, which shuts the worker pool down, then
* {@code clear()}, and {@code afterReset()} last, even when the wipe or a {@code clear()}
* failed. Shutdown runs only the teardown.
*/
class SageMakerEndpointManagerTest {
@Test
void teardownStopsThePoolAndClearRestoresIt() {
void teardownStopsThePoolAndAfterResetRestoresIt() {
SageMakerEndpointManager manager = manager();
assertTrue(manager.acceptsWork());
manager.stopManagedContainers();
assertFalse(manager.acceptsWork());
manager.clear();
// A reset whose wipe threw skips clear(); afterReset() alone must bring the pool back.
manager.afterReset();
assertTrue(manager.acceptsWork());
manager.clear();
manager.afterReset();
assertTrue(manager.acceptsWork());
manager.stopManagedContainers();
@@ -44,6 +46,7 @@ class SageMakerEndpointManagerTest {
SageMakerEndpointManager manager = manager();
manager.stopManagedContainers();
manager.clear();
manager.afterReset();
SageMakerService service = mock(SageMakerService.class);
CountDownLatch published = new CountDownLatch(1);
@@ -21,22 +21,24 @@ import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
/**
* A state reset runs the container teardown, which shuts the worker pool down, and then
* {@code clear()}. Shutdown runs only the teardown.
* A state reset runs the container teardown, which shuts the worker pool down, then
* {@code clear()}, and {@code afterReset()} last, even when the wipe or a {@code clear()}
* failed. Shutdown runs only the teardown.
*/
class SageMakerTrainingRunnerTest {
@Test
void teardownStopsThePoolAndClearRestoresIt() {
void teardownStopsThePoolAndAfterResetRestoresIt() {
SageMakerTrainingRunner runner = runner();
assertTrue(runner.acceptsWork());
runner.stopManagedContainers();
assertFalse(runner.acceptsWork());
runner.clear();
// A reset whose wipe threw skips clear(); afterReset() alone must bring the pool back.
runner.afterReset();
assertTrue(runner.acceptsWork());
runner.clear();
runner.afterReset();
assertTrue(runner.acceptsWork());
runner.stopManagedContainers();
@@ -47,6 +49,7 @@ class SageMakerTrainingRunnerTest {
SageMakerTrainingRunner runner = runner();
runner.stopManagedContainers();
runner.clear();
runner.afterReset();
SageMakerService service = mock(SageMakerService.class);
CountDownLatch reported = new CountDownLatch(1);