openraft 0.10.0-alpha.18

Advanced Raft consensus
Documentation
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Replication stream.

mod backoff_consumer;
pub(crate) mod backoff_state;
pub(crate) mod event_watcher;
pub(crate) mod inflight_append;
pub(crate) mod inflight_append_queue;
pub(crate) mod payload;
pub(crate) mod replicate;
pub(crate) mod replication_context;
pub(crate) mod replication_handle;
pub(crate) mod replication_progress;
mod replication_session_id;
pub(crate) mod response;
pub(crate) mod snapshot_transmitter;
pub(crate) mod snapshot_transmitter_handle;
pub(crate) mod stream_context;
pub(crate) mod stream_state;

use std::fmt;
use std::sync::Arc;
use std::time::Duration;

use display_more::DisplayOptionExt;
use futures_util::FutureExt;
use futures_util::StreamExt;
use payload::Payload;
use replication_progress::ReplicationProgress;
pub(crate) use replication_session_id::ReplicationSessionId;
pub(crate) use response::Progress;
use response::ReplicationResult;
use stream_state::StreamState;
use tracing::Instrument;

use crate::RaftNetworkFactory;
use crate::RaftTypeConfig;
use crate::async_runtime::Mutex;
use crate::async_runtime::watch::WatchReceiver;
use crate::base::BoxStream;
use crate::core::notification::Notification;
use crate::display_ext::display_instant::DisplayInstantExt;
use crate::errors::RPCError;
use crate::errors::ReplicationClosed;
use crate::log_id_range::LogIdRange;
use crate::network::NetBackoff;
use crate::network::NetStreamAppend;
use crate::network::RPCOption;
use crate::progress::inflight_id::InflightId;
use crate::raft::AppendEntriesRequest;
use crate::raft::StreamAppendError;
use crate::raft::StreamAppendResult;
use crate::raft_state::IOId;
use crate::replication::backoff_state::BackoffState;
use crate::replication::event_watcher::EventWatcher;
use crate::replication::inflight_append_queue::InflightAppendQueue;
use crate::replication::replication_context::ReplicationContext;
use crate::replication::stream_context::StreamContext;
use crate::storage::RaftLogStorage;
use crate::type_config::TypeConfigExt;
use crate::type_config::alias::InstantOf;
use crate::type_config::alias::JoinHandleOf;
use crate::type_config::alias::MutexOf;
use crate::type_config::async_runtime::mpsc::MpscSender;
use crate::vote::RaftVote;

/// A task responsible for sending replication events to a target follower in the Raft cluster.
///
/// NOTE: we do not stack replication requests to targets because this could result in
/// out-of-order delivery. We always buffer until we receive a success response, then send the
/// next payload from the buffer.
pub(crate) struct ReplicationCore<C, N, LS>
where
    C: RaftTypeConfig,
    N: RaftNetworkFactory<C>,
    LS: RaftLogStorage<C>,
{
    /// Shared context containing node IDs, session info, and notification channel.
    replication_context: ReplicationContext<C>,

    /// State shared with the request stream generator, protected by a mutex.
    stream_state: Arc<MutexOf<C, StreamState<C, LS>>>,

    /// A channel for receiving events from the RaftCore and snapshot transmitting task.
    event_watcher: EventWatcher<C>,

    /// The next replication payload to send, set when partially completed.
    next_action: Option<Payload<C>>,

    /// Identifies the current in-flight replication batch for progress tracking.
    inflight_id: Option<InflightId>,

    /// The network interface for replicating logs and heartbeat.
    network: Option<N::Network>,

    /// The log replication state tracking progress and matching logs for the follower.
    replication_progress: ReplicationProgress<C>,

    /// Backoff state for rate-limiting retries on persistent errors.
    ///
    /// Encapsulates both the accumulated error rank and the shared backoff iterator
    /// handed to [`StreamState`] via
    /// [`BackoffState::consumer`](crate::replication::backoff_state::BackoffState::consumer).
    /// See [issue #1723](https://github.com/databendlabs/openraft/issues/1723) for the
    /// invariant these two pieces must maintain together.
    backoff_state: BackoffState,
}

impl<C, N, LS> ReplicationCore<C, N, LS>
where
    C: RaftTypeConfig,
    N: RaftNetworkFactory<C>,
    LS: RaftLogStorage<C>,
{
    /// Spawn a new replication task for the target node.
    #[tracing::instrument(level = "trace", skip_all, fields(context=display(&replication_context)
    ))]
    #[allow(clippy::type_complexity)]
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn spawn(
        replication_context: ReplicationContext<C>,
        progress: ReplicationProgress<C>,
        network: N::Network,
        log_reader: LS::LogReader,
        event_watcher: EventWatcher<C>,
        span: tracing::Span,
    ) -> JoinHandleOf<C, Result<(), ReplicationClosed>> {
        tracing::debug!(
            "spawn replication: session_id={}, target={}, committed={}, matching={}",
            replication_context.stream_id,
            replication_context.target,
            progress.local_committed.display(),
            progress.remote_matched.display()
        );

        let backoff_state = BackoffState::new();

        let this = Self {
            replication_context: replication_context.clone(),
            stream_state: Arc::new(MutexOf::<C, _>::new(StreamState {
                replication_context,
                event_watcher: event_watcher.clone(),
                log_reader,
                payload: None,
                inflight_id: None,
                leader_committed: None,
                backoff_consumer: backoff_state.consumer(),
            })),
            inflight_id: None,
            event_watcher,
            network: Some(network),
            replication_progress: progress,
            backoff_state,
            next_action: None,
        };

        C::spawn(this.main().instrument(span))
    }

    /// Creates a stream of AppendEntries requests from the given context.
    fn new_request_stream(stream_context: StreamContext<C, LS>) -> BoxStream<'static, AppendEntriesRequest<C>> {
        let strm = futures_util::stream::unfold(stream_context, Self::next_append_request);
        Box::pin(strm)
    }

    /// Generates the next AppendEntries request and records it in the inflight queue.
    ///
    /// Used as the unfold function for the request stream.
    async fn next_append_request(
        stream_context: StreamContext<C, LS>,
    ) -> Option<(AppendEntriesRequest<C>, StreamContext<C, LS>)> {
        let req = {
            let mut state = stream_context.stream_state.as_ref().lock().await;
            state.next_request().await?
        };

        stream_context.inflight_append_queue.push(req.last_log_id());

        Some((req, stream_context))
    }

    /// Main replication loop that sends AppendEntries requests and processes responses.
    async fn main(mut self) -> Result<(), ReplicationClosed> {
        // Avoid holding a mut ref to self during streaming.
        let mut network = self.network.take().unwrap();

        // reset the streaming state
        self.next_action = None;
        self.inflight_id = None;

        loop {
            tracing::debug!(
                "ReplicationCore: new stream start, next_action = {:?}",
                self.next_action
            );

            let canceled = self.replication_context.cancel_rx.changed().now_or_never();
            if canceled.map(|x| x.is_err()) == Some(true) {
                tracing::info!("ReplicationCore: canceled, quit");
                return Err(ReplicationClosed::new("canceled"));
            }

            let accepted_io: IOId<C> = self.event_watcher.io_accepted_rx.borrow_watched().clone();
            let current_leader = accepted_io.leader_id().clone();
            let belonging_leader = self.replication_context.leader_vote.leader_id().clone();
            if current_leader != belonging_leader {
                tracing::info!(
                    "ReplicationCore: Leader changed from {} to {}, quit replication",
                    belonging_leader,
                    current_leader
                );
                return Err(ReplicationClosed::new("Leader changed"));
            }

            // Kept separate from `on_error` because only this scope holds a reference
            // to `network`, which is needed to construct the `Backoff` iterator.
            self.backoff_state.reconcile(|| network.backoff());

            if self.next_action.is_none() {
                self.drain_events().await?;
            } else {
                // If there is new data to send, even when the current data is not yet finished sending
                // discard the current data, start to send the new data.
                // When data is reverted in LogsSince mode, it needs such a mechanism.
                let new_data = self.event_watcher.replicate_rx.borrow_watched().clone();
                if self.inflight_id != Some(new_data.inflight_id) {
                    tracing::info!(
                        "ReplicationCore replaced current data with inflight id {:?} with new {}",
                        self.inflight_id,
                        new_data.inflight_id
                    );
                    self.inflight_id = Some(new_data.inflight_id);
                    self.next_action = Some(new_data.payload);
                }
            }

            let mut payload = self.next_action.take().unwrap();

            {
                let mut stream_state = self.stream_state.lock().await;
                stream_state.payload = Some(payload.clone());
                stream_state.inflight_id = self.inflight_id;
                stream_state.leader_committed = self.event_watcher.committed_rx.borrow_watched().clone()
            }

            let inflight_queue = InflightAppendQueue::new();

            let stream_context = StreamContext {
                stream_state: self.stream_state.clone(),
                inflight_append_queue: inflight_queue.clone(),
            };

            let req_strm = Self::new_request_stream(stream_context);

            let rpc_timeout = Duration::from_millis(self.replication_context.config.heartbeat_interval);
            let option = RPCOption::new(rpc_timeout);

            let resp_strm_res = network.stream_append(req_strm, option).await;

            let resp_strm = match resp_strm_res {
                Ok(resp_strm) => resp_strm,
                Err(rpc_err) => {
                    self.backoff_state.on_error(rpc_err.backoff_rank());
                    self.send_progress_error(rpc_err, "initiate-stream-replication").await;

                    continue;
                }
            };

            let res = self.handle_response_stream(resp_strm, inflight_queue).await;

            // Response stream is successfully exhausted.
            if res.is_ok() {
                // if partial success is returned, not all data is exhausted. keep sending
                payload.update_matching(self.replication_progress.remote_matched.clone());
                if payload.len() != Some(0) {
                    self.next_action = Some(payload);
                } else {
                    // Payload is all sent.
                    self.inflight_id = None;
                }
            }
        }
    }

    async fn handle_response_stream<'s>(
        &mut self,
        resp_strm: BoxStream<'s, Result<StreamAppendResult<C>, RPCError<C>>>,
        inflight_queue: InflightAppendQueue<C>,
    ) -> Result<(), &'static str> {
        let mut resp_strm = std::pin::pin!(resp_strm);

        while let Some(rpc_res) = resp_strm.next().await {
            tracing::debug!("AppendEntries RPC response: {:?}", rpc_res);

            self.backoff_state.observe(&rpc_res);
            let append_res = match rpc_res {
                Ok(stream_append_res) => stream_append_res,
                Err(rpc_err) => {
                    self.send_progress_error(rpc_err, "stream-replication").await;
                    return Err("RPCError");
                }
            };

            match append_res {
                Ok(matching) => {
                    let last_acked_sending_time = inflight_queue.drain_acked(&matching);

                    if let Some(last) = last_acked_sending_time {
                        self.notify_heartbeat_progress(last).await;
                    }

                    self.replication_progress.remote_matched = matching.clone();

                    self.notify_progress(ReplicationResult(Ok(matching))).await;
                }
                Err(append_err) => {
                    match append_err {
                        StreamAppendError::Conflict(conflict_log_id) => {
                            self.notify_progress(ReplicationResult(Err(conflict_log_id))).await;
                        }
                        StreamAppendError::HigherVote(higher) => {
                            self.replication_context
                                .tx_notify
                                .send(Notification::HigherVote {
                                    target: self.replication_context.target.clone(),
                                    higher,
                                    leader_vote: self.replication_context.leader_vote.clone(),
                                })
                                .await
                                .ok();
                        }
                    }

                    return Err("AppendError");
                }
            }
        }
        Ok(())
    }

    /// Send the error result to RaftCore.
    /// RaftCore will then submit another replication command.
    async fn send_progress_error(&mut self, err: RPCError<C>, when: impl fmt::Display) {
        tracing::warn!(
            "ReplicationCore recv RPCError: {}, when:({}); sending error to RaftCore",
            err,
            when
        );

        // no inflight id means there is no payload is sent, and no one is waiting the response, no need to
        // report.
        if self.inflight_id.is_none() {
            return;
        }
        self.replication_context
            .tx_notify
            .send(Notification::ReplicationProgress {
                progress: Progress {
                    target: self.replication_context.target.clone(),
                    result: Err(err.to_string()),
                },

                inflight_id: self.inflight_id,
            })
            .await
            .ok();
    }

    /// A successful replication implies a successful heartbeat.
    /// This method notifies [`RaftCore`] with a heartbeat progress.
    ///
    /// [`RaftCore`]: crate::core::RaftCore
    async fn notify_heartbeat_progress(&mut self, sending_time: InstantOf<C>) {
        tracing::debug!("ReplicationCore notify_heartbeat_progress: {}", sending_time.display());
        self.replication_context
            .tx_notify
            .send({
                Notification::HeartbeatProgress {
                    stream_id: self.replication_context.stream_id,
                    target: self.replication_context.target.clone(),
                    sending_time,
                }
            })
            .await
            .ok();
    }

    /// Notify RaftCore with the success replication result (log matching or conflict).
    async fn notify_progress(&mut self, replication_result: ReplicationResult<C>) {
        tracing::debug!(
            "{}: target={}, curr_matching={}, result={}, inflight_id={}",
            func_name!(),
            self.replication_context.target,
            self.replication_progress.remote_matched.display(),
            replication_result,
            self.inflight_id.display()
        );

        match &replication_result.0 {
            Ok(matching) => {
                self.replication_progress.remote_matched = matching.clone();

                // No need to notify
                if matching.is_none() {
                    return;
                }
            }
            Err(_conflict) => {
                // Conflict is not allowed to be less than the current matching.
            }
        }

        // always send Conflict error back, even when the inflight id is None
        // for heartbeat to detect log reversion
        self.replication_context
            .tx_notify
            .send({
                Notification::ReplicationProgress {
                    progress: Progress {
                        target: self.replication_context.target.clone(),
                        result: Ok(replication_result.clone()),
                    },
                    // If it is None, meaning it is not a response to a request with payload.
                    inflight_id: self.inflight_id,
                }
            })
            .await
            .ok();
    }

    /// Receive and process events from RaftCore and set `next_action` and `inflight_id`.
    ///
    /// - For log entries: sets `inflight_id = Some(id)` and `next_action = Some(payload)`
    /// - For committed update: sets `inflight_id = None` and `next_action =
    ///   Some(empty_range_payload)`
    ///
    /// It blocks until at least one event is received.
    #[tracing::instrument(level = "trace", skip_all)]
    pub async fn drain_events(&mut self) -> Result<(), ReplicationClosed> {
        tracing::debug!("drain_events");

        let entries = self.event_watcher.replicate_rx.changed();
        let committed = self.event_watcher.committed_rx.changed();

        futures_util::select! {
            entries_res = entries.fuse() => {
                entries_res.map_err(|_e| ReplicationClosed::new("replicate_rx closed"))?;
                let data = self.event_watcher.replicate_rx.borrow_watched().clone();
                self.inflight_id = Some(data.inflight_id);
                self.next_action = Some(data.payload);
            }
            committed_res = committed.fuse() => {
                committed_res.map_err(|_e| ReplicationClosed::new("committed_rx closed"))?;

                // Committed update: create an empty-range payload to sync commit index.
                let committed = self.event_watcher.committed_rx.borrow_watched().clone();
                self.replication_progress.local_committed = committed;

                let m = self.replication_progress.remote_matched.clone();
                let log_id_range = LogIdRange::new(m.clone(), m);
                self.inflight_id = None;
                self.next_action = Some(Payload::LogIdRange { log_id_range });
            }
        };

        tracing::debug!(
            "drain_events set: inflight_id={:?}, next_action={:?}",
            self.inflight_id,
            self.next_action
        );

        Ok(())
    }
}