Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

SourceFailure.tla

183 lines8.7 KB

1---------------------------- MODULE SourceFailure ----------------------------
2(***************************************************************************)
3(* The source failure law: a source either keeps trying (loading), becomes *)
4(* ready, or reports a final error for its sync attempt. The framework *)
5(* never heals an attempt; the app recovers by starting a fresh attempt *)
6(* (cleanup, then preload). Each element names the frozen property (F1 to *)
7(* F12) that it encodes; README.md holds the trace table. *)
8(* *)
9(* One source serves a readiness wait (preload, stateWhenReady, *)
10(* toArrayWhenReady, queryOnce), a direct live query, a live query over a *)
11(* lazy join or include, and a skipInitial Effect. Consumers react in the *)
12(* same step as the source signal, as their status listeners do. *)
13(***************************************************************************)
14EXTENDS Naturals
15​
16CONSTANTS
17 MaxAttempts,
18 \* Weakenings: each encodes one plausible wrong design.
19 HealOnReady, \* the source may mark ready after its final error (old #1751 healing)
20 RejectOnRetry, \* a transient error fails consumers while the source retries
21 LazyTerminal, \* lazy demand fails for good while the root follows status
22 DetachWaits, \* waiting loads detach for recovery instead of rejecting
23 SilentGiveUp, \* the SDK gives up without the adapter reporting markError
24 StaleApplies, \* a callback from a cleaned attempt reaches the new one
25 PoisonSource, \* a request-specific failure marks the source failed
26 FrameworkRetry, \* the framework restarts a failed attempt by itself
27 ClearOnLateFail \* a failure after ready discards the visible snapshot
28​
29Consumers == {"root", "lazy"}
30​
31VARIABLES
32 attempt, \* current sync attempt number (F7)
33 status, \* "idle" | "loading" | "ready" | "error"
34 retrying, \* the source reported a transient error and is retrying (F1, F9)
35 alive, \* the source is still trying in this attempt (F10)
36 finalFailed, \* the source reported a final error in this attempt (F2)
37 rows, \* a usable snapshot is visible (F5, F6)
38 wait, \* readiness wait: "none" | "pending" | "resolved" | "rejected" | "aborted"
39 live, \* live query state per consumer: "loading" | "ready" | "error"
40 effect, \* "active" | "disposed"
41 reqFailed, \* the root query's own request failed on a healthy source (F12)
42 byApp \* the last attempt change came from the app (F3, F9)
43​
44vars == <<attempt, status, retrying, alive, finalFailed, rows, wait, live, effect,
45 reqFailed, byApp>>
46​
47Init ==
48 /\ attempt = 0 /\ status = "idle" /\ retrying = FALSE /\ alive = FALSE
49 /\ finalFailed = FALSE /\ rows = FALSE /\ wait = "none"
50 /\ live = [c \in Consumers |-> "loading"] /\ effect = "active"
51 /\ reqFailed = FALSE /\ byApp = TRUE
52​
53\* A fresh attempt: all demand is requested again and every wait is new (F3).
54FreshAttempt(app) ==
55 /\ attempt' = attempt + 1 /\ status' = "loading" /\ alive' = TRUE
56 /\ retrying' = FALSE /\ finalFailed' = FALSE /\ rows' = FALSE
57 /\ wait' = "pending" /\ live' = [c \in Consumers |-> "loading"]
58 /\ effect' = "active" /\ reqFailed' = FALSE /\ byApp' = app
59​
60Start == attempt = 0 /\ FreshAttempt(TRUE)
61​
62\* The source keeps trying: a transient error with a retry (F1).
63TransientError ==
64 /\ status = "loading" /\ alive /\ ~retrying
65 /\ retrying' = TRUE
66 /\ IF RejectOnRetry
67 THEN /\ wait' = IF wait = "pending" THEN "rejected" ELSE wait
68 /\ live' = [c \in Consumers |-> "error"] /\ effect' = "disposed"
69 ELSE UNCHANGED <<wait, live, effect>>
70 /\ UNCHANGED <<attempt, status, alive, finalFailed, rows, reqFailed, byApp>>
71​
72RetrySucceeds ==
73 /\ retrying /\ alive
74 /\ retrying' = FALSE
75 /\ UNCHANGED <<attempt, status, alive, finalFailed, rows, wait, live, effect, reqFailed, byApp>>
76​
77\* The source establishes a usable snapshot (F5).
78SourceReady ==
79 /\ alive /\ ~retrying
80 /\ (status = "loading") \/ (HealOnReady /\ status = "error")
81 /\ status' = "ready" /\ rows' = TRUE
82 /\ wait' = IF wait = "pending" THEN "resolved" ELSE wait
83 /\ live' = [c \in Consumers |->
84 IF (c = "root" /\ reqFailed) \/ (LazyTerminal /\ live[c] = "error" /\ c = "lazy")
85 THEN live[c] ELSE "ready"]
86 /\ UNCHANGED <<attempt, retrying, alive, finalFailed, effect, reqFailed, byApp>>
87​
88\* A final error reaches every consumer with its cause (F2, F4).
89FinalError ==
90 /\ alive /\ status \in {"loading", "ready"}
91 /\ status' = "error" /\ finalFailed' = TRUE /\ retrying' = FALSE
92 /\ alive' = HealOnReady
93 /\ wait' = IF wait = "pending" /\ ~DetachWaits THEN "rejected" ELSE wait
94 /\ live' = [c \in Consumers |-> "error"] /\ effect' = "disposed"
95 /\ UNCHANGED <<attempt, rows, reqFailed, byApp>>
96​
97\* The SDK gives up inside the source; the adapter must report it (F10).
98SdkGivesUp ==
99 /\ alive /\ status = "loading"
100 /\ IF SilentGiveUp
101 THEN /\ alive' = FALSE /\ retrying' = FALSE
102 /\ UNCHANGED <<status, finalFailed, wait, live, effect>>
103 ELSE /\ status' = "error" /\ finalFailed' = TRUE /\ alive' = FALSE
104 /\ retrying' = FALSE
105 /\ wait' = IF wait = "pending" /\ ~DetachWaits THEN "rejected" ELSE wait
106 /\ live' = [c \in Consumers |-> "error"] /\ effect' = "disposed"
107 /\ UNCHANGED <<attempt, rows, reqFailed, byApp>>
108​
109\* A later refresh failure after ready keeps the snapshot (F6).
110LateFailure ==
111 /\ status = "ready" /\ alive
112 /\ rows' = ~ClearOnLateFail
113 /\ UNCHANGED <<attempt, status, retrying, alive, finalFailed, wait, live, effect, reqFailed, byApp>>
114​
115\* One request fails while its source is healthy (F12).
116RequestFails ==
117 /\ status \in {"loading", "ready"} /\ alive /\ ~reqFailed
118 /\ reqFailed' = TRUE
119 /\ IF PoisonSource
120 THEN /\ status' = "error" /\ alive' = FALSE
121 /\ wait' = IF wait = "pending" THEN "rejected" ELSE wait
122 /\ effect' = "disposed" /\ live' = [c \in Consumers |-> "error"]
123 ELSE /\ live' = [live EXCEPT !["root"] = "error"]
124 /\ UNCHANGED <<status, alive, wait, effect>>
125 /\ UNCHANGED <<attempt, retrying, finalFailed, rows, byApp>>
126​
127\* A callback from the cleaned previous attempt arrives (F7).
128StaleCallback ==
129 /\ attempt > 1 /\ status = "loading"
130 /\ IF StaleApplies
131 THEN /\ status' = "error" /\ live' = [c \in Consumers |-> "error"]
132 /\ wait' = IF wait = "pending" THEN "rejected" ELSE wait
133 /\ effect' = "disposed"
134 ELSE UNCHANGED <<status, live, wait, effect>>
135 /\ UNCHANGED <<attempt, retrying, alive, finalFailed, rows, reqFailed, byApp>>
136​
137\* The app recovers: cleanup ends the old waits quietly, then a fresh attempt (F3).
138AppRestart ==
139 /\ attempt > 0 /\ attempt < MaxAttempts
140 /\ FreshAttempt(TRUE)
141​
142\* Weakening: the framework restarts a failed attempt by itself (F9).
143AutoRestart ==
144 /\ FrameworkRetry /\ status = "error" /\ attempt < MaxAttempts
145 /\ FreshAttempt(FALSE)
146​
147Next ==
148 \/ Start \/ TransientError \/ RetrySucceeds \/ SourceReady \/ FinalError
149 \/ SdkGivesUp \/ LateFailure \/ RequestFails \/ StaleCallback
150 \/ AppRestart \/ AutoRestart
151​
152Spec == Init /\ [][Next]_vars
153​
154---------------------------------------------------------------------------
155TypeOK ==
156 /\ status \in {"idle", "loading", "ready", "error"}
157 /\ wait \in {"none", "pending", "resolved", "rejected", "aborted"}
158 /\ live \in [Consumers -> {"loading", "ready", "error"}]
159 /\ effect \in {"active", "disposed"}
160​
161\* F1: while the source retries, nothing reports failure.
162RetryIsNotFailure ==
163 retrying => /\ status = "loading" /\ wait # "rejected" /\ effect = "active"
164 /\ live["lazy"] # "error"
165\* F2: a final error is final for its attempt.
166NoHeal == finalFailed => status = "error"
167\* F4 and F8: a final error reaches every consumer, and no wait is left pending.
168PassThrough ==
169 status = "error" => /\ wait # "pending" /\ effect = "disposed"
170 /\ \A c \in Consumers : live[c] = "error"
171\* F5 and F6: ready means a usable snapshot is visible.
172ReadyMeansRows == status = "ready" => rows
173\* F10: a source that stopped trying has reported ready or a final error.
174StoppedSourceReported == (status = "loading" /\ attempt > 0) => alive
175\* F7: an error in the current attempt is that attempt's own final failure.
176ErrorIsOwn == status = "error" => finalFailed
177\* F11: consumers agree when no request-specific failure happened.
178ConsumersAgree == ~reqFailed => live["root"] = live["lazy"]
179\* F12: a request-specific failure leaves a healthy source healthy.
180RequestFailureIsLocal == (reqFailed /\ ~finalFailed) => status # "error"
181\* F3 and F9: only the app starts a new attempt.
182OnlyAppRestarts == byApp
183=============================================================================