From 1203ea26d924db87ca4c78b145f80a6eebecb341 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Mon, 2 Sep 2024 16:24:41 +0800 Subject: [PATCH] executor: fix index_hash_join hang when context canceled (#54855) (#55006) close pingcap/tidb#54688 --- DEPS.bzl | 4 +-- executor/index_lookup_hash_join.go | 4 +++ executor/index_lookup_join_test.go | 40 ++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/DEPS.bzl b/DEPS.bzl index 3195ca87d1e73..7f25a4ee87707 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -2952,8 +2952,8 @@ def go_deps(): name = "com_github_pingcap_failpoint", build_file_proto_mode = "disable_global", importpath = "github.com/pingcap/failpoint", - sum = "h1:kJolJWbyadVeL8RKBlqmXQR7FRKPsIeU85TUYyhbhiQ=", - version = "v0.0.0-20220423142525-ae43b7f4e5c3", + sum = "h1:UgrcL8INjEbPRKE2h8yVgZvjOn2OGkxK9CFvoBWzgbk=", + version = "v0.0.0-20240527053858-9b3b6e34194a", ) go_repository( name = "com_github_pingcap_fn", diff --git a/executor/index_lookup_hash_join.go b/executor/index_lookup_hash_join.go index 2a3394d7e2fb0..8cca9fe5df4f9 100644 --- a/executor/index_lookup_hash_join.go +++ b/executor/index_lookup_hash_join.go @@ -561,6 +561,7 @@ func (iw *indexHashJoinInnerWorker) getNewJoinResult(ctx context.Context) (*inde select { case joinResult.chk, ok = <-iw.joinChkResourceCh: case <-ctx.Done(): + joinResult.err = ctx.Err() return joinResult, false } return joinResult, ok @@ -779,7 +780,10 @@ func (iw *indexHashJoinInnerWorker) joinMatchedInnerRow2Chunk(ctx context.Contex select { case iw.resultCh <- joinResult: case <-ctx.Done(): + joinResult.err = ctx.Err() + return false, joinResult } + failpoint.InjectCall("joinMatchedInnerRow2Chunk") joinResult, ok = iw.getNewJoinResult(ctx) if !ok { return false, joinResult diff --git a/executor/index_lookup_join_test.go b/executor/index_lookup_join_test.go index b3910fdd1a872..e40ac2f5198ab 100644 --- a/executor/index_lookup_join_test.go +++ b/executor/index_lookup_join_test.go @@ -18,10 +18,12 @@ import ( "context" "fmt" "math/rand" + "runtime" "strings" "testing" "github.com/pingcap/failpoint" + "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/testkit" "github.com/stretchr/testify/require" ) @@ -521,3 +523,41 @@ func TestIssue45716(t *testing.T) { err := tk.QueryToErr("select /*+ inl_join(t2) */ * from t1 join t2 on t1.a = t2.a;") tk.MustContainErrMsg(err.Error(), "test inlNewInnerPanic") } + +func TestIssue54688(t *testing.T) { + val := runtime.GOMAXPROCS(1) + defer func() { + runtime.GOMAXPROCS(val) + }() + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists t, s;") + tk.MustExec("create table t(a int, index(a));") + tk.MustExec("create table s(a int, index(a));") + tk.MustExec("insert into t values(1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (16);") + tk.MustExec("insert into s values(1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (16);") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("insert into s select * from s") + tk.MustExec("set @@tidb_index_lookup_join_concurrency=1;") + tk.MustExec("set @@tidb_index_join_batch_size=1000000;") + + for i := 0; i <= 100; i++ { + rs, err := tk.Exec("select /*+ INL_HASH_JOIN(s) */ * from t join s on t.a=s.a") + require.NoError(t, err) + context, cancel := context.WithCancel(context.Background()) + require.NoError(t, failpoint.EnableCall("github.com/pingcap/tidb/executor/join/joinMatchedInnerRow2Chunk", + func() { + cancel() + }, + )) + _, _ = session.GetRows4Test(context, nil, rs) + rs.Close() + } +} diff --git a/go.mod b/go.mod index bebfea615ae94..7a9bd12c1b128 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,7 @@ require ( github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 github.com/pingcap/badger v1.5.1-0.20220314162537-ab58fbf40580 github.com/pingcap/errors v0.11.5-0.20220729040631-518f63d66278 - github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 + github.com/pingcap/failpoint v0.0.0-20240527053858-9b3b6e34194a github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059 github.com/pingcap/kvproto v0.0.0-20240112060601-a0e3fbb1eeee github.com/pingcap/log v1.1.1-0.20221116035753-734d527bc87c diff --git a/go.sum b/go.sum index d8d35a248daf3..cb6509e81accd 100644 --- a/go.sum +++ b/go.sum @@ -776,8 +776,8 @@ github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c/go.mod h1:X2r9ue github.com/pingcap/errors v0.11.5-0.20220729040631-518f63d66278 h1:3Dm0DWeQlwV8LbpQxP2tojHhxd9aY59KI+QN0ns6bBo= github.com/pingcap/errors v0.11.5-0.20220729040631-518f63d66278/go.mod h1:X2r9ueLEUZgtx2cIogM0v4Zj5uvvzhuuiu7Pn8HzMPg= github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew= -github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3 h1:kJolJWbyadVeL8RKBlqmXQR7FRKPsIeU85TUYyhbhiQ= -github.com/pingcap/failpoint v0.0.0-20220423142525-ae43b7f4e5c3/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew= +github.com/pingcap/failpoint v0.0.0-20240527053858-9b3b6e34194a h1:UgrcL8INjEbPRKE2h8yVgZvjOn2OGkxK9CFvoBWzgbk= +github.com/pingcap/failpoint v0.0.0-20240527053858-9b3b6e34194a/go.mod h1:gPdo4h708R0CrwKM/DO0/6xJ64fz9vxzp2yKE2QON+s= github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059 h1:Pe2LbxRmbTfAoKJ65bZLmhahmvHm7n9DUxGRQT00208= github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059/go.mod h1:fMRU1BA1y+r89AxUoaAar4JjrhUkVDt0o0Np6V8XbDQ= github.com/pingcap/goleveldb v0.0.0-20191226122134-f82aafb29989 h1:surzm05a8C9dN8dIUmo4Be2+pMRb6f55i+UIYrluu2E=