Skip to content

Commit

Permalink
refactor: MemStore by default turns on defensive check
Browse files Browse the repository at this point in the history
  • Loading branch information
drmingdrmer committed Aug 28, 2021
1 parent 420cdd7 commit a29b0e5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions async-raft/src/core/append_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl<D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>> Ra
}

// Update election timeout.
// TODO(xp): only update commit_index if the log present. e.g., append entries first, then update commit_index.
self.update_next_election_timeout(true);
let mut report_metrics = false;
self.commit_index = msg.leader_commit; // The value for `self.commit_index` is only updated here when not the leader.
Expand Down
13 changes: 9 additions & 4 deletions memstore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl MemStore {
let hs = RwLock::new(None);
let current_snapshot = RwLock::new(None);
Self {
defensive: RwLock::new(false),
defensive: RwLock::new(true),
id,
log,
sm,
Expand All @@ -134,7 +134,7 @@ impl MemStore {
let hs = RwLock::new(hs);
let current_snapshot = RwLock::new(current_snapshot);
Self {
defensive: RwLock::new(false),
defensive: RwLock::new(true),
id,
log,
sm,
Expand Down Expand Up @@ -179,8 +179,13 @@ impl MemStore {
return Err(anyhow::anyhow!("smaller term is now allowed"));
}

if hs.current_term == curr.current_term && hs.voted_for != curr.voted_for {
return Err(anyhow::anyhow!("voted_for can not change in one term"));
if hs.current_term == curr.current_term && curr.voted_for.is_some() && hs.voted_for != curr.voted_for {
return Err(anyhow::anyhow!(
"voted_for can not change in one term({}) curr: {:?} change to {:?}",
hs.current_term,
curr.voted_for,
hs.voted_for
));
}

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion memstore/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ struct MemStoreBuilder {}
#[async_trait]
impl StoreBuilder<ClientRequest, ClientResponse, MemStore> for MemStoreBuilder {
async fn new_store(&self, id: NodeId) -> MemStore {
MemStore::new(id)
let sto = MemStore::new(id);
sto.defensive(false).await;
sto
}
}

Expand Down

0 comments on commit a29b0e5

Please sign in to comment.