1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! How long an inspection read may take before the command that started it ends.
//!
//! The read-only clients read stores the product may not be serving them from:
//! this process opens the store directly, or asks the instance over the debug
//! channel. Either read can fail to return, and one that never returns holds the
//! command — and the store file it opened — for as long as it runs. So the
//! command carries its own answer for how long it may wait.
//!
//! The limit is the COMMAND's, not one statement's or one wait's: it starts when
//! the command starts reading, and a spent budget fails the read it was spent on
//! and every read after it instead of being reset per statement, per wait or per
//! store. The smallest value is an immediate failure, never an unbounded read —
//! the engine's own "zero means no limit" applies to the engine's limit, not to
//! this one, which the engine never sees.
//!
//! Two mechanisms enforce it, and the second exists because the first cannot always run:
//! - [`Read::check`] is the cooperative half: a read asks the budget whether it
//! is spent before each step, before each statement of a multi-statement walk,
//! and once the read is over. A spent budget is the limit's failure wherever it
//! is asked — including at the end, so a read that ran past the limit is not an
//! answer even though it came back.
//! - [`Watchdog`] is the half that does not depend on the read yielding: it ends
//! the process past the limit, which is what covers a read that never comes back
//! to its own check. Its grace ([`WATCHDOG_GRACE_SECS`]) is what keeps the
//! reporting with a read that does come back inside it; one that comes back
//! later than the grace is cut by the process ending instead. The route that
//! makes the reads arms it ([`Read::arm`]) — once for the whole route, not per
//! attempt or per round trip.
//!
//! This limit is the client's own; the engine's per-statement guard
//! ([`crate::db::store::statement_limit`]) is separate and stays where it is. A
//! read limit raised ABOVE that guard therefore lets the guard answer first, and
//! its own wording is what the user sees — passed through unchanged, never
//! rewritten as this limit's failure. The default sits below the guard, so the
//! default behaviour is this limit's.
use ;
use ;
use anyhow;
/// Env override for the limit, in seconds — the same whole-seconds override the
/// product's other bounded waits use ([`crate::util::env_duration_secs`]), read
/// at each call so a shortened limit is a deterministic outcome for a check.
pub const ENV: &str = "MAHBOT_DEBUG_READ_LIMIT_SECS";
/// Default limit. Well past an ordinary read of the largest store the product
/// keeps: measured here on a purpose-built store of the product's own log schema,
/// built larger than the biggest real one — 814 MB and 1.5M rows against the real
/// `core.db`'s ~667 MB — a whole-schema dump takes ~1.4 s with the page cache warm
/// and ~1.7 s with it evicted, about a fortieth of the limit (and that is the
/// DEBUG build, the slower one). Well short of the per-statement limit the engine
/// enforces on its own connections ([`crate::db::store::statement_limit`]) so that
/// on a slow read THIS limit is the one that ends the command and its failure is
/// the one the user sees.
const DEFAULT_SECS: u64 = 60;
/// Upper bound on the override: a day is past any read worth waiting for, and it
/// keeps an absurd knob value — `Instant + Duration` panics on overflow — from
/// turning the budget's own deadline and cutoff into a panic.
const MAX_SECS: u64 = 86_400;
/// How far past the limit the watchdog waits before ending the process.
///
/// This is the cooperative half's own margin, and it is small: a read that yields
/// (each step, each round trip) notices the spent budget as soon as it returns, so
/// the watchdog is here for the read that never returns to its loop — and the
/// closer it fires to the limit, the closer the command ends to the limit it promised.
pub const WATCHDOG_GRACE_SECS: u64 = 1;
/// What the process the watchdog ends exits with: the command's error output and
/// its exit status are its only surface — it opens no log (an instance holds the
/// stores) and has nothing durable to write to — and `1` is the failure status its
/// other failures exit with.
pub const WATCHDOG_EXIT_CODE: i32 = 1;
/// The command's read limit.
/// What a read the limit ended says: the target the user asked about, the limit it
/// did not finish within, and what the read was left in. The wording holds for
/// every way the limit can end a read — one cut mid-flight, one that came back
/// past the limit, and one that was never attempted because the budget was already
/// spent. The instance route's own half says the instance is holding the store and
/// that its file was left untouched — which is what separates this failure from a
/// channel that is not answering at all, and from a store that was opened behind
/// the instance's back.
/// The command's read budget: the one clock every read of the command runs under.
pub
pub
/// Ends the process if the read it guards is still running at its budget's
/// cutoff — the half of the limit that does not depend on the read yielding.
///
/// Disarmed by dropping it, so it must be bound to a named variable that lives as
/// long as the read does: `let _watchdog = read.arm()?;`. Every route keeps it
/// for the reading alone and drops it before the answer is written, so a read
/// that came back is printed as its answer — the process is ended only while a
/// read is still running.
///
/// In a test binary the process-ending half is off ([`enable_process_exit`]):
/// ending the process there would take the whole test run down instead of the one
/// failing test, so the path is exercised only by the child of
/// [`crate::db::debug`]'s watchdog test, where it is a process of its own.
pub
/// Whether the watchdog may end this process — see [`Watchdog`]. Never set in a
/// test binary except by the child of the debug watchdog test.
static PROCESS_EXIT: AtomicBool = new;
/// Let this process end itself when the read limit is spent. Called only by the
/// child process of the debug watchdog test: "the process is gone" is observable
/// across two processes, and a watchdog firing inside the test binary would end the whole run.
pub