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.
This commit is contained in:
Tobias Schottdorf 2019-04-26 15:24:05 +02:00
parent bc828e939a
commit 57a1b39fcd
2 changed files with 8 additions and 2 deletions

View File

@ -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
}

View File

@ -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