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