From 1e05975b1558a62cb6f44992bb1a28ced6dc87c2 Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Fri, 8 Mar 2024 15:39:50 +0000 Subject: [PATCH] Delay the next campaign if the node lost the vote Signed-off-by: Benjamin Wang --- raft/raft.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/raft/raft.go b/raft/raft.go index c80262ebaf0..2cee79c4e29 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -1405,6 +1405,17 @@ func stepCandidate(r *raft, m pb.Message) error { // pb.MsgPreVoteResp contains future term of pre-candidate // m.Term > r.Term; reuse r.Term r.becomeFollower(r.Term, None) + // Delay the next campaign if the node lost the vote. If a node lost + // the vote, it's highly likely it will also lose next campaign, so + // it makes more sense to prioritize campaigns by other nodes within + // the current term. Normally the randomized election timeout is in + // range [electiontimeout, 2*electiontimeout - 1], now it changes to + // [2*electiontimeout, 3*electiontimeout - 1]. Note all time parameters, + // including `randomizedElectionTimeout` will be automatically reset + // in next term. + if myVoteRespType == pb.MsgVoteResp { + r.randomizedElectionTimeout += r.electionTimeout + } } case pb.MsgTimeoutNow: r.logger.Debugf("%x [term %d state %v] ignored MsgTimeoutNow from %x", r.id, r.Term, r.state, m.From)