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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::borrow::Borrow;
use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use validit::Validate;
use crate::display_ext::DisplayOptionExt;
use crate::progress::inflight::Inflight;
use crate::progress::inflight::InflightError;
use crate::raft_state::LogStateReader;
use crate::summary::MessageSummary;
use crate::LogId;
use crate::LogIdOptionExt;
use crate::NodeId;
/// State of replication to a target node.
#[derive(Clone, Copy, Debug)]
#[derive(PartialEq, Eq)]
pub(crate) struct ProgressEntry<NID: NodeId> {
/// The id of the last matching log on the target following node.
pub(crate) matching: Option<LogId<NID>>,
pub(crate) curr_inflight_id: u64,
/// The data being transmitted in flight.
///
/// A non-none inflight expects a response when the data was successfully sent or failed.
pub(crate) inflight: Inflight<NID>,
/// One plus the max log index on the following node that might match the leader log.
pub(crate) searching_end: u64,
}
impl<NID: NodeId> ProgressEntry<NID> {
#[allow(dead_code)]
pub(crate) fn new(matching: Option<LogId<NID>>) -> Self {
Self {
matching: matching.clone(),
curr_inflight_id: 0,
inflight: Inflight::None,
searching_end: matching.next_index(),
}
}
/// Create a progress entry that does not have any matching log id.
///
/// It's going to initiate a binary search to find the minimal matching log id.
pub(crate) fn empty(end: u64) -> Self {
Self {
matching: None,
curr_inflight_id: 0,
inflight: Inflight::None,
searching_end: end,
}
}
// This method is only used by tests.
#[allow(dead_code)]
pub(crate) fn with_curr_inflight_id(mut self, v: u64) -> Self {
self.curr_inflight_id = v;
self
}
// This method is only used by tests.
#[allow(dead_code)]
pub(crate) fn with_inflight(mut self, inflight: Inflight<NID>) -> Self {
debug_assert_eq!(self.inflight, Inflight::None);
self.inflight = inflight;
self
}
/// Return if a range of log id `..=log_id` is inflight sending.
///
/// `prev_log_id` is never inflight.
pub(crate) fn is_log_range_inflight(&self, upto: &LogId<NID>) -> bool {
match &self.inflight {
Inflight::None => false,
Inflight::Logs { log_id_range, .. } => {
let lid = Some(upto.clone());
lid > log_id_range.prev
}
Inflight::Snapshot { last_log_id: _, .. } => false,
}
}
pub(crate) fn update_matching(
&mut self,
request_id: u64,
matching: Option<LogId<NID>>,
) -> Result<(), InflightError> {
tracing::debug!(
self = display(&self),
request_id = display(request_id),
matching = display(matching.summary()),
"update_matching"
);
self.inflight.ack(request_id, matching.clone())?;
debug_assert!(matching >= self.matching);
self.matching = matching;
let matching_next = self.matching.next_index();
self.searching_end = std::cmp::max(self.searching_end, matching_next);
Ok(())
}
/// Update conflicting log index.
///
/// Conflicting log index is the last found log index on a follower that is not matching the
/// leader log.
///
/// `request_id` is `None` for a Heartbeat request, meaning there is no inflight data, and
/// `Some(request_id)` for a normal AppendEntries request.
///
/// Usually if follower's data is lost, `conflict` is always greater than or equal `matching`.
/// But for testing purpose, a follower is allowed to clean its data and wait for leader to
/// replicate all data to it.
///
/// To allow a follower to clean its data, enable feature flag [`loosen-follower-log-revert`] .
///
/// [`loosen-follower-log-revert`]: crate::docs::feature_flags#feature-flag-loosen-follower-log-revert
pub(crate) fn update_conflicting(&mut self, request_id: Option<u64>, conflict: u64) -> Result<(), InflightError> {
tracing::debug!(
"update_conflict: current_progress={}, request_id={}, conflict={}",
self,
request_id.display(),
conflict
);
if let Some(request_id) = request_id {
self.inflight.conflict(request_id, conflict)?;
}
if conflict >= self.searching_end {
tracing::debug!(
"conflict {} >= searching_end {}; no need to update",
conflict,
self.searching_end
);
return Ok(());
}
self.searching_end = conflict;
// An already matching log id is found lost:
//
// - If log reversion is allowed, just restart the binary search from the beginning.
// - Otherwise, panic it.
{
#[cfg(feature = "loosen-follower-log-revert")]
if conflict < self.matching.next_index() {
tracing::warn!(
"conflict {} < last matching {}: follower log is reverted; with 'loosen-follower-log-revert' enabled, this is allowed.",
conflict,
self.matching.display(),
);
self.matching = None;
}
debug_assert!(
conflict >= self.matching.next_index(),
"follower log reversion is not allowed \
without `--features loosen-follower-log-revert`; \
matching: {}; conflict: {}",
self.matching.display(),
conflict
);
}
Ok(())
}
/// Initialize a replication action: sending log entries or sending snapshot.
///
/// If there is an action in progress, i.e., `inflight` is not None, it returns an `Err`
/// containing the current `inflight` data
#[allow(dead_code)]
pub(crate) fn next_send(
&mut self,
log_state: &impl LogStateReader<NID>,
max_entries: u64,
) -> Result<&Inflight<NID>, &Inflight<NID>> {
if !self.inflight.is_none() {
return Err(&self.inflight);
}
let last_next = log_state.last_log_id().next_index();
debug_assert!(
self.searching_end <= last_next,
"expect: searching_end: {} <= last_log_id.next_index: {}",
self.searching_end,
last_next
);
let purge_upto_next = {
let purge_upto = log_state.purge_upto();
purge_upto.next_index()
};
// `searching_end` is the max value for `start`.
// Snapshot condition 1: every candidate matching position is purged.
//
// The lowest usable `prev` is `purge_upto`, which sits at an index `>= searching_end`,
// a position already known not to match. The follower would reply with a conflict at
// that same index, which carries no new information, so log replication cannot make
// progress. `searching_end == purge_upto_next` is excluded: `prev = purge_upto` is then
// at index `searching_end - 1`, still a candidate worth probing.
if self.searching_end < purge_upto_next {
self.curr_inflight_id += 1;
let snapshot_last = log_state.snapshot_last_log_id();
self.inflight = Inflight::snapshot(snapshot_last.cloned()).with_id(self.curr_inflight_id);
return Ok(&self.inflight);
}
// Replicate by logs.
// Run a binary search to find the matching log id, if matching log id is not determined.
let matching_next = self.matching.next_index();
let mut start = Self::calc_mid(matching_next, self.searching_end);
if start < purge_upto_next {
start = purge_upto_next;
}
let end = std::cmp::min(start + max_entries, last_next);
if start == end {
// Snapshot condition 2: still probing, but the leader log is fully purged, so there
// is no entry for the probe to carry and `Inflight::logs` cannot represent an
// AppendEntries without payload. Without this the call would return `Inflight::None`
// on every attempt and the follower could never converge.
if matching_next < self.searching_end {
self.curr_inflight_id += 1;
let snapshot_last = log_state.snapshot_last_log_id();
self.inflight = Inflight::snapshot(snapshot_last.cloned()).with_id(self.curr_inflight_id);
return Ok(&self.inflight);
}
// The matching point is determined and there is nothing after it to send.
self.inflight = Inflight::None;
return Err(&self.inflight);
}
let prev = log_state.prev_log_id(start);
let last = log_state.prev_log_id(end);
self.curr_inflight_id += 1;
self.inflight = Inflight::logs(prev, last).with_id(self.curr_inflight_id);
Ok(&self.inflight)
}
/// Return the index range(`[start,end]`) of the first log in the next AppendEntries.
///
/// The returned range is left close and right close.
#[allow(dead_code)]
pub(crate) fn sending_start(&self) -> (u64, u64) {
let mid = Self::calc_mid(self.matching.next_index(), self.searching_end);
(mid, self.searching_end)
}
fn calc_mid(matching_next: u64, end: u64) -> u64 {
debug_assert!(matching_next <= end);
let d = end - matching_next;
let offset = d / 16 * 8;
matching_next + offset
}
}
impl<NID: NodeId> Borrow<Option<LogId<NID>>> for ProgressEntry<NID> {
fn borrow(&self) -> &Option<LogId<NID>> {
&self.matching
}
}
impl<NID: NodeId> Display for ProgressEntry<NID> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{[{}, {}), inflight:{}}}",
self.matching.summary(),
self.searching_end,
self.inflight
)
}
}
impl<NID: NodeId> Validate for ProgressEntry<NID> {
fn validate(&self) -> Result<(), Box<dyn Error>> {
validit::less_equal!(self.matching.next_index(), self.searching_end);
self.inflight.validate()?;
match &self.inflight {
Inflight::None => {}
Inflight::Logs { log_id_range, .. } => {
// matching <= prev_log_id <= last_log_id
// prev_log_id.next_index() <= searching_end
validit::less_equal!(&self.matching, &log_id_range.prev);
validit::less_equal!(log_id_range.prev.next_index(), self.searching_end);
}
Inflight::Snapshot { last_log_id, .. } => {
// There is no need to send a snapshot smaller than last matching.
validit::less!(&self.matching, &last_log_id);
}
}
Ok(())
}
}
#[cfg(test)]
mod tests;