Files
seaweedfs/seaweed-block/docs/spec/pagechain/PageChain.tla
T

174 lines
9.9 KiB
Plaintext

------------------------------ MODULE PageChain ------------------------------
(***************************************************************************)
(* V8 page-chain crash rows (2026-12-01; #894's list pages). *)
(* CleanList kept a list inside HEAD; here it is a chain of immutable *)
(* content-named page objects that HEAD names by its head page. *)
(* *)
(* One list of two pages: "h" (entry e2) names "t" (entry e1). A holder *)
(* publishes it in attempts; an attempt's pages have the attempt's bytes, *)
(* so a resend of one attempt names the same objects. It writes the pages *)
(* (owned by its session), then sends a CAS conditional on its base *)
(* generation and on HEAD naming no list; a CAS may be delayed, and its *)
(* reply lost. A consumer reads the page at HEAD's cursor, applies its *)
(* entries (idempotent effects), moves the cursor by CAS (reply may be *)
(* lost), then retires the page. A fallback sweep removes pages HEAD does *)
(* not reach and no open session owns. A metadata writer steps HEAD's *)
(* generation. A holder crash is followed by its session's abort, a HEAD *)
(* write; a consumer crash loses its memory. *)
(* *)
(* Break = "none", or ONE of: *)
(* "head_before_durable" the CAS is sent before every page is written *)
(* "sweep_ignores_session" the sweep removes session-owned pages *)
(* "abandon_unsettled" an attempt is abandoned (its unreached pages *)
(* removed) while its CAS may still land *)
(* "retire_by_membership" an abandoned attempt's pages are removed *)
(* although HEAD reaches them *)
(* "cursor_before_effects" the cursor moves before the page's effects *)
(* "retire_before_cursor" a page is retired on a lost cursor reply *)
(* "absent_is_end" a failed page read ends the chain *)
(***************************************************************************)
EXTENDS Integers, FiniteSets
CONSTANTS MaxAtt, MaxSend, MaxMeta, MaxCrashH, MaxCrashC, Break
Pg == {"h", "t"}
Pages(k) == {<<k, "h">>, <<k, "t">>}
Ent(x) == IF x = "h" THEN {"e2"} ELSE {"e1"}
AllEnt == {"e1", "e2"}
VARIABLES head, store, owned, hp, att, base, nsend, net, reply,
cp, cobj, done, published, nmeta, nch, ncc, swept
vars == <<head, store, owned, hp, att, base, nsend, net, reply,
cp, cobj, done, published, nmeta, nch, ncc, swept>>
\* the pages HEAD reaches: from its cursor to the tail
Reach == IF head.list = 0 THEN {}
ELSE IF head.cur = "h" THEN Pages(head.list) ELSE {<<head.list, "t">>}
ReachEnt == UNION {Ent(o[2]) : o \in Reach}
Init == /\ head = [gen |-> 0, list |-> 0, cur |-> "h"]
/\ store = {} /\ owned = {} /\ hp = "idle" /\ att = 0 /\ base = 0 /\ nsend = 0
/\ net = {} /\ reply = "none" /\ cp = "idle" /\ cobj = <<0, "h">> /\ done = {}
/\ published = FALSE /\ nmeta = 0 /\ nch = 0 /\ ncc = 0 /\ swept = FALSE
CVars == <<cp, cobj, done, ncc>>
\* ---- the holder ----
Start == /\ hp = "idle" /\ att < MaxAtt /\ head.list = 0
/\ att' = att + 1 /\ base' = head.gen /\ nsend' = 0 /\ hp' = "write" /\ reply' = "none"
/\ UNCHANGED <<head, store, owned, net, published, nmeta, nch, swept>> /\ UNCHANGED CVars
WritePage(x) == /\ hp = "write"
/\ store' = store \cup {<<att, x>>} /\ owned' = owned \cup {<<att, x>>}
/\ UNCHANGED <<head, hp, att, base, nsend, net, reply, published, nmeta, nch, swept>> /\ UNCHANGED CVars
Send == /\ hp = "write" /\ nsend < MaxSend
/\ (Pages(att) \subseteq store \/ Break = "head_before_durable")
/\ nsend' = nsend + 1 /\ net' = net \cup {[k |-> att, b |-> base, s |-> nsend + 1]} /\ hp' = "sent"
/\ UNCHANGED <<head, store, owned, att, base, reply, published, nmeta, nch, swept>> /\ UNCHANGED CVars
\* a CAS is delivered at any time; its reply reaches the holder or is lost
Deliver(m) ==
/\ m \in net /\ net' = net \ {m}
/\ LET ok == head.gen = m.b /\ head.list = 0 IN
/\ head' = IF ok THEN [gen |-> head.gen + 1, list |-> m.k, cur |-> "h"] ELSE head
/\ published' = (published \/ ok)
/\ \E lost \in BOOLEAN :
reply' = IF ~lost /\ m.k = att /\ m.b = base /\ hp = "sent" /\ reply = "none"
THEN (IF ok THEN "ok" ELSE "fail") ELSE reply
/\ UNCHANGED <<store, owned, hp, att, base, nsend, nmeta, nch, swept>> /\ UNCHANGED CVars
Landed == /\ hp \in {"sent", "unk"} /\ (reply = "ok" \/ head.list = att)
/\ hp' = "done" /\ owned' = owned \ Pages(att)
/\ UNCHANGED <<head, store, att, base, nsend, net, reply, published, nmeta, nch, swept>> /\ UNCHANGED CVars
TimeOut == /\ hp = "sent" /\ reply = "none" /\ hp' = "unk"
/\ UNCHANGED <<head, store, owned, att, base, nsend, net, reply, published, nmeta, nch, swept>> /\ UNCHANGED CVars
\* HEAD still at the base, naming another list or none: the CAS may still land; send it again
Resend == /\ hp = "unk" /\ head.list # att /\ head.gen = base /\ nsend < MaxSend
/\ nsend' = nsend + 1 /\ net' = net \cup {[k |-> att, b |-> base, s |-> nsend + 1]} /\ hp' = "sent"
/\ UNCHANGED <<head, store, owned, att, base, reply, published, nmeta, nch, swept>> /\ UNCHANGED CVars
\* the attempt can never land (HEAD moved past its base), or a refusal came back:
\* remove its pages HEAD does not reach, release the session's hold
Abandon == /\ \/ hp \in {"sent", "unk"} /\ reply = "fail"
\/ hp = "unk" /\ head.list # att /\ (head.gen > base \/ Break = "abandon_unsettled")
/\ store' = IF Break = "retire_by_membership" THEN store \ Pages(att) ELSE store \ (Pages(att) \ Reach)
/\ owned' = owned \ Pages(att) /\ hp' = "idle" /\ reply' = "none"
/\ UNCHANGED <<head, att, base, nsend, net, published, nmeta, nch, swept>> /\ UNCHANGED CVars
\* a crash, then the session's abort: a HEAD write that ends the session's hold
CrashH == /\ nch < MaxCrashH /\ nch' = nch + 1
/\ hp' = "idle" /\ reply' = "none" /\ owned' = {} /\ head' = [head EXCEPT !.gen = @ + 1]
/\ UNCHANGED <<store, att, base, nsend, net, published, nmeta, swept>> /\ UNCHANGED CVars
HVars == <<hp, att, base, nsend, net, reply, nch>>
\* ---- the consumer ----
CRead == /\ cp = "idle" /\ head.list # 0
/\ LET o == <<head.list, head.cur>> IN
\E fail \in BOOLEAN :
IF o \in store /\ ~fail
THEN /\ cp' = "read" /\ cobj' = o /\ UNCHANGED head
ELSE IF Break = "absent_is_end"
THEN /\ head' = [gen |-> head.gen + 1, list |-> 0, cur |-> "h"] /\ UNCHANGED <<cp, cobj>>
ELSE /\ UNCHANGED <<head, cp, cobj>> \* stop; read again later
/\ UNCHANGED <<store, owned, done, published, nmeta, ncc, swept>> /\ UNCHANGED HVars
CApply == /\ cp \in {"read", "advread"} /\ done' = done \cup Ent(cobj[2])
/\ cp' = IF cp = "read" THEN "applied" ELSE "adv"
/\ UNCHANGED <<head, store, owned, cobj, published, nmeta, ncc, swept>> /\ UNCHANGED HVars
Advanced(h) == IF cobj[2] = "h" THEN [h EXCEPT !.gen = @ + 1, !.cur = "t"]
ELSE [gen |-> h.gen + 1, list |-> 0, cur |-> "h"]
CAdvance == /\ \/ cp = "applied"
\/ cp = "read" /\ Break = "cursor_before_effects"
/\ head.list = cobj[1] /\ head.cur = cobj[2]
/\ \E applied, lost \in BOOLEAN :
/\ applied \/ lost \* only this consumer moves the cursor: no refusal
/\ head' = IF applied THEN Advanced(head) ELSE head
/\ cp' = IF lost THEN "unk"
ELSE IF cp = "read" THEN "advread" ELSE "adv"
/\ UNCHANGED <<store, owned, cobj, done, published, nmeta, ncc, swept>> /\ UNCHANGED HVars
CResolve == /\ cp = "unk"
/\ cp' = IF head.list = cobj[1] /\ head.cur = cobj[2] THEN "applied" ELSE "adv"
/\ UNCHANGED <<head, store, owned, cobj, done, published, nmeta, ncc, swept>> /\ UNCHANGED HVars
\* the cursor passed the page: retire it
CRetire == /\ \/ cp = "adv"
\/ cp = "unk" /\ Break = "retire_before_cursor"
/\ store' = store \ {cobj} /\ cp' = "idle"
/\ UNCHANGED <<head, owned, cobj, done, published, nmeta, ncc, swept>> /\ UNCHANGED HVars
CrashC == /\ ncc < MaxCrashC /\ ncc' = ncc + 1 /\ cp' = "idle"
/\ UNCHANGED <<head, store, owned, cobj, done, published, nmeta, swept>> /\ UNCHANGED HVars
\* ---- the environment ----
Sweep(o) == /\ o \in store /\ o \notin Reach /\ (o \notin owned \/ Break = "sweep_ignores_session")
/\ store' = store \ {o} /\ swept' = TRUE
/\ UNCHANGED <<head, owned, published, nmeta>> /\ UNCHANGED HVars /\ UNCHANGED CVars
Meta == /\ nmeta < MaxMeta /\ nmeta' = nmeta + 1 /\ head' = [head EXCEPT !.gen = @ + 1]
/\ UNCHANGED <<store, owned, published, swept>> /\ UNCHANGED HVars /\ UNCHANGED CVars
Next == \/ Start \/ \E x \in Pg : WritePage(x) \/ Send \/ \E m \in net : Deliver(m)
\/ Landed \/ TimeOut \/ Resend \/ Abandon \/ CrashH
\/ CRead \/ CApply \/ CAdvance \/ CResolve \/ CRetire \/ CrashC
\/ \E o \in store : Sweep(o) \/ Meta
Spec == Init /\ [][Next]_vars
\* ---- rules ----
NoDangling == Reach \subseteq store
NoLostWork == published => (AllEnt \ done) \subseteq ReachEnt
\* ---- witnesses (each expected reachable: its negation is violated) ----
WitConsumed == ~(published /\ done = AllEnt /\ head.list = 0 /\ cp = "idle" /\ store \cap Pages(1) = {})
WitRefusedLanded == ~(att = 1 /\ hp = "idle" /\ head.list = 1 /\ nsend = 2) \* a resend refused, the first landed; pages kept
WitSecondAttempt == ~(att = 2 /\ published /\ head.list = 2)
WitOrphanSwept == ~(swept /\ published)
=============================================================================