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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
pub(crate) mod update;
use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use display_more::DisplayOptionExt;
use validit::Validate;
use crate::LogIdOptionExt;
use crate::RaftState;
use crate::RaftTypeConfig;
use crate::engine::EngineConfig;
use crate::progress::VecProgressEntry;
use crate::progress::VecProgressEntryData;
use crate::progress::entry::update::Updater;
use crate::progress::inflight::Inflight;
use crate::progress::stream_id::StreamId;
use crate::raft_state::LogStateReader;
use crate::type_config::alias::LogIdOf;
/// State of replication to a target node.
#[derive(Clone, Debug)]
#[derive(PartialEq, Eq)]
pub(crate) struct ProgressEntry<C>
where C: RaftTypeConfig
{
pub(crate) id: C::NodeId,
/// The id of the last matching log on the target following node.
pub(crate) matching: Option<LogIdOf<C>>,
pub(crate) data: ProgressData<C>,
}
/// Application-owned replication state that is not used for quorum calculation.
#[derive(Clone, Debug)]
#[derive(PartialEq, Eq)]
pub(crate) struct ProgressData<C>
where C: RaftTypeConfig
{
pub(crate) stream_id: StreamId,
/// 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<C>,
/// One plus the max log index on the following node that might match the leader log.
pub(crate) searching_end: u64,
/// Index of the first entry for the next probe to send, tried before binary search.
///
/// The probe's `prev_log_id` is the entry before it. The next send consumes it, and skips it
/// if it no longer lies in the search range.
pub(crate) probe_start: Option<u64>,
/// If true, reset the progress by setting matching to `None` when the follower's
/// log is found reverted to an early state.
///
/// This allows the target node to clean its data and wait for the leader to replicate all data
/// to it.
///
/// This flag will be cleared after the progress entry is reset.
pub(crate) allow_log_reversion: bool,
}
impl<C> ProgressEntry<C>
where C: RaftTypeConfig
{
#[cfg(test)]
pub(crate) fn testing_new(id: C::NodeId, matching: Option<LogIdOf<C>>) -> Self {
Self {
id,
matching: matching.clone(),
data: ProgressData::new(StreamId::new(0), 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(id: C::NodeId, stream_id: StreamId, end: u64) -> Self {
Self {
id,
matching: None,
data: ProgressData::new(stream_id, end),
}
}
/// Start the next probe at entry `start`, before falling back to binary search.
pub(crate) fn with_probe_start(mut self, start: u64) -> Self {
self.data.probe_start = Some(start);
self
}
pub(crate) fn matching(&self) -> Option<&LogIdOf<C>> {
self.matching.as_ref()
}
// This method is only used by tests.
#[cfg(test)]
pub(crate) fn with_inflight(mut self, inflight: Inflight<C>) -> Self {
debug_assert_eq!(self.data.inflight, Inflight::None);
self.data.inflight = inflight;
self
}
pub(crate) fn new_updater<'a>(&'a mut self, engine_config: &'a EngineConfig<C>) -> Updater<'a, C> {
Updater::new(engine_config, self)
}
}
impl<C> ProgressData<C>
where C: RaftTypeConfig
{
pub(crate) fn new(stream_id: StreamId, searching_end: u64) -> Self {
Self {
stream_id,
inflight: Inflight::None,
searching_end,
probe_start: None,
allow_log_reversion: false,
}
}
}
impl<C> VecProgressEntry for ProgressEntry<C>
where C: RaftTypeConfig
{
type Id = C::NodeId;
type Progress = Option<LogIdOf<C>>;
fn id(&self) -> &Self::Id {
&self.id
}
fn progress(&self) -> &Self::Progress {
&self.matching
}
fn progress_mut(&mut self) -> &mut Self::Progress {
&mut self.matching
}
}
impl<C> VecProgressEntryData for ProgressEntry<C>
where C: RaftTypeConfig
{
type Data = ProgressData<C>;
fn data(&self) -> &Self::Data {
&self.data
}
fn data_mut(&mut self) -> &mut Self::Data {
&mut self.data
}
}
impl<C> ProgressEntry<C>
where C: RaftTypeConfig
{
/// 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: &LogIdOf<C>) -> bool {
match &self.data.inflight {
Inflight::None => false,
Inflight::Probe { log_id_range, .. } => {
let lid = Some(upto);
lid > log_id_range.prev.as_ref()
}
Inflight::Snapshot { inflight_id: _ } => false,
Inflight::LogsSince { prev, .. } => {
// All logs after prev are inflight in streaming mode
let lid = Some(upto);
lid > prev.as_ref()
}
}
}
/// Initialize a replication action: sending log entries or sending a snapshot.
///
/// If there is an action in progress, i.e., `inflight` is not None, it returns an `Err`
/// containing the current `inflight` data.
///
/// See: [Algorithm to find the last matching log id on a Follower][algo].
///
/// # Decision logic
///
/// The follower's last log id matching the leader's log is known to lie in the range
/// `[matching, searching_end)`; the invariant `matching.next_index() <= searching_end`
/// always holds. This puts the progress in one of two regimes:
///
/// - **Probing** (`matching.next_index() < searching_end`): the exact matching point is not yet
/// determined. Send one AppendEntries from the candidate range `(prev, last]`
/// ([`Inflight::Probe`]) with `prev` at a binary-search midpoint: a success response raises
/// `matching`, a conflict response lowers `searching_end`, until the range collapses.
///
/// - **Pipeline** (`matching.next_index() == searching_end`): the matching point is exactly
/// `matching`. Stream all logs after it, with no fixed upper bound ([`Inflight::LogsSince`]).
///
/// A pending [`ProgressData::probe_start`] is tried before either regime applies. It is raised
/// to the purge boundary and used only if it then lies in
/// `[matching.next_index(), searching_end]`. Every send consumes it.
///
/// Purging constrains what AppendEntries can be built: log entries at index
/// `<= purge_upto` are deleted, and only the log id at `purge_upto` itself is still
/// known, as the snapshot's last log id. Thus the lowest usable `prev` is `purge_upto`.
/// A snapshot must be sent instead of logs in exactly two situations:
///
/// 1. `searching_end < purge_upto_next`, in either regime: every candidate matching position
/// lies strictly below the purge boundary. The lowest possible probe, `prev = purge_upto`,
/// sits at an index `>= searching_end` — a position already known not to match — so the
/// follower would reply with a conflict at that same index, which carries no new information
/// and is discarded (see [`Updater::update_conflicting`]): 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 position worth probing.
///
/// 2. Probing while the leader log is fully purged (`purge_upto == last_log_id`, which makes
/// the send range empty: `start == end`): the probe cannot carry any entry, and
/// [`Inflight::probe`] cannot represent an AppendEntries without payload — an empty range
/// collapses to [`Inflight::None`]. Pipeline mode is not affected: [`Inflight::LogsSince`]
/// is an open-ended stream, and an empty tail is valid.
///
/// [algo]: crate::docs::protocol::replication::log_replication#algorithm-to-find-the-last-matching-log-id-on-a-follower
/// [`Updater::update_conflicting`]: crate::progress::entry::update::Updater::update_conflicting
pub(crate) fn next_send(
&mut self,
log_state: &mut RaftState<C>,
max_entries: u64,
) -> Result<&Inflight<C>, &Inflight<C>> {
if !self.data.inflight.is_none() {
return Err(&self.data.inflight);
}
let last_next = log_state.last_log_id().next_index();
debug_assert!(
self.data.searching_end <= last_next,
"expect: searching_end: {} <= last_log_id.next_index: {}",
self.data.searching_end,
last_next
);
let purge_upto_next = log_state.purge_upto().next_index();
let inflight_id = log_state.new_inflight_id();
// A pending probe applies to this send only, including when a snapshot is sent instead.
let probe_start = self.data.probe_start.take();
// Snapshot condition 1: all candidate matching positions are purged.
if self.data.searching_end < purge_upto_next {
self.data.inflight = Inflight::snapshot(inflight_id);
return Ok(&self.data.inflight);
}
let matching_next = self.matching().next_index();
if let Some(probe_start) = probe_start {
// Like a binary-search probe, start no lower than the purge boundary.
let start = std::cmp::max(probe_start, purge_upto_next);
let end = std::cmp::min(start + max_entries, last_next);
// Responses received since the probe was chosen may have narrowed the search range.
let in_search_range = matching_next <= start && start <= self.data.searching_end;
if in_search_range && start < end {
let prev = log_state.prev_log_id(start);
let last = log_state.prev_log_id(end);
self.data.inflight = Inflight::probe(prev, last, inflight_id);
return Ok(&self.data.inflight);
}
}
let is_probing = matching_next < self.data.searching_end;
if is_probing {
// Probe at the binary-search midpoint, but not below the purge boundary.
// `start <= searching_end` still holds: `mid <= searching_end` by construction,
// and `purge_upto_next <= searching_end` by snapshot condition 1 above.
let mid = Self::calc_mid(matching_next, self.data.searching_end);
let start = std::cmp::max(mid, purge_upto_next);
let end = std::cmp::min(start + max_entries, last_next);
// Snapshot condition 2: the leader log is fully purged; there is no entry
// for the probe to carry.
if start == end {
self.data.inflight = Inflight::snapshot(inflight_id);
return Ok(&self.data.inflight);
}
let prev = log_state.prev_log_id(start);
let last = log_state.prev_log_id(end);
self.data.inflight = Inflight::probe(prev, last, inflight_id);
} else {
// Pipeline: stream every log after the known matching point.
// Snapshot condition 1 ensured `matching >= purge_upto`: no needed log is purged.
self.data.inflight = Inflight::LogsSince {
prev: self.matching.clone(),
inflight_id,
};
}
Ok(&self.data.inflight)
}
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<C> Display for ProgressEntry<C>
where C: RaftTypeConfig
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{{P({})[{}, {}), inflight:{}}}",
self.data.stream_id,
self.matching().display(),
self.data.searching_end,
self.data.inflight,
)
}
}
impl<C> Validate for ProgressEntry<C>
where C: RaftTypeConfig
{
fn validate(&self) -> Result<(), Box<dyn Error>> {
validit::less_equal!(self.matching().next_index(), self.data.searching_end);
self.data.inflight.validate()?;
match &self.data.inflight {
Inflight::None => {}
Inflight::Probe { 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.as_ref());
validit::less_equal!(log_id_range.prev.next_index(), self.data.searching_end);
}
Inflight::Snapshot { inflight_id: _ } => {}
Inflight::LogsSince { .. } => {}
}
Ok(())
}
}
#[cfg(test)]
mod tests;