From 5f262198d22b71750c7bb83b32f91d21e54d0e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Fri, 17 Nov 2023 12:37:40 +0800 Subject: [PATCH] Fix: avoid panic with "loosen-follower-log-revert" enabled Problem: When "loosen-follower-log-revert" enabled, and a follower log gets reverted, e.g., all raft-logs are removed from that follower, the leader encounters a panic due to a `debug_assert()`. Solution: - Disable the assertion when this feature is enabled. - The test `feature_loosen_follower_log_revert` is also revised to reveal this bug. --- openraft/src/replication/mod.rs | 17 ++++++++++++++++- .../t60_feature_loosen_follower_log_revert.rs | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/openraft/src/replication/mod.rs b/openraft/src/replication/mod.rs index 7e4fc651a..c020f65cf 100644 --- a/openraft/src/replication/mod.rs +++ b/openraft/src/replication/mod.rs @@ -466,7 +466,22 @@ where func_name!() ); - debug_assert!(self.matching <= new_matching); + if cfg!(feature = "loosen-follower-log-revert") { + if self.matching > new_matching { + tracing::warn!( + "follower log is reverted from {} to {}; with 'loosen-follower-log-revert' enabled, this is allowed", + self.matching.display(), + new_matching.display(), + ); + } + } else { + debug_assert!( + self.matching <= new_matching, + "follower log is reverted from {} to {}", + self.matching.display(), + new_matching.display(), + ); + } self.matching = new_matching; diff --git a/tests/tests/replication/t60_feature_loosen_follower_log_revert.rs b/tests/tests/replication/t60_feature_loosen_follower_log_revert.rs index e5d7e01ff..ab1f8f110 100644 --- a/tests/tests/replication/t60_feature_loosen_follower_log_revert.rs +++ b/tests/tests/replication/t60_feature_loosen_follower_log_revert.rs @@ -17,6 +17,8 @@ async fn feature_loosen_follower_log_revert() -> Result<()> { Config { enable_tick: false, enable_heartbeat: false, + // Make sure the replication is done in more than one steps + max_payload_entries: 1, ..Default::default() } .validate()?,