interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / node / journal.om

1426 lines · 30894 bytes Raw Blame History
1note Omega Node durable journal and restart recovery (DRA-289). Concatenate
2note after transport.om, pool.om, super.om, sched.om and
3note seed/authority/action.om, before auth.om, which journals through it.
4note While no journal is open every append succeeds without writing, so the
5note authority layer also runs memory-only.
6note File DIR/journal: a 64-byte header, then 128-byte records. Header words:
7note 0 magic, 1 format version (2 for a file this build creates; 1 for one
8note a format-1 creator made, read and continued, see FORMAT), 2 record
9note size 128, 3..6 objective identity,
10note 7 FNV-1a 64 of words 0..6. Record words: 0 type, 1 seq (1, 2, 3, ...
11note with no gap), 2 fence, 3..14 payload, 15 FNV-1a 64 of words 0..14.
12note Types and payloads:
13note 1 OPEN 3 pid, 4 authority generation of the new creator, 5 the
14note scheduler policy version SPVER it was written under (0
15note from a format-1 creator)
16note 2 ADMIT 3 job id, 4 dependency topics, 5 flags, 6 resource request,
17note 7 table slot
18note 3 START 3..6 operation identity, 7 class, 8 job id, 9 attempt, 10 gen
19note 4 DONE 3..6 operation identity, 7 outcome (0 UNKNOWN, 1 not
20note performed, 2 performed), 8 job id, 9 how (1 reported,
21note 2 worker lost, 3 in flight at restart, 4 key present,
22note 5 key absent, 6 no external effect)
23note 5 RELEASE 3 job id, 4 outcome (the job's outcome; 300 cancelled)
24note 6 REVOKE 3 new generation
25note 7 PAUSE 3 1 paused, 0 resumed
26note 8 CANCEL 3 job id
27note 9 OPER an operator command the creator committed (DRA-291): 3
28note command id (never 0, never repeated), 4 op, 5 target,
29note 6..9 arguments, 10 view version it was acknowledged at.
30note Kept in order in a table of 128 for the creator to replay;
31note a reader without type 9 refuses it as unknown (24).
32note 10 RESULT the result an objective ended with (DRA-292): 3 objective
33note index (< 64), 4 result (1 PASS, else the failure; not 0),
34note 5 model turns performed. At most one per objective; a
35note restart reports it and never resumes that objective. A
36note reader without type 10 refuses it as unknown (24).
37note 11 POLICY 3 policy, 4 window, 5 dispatch budget, 6 bypass budget,
38note 7 ticket lifetime, 8 capacities (packed like a request):
39note the whole setting; the last one wins
40note 12 GOAL 3 job id, 4 topics, 5 estimate ms, 6 uncertainty ms
41note 13 FAIR 3 dispatch event, 4 assigned slot bitmap, 5 bypassed slot
42note bitmap, 6 cursor, 7 reserved slot + 1, 8 slots a
43note publication made ready (sched.om's ring)
44note 14 FORMAT 3 the format version the records after it follow (2). In
45note a version-1 file the first creator of this format writes
46note it once, before its OPEN, so a format-1 reader refuses
47note the file from then on (an unknown type, 24) instead of
48note continuing it.
49note A header of another version is refused (21). Records keep the format
50note of the incarnation that wrote them: in a version-1 file, an OPEN with
51note word 5 zero starts a format-1 incarnation, whose ADMITs carry no
52note request or slot (request 0, the lowest slot no unreleased job holds)
53note and which may hold no POLICY, GOAL or FAIR (25). An OPEN of another
54note SPVER, a format-1 OPEN after FORMAT or in a version-2 file, an OPEN of
55note this format before FORMAT in a version-1 file, and a FORMAT anywhere
56note else are refused at replay (33).
57note Order: a transition is appended and fdatasync'd before anything acts on
58note it or publishes it. An append whose write or sync fails marks the journal
59note failed and every later append is refused, so nothing is acted on that
60note might not be durable.
61note Fencing: DIR/lock is held with flock(LOCK_EX) for the creator's life. It
62note is opened close-on-exec, so tools never hold it, but forked workers do:
63note a new creator cannot open the journal until every worker of the old one
64note has exited. Each open appends OPEN with the next fence number, and every
65note record carries the fence of the creator that wrote it.
66note Torn tail: a partial last record, or a last full record whose checksum or
67note seq is wrong, was never acknowledged and is cut off. A bad record that is
68note not the last is corruption: the open is refused and nothing is changed.
69note Compatibility: a header whose magic, record size or checksum differs,
70note or whose version is neither 1 nor 2, is refused (21), never
71note reinterpreted; an objective identity that differs is refused (22). A
72note version-1 file keeps its header when this build continues it.
73text jnjour "/journal"
74text jnlock "/lock"
75text jnkey "/k."
76global jon ulong 1
77global jbad ulong 1
78global jfd int 1
79global jlk int 1
80global jseq ulong 1
81global jsz ulong 1
82global jfence ulong 1
83global jgen ulong 1
84global jcrash ulong 1
85global jdir uchar 128
86global jdn ulong 1
87global jpath uchar 160
88global jact ulong 1024
89global jjob ulong 2048
90global jstat ulong 8
91note jfmt: 0 the open file's header version, 1 1 once its records follow
92note this format (a version-2 file, or FORMAT seen), 2 1 while replaying a
93note format-1 incarnation.
94global jfmt ulong 3
95note Policy replay tables (DRA-398), per job entry k: jreq request, jslot
96note slot at admission, jbyp bypass count, jrdy 1 once a publication made
97note it ready; jsl slot -> job entry + 1 while admitted and not released;
98note jpol 0 seen, 1..5 policy words, 6 caps; jgoal per k: 0 seen, 1
99note topics, 2 estimate, 3 uncertainty; jfair 0 last event, 1 cursor, 2
100note reserved job entry + 1, 3 seen.
101global jreq ulong 256
102global jslot ulong 256
103global jbyp ulong 256
104global jrdy ulong 256
105global jsl ulong 64
106global jpol ulong 8
107global jgoal ulong 1024
108global jfair ulong 4
109global jopr ulong 1024
110global jnop ulong 1
111global jres ulong 128
112
113note Fault injection for the recovery tests: exit 77 at point p when armed.
114fn jn_hit p ulong to void
115if and gt p 0 eq get jcrash 0 p
116exit 77
117end
118end
119
120fn jn_sum r view ulong 16 n ulong to ulong
121h = ulong 14695981039346656037
122for i ulong n bound 16
123v = get r i
124for b ulong 8
125h = mul xor h and shr v mul b 8 255 1099511628211
126end
127end
128ret h
129end
130
131fn jn_enc r view ulong 16 b edit uchar 128 to void
132for i ulong 16
133v = get r i
134for k ulong 8
135put b add mul i 8 k uchar and shr v mul k 8 255
136end
137end
138end
139
140fn jn_dec b view uchar 128 r edit ulong 16 to void
141for i ulong 16
142v = ulong 0
143for k ulong 8
144v = or v shl ulong get b add mul i 8 k mul k 8
145end
146put r i v
147end
148end
149
150note DIR plus name k (0 journal, 1 lock, 2 key prefix) in jpath; the length.
151fn jn_name k ulong to ulong
152n = get jdn 0
153for i ulong n bound 128
154put jpath i get jdir i
155end
156m = len jnjour
157if eq k 1
158m = len jnlock
159end
160if eq k 2
161m = len jnkey
162end
163for i ulong m bound 16
164b = get jnkey 0
165if eq k 0
166b = get jnjour i
167end
168if eq k 1
169b = get jnlock i
170end
171if eq k 2
172b = get jnkey i
173end
174put jpath add n i b
175end
176ret add n m
177end
178
179note The key path DIR/k.<v> of an idempotent effect; the length.
180fn jn_keyp v ulong to ulong
181n = jn_name 2
182d = new uchar 20 0
183x = v
184k = ulong 20
185for i ulong 20
186k = sub k 1
187put d k uchar add rem x 10 48
188x = div x 10
189if eq x 0
190break
191end
192end
193for i ulong sub 20 k bound 20
194put jpath add n i get d add k i
195end
196ret add n sub 20 k
197end
198
199fn jn_reset to void
200for i ulong 1024
201put jact i 0
202end
203for i ulong 2048
204put jjob i 0
205end
206for i ulong 1024
207put jopr i 0
208end
209put jnop 0 0
210for i ulong 128
211put jres i 0
212end
213for i ulong 8
214put jstat i 0
215put jpol i 0
216end
217for i ulong 256
218put jreq i 0
219put jslot i 0
220put jbyp i 0
221put jrdy i 0
222end
223for i ulong 1024
224put jgoal i 0
225end
226for i ulong 64
227put jsl i 0
228end
229for i ulong 4
230put jfair i 0
231end
232for i ulong 3
233put jfmt i 0
234end
235put jon 0 0
236put jbad 0 0
237put jseq 0 0
238put jsz 0 0
239put jfence 0 0
240put jgen 0 0
241end
242
243note Action table, 64 entries of 16 words: 0 used, 1..4 operation identity,
244note 5 class, 6 state (as action.om: 1 ready, 2 in flight, 3 UNKNOWN,
245note 4 performed), 7 attempt, 8 gen, 9 job id. -1 when absent.
246fn jn_afind r view ulong 16 to long
247for k ulong 64
248b = mul k 16
249if ne get jact b 0
250m = true
251for i ulong 4
252if ne get jact add b add 1 i get r add 3 i
253m = false
254end
255end
256if m
257ret long k
258end
259end
260end
261ret neg long 1
262end
263
264note Job table, 256 entries of 8 words: 0 used, 1 job id, 2 deps, 3 flags,
265note 4 released, 5 outcome, 6 cancelled, 7 action entry + 1 of its last START.
266fn jn_jfind id ulong to long
267for k ulong 256
268if and ne get jjob mul k 8 0 eq get jjob add mul k 8 1 id
269ret long k
270end
271end
272ret neg long 1
273end
274
275fn jn_jnew id ulong to long
276for k ulong 256
277b = mul k 8
278if eq get jjob b 0
279for i ulong 8
280put jjob add b i 0
281end
282put jjob b 1
283put jjob add b 1 id
284ret long k
285end
286end
287ret neg long 1
288end
289
290fn jn_maxg g ulong to void
291if gt g get jgen 0
292put jgen 0 g
293end
294end
295
296fn jn_astart r view ulong 16 to ulong
297k = jn_afind r
298if lt k 0
299for i ulong 64
300if eq get jact mul i 16 0
301k = long i
302break
303end
304end
305end
306if lt k 0
307ret 25
308end
309b = mul ulong k 16
310if ne get jact b 0
311if ne get jact add b 6 1
312ret 25
313end
314end
315put jact b 1
316for i ulong 4
317put jact add b add 1 i get r add 3 i
318end
319put jact add b 5 get r 7
320put jact add b 6 2
321put jact add b 7 get r 9
322put jact add b 8 get r 10
323put jact add b 9 get r 8
324jn_maxg get r 10
325j = jn_jfind get r 8
326if ge j 0
327put jjob add mul ulong j 8 7 add ulong k 1
328end
329ret 0
330end
331
332fn jn_adone r view ulong 16 to ulong
333k = jn_afind r
334if lt k 0
335ret 25
336end
337b = mul ulong k 16
338s = get jact add b 6
339if not or eq s 2 eq s 3
340ret 25
341end
342o = get r 7
343if gt o 2
344ret 25
345end
346v = ulong 3
347if eq o 1
348v = 1
349end
350if eq o 2
351v = 4
352end
353put jact add b 6 v
354ret 0
355end
356
357note Apply one durable record to the recovery tables; 0, 24 unknown type,
358note 25 a transition the tables refuse (START of a performed operation, a
359note DONE without a START, a second ADMIT or RELEASE, a policy record that
360note does not hold, a POLICY, GOAL or FAIR in a format-1 incarnation), 33 an
361note OPEN or FORMAT the file's format does not allow (see FORMAT above).
362fn jn_apply r view ulong 16 to ulong
363t = get r 0
364if eq t 1
365e = jn_aopen r
366if ne e 0
367ret e
368end
369put jfence 0 get r 2
370jn_maxg get r 4
371ret 0
372end
373if eq t 2
374ret jn_aadmit r
375end
376if eq t 3
377ret jn_astart r
378end
379if eq t 4
380ret jn_adone r
381end
382if or eq t 5 eq t 8
383j = jn_jfind get r 3
384if lt j 0
385ret 25
386end
387b = mul ulong j 8
388if eq t 8
389put jjob add b 6 1
390if eq get jfair 2 add ulong j 1
391put jfair 2 0
392end
393ret 0
394end
395if ne get jjob add b 4 0
396ret 25
397end
398put jjob add b 4 1
399put jjob add b 5 get r 4
400s = get jslot ulong j
401if eq get jsl s add ulong j 1
402put jsl s 0
403end
404ret 0
405end
406if eq t 6
407jn_maxg get r 3
408ret 0
409end
410if eq t 7
411put jstat 2 get r 3
412ret 0
413end
414if eq t 9
415ret jn_oapply r
416end
417if eq t 10
418ret jn_rapply r
419end
420if eq t 14
421ret jn_afmt r
422end
423if gt t 10
424if and le t 13 ne get jfmt 2 0
425ret 25
426end
427ret jn_apol r
428end
429ret 24
430end
431
432note ADMIT into the tables: a new job entry with its request and slot; a
433note slot another unreleased job holds is refused (25). A format-1 ADMIT
434note records neither: request 0, the lowest slot no unreleased job holds
435note (25 when none is free).
436fn jn_aadmit r view ulong 16 to ulong
437if ge jn_jfind get r 3 0
438ret 25
439end
440s = get r 7
441q = get r 6
442if ne get jfmt 2 0
443s = jn_lfree
444q = ulong 0
445end
446if ge s 64
447ret 25
448end
449if ne get jsl s 0
450ret 25
451end
452j = jn_jnew get r 3
453if lt j 0
454ret 25
455end
456k = ulong j
457put jjob add mul k 8 2 get r 4
458put jjob add mul k 8 3 get r 5
459put jreq k q
460put jslot k s
461put jbyp k 0
462put jrdy k 0
463put jsl s add k 1
464ret 0
465end
466
467note The lowest slot no unreleased job holds in the replay table; 64 none.
468fn jn_lfree to ulong
469for s ulong 64
470if eq get jsl s 0
471ret s
472end
473end
474ret 64
475end
476
477note OPEN's format: word 5 SPVER once the file's records follow this format;
478note 0 (a format-1 creator) only in a version-1 file before FORMAT. 33 else.
479fn jn_aopen r view ulong 16 to ulong
480if eq get r 5 SPVER
481if eq get jfmt 1 0
482ret 33
483end
484put jfmt 2 0
485ret 0
486end
487if or ne get r 5 0 ne get jfmt 1 0
488ret 33
489end
490put jfmt 2 1
491ret 0
492end
493
494note FORMAT: only in a version-1 file, once, naming format 2. 33 else.
495fn jn_afmt r view ulong 16 to ulong
496if or ne get jfmt 0 1 ne get jfmt 1 0
497ret 33
498end
499if ne get r 3 2
500ret 33
501end
502put jfmt 1 1
503ret 0
504end
505
506note POLICY, GOAL and FAIR into the tables (types 11..13); 24 any other.
507fn jn_apol r view ulong 16 to ulong
508t = get r 0
509if eq t 11
510if not sched_polok get r 3 get r 4 get r 5 get r 6 get r 7
511ret 25
512end
513put jpol 0 1
514for i ulong 6
515put jpol add i 1 get r add i 3
516end
517ret 0
518end
519if eq t 12
520j = jn_jfind get r 3
521if lt j 0
522ret 25
523end
524b = mul ulong j 4
525put jgoal b 1
526for i ulong 3
527put jgoal add b add i 1 get r add i 4
528end
529ret 0
530end
531if eq t 13
532ret jn_afair r
533end
534ret 24
535end
536
537note FAIR into the tables. A decision record (word 8 zero) must follow the
538note last event; a readiness record (word 8 the slots a publication made
539note ready, nothing assigned or bypassed) may share it, since publication
540note does not count as a dispatch event. The cursor must lie in the
541note table; assigned and bypassed slots must be disjoint and hold live
542note ready jobs (no deps, or made ready by an earlier readiness record);
543note slots made ready must hold live waiting jobs; the reserved slot a live
544note ready job this record did not assign. Live: admitted, not released,
545note not cancelled. Nothing changes otherwise (25). Assigned jobs' bypass
546note counts restart at 0, bypassed ones go up by one, jobs made ready are
547note marked ready.
548fn jn_afair r view ulong 16 to ulong
549am = get r 4
550bm = get r 5
551rd = get r 8
552if not jn_fevt get r 3 or am bm rd
553ret 25
554end
555if or ge get r 6 64 ne and am bm 0
556ret 25
557end
558if or not jn_fok or am bm 1 not jn_fok rd 2
559ret 25
560end
561rs = get r 7
562if gt rs 64
563ret 25
564end
565rk = ulong 0
566if gt rs 0
567if ne and shr am sub rs 1 1 0
568ret 25
569end
570if not jn_fok shl ulong 1 sub rs 1 1
571ret 25
572end
573rk = get jsl sub rs 1
574end
575for s ulong 64
576if ne and shr am s 1 0
577put jbyp sub get jsl s 1 0
578end
579if ne and shr bm s 1 0
580k = sub get jsl s 1
581put jbyp k add get jbyp k 1
582end
583if ne and shr rd s 1 0
584put jrdy sub get jsl s 1 1
585end
586end
587put jfair 0 get r 3
588put jfair 1 get r 6
589put jfair 2 rk
590put jfair 3 1
591ret 0
592end
593
594note Event order of a FAIR at event ev with assigned or bypassed slots ab
595note and slots made ready rd: a readiness record (ab empty) is never also a
596note decision, even as the first FAIR; decisions come strictly after the
597note last event, readiness at or after it.
598fn jn_fevt ev ulong ab ulong rd ulong to bool
599if and ne rd 0 ne ab 0
600ret false
601end
602if eq get jfair 3 0
603ret true
604end
605if ne rd 0
606ret ge ev get jfair 0
607end
608ret gt ev get jfair 0
609end
610
611note Does every slot set in bitmap m hold a live job that is ready (kind 1:
612note no deps, or made ready) or waiting (kind 2: deps, not made ready)?
613fn jn_fok m ulong kind ulong to bool
614for s ulong 64
615if ne and shr m s 1 0
616x = get jsl s
617if eq x 0
618ret false
619end
620k = sub x 1
621b = mul k 8
622if or ne get jjob add b 4 0 ne get jjob add b 6 0
623ret false
624end
625w = and ne get jjob add b 2 0 eq get jrdy k 0
626if and w eq kind 1
627ret false
628end
629if and not w eq kind 2
630ret false
631end
632end
633end
634ret true
635end
636
637note OPER into the operator table: 25 for command id 0, an id already
638note there, or a full table. Entry k: 0 id, 1 op, 2 target, 3..6 arguments,
639note 7 view version.
640fn jn_oapply r view ulong 16 to ulong
641n = get jnop 0
642if or eq get r 3 0 ge n 128
643ret 25
644end
645if ge jn_ofind get r 3 0
646ret 25
647end
648for w ulong 8
649put jopr add mul n 8 w get r add 3 w
650end
651put jnop 0 add n 1
652ret 0
653end
654
655note RESULT into the result table (2 words per objective: result, turns):
656note 25 for an index past 63, result 0, or an objective that has one.
657fn jn_rapply r view ulong 16 to ulong
658i = get r 3
659if or ge i 64 eq get r 4 0
660ret 25
661end
662if ne get jres mul i 2 0
663ret 25
664end
665put jres mul i 2 get r 4
666put jres add mul i 2 1 get r 5
667ret 0
668end
669
670note Operator table index of command id, -1 when absent.
671fn jn_ofind id ulong to long
672for k ulong get jnop 0 bound 128
673if eq get jopr mul k 8 id
674ret long k
675end
676end
677ret neg long 1
678end
679
680fn jn_le64 b view uchar 128 off ulong to ulong
681v = ulong 0
682for k ulong 8
683v = or v shl ulong get b add off k mul k 8
684end
685ret v
686end
687
688note Make DIR's entry for a new journal durable.
689fn jn_dsync to ulong
690fd = fopen jdir 0 get jdn 0 540672 0
691if lt fd 0
692ret 27
693end
694r = fsync int fd
695x = fclose int fd
696if ne r 0
697ret 27
698end
699ret 0
700end
701
702note Create the header of an empty journal, or check an existing one.
703fn jn_head fd int obj view ulong 4 to ulong
704st = new uchar 128 0
705h = new ulong 16 0
706b = new uchar 128 0
707if ne fstat fd st 0 128 0
708ret 27
709end
710size = jn_le64 st 48
711if lt size 64
712if ne ftrunc fd 0 0
713ret 27
714end
715put h 0 5786624599
716put h 1 2
717put jfmt 0 2
718put jfmt 1 1
719put h 2 128
720for i ulong 4
721put h add 3 i get obj i
722end
723put h 7 jn_sum h 7
724jn_enc h b
725if ne pwrite fd b 0 64 0 64
726ret 27
727end
728if ne fsync fd 0
729ret 27
730end
731put jsz 0 64
732ret jn_dsync
733end
734if ne pread fd b 0 64 0 64
735ret 27
736end
737jn_dec b h
738s = jn_sum h 7
739if or ne get h 0 5786624599 ne get h 7 s
740ret 21
741end
742if ne get h 2 128
743ret 21
744end
745if and ne get h 1 1 ne get h 1 2
746ret 21
747end
748put jfmt 0 get h 1
749if eq get h 1 2
750put jfmt 1 1
751end
752for i ulong 4
753if ne get h add 3 i get obj i
754ret 22
755end
756end
757put jsz 0 size
758ret 0
759end
760
761fn jn_cut fd int off ulong to ulong
762if ne ftrunc fd long off 0
763ret 27
764end
765if ne fdsync fd 0
766ret 27
767end
768put jstat 0 add get jstat 0 sub get jsz 0 off
769put jsz 0 off
770ret 0
771end
772
773note Record i into r: 0 well formed (checksum and seq), 1 not, 27 I/O.
774fn jn_rd fd int i ulong r edit ulong 16 to ulong
775b = new uchar 128 0
776off = long add 64 mul i 128
777if ne pread fd b 0 128 off 128
778ret 27
779end
780jn_dec b r
781s = jn_sum r 15
782if ne get r 15 s
783ret 1
784end
785if ne get r 1 add i 1
786ret 1
787end
788ret 0
789end
790
791note Read every record into the tables; cut a torn tail; refuse corruption.
792fn jn_scan fd int to ulong
793r = new ulong 16 0
794n = div sub get jsz 0 64 128
795rm = rem sub get jsz 0 64 128
796for i ulong n bound 10000000
797g = jn_rd fd i r
798if eq g 27
799ret 27
800end
801if eq g 1
802if and eq add i 1 n eq rm 0
803ret jn_cut fd add 64 mul i 128
804end
805ret 23
806end
807e = jn_apply r
808if ne e 0
809ret e
810end
811put jseq 0 add i 1
812put jstat 1 add i 1
813end
814if gt rm 0
815ret jn_cut fd add 64 mul n 128
816end
817ret 0
818end
819
820note Append record r (caller sets word 0 and the payload) and make it
821note durable, after the dispatch decisions waiting in sched.om's replay
822note ring (jn_drain), so no transition is durable before the scheduling
823note that came before it. The tables check and take the transition first
824note (jn_apply changes nothing when it refuses), so a refused transition is
825note never written. 0, a table refusal, 28 write, 29 sync, 30 failed
826note earlier. After 28 or 29 the journal is failed: the transition may or
827note may not be durable, so the caller must not act on it, and nothing more
828note is written. Without an open journal: 0.
829fn jn_append r edit ulong 16 to ulong
830e = jn_drain
831if ne e 0
832ret e
833end
834ret jn_put r
835end
836
837note Write the replay ring as FAIR records and empty it; then, when the
838note cursor or the reservation moved without a record (a window scan that
839note assigned nothing), one FAIR with empty bitmaps. 0 or as jn_put; on
840note failure the ring keeps its records and dispatch stops when it fills.
841note The ring is attached by jn_recover, once the scheduler holds the
842note journaled state; before that, or without an open journal, it is
843note emptied and nothing is written.
844fn jn_drain to ulong
845if or eq get jon 0 0 not sched_jattached
846sched_frclear
847ret 0
848end
849r = new ulong 16 0
850n = sched_frn
851for k ulong n bound 64
852for i ulong 16
853put r i 0
854end
855put r 0 13
856for i ulong 6
857put r add 3 i sched_frw k i
858end
859e = jn_put r
860if ne e 0
861ret e
862end
863end
864sched_frclear
865rk = ulong 0
866rs = sched_stat 29
867if gt rs 0
868rk = get jsl sub rs 1
869end
870if and eq sched_stat 28 get jfair 1 eq rk get jfair 2
871ret 0
872end
873if eq sched_stat 2 get jfair 0
874ret 0
875end
876for i ulong 16
877put r i 0
878end
879put r 0 13
880put r 3 sched_stat 2
881put r 6 sched_stat 28
882put r 7 rs
883ret jn_put r
884end
885
886note jn_append without the drain.
887fn jn_put r edit ulong 16 to ulong
888if eq get jon 0 0
889ret 0
890end
891if ne get jbad 0 0
892ret 30
893end
894b = new uchar 128 0
895fd = get jfd 0
896put r 1 add get jseq 0 1
897put r 2 get jfence 0
898put r 15 jn_sum r 15
899e = jn_apply r
900if ne e 0
901ret e
902end
903jn_enc r b
904off = long add 64 mul get jseq 0 128
905if and eq get jcrash 0 6 eq get r 0 4
906x = pwrite fd b 0 60 off
907exit 77
908end
909if ne pwrite fd b 0 128 off 128
910put jbad 0 1
911ret 28
912end
913if ne fdsync fd 0
914put jbad 0 1
915ret 29
916end
917put jseq 0 add get jseq 0 1
918put jsz 0 add get jsz 0 128
919ret 0
920end
921
922fn jn_rec t ulong a ulong b ulong c ulong to ulong
923r = new ulong 16 0
924put r 0 t
925put r 3 a
926put r 4 b
927put r 5 c
928ret jn_append r
929end
930
931note Open (or create) the journal in directory dir (dn bytes) for objective
932note obj, waiting up to ms for the lock. 0, 20 fenced (another creator or
933note its workers hold the lock), 21 incompatible, 22 other objective, 23
934note corrupt, 24/25 refused record, 26 path too long, 27 I/O error.
935fn jn_open dir view uchar 128 dn ulong obj view ulong 4 ms ulong to ulong
936if or eq dn 0 gt dn 100
937ret 26
938end
939jn_reset
940for i ulong dn bound 128
941put jdir i get dir i
942end
943put jdn 0 dn
944r = makedir jdir 0 dn 448
945if and lt r 0 ne r neg long 17
946ret 27
947end
948n = jn_name 1
949lk = fopen jpath 0 n 524354 384
950if lt lk 0
951ret 27
952end
953got = false
954for t ulong add ms 1 bound 100000
955f = flock int lk 6
956if eq f 0
957got = true
958break
959end
960if ne f neg long 11
961break
962end
963x = sleepns 1000000
964end
965if not got
966x = fclose int lk
967ret 20
968end
969put jlk 0 int lk
970e = jn_load obj
971if ne e 0
972x = fclose int lk
973ret e
974end
975ret 0
976end
977
978fn jn_load obj view ulong 4 to ulong
979n = jn_name 0
980fd = fopen jpath 0 n 524354 384
981if lt fd 0
982ret 27
983end
984e = jn_head int fd obj
985if eq e 0
986e = jn_scan int fd
987end
988if ne e 0
989x = fclose int fd
990ret e
991end
992put jfd 0 int fd
993put jon 0 1
994if eq get jfmt 1 0
995e = jn_rec 14 2 0 0
996if ne e 0
997ret e
998end
999end
1000put jfence 0 add get jfence 0 1
1001put jgen 0 add get jgen 0 1
1002ret jn_rec 1 ulong getpid get jgen 0 SPVER
1003end
1004
1005note Close the journal (after draining the replay ring) and release the
1006note lock (other holders: forked workers).
1007fn jn_close to void
1008if ne get jon 0 0
1009dr = jn_drain
1010x = fclose get jfd 0
1011x = fclose get jlk 0
1012end
1013put jon 0 0
1014sched_jattach false
1015end
1016
1017note Probe the key of an idempotent operation: 1 present, 0 absent, 2 error.
1018fn jn_probe v ulong to ulong
1019st = new uchar 128 0
1020n = jn_keyp v
1021r = stat jpath 0 n st 0 128
1022if eq r 0
1023ret 1
1024end
1025if eq r neg long 2
1026ret 0
1027end
1028ret 2
1029end
1030
1031note Close every START left in flight by a crash with a DONE. Class 3 (an
1032note idempotent effect whose key DIR/k.<op word 0> is created atomically by
1033note the effect) is decided by its key; class 2 (no external effect) was not
1034note performed; anything else is UNKNOWN and is never run again.
1035fn jn_settle to ulong
1036r = new ulong 16 0
1037for k ulong 64
1038b = mul k 16
1039if and ne get jact b 0 eq get jact add b 6 2
1040for i ulong 4
1041put r add 3 i get jact add b add 1 i
1042end
1043put r 0 4
1044put r 8 get jact add b 9
1045cl = get jact add b 5
1046put r 7 0
1047put r 9 3
1048if eq cl 2
1049put r 7 1
1050put r 9 6
1051end
1052if eq cl 3
1053p = jn_probe get r 3
1054if eq p 2
1055ret 27
1056end
1057put r 7 add 1 p
1058put r 9 sub 5 p
1059end
1060e = jn_append r
1061if ne e 0
1062ret e
1063end
1064put jstat 3 add get jstat 3 1
1065end
1066end
1067ret 0
1068end
1069
1070note Recovery: the incarnation (the fence), then the last journaled policy
1071note and capacities. False when the scheduler refused one; a fence past the
1072note incarnation range (2^24 - 1) is refused, never folded onto an earlier
1073note incarnation.
1074fn jn_rpol to bool
1075if ne sched_incarnate get jfence 0 0
1076ret false
1077end
1078if eq get jpol 0 0
1079ret true
1080end
1081if ne sched_cap get jpol 6 0
1082ret false
1083end
1084p = get jpol 1
1085w = get jpol 2
1086d = get jpol 3
1087y = get jpol 4
1088ret eq sched_policy p w d y get jpol 5 0
1089end
1090
1091note Creator, after jn_open and sched_new: settle in-flight operations,
1092note set the scheduler's incarnation to the fence (proposals made for an
1093note earlier creator are stale), restore the last journaled policy and
1094note capacities, then for each admitted job not yet released: a performed
1095note or UNKNOWN last operation releases it with that outcome (it never runs
1096note again); a cancelled job is released as cancelled; any other is
1097note admitted again, in admission order, into its recorded slot, with its
1098note bypass count and declared goal. Last, the cursor, the dispatch event
1099note count and the reservation (on the job's slot) from the last FAIR.
1100note 0 or an error (31 a restore the scheduler refused).
1101fn jn_recover c long to ulong
1102e = jn_settle
1103if ne e 0
1104ret e
1105end
1106if not jn_rpol
1107ret 31
1108end
1109rs = ulong 0
1110for k ulong 256
1111b = mul k 8
1112if and ne get jjob b 0 eq get jjob add b 4 0
1113o = jn_rout k
1114if eq o 999
1115x = jn_readmit k
1116if lt x 0
1117ret 31
1118end
1119if eq get jfair 2 add k 1
1120rs = add ulong x 1
1121end
1122put jstat 5 add get jstat 5 1
1123else
1124e = jn_rec 5 get jjob add b 1 o 0
1125if ne e 0
1126ret e
1127end
1128put jstat 4 add get jstat 4 1
1129end
1130end
1131end
1132if ne get jfair 3 0
1133if ne sched_frestore get jfair 1 get jfair 0 rs 0
1134ret 31
1135end
1136end
1137sched_jattach true
1138ret 0
1139end
1140
1141note Recovery: the outcome job entry k is released with (2 performed, 0
1142note UNKNOWN, 300 cancelled), or 999 when it is to be admitted again.
1143fn jn_rout k ulong to ulong
1144b = mul k 8
1145o = ulong 999
1146a = get jjob add b 7
1147if gt a 0
1148s = get jact add mul sub a 1 16 6
1149if eq s 4
1150o = 2
1151end
1152if eq s 3
1153o = 0
1154end
1155end
1156if and eq o 999 ne get jjob add b 6 0
1157o = 300
1158end
1159ret o
1160end
1161
1162note Recovery: admit job entry k again into its recorded slot, ready again
1163note when a publication had made it ready, then its bypass count and goal.
1164note The slot, or < 0 when the scheduler refuses any of them (a recorded
1165note slot that is taken, a bypass count on a job that is not ready).
1166fn jn_readmit k ulong to long
1167b = mul k 8
1168x = sched_admits get jjob add b 1 get jjob add b 2 get jjob add b 3 get jreq k get jslot k
1169if lt x 0
1170ret x
1171end
1172j = ulong x
1173if ne get jrdy k 0
1174if lt sched_setready j 0
1175ret neg long 3
1176end
1177end
1178if ne get jbyp k 0
1179if lt sched_setbyp j get jbyp k 0
1180ret neg long 3
1181end
1182end
1183g = mul k 4
1184if ne get jgoal g 0
1185if lt sched_goal j get jgoal add g 1 get jgoal add g 2 get jgoal add g 3 0
1186ret neg long 3
1187end
1188end
1189ret x
1190end
1191
1192note Admit job id durably: ADMIT, with the request and the slot it will
1193note take, is appended before sched_admits. -4 an id already in the journal
1194note (checked first, so a duplicate never takes or refreshes an admission
1195note ticket), -8 a request no capacity could ever hold, -2 when sched_room
1196note refuses (admission limit, no free slot, or ticket holders ahead), -5
1197note the journal refused, else the slot.
1198fn jn_admitr id ulong deps ulong flags ulong req ulong to long
1199if ge jn_jfind id 0
1200ret neg long 4
1201end
1202if not sched_feas req
1203ret neg long 8
1204end
1205if not sched_room id
1206ret neg long 2
1207end
1208s = sched_slot
1209if ge s 64
1210ret neg long 2
1211end
1212r = new ulong 16 0
1213put r 0 2
1214put r 3 id
1215put r 4 deps
1216put r 5 flags
1217put r 6 req
1218put r 7 s
1219if ne jn_append r 0
1220ret neg long 5
1221end
1222jn_hit 1
1223ret sched_admits id deps flags req s
1224end
1225
1226note jn_admitr with no resource request.
1227fn jn_admit id ulong deps ulong flags ulong to long
1228ret jn_admitr id deps flags 0
1229end
1230
1231note Creator: sched_policy, journaled first (POLICY, with the current
1232note capacities). -7 refused, -5 the journal refused, else 0.
1233fn jn_policy p ulong win ulong db ulong by ulong ttl ulong to long
1234if not sched_polok p win db by ttl
1235ret neg long 7
1236end
1237if ne jn_pol p win db by ttl sched_caps 0
1238ret neg long 5
1239end
1240ret sched_policy p win db by ttl
1241end
1242
1243note Creator: sched_cap, journaled first (POLICY, with the current policy).
1244note -8 refused, -5 the journal refused, else 0.
1245fn jn_cap caps ulong to long
1246if not sched_capok caps
1247ret neg long 8
1248end
1249if ne jn_pol sched_stat 24 sched_stat 25 sched_stat 26 sched_stat 27 sched_stat 31 caps 0
1250ret neg long 5
1251end
1252ret sched_cap caps
1253end
1254
1255fn jn_pol p ulong win ulong db ulong by ulong ttl ulong caps ulong to ulong
1256r = new ulong 16 0
1257put r 0 11
1258put r 3 p
1259put r 4 win
1260put r 5 db
1261put r 6 by
1262put r 7 ttl
1263put r 8 caps
1264ret jn_append r
1265end
1266
1267note Creator: sched_goal for job slot j (waiting or ready), journaled first
1268note (GOAL). -3 no such job, -5 the journal refused, else 0.
1269fn jn_goal j ulong pro ulong est ulong unc ulong to long
1270if ge j 64
1271ret neg long 3
1272end
1273s = get sjst j
1274if or eq s 0 gt s 2
1275ret neg long 3
1276end
1277r = new ulong 16 0
1278put r 0 12
1279put r 3 get sjid j
1280put r 4 pro
1281put r 5 est
1282put r 6 unc
1283if ne jn_append r 0
1284ret neg long 5
1285end
1286ret sched_goal j pro est unc
1287end
1288
1289note Release reviewed job slot j durably: RELEASE before sched_review. -5
1290note while its operation's DONE is not yet durable or on refusal.
1291fn jn_release j ulong to long
1292id = get sjid j
1293k = jn_jfind id
1294if ge k 0
1295a = get jjob add mul ulong k 8 7
1296if gt a 0
1297if eq get jact add mul sub a 1 16 6 2
1298ret neg long 5
1299end
1300end
1301end
1302if ne jn_rec 5 id get sjout j 0 0
1303ret neg long 5
1304end
1305jn_hit 5
1306ret sched_review j
1307end
1308
1309fn jn_start op view ulong 4 cl ulong id ulong att ulong g ulong to ulong
1310r = new ulong 16 0
1311put r 0 3
1312for i ulong 4
1313put r add 3 i get op i
1314end
1315put r 7 cl
1316put r 8 id
1317put r 9 att
1318put r 10 g
1319ret jn_append r
1320end
1321
1322fn jn_done op view ulong 4 o ulong id ulong how ulong to ulong
1323r = new ulong 16 0
1324put r 0 4
1325for i ulong 4
1326put r add 3 i get op i
1327end
1328put r 7 o
1329put r 8 id
1330put r 9 how
1331ret jn_append r
1332end
1333
1334note Getters: whether job id is in the journal, released, its outcome; a
1335note recovery counter (0 torn bytes cut, 1 records read, 2 paused, 3 settled,
1336note 4 released at recovery, 5 admitted again); the fence; the generation.
1337fn jn_known id ulong to bool
1338ret ge jn_jfind id 0
1339end
1340
1341fn jn_done_j id ulong to bool
1342k = jn_jfind id
1343if lt k 0
1344ret false
1345end
1346ret ne get jjob add mul ulong k 8 4 0
1347end
1348
1349fn jn_jout id ulong to ulong
1350k = jn_jfind id
1351if lt k 0
1352ret 999
1353end
1354ret get jjob add mul ulong k 8 5
1355end
1356
1357fn jn_stat i ulong to ulong
1358ret get jstat and i 7
1359end
1360
1361fn jn_fence to ulong
1362ret get jfence 0
1363end
1364
1365fn jn_gen to ulong
1366ret get jgen 0
1367end
1368
1369note Action table entry k word i (k < 64, i < 16).
1370fn jn_act k ulong i ulong to ulong
1371ret get jact add mul and k 63 16 and i 15
1372end
1373
1374note Commit operator command id: OPER (op, target t, arguments a[0..3],
1375note view version v) appended and synced. 0, 25 refused, or a write error.
1376fn jn_oper id ulong op ulong t ulong a view ulong 4 v ulong to ulong
1377r = new ulong 16 0
1378put r 0 9
1379put r 3 id
1380put r 4 op
1381put r 5 t
1382for i ulong 4
1383put r add 6 i get a i
1384end
1385put r 10 v
1386ret jn_append r
1387end
1388
1389note Operator commands in the journal, and word w of the k-th (as jn_oapply).
1390fn jn_nops to ulong
1391ret get jnop 0
1392end
1393
1394fn jn_opw k ulong w ulong to ulong
1395ret get jopr add mul and k 127 8 and w 7
1396end
1397
1398note Commit that objective i ended with result r after t model turns: RESULT
1399note appended and synced. 0, 25 refused, or a write error.
1400fn jn_result i ulong r ulong t ulong to ulong
1401x = new ulong 16 0
1402put x 0 10
1403put x 3 i
1404put x 4 r
1405put x 5 t
1406ret jn_append x
1407end
1408
1409note The result objective i ended with in the journal (0 none), and its
1410note model turns.
1411fn jn_res i ulong to ulong
1412ret get jres mul and i 63 2
1413end
1414
1415fn jn_rturn i ulong to ulong
1416ret get jres add mul and i 63 2 1
1417end
1418
1419note True when job id has a durable START (an operation was granted to it).
1420fn jn_started id ulong to bool
1421k = jn_jfind id
1422if lt k 0
1423ret false
1424end
1425ret ne get jjob add mul ulong k 8 7 0
1426end