feat(glue): create and run workflows (#4471)

Glue had no workflows: CreateWorkflow, StartWorkflowRun and the rest
answered InvalidAction. Add CreateWorkflow, GetWorkflow, ListWorkflows,
BatchGetWorkflows, UpdateWorkflow, DeleteWorkflow, StartWorkflowRun,
StopWorkflowRun, ResumeWorkflowRun, GetWorkflowRun, GetWorkflowRuns,
GetWorkflowRunProperties and PutWorkflowRunProperties, and let triggers
belong to a workflow.

StartWorkflowRun fires the workflow's starting triggers inside a new
run. The workflow's conditional triggers fire only on job runs and
crawls of that run, evaluate AND within it, and start their actions in
it; job runs get --WORKFLOW_NAME and --WORKFLOW_RUN_ID. A run completes
once nothing in it runs and nothing more fires. GetWorkflow and
GetWorkflowRun report the graph of trigger, job and crawler nodes.

Closes #4380
This commit is contained in:
David authored and GitHub committed 2026-09-28 09:26:50 -04:00
1 parent b8b5fcd476
commit bc15516cc0
20 files changed
+1982 -66

No files matched your search

@@ -49,6 +49,9 @@ public class GlueCrawlerRunService {
private static final int MAX_RUNTIME_HISTORY = 100;
private final StorageBackend<String, CrawlerRunRecord> recordStore;
// Workflow run id to the crawls that ran in it. Kept apart from each crawler's recent history, which
// only holds its latest crawls, so an older workflow run keeps its crawls; cleared with the workflow.
private final StorageBackend<String, List<FinishedCrawl>> workflowCrawlStore;
private final GlueService glueService;
private final int runDurationSeconds;
private final Clock clock;
@@ -56,31 +59,34 @@ public class GlueCrawlerRunService {
@Inject
public GlueCrawlerRunService(StorageFactory storageFactory, GlueService glueService, EmulatorConfig config) {
this(storageFactory.create("glue", "crawler_runs.json", new TypeReference<>() {}),
storageFactory.create("glue", "workflow_run_crawls.json", new TypeReference<>() {}),
glueService, config.services().glue().crawlerRunDurationSeconds(), Clock.systemUTC());
}
GlueCrawlerRunService(StorageBackend<String, CrawlerRunRecord> recordStore, GlueService glueService,
GlueCrawlerRunService(StorageBackend<String, CrawlerRunRecord> recordStore,
StorageBackend<String, List<FinishedCrawl>> workflowCrawlStore, GlueService glueService,
int runDurationSeconds, Clock clock) {
if (runDurationSeconds < 0) {
throw new IllegalArgumentException(
"floci.services.glue.crawler-run-duration-seconds must not be negative: " + runDurationSeconds);
}
this.recordStore = recordStore;
this.workflowCrawlStore = workflowCrawlStore;
this.glueService = glueService;
this.runDurationSeconds = runDurationSeconds;
this.clock = clock;
}
public synchronized String startCrawler(String name) {
return startCrawler(name, null);
return startCrawler(name, null, null);
}
/**
* Starts a crawl and returns its crawl id ({@code crawl:} and a UUID, so ids never repeat, even for
* a crawler deleted and created again). A trigger passes the origin of its chain; a null origin
* makes the crawl its own origin.
* a crawler deleted and created again). A trigger passes the origin of its chain (a null origin
* makes the crawl its own origin) and, when it belongs to a workflow, the workflow run.
*/
public synchronized String startCrawler(String name, String originRunId) {
public synchronized String startCrawler(String name, String originRunId, String workflowRunId) {
glueService.getCrawler(name);
CrawlerRunRecord record = settledRecord(name);
if (record.getCurrentStart() != null) {
@@ -91,6 +97,7 @@ public class GlueCrawlerRunService {
record.setCurrentMessagePrefix(UUID.randomUUID().toString());
record.setCurrentCrawlId(crawlId);
record.setCurrentOriginRunId(originRunId != null ? originRunId : crawlId);
record.setCurrentWorkflowRunId(workflowRunId);
recordStore.put(name, record);
LOG.infov("Started Glue crawler {0}", name);
settle(record);
@@ -175,7 +182,7 @@ public class GlueCrawlerRunService {
for (FinishedCrawl crawl : record.getRecentCrawls()) {
if (crawl.getSequence() > after) {
completions.add(new GlueRunCompletion(Long.toString(crawl.getSequence()), crawl.getFinishedAt(),
crawl.getStatus(), originOf(name, crawl)));
crawl.getStatus(), originOf(name, crawl), crawl.getWorkflowRunId()));
}
}
return completions;
@@ -207,6 +214,48 @@ public class GlueCrawlerRunService {
return CRAWL_ID_PREFIX + crawlerName + ":" + crawl.getSequence();
}
/** A crawl of a workflow run as the workflow graph reports it: the {@code Crawl} structure. */
public record WorkflowCrawl(String crawlerName, String state, Instant startedOn, Instant completedOn) {}
/** The crawls, settled, that belong to a workflow run: finished ones and a crawl still running. */
public synchronized List<WorkflowCrawl> crawlsInWorkflowRun(String workflowRunId) {
List<WorkflowCrawl> running = new ArrayList<>();
for (CrawlerRunRecord stored : recordStore.scan(key -> true)) {
CrawlerRunRecord record = settledRecord(stored.getCrawlerName());
if (record.getCurrentStart() != null && workflowRunId.equals(record.getCurrentWorkflowRunId())) {
running.add(new WorkflowCrawl(record.getCrawlerName(), STATE_RUNNING, record.getCurrentStart(), null));
}
}
List<WorkflowCrawl> crawls = new ArrayList<>();
for (FinishedCrawl crawl : workflowCrawlStore.get(workflowRunId).orElse(List.of())) {
crawls.add(new WorkflowCrawl(crawl.getCrawlerName(), crawl.getStatus(), crawl.getStartedAt(),
crawl.getFinishedAt()));
}
crawls.addAll(running);
crawls.sort(Comparator.comparing(WorkflowCrawl::startedOn));
return crawls;
}
/**
* Forgets the crawls of a workflow run that no longer exists, and detaches a crawl still running in
* it, so that crawl does not record history under the removed run when it finishes.
*/
public synchronized void forgetWorkflowRun(String workflowRunId) {
workflowCrawlStore.delete(workflowRunId);
for (CrawlerRunRecord record : recordStore.scan(key -> true)) {
if (workflowRunId.equals(record.getCurrentWorkflowRunId())) {
record.setCurrentWorkflowRunId(null);
recordStore.put(record.getCrawlerName(), record);
}
}
}
/** Whether the crawler is crawling now, after settling a crawl that has run its time. */
public synchronized boolean isRunning(String name) {
glueService.getCrawler(name);
return settledRecord(name).getCurrentStart() != null;
}
public synchronized void updateCrawler(Crawler update) {
requireNotRunning(update.getName());
glueService.updateCrawler(update);
@@ -300,6 +349,14 @@ public class GlueCrawlerRunService {
finished.setStatus(status);
finished.setCrawlId(record.getCurrentCrawlId());
finished.setOriginRunId(record.getCurrentOriginRunId());
finished.setWorkflowRunId(record.getCurrentWorkflowRunId());
finished.setCrawlerName(record.getCrawlerName());
if (finished.getWorkflowRunId() != null) {
List<FinishedCrawl> ofRun = new ArrayList<>(
workflowCrawlStore.get(finished.getWorkflowRunId()).orElse(List.of()));
ofRun.add(finished);
workflowCrawlStore.put(finished.getWorkflowRunId(), ofRun);
}
List<FinishedCrawl> recent = new ArrayList<>(record.getRecentCrawls());
recent.add(finished);
if (recent.size() > MAX_RUNTIME_HISTORY) {
@@ -314,6 +371,7 @@ public class GlueCrawlerRunService {
record.setCurrentMessagePrefix(null);
record.setCurrentCrawlId(null);
record.setCurrentOriginRunId(null);
record.setCurrentWorkflowRunId(null);
recordStore.put(record.getCrawlerName(), record);
}
}
@@ -86,15 +86,16 @@ public class GlueJobRunService {
* unset field falls back to the job definition.
*/
public synchronized JobRun startJobRun(String jobName, String previousRunId, JobRun overrides) {
return startJobRun(jobName, previousRunId, overrides, null);
return startJobRun(jobName, previousRunId, overrides, null, null);
}
/**
* As {@link #startJobRun(String, String, JobRun)}, for a run a trigger starts on behalf of
* {@code originRunId}; a null origin makes the run its own origin.
* {@code originRunId} (a null origin makes the run its own origin), inside {@code workflowRunId}
* when the trigger belongs to a workflow.
*/
public synchronized JobRun startJobRun(String jobName, String previousRunId, JobRun overrides,
String originRunId) {
String originRunId, String workflowRunId) {
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());
@@ -142,6 +143,7 @@ public class GlueJobRunService {
runStore.put(run.getId(), run);
JobRunBookkeeping bookkeeping = new JobRunBookkeeping();
bookkeeping.setOriginRunId(originRunId != null ? originRunId : run.getId());
bookkeeping.setWorkflowRunId(workflowRunId);
bookkeepingStore.put(run.getId(), bookkeeping);
LOG.infov("Started Glue job run {0} for job {1}", run.getId(), jobName);
return settle(run);
@@ -212,8 +214,9 @@ public class GlueJobRunService {
if (isTerminal(run)) {
String position = positionOf(run);
if (afterPosition == null || afterPosition.isEmpty() || position.compareTo(afterPosition) > 0) {
JobRunBookkeeping bookkeeping = bookkeepingOf(run.getId());
completions.add(new GlueRunCompletion(position, run.getCompletedOn(), run.getJobRunState(),
bookkeepingOf(run.getId()).getOriginRunId()));
bookkeeping.getOriginRunId(), bookkeeping.getWorkflowRunId()));
}
}
}
@@ -231,6 +234,31 @@ public class GlueJobRunService {
return bookkeepingStore.get(originRunId).map(JobRunBookkeeping::getLegacyTriggeredRuns).orElse(0);
}
/**
* Whether a run of the job could start now without {@code ConcurrentRunsExceededException}, as
* {@link #startJobRun} decides it for a run that does not override JobRunQueuingEnabled.
*/
public synchronized boolean hasCapacity(String jobName) {
Job job = glueService.getJob(jobName);
if (Boolean.TRUE.equals(job.getJobRunQueuingEnabled())) {
return true;
}
long active = runsOf(jobName).stream().filter(run -> !isTerminal(settle(run))).count();
return active < maxConcurrentRuns(job);
}
/** The runs, settled, that belong to a workflow run, oldest first. */
public synchronized List<JobRun> runsInWorkflowRun(String workflowRunId) {
List<JobRun> runs = new ArrayList<>();
for (JobRun run : runStore.scan(key -> true)) {
if (workflowRunId.equals(bookkeepingOf(run.getId()).getWorkflowRunId())) {
runs.add(settle(run));
}
}
runs.sort(Comparator.comparing(JobRun::getStartedOn).thenComparing(JobRun::getId));
return runs;
}
/** 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)) {
@@ -35,6 +35,8 @@ public class GlueJsonHandler {
"StartJobRun", "GetJobRun", "GetJobRuns", "BatchStopJobRun",
"StartCrawler", "StopCrawler", "GetCrawler", "GetCrawlers", "BatchGetCrawlers", "GetCrawlerMetrics",
"UpdateCrawler",
"StartWorkflowRun", "StopWorkflowRun", "ResumeWorkflowRun", "GetWorkflowRun", "GetWorkflowRuns",
"GetWorkflow", "BatchGetWorkflows",
"StartTrigger");
private final GlueService glueService;
@@ -515,10 +517,91 @@ public class GlueJsonHandler {
triggerService.stopTrigger(name);
yield Response.ok(Map.of("Name", name)).build();
}
case "CreateWorkflow" -> {
Workflow workflow = new Workflow();
workflow.setName(request.path("Name").asText(null));
workflow.setDescription(request.path("Description").asText(null));
workflow.setDefaultRunProperties(readStringMap(request, "DefaultRunProperties"));
if (request.hasNonNull("MaxConcurrentRuns")) {
workflow.setMaxConcurrentRuns(request.get("MaxConcurrentRuns").asInt());
}
glueService.createWorkflow(workflow, readStringMap(request, "Tags"), region);
yield Response.ok(Map.of("Name", workflow.getName())).build();
}
case "GetWorkflow" -> Response.ok(Map.of("Workflow", triggerService.workflowView(
glueService.getWorkflow(request.path("Name").asText(null)),
request.path("IncludeGraph").asBoolean(false)))).build();
case "ListWorkflows" -> {
GlueService.Page<String> page = glueService.listWorkflows(readMaxResults(request), readNextToken(request));
yield Response.ok(pageResponse("Workflows", page.items(), page.nextToken())).build();
}
case "BatchGetWorkflows" -> {
List<String> names = request.hasNonNull("Names")
? mapper.convertValue(request.get("Names"), STRING_LIST)
: null;
GlueService.BatchGetWorkflowsResult result = glueService.batchGetWorkflows(names);
List<Workflow> workflows = new ArrayList<>();
for (Workflow workflow : result.workflows()) {
workflows.add(triggerService.workflowView(workflow, request.path("IncludeGraph").asBoolean(false)));
}
Map<String, Object> response = new LinkedHashMap<>();
response.put("Workflows", workflows);
response.put("MissingWorkflows", result.missingWorkflows());
yield Response.ok(response).build();
}
case "UpdateWorkflow" -> {
String name = request.path("Name").asText(null);
glueService.updateWorkflow(name, request.path("Description").asText(null),
readStringMap(request, "DefaultRunProperties"),
request.hasNonNull("MaxConcurrentRuns") ? request.get("MaxConcurrentRuns").asInt() : null);
yield Response.ok(Map.of("Name", name)).build();
}
case "DeleteWorkflow" -> {
String name = request.path("Name").asText(null);
triggerService.deleteWorkflow(name, region);
yield Response.ok(Map.of("Name", name)).build();
}
case "StartWorkflowRun" -> Response.ok(Map.of("RunId", triggerService.startWorkflowRun(
request.path("Name").asText(null), readStringMap(request, "RunProperties")))).build();
case "StopWorkflowRun" -> {
triggerService.stopWorkflowRun(request.path("Name").asText(null), request.path("RunId").asText(null));
yield Response.ok(Map.of()).build();
}
case "ResumeWorkflowRun" -> {
List<String> nodeIds = request.hasNonNull("NodeIds")
? mapper.convertValue(request.get("NodeIds"), STRING_LIST)
: null;
GlueTriggerService.ResumedRun resumed = triggerService.resumeWorkflowRun(
request.path("Name").asText(null), request.path("RunId").asText(null), nodeIds);
Map<String, Object> response = new LinkedHashMap<>();
response.put("RunId", resumed.runId());
response.put("NodeIds", resumed.nodeIds());
yield Response.ok(response).build();
}
case "GetWorkflowRun" -> Response.ok(Map.of("Run", triggerService.getWorkflowRun(
request.path("Name").asText(null), request.path("RunId").asText(null),
request.path("IncludeGraph").asBoolean(false)))).build();
case "GetWorkflowRuns" -> {
GlueService.Page<WorkflowRun> page = triggerService.getWorkflowRuns(request.path("Name").asText(null),
request.path("IncludeGraph").asBoolean(false), readMaxResults(request), readNextToken(request));
yield Response.ok(pageResponse("Runs", page.items(), page.nextToken())).build();
}
case "GetWorkflowRunProperties" -> Response.ok(Map.of("RunProperties",
triggerService.getWorkflowRunProperties(request.path("Name").asText(null),
request.path("RunId").asText(null)))).build();
case "PutWorkflowRunProperties" -> {
triggerService.putWorkflowRunProperties(request.path("Name").asText(null),
request.path("RunId").asText(null), readStringMap(request, "RunProperties"));
yield Response.ok(Map.of()).build();
}
default -> throw new AwsException("InvalidAction", "Action " + action + " is not supported", 400);
};
}
private Map<String, String> readStringMap(JsonNode request, String field) {
return request.hasNonNull(field) ? mapper.convertValue(request.get(field), STRING_MAP) : null;
}
/** The definition members CreateTrigger and TriggerUpdate share. */
private Trigger toTrigger(JsonNode node) throws Exception {
Trigger trigger = new Trigger();
@@ -6,6 +6,8 @@ import java.time.Instant;
* One finished job run or crawl, as a conditional trigger sees it. {@code position} orders the
* completions of one job or crawler; a trigger remembers the last position it has processed.
* {@code originRunId} is the run that set off the trigger chain this run belongs to (the run itself
* when no trigger started it): a job run id, or a crawl id beginning {@code crawl:}.
* when no trigger started it): a job run id, or a crawl id beginning {@code crawl:}. {@code workflowRunId}
* is the workflow run the run or crawl belongs to, or null.
*/
public record GlueRunCompletion(String position, Instant finishedAt, String state, String originRunId) {}
public record GlueRunCompletion(String position, Instant finishedAt, String state, String originRunId,
String workflowRunId) {}
@@ -36,6 +36,7 @@ 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.model.UserDefinedFunction;
import io.github.hectorvent.floci.services.glue.model.Workflow;
import io.github.hectorvent.floci.services.glue.schemaregistry.GlueSchemaRegistryService;
import io.github.hectorvent.floci.services.glue.schemaregistry.SchemaToColumnsConverter;
import io.github.hectorvent.floci.services.glue.schemaregistry.model.SchemaId;
@@ -163,6 +164,8 @@ public class GlueService {
private static final Set<String> CONDITION_JOB_STATES = Set.of("SUCCEEDED", "STOPPED", "FAILED", "TIMEOUT");
private static final Set<String> CONDITION_CRAWL_STATES = Set.of("SUCCEEDED", "CANCELLED", "FAILED");
private static final int MAX_TRIGGERS_PAGE_SIZE = 200;
private static final int MAX_WORKFLOWS_PAGE_SIZE = 25;
private static final int MAX_BATCH_GET_WORKFLOWS = 25;
// BatchGetCrawlers takes a CrawlerNameList, which the API model caps at 100 names.
private static final int MAX_BATCH_GET_CRAWLERS = 100;
@@ -182,6 +185,7 @@ public class GlueService {
private final StorageBackend<String, DataCatalogEncryptionSettings> encryptionSettingsStore;
private final StorageBackend<String, SecurityConfiguration> securityConfigurationStore;
private final StorageBackend<String, Trigger> triggerStore;
private final StorageBackend<String, Workflow> workflowStore;
private final GlueSchemaRegistryService schemaRegistryService;
private final RegionResolver regionResolver;
private final ResourceGroupsTaggingService resourceGroupsTaggingService;
@@ -213,6 +217,7 @@ public class GlueService {
this.securityConfigurationStore = storageFactory.create(
"glue", "security_configurations.json", new TypeReference<>() {});
this.triggerStore = storageFactory.create("glue", "triggers.json", new TypeReference<>() {});
this.workflowStore = storageFactory.create("glue", "workflows.json", new TypeReference<>() {});
this.schemaRegistryService = schemaRegistryService;
this.regionResolver = regionResolver;
this.resourceGroupsTaggingService = resourceGroupsTaggingService;
@@ -235,6 +240,7 @@ public class GlueService {
StorageBackend<String, DataCatalogEncryptionSettings> encryptionSettingsStore,
StorageBackend<String, SecurityConfiguration> securityConfigurationStore,
StorageBackend<String, Trigger> triggerStore,
StorageBackend<String, Workflow> workflowStore,
GlueSchemaRegistryService schemaRegistryService,
RegionResolver regionResolver,
ResourceGroupsTaggingService resourceGroupsTaggingService,
@@ -255,6 +261,7 @@ public class GlueService {
this.encryptionSettingsStore = encryptionSettingsStore;
this.securityConfigurationStore = securityConfigurationStore;
this.triggerStore = triggerStore;
this.workflowStore = workflowStore;
this.schemaRegistryService = schemaRegistryService;
this.regionResolver = regionResolver;
this.resourceGroupsTaggingService = resourceGroupsTaggingService;
@@ -1519,6 +1526,10 @@ public class GlueService {
return regionResolver.buildArn("glue", region, "trigger/" + triggerName);
}
private String workflowArn(String region, String workflowName) {
return regionResolver.buildArn("glue", region, "workflow/" + workflowName);
}
private static Pattern compileFunctionPattern(String pattern) {
if (pattern == null) {
return Pattern.compile(".*");
@@ -2166,6 +2177,108 @@ public class GlueService {
crawlerStore.put(name, crawler);
}
// ---- Workflows --------------------------------------------------------------------------
public void createWorkflow(Workflow workflow, Map<String, String> tags, String region) {
validateRequired(workflow.getName(), "Name");
validateWorkflowConcurrency(workflow.getMaxConcurrentRuns());
String name = workflow.getName();
if (workflowStore.get(name).isPresent()) {
throw new AwsException("AlreadyExistsException", "Workflow " + name + " already exists.", 400);
}
Instant now = Instant.now();
workflow.setCreatedOn(now);
workflow.setLastModifiedOn(now);
workflowStore.put(name, workflow);
if (tags != null && !tags.isEmpty()) {
resourceGroupsTaggingService.tagResources(List.of(workflowArn(region, name)), tags, region);
}
LOG.infov("Created Glue workflow {0}", name);
}
public Workflow getWorkflow(String name) {
validateRequired(name, "Name");
return workflowStore.get(name)
.orElseThrow(() -> new AwsException("EntityNotFoundException", "Workflow " + name + " not found.", 400));
}
public Page<String> listWorkflows(Integer maxResults, String nextToken) {
if (maxResults != null && (maxResults < 1 || maxResults > MAX_WORKFLOWS_PAGE_SIZE)) {
throw new AwsException("InvalidInputException",
"MaxResults must be between 1 and " + MAX_WORKFLOWS_PAGE_SIZE, 400);
}
List<String> names = new ArrayList<>();
for (Workflow workflow : workflowStore.scan(k -> true)) {
names.add(workflow.getName());
}
names.sort(Comparator.naturalOrder());
return paginate(names, maxResults, nextToken);
}
public BatchGetWorkflowsResult batchGetWorkflows(List<String> names) {
if (names == null || names.isEmpty() || names.size() > MAX_BATCH_GET_WORKFLOWS) {
throw new AwsException("InvalidInputException",
"Names must contain between 1 and " + MAX_BATCH_GET_WORKFLOWS + " names.", 400);
}
List<Workflow> workflows = new ArrayList<>();
List<String> missing = new ArrayList<>();
for (String name : names) {
Optional<Workflow> workflow = name == null ? Optional.empty() : workflowStore.get(name);
if (workflow.isPresent()) {
workflows.add(workflow.get());
} else {
missing.add(name);
}
}
return new BatchGetWorkflowsResult(workflows, missing);
}
public record BatchGetWorkflowsResult(List<Workflow> workflows, List<String> missingWorkflows) {}
/** Applies the members an UpdateWorkflow request sets; the rest of the workflow is kept. */
public void updateWorkflow(String name, String description, Map<String, String> defaultRunProperties,
Integer maxConcurrentRuns) {
Workflow workflow = getWorkflow(name);
validateWorkflowConcurrency(maxConcurrentRuns);
if (description != null) {
workflow.setDescription(description);
}
if (defaultRunProperties != null) {
workflow.setDefaultRunProperties(defaultRunProperties);
}
if (maxConcurrentRuns != null) {
workflow.setMaxConcurrentRuns(maxConcurrentRuns);
}
workflow.setLastModifiedOn(Instant.now());
workflowStore.put(name, workflow);
}
/**
* DeleteWorkflow declares no EntityNotFoundException: deleting a missing workflow succeeds. The
* workflow's triggers are left in place, as the API does not say they are removed.
*/
public void deleteWorkflow(String name, String region) {
validateRequired(name, "Name");
workflowStore.delete(name);
resourceGroupsTaggingService.deleteResources(List.of(workflowArn(region, name)), region);
}
public List<Trigger> triggersOfWorkflow(String workflowName) {
List<Trigger> triggers = new ArrayList<>();
for (Trigger trigger : allTriggers()) {
if (workflowName.equals(trigger.getWorkflowName())) {
triggers.add(trigger);
}
}
return triggers;
}
private static void validateWorkflowConcurrency(Integer maxConcurrentRuns) {
if (maxConcurrentRuns != null && maxConcurrentRuns < 1) {
throw new AwsException("InvalidInputException", "MaxConcurrentRuns must be at least 1.", 400);
}
}
// ---- Triggers ---------------------------------------------------------------------------
public void createTrigger(Trigger trigger, Map<String, String> tags, String region) {
@@ -2175,10 +2288,8 @@ public class GlueService {
throw new AwsException("InvalidInputException", "Invalid trigger type: " + trigger.getType(), 400);
}
if (trigger.getWorkflowName() != null) {
throw new AwsException("EntityNotFoundException",
"Workflow " + trigger.getWorkflowName() + " not found.", 400);
}
if ("EVENT".equals(trigger.getType())) {
getWorkflow(trigger.getWorkflowName());
} else if ("EVENT".equals(trigger.getType())) {
throw new AwsException("InvalidInputException", "An EVENT trigger must belong to a workflow.", 400);
}
validateTriggerDefinition(trigger);
@@ -2830,6 +2941,9 @@ public class GlueService {
} else if (resource.startsWith("trigger/")) {
getTrigger(resource.substring(8));
return;
} else if (resource.startsWith("workflow/")) {
getWorkflow(resource.substring(9));
return;
} else if (resource.startsWith("connection/")) {
getConnection(resource.substring(11), false);
return;
@@ -10,18 +10,24 @@ 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.model.Workflow;
import io.github.hectorvent.floci.services.glue.model.WorkflowRun;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
* Glue trigger lifecycle and firing. An ON_DEMAND trigger fires when started. A CONDITIONAL trigger,
@@ -31,6 +37,12 @@ import java.util.Set;
* trigger has not seen yet, in finishing order. A SCHEDULED trigger is stored and can be activated,
* but no timer fires it.
*
* <p>This service also runs workflows. StartWorkflowRun fires the workflow's starting (non
* CONDITIONAL) triggers inside a new workflow run; a workflow's CONDITIONAL triggers fire only on
* completions of runs and crawls that belong to a RUNNING run of that workflow, evaluate AND within
* that run, and start their actions inside it. A workflow run is COMPLETED once an evaluation ends
* with nothing left running in it and nothing more to fire.
*
* <p>Every public method is synchronized. This service calls the job run and crawler run services
* and they never call back, so the lock order is always this service first.
*/
@@ -44,6 +56,11 @@ public class GlueTriggerService {
static final String STATE_CREATED = "CREATED";
static final String STATE_ACTIVATED = "ACTIVATED";
static final String STATE_DEACTIVATED = "DEACTIVATED";
static final String STATUS_RUNNING = "RUNNING";
static final String STATUS_COMPLETED = "COMPLETED";
static final String STATUS_STOPPED = "STOPPED";
static final String STATUS_ERROR = "ERROR";
private static final String STATE_JOB_RUNNING = "RUNNING";
// Work done within one request; a longer chain continues on the next run-related request.
private static final int MAX_FIRING_ROUNDS = 25;
@@ -65,6 +82,8 @@ public class GlueTriggerService {
private final StorageBackend<String, Map<String, String>> seenStore;
private final StorageBackend<String, TriggerChainBudget> budgetStore;
private final int maxTrackedChains;
private final Clock clock;
private final StorageBackend<String, WorkflowRun> workflowRunStore;
private final GlueService glueService;
private final GlueJobRunService jobRunService;
private final GlueCrawlerRunService crawlerRunService;
@@ -77,22 +96,28 @@ public class GlueTriggerService {
// than rewritten in full on each claim.
storageFactory.create("glue", "trigger_chain_budgets.json", new TypeReference<>() {},
WriteProfile.APPEND_HEAVY),
storageFactory.create("glue", "workflow_runs.json", new TypeReference<>() {}),
glueService, jobRunService, crawlerRunService);
}
GlueTriggerService(StorageBackend<String, Map<String, String>> seenStore,
StorageBackend<String, TriggerChainBudget> budgetStore, GlueService glueService,
StorageBackend<String, TriggerChainBudget> budgetStore,
StorageBackend<String, WorkflowRun> workflowRunStore, GlueService glueService,
GlueJobRunService jobRunService, GlueCrawlerRunService crawlerRunService) {
this(seenStore, budgetStore, glueService, jobRunService, crawlerRunService, MAX_TRACKED_CHAINS);
this(seenStore, budgetStore, workflowRunStore, glueService, jobRunService, crawlerRunService,
MAX_TRACKED_CHAINS, Clock.systemUTC());
}
GlueTriggerService(StorageBackend<String, Map<String, String>> seenStore,
StorageBackend<String, TriggerChainBudget> budgetStore, GlueService glueService,
StorageBackend<String, TriggerChainBudget> budgetStore,
StorageBackend<String, WorkflowRun> workflowRunStore, GlueService glueService,
GlueJobRunService jobRunService, GlueCrawlerRunService crawlerRunService,
int maxTrackedChains) {
int maxTrackedChains, Clock clock) {
this.seenStore = seenStore;
this.budgetStore = budgetStore;
this.maxTrackedChains = maxTrackedChains;
this.clock = clock;
this.workflowRunStore = workflowRunStore;
this.glueService = glueService;
this.jobRunService = jobRunService;
this.crawlerRunService = crawlerRunService;
@@ -130,7 +155,7 @@ public class GlueTriggerService {
public synchronized void startTrigger(String name) {
Trigger trigger = glueService.getTrigger(name);
if (TYPE_ON_DEMAND.equals(trigger.getType())) {
fire(trigger, null, true);
fire(trigger, null, null, true);
return;
}
boolean wasActive = STATE_ACTIVATED.equals(trigger.getState());
@@ -160,27 +185,505 @@ public class GlueTriggerService {
public synchronized void fireConditionalTriggers() {
for (int round = 0; round < MAX_FIRING_ROUNDS; round++) {
boolean anyFired = false;
Set<String> runningWorkflowRuns = runningWorkflowRunIds();
for (Trigger trigger : glueService.allTriggers()) {
if (!isActiveConditional(trigger)) {
continue;
}
for (GlueRunCompletion cause : newFirings(trigger)) {
anyFired |= fire(trigger, cause.originRunId(), false) > 0;
for (GlueRunCompletion cause : newFirings(trigger, runningWorkflowRuns)) {
anyFired |= fire(trigger, cause.originRunId(),
trigger.getWorkflowName() != null ? cause.workflowRunId() : null, false) > 0;
}
}
if (!anyFired) {
// Nothing more fires, so a workflow run with nothing left running is finished. Not
// checked when the round limit cut the evaluation short: more may still fire then.
completeFinishedWorkflowRuns();
return;
}
}
}
// ---- Workflow runs ----------------------------------------------------------------------
/** Starts a run of the workflow: its starting (non CONDITIONAL) triggers fire inside the new run. */
public synchronized String startWorkflowRun(String workflowName, Map<String, String> runProperties) {
Workflow workflow = glueService.getWorkflow(workflowName);
requireCapacity(workflow);
Map<String, String> properties = new LinkedHashMap<>();
if (workflow.getDefaultRunProperties() != null) {
properties.putAll(workflow.getDefaultRunProperties());
}
if (runProperties != null) {
properties.putAll(runProperties);
}
WorkflowRun run = newWorkflowRun(workflowName, null, properties);
int actions = 0;
int started = 0;
for (Trigger trigger : glueService.triggersOfWorkflow(workflowName)) {
if (!TYPE_CONDITIONAL.equals(trigger.getType())) {
actions += trigger.getActions().size();
started += fire(trigger, null, run.getWorkflowRunId(), false);
}
}
if (actions > 0 && started == 0) {
// Nothing the workflow starts with could be launched; the reasons are in ErrorMessage.
finishWorkflowRun(workflowRunStore.get(run.getWorkflowRunId()).orElse(run), STATUS_ERROR, clock.instant());
return run.getWorkflowRunId();
}
fireConditionalTriggers();
return run.getWorkflowRunId();
}
public synchronized void stopWorkflowRun(String workflowName, String runId) {
findWorkflowRun(workflowName, runId);
// A run whose last action finished since the previous evaluation is COMPLETED, not stoppable.
fireConditionalTriggers();
WorkflowRun run = findWorkflowRun(workflowName, runId);
if (!STATUS_RUNNING.equals(run.getStatus())) {
throw new AwsException("IllegalWorkflowStateException",
"Workflow run " + runId + " is not running; its status is " + run.getStatus(), 400);
}
stopWhatIsRunning(runId);
finishWorkflowRun(run, STATUS_STOPPED, clock.instant());
}
/**
* Starts the given job and crawler nodes of a finished run again in a new run of the workflow, whose
* conditional triggers then carry on downstream. Each node must have run in the original run.
*/
public synchronized ResumedRun resumeWorkflowRun(String workflowName, String runId, List<String> requestedNodeIds) {
WorkflowRun original = findWorkflowRun(workflowName, runId);
if (STATUS_RUNNING.equals(original.getStatus())) {
throw new AwsException("IllegalWorkflowStateException",
"Workflow run " + runId + " is still running.", 400);
}
if (requestedNodeIds == null || requestedNodeIds.isEmpty()) {
throw new AwsException("InvalidInputException", "NodeIds is required.", 400);
}
// A node named twice is restarted once: launching it a second time would collide with the first.
List<String> nodeIds = new ArrayList<>(new LinkedHashSet<>(requestedNodeIds));
Map<String, Map<String, Object>> nodes = new HashMap<>();
for (Map<String, Object> node : graphNodes(workflowName)) {
nodes.put((String) node.get("UniqueId"), node);
}
Set<String> ranJobs = new LinkedHashSet<>();
for (JobRun jobRun : jobRunService.runsInWorkflowRun(runId)) {
ranJobs.add(jobRun.getJobName());
}
Set<String> ranCrawlers = new LinkedHashSet<>();
for (GlueCrawlerRunService.WorkflowCrawl crawl : crawlerRunService.crawlsInWorkflowRun(runId)) {
ranCrawlers.add(crawl.crawlerName());
}
for (String nodeId : nodeIds) {
Map<String, Object> node = nodes.get(nodeId);
boolean ran = node != null && ("JOB".equals(node.get("Type")) ? ranJobs.contains((String) node.get("Name"))
: "CRAWLER".equals(node.get("Type")) && ranCrawlers.contains((String) node.get("Name")));
if (!ran) {
throw new AwsException("InvalidInputException",
"Node " + nodeId + " is not a job or crawler node that ran in workflow run " + runId, 400);
}
}
requireCapacity(glueService.getWorkflow(workflowName));
// Refuse before anything starts when a node cannot launch, so a failed resume normally leaves
// no run and no action behind.
for (String nodeId : nodeIds) {
Map<String, Object> node = nodes.get(nodeId);
String name = (String) node.get("Name");
if ("JOB".equals(node.get("Type")) && !jobRunService.hasCapacity(name)) {
throw new AwsException("ConcurrentRunsExceededException", "Concurrent runs exceeded for " + name, 400);
}
if ("CRAWLER".equals(node.get("Type")) && crawlerRunService.isRunning(name)) {
throw new AwsException("CrawlerRunningException", "Crawler with name " + name + " has already started", 400);
}
}
Map<String, JobRun> originalJobRuns = new HashMap<>();
for (JobRun jobRun : jobRunService.runsInWorkflowRun(runId)) {
originalJobRuns.put(jobRun.getJobName(), jobRun);
}
WorkflowRun resumed = newWorkflowRun(workflowName, runId, original.getWorkflowRunProperties());
String resumedId = resumed.getWorkflowRunId();
try {
for (String nodeId : nodeIds) {
Map<String, Object> node = nodes.get(nodeId);
String name = (String) node.get("Name");
if ("JOB".equals(node.get("Type"))) {
// The job runs again with the arguments it had in the original run, now in the new run.
JobRun before = originalJobRuns.get(name);
JobRun overrides = new JobRun();
overrides.setArguments(withWorkflowArguments(before != null ? before.getArguments() : null, resumedId));
if (before != null) {
overrides.setTimeout(before.getTimeout());
overrides.setSecurityConfiguration(before.getSecurityConfiguration());
overrides.setNotificationProperty(before.getNotificationProperty());
overrides.setTriggerName(before.getTriggerName());
}
jobRunService.startJobRun(name, null, overrides, null, resumedId);
} else {
crawlerRunService.startCrawler(name, null, resumedId);
}
}
} catch (AwsException e) {
// Only reachable if a node became unable to launch after the check above. What already
// started keeps its run, so the run is kept too, stopped and marked ERROR with the reason.
noteLaunchFailure(resumedId, "Resume could not start every node: " + e.getMessage());
stopWhatIsRunning(resumedId);
finishWorkflowRun(workflowRunStore.get(resumedId).orElse(resumed), STATUS_ERROR, clock.instant());
throw e;
}
fireConditionalTriggers();
return new ResumedRun(resumedId, nodeIds);
}
public synchronized WorkflowRun getWorkflowRun(String workflowName, String runId, boolean includeGraph) {
fireConditionalTriggers();
return view(findWorkflowRun(workflowName, runId), includeGraph);
}
public synchronized GlueService.Page<WorkflowRun> getWorkflowRuns(String workflowName, boolean includeGraph,
Integer maxResults, String nextToken) {
glueService.getWorkflow(workflowName);
fireConditionalTriggers();
List<WorkflowRun> runs = new ArrayList<>();
for (WorkflowRun run : runsOfWorkflow(workflowName)) {
runs.add(view(run, includeGraph));
}
runs.sort(Comparator.comparing(WorkflowRun::getStartedOn).reversed()
.thenComparing(WorkflowRun::getWorkflowRunId));
return glueService.paginate(runs, maxResults, nextToken);
}
public synchronized Map<String, String> getWorkflowRunProperties(String workflowName, String runId) {
WorkflowRun run = findWorkflowRun(workflowName, runId);
return run.getWorkflowRunProperties() != null ? run.getWorkflowRunProperties() : Map.of();
}
/** Adds the properties to the run's, replacing any with the same name. */
public synchronized void putWorkflowRunProperties(String workflowName, String runId, Map<String, String> properties) {
if (properties == null) {
throw new AwsException("InvalidInputException", "RunProperties is required.", 400);
}
WorkflowRun run = findWorkflowRun(workflowName, runId);
Map<String, String> merged = new LinkedHashMap<>();
if (run.getWorkflowRunProperties() != null) {
merged.putAll(run.getWorkflowRunProperties());
}
merged.putAll(properties);
run.setWorkflowRunProperties(merged);
workflowRunStore.put(run.getWorkflowRunId(), run);
}
/** The workflow as GetWorkflow and BatchGetWorkflows report it: with LastRun, and the graph if asked. */
public synchronized Workflow workflowView(Workflow stored, boolean includeGraph) {
fireConditionalTriggers();
Workflow workflow = new Workflow();
workflow.setName(stored.getName());
workflow.setDescription(stored.getDescription());
workflow.setDefaultRunProperties(stored.getDefaultRunProperties());
workflow.setCreatedOn(stored.getCreatedOn());
workflow.setLastModifiedOn(stored.getLastModifiedOn());
workflow.setMaxConcurrentRuns(stored.getMaxConcurrentRuns());
runsOfWorkflow(stored.getName()).stream()
.max(Comparator.comparing(WorkflowRun::getStartedOn))
.ifPresent(last -> workflow.setLastRun(view(last, false)));
if (includeGraph) {
workflow.setGraph(graph(stored.getName(), null));
}
return workflow;
}
/** Deletes the workflow and its runs; its triggers stay, as they do not belong to the workflow's storage. */
public synchronized void deleteWorkflow(String workflowName, String region) {
for (WorkflowRun run : runsOfWorkflow(workflowName)) {
workflowRunStore.delete(run.getWorkflowRunId());
forgetWorkflowRunState(run.getWorkflowRunId());
crawlerRunService.forgetWorkflowRun(run.getWorkflowRunId());
}
glueService.deleteWorkflow(workflowName, region);
}
private void stopWhatIsRunning(String workflowRunId) {
for (JobRun jobRun : jobRunService.runsInWorkflowRun(workflowRunId)) {
if (STATE_JOB_RUNNING.equals(jobRun.getJobRunState())) {
jobRunService.batchStopJobRun(jobRun.getJobName(), List.of(jobRun.getId()));
}
}
for (GlueCrawlerRunService.WorkflowCrawl crawl : crawlerRunService.crawlsInWorkflowRun(workflowRunId)) {
if (STATE_JOB_RUNNING.equals(crawl.state())) {
crawlerRunService.stopCrawler(crawl.crawlerName());
}
}
}
private void requireCapacity(Workflow workflow) {
// Settles runs, fires what is due and completes finished workflow runs before counting.
fireConditionalTriggers();
if (workflow.getMaxConcurrentRuns() == null) {
return;
}
long running = runsOfWorkflow(workflow.getName()).stream()
.filter(run -> STATUS_RUNNING.equals(run.getStatus())).count();
if (running >= workflow.getMaxConcurrentRuns()) {
throw new AwsException("ConcurrentRunsExceededException",
"Concurrent runs exceeded for workflow " + workflow.getName(), 400);
}
}
private WorkflowRun newWorkflowRun(String workflowName, String previousRunId, Map<String, String> properties) {
WorkflowRun run = new WorkflowRun();
run.setName(workflowName);
run.setWorkflowRunId("wr_" + UUID.randomUUID().toString().replace("-", "")
+ UUID.randomUUID().toString().replace("-", ""));
run.setPreviousRunId(previousRunId);
run.setWorkflowRunProperties(properties != null ? new LinkedHashMap<>(properties) : new LinkedHashMap<>());
run.setStartedOn(clock.instant());
run.setStatus(STATUS_RUNNING);
workflowRunStore.put(run.getWorkflowRunId(), run);
LOG.infov("Started Glue workflow run {0} of {1}", run.getWorkflowRunId(), workflowName);
return run;
}
private WorkflowRun findWorkflowRun(String workflowName, String runId) {
glueService.getWorkflow(workflowName);
if (runId == null) {
throw new AwsException("InvalidInputException", "RunId is required.", 400);
}
return workflowRunStore.get(runId)
.filter(run -> workflowName.equals(run.getName()))
.orElseThrow(() -> new AwsException("EntityNotFoundException",
"Workflow run " + runId + " not found for workflow " + workflowName, 400));
}
private List<WorkflowRun> runsOfWorkflow(String workflowName) {
List<WorkflowRun> runs = new ArrayList<>();
for (WorkflowRun run : workflowRunStore.scan(key -> true)) {
if (workflowName.equals(run.getName())) {
runs.add(run);
}
}
return runs;
}
private Set<String> runningWorkflowRunIds() {
Set<String> running = new LinkedHashSet<>();
for (WorkflowRun run : workflowRunStore.scan(key -> true)) {
if (STATUS_RUNNING.equals(run.getStatus())) {
running.add(run.getWorkflowRunId());
}
}
return running;
}
private String workflowNameOf(String workflowRunId) {
return workflowRunStore.get(workflowRunId).map(WorkflowRun::getName).orElse(null);
}
private void completeFinishedWorkflowRuns() {
for (WorkflowRun run : workflowRunStore.scan(key -> true)) {
if (!STATUS_RUNNING.equals(run.getStatus())) {
continue;
}
Instant lastFinished = run.getStartedOn();
boolean anyRunning = false;
for (JobRun jobRun : jobRunService.runsInWorkflowRun(run.getWorkflowRunId())) {
if (jobRun.getCompletedOn() == null) {
anyRunning = true;
} else if (jobRun.getCompletedOn().isAfter(lastFinished)) {
lastFinished = jobRun.getCompletedOn();
}
}
for (GlueCrawlerRunService.WorkflowCrawl crawl : crawlerRunService.crawlsInWorkflowRun(run.getWorkflowRunId())) {
if (crawl.completedOn() == null) {
anyRunning = true;
} else if (crawl.completedOn().isAfter(lastFinished)) {
lastFinished = crawl.completedOn();
}
}
if (!anyRunning) {
finishWorkflowRun(run, STATUS_COMPLETED, lastFinished);
}
}
}
private void finishWorkflowRun(WorkflowRun run, String status, Instant completedOn) {
run.setStatus(status);
run.setCompletedOn(completedOn);
workflowRunStore.put(run.getWorkflowRunId(), run);
forgetWorkflowRunState(run.getWorkflowRunId());
LOG.infov("Glue workflow run {0} is {1}", run.getWorkflowRunId(), status);
}
/** A finished workflow run's triggers no longer need the outcomes they kept for it. */
private void forgetWorkflowRunState(String workflowRunId) {
String prefix = STATE + workflowRunId + "|";
for (String triggerName : seenStore.keys()) {
Map<String, String> seen = seenStore.get(triggerName).orElse(Map.of());
if (seen.keySet().stream().anyMatch(key -> key.startsWith(prefix))) {
Map<String, String> kept = new HashMap<>(seen);
kept.keySet().removeIf(key -> key.startsWith(prefix));
seenStore.put(triggerName, kept);
}
}
}
private WorkflowRun view(WorkflowRun stored, boolean includeGraph) {
WorkflowRun run = new WorkflowRun();
run.setName(stored.getName());
run.setWorkflowRunId(stored.getWorkflowRunId());
run.setPreviousRunId(stored.getPreviousRunId());
run.setWorkflowRunProperties(stored.getWorkflowRunProperties());
run.setStartedOn(stored.getStartedOn());
run.setCompletedOn(stored.getCompletedOn());
run.setStatus(stored.getStatus());
run.setErrorMessage(stored.getErrorMessage());
run.setStatistics(statistics(stored.getWorkflowRunId()));
if (includeGraph) {
run.setGraph(graph(stored.getName(), stored.getWorkflowRunId()));
}
return run;
}
private Map<String, Integer> statistics(String workflowRunId) {
Map<String, Integer> counts = new LinkedHashMap<>();
for (String key : List.of("TotalActions", "TimeoutActions", "FailedActions", "StoppedActions",
"SucceededActions", "RunningActions", "ErroredActions", "WaitingActions")) {
counts.put(key, 0);
}
List<String> states = new ArrayList<>();
for (JobRun jobRun : jobRunService.runsInWorkflowRun(workflowRunId)) {
states.add(jobRun.getJobRunState());
}
for (GlueCrawlerRunService.WorkflowCrawl crawl : crawlerRunService.crawlsInWorkflowRun(workflowRunId)) {
states.add("CANCELLED".equals(crawl.state()) ? "STOPPED" : crawl.state());
}
for (String state : states) {
counts.merge("TotalActions", 1, Integer::sum);
String bucket = switch (state) {
case "SUCCEEDED" -> "SucceededActions";
case "FAILED" -> "FailedActions";
case "STOPPED", "STOPPING" -> "StoppedActions";
case "TIMEOUT" -> "TimeoutActions";
case "ERROR" -> "ErroredActions";
case "WAITING" -> "WaitingActions";
default -> "RunningActions";
};
counts.merge(bucket, 1, Integer::sum);
}
return counts;
}
/**
* The workflow graph: a TRIGGER node per trigger of the workflow, a JOB or CRAWLER node per job or
* crawler its triggers start or watch, an edge from each watched node to the trigger and from the
* trigger to each node it starts. With a workflow run, job nodes list the run's job runs and crawler
* nodes its crawls.
*/
private Map<String, Object> graph(String workflowName, String workflowRunId) {
List<Map<String, Object>> nodes = new ArrayList<>(graphNodes(workflowName));
List<Map<String, Object>> edges = new ArrayList<>();
for (Trigger trigger : glueService.triggersOfWorkflow(workflowName)) {
String triggerId = nodeId(workflowName, "TRIGGER", trigger.getName());
if (trigger.getPredicate() != null && trigger.getPredicate().getConditions() != null) {
for (TriggerCondition condition : trigger.getPredicate().getConditions()) {
edges.add(edge(nodeId(workflowName, condition.getJobName() != null ? "JOB" : "CRAWLER",
condition.getJobName() != null ? condition.getJobName() : condition.getCrawlerName()), triggerId));
}
}
for (TriggerAction action : trigger.getActions()) {
edges.add(edge(triggerId, nodeId(workflowName, action.getJobName() != null ? "JOB" : "CRAWLER",
action.getJobName() != null ? action.getJobName() : action.getCrawlerName())));
}
}
if (workflowRunId != null) {
List<JobRun> jobRuns = jobRunService.runsInWorkflowRun(workflowRunId);
List<GlueCrawlerRunService.WorkflowCrawl> crawls = crawlerRunService.crawlsInWorkflowRun(workflowRunId);
for (Map<String, Object> node : nodes) {
String name = (String) node.get("Name");
if ("JOB".equals(node.get("Type"))) {
node.put("JobDetails", Map.of("JobRuns",
jobRuns.stream().filter(run -> name.equals(run.getJobName())).toList()));
} else if ("CRAWLER".equals(node.get("Type"))) {
List<Map<String, Object>> crawlList = new ArrayList<>();
for (GlueCrawlerRunService.WorkflowCrawl crawl : crawls) {
if (name.equals(crawl.crawlerName())) {
Map<String, Object> entry = new LinkedHashMap<>();
entry.put("State", crawl.state());
entry.put("StartedOn", epochSeconds(crawl.startedOn()));
if (crawl.completedOn() != null) {
entry.put("CompletedOn", epochSeconds(crawl.completedOn()));
}
entry.put("LogGroup", "/aws-glue/crawlers");
entry.put("LogStream", name);
crawlList.add(entry);
}
}
node.put("CrawlerDetails", Map.of("Crawls", crawlList));
}
}
}
Map<String, Object> graph = new LinkedHashMap<>();
graph.put("Nodes", nodes);
graph.put("Edges", edges);
return graph;
}
private List<Map<String, Object>> graphNodes(String workflowName) {
Map<String, Map<String, Object>> nodes = new LinkedHashMap<>();
for (Trigger trigger : glueService.triggersOfWorkflow(workflowName)) {
Map<String, Object> triggerNode = node(workflowName, "TRIGGER", trigger.getName());
triggerNode.put("TriggerDetails", Map.of("Trigger", trigger));
nodes.putIfAbsent((String) triggerNode.get("UniqueId"), triggerNode);
if (trigger.getPredicate() != null && trigger.getPredicate().getConditions() != null) {
for (TriggerCondition condition : trigger.getPredicate().getConditions()) {
Map<String, Object> watched = condition.getJobName() != null
? node(workflowName, "JOB", condition.getJobName())
: node(workflowName, "CRAWLER", condition.getCrawlerName());
nodes.putIfAbsent((String) watched.get("UniqueId"), watched);
}
}
for (TriggerAction action : trigger.getActions()) {
Map<String, Object> started = action.getJobName() != null
? node(workflowName, "JOB", action.getJobName())
: node(workflowName, "CRAWLER", action.getCrawlerName());
nodes.putIfAbsent((String) started.get("UniqueId"), started);
}
}
return new ArrayList<>(nodes.values());
}
private static Map<String, Object> node(String workflowName, String type, String name) {
Map<String, Object> node = new LinkedHashMap<>();
node.put("Type", type);
node.put("Name", name);
node.put("UniqueId", nodeId(workflowName, type, name));
return node;
}
private static Map<String, Object> edge(String sourceId, String destinationId) {
Map<String, Object> edge = new LinkedHashMap<>();
edge.put("SourceId", sourceId);
edge.put("DestinationId", destinationId);
return edge;
}
/** Stable per workflow, type and name, so a node keeps its id across reads and resumes. */
private static String nodeId(String workflowName, String type, String name) {
return "wnode_" + UUID.nameUUIDFromBytes((workflowName + "\u0000" + type + "\u0000" + name)
.getBytes(StandardCharsets.UTF_8)).toString().replace("-", "");
}
private static double epochSeconds(Instant instant) {
return instant.toEpochMilli() / 1000.0;
}
/**
* Processes the completions this trigger has not seen yet, in finishing order, and returns the
* completions that fire it. ANY fires on
* each completion that matches one of its conditions; AND fires on a matching completion once
* every condition's latest outcome matches.
* completions that fire it. ANY fires on each completion that matches one of its conditions; AND
* fires on a matching completion once every condition's latest outcome matches. A workflow's
* trigger only counts completions from a running run of its workflow and keeps its latest outcomes
* per workflow run, so AND is met within one run.
*/
private List<GlueRunCompletion> newFirings(Trigger trigger) {
private List<GlueRunCompletion> newFirings(Trigger trigger, Set<String> runningWorkflowRuns) {
Map<String, String> before = seenStore.get(trigger.getName()).orElse(Map.of());
Map<String, String> seen = new HashMap<>(before);
List<KeyedCompletion> fresh = new ArrayList<>();
@@ -200,11 +703,20 @@ public class GlueTriggerService {
if (processed == null || event.completion().position().compareTo(processed) > 0) {
seen.put(POSITION + event.key(), event.completion().position());
}
seen.put(STATE + event.key(), event.completion().state());
String scope = "";
if (trigger.getWorkflowName() != null) {
String workflowRunId = event.completion().workflowRunId();
if (workflowRunId == null || !runningWorkflowRuns.contains(workflowRunId)
|| !trigger.getWorkflowName().equals(workflowNameOf(workflowRunId))) {
continue;
}
scope = workflowRunId + "|";
}
seen.put(STATE + scope + event.key(), event.completion().state());
boolean eventMatches = false;
boolean allMatch = true;
for (TriggerCondition condition : trigger.getPredicate().getConditions()) {
boolean matches = expectedState(condition).equals(seen.get(STATE + keyOf(condition)));
boolean matches = expectedState(condition).equals(seen.get(STATE + scope + keyOf(condition)));
allMatch &= matches;
eventMatches |= matches && keyOf(condition).equals(event.key())
&& expectedState(condition).equals(event.completion().state());
@@ -282,7 +794,7 @@ public class GlueTriggerService {
forgetLeastRecentlyUsedChainIfFull();
}
budget.setTriggeredRuns(budget.getTriggeredRuns() + 1);
budget.setLastUsed(Instant.now());
budget.setLastUsed(clock.instant());
budgetStore.put(originRunId, budget);
return true;
}
@@ -320,12 +832,14 @@ public class GlueTriggerService {
}
/**
* 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.
* Starts the trigger's actions on behalf of {@code originRunId} and returns how many started, inside
* {@code workflowRunId} when it is not null. With a null origin (StartTrigger on an ON_DEMAND trigger,
* or a workflow's starting 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. A job run in a
* workflow run gets the {@code --WORKFLOW_NAME} and {@code --WORKFLOW_RUN_ID} arguments, as on AWS.
*/
private int fire(Trigger trigger, String originRunId, boolean propagateFailures) {
private int fire(Trigger trigger, String originRunId, String workflowRunId, boolean propagateFailures) {
LOG.infov("Firing Glue trigger {0}", trigger.getName());
int started = 0;
for (TriggerAction action : trigger.getActions()) {
@@ -337,14 +851,14 @@ public class GlueTriggerService {
try {
if (action.getJobName() != null) {
JobRun overrides = new JobRun();
overrides.setArguments(action.getArguments());
overrides.setArguments(withWorkflowArguments(action.getArguments(), workflowRunId));
overrides.setTimeout(action.getTimeout());
overrides.setSecurityConfiguration(action.getSecurityConfiguration());
overrides.setNotificationProperty(action.getNotificationProperty());
overrides.setTriggerName(trigger.getName());
jobRunService.startJobRun(action.getJobName(), null, overrides, originRunId);
jobRunService.startJobRun(action.getJobName(), null, overrides, originRunId, workflowRunId);
} else {
crawlerRunService.startCrawler(action.getCrawlerName(), originRunId);
crawlerRunService.startCrawler(action.getCrawlerName(), originRunId, workflowRunId);
}
started++;
} catch (AwsException e) {
@@ -354,13 +868,38 @@ public class GlueTriggerService {
if (propagateFailures) {
throw e;
}
LOG.warnv("Glue trigger {0} could not start {1}: {2}", trigger.getName(),
action.getJobName() != null ? action.getJobName() : action.getCrawlerName(), e.getMessage());
String target = action.getJobName() != null ? action.getJobName() : action.getCrawlerName();
LOG.warnv("Glue trigger {0} could not start {1}: {2}", trigger.getName(), target, e.getMessage());
if (workflowRunId != null) {
noteLaunchFailure(workflowRunId, "Trigger " + trigger.getName() + " could not start " + target
+ ": " + e.getMessage());
}
}
}
return started;
}
/** Records on the workflow run that one of its actions could not be launched. */
private void noteLaunchFailure(String workflowRunId, String message) {
workflowRunStore.get(workflowRunId).ifPresent(run -> {
run.setErrorMessage(run.getErrorMessage() == null ? message : run.getErrorMessage() + "; " + message);
workflowRunStore.put(workflowRunId, run);
});
}
private Map<String, String> withWorkflowArguments(Map<String, String> arguments, String workflowRunId) {
if (workflowRunId == null) {
return arguments;
}
Map<String, String> merged = new LinkedHashMap<>();
if (arguments != null) {
merged.putAll(arguments);
}
merged.put("--WORKFLOW_NAME", workflowNameOf(workflowRunId));
merged.put("--WORKFLOW_RUN_ID", workflowRunId);
return merged;
}
private static String expectedState(TriggerCondition condition) {
return condition.getJobName() != null ? condition.getState() : condition.getCrawlState();
}
@@ -374,4 +913,7 @@ public class GlueTriggerService {
}
private record KeyedCompletion(String key, GlueRunCompletion completion) {}
/** ResumeWorkflowRun's answer: the new run and the nodes restarted in it. */
public record ResumedRun(String runId, List<String> nodeIds) {}
}
@@ -17,6 +17,7 @@ public class CrawlerRunRecord {
private String currentMessagePrefix;
private String currentCrawlId;
private String currentOriginRunId;
private String currentWorkflowRunId;
private Instant lastStart;
private String lastStatus;
private String lastMessagePrefix;
@@ -44,6 +45,9 @@ public class CrawlerRunRecord {
public String getCurrentOriginRunId() { return currentOriginRunId; }
public void setCurrentOriginRunId(String currentOriginRunId) { this.currentOriginRunId = currentOriginRunId; }
public String getCurrentWorkflowRunId() { return currentWorkflowRunId; }
public void setCurrentWorkflowRunId(String currentWorkflowRunId) { this.currentWorkflowRunId = currentWorkflowRunId; }
public String getLastStatus() { return lastStatus; }
public void setLastStatus(String lastStatus) { this.lastStatus = lastStatus; }
@@ -13,11 +13,13 @@ import java.time.Instant;
@JsonIgnoreProperties(ignoreUnknown = true)
public class FinishedCrawl {
private long sequence;
private String crawlerName;
private Instant startedAt;
private Instant finishedAt;
private String status;
private String crawlId;
private String originRunId;
private String workflowRunId;
@JsonProperty(value = "triggeredRuns", access = JsonProperty.Access.WRITE_ONLY)
private int legacyTriggeredRuns;
@@ -27,6 +29,9 @@ public class FinishedCrawl {
public int getLegacyTriggeredRuns() { return legacyTriggeredRuns; }
public void setLegacyTriggeredRuns(int legacyTriggeredRuns) { this.legacyTriggeredRuns = legacyTriggeredRuns; }
public String getCrawlerName() { return crawlerName; }
public void setCrawlerName(String crawlerName) { this.crawlerName = crawlerName; }
public long getSequence() { return sequence; }
public void setSequence(long sequence) { this.sequence = sequence; }
@@ -45,4 +50,6 @@ public class FinishedCrawl {
public String getOriginRunId() { return originRunId; }
public void setOriginRunId(String originRunId) { this.originRunId = originRunId; }
public String getWorkflowRunId() { return workflowRunId; }
public void setWorkflowRunId(String workflowRunId) { this.workflowRunId = workflowRunId; }
}
@@ -6,7 +6,8 @@ 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, and the run that set off its trigger chain.
* in which Floci saw it finish, the run that set off its trigger chain, and the workflow run it belongs
* to.
*/
@RegisterForReflection
// Earlier versions stored the chain's run count here as triggeredRuns: it is still read, so a chain in
@@ -15,6 +16,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
public class JobRunBookkeeping {
private long completionOrder;
private String originRunId;
private String workflowRunId;
@JsonProperty(value = "triggeredRuns", access = JsonProperty.Access.WRITE_ONLY)
private int legacyTriggeredRuns;
@@ -30,4 +32,6 @@ public class JobRunBookkeeping {
public String getOriginRunId() { return originRunId; }
public void setOriginRunId(String originRunId) { this.originRunId = originRunId; }
public String getWorkflowRunId() { return workflowRunId; }
public void setWorkflowRunId(String workflowRunId) { this.workflowRunId = workflowRunId; }
}
@@ -0,0 +1,69 @@
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;
/**
* A Glue workflow. LastRun and Graph are filled in when the workflow is read; the stored copy
* leaves them unset.
*/
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Workflow {
@JsonProperty("Name")
private String name;
@JsonProperty("Description")
private String description;
@JsonProperty("DefaultRunProperties")
private Map<String, String> defaultRunProperties;
@JsonProperty("CreatedOn")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant createdOn;
@JsonProperty("LastModifiedOn")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant lastModifiedOn;
@JsonProperty("LastRun")
private WorkflowRun lastRun;
@JsonProperty("Graph")
private Map<String, Object> graph;
@JsonProperty("MaxConcurrentRuns")
private Integer maxConcurrentRuns;
public Workflow() {}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public Map<String, String> getDefaultRunProperties() { return defaultRunProperties; }
public void setDefaultRunProperties(Map<String, String> defaultRunProperties) { this.defaultRunProperties = defaultRunProperties; }
public Instant getCreatedOn() { return createdOn; }
public void setCreatedOn(Instant createdOn) { this.createdOn = createdOn; }
public Instant getLastModifiedOn() { return lastModifiedOn; }
public void setLastModifiedOn(Instant lastModifiedOn) { this.lastModifiedOn = lastModifiedOn; }
public WorkflowRun getLastRun() { return lastRun; }
public void setLastRun(WorkflowRun lastRun) { this.lastRun = lastRun; }
public Map<String, Object> getGraph() { return graph; }
public void setGraph(Map<String, Object> graph) { this.graph = graph; }
public Integer getMaxConcurrentRuns() { return maxConcurrentRuns; }
public void setMaxConcurrentRuns(Integer maxConcurrentRuns) { this.maxConcurrentRuns = maxConcurrentRuns; }
}
@@ -0,0 +1,81 @@
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;
/**
* One run of a Glue workflow. Statistics and Graph are computed from the job runs and crawls of
* the run when it is read; the stored copy leaves them unset.
*/
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_NULL)
public class WorkflowRun {
@JsonProperty("Name")
private String name;
@JsonProperty("WorkflowRunId")
private String workflowRunId;
@JsonProperty("PreviousRunId")
private String previousRunId;
@JsonProperty("WorkflowRunProperties")
private Map<String, String> workflowRunProperties;
@JsonProperty("StartedOn")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant startedOn;
@JsonProperty("CompletedOn")
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant completedOn;
@JsonProperty("Status")
private String status;
@JsonProperty("ErrorMessage")
private String errorMessage;
@JsonProperty("Statistics")
private Map<String, Integer> statistics;
@JsonProperty("Graph")
private Map<String, Object> graph;
public WorkflowRun() {}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getWorkflowRunId() { return workflowRunId; }
public void setWorkflowRunId(String workflowRunId) { this.workflowRunId = workflowRunId; }
public String getPreviousRunId() { return previousRunId; }
public void setPreviousRunId(String previousRunId) { this.previousRunId = previousRunId; }
public Map<String, String> getWorkflowRunProperties() { return workflowRunProperties; }
public void setWorkflowRunProperties(Map<String, String> workflowRunProperties) { this.workflowRunProperties = workflowRunProperties; }
public Instant getStartedOn() { return startedOn; }
public void setStartedOn(Instant startedOn) { this.startedOn = startedOn; }
public Instant getCompletedOn() { return completedOn; }
public void setCompletedOn(Instant completedOn) { this.completedOn = completedOn; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getErrorMessage() { return errorMessage; }
public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
public Map<String, Integer> getStatistics() { return statistics; }
public void setStatistics(Map<String, Integer> statistics) { this.statistics = statistics; }
public Map<String, Object> getGraph() { return graph; }
public void setGraph(Map<String, Object> graph) { this.graph = graph; }
}