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