mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11:48 -05:00
feat(emr-serverless): start, get, list and cancel job runs (#6202)
Adds StartJobRun, GetJobRun, ListJobRuns and CancelJobRun under
/applications/{applicationId}/jobruns. Job runs are metadata only, like
Glue's: a run is RUNNING from submission and settles when read. With the
new floci.services.emrserverless.job-run-duration-seconds at its default
of 0, a BATCH run is SUCCESS as soon as it starts; a positive duration
keeps it RUNNING that long, and a run that would outlast its
executionTimeoutMinutes (720 when not given) is CANCELLED at the
timeout. A STREAMING run stays RUNNING until it is cancelled.
A run on a CREATED or STOPPED application starts it, as auto-start
defaults to on; with auto-start off the run is refused. clientToken is
the idempotency token: a retry returns the original run, and the same
token with another request answers ConflictException "Request did not
match the original". CancelJobRun on a finished run answers
ValidationException "Job run is not in a cancellable state". ListJobRuns
returns the newest runs first, filtered by states, creation time and
mode, and its nextToken resumes only the listing it came from. A run
needs a jobDriver that sets exactly one of sparkSubmit and hive, as
GetJobRun's JobRun always carries one, and tags are checked before
auto-start, so a refused run leaves the application as it was.
createdBy is the caller's ARN.
Application createdAt and updatedAt now go out as epoch seconds, the
restJson1 default for the model's Date shape, as job runs do. They were
epoch milliseconds, which the Java SDK reads as the year 58774 and
botocore refuses as out of range. Auto-start rewrites updatedAt, so a
run's own submission reached the bug. Applications persisted with
millisecond timestamps read back at the same instant.
Part of #6046 (part 8).
This commit is contained in:
1 parent
9e66185c9d
commit
1f1e5db1bc
25 files changed
+1918
-37
No files matched your search
+149
@@ -0,0 +1,149 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.emrserverless.EmrServerlessClient;
|
||||
import software.amazon.awssdk.services.emrserverless.model.Application;
|
||||
import software.amazon.awssdk.services.emrserverless.model.ApplicationState;
|
||||
import software.amazon.awssdk.services.emrserverless.model.ConflictException;
|
||||
import software.amazon.awssdk.services.emrserverless.model.JobRun;
|
||||
import software.amazon.awssdk.services.emrserverless.model.JobRunMode;
|
||||
import software.amazon.awssdk.services.emrserverless.model.JobRunState;
|
||||
import software.amazon.awssdk.services.emrserverless.model.JobRunSummary;
|
||||
import software.amazon.awssdk.services.emrserverless.model.StartJobRunResponse;
|
||||
import software.amazon.awssdk.services.emrserverless.model.ValidationException;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("EMR Serverless job runs")
|
||||
class EmrServerlessJobRunTest {
|
||||
|
||||
private static final String ROLE = "arn:aws:iam::000000000000:role/emr-serverless-job";
|
||||
|
||||
private static EmrServerlessClient emr;
|
||||
private static String applicationId;
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
emr = TestFixtures.emrServerlessClient();
|
||||
applicationId = emr.createApplication(r -> r
|
||||
.name(TestFixtures.uniqueName("jobs-app"))
|
||||
.releaseLabel("emr-7.5.0")
|
||||
.type("SPARK")
|
||||
.clientToken(UUID.randomUUID().toString())).applicationId();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (emr == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
emr.stopApplication(r -> r.applicationId(applicationId));
|
||||
emr.deleteApplication(r -> r.applicationId(applicationId));
|
||||
}
|
||||
catch (Exception ignored) {
|
||||
// Cleanup is best effort: the application is unique to this run and the emulator is disposable.
|
||||
}
|
||||
emr.close();
|
||||
}
|
||||
|
||||
private static StartJobRunResponse startSparkRun(JobRunMode mode) {
|
||||
return emr.startJobRun(r -> r
|
||||
.applicationId(applicationId)
|
||||
.executionRoleArn(ROLE)
|
||||
.name("nightly")
|
||||
.mode(mode)
|
||||
.tags(Map.of("team", "data"))
|
||||
.jobDriver(d -> d.sparkSubmit(s -> s
|
||||
.entryPoint("s3://bucket/jobs/app.jar")
|
||||
.entryPointArguments("s3://bucket/in")
|
||||
.sparkSubmitParameters("--class example.Main"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("StartJobRun auto-starts the application and GetJobRun reports a finished run")
|
||||
void startAndGet() {
|
||||
Instant before = Instant.now().minus(1, ChronoUnit.DAYS);
|
||||
StartJobRunResponse started = startSparkRun(JobRunMode.BATCH);
|
||||
Instant after = Instant.now().plus(1, ChronoUnit.DAYS);
|
||||
|
||||
assertThat(started.applicationId()).isEqualTo(applicationId);
|
||||
assertThat(started.arn()).endsWith("/applications/" + applicationId + "/jobruns/" + started.jobRunId());
|
||||
Application application = emr.getApplication(r -> r.applicationId(applicationId)).application();
|
||||
assertThat(application.state()).isEqualTo(ApplicationState.STARTED);
|
||||
// Epoch seconds on the wire; milliseconds would read as a year past 50,000.
|
||||
assertThat(application.createdAt()).isBetween(before, after);
|
||||
assertThat(application.updatedAt()).isBetween(application.createdAt(), after);
|
||||
|
||||
JobRun run = emr.getJobRun(r -> r.applicationId(applicationId).jobRunId(started.jobRunId())).jobRun();
|
||||
assertThat(run.state()).isEqualTo(JobRunState.SUCCESS);
|
||||
assertThat(run.executionRole()).isEqualTo(ROLE);
|
||||
assertThat(run.releaseLabel()).isEqualTo("emr-7.5.0");
|
||||
assertThat(run.createdBy()).startsWith("arn:aws:");
|
||||
assertThat(run.createdAt()).isBetween(before, after);
|
||||
assertThat(run.jobDriver().sparkSubmit().entryPoint()).isEqualTo("s3://bucket/jobs/app.jar");
|
||||
assertThat(run.tags()).containsEntry("team", "data");
|
||||
assertThat(run.executionTimeoutMinutes()).isEqualTo(720L);
|
||||
assertThat(run.mode()).isEqualTo(JobRunMode.BATCH);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("A repeated client token returns the original run; reused with another request it conflicts")
|
||||
void clientTokenIdempotency() {
|
||||
String token = UUID.randomUUID().toString();
|
||||
String first = emr.startJobRun(r -> r.applicationId(applicationId).clientToken(token)
|
||||
.executionRoleArn(ROLE).jobDriver(d -> d.sparkSubmit(s -> s.entryPoint("s3://bucket/a.jar"))))
|
||||
.jobRunId();
|
||||
String again = emr.startJobRun(r -> r.applicationId(applicationId).clientToken(token)
|
||||
.executionRoleArn(ROLE).jobDriver(d -> d.sparkSubmit(s -> s.entryPoint("s3://bucket/a.jar"))))
|
||||
.jobRunId();
|
||||
assertThat(again).isEqualTo(first);
|
||||
|
||||
assertThatThrownBy(() -> emr.startJobRun(r -> r.applicationId(applicationId).clientToken(token)
|
||||
.executionRoleArn(ROLE).jobDriver(d -> d.sparkSubmit(s -> s.entryPoint("s3://bucket/b.jar")))))
|
||||
.isInstanceOf(ConflictException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CancelJobRun cancels a streaming run and refuses a finished one")
|
||||
void cancel() {
|
||||
String streaming = startSparkRun(JobRunMode.STREAMING).jobRunId();
|
||||
assertThat(emr.getJobRun(r -> r.applicationId(applicationId).jobRunId(streaming)).jobRun().state())
|
||||
.isEqualTo(JobRunState.RUNNING);
|
||||
|
||||
emr.cancelJobRun(r -> r.applicationId(applicationId).jobRunId(streaming));
|
||||
assertThat(emr.getJobRun(r -> r.applicationId(applicationId).jobRunId(streaming)).jobRun().state())
|
||||
.isEqualTo(JobRunState.CANCELLED);
|
||||
|
||||
assertThatThrownBy(() -> emr.cancelJobRun(r -> r.applicationId(applicationId).jobRunId(streaming)))
|
||||
.isInstanceOf(ValidationException.class)
|
||||
.hasMessageContaining("Job run is not in a cancellable state");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ListJobRuns returns summaries filtered by state")
|
||||
void list() {
|
||||
String streaming = startSparkRun(JobRunMode.STREAMING).jobRunId();
|
||||
try {
|
||||
List<JobRunSummary> running = emr.listJobRuns(r -> r.applicationId(applicationId)
|
||||
.states(JobRunState.RUNNING)).jobRuns();
|
||||
assertThat(running).extracting(JobRunSummary::id).contains(streaming);
|
||||
assertThat(running).allSatisfy(summary -> assertThat(summary.state()).isEqualTo(JobRunState.RUNNING));
|
||||
assertThat(emr.listJobRunsPaginator(r -> r.applicationId(applicationId)).jobRuns())
|
||||
.extracting(JobRunSummary::applicationId).containsOnly(applicationId);
|
||||
}
|
||||
finally {
|
||||
emr.cancelJobRun(r -> r.applicationId(applicationId).jobRunId(streaming));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user