Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unexpected slow query during GC running after stop 1 tikv-server #899

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/locate/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (state *accessFollower) next(bo *retry.Backoffer, selector *replicaSelector
for i := 0; i < len(selector.replicas) && !state.option.leaderOnly; i++ {
idx := AccessIndex((int(state.lastIdx) + i) % len(selector.replicas))
selectReplica := selector.replicas[idx]
if state.isCandidate(idx, selectReplica) {
if state.isCandidate(idx, selectReplica) && selectReplica.store.getLivenessState() != unreachable {
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
state.lastIdx = idx
selector.targetIdx = idx
break
Expand Down
77 changes: 77 additions & 0 deletions internal/locate/region_request3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,80 @@ func (s *testRegionRequestToThreeStoresSuite) TestReplicaReadFallbackToLeaderReg
// after region error returned, the region should be invalidated.
s.False(region.isValid())
}

func (s *testRegionRequestToThreeStoresSuite) TestAccessFollowerAfter1TiKVDown() {
var leaderAddr string
s.regionRequestSender.client = &fnClient{fn: func(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (response *tikvrpc.Response, err error) {
// Returns error when accesses non-leader.
if leaderAddr != addr {
return nil, context.DeadlineExceeded
}
return &tikvrpc.Response{Resp: &kvrpcpb.GetResponse{
Value: []byte("value"),
}}, nil
}}

req := tikvrpc.NewRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{
Key: []byte("key"),
})
req.ReplicaReadType = kv.ReplicaReadMixed

loc, err := s.cache.LocateKey(s.bo, []byte("key"))
s.Nil(err)
region := s.cache.GetCachedRegionWithRLock(loc.Region)
s.NotNil(region)
regionStore := region.getStore()
leaderAddr = regionStore.stores[regionStore.workTiKVIdx].addr
s.NotEqual(leaderAddr, "")
for i := 0; i < 10; i++ {
bo := retry.NewBackofferWithVars(context.Background(), 100, nil)
resp, _, err := s.regionRequestSender.SendReqCtx(bo, req, loc.Region, time.Second, tikvrpc.TiKV)
s.Nil(err)
s.NotNil(resp)

// Since send req to follower will receive error, then all follower will be marked as unreachable and epoch stale.
allFollowerStoreEpochStale := true
for i, store := range regionStore.stores {
if i == int(regionStore.workTiKVIdx) {
continue
}
if store.epoch == regionStore.storeEpochs[i] {
allFollowerStoreEpochStale = false
break
} else {
s.Equal(store.getLivenessState(), unreachable)
}
}
if allFollowerStoreEpochStale {
break
}
}

// mock for GC leader reload all regions.
bo := retry.NewBackofferWithVars(context.Background(), 10, nil)
_, err = s.cache.BatchLoadRegionsWithKeyRange(bo, []byte(""), nil, 1)
s.Nil(err)

loc, err = s.cache.LocateKey(s.bo, []byte("key"))
s.Nil(err)
region = s.cache.GetCachedRegionWithRLock(loc.Region)
s.NotNil(region)
regionStore = region.getStore()
for i, store := range regionStore.stores {
if i == int(regionStore.workTiKVIdx) {
continue
}
// After reload region, the region epoch will be updated, but the store liveness state is still unreachable.
s.Equal(store.epoch, regionStore.storeEpochs[i])
s.Equal(store.getLivenessState(), unreachable)
}

for i := 0; i < 100; i++ {
bo := retry.NewBackofferWithVars(context.Background(), 1, nil)
resp, _, err := s.regionRequestSender.SendReqCtx(bo, req, loc.Region, time.Second, tikvrpc.TiKV)
s.Nil(err)
s.NotNil(resp)
// since all follower'store is unreachable, the request will be sent to leader, the backoff times should be 0.
s.Equal(0, bo.GetTotalBackoffTimes())
}
}