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:
Priamo Germosen authored and GitHub committed 2026-11-14 15:18:49 -04:00
1 parent bbcc58f03b
commit 7981cadc7c
8 files changed
+303 -76

No files matched your search

@@ -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();