diff --git a/docs/services/emr-serverless.md b/docs/services/emr-serverless.md index 4834b17b1d..9655c29bd2 100644 --- a/docs/services/emr-serverless.md +++ b/docs/services/emr-serverless.md @@ -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. diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessController.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessController.java index 5baeb3b0f8..1c39368544 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessController.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessController.java @@ -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; } diff --git a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java index ca72546768..2c74b16a48 100644 --- a/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java +++ b/src/main/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunService.java @@ -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. diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunIntegrationTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunIntegrationTest.java index 10c4d7d43c..e38d0397a9 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunIntegrationTest.java @@ -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(""); diff --git a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java index 86c053a6f4..572bb46c3e 100644 --- a/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java +++ b/src/test/java/io/floci/aws/services/emrserverless/EmrServerlessJobRunServiceTest.java @@ -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); diff --git a/src/test/java/io/floci/aws/services/stepfunctions/AslExecutorEmrServerlessSyncTest.java b/src/test/java/io/floci/aws/services/stepfunctions/AslExecutorEmrServerlessSyncTest.java index fa0692617a..383cffaf79 100644 --- a/src/test/java/io/floci/aws/services/stepfunctions/AslExecutorEmrServerlessSyncTest.java +++ b/src/test/java/io/floci/aws/services/stepfunctions/AslExecutorEmrServerlessSyncTest.java @@ -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()); }