Skip to content

Commit

Permalink
Add the stale read retry test
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Jun 2, 2021
1 parent fe99fe4 commit 2d7bd46
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
8 changes: 7 additions & 1 deletion store/tikv/region_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (s *RegionRequestSender) SendReqCtx(

var lastPeerID uint64
if rpcCtx != nil {
lastPeerID = rpcCtx.lastPeerID
lastPeerID = rpcCtx.Peer.GetId()
}
rpcCtx, err = s.getRPCContext(bo, req, regionID, et, opts...)
if err != nil {
Expand Down Expand Up @@ -337,6 +337,12 @@ func (s *RegionRequestSender) SendReqCtx(
if err != nil {
return nil, nil, errors.Trace(err)
}
failpoint.Inject("mockDataIsNotReadyError", func(val failpoint.Value) {
regionErr = &errorpb.Error{}
if tryTimesLimit, ok := val.(int); ok && tryTimes <= tryTimesLimit {
regionErr.DataIsNotReady = &errorpb.DataIsNotReady{}
}
})
if regionErr != nil {
retry, opts, err = s.onRegionError(bo, rpcCtx, req.ReplicaReadSeed, regionErr)
if err != nil {
Expand Down
32 changes: 29 additions & 3 deletions store/tikv/region_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/kvproto/pkg/coprocessor_v2"
"github.com/pingcap/kvproto/pkg/errorpb"
Expand Down Expand Up @@ -218,9 +219,34 @@ func (s *testRegionRequestToThreeStoresSuite) TestGetRPCContextWithExcludedPeerI
c.Assert(rpcCtx.Peer.GetId(), Equals, s.leaderPeer)
}

// TODO: test whether the Stale Read request will retry the leader or next peer on error.
// func (s *testRegionRequestToThreeStoresSuite) TestStaleReadRetry(c *C) {
// }
// Test whether the Stale Read request will retry the leader or next peer on error.
func (s *testRegionRequestToThreeStoresSuite) TestStaleReadRetry(c *C) {
region, err := s.cache.LocateRegionByID(s.bo, s.regionID)
c.Assert(err, IsNil)
c.Assert(region, NotNil)
var seed uint32 = 0
req := tikvrpc.NewReplicaReadRequest(tikvrpc.CmdGet, &kvrpcpb.GetRequest{}, kv.ReplicaReadMixed, &seed)
req.EnableStaleRead()
// Retry 3 times.
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/mockDataIsNotReadyError", `return(3)`), IsNil)
resp, ctx, err := s.regionRequestSender.SendReqCtx(s.bo, req, region.Region, time.Second, tikvrpc.TiKV)
c.Assert(err, IsNil)
c.Assert(resp.Resp, NotNil)
c.Assert(ctx, NotNil)
peerID := ctx.Peer.GetId()

region, err = s.cache.LocateRegionByID(s.bo, s.regionID)
c.Assert(err, IsNil)
c.Assert(region, NotNil)
// Retry 1 time.
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/mockDataIsNotReadyError", `return(1)`), IsNil)
resp, ctx, err = s.regionRequestSender.SendReqCtx(s.bo, req, region.Region, time.Second, tikvrpc.TiKV)
c.Assert(err, IsNil)
c.Assert(resp.Resp, NotNil)
c.Assert(ctx, NotNil)
c.Assert(peerID, Not(Equals), ctx.Peer.GetId())
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/mockDataIsNotReadyError"), IsNil)
}

func (s *testRegionRequestToSingleStoreSuite) TestOnSendFailedWithStoreRestart(c *C) {
req := tikvrpc.NewRequest(tikvrpc.CmdRawPut, &kvrpcpb.RawPutRequest{
Expand Down

0 comments on commit 2d7bd46

Please sign in to comment.