Skip to content

Commit

Permalink
make pre-vote enabled by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiaayachi committed Mar 28, 2024
1 parent 921be2d commit 4f6fc13
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
7 changes: 4 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ type Raft struct {
// mainThreadSaturation measures the saturation of the main raft goroutine.
mainThreadSaturation *saturationMetric

// preVote control if the pre-vote feature is activated
preVote bool
// preVoteDisabled control if the pre-vote feature is activated,
// prevote feature is disabled if set to true.
preVoteDisabled bool
}

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

r.conf.Store(*conf)
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ type Config struct {
// raft's configuration and index values.
NoSnapshotRestoreOnStart bool

// PreVote activate the pre-vote feature
PreVote bool
// PreVoteDisabled deactivate the pre-vote feature when set to true
PreVoteDisabled bool

// skipStartup allows NewRaft() to bypass all background work goroutines
skipStartup bool
Expand Down
4 changes: 2 additions & 2 deletions raft-compat/prevote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestRaft_PreVote_BootStrap_PreVote(t *testing.T) {

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

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

Expand Down
8 changes: 3 additions & 5 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,9 @@ func (r *Raft) runCandidate() {
var voteCh <-chan *voteResult
var prevoteCh <-chan *preVoteResult

// check if pre-vote is active and that this is not a leader transfer

// Leader transfer do not perform prevote by design, as the selected server is very likely to be fit
// and an election will happen in all cases.
if r.preVote && !r.candidateFromLeadershipTransfer.Load() {
// check if pre-vote is active and that this is not a leader transfer.
// Leader transfer do not perform prevote by design
if !r.preVoteDisabled && !r.candidateFromLeadershipTransfer.Load() {
prevoteCh = r.preElectSelf()
} else {
voteCh = r.electSelf()
Expand Down
6 changes: 3 additions & 3 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2091,13 +2091,13 @@ func TestRaft_PreVoteMixedCluster(t *testing.T) {
}

conf := inmemConfig(t)
conf.PreVote = tc.prevoteNum > tc.noprevoteNum
conf.PreVoteDisabled = tc.prevoteNum <= tc.noprevoteNum
c := MakeCluster(majority, t, conf)
defer c.Close()

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

// Merge clusters.
Expand Down Expand Up @@ -2126,7 +2126,7 @@ func TestRaft_PreVoteMixedCluster(t *testing.T) {
func TestRaft_PreVoteAvoidElectionWithPartition(t *testing.T) {
// Make a prevote cluster.
conf := inmemConfig(t)
conf.PreVote = true
conf.PreVoteDisabled = false
c := MakeCluster(5, t, conf)
defer c.Close()

Expand Down

0 comments on commit 4f6fc13

Please sign in to comment.