Skip to content

Commit

Permalink
fix: when handle_update_match_index(), non-voter should also be consi…
Browse files Browse the repository at this point in the history
…dered, because when member change a non-voter is also count as a quorum member
  • Loading branch information
drmingdrmer committed Jun 16, 2021
1 parent d882e74 commit a10d990
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
30 changes: 18 additions & 12 deletions async-raft/src/core/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,33 +138,39 @@ impl<'a, D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>
}

/// Handle events from a replication stream which updates the target node's match index.
#[tracing::instrument(level = "trace", skip(self, target, match_index))]
#[tracing::instrument(level = "trace", skip(self))]
async fn handle_update_match_index(
&mut self,
target: NodeId,
match_index: u64,
match_term: u64,
) -> RaftResult<()> {
// If this is a non-voter, then update and return.
let mut found = false;

if let Some(state) = self.non_voters.get_mut(&target) {
state.state.match_index = match_index;
state.state.match_term = match_term;
return Ok(());
found = true;
}

// Update target's match index & check if it is awaiting removal.
let mut needs_removal = false;
match self.nodes.get_mut(&target) {
Some(state) => {
state.match_index = match_index;
state.match_term = match_term;
if let Some(threshold) = &state.remove_after_commit {
if &match_index >= threshold {
needs_removal = true;
}

if let Some(state) = self.nodes.get_mut(&target) {
state.match_index = match_index;
state.match_term = match_term;
found = true;

if let Some(threshold) = &state.remove_after_commit {
if &match_index >= threshold {
needs_removal = true;
}
}
_ => return Ok(()), // Node not found.
}

if !found {
// no such node
return Ok(());
}

// Drop replication stream if needed.
Expand Down
5 changes: 4 additions & 1 deletion async-raft/src/replication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,12 @@ impl<D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>>
.map(|last| (last.as_ref().index, last.as_ref().term));
self.outbound_buffer.clear(); // Once we've successfully sent a payload of entries, don't send them again.

tracing::debug!("append_entries last: {:?}", last_index_and_term);

// Handle success conditions.
if res.success {
tracing::trace!("append entries succeeded");
tracing::debug!("append entries succeeded to {:?}", last_index_and_term);

// If this was a proper replication event (last index & term were provided), then update state.
if let Some((index, term)) = last_index_and_term {
self.next_index = index + 1; // This should always be the next expected index.
Expand Down

0 comments on commit a10d990

Please sign in to comment.