فهرست منبع

Replace the last eprintln() with check_or_reocrd.

Jing Yang 4 سال پیش
والد
کامیت
7f6e0854aa
2فایلهای تغییر یافته به همراه48 افزوده شده و 16 حذف شده
  1. 12 0
      src/daemon_env.rs
  2. 36 16
      src/sync_log_entries.rs

+ 12 - 0
src/daemon_env.rs

@@ -59,6 +59,18 @@ pub(crate) enum ErrorKind {
     SnapshotBeforeCommitted(usize, Term),
     /// The application sent a snapshot that contains items beyond the log end.
     SnapshotAfterLogEnd(usize),
+    /// The recipient of [`crate::InstallSnapshot`] should have been able to
+    /// verify the term at index `.0` but did not. The index `.0` is after
+    /// their commit index `.1`, and thus not yet committed or archived into a
+    /// local snapshot. The recipient should still have the log entry at `.0`.
+    RefusedSnapshotAfterCommitted(usize, usize),
+    /// Similar to [`Self::RollbackCommitted`], but this error is logged by the
+    /// leader after receiving a reply from a follower.
+    DivergedBeforeCommitted(usize, usize),
+    /// A follower committed a log entry that is different from the leader. An
+    /// opportunistic check that looks for log mismatches, missing committed log
+    /// entries or other corruptions.
+    DivergedAtCommitted(usize),
 }
 
 impl DaemonEnv {

+ 36 - 16
src/sync_log_entries.rs

@@ -4,6 +4,8 @@ use std::time::Duration;
 
 use parking_lot::{Condvar, Mutex};
 
+use crate::check_or_record;
+use crate::daemon_env::ErrorKind;
 use crate::index_term::IndexTerm;
 use crate::utils::{retry_rpc, RPC_DEADLINE};
 use crate::{
@@ -148,14 +150,22 @@ where
                 }
             }
             Ok(SyncLogEntriesResult::Archived(committed)) => {
-                if prev_log_index >= committed.index {
-                    eprintln!(
-                        "Peer {} misbehaves: send prev log index {}, got committed {:?}",
+                let mut rf = rf.lock();
+
+                check_or_record!(
+                    prev_log_index < committed.index,
+                    ErrorKind::RefusedSnapshotAfterCommitted(
+                        prev_log_index,
+                        committed.index
+                    ),
+                    format!(
+                        "Peer {} misbehaves: claimed log index {} is archived, \
+                        but commit index is at {:?}) which is before that",
                         peer_index, prev_log_index, committed
-                    );
-                }
+                    ),
+                    &rf
+                );
 
-                let mut rf = rf.lock();
                 Self::check_committed(&rf, peer, committed.clone());
 
                 rf.current_step[peer_index] = 0;
@@ -165,13 +175,20 @@ where
                 let _ = rerun.send(Some(Peer(peer_index)));
             }
             Ok(SyncLogEntriesResult::Diverged(committed)) => {
-                if prev_log_index < committed.index {
-                    eprintln!(
-                        "Peer {} misbehaves: diverged at {}, but committed {:?}",
-                        peer_index, prev_log_index, committed
-                    );
-                }
                 let mut rf = rf.lock();
+                check_or_record!(
+                    prev_log_index > committed.index,
+                    ErrorKind::DivergedBeforeCommitted(
+                        prev_log_index,
+                        committed.index
+                    ),
+                    format!(
+                        "Peer {} claimed log index {} does not match, \
+                         but commit index is at {:?}) which is after that.",
+                        peer_index, prev_log_index, committed
+                    ),
+                    &rf
+                );
                 Self::check_committed(&rf, peer, committed.clone());
 
                 let step = &mut rf.current_step[peer_index];
@@ -216,12 +233,15 @@ where
             return;
         }
         let local_term = rf.log.at(committed.index).term;
-        if committed.term != local_term {
-            eprintln!(
+        check_or_record!(
+            committed.term == local_term,
+            ErrorKind::DivergedAtCommitted(committed.index),
+            format!(
                 "{:?} committed log diverged at {:?}: {:?} v.s. leader {:?}",
                 peer, committed.index, committed.term, local_term
-            );
-        }
+            ),
+            rf
+        );
     }
 
     fn build_sync_log_entries(