Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Raft] Add prevote to make it better #3322

Merged
merged 9 commits into from
Nov 25, 2021
Merged

Conversation

critical27
Copy link
Contributor

@critical27 critical27 commented Nov 16, 2021

Reference

See $9.6 in https://web.stanford.edu/~ouster/cgi-bin/papers/OngaroPhD.pdf, my implementation is almost same in https://www.openlife.cc/sites/default/files/4-modifications-for-Raft-consensus.pdf

Change notes:

  • Prevote is used to prevent some unstable case when a network separated node rejoin (see the reference for details). Use current_term + 1 to start a prevote, if I get majority votes, then start a formal election with ++current_term (which is the same term in prevote). I believe most of the election logic is same as paper after this PR. Many scenario when we failed to get a leader will be eliminated.
  • Some wal_->rollbackToLog(lastLogId_); is added, because if term changed during sending logs, the lastLogId_ won't be updated. However, since we have write some logs into wal, if we don't rollback those logs, we can't write any logs anymore because the logId inconsistency in wal.

Close #3108

@kikimo and I will do some test first, but ready to review.

@darionyaphet
Copy link
Contributor

please fix the conflict thanks ~


// The method returns the partition's role after the election
Role processElectionResponses(const ElectionResponses& results,
// Return true if I have been granted majority votes on proposedTerm, no matter isPreVote or not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments of this method is exact the same as the above one...

if (needToStartElection()) {
if (leaderElection().get()) {
VLOG(2) << idStr_ << "Stop the election";
if (leaderElection(true).get() && leaderElection(false).get()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call leaderElection twice in a row 👍

Copy link
Contributor

@darionyaphet darionyaphet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is looks simpler

@critical27 critical27 added the ready-for-testing PR: ready for the CI test label Nov 24, 2021
@critical27
Copy link
Contributor Author

Wait @kikimo to do a final test.

Copy link
Contributor

@kikimo kikimo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM , it's time to merge and I'll retest all these raft PRs later.

@critical27 critical27 merged commit c6d1046 into vesoft-inc:master Nov 25, 2021
@critical27 critical27 deleted the prevote branch November 25, 2021 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-testing PR: ready for the CI test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement raft prevote to prevent unnecessary leader election/change.
4 participants