Skip to content

Commit 4f6fc13

Browse files
committed
make pre-vote enabled by default
1 parent 921be2d commit 4f6fc13

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

api.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,9 @@ type Raft struct {
214214
// mainThreadSaturation measures the saturation of the main raft goroutine.
215215
mainThreadSaturation *saturationMetric
216216

217-
// preVote control if the pre-vote feature is activated
218-
preVote bool
217+
// preVoteDisabled control if the pre-vote feature is activated,
218+
// prevote feature is disabled if set to true.
219+
preVoteDisabled bool
219220
}
220221

221222
// BootstrapCluster initializes a server's storage with the given cluster
@@ -564,7 +565,7 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna
564565
leaderNotifyCh: make(chan struct{}, 1),
565566
followerNotifyCh: make(chan struct{}, 1),
566567
mainThreadSaturation: newSaturationMetric([]string{"raft", "thread", "main", "saturation"}, 1*time.Second),
567-
preVote: conf.PreVote && transportSupportPreVote,
568+
preVoteDisabled: conf.PreVoteDisabled || !transportSupportPreVote,
568569
}
569570

570571
r.conf.Store(*conf)

config.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ type Config struct {
232232
// raft's configuration and index values.
233233
NoSnapshotRestoreOnStart bool
234234

235-
// PreVote activate the pre-vote feature
236-
PreVote bool
235+
// PreVoteDisabled deactivate the pre-vote feature when set to true
236+
PreVoteDisabled bool
237237

238238
// skipStartup allows NewRaft() to bypass all background work goroutines
239239
skipStartup bool

raft-compat/prevote_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestRaft_PreVote_BootStrap_PreVote(t *testing.T) {
8989

9090
//Create an upgraded node with the store
9191
rUIT := testcluster.InitUITWithStore(t, id, store.(*raftprevious.InmemStore), func(config *raft.Config) {
92-
config.PreVote = tc.preVote
92+
config.PreVoteDisabled = !tc.preVote
9393
})
9494
future := getLeader.GetRaft().(*raftprevious.Raft).AddVoter(raftprevious.ServerID(rUIT.GetLocalID()), raftprevious.ServerAddress(rUIT.GetLocalAddr()), 0, 0)
9595
utils.WaitFuture(t, future)
@@ -124,7 +124,7 @@ func TestRaft_PreVote_BootStrap_PreVote(t *testing.T) {
124124
require.NotEmpty(t, getLeader)
125125

126126
// Create a new node to replace the deleted one
127-
rUIT := testcluster.InitUITWithStore(t, id, store.(*raftprevious.InmemStore), func(config *raft.Config) { config.PreVote = true })
127+
rUIT := testcluster.InitUITWithStore(t, id, store.(*raftprevious.InmemStore), func(config *raft.Config) { config.PreVoteDisabled = false })
128128
fa := getLeader.GetRaft().(*raft.Raft).AddVoter(raft.ServerID(rUIT.GetLocalID()), raft.ServerAddress(rUIT.GetLocalAddr()), 0, 0)
129129
utils.WaitFuture(t, fa)
130130

raft.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,9 @@ func (r *Raft) runCandidate() {
291291
var voteCh <-chan *voteResult
292292
var prevoteCh <-chan *preVoteResult
293293

294-
// check if pre-vote is active and that this is not a leader transfer
295-
296-
// Leader transfer do not perform prevote by design, as the selected server is very likely to be fit
297-
// and an election will happen in all cases.
298-
if r.preVote && !r.candidateFromLeadershipTransfer.Load() {
294+
// check if pre-vote is active and that this is not a leader transfer.
295+
// Leader transfer do not perform prevote by design
296+
if !r.preVoteDisabled && !r.candidateFromLeadershipTransfer.Load() {
299297
prevoteCh = r.preElectSelf()
300298
} else {
301299
voteCh = r.electSelf()

raft_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2091,13 +2091,13 @@ func TestRaft_PreVoteMixedCluster(t *testing.T) {
20912091
}
20922092

20932093
conf := inmemConfig(t)
2094-
conf.PreVote = tc.prevoteNum > tc.noprevoteNum
2094+
conf.PreVoteDisabled = tc.prevoteNum <= tc.noprevoteNum
20952095
c := MakeCluster(majority, t, conf)
20962096
defer c.Close()
20972097

20982098
// Set up another server speaking protocol version 2.
20992099
conf = inmemConfig(t)
2100-
conf.PreVote = tc.prevoteNum < tc.noprevoteNum
2100+
conf.PreVoteDisabled = tc.prevoteNum >= tc.noprevoteNum
21012101
c1 := MakeClusterNoBootstrap(minority, t, conf)
21022102

21032103
// Merge clusters.
@@ -2126,7 +2126,7 @@ func TestRaft_PreVoteMixedCluster(t *testing.T) {
21262126
func TestRaft_PreVoteAvoidElectionWithPartition(t *testing.T) {
21272127
// Make a prevote cluster.
21282128
conf := inmemConfig(t)
2129-
conf.PreVote = true
2129+
conf.PreVoteDisabled = false
21302130
c := MakeCluster(5, t, conf)
21312131
defer c.Close()
21322132

0 commit comments

Comments
 (0)