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

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