mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sagemaker): start endpoint containers again when Floci restarts (#9367)
Floci's shutdown stops every endpoint container, and nothing started them again, so after a restart with persistent storage an endpoint still reported InService while InvokeEndpoint failed with ModelError. At startup, each endpoint that was InService or still Creating goes back to Creating and is started again from its endpoint config and model, as CreateEndpoint starts one, in the account it is stored under. It reports InService once its container answers /ping, or Failed with the reason. A Failed endpoint is left as it is, and one a crash left Deleting is deleted. Part of #5664.
This commit is contained in:
1 parent
464aaaf734
commit
2bcde72f1e
12 files changed
+622
-30
No files matched your search
@@ -107,6 +107,15 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
}
|
||||
}
|
||||
|
||||
/** Deletes {@code key} in {@code accountId}; the counterpart of {@link #getInAccount}. */
|
||||
public static <V> void deleteInAccount(StorageBackend<String, V> store, String accountId, String key) {
|
||||
if (accountId != null && store instanceof AccountAwareStorageBackend<V> aware) {
|
||||
aware.deleteForAccount(accountId, key);
|
||||
} else {
|
||||
store.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(String key, V value) {
|
||||
String account = prefix();
|
||||
|
||||
@@ -43,6 +43,7 @@ import io.floci.aws.services.rds.RdsService;
|
||||
import io.floci.aws.services.rds.container.RdsContainerManager;
|
||||
import io.floci.aws.services.rds.proxy.RdsProxyManager;
|
||||
import io.floci.aws.services.redshift.RedshiftDynamoDbZeroEtlConsumer;
|
||||
import io.floci.aws.services.sagemaker.SageMakerService;
|
||||
import io.floci.aws.services.stepfunctions.StepFunctionsService;
|
||||
import io.floci.aws.services.timestreaminfluxdb.TimestreamInfluxDbService;
|
||||
import io.quarkus.runtime.Quarkus;
|
||||
@@ -102,6 +103,7 @@ public class EmulatorLifecycle {
|
||||
private final NeptuneProxyManager neptuneProxyManager;
|
||||
private final RabbitMqManager rabbitMqManager;
|
||||
private final FlinkContainerManager flinkContainerManager;
|
||||
private final SageMakerService sageMakerService;
|
||||
private final RdsService rdsService;
|
||||
private final TimestreamInfluxDbService timestreamInfluxDbService;
|
||||
private final ElbV2Service elbV2Service;
|
||||
@@ -149,6 +151,7 @@ public class EmulatorLifecycle {
|
||||
NeptuneProxyManager neptuneProxyManager,
|
||||
RabbitMqManager rabbitMqManager,
|
||||
FlinkContainerManager flinkContainerManager,
|
||||
SageMakerService sageMakerService,
|
||||
RdsService rdsService,
|
||||
TimestreamInfluxDbService timestreamInfluxDbService,
|
||||
ElbV2Service elbV2Service,
|
||||
@@ -193,6 +196,7 @@ public class EmulatorLifecycle {
|
||||
this.neptuneProxyManager = neptuneProxyManager;
|
||||
this.rabbitMqManager = rabbitMqManager;
|
||||
this.flinkContainerManager = flinkContainerManager;
|
||||
this.sageMakerService = sageMakerService;
|
||||
this.rdsService = rdsService;
|
||||
this.timestreamInfluxDbService = timestreamInfluxDbService;
|
||||
this.elbV2Service = elbV2Service;
|
||||
@@ -294,6 +298,14 @@ public class EmulatorLifecycle {
|
||||
return null;
|
||||
});
|
||||
}
|
||||
if (config.services().sagemaker().enabled()) {
|
||||
// Restores every endpoint it can and throws the ones it could not, which must not stop Floci starting.
|
||||
try {
|
||||
sageMakerService.restorePersistedRuntime();
|
||||
} catch (RuntimeException e) {
|
||||
LOG.error("Failed to restore persisted SageMaker endpoints at startup", e);
|
||||
}
|
||||
}
|
||||
if (config.services().elbv2().enabled()) {
|
||||
elbV2Service.restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@@ -57,6 +57,7 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable {
|
||||
private final S3Service s3Service;
|
||||
// Replaced by afterReset() after a state reset, whose container teardown shuts this pool down.
|
||||
private volatile ExecutorService executor = Executors.newCachedThreadPool();
|
||||
// Keyed by account, Region and name, which together identify an endpoint.
|
||||
private final ConcurrentHashMap<String, String> containers = new ConcurrentHashMap<>();
|
||||
private final HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build();
|
||||
|
||||
@@ -87,7 +88,7 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable {
|
||||
if (image == null) {
|
||||
throw new IllegalArgumentException("PrimaryContainer.Image is required");
|
||||
}
|
||||
String name = ContainerStorageHelper.dockerName(config, "sagemaker-endpoint-" + endpoint.endpointName);
|
||||
String name = containerName(config, endpoint.accountId, endpoint.region, endpoint.endpointName);
|
||||
lifecycleManager.removeIfExists(name);
|
||||
ContainerBuilder.Builder builder = containerBuilder.newContainer(image)
|
||||
.withName(name)
|
||||
@@ -111,7 +112,7 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable {
|
||||
endpoint.containerId = containerId;
|
||||
endpoint.invokeHost = ep.host();
|
||||
endpoint.invokePort = ep.port();
|
||||
containers.put(endpoint.endpointName, containerId);
|
||||
containers.put(identity(endpoint), containerId);
|
||||
waitForPing(endpoint.invokeHost, endpoint.invokePort);
|
||||
endpoint.endpointStatus = "InService";
|
||||
endpoint.failureReason = null;
|
||||
@@ -139,19 +140,40 @@ public class SageMakerEndpointManager implements ContainerTeardown, Resettable {
|
||||
}
|
||||
LOG.infov("SageMaker endpoint {0} start superseded by a delete or newer update; discarding result",
|
||||
endpoint.endpointName);
|
||||
String containerId = endpoint.containerId != null ? endpoint.containerId : containers.remove(endpoint.endpointName);
|
||||
String containerId = endpoint.containerId != null ? endpoint.containerId : containers.remove(identity(endpoint));
|
||||
if (containerId != null) {
|
||||
lifecycleManager.stopAndRemove(containerId, null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops and removes the endpoint's container: the one its record names, else the one a start of this process
|
||||
* brought up, else any container of the endpoint's name, which a crash can leave behind with no record of it.
|
||||
*/
|
||||
public void stopEndpoint(EndpointResource endpoint) {
|
||||
String containerId = endpoint.containerId != null ? endpoint.containerId : containers.remove(endpoint.endpointName);
|
||||
String tracked = containers.remove(identity(endpoint));
|
||||
String containerId = endpoint.containerId != null ? endpoint.containerId : tracked;
|
||||
if (containerId != null) {
|
||||
lifecycleManager.stopAndRemove(containerId, null);
|
||||
} else {
|
||||
lifecycleManager.removeIfExists(containerName(config, endpoint.accountId, endpoint.region,
|
||||
endpoint.endpointName));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The endpoint's container name. An endpoint is identified by its account, Region and name, so endpoints of the
|
||||
* same name in two accounts or Regions get containers of their own.
|
||||
*/
|
||||
static String containerName(EmulatorConfig config, String accountId, String region, String endpointName) {
|
||||
return ContainerStorageHelper.dockerName(config,
|
||||
"sagemaker-endpoint-" + accountId + "-" + region + "-" + endpointName);
|
||||
}
|
||||
|
||||
private static String identity(EndpointResource endpoint) {
|
||||
return endpoint.accountId + "/" + endpoint.region + "/" + endpoint.endpointName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void stopManagedContainers() {
|
||||
containers.forEach((name, id) -> lifecycleManager.stopAndRemove(id, null));
|
||||
|
||||
@@ -80,9 +80,11 @@ public class SageMakerRuntimeController {
|
||||
response.headers().firstValue("Content-Type").ifPresent(out::type);
|
||||
return out.build();
|
||||
} catch (Exception e) {
|
||||
LOG.warnv("SageMaker endpoint invocation failed for {0}: {1}", endpointName, e.getMessage());
|
||||
// A refused connection, as to an endpoint whose container is gone, carries no message.
|
||||
String message = e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName();
|
||||
LOG.warnv("SageMaker endpoint invocation failed for {0}: {1}", endpointName, message);
|
||||
return Response.status(424).type(MediaType.APPLICATION_JSON)
|
||||
.entity(Map.of("__type", "ModelError", "message", e.getMessage())).build();
|
||||
.entity(Map.of("__type", "ModelError", "message", message)).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.EpochSeconds;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend.AccountEntry;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.sagemaker.model.SageMakerEntities.EndpointConfigResource;
|
||||
@@ -35,6 +37,7 @@ import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static io.floci.aws.core.storage.AccountAwareStorageBackend.deleteInAccount;
|
||||
import static io.floci.aws.core.storage.AccountAwareStorageBackend.getInAccount;
|
||||
import static io.floci.aws.core.storage.AccountAwareStorageBackend.putInAccount;
|
||||
|
||||
@@ -266,7 +269,7 @@ public class SageMakerService {
|
||||
ep.tags = tagsFromList(request.path("Tags"));
|
||||
ep.generation = 1;
|
||||
endpointStore.put(regionKey(region, name), ep);
|
||||
endpointManager.startEndpointAsync(ep, this);
|
||||
endpointManager.startEndpointAsync(startRun(ep), this);
|
||||
ObjectNode out = mapper.createObjectNode();
|
||||
out.put("EndpointArn", ep.endpointArn);
|
||||
return out;
|
||||
@@ -287,7 +290,7 @@ public class SageMakerService {
|
||||
ep.lastModifiedTime = nowMillis();
|
||||
ep.generation++;
|
||||
endpointStore.put(regionKey(region, name), ep);
|
||||
endpointManager.startEndpointAsync(ep, this);
|
||||
endpointManager.startEndpointAsync(startRun(ep), this);
|
||||
ObjectNode out = mapper.createObjectNode();
|
||||
out.put("EndpointArn", ep.endpointArn);
|
||||
return out;
|
||||
@@ -509,19 +512,113 @@ public class SageMakerService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the result of an endpoint start (success or failure) unless a delete or a
|
||||
* newer start has superseded it since {@code ep} was handed to {@link SageMakerEndpointManager}.
|
||||
* Returns {@code false} when the caller should tear down whatever it just started instead
|
||||
* of publishing it, because the endpoint it belongs to is gone or has moved on.
|
||||
* Brings the persisted endpoints back after Floci restarts. Shutdown stops their containers
|
||||
* ({@link SageMakerEndpointManager#stopManagedContainers}), so an endpoint stored {@code InService}, or still
|
||||
* {@code Creating}, has nothing behind it. Each goes back to {@code Creating} and is started again from its
|
||||
* endpoint config and model, as {@code CreateEndpoint} starts one, which replaces a container a crash left
|
||||
* running: it reports {@code InService} once the container answers {@code /ping}, or {@code Failed} with the
|
||||
* reason. A {@code Failed} endpoint is left as it is, and one a crash left {@code Deleting} is deleted with its
|
||||
* container. Every endpoint is tried, and the ones that could not be restored are thrown together at the end.
|
||||
*/
|
||||
synchronized boolean finalizeEndpointStart(EndpointResource ep) {
|
||||
String key = regionKey(ep.region, ep.endpointName);
|
||||
EndpointResource current = getInAccount(endpointStore, ep.accountId, key).orElse(null);
|
||||
if (current == null || current.generation != ep.generation) {
|
||||
public synchronized void restorePersistedRuntime() {
|
||||
if (endpointStore instanceof AccountAwareStorageBackend<EndpointResource> aware) {
|
||||
// Records from before stores were account-scoped belong to the default account; move them there, so the
|
||||
// restore writes and deletes the records the store reads.
|
||||
aware.migrateLegacyEntries(regionResolver.getDefaultAccountId(), key -> true,
|
||||
ep -> regionKey(ep.region, ep.endpointName), ep -> true);
|
||||
}
|
||||
RuntimeException failure = null;
|
||||
for (AccountEntry<EndpointResource> entry : persistedEndpoints()) {
|
||||
EndpointResource ep = entry.value();
|
||||
try {
|
||||
restoreEndpoint(entry.accountId(), entry.key(), ep);
|
||||
} catch (RuntimeException e) {
|
||||
RuntimeException restoreFailure = new IllegalStateException("Could not restore SageMaker endpoint "
|
||||
+ ep.endpointName + " of account " + entry.accountId(), e);
|
||||
if (failure == null) {
|
||||
failure = restoreFailure;
|
||||
} else {
|
||||
failure.addSuppressed(restoreFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreEndpoint(String accountId, String key, EndpointResource ep) {
|
||||
ep.accountId = accountId;
|
||||
if ("Failed".equals(ep.endpointStatus)) {
|
||||
return;
|
||||
}
|
||||
if ("Deleting".equals(ep.endpointStatus)) {
|
||||
endpointManager.stopEndpoint(ep);
|
||||
deleteInAccount(endpointStore, accountId, key);
|
||||
return;
|
||||
}
|
||||
ep.endpointStatus = "Creating";
|
||||
ep.failureReason = null;
|
||||
ep.containerId = null;
|
||||
ep.invokeHost = null;
|
||||
ep.invokePort = 0;
|
||||
ep.generation++;
|
||||
putInAccount(endpointStore, accountId, key, ep);
|
||||
endpointManager.startEndpointAsync(startRun(ep), this);
|
||||
}
|
||||
|
||||
private List<AccountEntry<EndpointResource>> persistedEndpoints() {
|
||||
if (endpointStore instanceof AccountAwareStorageBackend<EndpointResource> aware) {
|
||||
return aware.scanAllAccountEntries(key -> true);
|
||||
}
|
||||
return endpointStore.scan(key -> true).stream()
|
||||
.map(ep -> new AccountEntry<>(ep.accountId, regionKey(ep.region, ep.endpointName), ep))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* The endpoint a start worker runs with: a copy, so a later update or delete, which changes the stored record,
|
||||
* cannot change the generation the worker's result is checked against.
|
||||
*/
|
||||
private static EndpointResource startRun(EndpointResource ep) {
|
||||
EndpointResource run = new EndpointResource();
|
||||
run.endpointName = ep.endpointName;
|
||||
run.endpointArn = ep.endpointArn;
|
||||
run.endpointConfigName = ep.endpointConfigName;
|
||||
run.endpointStatus = ep.endpointStatus;
|
||||
run.failureReason = ep.failureReason;
|
||||
run.creationTime = ep.creationTime;
|
||||
run.lastModifiedTime = ep.lastModifiedTime;
|
||||
run.region = ep.region;
|
||||
run.accountId = ep.accountId;
|
||||
run.containerId = ep.containerId;
|
||||
run.invokeHost = ep.invokeHost;
|
||||
run.invokePort = ep.invokePort;
|
||||
run.generation = ep.generation;
|
||||
run.tags = new LinkedHashMap<>(ep.tags);
|
||||
return run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the result of an endpoint start (success or failure) unless a delete or a
|
||||
* newer start has superseded it since {@code run} was handed to {@link SageMakerEndpointManager}.
|
||||
* Returns {@code false} when the caller should tear down whatever it just started instead
|
||||
* of publishing it, because the endpoint it belongs to is gone or has moved on. Only the start's
|
||||
* own fields are written onto the stored endpoint, so tags added meanwhile are kept.
|
||||
*/
|
||||
synchronized boolean finalizeEndpointStart(EndpointResource run) {
|
||||
String key = regionKey(run.region, run.endpointName);
|
||||
EndpointResource current = getInAccount(endpointStore, run.accountId, key).orElse(null);
|
||||
if (current == null || current.generation != run.generation) {
|
||||
return false;
|
||||
}
|
||||
ep.lastModifiedTime = nowMillis();
|
||||
putInAccount(endpointStore, ep.accountId, key, ep);
|
||||
current.endpointStatus = run.endpointStatus;
|
||||
current.failureReason = run.failureReason;
|
||||
current.containerId = run.containerId;
|
||||
current.invokeHost = run.invokeHost;
|
||||
current.invokePort = run.invokePort;
|
||||
current.lastModifiedTime = nowMillis();
|
||||
putInAccount(endpointStore, run.accountId, key, current);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user