Skip to main content

uni_store/runtime/
l0_manager.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2026 Dragonscale Team
3
4use crate::runtime::l0::L0Buffer;
5use crate::runtime::wal::WriteAheadLog;
6use parking_lot::RwLock;
7use std::sync::Arc;
8
9/// Per-generation pin marker for snapshot isolation (Component C1).
10///
11/// Held by exactly two classes: the [`L0Manager`] keeps one clone for the
12/// current generation, and every live [`SnapshotView`] holds one. So
13/// `Arc::strong_count` on the manager's clone is `1 + (live snapshots of the
14/// current generation)`, which [`L0Manager::is_current_pinned`] uses to decide
15/// whether a commit must freeze the generation aside before mutating it. The
16/// private field stops any other code from minting a token and breaking that
17/// invariant.
18///
19/// Always compiled (so the inert threading types exist in every build); it is
20/// only ever *minted* by [`L0Manager::pin_snapshot`], which a transaction calls
21/// only when `UniConfig::ssi_enabled` is `true`.
22#[derive(Debug)]
23pub struct PinToken(());
24
25/// An isolated, reference-counted view of the L0 tier captured at a point in time.
26///
27/// Reads built from a `SnapshotView` see the L0 generation(s) that were visible
28/// at capture, not later commits: while any view of a generation is alive a
29/// commit that would mutate it first freezes it aside
30/// ([`L0Manager::freeze_current_for_snapshot`]), so the buffers behind `main`
31/// and `extra` are never mutated after capture. Dropping the view releases its
32/// pin; `Arc` reference counting reclaims a frozen generation once no view holds
33/// it. `started_at_version` is captured for the future C2 base-pinning hook and
34/// is not yet consulted.
35///
36/// Always compiled so it can thread through the executor as an inert
37/// `Option<SnapshotView>` in every build; it is only ever *constructed* by
38/// [`L0Manager::pin_snapshot`], which a transaction calls only when
39/// `UniConfig::ssi_enabled` is `true`, so with SSI off the threaded option is
40/// always `None`.
41#[derive(Clone)]
42pub struct SnapshotView {
43    /// The pinned main L0 generation at capture time.
44    pub main: Arc<RwLock<L0Buffer>>,
45    /// Generations being flushed at capture time, read after `main` (oldest visible state).
46    pub extra: Vec<Arc<RwLock<L0Buffer>>>,
47    /// Pin marker keeping the captured generation freeze-on-commit.
48    pin: Arc<PinToken>,
49    /// Main-L0 version at capture (the C2 hwm fed into `pinned_storage`).
50    pub started_at_version: u64,
51    /// C2: a `StorageManager` clone pinned to `started_at_version`
52    /// (`StorageManager::pinned_at_version`), so L1 scans filter to
53    /// `_version <= started_at_version` and an L0→L1 flush completing
54    /// mid-transaction cannot leak post-snapshot rows. Installed by the
55    /// transaction at begin (one per transaction — the pinned manager
56    /// carries a fresh `AdjacencyManager`); `None` for snapshots taken
57    /// without a storage pin.
58    pub pinned_storage: Option<Arc<crate::storage::manager::StorageManager>>,
59}
60
61impl std::fmt::Debug for SnapshotView {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        // Avoid requiring `L0Buffer: Debug` and dumping buffer contents.
64        f.debug_struct("SnapshotView")
65            .field("extra_generations", &self.extra.len())
66            .field("pins", &Arc::strong_count(&self.pin))
67            .field("started_at_version", &self.started_at_version)
68            .finish_non_exhaustive()
69    }
70}
71
72pub struct L0Manager {
73    // The current active L0 buffer.
74    // Outer RwLock protects the Arc (swapping L0s).
75    // Inner RwLock protects the L0Buffer content (concurrent reads/writes).
76    current: RwLock<Arc<RwLock<L0Buffer>>>,
77    // L0 buffers currently being flushed to L1.
78    // These remain visible to reads until flush completes successfully.
79    // This prevents data loss if L1 writes fail after rotation.
80    pending_flush: RwLock<Vec<Arc<RwLock<L0Buffer>>>>,
81    // Snapshot-isolation pin token for the current generation (Component C1).
82    // Reset on every rotate so a fresh generation starts unpinned. Read/cloned
83    // only under the `current` lock so a snapshot captures a buffer and token
84    // from the same generation. See `PinToken`.
85    current_pin: RwLock<Arc<PinToken>>,
86}
87
88impl L0Manager {
89    pub fn new(start_version: u64, wal: Option<Arc<WriteAheadLog>>) -> Self {
90        let l0 = L0Buffer::new(start_version, wal);
91        Self {
92            current: RwLock::new(Arc::new(RwLock::new(l0))),
93            pending_flush: RwLock::new(Vec::new()),
94            current_pin: RwLock::new(Arc::new(PinToken(()))),
95        }
96    }
97
98    /// Create a read-only snapshot L0Manager from existing buffers.
99    ///
100    /// Used by the algorithm execution path to provide L0 visibility
101    /// without owning the actual L0 lifecycle (rotation, flush, WAL).
102    pub fn from_snapshot(
103        current: Arc<RwLock<L0Buffer>>,
104        pending_flush: Vec<Arc<RwLock<L0Buffer>>>,
105    ) -> Self {
106        Self {
107            current: RwLock::new(current),
108            pending_flush: RwLock::new(pending_flush),
109            current_pin: RwLock::new(Arc::new(PinToken(()))),
110        }
111    }
112
113    /// Get the current L0 buffer.
114    pub fn get_current(&self) -> Arc<RwLock<L0Buffer>> {
115        self.current.read().clone()
116    }
117
118    /// Get all L0 buffers that should be visible to reads.
119    /// This includes the current L0 plus any L0s being flushed.
120    pub fn get_all_readable(&self) -> Vec<Arc<RwLock<L0Buffer>>> {
121        let current = self.get_current();
122        let pending = self.pending_flush.read().clone();
123        let mut all = vec![current];
124        all.extend(pending);
125        all
126    }
127
128    /// Get L0 buffers currently being flushed (for QueryContext).
129    pub fn get_pending_flush(&self) -> Vec<Arc<RwLock<L0Buffer>>> {
130        self.pending_flush.read().clone()
131    }
132
133    /// Rotate L0. Returns the OLD L0 buffer.
134    /// The new L0 is initialized with `next_version` and `new_wal`.
135    pub fn rotate(
136        &self,
137        next_version: u64,
138        new_wal: Option<Arc<WriteAheadLog>>,
139    ) -> Arc<RwLock<L0Buffer>> {
140        let mut guard = self.current.write();
141        let old_l0 = guard.clone();
142
143        let new_l0 = L0Buffer::new(next_version, new_wal);
144        *guard = Arc::new(RwLock::new(new_l0));
145
146        // A fresh generation starts unpinned. Reset the pin token while still
147        // holding the `current` write guard: `pin_snapshot` clones the buffer
148        // and token under `current.read()`, so this serializes against it and a
149        // snapshot can never capture a buffer/token from different generations.
150        *self.current_pin.write() = Arc::new(PinToken(()));
151
152        old_l0
153    }
154
155    /// Begin flush: rotate L0 and add old L0 to pending flush list.
156    /// The old L0 remains visible to reads until `complete_flush` is called.
157    /// Returns the old L0 buffer to be flushed.
158    pub fn begin_flush(
159        &self,
160        next_version: u64,
161        new_wal: Option<Arc<WriteAheadLog>>,
162    ) -> Arc<RwLock<L0Buffer>> {
163        let old_l0 = self.rotate(next_version, new_wal);
164        self.pending_flush.write().push(old_l0.clone());
165        old_l0
166    }
167
168    /// Complete flush: remove the flushed L0 from pending list.
169    /// Call this only after L1 writes have succeeded.
170    pub fn complete_flush(&self, l0: &Arc<RwLock<L0Buffer>>) {
171        let mut pending = self.pending_flush.write();
172        pending.retain(|x| !Arc::ptr_eq(x, l0));
173    }
174
175    /// Captures an isolated snapshot of the current L0 (strategy D).
176    ///
177    /// Freezes the current buffer by rotating it aside — writers re-fetch
178    /// `get_current()` at write time, so they move to the fresh buffer and can
179    /// never mutate the frozen one — and keeps it readable via the pending
180    /// list. Returns the `(frozen_main, pending)` pair used to build a
181    /// [`QueryContext`] whose reads are isolated from later writes. Capture is
182    /// O(1): one empty-buffer allocation and an `Arc` move, with no deep copy.
183    ///
184    /// The caller must coordinate with the commit path (e.g. hold the writer's
185    /// `flush_lock`) so the rotation does not race an in-flight merge into the
186    /// current buffer. The frozen generation currently rides the pending-flush
187    /// list; a dedicated generation list with reader-count GC is the production
188    /// follow-up (see the proposal's open questions).
189    ///
190    /// [`QueryContext`]: crate::runtime::QueryContext
191    pub fn snapshot_isolated(
192        &self,
193        next_version: u64,
194        new_wal: Option<Arc<WriteAheadLog>>,
195    ) -> (Arc<RwLock<L0Buffer>>, Vec<Arc<RwLock<L0Buffer>>>) {
196        // Capture pending before freezing so the frozen buffer becomes the
197        // snapshot's main view rather than one of its pending peers.
198        let pending = self.pending_flush.read().clone();
199        let frozen = self.rotate(next_version, new_wal);
200        // Keep the frozen generation visible to latest (non-snapshot) reads.
201        self.pending_flush.write().push(frozen.clone());
202        (frozen, pending)
203    }
204
205    /// Pins an isolated view of the current L0 tier for a transaction.
206    ///
207    /// O(1): clones the current buffer handle, the pending-flush set, and the
208    /// generation's pin token. No freeze happens here — the current buffer keeps
209    /// taking writes; it is frozen aside lazily, and only if still pinned, when a
210    /// commit would next mutate it (see [`Self::freeze_current_for_snapshot`] and
211    /// [`Self::is_current_pinned`]). Holds the `current` read lock across the
212    /// buffer and token clones so both come from the same generation even if a
213    /// rotate races. Does not require the writer's `flush_lock`.
214    ///
215    /// # Examples
216    /// ```ignore
217    /// let snap = writer.l0_manager().pin_snapshot();
218    /// // build a QueryContext from `snap.main` + `snap.extra`
219    /// ```
220    pub fn pin_snapshot(&self) -> SnapshotView {
221        // Hold `current` read across both clones: a concurrent `rotate` needs
222        // `current.write()` and resets the pin token under it, so it cannot
223        // interleave and split the buffer/token across generations.
224        let current_guard = self.current.read();
225        let main = current_guard.clone();
226        let pin = self.current_pin.read().clone();
227        let started_at_version = main.read().current_version;
228        let extra = self.pending_flush.read().clone();
229        drop(current_guard);
230        SnapshotView {
231            main,
232            extra,
233            pin,
234            started_at_version,
235            pinned_storage: None,
236        }
237    }
238
239    /// Returns `true` if any live [`SnapshotView`] pins the current generation.
240    ///
241    /// `strong_count > 1` means a snapshot besides the manager holds the token.
242    /// Call under the writer's `flush_lock` at commit so the decision and any
243    /// resulting freeze are atomic with respect to the merge.
244    pub fn is_current_pinned(&self) -> bool {
245        Arc::strong_count(&self.current_pin.read()) > 1
246    }
247
248    /// Clones the current (pinned) generation aside so a commit can mutate a
249    /// fresh buffer without the pinning snapshots observing the write — lazy
250    /// copy-on-write, performed only when [`Self::is_current_pinned`] holds.
251    ///
252    /// The outgoing buffer — which the pinning [`SnapshotView`]s hold via `main`
253    /// — becomes immutable: a deep copy carrying the same data is installed as
254    /// the new current, the commit merges into that copy, and the original is
255    /// never mutated again. `L0Buffer::clone` drops the WAL handle, so the
256    /// original's WAL (already flushed at this commit's WAL step) is handed to
257    /// the copy; the frozen original keeps none, as it takes no more writes. The
258    /// original is **not** placed on the pending-flush list — it is reclaimed by
259    /// `Arc` refcount once the last snapshot drops, so nothing leaks. The new
260    /// generation starts unpinned (the pin token is reset). Must be called under
261    /// the writer's `flush_lock`, since it swaps the current buffer.
262    pub fn freeze_current_for_snapshot(&self) {
263        let mut guard = self.current.write();
264        let frozen = guard.clone();
265        let mut new_buf = frozen.read().clone();
266        // Hand the WAL from the now-frozen original to the writable copy.
267        new_buf.wal = frozen.write().wal.take();
268        *guard = Arc::new(RwLock::new(new_buf));
269        // The fresh generation starts unpinned; reset under the `current` write
270        // guard (consistent with `rotate`, which a non-clone path would use).
271        *self.current_pin.write() = Arc::new(PinToken(()));
272    }
273
274    /// Minimum `wal_lsn_at_start` among pending-flush L0s other than `except`.
275    ///
276    /// This is the floor below which every WAL entry is durable in L1: a pending
277    /// flush — one still streaming, or one whose flush FAILED and left the buffer
278    /// in `pending_flush` — holds committed WAL entries strictly above its start
279    /// that are not yet in L1. WAL truncation and the published
280    /// `wal_high_water_mark` must not advance past this floor, or that buffer's
281    /// committed-but-unflushed data is silently dropped by the next (e.g.
282    /// shutdown) flush. Using the high watermark (`wal_lsn_at_flush`) here was the
283    /// lost-commit bug: it truncated / checkpointed past the pending buffer's own
284    /// entries.
285    ///
286    /// `except` is the buffer the caller is itself flushing — its data IS entering
287    /// the new snapshot, so it must not constrain the floor. At truncation time it
288    /// has already been removed via `complete_flush`, so passing it is a harmless
289    /// no-op; during the stream phase it is still pending and the exclusion is
290    /// load-bearing.
291    ///
292    /// Returns `None` when no other pending flush exists.
293    pub fn min_pending_wal_lsn_start(&self, except: &Arc<RwLock<L0Buffer>>) -> Option<u64> {
294        self.pending_flush
295            .read()
296            .iter()
297            .filter(|l0_arc| !Arc::ptr_eq(l0_arc, except))
298            .map(|l0_arc| l0_arc.read().wal_lsn_at_start)
299            .min()
300    }
301}
302
303#[cfg(test)]
304mod snapshot_tests {
305    use super::*;
306    use crate::runtime::QueryContext;
307    use crate::runtime::l0_visibility::lookup_vertex_prop;
308    use uni_common::core::id::Vid;
309    use uni_common::{Properties, Value};
310
311    fn named(name: &str) -> Properties {
312        let mut props = Properties::new();
313        props.insert("name".to_string(), Value::String(name.to_string()));
314        props
315    }
316
317    fn name_of(vid: Vid, ctx: &QueryContext) -> Option<String> {
318        match lookup_vertex_prop(vid, "name", Some(ctx)) {
319            Some(Value::String(s)) => Some(s),
320            _ => None,
321        }
322    }
323
324    /// A strategy-D snapshot must not observe writes that land after capture,
325    /// while a fresh latest view must, and frozen data must stay visible.
326    #[test]
327    fn snapshot_isolated_from_later_writes() {
328        let mgr = L0Manager::new(0, None);
329        let alice = Vid::from(1_u64);
330        let bob = Vid::from(2_u64);
331        let labels = ["Node".to_string()];
332
333        // Pre-snapshot state.
334        {
335            let current = mgr.get_current();
336            let mut guard = current.write();
337            guard.insert_vertex_with_labels(alice, named("alice"), &labels);
338            guard.insert_vertex_with_labels(bob, named("bob"), &labels);
339        }
340
341        // Freeze-rotate snapshot.
342        let (frozen, pending) = mgr.snapshot_isolated(1, None);
343        let snap = QueryContext::new_with_pending(frozen, None, pending);
344
345        // Post-snapshot write into the fresh current buffer.
346        mgr.get_current()
347            .write()
348            .insert_vertex_with_labels(alice, named("alice2"), &labels);
349
350        // The snapshot is isolated: it still sees the pre-write value.
351        assert_eq!(name_of(alice, &snap).as_deref(), Some("alice"));
352
353        // A fresh latest view sees the new value...
354        let latest =
355            QueryContext::new_with_pending(mgr.get_current(), None, mgr.get_pending_flush());
356        assert_eq!(name_of(alice, &latest).as_deref(), Some("alice2"));
357
358        // ...and the untouched vertex remains visible via the frozen generation.
359        assert_eq!(name_of(bob, &latest).as_deref(), Some("bob"));
360    }
361
362    /// A pin marks the current generation; dropping the snapshot releases it.
363    #[test]
364    fn pin_marks_current_generation() {
365        let mgr = L0Manager::new(0, None);
366        assert!(!mgr.is_current_pinned());
367        let snap = mgr.pin_snapshot();
368        assert!(mgr.is_current_pinned());
369        drop(snap);
370        assert!(
371            !mgr.is_current_pinned(),
372            "dropping the snapshot releases the pin"
373        );
374    }
375
376    /// Clone-on-freeze: after a pinned generation is frozen aside, the snapshot
377    /// still observes its captured state while the new generation takes writes,
378    /// and the new generation starts unpinned.
379    #[test]
380    fn clone_freeze_isolates_pinned_snapshot() {
381        let mgr = L0Manager::new(0, None);
382        let alice = Vid::from(1_u64);
383        let labels = ["Node".to_string()];
384        mgr.get_current()
385            .write()
386            .insert_vertex_with_labels(alice, named("alice"), &labels);
387
388        let snap = mgr.pin_snapshot();
389        assert!(mgr.is_current_pinned());
390
391        // Commit-equivalent: freeze the pinned generation aside, then mutate the
392        // fresh current (where a real commit's merge would land).
393        mgr.freeze_current_for_snapshot();
394        assert!(
395            !mgr.is_current_pinned(),
396            "the fresh generation starts unpinned"
397        );
398        mgr.get_current()
399            .write()
400            .insert_vertex_with_labels(alice, named("alice2"), &labels);
401
402        // The snapshot still sees the pre-freeze value (isolated).
403        let snap_ctx = QueryContext::new_with_pending(snap.main.clone(), None, snap.extra.clone());
404        assert_eq!(name_of(alice, &snap_ctx).as_deref(), Some("alice"));
405
406        // A fresh latest view sees the post-freeze value.
407        let latest =
408            QueryContext::new_with_pending(mgr.get_current(), None, mgr.get_pending_flush());
409        assert_eq!(name_of(alice, &latest).as_deref(), Some("alice2"));
410
411        // Dropping the snapshot releases its hold on the frozen generation.
412        drop(snap);
413    }
414}