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

a NonVoter node should never be able to transition to a Candidate state #483

Merged
merged 4 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,6 @@ func hasVote(configuration Configuration, id ServerID) bool {
return false
}

// hasVote returns true if the server identified by 'id' is a Voter in the
// provided Configuration.
func inConfig(configuration Configuration, id ServerID) bool {
for _, server := range configuration.Servers {
if server.ID == id {
return true
}
}
return false
}

// checkConfiguration tests a cluster membership configuration for common
// errors.
func checkConfiguration(configuration Configuration) error {
Expand Down
10 changes: 4 additions & 6 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,13 @@ func (r *Raft) runFollower() {
}
} else {
metrics.IncrCounter([]string{"raft", "transition", "heartbeat_timeout"}, 1)
if inConfig(r.configurations.latest, r.localID) {
if hasVote(r.configurations.latest, r.localID) {
r.logger.Warn("heartbeat timeout reached, starting election", "last-leader", lastLeader)
r.setState(Candidate)
return
} else {
if !didWarn {
r.logger.Warn("heartbeat timeout reached, not part of stable configuration, not triggering a leader election")
didWarn = true
}
} else if !didWarn {
r.logger.Warn("heartbeat timeout reached, not part of a stable configuration or a non-voter, not triggering a leader election")
didWarn = true
}
}

Expand Down
44 changes: 44 additions & 0 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,47 @@ func TestRaft_InstallSnapshot_InvalidPeers(t *testing.T) {
require.Error(t, resp.Error)
require.Contains(t, resp.Error.Error(), "failed to decode peers")
}

func TestRaft_runFollower_State_Transition(t *testing.T) {
type fields struct {
conf *Config
servers []Server
serverID ServerID
}
tests := []struct {
name string
fields fields
expectedState RaftState
}{
{"NonVoter", fields{conf: DefaultConfig(), servers: []Server{{Nonvoter, "first", ""}}, serverID: "first"}, Follower},
{"Voter", fields{conf: DefaultConfig(), servers: []Server{{Voter, "first", ""}}, serverID: "first"}, Candidate},
{"Not in Config", fields{conf: DefaultConfig(), servers: []Server{{Voter, "second", ""}}, serverID: "first"}, Follower},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set timeout to tests specific
tt.fields.conf.LocalID = tt.fields.serverID
tt.fields.conf.HeartbeatTimeout = 50 * time.Millisecond
tt.fields.conf.ElectionTimeout = 50 * time.Millisecond
tt.fields.conf.LeaderLeaseTimeout = 50 * time.Millisecond
tt.fields.conf.CommitTimeout = 5 * time.Millisecond
tt.fields.conf.SnapshotThreshold = 100
tt.fields.conf.TrailingLogs = 10
tt.fields.conf.skipStartup = true

// Create a raft instance and set the latest configuration
env1 := MakeRaft(t, tt.fields.conf, false)
env1.raft.setLatestConfiguration(Configuration{Servers: tt.fields.servers}, 1)
env1.raft.setState(Follower)

// run the follower loop exclusively
go env1.raft.runFollower()

// wait enough time to have HeartbeatTimeout
time.Sleep(tt.fields.conf.HeartbeatTimeout * 3)
Copy link
Member

Choose a reason for hiding this comment

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

I like having a test for this. I'm not sure how reliable this will be over time given that it's concurrent and timing dependent. Perhaps worth re-running this in CI a few times and seeing if it fails? We could increase the multiplier here if it does too I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran it locally few times, I will try on the CI. The wait is deterministic because the follower loop will run after HeartbeatTimeout and there is no concurrency other then the test and the runFollower loop ( because we avoid starting the raft routines) so the state is in a controlled environment.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds low risk then. In the past we've found even "safe" waits like this can eventually end up causing flakes in highly CPU starved CI environments where t.Parallel means that many more tests are running than real CPU cores available and so scheduling delays can last hundreds of milliseconds. If we also avoid running in parallel that probably helps too.

For now it seems OK just wanted to note that based on previous tests that have ended up flaky even 3 * heartbeat might not be enough in the long run!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes in consul we have a lot of those. I will retry the CI a couple of times before merging to see how it hold.


// Check the follower loop set the right state
require.Equal(t, tt.expectedState, env1.raft.getState())
})
}
}