Skip to main content

simraft_core/
event.rs

1//! Inputs to the sans-I/O core.
2//!
3//! A driver (`simraft-sim` or `simraft-node`) feeds `Node` one `Event`
4//! at a time and executes the `Action` values that come back.
5
6use crate::ids::{ClientId, LogIndex, NodeId, Term, Tick};
7use crate::log::LogEntry;
8use crate::membership::ConfigChange;
9use crate::message::Message;
10use crate::snapshot::SnapshotMeta;
11
12/// Which kind of durable write a `Persisted` event acknowledges.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum PersistKind {
15    /// A hard state write (current term and voted-for) completed.
16    HardState,
17    /// A log entries write completed through `up_to_index`.
18    Entries,
19}
20
21/// The persistent state a node restarts with: its hard state and its
22/// durable log entries, exactly as the storage layer recovered them.
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct HardState {
25    /// The latest term the node had seen before the crash.
26    pub current_term: Term,
27    /// The candidate the node voted for in `current_term`, if any.
28    pub voted_for: Option<NodeId>,
29}
30
31/// One input to `Node::step`.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum Event {
34    /// The driver's clock reached `now`. The core compares this against
35    /// its own deadlines; it never reads any other clock.
36    Tick {
37        /// The current virtual time.
38        now: Tick,
39    },
40    /// A client asks the leader for a linearizable read of `key`.
41    ///
42    /// The core runs the `ReadIndex` protocol (docs/ARCHITECTURE.md
43    /// section 8) and answers with `Action::ReadReady` once the read is
44    /// safe to serve, or `Action::ReadRejected` when this node cannot
45    /// serve it. The key is carried opaquely; the value lives in the
46    /// state machine, not the core.
47    ReadIndex {
48        /// The client-chosen correlation id for this read.
49        request_id: u64,
50        /// The key to read.
51        key: Vec<u8>,
52    },
53    /// A client proposes a command to this node.
54    Propose {
55        /// The proposing client.
56        client_id: ClientId,
57        /// The per-client sequence number.
58        seq: u64,
59        /// The canonical command payload (simraft-kv encoding).
60        payload: Vec<u8>,
61    },
62    /// An operator proposes a single-server membership change
63    /// (docs/ARCHITECTURE.md section 9). The leader answers with
64    /// `Action::ConfigResponse`: immediately when the change is
65    /// rejected, and when the config entry commits when it is
66    /// accepted.
67    ProposeConfig {
68        /// The change being proposed.
69        change: ConfigChange,
70    },
71    /// The driver loaded this node's snapshot payload from its state
72    /// machine, either for the node's own log compaction or because
73    /// the node leads and a follower needs the snapshot relayed. The
74    /// core holds the bytes until every lagging follower has passed
75    /// the snapshot base.
76    SnapshotLoaded {
77        /// The snapshot's identity.
78        meta: SnapshotMeta,
79        /// The complete snapshot payload bytes.
80        data: Vec<u8>,
81    },
82    /// A snapshot the driver was asked to persist (its own, received
83    /// over the wire and assembled, or relayed) is now durable. Only
84    /// on this acknowledgment does the core install the snapshot:
85    /// advance the log base, retain or discard the log per the prefix
86    /// rule, and jump the apply cursor (invariant T-02 applied to
87    /// snapshots).
88    SnapshotPersisted {
89        /// The persisted snapshot's identity.
90        meta: SnapshotMeta,
91    },
92    /// An RPC message arrived from another node.
93    Message {
94        /// The sender.
95        from: NodeId,
96        /// The message.
97        msg: Message,
98    },
99    /// A durable write the driver was asked to perform has completed.
100    /// Dependent responses are released only when this arrives
101    /// (invariant T-02).
102    Persisted {
103        /// Which write completed.
104        kind: PersistKind,
105        /// For `PersistKind::Entries`, the highest index now durable.
106        /// Ignored for `PersistKind::HardState`.
107        up_to_index: LogIndex,
108    },
109    /// The node restarted; this is its recovered durable state. Sent by
110    /// the driver as the first event after construction when restarting
111    /// a crashed node.
112    Restored {
113        /// The recovered hard state.
114        hard_state: HardState,
115        /// The recovered durable log entries above the snapshot
116        /// boundary, in index order: every durable entry when
117        /// `snapshot` is `None`, and only the entries the
118        /// snapshot does not cover when it is present (invariant
119        /// N-08). A store never returns covered entries at
120        /// restore.
121        entries: Vec<LogEntry>,
122        /// The recovered durable snapshot's boundary, when the
123        /// storage layer holds one whose payload the driver has
124        /// verified and installed into its state machine. The
125        /// core installs the boundary at restore (invariant
126        /// N-07): without it a restarted node's base never
127        /// reflects its snapshot, and the recovered suffix is
128        /// replayed at the wrong indexes.
129        snapshot: Option<SnapshotMeta>,
130    },
131}