diff --git a/src/raft.rs b/src/raft.rs index 5af1eca4..1c3e79a6 100644 --- a/src/raft.rs +++ b/src/raft.rs @@ -380,6 +380,9 @@ impl Raft { if raft_state.hard_state != HardState::default() { r.load_state(&raft_state.hard_state); } + if !r.prs.is_empty() && r.term == 0 { + fatal!(r.logger, "Invalid term value: 0"); + } if c.applied > 0 { r.commit_apply(c.applied); } diff --git a/src/storage.rs b/src/storage.rs index ae58f566..b40b1966 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -419,6 +419,8 @@ impl MemStorage { // In practice, we choose the second way by assigning non-zero index to first index. Here // we choose the first way for historical reason and easier to write tests. core.raft_state.conf_state = ConfState::from(conf_state); + // Initialize term with a valid value (anything except 0) + core.raft_state.hard_state.term = 1; } /// Opens up a read lock on the storage and returns a guard handle. Use this diff --git a/src/tracker.rs b/src/tracker.rs index 4814d381..6d22751c 100644 --- a/src/tracker.rs +++ b/src/tracker.rs @@ -240,6 +240,10 @@ impl ProgressTracker { self.votes.clear(); } + pub(crate) fn is_empty(&self) -> bool { + self.progress.is_empty() && self.votes.is_empty() + } + /// Returns true if (and only if) there is only one voting member /// (i.e. the leader) in the current configuration. pub fn is_singleton(&self) -> bool {