From e7157c441822eb47d3673c244c2f2fab457aa4da Mon Sep 17 00:00:00 2001 From: ghassmo Date: Mon, 2 May 2022 16:28:15 +0300 Subject: [PATCH] raft: fix a bug related to commits length --- src/raft/consensus.rs | 5 +++-- src/raft/datastore.rs | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/raft/consensus.rs b/src/raft/consensus.rs index 227b2d613..c7ead0d33 100644 --- a/src/raft/consensus.rs +++ b/src/raft/consensus.rs @@ -118,8 +118,9 @@ impl Raft { let datastore = DataStore::new(db_path_str)?; current_term = datastore.current_term.get_last()?.unwrap_or(0); voted_for = datastore.voted_for.get_last()?.flatten(); + // TODO using sled instead of memory logs = Logs(datastore.logs.get_all()?); - commit_length = datastore.commits_length.get_last()?.unwrap_or(0); + commit_length = datastore.commits.get_all()?.len() as u64; datastore } else { DataStore::new(db_path_str)? @@ -675,7 +676,7 @@ impl Raft { fn set_commit_length(&mut self, i: &u64) -> Result<()> { self.commit_length = *i; - self.datastore.commits_length.insert(i) + Ok(()) } fn set_current_term(&mut self, i: &u64) -> Result<()> { self.current_term = *i; diff --git a/src/raft/datastore.rs b/src/raft/datastore.rs index 443260d03..beb789160 100644 --- a/src/raft/datastore.rs +++ b/src/raft/datastore.rs @@ -12,7 +12,7 @@ use super::primitives::{Log, NodeId}; const SLED_LOGS_TREE: &[u8] = b"_logs"; const SLED_COMMITS_TREE: &[u8] = b"_commits"; -const SLED_COMMITS_LENGTH_TREE: &[u8] = b"_commit_length"; +const _SLED_COMMITS_LENGTH_TREE: &[u8] = b"_commit_length"; const SLED_VOTED_FOR_TREE: &[u8] = b"_voted_for"; const SLED_CURRENT_TERM_TREE: &[u8] = b"_current_term"; @@ -20,7 +20,6 @@ pub struct DataStore { _db: sled::Db, pub logs: DataTree, pub commits: DataTree, - pub commits_length: DataTree, pub voted_for: DataTree>, pub current_term: DataTree, } @@ -30,11 +29,10 @@ impl DataStore { let _db = sled::open(db_path)?; let logs = DataTree::new(&_db, SLED_LOGS_TREE)?; let commits = DataTree::new(&_db, SLED_COMMITS_TREE)?; - let commits_length = DataTree::new(&_db, SLED_COMMITS_LENGTH_TREE)?; let voted_for = DataTree::new(&_db, SLED_VOTED_FOR_TREE)?; let current_term = DataTree::new(&_db, SLED_CURRENT_TERM_TREE)?; - Ok(Self { _db, logs, commits, commits_length, voted_for, current_term }) + Ok(Self { _db, logs, commits, voted_for, current_term }) } pub async fn cancel(&self) -> Result<()> { debug!(target: "raft", "DataStore flush");