Skip to content

Commit

Permalink
return the actually committed index if it's greater than the official…
Browse files Browse the repository at this point in the history
… committed index for ReadIndex

Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Sep 30, 2023
1 parent 645ea12 commit aa5e2cb
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ func stepLeader(r *raft, m pb.Message) error {
case pb.MsgReadIndex:
// only one voting member (the leader) in the cluster
if r.prs.IsSingleton() {
if resp := r.responseToReadIndexReq(m, r.raftLog.committed); resp.To != None {
if resp := r.responseToReadIndexReq(m, r.readIndex()); resp.To != None {
r.send(resp)
}
return nil
Expand Down Expand Up @@ -2086,13 +2086,22 @@ func sendMsgReadIndexResponse(r *raft, m pb.Message) {
switch r.readOnly.option {
// If more than the local vote is needed, go through a full broadcast.
case ReadOnlySafe:
r.readOnly.addRequest(r.raftLog.committed, m)
r.readOnly.addRequest(r.readIndex(), m)
// The local node automatically acks the request.
r.readOnly.recvAck(r.id, m.Entries[0].Data)
r.bcastHeartbeatWithCtx(m.Entries[0].Data)
case ReadOnlyLeaseBased:
if resp := r.responseToReadIndexReq(m, r.raftLog.committed); resp.To != None {
if resp := r.responseToReadIndexReq(m, r.readIndex()); resp.To != None {
r.send(resp)
}
}
}

// As long as a log has been replicated to majority members, the log is considered
// as "actually committed", which is `r.prs.Committed()`. When the log is further
// confirmed by the leader, then it's considered as "officially committed", which
// is `r.raftLog.committed`. Usually "officially committed" <= "actually committed".
// We should return the bigger one.
func (r *raft) readIndex() uint64 {
return max(r.raftLog.committed, r.prs.Committed())
}

0 comments on commit aa5e2cb

Please sign in to comment.