Просмотр исходного кода

raft: fix a bug related to commits length

ghassmo 4 лет назад
Родитель
Сommit
e7157c4418
2 измененных файлов с 5 добавлено и 6 удалено
  1. 3 2
      src/raft/consensus.rs
  2. 2 4
      src/raft/datastore.rs

+ 3 - 2
src/raft/consensus.rs

@@ -118,8 +118,9 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
             let datastore = DataStore::new(db_path_str)?;
             let datastore = DataStore::new(db_path_str)?;
             current_term = datastore.current_term.get_last()?.unwrap_or(0);
             current_term = datastore.current_term.get_last()?.unwrap_or(0);
             voted_for = datastore.voted_for.get_last()?.flatten();
             voted_for = datastore.voted_for.get_last()?.flatten();
+            // TODO using sled instead of memory
             logs = Logs(datastore.logs.get_all()?);
             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
             datastore
         } else {
         } else {
             DataStore::new(db_path_str)?
             DataStore::new(db_path_str)?
@@ -675,7 +676,7 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
 
 
     fn set_commit_length(&mut self, i: &u64) -> Result<()> {
     fn set_commit_length(&mut self, i: &u64) -> Result<()> {
         self.commit_length = *i;
         self.commit_length = *i;
-        self.datastore.commits_length.insert(i)
+        Ok(())
     }
     }
     fn set_current_term(&mut self, i: &u64) -> Result<()> {
     fn set_current_term(&mut self, i: &u64) -> Result<()> {
         self.current_term = *i;
         self.current_term = *i;

+ 2 - 4
src/raft/datastore.rs

@@ -12,7 +12,7 @@ use super::primitives::{Log, NodeId};
 
 
 const SLED_LOGS_TREE: &[u8] = b"_logs";
 const SLED_LOGS_TREE: &[u8] = b"_logs";
 const SLED_COMMITS_TREE: &[u8] = b"_commits";
 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_VOTED_FOR_TREE: &[u8] = b"_voted_for";
 const SLED_CURRENT_TERM_TREE: &[u8] = b"_current_term";
 const SLED_CURRENT_TERM_TREE: &[u8] = b"_current_term";
 
 
@@ -20,7 +20,6 @@ pub struct DataStore<T> {
     _db: sled::Db,
     _db: sled::Db,
     pub logs: DataTree<Log>,
     pub logs: DataTree<Log>,
     pub commits: DataTree<T>,
     pub commits: DataTree<T>,
-    pub commits_length: DataTree<u64>,
     pub voted_for: DataTree<Option<NodeId>>,
     pub voted_for: DataTree<Option<NodeId>>,
     pub current_term: DataTree<u64>,
     pub current_term: DataTree<u64>,
 }
 }
@@ -30,11 +29,10 @@ impl<T: Encodable + Decodable> DataStore<T> {
         let _db = sled::open(db_path)?;
         let _db = sled::open(db_path)?;
         let logs = DataTree::new(&_db, SLED_LOGS_TREE)?;
         let logs = DataTree::new(&_db, SLED_LOGS_TREE)?;
         let commits = DataTree::new(&_db, SLED_COMMITS_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 voted_for = DataTree::new(&_db, SLED_VOTED_FOR_TREE)?;
         let current_term = DataTree::new(&_db, SLED_CURRENT_TERM_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<()> {
     pub async fn cancel(&self) -> Result<()> {
         debug!(target: "raft", "DataStore flush");
         debug!(target: "raft", "DataStore flush");