From d429ab0296dedb53e3c824850a48453864fd6a47 Mon Sep 17 00:00:00 2001 From: Drake Stapleton <213298340+drakestapleton@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:48:02 -0600 Subject: [PATCH 1/4] feat(node): durable journal and restart recovery (DRA-289) journal.om: checksummed append-only journal of authority transitions, fdatasync before act/publish, flock fencing, torn-tail cut, version and objective compatibility. Recovery settles in-flight operations (class 3 by key, class 1 UNKNOWN), releases or readmits jobs, restores generation and pause. auth.om journals through it; journal-gate.om crash/restart tests with ten negative controls. --- CONTRIBUTORS.md | 1 + seed/node/README.md | 136 +- seed/node/auth-gate.om | 887 +++++++- seed/node/auth.om | 100 +- seed/node/journal-gate.om | 4010 +++++++++++++++++++++++++++++++++++++ seed/node/journal-test.om | 852 ++++++++ seed/node/journal.om | 787 ++++++++ 7 files changed, 6753 insertions(+), 20 deletions(-) create mode 100644 seed/node/journal-gate.om create mode 100644 seed/node/journal-test.om create mode 100644 seed/node/journal.om diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 18454a818..78cee1798 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -11,6 +11,7 @@ The current development method is [Human-Directed Agentic Engineering (HDAE)](RE | Claude Opus 5.5 (via omp) | DRA-286 for `seed/node`: `pool.om`, epoch-based reuse of K pre-forked shared arenas. Reader holds are checked again after they are stored, and rollover fails closed when every other arena is held. `super.om`: forked-worker supervision with close-on-exec life pipes for worker death and creator loss, exact-PID reaping, and a bounded stop with SIGKILL escalation. `node_reset`/`node_epoch` added to `transport.om`. `pool-gate.om` runs a real-process acceptance test and a 2000-epoch concurrent stress test. It has ten deterministic negative controls plus two stress controls. Native AArch64 host evidence only; not QUALIFIED on the Spark gate. | | Claude Opus 5.5 (via omp) | DRA-287 for `seed/node`: `sched.om`, a cooperative scheduler in which one creator owns admission, assignment, collection and review. Workers have topic subscriptions, and jobs can wait on dependency topics. Wake-ups are eventfd hints followed by a rescan, so none is lost. Admitted work and work in review have separate limits; a full review queue stops assignment instead of dropping results. A dead worker's job is rerun when marked safe, otherwise it becomes UNKNOWN. There is no hive-wide barrier. `node_count` added to `transport.om`. `sched-gate.om` runs a real-process acceptance test that measures idle CPU from `/proc` and checks exactly one wake per round over 200 publication rounds. It has eight negative controls. Native AArch64 host evidence only; not QUALIFIED on the Spark gate. | | Claude Opus 5.5 (via omp) | DRA-288 for `seed/node`: `auth.om`, typed worker proposals decided and committed by the creator alone through the action records of `seed/authority/action.om` (one record per operation identity; a performed or UNKNOWN operation is never started again). Proposals are refused as malformed, not the current assignment, another incarnation, stale input, revoked authority or cancelled. Granted tools run as their own process group; cancellation, revocation, stop and worker death kill the whole group, and the outcome comes from the wait status, so a tool that already exited stays performed and a killed one stays UNKNOWN. Operator pause, cancel and revoke reach workers through a per-worker control eventfd with acknowledged seqs. `sched.om` gains `sched_pause` and `sched_cancel`. `auth-gate.om` runs real forked workers and `/bin/sh` tools through seven phases, with twelve negative controls. Native AArch64 host evidence only; not QUALIFIED on the Spark gate. | +| Claude Opus 5.5 (via omp) | DRA-289 for `seed/node`: `journal.om`, a checksummed append-only journal of every authority transition (OPEN, ADMIT, START, DONE, RELEASE, REVOKE, PAUSE, CANCEL), each checked by the recovery tables, written and fdatasync'd before anything acts on it or publishes it; a failed write or sync stops all later appends. `flock` fencing that forked workers also hold, fence numbers per creator incarnation, torn-tail cutting, refusal of corrupt records, other format versions and other objectives. Restart recovery settles operations left in flight (idempotent class 3 by its key, class 1 UNKNOWN and never rerun), releases or readmits each job, and restores authority generation and pause. `auth.om` journals through it and accepts class 3. `journal-gate.om` crashes real creators at every boundary, SIGKILLs them mid-tool and checks the result with its own reader; ten negative controls. Native AArch64 host evidence only; not QUALIFIED on the Spark gate. | | Grok 4.7 (via Cursor Agent) | Merged main `fd3751b31` (#1020, carrying #1045 and #1050) into #1052. The only conflict was the generated root `.crumb` (child `last_commit` stamps, the `tests` digest, and the root digest). Recompiled with the crumb.lock tool (`d0f0b95d`); `crumb verify .` current. The #525/#962 pins were left as committed: `src/checker2/c2_check.c`, `src/compiler/osc_ir.c` and `src/compiler/osc_ir.h` still hash to the SHA-256s in `ACCEPTED`. | | Claude Opus 5.5 (via omp) | #1020 (actions/cache-4.3.0 ->6.1.0): merged main `d3d1b1bc` and recompiled the root crumb with the crumb.lock tool (`d0f0b95d`). The root crumb's source tree covers `.github/workflows/`, so the bump alone left it stale and `AIEN Checks` failed at `crumb verify .` (`1 of 188 crumbs stale`). | | Claude Opus 5.5 (via omp) | #1018 (actions/upload-artifact-4 ->7): merged main `d3d1b1bc` and recompiled the root crumb with the crumb.lock tool (`d0f0b95d`). The root crumb's source tree covers `.github/workflows/`, so the bump alone left it stale and `AIEN Checks` failed at `crumb verify .` (`1 of 188 crumbs stale`). | diff --git a/seed/node/README.md b/seed/node/README.md index e9786b535..416662ec2 100644 --- a/seed/node/README.md +++ b/seed/node/README.md @@ -1,4 +1,4 @@ -# Omega Node shared-memory transport, epoch pool, worker supervision, scheduling and authority +# Omega Node shared-memory transport, epoch pool, worker supervision, scheduling, authority and journal This is the first implementation component of [Omega Node](https://github.com/aien-dev/omega-harness/issues/205). It provides a bounded, single-publisher artifact log for trusted forked Omega @@ -6,7 +6,7 @@ processes. Runtime consumers can share artifact storage and wake only interested workers. The existing `seed/agent/agent.om` is the coding-agent consumer to evaluate for integration; this change does not replace its model or tool dispatcher. -Four source fragments, concatenated in this order before one program with `fn main`: +Source fragments, concatenated in this order before one program with `fn main`: | Fragment | Adds | | --- | --- | @@ -14,10 +14,12 @@ Four source fragments, concatenated in this order before one program with `fn ma | `pool.om` | K arenas reused by epoch, reader holds, fail-closed rollover (DRA-286) | | `super.om` | forked workers, death detection, reaping, creator-loss detection, bounded stop (DRA-286) | | `sched.om` | subscriptions, dependency wake-ups, bounded admission and review, job loss handling (DRA-287) | +| `seed/authority/action.om` | the existing action records the authority layer commits through | +| `journal.om` | durable journal of every authority transition, fencing, restart recovery (DRA-289) | +| `auth.om` | typed proposals, one commit owner, cancellation of real child tools, operator controls (DRA-288) | -The component is not yet connected to the live hive. Authority integration, -persistent recovery, a dashboard and a real model-backed coding workflow remain -implementation work (DRA-288 to DRA-292). The +The component is not yet connected to the live hive. A dashboard and a real +model-backed coding workflow remain implementation work (DRA-290 to DRA-292). The [Linear project](https://linear.app/drake-stapleton/project/omega-node-shared-memory-hive-runtime-37058a1eac4b) tracks that integration. This is a component contract, not a whole-system master plan. @@ -365,7 +367,7 @@ below on native Linux AArch64, with real forked workers running real child tools (`/bin/sh`). Evidence is in the DRA-288 pull request. ```sh -cat seed/node/transport.om seed/node/pool.om seed/node/super.om seed/node/sched.om seed/authority/action.om seed/node/auth.om seed/node/auth-test.om > build/node/auth-composed.om +cat seed/node/transport.om seed/node/pool.om seed/node/super.om seed/node/sched.om seed/authority/action.om seed/node/journal.om seed/node/auth.om seed/node/auth-test.om > build/node/auth-composed.om cmp build/node/auth-composed.om seed/node/auth-gate.om seed/out/omc < seed/node/auth-gate.om > build/node/auth-gate chmod +x build/node/auth-gate @@ -428,8 +430,8 @@ generation and the paused flag. | `auth_wtool c w q pk pn class`, `auth_wreport w q outcome` | Worker: run the granted tool (packed argv as `execprog`), report its outcome. | | `auth_wwait c w x interval`, `auth_wctl w` | Worker wait on wake, stop, creator loss, control and optionally fd x; the control handler (ack, paused, cancel target). | -Limits: action records live in creator memory only, so a creator restart -loses them (DRA-289 adds the journal). Between a worker's death and the +Limits: without an open journal (the DRA-288 gate) action records live in +creator memory only; `journal.om` makes them durable (DRA-289). Between a worker's death and the creator's `auth_step` the tool's group id could in principle be reused; the window is one creator step. At most 64 action records are live. @@ -459,3 +461,121 @@ phases: | `auth.om`: delete `x = killpid neg int p 9` in `au_lost` (dead worker's tool left running) | 96 | | `auth.om`: decide proposals while paused | 120 | | `sched.om`: `if ne get scpause 0 0` to `if false` (assign while paused) | 121 | + +## Durable journal and restart recovery (DRA-289) + +Status: IMPLEMENTED in `journal.om` and `auth.om`, exercised by +`journal-gate.om` and the controls below on native Linux AArch64: a conductor +forks real creators (with real workers and `/bin/sh` tools), crashes them at +every boundary and restarts them. Evidence is in the DRA-289 pull request. + +```sh +cat seed/node/transport.om seed/node/pool.om seed/node/super.om seed/node/sched.om seed/authority/action.om seed/node/journal.om seed/node/auth.om seed/node/journal-test.om > build/node/journal-composed.om +cmp build/node/journal-composed.om seed/node/journal-gate.om +seed/out/omc < seed/node/journal-gate.om > build/node/journal-gate +chmod +x build/node/journal-gate +rm -rf build/node/jt +timeout 120 build/node/journal-gate # ends "PASS node journal"; uses build/node/jt +``` + +### Journal + +`DIR/journal` is a 64-byte header and then 128-byte records, little endian +64-bit words. Header: magic, format version 1, record size 128, the 4-word +objective identity, FNV-1a 64 of words 0..6. Record: type, seq (1, 2, 3, ... +without gaps), fence, 12 payload words, FNV-1a 64 of words 0..14. + +| Type | Payload | Appended by | +| --- | --- | --- | +| 1 OPEN | pid, authority generation | `jn_open`, every creator incarnation | +| 2 ADMIT | job id, dependency topics, flags | `jn_admit`, before `sched_admit` | +| 3 START | operation identity, class, job id, attempt, generation | `au_decide`, after `actstart`, before the grant is published | +| 4 DONE | operation identity, outcome, job id, how (1 reported, 2 worker lost, 3 in flight at restart, 4 key present, 5 key absent, 6 no external effect) | `au_record`, `au_lost`, recovery | +| 5 RELEASE | job id, outcome (300 cancelled) | `jn_release`, before `sched_review`; `auth_cancel` | +| 6 REVOKE | new generation | `auth_revoke` | +| 7 PAUSE | 1 paused, 0 resumed | `auth_pause` | +| 8 CANCEL | job id | `auth_cancel`, before the worker is told | + +Order: the in-memory tables first check the transition (a refused one is +never written), then the record is written with `pwrite` and made durable +with `fdatasync`, and only then does anything act on it or publish it: a +grant reaches the worker after its START is durable, a job is reviewed after +its RELEASE is durable, and a job is not released while its operation's DONE +is not durable. A write or sync failure marks the journal failed and every +later append is refused (30), so the creator never acts on a transition that +might not be durable. A new journal's directory entry is synced +(`jn_dsync`). + +Fencing: `DIR/lock` is held with `flock(LOCK_EX)` for the creator's life. It +is opened close-on-exec, so tools never inherit it; forked workers do, so a +new creator cannot open the journal until every worker of the old one has +exited (refused with 20 after the wait). Each open appends OPEN with the next +fence number and every record carries the fence of the creator that wrote it, +so each incarnation's records are attributable. The pid lock in +`seed/authority/lock.om` was not reused: it reclaims stale locks by pid, which +races, and has a fixed path. + +Torn tail: a partial last record, or a last full record whose checksum or seq +is wrong, was never acknowledged and is cut off (`ftruncate` plus sync). A bad +record before the last one is corruption: the open is refused (23) and the +file is not changed. + +Compatibility: a header whose magic, version, record size or checksum differs +is refused (21), never reinterpreted; a different objective identity is +refused (22). A future format must change the version. + +### Recovery + +`jn_open` reads the journal into the recovery tables; `auth_recover` (after +`sched_new` and `auth_new`, before spawning workers) settles and readmits: +- A START with no DONE was in flight at the crash. Class 3 (an idempotent effect whose key `DIR/k.` the effect itself creates atomically, for example with `ln`) is decided by the key: present is performed, absent is not performed and the job runs again. Class 2 (no external effect) is not performed. Class 1 is UNKNOWN and is never run again. Each gets a DONE. +- An admitted job not yet released: a performed or UNKNOWN last operation releases it with that outcome; a cancelled job is released as cancelled; any other job is admitted again with its dependency topics. +- The action records (state, attempt, generation), the authority generation (above every journaled one) and the pause state are restored, so a performed or UNKNOWN operation is refused if proposed again and a paused objective stays paused. +- A restart that finds nothing in flight adds only its OPEN. + +A crash between START and publishing the grant leaves a class 1 operation +UNKNOWN even though its tool never ran. That is the pessimistic side: an +effect is never repeated without proof that it did not happen. + +| Function | Contract | +| --- | --- | +| `jn_open dir dn obj ms`, `jn_close` | Creator: take the lock (wait up to ms), create or check the header, cut a torn tail, read every record, append OPEN; release. | +| `auth_recover c` | Settle, release or readmit, restore authority and pause. | +| `jn_admit id deps flags`, `jn_release j` | Durable admission and release around `sched_admit` and `sched_review`. | +| `jn_known id`, `jn_done_j id`, `jn_jout id`, `jn_stat i`, `jn_fence`, `jn_gen`, `jn_act k i` | Recovered state. | + +Limits: no checkpoint or compaction yet; recovery replays the whole journal +and the tables hold 64 operations and 256 jobs. Inputs published to the pool +are not journaled, so a recovered job reads whatever its dependency topics +hold after the restart. The key of a class 3 effect must be created by the +effect itself, atomically and last. + +### Acceptance and controls + +The gate runs, each in a fresh `build/node/jt`, and checks with its own +reader (header, checksums, contiguous seq, fences, START/DONE pairing, one +RELEASE per job, sink lines, keys): +- 0 baseline: a class 1 and a class 3 job each performed once. +- 1 crash after ADMIT: the admitted job is readmitted, the other admitted; both performed once. +- 2 crash after START, before the grant: class 1 UNKNOWN, its tool never ran; two more restarts add only OPEN records. +- 3 the same for class 3: key absent, run again (attempt 2), performed once. +- 4, 5 crash after the tool reported, before DONE: class 1 UNKNOWN with the effect once; class 3 performed by its key. +- 6, 7 crash after DONE, and after RELEASE: released once, nothing run again. +- 8 crash in the middle of writing DONE (60 bytes on disk): the tail is cut, UNKNOWN. +- 9, 10 SIGKILL of the creator while a slow tool runs: the workers kill it; class 3 runs again and performs once, class 1 is UNKNOWN with no effect. +- 11 a second creator while the first runs is fenced (20) and changes nothing. +- 12 another objective (22), another format version (21), a corrupt middle record (23): refused, journal unchanged. +- 15 a paused objective stays paused across a restart until resumed. + +| Change | Expected exit | +| --- | --- | +| `journal.om`: cut any bad record as a torn tail (`if true`) | 157 | +| `journal.om`: in `jn_settle`, class 1 in flight becomes not performed (replayed) | 33 | +| `journal.om`: in `jn_settle`, class 3 not probed | 43 | +| `journal.om`: `jn_probe` always reports the key absent | 66 | +| `auth.om`: the reported outcome is not journaled | 10 | +| `journal.om`: no `flock` (no fencing) | 141 | +| `journal.om`: `jn_recover` skips `jn_settle` (the restart never releases the job) | 35 | +| `journal.om`: objective check removed | 151 | +| `journal.om`: version and record-size check removed | 154 | +| `auth.om`: `auth_recover` does not restore pause | 161 | diff --git a/seed/node/auth-gate.om b/seed/node/auth-gate.om index 679722c7d..c826a9481 100644 --- a/seed/node/auth-gate.om +++ b/seed/node/auth-gate.om @@ -1518,6 +1518,793 @@ put r 1 4 end ret 0 end +note Omega Node durable journal and restart recovery (DRA-289). Concatenate +note after transport.om, pool.om, super.om, sched.om and +note seed/authority/action.om, before auth.om, which journals through it. +note While no journal is open every append succeeds without writing, so the +note authority layer also runs memory-only. +note File DIR/journal: a 64-byte header, then 128-byte records. Header words: +note 0 magic, 1 format version 1, 2 record size 128, 3..6 objective identity, +note 7 FNV-1a 64 of words 0..6. Record words: 0 type, 1 seq (1, 2, 3, ... +note with no gap), 2 fence, 3..14 payload, 15 FNV-1a 64 of words 0..14. +note Types and payloads: +note 1 OPEN 3 pid, 4 authority generation of the new creator +note 2 ADMIT 3 job id, 4 dependency topics, 5 flags +note 3 START 3..6 operation identity, 7 class, 8 job id, 9 attempt, 10 gen +note 4 DONE 3..6 operation identity, 7 outcome (0 UNKNOWN, 1 not +note performed, 2 performed), 8 job id, 9 how (1 reported, +note 2 worker lost, 3 in flight at restart, 4 key present, +note 5 key absent, 6 no external effect) +note 5 RELEASE 3 job id, 4 outcome (the job's outcome; 300 cancelled) +note 6 REVOKE 3 new generation +note 7 PAUSE 3 1 paused, 0 resumed +note 8 CANCEL 3 job id +note Order: a transition is appended and fdatasync'd before anything acts on +note it or publishes it. An append whose write or sync fails marks the journal +note failed and every later append is refused, so nothing is acted on that +note might not be durable. +note Fencing: DIR/lock is held with flock(LOCK_EX) for the creator's life. It +note is opened close-on-exec, so tools never hold it, but forked workers do: +note a new creator cannot open the journal until every worker of the old one +note has exited. Each open appends OPEN with the next fence number, and every +note record carries the fence of the creator that wrote it. +note Torn tail: a partial last record, or a last full record whose checksum or +note seq is wrong, was never acknowledged and is cut off. A bad record that is +note not the last is corruption: the open is refused and nothing is changed. +note Compatibility: a header whose magic, version, record size or checksum +note differs is refused (21), never reinterpreted; an objective identity that +note differs is refused (22). +text jnjour "/journal" +text jnlock "/lock" +text jnkey "/k." +global jon ulong 1 +global jbad ulong 1 +global jfd int 1 +global jlk int 1 +global jseq ulong 1 +global jsz ulong 1 +global jfence ulong 1 +global jgen ulong 1 +global jcrash ulong 1 +global jdir uchar 128 +global jdn ulong 1 +global jpath uchar 160 +global jact ulong 1024 +global jjob ulong 2048 +global jstat ulong 8 + +note Fault injection for the recovery tests: exit 77 at point p when armed. +fn jn_hit p ulong to void +if and gt p 0 eq get jcrash 0 p +exit 77 +end +end + +fn jn_sum r view ulong 16 n ulong to ulong +h = ulong 14695981039346656037 +for i ulong n bound 16 +v = get r i +for b ulong 8 +h = mul xor h and shr v mul b 8 255 1099511628211 +end +end +ret h +end + +fn jn_enc r view ulong 16 b edit uchar 128 to void +for i ulong 16 +v = get r i +for k ulong 8 +put b add mul i 8 k uchar and shr v mul k 8 255 +end +end +end + +fn jn_dec b view uchar 128 r edit ulong 16 to void +for i ulong 16 +v = ulong 0 +for k ulong 8 +v = or v shl ulong get b add mul i 8 k mul k 8 +end +put r i v +end +end + +note DIR plus name k (0 journal, 1 lock, 2 key prefix) in jpath; the length. +fn jn_name k ulong to ulong +n = get jdn 0 +for i ulong n bound 128 +put jpath i get jdir i +end +m = len jnjour +if eq k 1 +m = len jnlock +end +if eq k 2 +m = len jnkey +end +for i ulong m bound 16 +b = get jnkey 0 +if eq k 0 +b = get jnjour i +end +if eq k 1 +b = get jnlock i +end +if eq k 2 +b = get jnkey i +end +put jpath add n i b +end +ret add n m +end + +note The key path DIR/k. of an idempotent effect; the length. +fn jn_keyp v ulong to ulong +n = jn_name 2 +d = new uchar 20 0 +x = v +k = ulong 20 +for i ulong 20 +k = sub k 1 +put d k uchar add rem x 10 48 +x = div x 10 +if eq x 0 +break +end +end +for i ulong sub 20 k bound 20 +put jpath add n i get d add k i +end +ret add n sub 20 k +end + +fn jn_reset to void +for i ulong 1024 +put jact i 0 +end +for i ulong 2048 +put jjob i 0 +end +for i ulong 8 +put jstat i 0 +end +put jon 0 0 +put jbad 0 0 +put jseq 0 0 +put jsz 0 0 +put jfence 0 0 +put jgen 0 0 +end + +note Action table, 64 entries of 16 words: 0 used, 1..4 operation identity, +note 5 class, 6 state (as action.om: 1 ready, 2 in flight, 3 UNKNOWN, +note 4 performed), 7 attempt, 8 gen, 9 job id. -1 when absent. +fn jn_afind r view ulong 16 to long +for k ulong 64 +b = mul k 16 +if ne get jact b 0 +m = true +for i ulong 4 +if ne get jact add b add 1 i get r add 3 i +m = false +end +end +if m +ret long k +end +end +end +ret neg long 1 +end + +note Job table, 256 entries of 8 words: 0 used, 1 job id, 2 deps, 3 flags, +note 4 released, 5 outcome, 6 cancelled, 7 action entry + 1 of its last START. +fn jn_jfind id ulong to long +for k ulong 256 +if and ne get jjob mul k 8 0 eq get jjob add mul k 8 1 id +ret long k +end +end +ret neg long 1 +end + +fn jn_jnew id ulong to long +for k ulong 256 +b = mul k 8 +if eq get jjob b 0 +for i ulong 8 +put jjob add b i 0 +end +put jjob b 1 +put jjob add b 1 id +ret long k +end +end +ret neg long 1 +end + +fn jn_maxg g ulong to void +if gt g get jgen 0 +put jgen 0 g +end +end + +fn jn_astart r view ulong 16 to ulong +k = jn_afind r +if lt k 0 +for i ulong 64 +if eq get jact mul i 16 0 +k = long i +break +end +end +end +if lt k 0 +ret 25 +end +b = mul ulong k 16 +if ne get jact b 0 +if ne get jact add b 6 1 +ret 25 +end +end +put jact b 1 +for i ulong 4 +put jact add b add 1 i get r add 3 i +end +put jact add b 5 get r 7 +put jact add b 6 2 +put jact add b 7 get r 9 +put jact add b 8 get r 10 +put jact add b 9 get r 8 +jn_maxg get r 10 +j = jn_jfind get r 8 +if ge j 0 +put jjob add mul ulong j 8 7 add ulong k 1 +end +ret 0 +end + +fn jn_adone r view ulong 16 to ulong +k = jn_afind r +if lt k 0 +ret 25 +end +b = mul ulong k 16 +s = get jact add b 6 +if not or eq s 2 eq s 3 +ret 25 +end +o = get r 7 +if gt o 2 +ret 25 +end +v = ulong 3 +if eq o 1 +v = 1 +end +if eq o 2 +v = 4 +end +put jact add b 6 v +ret 0 +end + +note Apply one durable record to the recovery tables; 0, 24 unknown type or +note 25 a transition the tables refuse (START of a performed operation, a +note DONE without a START, a second ADMIT or RELEASE). +fn jn_apply r view ulong 16 to ulong +t = get r 0 +if eq t 1 +put jfence 0 get r 2 +jn_maxg get r 4 +ret 0 +end +if eq t 2 +if ge jn_jfind get r 3 0 +ret 25 +end +j = jn_jnew get r 3 +if lt j 0 +ret 25 +end +put jjob add mul ulong j 8 2 get r 4 +put jjob add mul ulong j 8 3 get r 5 +ret 0 +end +if eq t 3 +ret jn_astart r +end +if eq t 4 +ret jn_adone r +end +if or eq t 5 eq t 8 +j = jn_jfind get r 3 +if lt j 0 +ret 25 +end +b = mul ulong j 8 +if eq t 8 +put jjob add b 6 1 +ret 0 +end +if ne get jjob add b 4 0 +ret 25 +end +put jjob add b 4 1 +put jjob add b 5 get r 4 +ret 0 +end +if eq t 6 +jn_maxg get r 3 +ret 0 +end +if eq t 7 +put jstat 2 get r 3 +ret 0 +end +ret 24 +end + +fn jn_le64 b view uchar 128 off ulong to ulong +v = ulong 0 +for k ulong 8 +v = or v shl ulong get b add off k mul k 8 +end +ret v +end + +note Make DIR's entry for a new journal durable. +fn jn_dsync to ulong +fd = fopen jdir 0 get jdn 0 540672 0 +if lt fd 0 +ret 27 +end +r = fsync int fd +x = fclose int fd +if ne r 0 +ret 27 +end +ret 0 +end + +note Create the header of an empty journal, or check an existing one. +fn jn_head fd int obj view ulong 4 to ulong +st = new uchar 128 0 +h = new ulong 16 0 +b = new uchar 128 0 +if ne fstat fd st 0 128 0 +ret 27 +end +size = jn_le64 st 48 +if lt size 64 +if ne ftrunc fd 0 0 +ret 27 +end +put h 0 5786624599 +put h 1 1 +put h 2 128 +for i ulong 4 +put h add 3 i get obj i +end +put h 7 jn_sum h 7 +jn_enc h b +if ne pwrite fd b 0 64 0 64 +ret 27 +end +if ne fsync fd 0 +ret 27 +end +put jsz 0 64 +ret jn_dsync +end +if ne pread fd b 0 64 0 64 +ret 27 +end +jn_dec b h +s = jn_sum h 7 +if or ne get h 0 5786624599 ne get h 7 s +ret 21 +end +if or ne get h 1 1 ne get h 2 128 +ret 21 +end +for i ulong 4 +if ne get h add 3 i get obj i +ret 22 +end +end +put jsz 0 size +ret 0 +end + +fn jn_cut fd int off ulong to ulong +if ne ftrunc fd long off 0 +ret 27 +end +if ne fdsync fd 0 +ret 27 +end +put jstat 0 add get jstat 0 sub get jsz 0 off +put jsz 0 off +ret 0 +end + +note Record i into r: 0 well formed (checksum and seq), 1 not, 27 I/O. +fn jn_rd fd int i ulong r edit ulong 16 to ulong +b = new uchar 128 0 +off = long add 64 mul i 128 +if ne pread fd b 0 128 off 128 +ret 27 +end +jn_dec b r +s = jn_sum r 15 +if ne get r 15 s +ret 1 +end +if ne get r 1 add i 1 +ret 1 +end +ret 0 +end + +note Read every record into the tables; cut a torn tail; refuse corruption. +fn jn_scan fd int to ulong +r = new ulong 16 0 +n = div sub get jsz 0 64 128 +rm = rem sub get jsz 0 64 128 +for i ulong n bound 10000000 +g = jn_rd fd i r +if eq g 27 +ret 27 +end +if eq g 1 +if and eq add i 1 n eq rm 0 +ret jn_cut fd add 64 mul i 128 +end +ret 23 +end +e = jn_apply r +if ne e 0 +ret e +end +put jseq 0 add i 1 +put jstat 1 add i 1 +end +if gt rm 0 +ret jn_cut fd add 64 mul n 128 +end +ret 0 +end + +note Append record r (caller sets word 0 and the payload) and make it +note durable. The tables check and take the transition first (jn_apply +note changes nothing when it refuses), so a refused transition is never +note written. 0, a table refusal, 28 write, 29 sync, 30 failed earlier. +note After 28 or 29 the journal is failed: the transition may or may not be +note durable, so the caller must not act on it, and nothing more is written. +note Without an open journal: 0. +fn jn_append r edit ulong 16 to ulong +if eq get jon 0 0 +ret 0 +end +if ne get jbad 0 0 +ret 30 +end +b = new uchar 128 0 +fd = get jfd 0 +put r 1 add get jseq 0 1 +put r 2 get jfence 0 +put r 15 jn_sum r 15 +e = jn_apply r +if ne e 0 +ret e +end +jn_enc r b +off = long add 64 mul get jseq 0 128 +if and eq get jcrash 0 6 eq get r 0 4 +x = pwrite fd b 0 60 off +exit 77 +end +if ne pwrite fd b 0 128 off 128 +put jbad 0 1 +ret 28 +end +if ne fdsync fd 0 +put jbad 0 1 +ret 29 +end +put jseq 0 add get jseq 0 1 +put jsz 0 add get jsz 0 128 +ret 0 +end + +fn jn_rec t ulong a ulong b ulong c ulong to ulong +r = new ulong 16 0 +put r 0 t +put r 3 a +put r 4 b +put r 5 c +ret jn_append r +end + +note Open (or create) the journal in directory dir (dn bytes) for objective +note obj, waiting up to ms for the lock. 0, 20 fenced (another creator or +note its workers hold the lock), 21 incompatible, 22 other objective, 23 +note corrupt, 24/25 refused record, 26 path too long, 27 I/O error. +fn jn_open dir view uchar 128 dn ulong obj view ulong 4 ms ulong to ulong +if or eq dn 0 gt dn 100 +ret 26 +end +jn_reset +for i ulong dn bound 128 +put jdir i get dir i +end +put jdn 0 dn +r = makedir jdir 0 dn 448 +if and lt r 0 ne r neg long 17 +ret 27 +end +n = jn_name 1 +lk = fopen jpath 0 n 524354 384 +if lt lk 0 +ret 27 +end +got = false +for t ulong add ms 1 bound 100000 +f = flock int lk 6 +if eq f 0 +got = true +break +end +if ne f neg long 11 +break +end +x = sleepns 1000000 +end +if not got +x = fclose int lk +ret 20 +end +put jlk 0 int lk +e = jn_load obj +if ne e 0 +x = fclose int lk +ret e +end +ret 0 +end + +fn jn_load obj view ulong 4 to ulong +n = jn_name 0 +fd = fopen jpath 0 n 524354 384 +if lt fd 0 +ret 27 +end +e = jn_head int fd obj +if eq e 0 +e = jn_scan int fd +end +if ne e 0 +x = fclose int fd +ret e +end +put jfd 0 int fd +put jon 0 1 +put jfence 0 add get jfence 0 1 +put jgen 0 add get jgen 0 1 +ret jn_rec 1 ulong getpid get jgen 0 0 +end + +note Close the journal and release the lock (other holders: forked workers). +fn jn_close to void +if ne get jon 0 0 +x = fclose get jfd 0 +x = fclose get jlk 0 +end +put jon 0 0 +end + +note Probe the key of an idempotent operation: 1 present, 0 absent, 2 error. +fn jn_probe v ulong to ulong +st = new uchar 128 0 +n = jn_keyp v +r = stat jpath 0 n st 0 128 +if eq r 0 +ret 1 +end +if eq r neg long 2 +ret 0 +end +ret 2 +end + +note Close every START left in flight by a crash with a DONE. Class 3 (an +note idempotent effect whose key DIR/k. is created atomically by +note the effect) is decided by its key; class 2 (no external effect) was not +note performed; anything else is UNKNOWN and is never run again. +fn jn_settle to ulong +r = new ulong 16 0 +for k ulong 64 +b = mul k 16 +if and ne get jact b 0 eq get jact add b 6 2 +for i ulong 4 +put r add 3 i get jact add b add 1 i +end +put r 0 4 +put r 8 get jact add b 9 +cl = get jact add b 5 +put r 7 0 +put r 9 3 +if eq cl 2 +put r 7 1 +put r 9 6 +end +if eq cl 3 +p = jn_probe get r 3 +if eq p 2 +ret 27 +end +put r 7 add 1 p +put r 9 sub 5 p +end +e = jn_append r +if ne e 0 +ret e +end +put jstat 3 add get jstat 3 1 +end +end +ret 0 +end + +note Creator, after jn_open and sched_new: settle in-flight operations, then +note for each admitted job not yet released: a performed or UNKNOWN last +note operation releases it with that outcome (it never runs again); a +note cancelled job is released as cancelled; any other is admitted again. +note 0 or an error. +fn jn_recover c long to ulong +e = jn_settle +if ne e 0 +ret e +end +for k ulong 256 +b = mul k 8 +if and ne get jjob b 0 eq get jjob add b 4 0 +o = ulong 999 +a = get jjob add b 7 +if gt a 0 +s = get jact add mul sub a 1 16 6 +if eq s 4 +o = 2 +end +if eq s 3 +o = 0 +end +end +if and eq o 999 ne get jjob add b 6 0 +o = 300 +end +if eq o 999 +x = sched_admit get jjob add b 1 get jjob add b 2 get jjob add b 3 +if lt x 0 +ret 31 +end +put jstat 5 add get jstat 5 1 +else +e = jn_rec 5 get jjob add b 1 o 0 +if ne e 0 +ret e +end +put jstat 4 add get jstat 4 1 +end +end +end +ret 0 +end + +note Admit job id durably: ADMIT is appended before sched_admit. -2 at the +note admission limit, -4 an id already in the journal, -5 the journal +note refused, else sched_admit's slot. +fn jn_admit id ulong deps ulong flags ulong to long +if ge sched_count 1 3 get sclim 0 +ret neg long 2 +end +if ge jn_jfind id 0 +ret neg long 4 +end +if ne jn_rec 2 id deps flags 0 +ret neg long 5 +end +jn_hit 1 +ret sched_admit id deps flags +end + +note Release reviewed job slot j durably: RELEASE before sched_review. -5 +note while its operation's DONE is not yet durable or on refusal. +fn jn_release j ulong to long +id = get sjid j +k = jn_jfind id +if ge k 0 +a = get jjob add mul ulong k 8 7 +if gt a 0 +if eq get jact add mul sub a 1 16 6 2 +ret neg long 5 +end +end +end +if ne jn_rec 5 id get sjout j 0 0 +ret neg long 5 +end +jn_hit 5 +ret sched_review j +end + +fn jn_start op view ulong 4 cl ulong id ulong at ulong g ulong to ulong +r = new ulong 16 0 +put r 0 3 +for i ulong 4 +put r add 3 i get op i +end +put r 7 cl +put r 8 id +put r 9 at +put r 10 g +ret jn_append r +end + +fn jn_done op view ulong 4 o ulong id ulong how ulong to ulong +r = new ulong 16 0 +put r 0 4 +for i ulong 4 +put r add 3 i get op i +end +put r 7 o +put r 8 id +put r 9 how +ret jn_append r +end + +note Getters: whether job id is in the journal, released, its outcome; a +note recovery counter (0 torn bytes cut, 1 records read, 2 paused, 3 settled, +note 4 released at recovery, 5 admitted again); the fence; the generation. +fn jn_known id ulong to bool +ret ge jn_jfind id 0 +end + +fn jn_done_j id ulong to bool +k = jn_jfind id +if lt k 0 +ret false +end +ret ne get jjob add mul ulong k 8 4 0 +end + +fn jn_jout id ulong to ulong +k = jn_jfind id +if lt k 0 +ret 999 +end +ret get jjob add mul ulong k 8 5 +end + +fn jn_stat i ulong to ulong +ret get jstat and i 7 +end + +fn jn_fence to ulong +ret get jfence 0 +end + +fn jn_gen to ulong +ret get jgen 0 +end + +note Action table entry k word i (k < 64, i < 16). +fn jn_act k ulong i ulong to ulong +ret get jact add mul and k 63 16 and i 15 +end note Omega Node proposals, authority and cancellation (DRA-288). Concatenate note after transport.om, pool.om, super.om, sched.om and note seed/authority/action.om, before one program with fn main. @@ -1538,8 +2325,9 @@ note the authority generation and 8200 the paused flag. note Proposal page: 4096 bytes per worker at 4096*w. Words 0..15: 0 version note 1, 1 kind 1, 2 job id, 3 assignment seq, 4 slot, 5 incarnation, 6 input note epoch, 7 input version, 8 generation, 9..12 operation identity, 13 class -note (1 external effect, 2 no external effect), 14 and 15 zero. Bytes -note 128..4095 hold the running tool's output. +note (1 external effect, 2 no external effect, 3 idempotent external effect +note whose completion is visible as a key, see journal.om), 14 and 15 zero. +note Bytes 128..4095 hold the running tool's output. note Verdicts: 0 granted, 1 malformed, 2 not the worker's assignment, 3 other note incarnation, 4 stale input, 5 stale authority, 6 cancelled, 7 already note performed or UNKNOWN, 8 attempt limit, 9 refused by the action contract. @@ -1725,7 +2513,7 @@ ret 0 end note Worker w: propose the effect of assignment q with operation identity -note op and class (1 external effect, 2 none). The words go first, then the +note op and class (1 external effect, 2 none, 3 keyed idempotent). The words go first, then the note proposal seq, then an ack to the creator. 0 or the ack refusal. fn auth_propose w ulong q ulong op view ulong 4 class ulong to long if ge w 64 @@ -2089,7 +2877,7 @@ if or ne au_pw w 0 1 ne au_pw w 1 1 ret 1 end cl = au_pw w 13 -if or eq cl 0 gt cl 2 +if or eq cl 0 gt cl 3 ret 1 end if or ne au_pw w 14 0 ne au_pw w 15 0 @@ -2124,18 +2912,51 @@ end ret 0 end +note Creator: decide worker w's pending proposal. A grant is journaled +note (START, made durable) before the verdict is published; if the journal +note refuses, the action is closed as not performed and the verdict is 9. fn au_decide c long w ulong to void q = au_cell w 72 v = au_valid c w if eq v 0 v = au_commit w end +if eq v 0 +if ne au_jstart w 0 +e = au_recon sub get aujob w 1 1 +put aujob w 0 +v = 9 +end +end +if eq v 0 +jn_hit 2 +end au_set w 24 v au_set w 32 get augen 0 au_set w 16 q r = sup_wake w end +fn au_jstart w ulong to ulong +op = new ulong 4 0 +k = sub get aujob w 1 +for i ulong 4 +put op i au_pw w add 9 i +end +b = mul k 32 +ret jn_start op au_pw w 13 au_pw w 2 get aurec add b 2 get aurec add b 4 +end + +note Journal DONE for the action in slot b-1 of worker w's job. +fn au_jdone b ulong o ulong w ulong how ulong to ulong +op = new ulong 4 0 +k = sub b 1 +for i ulong 4 +put op i get aurec add mul k 32 add 8 i +end +ret jn_done op o au_pw w 2 how +end + note Creator: apply receipt outcome o to action slot k (actrecon). An note outcome of 0 leaves the action UNKNOWN. fn au_recon k ulong o ulong to ulong @@ -2155,12 +2976,21 @@ au_save k r ret e end +note Creator: record worker w's report. DONE is made durable before the +note action record changes; if the journal refuses, the report stays +note unrecorded and is tried again at the next step. fn au_record w ulong to void -put aured w au_cell w 80 b = get aujob w if eq b 0 +put aured w au_cell w 80 ret end +jn_hit 3 +if ne au_jdone b au_cell w 88 w 1 0 +ret +end +jn_hit 4 +put aured w au_cell w 80 put aujob w 0 e = au_recon sub b 1 au_cell w 88 end @@ -2178,6 +3008,9 @@ b = get aujob w if eq b 0 ret end +if ne au_jdone b 0 w 2 0 +ret +end put aujob w 0 k = sub b 1 au_load k r @@ -2214,6 +3047,7 @@ note recorded as reported: performed if it had already exited, UNKNOWN if note it was killed. Returns the new generation. fn auth_revoke c long to ulong g = add get augen 0 1 +x = jn_rec 6 g 0 0 put augen 0 g r = mapset get aumap 0 8192 8 g n = mapget c 48 8 @@ -2233,6 +3067,7 @@ v = ulong 0 if on v = 1 end +x = jn_rec 7 v 0 0 r = mapset get aumap 0 8200 8 v n = mapget c 48 8 for w ulong n bound 64 @@ -2246,9 +3081,15 @@ end note Creator: cancel job j. 1 freed before it started (no effect), 2 its note worker was told to cancel and the outcome comes from its report (a note pending proposal is refused with 6), 3 already finished, 4 UNKNOWN, -note -3 no such job. +note -3 no such job. CANCEL is journaled first; a job freed before it +note started is also released (outcome 300). fn auth_cancel c long j ulong to long +id = get sjid j +x = jn_rec 8 id 0 0 s = sched_cancel j +if eq s 1 +x = jn_rec 5 id 300 0 +end if eq s 2 w = get sjw j au_ctl w sc_cell w 0 @@ -2256,6 +3097,40 @@ end ret s end +note Creator, after auth_new, jn_open and sched_new, before any spawn: +note settle and release from the journal (jn_recover), then install every +note journaled action into the action records, move the generation past +note every journaled one and restore the paused flag. 0 or an error. +fn auth_recover c long to ulong +e = jn_recover c +if ne e 0 +ret e +end +op = new ulong 4 0 +for k ulong 64 +if ne jn_act k 0 0 +for i ulong 4 +put op i jn_act k add 1 i +end +s = au_alloc op jn_act k 5 +if lt s 0 +ret 32 +end +b = mul ulong s 32 +put aurec add b 1 jn_act k 6 +put aurec add b 2 jn_act k 7 +put aurec add b 4 jn_act k 8 +end +end +put augen 0 jn_gen +r = mapset get aumap 0 8192 8 jn_gen +if ne jn_stat 2 0 +r = mapset get aumap 0 8200 8 1 +x = sched_pause c true +end +ret 0 +end + note Creator: word i (0..31) of the action record for op, or -1 if none. fn auth_act op view ulong 4 i ulong to long k = au_find op diff --git a/seed/node/auth.om b/seed/node/auth.om index 3f319c49e..a2b2f84ec 100644 --- a/seed/node/auth.om +++ b/seed/node/auth.om @@ -18,8 +18,9 @@ note the authority generation and 8200 the paused flag. note Proposal page: 4096 bytes per worker at 4096*w. Words 0..15: 0 version note 1, 1 kind 1, 2 job id, 3 assignment seq, 4 slot, 5 incarnation, 6 input note epoch, 7 input version, 8 generation, 9..12 operation identity, 13 class -note (1 external effect, 2 no external effect), 14 and 15 zero. Bytes -note 128..4095 hold the running tool's output. +note (1 external effect, 2 no external effect, 3 idempotent external effect +note whose completion is visible as a key, see journal.om), 14 and 15 zero. +note Bytes 128..4095 hold the running tool's output. note Verdicts: 0 granted, 1 malformed, 2 not the worker's assignment, 3 other note incarnation, 4 stale input, 5 stale authority, 6 cancelled, 7 already note performed or UNKNOWN, 8 attempt limit, 9 refused by the action contract. @@ -205,7 +206,7 @@ ret 0 end note Worker w: propose the effect of assignment q with operation identity -note op and class (1 external effect, 2 none). The words go first, then the +note op and class (1 external effect, 2 none, 3 keyed idempotent). The words go first, then the note proposal seq, then an ack to the creator. 0 or the ack refusal. fn auth_propose w ulong q ulong op view ulong 4 class ulong to long if ge w 64 @@ -569,7 +570,7 @@ if or ne au_pw w 0 1 ne au_pw w 1 1 ret 1 end cl = au_pw w 13 -if or eq cl 0 gt cl 2 +if or eq cl 0 gt cl 3 ret 1 end if or ne au_pw w 14 0 ne au_pw w 15 0 @@ -604,18 +605,51 @@ end ret 0 end +note Creator: decide worker w's pending proposal. A grant is journaled +note (START, made durable) before the verdict is published; if the journal +note refuses, the action is closed as not performed and the verdict is 9. fn au_decide c long w ulong to void q = au_cell w 72 v = au_valid c w if eq v 0 v = au_commit w end +if eq v 0 +if ne au_jstart w 0 +e = au_recon sub get aujob w 1 1 +put aujob w 0 +v = 9 +end +end +if eq v 0 +jn_hit 2 +end au_set w 24 v au_set w 32 get augen 0 au_set w 16 q r = sup_wake w end +fn au_jstart w ulong to ulong +op = new ulong 4 0 +k = sub get aujob w 1 +for i ulong 4 +put op i au_pw w add 9 i +end +b = mul k 32 +ret jn_start op au_pw w 13 au_pw w 2 get aurec add b 2 get aurec add b 4 +end + +note Journal DONE for the action in slot b-1 of worker w's job. +fn au_jdone b ulong o ulong w ulong how ulong to ulong +op = new ulong 4 0 +k = sub b 1 +for i ulong 4 +put op i get aurec add mul k 32 add 8 i +end +ret jn_done op o au_pw w 2 how +end + note Creator: apply receipt outcome o to action slot k (actrecon). An note outcome of 0 leaves the action UNKNOWN. fn au_recon k ulong o ulong to ulong @@ -635,12 +669,21 @@ au_save k r ret e end +note Creator: record worker w's report. DONE is made durable before the +note action record changes; if the journal refuses, the report stays +note unrecorded and is tried again at the next step. fn au_record w ulong to void -put aured w au_cell w 80 b = get aujob w if eq b 0 +put aured w au_cell w 80 +ret +end +jn_hit 3 +if ne au_jdone b au_cell w 88 w 1 0 ret end +jn_hit 4 +put aured w au_cell w 80 put aujob w 0 e = au_recon sub b 1 au_cell w 88 end @@ -658,6 +701,9 @@ b = get aujob w if eq b 0 ret end +if ne au_jdone b 0 w 2 0 +ret +end put aujob w 0 k = sub b 1 au_load k r @@ -694,6 +740,7 @@ note recorded as reported: performed if it had already exited, UNKNOWN if note it was killed. Returns the new generation. fn auth_revoke c long to ulong g = add get augen 0 1 +x = jn_rec 6 g 0 0 put augen 0 g r = mapset get aumap 0 8192 8 g n = mapget c 48 8 @@ -713,6 +760,7 @@ v = ulong 0 if on v = 1 end +x = jn_rec 7 v 0 0 r = mapset get aumap 0 8200 8 v n = mapget c 48 8 for w ulong n bound 64 @@ -726,9 +774,15 @@ end note Creator: cancel job j. 1 freed before it started (no effect), 2 its note worker was told to cancel and the outcome comes from its report (a note pending proposal is refused with 6), 3 already finished, 4 UNKNOWN, -note -3 no such job. +note -3 no such job. CANCEL is journaled first; a job freed before it +note started is also released (outcome 300). fn auth_cancel c long j ulong to long +id = get sjid j +x = jn_rec 8 id 0 0 s = sched_cancel j +if eq s 1 +x = jn_rec 5 id 300 0 +end if eq s 2 w = get sjw j au_ctl w sc_cell w 0 @@ -736,6 +790,40 @@ end ret s end +note Creator, after auth_new, jn_open and sched_new, before any spawn: +note settle and release from the journal (jn_recover), then install every +note journaled action into the action records, move the generation past +note every journaled one and restore the paused flag. 0 or an error. +fn auth_recover c long to ulong +e = jn_recover c +if ne e 0 +ret e +end +op = new ulong 4 0 +for k ulong 64 +if ne jn_act k 0 0 +for i ulong 4 +put op i jn_act k add 1 i +end +s = au_alloc op jn_act k 5 +if lt s 0 +ret 32 +end +b = mul ulong s 32 +put aurec add b 1 jn_act k 6 +put aurec add b 2 jn_act k 7 +put aurec add b 4 jn_act k 8 +end +end +put augen 0 jn_gen +r = mapset get aumap 0 8192 8 jn_gen +if ne jn_stat 2 0 +r = mapset get aumap 0 8200 8 1 +x = sched_pause c true +end +ret 0 +end + note Creator: word i (0..31) of the action record for op, or -1 if none. fn auth_act op view ulong 4 i ulong to long k = au_find op diff --git a/seed/node/journal-gate.om b/seed/node/journal-gate.om new file mode 100644 index 000000000..7a4a8cf8d --- /dev/null +++ b/seed/node/journal-gate.om @@ -0,0 +1,4010 @@ +note Omega Node transport, concatenate with a program-mode consumer. +note Trusted forked processes only. This is transport, not write authority. +note One publisher PID, 64 immutable records per epoch; reuse only via node_reset. +note Layout: 4096 byte header then 64 slots of 4160 bytes. +note Header u64: magic, ABI, published count, stop, publisher PID, epoch. +note Slot u64: version, length, topic mask; payload begins at offset 64. +const NCAP ulong 64 +const NBYTES ulong 270336 +const NMAGIC ulong 1313817669 + +fn node_new to long +h = mapnew neg int 1 0 NBYTES 3 33 +if le h 0 +ret h +end +r = mapset h 8 8 1 +r = mapset h 32 8 ulong getpid +r = mapset h 0 8 NMAGIC +ret h +end + +fn node_valid h long to bool +ret and eq mapget h 0 8 NMAGIC eq mapget h 8 8 1 +end + +fn node_offset v ulong to ulong +ret add 4096 mul sub v 1 4160 +end + +note Return version, or -1 stopped, -2 full, -3 length, -4 topic, +note -5 invalid header, -6 wrong publisher. Refusals do not publish. +fn node_publish h long topics ulong src view uchar 4096 n ulong to long +if not node_valid h +ret neg long 5 +end +if ne mapget h 32 8 ulong getpid +ret neg long 6 +end +if ne mapget h 24 8 0 +ret neg long 1 +end +if gt n 4096 +ret neg long 3 +end +if eq topics 0 +ret neg long 4 +end +v = mapget h 16 8 +if gt v NCAP +ret neg long 5 +end +if eq v NCAP +ret neg long 2 +end +v = add v 1 +off = node_offset v +r = mapwrite h add off 64 src 0 n +r = mapset h add off 8 8 n +r = mapset h add off 16 8 topics +note mapset has barriers before and after its aligned store. +r = mapset h off 8 v +r = mapset h 16 8 v +ret long v +end + +note Header and record checks precede copying. No partial publication is read. +note Returns payload length, -5 invalid header/record, -7 unavailable version. +fn node_read h long v ulong dst edit uchar 4096 to long +if not node_valid h +ret neg long 5 +end +if or eq v 0 gt v NCAP +ret neg long 7 +end +latest = mapget h 16 8 +if gt latest NCAP +ret neg long 5 +end +if gt v latest +ret neg long 7 +end +off = node_offset v +if ne mapget h off 8 v +ret neg long 5 +end +n = mapget h add off 8 8 +if gt n 4096 +ret neg long 5 +end +ret mapread h add off 64 dst 0 n +end + +note Return the next matching version after cursor, zero if caught up, +note or negative refusal. Caller advances its cursor through returned versions. +fn node_next h long cursor ulong topics ulong to long +if not node_valid h +ret neg long 5 +end +if or gt cursor NCAP eq topics 0 +ret neg long 7 +end +latest = mapget h 16 8 +if or gt latest NCAP gt cursor latest +ret neg long 5 +end +v = cursor +note At most NCAP published slots can be examined. +while lt v latest bound 64 +v = add v 1 +off = node_offset v +if ne mapget h off 8 v +ret neg long 5 +end +if ne and mapget h add off 16 8 topics 0 +ret long v +end +end +ret 0 +end + +fn node_stop h long to long +if not node_valid h +ret neg long 5 +end +if ne mapget h 32 8 ulong getpid +ret neg long 6 +end +ret mapset h 24 8 1 +end + +note Creator only. Retire every record so a pool can reuse the arena for a new +note epoch. The caller must first prove no reader holds the old epoch (pool.om). +note The count drops to zero before any record version is cleared. +fn node_reset h long epoch ulong to long +if not node_valid h +ret neg long 5 +end +if ne mapget h 32 8 ulong getpid +ret neg long 6 +end +if eq epoch 0 +ret neg long 7 +end +r = mapset h 16 8 0 +for i ulong NCAP +r = mapset h node_offset add i 1 8 0 +end +r = mapset h 24 8 0 +r = mapset h 40 8 epoch +ret 0 +end + +fn node_epoch h long to ulong +ret mapget h 40 8 +end + +note The latest published version of the arena's current epoch (0 when empty). +fn node_count h long to ulong +ret mapget h 16 8 +end + +note One nonblocking eventfd per consumer. Saturation means already readable. +note Interrupted operations retry at most 64 times, then return EINTR. +fn node_notify fd int to long +buf = new uchar 8 0 +put buf 0 1 +for i ulong 64 +r = write fd buf 0 8 +if eq r 8 +ret 0 +end +if eq r neg long 11 +ret 0 +end +if ne r neg long 4 +if lt r 0 +ret r +end +ret neg long 5 +end +end +ret neg long 4 +end + +fn node_drain fd int to long +buf = new uchar 8 0 +for i ulong 64 +r = read fd buf 0 8 +if or eq r 8 eq r neg long 11 +ret 0 +end +if ne r neg long 4 +if lt r 0 +ret r +end +ret neg long 5 +end +end +ret neg long 4 +end + +fn node_pollfd p edit uchar 16 off ulong fd int to void +for i ulong 4 +put p add off i uchar and shr uint fd uint mul i 8 255 +end +put p add off 4 1 +end + +note Returns 2 stop, 1 wake, 0 timeout or negative errno. +note Control takes priority when both descriptors are ready. +note EINTR returns to caller so it can recompute its monotonic deadline. +fn node_wait h long wake int stop int interval long to long +p = new uchar 16 0 +if not node_valid h +ret neg long 5 +end +if or lt wake 0 lt stop 0 +ret neg long 9 +end +if eq wake stop +ret neg long 22 +end +if ne mapget h 24 8 0 +ret 2 +end +node_pollfd p 0 wake +node_pollfd p 8 stop +r = pollfds p 0 16 interval +if lt r 0 +ret r +end +if ne mapget h 24 8 0 +ret 2 +end +if ne and get p 14 56 0 +ret neg long 5 +end +if ne and get p 14 1 0 +r = node_drain stop +if lt r 0 +ret r +end +ret 2 +end +if ne and get p 6 56 0 +ret neg long 5 +end +if ne and get p 6 1 0 +r = node_drain wake +if lt r 0 +ret r +end +ret 1 +end +ret 0 +end +note Omega Node epoch pool. Concatenate after transport.om, before super.om. +note K arenas (1..8) and one control page are mapped before any worker fork, +note so every forked worker inherits the same shared mappings. +note Single writer: the creator writes the current epoch, the arena table and +note every arena record. Worker w alone writes hold word w while it lives; +note the creator clears hold w only after it has reaped worker w. +note Control u64: 0 magic, 8 ABI, 16 current epoch, 24 stop, 32 creator PID, +note 40 arena count K, 48 worker slots W, 64+8k arena handle k, +note 128+8k epoch of arena k (0 free), 256+8w hold of worker w (0 none), +note 768+8w worker PID, 1280+8w worker state (0 empty, 1 live, 2 reaped). +const PMAGIC ulong 1347374924 +const PCAP ulong 8 +const PWCAP ulong 64 +const PBYTES ulong 4096 + +fn pool_valid c long to bool +ret and eq mapget c 0 8 PMAGIC eq mapget c 8 8 1 +end + +fn pool_mine c long to bool +ret eq mapget c 32 8 ulong getpid +end + +fn pool_arena c long k ulong to long +ret long mapget c add 64 mul k 8 8 +end + +note Drop the first n arenas and the control page of a pool being refused. +fn pool_unwind c long n ulong to long +for i ulong n bound 8 +r = mapdrop pool_arena c i +end +ret mapdrop c +end + +note Return the control handle, or -7 bad size, or a negative mapping errno. +note Arena 0 starts as epoch 1. Call before forking any worker. +fn pool_new k ulong w ulong to long +if or eq k 0 gt k PCAP +ret neg long 7 +end +if or eq w 0 gt w PWCAP +ret neg long 7 +end +c = mapnew neg int 1 0 PBYTES 3 33 +if le c 0 +ret c +end +r = mapset c 32 8 ulong getpid +r = mapset c 40 8 k +r = mapset c 48 8 w +for i ulong k bound 8 +h = node_new +if le h 0 +r = pool_unwind c i +ret h +end +r = mapset c add 64 mul i 8 8 ulong h +end +r = node_reset pool_arena c 0 1 +r = mapset c 128 8 1 +r = mapset c 16 8 1 +r = mapset c 8 8 1 +r = mapset c 0 8 PMAGIC +ret c +end + +note Arena index holding epoch e, or -5 when no arena holds it. +fn pool_slot c long e ulong to long +k = mapget c 40 8 +if gt k PCAP +ret neg long 5 +end +for i ulong k bound 8 +if eq mapget c add 128 mul i 8 8 e +ret long i +end +end +ret neg long 5 +end + +note Creator: the arena of the current epoch, or a negative refusal. +fn pool_current c long to long +if not pool_valid c +ret neg long 5 +end +k = pool_slot c mapget c 16 8 +if lt k 0 +ret k +end +ret pool_arena c ulong k +end + +fn pool_epoch c long to ulong +ret mapget c 16 8 +end + +note Worker step: store hold e for worker w, then read the epoch again. mapset +note and mapget fence both sides, so a creator that moved the epoch before it +note scanned holds either sees this hold or this read sees the move. Returns +note the arena of e, -8 when the epoch moved, or -5 when no arena holds e. +note Every refusal clears the hold. +fn pool_hold c long w ulong e ulong to long +if ge w mapget c 48 8 +ret neg long 7 +end +hw = add 256 mul w 8 +r = mapset c hw 8 e +if ne mapget c 16 8 e +r = mapset c hw 8 0 +ret neg long 8 +end +k = pool_slot c e +if lt k 0 +r = mapset c hw 8 0 +ret k +end +h = pool_arena c ulong k +if ne node_epoch h e +r = mapset c hw 8 0 +ret neg long 5 +end +ret h +end + +note Worker w holds the current epoch and returns its arena. Returns -1 +note stopped, -5 invalid, -7 bad worker, -8 the epoch kept moving. +fn pool_acquire c long w ulong to long +if not pool_valid c +ret neg long 5 +end +if ge w mapget c 48 8 +ret neg long 7 +end +for t ulong 16 +if ne mapget c 24 8 0 +r = mapset c add 256 mul w 8 8 0 +ret neg long 1 +end +h = pool_hold c w mapget c 16 8 +if ne h neg long 8 +ret h +end +end +ret neg long 8 +end + +note Worker w gives up its hold. Its old arena must not be read afterwards. +fn pool_release c long w ulong to long +if ge w mapget c 48 8 +ret neg long 7 +end +ret mapset c add 256 mul w 8 8 0 +end + +fn pool_held c long e ulong to bool +n = mapget c 48 8 +for w ulong n bound 64 +if eq mapget c add 256 mul w 8 8 e +ret true +end +end +ret false +end + +note Creator step: retire arena i and open epoch n in it, then publish n. +fn pool_reuse c long i ulong n ulong to long +off = add 128 mul i 8 +r = mapset c off 8 0 +r = node_reset pool_arena c i n +if lt r 0 +ret r +end +r = mapset c off 8 n +r = mapset c 16 8 n +ret long n +end + +note Creator step: an arena that is free or whose older epoch no worker holds, +note or -9. Call only after the current epoch has moved past that arena. +fn pool_pick c long to long +e = mapget c 16 8 +k = mapget c 40 8 +for i ulong k bound 8 +ek = mapget c add 128 mul i 8 8 +if ne ek e +if or eq ek 0 not pool_held c ek +ret long i +end +end +end +ret neg long 9 +end + +note Creator: open the next epoch in a free or unheld arena and make it current. +note Returns the new epoch, -1 stopped, -5 invalid, -6 not creator, or -9 when +note every other arena is still held: the pool fails closed, nothing changes. +fn pool_roll c long to long +if not pool_valid c +ret neg long 5 +end +if not pool_mine c +ret neg long 6 +end +if ne mapget c 24 8 0 +ret neg long 1 +end +k = pool_pick c +if lt k 0 +ret k +end +ret pool_reuse c ulong k add mapget c 16 8 1 +end + +note Creator: publish to the current epoch; when it is full, roll once and retry. +note Returns the version, or the refusal of the publish or of the roll. +fn pool_publish c long topics ulong src view uchar 4096 n ulong to long +h = pool_current c +if lt h 0 +ret h +end +v = node_publish h topics src n +if ne v neg long 2 +ret v +end +r = pool_roll c +if lt r 0 +ret r +end +h = pool_current c +if lt h 0 +ret h +end +ret node_publish h topics src n +end + +fn pool_stop c long to long +if not pool_valid c +ret neg long 5 +end +if not pool_mine c +ret neg long 6 +end +ret mapset c 24 8 1 +end + +fn pool_stopped c long to bool +ret ne mapget c 24 8 0 +end + +note Creator: record worker w's PID and state for observers. +fn pool_setw c long w ulong pid ulong st ulong to long +if not pool_mine c +ret neg long 6 +end +if ge w mapget c 48 8 +ret neg long 7 +end +r = mapset c add 768 mul w 8 8 pid +ret mapset c add 1280 mul w 8 8 st +end + +note Creator: clear hold w. Only after worker w has been reaped. +fn pool_clear c long w ulong to long +if not pool_mine c +ret neg long 6 +end +if ge w mapget c 48 8 +ret neg long 7 +end +ret mapset c add 256 mul w 8 8 0 +end + +note Drop every mapping of the pool in this process. Other processes keep +note their own inherited mappings until they drop them or exit. +fn pool_free c long to long +ret pool_unwind c mapget c 40 8 +end +note Omega Node worker supervision. Concatenate after transport.om and pool.om. +note The creator forks trusted workers from its own binary: sup_spawn returns 0 +note in the child, which runs its worker function and exits with its result. +note Death is seen without polling PIDs. Worker w alone holds the write end of +note its close-on-exec life pipe, so its exit hangs up the creator's read end. +note Every worker holds the read end of the creator's life pipe, whose only +note writer is the creator, so creator death hangs up every worker. +note Per worker: wake, stop and ack eventfds, kept across respawns. +note Workers must not exec: exec closes the life pipe and reads as death. +global swpid long 64 +global swwake int 64 +global swstop int 64 +global swack int 64 +global swlife int 64 +global swst ulong 64 +global swpw ulong 64 +global swpar int 2 +global swme ulong 1 +global swws uchar 4 +global swpf uchar 8 +global swpoll uchar 1024 + +fn sup_fd off ulong to int +v = ulong get swpf off +v = or v shl ulong get swpf add off 1 8 +v = or v shl ulong get swpf add off 2 16 +v = or v shl ulong get swpf add off 3 24 +ret int v +end + +note Poll record j at swpoll offset off; a negative fd is stored as itself +note (bit pattern) so the kernel ignores that record. +fn sup_gpfd off ulong fd int to void +u = bitcast uint fd +for i ulong 4 +put swpoll add off i uchar and shr u uint mul i 8 255 +end +put swpoll add off 4 1 +put swpoll add off 5 0 +put swpoll add off 6 0 +put swpoll add off 7 0 +end + +fn sup_lpfd p edit uchar 24 off ulong fd int to void +for i ulong 4 +put p add off i uchar and shr uint fd uint mul i 8 255 +end +put p add off 4 1 +end + +note Read and reset an eventfd counter: the number of hints since the last +note take, 0 when none is pending, or a negative errno. +fn sup_take fd int to long +buf = new uchar 8 0 +for i ulong 64 +r = read fd buf 0 8 +if eq r 8 +v = ulong 0 +for j ulong 8 +v = or v shl ulong get buf j mul j 8 +end +ret long and v 9223372036854775807 +end +if eq r neg long 11 +ret 0 +end +if ne r neg long 4 +if lt r 0 +ret r +end +ret neg long 5 +end +end +ret neg long 4 +end + +fn sup_closeall to void +for i ulong 64 +if ge get swwake i 0 +r = fclose get swwake i +end +if ge get swstop i 0 +r = fclose get swstop i +end +if ge get swack i 0 +r = fclose get swack i +end +if ge get swlife i 0 +r = fclose get swlife i +end +put swwake i neg int 1 +put swstop i neg int 1 +put swack i neg int 1 +put swlife i neg int 1 +end +for i ulong 2 +if ge get swpar i 0 +r = fclose get swpar i +end +put swpar i neg int 1 +end +end + +note Creator: create the creator life pipe and three eventfds per worker slot. +note Call once after pool_new and before sup_spawn. 0 or a negative errno; +note a failure closes everything this call opened. +fn sup_init c long to long +if not pool_mine c +ret neg long 6 +end +for i ulong 64 +put swpid i 0 +put swst i 0 +put swwake i neg int 1 +put swstop i neg int 1 +put swack i neg int 1 +put swlife i neg int 1 +end +put swme 0 0 +put swpar 0 neg int 1 +put swpar 1 neg int 1 +r = pipefds swpf 0 8 524288 +if lt r 0 +ret r +end +put swpar 0 sup_fd 0 +put swpar 1 sup_fd 4 +n = mapget c 48 8 +for i ulong n bound 64 +a = eventfd uint 0 +if lt a 0 +sup_closeall +ret a +end +put swwake i int a +a = eventfd uint 0 +if lt a 0 +sup_closeall +ret a +end +put swstop i int a +a = eventfd uint 0 +if lt a 0 +sup_closeall +ret a +end +put swack i int a +end +ret 0 +end + +note Creator: fork worker w. Returns 0 in the child, the child PID in the +note creator, -7 bad slot, -16 slot still live, or a negative errno. The +note child keeps only its own life write end; the creator keeps the read end. +fn sup_spawn c long w ulong to long +if not pool_mine c +ret neg long 6 +end +if ge w mapget c 48 8 +ret neg long 7 +end +if ne get swpid w 0 +ret neg long 16 +end +r = pipefds swpf 0 8 524288 +if lt r 0 +ret r +end +rd = sup_fd 0 +wr = sup_fd 4 +pid = fork +if eq pid 0 +r = fclose get swpar 1 +put swpar 1 neg int 1 +r = fclose rd +put swlife w wr +put swme 0 add w 1 +ret 0 +end +r = fclose wr +if lt pid 0 +r = fclose rd +ret pid +end +put swpid w pid +put swlife w rd +r = pool_setw c w ulong pid 1 +ret pid +end + +note Worker: 3 creator gone, 2 stop, 1 wake hint, 0 timeout, negative errno. +note Creator loss outranks stop, which outranks work. A set stop flag makes +note the poll nonblocking. Hints are drained; rescan shared state after 1. +fn sup_wwait c long w ulong interval long to long +p = new uchar 24 0 +if ge w 64 +ret neg long 7 +end +iv = interval +if pool_stopped c +iv = 0 +end +sup_lpfd p 0 get swwake w +sup_lpfd p 8 get swstop w +sup_lpfd p 16 get swpar 0 +r = pollfds p 0 24 iv +if lt r 0 +ret r +end +if ne get p 22 0 +ret 3 +end +if pool_stopped c +ret 2 +end +if ne and get p 14 56 0 +ret neg long 5 +end +if ne and get p 14 1 0 +r = node_drain get swstop w +ret 2 +end +if ne and get p 6 56 0 +ret neg long 5 +end +if ne and get p 6 1 0 +r = node_drain get swwake w +if lt r 0 +ret r +end +ret 1 +end +ret 0 +end + +note Worker or creator: post a hint on worker w's ack eventfd. +fn sup_ack w ulong to long +if ge w 64 +ret neg long 7 +end +ret node_notify get swack w +end + +note Creator: wake worker w with a work hint. +fn sup_wake w ulong to long +if ge w 64 +ret neg long 7 +end +if le get swpid w 0 +ret neg long 3 +end +ret node_notify get swwake w +end + +fn sup_live c long to ulong +n = mapget c 48 8 +k = ulong 0 +for w ulong n bound 64 +if gt get swpid w 0 +k = add k 1 +end +end +ret k +end + +note Creator: reap worker w, then release what it held. A worker that has not +note exited within about one second gets SIGKILL first. Only after the exact +note PID is reaped are its hold, PID and life pipe released. Returns the raw +note wait status, -3 no live worker, or the waitpid errno (nothing released). +fn sup_reap c long w ulong to long +if ge w 64 +ret neg long 7 +end +pid = get swpid w +if le pid 0 +ret neg long 3 +end +done = false +for t ulong 100 +r = waitpid int pid swws 0 4 1 +if eq r pid +done = true +break +end +if ne r 0 +ret r +end +r = sleepns 10000000 +end +if not done +r = killpid int pid 9 +r = waitpid int pid swws 0 4 0 +if ne r pid +ret r +end +end +st = ulong get swws 0 +st = or st shl ulong get swws 1 8 +put swst w st +r = fclose get swlife w +put swlife w neg int 1 +put swpid w 0 +r = pool_clear c w +r = pool_setw c w 0 2 +ret long st +end + +note Creator: wait up to interval ns for deaths and, when acks is true, acks. +note Ack counts are left pending for sup_take. Every worker whose life pipe +note hung up is reaped. Returns the number reaped, or a negative errno. +fn sup_pollx c long interval long acks bool to long +n = mapget c 48 8 +m = ulong 0 +for w ulong n bound 64 +if gt get swpid w 0 +o = mul m 16 +if acks +sup_gpfd o get swack w +else +sup_gpfd o neg int 1 +end +sup_gpfd add o 8 get swlife w +put swpw m w +m = add m 1 +end +end +if eq m 0 +ret 0 +end +r = pollfds swpoll 0 mul m 16 interval +if lt r 0 +ret r +end +dead = long 0 +for j ulong m bound 64 +o = add mul j 16 14 +if ne get swpoll o 0 +r = sup_reap c get swpw j +if lt r 0 +ret r +end +dead = add dead 1 +end +end +ret dead +end + +note Creator: wait up to interval ns for acks and deaths (see sup_pollx). +fn sup_poll c long interval long to long +ret sup_pollx c interval true +end + +note Creator: stop admission, post every live worker's stop eventfd, then reap +note until all have exited or interval ns pass; survivors get SIGKILL and are +note reaped. Returns how many had to be killed, or a refusal. +fn sup_stop c long interval long to long +r = pool_stop c +if lt r 0 +ret r +end +n = mapget c 48 8 +for w ulong n bound 64 +if gt get swpid w 0 +r = node_notify get swstop w +end +end +dl = add monons interval +for t ulong 100000 +if eq sup_live c 0 +ret 0 +end +now = monons +if ge now dl +break +end +r = sup_pollx c sub dl now false +if lt r 0 +if ne r neg long 4 +break +end +end +end +killed = long 0 +for w ulong n bound 64 +if gt get swpid w 0 +r = killpid int get swpid w 9 +r = sup_reap c w +killed = add killed 1 +end +end +ret killed +end +note Omega Node cooperative scheduling (DRA-287). Concatenate after +note transport.om, pool.om and super.om, before one program with fn main. +note One creator admits, assigns, collects and reviews jobs. Workers never +note write each other's state, and no step waits for every worker. +note Scheduling page: one 128-byte cell per worker w at 128*w. +note Creator writes 0 assigned seq, 8 job id, 16 input epoch, 24 input +note version, 32 interest topics, 40 wake stamp (monons at the last post). +note Worker writes 64 done seq, 72 outcome, 80 wakes, 88 last wake latency +note ns, 96 largest wake latency ns. +note Outbox: worker w's result bytes at 4096*w of a second mapping, written +note before its done seq. Each word has one writer; the creator writes a +note worker word only after it has reaped that worker. +note Job states: 0 free, 1 waiting for deps, 2 ready, 3 running, 4 in review, +note 6 UNKNOWN (its worker died mid-job and the job is not safe to rerun). +const SJCAP ulong 64 +global scmap long 2 +global sclim ulong 2 +global sjst ulong 64 +global sjdep ulong 64 +global sjid ulong 64 +global sjw ulong 64 +global sjatt ulong 64 +global sjfl ulong 64 +global sjout ulong 64 +global scbusy ulong 64 +global scseq ulong 64 +global swcur ulong 2 +global scpause ulong 1 + +fn sc_cell w ulong off ulong to ulong +ret mapget get scmap 0 add mul w 128 off 8 +end + +fn sc_set w ulong off ulong v ulong to void +r = mapset get scmap 0 add mul w 128 off 8 v +end + +note Creator, before any fork: map the scheduling page and the outboxes and +note set the limits on admitted (a) and in-review (rl) jobs, each 1..64. +note Returns 0, -6 not creator, -7 bad limit, or a mapping errno. +fn sched_new c long a ulong rl ulong to long +if not pool_mine c +ret neg long 6 +end +if or eq a 0 gt a SJCAP +ret neg long 7 +end +if or eq rl 0 gt rl SJCAP +ret neg long 7 +end +s = mapnew neg int 1 0 8192 3 33 +if le s 0 +ret s +end +o = mapnew neg int 1 0 262144 3 33 +if le o 0 +r = mapdrop s +ret o +end +put scmap 0 s +put scmap 1 o +put sclim 0 a +put sclim 1 rl +for j ulong 64 +put sjst j 0 +put scbusy j 0 +put scseq j 0 +end +put scpause 0 0 +ret 0 +end + +note Creator: drop this process's scheduling mappings. +fn sched_free to long +r = mapdrop get scmap 1 +if lt r 0 +ret r +end +ret mapdrop get scmap 0 +end + +note Jobs whose state is in lo..hi. +fn sched_count lo ulong hi ulong to ulong +n = ulong 0 +for j ulong 64 +s = get sjst j +if and ge s lo le s hi +n = add n 1 +end +end +ret n +end + +note Creator: admit job id. deps 0 makes it ready at once; otherwise it waits +note for a publication whose topics meet deps. flags bit 0: safe to rerun +note after its worker dies. Returns the job slot, or -2 when admitted work +note (waiting, ready, running) is at its limit: refused, not queued. +fn sched_admit id ulong deps ulong flags ulong to long +if ge sched_count 1 3 get sclim 0 +ret neg long 2 +end +for j ulong 64 +if eq get sjst j 0 +put sjid j id +put sjdep j deps +put sjfl j flags +put sjatt j 0 +put sjout j 0 +put sjw j 0 +if eq deps 0 +put sjst j 2 +else +put sjst j 1 +end +ret long j +end +end +ret neg long 2 +end + +note Creator: worker w is woken by publications whose topics meet topics. +fn sched_want w ulong topics ulong to long +if ge w 64 +ret neg long 7 +end +sc_set w 32 topics +ret 0 +end + +fn sc_wake w ulong to void +sc_set w 40 ulong monons +r = sup_wake w +end + +note Creator: give ready jobs to idle live workers, lowest slot first. A +note worker is idle when the creator has collected its last assignment. +note The job id and the input epoch and version are written before the seq. +note Returns the number assigned. +fn sched_dispatch c long to ulong +if ne get scpause 0 0 +ret 0 +end +n = mapget c 48 8 +k = ulong 0 +for j ulong 64 +if eq get sjst j 2 +for w ulong n bound 64 +if and gt get swpid w 0 eq get scbusy w 0 +q = add get scseq w 1 +put scseq w q +put scbusy w add j 1 +put sjst j 3 +put sjw j w +put sjatt j add get sjatt j 1 +sc_set w 8 get sjid j +sc_set w 16 pool_epoch c +sc_set w 24 node_count pool_current c +sc_set w 0 q +sc_wake w +k = add k 1 +break +end +end +end +end +ret k +end + +note Creator: move finished jobs into review while review has room. A worker +note whose result is waiting for room keeps it and gets no new job, so a +note full review queue stops assignment instead of dropping work. +fn sched_collect c long to ulong +n = mapget c 48 8 +k = ulong 0 +for w ulong n bound 64 +b = get scbusy w +if gt b 0 +if eq sc_cell w 64 get scseq w +if ge sched_count 4 4 get sclim 1 +ret k +end +j = sub b 1 +put sjst j 4 +put sjout j sc_cell w 72 +put scbusy w 0 +k = add k 1 +end +end +end +ret k +end + +note Creator, after worker w was reaped: retire its unfinished assignment. +note A rerunnable job is ready again; any other becomes UNKNOWN (6), since +note it may have had effects. A finished result is left for sched_collect. +note Returns 1 when an assignment was retired, 0 when none, -16 still live. +fn sched_lost c long w ulong to long +if ge w 64 +ret neg long 7 +end +if gt get swpid w 0 +ret neg long 16 +end +b = get scbusy w +if eq b 0 +ret 0 +end +if eq sc_cell w 64 get scseq w +ret 0 +end +j = sub b 1 +sc_set w 64 get scseq w +put scbusy w 0 +if ne and get sjfl j 1 0 +put sjst j 2 +else +put sjst j 6 +end +ret 1 +end + +note Creator loop step: wait up to interval ns for an ack or a death, take +note acks, retire the jobs of reaped workers, collect, then dispatch. +note Returns the number of workers reaped, or a negative errno. +fn sched_step c long interval long to long +d = sup_poll c interval +if lt d 0 +ret d +end +n = mapget c 48 8 +for w ulong n bound 64 +if gt get swpid w 0 +r = sup_take get swack w +else +r = sched_lost c w +end +end +k = sched_collect c +k = sched_dispatch c +ret d +end + +note Creator: publish an artifact, make waiting jobs whose deps meet topics +note ready, wake each live worker whose interest meets topics, dispatch. +note Returns the version, or the publish refusal with nothing woken. +fn sched_publish c long topics ulong src view uchar 4096 n ulong to long +v = pool_publish c topics src n +if le v 0 +ret v +end +for j ulong 64 +if eq get sjst j 1 +if ne and get sjdep j topics 0 +put sjst j 2 +end +end +end +m = mapget c 48 8 +for w ulong m bound 64 +if gt get swpid w 0 +if ne and sc_cell w 32 topics 0 +sc_wake w +end +end +end +k = sched_dispatch c +ret v +end + +note Creator: accept the result of job j in review and free its slot. +note Returns its outcome, or -3 when j is not in review. +fn sched_review j ulong to long +if ge j 64 +ret neg long 3 +end +if ne get sjst j 4 +ret neg long 3 +end +put sjst j 0 +ret long get sjout j +end + +note Creator: free an UNKNOWN job once its outcome has been settled +note elsewhere. Returns 0, or -3 when j is not UNKNOWN. +fn sched_settle j ulong to long +if ge j 64 +ret neg long 3 +end +if ne get sjst j 6 +ret neg long 3 +end +put sjst j 0 +ret 0 +end + +note Creator: while paused (on true) no job is assigned; running jobs go on +note and finished results are still collected. Resuming dispatches at once. +note Returns the number assigned on resume, else 0. +fn sched_pause c long on bool to ulong +if on +put scpause 0 1 +ret 0 +end +put scpause 0 0 +ret sched_dispatch c +end + +note Creator: cancel job j. A waiting or ready job never started, so it is +note freed with no effect: 1. A running job needs its worker: 2, and the +note outcome comes from the worker. A job in review already finished: 3, +note nothing is cancelled. UNKNOWN stays UNKNOWN: 4. -3 free or bad slot. +fn sched_cancel j ulong to long +if ge j 64 +ret neg long 3 +end +s = get sjst j +if or eq s 1 eq s 2 +put sjst j 0 +ret 1 +end +if eq s 3 +ret 2 +end +if eq s 4 +ret 3 +end +if eq s 6 +ret 4 +end +ret neg long 3 +end + +note Creator: stop every worker within interval ns (sup_stop), retire the +note assignments of the reaped workers, collect finished results. +note Returns how many workers had to be killed, or a refusal. +fn sched_stop c long interval long to long +k = sup_stop c interval +if lt k 0 +ret k +end +n = mapget c 48 8 +for w ulong n bound 64 +r = sched_lost c w +end +m = sched_collect c +ret k +end + +note Worker w: the assignment seq when a job is waiting, else 0. Job id, +note input epoch and input version are words 8, 16 and 24 of its cell. +fn sched_wjob w ulong to ulong +q = sc_cell w 0 +if gt q sc_cell w 64 +ret q +end +ret 0 +end + +note Worker w: finish assignment q with outcome and the first n (at most +note 4096) bytes of src. Bytes and outcome are written before the done seq; +note then the creator gets an ack. 0, -7 bad n, or the ack refusal. +fn sched_wdone w ulong q ulong outcome ulong src view uchar 4096 n ulong to long +if gt n 4096 +ret neg long 7 +end +if gt n 0 +r = mapwrite get scmap 1 mul w 4096 src 0 n +end +sc_set w 72 outcome +sc_set w 64 q +ret sup_ack w +end + +note Creator: copy worker w's last result bytes into dst. +fn sched_result w ulong dst edit uchar 4096 to long +ret mapread get scmap 1 mul w 4096 dst 0 4096 +end + +note Worker w: sup_wwait that records its wakes and wake latency. +fn sched_wwait c long w ulong interval long to long +r = sup_wwait c w interval +if eq r 1 +now = ulong monons +st = sc_cell w 40 +lat = ulong 0 +if gt now st +lat = sub now st +end +sc_set w 80 add sc_cell w 80 1 +sc_set w 88 lat +if gt lat sc_cell w 96 +sc_set w 96 lat +end +end +ret r +end + +note Worker w: count the versions of the current epoch that meet its +note interest and are newer than its cursor, moving the cursor past them. +note A new epoch restarts the cursor at 0. A worker with no interest scans +note nothing. Returns the count or a refusal. +fn sched_wscan c long w ulong to long +t = sc_cell w 32 +if eq t 0 +ret 0 +end +h = pool_acquire c w +if le h 0 +ret h +end +e = mapget c add 256 mul w 8 8 +if ne get swcur 0 e +put swcur 0 e +put swcur 1 0 +end +k = long 0 +for i ulong 65 +v = node_next h get swcur 1 t +if lt v 0 +r = pool_release c w +ret v +end +if eq v 0 +break +end +put swcur 1 ulong v +k = add k 1 +end +r = pool_release c w +ret k +end +note SPDX-License-Identifier: AGPL-3.0-only +note OM-CONTROL-659 action contract v1. See ../README.md for the trusted-caller boundary. +note No I/O, authority minting or persistence. Caller serializes and durably commits transitions. +note Record words 0..7: version,state,attempt,limit,generation,epoch,expiry,reserved. +note Words 8..31: operation,request,scope,provider,policy,grant identities, four words each. +note State 1 ready,2 in-flight,3 unknown,4 performed. No timeout implies not performed. + +fn actvalid r view ulong 32 to ulong +if ne get r 0 1 +ret 11 +end +if or lt get r 1 1 gt get r 1 4 +ret 12 +end +if or eq get r 3 0 gt get r 3 1024 +ret 12 +end +if gt get r 2 get r 3 +ret 12 +end +if and ne get r 1 1 eq get r 2 0 +ret 12 +end +if or eq get r 4 0 eq get r 5 0 +ret 12 +end +if or eq get r 6 0 ne get r 7 0 +ret 12 +end +for g ulong 6 +bits = ulong 0 +for j ulong 4 +bits = or bits get r add 8 add mul g 4 j +end +if eq bits 0 +ret 12 +end +end +ret 0 +end + +fn actbind r view ulong 32 c view ulong 32 to ulong +for g ulong 6 +diff = ulong 0 +for j ulong 4 +at = add 8 add mul g 4 j +diff = or diff xor get r at get c at +end +if ne diff 0 +ret add 40 g +end +end +ret 0 +end + +fn actauth r view ulong 32 c view ulong 32 to ulong +if ne get c 0 1 +ret 11 +end +if or ne get c 3 0 or ne get c 6 0 ne get c 7 0 +ret 12 +end +if ne get c 1 1 +ret 30 +end +if ne get r 4 get c 4 +ret 31 +end +if ne get r 5 get c 5 +ret 32 +end +if ge get c 2 get r 6 +ret 33 +end +ret actbind r c +end + +fn actstart r edit ulong 32 c view ulong 32 to ulong +err = actvalid r +if ne err 0 +ret err +end +if eq get r 1 3 +ret 21 +end +if eq get r 1 4 +ret 22 +end +if ne get r 1 1 +ret 20 +end +if ge get r 2 get r 3 +ret 23 +end +err = actauth r c +if ne err 0 +ret err +end +put r 2 add get r 2 1 +put r 1 2 +ret 0 +end + +fn actlost r edit ulong 32 to ulong +err = actvalid r +if ne err 0 +ret err +end +if eq get r 1 3 +ret 0 +end +if ne get r 1 2 +ret 20 +end +put r 1 3 +ret 0 +end + +note A trusted receipt maps outcome 0 unknown,1 definitely not performed,2 performed. +note Not-found or timeout is outcome 0 unless the provider proves absence and quiescence. +note Receipt words 2/3: exact attempt and verification=1. Words 6/7 must be zero. +note Recording an observed outcome is permitted after expiry; another attempt is not. +fn actrecon r edit ulong 32 s view ulong 32 to ulong +err = actvalid r +if ne err 0 +ret err +end +if not or eq get r 1 2 eq get r 1 3 +ret 20 +end +if ne get s 0 1 +ret 11 +end +if or gt get s 1 2 or ne get s 6 0 ne get s 7 0 +ret 12 +end +if ne get s 3 1 +ret 36 +end +if ne get s 2 get r 2 +ret 35 +end +if ne get s 4 get r 4 +ret 31 +end +if ne get s 5 get r 5 +ret 32 +end +err = actbind r s +if ne err 0 +ret err +end +if eq get s 1 0 +put r 1 3 +ret 21 +end +if eq get s 1 1 +put r 1 1 +else +put r 1 4 +end +ret 0 +end +note Omega Node durable journal and restart recovery (DRA-289). Concatenate +note after transport.om, pool.om, super.om, sched.om and +note seed/authority/action.om, before auth.om, which journals through it. +note While no journal is open every append succeeds without writing, so the +note authority layer also runs memory-only. +note File DIR/journal: a 64-byte header, then 128-byte records. Header words: +note 0 magic, 1 format version 1, 2 record size 128, 3..6 objective identity, +note 7 FNV-1a 64 of words 0..6. Record words: 0 type, 1 seq (1, 2, 3, ... +note with no gap), 2 fence, 3..14 payload, 15 FNV-1a 64 of words 0..14. +note Types and payloads: +note 1 OPEN 3 pid, 4 authority generation of the new creator +note 2 ADMIT 3 job id, 4 dependency topics, 5 flags +note 3 START 3..6 operation identity, 7 class, 8 job id, 9 attempt, 10 gen +note 4 DONE 3..6 operation identity, 7 outcome (0 UNKNOWN, 1 not +note performed, 2 performed), 8 job id, 9 how (1 reported, +note 2 worker lost, 3 in flight at restart, 4 key present, +note 5 key absent, 6 no external effect) +note 5 RELEASE 3 job id, 4 outcome (the job's outcome; 300 cancelled) +note 6 REVOKE 3 new generation +note 7 PAUSE 3 1 paused, 0 resumed +note 8 CANCEL 3 job id +note Order: a transition is appended and fdatasync'd before anything acts on +note it or publishes it. An append whose write or sync fails marks the journal +note failed and every later append is refused, so nothing is acted on that +note might not be durable. +note Fencing: DIR/lock is held with flock(LOCK_EX) for the creator's life. It +note is opened close-on-exec, so tools never hold it, but forked workers do: +note a new creator cannot open the journal until every worker of the old one +note has exited. Each open appends OPEN with the next fence number, and every +note record carries the fence of the creator that wrote it. +note Torn tail: a partial last record, or a last full record whose checksum or +note seq is wrong, was never acknowledged and is cut off. A bad record that is +note not the last is corruption: the open is refused and nothing is changed. +note Compatibility: a header whose magic, version, record size or checksum +note differs is refused (21), never reinterpreted; an objective identity that +note differs is refused (22). +text jnjour "/journal" +text jnlock "/lock" +text jnkey "/k." +global jon ulong 1 +global jbad ulong 1 +global jfd int 1 +global jlk int 1 +global jseq ulong 1 +global jsz ulong 1 +global jfence ulong 1 +global jgen ulong 1 +global jcrash ulong 1 +global jdir uchar 128 +global jdn ulong 1 +global jpath uchar 160 +global jact ulong 1024 +global jjob ulong 2048 +global jstat ulong 8 + +note Fault injection for the recovery tests: exit 77 at point p when armed. +fn jn_hit p ulong to void +if and gt p 0 eq get jcrash 0 p +exit 77 +end +end + +fn jn_sum r view ulong 16 n ulong to ulong +h = ulong 14695981039346656037 +for i ulong n bound 16 +v = get r i +for b ulong 8 +h = mul xor h and shr v mul b 8 255 1099511628211 +end +end +ret h +end + +fn jn_enc r view ulong 16 b edit uchar 128 to void +for i ulong 16 +v = get r i +for k ulong 8 +put b add mul i 8 k uchar and shr v mul k 8 255 +end +end +end + +fn jn_dec b view uchar 128 r edit ulong 16 to void +for i ulong 16 +v = ulong 0 +for k ulong 8 +v = or v shl ulong get b add mul i 8 k mul k 8 +end +put r i v +end +end + +note DIR plus name k (0 journal, 1 lock, 2 key prefix) in jpath; the length. +fn jn_name k ulong to ulong +n = get jdn 0 +for i ulong n bound 128 +put jpath i get jdir i +end +m = len jnjour +if eq k 1 +m = len jnlock +end +if eq k 2 +m = len jnkey +end +for i ulong m bound 16 +b = get jnkey 0 +if eq k 0 +b = get jnjour i +end +if eq k 1 +b = get jnlock i +end +if eq k 2 +b = get jnkey i +end +put jpath add n i b +end +ret add n m +end + +note The key path DIR/k. of an idempotent effect; the length. +fn jn_keyp v ulong to ulong +n = jn_name 2 +d = new uchar 20 0 +x = v +k = ulong 20 +for i ulong 20 +k = sub k 1 +put d k uchar add rem x 10 48 +x = div x 10 +if eq x 0 +break +end +end +for i ulong sub 20 k bound 20 +put jpath add n i get d add k i +end +ret add n sub 20 k +end + +fn jn_reset to void +for i ulong 1024 +put jact i 0 +end +for i ulong 2048 +put jjob i 0 +end +for i ulong 8 +put jstat i 0 +end +put jon 0 0 +put jbad 0 0 +put jseq 0 0 +put jsz 0 0 +put jfence 0 0 +put jgen 0 0 +end + +note Action table, 64 entries of 16 words: 0 used, 1..4 operation identity, +note 5 class, 6 state (as action.om: 1 ready, 2 in flight, 3 UNKNOWN, +note 4 performed), 7 attempt, 8 gen, 9 job id. -1 when absent. +fn jn_afind r view ulong 16 to long +for k ulong 64 +b = mul k 16 +if ne get jact b 0 +m = true +for i ulong 4 +if ne get jact add b add 1 i get r add 3 i +m = false +end +end +if m +ret long k +end +end +end +ret neg long 1 +end + +note Job table, 256 entries of 8 words: 0 used, 1 job id, 2 deps, 3 flags, +note 4 released, 5 outcome, 6 cancelled, 7 action entry + 1 of its last START. +fn jn_jfind id ulong to long +for k ulong 256 +if and ne get jjob mul k 8 0 eq get jjob add mul k 8 1 id +ret long k +end +end +ret neg long 1 +end + +fn jn_jnew id ulong to long +for k ulong 256 +b = mul k 8 +if eq get jjob b 0 +for i ulong 8 +put jjob add b i 0 +end +put jjob b 1 +put jjob add b 1 id +ret long k +end +end +ret neg long 1 +end + +fn jn_maxg g ulong to void +if gt g get jgen 0 +put jgen 0 g +end +end + +fn jn_astart r view ulong 16 to ulong +k = jn_afind r +if lt k 0 +for i ulong 64 +if eq get jact mul i 16 0 +k = long i +break +end +end +end +if lt k 0 +ret 25 +end +b = mul ulong k 16 +if ne get jact b 0 +if ne get jact add b 6 1 +ret 25 +end +end +put jact b 1 +for i ulong 4 +put jact add b add 1 i get r add 3 i +end +put jact add b 5 get r 7 +put jact add b 6 2 +put jact add b 7 get r 9 +put jact add b 8 get r 10 +put jact add b 9 get r 8 +jn_maxg get r 10 +j = jn_jfind get r 8 +if ge j 0 +put jjob add mul ulong j 8 7 add ulong k 1 +end +ret 0 +end + +fn jn_adone r view ulong 16 to ulong +k = jn_afind r +if lt k 0 +ret 25 +end +b = mul ulong k 16 +s = get jact add b 6 +if not or eq s 2 eq s 3 +ret 25 +end +o = get r 7 +if gt o 2 +ret 25 +end +v = ulong 3 +if eq o 1 +v = 1 +end +if eq o 2 +v = 4 +end +put jact add b 6 v +ret 0 +end + +note Apply one durable record to the recovery tables; 0, 24 unknown type or +note 25 a transition the tables refuse (START of a performed operation, a +note DONE without a START, a second ADMIT or RELEASE). +fn jn_apply r view ulong 16 to ulong +t = get r 0 +if eq t 1 +put jfence 0 get r 2 +jn_maxg get r 4 +ret 0 +end +if eq t 2 +if ge jn_jfind get r 3 0 +ret 25 +end +j = jn_jnew get r 3 +if lt j 0 +ret 25 +end +put jjob add mul ulong j 8 2 get r 4 +put jjob add mul ulong j 8 3 get r 5 +ret 0 +end +if eq t 3 +ret jn_astart r +end +if eq t 4 +ret jn_adone r +end +if or eq t 5 eq t 8 +j = jn_jfind get r 3 +if lt j 0 +ret 25 +end +b = mul ulong j 8 +if eq t 8 +put jjob add b 6 1 +ret 0 +end +if ne get jjob add b 4 0 +ret 25 +end +put jjob add b 4 1 +put jjob add b 5 get r 4 +ret 0 +end +if eq t 6 +jn_maxg get r 3 +ret 0 +end +if eq t 7 +put jstat 2 get r 3 +ret 0 +end +ret 24 +end + +fn jn_le64 b view uchar 128 off ulong to ulong +v = ulong 0 +for k ulong 8 +v = or v shl ulong get b add off k mul k 8 +end +ret v +end + +note Make DIR's entry for a new journal durable. +fn jn_dsync to ulong +fd = fopen jdir 0 get jdn 0 540672 0 +if lt fd 0 +ret 27 +end +r = fsync int fd +x = fclose int fd +if ne r 0 +ret 27 +end +ret 0 +end + +note Create the header of an empty journal, or check an existing one. +fn jn_head fd int obj view ulong 4 to ulong +st = new uchar 128 0 +h = new ulong 16 0 +b = new uchar 128 0 +if ne fstat fd st 0 128 0 +ret 27 +end +size = jn_le64 st 48 +if lt size 64 +if ne ftrunc fd 0 0 +ret 27 +end +put h 0 5786624599 +put h 1 1 +put h 2 128 +for i ulong 4 +put h add 3 i get obj i +end +put h 7 jn_sum h 7 +jn_enc h b +if ne pwrite fd b 0 64 0 64 +ret 27 +end +if ne fsync fd 0 +ret 27 +end +put jsz 0 64 +ret jn_dsync +end +if ne pread fd b 0 64 0 64 +ret 27 +end +jn_dec b h +s = jn_sum h 7 +if or ne get h 0 5786624599 ne get h 7 s +ret 21 +end +if or ne get h 1 1 ne get h 2 128 +ret 21 +end +for i ulong 4 +if ne get h add 3 i get obj i +ret 22 +end +end +put jsz 0 size +ret 0 +end + +fn jn_cut fd int off ulong to ulong +if ne ftrunc fd long off 0 +ret 27 +end +if ne fdsync fd 0 +ret 27 +end +put jstat 0 add get jstat 0 sub get jsz 0 off +put jsz 0 off +ret 0 +end + +note Record i into r: 0 well formed (checksum and seq), 1 not, 27 I/O. +fn jn_rd fd int i ulong r edit ulong 16 to ulong +b = new uchar 128 0 +off = long add 64 mul i 128 +if ne pread fd b 0 128 off 128 +ret 27 +end +jn_dec b r +s = jn_sum r 15 +if ne get r 15 s +ret 1 +end +if ne get r 1 add i 1 +ret 1 +end +ret 0 +end + +note Read every record into the tables; cut a torn tail; refuse corruption. +fn jn_scan fd int to ulong +r = new ulong 16 0 +n = div sub get jsz 0 64 128 +rm = rem sub get jsz 0 64 128 +for i ulong n bound 10000000 +g = jn_rd fd i r +if eq g 27 +ret 27 +end +if eq g 1 +if and eq add i 1 n eq rm 0 +ret jn_cut fd add 64 mul i 128 +end +ret 23 +end +e = jn_apply r +if ne e 0 +ret e +end +put jseq 0 add i 1 +put jstat 1 add i 1 +end +if gt rm 0 +ret jn_cut fd add 64 mul n 128 +end +ret 0 +end + +note Append record r (caller sets word 0 and the payload) and make it +note durable. The tables check and take the transition first (jn_apply +note changes nothing when it refuses), so a refused transition is never +note written. 0, a table refusal, 28 write, 29 sync, 30 failed earlier. +note After 28 or 29 the journal is failed: the transition may or may not be +note durable, so the caller must not act on it, and nothing more is written. +note Without an open journal: 0. +fn jn_append r edit ulong 16 to ulong +if eq get jon 0 0 +ret 0 +end +if ne get jbad 0 0 +ret 30 +end +b = new uchar 128 0 +fd = get jfd 0 +put r 1 add get jseq 0 1 +put r 2 get jfence 0 +put r 15 jn_sum r 15 +e = jn_apply r +if ne e 0 +ret e +end +jn_enc r b +off = long add 64 mul get jseq 0 128 +if and eq get jcrash 0 6 eq get r 0 4 +x = pwrite fd b 0 60 off +exit 77 +end +if ne pwrite fd b 0 128 off 128 +put jbad 0 1 +ret 28 +end +if ne fdsync fd 0 +put jbad 0 1 +ret 29 +end +put jseq 0 add get jseq 0 1 +put jsz 0 add get jsz 0 128 +ret 0 +end + +fn jn_rec t ulong a ulong b ulong c ulong to ulong +r = new ulong 16 0 +put r 0 t +put r 3 a +put r 4 b +put r 5 c +ret jn_append r +end + +note Open (or create) the journal in directory dir (dn bytes) for objective +note obj, waiting up to ms for the lock. 0, 20 fenced (another creator or +note its workers hold the lock), 21 incompatible, 22 other objective, 23 +note corrupt, 24/25 refused record, 26 path too long, 27 I/O error. +fn jn_open dir view uchar 128 dn ulong obj view ulong 4 ms ulong to ulong +if or eq dn 0 gt dn 100 +ret 26 +end +jn_reset +for i ulong dn bound 128 +put jdir i get dir i +end +put jdn 0 dn +r = makedir jdir 0 dn 448 +if and lt r 0 ne r neg long 17 +ret 27 +end +n = jn_name 1 +lk = fopen jpath 0 n 524354 384 +if lt lk 0 +ret 27 +end +got = false +for t ulong add ms 1 bound 100000 +f = flock int lk 6 +if eq f 0 +got = true +break +end +if ne f neg long 11 +break +end +x = sleepns 1000000 +end +if not got +x = fclose int lk +ret 20 +end +put jlk 0 int lk +e = jn_load obj +if ne e 0 +x = fclose int lk +ret e +end +ret 0 +end + +fn jn_load obj view ulong 4 to ulong +n = jn_name 0 +fd = fopen jpath 0 n 524354 384 +if lt fd 0 +ret 27 +end +e = jn_head int fd obj +if eq e 0 +e = jn_scan int fd +end +if ne e 0 +x = fclose int fd +ret e +end +put jfd 0 int fd +put jon 0 1 +put jfence 0 add get jfence 0 1 +put jgen 0 add get jgen 0 1 +ret jn_rec 1 ulong getpid get jgen 0 0 +end + +note Close the journal and release the lock (other holders: forked workers). +fn jn_close to void +if ne get jon 0 0 +x = fclose get jfd 0 +x = fclose get jlk 0 +end +put jon 0 0 +end + +note Probe the key of an idempotent operation: 1 present, 0 absent, 2 error. +fn jn_probe v ulong to ulong +st = new uchar 128 0 +n = jn_keyp v +r = stat jpath 0 n st 0 128 +if eq r 0 +ret 1 +end +if eq r neg long 2 +ret 0 +end +ret 2 +end + +note Close every START left in flight by a crash with a DONE. Class 3 (an +note idempotent effect whose key DIR/k. is created atomically by +note the effect) is decided by its key; class 2 (no external effect) was not +note performed; anything else is UNKNOWN and is never run again. +fn jn_settle to ulong +r = new ulong 16 0 +for k ulong 64 +b = mul k 16 +if and ne get jact b 0 eq get jact add b 6 2 +for i ulong 4 +put r add 3 i get jact add b add 1 i +end +put r 0 4 +put r 8 get jact add b 9 +cl = get jact add b 5 +put r 7 0 +put r 9 3 +if eq cl 2 +put r 7 1 +put r 9 6 +end +if eq cl 3 +p = jn_probe get r 3 +if eq p 2 +ret 27 +end +put r 7 add 1 p +put r 9 sub 5 p +end +e = jn_append r +if ne e 0 +ret e +end +put jstat 3 add get jstat 3 1 +end +end +ret 0 +end + +note Creator, after jn_open and sched_new: settle in-flight operations, then +note for each admitted job not yet released: a performed or UNKNOWN last +note operation releases it with that outcome (it never runs again); a +note cancelled job is released as cancelled; any other is admitted again. +note 0 or an error. +fn jn_recover c long to ulong +e = jn_settle +if ne e 0 +ret e +end +for k ulong 256 +b = mul k 8 +if and ne get jjob b 0 eq get jjob add b 4 0 +o = ulong 999 +a = get jjob add b 7 +if gt a 0 +s = get jact add mul sub a 1 16 6 +if eq s 4 +o = 2 +end +if eq s 3 +o = 0 +end +end +if and eq o 999 ne get jjob add b 6 0 +o = 300 +end +if eq o 999 +x = sched_admit get jjob add b 1 get jjob add b 2 get jjob add b 3 +if lt x 0 +ret 31 +end +put jstat 5 add get jstat 5 1 +else +e = jn_rec 5 get jjob add b 1 o 0 +if ne e 0 +ret e +end +put jstat 4 add get jstat 4 1 +end +end +end +ret 0 +end + +note Admit job id durably: ADMIT is appended before sched_admit. -2 at the +note admission limit, -4 an id already in the journal, -5 the journal +note refused, else sched_admit's slot. +fn jn_admit id ulong deps ulong flags ulong to long +if ge sched_count 1 3 get sclim 0 +ret neg long 2 +end +if ge jn_jfind id 0 +ret neg long 4 +end +if ne jn_rec 2 id deps flags 0 +ret neg long 5 +end +jn_hit 1 +ret sched_admit id deps flags +end + +note Release reviewed job slot j durably: RELEASE before sched_review. -5 +note while its operation's DONE is not yet durable or on refusal. +fn jn_release j ulong to long +id = get sjid j +k = jn_jfind id +if ge k 0 +a = get jjob add mul ulong k 8 7 +if gt a 0 +if eq get jact add mul sub a 1 16 6 2 +ret neg long 5 +end +end +end +if ne jn_rec 5 id get sjout j 0 0 +ret neg long 5 +end +jn_hit 5 +ret sched_review j +end + +fn jn_start op view ulong 4 cl ulong id ulong at ulong g ulong to ulong +r = new ulong 16 0 +put r 0 3 +for i ulong 4 +put r add 3 i get op i +end +put r 7 cl +put r 8 id +put r 9 at +put r 10 g +ret jn_append r +end + +fn jn_done op view ulong 4 o ulong id ulong how ulong to ulong +r = new ulong 16 0 +put r 0 4 +for i ulong 4 +put r add 3 i get op i +end +put r 7 o +put r 8 id +put r 9 how +ret jn_append r +end + +note Getters: whether job id is in the journal, released, its outcome; a +note recovery counter (0 torn bytes cut, 1 records read, 2 paused, 3 settled, +note 4 released at recovery, 5 admitted again); the fence; the generation. +fn jn_known id ulong to bool +ret ge jn_jfind id 0 +end + +fn jn_done_j id ulong to bool +k = jn_jfind id +if lt k 0 +ret false +end +ret ne get jjob add mul ulong k 8 4 0 +end + +fn jn_jout id ulong to ulong +k = jn_jfind id +if lt k 0 +ret 999 +end +ret get jjob add mul ulong k 8 5 +end + +fn jn_stat i ulong to ulong +ret get jstat and i 7 +end + +fn jn_fence to ulong +ret get jfence 0 +end + +fn jn_gen to ulong +ret get jgen 0 +end + +note Action table entry k word i (k < 64, i < 16). +fn jn_act k ulong i ulong to ulong +ret get jact add mul and k 63 16 and i 15 +end +note Omega Node proposals, authority and cancellation (DRA-288). Concatenate +note after transport.om, pool.om, super.om, sched.om and +note seed/authority/action.om, before one program with fn main. +note A worker proposes and the creator alone decides and commits. A proposal +note is typed and bound to its operation identity, the job id and +note assignment seq, the worker slot and incarnation, the input epoch and +note version it read, and the authority generation it holds. Every effect is +note one action record of seed/authority/action.om per operation identity: +note started with actstart, closed with actrecon or actlost. A performed +note operation is never started again and an UNKNOWN one is never retried. +note Control page (16384 bytes), cell of worker w at 128*w. Creator writes +note 0 control seq, 8 cancel target (assignment seq, 0 none), 16 decided +note proposal seq, 24 verdict, 32 generation at the decision. Worker +note writes 64 control ack seq, 72 proposal seq, 80 report seq, 88 report +note outcome, 96 tool wait status, 104 paused as last seen, 112 running tool +note pid (its process group), 120 tool output bytes held. Word 8192 mirrors +note the authority generation and 8200 the paused flag. +note Proposal page: 4096 bytes per worker at 4096*w. Words 0..15: 0 version +note 1, 1 kind 1, 2 job id, 3 assignment seq, 4 slot, 5 incarnation, 6 input +note epoch, 7 input version, 8 generation, 9..12 operation identity, 13 class +note (1 external effect, 2 no external effect, 3 idempotent external effect +note whose completion is visible as a key, see journal.om), 14 and 15 zero. +note Bytes 128..4095 hold the running tool's output. +note Verdicts: 0 granted, 1 malformed, 2 not the worker's assignment, 3 other +note incarnation, 4 stale input, 5 stale authority, 6 cancelled, 7 already +note performed or UNKNOWN, 8 attempt limit, 9 refused by the action contract. +note Outcomes, as action.om receipts: 0 UNKNOWN, 1 not performed, 2 performed. +text audevnul "/dev/null" +global aumap long 2 +global auctl int 64 +global auinc ulong 64 +global augen ulong 1 +global aurec ulong 2048 +global auuse ulong 64 +global aujob ulong 64 +global aured ulong 64 +global aupf uchar 8 +global aufd int 1 + +fn au_cell w ulong off ulong to ulong +ret mapget get aumap 0 add mul w 128 off 8 +end + +fn au_set w ulong off ulong v ulong to void +r = mapset get aumap 0 add mul w 128 off 8 v +end + +fn au_pw w ulong i ulong to ulong +ret mapget get aumap 1 add mul w 4096 mul i 8 8 +end + +fn au_spw w ulong i ulong v ulong to void +r = mapset get aumap 1 add mul w 4096 mul i 8 8 v +end + +fn au_pfd off ulong to int +v = ulong get aupf off +v = or v shl ulong get aupf add off 1 8 +v = or v shl ulong get aupf add off 2 16 +v = or v shl ulong get aupf add off 3 24 +ret int v +end + +note Poll record at off; a negative fd keeps its bit pattern, so the kernel +note ignores that record. +fn au_lpfd p edit uchar 40 off ulong fd int to void +u = bitcast uint fd +for i ulong 4 +put p add off i uchar and shr u uint mul i 8 255 +end +put p add off 4 1 +end + +note Creator, after sup_init and before any spawn: map the control and +note proposal pages, one control eventfd per worker slot, generation 1. +note 0, -6 not creator, or a negative errno. +fn auth_new c long to long +if not pool_mine c +ret neg long 6 +end +a = mapnew neg int 1 0 16384 3 33 +if le a 0 +ret a +end +b = mapnew neg int 1 0 262144 3 33 +if le b 0 +r = mapdrop a +ret b +end +put aumap 0 a +put aumap 1 b +for i ulong 64 +put auctl i neg int 1 +put auinc i 0 +put auuse i 0 +put aujob i 0 +put aured i 0 +end +n = mapget c 48 8 +for i ulong n bound 64 +e = eventfd uint 0 +if lt e 0 +x = auth_free +ret e +end +put auctl i int e +end +put augen 0 1 +r = mapset a 8192 8 1 +r = mapset a 8200 8 0 +ret 0 +end + +note Creator: close the control eventfds and drop both pages. +fn auth_free to long +for i ulong 64 +if ge get auctl i 0 +r = fclose get auctl i +end +put auctl i neg int 1 +end +r = mapdrop get aumap 1 +if lt r 0 +ret r +end +ret mapdrop get aumap 0 +end + +note Creator: start a new incarnation of worker slot w (sup_spawn). The child +note inherits its incarnation number. 0 in the child, else as sup_spawn. +fn auth_spawn c long w ulong to long +if ge w 64 +ret neg long 7 +end +if ne get swpid w 0 +ret neg long 16 +end +put auinc w add get auinc w 1 +ret sup_spawn c w +end + +note Creator: post control to worker w. A nonzero target asks it to cancel +note that assignment. The state words go first, then the seq, then the hint. +fn au_ctl w ulong target ulong to void +if gt target 0 +au_set w 8 target +end +au_set w 0 add au_cell w 0 1 +r = node_notify get auctl w +end + +note Worker w: bounded control handler. Records the paused flag it saw and +note acknowledges the control seq; returns the cancel target (0 none). +fn auth_wctl w ulong to ulong +s = au_cell w 0 +au_set w 104 mapget get aumap 0 8200 8 +au_set w 64 s +ret au_cell w 8 +end + +note Worker w: wait up to interval ns. 3 creator gone, 2 stop, 4 control, +note 5 descriptor x readable or hung up (x negative: none), 1 work hint, +note 0 timeout, or a negative errno. Creator loss outranks stop, stop +note outranks control, control outranks output and work. Control and work +note hints are drained; reread the shared state after any return. +fn auth_wwait c long w ulong x int interval long to long +p = new uchar 40 0 +if ge w 64 +ret neg long 7 +end +iv = interval +if pool_stopped c +iv = long 0 +end +au_lpfd p 0 get swwake w +au_lpfd p 8 get swstop w +au_lpfd p 16 get swpar 0 +au_lpfd p 24 get auctl w +au_lpfd p 32 x +r = pollfds p 0 40 iv +if lt r 0 +ret r +end +if ne get p 22 0 +ret 3 +end +if pool_stopped c +ret 2 +end +if ne and get p 14 1 0 +r = node_drain get swstop w +ret 2 +end +if ne get p 30 0 +r = node_drain get auctl w +ret 4 +end +if ne get p 38 0 +ret 5 +end +if ne and get p 6 1 0 +r = node_drain get swwake w +ret 1 +end +ret 0 +end + +note Worker w: propose the effect of assignment q with operation identity +note op and class (1 external effect, 2 none, 3 keyed idempotent). The words go first, then the +note proposal seq, then an ack to the creator. 0 or the ack refusal. +fn auth_propose w ulong q ulong op view ulong 4 class ulong to long +if ge w 64 +ret neg long 7 +end +au_spw w 0 1 +au_spw w 1 1 +au_spw w 2 sc_cell w 8 +au_spw w 3 q +au_spw w 4 w +au_spw w 5 get auinc w +au_spw w 6 sc_cell w 16 +au_spw w 7 sc_cell w 24 +au_spw w 8 mapget get aumap 0 8192 8 +for i ulong 4 +au_spw w add 9 i get op i +end +au_spw w 13 class +au_spw w 14 0 +au_spw w 15 0 +au_set w 72 q +ret sup_ack w +end + +note Worker w: wait up to interval ns for the decision on proposal q, +note handling control meanwhile. The verdict (0 granted), 102 stop, 103 +note creator gone, 110 no decision yet, or a negative errno. +fn auth_wdecide c long w ulong q ulong interval long to long +dl = add ulong monons ulong interval +for it ulong 100000000 +if eq au_cell w 16 q +ret long au_cell w 24 +end +now = ulong monons +if ge now dl +ret 110 +end +r = auth_wwait c w neg int 1 long sub dl now +if lt r 0 +ret r +end +if or eq r 2 eq r 3 +ret add 100 r +end +if eq r 4 +t = auth_wctl w +end +end +ret 110 +end + +note Worker w: fork the packed argv pk (pn bytes) as the leader of a new +note process group, stdin on /dev/null, stdout and stderr on a pipe whose +note read end is left in aufd. Both sides call setpgid, so the group exists +note before either side goes on. The pid, or a negative errno. +fn au_fork w ulong pk view uchar 4096 pn ulong to long +r = pipefds aupf 0 8 524288 +if lt r 0 +ret r +end +rfd = au_pfd 0 +wfd = au_pfd 4 +dn = fopen audevnul 0 len audevnul 524288 0 +pid = fork +if eq pid 0 +r = setpgid 0 0 +r = dupfd wfd 1 0 +r = dupfd wfd 2 0 +if ge dn 0 +r = dupfd int dn 0 0 +end +r = execprog pk 0 pn +exit 127 +end +r = fclose wfd +if ge dn 0 +r = fclose int dn +end +if lt pid 0 +r = fclose rfd +ret pid +end +r = setpgid int pid int pid +au_set w 120 0 +au_set w 112 ulong pid +put aufd 0 rfd +ret pid +end + +note Worker w: move available tool output into its proposal page from byte +note 128 (bytes past 4096 are dropped). false at end of file or on error. +fn au_out w ulong rfd int to bool +buf = new uchar 4096 0 +n = read rfd buf 0 4096 +if eq n 0 +ret false +end +if lt n 0 +ret or eq n neg long 4 eq n neg long 11 +end +k = au_cell w 120 +m = ulong n +if gt add k m 3968 +m = sub 3968 k +end +if gt m 0 +h = get aumap 1 +off = add mul w 4096 add 128 k +r = mapwrite h off buf 0 m +au_set w 120 add k m +end +ret true +end + +note Outcome of a reaped tool from its wait status: it exited, so it ran +note (2); it was killed or crashed, so an external effect is UNKNOWN (0) and +note a class 2 tool had none (1). +fn au_outcome st ulong class ulong to long +if eq and st 127 0 +ret 2 +end +if eq class 2 +ret 1 +end +ret 0 +end + +note Worker w: finish tool pid of assignment q. Without kill, wait for its +note exit while still obeying control. With kill, a tool that has already +note exited is not cancelled; otherwise SIGKILL its whole group. Its wait +note status alone decides the outcome, never the fact that a kill was sent. +note Group members left behind are killed either way. +fn au_end c long w ulong q ulong pid long class ulong kill bool to long +ws = new uchar 4 0 +k = kill +g = long 0 +if not k +for it ulong 100000000 +g = waitpid int pid ws 0 4 1 +if ne g 0 +break +end +r = auth_wwait c w neg int 1 5000000 +if eq r 4 +if eq auth_wctl w q +k = true +break +end +end +if or eq r 2 eq r 3 +k = true +break +end +end +end +if k +g = waitpid int pid ws 0 4 1 +end +if eq g 0 +r = killpid neg int pid 9 +if lt r 0 +r = killpid int pid 9 +end +g = waitpid int pid ws 0 4 0 +else +r = killpid neg int pid 9 +end +r = fclose get aufd 0 +put aufd 0 neg int 1 +au_set w 112 0 +if lt g 0 +au_set w 96 65535 +ret 0 +end +st = or ulong get ws 0 shl ulong get ws 1 8 +au_set w 96 st +ret au_outcome st class +end + +note Worker w: run the granted tool pk of assignment q (see au_fork). Its +note output goes to the proposal page; cancellation of q, stop or creator +note loss kill its process group. Returns the outcome: 1 when q was +note cancelled before the tool started or the fork failed, else from +note au_end. The wait status is in cell word 96. +fn auth_wtool c long w ulong q ulong pk view uchar 4096 pn ulong class ulong to long +if eq auth_wctl w q +ret 1 +end +pid = au_fork w pk pn +if le pid 0 +ret 1 +end +rfd = get aufd 0 +for it ulong 100000000 +r = auth_wwait c w rfd 1000000000 +if eq r 5 +if not au_out w rfd +ret au_end c w q pid class false +end +end +if eq r 4 +if eq auth_wctl w q +ret au_end c w q pid class true +end +end +if or eq r 2 eq r 3 +ret au_end c w q pid class true +end +if lt r 0 +ret au_end c w q pid class true +end +end +ret au_end c w q pid class true +end + +note Worker w: report the outcome of the granted tool of assignment q. +fn auth_wreport w ulong q ulong outcome ulong to long +au_set w 88 outcome +au_set w 80 q +ret sup_ack w +end + +note Creator: the action slot whose operation identity is op, or -1. +fn au_find op view ulong 4 to long +for k ulong 64 +if ne get auuse k 0 +m = true +for i ulong 4 +if ne get aurec add mul k 32 add 8 i get op i +m = false +end +end +if m +ret long k +end +end +end +ret neg long 1 +end + +note Creator: a new ready action record for op and class in a free slot, +note bound to the current generation; -1 when all 64 are in use. +note Identities: operation and request op, scope node, provider class, +note policy 1, grant 1. Attempt limit 3, epoch 1, no expiry in practice. +fn au_alloc op view ulong 4 class ulong to long +for k ulong 64 +if eq get auuse k 0 +b = mul k 32 +for i ulong 32 +put aurec add b i 0 +end +put aurec b 1 +put aurec add b 1 1 +put aurec add b 3 3 +put aurec add b 4 get augen 0 +put aurec add b 5 1 +put aurec add b 6 9223372036854775807 +for i ulong 4 +put aurec add b add 8 i get op i +put aurec add b add 12 i get op i +end +put aurec add b 16 1313817669 +put aurec add b 20 class +put aurec add b 24 1 +put aurec add b 28 1 +put auuse k 1 +ret long k +end +end +ret neg long 1 +end + +fn au_load k ulong r edit ulong 32 to void +for i ulong 32 +put r i get aurec add mul k 32 i +end +end + +fn au_save k ulong r view ulong 32 to void +for i ulong 32 +put aurec add mul k 32 i get r i +end +end + +note Creator: commit the start of the operation proposed by worker w. +note A ready record moves to the current generation; actstart checks the +note state, the attempt limit and the credential. The verdict. +fn au_commit w ulong to ulong +op = new ulong 4 0 +r = new ulong 32 0 +cr = new ulong 32 0 +for i ulong 4 +put op i au_pw w add 9 i +end +k = au_find op +if lt k 0 +k = au_alloc op au_pw w 13 +if lt k 0 +ret 9 +end +end +au_load ulong k r +if and eq get r 1 1 ne get r 4 get augen 0 +put r 4 get augen 0 +end +for i ulong 32 +put cr i get r i +end +put cr 1 1 +put cr 2 1 +put cr 3 0 +put cr 4 get augen 0 +put cr 6 0 +put cr 7 0 +e = actstart r cr +if eq e 0 +au_save ulong k r +put aujob w add ulong k 1 +ret 0 +end +if or eq e 21 eq e 22 +ret 7 +end +if eq e 23 +ret 8 +end +if eq e 31 +ret 5 +end +ret 9 +end + +note Creator: a proposal is stale when the input it read is not the +note assignment's, its epoch is no longer current, or a newer version on the +note job's dependency topics has been published since. +fn au_stale c long w ulong j ulong to bool +if ne au_pw w 6 sc_cell w 16 +ret true +end +if ne au_pw w 7 sc_cell w 24 +ret true +end +if ne au_pw w 6 pool_epoch c +ret true +end +t = get sjdep j +if eq t 0 +ret false +end +h = pool_current c +if le h 0 +ret true +end +ret ne node_next h au_pw w 7 t 0 +end + +note Creator: check the pending proposal of worker w; the verdict. +fn au_valid c long w ulong to ulong +q = au_cell w 72 +if or ne au_pw w 0 1 ne au_pw w 1 1 +ret 1 +end +cl = au_pw w 13 +if or eq cl 0 gt cl 3 +ret 1 +end +if or ne au_pw w 14 0 ne au_pw w 15 0 +ret 1 +end +b = get scbusy w +if eq b 0 +ret 2 +end +j = sub b 1 +if ne get sjst j 3 +ret 2 +end +if or ne au_pw w 3 q ne q sc_cell w 0 +ret 2 +end +if or ne au_pw w 2 get sjid j ne au_pw w 4 w +ret 2 +end +if ne au_pw w 5 get auinc w +ret 3 +end +if au_stale c w j +ret 4 +end +if ne au_pw w 8 get augen 0 +ret 5 +end +if eq au_cell w 8 q +ret 6 +end +ret 0 +end + +note Creator: decide worker w's pending proposal. A grant is journaled +note (START, made durable) before the verdict is published; if the journal +note refuses, the action is closed as not performed and the verdict is 9. +fn au_decide c long w ulong to void +q = au_cell w 72 +v = au_valid c w +if eq v 0 +v = au_commit w +end +if eq v 0 +if ne au_jstart w 0 +e = au_recon sub get aujob w 1 1 +put aujob w 0 +v = 9 +end +end +if eq v 0 +jn_hit 2 +end +au_set w 24 v +au_set w 32 get augen 0 +au_set w 16 q +r = sup_wake w +end + +fn au_jstart w ulong to ulong +op = new ulong 4 0 +k = sub get aujob w 1 +for i ulong 4 +put op i au_pw w add 9 i +end +b = mul k 32 +ret jn_start op au_pw w 13 au_pw w 2 get aurec add b 2 get aurec add b 4 +end + +note Journal DONE for the action in slot b-1 of worker w's job. +fn au_jdone b ulong o ulong w ulong how ulong to ulong +op = new ulong 4 0 +k = sub b 1 +for i ulong 4 +put op i get aurec add mul k 32 add 8 i +end +ret jn_done op o au_pw w 2 how +end + +note Creator: apply receipt outcome o to action slot k (actrecon). An +note outcome of 0 leaves the action UNKNOWN. +fn au_recon k ulong o ulong to ulong +r = new ulong 32 0 +s = new ulong 32 0 +au_load k r +for i ulong 32 +put s i get r i +end +put s 0 1 +put s 1 o +put s 3 1 +put s 6 0 +put s 7 0 +e = actrecon r s +au_save k r +ret e +end + +note Creator: record worker w's report. DONE is made durable before the +note action record changes; if the journal refuses, the report stays +note unrecorded and is tried again at the next step. +fn au_record w ulong to void +b = get aujob w +if eq b 0 +put aured w au_cell w 80 +ret +end +jn_hit 3 +if ne au_jdone b au_cell w 88 w 1 0 +ret +end +jn_hit 4 +put aured w au_cell w 80 +put aujob w 0 +e = au_recon sub b 1 au_cell w 88 +end + +note Creator, after worker w was reaped: kill the process group of a tool +note it left running and make its in-flight action UNKNOWN (actlost). +fn au_lost w ulong to void +r = new ulong 32 0 +p = au_cell w 112 +if gt p 0 +x = killpid neg int p 9 +au_set w 112 0 +end +b = get aujob w +if eq b 0 +ret +end +if ne au_jdone b 0 w 2 0 +ret +end +put aujob w 0 +k = sub b 1 +au_load k r +e = actlost r +au_save k r +end + +note Creator, after sched_step: record tool reports, close the actions of +note reaped workers, and decide pending proposals unless paused. Returns +note the number of proposals decided. +fn auth_step c long to ulong +n = mapget c 48 8 +k = ulong 0 +for w ulong n bound 64 +if gt au_cell w 80 get aured w +au_record w +end +if gt get swpid w 0 +if and eq get scpause 0 0 gt au_cell w 72 au_cell w 16 +au_decide c w +k = add k 1 +end +else +au_lost w +end +end +ret k +end + +note Creator: revoke the current authority. The generation moves on, so a +note pending or later proposal made under the old one is refused (5), and +note every granted tool still running is told to cancel. Its outcome is +note recorded as reported: performed if it had already exited, UNKNOWN if +note it was killed. Returns the new generation. +fn auth_revoke c long to ulong +g = add get augen 0 1 +x = jn_rec 6 g 0 0 +put augen 0 g +r = mapset get aumap 0 8192 8 g +n = mapget c 48 8 +for w ulong n bound 64 +if and gt get swpid w 0 gt get aujob w 0 +au_ctl w au_cell w 16 +end +end +ret g +end + +note Creator: pause (on true) or resume. While paused no job is assigned and +note no proposal is decided; running tools go on. Every live worker gets a +note control notification and acknowledges it. Returns sched_pause's count. +fn auth_pause c long on bool to ulong +v = ulong 0 +if on +v = 1 +end +x = jn_rec 7 v 0 0 +r = mapset get aumap 0 8200 8 v +n = mapget c 48 8 +for w ulong n bound 64 +if gt get swpid w 0 +au_ctl w 0 +end +end +ret sched_pause c on +end + +note Creator: cancel job j. 1 freed before it started (no effect), 2 its +note worker was told to cancel and the outcome comes from its report (a +note pending proposal is refused with 6), 3 already finished, 4 UNKNOWN, +note -3 no such job. CANCEL is journaled first; a job freed before it +note started is also released (outcome 300). +fn auth_cancel c long j ulong to long +id = get sjid j +x = jn_rec 8 id 0 0 +s = sched_cancel j +if eq s 1 +x = jn_rec 5 id 300 0 +end +if eq s 2 +w = get sjw j +au_ctl w sc_cell w 0 +end +ret s +end + +note Creator, after auth_new, jn_open and sched_new, before any spawn: +note settle and release from the journal (jn_recover), then install every +note journaled action into the action records, move the generation past +note every journaled one and restore the paused flag. 0 or an error. +fn auth_recover c long to ulong +e = jn_recover c +if ne e 0 +ret e +end +op = new ulong 4 0 +for k ulong 64 +if ne jn_act k 0 0 +for i ulong 4 +put op i jn_act k add 1 i +end +s = au_alloc op jn_act k 5 +if lt s 0 +ret 32 +end +b = mul ulong s 32 +put aurec add b 1 jn_act k 6 +put aurec add b 2 jn_act k 7 +put aurec add b 4 jn_act k 8 +end +end +put augen 0 jn_gen +r = mapset get aumap 0 8192 8 jn_gen +if ne jn_stat 2 0 +r = mapset get aumap 0 8200 8 1 +x = sched_pause c true +end +ret 0 +end + +note Creator: word i (0..31) of the action record for op, or -1 if none. +fn auth_act op view ulong 4 i ulong to long +k = au_find op +if or lt k 0 gt i 31 +ret neg long 1 +end +ret long get aurec add mul ulong k 32 i +end + +note Worker w's control or proposal cell word, for the creator's view. +fn auth_cell w ulong off ulong to ulong +ret au_cell w off +end + +note Copy up to 3968 bytes of worker w's tool output into dst; the count. +fn auth_wout w ulong dst edit uchar 4096 to ulong +n = au_cell w 120 +if gt n 3968 +n = 3968 +end +if gt n 0 +r = mapread get aumap 1 add mul w 4096 128 dst 0 n +end +ret n +end +note Concatenate after transport.om, pool.om, super.om, sched.om, +note seed/authority/action.om, journal.om and auth.om. CPU-only acceptance of +note DRA-289: a conductor forks a creator, makes it crash at each durable and +note effect boundary (exit 77 at jcrash points 1..6, or SIGKILL while a tool +note runs), restarts it, and then checks the journal, the effects and the +note recovered outcomes with its own reader (jt_load, not jn_scan). +note Journal directory build/node/jt, relative to the repository root. +note Job id kind*1000 + op. Tools (/bin/sh -c), all writing under the +note directory: kind 1 appends "" to sink; kind 2 sleeps 1 s first; +note kind 3 is idempotent: it creates key k. with ln (never replaces) +note and appends "" only when it did; kind 4 appends "s", sleeps +note 1 s, then acts as kind 3. Kinds 1 and 2 are class 1 (external effect), +note kinds 3 and 4 class 3 (idempotent, decided by the key at recovery). +text jtsh "/bin/sh" +text jtdc "-c" +text jtt2 "echo " +text jtt3 " >> build/node/jt/sink" +text jtt4 "sleep 1; " +text jtt5 " > build/node/jt/t." +text jtt6 " && ln build/node/jt/t." +text jtt7 " build/node/jt/k." +text jtt8 " && echo " +text jtt9 "; rm -f build/node/jt/t." +text jtts "echo s" +text jtdir "build/node/jt" +text jtfj "build/node/jt/journal" +text jtfl "build/node/jt/lock" +text jtfs "build/node/jt/sink" +text jtfk "build/node/jt/k." +text jtft "build/node/jt/t." +text jtok "PASS node journal\n" +global jtm long 1 +global jtr ulong 4096 +global jtn ulong 1 +global jtb uchar 65536 +global jtp uchar 64 + +fn jt_ok ok bool code int to void +if not ok +exit code +end +end + +fn jt_tlen k ulong to ulong +if eq k 0 +ret len jtsh +end +if eq k 1 +ret len jtdc +end +if eq k 2 +ret len jtt2 +end +if eq k 3 +ret len jtt3 +end +if eq k 4 +ret len jtt4 +end +if eq k 5 +ret len jtt5 +end +if eq k 6 +ret len jtt6 +end +if eq k 7 +ret len jtt7 +end +if eq k 8 +ret len jtt8 +end +if eq k 9 +ret len jtt9 +end +ret len jtts +end + +fn jt_tb k ulong i ulong to uchar +if eq k 0 +ret get jtsh i +end +if eq k 1 +ret get jtdc i +end +if eq k 2 +ret get jtt2 i +end +if eq k 3 +ret get jtt3 i +end +if eq k 4 +ret get jtt4 i +end +if eq k 5 +ret get jtt5 i +end +if eq k 6 +ret get jtt6 i +end +if eq k 7 +ret get jtt7 i +end +if eq k 8 +ret get jtt8 i +end +if eq k 9 +ret get jtt9 i +end +ret get jtts i +end + +note Append text k to s at n; the new length. +fn jt_add s edit uchar 1024 n ulong k ulong to ulong +m = jt_tlen k +for i ulong m bound 64 +put s add n i jt_tb k i +end +ret add n m +end + +note Append the decimal v to s at n; the new length. +fn jt_num s edit uchar 1024 n ulong v ulong to ulong +d = new uchar 20 0 +x = v +k = ulong 20 +for i ulong 20 +k = sub k 1 +put d k uchar add rem x 10 48 +x = div x 10 +if eq x 0 +break +end +end +for i ulong sub 20 k bound 20 +put s add n i get d add k i +end +ret add n sub 20 k +end + +note The idempotent part: echo N > t.N && ln t.N k.N && echo N >> sink; rm -f t.N +fn jt_keyed s edit uchar 1024 n ulong op ulong to ulong +m = jt_add s n 2 +m = jt_num s m op +m = jt_add s m 5 +m = jt_num s m op +m = jt_add s m 6 +m = jt_num s m op +m = jt_add s m 7 +m = jt_num s m op +m = jt_add s m 8 +m = jt_num s m op +m = jt_add s m 3 +m = jt_add s m 9 +ret jt_num s m op +end + +fn jt_script kind ulong op ulong s edit uchar 1024 to ulong +n = ulong 0 +if eq kind 4 +n = jt_add s n 10 +n = jt_num s n op +n = jt_add s n 3 +put s n 59 +put s add n 1 32 +n = add n 2 +end +if or eq kind 2 eq kind 4 +n = jt_add s n 4 +end +if ge kind 3 +ret jt_keyed s n op +end +n = jt_add s n 2 +n = jt_num s n op +ret jt_add s n 3 +end + +note Packed argv: /bin/sh -c