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