workflow design
Design goal
Data-parallel plans cover one operation at a time. Real programs also spawn and
join tasks, pass work through channels, and protect shared state with locks.
The workflow package gives these a single description: a task graph whose
nodes are typed steps, whose resources are explicit capabilities, and whose
edges fix the order of execution. The graph is data, so it can be validated in
MoonBit on any target before a runtime executes it.
Constraints
- The package must build on every target, so a workflow is pure data that a runtime interprets later; nothing in it may refer to threads or C objects.
- A workflow must embed plans unchanged, so that a compute node is checked by the same rules as a standalone plan.
- The native runtime receives the graph as flat integer arrays, so nodes and capabilities are identified by integer ids rather than by references.
Main design decisions
Capabilities are declared, nodes refer to them by id
Synchronisation resources exist independently of the steps that use them: two
nodes that send and receive must agree on one channel. Declaring a capability
once and referring to it by id makes that agreement explicit and checkable,
and gives the runtime a dense table of resources to allocate. The access mode
records the intent of the declaration, such as MoveOnly for a channel or
ReadOnly for a shared view, so that validate can reject a WriteShared
node on a read-only buffer.
Every edge kind orders the same way
DataDependency, ControlDependency, OwnershipTransfer and
SynchronizationDependency all mean completes before starts. The
kind documents why the edge exists, and the specification gives ownership
transfer further meaning, but v1 checks and schedules all edges alike, so the
order relation does not depend on the kinds.
Builders mutate in place
Workflow stores its capabilities, nodes and edges in Arrays, and
add_capability, add_node and add_edge push onto them and return the same
workflow. This makes chained construction cheap, amortised per step, but
it is not a persistent builder: after let b = a.add_node(n), a and b are
the same value and both contain n. Code that needs two variants must build
two workflows.
Validation is layered
validate in MoonBit checks the whole data model on every target: policy,
capability kinds and access modes, node and capability references, edge
endpoints, the plans of compute nodes, and cycles. The native runtime checks
again what it relies on when a workflow is submitted. The two layers agree on
most rules but not all:
| Rule | validate | Native runtime |
|---|---|---|
RwLock, Semaphore capabilities | accepted | rejected (status 8) |
Lock and Unlock on an RwLock | accepted | rejected |
| Access modes, duplicate ids, compute plans | checked | not checked |
| Cycles | partial (below) | complete (status 9) |
The MoonBit layer reports every issue, so a user sees all problems at once; the native layer stops at the first, because it only decides whether to start.
A cheap cycle check in MoonBit, a complete one in C
validate looks for cycles with two local tests instead of a topological
sort, and the native runtime runs Kahn’s algorithm before it starts any
thread. The MoonBit test never reports a cycle that is not there (for graphs
whose edges join existing nodes), but it misses many cycles; the derivation is
under the MoonBit cycle check below.
Mathematical background
A workflow is a triple of capabilities, nodes and edges.
- Each capability has an id, a kind and an access mode .
- Each node has an id, a kind and optionally a capability .
- Each edge says that may start only after has completed.
Two relations type the graph. lists the valid access modes of each capability kind, and the capability kinds each node kind may use (both tables are in the workflow API). A workflow is well formed when ids are unique, edges join existing distinct nodes, and
The edges define the relation , “there is a non-empty path from to ”. An execution order is a sequence of all nodes in which comes before whenever , a topological order.
Topological orders and Kahn’s algorithm
Claim. A finite graph has a topological order exactly when it has no directed cycle.
If is a cycle with , an order would place before , before , and so on, hence before itself; no order exists.
Conversely, suppose there is no cycle. Kahn’s algorithm keeps a set of remaining nodes, initially , and repeatedly removes a node of that has no incoming edge from , appending it to the order. Each node removed has all its predecessors already in the order, so whenever the algorithm removes every node, the result is a topological order. It cannot get stuck with : then every has a predecessor in , and walking backwards inside visits nodes after steps, so some node repeats, with , and is a cycle.
The same argument shows that Kahn’s algorithm decides acyclicity: it removes
all of if and only if the graph is acyclic. The C function
workflow_has_cycle implements it by repeated passes that remove every node
whose in-degree from the remaining nodes is zero, in
per pass and at most passes; it returns status 9
(RUNTIME_BROKEN) when nodes remain.
The MoonBit cycle check
Let count the edges whose target is , including edges whose
source is not a node of the workflow. validate reports CyclicDependency
exactly when
where is allowed, so a self edge satisfies (b).
Soundness. Assume every edge joins existing nodes. If (b) holds with , then is a self edge, a cycle of length one. If , then is a cycle of length two. If (a) holds, the backward walk of the previous section runs inside all of and finds a cycle. So every report names a real cycle.
Incompleteness. Suppose some node has and no two edges
are opposite. Then (a) fails because of and (b) fails by assumption, so
nothing is reported, whatever the rest of the graph looks like. Any cycle of
length without a chord in the reverse direction escapes, and
need not be connected to it. In the graph with nodes and edges
, node is isolated, , and validate
reports nothing, while Kahn’s algorithm removes , finds no node of
in-degree zero among and rejects the graph. Without node ,
condition (a) holds and the cycle is reported.
Unknown sources. When an edge starts at an id that is not a node, it
still counts in , so (a) can hold without a cycle: one node with
the edge gets CyclicDependency next to InvalidNodeReference
and InvalidDependency. This is the only false report, and it only occurs
together with the issues that explain it.
Correctness / invariants
- Soundness of acceptance. If
validatereturns no issue, the workflow is well formed in the sense above, except possibly for a cycle of length three or more in a graph that has at least one node without incoming edges; such a cycle need not be connected to that node. - Soundness of rejection. Every issue names a real violation, with the one
CyclicDependencycaveat for edges from unknown nodes.InvalidPolicynever occurs for workflows built outside these packages, because their policies are always valid (see the shared design). - Submission.
submit(w, backend)is accepted exactly whenvalidate(w)is empty andbackendisNative; it never setscompleted. - Complexity.
validateruns in : duplicate detection filters the whole array for each element, endpoint and in-degree lookups scan the node and edge arrays, and the opposite-edge test compares all pairs of edges.
Alternatives rejected
- Capabilities embedded in nodes. A channel stored inside each
Sendnode could not be shared with the matchingRecv. - Typed edges with different scheduling. Giving each edge kind its own scheduling rule would make the order relation depend on runtime policy; v1 keeps one rule.
- A persistent builder. Copying the arrays on every
add_*would make construction quadratic for the convenience of keeping old versions. - Full cycle detection in MoonBit. A topological sort in
validatewould close the gap described above; the current check is cheaper and the native runtime catches the remaining cases. The gap is a candidate for a fix.
Boundaries
The package does not schedule or execute workflows, move data between nodes,
check that blocking nodes are matched (that a Recv has a Send that runs
first), detect deadlocks, or give edge kinds different meanings.