mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(emr): keep a cancelled running step's place until its driver is gone (#8351)
CancelSteps on a RUNNING step stopped its driver and started the next
PENDING step at once, but stopping a driver only asks for it: with a
step concurrency level of 1 the next step's driver could start beside
the cancelled one's. The Management Guide's "Cancel steps": "Canceling
a running step does not allow a pending step to start running, assuming
no change to stepConcurrencyLevel", and CancelSteps "is idempotent but
asynchronous".
In the docker runner mode a cancelled RUNNING step is now CANCEL_PENDING
while its driver stops, with the guide's step event for that state ("was
cancelled at Time and is pending cancellation", WARN), and keeps its
place among the running steps. Once its driver is gone, however it
ended, the step is CANCELLED, takes no ActionOnFailure, and the next
PENDING step starts. A second cancel answers SUBMITTED. A driver Floci's
own teardown stops leaves the step CANCEL_PENDING, as it leaves a
RUNNING one, and a restart cancels it, in either runner mode;
TerminateJobFlows cancels it at once. Until it is gone it also counts as
running for a failed step's CANCEL_AND_WAIT. In the immediate mode
nothing runs, so a cancelled step is still CANCELLED at once.
This commit is contained in:
1 parent
bbcc58f03b
commit
7981cadc7c
8 files changed
+303
-76
No files matched your search
+21
-11
@@ -68,12 +68,21 @@ API reference says. Each call finds the clusters it reads as they stand at that
|
||||
duration at its default of 0, a cluster is ready, and its steps complete, before the call that started
|
||||
them returns.
|
||||
|
||||
`CancelSteps` cancels a `PENDING` step, and a `RUNNING` one from release `emr-5.28.0`, as its API
|
||||
reference says. A cancelled `RUNNING` step makes room for the next `PENDING` one. Its container stops, and what the
|
||||
applications it started on YARN have logged goes to `containers/` (see **Output**), while an application in
|
||||
`--deploy-mode cluster` runs on to its end: EMR's cancel signals the step and its descendant processes, and
|
||||
"asynchronous processes are not affected", as the Management Guide puts it. On an earlier release
|
||||
a `RUNNING` step answers `FAILED` with "Step is not in a cancellable state: RUNNING".
|
||||
`CancelSteps` cancels a `PENDING` step, and a `RUNNING` one from release `emr-5.28.0`, as its API reference says,
|
||||
adding that it "is idempotent but asynchronous". On an earlier release a `RUNNING` step answers `FAILED` with "Step
|
||||
is not in a cancellable state: RUNNING".
|
||||
|
||||
In the `docker` runner mode a cancelled `RUNNING` step is `CANCEL_PENDING` while its driver stops, and keeps its
|
||||
place among the running steps until the driver is gone: then it is `CANCELLED`, and the next `PENDING` step starts.
|
||||
That is Floci's reading of the Management Guide's "Canceling a running step does not allow a pending step to start
|
||||
running, assuming no change to stepConcurrencyLevel", beside "Canceling a running or pending step removes that step
|
||||
from the active step count"; its step events have a `CANCEL_PENDING` row, "was cancelled at Time and is pending
|
||||
cancellation". Cancelling a `CANCEL_PENDING` step again answers `SUBMITTED`, and the step is `CANCELLED` however
|
||||
its driver ends. The step's container stops, and what the applications it started on YARN have logged goes to
|
||||
`containers/` (see **Output**), while an application in `--deploy-mode cluster` runs on to its end: EMR's cancel
|
||||
signals the step and its descendant processes, and "asynchronous processes are not affected", as the Management
|
||||
Guide puts it. In the `immediate` mode nothing runs, so a cancelled `RUNNING` step is `CANCELLED` at once and makes
|
||||
room for the next.
|
||||
|
||||
A cluster created with `Instances.KeepJobFlowAliveWhenNoSteps` set to `false` terminates after its
|
||||
initial steps or steps added later complete, reporting `ALL_STEPS_COMPLETED`, without waiting first. A
|
||||
@@ -439,11 +448,12 @@ holding the driver and its executors, or a command or a script. The cluster itse
|
||||
its steps. A cluster a step failure ends cancels its other steps and goes `TERMINATING`, then
|
||||
`TERMINATED_WITH_ERRORS`, with the reason `STEP_FAILURE` and the message "Shut down as step failed".
|
||||
While `StepConcurrencyLevel` is above 1 no action is taken, and with level 1 and other steps still
|
||||
running `CANCEL_AND_WAIT` is not either, as the guide says.
|
||||
running, a `CANCEL_PENDING` one among them, `CANCEL_AND_WAIT` is not either, as the guide says.
|
||||
* **Cancelling**: `CancelSteps` on a `RUNNING` step, and `TerminateJobFlows`, stop its driver at once; a
|
||||
cancelled step takes no `ActionOnFailure`. A step Floci was running when it stopped is `FAILED` once
|
||||
Floci is back, and its `ActionOnFailure` is taken then. Floci's shutdown and a state reset stop every
|
||||
driver, then the clusters' HDFS, a step not yet at its driver as if its driver were stopped, and start no other
|
||||
Floci is back, and its `ActionOnFailure` is taken then; one it was cancelling is `CANCELLED`, in either runner
|
||||
mode. Floci's
|
||||
shutdown and a state reset stop every driver, then the clusters' HDFS, a step not yet at its driver as if its driver were stopped, and start no other
|
||||
step; a driver a hard stop left running is removed when the EMR service
|
||||
starts, before any step can start, unless `reconcile-containers-on-startup` is off.
|
||||
* **What `spark-submit` gets**: the step's arguments, after the `spark-defaults` properties of the
|
||||
@@ -551,7 +561,7 @@ Each change sends its events:
|
||||
| The cluster is ready, or a step completes | the step `COMPLETED`; each step it makes room for `RUNNING`; once none is left, the cluster `WAITING`, or `TERMINATED` with `ALL_STEPS_COMPLETED` when it ends after its steps |
|
||||
| A step fails (`docker` runner mode) | the step `FAILED`; then as its `ActionOnFailure` goes: as a completed step, or `CANCELLED` for each step it cancels and the cluster `WAITING`, or `CANCELLED` for each step not yet completed and the cluster `TERMINATED_WITH_ERRORS` with `STEP_FAILURE` |
|
||||
| `TerminateJobFlows` | `CANCELLED` for each step not yet completed; the cluster `TERMINATED` with `USER_REQUEST` once it has been `TERMINATING` for the delay, also when a protected cluster fails the call |
|
||||
| `CancelSteps` | `CANCELLED` for each step it cancels, and `RUNNING` for the step a cancelled `RUNNING` one makes room for |
|
||||
| `CancelSteps` | `CANCELLED` for each `PENDING` step it cancels; for a `RUNNING` one, `CANCEL_PENDING`, then `CANCELLED` once its driver is gone (at once in the `immediate` runner mode), and `RUNNING` for the step it makes room for |
|
||||
|
||||
A change that falls due between calls, such as a step completing after `step-duration-seconds`, is
|
||||
made at that time and its events sent then, whether or not anything reads the cluster.
|
||||
@@ -560,7 +570,7 @@ A `STARTING` or `WAITING` cluster's `stateChangeReason` is `{"code":""}`, as in
|
||||
and a terminated one's carries the reason's code and message, such as
|
||||
`{"code":"USER_REQUEST","message":"Terminated by user request"}`. `TERMINATED` is `INFO` for
|
||||
`USER_REQUEST` and `ALL_STEPS_COMPLETED` and `CRITICAL` for any other reason, and
|
||||
`TERMINATED_WITH_ERRORS` is `CRITICAL`; a cancelled step is `WARN` and a failed one `ERROR`. The tables have no row for a cluster's `BOOTSTRAPPING`, `RUNNING` or `TERMINATING`, which
|
||||
`TERMINATED_WITH_ERRORS` is `CRITICAL`; a `CANCEL_PENDING` or cancelled step is `WARN` and a failed one `ERROR`. The tables have no row for a cluster's `BOOTSTRAPPING`, `RUNNING` or `TERMINATING`, which
|
||||
send nothing, so a `WAITING` cluster that runs added steps sends `WAITING` again when they are done.
|
||||
The guide gives two `WAITING` messages without saying when each applies: a cluster that ran steps
|
||||
reports it "finished running all pending steps", one that did not that it "was created ... and is
|
||||
|
||||
@@ -38,6 +38,7 @@ final class EmrClusterLifecycle {
|
||||
static final String TERMINATED = "TERMINATED";
|
||||
static final String PENDING = "PENDING";
|
||||
static final String COMPLETED = "COMPLETED";
|
||||
static final String CANCEL_PENDING = "CANCEL_PENDING";
|
||||
static final String CANCELLED = "CANCELLED";
|
||||
static final String FAILED = "FAILED";
|
||||
static final String TERMINATED_WITH_ERRORS = "TERMINATED_WITH_ERRORS";
|
||||
@@ -148,22 +149,35 @@ final class EmrClusterLifecycle {
|
||||
cluster.setStateChangeMessage(message);
|
||||
cluster.setTerminatingDateTime(now);
|
||||
for (EmrStep step : cluster.getSteps()) {
|
||||
if (PENDING.equals(step.getState()) || RUNNING.equals(step.getState())) {
|
||||
if (PENDING.equals(step.getState()) || holdsAPlace(step)) {
|
||||
cancelStep(cluster, step, now, region);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CancelSteps for one step: null once it is cancelled, or why it cannot be. CancelSteps: "When you
|
||||
* use Amazon EMR releases 5.28.0 and later, you can cancel steps that are in a PENDING or RUNNING
|
||||
* state." A cancelled RUNNING step makes room for the next PENDING one.
|
||||
* CancelSteps for one step: null once its cancel is under way, or why it cannot be. CancelSteps: "When you
|
||||
* use Amazon EMR releases 5.28.0 and later, you can cancel steps that are in a PENDING or RUNNING state",
|
||||
* and it "is idempotent but asynchronous". A RUNNING step whose driver runs is CANCEL_PENDING, "cancelled
|
||||
* at Time and is pending cancellation" as the guide's step events put it, until its driver is gone
|
||||
* ({@link #stepEnded}), and keeps its place among the running steps meanwhile: "Canceling a running step does
|
||||
* not allow a pending step to start running, assuming no change to stepConcurrencyLevel". One that completes
|
||||
* by time, nothing running it, is CANCELLED at once and makes room for the next PENDING one.
|
||||
*/
|
||||
String cancel(EmrCluster cluster, EmrStep step, Instant now, String region) {
|
||||
if (CANCEL_PENDING.equals(step.getState())) {
|
||||
return null;
|
||||
}
|
||||
boolean running = RUNNING.equals(step.getState()) && cancelsRunningSteps(cluster.getReleaseLabel());
|
||||
if (!running && !PENDING.equals(step.getState())) {
|
||||
return "Step is not in a cancellable state: " + step.getState();
|
||||
}
|
||||
if (running && !timed(step)) {
|
||||
step.setState(CANCEL_PENDING);
|
||||
events.queue(events.stepCancelPending(cluster, step, now), region);
|
||||
execution.stop(step);
|
||||
return null;
|
||||
}
|
||||
cancelStep(cluster, step, now, region);
|
||||
if (running) {
|
||||
runSteps(cluster, now, region);
|
||||
@@ -271,7 +285,7 @@ final class EmrClusterLifecycle {
|
||||
* cluster is done.
|
||||
*/
|
||||
private void runSteps(EmrCluster cluster, Instant at, String region) {
|
||||
long running = cluster.getSteps().stream().filter(step -> RUNNING.equals(step.getState())).count();
|
||||
long running = cluster.getSteps().stream().filter(EmrClusterLifecycle::holdsAPlace).count();
|
||||
// The model allows 1 to 256; Floci stores any level it is given, and a lower one would never start a step.
|
||||
int level = Math.max(1, cluster.getStepConcurrencyLevel());
|
||||
List<EmrStep> started = new ArrayList<>();
|
||||
@@ -303,10 +317,16 @@ final class EmrClusterLifecycle {
|
||||
|
||||
/**
|
||||
* A step run for real has ended: COMPLETED when {@code failure} is null, otherwise FAILED, with
|
||||
* {@code failure} as its FailureDetails message, and its ActionOnFailure taken. Either way the
|
||||
* cluster goes on as it would once a step is done.
|
||||
* {@code failure} as its FailureDetails message, and its ActionOnFailure taken. A CANCEL_PENDING one is
|
||||
* CANCELLED, however its driver ended, and takes no action: "Canceling a running step does not trigger the
|
||||
* step ActionOnFailure". Either way the cluster goes on as it would once a step is done.
|
||||
*/
|
||||
void stepEnded(EmrCluster cluster, EmrStep step, String failure, Instant at, String region) {
|
||||
if (CANCEL_PENDING.equals(step.getState())) {
|
||||
cancelStep(cluster, step, at, region);
|
||||
runSteps(cluster, at, region);
|
||||
return;
|
||||
}
|
||||
step.setEndDateTime(at);
|
||||
if (failure == null) {
|
||||
step.setState(COMPLETED);
|
||||
@@ -340,7 +360,8 @@ final class EmrClusterLifecycle {
|
||||
terminate(cluster, STEP_FAILURE, SHUT_DOWN_AS_STEP_FAILED, at, region);
|
||||
return;
|
||||
}
|
||||
boolean othersRunning = cluster.getSteps().stream().anyMatch(other -> RUNNING.equals(other.getState()));
|
||||
// The failed step is FAILED by now; one whose cancel is pending still runs until its driver is gone.
|
||||
boolean othersRunning = cluster.getSteps().stream().anyMatch(EmrClusterLifecycle::holdsAPlace);
|
||||
if (activates && "CANCEL_AND_WAIT".equals(action) && !othersRunning) {
|
||||
if (cluster.isAutoTerminate()) {
|
||||
terminate(cluster, STEP_FAILURE, SHUT_DOWN_AS_STEP_FAILED, at, region);
|
||||
@@ -397,6 +418,11 @@ final class EmrClusterLifecycle {
|
||||
return bootstrapping == null || !cluster.isBootstrapInContainer();
|
||||
}
|
||||
|
||||
/** Whether {@code step} takes one of its cluster's StepConcurrencyLevel places: RUNNING, or CANCEL_PENDING. */
|
||||
static boolean holdsAPlace(EmrStep step) {
|
||||
return RUNNING.equals(step.getState()) || CANCEL_PENDING.equals(step.getState());
|
||||
}
|
||||
|
||||
/** TERMINATED, or TERMINATED_WITH_ERRORS: the cluster has ended. */
|
||||
static boolean isTerminated(String state) {
|
||||
return TERMINATED.equals(state) || TERMINATED_WITH_ERRORS.equals(state);
|
||||
|
||||
@@ -88,18 +88,31 @@ public class EmrEventPublisher {
|
||||
* FAILED.
|
||||
*/
|
||||
Event stepStatusChange(EmrCluster cluster, EmrStep step, String state) {
|
||||
ObjectNode detail = objectMapper.createObjectNode();
|
||||
detail.put("severity", switch (state) {
|
||||
return stepEvent(cluster, step, state, switch (state) {
|
||||
case "CANCELLED" -> "WARN";
|
||||
case "FAILED" -> "ERROR";
|
||||
default -> "INFO";
|
||||
});
|
||||
}, stepMessage(cluster, step, state));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code step} of {@code cluster} going CANCEL_PENDING, cancelled at {@code at}: WARN, as the guide's step events
|
||||
* table has it.
|
||||
*/
|
||||
Event stepCancelPending(EmrCluster cluster, EmrStep step, Instant at) {
|
||||
return stepEvent(cluster, step, "CANCEL_PENDING", "WARN", "Step " + named(step) + " in Amazon EMR cluster "
|
||||
+ named(cluster) + " was cancelled at " + time(at) + " and is pending cancellation.");
|
||||
}
|
||||
|
||||
private Event stepEvent(EmrCluster cluster, EmrStep step, String state, String severity, String message) {
|
||||
ObjectNode detail = objectMapper.createObjectNode();
|
||||
detail.put("severity", severity);
|
||||
detail.put("actionOnFailure", step.getActionOnFailure());
|
||||
detail.put("stepId", step.getId());
|
||||
detail.put("name", step.getName());
|
||||
detail.put("clusterId", cluster.getId());
|
||||
detail.put("state", state);
|
||||
detail.put("message", stepMessage(cluster, step, state));
|
||||
detail.put("message", message);
|
||||
return new Event(STEP_STATUS_CHANGE, detail.toString());
|
||||
}
|
||||
|
||||
@@ -137,7 +150,7 @@ public class EmrEventPublisher {
|
||||
}
|
||||
|
||||
private static String clusterMessage(EmrCluster cluster, String state, Instant at) {
|
||||
String named = cluster.getId() + " (" + cluster.getName() + ")";
|
||||
String named = named(cluster);
|
||||
return switch (state) {
|
||||
case "STARTING" -> "Amazon EMR cluster " + named + " was requested at " + time(at)
|
||||
+ " and is being created.";
|
||||
@@ -153,8 +166,8 @@ public class EmrEventPublisher {
|
||||
}
|
||||
|
||||
private static String stepMessage(EmrCluster cluster, EmrStep step, String state) {
|
||||
String stepNamed = step.getId() + " (" + step.getName() + ")";
|
||||
String clusterNamed = cluster.getId() + " (" + cluster.getName() + ")";
|
||||
String stepNamed = named(step);
|
||||
String clusterNamed = named(cluster);
|
||||
return switch (state) {
|
||||
case "PENDING" -> "Step " + stepNamed + " was added to Amazon EMR cluster " + clusterNamed + " at "
|
||||
+ time(step.getCreationDateTime()) + " and is pending execution.";
|
||||
@@ -174,6 +187,16 @@ public class EmrEventPublisher {
|
||||
};
|
||||
}
|
||||
|
||||
// "StepID (StepName)", as the guide's tables write a step.
|
||||
private static String named(EmrStep step) {
|
||||
return step.getId() + " (" + step.getName() + ")";
|
||||
}
|
||||
|
||||
// "ClusterId (ClusterName)", as the guide's tables write a cluster.
|
||||
private static String named(EmrCluster cluster) {
|
||||
return cluster.getId() + " (" + cluster.getName() + ")";
|
||||
}
|
||||
|
||||
private static String time(Instant instant) {
|
||||
return TIME.format(instant);
|
||||
}
|
||||
|
||||
@@ -286,7 +286,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
for (AccountAwareStorageBackend.AccountEntry<EmrCluster> entry
|
||||
: clusterStore.scanAllAccountEntries(k -> true)) {
|
||||
EmrCluster cluster = entry.value();
|
||||
boolean interrupted = sparkRunner != null && hasContainerStepRunning(cluster)
|
||||
boolean interrupted = cluster.getSteps().stream().anyMatch(this::leftByARestart)
|
||||
|| bootstrapRunner != null && bootstrapsInContainer(cluster);
|
||||
Instant due = interrupted ? clock.instant() : lifecycle.nextChange(cluster);
|
||||
if (due != null) {
|
||||
@@ -528,11 +528,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
public EmrStep describeStep(String clusterId, String stepId) {
|
||||
return call(() -> {
|
||||
EmrCluster cluster = snapshotOf(requireCluster(clusterId));
|
||||
return cluster.getSteps().stream()
|
||||
.filter(s -> s.getId().equals(stepId))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AwsException("InvalidRequestException",
|
||||
"Step " + stepId + " does not exist.", 400));
|
||||
return stepOf(cluster, stepId).orElseThrow(() -> new AwsException("InvalidRequestException",
|
||||
"Step " + stepId + " does not exist.", 400));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -563,8 +560,7 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
EmrCluster cluster = requireCluster(clusterId);
|
||||
List<CancelStepInfo> result = new ArrayList<>();
|
||||
for (String stepId : stepIds) {
|
||||
EmrStep step = cluster.getSteps().stream()
|
||||
.filter(s -> s.getId().equals(stepId)).findFirst().orElse(null);
|
||||
EmrStep step = stepOf(cluster, stepId).orElse(null);
|
||||
String refusal = step == null ? "Step does not exist."
|
||||
: lifecycle.cancel(cluster, step, now(), regionOf(cluster));
|
||||
result.add(refusal == null
|
||||
@@ -1403,25 +1399,35 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
}
|
||||
|
||||
private boolean failInterruptedSteps(EmrCluster cluster) {
|
||||
if (sparkRunner == null) {
|
||||
return false;
|
||||
}
|
||||
boolean failed = false;
|
||||
boolean ended = false;
|
||||
for (EmrStep step : List.copyOf(cluster.getSteps())) {
|
||||
if (runsInContainer(step) && !executing.contains(stepKey(regionResolver.getAccountId(), step.getId()))) {
|
||||
if (leftByARestart(step)) {
|
||||
lifecycle.stepEnded(cluster, step, EmrSparkSteps.INTERRUPTED, now(), regionOf(cluster));
|
||||
failed = true;
|
||||
ended = true;
|
||||
}
|
||||
}
|
||||
return failed;
|
||||
return ended;
|
||||
}
|
||||
|
||||
private static boolean hasContainerStepRunning(EmrCluster cluster) {
|
||||
return cluster.getSteps().stream().anyMatch(EmrService::runsInContainer);
|
||||
/**
|
||||
* Whether a step a container ran is one no thread runs any more, since Floci stopped while it ran: it ends,
|
||||
* FAILED, or CANCELLED when its cancel was pending. Without the docker runner mode only one whose cancel was
|
||||
* pending is, as nothing would end it; a RUNNING one completes by time there.
|
||||
*/
|
||||
private boolean leftByARestart(EmrStep step) {
|
||||
if (sparkRunner == null) {
|
||||
return EmrClusterLifecycle.CANCEL_PENDING.equals(step.getState());
|
||||
}
|
||||
return runsInContainer(step) && !executing.contains(stepKey(regionResolver.getAccountId(), step.getId()));
|
||||
}
|
||||
|
||||
// A step a container runs: RUNNING, or CANCEL_PENDING until its driver is gone.
|
||||
private static boolean runsInContainer(EmrStep step) {
|
||||
return EmrClusterLifecycle.RUNNING.equals(step.getState()) && step.isRunInContainer();
|
||||
return EmrClusterLifecycle.holdsAPlace(step) && step.isRunInContainer();
|
||||
}
|
||||
|
||||
private static Optional<EmrStep> stepOf(EmrCluster cluster, String stepId) {
|
||||
return cluster.getSteps().stream().filter(step -> step.getId().equals(stepId)).findFirst();
|
||||
}
|
||||
|
||||
private static String stepKey(String accountId, String stepId) {
|
||||
@@ -1458,8 +1464,9 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a step its thread ran, COMPLETED when {@code failure} is null and FAILED otherwise, unless a
|
||||
* reset came since it started or the step is no longer RUNNING: cancelled, or on a cluster that ended.
|
||||
* Ends a step its thread ran, COMPLETED when {@code failure} is null and FAILED otherwise, or CANCELLED when a
|
||||
* cancel had made it CANCEL_PENDING, unless a reset came since it started or the step is past running:
|
||||
* cancelled, or on a cluster that ended.
|
||||
*/
|
||||
private void stepEnded(StepLaunch launch, String failure) {
|
||||
if (launch.launchedIn() != generation) {
|
||||
@@ -1468,9 +1475,8 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
// Read while the step still counts as running, so it is not taken for one a restart left.
|
||||
Optional<EmrCluster> found = findCluster(launch.clusterId());
|
||||
executing.remove(stepKey(launch.accountId(), launch.stepId()));
|
||||
EmrStep step = found.flatMap(cluster -> cluster.getSteps().stream()
|
||||
.filter(candidate -> launch.stepId().equals(candidate.getId())).findFirst()).orElse(null);
|
||||
if (step == null || !EmrClusterLifecycle.RUNNING.equals(step.getState())) {
|
||||
EmrStep step = found.flatMap(cluster -> stepOf(cluster, launch.stepId())).orElse(null);
|
||||
if (step == null || !EmrClusterLifecycle.holdsAPlace(step)) {
|
||||
// A step of a cluster that has ended no longer runs: what the cluster kept may go with it, its last one.
|
||||
found.ifPresent(this::releaseEnded);
|
||||
return;
|
||||
@@ -1482,16 +1488,30 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
}
|
||||
|
||||
/**
|
||||
* A step's driver was stopped. A cancel, or its cluster's end, has ended the step already. One still
|
||||
* RUNNING was stopped by Floci itself, by a reset's teardown or its shutdown, and is left as it is, still
|
||||
* counted as running: the reset wipes it, a restart fails it, and its ActionOnFailure, which could start
|
||||
* the next step after the teardown has passed, is not taken now.
|
||||
* A step's driver was stopped. A cancel has made the step CANCEL_PENDING, which is CANCELLED now that its driver
|
||||
* is gone, and makes room for the next; its cluster's end has ended it already. One still RUNNING, or
|
||||
* CANCEL_PENDING, that Floci itself stopped, by a reset's teardown or its shutdown, is left as it is, still
|
||||
* counted as running: the reset wipes it, a restart ends it, and what its end leads to, which could start the
|
||||
* next step after the teardown has passed, does not happen now.
|
||||
*/
|
||||
private void driverStopped(StepLaunch launch) {
|
||||
if (!stillRunning(launch)) {
|
||||
executing.remove(stepKey(launch.accountId(), launch.stepId()));
|
||||
clusterStore.get(launch.clusterId()).ifPresent(this::releaseEnded);
|
||||
if (stillRunning(launch)) {
|
||||
return;
|
||||
}
|
||||
Optional<EmrCluster> found = clusterStore.get(launch.clusterId());
|
||||
Optional<EmrStep> cancelPending = found.flatMap(cluster -> stepOf(cluster, launch.stepId()))
|
||||
.filter(step -> EmrClusterLifecycle.CANCEL_PENDING.equals(step.getState()));
|
||||
if (cancelPending.isPresent() && teardowns.get() != launch.teardownsBefore()) {
|
||||
return;
|
||||
}
|
||||
executing.remove(stepKey(launch.accountId(), launch.stepId()));
|
||||
if (cancelPending.isPresent() && launch.launchedIn() == generation) {
|
||||
EmrCluster cluster = found.get();
|
||||
lifecycle.stepEnded(cluster, cancelPending.get(), null, now(), regionOf(cluster));
|
||||
store(cluster);
|
||||
return;
|
||||
}
|
||||
found.ifPresent(this::releaseEnded);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1509,10 +1529,9 @@ public class EmrService implements Resettable, StoredResourceArns, ContainerTear
|
||||
// Whether a step's driver may still start, or go on: one cancelled, on a cluster that ended, or a reset
|
||||
// removed, is past it.
|
||||
private synchronized boolean stillRunning(StepLaunch launch) {
|
||||
return launch.launchedIn() == generation && clusterStore.get(launch.clusterId()).stream()
|
||||
.flatMap(cluster -> cluster.getSteps().stream())
|
||||
.anyMatch(step -> launch.stepId().equals(step.getId())
|
||||
&& EmrClusterLifecycle.RUNNING.equals(step.getState()));
|
||||
return launch.launchedIn() == generation && clusterStore.get(launch.clusterId())
|
||||
.flatMap(cluster -> stepOf(cluster, launch.stepId()))
|
||||
.filter(step -> EmrClusterLifecycle.RUNNING.equals(step.getState())).isPresent();
|
||||
}
|
||||
|
||||
private synchronized boolean resetSince(StepLaunch launch) {
|
||||
|
||||
@@ -214,6 +214,25 @@ class EmrEventPublisherTest {
|
||||
+ " cancelled.", detail.path("message").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepPendingCancellationIsAWarningSayingWhenItWasCancelled() throws Exception {
|
||||
EmrCluster cluster = service.runJobFlow(cluster("cancelling", true, step("doomed")), REGION);
|
||||
EmrStep step = cluster.getSteps().get(0);
|
||||
|
||||
EmrEventPublisher.Event event = publisher.stepCancelPending(cluster, step,
|
||||
Instant.parse("2026-10-24T09:41:30Z"));
|
||||
|
||||
assertEquals("EMR Step Status Change", event.detailType());
|
||||
JsonNode detail = fixture.objectMapper.readTree(event.detail());
|
||||
assertEquals(List.of("severity", "actionOnFailure", "stepId", "name", "clusterId", "state", "message"),
|
||||
fieldNames(detail));
|
||||
assertEquals("WARN", detail.path("severity").asText());
|
||||
assertEquals("CANCEL_PENDING", detail.path("state").asText());
|
||||
assertEquals("Step " + step.getId() + " (doomed) in Amazon EMR cluster " + cluster.getId() + " (cancelling)"
|
||||
+ " was cancelled at 2026-10-24 09:41 UTC and is pending cancellation.",
|
||||
detail.path("message").asText());
|
||||
}
|
||||
|
||||
private void assertCluster(JsonNode event, EmrCluster cluster, String state, String severity, String reason,
|
||||
String message) {
|
||||
assertEquals("EMR Cluster State Change", event.path("detail-type").asText());
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.util.function.BooleanSupplier;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.ACCOUNT;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.REGION;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.cluster;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.stepStates;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@@ -999,20 +1000,141 @@ class EmrServiceContainerStepsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void cancellingARunningStepStopsItsDriverAndStartsTheNext() {
|
||||
void aCancelledRunningStepKeepsItsPlaceWhileItsDriverStopsThenTheNextStarts() {
|
||||
EmrCluster cluster = service.runJobFlow(cluster("cancelling", true,
|
||||
sparkStep("stopped", "TERMINATE_CLUSTER"), sparkStep("next", "CONTINUE")), REGION);
|
||||
String stoppedId = cluster.getSteps().get(0).getId();
|
||||
runner.whileRunning = () -> service.cancelSteps(cluster.getId(), List.of(stoppedId));
|
||||
List<String> whileStopping = new ArrayList<>();
|
||||
List<EmrService.CancelStepInfo> again = new ArrayList<>();
|
||||
runner.whileRunning = () -> {
|
||||
service.cancelSteps(cluster.getId(), List.of(stoppedId));
|
||||
again.addAll(service.cancelSteps(cluster.getId(), List.of(stoppedId)));
|
||||
// A step added meanwhile waits too.
|
||||
service.addJobFlowSteps(cluster.getId(), List.of(sparkStep("added", "CONTINUE")));
|
||||
EmrCluster stopping = service.describeCluster(cluster.getId());
|
||||
whileStopping.add(stopping.getState());
|
||||
stopping.getSteps().forEach(step -> whileStopping.add(step.getState()));
|
||||
};
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
// The next step starts once the driver is gone, before anything reads the cluster.
|
||||
assertEquals(1, fixture.stepThreadsWaiting());
|
||||
assertEquals(Set.of(stoppedId), runner.stopped);
|
||||
// "Canceling a running step does not allow a pending step to start running" while its driver stops.
|
||||
assertEquals(List.of("RUNNING", "CANCEL_PENDING", "PENDING", "PENDING"), whileStopping);
|
||||
// "CancelSteps is idempotent".
|
||||
assertEquals(List.of(new EmrService.CancelStepInfo(stoppedId, "SUBMITTED", null)), again);
|
||||
EmrCluster running = service.describeCluster(cluster.getId());
|
||||
assertEquals("CANCELLED", running.getSteps().get(0).getState());
|
||||
// "Canceling a running step does not trigger the step ActionOnFailure."
|
||||
assertEquals("RUNNING", running.getState());
|
||||
assertEquals("RUNNING", running.getSteps().get(1).getState());
|
||||
assertEquals(List.of("CANCELLED", "RUNNING", "PENDING"), stepStates(running.getSteps()));
|
||||
assertEquals(List.of("STARTING", "PENDING stopped", "PENDING next", "RUNNING stopped", "CANCEL_PENDING stopped",
|
||||
"PENDING added", "CANCELLED stopped", "RUNNING next"), fixture.states());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepWhoseDriverEndsByItselfWhileItsCancelIsPendingIsCancelledAndTakesNoAction() {
|
||||
EmrCluster cluster = service.runJobFlow(cluster("raced", true,
|
||||
sparkStep("raced", "TERMINATE_CLUSTER"), sparkStep("next", "CONTINUE")), REGION);
|
||||
String racedId = cluster.getSteps().get(0).getId();
|
||||
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
|
||||
runner.whileRunning = () -> {
|
||||
service.cancelSteps(cluster.getId(), List.of(racedId));
|
||||
// Its driver exits before the stop reaches it.
|
||||
runner.stopped.remove(racedId);
|
||||
};
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
assertEquals(1, fixture.stepThreadsWaiting());
|
||||
EmrCluster going = service.describeCluster(cluster.getId());
|
||||
assertEquals("CANCELLED", going.getSteps().get(0).getState());
|
||||
assertNull(going.getSteps().get(0).getFailureMessage());
|
||||
assertEquals("RUNNING", going.getState());
|
||||
assertEquals("RUNNING", going.getSteps().get(1).getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepFlociStopsWhileItsCancelIsPendingIsLeftForTheRestartToCancel() {
|
||||
EmrCluster cluster = service.runJobFlow(
|
||||
cluster("torn-down", true, sparkStep("cut", "CONTINUE"), sparkStep("next", "CONTINUE")), REGION);
|
||||
String stepId = cluster.getSteps().get(0).getId();
|
||||
runner.whileRunning = () -> {
|
||||
service.cancelSteps(cluster.getId(), List.of(stepId));
|
||||
service.stopManagedContainers();
|
||||
};
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
// Cancelling it now would start the next step's driver after the teardown has passed.
|
||||
assertEquals("CANCEL_PENDING", service.describeStep(cluster.getId(), stepId).getState());
|
||||
assertEquals(0, fixture.stepThreadsWaiting());
|
||||
EmrServiceFixture restarted = fixture.restart();
|
||||
restarted.pass(Duration.ZERO);
|
||||
assertEquals("CANCELLED", restarted.service.describeStep(cluster.getId(), stepId).getState());
|
||||
assertEquals(List.of("CANCELLED cut", "RUNNING next"), restarted.states());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepWhoseCancelWasPendingIsCancelledWhenFlociComesBackInTheImmediateMode() {
|
||||
EmrCluster cluster = service.runJobFlow(
|
||||
cluster("to-immediate", true, sparkStep("cut", "CONTINUE"), sparkStep("next", "CONTINUE")), REGION);
|
||||
String stepId = cluster.getSteps().get(0).getId();
|
||||
runner.whileRunning = () -> {
|
||||
service.cancelSteps(cluster.getId(), List.of(stepId));
|
||||
service.stopManagedContainers();
|
||||
};
|
||||
fixture.runStepThread();
|
||||
|
||||
EmrServiceFixture immediate = fixture.restartImmediate();
|
||||
immediate.pass(Duration.ZERO);
|
||||
|
||||
// Nothing would end it there: it is cancelled at once, and the next step completes by time.
|
||||
assertEquals(List.of("CANCELLED cut", "RUNNING next", "COMPLETED next", "WAITING"), immediate.states());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aStepWhoseCancelIsPendingStillRunsForAFailedStepsCancelAndWait() {
|
||||
EmrCluster cluster = service.runJobFlow(cluster("lowered", true, sparkStep("failing", "CANCEL_AND_WAIT"),
|
||||
sparkStep("cancelled", "CONTINUE"), sparkStep("queued", "CONTINUE")), REGION);
|
||||
String cancelledId = cluster.getSteps().get(1).getId();
|
||||
// Both run at level 2, which is lowered to 1 while they do.
|
||||
service.modifyCluster(cluster.getId(), 2);
|
||||
runner.outcome = SparkJobRunner.Outcome.exitedWith(1);
|
||||
runner.whileRunning = () -> {
|
||||
service.modifyCluster(cluster.getId(), 1);
|
||||
service.cancelSteps(cluster.getId(), List.of(cancelledId));
|
||||
};
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
// "If a cluster has step concurrency level 1 but has multiple running steps, ... CANCEL_AND_WAIT
|
||||
// ActionOnFailure will not" activate.
|
||||
assertEquals(List.of("FAILED", "CANCEL_PENDING", "PENDING"),
|
||||
stepStates(service.describeCluster(cluster.getId()).getSteps()));
|
||||
runner.whileRunning = () -> { };
|
||||
fixture.runStepThread();
|
||||
assertEquals(List.of("FAILED", "CANCELLED", "RUNNING"),
|
||||
stepStates(service.describeCluster(cluster.getId()).getSteps()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminatingAClusterWhileAStepsCancelIsPendingCancelsTheStepOnce() {
|
||||
EmrCluster cluster = service.runJobFlow(cluster("ended", true, sparkStep("cut", "CONTINUE")), REGION);
|
||||
String stepId = cluster.getSteps().get(0).getId();
|
||||
runner.whileRunning = () -> {
|
||||
service.cancelSteps(cluster.getId(), List.of(stepId));
|
||||
service.terminateJobFlows(List.of(cluster.getId()));
|
||||
};
|
||||
|
||||
fixture.runStepThread();
|
||||
|
||||
EmrCluster ended = service.describeCluster(cluster.getId());
|
||||
assertEquals("TERMINATED", ended.getState());
|
||||
assertEquals("CANCELLED", ended.getSteps().get(0).getState());
|
||||
assertEquals(List.of("STARTING", "PENDING cut", "RUNNING cut", "CANCEL_PENDING cut", "CANCELLED cut",
|
||||
"TERMINATED"), fixture.states());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -248,6 +248,11 @@ final class EmrServiceFixture {
|
||||
return new EmrServiceFixture(storage, clock, runner, bootstrap, clusterStorage, releaseImages, iam, durations);
|
||||
}
|
||||
|
||||
/** A new service on the same storage and clock, as {@link #restart} builds, in the {@code immediate} mode. */
|
||||
EmrServiceFixture restartImmediate() {
|
||||
return new EmrServiceFixture(storage, clock, null, null, null, null, iam, durations);
|
||||
}
|
||||
|
||||
/**
|
||||
* A new service on the same storage and clock, as {@link #restart} builds, but with {@code runner},
|
||||
* its wakeups set on {@code wakeups}, and leftover drivers removed when it starts.
|
||||
@@ -334,6 +339,11 @@ final class EmrServiceFixture {
|
||||
}).toList();
|
||||
}
|
||||
|
||||
/** Each step's state, in order. */
|
||||
static List<String> stepStates(List<EmrStep> steps) {
|
||||
return steps.stream().map(EmrStep::getState).toList();
|
||||
}
|
||||
|
||||
static EmrCluster cluster(String name, boolean keepAlive, EmrStep... steps) {
|
||||
EmrCluster cluster = new EmrCluster();
|
||||
cluster.setName(name);
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.REGION;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.cluster;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.step;
|
||||
import static io.floci.aws.services.emr.EmrServiceFixture.stepStates;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@@ -123,10 +124,10 @@ class EmrServiceTest {
|
||||
EmrStep b = cluster.getSteps().get(1);
|
||||
|
||||
assertEquals("RUNNING", service.describeCluster(id).getState());
|
||||
assertEquals(List.of("PENDING", "RUNNING"), states(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of("PENDING", "RUNNING"), stepStates(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of(b.getId()), ids(service.listSteps(id, List.of("PENDING"), null)));
|
||||
fixture.clock.advance(Duration.ofSeconds(60));
|
||||
assertEquals(List.of("RUNNING", "COMPLETED"), states(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of("RUNNING", "COMPLETED"), stepStates(service.listSteps(id, null, null)));
|
||||
assertEquals(a.getStartDateTime().plusSeconds(60), a.getEndDateTime());
|
||||
assertEquals(a.getEndDateTime(), b.getStartDateTime());
|
||||
fixture.clock.advance(Duration.ofSeconds(60));
|
||||
@@ -146,15 +147,15 @@ class EmrServiceTest {
|
||||
parallel.setStepConcurrencyLevel(2);
|
||||
String id = service.runJobFlow(parallel, REGION).getId();
|
||||
|
||||
assertEquals(List.of("PENDING", "RUNNING", "RUNNING"), states(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of("PENDING", "RUNNING", "RUNNING"), stepStates(service.listSteps(id, null, null)));
|
||||
fixture.clock.advance(Duration.ofSeconds(10));
|
||||
service.modifyCluster(id, 3);
|
||||
assertEquals(List.of("RUNNING", "RUNNING", "RUNNING"), states(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of("RUNNING", "RUNNING", "RUNNING"), stepStates(service.listSteps(id, null, null)));
|
||||
// Lowering the level lets the steps already running complete.
|
||||
service.modifyCluster(id, 1);
|
||||
fixture.clock.advance(Duration.ofSeconds(60));
|
||||
|
||||
assertEquals(List.of("COMPLETED", "COMPLETED", "COMPLETED"), states(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of("COMPLETED", "COMPLETED", "COMPLETED"), stepStates(service.listSteps(id, null, null)));
|
||||
assertEquals("WAITING", service.describeCluster(id).getState());
|
||||
}
|
||||
|
||||
@@ -226,7 +227,7 @@ class EmrServiceTest {
|
||||
List<EmrService.CancelStepInfo> infos = service.cancelSteps(cluster.getId(), List.of(b.getId()));
|
||||
|
||||
assertEquals(List.of(new EmrService.CancelStepInfo(b.getId(), "SUBMITTED", null)), infos);
|
||||
assertEquals(List.of("CANCELLED", "RUNNING"), states(service.listSteps(cluster.getId(), null, null)));
|
||||
assertEquals(List.of("CANCELLED", "RUNNING"), stepStates(service.listSteps(cluster.getId(), null, null)));
|
||||
assertEquals("CANCELLED b", fixture.states().get(fixture.states().size() - 1));
|
||||
}
|
||||
|
||||
@@ -241,7 +242,7 @@ class EmrServiceTest {
|
||||
List<EmrService.CancelStepInfo> infos = service.cancelSteps(cluster.getId(), List.of(a.getId()));
|
||||
|
||||
assertEquals(List.of(new EmrService.CancelStepInfo(a.getId(), "SUBMITTED", null)), infos);
|
||||
assertEquals(List.of("RUNNING", "CANCELLED"), states(service.listSteps(cluster.getId(), null, null)));
|
||||
assertEquals(List.of("RUNNING", "CANCELLED"), stepStates(service.listSteps(cluster.getId(), null, null)));
|
||||
assertEquals(a.getEndDateTime(), cluster.getSteps().get(1).getStartDateTime());
|
||||
assertEquals(List.of("CANCELLED a", "RUNNING b"),
|
||||
fixture.states().subList(fixture.states().size() - 2, fixture.states().size()));
|
||||
@@ -284,7 +285,7 @@ class EmrServiceTest {
|
||||
assertEquals("TERMINATING", terminating.getState());
|
||||
assertEquals("USER_REQUEST", terminating.getStateChangeReasonCode());
|
||||
assertNull(terminating.getEndDateTime());
|
||||
assertEquals(List.of("CANCELLED", "CANCELLED"), states(service.listSteps(id, null, null)));
|
||||
assertEquals(List.of("CANCELLED", "CANCELLED"), stepStates(service.listSteps(id, null, null)));
|
||||
assertEquals("RUNNING", service.listInstanceGroups(id).get(0).getState());
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> service.addJobFlowSteps(id, List.of(step("late"))));
|
||||
@@ -506,7 +507,7 @@ class EmrServiceTest {
|
||||
|
||||
assertEquals("TERMINATED", service.describeCluster(id).getState());
|
||||
assertEquals(List.of("RUNNING", "RUNNING", "RUNNING"), clusters.stream().map(EmrCluster::getState).toList());
|
||||
assertEquals(List.of("RUNNING", "RUNNING"), states(steps));
|
||||
assertEquals(List.of("RUNNING", "RUNNING"), stepStates(steps));
|
||||
assertNull(steps.get(0).getEndDateTime());
|
||||
assertEquals(2, group.getRunningInstanceCount());
|
||||
assertEquals(2, fleet.getProvisionedOnDemandCapacity());
|
||||
@@ -583,9 +584,6 @@ class EmrServiceTest {
|
||||
}
|
||||
|
||||
/** ListSteps' states, newest step first, as it lists them. */
|
||||
private static List<String> states(List<EmrStep> steps) {
|
||||
return steps.stream().map(EmrStep::getState).toList();
|
||||
}
|
||||
|
||||
private static List<String> ids(List<EmrStep> steps) {
|
||||
return steps.stream().map(EmrStep::getId).toList();
|
||||
|
||||
Reference in new issue
Block a user