Skip to content

Commit

Permalink
*: handle region error for GetMvccByEncodedKey API(#47811) (#47808)
Browse files Browse the repository at this point in the history
* add backoff and region error check for GetMvccByEncodedKey API

Signed-off-by: crazycs520 <crazycs520@gmail.com>

* add test

Signed-off-by: crazycs520 <crazycs520@gmail.com>

* refine

Signed-off-by: crazycs520 <crazycs520@gmail.com>

* refine test

Signed-off-by: crazycs520 <crazycs520@gmail.com>

* update client-go

Signed-off-by: crazycs520 <crazycs520@gmail.com>

* make bazel_prepare

Signed-off-by: crazycs520 <crazycs520@gmail.com>

---------

Signed-off-by: crazycs520 <crazycs520@gmail.com>
  • Loading branch information
crazycs520 authored Oct 20, 2023
1 parent 095b1e4 commit d8fb9d4
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 20 deletions.
4 changes: 2 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3703,8 +3703,8 @@ def go_deps():
name = "com_github_tikv_client_go_v2",
build_file_proto_mode = "disable_global",
importpath = "github.com/tikv/client-go/v2",
sum = "h1:LnjpGVXmlHM/isD1m34+gYn0CTynN+BiSt3DkjjtWwo=",
version = "v2.0.4-0.20231012015822-35e4902b28af",
sum = "h1:x4x8yO7tHtxOepMGpPnnLMVw2geDsrYbbbB7k+0zusY=",
version = "v2.0.4-0.20231020030327-4ecf7c282e37",
)
go_repository(
name = "com_github_tikv_pd_client",
Expand Down
30 changes: 30 additions & 0 deletions executor/executor_failpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/copr"
"github.com/pingcap/tidb/store/helper"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/deadlockhistory"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -622,3 +624,31 @@ func TestTiKVClientReadTimeout(t *testing.T) {
explain = fmt.Sprintf("%v", rows[0])
require.Regexp(t, ".*TableReader.* root time:.*, loops:.* cop_task: {num: 1, .* rpc_num: 2.*", explain)
}

func TestGetMvccByEncodedKeyRegionError(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
h := helper.NewHelper(store.(helper.Storage))
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
schemaVersion := tk.Session().GetDomainInfoSchema().SchemaMetaVersion()
key := m.EncodeSchemaDiffKey(schemaVersion)

resp, err := h.GetMvccByEncodedKey(key)
require.NoError(t, err)
require.NotNil(t, resp.Info)
require.Equal(t, 1, len(resp.Info.Writes))
require.Less(t, uint64(0), resp.Info.Writes[0].CommitTs)
commitTs := resp.Info.Writes[0].CommitTs

require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/store/mockstore/unistore/epochNotMatch", "1*return(true)"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/store/mockstore/unistore/epochNotMatch"))
}()
resp, err = h.GetMvccByEncodedKey(key)
require.NoError(t, err)
require.NotNil(t, resp.Info)
require.Equal(t, 1, len(resp.Info.Writes))
require.Equal(t, commitTs, resp.Info.Writes[0].CommitTs)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ require (
github.com/stretchr/testify v1.8.0
github.com/tdakkota/asciicheck v0.1.1
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.4-0.20231012015822-35e4902b28af
github.com/tikv/client-go/v2 v2.0.4-0.20231020030327-4ecf7c282e37
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
github.com/twmb/murmur3 v1.1.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJfDRtkanvQPiooDH8HvJ2FBh+iKT/OmiQQ=
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU=
github.com/tikv/client-go/v2 v2.0.4-0.20231012015822-35e4902b28af h1:LnjpGVXmlHM/isD1m34+gYn0CTynN+BiSt3DkjjtWwo=
github.com/tikv/client-go/v2 v2.0.4-0.20231012015822-35e4902b28af/go.mod h1:mmVCLP2OqWvQJPOIevQPZvGphzh/oq9vv8J5LDfpadQ=
github.com/tikv/client-go/v2 v2.0.4-0.20231020030327-4ecf7c282e37 h1:x4x8yO7tHtxOepMGpPnnLMVw2geDsrYbbbB7k+0zusY=
github.com/tikv/client-go/v2 v2.0.4-0.20231020030327-4ecf7c282e37/go.mod h1:mmVCLP2OqWvQJPOIevQPZvGphzh/oq9vv8J5LDfpadQ=
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07 h1:ckPpxKcl75mO2N6a4cJXiZH43hvcHPpqc9dh1TmH1nc=
github.com/tikv/pd/client v0.0.0-20221031025758-80f0d8ca4d07/go.mod h1:CipBxPfxPUME+BImx9MUYXCnAVLS3VJUr3mnSJwh40A=
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144 h1:kl4KhGNsJIbDHS9/4U9yQo1UcPQM0kOMJHn29EoH/Ro=
Expand Down
53 changes: 38 additions & 15 deletions store/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,23 +96,46 @@ func NewHelper(store Storage) *Helper {

// GetMvccByEncodedKey get the MVCC value by the specific encoded key.
func (h *Helper) GetMvccByEncodedKey(encodedKey kv.Key) (*kvrpcpb.MvccGetByKeyResponse, error) {
keyLocation, err := h.RegionCache.LocateKey(tikv.NewBackofferWithVars(context.Background(), 500, nil), encodedKey)
if err != nil {
return nil, derr.ToTiDBErr(err)
}

bo := tikv.NewBackofferWithVars(context.Background(), 5000, nil)
tikvReq := tikvrpc.NewRequest(tikvrpc.CmdMvccGetByKey, &kvrpcpb.MvccGetByKeyRequest{Key: encodedKey})
kvResp, err := h.Store.SendReq(tikv.NewBackofferWithVars(context.Background(), 500, nil), tikvReq, keyLocation.Region, time.Minute)
if err != nil {
logutil.BgLogger().Info("get MVCC by encoded key failed",
zap.Stringer("encodeKey", encodedKey),
zap.Reflect("region", keyLocation.Region),
zap.Stringer("keyLocation", keyLocation),
zap.Reflect("kvResp", kvResp),
zap.Error(err))
return nil, errors.Trace(err)
for {
keyLocation, err := h.RegionCache.LocateKey(bo, encodedKey)
if err != nil {
return nil, derr.ToTiDBErr(err)
}

kvResp, err := h.Store.SendReq(bo, tikvReq, keyLocation.Region, time.Minute)
if err != nil {
logutil.BgLogger().Info("get MVCC by encoded key failed",
zap.Stringer("encodeKey", encodedKey),
zap.Reflect("region", keyLocation.Region),
zap.Stringer("keyLocation", keyLocation),
zap.Reflect("kvResp", kvResp),
zap.Error(err))
return nil, errors.Trace(err)
}
regionErr, err := kvResp.GetRegionError()
if err != nil {
return nil, errors.Trace(err)
}
if regionErr != nil {
if err = bo.Backoff(tikv.BoRegionMiss(), errors.New(regionErr.String())); err != nil {
return nil, err
}
continue
}
mvccResp := kvResp.Resp.(*kvrpcpb.MvccGetByKeyResponse)
if errMsg := mvccResp.GetError(); errMsg != "" {
logutil.BgLogger().Info("get MVCC by encoded key failed",
zap.Stringer("encodeKey", encodedKey),
zap.Reflect("region", keyLocation.Region),
zap.Stringer("keyLocation", keyLocation),
zap.Reflect("kvResp", kvResp),
zap.String("error", errMsg))
return nil, errors.New(errMsg)
}
return mvccResp, nil
}
return kvResp.Resp.(*kvrpcpb.MvccGetByKeyResponse), nil
}

// MvccKV wraps the key's mvcc info in tikv.
Expand Down
5 changes: 5 additions & 0 deletions store/mockstore/unistore/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (c *RPCClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.R
failpoint.Return(tikvrpc.GenRegionErrorResp(req, &errorpb.Error{ServerIsBusy: &errorpb.ServerIsBusy{}}))
}
})
failpoint.Inject("epochNotMatch", func(val failpoint.Value) {
if val.(bool) {
failpoint.Return(tikvrpc.GenRegionErrorResp(req, &errorpb.Error{EpochNotMatch: &errorpb.EpochNotMatch{}}))
}
})

failpoint.Inject("unistoreRPCClientSendHook", func(val failpoint.Value) {
if val.(bool) && UnistoreRPCClientSendHook != nil {
Expand Down

0 comments on commit d8fb9d4

Please sign in to comment.