fix(emr-serverless): keep an application with runs going from stopping (#8038)

StopApplication stopped an application whose runs had not finished,
where the API reference says "All scheduled and running jobs must be
completed or cancelled before stopping an application."

A run that has not finished, a STREAMING or CANCELLING one included, now
refuses the stop with ValidationException, the one error the model
declares for it, carrying that sentence, as AWS's own message is not
recorded. The check holds the job runs' lock, as StartJobRun's admission
does, so no run starts between it and the stop. Step Functions'
stopApplication goes the same way.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-11 14:05:46 -06:00
1 parent 9e6f62e959
commit a0741e5011
6 files changed
+66 -4

No files matched your search

+5 -1
View File
@@ -83,6 +83,11 @@ runner mode a Spark run executes; see [Running Spark job runs](#running-spark-jo
the 7.11.0 release notes say; a run outside that is refused with `ValidationException`.
* **CancelJobRun** moves a run that has not finished to `CANCELLED`. A finished run answers
`ValidationException` "Job run is not in a cancellable state".
* **StopApplication** refuses an application with a run that has not finished, a `STREAMING` run or
a `CANCELLING` one included, as the API reference says "All scheduled and running jobs must be
completed or cancelled before stopping an application." It answers `ValidationException`, the one
error the model declares for this, with that sentence as its message: AWS's own message is not
recorded, so the wording is Floci's reading.
* **ListJobRuns** returns the newest runs first, filtered by `states`, `createdAtAfter`,
`createdAtBefore` and `mode`, at most 50 per page. With several `states`, the runs are grouped by
state in the order the filter names them. A state or mode that does not exist, or more than 8
@@ -241,7 +246,6 @@ application's `imageConfiguration`; a `TezTask` image is not used, as Tez's task
directory; Spark alone unpacks the others, into its own files folder. Floci reads an archive itself,
whatever the execution role may read, though Spark's own read of it uses the role's credentials.
* **Custom images**: Floci does not check an `imageUri` against the model's pattern, which would refuse the repository URIs Floci's own ECR hands out, nor that the image was built from its release's base image (AWS refuses one that was not from `emr-7.14.0` and `emr-spark-8.1.0` on), nor the image's size. An image that cannot be pulled fails the run, not the request that names it. Every run resolves its image again, as releases from 7.14 do and earlier ones with `applicationLevelDigestResolution` false: Floci keeps no digest from the application's start, and reports no `resolvedImageDigest`, on the application or on a job run.
* **StopApplication with running jobs**: AWS requires running jobs to finish or be cancelled before an application stops. Floci does not enforce it; with the default duration of `0` no `BATCH` run is ever running.
* **Not emulated**: the "EMR Serverless Job Run Retry" event, as Floci sends no EMR Serverless events, queuing (`QUEUED`), `totalResourceUtilization` and `billedResourceUtilization`, and `shutdownGracePeriodInSeconds` (accepted and ignored: a cancelled run's driver is stopped at once).
* **Instant Start/Stop**: Floci marks the application `STARTED` or `STOPPED` immediately without provisioning actual compute capacity in the background.
* **Data Plane**: Floci does not implement the data-plane API, such as interactive sessions.
@@ -123,7 +123,7 @@ public class EmrServerlessController {
@POST
@Path("/{applicationId}/stop")
public StopApplicationResponse stopApplication(@PathParam("applicationId") String applicationId) {
service.stopApplication(applicationId);
jobRuns.stopApplication(applicationId);
StopApplicationResponse response = new StopApplicationResponse();
return response;
}
@@ -369,6 +369,25 @@ public class EmrServerlessJobRunService implements Resettable {
page.nextToken());
}
/**
* Stops the application once none of its runs is still going, as StopApplication's reference has it: "All
* scheduled and running jobs must be completed or cancelled before stopping an application." AWS's answer
* otherwise is not recorded, so the refusal is the one error the model declares for it, carrying that
* sentence. Under this service's lock, as StartJobRun's admission is, so no run starts between the two.
*/
public synchronized void stopApplication(String applicationId) {
applications.getApplication(applicationId);
for (JobRunRecord record : store.scan(key -> true)) {
JobRun run = record.getJobRun();
if (applicationId.equals(run.getApplicationId()) && inRequestRegion(run)
&& !isTerminal(settle(record).getJobRun())) {
throw new AwsException("ValidationException", "All scheduled and running jobs must be completed "
+ "or cancelled before stopping an application.", 400);
}
}
applications.stopApplication(applicationId);
}
/**
* Cancels a run that has not finished. A finished run is refused, with AWS's wording. A container
* run is CANCELLING until its driver has stopped.
@@ -362,6 +362,27 @@ class EmrServerlessJobRunIntegrationTest {
.body("jobRun.endedAt", instanceOf(Number.class));
}
@Test
void anApplicationWithARunStillGoingIsNotStopped() {
String applicationId = createApplication("");
String application = "/applications/" + applicationId;
startedRunId(applicationId, sparkRun(UUID.randomUUID().toString(), ""));
String streaming = startedRunId(applicationId,
sparkRun(UUID.randomUUID().toString(), ", \"mode\": \"STREAMING\""));
givenReq().when().post(application + "/stop").then().statusCode(400)
.body("__type", equalTo("ValidationException"))
.body("message", equalTo("All scheduled and running jobs must be completed or cancelled "
+ "before stopping an application."));
givenReq().when().get(application).then().statusCode(200)
.body("application.state", equalTo("STARTED"));
givenReq().when().delete(application + "/jobruns/" + streaming).then().statusCode(200);
givenReq().when().post(application + "/stop").then().statusCode(200);
givenReq().when().get(application).then().statusCode(200)
.body("application.state", equalTo("STOPPED"));
}
@Test
void listJobRunsReturnsTheNewestFirst() {
String applicationId = createApplication("");
@@ -137,6 +137,24 @@ class EmrServerlessJobRunServiceTest {
assertEquals("CANCELLED", service.getJobRun(applicationId, jobRunId, null).getState());
}
@Test
void anApplicationStopsOnlyOnceNoneOfItsRunsIsStillGoing() {
EmrServerlessJobRunService service = service(30);
String other = application("other", "emr-7.5.0", null);
service.startJobRun(applicationId, request(null, null));
service.startJobRun(other, request("STREAMING", null));
AwsException running = assertThrows(AwsException.class, () -> service.stopApplication(applicationId));
clock.advance(Duration.ofSeconds(30));
service.stopApplication(applicationId);
assertEquals("ValidationException", running.getErrorCode());
assertEquals("All scheduled and running jobs must be completed or cancelled before stopping an application.",
running.getMessage());
assertEquals("STOPPED", applications.getApplication(applicationId).getState());
assertEquals("STARTED", applications.getApplication(other).getState());
}
@Test
void runIsCancelledAtItsExecutionTimeoutWhenItWouldRunLonger() {
EmrServerlessJobRunService service = service(600);
@@ -223,7 +223,7 @@ class AslExecutorEmrServerlessSyncTest {
run(task("startApplication.sync"), APPLICATION);
verify(applications).startApplication(APPLICATION_ID);
verify(applications).stopApplication(APPLICATION_ID);
verify(jobRuns).stopApplication(APPLICATION_ID);
assertEquals(List.of(), history.stream().map(HistoryEvent::getType)
.filter(type -> type.equals("TaskFailed") || type.equals("TaskSucceeded")).toList());
}
@@ -235,7 +235,7 @@ class AslExecutorEmrServerlessSyncTest {
Execution execution = run(timingOut("stopApplication.sync"), APPLICATION);
assertEquals("States.Timeout", execution.getError());
verify(applications).stopApplication(APPLICATION_ID);
verify(jobRuns).stopApplication(APPLICATION_ID);
verify(applications, never()).startApplication(anyString());
verify(applications, never()).deleteApplication(anyString());
}