Skip to content

Commit

Permalink
Bug fix: fix the Round-Robin algorithm when "ReadMode == ReplicaReadM…
Browse files Browse the repository at this point in the history
…ixed". (#663)

Signed-off-by: Lucasliang <nkcs_lykx@hotmail.com>
  • Loading branch information
LykxSassinator authored Jan 11, 2023
1 parent f313ddf commit c598334
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,16 @@ type accessFollower struct {
}

func (state *accessFollower) next(bo *retry.Backoffer, selector *replicaSelector) (*RPCContext, error) {
replicaSize := len(selector.replicas)
if state.lastIdx < 0 {
if state.tryLeader {
state.lastIdx = AccessIndex(rand.Intn(len(selector.replicas)))
state.lastIdx = AccessIndex(rand.Intn(replicaSize))
} else {
if len(selector.replicas) <= 1 {
if replicaSize <= 1 {
state.lastIdx = state.leaderIdx
} else {
// Randomly select a non-leader peer
state.lastIdx = AccessIndex(rand.Intn(len(selector.replicas) - 1))
state.lastIdx = AccessIndex(rand.Intn(replicaSize - 1))
if state.lastIdx >= state.leaderIdx {
state.lastIdx++
}
Expand All @@ -547,8 +548,13 @@ func (state *accessFollower) next(bo *retry.Backoffer, selector *replicaSelector
state.lastIdx++
}

for i := 0; i < len(selector.replicas) && !state.option.leaderOnly; i++ {
idx := AccessIndex((int(state.lastIdx) + i) % len(selector.replicas))
for i := 0; i < replicaSize && !state.option.leaderOnly; i++ {
idx := AccessIndex((int(state.lastIdx) + i) % replicaSize)
// If the given store is abnormal to be accessed under `ReplicaReadMixed` mode, we should choose other followers or leader
// as candidates to serve the Read request. Meanwhile, we should make the choice of next() meet Uniform Distribution.
for cnt := 0; cnt < replicaSize && !state.isCandidate(idx, selector.replicas[idx]); cnt++ {
idx = AccessIndex((int(idx) + rand.Intn(replicaSize)) % replicaSize)
}
if state.isCandidate(idx, selector.replicas[idx]) {
state.lastIdx = idx
selector.targetIdx = idx
Expand Down

0 comments on commit c598334

Please sign in to comment.