shikibu 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +121 -0
  3. data/lib/shikibu/activity.rb +84 -48
  4. data/lib/shikibu/actors.rb +129 -0
  5. data/lib/shikibu/app.rb +45 -19
  6. data/lib/shikibu/blobs.rb +42 -0
  7. data/lib/shikibu/channels.rb +240 -64
  8. data/lib/shikibu/constants.rb +8 -0
  9. data/lib/shikibu/context.rb +184 -14
  10. data/lib/shikibu/errors.rb +56 -0
  11. data/lib/shikibu/fs.rb +93 -0
  12. data/lib/shikibu/integrations/sidekiq.rb +9 -13
  13. data/lib/shikibu/middleware/rack_app.rb +17 -1
  14. data/lib/shikibu/replay.rb +248 -100
  15. data/lib/shikibu/storage/actor_ops.rb +331 -0
  16. data/lib/shikibu/storage/channel_ops.rb +421 -0
  17. data/lib/shikibu/storage/cursor_ops.rb +37 -0
  18. data/lib/shikibu/storage/sequel_storage.rb +400 -324
  19. data/lib/shikibu/storage/sqlite_ops.rb +58 -0
  20. data/lib/shikibu/supervisor.rb +279 -0
  21. data/lib/shikibu/tasks.rb +193 -0
  22. data/lib/shikibu/typed_payload.rb +189 -0
  23. data/lib/shikibu/version.rb +1 -1
  24. data/lib/shikibu/worker.rb +155 -73
  25. data/lib/shikibu/workflow.rb +203 -50
  26. data/lib/shikibu.rb +5 -0
  27. data/schema/SEMANTICS.md +351 -0
  28. data/schema/db/migrations/mysql/20261123000000_outbox_claimed_at.sql +5 -0
  29. data/schema/db/migrations/mysql/20261201000000_actor_registry.sql +24 -0
  30. data/schema/db/migrations/mysql/20261202000000_actor_capabilities.sql +15 -0
  31. data/schema/db/migrations/mysql/20261203000000_payload_blobs.sql +11 -0
  32. data/schema/db/migrations/mysql/20261204000000_actor_fs.sql +13 -0
  33. data/schema/db/migrations/mysql/20261205000000_actor_tasks.sql +15 -0
  34. data/schema/db/migrations/mysql/20261206000000_actor_tasks_claimed_at.sql +7 -0
  35. data/schema/db/migrations/mysql/20261207000000_add_sandboxed.sql +5 -0
  36. data/schema/db/migrations/mysql/20261208000000_widen_actor_owner_columns.sql +15 -0
  37. data/schema/db/migrations/postgresql/20261123000000_outbox_claimed_at.sql +5 -0
  38. data/schema/db/migrations/postgresql/20261201000000_actor_registry.sql +23 -0
  39. data/schema/db/migrations/postgresql/20261202000000_actor_capabilities.sql +15 -0
  40. data/schema/db/migrations/postgresql/20261203000000_payload_blobs.sql +11 -0
  41. data/schema/db/migrations/postgresql/20261204000000_actor_fs.sql +13 -0
  42. data/schema/db/migrations/postgresql/20261205000000_actor_tasks.sql +15 -0
  43. data/schema/db/migrations/postgresql/20261206000000_actor_tasks_claimed_at.sql +7 -0
  44. data/schema/db/migrations/postgresql/20261207000000_add_sandboxed.sql +5 -0
  45. data/schema/db/migrations/postgresql/20261208000000_widen_actor_owner_columns.sql +7 -0
  46. data/schema/db/migrations/sqlite/20261123000000_outbox_claimed_at.sql +5 -0
  47. data/schema/db/migrations/sqlite/20261201000000_actor_registry.sql +23 -0
  48. data/schema/db/migrations/sqlite/20261202000000_actor_capabilities.sql +15 -0
  49. data/schema/db/migrations/sqlite/20261203000000_payload_blobs.sql +11 -0
  50. data/schema/db/migrations/sqlite/20261204000000_actor_fs.sql +13 -0
  51. data/schema/db/migrations/sqlite/20261205000000_actor_tasks.sql +15 -0
  52. data/schema/db/migrations/sqlite/20261206000000_actor_tasks_claimed_at.sql +7 -0
  53. data/schema/db/migrations/sqlite/20261207000000_add_sandboxed.sql +5 -0
  54. data/schema/db/migrations/sqlite/20261208000000_widen_actor_owner_columns.sql +7 -0
  55. data/schema/docs/column-values.md +10 -0
  56. data/schema/spec/ActorFS.tla +117 -0
  57. data/schema/spec/ActorFS_lww_monotonic.cfg +7 -0
  58. data/schema/spec/ActorFS_new.cfg +8 -0
  59. data/schema/spec/ActorFS_old.cfg +7 -0
  60. data/schema/spec/CompetingMessageClaim.tla +127 -0
  61. data/schema/spec/CompetingMessageClaimSplit.tla +132 -0
  62. data/schema/spec/CompetingMessageClaimSplit_atomic_lost.cfg +7 -0
  63. data/schema/spec/CompetingMessageClaimSplit_atomic_once.cfg +7 -0
  64. data/schema/spec/CompetingMessageClaimSplit_fixed.cfg +7 -0
  65. data/schema/spec/CompetingMessageClaimSplit_split_lost.cfg +7 -0
  66. data/schema/spec/CompetingMessageClaimSplit_split_once.cfg +7 -0
  67. data/schema/spec/CompetingMessageClaimSplit_waitonly_lost.cfg +7 -0
  68. data/schema/spec/CompetingMessageClaimSplit_waitonly_once.cfg +7 -0
  69. data/schema/spec/CompetingMessageClaim_new.cfg +10 -0
  70. data/schema/spec/CompetingMessageClaim_old.cfg +11 -0
  71. data/schema/spec/EpochFencing.tla +87 -0
  72. data/schema/spec/EpochFencingRuns.tla +107 -0
  73. data/schema/spec/EpochFencingRuns_epoch.cfg +6 -0
  74. data/schema/spec/EpochFencingRuns_none.cfg +6 -0
  75. data/schema/spec/EpochFencingRuns_witness.cfg +6 -0
  76. data/schema/spec/EpochFencingRuns_worker.cfg +6 -0
  77. data/schema/spec/EpochFencingRuns_worker1.cfg +6 -0
  78. data/schema/spec/EpochFencing_new.cfg +8 -0
  79. data/schema/spec/EpochFencing_old.cfg +9 -0
  80. data/schema/spec/InstanceClaim.tla +196 -0
  81. data/schema/spec/InstanceClaimVerdict.tla +116 -0
  82. data/schema/spec/InstanceClaimVerdict_changedrows.cfg +6 -0
  83. data/schema/spec/InstanceClaimVerdict_changedrows_live.cfg +6 -0
  84. data/schema/spec/InstanceClaimVerdict_foundrows.cfg +6 -0
  85. data/schema/spec/InstanceClaimVerdict_foundrows_live.cfg +6 -0
  86. data/schema/spec/InstanceClaim_atomic_unfenced.cfg +9 -0
  87. data/schema/spec/InstanceClaim_live.cfg +9 -0
  88. data/schema/spec/InstanceClaim_new.cfg +9 -0
  89. data/schema/spec/InstanceClaim_old.cfg +11 -0
  90. data/schema/spec/InstanceClaim_racy_fenced.cfg +9 -0
  91. data/schema/spec/README.md +97 -0
  92. data/schema/spec/SupervisorRestart.tla +108 -0
  93. data/schema/spec/SupervisorRestartCAS.tla +84 -0
  94. data/schema/spec/SupervisorRestartCAS_nonsticky.cfg +6 -0
  95. data/schema/spec/SupervisorRestartCAS_snapshot.cfg +6 -0
  96. data/schema/spec/SupervisorRestartCAS_sticky.cfg +6 -0
  97. data/schema/spec/SupervisorRestart_racy.cfg +7 -0
  98. data/schema/spec/SupervisorRestart_strict.cfg +7 -0
  99. data/schema/spec/TaskClaim.tla +92 -0
  100. data/schema/spec/TaskClaimReadBack.tla +84 -0
  101. data/schema/spec/TaskClaimReadBack_byid.cfg +6 -0
  102. data/schema/spec/TaskClaimReadBack_order.cfg +6 -0
  103. data/schema/spec/TaskClaim_new.cfg +8 -0
  104. data/schema/spec/TaskClaim_old.cfg +9 -0
  105. data/schema/spec/TaskReclaim.tla +110 -0
  106. data/schema/spec/TaskReclaimAttempts.tla +73 -0
  107. data/schema/spec/TaskReclaimAttempts_attempt.cfg +6 -0
  108. data/schema/spec/TaskReclaimAttempts_claimant.cfg +6 -0
  109. data/schema/spec/TaskReclaim_new.cfg +8 -0
  110. data/schema/spec/TaskReclaim_old.cfg +8 -0
  111. data/schema/spec/WaitRegistration.tla +170 -0
  112. data/schema/spec/WaitRegistrationSub.tla +133 -0
  113. data/schema/spec/WaitRegistrationSub_fixed.cfg +6 -0
  114. data/schema/spec/WaitRegistrationSub_fixed_guarded.cfg +6 -0
  115. data/schema/spec/WaitRegistrationSub_guardonly_reg.cfg +6 -0
  116. data/schema/spec/WaitRegistrationSub_guardonly_res.cfg +6 -0
  117. data/schema/spec/WaitRegistrationSub_head_reg.cfg +6 -0
  118. data/schema/spec/WaitRegistrationSub_head_res.cfg +6 -0
  119. data/schema/spec/WaitRegistrationSub_noclear_reg.cfg +6 -0
  120. data/schema/spec/WaitRegistrationSub_noclear_res.cfg +6 -0
  121. data/schema/spec/WaitRegistrationSub_unfencedreg_reg.cfg +6 -0
  122. data/schema/spec/WaitRegistrationSub_unfencedreg_res.cfg +6 -0
  123. data/schema/spec/WaitRegistration_new.cfg +10 -0
  124. data/schema/spec/WaitRegistration_old.cfg +11 -0
  125. data/schema/spec/WaitRegistration_residual.cfg +11 -0
  126. data/schema/spec/WaitRegistration_sweep.cfg +11 -0
  127. data/schema/spec/actorfs_check_test.go +165 -0
  128. data/schema/spec/claim_check_test.go +387 -0
  129. data/schema/spec/competing_claim_test.go +194 -0
  130. data/schema/spec/epoch_check_test.go +128 -0
  131. data/schema/spec/go.mod +3 -0
  132. data/schema/spec/supervisorrestart_check_test.go +131 -0
  133. data/schema/spec/taskclaim_check_test.go +135 -0
  134. data/schema/spec/taskreclaim_check_test.go +125 -0
  135. data/schema/spec/tlc-check.sh +45 -0
  136. data/schema/spec/tlc-matrix.txt +68 -0
  137. data/schema/spec/wait_check_test.go +256 -0
  138. metadata +136 -2
@@ -0,0 +1,108 @@
1
+ ---- MODULE SupervisorRestart ----
2
+ EXTENDS Naturals
3
+
4
+ (***************************************************************************)
5
+ (* Supervisor one_for_all restart with cooperative cancellation. *)
6
+ (* *)
7
+ (* Child A is the permanent child whose failure triggers a group restart. *)
8
+ (* Child B is a running sibling. The restart expansion set is decided ONCE *)
9
+ (* from the observed snapshot (B non-terminal => B is in the set), then *)
10
+ (* the supervisor cancels B and respawns it as a new generation. *)
11
+ (* Cancellation is COOPERATIVE: the cancel is a state write the child must *)
12
+ (* observe, so B may complete normally before the cancel lands — but the *)
13
+ (* respawn decision is already fixed. *)
14
+ (* *)
15
+ (* Configs: *)
16
+ (* strict: cancellation is reliable — once the supervisor enters the *)
17
+ (* restart sequence B can no longer complete. *)
18
+ (* -> CompletedNeverRespawned holds *)
19
+ (* racy: B may complete in the cancel/spawn window. *)
20
+ (* -> CompletedNeverRespawned violated: B-gen1 completed AND *)
21
+ (* B-gen2 was spawned anyway. *)
22
+ (* *)
23
+ (* The racy violation is inherent to cooperative cancellation: the model *)
24
+ (* pins down exactly which guarantee (reliable cancellation) the *)
25
+ (* "completed is terminal" property depends on. *)
26
+ (***************************************************************************)
27
+
28
+ CONSTANTS MaxGen, \* generation bound for A (finite model)
29
+ STRICTCANCEL \* TRUE: cancel reliable; FALSE: cooperative/racy
30
+
31
+ VARIABLES
32
+ genA, \* current generation of child A
33
+ gsA, \* state of A's current generation: running/failed/completed
34
+ gsB, \* state of B's current generation: running/completed/cancelled
35
+ inRestart, \* TRUE if B was non-terminal at decision time (snapshot)
36
+ compB, \* TRUE once any generation of B completed
37
+ respawnedB, \* violation witness: B respawned despite completion
38
+ phase \* "stable" | "cancelling" | "spawning"
39
+
40
+ vars == <<genA, gsA, gsB, inRestart, compB, respawnedB, phase>>
41
+
42
+ Init ==
43
+ /\ genA = 1
44
+ /\ gsA = "running"
45
+ /\ gsB = "running"
46
+ /\ inRestart = FALSE
47
+ /\ compB = FALSE
48
+ /\ respawnedB = FALSE
49
+ /\ phase = "stable"
50
+
51
+ \* The trigger child fails.
52
+ FailA ==
53
+ /\ gsA = "running"
54
+ /\ phase = "stable"
55
+ /\ gsA' = "failed"
56
+ /\ UNCHANGED <<genA, gsB, inRestart, compB, respawnedB, phase>>
57
+
58
+ \* Supervisor polls, observes the failure, and snapshots the expansion set.
59
+ DecideRestart ==
60
+ /\ phase = "stable"
61
+ /\ gsA = "failed"
62
+ /\ genA < MaxGen
63
+ /\ inRestart' = (gsB /= "completed")
64
+ /\ phase' = "cancelling"
65
+ /\ UNCHANGED <<genA, gsA, gsB, compB, respawnedB>>
66
+
67
+ \* Cancel attempt on B. Reliable or not, cancel of a completed generation
68
+ \* is a no-op. Under STRICTCANCEL, B cannot still be running here unless
69
+ \* it was running at decision time — it is then cancelled for sure.
70
+ CancelB ==
71
+ /\ phase = "cancelling"
72
+ /\ gsB' = IF gsB = "running" THEN "cancelled" ELSE gsB
73
+ /\ phase' = "spawning"
74
+ /\ UNCHANGED <<genA, gsA, inRestart, compB, respawnedB>>
75
+
76
+ \* B completes normally. Under reliable cancellation this cannot happen
77
+ \* once the restart sequence has begun (the cancel is effective at once);
78
+ \* under cooperative cancellation it may land in the window.
79
+ CompleteB ==
80
+ /\ gsB = "running"
81
+ /\ (~STRICTCANCEL \/ phase = "stable")
82
+ /\ compB' = TRUE
83
+ /\ gsB' = "completed"
84
+ /\ UNCHANGED <<genA, gsA, inRestart, respawnedB, phase>>
85
+
86
+ \* Spawn the new generations. A is always respawned; B is respawned iff it
87
+ \* was in the expansion set — regardless of what B did since the snapshot.
88
+ SpawnNewGens ==
89
+ /\ phase = "spawning"
90
+ /\ genA' = genA + 1
91
+ /\ gsA' = "running"
92
+ /\ gsB' = IF inRestart THEN "running" ELSE gsB
93
+ /\ respawnedB' = (respawnedB \/ (inRestart /\ compB))
94
+ /\ inRestart' = FALSE
95
+ /\ phase' = "stable"
96
+ /\ UNCHANGED compB
97
+
98
+ Next == FailA \/ DecideRestart \/ CancelB \/ CompleteB \/ SpawnNewGens
99
+
100
+ Spec == Init /\ [][Next]_vars
101
+
102
+ \* A child that completed is never followed by a new generation.
103
+ CompletedNeverRespawned == ~respawnedB
104
+
105
+ \* Sanity: the model does exercise restarts.
106
+ GenBounded == genA <= MaxGen
107
+
108
+ ====
@@ -0,0 +1,84 @@
1
+ ---- MODULE SupervisorRestartCAS ----
2
+ EXTENDS Naturals
3
+
4
+ (***************************************************************************)
5
+ (* Review variant of SupervisorRestart.tla. *)
6
+ (* *)
7
+ (* CASCancel = TRUE models romancy's superviseSync as written at HEAD: *)
8
+ (* CancelInstance is UPDATE ... WHERE status IN (non-terminal) and an *)
9
+ (* affected-rows = 0 result returns ErrWorkflowNotCancellable, which *)
10
+ (* aborts (rolls back) the sync activity -- no respawn on that pass. *)
11
+ (* CASCancel = FALSE is the original model: respawn from the snapshot no *)
12
+ (* matter what the cancel did. *)
13
+ (* StickyCancel = FALSE models bug 6: a later write of the old generation *)
14
+ (* (completion fenced only on locked_by, timer/channel resume) can move *)
15
+ (* the row off 'cancelled'. *)
16
+ (***************************************************************************)
17
+
18
+ CONSTANTS MaxGen, CASCancel, StickyCancel
19
+
20
+ VARIABLES genA, gsA, gsB, inRestart, compB, respawnedB, phase
21
+
22
+ vars == <<genA, gsA, gsB, inRestart, compB, respawnedB, phase>>
23
+
24
+ Init ==
25
+ /\ genA = 1
26
+ /\ gsA = "running"
27
+ /\ gsB = "running"
28
+ /\ inRestart = FALSE
29
+ /\ compB = FALSE
30
+ /\ respawnedB = FALSE
31
+ /\ phase = "stable"
32
+
33
+ FailA ==
34
+ /\ gsA = "running"
35
+ /\ phase = "stable"
36
+ /\ gsA' = "failed"
37
+ /\ UNCHANGED <<genA, gsB, inRestart, compB, respawnedB, phase>>
38
+
39
+ DecideRestart ==
40
+ /\ phase = "stable"
41
+ /\ gsA = "failed"
42
+ /\ genA < MaxGen
43
+ /\ inRestart' = (gsB /= "completed")
44
+ /\ phase' = "cancelling"
45
+ /\ UNCHANGED <<genA, gsA, gsB, compB, respawnedB>>
46
+
47
+ CancelB ==
48
+ /\ phase = "cancelling"
49
+ /\ IF CASCancel /\ inRestart /\ gsB = "completed"
50
+ THEN \* conditional cancel matched no row -> error -> sync rolled back
51
+ /\ phase' = "stable"
52
+ /\ inRestart' = FALSE
53
+ /\ UNCHANGED gsB
54
+ ELSE /\ gsB' = IF gsB = "running" THEN "cancelled" ELSE gsB
55
+ /\ phase' = "spawning"
56
+ /\ UNCHANGED inRestart
57
+ /\ UNCHANGED <<genA, gsA, compB, respawnedB>>
58
+
59
+ \* Cooperative cancellation: B may complete at any time while running.
60
+ \* Without sticky cancellation its completion write also lands on a
61
+ \* cancelled row (the write is fenced on the lock, not on the status).
62
+ CompleteB ==
63
+ /\ \/ gsB = "running"
64
+ \/ ~StickyCancel /\ gsB = "cancelled"
65
+ /\ compB' = TRUE
66
+ /\ gsB' = "completed"
67
+ /\ UNCHANGED <<genA, gsA, inRestart, respawnedB, phase>>
68
+
69
+ SpawnNewGens ==
70
+ /\ phase = "spawning"
71
+ /\ genA' = genA + 1
72
+ /\ gsA' = "running"
73
+ /\ gsB' = IF inRestart THEN "running" ELSE gsB
74
+ /\ respawnedB' = (respawnedB \/ (inRestart /\ compB))
75
+ /\ inRestart' = FALSE
76
+ /\ phase' = "stable"
77
+ /\ UNCHANGED compB
78
+
79
+ Next == FailA \/ DecideRestart \/ CancelB \/ CompleteB \/ SpawnNewGens
80
+
81
+ Spec == Init /\ [][Next]_vars
82
+
83
+ CompletedNeverRespawned == ~respawnedB
84
+ ====
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ MaxGen = 3
3
+ CASCancel = TRUE
4
+ StickyCancel = FALSE
5
+ SPECIFICATION Spec
6
+ INVARIANTS CompletedNeverRespawned
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ MaxGen = 3
3
+ CASCancel = FALSE
4
+ StickyCancel = TRUE
5
+ SPECIFICATION Spec
6
+ INVARIANTS CompletedNeverRespawned
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ MaxGen = 3
3
+ CASCancel = TRUE
4
+ StickyCancel = TRUE
5
+ SPECIFICATION Spec
6
+ INVARIANTS CompletedNeverRespawned
@@ -0,0 +1,7 @@
1
+ \* Cooperative cancellation: cancel can lose to a concurrent completion.
2
+ \* Expected TLC result: CompletedNeverRespawned violated.
3
+ CONSTANTS
4
+ MaxGen = 3
5
+ STRICTCANCEL = FALSE
6
+ SPECIFICATION Spec
7
+ INVARIANTS CompletedNeverRespawned
@@ -0,0 +1,7 @@
1
+ \* Reliable cancellation: a completed child is never respawned.
2
+ \* Expected TLC result: no errors.
3
+ CONSTANTS
4
+ MaxGen = 3
5
+ STRICTCANCEL = TRUE
6
+ SPECIFICATION Spec
7
+ INVARIANTS CompletedNeverRespawned
@@ -0,0 +1,92 @@
1
+ ---- MODULE TaskClaim ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Task claim protocol (actor_tasks). *)
6
+ (* *)
7
+ (* Workers compete for open tasks on a channel. The real implementation *)
8
+ (* claims via a single atomic statement: *)
9
+ (* UPDATE actor_tasks SET state='claimed', claimed_by=w *)
10
+ (* WHERE id = (SELECT id FROM actor_tasks *)
11
+ (* WHERE channel=c AND state='open' *)
12
+ (* ORDER BY created_at LIMIT 1) AND state='open' *)
13
+ (* which we model as a non-atomic PICK (select a candidate) followed by *)
14
+ (* a CLAIM — CAS-guarded or not. The interesting race: two workers pick *)
15
+ (* the same open task and both attempt to claim it. *)
16
+ (* *)
17
+ (* Configs: *)
18
+ (* old: unconditional claim write (no CAS guard) -> violated *)
19
+ (* new: claim only when state is still 'open' -> holds *)
20
+ (* *)
21
+ (* Orphan reclaim is deliberately not modeled: with reclaim, two workers *)
22
+ (* may legitimately hold the same task at different epochs. SingleClaimant *)
23
+ (* here means "no two workers both believe they won the SAME claim". *)
24
+ (***************************************************************************)
25
+
26
+ CONSTANTS Workers, \* set of worker ids
27
+ Tasks, \* set of task ids
28
+ CAS \* TRUE: guarded claim; FALSE: racy write
29
+
30
+ VARIABLES
31
+ state, \* state[t] in {"open","claimed","done"}
32
+ claimedBy, \* claimedBy[t] in Workers \/ {none}
33
+ picked, \* picked[w]: candidate task w's SELECT returned (none when idle)
34
+ won, \* won[w]: set of tasks w's claim returned success for
35
+ violated \* TRUE once two workers won the same task
36
+
37
+ vars == <<state, claimedBy, picked, won, violated>>
38
+
39
+ None == "none"
40
+
41
+ Init ==
42
+ /\ state = [t \in Tasks |-> "open"]
43
+ /\ claimedBy = [t \in Tasks |-> None]
44
+ /\ picked = [w \in Workers |-> None]
45
+ /\ won = [w \in Workers |-> {}]
46
+ /\ violated = FALSE
47
+
48
+ \* Non-atomic candidate selection: w observes an open task.
49
+ Pick(w, t) ==
50
+ /\ picked[w] = None
51
+ /\ state[t] = "open"
52
+ /\ picked' = [picked EXCEPT ![w] = t]
53
+ /\ UNCHANGED <<state, claimedBy, won, violated>>
54
+
55
+ \* Claim attempt. CAS: succeeds only if the task is STILL open — models
56
+ \* the AND state='open' guard. Non-CAS: the write always lands.
57
+ Claim(w) ==
58
+ LET t == picked[w]
59
+ success == CAS => (state[t] = "open")
60
+ IN
61
+ /\ t # None
62
+ /\ IF success
63
+ THEN /\ state' = [state EXCEPT ![t] = "claimed"]
64
+ /\ claimedBy' = [claimedBy EXCEPT ![t] = w]
65
+ /\ won' = [won EXCEPT ![w] = @ \cup {t}]
66
+ /\ violated' = \/ violated
67
+ \/ \E w2 \in Workers \ {w}: t \in won[w2]
68
+ ELSE UNCHANGED <<state, claimedBy, won, violated>>
69
+ /\ picked' = [picked EXCEPT ![w] = None]
70
+
71
+ \* The claimant finishes its task.
72
+ Complete(w, t) ==
73
+ /\ state[t] = "claimed"
74
+ /\ claimedBy[t] = w
75
+ /\ state' = [state EXCEPT ![t] = "done"]
76
+ /\ UNCHANGED <<claimedBy, picked, won, violated>>
77
+
78
+ Next ==
79
+ \/ \E w \in Workers, t \in Tasks : Pick(w, t)
80
+ \/ \E w \in Workers : Claim(w)
81
+ \/ \E w \in Workers, t \in Tasks : Complete(w, t)
82
+
83
+ Spec == Init /\ [][Next]_vars /\ WF_vars(Next)
84
+
85
+ \* Core safety: no two workers both believe they claimed the same task.
86
+ SingleClaimant == ~violated
87
+
88
+ \* The recorded claimant really did win its claim.
89
+ ClaimantIsWinner ==
90
+ \A t \in Tasks : (state[t] = "claimed") => claimedBy[t] \in Workers
91
+
92
+ ====
@@ -0,0 +1,84 @@
1
+ ---- MODULE TaskClaimReadBack ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Review extension of TaskClaim.tla. *)
6
+ (* *)
7
+ (* TaskClaim.tla hands the winner the id of the row its UPDATE changed *)
8
+ (* (won' = won \cup {t}). The UPDATE ... WHERE id = (subquery) statement *)
9
+ (* used by romancy HEAD and edda does not return the id; the claimant *)
10
+ (* reads it back with *)
11
+ (* SELECT ... WHERE channel = ? AND claimed_by = ? AND state = 'claimed' *)
12
+ (* ORDER BY updated_at DESC LIMIT 1 *)
13
+ (* which is ambiguous when the claimant holds more than one claimed task *)
14
+ (* and updated_at ties (MySQL TIMESTAMP is whole seconds) or is skewed *)
15
+ (* (updated_at comes from the app clock of whichever host ran the claim). *)
16
+ (* *)
17
+ (* ReadBackById = FALSE : the read-back may return any claimed row of w *)
18
+ (* ReadBackById = TRUE : pick the id first, CAS on it, read that id *)
19
+ (* (romancy working tree, shikibu) *)
20
+ (***************************************************************************)
21
+
22
+ CONSTANTS Workers, Tasks, ReadBackById
23
+
24
+ VARIABLES state, claimedBy, picked, pcW, lastWon, believed
25
+
26
+ vars == <<state, claimedBy, picked, pcW, lastWon, believed>>
27
+
28
+ None == "none"
29
+
30
+ Init ==
31
+ /\ state = [t \in Tasks |-> "open"]
32
+ /\ claimedBy = [t \in Tasks |-> None]
33
+ /\ picked = [w \in Workers |-> None]
34
+ /\ pcW = [w \in Workers |-> "idle"]
35
+ /\ lastWon = [w \in Workers |-> None]
36
+ /\ believed = [w \in Workers |-> None]
37
+
38
+ Pick(w, t) ==
39
+ /\ pcW[w] = "idle"
40
+ /\ picked[w] = None
41
+ /\ state[t] = "open"
42
+ /\ picked' = [picked EXCEPT ![w] = t]
43
+ /\ UNCHANGED <<state, claimedBy, pcW, lastWon, believed>>
44
+
45
+ \* CAS claim (AND state = 'open'). The statement reports only a row count.
46
+ Claim(w) ==
47
+ LET t == picked[w] IN
48
+ /\ t # None
49
+ /\ IF state[t] = "open"
50
+ THEN /\ state' = [state EXCEPT ![t] = "claimed"]
51
+ /\ claimedBy' = [claimedBy EXCEPT ![t] = w]
52
+ /\ lastWon' = [lastWon EXCEPT ![w] = t]
53
+ /\ pcW' = [pcW EXCEPT ![w] = "readback"]
54
+ ELSE UNCHANGED <<state, claimedBy, lastWon, pcW>>
55
+ /\ picked' = [picked EXCEPT ![w] = None]
56
+ /\ UNCHANGED believed
57
+
58
+ ReadBack(w) ==
59
+ /\ pcW[w] = "readback"
60
+ /\ IF ReadBackById
61
+ THEN believed' = [believed EXCEPT ![w] = lastWon[w]]
62
+ ELSE \E t2 \in Tasks :
63
+ /\ state[t2] = "claimed"
64
+ /\ claimedBy[t2] = w
65
+ /\ believed' = [believed EXCEPT ![w] = t2]
66
+ /\ pcW' = [pcW EXCEPT ![w] = "idle"]
67
+ /\ UNCHANGED <<state, claimedBy, picked, lastWon>>
68
+
69
+ Complete(w, t) ==
70
+ /\ state[t] = "claimed"
71
+ /\ claimedBy[t] = w
72
+ /\ state' = [state EXCEPT ![t] = "done"]
73
+ /\ UNCHANGED <<claimedBy, picked, pcW, lastWon, believed>>
74
+
75
+ Next ==
76
+ \/ \E w \in Workers, t \in Tasks : Pick(w, t) \/ Complete(w, t)
77
+ \/ \E w \in Workers : Claim(w) \/ ReadBack(w)
78
+
79
+ Spec == Init /\ [][Next]_vars
80
+
81
+ \* The task a claimant works on is the task its claim actually took.
82
+ ReadBackIsOwnClaim ==
83
+ \A w \in Workers : (pcW[w] = "idle" /\ believed[w] # None) => believed[w] = lastWon[w]
84
+ ====
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ Tasks = {t1, t2}
4
+ ReadBackById = TRUE
5
+ SPECIFICATION Spec
6
+ INVARIANTS ReadBackIsOwnClaim
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Workers = {w1, w2}
3
+ Tasks = {t1, t2}
4
+ ReadBackById = FALSE
5
+ SPECIFICATION Spec
6
+ INVARIANTS ReadBackIsOwnClaim
@@ -0,0 +1,8 @@
1
+ \* CAS-guarded claim: two workers can never win the same task.
2
+ \* Expected TLC result: no errors.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ Tasks = {t1, t2}
6
+ CAS = TRUE
7
+ SPECIFICATION Spec
8
+ INVARIANTS SingleClaimant ClaimantIsWinner
@@ -0,0 +1,9 @@
1
+ \* Unguarded claim write: two workers pick the same open task and both
2
+ \* win — the race the AND state='open' guard exists to prevent.
3
+ \* Expected TLC result: SingleClaimant violated.
4
+ CONSTANTS
5
+ Workers = {w1, w2}
6
+ Tasks = {t1, t2}
7
+ CAS = FALSE
8
+ SPECIFICATION Spec
9
+ INVARIANTS SingleClaimant
@@ -0,0 +1,110 @@
1
+ ---- MODULE TaskReclaim ----
2
+ EXTENDS Naturals
3
+
4
+ (***************************************************************************)
5
+ (* Orphan task reclaim protocol (actor_tasks). *)
6
+ (* *)
7
+ (* A worker claims a task, performs its side effect (unconditionally — the *)
8
+ (* worker does not consult the DB before doing the work), then writes the *)
9
+ (* completion. Meanwhile a sweep may reclaim tasks whose claimed_at is *)
10
+ (* stale. There is NO heartbeat/renewal for task claims, so staleness is *)
11
+ (* a heuristic: a slow-but-alive worker's claim can be reclaimed, and a *)
12
+ (* second worker can then claim the same task. *)
13
+ (* *)
14
+ (* Configs: *)
15
+ (* old: unconditional completion write -> violates NoClobber *)
16
+ (* new: write fenced by claimed_by = me -> holds *)
17
+ (* (matches the implementation: UPDATE ... WHERE claimed_by = ? *)
18
+ (* AND state = 'claimed') *)
19
+ (* *)
20
+ (* execs is capped at 2 per worker solely to keep the state space finite; *)
21
+ (* every write interleaving is already observable within that bound. *)
22
+ (* *)
23
+ (* Expected reachability in BOTH configs: duplicate side-effect execution *)
24
+ (* (both workers execute the task's work). Reclaim makes execution *)
25
+ (* at-least-once; fencing protects the row, not the side effect. *)
26
+ (***************************************************************************)
27
+
28
+ CONSTANTS Workers, \* set of worker ids
29
+ Task, \* the single task (a value, e.g. "t1")
30
+ FENCED \* TRUE: completion write guarded by claimed_by
31
+
32
+ VARIABLES
33
+ state, \* "open" | "claimed" | "done"
34
+ claimedBy, \* worker or none
35
+ phase, \* phase[w] in {"idle","busy","execed"}
36
+ execs, \* execs[w]: number of side-effect executions by w
37
+ writer, \* worker whose completion write landed, or none
38
+ clobbered, \* TRUE once a write overwrote an already-done row
39
+ writes \* number of completion writes that landed
40
+
41
+ vars == <<state, claimedBy, phase, execs, writer, clobbered, writes>>
42
+
43
+ None == "none"
44
+
45
+ Init ==
46
+ /\ state = "open"
47
+ /\ claimedBy = None
48
+ /\ phase = [w \in Workers |-> "idle"]
49
+ /\ execs = [w \in Workers |-> 0]
50
+ /\ writer = None
51
+ /\ clobbered = FALSE
52
+ /\ writes = 0
53
+
54
+ \* CAS claim: only when still open (TaskClaim covers the race itself).
55
+ Claim(w) ==
56
+ /\ phase[w] = "idle"
57
+ /\ state = "open"
58
+ /\ state' = "claimed"
59
+ /\ claimedBy' = w
60
+ /\ phase' = [phase EXCEPT ![w] = "busy"]
61
+ /\ UNCHANGED <<execs, writer, clobbered, writes>>
62
+
63
+ \* The work itself. Unconditional: once the claim is held the worker does
64
+ \* the side effect regardless of what the DB row later becomes.
65
+ ExecSideEffect(w) ==
66
+ /\ phase[w] = "busy"
67
+ /\ execs[w] < 2
68
+ /\ execs' = [execs EXCEPT ![w] = @ + 1]
69
+ /\ phase' = [phase EXCEPT ![w] = "execed"]
70
+ /\ UNCHANGED <<state, claimedBy, writer, clobbered, writes>>
71
+
72
+ \* Completion write. Fenced: lands only if the row still names w as the
73
+ \* claimant — models WHERE claimed_by = w AND state = 'claimed'.
74
+ \* Unfenced: always lands, possibly clobbering a done row.
75
+ WriteCompletion(w) ==
76
+ /\ phase[w] = "execed"
77
+ /\ LET guarded == state = "claimed" /\ claimedBy = w
78
+ lands == IF FENCED THEN guarded ELSE TRUE
79
+ IN
80
+ IF lands
81
+ THEN /\ clobbered' = \/ clobbered \/ (state = "done")
82
+ /\ state' = "done"
83
+ /\ writer' = w
84
+ /\ writes' = writes + 1
85
+ ELSE UNCHANGED <<state, writer, clobbered, writes>>
86
+ /\ phase' = [phase EXCEPT ![w] = "idle"]
87
+ /\ UNCHANGED <<claimedBy, execs>>
88
+
89
+ \* Stale sweep: any claimed task may return to open. Models the absence of
90
+ \* heartbeats: the claimant may be slow-but-alive and keeps executing.
91
+ Reclaim ==
92
+ /\ state = "claimed"
93
+ /\ state' = "open"
94
+ /\ claimedBy' = None
95
+ /\ UNCHANGED <<phase, execs, writer, clobbered, writes>>
96
+
97
+ Next ==
98
+ \/ \E w \in Workers : Claim(w) \/ ExecSideEffect(w) \/ WriteCompletion(w)
99
+ \/ Reclaim
100
+
101
+ Spec == Init /\ [][Next]_vars /\ WF_vars(Next)
102
+
103
+ \* A write never overwrites an already-completed task row.
104
+ NoClobber == ~clobbered
105
+
106
+ \* At most one completion write ever lands. (The original definition,
107
+ \* writer = None \/ state = "done", is a tautology: state never leaves done.)
108
+ SingleWriter == writes <= 1
109
+
110
+ ====
@@ -0,0 +1,73 @@
1
+ ---- MODULE TaskReclaimAttempts ----
2
+ EXTENDS Naturals
3
+
4
+ (***************************************************************************)
5
+ (* Review extension of TaskReclaim.tla. *)
6
+ (* *)
7
+ (* TaskReclaim.tla gives each worker ONE phase, so a claimant can never *)
8
+ (* have two executions of the same task in flight. In all three *)
9
+ (* implementations claimed_by is the workflow INSTANCE id, and the same *)
10
+ (* instance can hold two attempts: a stale run of the instance (lost lease)*)
11
+ (* next to its replacement run, or (romancy HEAD, edda, shikibu HEAD) a *)
12
+ (* replay that re-executes the unrecorded ClaimTask after an orphan *)
13
+ (* reclaim. The completion fence *)
14
+ (* WHERE id = ? AND claimed_by = ? AND state = 'claimed' *)
15
+ (* then cannot tell the attempts apart. *)
16
+ (* *)
17
+ (* FenceAttempt = FALSE : fence on claimant only (all implementations) *)
18
+ (* FenceAttempt = TRUE : also AND attempts = <attempt the run claimed> *)
19
+ (***************************************************************************)
20
+
21
+ CONSTANTS Claimants, MaxAttempts, FenceAttempt
22
+
23
+ VARIABLES state, claimedBy, attempt, inflight, staleLanded
24
+
25
+ vars == <<state, claimedBy, attempt, inflight, staleLanded>>
26
+
27
+ None == "none"
28
+
29
+ Init ==
30
+ /\ state = "open"
31
+ /\ claimedBy = None
32
+ /\ attempt = 0
33
+ /\ inflight = {}
34
+ /\ staleLanded = FALSE
35
+
36
+ Claim(c) ==
37
+ /\ state = "open"
38
+ /\ attempt < MaxAttempts
39
+ /\ state' = "claimed"
40
+ /\ claimedBy' = c
41
+ /\ attempt' = attempt + 1
42
+ /\ inflight' = inflight \cup {[c |-> c, a |-> attempt + 1]}
43
+ /\ UNCHANGED staleLanded
44
+
45
+ \* Orphan sweep: claimed_at older than 300 s (no heartbeat on task claims).
46
+ Reclaim ==
47
+ /\ state = "claimed"
48
+ /\ state' = "open"
49
+ /\ claimedBy' = None
50
+ /\ UNCHANGED <<attempt, inflight, staleLanded>>
51
+
52
+ Complete(x) ==
53
+ /\ x \in inflight
54
+ /\ LET lands == /\ state = "claimed"
55
+ /\ claimedBy = x.c
56
+ /\ FenceAttempt => attempt = x.a
57
+ IN IF lands
58
+ THEN /\ state' = "done"
59
+ /\ staleLanded' = (staleLanded \/ attempt # x.a)
60
+ ELSE UNCHANGED <<state, staleLanded>>
61
+ /\ inflight' = inflight \ {x}
62
+ /\ UNCHANGED <<claimedBy, attempt>>
63
+
64
+ Next ==
65
+ \/ \E c \in Claimants : Claim(c)
66
+ \/ Reclaim
67
+ \/ \E x \in inflight : Complete(x)
68
+
69
+ Spec == Init /\ [][Next]_vars
70
+
71
+ \* The completion that lands comes from the attempt that holds the claim.
72
+ NoStaleAttemptLands == ~staleLanded
73
+ ====
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Claimants = {i1, i2}
3
+ MaxAttempts = 3
4
+ FenceAttempt = TRUE
5
+ SPECIFICATION Spec
6
+ INVARIANTS NoStaleAttemptLands
@@ -0,0 +1,6 @@
1
+ CONSTANTS
2
+ Claimants = {i1}
3
+ MaxAttempts = 3
4
+ FenceAttempt = FALSE
5
+ SPECIFICATION Spec
6
+ INVARIANTS NoStaleAttemptLands
@@ -0,0 +1,8 @@
1
+ \* Fenced completion write: a stale worker's write is rejected.
2
+ \* Expected TLC result: no errors.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ Task = t1
6
+ FENCED = TRUE
7
+ SPECIFICATION Spec
8
+ INVARIANTS NoClobber SingleWriter
@@ -0,0 +1,8 @@
1
+ \* Unconditional completion write: stale write clobbers the done row.
2
+ \* Expected TLC result: NoClobber violated.
3
+ CONSTANTS
4
+ Workers = {w1, w2}
5
+ Task = t1
6
+ FENCED = FALSE
7
+ SPECIFICATION Spec
8
+ INVARIANTS NoClobber SingleWriter