feat(glue): create, run and chain triggers (#4436)

Glue had no triggers: CreateTrigger and the rest answered InvalidAction.
Add CreateTrigger, GetTrigger, GetTriggers, ListTriggers,
BatchGetTriggers, UpdateTrigger, DeleteTrigger, StartTrigger and
StopTrigger.

StartTrigger runs an ON_DEMAND trigger's actions. An activated
CONDITIONAL trigger fires when a watched job run or crawl finishes in
the named state, evaluated after every Glue request, so a chain of
triggers completes within the request that starts it when runs finish
at once. Runs started by a trigger carry its TriggerName. SCHEDULED
triggers are stored and can be activated but do not fire on a timer.

Part of #4380
This commit is contained in:
David authored and GitHub committed 2026-09-25 23:14:51 -04:00
1 parent 7a583cc293
commit 0d813adf95
23 files changed
+2015 -14

No files matched your search

@@ -61,7 +61,7 @@ class GlueJobRunServiceTest {
}
private GlueJobRunService service(int runDurationSeconds) {
return new GlueJobRunService(runStore, glueService, runDurationSeconds, clock);
return new GlueJobRunService(runStore, new InMemoryStorage<>(), glueService, runDurationSeconds, clock);
}
private Job createJob(String name, Integer maxConcurrentRuns, Integer timeoutMinutes) {
@@ -309,7 +309,7 @@ class GlueJobRunServiceTest {
void startRacingDeleteNeverLeavesARunBehind() throws Exception {
createJob("etl", null, null);
PausingStore store = new PausingStore(runStore);
GlueJobRunService service = new GlueJobRunService(store, glueService, 60, clock);
GlueJobRunService service = new GlueJobRunService(store, new InMemoryStorage<>(), glueService, 60, clock);
ExecutorService pool = Executors.newFixedThreadPool(2);
try {
Future<?> start = pool.submit(() -> service.startJobRun("etl", null, new JobRun()));
@@ -45,9 +45,13 @@ class GlueJsonHandlerEmptyListTest {
GlueService glueService = new GlueService(
storageFactory, schemaRegistryService, regionResolver,
new ResourceGroupsTaggingService(storageFactory), new KmsService(storageFactory, regionResolver));
handler = new GlueJsonHandler(glueService,
new GlueJobRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC()),
new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC()),
GlueJobRunService jobRunService =
new GlueJobRunService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
GlueCrawlerRunService crawlerRunService =
new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
handler = new GlueJsonHandler(glueService, jobRunService, crawlerRunService,
new GlueTriggerService(new InMemoryStorage<>(), glueService,
jobRunService, crawlerRunService),
schemaRegistryService, mapper);
}
@@ -46,9 +46,13 @@ class GlueJsonHandlerTest {
GlueService glueService = new GlueService(
storageFactory, schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(storageFactory),
new KmsService(storageFactory, regionResolver));
handler = new GlueJsonHandler(glueService,
new GlueJobRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC()),
new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC()),
GlueJobRunService jobRunService =
new GlueJobRunService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
GlueCrawlerRunService crawlerRunService =
new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
handler = new GlueJsonHandler(glueService, jobRunService, crawlerRunService,
new GlueTriggerService(new InMemoryStorage<>(), glueService,
jobRunService, crawlerRunService),
schemaRegistryService, mapper);
}
@@ -947,4 +951,74 @@ class GlueJsonHandlerTest {
assertEquals("READY", got.get("Crawlers").get(0).get("State").asText());
assertEquals("absent", got.get("CrawlersNotFound").get(0).asText());
}
/**
* Trigger CRUD on the wire: Create, Start, Stop and Delete answer with the name, Get and
* UpdateTrigger with the trigger, and the definition round trips with its predicate.
*/
@Test
void triggerOperationsAnswerWithTheDocumentedBodies() throws Exception {
createJobWithTags("extract", Map.of());
createJobWithTags("load", Map.of());
ObjectNode create = mapper.createObjectNode();
create.put("Name", "after-extract");
create.put("Type", "CONDITIONAL");
create.put("StartOnCreation", true);
create.putArray("Actions").addObject().put("JobName", "load");
ObjectNode predicate = create.putObject("Predicate");
predicate.put("Logical", "AND");
predicate.putArray("Conditions").addObject()
.put("LogicalOperator", "EQUALS").put("JobName", "extract").put("State", "SUCCEEDED");
create.putObject("Tags").put("team", "data");
JsonNode created = mapper.valueToTree(handler.handle("CreateTrigger", create, REGION).getEntity());
assertEquals("after-extract", created.get("Name").asText());
ObjectNode byName = mapper.createObjectNode().put("Name", "after-extract");
JsonNode trigger = mapper.valueToTree(handler.handle("GetTrigger", byName, REGION).getEntity()).get("Trigger");
assertEquals("CONDITIONAL", trigger.get("Type").asText());
assertEquals("ACTIVATED", trigger.get("State").asText());
assertEquals("load", trigger.get("Actions").get(0).get("JobName").asText());
assertEquals("SUCCEEDED", trigger.get("Predicate").get("Conditions").get(0).get("State").asText());
assertFalse(trigger.has("Schedule"));
ObjectNode tags = mapper.createObjectNode();
tags.put("ResourceArn", "arn:aws:glue:" + REGION + ":" + ACCOUNT_ID + ":trigger/after-extract");
assertEquals("data", mapper.valueToTree(handler.handle("GetTags", tags, REGION).getEntity())
.get("Tags").get("team").asText());
ObjectNode update = mapper.createObjectNode().put("Name", "after-extract");
update.putObject("TriggerUpdate").put("Description", "load after extract");
JsonNode updated = mapper.valueToTree(handler.handle("UpdateTrigger", update, REGION).getEntity());
assertEquals("load after extract", updated.get("Trigger").get("Description").asText());
assertEquals("after-extract", mapper.valueToTree(
handler.handle("StopTrigger", byName, REGION).getEntity()).get("Name").asText());
assertEquals("after-extract", mapper.valueToTree(
handler.handle("DeleteTrigger", byName, REGION).getEntity()).get("Name").asText());
JsonNode names = mapper.valueToTree(handler.handle("ListTriggers", mapper.createObjectNode(), REGION).getEntity());
assertEquals(0, names.get("TriggerNames").size());
}
/** A conditional trigger fires within the request that finishes the run it watches. */
@Test
void conditionalTriggerFiresAfterTheRequestThatFinishesTheWatchedRun() throws Exception {
createJobWithTags("extract", Map.of());
createJobWithTags("load", Map.of());
ObjectNode create = mapper.createObjectNode();
create.put("Name", "after-extract");
create.put("Type", "CONDITIONAL");
create.put("StartOnCreation", true);
create.putArray("Actions").addObject().put("JobName", "load");
create.putObject("Predicate").putArray("Conditions").addObject()
.put("LogicalOperator", "EQUALS").put("JobName", "extract").put("State", "SUCCEEDED");
handler.handle("CreateTrigger", create, REGION);
handler.handle("StartJobRun", mapper.createObjectNode().put("JobName", "extract"), REGION);
JsonNode runs = mapper.valueToTree(handler.handle(
"GetJobRuns", mapper.createObjectNode().put("JobName", "load"), REGION).getEntity()).get("JobRuns");
assertEquals(1, runs.size());
assertEquals("after-extract", runs.get(0).get("TriggerName").asText());
}
}
@@ -31,6 +31,7 @@ import io.github.hectorvent.floci.services.glue.model.PartitionIndexDescriptor;
import io.github.hectorvent.floci.services.glue.model.S3Target;
import io.github.hectorvent.floci.services.glue.model.SchemaReference;
import io.github.hectorvent.floci.services.glue.model.SecurityConfiguration;
import io.github.hectorvent.floci.services.glue.model.Trigger;
import io.github.hectorvent.floci.services.glue.model.StorageDescriptor;
import io.github.hectorvent.floci.services.glue.model.Table;
import io.github.hectorvent.floci.services.glue.model.UserDefinedFunction;
@@ -110,6 +111,7 @@ class GlueServiceTest {
new InMemoryStorage<String, GluePolicy>(),
new InMemoryStorage<String, DataCatalogEncryptionSettings>(),
new InMemoryStorage<String, SecurityConfiguration>(),
new InMemoryStorage<String, Trigger>(),
schemaRegistryService, regionResolver, new ResourceGroupsTaggingService(null), kmsService);
glueService.createDatabase(new Database("db1"));
}
@@ -0,0 +1,73 @@
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.hasSize;
@QuarkusTest
class GlueTriggerIntegrationTest {
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 onDemandAndConditionalTriggersStartTheirJobs() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String extract = "extract-" + suffix;
String load = "load-" + suffix;
for (String job : new String[] {extract, load}) {
call("CreateJob", """
{ "Name": "%s", "Role": "%s", "Command": { "Name": "glueetl" },
"ExecutionProperty": { "MaxConcurrentRuns": 5 } }
""".formatted(job, ROLE)).statusCode(200);
}
call("CreateTrigger", """
{ "Name": "manual-%s", "Type": "ON_DEMAND", "Actions": [ { "JobName": "%s" } ] }
""".formatted(suffix, extract)).statusCode(200);
call("CreateTrigger", """
{ "Name": "chain-%s", "Type": "CONDITIONAL", "StartOnCreation": true,
"Actions": [ { "JobName": "%s" } ],
"Predicate": { "Conditions": [
{ "LogicalOperator": "EQUALS", "JobName": "%s", "State": "SUCCEEDED" } ] } }
""".formatted(suffix, load, extract)).statusCode(200);
call("StartTrigger", "{ \"Name\": \"manual-" + suffix + "\" }")
.statusCode(200)
.body("Name", equalTo("manual-" + suffix));
call("GetJobRuns", "{ \"JobName\": \"" + extract + "\" }")
.statusCode(200)
.body("JobRuns", hasSize(1))
.body("JobRuns[0].TriggerName", equalTo("manual-" + suffix));
call("GetJobRuns", "{ \"JobName\": \"" + load + "\" }")
.statusCode(200)
.body("JobRuns", hasSize(1))
.body("JobRuns[0].TriggerName", equalTo("chain-" + suffix));
call("DeleteTrigger", "{ \"Name\": \"chain-" + suffix + "\" }").statusCode(200);
call("GetTrigger", "{ \"Name\": \"chain-" + suffix + "\" }")
.statusCode(400)
.body("__type", equalTo("EntityNotFoundException"));
}
}
@@ -0,0 +1,630 @@
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.StorageFactory;
import io.github.hectorvent.floci.services.glue.model.Crawler;
import io.github.hectorvent.floci.services.glue.model.CrawlerTargets;
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.model.Predicate;
import io.github.hectorvent.floci.services.glue.model.S3Target;
import io.github.hectorvent.floci.services.glue.model.Trigger;
import io.github.hectorvent.floci.services.glue.model.TriggerAction;
import io.github.hectorvent.floci.services.glue.model.TriggerCondition;
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 io.github.hectorvent.floci.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 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 GlueTriggerServiceTest {
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 GlueService glueService;
private GlueJobRunService jobRuns;
private GlueCrawlerRunService crawls;
private GlueTriggerService triggers;
@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));
jobRuns = new GlueJobRunService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
crawls = new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
triggers = new GlueTriggerService(new InMemoryStorage<>(), glueService, jobRuns, crawls);
for (String job : List.of("extract", "load", "report")) {
createJob(job);
}
Crawler crawler = new Crawler();
crawler.setName("raw");
crawler.setRole(ROLE);
S3Target target = new S3Target();
target.setPath("s3://raw/data");
CrawlerTargets targets = new CrawlerTargets();
targets.setS3Targets(List.of(target));
crawler.setTargets(targets);
glueService.createCrawler(crawler, null, REGION);
}
private void createJob(String name) {
Job job = new Job();
job.setName(name);
job.setRole(ROLE);
job.setCommand(new JobCommand());
ExecutionProperty property = new ExecutionProperty();
property.setMaxConcurrentRuns(100);
job.setExecutionProperty(property);
glueService.createJob(job, null, REGION);
}
private static TriggerAction startJob(String job) {
TriggerAction action = new TriggerAction();
action.setJobName(job);
return action;
}
private static TriggerCondition jobIs(String job, String state) {
TriggerCondition condition = new TriggerCondition();
condition.setLogicalOperator("EQUALS");
condition.setJobName(job);
condition.setState(state);
return condition;
}
private static TriggerCondition crawlIs(String crawler, String state) {
TriggerCondition condition = new TriggerCondition();
condition.setLogicalOperator("EQUALS");
condition.setCrawlerName(crawler);
condition.setCrawlState(state);
return condition;
}
private static Trigger trigger(String name, String type, List<TriggerAction> actions) {
Trigger trigger = new Trigger();
trigger.setName(name);
trigger.setType(type);
trigger.setActions(actions);
return trigger;
}
private static Trigger conditional(String name, String logical, List<TriggerCondition> conditions,
String startsJob) {
Trigger trigger = trigger(name, "CONDITIONAL", List.of(startJob(startsJob)));
Predicate predicate = new Predicate();
predicate.setLogical(logical);
predicate.setConditions(conditions);
trigger.setPredicate(predicate);
return trigger;
}
private List<JobRun> runsOf(String job) {
return jobRuns.getJobRuns(job, 200, null).items();
}
/** Keeps evaluating, as later run-related requests would, until nothing more fires. */
private void settleEverything() {
for (int request = 0; request < 20; request++) {
triggers.fireConditionalTriggers();
}
}
private String startAndSettle(String job) {
String runId = jobRuns.startJobRun(job, null, new JobRun()).getId();
triggers.fireConditionalTriggers();
return runId;
}
@Test
void definitionsAreValidatedAgainstTheApiRules() {
assertEquals("InvalidInputException", assertThrows(AwsException.class, () ->
triggers.createTrigger(trigger("t", "ON_DEMAND", List.of()), false, null, REGION)).getErrorCode());
assertEquals("EntityNotFoundException", assertThrows(AwsException.class, () -> triggers.createTrigger(
trigger("t", "ON_DEMAND", List.of(startJob("missing"))), false, null, REGION)).getErrorCode());
TriggerAction both = startJob("load");
both.setCrawlerName("raw");
assertEquals("InvalidInputException", assertThrows(AwsException.class, () ->
triggers.createTrigger(trigger("t", "ON_DEMAND", List.of(both)), false, null, REGION)).getErrorCode());
assertEquals("InvalidInputException", assertThrows(AwsException.class, () -> triggers.createTrigger(
trigger("t", "SCHEDULED", List.of(startJob("load"))), false, null, REGION)).getErrorCode());
assertEquals("InvalidInputException", assertThrows(AwsException.class, () -> triggers.createTrigger(
trigger("t", "CONDITIONAL", List.of(startJob("load"))), false, null, REGION)).getErrorCode());
assertEquals("InvalidInputException", assertThrows(AwsException.class, () -> triggers.createTrigger(
conditional("t", null, List.of(jobIs("extract", "RUNNING")), "load"), false, null, REGION))
.getErrorCode());
assertEquals("InvalidInputException", assertThrows(AwsException.class, () -> triggers.createTrigger(
trigger("t", "ON_DEMAND", List.of(startJob("load"))), true, null, REGION)).getErrorCode());
Trigger inWorkflow = trigger("t", "ON_DEMAND", List.of(startJob("load")));
inWorkflow.setWorkflowName("pipeline");
assertEquals("EntityNotFoundException", assertThrows(AwsException.class, () ->
triggers.createTrigger(inWorkflow, false, null, REGION)).getErrorCode());
triggers.createTrigger(trigger("t", "ON_DEMAND", List.of(startJob("load"))), false, null, REGION);
assertEquals("AlreadyExistsException", assertThrows(AwsException.class, () -> triggers.createTrigger(
trigger("t", "ON_DEMAND", List.of(startJob("load"))), false, null, REGION)).getErrorCode());
}
@Test
void stateFollowsCreationStartAndStop() {
Trigger nightly = trigger("nightly", "SCHEDULED", List.of(startJob("load")));
nightly.setSchedule("cron(0 2 * * ? *)");
triggers.createTrigger(nightly, false, null, REGION);
assertEquals("CREATED", glueService.getTrigger("nightly").getState());
triggers.startTrigger("nightly");
assertEquals("ACTIVATED", glueService.getTrigger("nightly").getState());
triggers.stopTrigger("nightly");
assertEquals("DEACTIVATED", glueService.getTrigger("nightly").getState());
triggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
assertEquals("ACTIVATED", glueService.getTrigger("after").getState());
triggers.createTrigger(trigger("manual", "ON_DEMAND", List.of(startJob("load"))), false, null, REGION);
assertEquals("InvalidInputException",
assertThrows(AwsException.class, () -> triggers.stopTrigger("manual")).getErrorCode());
}
@Test
void startingAnOnDemandTriggerRunsItsActions() {
TriggerAction load = startJob("load");
load.setArguments(Map.of("--mode", "full"));
TriggerAction crawl = new TriggerAction();
crawl.setCrawlerName("raw");
triggers.createTrigger(trigger("manual", "ON_DEMAND", List.of(load, crawl)), false, null, REGION);
triggers.startTrigger("manual");
JobRun run = runsOf("load").getFirst();
assertEquals("manual", run.getTriggerName());
assertEquals(Map.of("--mode", "full"), run.getArguments());
assertEquals("SUCCEEDED", crawls.completionsAfter("raw", null).getLast().state());
assertEquals("CREATED", glueService.getTrigger("manual").getState());
}
@Test
void conditionalTriggerFiresOnceWhenTheWatchedJobSucceeds() {
triggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
startAndSettle("extract");
triggers.fireConditionalTriggers();
assertEquals(1, runsOf("load").size());
assertEquals("after", runsOf("load").getFirst().getTriggerName());
}
@Test
void runsFinishedBeforeActivationDoNotFire() {
startAndSettle("extract");
triggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
false, null, REGION);
triggers.startTrigger("after");
triggers.fireConditionalTriggers();
assertTrue(runsOf("load").isEmpty());
}
@Test
void conditionOnAnotherStateDoesNotFire() {
triggers.createTrigger(conditional("onFailure", null, List.of(jobIs("extract", "FAILED")), "report"),
true, null, REGION);
startAndSettle("extract");
assertTrue(runsOf("report").isEmpty());
}
@Test
void andWaitsForEveryConditionWhileAnyFiresOnTheFirst() {
List<TriggerCondition> conditions = List.of(jobIs("extract", "SUCCEEDED"), crawlIs("raw", "SUCCEEDED"));
triggers.createTrigger(conditional("both", "AND", conditions, "load"), true, null, REGION);
triggers.createTrigger(conditional("either", "ANY", conditions, "report"), true, null, REGION);
startAndSettle("extract");
assertTrue(runsOf("load").isEmpty());
assertEquals(1, runsOf("report").size());
crawls.startCrawler("raw");
triggers.fireConditionalTriggers();
assertEquals(1, runsOf("load").size());
assertEquals(2, runsOf("report").size());
}
@Test
void chainedTriggersRunTheWholePipelineInOneRequest() {
triggers.createTrigger(conditional("step1", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
triggers.createTrigger(conditional("step2", null, List.of(jobIs("load", "SUCCEEDED")), "report"),
true, null, REGION);
startAndSettle("extract");
assertEquals(1, runsOf("load").size());
assertEquals(1, runsOf("report").size());
assertEquals("step2", runsOf("report").getFirst().getTriggerName());
}
@Test
void aTriggerThatRestartsItsOwnJobStopsAtTheRunBudget() {
triggers.createTrigger(conditional("loop", null, List.of(jobIs("extract", "SUCCEEDED")), "extract"),
true, null, REGION);
startAndSettle("extract");
assertEquals(26, runsOf("extract").size());
settleEverything();
assertEquals(1 + GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN, runsOf("extract").size());
assertEquals("ACTIVATED", glueService.getTrigger("loop").getState());
}
@Test
void aTwoTriggerCycleStopsAtTheRunBudget() {
triggers.createTrigger(conditional("forth", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
triggers.createTrigger(conditional("back", null, List.of(jobIs("load", "SUCCEEDED")), "extract"),
true, null, REGION);
startAndSettle("extract");
settleEverything();
assertEquals(1 + GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN, runsOf("extract").size() + runsOf("load").size());
assertEquals("ACTIVATED", glueService.getTrigger("forth").getState());
assertEquals("ACTIVATED", glueService.getTrigger("back").getState());
}
/** Two actions restarting the watched job double the runs each hop; the budget bounds runs, not hops. */
@Test
void aBranchingLoopStopsAtTheRunBudget() {
Trigger branching = conditional("branch", null, List.of(jobIs("extract", "SUCCEEDED")), "extract");
branching.setActions(List.of(startJob("extract"), startJob("extract")));
triggers.createTrigger(branching, true, null, REGION);
startAndSettle("extract");
settleEverything();
assertEquals(1 + GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN, runsOf("extract").size());
}
/** Every crawl has its own id, so a crawler deleted and created again never inherits old bookkeeping. */
@Test
void aRecreatedCrawlerStartsANewChainWithItsOwnBudget() {
triggers.createTrigger(conditional("each", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "load"),
true, null, REGION);
String first = crawls.startCrawler("raw");
triggers.fireConditionalTriggers();
triggers.deleteTrigger("each", REGION);
crawls.deleteCrawler("raw", REGION);
Crawler again = new Crawler();
again.setName("raw");
again.setRole(ROLE);
S3Target target = new S3Target();
target.setPath("s3://raw/data");
CrawlerTargets targets = new CrawlerTargets();
targets.setS3Targets(List.of(target));
again.setTargets(targets);
glueService.createCrawler(again, null, REGION);
triggers.createTrigger(conditional("each", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "load"),
true, null, REGION);
String second = crawls.startCrawler("raw");
triggers.fireConditionalTriggers();
assertTrue(!first.equals(second), first + " was reused");
assertEquals(2, runsOf("load").size());
}
/**
* A ring longer than the rounds one request runs: names sort against the ring order so each round
* advances one link, and the loop still has to stop across requests.
*/
@Test
void aTriggerRingLongerThanOneRequestStopsAtTheRunBudget() {
int size = 26;
for (int i = 0; i < size; i++) {
createJob("ring" + i);
}
for (int i = 0; i < size; i++) {
triggers.createTrigger(conditional("hop" + (100 - i), null, List.of(jobIs("ring" + i, "SUCCEEDED")),
"ring" + ((i + 1) % size)), true, null, REGION);
}
startAndSettle("ring0");
settleEverything();
int total = 0;
for (int i = 0; i < size; i++) {
total += runsOf("ring" + i).size();
}
assertEquals(1 + GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN, total);
}
/**
* Trigger names sorting against the chain order make every round advance one link only; a chain
* longer than the round limit must keep its triggers and finish on the next evaluation.
*/
@Test
void aChainLongerThanTheRoundLimitIsNotDeactivated() {
int links = 30;
for (int i = 1; i <= links; i++) {
createJob("step" + i);
}
for (int i = 0; i < links; i++) {
String watched = i == 0 ? "extract" : "step" + i;
triggers.createTrigger(conditional("link" + (100 - i), null, List.of(jobIs(watched, "SUCCEEDED")),
"step" + (i + 1)), true, null, REGION);
}
startAndSettle("extract");
assertTrue(runsOf("step" + links).isEmpty());
triggers.fireConditionalTriggers();
assertEquals(1, runsOf("step" + links).size());
for (Trigger trigger : glueService.allTriggers()) {
assertEquals("ACTIVATED", trigger.getState(), trigger.getName());
}
}
/** A trigger fed by two branches fires in two rounds without being part of any cycle. */
@Test
void aFanInTriggerFiringInSeveralRoundsOfALongPipelineStaysActive() {
int links = 30;
for (int i = 1; i <= links; i++) {
createJob("step" + i);
}
for (int i = 0; i < links; i++) {
String watched = i == 0 ? "extract" : "step" + i;
triggers.createTrigger(conditional("link" + (100 - i), null, List.of(jobIs(watched, "SUCCEEDED")),
"step" + (i + 1)), true, null, REGION);
}
triggers.createTrigger(conditional("sink", "ANY",
List.of(jobIs("step2", "SUCCEEDED"), jobIs("step20", "SUCCEEDED")), "report"), true, null, REGION);
startAndSettle("extract");
triggers.fireConditionalTriggers();
assertEquals("ACTIVATED", glueService.getTrigger("sink").getState());
assertEquals(2, runsOf("report").size());
assertEquals(1, runsOf("step" + links).size());
}
/**
* forward watches step5 succeed and starts phase; back watches phase fail and starts step5. The
* definitions form a loop, but phase runs succeed, so back never fires and nothing repeats: forward
* must stay active even when a long pipeline reaches the round limit.
*/
@Test
void aCycleThatNeverRunsDeactivatesNothing() {
int links = 30;
for (int i = 1; i <= links; i++) {
createJob("step" + i);
}
createJob("phase");
for (int i = 0; i < links; i++) {
String watched = i == 0 ? "extract" : "step" + i;
triggers.createTrigger(conditional("link" + (100 - i), null, List.of(jobIs(watched, "SUCCEEDED")),
"step" + (i + 1)), true, null, REGION);
}
triggers.createTrigger(conditional("forward", null, List.of(jobIs("step5", "SUCCEEDED")), "phase"),
true, null, REGION);
triggers.createTrigger(conditional("back", null, List.of(jobIs("phase", "FAILED")), "step5"),
true, null, REGION);
startAndSettle("extract");
triggers.fireConditionalTriggers();
assertEquals(1, runsOf("step" + links).size());
assertEquals(1, runsOf("phase").size());
assertEquals("ACTIVATED", glueService.getTrigger("forward").getState());
assertEquals("ACTIVATED", glueService.getTrigger("back").getState());
}
@Test
void updatingOrRestartingAnActiveTriggerKeepsCompletionsItHasNotEvaluated() {
triggers.createTrigger(conditional("after", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "load"),
true, null, REGION);
crawls.startCrawler("raw");
Trigger update = new Trigger();
update.setDescription("changed");
triggers.updateTrigger("after", update);
triggers.fireConditionalTriggers();
assertEquals(1, runsOf("load").size());
crawls.startCrawler("raw");
triggers.startTrigger("after");
triggers.fireConditionalTriggers();
assertEquals(2, runsOf("load").size());
}
@Test
void aRejectedUpdateLeavesTheStoredTriggerUnchanged() {
triggers.createTrigger(trigger("manual", "ON_DEMAND", List.of(startJob("load"))), false, null, REGION);
Trigger update = new Trigger();
update.setDescription("changed");
update.setActions(List.of(startJob("missing")));
assertThrows(AwsException.class, () -> triggers.updateTrigger("manual", update));
Trigger stored = glueService.getTrigger("manual");
assertNull(stored.getDescription());
assertEquals("load", stored.getActions().getFirst().getJobName());
}
@Test
void conditionWithoutAStateIsAnInputError() {
TriggerCondition noState = jobIs("extract", null);
TriggerCondition noCrawlState = crawlIs("raw", null);
for (TriggerCondition condition : List.of(noState, noCrawlState)) {
assertEquals("InvalidInputException", assertThrows(AwsException.class, () -> triggers.createTrigger(
conditional("t", null, List.of(condition), "load"), false, null, REGION)).getErrorCode());
}
}
@Test
void aSuccessIsNotLostWhenALaterRunOfTheSameJobIsStoppedFirst() {
MutableClock clock = new MutableClock();
GlueJobRunService slowRuns = new GlueJobRunService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, 60, clock);
GlueTriggerService slowTriggers = new GlueTriggerService(new InMemoryStorage<>(), glueService, slowRuns, crawls);
slowTriggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
slowRuns.startJobRun("extract", null, new JobRun());
clock.advance(Duration.ofSeconds(10));
String second = slowRuns.startJobRun("extract", null, new JobRun()).getId();
clock.advance(Duration.ofSeconds(55));
slowRuns.batchStopJobRun("extract", List.of(second));
slowTriggers.fireConditionalTriggers();
assertEquals(1, slowRuns.getJobRuns("load", null, null).items().size());
}
@Test
void jobRunsFinishingInTheSameClockTickEachCount() {
Clock frozen = Clock.fixed(Instant.parse("2026-09-25T12:00:00Z"), ZoneOffset.UTC);
GlueJobRunService frozenRuns =
new GlueJobRunService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, 0, frozen);
GlueTriggerService frozenTriggers = new GlueTriggerService(new InMemoryStorage<>(), glueService, frozenRuns, crawls);
frozenTriggers.createTrigger(conditional("each", "ANY", List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
for (int i = 0; i < 5; i++) {
frozenRuns.startJobRun("extract", null, new JobRun());
frozenTriggers.fireConditionalTriggers();
}
assertEquals(5, frozenRuns.getJobRuns("load", null, null).items().size());
}
@Test
void crawlsFinishingInTheSameClockTickEachCount() {
Clock frozen = Clock.fixed(Instant.parse("2026-09-25T12:00:00Z"), ZoneOffset.UTC);
GlueCrawlerRunService frozenCrawls = new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, frozen);
GlueTriggerService frozenTriggers =
new GlueTriggerService(new InMemoryStorage<>(), glueService, jobRuns, frozenCrawls);
frozenTriggers.createTrigger(conditional("each", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "load"),
true, null, REGION);
frozenCrawls.startCrawler("raw");
frozenCrawls.startCrawler("raw");
frozenTriggers.fireConditionalTriggers();
assertEquals(2, runsOf("load").size());
}
@Test
void deactivatedTriggersDoNotFire() {
triggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
triggers.stopTrigger("after");
startAndSettle("extract");
assertTrue(runsOf("load").isEmpty());
}
@Test
void updateMergesTheGivenMembersAndDeleteIsIdempotent() {
triggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
Trigger update = new Trigger();
update.setDescription("nightly load");
update.setActions(List.of(startJob("report")));
Trigger updated = triggers.updateTrigger("after", update);
assertEquals("nightly load", updated.getDescription());
assertEquals("report", updated.getActions().getFirst().getJobName());
assertEquals("ACTIVATED", updated.getState());
assertEquals("extract", updated.getPredicate().getConditions().getFirst().getJobName());
triggers.deleteTrigger("after", REGION);
triggers.deleteTrigger("after", REGION);
assertEquals("EntityNotFoundException",
assertThrows(AwsException.class, () -> glueService.getTrigger("after")).getErrorCode());
}
@Test
void dependentJobNameReturnsTheTriggersThatStartTheJobOrElseAll() {
triggers.createTrigger(trigger("loads", "ON_DEMAND", List.of(startJob("load"))), false, null, REGION);
triggers.createTrigger(trigger("reports", "ON_DEMAND", List.of(startJob("report"))), false, null, REGION);
assertEquals(List.of("loads"), glueService.getTriggers("load", null, null).items().stream()
.map(Trigger::getName).toList());
assertEquals(2, glueService.getTriggers("extract", null, null).items().size());
assertEquals("EntityNotFoundException", assertThrows(AwsException.class,
() -> glueService.getTriggers("missing", null, null)).getErrorCode());
}
@Test
void concurrentEvaluationsFireATriggerOncePerCompletion() throws Exception {
triggers.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
jobRuns.startJobRun("extract", null, new JobRun());
int threads = 8;
CountDownLatch go = new CountDownLatch(1);
ExecutorService pool = Executors.newFixedThreadPool(threads);
try {
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < threads; i++) {
futures.add(pool.submit(() -> {
go.await();
triggers.fireConditionalTriggers();
return null;
}));
}
go.countDown();
for (Future<?> future : futures) {
future.get(10, TimeUnit.SECONDS);
}
} finally {
pool.shutdownNow();
}
assertEquals(1, runsOf("load").size());
}
private static final class InMemoryStorageFactory extends StorageFactory {
private InMemoryStorageFactory() {
super(null, null);
}
@Override
public <V> AccountAwareStorageBackend<V> create(String serviceName,
String fileName,
TypeReference<Map<String, V>> typeReference) {
return AccountAwareStorageBackend.inMemory(ACCOUNT_ID);
}
}
}