diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueJobRunTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueJobRunTest.java new file mode 100644 index 0000000000..8276f28679 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/GlueJobRunTest.java @@ -0,0 +1,108 @@ +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.glue.GlueClient; +import software.amazon.awssdk.services.glue.model.BatchGetJobsResponse; +import software.amazon.awssdk.services.glue.model.BatchStopJobRunResponse; +import software.amazon.awssdk.services.glue.model.EntityNotFoundException; +import software.amazon.awssdk.services.glue.model.JobRun; +import software.amazon.awssdk.services.glue.model.JobRunState; +import software.amazon.awssdk.services.glue.model.WorkerType; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@DisplayName("Glue job runs") +class GlueJobRunTest { + + private static final String JOB_NAME = TestFixtures.uniqueName("run_job"); + private static final String ROLE = "arn:aws:iam::000000000000:role/glue"; + + private static GlueClient glue; + + @BeforeAll + static void setup() { + glue = TestFixtures.glueClient(); + glue.createJob(r -> r + .name(JOB_NAME) + .role(ROLE) + .command(c -> c.name("glueetl").scriptLocation("s3://scripts/etl.py")) + .workerType(WorkerType.G_1_X) + .numberOfWorkers(2) + .tags(Map.of("team", "data"))); + } + + @AfterAll + static void cleanup() { + if (glue == null) { + return; + } + try { + glue.deleteJob(r -> r.jobName(JOB_NAME)); + } + catch (Exception ignored) { + // Cleanup is best effort: the job name is unique to this run and the emulator is disposable. + } + glue.close(); + } + + @Test + @DisplayName("StartJobRun, GetJobRun and GetJobRuns parse with the SDK") + void startAndReadARun() { + String runId = glue.startJobRun(r -> r + .jobName(JOB_NAME) + .arguments(Map.of("--day", "2026-09-25")) + .numberOfWorkers(4)).jobRunId(); + + assertThat(runId).matches("jr_[0-9a-f]{64}"); + + JobRun run = glue.getJobRun(r -> r.jobName(JOB_NAME).runId(runId)).jobRun(); + assertThat(run.id()).isEqualTo(runId); + assertThat(run.jobName()).isEqualTo(JOB_NAME); + assertThat(run.jobRunState()).isEqualTo(JobRunState.SUCCEEDED); + assertThat(run.arguments()).containsEntry("--day", "2026-09-25"); + assertThat(run.workerType()).isEqualTo(WorkerType.G_1_X); + assertThat(run.numberOfWorkers()).isEqualTo(4); + assertThat(run.startedOn()).isNotNull(); + assertThat(run.completedOn()).isNotNull(); + + assertThat(glue.getJobRuns(r -> r.jobName(JOB_NAME)).jobRuns()) + .extracting(JobRun::id) + .contains(runId); + } + + @Test + @DisplayName("BatchStopJobRun reports a finished run under Errors") + void stopAFinishedRun() { + String runId = glue.startJobRun(r -> r.jobName(JOB_NAME)).jobRunId(); + + BatchStopJobRunResponse response = glue.batchStopJobRun(r -> r.jobName(JOB_NAME).jobRunIds(runId)); + + assertThat(response.successfulSubmissions()).isEmpty(); + assertThat(response.errors()).hasSize(1); + assertThat(response.errors().get(0).jobRunId()).isEqualTo(runId); + assertThat(response.errors().get(0).errorDetail().errorCode()).isEqualTo("InvalidInputException"); + } + + @Test + @DisplayName("ListJobs filters on tags and BatchGetJobs reports missing names") + void listAndBatchGetJobs() { + assertThat(glue.listJobs(r -> r.tags(Map.of("team", "data"))).jobNames()).contains(JOB_NAME); + + BatchGetJobsResponse response = glue.batchGetJobs(r -> r.jobNames(JOB_NAME, "absent_job")); + assertThat(response.jobs()).extracting(job -> job.name()).containsExactly(JOB_NAME); + assertThat(response.jobsNotFound()).containsExactly("absent_job"); + } + + @Test + @DisplayName("GetJobRun on an unknown run is EntityNotFoundException") + void unknownRun() { + assertThatThrownBy(() -> glue.getJobRun(r -> r.jobName(JOB_NAME).runId("jr_missing"))) + .isInstanceOf(EntityNotFoundException.class); + } +} diff --git a/docs/services/glue.md b/docs/services/glue.md index 4cf4b50c78..9c49f183e9 100644 --- a/docs/services/glue.md +++ b/docs/services/glue.md @@ -140,8 +140,23 @@ retroactively. | CreateJob | Creates a new job definition. | | GetJob | Retrieves an existing job definition. | | GetJobs | Retrieves all current job definitions. | +| ListJobs | Lists job names, optionally only those carrying every given tag. | +| BatchGetJobs | Retrieves several job definitions by name and reports the names not found. | | UpdateJob | Updates an existing job definition. | -| DeleteJob | Deletes a specified job definition. | +| DeleteJob | Deletes a specified job definition and its runs. | +| StartJobRun | Starts a run of a job, with per-run arguments and capacity overrides. | +| GetJobRun | Retrieves one run of a job. | +| GetJobRuns | Lists a job's runs, newest first. | +| BatchStopJobRun | Stops running runs of a job and reports the rest in `Errors`. | + +Job runs do not execute the job's script. A run follows Glue's state machine and response shape: it +takes the job's worker type, worker count, timeout and Glue version unless the request overrides them, +and `MaxConcurrentRuns` (1 when unset) rejects a start with `ConcurrentRunsExceededException` unless +`JobRunQueuingEnabled` is set. With the default `job-run-duration-seconds` of 0 a run is `SUCCEEDED` as +soon as it starts. A positive value keeps each run `RUNNING` for that many seconds, so a test can observe +`BatchStopJobRun` (the run ends `STOPPED`) and the concurrency limit; a run whose duration exceeds the job +timeout ends `TIMEOUT` when the timeout is reached. Queued runs are admitted straight away rather than +waiting in `WAITING`. #### Crawlers @@ -215,6 +230,7 @@ Supported schema formats are `AVRO`, `JSON`, and `PROTOBUF`. Compatibility modes | Variable | Default | Description | |---|---|---| | `FLOCI_SERVICES_GLUE_ENABLED` | `true` | Enable or disable the service | +| `FLOCI_SERVICES_GLUE_JOB_RUN_DURATION_SECONDS` | `0` | Seconds a job run stays `RUNNING` before it succeeds; `0` finishes it as soon as it starts | ## Integration with Athena diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 0856df3f9f..d2842ea2d7 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -1957,6 +1957,10 @@ public interface EmulatorConfig { interface GlueServiceConfig { @WithDefault("true") boolean enabled(); + + /** How long a job run stays RUNNING before it succeeds; 0 = a run succeeds as soon as it starts. */ + @WithDefault("0") + int jobRunDurationSeconds(); } interface SesServiceConfig { diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJobRunService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJobRunService.java new file mode 100644 index 0000000000..f08df58860 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJobRunService.java @@ -0,0 +1,264 @@ +package io.github.hectorvent.floci.services.glue; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.glue.model.ExecutionProperty; +import io.github.hectorvent.floci.services.glue.model.Job; +import io.github.hectorvent.floci.services.glue.model.JobRun; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; + +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +/** + * Glue job runs. No script executes: a run follows Glue's state machine and its response shape, + * and settles lazily when read. With the default {@code job-run-duration-seconds} of 0 a run is + * SUCCEEDED as soon as it starts; a positive duration keeps it RUNNING until that much time has + * passed, which is what makes BatchStopJobRun and MaxConcurrentRuns observable. + * + *

Every public method is synchronized: the store has no atomic compare and set, so admitting a + * run (count the live runs, check the limit, write the run), stopping one, settling one and + * removing a deleted job's runs must not interleave. + */ +@ApplicationScoped +public class GlueJobRunService { + + private static final Logger LOG = Logger.getLogger(GlueJobRunService.class); + + static final String STATE_RUNNING = "RUNNING"; + static final String STATE_SUCCEEDED = "SUCCEEDED"; + static final String STATE_STOPPED = "STOPPED"; + static final String STATE_TIMEOUT = "TIMEOUT"; + + private static final Set TERMINAL_STATES = + Set.of(STATE_SUCCEEDED, STATE_STOPPED, "FAILED", STATE_TIMEOUT, "ERROR", "EXPIRED"); + + // ExecutionProperty.MaxConcurrentRuns defaults to 1 when a job does not set it. + private static final int DEFAULT_MAX_CONCURRENT_RUNS = 1; + private static final int MAX_JOB_RUNS_PAGE_SIZE = 200; + private static final String LOG_GROUP_NAME = "/aws-glue/jobs"; + + private final StorageBackend runStore; + private final GlueService glueService; + private final int runDurationSeconds; + private final Clock clock; + + @Inject + public GlueJobRunService(StorageFactory storageFactory, GlueService glueService, EmulatorConfig config) { + this(storageFactory.create("glue", "job_runs.json", new TypeReference<>() {}), + glueService, config.services().glue().jobRunDurationSeconds(), Clock.systemUTC()); + } + + GlueJobRunService(StorageBackend runStore, GlueService glueService, + int runDurationSeconds, Clock clock) { + if (runDurationSeconds < 0) { + throw new IllegalArgumentException( + "floci.services.glue.job-run-duration-seconds must not be negative: " + runDurationSeconds); + } + this.runStore = runStore; + this.glueService = glueService; + this.runDurationSeconds = runDurationSeconds; + this.clock = clock; + } + + /** + * Starts a run of {@code jobName}. {@code overrides} carries the per-run request fields + * (Arguments, Timeout, WorkerType, NumberOfWorkers, MaxCapacity, AllocatedCapacity, + * SecurityConfiguration, NotificationProperty, ExecutionClass, JobRunQueuingEnabled); an + * unset field falls back to the job definition. + */ + public synchronized JobRun startJobRun(String jobName, String previousRunId, JobRun overrides) { + Job job = glueService.getJob(jobName); + // The job's own Timeout is stored as given by CreateJob/UpdateJob, so check what the run inherits. + Integer timeout = firstNonNull(overrides.getTimeout(), job.getTimeout()); + if (timeout != null && timeout < 1) { + throw new AwsException("InvalidInputException", "Timeout must be at least 1 minute.", 400); + } + if (previousRunId != null) { + findRun(jobName, previousRunId); + } + + boolean queuingEnabled = Boolean.TRUE.equals(firstNonNull( + overrides.getJobRunQueuingEnabled(), job.getJobRunQueuingEnabled())); + long active = runsOf(jobName).stream().filter(run -> !isTerminal(settle(run))).count(); + if (!queuingEnabled && active >= maxConcurrentRuns(job)) { + throw new AwsException("ConcurrentRunsExceededException", + "Concurrent runs exceeded for " + jobName, 400); + } + + Instant now = clock.instant(); + JobRun run = new JobRun(); + run.setId(newRunId()); + run.setAttempt(0); + run.setPreviousRunId(previousRunId); + run.setJobName(job.getName()); + run.setJobMode(job.getJobMode()); + run.setJobRunQueuingEnabled(queuingEnabled); + run.setStartedOn(now); + run.setLastModifiedOn(now); + run.setJobRunState(STATE_RUNNING); + run.setArguments(overrides.getArguments()); + run.setAllocatedCapacity(firstNonNull(overrides.getAllocatedCapacity(), job.getAllocatedCapacity())); + run.setTimeout(timeout); + run.setMaxCapacity(firstNonNull(overrides.getMaxCapacity(), job.getMaxCapacity())); + run.setWorkerType(firstNonNull(overrides.getWorkerType(), job.getWorkerType())); + run.setNumberOfWorkers(firstNonNull(overrides.getNumberOfWorkers(), job.getNumberOfWorkers())); + run.setSecurityConfiguration( + firstNonNull(overrides.getSecurityConfiguration(), job.getSecurityConfiguration())); + run.setNotificationProperty( + firstNonNull(overrides.getNotificationProperty(), job.getNotificationProperty())); + run.setExecutionClass(firstNonNull(overrides.getExecutionClass(), job.getExecutionClass())); + run.setGlueVersion(job.getGlueVersion()); + run.setLogGroupName(LOG_GROUP_NAME); + run.setExecutionTime(0); + runStore.put(run.getId(), run); + LOG.infov("Started Glue job run {0} for job {1}", run.getId(), jobName); + return settle(run); + } + + public synchronized JobRun getJobRun(String jobName, String runId) { + glueService.getJob(jobName); + if (runId == null) { + throw new AwsException("InvalidInputException", "RunId is required.", 400); + } + return settle(findRun(jobName, runId)); + } + + public synchronized GlueService.Page getJobRuns(String jobName, Integer maxResults, + String nextToken) { + glueService.getJob(jobName); + if (maxResults != null && (maxResults < 1 || maxResults > MAX_JOB_RUNS_PAGE_SIZE)) { + throw new AwsException("InvalidInputException", + "MaxResults must be between 1 and " + MAX_JOB_RUNS_PAGE_SIZE, 400); + } + List runs = new ArrayList<>(); + for (JobRun run : runsOf(jobName)) { + runs.add(settle(run)); + } + runs.sort(Comparator.comparing(JobRun::getStartedOn).reversed().thenComparing(JobRun::getId)); + return glueService.paginate(runs, maxResults, nextToken); + } + + public synchronized StopResult batchStopJobRun(String jobName, List runIds) { + if (jobName == null) { + throw new AwsException("InvalidInputException", "JobName is required.", 400); + } + if (runIds == null || runIds.isEmpty()) { + throw new AwsException("InvalidInputException", "JobRunIds is required.", 400); + } + List stopped = new ArrayList<>(); + List errors = new ArrayList<>(); + for (String runId : runIds) { + JobRun run = runStore.get(runId) + .filter(candidate -> jobName.equals(candidate.getJobName())) + .orElse(null); + if (run == null) { + errors.add(new StopError(jobName, runId, "EntityNotFoundException", + "Job run " + runId + " not found for job " + jobName)); + continue; + } + settle(run); + if (isTerminal(run)) { + errors.add(new StopError(jobName, runId, "InvalidInputException", + "Job run " + runId + " is not running; its state is " + run.getJobRunState())); + continue; + } + complete(run, STATE_STOPPED, clock.instant(), null); + stopped.add(new StoppedRun(jobName, runId)); + } + return new StopResult(stopped, errors); + } + + /** Runs belong to their job: deleting the job removes them, as it does on AWS. */ + public synchronized void deleteRuns(String jobName) { + for (JobRun run : runsOf(jobName)) { + runStore.delete(run.getId()); + } + } + + private JobRun settle(JobRun run) { + if (isTerminal(run)) { + return run; + } + Instant startedOn = run.getStartedOn(); + long timeoutSeconds = run.getTimeout() == null ? Long.MAX_VALUE : run.getTimeout() * 60L; + if (runDurationSeconds >= timeoutSeconds) { + Instant timedOutAt = startedOn.plusSeconds(timeoutSeconds); + if (!clock.instant().isBefore(timedOutAt)) { + complete(run, STATE_TIMEOUT, timedOutAt, + "Job run exceeded the timeout of " + run.getTimeout() + " minutes"); + } + return run; + } + Instant finishedAt = startedOn.plusSeconds(runDurationSeconds); + if (!clock.instant().isBefore(finishedAt)) { + complete(run, STATE_SUCCEEDED, finishedAt, null); + } + return run; + } + + private void complete(JobRun run, String state, Instant completedOn, String errorMessage) { + run.setJobRunState(state); + run.setCompletedOn(completedOn); + run.setLastModifiedOn(completedOn); + run.setExecutionTime((int) Duration.between(run.getStartedOn(), completedOn).toSeconds()); + run.setErrorMessage(errorMessage); + runStore.put(run.getId(), run); + } + + private JobRun findRun(String jobName, String runId) { + return runStore.get(runId) + .filter(run -> jobName.equals(run.getJobName())) + .orElseThrow(() -> new AwsException("EntityNotFoundException", + "Job run " + runId + " not found for job " + jobName, 400)); + } + + private List runsOf(String jobName) { + List runs = new ArrayList<>(); + for (JobRun run : runStore.scan(key -> true)) { + if (jobName.equals(run.getJobName())) { + runs.add(run); + } + } + return runs; + } + + private static boolean isTerminal(JobRun run) { + return TERMINAL_STATES.contains(run.getJobRunState()); + } + + private static int maxConcurrentRuns(Job job) { + ExecutionProperty property = job.getExecutionProperty(); + if (property == null || property.getMaxConcurrentRuns() == null) { + return DEFAULT_MAX_CONCURRENT_RUNS; + } + return property.getMaxConcurrentRuns(); + } + + // Glue job run ids are "jr_" followed by 64 hex characters. + private static String newRunId() { + return "jr_" + UUID.randomUUID().toString().replace("-", "") + + UUID.randomUUID().toString().replace("-", ""); + } + + private static T firstNonNull(T preferred, T fallback) { + return preferred != null ? preferred : fallback; + } + + public record StoppedRun(String jobName, String jobRunId) {} + + public record StopError(String jobName, String jobRunId, String errorCode, String errorMessage) {} + + public record StopResult(List stopped, List errors) {} +} diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java index 7adc50be74..670b124714 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJsonHandler.java @@ -31,14 +31,17 @@ public class GlueJsonHandler { private static final TypeReference> PARTITION_LIST = new TypeReference<>() {}; private final GlueService glueService; + private final GlueJobRunService jobRunService; private final GlueSchemaRegistryService schemaRegistryService; private final ObjectMapper mapper; @Inject public GlueJsonHandler(GlueService glueService, + GlueJobRunService jobRunService, GlueSchemaRegistryService schemaRegistryService, ObjectMapper mapper) { this.glueService = glueService; + this.jobRunService = jobRunService; this.schemaRegistryService = schemaRegistryService; this.mapper = mapper; } @@ -233,9 +236,43 @@ public class GlueJsonHandler { } case "DeleteJob" -> { DeleteJobRequest req = mapper.treeToValue(request, DeleteJobRequest.class); + // Job first, runs second: a StartJobRun that checked the job before it was deleted has + // stored its run by the time deleteRuns takes the run service's lock. glueService.deleteJob(req.getJobName(), region); + jobRunService.deleteRuns(req.getJobName()); yield Response.ok(new DeleteJobResponse(req.getJobName())).build(); } + case "ListJobs" -> { + Map tags = request.hasNonNull("Tags") + ? mapper.convertValue(request.get("Tags"), STRING_MAP) + : null; + GlueService.Page page = glueService.listJobs( + readMaxResults(request), readNextToken(request), tags, region); + yield Response.ok(pageResponse("JobNames", page.items(), page.nextToken())).build(); + } + case "BatchGetJobs" -> { + List names = request.hasNonNull("JobNames") + ? mapper.convertValue(request.get("JobNames"), STRING_LIST) + : null; + GlueService.BatchGetJobsResult result = glueService.batchGetJobs(names); + Map response = new LinkedHashMap<>(); + response.put("Jobs", result.jobs()); + response.put("JobsNotFound", result.jobsNotFound()); + yield Response.ok(response).build(); + } + case "StartJobRun" -> { + JobRun run = jobRunService.startJobRun(request.path("JobName").asText(null), + request.path("JobRunId").asText(null), jobRunOverrides(request)); + yield Response.ok(Map.of("JobRunId", run.getId())).build(); + } + case "GetJobRun" -> Response.ok(Map.of("JobRun", jobRunService.getJobRun( + request.path("JobName").asText(null), request.path("RunId").asText(null)))).build(); + case "GetJobRuns" -> { + GlueService.Page page = jobRunService.getJobRuns( + request.path("JobName").asText(null), readMaxResults(request), readNextToken(request)); + yield Response.ok(pageResponse("JobRuns", page.items(), page.nextToken())).build(); + } + case "BatchStopJobRun" -> handleBatchStopJobRun(request); case "CreateClassifier" -> { Classifier classifier = mapper.treeToValue(request, Classifier.class); glueService.createClassifier(classifier); @@ -971,6 +1008,72 @@ public class GlueJsonHandler { return Response.ok(Map.of()).build(); } + private JobRun jobRunOverrides(JsonNode request) throws Exception { + JobRun overrides = new JobRun(); + if (request.hasNonNull("Arguments")) { + overrides.setArguments(mapper.convertValue(request.get("Arguments"), STRING_MAP)); + } + if (request.hasNonNull("AllocatedCapacity")) { + overrides.setAllocatedCapacity(request.get("AllocatedCapacity").asInt()); + } + if (request.hasNonNull("Timeout")) { + overrides.setTimeout(request.get("Timeout").asInt()); + } + if (request.hasNonNull("MaxCapacity")) { + overrides.setMaxCapacity(request.get("MaxCapacity").asDouble()); + } + if (request.hasNonNull("WorkerType")) { + overrides.setWorkerType(request.get("WorkerType").asText()); + } + if (request.hasNonNull("NumberOfWorkers")) { + overrides.setNumberOfWorkers(request.get("NumberOfWorkers").asInt()); + } + if (request.hasNonNull("SecurityConfiguration")) { + overrides.setSecurityConfiguration(request.get("SecurityConfiguration").asText()); + } + if (request.hasNonNull("NotificationProperty")) { + overrides.setNotificationProperty( + mapper.treeToValue(request.get("NotificationProperty"), NotificationProperty.class)); + } + if (request.hasNonNull("ExecutionClass")) { + overrides.setExecutionClass(request.get("ExecutionClass").asText()); + } + if (request.hasNonNull("JobRunQueuingEnabled")) { + overrides.setJobRunQueuingEnabled(request.get("JobRunQueuingEnabled").asBoolean()); + } + return overrides; + } + + private Response handleBatchStopJobRun(JsonNode request) { + List runIds = request.hasNonNull("JobRunIds") + ? mapper.convertValue(request.get("JobRunIds"), STRING_LIST) + : null; + GlueJobRunService.StopResult result = + jobRunService.batchStopJobRun(request.path("JobName").asText(null), runIds); + List> submissions = new ArrayList<>(); + for (GlueJobRunService.StoppedRun stopped : result.stopped()) { + Map entry = new LinkedHashMap<>(); + entry.put("JobName", stopped.jobName()); + entry.put("JobRunId", stopped.jobRunId()); + submissions.add(entry); + } + List> errors = new ArrayList<>(); + for (GlueJobRunService.StopError error : result.errors()) { + Map detail = new LinkedHashMap<>(); + detail.put("ErrorCode", error.errorCode()); + detail.put("ErrorMessage", error.errorMessage()); + Map entry = new LinkedHashMap<>(); + entry.put("JobName", error.jobName()); + entry.put("JobRunId", error.jobRunId()); + entry.put("ErrorDetail", detail); + errors.add(entry); + } + Map response = new LinkedHashMap<>(); + response.put("SuccessfulSubmissions", submissions); + response.put("Errors", errors); + return Response.ok(response).build(); + } + private Response handleGetTags(JsonNode request, String region) { String arn = request.path("ResourceArn").asText(null); Map tags = glueService.getTags(arn, region); diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java index faec21a00e..14277b5803 100644 --- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java +++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java @@ -54,6 +54,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.regex.Matcher; @@ -1728,6 +1729,46 @@ public class GlueService { return paginate(all, maxResults, nextToken); } + public Page listJobs(Integer maxResults, String nextToken, Map tags, String region) { + List names = new ArrayList<>(); + for (Job job : jobStore.scan(k -> true)) { + if (tags == null || tags.isEmpty() || hasTags(jobArn(region, job.getName()), tags, region)) { + names.add(job.getName()); + } + } + names.sort(Comparator.naturalOrder()); + return paginate(names, maxResults, nextToken); + } + + public BatchGetJobsResult batchGetJobs(List names) { + if (names == null || names.isEmpty()) { + throw new AwsException("InvalidInputException", "JobNames is required.", 400); + } + List jobs = new ArrayList<>(); + List notFound = new ArrayList<>(); + for (String name : names) { + Optional job = name == null ? Optional.empty() : jobStore.get(name); + if (job.isPresent()) { + jobs.add(job.get()); + } else { + notFound.add(name); + } + } + return new BatchGetJobsResult(jobs, notFound); + } + + public record BatchGetJobsResult(List jobs, List jobsNotFound) {} + + private boolean hasTags(String arn, Map wanted, String region) { + Map actual = resourceGroupsTaggingService.getTagsForResource(region, arn); + for (Map.Entry tag : wanted.entrySet()) { + if (!Objects.equals(actual.get(tag.getKey()), tag.getValue())) { + return false; + } + } + return true; + } + public void updateJob(String name, JobUpdate update) { validateRequired(name, "JobName"); validateRequired(update, "JobUpdate"); @@ -2476,7 +2517,7 @@ public class GlueService { public record Page(List items, String nextToken) {} - private Page paginate(List all, Integer maxResults, String nextToken) { + Page paginate(List all, Integer maxResults, String nextToken) { if (maxResults != null && (maxResults < 1 || maxResults > 1000)) { throw new AwsException("InvalidInputException", "MaxResults must be between 1 and 1000", 400); } diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRun.java b/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRun.java new file mode 100644 index 0000000000..2a8affdc25 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRun.java @@ -0,0 +1,156 @@ +package io.github.hectorvent.floci.services.glue.model; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import io.quarkus.runtime.annotations.RegisterForReflection; + +import java.time.Instant; +import java.util.Map; + +@RegisterForReflection +@JsonInclude(JsonInclude.Include.NON_NULL) +public class JobRun { + @JsonProperty("Id") + private String id; + + @JsonProperty("Attempt") + private Integer attempt; + + @JsonProperty("PreviousRunId") + private String previousRunId; + + @JsonProperty("JobName") + private String jobName; + + @JsonProperty("JobMode") + private String jobMode; + + @JsonProperty("JobRunQueuingEnabled") + private Boolean jobRunQueuingEnabled; + + @JsonProperty("StartedOn") + @JsonFormat(shape = JsonFormat.Shape.NUMBER) + private Instant startedOn; + + @JsonProperty("LastModifiedOn") + @JsonFormat(shape = JsonFormat.Shape.NUMBER) + private Instant lastModifiedOn; + + @JsonProperty("CompletedOn") + @JsonFormat(shape = JsonFormat.Shape.NUMBER) + private Instant completedOn; + + @JsonProperty("JobRunState") + private String jobRunState; + + @JsonProperty("Arguments") + private Map arguments; + + @JsonProperty("ErrorMessage") + private String errorMessage; + + @JsonProperty("AllocatedCapacity") + private Integer allocatedCapacity; + + @JsonProperty("ExecutionTime") + private Integer executionTime; + + @JsonProperty("Timeout") + private Integer timeout; + + @JsonProperty("MaxCapacity") + private Double maxCapacity; + + @JsonProperty("WorkerType") + private String workerType; + + @JsonProperty("NumberOfWorkers") + private Integer numberOfWorkers; + + @JsonProperty("SecurityConfiguration") + private String securityConfiguration; + + @JsonProperty("LogGroupName") + private String logGroupName; + + @JsonProperty("NotificationProperty") + private NotificationProperty notificationProperty; + + @JsonProperty("GlueVersion") + private String glueVersion; + + @JsonProperty("ExecutionClass") + private String executionClass; + + public JobRun() {} + + public String getId() { return id; } + public void setId(String id) { this.id = id; } + + public Integer getAttempt() { return attempt; } + public void setAttempt(Integer attempt) { this.attempt = attempt; } + + public String getPreviousRunId() { return previousRunId; } + public void setPreviousRunId(String previousRunId) { this.previousRunId = previousRunId; } + + public String getJobName() { return jobName; } + public void setJobName(String jobName) { this.jobName = jobName; } + + public String getJobMode() { return jobMode; } + public void setJobMode(String jobMode) { this.jobMode = jobMode; } + + public Boolean getJobRunQueuingEnabled() { return jobRunQueuingEnabled; } + public void setJobRunQueuingEnabled(Boolean jobRunQueuingEnabled) { this.jobRunQueuingEnabled = jobRunQueuingEnabled; } + + public Instant getStartedOn() { return startedOn; } + public void setStartedOn(Instant startedOn) { this.startedOn = startedOn; } + + public Instant getLastModifiedOn() { return lastModifiedOn; } + public void setLastModifiedOn(Instant lastModifiedOn) { this.lastModifiedOn = lastModifiedOn; } + + public Instant getCompletedOn() { return completedOn; } + public void setCompletedOn(Instant completedOn) { this.completedOn = completedOn; } + + public String getJobRunState() { return jobRunState; } + public void setJobRunState(String jobRunState) { this.jobRunState = jobRunState; } + + public Map getArguments() { return arguments; } + public void setArguments(Map arguments) { this.arguments = arguments; } + + public String getErrorMessage() { return errorMessage; } + public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; } + + public Integer getAllocatedCapacity() { return allocatedCapacity; } + public void setAllocatedCapacity(Integer allocatedCapacity) { this.allocatedCapacity = allocatedCapacity; } + + public Integer getExecutionTime() { return executionTime; } + public void setExecutionTime(Integer executionTime) { this.executionTime = executionTime; } + + public Integer getTimeout() { return timeout; } + public void setTimeout(Integer timeout) { this.timeout = timeout; } + + public Double getMaxCapacity() { return maxCapacity; } + public void setMaxCapacity(Double maxCapacity) { this.maxCapacity = maxCapacity; } + + public String getWorkerType() { return workerType; } + public void setWorkerType(String workerType) { this.workerType = workerType; } + + public Integer getNumberOfWorkers() { return numberOfWorkers; } + public void setNumberOfWorkers(Integer numberOfWorkers) { this.numberOfWorkers = numberOfWorkers; } + + public String getSecurityConfiguration() { return securityConfiguration; } + public void setSecurityConfiguration(String securityConfiguration) { this.securityConfiguration = securityConfiguration; } + + public String getLogGroupName() { return logGroupName; } + public void setLogGroupName(String logGroupName) { this.logGroupName = logGroupName; } + + public NotificationProperty getNotificationProperty() { return notificationProperty; } + public void setNotificationProperty(NotificationProperty notificationProperty) { this.notificationProperty = notificationProperty; } + + public String getGlueVersion() { return glueVersion; } + public void setGlueVersion(String glueVersion) { this.glueVersion = glueVersion; } + + public String getExecutionClass() { return executionClass; } + public void setExecutionClass(String executionClass) { this.executionClass = executionClass; } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e351066351..882d7bcd02 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -509,6 +509,7 @@ floci: default-image: "floci/floci-duck:latest" glue: enabled: true + job-run-duration-seconds: 0 # 0 = job runs succeed as soon as they start ses: enabled: true # smtp-host: mailpit # SMTP server for email relay (empty = store only) diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunIntegrationTest.java new file mode 100644 index 0000000000..29ae53aa25 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunIntegrationTest.java @@ -0,0 +1,62 @@ +package io.github.hectorvent.floci.services.glue; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.matchesPattern; + +@QuarkusTest +class GlueJobRunIntegrationTest { + + private static final String CONTENT_TYPE = "application/x-amz-json-1.1"; + private static final String ROLE = "arn:aws:iam::000000000000:role/my-role"; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + private static ValidatableResponse call(String action, String body) { + return given().contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AWSGlue." + action) + .body(body) + .when().post("/") + .then(); + } + + @Test + void jobRunStartsSucceedsAndIsRemovedWithItsJob() { + String jobName = "run-job-" + UUID.randomUUID().toString().substring(0, 8); + call("CreateJob", """ + { "Name": "%s", "Role": "%s", "Command": { "Name": "glueetl" } } + """.formatted(jobName, ROLE)).statusCode(200); + + String runId = call("StartJobRun", "{ \"JobName\": \"" + jobName + "\" }") + .statusCode(200) + .body("JobRunId", matchesPattern("jr_[0-9a-f]{64}")) + .extract().path("JobRunId"); + + call("GetJobRun", "{ \"JobName\": \"" + jobName + "\", \"RunId\": \"" + runId + "\" }") + .statusCode(200) + .body("JobRun.Id", equalTo(runId)) + .body("JobRun.JobRunState", equalTo("SUCCEEDED")); + + call("ListJobs", "{}") + .statusCode(200) + .body("JobNames", hasItem(jobName)); + + call("DeleteJob", "{ \"JobName\": \"" + jobName + "\" }").statusCode(200); + + call("GetJobRuns", "{ \"JobName\": \"" + jobName + "\" }") + .statusCode(400) + .body("__type", equalTo("EntityNotFoundException")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java new file mode 100644 index 0000000000..178af64361 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJobRunServiceTest.java @@ -0,0 +1,465 @@ +package io.github.hectorvent.floci.services.glue; + +import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; +import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.core.storage.StorageFactory; +import io.github.hectorvent.floci.services.glue.model.ExecutionProperty; +import io.github.hectorvent.floci.services.glue.model.Job; +import io.github.hectorvent.floci.services.glue.model.JobCommand; +import io.github.hectorvent.floci.services.glue.model.JobRun; +import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService; +import io.github.hectorvent.floci.services.kms.KmsService; +import io.github.hectorvent.floci.services.resourcegroupstagging.ResourceGroupsTaggingService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.time.Clock; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class GlueJobRunServiceTest { + + private static final String REGION = "us-east-1"; + private static final String ACCOUNT_ID = "000000000000"; + private static final String ROLE = "arn:aws:iam::000000000000:role/glue"; + private static final Instant T0 = Instant.parse("2026-09-25T12:00:00Z"); + + private GlueService glueService; + private InMemoryStorage runStore; + private MutableClock clock; + + @BeforeEach + void setUp() { + RegionResolver regionResolver = new RegionResolver(REGION, ACCOUNT_ID); + StorageFactory storageFactory = new InMemoryStorageFactory(); + glueService = new GlueService(storageFactory, new GlueSchemaRegistryService(storageFactory, regionResolver), + regionResolver, new ResourceGroupsTaggingService(storageFactory), + new KmsService(storageFactory, regionResolver)); + runStore = new InMemoryStorage<>(); + clock = new MutableClock(T0); + } + + private GlueJobRunService service(int runDurationSeconds) { + return new GlueJobRunService(runStore, glueService, runDurationSeconds, clock); + } + + private Job createJob(String name, Integer maxConcurrentRuns, Integer timeoutMinutes) { + Job job = new Job(); + job.setName(name); + job.setRole(ROLE); + job.setCommand(new JobCommand()); + job.setWorkerType("G.1X"); + job.setNumberOfWorkers(2); + job.setTimeout(timeoutMinutes); + if (maxConcurrentRuns != null) { + ExecutionProperty property = new ExecutionProperty(); + property.setMaxConcurrentRuns(maxConcurrentRuns); + job.setExecutionProperty(property); + } + glueService.createJob(job, null, REGION); + return job; + } + + @Test + void runSucceedsAsSoonAsItStartsByDefault() { + createJob("etl", null, null); + + JobRun run = service(0).startJobRun("etl", null, new JobRun()); + + assertTrue(run.getId().matches("jr_[0-9a-f]{64}"), run.getId()); + assertEquals("SUCCEEDED", run.getJobRunState()); + assertEquals(T0, run.getStartedOn()); + assertEquals(T0, run.getCompletedOn()); + assertEquals(0, run.getExecutionTime()); + assertEquals(0, run.getAttempt()); + assertEquals("G.1X", run.getWorkerType()); + assertEquals(2, run.getNumberOfWorkers()); + assertEquals(480, run.getTimeout()); + assertEquals("5.1", run.getGlueVersion()); + assertEquals("/aws-glue/jobs", run.getLogGroupName()); + } + + @Test + void requestFieldsOverrideTheJobDefinitionForThatRunOnly() { + createJob("etl", null, null); + JobRun overrides = new JobRun(); + overrides.setArguments(Map.of("--day", "2026-09-25")); + overrides.setWorkerType("G.2X"); + overrides.setNumberOfWorkers(10); + overrides.setTimeout(30); + + JobRun run = service(0).startJobRun("etl", null, overrides); + + assertEquals(Map.of("--day", "2026-09-25"), run.getArguments()); + assertEquals("G.2X", run.getWorkerType()); + assertEquals(10, run.getNumberOfWorkers()); + assertEquals(30, run.getTimeout()); + assertEquals("G.1X", glueService.getJob("etl").getWorkerType()); + } + + @Test + void runStaysRunningUntilTheConfiguredDurationHasPassed() { + createJob("etl", null, null); + GlueJobRunService service = service(60); + String runId = service.startJobRun("etl", null, new JobRun()).getId(); + + clock.set(T0.plusSeconds(59)); + JobRun running = service.getJobRun("etl", runId); + assertEquals("RUNNING", running.getJobRunState()); + assertNull(running.getCompletedOn()); + + clock.set(T0.plusSeconds(90)); + JobRun finished = service.getJobRun("etl", runId); + assertEquals("SUCCEEDED", finished.getJobRunState()); + assertEquals(T0.plusSeconds(60), finished.getCompletedOn()); + assertEquals(60, finished.getExecutionTime()); + } + + @Test + void runLongerThanTheJobTimeoutEndsInTimeout() { + createJob("etl", null, 1); + GlueJobRunService service = service(120); + String runId = service.startJobRun("etl", null, new JobRun()).getId(); + + clock.set(T0.plusSeconds(61)); + JobRun run = service.getJobRun("etl", runId); + + assertEquals("TIMEOUT", run.getJobRunState()); + assertEquals(T0.plusSeconds(60), run.getCompletedOn()); + assertEquals(60, run.getExecutionTime()); + } + + @Test + void secondRunIsRejectedWhileMaxConcurrentRunsDefaultsToOne() { + createJob("etl", null, null); + GlueJobRunService service = service(60); + service.startJobRun("etl", null, new JobRun()); + + AwsException error = assertThrows(AwsException.class, + () -> service.startJobRun("etl", null, new JobRun())); + assertEquals("ConcurrentRunsExceededException", error.getErrorCode()); + + clock.set(T0.plusSeconds(60)); + assertEquals("RUNNING", service.startJobRun("etl", null, new JobRun()).getJobRunState()); + } + + @Test + void maxConcurrentRunsAndQueuingBothAdmitAnotherRun() { + createJob("wide", 2, null); + createJob("queued", null, null); + GlueJobRunService service = service(60); + + service.startJobRun("wide", null, new JobRun()); + service.startJobRun("wide", null, new JobRun()); + assertThrows(AwsException.class, () -> service.startJobRun("wide", null, new JobRun())); + + JobRun queuing = new JobRun(); + queuing.setJobRunQueuingEnabled(true); + service.startJobRun("queued", null, new JobRun()); + assertTrue(service.startJobRun("queued", null, queuing).getJobRunQueuingEnabled()); + } + + @Test + void batchStopStopsARunningRunAndReportsTheRest() { + createJob("etl", 5, null); + GlueJobRunService service = service(60); + String finished = service.startJobRun("etl", null, new JobRun()).getId(); + clock.set(T0.plusSeconds(60)); + String stillRunning = service.startJobRun("etl", null, new JobRun()).getId(); + clock.set(T0.plusSeconds(70)); + + GlueJobRunService.StopResult result = + service.batchStopJobRun("etl", List.of(stillRunning, finished, "jr_missing")); + + assertEquals(List.of(new GlueJobRunService.StoppedRun("etl", stillRunning)), result.stopped()); + assertEquals(2, result.errors().size()); + assertEquals("InvalidInputException", result.errors().get(0).errorCode()); + assertEquals("EntityNotFoundException", result.errors().get(1).errorCode()); + JobRun stopped = service.getJobRun("etl", stillRunning); + assertEquals("STOPPED", stopped.getJobRunState()); + assertEquals(10, stopped.getExecutionTime()); + assertEquals("SUCCEEDED", service.getJobRun("etl", finished).getJobRunState()); + } + + @Test + void getJobRunsListsNewestFirstAndPages() { + createJob("etl", 10, null); + GlueJobRunService service = service(0); + String first = service.startJobRun("etl", null, new JobRun()).getId(); + clock.set(T0.plusSeconds(1)); + String second = service.startJobRun("etl", null, new JobRun()).getId(); + + GlueService.Page page = service.getJobRuns("etl", 1, null); + assertEquals(second, page.items().getFirst().getId()); + GlueService.Page next = service.getJobRuns("etl", 1, page.nextToken()); + assertEquals(first, next.items().getFirst().getId()); + assertNull(next.nextToken()); + + AwsException tooMany = assertThrows(AwsException.class, () -> service.getJobRuns("etl", 201, null)); + assertEquals("InvalidInputException", tooMany.getErrorCode()); + } + + @Test + void runsAreScopedToTheirJob() { + createJob("a", null, null); + createJob("b", null, null); + GlueJobRunService service = service(0); + String runOfA = service.startJobRun("a", null, new JobRun()).getId(); + + AwsException error = assertThrows(AwsException.class, () -> service.getJobRun("b", runOfA)); + assertEquals("EntityNotFoundException", error.getErrorCode()); + assertTrue(service.getJobRuns("b", null, null).items().isEmpty()); + } + + @Test + void unknownJobOrRetriedRunIsEntityNotFound() { + createJob("etl", null, null); + GlueJobRunService service = service(0); + + assertEquals("EntityNotFoundException", assertThrows(AwsException.class, + () -> service.startJobRun("missing", null, new JobRun())).getErrorCode()); + assertEquals("EntityNotFoundException", assertThrows(AwsException.class, + () -> service.startJobRun("etl", "jr_missing", new JobRun())).getErrorCode()); + + String first = service.startJobRun("etl", null, new JobRun()).getId(); + assertEquals(first, service.startJobRun("etl", first, new JobRun()).getPreviousRunId()); + } + + @Test + void deletingTheJobRemovesItsRuns() { + createJob("etl", null, null); + GlueJobRunService service = service(0); + service.startJobRun("etl", null, new JobRun()); + + glueService.deleteJob("etl", REGION); + service.deleteRuns("etl"); + createJob("etl", null, null); + + assertTrue(service.getJobRuns("etl", null, null).items().isEmpty()); + assertTrue(runStore.scan(key -> true).isEmpty()); + } + + @Test + void concurrentStartsAdmitNoMoreThanMaxConcurrentRuns() throws Exception { + createJob("etl", null, null); + GlueJobRunService service = service(60); + int threads = 16; + CountDownLatch go = new CountDownLatch(1); + AtomicInteger admitted = new AtomicInteger(); + AtomicInteger rejected = new AtomicInteger(); + ExecutorService pool = Executors.newFixedThreadPool(threads); + try { + List> futures = new ArrayList<>(); + for (int i = 0; i < threads; i++) { + futures.add(pool.submit(() -> { + go.await(); + try { + service.startJobRun("etl", null, new JobRun()); + admitted.incrementAndGet(); + } catch (AwsException expected) { + // ConcurrentRunsExceededException is the outcome under test for all but one start. + rejected.incrementAndGet(); + } + return null; + })); + } + go.countDown(); + for (Future future : futures) { + future.get(10, TimeUnit.SECONDS); + } + } finally { + pool.shutdownNow(); + } + + assertEquals(1, admitted.get()); + assertEquals(threads - 1, rejected.get()); + assertEquals(1, runStore.scan(key -> true).size()); + } + + /** + * Holds the run write inside StartJobRun open while DeleteJob runs. Without the service lock, + * deleteRuns scans before the run is stored and the run outlives its job; with it, deleteRuns + * waits for the write and removes the run. + */ + @Test + void startRacingDeleteNeverLeavesARunBehind() throws Exception { + createJob("etl", null, null); + PausingStore store = new PausingStore(runStore); + GlueJobRunService service = new GlueJobRunService(store, glueService, 60, clock); + ExecutorService pool = Executors.newFixedThreadPool(2); + try { + Future start = pool.submit(() -> service.startJobRun("etl", null, new JobRun())); + assertTrue(store.putStarted.await(10, TimeUnit.SECONDS)); + + Future delete = pool.submit(() -> { + glueService.deleteJob("etl", REGION); + service.deleteRuns("etl"); + return null; + }); + try { + delete.get(500, TimeUnit.MILLISECONDS); + } catch (TimeoutException expected) { + // With the lock held by the paused start, the delete is still waiting here. + } + store.release.countDown(); + start.get(10, TimeUnit.SECONDS); + delete.get(10, TimeUnit.SECONDS); + } finally { + pool.shutdownNow(); + } + + assertTrue(runStore.scan(key -> true).isEmpty()); + } + + @Test + void timeoutBelowOneMinuteIsRejected() { + createJob("etl", null, null); + JobRun overrides = new JobRun(); + overrides.setTimeout(-5); + + AwsException error = assertThrows(AwsException.class, + () -> service(0).startJobRun("etl", null, overrides)); + + assertEquals("InvalidInputException", error.getErrorCode()); + assertTrue(runStore.scan(key -> true).isEmpty()); + } + + @Test + void zeroTimeoutInheritedFromTheJobIsRejected() { + createJob("etl", null, 0); + + AwsException error = assertThrows(AwsException.class, + () -> service(0).startJobRun("etl", null, new JobRun())); + + assertEquals("InvalidInputException", error.getErrorCode()); + assertTrue(runStore.scan(key -> true).isEmpty()); + } + + @Test + void negativeRunDurationIsRefusedAtConstruction() { + assertThrows(IllegalArgumentException.class, () -> service(-1)); + } + + /** Delegates to a store, pausing the first put until {@code release} opens. */ + private static final class PausingStore implements StorageBackend { + private final StorageBackend delegate; + private final CountDownLatch putStarted = new CountDownLatch(1); + private final CountDownLatch release = new CountDownLatch(1); + + private PausingStore(StorageBackend delegate) { + this.delegate = delegate; + } + + @Override + public void put(String key, JobRun value) { + if (putStarted.getCount() > 0) { + putStarted.countDown(); + try { + release.await(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("interrupted while paused", e); + } + } + delegate.put(key, value); + } + + @Override + public Optional get(String key) { + return delegate.get(key); + } + + @Override + public void delete(String key) { + delegate.delete(key); + } + + @Override + public List scan(Predicate keyFilter) { + return delegate.scan(keyFilter); + } + + @Override + public Set keys() { + return delegate.keys(); + } + + @Override + public void flush() { + delegate.flush(); + } + + @Override + public void load() { + delegate.load(); + } + + @Override + public void clear() { + delegate.clear(); + } + } + + private static final class InMemoryStorageFactory extends StorageFactory { + private InMemoryStorageFactory() { + super(null, null); + } + + @Override + public AccountAwareStorageBackend create(String serviceName, + String fileName, + TypeReference> typeReference) { + return AccountAwareStorageBackend.inMemory(ACCOUNT_ID); + } + } + + private static final class MutableClock extends Clock { + private Instant now; + + private MutableClock(Instant now) { + this.now = now; + } + + void set(Instant now) { + this.now = now; + } + + @Override + public ZoneOffset getZone() { + return ZoneOffset.UTC; + } + + @Override + public Clock withZone(ZoneId zone) { + return this; + } + + @Override + public Instant instant() { + return now; + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerEmptyListTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerEmptyListTest.java index 281fd7a6b6..b28c93ec7d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerEmptyListTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerEmptyListTest.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.type.TypeReference; +import java.time.Clock; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -44,7 +45,9 @@ class GlueJsonHandlerEmptyListTest { GlueService glueService = new GlueService( storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory), new KmsService(storageFactory, regionResolver)); - handler = new GlueJsonHandler(glueService, schemaRegistryService, mapper); + handler = new GlueJsonHandler(glueService, + new GlueJobRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC()), + schemaRegistryService, mapper); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java index 7326862cfa..88ab00f401 100644 --- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueJsonHandlerTest.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.core.storage.InMemoryStorage; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService; import io.github.hectorvent.floci.services.kms.KmsService; @@ -16,6 +17,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.type.TypeReference; +import java.time.Clock; import java.util.List; import java.util.Map; @@ -44,7 +46,9 @@ class GlueJsonHandlerTest { GlueService glueService = new GlueService( storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory), new KmsService(storageFactory, regionResolver)); - handler = new GlueJsonHandler(glueService, schemaRegistryService, mapper); + handler = new GlueJsonHandler(glueService, + new GlueJobRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC()), + schemaRegistryService, mapper); } private void createDatabaseAndTable(String dbName, String tableName) throws Exception { @@ -793,4 +797,82 @@ class GlueJsonHandlerTest { assertEquals("alias/glue", after.get("ConnectionPasswordEncryption").get("AwsKmsKeyId").asText()); assertEquals("DISABLED", after.get("EncryptionAtRest").get("CatalogEncryptionMode").asText()); } + + private void createJobWithTags(String name, Map tags) throws Exception { + ObjectNode create = mapper.createObjectNode(); + create.put("Name", name); + create.put("Role", "arn:aws:iam::000000000000:role/glue"); + create.putObject("Command").put("Name", "glueetl"); + ObjectNode tagNode = create.putObject("Tags"); + tags.forEach(tagNode::put); + assertEquals(200, handler.handle("CreateJob", create, REGION).getStatus()); + } + + /** + * The wire shape a job run client reads: StartJobRun answers with the run id alone, GetJobRun + * nests the run under "JobRun" with numeric timestamps, and stopping a finished run is an entry + * in Errors rather than a failed request. + */ + @Test + void jobRunOperationsAnswerWithTheDocumentedBodies() throws Exception { + createJobWithTags("nightly", Map.of()); + + ObjectNode start = mapper.createObjectNode(); + start.put("JobName", "nightly"); + start.putObject("Arguments").put("--day", "2026-09-25"); + JsonNode started = mapper.valueToTree(handler.handle("StartJobRun", start, REGION).getEntity()); + assertEquals(1, started.size()); + String runId = started.get("JobRunId").asText(); + + ObjectNode get = mapper.createObjectNode().put("JobName", "nightly").put("RunId", runId); + JsonNode run = mapper.valueToTree(handler.handle("GetJobRun", get, REGION).getEntity()).get("JobRun"); + assertEquals(runId, run.get("Id").asText()); + assertEquals("nightly", run.get("JobName").asText()); + assertEquals("SUCCEEDED", run.get("JobRunState").asText()); + assertEquals("2026-09-25", run.get("Arguments").get("--day").asText()); + assertTrue(run.get("StartedOn").isNumber()); + assertTrue(run.get("CompletedOn").isNumber()); + assertFalse(run.has("ErrorMessage")); + + JsonNode runs = mapper.valueToTree(handler.handle( + "GetJobRuns", mapper.createObjectNode().put("JobName", "nightly"), REGION).getEntity()); + assertEquals(1, runs.get("JobRuns").size()); + assertFalse(runs.has("NextToken")); + + ObjectNode stop = mapper.createObjectNode().put("JobName", "nightly"); + stop.putArray("JobRunIds").add(runId); + JsonNode stopped = mapper.valueToTree(handler.handle("BatchStopJobRun", stop, REGION).getEntity()); + assertEquals(0, stopped.get("SuccessfulSubmissions").size()); + JsonNode error = stopped.get("Errors").get(0); + assertEquals("nightly", error.get("JobName").asText()); + assertEquals(runId, error.get("JobRunId").asText()); + assertEquals("InvalidInputException", error.get("ErrorDetail").get("ErrorCode").asText()); + + handler.handle("DeleteJob", mapper.createObjectNode().put("JobName", "nightly"), REGION); + AwsException gone = assertThrows(AwsException.class, () -> handler.handle( + "GetJobRuns", mapper.createObjectNode().put("JobName", "nightly"), REGION)); + assertEquals("EntityNotFoundException", gone.getErrorCode()); + } + + @Test + void listJobsFiltersOnTagsAndBatchGetJobsReportsMissingNames() throws Exception { + createJobWithTags("tagged", Map.of("team", "data")); + createJobWithTags("plain", Map.of()); + + JsonNode all = mapper.valueToTree(handler.handle("ListJobs", mapper.createObjectNode(), REGION).getEntity()); + assertEquals(List.of("plain", "tagged"), mapper.convertValue(all.get("JobNames"), List.class)); + assertFalse(all.has("NextToken")); + + ObjectNode filtered = mapper.createObjectNode(); + filtered.putObject("Tags").put("team", "data"); + JsonNode byTag = mapper.valueToTree(handler.handle("ListJobs", filtered, REGION).getEntity()); + assertEquals(List.of("tagged"), mapper.convertValue(byTag.get("JobNames"), List.class)); + + ObjectNode batch = mapper.createObjectNode(); + batch.putArray("JobNames").add("tagged").add("absent"); + JsonNode got = mapper.valueToTree(handler.handle("BatchGetJobs", batch, REGION).getEntity()); + assertEquals(1, got.get("Jobs").size()); + assertEquals("tagged", got.get("Jobs").get(0).get("Name").asText()); + assertEquals("absent", got.get("JobsNotFound").get(0).asText()); + } }