Skip to content

Commit

Permalink
change: VoteRequest: use last_log_id:LogId to replace last_log_term a…
Browse files Browse the repository at this point in the history
…nd last_log_index
  • Loading branch information
drmingdrmer committed Sep 9, 2021
1 parent deda6d7 commit 734eec6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion async-raft/src/core/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ impl<D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>> Ra
self.save_hard_state().await?;
}

// TODO: bug: (2,1), (1,2)
// Check if candidate's log is at least as up-to-date as this node's.
// If candidate's log is not at least as up-to-date as this node, then reject.
let client_is_uptodate =
(msg.last_log_term >= self.last_log_id.term) && (msg.last_log_index >= self.last_log_id.index);
(msg.last_log_id.term >= self.last_log_id.term) && (msg.last_log_id.index >= self.last_log_id.index);
if !client_is_uptodate {
tracing::debug!(
{ candidate = msg.candidate_id },
Expand Down
12 changes: 6 additions & 6 deletions async-raft/src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,8 @@ pub struct VoteRequest {
pub term: u64,
/// The candidate's ID.
pub candidate_id: u64,
/// The index of the candidate’s last log entry (§5.4).
pub last_log_index: u64,
/// The term of the candidate’s last log entry (§5.4).
pub last_log_term: u64,

pub last_log_id: LogId,
}

impl MessageSummary for VoteRequest {
Expand All @@ -666,8 +664,10 @@ impl VoteRequest {
Self {
term,
candidate_id,
last_log_index,
last_log_term,
last_log_id: LogId {
term: last_log_term,
index: last_log_index,
},
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions async-raft/tests/leader_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,19 @@ async fn leader_metrics() -> Result<()> {
.send_vote(0, VoteRequest {
term: 100,
candidate_id: 100,
last_log_index: 100,
last_log_term: 10,
last_log_id: LogId { term: 10, index: 100 },
})
.await?;

router.wait_for_state(&btreeset![0], State::Candidate, timeout, "node 0 to candidate").await?;
// The next election may have finished before waiting.
router
.wait_for_metrics(
&0,
|x| x.state == State::Candidate || (x.state == State::Leader && x.current_term == 101),
timeout,
"node 0 becomes candidate or becomes a new leader",
)
.await?;

router
.wait_for_metrics(
Expand Down

0 comments on commit 734eec6

Please sign in to comment.