From 20422c5b4dabd7c0f09fa1e56ad9a60db6aa0d7d Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Tue, 26 Jun 2018 15:29:51 -0400 Subject: [PATCH] raft: Really avoid scanning raft log in becomeLeader I meant to do this in #9073, but sent the PR before it was finished. The last log index is known directly; there is no need to fetch any entries here. --- raft/raft.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/raft/raft.go b/raft/raft.go index 468776c47c2..0c8c96c3f80 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -694,19 +694,13 @@ func (r *raft) becomeLeader() { r.tick = r.tickHeartbeat r.lead = r.id r.state = StateLeader - ents, err := r.raftLog.entries(r.raftLog.committed+1, noLimit) - if err != nil { - r.logger.Panicf("unexpected error getting uncommitted entries (%v)", err) - } // Conservatively set the pendingConfIndex to the last index in the // log. There may or may not be a pending config change, but it's // safe to delay any future proposals until we commit all our // pending log entries, and scanning the entire tail of the log // could be expensive. - if len(ents) > 0 { - r.pendingConfIndex = ents[len(ents)-1].Index - } + r.pendingConfIndex = r.raftLog.lastIndex() r.appendEntry(pb.Entry{Data: nil}) r.logger.Infof("%x became leader at term %d", r.id, r.Term)