From 57a1b39fcd3b02ea63008fb0708f4ea47e2d7a52 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Fri, 26 Apr 2019 15:24:05 +0200 Subject: [PATCH] raft: avoid another call to quorum() This particular caller just wanted to know whether it was in a single-voter cluster configuration, which is now a question prs can answer. --- raft/progress.go | 6 ++++++ raft/raft.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/raft/progress.go b/raft/progress.go index 0df0c2a30..2833730c6 100644 --- a/raft/progress.go +++ b/raft/progress.go @@ -310,6 +310,12 @@ func makePRS(maxInflight int) prs { return p } +// isSingleton returns true if (and only if) there is only one voting member +// (i.e. the leader) in the current configuration. +func (p *prs) isSingleton() bool { + return len(p.nodes) == 1 +} + func (p *prs) quorum() int { return len(p.nodes)/2 + 1 } diff --git a/raft/raft.go b/raft/raft.go index 750d7665c..4c630dfd8 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -988,7 +988,7 @@ func stepLeader(r *raft, m pb.Message) error { r.bcastAppend() return nil case pb.MsgReadIndex: - if r.prs.quorum() > 1 { + if !r.prs.isSingleton() { // more than one voting member in cluster if r.raftLog.zeroTermOnErrCompacted(r.raftLog.term(r.raftLog.committed)) != r.Term { // Reject read only request when this leader has not committed any log entry at its term. return nil @@ -1009,7 +1009,7 @@ func stepLeader(r *raft, m pb.Message) error { r.send(pb.Message{To: m.From, Type: pb.MsgReadIndexResp, Index: ri, Entries: m.Entries}) } } - } else { // there is only one voting member (the leader) in the cluster + } else { // only one voting member (the leader) in the cluster if m.From == None || m.From == r.id { // from leader itself r.readStates = append(r.readStates, ReadState{Index: r.raftLog.committed, RequestCtx: m.Entries[0].Data}) } else { // from learner member