mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: Avoid scanning raft log in becomeLeader
Scanning the uncommitted portion of the raft log to determine whether there are any pending config changes can be expensive. In cockroachdb/cockroach#18601, we've seen that a new leader can spend so much time scanning its log post-election that it fails to send its first heartbeats in time to prevent a second election from starting immediately. Instead of tracking whether a pending config change exists with a boolean, this commit tracks the latest log index at which a pending config change *could* exist. This is a less expensive solution to the problem, and the impact of false positives should be minimal since a newly-elected leader should be able to quickly commit the tail of its log.
This commit is contained in:
@@ -348,6 +348,7 @@ func TestNodeProposeAddDuplicateNode(t *testing.T) {
|
||||
n.Tick()
|
||||
case rd := <-n.Ready():
|
||||
s.Append(rd.Entries)
|
||||
applied := false
|
||||
for _, e := range rd.Entries {
|
||||
rdyEntries = append(rdyEntries, e)
|
||||
switch e.Type {
|
||||
@@ -356,10 +357,13 @@ func TestNodeProposeAddDuplicateNode(t *testing.T) {
|
||||
var cc raftpb.ConfChange
|
||||
cc.Unmarshal(e.Data)
|
||||
n.ApplyConfChange(cc)
|
||||
applyConfChan <- struct{}{}
|
||||
applied = true
|
||||
}
|
||||
}
|
||||
n.Advance()
|
||||
if applied {
|
||||
applyConfChan <- struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user