raft: fix a bug related to commits length

This commit is contained in:
ghassmo
2022-05-02 16:28:15 +03:00
parent 8cdd47bf1b
commit e7157c4418
2 changed files with 5 additions and 6 deletions

View File

@@ -118,8 +118,9 @@ impl<T: Decodable + Encodable + Clone> Raft<T> {
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<T: Decodable + Encodable + Clone> Raft<T> {
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;

View File

@@ -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<T> {
_db: sled::Db,
pub logs: DataTree<Log>,
pub commits: DataTree<T>,
pub commits_length: DataTree<u64>,
pub voted_for: DataTree<Option<NodeId>>,
pub current_term: DataTree<u64>,
}
@@ -30,11 +29,10 @@ impl<T: Encodable + Decodable> DataStore<T> {
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");