Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(glue): keep counting a trigger chain when its origin is gone (#4472)

Davide186a772a8b0
diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueCrawlerRunService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueCrawlerRunService.java
index b24d8a7352..1b27493e3b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueCrawlerRunService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueCrawlerRunService.java
@@ -175,30 +175,36 @@ public class GlueCrawlerRunService {
for (FinishedCrawl crawl : record.getRecentCrawls()) {
if (crawl.getSequence() > after) {
completions.add(new GlueRunCompletion(Long.toString(crawl.getSequence()), crawl.getFinishedAt(),
- crawl.getStatus(), crawl.getOriginRunId()));
+ crawl.getStatus(), originOf(name, crawl)));
}
}
return completions;
}
- /**
- * Records that triggers are about to start {@code runs} more runs on behalf of the origin crawl, if
- * that keeps it within {@code limit}. False when the crawl is no longer in its crawler's history.
- */
- public synchronized boolean claimTriggeredRuns(String originCrawlId, int runs, int limit) {
+ /** The run count an origin crawl recorded before counts moved out of crawl history; 0 if none. */
+ public synchronized int legacyTriggeredRuns(String originCrawlId) {
for (CrawlerRunRecord record : recordStore.scan(key -> true)) {
for (FinishedCrawl crawl : record.getRecentCrawls()) {
if (originCrawlId.equals(crawl.getCrawlId())) {
- if (crawl.getTriggeredRuns() + runs > limit) {
- return false;
- }
- crawl.setTriggeredRuns(crawl.getTriggeredRuns() + runs);
- recordStore.put(record.getCrawlerName(), record);
- return true;
+ return crawl.getLegacyTriggeredRuns();
}
}
}
- return false;
+ return 0;
+ }
+
+ /**
+ * A crawl persisted before crawls recorded their origin (or their id) is its own origin; the
+ * crawler name and sequence number still identify it.
+ */
+ private static String originOf(String crawlerName, FinishedCrawl crawl) {
+ if (crawl.getOriginRunId() != null) {
+ return crawl.getOriginRunId();
+ }
+ if (crawl.getCrawlId() != null) {
+ return crawl.getCrawlId();
+ }
+ return CRAWL_ID_PREFIX + crawlerName + ":" + crawl.getSequence();
}
public synchronized void updateCrawler(Crawler update) {
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
index 8d1f138d70..1832c09f3f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueJobRunService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueJobRunService.java
@@ -19,7 +19,6 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
-import java.util.Optional;
import java.util.Set;
import java.util.UUID;
@@ -227,6 +226,11 @@ public class GlueJobRunService {
return String.format("%020d", bookkeepingOf(run.getId()).getCompletionOrder());
}
+ /** The run count an origin job run recorded before counts moved out of run bookkeeping; 0 if none. */
+ public synchronized int legacyTriggeredRuns(String originRunId) {
+ return bookkeepingStore.get(originRunId).map(JobRunBookkeeping::getLegacyTriggeredRuns).orElse(0);
+ }
+
/** 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)) {
@@ -287,20 +291,6 @@ public class GlueJobRunService {
});
}
- /**
- * Records that triggers are about to start {@code runs} more runs on behalf of the origin job run,
- * if that keeps the origin within {@code limit}. False when the origin run no longer exists.
- */
- public synchronized boolean claimTriggeredRuns(String originRunId, int runs, int limit) {
- Optional<JobRunBookkeeping> origin = bookkeepingStore.get(originRunId);
- if (origin.isEmpty() || origin.get().getTriggeredRuns() + runs > limit) {
- return false;
- }
- origin.get().setTriggeredRuns(origin.get().getTriggeredRuns() + runs);
- bookkeepingStore.put(originRunId, origin.get());
- return true;
- }
-
private JobRun findRun(String jobName, String runId) {
return runStore.get(runId)
.filter(run -> jobName.equals(run.getJobName()))
diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java b/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java
index ad99472ef2..b045e23608 100644
--- a/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/GlueTriggerService.java
@@ -4,14 +4,17 @@ import com.fasterxml.jackson.core.type.TypeReference;
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.core.storage.WriteProfile;
import io.github.hectorvent.floci.services.glue.model.JobRun;
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.TriggerChainBudget;
import io.github.hectorvent.floci.services.glue.model.TriggerCondition;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
@@ -48,6 +51,11 @@ public class GlueTriggerService {
// that no trigger started (the origin of a chain). Here every run succeeds, so without it a trigger
// loop of any shape, branching or not, would never stop.
static final int MAX_TRIGGERED_RUNS_PER_ORIGIN = 100;
+ // Chains whose counts are kept; beyond it the least recently used is forgotten, so the store stays
+ // bounded however many runs set off chains. The trade-off: a chain forgotten while a descendant still
+ // runs starts a fresh count. It takes this many newer chains to push an active one out, so the bound
+ // is set well above what a local pipeline produces.
+ static final int MAX_TRACKED_CHAINS = 10_000;
private static final String JOB_KEY = "job:";
private static final String CRAWLER_KEY = "crawler:";
// Per watched job or crawler, the seen map holds the last processed position and that outcome's state.
@@ -55,6 +63,8 @@ public class GlueTriggerService {
private static final String STATE = "state:";
private final StorageBackend<String, Map<String, String>> seenStore;
+ private final StorageBackend<String, TriggerChainBudget> budgetStore;
+ private final int maxTrackedChains;
private final GlueService glueService;
private final GlueJobRunService jobRunService;
private final GlueCrawlerRunService crawlerRunService;
@@ -63,12 +73,26 @@ public class GlueTriggerService {
public GlueTriggerService(StorageFactory storageFactory, GlueService glueService,
GlueJobRunService jobRunService, GlueCrawlerRunService crawlerRunService) {
this(storageFactory.create("glue", "trigger_seen_runs.json", new TypeReference<>() {}),
+ // Every claimed run writes here, so under persistent mode the store is journaled rather
+ // than rewritten in full on each claim.
+ storageFactory.create("glue", "trigger_chain_budgets.json", new TypeReference<>() {},
+ WriteProfile.APPEND_HEAVY),
glueService, jobRunService, crawlerRunService);
}
- GlueTriggerService(StorageBackend<String, Map<String, String>> seenStore, GlueService glueService,
+ GlueTriggerService(StorageBackend<String, Map<String, String>> seenStore,
+ StorageBackend<String, TriggerChainBudget> budgetStore, GlueService glueService,
GlueJobRunService jobRunService, GlueCrawlerRunService crawlerRunService) {
+ this(seenStore, budgetStore, glueService, jobRunService, crawlerRunService, MAX_TRACKED_CHAINS);
+ }
+
+ GlueTriggerService(StorageBackend<String, Map<String, String>> seenStore,
+ StorageBackend<String, TriggerChainBudget> budgetStore, GlueService glueService,
+ GlueJobRunService jobRunService, GlueCrawlerRunService crawlerRunService,
+ int maxTrackedChains) {
this.seenStore = seenStore;
+ this.budgetStore = budgetStore;
+ this.maxTrackedChains = maxTrackedChains;
this.glueService = glueService;
this.jobRunService = jobRunService;
this.crawlerRunService = crawlerRunService;
@@ -141,14 +165,7 @@ public class GlueTriggerService {
continue;
}
for (GlueRunCompletion cause : newFirings(trigger)) {
- String origin = cause.originRunId();
- if (!claimTriggeredRuns(origin, trigger.getActions().size())) {
- LOG.warnv("Glue trigger {0} not fired: triggers already started {1} runs on behalf of {2}, "
- + "so they probably form a loop", trigger.getName(), MAX_TRIGGERED_RUNS_PER_ORIGIN, origin);
- continue;
- }
- fire(trigger, origin, false);
- anyFired = true;
+ anyFired |= fire(trigger, cause.originRunId(), false) > 0;
}
}
if (!anyFired) {
@@ -246,22 +263,77 @@ public class GlueTriggerService {
return crawlerRunService.completionsAfter(key.substring(CRAWLER_KEY.length()), position);
}
- private boolean claimTriggeredRuns(String originRunId, int runs) {
- if (originRunId == null) {
+ /**
+ * Counts one more run started on behalf of the chain's origin, if the chain is still within its
+ * budget. The count lives in its own store rather than on the origin run, so it survives the origin
+ * run or crawl being deleted or aging out of history; a chain seen for the first time, including one
+ * started before this bookkeeping existed, starts at zero.
+ */
+ private boolean claimTriggeredRun(String originRunId) {
+ TriggerChainBudget budget = budgetStore.get(originRunId).orElseGet(() -> {
+ TriggerChainBudget fresh = new TriggerChainBudget();
+ fresh.setTriggeredRuns(legacyTriggeredRuns(originRunId));
+ return fresh;
+ });
+ if (budget.getTriggeredRuns() >= MAX_TRIGGERED_RUNS_PER_ORIGIN) {
return false;
}
+ if (budget.getLastUsed() == null) {
+ forgetLeastRecentlyUsedChainIfFull();
+ }
+ budget.setTriggeredRuns(budget.getTriggeredRuns() + 1);
+ budget.setLastUsed(Instant.now());
+ budgetStore.put(originRunId, budget);
+ return true;
+ }
+
+ /** A chain counted before its count moved here carries on from that count. */
+ private int legacyTriggeredRuns(String originRunId) {
return originRunId.startsWith(GlueCrawlerRunService.CRAWL_ID_PREFIX)
- ? crawlerRunService.claimTriggeredRuns(originRunId, runs, MAX_TRIGGERED_RUNS_PER_ORIGIN)
- : jobRunService.claimTriggeredRuns(originRunId, runs, MAX_TRIGGERED_RUNS_PER_ORIGIN);
+ ? crawlerRunService.legacyTriggeredRuns(originRunId)
+ : jobRunService.legacyTriggeredRuns(originRunId);
+ }
+
+ /** Gives back a claim for a run that could not be started. */
+ private void releaseTriggeredRun(String originRunId) {
+ budgetStore.get(originRunId).ifPresent(budget -> {
+ budget.setTriggeredRuns(Math.max(0, budget.getTriggeredRuns() - 1));
+ budgetStore.put(originRunId, budget);
+ });
+ }
+
+ private void forgetLeastRecentlyUsedChainIfFull() {
+ Set<String> origins = budgetStore.keys();
+ if (origins.size() < maxTrackedChains) {
+ return;
+ }
+ String oldest = null;
+ Instant oldestUse = null;
+ for (String origin : origins) {
+ Instant lastUsed = budgetStore.get(origin).map(TriggerChainBudget::getLastUsed).orElse(Instant.MIN);
+ if (oldestUse == null || lastUsed.isBefore(oldestUse)) {
+ oldest = origin;
+ oldestUse = lastUsed;
+ }
+ }
+ budgetStore.delete(oldest);
}
/**
- * Starts the trigger's actions on behalf of {@code originRunId}; with a null origin (StartTrigger on
- * an ON_DEMAND trigger) each run or crawl started is its own origin.
+ * Starts the trigger's actions on behalf of {@code originRunId} and returns how many started; with a
+ * null origin (StartTrigger on an ON_DEMAND trigger) each run or crawl started is its own origin and
+ * no budget applies. With an origin, each action claims one run of the chain's budget before it
+ * starts and gives it back if it cannot start, so only runs that actually start are counted.
*/
- private void fire(Trigger trigger, String originRunId, boolean propagateFailures) {
+ private int fire(Trigger trigger, String originRunId, boolean propagateFailures) {
LOG.infov("Firing Glue trigger {0}", trigger.getName());
+ int started = 0;
for (TriggerAction action : trigger.getActions()) {
+ if (originRunId != null && !claimTriggeredRun(originRunId)) {
+ LOG.warnv("Glue trigger {0} not fired further: triggers already started {1} runs on behalf of {2}, "
+ + "so they probably form a loop", trigger.getName(), MAX_TRIGGERED_RUNS_PER_ORIGIN, originRunId);
+ return started;
+ }
try {
if (action.getJobName() != null) {
JobRun overrides = new JobRun();
@@ -274,7 +346,11 @@ public class GlueTriggerService {
} else {
crawlerRunService.startCrawler(action.getCrawlerName(), originRunId);
}
+ started++;
} catch (AwsException e) {
+ if (originRunId != null) {
+ releaseTriggeredRun(originRunId);
+ }
if (propagateFailures) {
throw e;
}
@@ -282,6 +358,7 @@ public class GlueTriggerService {
action.getJobName() != null ? action.getJobName() : action.getCrawlerName(), e.getMessage());
}
}
+ return started;
}
private static String expectedState(TriggerCondition condition) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/model/FinishedCrawl.java b/src/main/java/io/github/hectorvent/floci/services/glue/model/FinishedCrawl.java
index 9327e55c22..24f6f198a4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/glue/model/FinishedCrawl.java
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/model/FinishedCrawl.java
@@ -1,11 +1,16 @@
package io.github.hectorvent.floci.services.glue.model;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
/** One finished crawl in a crawler's recent history; Floci bookkeeping, never sent on the wire. */
@RegisterForReflection
+// Earlier versions stored the chain's run count here as triggeredRuns: it is still read, so a chain in
+// flight across an upgrade keeps its count, but never written back.
+@JsonIgnoreProperties(ignoreUnknown = true)
public class FinishedCrawl {
private long sequence;
private Instant startedAt;
@@ -13,10 +18,15 @@ public class FinishedCrawl {
private String status;
private String crawlId;
private String originRunId;
- private int triggeredRuns;
+
+ @JsonProperty(value = "triggeredRuns", access = JsonProperty.Access.WRITE_ONLY)
+ private int legacyTriggeredRuns;
public FinishedCrawl() {}
+ public int getLegacyTriggeredRuns() { return legacyTriggeredRuns; }
+ public void setLegacyTriggeredRuns(int legacyTriggeredRuns) { this.legacyTriggeredRuns = legacyTriggeredRuns; }
+
public long getSequence() { return sequence; }
public void setSequence(long sequence) { this.sequence = sequence; }
@@ -35,6 +45,4 @@ public class FinishedCrawl {
public String getOriginRunId() { return originRunId; }
public void setOriginRunId(String originRunId) { this.originRunId = originRunId; }
- public int getTriggeredRuns() { return triggeredRuns; }
- public void setTriggeredRuns(int triggeredRuns) { this.triggeredRuns = triggeredRuns; }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRunBookkeeping.java b/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRunBookkeeping.java
index 769db54b3f..8956652584 100644
--- a/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRunBookkeeping.java
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/model/JobRunBookkeeping.java
@@ -1,26 +1,33 @@
package io.github.hectorvent.floci.services.glue.model;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
/**
* Floci's own bookkeeping for one job run, never sent on the wire, and deleted with the run: the order
- * in which Floci saw it finish, the run that set off its trigger chain, and, when the run is itself
- * such an origin, how many runs triggers have started on its behalf.
+ * in which Floci saw it finish, and the run that set off its trigger chain.
*/
@RegisterForReflection
+// Earlier versions stored the chain's run count here as triggeredRuns: it is still read, so a chain in
+// flight across an upgrade keeps its count, but never written back.
+@JsonIgnoreProperties(ignoreUnknown = true)
public class JobRunBookkeeping {
private long completionOrder;
private String originRunId;
- private int triggeredRuns;
+
+ @JsonProperty(value = "triggeredRuns", access = JsonProperty.Access.WRITE_ONLY)
+ private int legacyTriggeredRuns;
public JobRunBookkeeping() {}
+ public int getLegacyTriggeredRuns() { return legacyTriggeredRuns; }
+ public void setLegacyTriggeredRuns(int legacyTriggeredRuns) { this.legacyTriggeredRuns = legacyTriggeredRuns; }
+
public long getCompletionOrder() { return completionOrder; }
public void setCompletionOrder(long completionOrder) { this.completionOrder = completionOrder; }
public String getOriginRunId() { return originRunId; }
public void setOriginRunId(String originRunId) { this.originRunId = originRunId; }
- public int getTriggeredRuns() { return triggeredRuns; }
- public void setTriggeredRuns(int triggeredRuns) { this.triggeredRuns = triggeredRuns; }
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/glue/model/TriggerChainBudget.java b/src/main/java/io/github/hectorvent/floci/services/glue/model/TriggerChainBudget.java
new file mode 100644
index 0000000000..d4a8297d60
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/glue/model/TriggerChainBudget.java
@@ -0,0 +1,23 @@
+package io.github.hectorvent.floci.services.glue.model;
+
+import io.quarkus.runtime.annotations.RegisterForReflection;
+
+import java.time.Instant;
+
+/**
+ * Floci's own bookkeeping, never sent on the wire: how many runs triggers have started on behalf of one
+ * chain origin, and when that count last changed (the least recently used chains are forgotten first).
+ */
+@RegisterForReflection
+public class TriggerChainBudget {
+ private int triggeredRuns;
+ private Instant lastUsed;
+
+ public TriggerChainBudget() {}
+
+ public int getTriggeredRuns() { return triggeredRuns; }
+ public void setTriggeredRuns(int triggeredRuns) { this.triggeredRuns = triggeredRuns; }
+
+ public Instant getLastUsed() { return lastUsed; }
+ public void setLastUsed(Instant lastUsed) { this.lastUsed = lastUsed; }
+}
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 ac519dd348..7b227578e2 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
@@ -50,7 +50,7 @@ class GlueJsonHandlerEmptyListTest {
GlueCrawlerRunService crawlerRunService =
new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
handler = new GlueJsonHandler(glueService, jobRunService, crawlerRunService,
- new GlueTriggerService(new InMemoryStorage<>(), glueService,
+ new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService,
jobRunService, crawlerRunService),
schemaRegistryService, mapper);
}
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 0f574d5d73..c370eb5d13 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
@@ -51,7 +51,7 @@ class GlueJsonHandlerTest {
GlueCrawlerRunService crawlerRunService =
new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 0, Clock.systemUTC());
handler = new GlueJsonHandler(glueService, jobRunService, crawlerRunService,
- new GlueTriggerService(new InMemoryStorage<>(), glueService,
+ new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService,
jobRunService, crawlerRunService),
schemaRegistryService, mapper);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/glue/GlueTriggerServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/glue/GlueTriggerServiceTest.java
index 71ccb2b855..6ed40a69ba 100644
--- a/src/test/java/io/github/hectorvent/floci/services/glue/GlueTriggerServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/glue/GlueTriggerServiceTest.java
@@ -1,21 +1,26 @@
package io.github.hectorvent.floci.services.glue;
import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
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.CrawlerRunRecord;
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.FinishedCrawl;
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.JobRunBookkeeping;
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.TriggerChainBudget;
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;
@@ -38,6 +43,7 @@ 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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -62,7 +68,7 @@ class GlueTriggerServiceTest {
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);
+ triggers = new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, jobRuns, crawls);
for (String job : List.of("extract", "load", "report")) {
createJob(job);
}
@@ -317,6 +323,137 @@ class GlueTriggerServiceTest {
assertEquals(1 + GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN, runsOf("extract").size());
}
+ /** A run that could not start gives its claim back, so a failing action does not shorten the chain. */
+ @Test
+ void actionsThatFailToStartDoNotUseUpTheBudget() {
+ GlueCrawlerRunService slowCrawls = new GlueCrawlerRunService(new InMemoryStorage<>(), glueService, 600,
+ new MutableClock());
+ GlueTriggerService loopTriggers = new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(),
+ glueService, jobRuns, slowCrawls);
+ slowCrawls.startCrawler("raw");
+ Trigger loop = conditional("loop", null, List.of(jobIs("extract", "SUCCEEDED")), "extract");
+ TriggerAction crawl = new TriggerAction();
+ crawl.setCrawlerName("raw");
+ loop.setActions(List.of(crawl, startJob("extract")));
+ loopTriggers.createTrigger(loop, true, null, REGION);
+
+ jobRuns.startJobRun("extract", null, new JobRun());
+ for (int request = 0; request < 20; request++) {
+ loopTriggers.fireConditionalTriggers();
+ }
+
+ assertEquals(1 + GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN, runsOf("extract").size());
+ }
+
+ /** The chain's count does not live on the origin, so deleting the origin's crawler does not end the chain. */
+ @Test
+ void aChainKeepsFiringAfterItsOriginCrawlerIsDeleted() {
+ MutableClock clock = new MutableClock();
+ GlueJobRunService slowRuns = new GlueJobRunService(new InMemoryStorage<>(), new InMemoryStorage<>(),
+ glueService, 60, clock);
+ GlueTriggerService chain = new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(),
+ glueService, slowRuns, crawls);
+ chain.createTrigger(conditional("after-crawl", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "extract"),
+ true, null, REGION);
+ chain.createTrigger(conditional("after-extract", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
+ true, null, REGION);
+ crawls.startCrawler("raw");
+ chain.fireConditionalTriggers();
+ assertEquals(1, slowRuns.getJobRuns("extract", null, null).items().size());
+
+ chain.deleteTrigger("after-crawl", REGION);
+ crawls.deleteCrawler("raw", REGION);
+ clock.advance(Duration.ofSeconds(61));
+ chain.fireConditionalTriggers();
+
+ assertEquals(1, slowRuns.getJobRuns("load", null, null).items().size());
+ }
+
+ /** Runs persisted before origins were recorded finish as their own origins and still fire triggers. */
+ @Test
+ void runsPersistedBeforeOriginsWereRecordedStillFireTriggers() {
+ MutableClock clock = new MutableClock();
+ InMemoryStorage<String, JobRunBookkeeping> bookkeeping = new InMemoryStorage<>();
+ GlueJobRunService slowRuns = new GlueJobRunService(new InMemoryStorage<>(), bookkeeping, glueService, 60, clock);
+ InMemoryStorage<String, CrawlerRunRecord> crawlRecords = new InMemoryStorage<>();
+ GlueCrawlerRunService slowCrawls = new GlueCrawlerRunService(crawlRecords, glueService, 60, clock);
+ GlueTriggerService legacy = new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(),
+ glueService, slowRuns, slowCrawls);
+ legacy.createTrigger(conditional("after-extract", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
+ true, null, REGION);
+ legacy.createTrigger(conditional("after-crawl", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "report"),
+ true, null, REGION);
+
+ String runId = slowRuns.startJobRun("extract", null, new JobRun()).getId();
+ bookkeeping.delete(runId);
+ slowCrawls.startCrawler("raw");
+ CrawlerRunRecord record = crawlRecords.get("raw").orElseThrow();
+ record.setCurrentCrawlId(null);
+ record.setCurrentOriginRunId(null);
+ crawlRecords.put("raw", record);
+ clock.advance(Duration.ofSeconds(61));
+ legacy.fireConditionalTriggers();
+
+ assertEquals(1, slowRuns.getJobRuns("load", null, null).items().size());
+ assertEquals(1, slowRuns.getJobRuns("report", null, null).items().size());
+ }
+
+ /** Records persisted before the count moved keep it readable, and it is never written back to them. */
+ @Test
+ void aLegacyRunCountIsReadButNotWritten() throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ JobRunBookkeeping legacy = mapper.readValue(
+ "{\"originRunId\":\"jr_a\",\"completionOrder\":3,\"triggeredRuns\":42}", JobRunBookkeeping.class);
+ FinishedCrawl legacyCrawl = mapper.readValue(
+ "{\"crawlId\":\"crawl:x\",\"sequence\":1,\"triggeredRuns\":7}", FinishedCrawl.class);
+
+ assertEquals(42, legacy.getLegacyTriggeredRuns());
+ assertEquals(7, legacyCrawl.getLegacyTriggeredRuns());
+ assertFalse(mapper.writeValueAsString(legacy).contains("riggeredRuns"));
+ assertFalse(mapper.writeValueAsString(legacyCrawl).contains("riggeredRuns"));
+ }
+
+ /** A chain in flight across the upgrade carries on from the count its origin recorded. */
+ @Test
+ void aChainCountedBeforeTheUpgradeCarriesOnFromItsCount() {
+ MutableClock clock = new MutableClock();
+ InMemoryStorage<String, JobRunBookkeeping> bookkeeping = new InMemoryStorage<>();
+ GlueJobRunService slowRuns = new GlueJobRunService(new InMemoryStorage<>(), bookkeeping, glueService, 60, clock);
+ GlueTriggerService upgraded = new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(),
+ glueService, slowRuns, crawls);
+ upgraded.createTrigger(conditional("loop", null, List.of(jobIs("extract", "SUCCEEDED")), "extract"),
+ true, null, REGION);
+ String origin = slowRuns.startJobRun("extract", null, new JobRun()).getId();
+ JobRunBookkeeping record = bookkeeping.get(origin).orElseThrow();
+ record.setLegacyTriggeredRuns(GlueTriggerService.MAX_TRIGGERED_RUNS_PER_ORIGIN - 1);
+ bookkeeping.put(origin, record);
+
+ for (int request = 0; request < 5; request++) {
+ clock.advance(Duration.ofSeconds(61));
+ upgraded.fireConditionalTriggers();
+ }
+
+ assertEquals(2, slowRuns.getJobRuns("extract", null, null).items().size());
+ }
+
+ @Test
+ void theBudgetStoreKeepsAtMostTheTrackedChains() {
+ int cap = 20;
+ InMemoryStorage<String, TriggerChainBudget> budgets = new InMemoryStorage<>();
+ GlueTriggerService bounded = new GlueTriggerService(new InMemoryStorage<>(), budgets, glueService, jobRuns,
+ crawls, cap);
+ bounded.createTrigger(conditional("after", null, List.of(jobIs("extract", "SUCCEEDED")), "load"),
+ true, null, REGION);
+
+ for (int origin = 0; origin < cap + 5; origin++) {
+ jobRuns.startJobRun("extract", null, new JobRun());
+ bounded.fireConditionalTriggers();
+ }
+
+ assertEquals(cap, budgets.keys().size());
+ assertEquals(cap + 5, runsOf("load").size());
+ }
+
/** Every crawl has its own id, so a crawler deleted and created again never inherits old bookkeeping. */
@Test
void aRecreatedCrawlerStartsANewChainWithItsOwnBudget() {
@@ -496,7 +633,7 @@ class GlueTriggerServiceTest {
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);
+ GlueTriggerService slowTriggers = new GlueTriggerService(new InMemoryStorage<>(), 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());
@@ -515,7 +652,7 @@ class GlueTriggerServiceTest {
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);
+ GlueTriggerService frozenTriggers = new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, frozenRuns, crawls);
frozenTriggers.createTrigger(conditional("each", "ANY", List.of(jobIs("extract", "SUCCEEDED")), "load"),
true, null, REGION);
@@ -532,7 +669,7 @@ class GlueTriggerServiceTest {
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);
+ new GlueTriggerService(new InMemoryStorage<>(), new InMemoryStorage<>(), glueService, jobRuns, frozenCrawls);
frozenTriggers.createTrigger(conditional("each", "ANY", List.of(crawlIs("raw", "SUCCEEDED")), "load"),
true, null, REGION);