Skip to content

Commit

Permalink
refine calculateScore logic, if the replica already tried, decrease t…
Browse files Browse the repository at this point in the history
…he score

Signed-off-by: crazycs520 <[email protected]>
  • Loading branch information
crazycs520 committed Feb 23, 2024
1 parent 2c141f7 commit 4db2bdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/locate/replica_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ func (s *ReplicaSelectMixedStrategy) calculateScore(r *replica, isLeader bool) i
score += scoreOfNotSlow
//fmt.Printf("store %v is not slow ------\n", r.store.storeID)
}
if score > 0 && r.attempts > 0 {
// if the replica already tried, decrease the score.
score = score - 1
}
return score
}

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

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

ca = replicaSelectorAccessPathCase{
reqType: tikvrpc.CmdGet,
readType: kv.ReplicaReadMixed,
Expand Down Expand Up @@ -2857,6 +2880,19 @@ func TestReplicaSelectorAccessPathByCase(t *testing.T) {
s.True(s.runCaseAndCompare(ca))
}

func (s *testReplicaSelectorSuite) changeRegionLeader(storeId uint64) {
loc, err := s.cache.LocateKey(s.bo, []byte("key"))
s.Nil(err)
rc := s.cache.GetCachedRegionWithRLock(loc.Region)
for _, peer := range rc.meta.Peers {
if peer.StoreId == storeId {
s.cluster.ChangeLeader(rc.meta.Id, peer.Id)
}
}
// Invalidate region cache to reload.
s.cache.InvalidateCachedRegion(loc.Region)
}

func (s *testReplicaSelectorSuite) runCaseAndCompare(ca1 replicaSelectorAccessPathCase) bool {
ca2 := ca1
config.UpdateGlobal(func(conf *config.Config) {
Expand Down

0 comments on commit 4db2bdf

Please sign in to comment.