shikibu 0.3.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 (137) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +82 -0
  3. data/lib/shikibu/activity.rb +84 -48
  4. data/lib/shikibu/actors.rb +129 -0
  5. data/lib/shikibu/app.rb +33 -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 +242 -107
  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/version.rb +1 -1
  23. data/lib/shikibu/worker.rb +155 -73
  24. data/lib/shikibu/workflow.rb +106 -48
  25. data/lib/shikibu.rb +4 -0
  26. data/schema/SEMANTICS.md +351 -0
  27. data/schema/db/migrations/mysql/20261123000000_outbox_claimed_at.sql +5 -0
  28. data/schema/db/migrations/mysql/20261201000000_actor_registry.sql +24 -0
  29. data/schema/db/migrations/mysql/20261202000000_actor_capabilities.sql +15 -0
  30. data/schema/db/migrations/mysql/20261203000000_payload_blobs.sql +11 -0
  31. data/schema/db/migrations/mysql/20261204000000_actor_fs.sql +13 -0
  32. data/schema/db/migrations/mysql/20261205000000_actor_tasks.sql +15 -0
  33. data/schema/db/migrations/mysql/20261206000000_actor_tasks_claimed_at.sql +7 -0
  34. data/schema/db/migrations/mysql/20261207000000_add_sandboxed.sql +5 -0
  35. data/schema/db/migrations/mysql/20261208000000_widen_actor_owner_columns.sql +15 -0
  36. data/schema/db/migrations/postgresql/20261123000000_outbox_claimed_at.sql +5 -0
  37. data/schema/db/migrations/postgresql/20261201000000_actor_registry.sql +23 -0
  38. data/schema/db/migrations/postgresql/20261202000000_actor_capabilities.sql +15 -0
  39. data/schema/db/migrations/postgresql/20261203000000_payload_blobs.sql +11 -0
  40. data/schema/db/migrations/postgresql/20261204000000_actor_fs.sql +13 -0
  41. data/schema/db/migrations/postgresql/20261205000000_actor_tasks.sql +15 -0
  42. data/schema/db/migrations/postgresql/20261206000000_actor_tasks_claimed_at.sql +7 -0
  43. data/schema/db/migrations/postgresql/20261207000000_add_sandboxed.sql +5 -0
  44. data/schema/db/migrations/postgresql/20261208000000_widen_actor_owner_columns.sql +7 -0
  45. data/schema/db/migrations/sqlite/20261123000000_outbox_claimed_at.sql +5 -0
  46. data/schema/db/migrations/sqlite/20261201000000_actor_registry.sql +23 -0
  47. data/schema/db/migrations/sqlite/20261202000000_actor_capabilities.sql +15 -0
  48. data/schema/db/migrations/sqlite/20261203000000_payload_blobs.sql +11 -0
  49. data/schema/db/migrations/sqlite/20261204000000_actor_fs.sql +13 -0
  50. data/schema/db/migrations/sqlite/20261205000000_actor_tasks.sql +15 -0
  51. data/schema/db/migrations/sqlite/20261206000000_actor_tasks_claimed_at.sql +7 -0
  52. data/schema/db/migrations/sqlite/20261207000000_add_sandboxed.sql +5 -0
  53. data/schema/db/migrations/sqlite/20261208000000_widen_actor_owner_columns.sql +7 -0
  54. data/schema/docs/column-values.md +10 -0
  55. data/schema/spec/ActorFS.tla +117 -0
  56. data/schema/spec/ActorFS_lww_monotonic.cfg +7 -0
  57. data/schema/spec/ActorFS_new.cfg +8 -0
  58. data/schema/spec/ActorFS_old.cfg +7 -0
  59. data/schema/spec/CompetingMessageClaim.tla +127 -0
  60. data/schema/spec/CompetingMessageClaimSplit.tla +132 -0
  61. data/schema/spec/CompetingMessageClaimSplit_atomic_lost.cfg +7 -0
  62. data/schema/spec/CompetingMessageClaimSplit_atomic_once.cfg +7 -0
  63. data/schema/spec/CompetingMessageClaimSplit_fixed.cfg +7 -0
  64. data/schema/spec/CompetingMessageClaimSplit_split_lost.cfg +7 -0
  65. data/schema/spec/CompetingMessageClaimSplit_split_once.cfg +7 -0
  66. data/schema/spec/CompetingMessageClaimSplit_waitonly_lost.cfg +7 -0
  67. data/schema/spec/CompetingMessageClaimSplit_waitonly_once.cfg +7 -0
  68. data/schema/spec/CompetingMessageClaim_new.cfg +10 -0
  69. data/schema/spec/CompetingMessageClaim_old.cfg +11 -0
  70. data/schema/spec/EpochFencing.tla +87 -0
  71. data/schema/spec/EpochFencingRuns.tla +107 -0
  72. data/schema/spec/EpochFencingRuns_epoch.cfg +6 -0
  73. data/schema/spec/EpochFencingRuns_none.cfg +6 -0
  74. data/schema/spec/EpochFencingRuns_witness.cfg +6 -0
  75. data/schema/spec/EpochFencingRuns_worker.cfg +6 -0
  76. data/schema/spec/EpochFencingRuns_worker1.cfg +6 -0
  77. data/schema/spec/EpochFencing_new.cfg +8 -0
  78. data/schema/spec/EpochFencing_old.cfg +9 -0
  79. data/schema/spec/InstanceClaim.tla +196 -0
  80. data/schema/spec/InstanceClaimVerdict.tla +116 -0
  81. data/schema/spec/InstanceClaimVerdict_changedrows.cfg +6 -0
  82. data/schema/spec/InstanceClaimVerdict_changedrows_live.cfg +6 -0
  83. data/schema/spec/InstanceClaimVerdict_foundrows.cfg +6 -0
  84. data/schema/spec/InstanceClaimVerdict_foundrows_live.cfg +6 -0
  85. data/schema/spec/InstanceClaim_atomic_unfenced.cfg +9 -0
  86. data/schema/spec/InstanceClaim_live.cfg +9 -0
  87. data/schema/spec/InstanceClaim_new.cfg +9 -0
  88. data/schema/spec/InstanceClaim_old.cfg +11 -0
  89. data/schema/spec/InstanceClaim_racy_fenced.cfg +9 -0
  90. data/schema/spec/README.md +97 -0
  91. data/schema/spec/SupervisorRestart.tla +108 -0
  92. data/schema/spec/SupervisorRestartCAS.tla +84 -0
  93. data/schema/spec/SupervisorRestartCAS_nonsticky.cfg +6 -0
  94. data/schema/spec/SupervisorRestartCAS_snapshot.cfg +6 -0
  95. data/schema/spec/SupervisorRestartCAS_sticky.cfg +6 -0
  96. data/schema/spec/SupervisorRestart_racy.cfg +7 -0
  97. data/schema/spec/SupervisorRestart_strict.cfg +7 -0
  98. data/schema/spec/TaskClaim.tla +92 -0
  99. data/schema/spec/TaskClaimReadBack.tla +84 -0
  100. data/schema/spec/TaskClaimReadBack_byid.cfg +6 -0
  101. data/schema/spec/TaskClaimReadBack_order.cfg +6 -0
  102. data/schema/spec/TaskClaim_new.cfg +8 -0
  103. data/schema/spec/TaskClaim_old.cfg +9 -0
  104. data/schema/spec/TaskReclaim.tla +110 -0
  105. data/schema/spec/TaskReclaimAttempts.tla +73 -0
  106. data/schema/spec/TaskReclaimAttempts_attempt.cfg +6 -0
  107. data/schema/spec/TaskReclaimAttempts_claimant.cfg +6 -0
  108. data/schema/spec/TaskReclaim_new.cfg +8 -0
  109. data/schema/spec/TaskReclaim_old.cfg +8 -0
  110. data/schema/spec/WaitRegistration.tla +170 -0
  111. data/schema/spec/WaitRegistrationSub.tla +133 -0
  112. data/schema/spec/WaitRegistrationSub_fixed.cfg +6 -0
  113. data/schema/spec/WaitRegistrationSub_fixed_guarded.cfg +6 -0
  114. data/schema/spec/WaitRegistrationSub_guardonly_reg.cfg +6 -0
  115. data/schema/spec/WaitRegistrationSub_guardonly_res.cfg +6 -0
  116. data/schema/spec/WaitRegistrationSub_head_reg.cfg +6 -0
  117. data/schema/spec/WaitRegistrationSub_head_res.cfg +6 -0
  118. data/schema/spec/WaitRegistrationSub_noclear_reg.cfg +6 -0
  119. data/schema/spec/WaitRegistrationSub_noclear_res.cfg +6 -0
  120. data/schema/spec/WaitRegistrationSub_unfencedreg_reg.cfg +6 -0
  121. data/schema/spec/WaitRegistrationSub_unfencedreg_res.cfg +6 -0
  122. data/schema/spec/WaitRegistration_new.cfg +10 -0
  123. data/schema/spec/WaitRegistration_old.cfg +11 -0
  124. data/schema/spec/WaitRegistration_residual.cfg +11 -0
  125. data/schema/spec/WaitRegistration_sweep.cfg +11 -0
  126. data/schema/spec/actorfs_check_test.go +165 -0
  127. data/schema/spec/claim_check_test.go +387 -0
  128. data/schema/spec/competing_claim_test.go +194 -0
  129. data/schema/spec/epoch_check_test.go +128 -0
  130. data/schema/spec/go.mod +3 -0
  131. data/schema/spec/supervisorrestart_check_test.go +131 -0
  132. data/schema/spec/taskclaim_check_test.go +135 -0
  133. data/schema/spec/taskreclaim_check_test.go +125 -0
  134. data/schema/spec/tlc-check.sh +45 -0
  135. data/schema/spec/tlc-matrix.txt +68 -0
  136. data/schema/spec/wait_check_test.go +256 -0
  137. metadata +121 -2
@@ -0,0 +1,13 @@
1
+ -- migrate:up
2
+ CREATE TABLE IF NOT EXISTS actor_fs (
3
+ path TEXT PRIMARY KEY,
4
+ owner TEXT,
5
+ data BLOB NOT NULL,
6
+ version INTEGER NOT NULL DEFAULT 1,
7
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
8
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
9
+ );
10
+ CREATE INDEX IF NOT EXISTS idx_actor_fs_owner ON actor_fs(owner);
11
+
12
+ -- migrate:down
13
+ DROP TABLE IF EXISTS actor_fs;
@@ -0,0 +1,15 @@
1
+ -- migrate:up
2
+ CREATE TABLE IF NOT EXISTS actor_tasks (
3
+ id TEXT PRIMARY KEY,
4
+ channel TEXT NOT NULL,
5
+ data TEXT,
6
+ state TEXT NOT NULL DEFAULT 'open',
7
+ claimed_by TEXT,
8
+ result TEXT,
9
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
10
+ updated_at TEXT NOT NULL DEFAULT (datetime('now'))
11
+ );
12
+ CREATE INDEX IF NOT EXISTS idx_actor_tasks_channel_state ON actor_tasks(channel, state);
13
+
14
+ -- migrate:down
15
+ DROP TABLE IF EXISTS actor_tasks;
@@ -0,0 +1,7 @@
1
+ -- migrate:up
2
+ ALTER TABLE actor_tasks ADD COLUMN claimed_at TEXT;
3
+ ALTER TABLE actor_tasks ADD COLUMN attempts INTEGER NOT NULL DEFAULT 0;
4
+
5
+ -- migrate:down
6
+ ALTER TABLE actor_tasks DROP COLUMN claimed_at;
7
+ ALTER TABLE actor_tasks DROP COLUMN attempts;
@@ -0,0 +1,5 @@
1
+ -- migrate:up
2
+ ALTER TABLE workflow_instances ADD COLUMN sandboxed INTEGER NOT NULL DEFAULT 0;
3
+
4
+ -- migrate:down
5
+ ALTER TABLE workflow_instances DROP COLUMN sandboxed;
@@ -0,0 +1,7 @@
1
+ -- migrate:up
2
+ -- MySQL only: these columns are TEXT here, with no width to widen. The
3
+ -- version exists in every dialect so the migration sets stay identical.
4
+ SELECT 1;
5
+
6
+ -- migrate:down
7
+ SELECT 1;
@@ -65,6 +65,16 @@ All event types use **PascalCase** for cross-language consistency.
65
65
  | `invalid` | Event is malformed and cannot be published |
66
66
  | `expired` | Event expired before it could be published |
67
67
 
68
+ ## workflow_instances.sandboxed
69
+
70
+ | Value | Description |
71
+ |-------|-------------|
72
+ | `0` | Instance is unsandboxed and may mint capabilities |
73
+ | `1` | Instance is sandboxed; capability minting is forbidden and children inherit the flag |
74
+
75
+ This is stored as `INTEGER` rather than a dialect-native boolean for
76
+ cross-database consistency.
77
+
68
78
  ## workflow_instances.framework
69
79
 
70
80
  | Value | Description |
@@ -0,0 +1,117 @@
1
+ ---- MODULE ActorFS ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Shared-FS optimistic-concurrency (CAS) protocol. *)
6
+ (* *)
7
+ (* Multiple writers race on one path. Each write carries expected_version: *)
8
+ (* expected 0 -> create (only when absent) *)
9
+ (* expected >0 -> update only if stored version matches *)
10
+ (* *)
11
+ (* The bug class under test: last-writer-wins (unconditional writes) let *)
12
+ (* two writers who both observed version v both "succeed", silently *)
13
+ (* losing one update. CAS must ensure at most one writer succeeds per base *)
14
+ (* version. *)
15
+ (* *)
16
+ (* Configs: *)
17
+ (* old: unconditional writes (LWW) -> NoLostUpdate violated *)
18
+ (* new: conditional CAS writes -> NoLostUpdate holds *)
19
+ (***************************************************************************)
20
+
21
+ CONSTANTS Writers, \* set of writer ids
22
+ CAS \* TRUE: CAS writes (new); FALSE: LWW (old)
23
+
24
+ VARIABLES
25
+ exists, \* file exists at path
26
+ version, \* stored version (0 when absent)
27
+ storedBy, \* last successful writer ("none" when absent)
28
+ readVer, \* readVer[w]: version writer w last observed
29
+ hasRead, \* hasRead[w]: w has an observed version
30
+ outcome, \* outcome[w]: "none" | "ok" | "conflict" for the last attempt
31
+ winsAt \* winsAt[v]: set of writers that succeeded writing at base v
32
+
33
+ vars == <<exists, version, storedBy, readVer, hasRead, outcome, winsAt>>
34
+
35
+ NoWriter == "none"
36
+ MaxVersion == Cardinality(Writers)
37
+
38
+ Init ==
39
+ /\ exists = FALSE
40
+ /\ version = 0
41
+ /\ storedBy = NoWriter
42
+ /\ readVer = [w \in Writers |-> 0]
43
+ /\ hasRead = [w \in Writers |-> FALSE]
44
+ /\ outcome = [w \in Writers |-> "none"]
45
+ /\ winsAt = [v \in 0..MaxVersion |-> {}]
46
+
47
+ \* Writer observes the current stored version.
48
+ WRead(w) ==
49
+ /\ readVer' = [readVer EXCEPT ![w] = version]
50
+ /\ hasRead' = [hasRead EXCEPT ![w] = TRUE]
51
+ /\ UNCHANGED <<exists, version, storedBy, outcome, winsAt>>
52
+
53
+ \* Create: expected_version = 0 — succeeds only when absent.
54
+ \* (Under LWW the create clobbers unconditionally.)
55
+ WCreate(w) ==
56
+ /\ outcome' = [outcome EXCEPT ![w] =
57
+ IF CAS
58
+ THEN IF ~exists THEN "ok" ELSE "conflict"
59
+ ELSE "ok"]
60
+ /\ IF outcome'[w] = "ok"
61
+ THEN /\ winsAt' = [winsAt EXCEPT ![0] = @ \cup {w}]
62
+ /\ exists' = TRUE
63
+ /\ version' = 1
64
+ /\ storedBy' = w
65
+ ELSE UNCHANGED <<exists, version, storedBy, winsAt>>
66
+ /\ UNCHANGED <<readVer, hasRead>>
67
+
68
+ \* Update: expected_version = last observed version — succeeds only when
69
+ \* the stored version still matches (CAS) or always (LWW).
70
+ WUpdate(w) ==
71
+ /\ hasRead[w] /\ readVer[w] > 0
72
+ /\ ~\/ outcome[w] = "ok"
73
+ /\ version < MaxVersion
74
+ /\ LET base == readVer[w] IN
75
+ /\ outcome' = [outcome EXCEPT ![w] =
76
+ IF CAS
77
+ THEN IF version = base THEN "ok" ELSE "conflict"
78
+ ELSE "ok"]
79
+ /\ IF outcome'[w] = "ok"
80
+ THEN /\ winsAt' = [winsAt EXCEPT ![base] = @ \cup {w}]
81
+ /\ version' = version + 1
82
+ /\ storedBy' = w
83
+ /\ exists' = TRUE
84
+ ELSE UNCHANGED <<exists, version, storedBy, winsAt>>
85
+ /\ UNCHANGED <<readVer, hasRead>>
86
+
87
+ \* Re-arm a writer after a conflict: it may re-read and retry (CAS retry loop).
88
+ WRetry(w) ==
89
+ /\ outcome[w] = "conflict"
90
+ /\ outcome' = [outcome EXCEPT ![w] = "none"]
91
+ /\ UNCHANGED <<exists, version, storedBy, readVer, hasRead, winsAt>>
92
+
93
+ Next == \E w \in Writers : WRead(w) \/ WCreate(w) \/ WUpdate(w) \/ WRetry(w)
94
+
95
+ Spec == Init /\ [][Next]_vars /\ WF_vars(Next)
96
+
97
+ \* At most one writer may succeed a write based on any single version.
98
+ NoLostUpdate ==
99
+ \A v \in 0..MaxVersion : Cardinality(winsAt[v]) <= 1
100
+
101
+ \* State sanity (this is NOT monotonicity: it cannot see a regression).
102
+ VersionMonotonic == version >= 0 /\ (exists => version >= 1)
103
+
104
+ \* Successful writes only ever extend the file: the stored version never
105
+ \* regresses. An action property -- check it under PROPERTIES.
106
+ VersionNeverRegresses == [][version' >= version]_version
107
+
108
+ \* Liveness: every writer that entered a write attempt resolves it
109
+ \* (ok or conflict) — CAS never leaves a writer stuck mid-attempt.
110
+ AttemptResolves == \A w \in Writers : hasRead[w] ~> (outcome[w] # "none")
111
+
112
+ \* AttemptResolves needs per-writer fairness: under WF_vars(Next) alone one
113
+ \* writer can loop WCreate(conflict)/WRetry forever while the other never
114
+ \* moves (TLC: "Temporal property AttemptResolves was violated").
115
+ FairSpec == Spec /\ \A w \in Writers : WF_vars(WCreate(w) \/ WUpdate(w))
116
+
117
+ ====
@@ -0,0 +1,7 @@
1
+ \* Last-writer-wins: the stored version can go backwards.
2
+ \* Expected TLC result: action property VersionNeverRegresses violated.
3
+ CONSTANTS
4
+ Writers = {w1, w2}
5
+ CAS = FALSE
6
+ SPECIFICATION Spec
7
+ PROPERTIES VersionNeverRegresses
@@ -0,0 +1,8 @@
1
+ \* CAS protocol: at most one winner per base version.
2
+ \* Expected TLC result: no errors.
3
+ CONSTANTS
4
+ Writers = {w1, w2}
5
+ CAS = TRUE
6
+ SPECIFICATION FairSpec
7
+ INVARIANTS NoLostUpdate VersionMonotonic
8
+ PROPERTIES AttemptResolves VersionNeverRegresses
@@ -0,0 +1,7 @@
1
+ \* Last-writer-wins: unconditional writes.
2
+ \* Expected TLC result: NoLostUpdate violated (two writers, same base, both win).
3
+ CONSTANTS
4
+ Writers = {w1, w2}
5
+ CAS = FALSE
6
+ SPECIFICATION Spec
7
+ INVARIANTS NoLostUpdate VersionMonotonic
@@ -0,0 +1,127 @@
1
+ ---- MODULE CompetingMessageClaim ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Competing-channel message claim protocol. *)
6
+ (* *)
7
+ (* One message, N competing consumers. A consumer must claim the message *)
8
+ (* before recording receive history; recording consumes (deletes) it. *)
9
+ (* *)
10
+ (* The bug class under test: a consumer that claims and dies before *)
11
+ (* consuming leaves an orphan claim. Without takeover rules (stale claim, *)
12
+ (* terminal owner) the message is hidden from every other consumer *)
13
+ (* forever. *)
14
+ (* *)
15
+ (* Configs: *)
16
+ (* old: claim once, never take over -> liveness violated *)
17
+ (* new: takeover on stale/terminal -> progress holds *)
18
+ (***************************************************************************)
19
+
20
+ CONSTANTS Consumers, \* set of consumer ids
21
+ Takeover \* TRUE: stale/terminal takeover enabled (new protocol)
22
+
23
+ VARIABLES
24
+ msgState, \* "unclaimed" | "claimed" | "consumed"
25
+ owner, \* current claim holder ("none" when unclaimed)
26
+ fresh, \* claim freshness: FALSE models past the stale threshold
27
+ alive, \* alive[c]: consumer c has not died
28
+ consumedBy \* consumers that recorded receive history (the consume event)
29
+
30
+ vars == <<msgState, owner, fresh, alive, consumedBy>>
31
+
32
+ NoOwner == "none"
33
+
34
+ Init ==
35
+ /\ msgState = "unclaimed"
36
+ /\ owner = NoOwner
37
+ /\ fresh = FALSE
38
+ /\ alive = [c \in Consumers |-> TRUE]
39
+ /\ consumedBy = {}
40
+
41
+ (***************************************************************************)
42
+ (* Claim: succeeds for consumer c when the message is unclaimed, or the *)
43
+ (* existing claim is voidable: same owner (idempotent reclaim), stale *)
44
+ (* claim, or a claim whose owner reached a terminal state. A fresh claim *)
45
+ (* by another live consumer is the only thing that blocks. *)
46
+ (***************************************************************************)
47
+ Claim(c) ==
48
+ /\ alive[c]
49
+ /\ msgState /= "consumed"
50
+ /\ \/ msgState = "unclaimed"
51
+ \/ /\ Takeover
52
+ /\ msgState = "claimed"
53
+ /\ \/ owner = c
54
+ \/ ~fresh
55
+ \/ ~alive[owner]
56
+ /\ msgState' = "claimed"
57
+ /\ owner' = c
58
+ /\ fresh' = TRUE
59
+ /\ UNCHANGED <<alive, consumedBy>>
60
+
61
+ (***************************************************************************)
62
+ (* Consume: the claim owner records receive history and deletes the *)
63
+ (* message+claim. Atomic enough for the model: history recorded and row *)
64
+ (* deleted in one step. The owner's own claim stays consumable regardless *)
65
+ (* of its freshness. *)
66
+ (***************************************************************************)
67
+ Consume(c) ==
68
+ /\ alive[c]
69
+ /\ msgState = "claimed"
70
+ /\ owner = c
71
+ /\ msgState' = "consumed"
72
+ /\ consumedBy' = consumedBy \cup {c}
73
+ /\ UNCHANGED <<owner, fresh, alive>>
74
+
75
+ (***************************************************************************)
76
+ (* Time passing: a held claim ages past the staleness threshold. *)
77
+ (***************************************************************************)
78
+ Expire ==
79
+ /\ msgState = "claimed"
80
+ /\ fresh
81
+ /\ fresh' = FALSE
82
+ /\ UNCHANGED <<msgState, owner, alive, consumedBy>>
83
+
84
+ (***************************************************************************)
85
+ (* A consumer dies (process crash or its workflow reaches a terminal *)
86
+ (* status) possibly while holding a claim. It never revives. *)
87
+ (***************************************************************************)
88
+ Die(c) ==
89
+ /\ alive[c]
90
+ /\ alive' = [alive EXCEPT ![c] = FALSE]
91
+ /\ UNCHANGED <<msgState, owner, fresh, consumedBy>>
92
+
93
+ Next ==
94
+ \/ \E c \in Consumers : Claim(c)
95
+ \/ \E c \in Consumers : Consume(c)
96
+ \/ \E c \in Consumers : Die(c)
97
+ \/ Expire
98
+
99
+ (***************************************************************************)
100
+ (* Safety: at most one consumer ever records the message — the *)
101
+ (* "exactly one subscriber" guarantee of competing mode. *)
102
+ (***************************************************************************)
103
+ AtMostOneConsume == Cardinality(consumedBy) =< 1
104
+
105
+ (***************************************************************************)
106
+ (* Liveness: the message is eventually consumed provided some consumer *)
107
+ (* stays alive forever and keeps trying. Modelled weakly: if at least one *)
108
+ (* consumer is permanently alive, the message must eventually be consumed. *)
109
+ (* Fairness on claims is assumed; without it a deadlocked claimant could *)
110
+ (* hold the message while everyone waits. *)
111
+ (***************************************************************************)
112
+ SomeAliveForever == \E c \in Consumers : []<>(alive[c])
113
+
114
+ ConsumedEventually == SomeAliveForever ~> (msgState = "consumed")
115
+
116
+ (***************************************************************************)
117
+ (* Fairness: a live consumer eventually claims when it can (WF on Claim), *)
118
+ (* and a consumer that is infinitely often able to consume does so (SF on *)
119
+ (* Consume — without it, two consumers could pass a stale claim back and *)
120
+ (* forth forever, each one's Consume enabled only during its own claim). *)
121
+ (* Die and Expire are environment actions: they MAY happen, progress must *)
122
+ (* hold whether or not they do. *)
123
+ (***************************************************************************)
124
+ Spec == Init /\ [][Next]_vars
125
+ /\ \A c \in Consumers : WF_vars(Claim(c)) /\ SF_vars(Consume(c))
126
+
127
+ ====
@@ -0,0 +1,132 @@
1
+ ---- MODULE CompetingMessageClaimSplit ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Review extension of CompetingMessageClaim.tla. *)
6
+ (* *)
7
+ (* The original Consume(c) re-checks `owner = c` and records + deletes in *)
8
+ (* one atomic step, so AtMostOneConsume holds by construction. In the *)
9
+ (* implementations the three steps are separate statements (romancy HEAD *)
10
+ (* and working tree: DeliverChannelMessageWithLock and Receive; edda pull *)
11
+ (* path; shikibu HEAD): claim (INSERT or takeover UPDATE) -> INSERT history *)
12
+ (* (fenced on the INSTANCE lock, not on the claim) -> DELETE claim+message. *)
13
+ (* *)
14
+ (* AtomicConsume = FALSE : claim, record, delete are separate steps and *)
15
+ (* record does not re-check the claim *)
16
+ (* AtomicConsume = TRUE : record+delete happen in the claim's *)
17
+ (* transaction while the claim row is locked *)
18
+ (* (equivalent to the original model) *)
19
+ (* OnlyWaiting = FALSE : the push path may deliver to a subscriber *)
20
+ (* whose Receive is not suspended; the history *)
21
+ (* slot it writes is never read (bug 5) *)
22
+ (***************************************************************************)
23
+
24
+ CONSTANTS Consumers, Takeover, AtomicConsume, OnlyWaiting
25
+
26
+ VARIABLES msgState, \* "unclaimed" | "claimed" | "consumed"
27
+ owner, \* claim holder
28
+ fresh, \* claim younger than the stale threshold
29
+ alive,
30
+ pcC, \* pcC[c]: "idle" | "claimed" | "recorded"
31
+ route, \* route[c]: "push" (publisher delivers into c's wait slot)
32
+ \* | "drain" (c's own running Receive) | "none"
33
+ waiting, \* waiting[c]: c's Receive is suspended on the channel
34
+ consumedBy, \* consumers that recorded the message in history
35
+ lostRecord \* a record landed in a slot no Receive reads
36
+
37
+ vars == <<msgState, owner, fresh, alive, pcC, route, waiting, consumedBy, lostRecord>>
38
+
39
+ NoOwner == "none"
40
+
41
+ Init ==
42
+ /\ msgState = "unclaimed"
43
+ /\ owner = NoOwner
44
+ /\ fresh = FALSE
45
+ /\ alive = [c \in Consumers |-> TRUE]
46
+ /\ pcC = [c \in Consumers |-> "idle"]
47
+ /\ route = [c \in Consumers |-> "none"]
48
+ /\ waiting = [c \in Consumers |-> FALSE]
49
+ /\ consumedBy = {}
50
+ /\ lostRecord = FALSE
51
+
52
+ Claimable(c) ==
53
+ \/ msgState = "unclaimed"
54
+ \/ /\ Takeover
55
+ /\ msgState = "claimed"
56
+ /\ \/ owner = c
57
+ \/ ~fresh
58
+ \/ ~alive[owner]
59
+
60
+ \* Delivery attempt on behalf of c (push path, holding c's instance lock,
61
+ \* or c's own Receive drain). Claims first.
62
+ Claim(c, r) ==
63
+ /\ alive[c]
64
+ /\ pcC[c] = "idle"
65
+ /\ msgState /= "consumed"
66
+ /\ Claimable(c)
67
+ /\ IF r = "drain" THEN ~waiting[c] ELSE (OnlyWaiting => waiting[c])
68
+ /\ msgState' = "claimed"
69
+ /\ owner' = c
70
+ /\ fresh' = TRUE
71
+ /\ pcC' = [pcC EXCEPT ![c] = "claimed"]
72
+ /\ route' = [route EXCEPT ![c] = r]
73
+ /\ UNCHANGED <<alive, waiting, consumedBy, lostRecord>>
74
+
75
+ Record(c) ==
76
+ /\ alive[c]
77
+ /\ pcC[c] = "claimed"
78
+ /\ AtomicConsume => (msgState = "claimed" /\ owner = c)
79
+ /\ consumedBy' = consumedBy \cup {c}
80
+ /\ lostRecord' = (lostRecord \/ (route[c] = "push" /\ ~waiting[c]))
81
+ /\ IF AtomicConsume
82
+ THEN /\ msgState' = "consumed"
83
+ /\ pcC' = [pcC EXCEPT ![c] = "idle"]
84
+ ELSE /\ pcC' = [pcC EXCEPT ![c] = "recorded"]
85
+ /\ UNCHANGED msgState
86
+ /\ UNCHANGED <<owner, fresh, alive, waiting, route>>
87
+
88
+ \* Separate DELETE of claim + message (no-op if already gone).
89
+ Delete(c) ==
90
+ /\ ~AtomicConsume
91
+ /\ pcC[c] = "recorded"
92
+ /\ msgState' = "consumed"
93
+ /\ pcC' = [pcC EXCEPT ![c] = "idle"]
94
+ /\ UNCHANGED <<owner, fresh, alive, waiting, route, consumedBy, lostRecord>>
95
+
96
+ \* Atomic variant: a consumer whose claim was taken over gives up.
97
+ Abandon(c) ==
98
+ /\ AtomicConsume
99
+ /\ pcC[c] = "claimed"
100
+ /\ ~(msgState = "claimed" /\ owner = c)
101
+ /\ pcC' = [pcC EXCEPT ![c] = "idle"]
102
+ /\ UNCHANGED <<msgState, owner, fresh, alive, waiting, route, consumedBy, lostRecord>>
103
+
104
+ Expire ==
105
+ /\ msgState = "claimed"
106
+ /\ fresh
107
+ /\ fresh' = FALSE
108
+ /\ UNCHANGED <<msgState, owner, alive, pcC, route, waiting, consumedBy, lostRecord>>
109
+
110
+ Die(c) ==
111
+ /\ alive[c]
112
+ /\ alive' = [alive EXCEPT ![c] = FALSE]
113
+ /\ UNCHANGED <<msgState, owner, fresh, pcC, route, waiting, consumedBy, lostRecord>>
114
+
115
+ \* c's Receive suspends / is resumed by something else (timer, sweep,
116
+ \* another message). Not while a delivery holds c's instance lock.
117
+ ToggleWait(c) ==
118
+ /\ alive[c]
119
+ /\ pcC[c] = "idle"
120
+ /\ waiting' = [waiting EXCEPT ![c] = ~waiting[c]]
121
+ /\ UNCHANGED <<msgState, owner, fresh, alive, pcC, route, consumedBy, lostRecord>>
122
+
123
+ Next ==
124
+ \/ Expire
125
+ \/ \E c \in Consumers :
126
+ Claim(c, "push") \/ Claim(c, "drain") \/ Record(c) \/ Delete(c) \/ Abandon(c) \/ Die(c) \/ ToggleWait(c)
127
+
128
+ Spec == Init /\ [][Next]_vars
129
+
130
+ AtMostOneConsume == Cardinality(consumedBy) =< 1
131
+ NoLostMessage == ~lostRecord
132
+ ====
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = TRUE
5
+ OnlyWaiting = FALSE
6
+ SPECIFICATION Spec
7
+ INVARIANTS NoLostMessage
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = TRUE
5
+ OnlyWaiting = FALSE
6
+ SPECIFICATION Spec
7
+ INVARIANTS AtMostOneConsume
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = TRUE
5
+ OnlyWaiting = TRUE
6
+ SPECIFICATION Spec
7
+ INVARIANTS AtMostOneConsume NoLostMessage
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = FALSE
5
+ OnlyWaiting = FALSE
6
+ SPECIFICATION Spec
7
+ INVARIANTS NoLostMessage
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = FALSE
5
+ OnlyWaiting = FALSE
6
+ SPECIFICATION Spec
7
+ INVARIANTS AtMostOneConsume
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = FALSE
5
+ OnlyWaiting = TRUE
6
+ SPECIFICATION Spec
7
+ INVARIANTS NoLostMessage
@@ -0,0 +1,7 @@
1
+ CONSTANTS
2
+ Consumers = {c1, c2}
3
+ Takeover = TRUE
4
+ AtomicConsume = FALSE
5
+ OnlyWaiting = TRUE
6
+ SPECIFICATION Spec
7
+ INVARIANTS AtMostOneConsume
@@ -0,0 +1,10 @@
1
+ \* Current protocol: takeover on stale claim or terminal owner, plus
2
+ \* consume-delete on successful delivery.
3
+ \* Expected TLC result: no errors — exactly-once holds and an orphan
4
+ \* claim cannot permanently hide the message.
5
+ CONSTANTS
6
+ Consumers = {c1, c2}
7
+ Takeover = TRUE
8
+ SPECIFICATION Spec
9
+ INVARIANTS AtMostOneConsume
10
+ PROPERTIES ConsumedEventually
@@ -0,0 +1,11 @@
1
+ \* Pre-fix protocol: a claim can never be taken over. A consumer that
2
+ \* claims and dies leaves the message claimed forever — hidden from every
3
+ \* other consumer (orphan claim).
4
+ \* Expected TLC result: liveness violation (ConsumedEventually fails) and
5
+ \* a reachable state where nothing alive can claim.
6
+ CONSTANTS
7
+ Consumers = {c1, c2}
8
+ Takeover = FALSE
9
+ SPECIFICATION Spec
10
+ INVARIANTS AtMostOneConsume
11
+ PROPERTIES ConsumedEventually
@@ -0,0 +1,87 @@
1
+ ---- MODULE EpochFencing ----
2
+ EXTENDS Naturals, FiniteSets
3
+
4
+ (***************************************************************************)
5
+ (* Epoch fencing protocol. *)
6
+ (* *)
7
+ (* Every lock acquisition mints a NEW epoch (worker_id#epoch). A fenced *)
8
+ (* write succeeds only while the writer's held epoch equals the stored *)
9
+ (* lock epoch. The critical case: the SAME worker loses the lease and *)
10
+ (* reacquires — it receives a different epoch, so writes attempted with *)
11
+ (* the stale epoch must be rejected even though the worker identity is *)
12
+ (* unchanged. *)
13
+ (* *)
14
+ (* Configs: *)
15
+ (* old: unfenced writes (stale holders can write) -> NoStaleWrite violated*)
16
+ (* new: fenced writes -> NoStaleWrite holds *)
17
+ (* *)
18
+ (* LIMITATION: heldEpoch[w] holds ONE credential per worker, so a *)
19
+ (* re-acquire overwrites the old run's epoch and the critical case above *)
20
+ (* is not representable here: fencing on `holder = w` alone passes the *)
21
+ (* same 129 states. EpochFencingRuns.tla keeps one credential per run and *)
22
+ (* shows worker-id fencing failing and epoch fencing holding. *)
23
+ (***************************************************************************)
24
+
25
+ CONSTANTS Workers, \* set of worker ids
26
+ Fenced, \* TRUE: fenced writes; FALSE: unfenced (old)
27
+ MaxEpoch \* bound for finite checking (>= 2 * Cardinality(Workers))
28
+
29
+ VARIABLES
30
+ epoch, \* epoch counter (next epoch = epoch + 1)
31
+ holder, \* current lock holder worker id ("none" when unlocked)
32
+ heldEpoch, \* heldEpoch[w]: epoch worker w believes it holds (0 = none)
33
+ wrote, \* wrote[w]: w has committed a write (bounded flag)
34
+ staleWrite \* TRUE once a write succeeded under a stale epoch
35
+
36
+ vars == <<epoch, holder, heldEpoch, wrote, staleWrite>>
37
+
38
+ NoHolder == "none"
39
+
40
+ Init ==
41
+ /\ epoch = 0
42
+ /\ holder = NoHolder
43
+ /\ heldEpoch = [w \in Workers |-> 0]
44
+ /\ wrote = [w \in Workers |-> FALSE]
45
+ /\ staleWrite = FALSE
46
+
47
+ \* Acquire the lock: mints a fresh epoch. Always succeeds (models the
48
+ \* atomic conditional-UPDATE claim — the old holder's lease is gone).
49
+ Acquire(w) ==
50
+ /\ epoch < MaxEpoch
51
+ /\ epoch' = epoch + 1
52
+ /\ holder' = w
53
+ /\ heldEpoch' = [heldEpoch EXCEPT ![w] = epoch']
54
+ /\ UNCHANGED <<wrote, staleWrite>>
55
+
56
+ \* Lease expiry: the holder loses the lock but keeps its (now stale) epoch
57
+ \* credential — this is what makes the reacquire case interesting.
58
+ Expire ==
59
+ /\ holder # NoHolder
60
+ /\ holder' = NoHolder
61
+ /\ UNCHANGED <<epoch, heldEpoch, wrote, staleWrite>>
62
+
63
+ \* Fenced write attempt: succeeds iff the writer's epoch is current.
64
+ \* (Each worker writes at most once -- same bound as epoch_check_test.go.)
65
+ WWrite(w) ==
66
+ /\ heldEpoch[w] > 0
67
+ /\ ~wrote[w]
68
+ /\ IF Fenced
69
+ THEN /\ holder = w /\ heldEpoch[w] = epoch
70
+ /\ wrote' = [wrote EXCEPT ![w] = TRUE]
71
+ /\ UNCHANGED staleWrite
72
+ ELSE /\ wrote' = [wrote EXCEPT ![w] = TRUE]
73
+ /\ staleWrite' = (staleWrite \/ holder # w \/ heldEpoch[w] # epoch)
74
+ /\ UNCHANGED <<epoch, holder, heldEpoch>>
75
+
76
+ Next == \E w \in Workers : Acquire(w) \/ WWrite(w) \/ Expire
77
+
78
+ Spec == Init /\ [][Next]_vars /\ WF_vars(Next)
79
+
80
+ \* Core safety: no write ever commits under a stale epoch — including
81
+ \* writes by a worker that lost and reacquired the lock.
82
+ NoStaleWrite == ~staleWrite
83
+
84
+ \* Liveness: whoever currently holds the lock can write.
85
+ WriteProgress == [](holder # NoHolder => <>(\E w \in Workers : wrote[w]))
86
+
87
+ ====