simraft_core/snapshot.rs
1//! Snapshot metadata and chunk transfer state.
2//!
3//! The core never interprets snapshot payload bytes (that is
4//! simraft-kv's `snapshot_kv` layout, docs/ARCHITECTURE.md section
5//! 7.4). It tracks identity and transfer progress only: which snapshot
6//! a follower is buffering, how many bytes it holds, and which
7//! snapshot a leader has loaded for relay to lagging followers.
8
9use crate::ids::{LogIndex, Term};
10use crate::membership::ClusterConfig;
11
12/// The identity of a snapshot: the last log entry it covers and the
13/// configuration in force at that entry.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct SnapshotMeta {
16 /// The highest log index included in the snapshot.
17 pub last_included_index: LogIndex,
18 /// The term of the entry at `last_included_index`.
19 pub last_included_term: Term,
20 /// The cluster configuration in force at `last_included_index`.
21 pub config: ClusterConfig,
22}
23
24/// A follower's partially received snapshot. Chunks are accepted only
25/// at the exact buffered length (invariant N-02); a chunk carrying a
26/// different identity replaces this buffer wholesale.
27#[derive(Debug, Clone)]
28pub(crate) struct IncomingSnapshot {
29 /// The identity of the snapshot being received.
30 pub meta: SnapshotMeta,
31 /// The bytes buffered so far, in offset order.
32 pub data: Vec<u8>,
33}
34
35impl IncomingSnapshot {
36 /// A fresh buffer for `meta` with no bytes yet.
37 pub const fn new(meta: SnapshotMeta) -> Self {
38 Self {
39 meta,
40 data: Vec::new(),
41 }
42 }
43
44 /// The offset the next accepted chunk must carry.
45 pub const fn expected_offset(&self) -> u64 {
46 self.data.len() as u64
47 }
48}
49
50/// A snapshot a node loaded from its own state machine, held by a
51/// leader for relay to followers whose next index has fallen to or
52/// below the snapshot base (invariant N-01), and by any node for its
53/// own log compaction once the driver persists it.
54#[derive(Debug, Clone)]
55pub(crate) struct LoadedSnapshot {
56 /// The identity of the loaded snapshot.
57 pub meta: SnapshotMeta,
58 /// The complete snapshot payload bytes.
59 pub data: Vec<u8>,
60}