From 76fab0ede210c4cf210edac2d8d7c30e1e609c0c Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 10 Nov 2023 11:35:12 +0800 Subject: [PATCH] *: handle region error for GetMvccByEncodedKey API (#47811) (#47820) close pingcap/tidb#47807 --- pkg/executor/BUILD.bazel | 1 + pkg/executor/executor_failpoint_test.go | 30 +++++++++++++++++++++++++ pkg/store/mockstore/unistore/rpc.go | 5 +++++ 3 files changed, 36 insertions(+) diff --git a/pkg/executor/BUILD.bazel b/pkg/executor/BUILD.bazel index 42104652fdef0..316d0f5f3b3c9 100644 --- a/pkg/executor/BUILD.bazel +++ b/pkg/executor/BUILD.bazel @@ -388,6 +388,7 @@ go_test( "//pkg/expression/aggregation", "//pkg/infoschema", "//pkg/kv", + "//pkg/meta", "//pkg/meta/autoid", "//pkg/metrics", "//pkg/parser", diff --git a/pkg/executor/executor_failpoint_test.go b/pkg/executor/executor_failpoint_test.go index 16e10453c2c1b..01b4ff86e1469 100644 --- a/pkg/executor/executor_failpoint_test.go +++ b/pkg/executor/executor_failpoint_test.go @@ -27,9 +27,11 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/tidb/pkg/config" "github.com/pingcap/tidb/pkg/ddl" + "github.com/pingcap/tidb/pkg/meta" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/session" "github.com/pingcap/tidb/pkg/store/copr" + "github.com/pingcap/tidb/pkg/store/helper" "github.com/pingcap/tidb/pkg/testkit" "github.com/pingcap/tidb/pkg/util/dbterror/exeerrors" "github.com/pingcap/tidb/pkg/util/deadlockhistory" @@ -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/pkg/store/mockstore/unistore/epochNotMatch", "2*return(true)")) + defer func() { + require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/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) +} diff --git a/pkg/store/mockstore/unistore/rpc.go b/pkg/store/mockstore/unistore/rpc.go index ccbeb28748a44..a564e0f2225f9 100644 --- a/pkg/store/mockstore/unistore/rpc.go +++ b/pkg/store/mockstore/unistore/rpc.go @@ -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 fn := UnistoreRPCClientSendHook.Load(); val.(bool) && fn != nil {