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
//! Shippable WAL cursor: an additive, read-only API over the existing
//! append-only WAL framing.
//!
//! # Purpose
//!
//! Replication consumers (e.g. premium clustering / Raft) need to identify a
//! stable position in the write-ahead log and stream entries forward from it,
//! resuming after a restart without core silently discarding a position they
//! still need. This module defines the *seam* for that capability:
//!
//! - [`WalPosition`] — an opaque, comparable byte-offset into the log.
//! - [`WalRecord`] — one framed record plus the cursor immediately after it.
//! - [`WalConsumerId`] — an opaque replication-consumer identity.
//! - [`WalCursor`] — the read-only, forward-only trait consumers drive.
//!
//! # On-disk format
//!
//! This is an **additive read API** over the existing per-entry marker/CRC
//! framing in [`super::wal_entry`] and [`super::log_payload`]. It introduces
//! **no** on-disk format change: existing stored WALs remain readable, and no
//! new header or field is written. Consumer watermarks are in-memory
//! registration state, never a new on-disk artifact (Requirement 6.4).
//!
//! # Boundary
//!
//! Core exposes this trait; premium consumes it. Core never depends on premium
//! (Requirement 6.5).
use Mutex;
use FxHashMap;
use ;
/// A stable, monotonic position within the WAL.
///
/// Opaque byte-offset into the append-only log. It is `Copy` and totally
/// ordered so a consumer can persist it and resume reading from exactly where
/// it left off (Requirement 6.2). Positions only ever increase in append
/// order; the `Ord` impl reflects that append order directly.
;
/// One shippable WAL record.
///
/// Carries its own [`WalPosition`], the position immediately after it (the
/// consumer's `next` cursor), and the raw framed bytes exactly as stored on
/// disk — marker, id, and (for v2 records) length/payload/CRC. Consumers ship
/// `bytes` verbatim and resume from `next`.
/// Opaque replication-consumer identity.
///
/// Handed out by [`WalCursor::register_consumer`] and used to advance or drop
/// that consumer's retained low-watermark. Core assigns and interprets the
/// inner value; consumers treat it as opaque.
;
/// Read-only, forward-only cursor over the WAL for replication consumers.
///
/// Implemented in core over the existing WAL framing; consumed by premium's
/// replication layer. Core never depends on premium (Requirement 6.5).
///
/// The read methods ([`read_from`](WalCursor::read_from),
/// [`tail_position`](WalCursor::tail_position)) are the surface defined by this
/// task; the low-watermark retention methods form the retention contract wired
/// into compaction/recovery separately.
/// In-memory low-watermark registration state for replication consumers.
///
/// Backs the retention half of [`WalCursor`]: it hands out
/// [`WalConsumerId`]s, records each consumer's retained low-watermark (the
/// oldest [`WalPosition`] it still needs), and exposes the minimum across all
/// registered consumers so compaction/recovery truncation can decide what may
/// be reclaimed (Requirement 6.3).
///
/// # Retention semantics
///
/// - A freshly registered consumer starts at [`WalPosition::START`] — it holds
/// retention over the entire durable log until it explicitly advances. A
/// consumer that only wants new records advances to the current tail right
/// after registering.
/// - Watermarks only ever move forward; a stale or out-of-order
/// [`advance`](Self::advance) call is ignored so retention can never
/// silently shrink beneath a position a consumer already passed.
/// - [`min_watermark`](Self::min_watermark) returns `None` when no consumer is
/// registered, which callers treat as "no retention hold" — truncation then
/// behaves exactly as it did before this API existed.
///
/// This is purely in-memory registration state: it is never persisted and adds
/// no on-disk artifact (Requirement 6.4). Consumer registration therefore
/// resets across a restart, by design.
/// Decides whether the WAL may be fully reclaimed (truncated to empty) given
/// the registry's minimum retained watermark and the current append `tail`.
///
/// Full truncation reclaims every record below `tail`, so it is only safe when
/// no registered consumer still needs a position below `tail`:
///
/// - no consumer (`None`) → reclaim as today (Requirement 6.3 baseline);
/// - a consumer whose watermark already reached (or passed) `tail` → every
/// durable record is strictly below its watermark, so all are reclaimable;
/// - otherwise a record at or beyond the minimum watermark must survive, so
/// truncation is held (nothing is reclaimed — a subset of "records below the
/// watermark", which the contract permits).
pub