1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fmt;
use display_more::DisplayOptionExt;
use display_more::DisplaySliceExt;
use openraft_macros::since;
use crate::RaftTypeConfig;
use crate::entry::RaftEntry;
use crate::log_id_range::LogIdRange;
use crate::type_config::alias::LogIdOf;
use crate::type_config::alias::VoteOf;
/// An RPC sent by a cluster leader to replicate log entries (§5.3), and as a heartbeat (§5.2).
///
/// In Openraft a heartbeat [`AppendEntriesRequest`] message could have `prev_log_id=None` and
/// `entries` empty. Which means: to append nothing at the very beginning position on the Follower,
/// which is always valid. Because `prev_log_id` is used to assert `entries` to be consecutive with
/// the previous log entries, and `prev_log_id=None` is the very beginning position and there are no
/// previous log entries.
#[derive(Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(bound = ""))]
pub struct AppendEntriesRequest<C: RaftTypeConfig> {
/// The leader's current vote.
pub vote: VoteOf<C>,
/// The log id immediately preceding the new entries.
pub prev_log_id: Option<LogIdOf<C>>,
/// The new log entries to store.
///
/// This may be empty when the leader is sending heartbeats. Entries
/// are batched for efficiency.
pub entries: Vec<C::Entry>,
/// The log id up to which the leader lets the receiver apply entries.
///
/// Normally it is the leader's cluster-committed log id, which a quorum has granted. With
/// [`Config::enable_relaxed_read`], the leader caps it at its applied log id, so that no node
/// applies an entry before the leader does. For example, a leader that has committed through
/// index 25 but applied only through index 21 sends the log id at index 25 without relaxed
/// reads, and the one at index 21 with them. The [read protocol] explains why.
///
/// The receiver records this value as its cluster-committed log id. It applies only entries
/// that match the leader's log: up to the smaller of this value and the last log id it has
/// accepted from this leader. [`RaftLogStorage::append`] makes entries readable when it
/// returns, and the receiver may apply them from then on, before they are flushed to disk. A
/// successful reply does wait for durability: the receiver sends it only after its vote and
/// its log through [`last_log_id()`](Self::last_log_id) are flushed.
///
/// [`Config::enable_relaxed_read`]: crate::Config::enable_relaxed_read
/// [read protocol]: crate::docs::protocol::read#followers-apply-only-what-the-leader-has-applied
/// [`RaftLogStorage::append`]: crate::storage::RaftLogStorage::append
pub leader_commit: Option<LogIdOf<C>>,
}
impl<C: RaftTypeConfig> fmt::Debug for AppendEntriesRequest<C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AppendEntriesRequest")
.field("vote", &self.vote)
.field("prev_log_id", &self.prev_log_id)
.field("entries", &self.entries)
.field("leader_commit", &self.leader_commit)
.finish()
}
}
impl<C> fmt::Display for AppendEntriesRequest<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"vote={}, prev_log_id={}, leader_commit={}, entries={}",
self.vote,
self.prev_log_id.display(),
self.leader_commit.display(),
self.entries.display()
)
}
}
impl<C> AppendEntriesRequest<C>
where C: RaftTypeConfig
{
/// Returns the last log id in this request.
///
/// This is the log id of the last entry, or `prev_log_id` if entries is empty. A follower that
/// accepts the whole request acknowledges this log id.
#[since(version = "0.10.0")]
pub fn last_log_id(&self) -> Option<LogIdOf<C>> {
self.entries.last().map(|e| e.log_id()).or(self.prev_log_id.clone())
}
/// Returns the log id range covered by this request.
///
/// The range is `(prev_log_id, last_log_id]` where `last_log_id` is the log id
/// of the last entry, or `prev_log_id` if entries is empty.
pub(crate) fn log_id_range(&self) -> LogIdRange<C> {
LogIdRange::new(self.prev_log_id.clone(), self.last_log_id())
}
}
#[cfg(test)]
mod tests {
use crate::Vote;
use crate::engine::testing::UTConfig;
use crate::engine::testing::log_id;
use crate::raft::AppendEntriesRequest;
use crate::testing::blank_ent;
fn req(prev: Option<u64>, entries: Vec<u64>) -> AppendEntriesRequest<UTConfig> {
AppendEntriesRequest {
vote: Vote::new_committed(1, 1),
prev_log_id: prev.map(|i| log_id(1, 1, i)),
entries: entries.into_iter().map(|i| blank_ent::<UTConfig>(1, 1, i)).collect(),
leader_commit: None,
}
}
#[test]
fn test_last_log_id() {
assert_eq!(req(None, vec![]).last_log_id(), None);
assert_eq!(req(Some(5), vec![]).last_log_id(), Some(log_id(1, 1, 5)));
assert_eq!(req(None, vec![1, 2]).last_log_id(), Some(log_id(1, 1, 2)));
assert_eq!(req(Some(5), vec![6, 7]).last_log_id(), Some(log_id(1, 1, 7)));
}
#[test]
fn test_log_id_range() {
let r = req(None, vec![]).log_id_range();
assert_eq!((r.prev, r.last), (None, None));
let r = req(Some(5), vec![]).log_id_range();
assert_eq!((r.prev, r.last), (Some(log_id(1, 1, 5)), Some(log_id(1, 1, 5))));
let r = req(None, vec![1, 2]).log_id_range();
assert_eq!((r.prev, r.last), (None, Some(log_id(1, 1, 2))));
let r = req(Some(5), vec![6, 7]).log_id_range();
assert_eq!((r.prev, r.last), (Some(log_id(1, 1, 5)), Some(log_id(1, 1, 7))));
}
}