Browse Source

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.
Tobias Schottdorf 6 years ago
parent
commit
57a1b39fcd
2 changed files with 8 additions and 2 deletions
  1. 6 0
      raft/progress.go
  2. 2 2
      raft/raft.go

+ 6 - 0
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
 }

+ 2 - 2
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