mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(kinesisanalytics): start Flink clusters again when Floci restarts (#9390)
Floci's shutdown removes every application's Flink cluster, and nothing started them again, so after a restart with persistent storage an application still reported RUNNING with no cluster and no job behind it. At startup, each application that was RUNNING or still STARTING goes back to STARTING, and its cluster starts in the background. A cluster that outlived Floci is kept when its JobManager, TaskManager and job still run. Otherwise what survives of it is removed, under the current or the old floci- names, and the application gets a new cluster, as StartApplication starts one, in the application's own account, and the readiness poller runs its job afresh and reports it RUNNING. An application whose cluster cannot be started goes back to READY, from which StartApplication starts it again. An application stopped while its restore is starting the cluster is STOPPING, as AWS reports a stop in progress, until the restore has removed that cluster by its containers' IDs, and READY then. StartApplication and DeleteApplication refuse it meanwhile, so no second start runs under the cluster's names. One still STOPPING at the next start has what survives of its cluster removed before it turns READY. Each removal is confirmed with Docker: while Docker keeps the cluster, the application stays STOPPING and the cluster stays named and tracked, for shutdown and the next start. Records stored under the keys of older builds move onto their current keys first, so no application is restored twice. A start that shutdown overtakes removes its containers. Part of #5664.
This commit is contained in:
1 parent
874c3d6123
commit
732001a90f
11 files changed
+1480
-52
No files matched your search
@@ -29,6 +29,7 @@ import io.floci.aws.services.elb.ElbClassicService;
|
||||
import io.floci.aws.services.elbv2.ElbV2Service;
|
||||
import io.floci.aws.services.floci.ui.FlociUiManager;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.services.kinesisanalytics.KinesisAnalyticsV2Service;
|
||||
import io.floci.aws.services.kinesisanalytics.container.FlinkContainerManager;
|
||||
import io.floci.aws.services.lambda.DynamoDbStreamsEventSourcePoller;
|
||||
import io.floci.aws.services.lambda.KinesisEventSourcePoller;
|
||||
@@ -105,6 +106,7 @@ public class EmulatorLifecycle {
|
||||
private final RabbitMqManager rabbitMqManager;
|
||||
private final AmazonMqService amazonMqService;
|
||||
private final FlinkContainerManager flinkContainerManager;
|
||||
private final KinesisAnalyticsV2Service kinesisAnalyticsService;
|
||||
private final SageMakerService sageMakerService;
|
||||
private final RdsService rdsService;
|
||||
private final TimestreamInfluxDbService timestreamInfluxDbService;
|
||||
@@ -154,6 +156,7 @@ public class EmulatorLifecycle {
|
||||
RabbitMqManager rabbitMqManager,
|
||||
AmazonMqService amazonMqService,
|
||||
FlinkContainerManager flinkContainerManager,
|
||||
KinesisAnalyticsV2Service kinesisAnalyticsService,
|
||||
SageMakerService sageMakerService,
|
||||
RdsService rdsService,
|
||||
TimestreamInfluxDbService timestreamInfluxDbService,
|
||||
@@ -200,6 +203,7 @@ public class EmulatorLifecycle {
|
||||
this.rabbitMqManager = rabbitMqManager;
|
||||
this.amazonMqService = amazonMqService;
|
||||
this.flinkContainerManager = flinkContainerManager;
|
||||
this.kinesisAnalyticsService = kinesisAnalyticsService;
|
||||
this.sageMakerService = sageMakerService;
|
||||
this.rdsService = rdsService;
|
||||
this.timestreamInfluxDbService = timestreamInfluxDbService;
|
||||
@@ -317,6 +321,13 @@ public class EmulatorLifecycle {
|
||||
return null;
|
||||
});
|
||||
}
|
||||
if (config.services().kinesisAnalytics().enabled()) {
|
||||
// Starts the applications' Flink clusters in the background, failing the future with the ones it could not.
|
||||
kinesisAnalyticsService.restorePersistedRuntime().exceptionally(ex -> {
|
||||
LOG.error("Failed to restore persisted Kinesis Analytics V2 applications at startup", ex);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
if (config.services().elbv2().enabled()) {
|
||||
elbV2Service.restorePersistedRuntime();
|
||||
}
|
||||
|
||||
+317
-45
@@ -7,9 +7,12 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.common.StripedLocks;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
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.kinesisanalytics.container.FlinkContainerManager;
|
||||
@@ -30,6 +33,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -69,6 +75,19 @@ public class KinesisAnalyticsV2Service implements StoredResourceArns {
|
||||
private final RegionResolver regionResolver;
|
||||
private final FlinkContainerManager containerManager;
|
||||
private final ScheduledExecutorService poller = Executors.newSingleThreadScheduledExecutor();
|
||||
/**
|
||||
* The applications a restore is bringing back, by {@code account:key}, which the readiness poller leaves alone:
|
||||
* their records still name the old cluster until the restore keeps it or starts a new one. In memory only, so
|
||||
* after a crash the record keeps what the next start probes a surviving cluster on.
|
||||
*/
|
||||
private final Set<String> restoringApplications = ConcurrentHashMap.newKeySet();
|
||||
/**
|
||||
* Monitors over one stored application, by {@code account:key}. The restore reads an application again, and ends
|
||||
* its restore, under its lock, under which {@code StopApplication} finds whether a restore is starting the
|
||||
* application's cluster. If one is, the stop leaves the application {@code STOPPING} for the restore to finish,
|
||||
* so the restore never undoes a stop, and no other start or stop runs on the cluster it is starting.
|
||||
*/
|
||||
private final StripedLocks storedApplicationLocks = new StripedLocks(256);
|
||||
|
||||
@Inject
|
||||
public KinesisAnalyticsV2Service(StorageFactory storageFactory, EmulatorConfig config,
|
||||
@@ -330,13 +349,27 @@ public class KinesisAnalyticsV2Service implements StoredResourceArns {
|
||||
"Application " + applicationName + " cannot be stopped while in state "
|
||||
+ app.getApplicationStatus() + "; it must be RUNNING or STARTING", 400);
|
||||
}
|
||||
if (!config.services().kinesisAnalytics().mock()) {
|
||||
containerManager.stopCluster(app);
|
||||
boolean restoring;
|
||||
synchronized (storedApplicationLock(regionResolver.getAccountId(), applicationKey(region, applicationName))) {
|
||||
restoring = restoringApplications.contains(restoringKey(app));
|
||||
if (restoring) {
|
||||
// The startup restore is starting its cluster, which it removes once that start is through, and the
|
||||
// application is READY then. Until then it is STOPPING, as AWS reports a stop in progress, which
|
||||
// StartApplication and DeleteApplication refuse: no other start begins under the cluster's names.
|
||||
app.setApplicationStatus(ApplicationStatus.STOPPING);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
putApplication(app);
|
||||
}
|
||||
}
|
||||
if (!restoring) {
|
||||
if (!config.services().kinesisAnalytics().mock()) {
|
||||
containerManager.stopCluster(app);
|
||||
}
|
||||
// Stopping the JobManager is synchronous, so the application returns to READY directly.
|
||||
app.setApplicationStatus(ApplicationStatus.READY);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
putApplication(app);
|
||||
}
|
||||
// Stopping the JobManager is synchronous, so the application returns to READY directly.
|
||||
app.setApplicationStatus(ApplicationStatus.READY);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
putApplication(app);
|
||||
LOG.infov("Stopped Kinesis Analytics V2 application {0}", applicationName);
|
||||
return app;
|
||||
}
|
||||
@@ -721,46 +754,285 @@ public class KinesisAnalyticsV2Service implements StoredResourceArns {
|
||||
"No application found for ARN: " + resourceArn, 400));
|
||||
}
|
||||
|
||||
private void startReadinessPoller() {
|
||||
poller.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
if (config.services().kinesisAnalytics().mock()) {
|
||||
return;
|
||||
}
|
||||
for (FlinkApplication app : allApplications()) {
|
||||
boolean changed = false;
|
||||
if (app.getApplicationStatus() == ApplicationStatus.STARTING) {
|
||||
String previousJobId = app.getFlinkJobId();
|
||||
if (containerManager.advanceToRunning(app)) {
|
||||
LOG.infov("Kinesis Analytics V2 application {0} is now RUNNING",
|
||||
app.getApplicationName());
|
||||
app.setApplicationStatus(ApplicationStatus.RUNNING);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
changed = true;
|
||||
} else if (!Objects.equals(previousJobId, app.getFlinkJobId())) {
|
||||
// The Flink job was just submitted (flinkJobId newly assigned) but hasn't
|
||||
// reached RUNNING yet. Persist now rather than waiting for that: pendingJars
|
||||
// (an in-process-only cache) is already cleared at this point, so if the
|
||||
// emulator restarts before the next RUNNING-triggered persist, an unpersisted
|
||||
// flinkJobId would leave the application stuck — it could never re-fetch the
|
||||
// JAR to resubmit, nor know a job was already running to poll instead.
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
for (Snapshot snapshot : app.getSnapshots().values()) {
|
||||
if (snapshot.getSnapshotStatus() == SnapshotStatus.CREATING
|
||||
&& containerManager.advanceSnapshot(app, snapshot)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
putApplication(app);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error in Kinesis Analytics V2 readiness poller", e);
|
||||
/**
|
||||
* Brings the persisted applications back after Floci restarts. Shutdown removes their Flink clusters
|
||||
* ({@link FlinkContainerManager#stopAll}), so an application stored {@code RUNNING}, or still starting, has no
|
||||
* cluster and no job. Each goes back to {@code STARTING} at once, and the clusters start in the background, so
|
||||
* Floci does not wait for them to report itself ready; the poller leaves an application alone while its restore
|
||||
* runs.
|
||||
*
|
||||
* <p>An application whose cluster outlived Floci, as when Floci is killed, keeps that cluster when it still
|
||||
* serves the application, its job and state included. Otherwise it gets a new cluster, as
|
||||
* {@code StartApplication} starts one, in its own account, which reads its code from S3 again, and the poller
|
||||
* runs its job and reports it {@code RUNNING}. Like {@code StartApplication}, the job starts afresh rather than
|
||||
* from a snapshot; the snapshots stay in their volume. An application whose cluster cannot be started is
|
||||
* returned to {@code READY}, from which {@code StartApplication} starts it again, and a {@code READY} one is left
|
||||
* as it is. A surviving cluster not kept is removed before a new one starts. An application stopped during its
|
||||
* restore is {@code STOPPING} until the restore has removed the cluster it kept or started, and {@code READY} then;
|
||||
* one still {@code STOPPING} at the next start has what survives of its cluster removed, and is {@code READY}
|
||||
* then. While Docker keeps a cluster the restore removes, its application stays {@code STOPPING}, for a later
|
||||
* start to remove it. Every application is tried, and the ones that could not be restored
|
||||
* fail the returned future together. A shutdown during the restore leaves the applications not started yet
|
||||
* {@code STARTING}, for the next start.
|
||||
*/
|
||||
public CompletableFuture<Void> restorePersistedRuntime() {
|
||||
if (config.services().kinesisAnalytics().mock()) {
|
||||
return CompletableFuture.completedFuture(null);
|
||||
}
|
||||
List<RestoringApplication> pending = new ArrayList<>();
|
||||
for (AccountEntry<FlinkApplication> entry : persistedApplications()) {
|
||||
RestoringApplication restoring = prepareRestore(entry.accountId(), entry.key());
|
||||
if (restoring != null) {
|
||||
pending.add(restoring);
|
||||
}
|
||||
}, 1, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
RuntimeException failure = null;
|
||||
for (RestoringApplication restoring : pending) {
|
||||
if (containerManager.isShuttingDown()) {
|
||||
// Applications not reached yet stay starting, which the next start restores.
|
||||
break;
|
||||
}
|
||||
FlinkApplication app = restoring.app();
|
||||
try {
|
||||
String region = regionResolver.regionOf(app.getApplicationArn());
|
||||
RequestScopes.runAs(restoring.accountId(), region, () -> restoreApplication(restoring));
|
||||
} catch (FlinkContainerManager.ShuttingDownException e) {
|
||||
LOG.debugv("Kinesis Analytics V2 application {0} not restored: {1}", app.getApplicationName(),
|
||||
e.getMessage());
|
||||
} catch (RuntimeException e) {
|
||||
RuntimeException restoreFailure = new IllegalStateException("Could not restore Kinesis "
|
||||
+ "Analytics V2 application " + app.getApplicationName() + " of account "
|
||||
+ restoring.accountId(), e);
|
||||
if (failure == null) {
|
||||
failure = restoreFailure;
|
||||
} else {
|
||||
failure.addSuppressed(restoreFailure);
|
||||
}
|
||||
} finally {
|
||||
endRestore(restoring);
|
||||
}
|
||||
}
|
||||
if (failure != null) {
|
||||
throw failure;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Marks an application the restore brings back, under its lock; null for one it leaves as it is. */
|
||||
private RestoringApplication prepareRestore(String accountId, String key) {
|
||||
synchronized (storedApplicationLock(accountId, key)) {
|
||||
FlinkApplication app = storedApplication(accountId, key).orElse(null);
|
||||
if (app == null || app.getApplicationStatus() == ApplicationStatus.READY) {
|
||||
return null;
|
||||
}
|
||||
if (app.getAccountId() == null) {
|
||||
app.setAccountId(accountId);
|
||||
}
|
||||
restoringApplications.add(restoringKey(app));
|
||||
// A STOPPING application was stopped during a restore that Floci did not live to finish: the stop stands,
|
||||
// and its restore removes what survives of its cluster before it turns READY.
|
||||
if (app.getApplicationStatus() != ApplicationStatus.STOPPING) {
|
||||
app.setApplicationStatus(ApplicationStatus.STARTING);
|
||||
}
|
||||
putApplication(app);
|
||||
return new RestoringApplication(accountId, key, app);
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreApplication(RestoringApplication restoring) {
|
||||
FlinkApplication app = restoring.app();
|
||||
Object lock = storedApplicationLock(restoring.accountId(), restoring.key());
|
||||
ApplicationStatus status;
|
||||
synchronized (lock) {
|
||||
status = storedStatus(restoring);
|
||||
}
|
||||
if (status != ApplicationStatus.STARTING) {
|
||||
if (status == ApplicationStatus.STOPPING) {
|
||||
// Stopped while it waited its turn, or before an earlier Floci finished restoring it: a cluster that
|
||||
// outlived Floci goes too. Nothing else starts one under its names while the application is STOPPING,
|
||||
// which it is until its restore ends.
|
||||
containerManager.removeSurvivingCluster(app);
|
||||
finishStop(restoring);
|
||||
}
|
||||
return;
|
||||
}
|
||||
boolean kept;
|
||||
try {
|
||||
kept = containerManager.adoptRunningCluster(app);
|
||||
if (!kept) {
|
||||
// What survives of a cluster not kept goes first, under either name form, so two jobs never run.
|
||||
containerManager.removeSurvivingCluster(app);
|
||||
// The job ran on the cluster shutdown removed; the new cluster runs it again.
|
||||
app.setFlinkJobId(null);
|
||||
containerManager.startCluster(app);
|
||||
}
|
||||
} catch (FlinkContainerManager.ShuttingDownException e) {
|
||||
throw e;
|
||||
} catch (RuntimeException e) {
|
||||
// READY, from which StartApplication starts it again, once its cluster is gone; STOPPING while Docker keeps
|
||||
// some of it, naming it for a later start to remove.
|
||||
ApplicationStatus settled = ApplicationStatus.READY;
|
||||
try {
|
||||
containerManager.removeOwnCluster(app);
|
||||
// A start that failed part way cleaned up after itself without confirming it, and forgot its
|
||||
// containers: what is left under the cluster's names goes too, confirmed.
|
||||
containerManager.removeSurvivingCluster(app);
|
||||
} catch (RuntimeException cleanup) {
|
||||
e.addSuppressed(cleanup);
|
||||
settled = ApplicationStatus.STOPPING;
|
||||
}
|
||||
synchronized (lock) {
|
||||
if (storedStatus(restoring) != null) {
|
||||
restoringApplications.remove(restoringKey(app));
|
||||
app.setApplicationStatus(settled);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
putApplication(app);
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
synchronized (lock) {
|
||||
if (storedStatus(restoring) == ApplicationStatus.STARTING) {
|
||||
// Its restore ends here: from now on a stop stops the cluster itself.
|
||||
restoringApplications.remove(restoringKey(app));
|
||||
if (kept) {
|
||||
app.setApplicationStatus(ApplicationStatus.RUNNING);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
}
|
||||
putApplication(app);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Stopped while its cluster started: the cluster goes before the application turns READY, from which another
|
||||
// start could begin under the same names. Only this cluster goes, by its containers. While Docker keeps some of
|
||||
// it, this throws and the application stays STOPPING, for a later start to remove the rest.
|
||||
containerManager.removeOwnCluster(app);
|
||||
finishStop(restoring);
|
||||
}
|
||||
|
||||
/** Ends the restore of an application stopped during it, now its cluster is gone: it turns {@code READY}. */
|
||||
private void finishStop(RestoringApplication restoring) {
|
||||
FlinkApplication app = restoring.app();
|
||||
synchronized (storedApplicationLock(restoring.accountId(), restoring.key())) {
|
||||
restoringApplications.remove(restoringKey(app));
|
||||
if (storedStatus(restoring) == ApplicationStatus.STOPPING) {
|
||||
app.setApplicationStatus(ApplicationStatus.READY);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
putApplication(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends an application's restore, if it has not ended already: the poller and {@code StopApplication} treat the
|
||||
* application as any other again. One still {@code STOPPING} here, when Docker kept its cluster or shutdown
|
||||
* overtook its restore, stays so, for the next start to remove what is left.
|
||||
*/
|
||||
private void endRestore(RestoringApplication restoring) {
|
||||
synchronized (storedApplicationLock(restoring.accountId(), restoring.key())) {
|
||||
restoringApplications.remove(restoringKey(restoring.app()));
|
||||
}
|
||||
}
|
||||
|
||||
/** The stored application's status, or null when it is gone or another application was created under its name. */
|
||||
private ApplicationStatus storedStatus(RestoringApplication restoring) {
|
||||
return storedApplication(restoring.accountId(), restoring.key())
|
||||
.filter(current -> Objects.equals(current.getCreateTimestamp(), restoring.app().getCreateTimestamp()))
|
||||
.map(FlinkApplication::getApplicationStatus)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The stored applications, each found as a describe finds it, by its account, Region and name. A record stored
|
||||
* under an older key moves onto its current one, so the restore writing it back leaves no second record behind
|
||||
* for the next start to restore again.
|
||||
*/
|
||||
private List<AccountEntry<FlinkApplication>> persistedApplications() {
|
||||
List<AccountEntry<FlinkApplication>> stored;
|
||||
if (storage instanceof AccountAwareStorageBackend<FlinkApplication> aware) {
|
||||
stored = aware.scanAllAccountEntries(key -> true);
|
||||
} else {
|
||||
stored = storage.scan(key -> true).stream()
|
||||
.map(app -> new AccountEntry<>(Objects.requireNonNullElse(app.getAccountId(),
|
||||
regionResolver.getDefaultAccountId()), app.getApplicationName(), app))
|
||||
.toList();
|
||||
}
|
||||
Map<String, AccountEntry<FlinkApplication>> applications = new LinkedHashMap<>();
|
||||
for (AccountEntry<FlinkApplication> entry : stored) {
|
||||
String accountId = entry.accountId();
|
||||
String region = regionResolver.regionOf(entry.value().getApplicationArn());
|
||||
String name = entry.value().getApplicationName();
|
||||
String key = applicationKey(region, name);
|
||||
getStoredApplication(accountId, region, name).ifPresent(app ->
|
||||
applications.put(accountId + ":" + key, new AccountEntry<>(accountId, key, app)));
|
||||
}
|
||||
return List.copyOf(applications.values());
|
||||
}
|
||||
|
||||
private Optional<FlinkApplication> storedApplication(String accountId, String key) {
|
||||
if (storage instanceof AccountAwareStorageBackend<FlinkApplication> aware) {
|
||||
return aware.getForAccount(accountId, key);
|
||||
}
|
||||
return storage.get(key);
|
||||
}
|
||||
|
||||
private Object storedApplicationLock(String accountId, String key) {
|
||||
return storedApplicationLocks.lockFor(accountId + ":" + key);
|
||||
}
|
||||
|
||||
/** An application the restore brings back, with the account and key it is stored under. */
|
||||
private record RestoringApplication(String accountId, String key, FlinkApplication app) {
|
||||
}
|
||||
|
||||
private String restoringKey(FlinkApplication app) {
|
||||
return app.getAccountId() + ":" + app.getApplicationArn();
|
||||
}
|
||||
|
||||
private void startReadinessPoller() {
|
||||
poller.scheduleAtFixedRate(this::pollReadiness, 1, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/** One pass of the readiness poller: drives each starting application toward RUNNING, and its snapshots. */
|
||||
void pollReadiness() {
|
||||
try {
|
||||
if (config.services().kinesisAnalytics().mock()) {
|
||||
return;
|
||||
}
|
||||
for (FlinkApplication app : allApplications()) {
|
||||
boolean changed = false;
|
||||
if (app.getApplicationStatus() == ApplicationStatus.STARTING
|
||||
&& !restoringApplications.contains(restoringKey(app))) {
|
||||
String previousJobId = app.getFlinkJobId();
|
||||
if (containerManager.advanceToRunning(app)) {
|
||||
LOG.infov("Kinesis Analytics V2 application {0} is now RUNNING",
|
||||
app.getApplicationName());
|
||||
app.setApplicationStatus(ApplicationStatus.RUNNING);
|
||||
app.setLastUpdateTimestamp(Instant.now());
|
||||
changed = true;
|
||||
} else if (!Objects.equals(previousJobId, app.getFlinkJobId())) {
|
||||
// The Flink job was just submitted (flinkJobId newly assigned) but hasn't
|
||||
// reached RUNNING yet. Persist now rather than waiting for that: pendingJars
|
||||
// (an in-process-only cache) is already cleared at this point, so if the
|
||||
// emulator restarts before the next RUNNING-triggered persist, an unpersisted
|
||||
// flinkJobId would leave the application stuck — it could never re-fetch the
|
||||
// JAR to resubmit, nor know a job was already running to poll instead.
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
for (Snapshot snapshot : app.getSnapshots().values()) {
|
||||
if (snapshot.getSnapshotStatus() == SnapshotStatus.CREATING
|
||||
&& containerManager.advanceSnapshot(app, snapshot)) {
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed) {
|
||||
putApplication(app);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error in Kinesis Analytics V2 readiness poller", e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<FlinkApplication> allApplications() {
|
||||
|
||||
+142
-2
@@ -3,6 +3,7 @@ package io.floci.aws.services.kinesisanalytics.container;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.github.dockerjava.api.model.Container;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
@@ -81,6 +82,16 @@ public class FlinkContainerManager {
|
||||
private final Map<String, byte[]> pendingJars = new ConcurrentHashMap<>();
|
||||
// Applications whose job submission hard-failed (e.g. a bad JAR) — not retried by the poller.
|
||||
private final Set<String> submissionFailed = ConcurrentHashMap.newKeySet();
|
||||
// Set by stopAll(). A start still running when shutdown removes the containers, as a restore's can be while an
|
||||
// image pulls, removes its own containers rather than leave them behind after the cleanup.
|
||||
private boolean shuttingDown;
|
||||
|
||||
/** Thrown by a start that shutdown overtook; its containers, if any, are already removed. */
|
||||
public static final class ShuttingDownException extends IllegalStateException {
|
||||
public ShuttingDownException(String applicationName) {
|
||||
super("Floci is shutting down; not starting the Flink cluster of application " + applicationName);
|
||||
}
|
||||
}
|
||||
|
||||
@Inject
|
||||
public FlinkContainerManager(ContainerBuilder containerBuilder,
|
||||
@@ -139,6 +150,9 @@ public class FlinkContainerManager {
|
||||
* StartApplication fast; the JAR is uploaded/run asynchronously by the readiness poller.
|
||||
*/
|
||||
public void startCluster(FlinkApplication app) {
|
||||
if (isShuttingDown()) {
|
||||
throw new ShuttingDownException(app.getApplicationName());
|
||||
}
|
||||
String image = KinesisAnalyticsRuntimes.resolveImage(
|
||||
config.services().kinesisAnalytics().defaultImage(), app.getRuntimeEnvironment());
|
||||
String jmName = containerName(app);
|
||||
@@ -203,8 +217,14 @@ public class FlinkContainerManager {
|
||||
lifecycleManager.removeIfExists(jmName);
|
||||
throw e;
|
||||
}
|
||||
synchronized (this) {
|
||||
if (shuttingDown) {
|
||||
lifecycleManager.stopAndRemove(jm.containerId(), null);
|
||||
throw new ShuttingDownException(app.getApplicationName());
|
||||
}
|
||||
containerIds.put(runtimeKey(app), jm.containerId());
|
||||
}
|
||||
app.setContainerId(jm.containerId());
|
||||
containerIds.put(runtimeKey(app), jm.containerId());
|
||||
EndpointInfo rest = jm.getEndpoint(JOBMANAGER_REST_PORT);
|
||||
app.setRestEndpoint("http://" + rest.host() + ":" + rest.port());
|
||||
attachLogs(app, jm.containerId());
|
||||
@@ -234,7 +254,12 @@ public class FlinkContainerManager {
|
||||
msfStyleLog4j2Config(app), true);
|
||||
ContainerInfo tm = lifecycleManager.startCreated(tmContainerId, tmSpec);
|
||||
app.setTaskManagerContainerId(tm.containerId());
|
||||
taskManagerIds.put(runtimeKey(app), tm.containerId());
|
||||
synchronized (this) {
|
||||
if (shuttingDown) {
|
||||
throw new ShuttingDownException(app.getApplicationName());
|
||||
}
|
||||
taskManagerIds.put(runtimeKey(app), tm.containerId());
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
// Roll back the whole cluster so a failed start leaves nothing behind.
|
||||
lifecycleManager.removeIfExists(tmName);
|
||||
@@ -599,12 +624,79 @@ public class FlinkContainerManager {
|
||||
return app.getDockerVolumeName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes whatever of {@code app}'s cluster outlived Floci, under the current or the legacy {@code floci-} names:
|
||||
* for a restore that does not keep that cluster, before it starts a new one, and for an application stopped before
|
||||
* its restore reached it. Nothing else runs a cluster under these names while a restore holds the application.
|
||||
* The record forgets that cluster and its job, as {@link #stopCluster} leaves it, so a later start submits the
|
||||
* code again rather than waiting on the old job. Each removal is confirmed: when Docker cannot remove a container
|
||||
* this throws, and the record still names the cluster.
|
||||
*/
|
||||
public void removeSurvivingCluster(FlinkApplication app) {
|
||||
String name = containerName(app);
|
||||
// The TaskManager first, as stopCluster does: it shares the JobManager's network namespace.
|
||||
removeConfirmed(name + "-tm");
|
||||
removeConfirmed(name);
|
||||
forgetCluster(app);
|
||||
}
|
||||
|
||||
/** Removes the container under {@code name} and under its legacy form, failing unless Docker confirms both gone. */
|
||||
private void removeConfirmed(String name) {
|
||||
lifecycleManager.removeIfExistsStrict(name);
|
||||
String legacyName = ContainerStorageHelper.legacyDockerName(config, name);
|
||||
if (!legacyName.equals(name)) {
|
||||
lifecycleManager.removeIfExistsStrict(legacyName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the cluster {@code app} itself names, by its containers' IDs, for a restore whose application was
|
||||
* stopped, deleted or created again while that cluster started. What the application's key tracks is cleared only
|
||||
* while it still names these containers: an application created again under the same name shares the key, and its
|
||||
* cluster, pending code and logs are left alone. The record forgets the cluster and its job, as
|
||||
* {@link #stopCluster} leaves it. Each removal is confirmed: when Docker cannot remove a container this throws,
|
||||
* and the record still names it and it stays tracked, for shutdown and a later attempt to remove.
|
||||
*/
|
||||
public void removeOwnCluster(FlinkApplication app) {
|
||||
String key = runtimeKey(app);
|
||||
String tmId = app.getTaskManagerContainerId();
|
||||
if (tmId != null) {
|
||||
lifecycleManager.stopAndRemove(tmId, null);
|
||||
// stopAndRemove only logs a removal Docker refused; this throws for it, before the tracking is cleared.
|
||||
lifecycleManager.removeIfExistsStrict(tmId);
|
||||
taskManagerIds.remove(key, tmId);
|
||||
}
|
||||
String jmId = app.getContainerId();
|
||||
if (jmId != null) {
|
||||
Closeable logHandle = jmId.equals(containerIds.get(key)) ? logStreams.get(key) : null;
|
||||
lifecycleManager.stopAndRemove(jmId, logHandle);
|
||||
lifecycleManager.removeIfExistsStrict(jmId);
|
||||
if (containerIds.remove(key, jmId)) {
|
||||
if (logHandle != null) {
|
||||
logStreams.remove(key, logHandle);
|
||||
}
|
||||
pendingJars.remove(key);
|
||||
}
|
||||
}
|
||||
forgetCluster(app);
|
||||
}
|
||||
|
||||
private static void forgetCluster(FlinkApplication app) {
|
||||
app.setContainerId(null);
|
||||
app.setRestEndpoint(null);
|
||||
app.setTaskManagerContainerId(null);
|
||||
app.setFlinkJobId(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops and removes every running Flink container (JobManagers and TaskManagers). Wired into
|
||||
* {@code EmulatorLifecycle.onStop()} so containers are torn down on shutdown alongside the other
|
||||
* container managers.
|
||||
*/
|
||||
public void stopAll() {
|
||||
synchronized (this) {
|
||||
shuttingDown = true;
|
||||
}
|
||||
if (!containerIds.isEmpty()) {
|
||||
LOG.infov("Stopping {0} Flink cluster(s) on shutdown", containerIds.size());
|
||||
}
|
||||
@@ -624,6 +716,54 @@ public class FlinkContainerManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether shutdown has removed the containers, after which no Flink cluster starts. */
|
||||
public synchronized boolean isShuttingDown() {
|
||||
return shuttingDown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps an application's cluster that outlived Floci, as one does when Floci is killed rather than shut down,
|
||||
* when it still serves the application: its JobManager, and its TaskManager when it runs code, are running, the
|
||||
* JobManager answers on the REST endpoint the application's record names, and the job the record names is still
|
||||
* {@code RUNNING}. Its job and its state carry on. The containers are tracked again, so shutdown removes them,
|
||||
* and the JobManager's logs are streamed again. Returns whether the cluster was kept.
|
||||
*/
|
||||
public boolean adoptRunningCluster(FlinkApplication app) {
|
||||
String rest = app.getRestEndpoint();
|
||||
if (rest == null || (app.hasCode() && app.getFlinkJobId() == null)) {
|
||||
return false;
|
||||
}
|
||||
Optional<Container> jobManager = runningContainer(containerName(app));
|
||||
Optional<Container> taskManager = app.hasCode()
|
||||
? runningContainer(containerName(app) + "-tm") : Optional.empty();
|
||||
if (jobManager.isEmpty() || (app.hasCode() && taskManager.isEmpty()) || !flinkRest.isRestUp(rest)
|
||||
|| (app.hasCode() && !"RUNNING".equals(flinkRest.jobState(rest, app.getFlinkJobId())))) {
|
||||
return false;
|
||||
}
|
||||
synchronized (this) {
|
||||
if (shuttingDown) {
|
||||
return false;
|
||||
}
|
||||
containerIds.put(runtimeKey(app), jobManager.get().getId());
|
||||
taskManager.ifPresent(container -> taskManagerIds.put(runtimeKey(app), container.getId()));
|
||||
}
|
||||
app.setContainerId(jobManager.get().getId());
|
||||
app.setTaskManagerContainerId(taskManager.map(Container::getId).orElse(null));
|
||||
attachLogs(app, jobManager.get().getId());
|
||||
LOG.infov("Kept the running Flink cluster of application {0}", app.getApplicationName());
|
||||
return true;
|
||||
}
|
||||
|
||||
/** The running container of the name, or of its name before the floci-aws- prefix. */
|
||||
private Optional<Container> runningContainer(String name) {
|
||||
Optional<Container> container = lifecycleManager.findByName(name);
|
||||
String legacyName = ContainerStorageHelper.legacyDockerName(config, name);
|
||||
if (container.isEmpty() && !legacyName.equals(name)) {
|
||||
container = lifecycleManager.findByName(legacyName);
|
||||
}
|
||||
return container.filter(running -> "running".equals(running.getState()));
|
||||
}
|
||||
|
||||
private void attachLogs(FlinkApplication app, String containerId) {
|
||||
String shortId = containerId.length() >= 8 ? containerId.substring(0, 8) : containerId;
|
||||
String logGroup = "/aws/kinesis-analytics/" + app.getApplicationName();
|
||||
|
||||
@@ -10,8 +10,9 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
* <p>Only the states this emulator transitions through are modelled:
|
||||
* {@code CreateApplication} lands in {@link #READY}; {@code StartApplication} moves
|
||||
* {@link #STARTING} → {@link #RUNNING} (the backing Flink container coming up);
|
||||
* {@code StopApplication} returns to {@link #READY}; {@code DeleteApplication} passes
|
||||
* through {@link #DELETING}.
|
||||
* {@code StopApplication} returns to {@link #READY}, through {@link #STOPPING} when the startup
|
||||
* restore is starting the application's cluster; {@code DeleteApplication} passes through
|
||||
* {@link #DELETING}.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
public enum ApplicationStatus {
|
||||
|
||||
Reference in new issue
Block a user