From 58f442b82cf9e8deb9fe9043170e181c1bcfd354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Tue, 27 Dec 2022 09:29:09 +0800 Subject: [PATCH] Refactor: add comment explaining install-snapshot --- openraft/src/engine/engine_impl.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/openraft/src/engine/engine_impl.rs b/openraft/src/engine/engine_impl.rs index 9398ff43e..9f5fd984c 100644 --- a/openraft/src/engine/engine_impl.rs +++ b/openraft/src/engine/engine_impl.rs @@ -786,6 +786,22 @@ where /// Follower/Learner handles install-snapshot. #[tracing::instrument(level = "debug", skip_all)] pub(crate) fn install_snapshot(&mut self, meta: SnapshotMeta) { + // There are two special cases in which snapshot last log id does not exists locally: + // Snapshot last log id before the local last-purged-log-id, or after the local last-log-id: + // + // snapshot ----. + // v + // -----------------------llllllllll---> + // + // snapshot ----. + // v + // ----lllllllllll---------------------> + // + // In the first case, snapshot-last-log-id <= last-purged-log-id <= local-snapshot-last-log-id. + // Thus snapshot is obsolete and won't be installed. + // + // In the second case, all local logs will be purged after install. + tracing::info!("install_snapshot: meta:{:?}", meta); let snap_last_log_id = meta.last_log_id; @@ -848,6 +864,10 @@ where // A local log that is <= snap_last_log_id can not conflict with the leader. // But there will be a hole in the logs. Thus it's better remove all logs. + + // In the second case, if local-last-log-id is smaller than snapshot-last-log-id, + // and this node crashes after installing snapshot and before purging logs, + // the log will be purged the next start up, in [`RaftState::get_initial_state`]. self.purge_log(snap_last_log_id) }