Skip to main content

simraft_core/
action.rs

1//! Outputs of the sans-I/O core.
2//!
3//! `Node::step` returns actions in order. The driver executes them:
4//! sends go to the network, persists go to storage, applies go to the
5//! state machine. The core itself performs no I/O.
6
7use crate::error::ProtocolError;
8use crate::ids::{ClientId, LogIndex, NodeId, Term};
9use crate::log::LogEntry;
10use crate::membership::ConfigChange;
11use crate::message::Message;
12use crate::snapshot::SnapshotMeta;
13
14/// The outcome of a client proposal, delivered when the entry that
15/// carries it commits and applies (or immediately, when this node cannot
16/// accept it).
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum ClientResult {
19    /// The proposal committed and applied exactly once.
20    Committed,
21    /// This node is not the leader. `leader` is its current hint for the
22    /// leader's identity, when it has one.
23    NotLeader {
24        /// The known leader, if any.
25        leader: Option<NodeId>,
26    },
27}
28
29/// The outcome of a membership change proposal, delivered
30/// immediately when the change is rejected or this node is not the
31/// leader, and when the config entry commits when it is accepted.
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub enum ConfigResult {
34    /// The config entry committed; the new configuration is in force.
35    Committed,
36    /// This node is not the leader. `leader` is its current hint for
37    /// the leader's identity, when it has one.
38    NotLeader {
39        /// The known leader, if any.
40        leader: Option<NodeId>,
41    },
42    /// The change broke a membership rule; the error names the values
43    /// that decided the rejection.
44    Rejected {
45        /// The rule the change broke.
46        error: ProtocolError,
47    },
48}
49
50/// One output of `Node::step`.
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub enum Action {
53    /// Send an RPC message to a peer.
54    Send {
55        /// The recipient.
56        to: NodeId,
57        /// The message.
58        msg: Message,
59    },
60    /// Persist the hard state (current term and voted-for) before any
61    /// response that depends on it is released.
62    PersistHardState {
63        /// The current term to store.
64        current_term: Term,
65        /// The vote to store.
66        voted_for: Option<NodeId>,
67    },
68    /// Persist log entries. Storage semantics: truncate the durable log
69    /// to `from_index - 1`, then store `entries` at consecutive indexes
70    /// starting at `from_index`.
71    PersistEntries {
72        /// The index of the first entry in `entries`.
73        from_index: LogIndex,
74        /// The entries to store, in index order.
75        entries: Vec<LogEntry>,
76    },
77    /// Apply the committed entry at `index` to the state machine.
78    /// Applies are emitted in index order, exactly once per index per
79    /// node lifetime.
80    Apply {
81        /// The entry's log index.
82        index: LogIndex,
83        /// The entry to apply.
84        entry: LogEntry,
85    },
86    /// A `ReadIndex` read is confirmed linearizable. The driver serves
87    /// the value of `key` from its state machine once its applied index
88    /// reaches `read_index`, and must not serve a newer-or-older value.
89    ReadReady {
90        /// The correlation id from `Event::ReadIndex`.
91        request_id: u64,
92        /// The key that was read.
93        key: Vec<u8>,
94        /// The commit index the read is pinned to.
95        read_index: LogIndex,
96    },
97    /// A `ReadIndex` read cannot be served by this node (it is not the
98    /// leader, or it stopped being the leader mid-round).
99    ReadRejected {
100        /// The correlation id from `Event::ReadIndex`.
101        request_id: u64,
102        /// The known leader, if any.
103        leader: Option<NodeId>,
104    },
105    /// Answer a client proposal.
106    ClientResponse {
107        /// The proposing client.
108        client_id: ClientId,
109        /// The proposal's sequence number.
110        seq: u64,
111        /// The outcome.
112        result: ClientResult,
113    },
114    /// Persist a fully assembled snapshot payload (received over the
115    /// wire, or this node's own once loaded) before the core installs
116    /// it. The matching `Event::SnapshotPersisted` releases the
117    /// install.
118    PersistSnapshot {
119        /// The snapshot's identity.
120        meta: SnapshotMeta,
121        /// The complete snapshot payload bytes.
122        data: Vec<u8>,
123    },
124    /// The core needs this node's snapshot payload (its log base has
125    /// advanced past entries a follower still needs, or the driver
126    /// asked for compaction headroom). The driver answers with
127    /// `Event::SnapshotLoaded`.
128    LoadSnapshot {
129        /// The identity of the snapshot to load.
130        meta: SnapshotMeta,
131    },
132    /// Answer a membership change proposal.
133    ConfigResponse {
134        /// The change that was proposed.
135        change: ConfigChange,
136        /// The outcome.
137        result: ConfigResult,
138    },
139    /// This node was a leader and stepped down on discovering a higher
140    /// term. Drivers use it to fail in-flight leader work promptly.
141    StepDown {
142        /// The term the node stepped down into.
143        term: Term,
144    },
145}