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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Always-on READ-PATH ARM probes (issue #3058).
//!
//! The Flight `do_get` row route can be served by two structurally different
//! arms: the k-way compaction merge (`KWayMerger` + the compaction reconciler)
//! and — when exactly ONE post-prune source is involved — the single-generation
//! query scan. "Which arm ran" is not observable from the RESULT (both arms are
//! required to return the same rows), so a test that wants to pin the routing
//! must observe an explicit MARKER. These counters are that marker.
//!
//! # Why an explicit marker, not a timing/throughput inference
//!
//! Spec `flight-single-sstable-bypass` (issue AC #1) requires the path-taken
//! assertion to FAIL when the merge path is entered, and forbids inferring the
//! arm from elapsed time, throughput, or CPU share (a timing assertion is both
//! host-dependent and, per issue #2877, capable of passing while the work it
//! claims to have removed is still being done). A counter incremented at the
//! merge's own construction / reconcile / cell-metadata-allocation sites is a
//! direct observation of the work, so `== 0` is a proof, not a correlation.
//!
//! # Always-on, like `SCAN_FOR_KEY_CALLS`
//!
//! These are plain `Relaxed` adds on the MERGE arm only (never on the scan
//! arm), following the always-on `SCAN_FOR_KEY_CALLS` precedent
//! (`data_access/model.rs`) rather than the `cfg`-gated `read_work_counters`
//! pattern: the consumers are integration tests in a DIFFERENT crate
//! (`cqlite-flight/tests/`), which cannot see `cfg(test)` counters and must not
//! be forced to rebuild `cqlite-core` under a non-default feature. The merge arm
//! costs microseconds per row, so one uncontended relaxed increment per merged
//! row is unmeasurable there; the fast arm pays literally nothing because it
//! never reaches these sites.
//!
//! # Reading them
//!
//! They are PROCESS-GLOBAL. A test observes a DELTA around the operation under
//! test ([`ReadPathProbe::snapshot`] → run → [`ReadPathProbe::delta_since`]) and
//! must serialize against any sibling test in the same binary that also merges
//! (one file = one process is the strongest form; a mutex is the minimum).
use ;
static MERGERS_BUILT: AtomicU64 = new;
static RECONCILE_ENTRIES: AtomicU64 = new;
static CELL_METADATA_MAPS: AtomicU64 = new;
/// Record one k-way merger construction over an already-open reader set
/// (`KWayMerger::new_from_readers` — the Flight warm row route's merge arm).
/// Record one entry into the compaction reconciler
/// (`KWayMerger::reconcile_cluster_with_overlap_counted`).
/// Record one per-row `HashMap<String, CellWriteMetadata>` allocation in the row
/// decoder (i.e. one row decoded with `want_cell_metadata == true`).
/// A point-in-time reading of the three arm counters.
// ---------------------------------------------------------------------------
// Query-row producer completion (issue #3384)
// ---------------------------------------------------------------------------
/// Producers that have finished decoding, published with RELEASE ordering.
///
/// Deliberately NOT a field of [`ReadPathProbe`]: every field there is loaded
/// `Relaxed`, which is right for counters read after the work is known to be over,
/// and wrong for this one — its whole job is to TELL you the work is over.
static QUERY_ROW_PRODUCERS_FINISHED: AtomicU64 = new;
/// Publish that one `QueryRowStream` producer has finished decoding.
///
/// Call BEFORE the terminal message is sent, never after (roborev, issue #3384):
/// a consumer holding the terminal message must be able to conclude that this
/// producer can no longer publish, or a producer from a PRIOR test case can
/// increment into a LATER case's freshly-reset counter and the later case will
/// observe a completion that was never its own.
/// `QueryRowStream` producers that have finished decoding since the last
/// [`reset_query_row_producers_finished`] (issue #3384).
///
/// The CAUSAL completion signal for an abandoned walk. A test that instead waits
/// for a work counter to "stop growing" is sampling another thread's progress: a
/// producer merely descheduled, or paused between two increments, is
/// indistinguishable from one that has stopped, so the walk can resume and drain
/// the table right after the assertion passed.
///
/// # What it proves, and what it does NOT
///
/// It proves the OUTER query-row producer stopped. It does NOT prove the INNER
/// batched scan task has (roborev, issue #3384): `drive_full_scan_rows` drops
/// `BatchedScanStream` on its way out and that task is never joined. That trail is
/// BOUNDED — the inner loop consults its consumer only when a batch fills, so after
/// the receiver is dropped it decodes at most one more emit batch before its `send`
/// fails and it returns — so it cannot run away, but a reader wanting a FINAL work
/// count should let the trail land rather than read the instant this signal moves.
/// Joining the inner task on shutdown is tracked on #3428.
///
/// The `Acquire` load is load-bearing, not decoration. With `Relaxed` on both
/// sides, observing a non-zero count would establish NO happens-before edge with
/// the producer's earlier work-counter increments, so a reader could see
/// "finished" and then read a STALE row count — precisely the guarantee this
/// signal exists to give (roborev, issue #3384).
/// Blocking scan tasks currently running (issue #3384). A GAUGE, not a counter.
static BLOCKING_SCAN_TASKS_INFLIGHT: AtomicI64 = new;
/// RAII marker for one blocking scan task: increments on construction, decrements on
/// drop (issue #3384).
///
/// The stitching path's parse/feed halves run under `spawn_blocking`, and blocking
/// tasks are NOT cancellable — dropping their `JoinHandle` DETACHES them. So neither
/// the outer query-row producer's completion nor the scan future's drop guard proves
/// decoding has stopped: both can fire while a detached blocking half is still
/// draining. This gauge is the THIRD and last of the completion signals — the other two
/// are `query_row_producers_finished` (outer thread) and the scan future's own drop
/// guard, and neither covers detached blocking work. A gauge rather than a completion counter
/// because the number of blocking halves is an implementation detail — a reader waits
/// for it to reach ZERO instead of knowing how many to expect.
///
/// Decrement is a `Drop` impl so an unwinding task still clears its slot; a leaked
/// increment would hang every future reader.
/// The private field is load-bearing (roborev, issue #3384): as a UNIT struct this
/// was constructible by name, so a caller could make one WITHOUT the increment and
/// then decrement the gauge below zero on drop — silently breaking every completion
/// check that reads it. `pub(crate)` for the same reason, narrowed further: nothing
/// outside this crate needs to register a blocking task, and the observers
/// ([`blocking_scan_tasks_inflight`]) stay public.
pub );
/// Blocking scan tasks still running (issue #3384). ZERO means every detached
/// blocking half has stopped, so `stream_walk_partitions_parsed` is final.
/// Times a batched scan took the STITCHING branch (issue #3384).
static BATCHED_SCAN_STITCHING_PATHS: AtomicU64 = new;
/// Record that a batched scan routed to `run_scan_stream_windowed` — the
/// STITCHING branch (issue #3384).
///
/// Exists so a test can AFFIRMATIVELY measure which branch its fixture took, rather
/// than infer it. The completion signal below covers only the non-stitching loop, and
/// the obvious proxies for "am I on that loop" are all wrong: absence of
/// `CompressionInfo.db` does not imply it (roborev), and neither does the fixture being
/// uncompressed in the colloquial sense — `requires_chunk_stitching()` is
/// `data_format() == V5CompressedLegacy && is_nb_format()`, and `V5_0Uncompressed` maps
/// to the FORMER but not the LATTER. Two proxies, two different wrong answers; the
/// branch itself is the only thing worth asserting on.
/// Batched scans that took the stitching branch since
/// [`reset_batched_scan_stitching_paths`] (issue #3384).
/// Zero the stitching-branch count (issue #3384).
/// Detached `BatchedScanStream` tasks that have terminated, published with
/// RELEASE ordering (issue #3384).
static BATCHED_SCANS_FINISHED: AtomicU64 = new;
/// Publish that one detached batched-scan task has terminated.
///
/// The SECOND of three abandoned-walk completion signals. `mark_query_row_producer_finished`
/// proves the outer query-row thread stopped pulling; this proves the detached task it
/// dropped without joining has stopped DECODING. Only all three together make
/// `stream_walk_partitions_parsed` final — waiting a fixed interval instead merely
/// makes the race less likely, which is the defect this issue exists to remove.
///
/// # Scope, and it is narrower than "the scan has finished" (roborev, issue #3384)
///
/// This is published from a DROP guard on the spawned future, so it covers everything
/// that decodes INSIDE that future — which is the whole of the block-by-block loop
/// taken by non-stitching (uncompressed) readers.
///
/// It does NOT cover the STITCHING path. `requires_chunk_stitching()` (compressed `nb`)
/// routes to `run_scan_stream_windowed`, which dispatches `spawn_blocking` parse/feed
/// tasks; blocking tasks are not cancellable, so they can still be decoding and
/// advancing `stream_walk_partitions_parsed` when this signal fires. On that path the
/// signal is PREMATURE and must not be read as finality. Joining those tasks on
/// cancellation is the real fix and is tracked on #3428.
/// Detached batched-scan tasks that have terminated since
/// [`reset_batched_scans_finished`] (issue #3384). `Acquire`, for the same
/// happens-before reason as [`query_row_producers_finished`].
/// Zero the detached batched-scan completion count (issue #3384).
/// Zero the completion count. A test that will wait on
/// [`query_row_producers_finished`] must call this first, while holding whatever
/// lock serializes producers in its binary.