Skip to content

Commit

Permalink
fix issue replica selector v2 not compatible with v1 in mixed mode
Browse files Browse the repository at this point in the history
Signed-off-by: crazycs520 <[email protected]>
  • Loading branch information
crazycs520 committed Apr 7, 2024
1 parent abc9bb2 commit 6209394
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
20 changes: 12 additions & 8 deletions internal/locate/replica_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func newReplicaSelectorV2(
for _, op := range opts {
op(&option)
}
if req.ReplicaReadType == kv.ReplicaReadPreferLeader {
WithPerferLeader()(&option)
}
return &replicaSelectorV2{
baseReplicaSelector: baseReplicaSelector{
regionCache: regionCache,
Expand Down Expand Up @@ -166,17 +169,10 @@ func (s *replicaSelectorV2) nextForReplicaReadMixed(req *tikvrpc.Request) {
return
}
}
preferLeader := req.ReplicaReadType == kv.ReplicaReadPreferLeader
if s.attempts > 1 {
if req.ReplicaReadType == kv.ReplicaReadMixed {
// For mixed read retry, prefer retry leader first.
preferLeader = true
}
}
strategy := ReplicaSelectMixedStrategy{
leaderIdx: leaderIdx,
tryLeader: req.ReplicaReadType == kv.ReplicaReadMixed || req.ReplicaReadType == kv.ReplicaReadPreferLeader,
preferLeader: preferLeader,
preferLeader: s.option.preferLeader,
leaderOnly: s.option.leaderOnly,
learnerOnly: req.ReplicaReadType == kv.ReplicaReadLearner,
labels: s.option.labels,
Expand All @@ -201,6 +197,14 @@ func (s *replicaSelectorV2) nextForReplicaReadMixed(req *tikvrpc.Request) {
req.StaleRead = false
req.ReplicaRead = s.isReadOnlyReq
}
// Monitor the flows destination if selector is under `ReplicaReadPreferLeader` mode.
if s.option.preferLeader {
if s.target.peer.Id != s.region.GetLeaderPeerID() {
s.target.store.recordReplicaFlowsStats(toFollower)
} else {
s.target.store.recordReplicaFlowsStats(toLeader)
}
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions internal/locate/replica_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,28 @@ func TestReplicaReadAccessPathByCase(t *testing.T) {
},
}
s.True(s.runCaseAndCompare(ca))

s.changeRegionLeader(3)
ca = replicaSelectorAccessPathCase{
reqType: tikvrpc.CmdGet,
readType: kv.ReplicaReadMixed,
staleRead: false,
accessErr: []RegionErrorType{ServerIsBusyErr, ServerIsBusyErr, ServerIsBusyErr},
expect: &accessPathResult{
accessPath: []string{
"{addr: store1, replica-read: true, stale-read: false}",
"{addr: store2, replica-read: true, stale-read: false}",
"{addr: store3, replica-read: true, stale-read: false}",
},
respErr: "",
respRegionError: fakeEpochNotMatch,
backoffCnt: 1,
backoffDetail: []string{"tikvServerBusy+1"},
regionIsValid: false,
},
}
s.True(s.runCaseAndCompare(ca))
s.changeRegionLeader(1)
}

func TestReplicaReadAccessPathByCase2(t *testing.T) {
Expand Down

0 comments on commit 6209394

Please sign in to comment.