From 03aabeea0331fef205378307a9b2ee194227574a Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 17 Oct 2023 14:53:05 +0800 Subject: [PATCH 1/7] init Signed-off-by: crazycs520 --- go.mod | 2 + .../sessiontest/session_fail_test.go | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/go.mod b/go.mod index 59a0f1836ee1d..be849ec707165 100644 --- a/go.mod +++ b/go.mod @@ -313,3 +313,5 @@ replace ( github.com/go-ldap/ldap/v3 => github.com/YangKeao/ldap/v3 v3.4.5-0.20230421065457-369a3bab1117 github.com/pingcap/tidb/pkg/parser => ./pkg/parser ) + +replace github.com/tikv/client-go/v2 => ../client-go diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index 8c0952eb4fb59..7617cb1ab831f 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -17,8 +17,12 @@ package sessiontest import ( "context" "fmt" + "github.com/tikv/client-go/v2/tikv" + "math/rand" "strconv" + "sync" "testing" + "time" "github.com/pingcap/failpoint" "github.com/pingcap/tidb/pkg/config" @@ -317,3 +321,44 @@ func TestTiKVClientReadTimeout(t *testing.T) { explain = fmt.Sprintf("%v", rows[0]) require.Regexp(t, ".*TableReader.* root time:.*, loops:.* cop_task: {num: 1, .* rpc_num: (3|4|5).*", explain) } + +func TestBatchClientSendLoopPanic(t *testing.T) { + if !*realtikvtest.WithRealTiKV { + t.Skip("skip test since it's only work for tikv") + } + config.UpdateGlobal(func(conf *config.Config) { + conf.TiKVClient.MaxBatchSize = 128 + }) + store := realtikvtest.CreateMockStoreAndSetup(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table if not exists t (a int primary key, b int)") + + var wg sync.WaitGroup + for i := 0; i < 100; i++ { + wg.Add(1) + go func() { + defer wg.Done() + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + for j := 0; j < 100; j++ { + ctx, cancel := context.WithCancel(context.Background()) + go func() { + // mock for kill query execution or timeout. + time.Sleep(time.Millisecond * time.Duration(rand.Intn(10)+1)) + cancel() + }() + rs, err := tk.ExecWithContext(ctx, "select * from t where a = 1") + if err == nil { + require.NotNil(t, rs) + session.ResultSetToStringSlice(ctx, tk.Session(), rs) + } else { + require.Equal(t, context.Canceled, err) + } + } + + }() + } + wg.Wait() + require.Equal(t, int64(0), tikv.GetBatchSendLoopPanicCounter()) +} From 95f4f6e87a21d7d009ff4d02abdf10de2bc1d9b4 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 17 Oct 2023 16:57:38 +0800 Subject: [PATCH 2/7] *: update tikv/client-go to fix batch-client send loop panic issue Signed-off-by: crazycs520 --- go.mod | 4 +--- go.sum | 4 ++-- tests/realtikvtest/sessiontest/session_fail_test.go | 12 ++++-------- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index be849ec707165..72fd4b54e4763 100644 --- a/go.mod +++ b/go.mod @@ -103,7 +103,7 @@ require ( github.com/stretchr/testify v1.8.4 github.com/tdakkota/asciicheck v0.2.0 github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 - github.com/tikv/client-go/v2 v2.0.8-0.20231010061802-07432ef6c031 + github.com/tikv/client-go/v2 v2.0.8-0.20231017074406-2eaf68e0cd3d github.com/tikv/pd/client v0.0.0-20230912103610-2f57a9f050eb github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966 github.com/twmb/murmur3 v1.1.6 @@ -313,5 +313,3 @@ replace ( github.com/go-ldap/ldap/v3 => github.com/YangKeao/ldap/v3 v3.4.5-0.20230421065457-369a3bab1117 github.com/pingcap/tidb/pkg/parser => ./pkg/parser ) - -replace github.com/tikv/client-go/v2 => ../client-go diff --git a/go.sum b/go.sum index b94e413d9b828..d1de329e8eb4a 100644 --- a/go.sum +++ b/go.sum @@ -1027,8 +1027,8 @@ github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2 h1:mbAskLJ0oJf github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfKggNGDuadAa0LElHrByyrz4JPZ9fFx6Gs7nx7ZZU= github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4= github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a/go.mod h1:h4xBhSNtOeEosLJ4P7JyKXX7Cabg7AVkWCK5gV2vOrM= -github.com/tikv/client-go/v2 v2.0.8-0.20231010061802-07432ef6c031 h1:+D3p4UBbL1V3vCZ7FtaW9mZgeGq3U14+VfcHyiYn1mU= -github.com/tikv/client-go/v2 v2.0.8-0.20231010061802-07432ef6c031/go.mod h1:AjaU1PM3aVQih/zsYvc5sE9z7dAMxV+e881GNeZGTck= +github.com/tikv/client-go/v2 v2.0.8-0.20231017074406-2eaf68e0cd3d h1:PLGpRLh1E1zBdXNER52L5VCDCAuk8Bh1mowNvO1OUp4= +github.com/tikv/client-go/v2 v2.0.8-0.20231017074406-2eaf68e0cd3d/go.mod h1:AjaU1PM3aVQih/zsYvc5sE9z7dAMxV+e881GNeZGTck= github.com/tikv/pd/client v0.0.0-20230912103610-2f57a9f050eb h1:hAcH9tFjQzQ3+ofrAHm4ajOTLliYCOfXpj3+boKOtac= github.com/tikv/pd/client v0.0.0-20230912103610-2f57a9f050eb/go.mod h1:E+6qtPu8fJm5kNjvKWPVFqSgNAFPk07y2EjD03GWzuI= github.com/timakin/bodyclose v0.0.0-20230421092635-574207250966 h1:quvGphlmUVU+nhpFa4gg4yJyTRJ13reZMDHrKwYw53M= diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index 7617cb1ab831f..c9c7996936c92 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -17,7 +17,6 @@ package sessiontest import ( "context" "fmt" - "github.com/tikv/client-go/v2/tikv" "math/rand" "strconv" "sync" @@ -322,7 +321,8 @@ func TestTiKVClientReadTimeout(t *testing.T) { require.Regexp(t, ".*TableReader.* root time:.*, loops:.* cop_task: {num: 1, .* rpc_num: (3|4|5).*", explain) } -func TestBatchClientSendLoopPanic(t *testing.T) { +func TestBatchClientDataRace(t *testing.T) { + // This test uses to detect data race, so it should run by `go test -race`. if !*realtikvtest.WithRealTiKV { t.Skip("skip test since it's only work for tikv") } @@ -348,17 +348,13 @@ func TestBatchClientSendLoopPanic(t *testing.T) { time.Sleep(time.Millisecond * time.Duration(rand.Intn(10)+1)) cancel() }() - rs, err := tk.ExecWithContext(ctx, "select * from t where a = 1") - if err == nil { - require.NotNil(t, rs) + rs, _ := tk.ExecWithContext(ctx, "select * from t where a = 1") + if rs != nil { session.ResultSetToStringSlice(ctx, tk.Session(), rs) - } else { - require.Equal(t, context.Canceled, err) } } }() } wg.Wait() - require.Equal(t, int64(0), tikv.GetBatchSendLoopPanicCounter()) } From 2dec85a32c5552fb6e5cce1bfb6122c32e76363c Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Tue, 17 Oct 2023 18:23:43 +0800 Subject: [PATCH 3/7] make bazel_prepare Signed-off-by: crazycs520 --- DEPS.bzl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DEPS.bzl b/DEPS.bzl index 91adc5614dc12..728af4e2f7e57 100644 --- a/DEPS.bzl +++ b/DEPS.bzl @@ -7067,13 +7067,13 @@ def go_deps(): name = "com_github_tikv_client_go_v2", build_file_proto_mode = "disable_global", importpath = "github.com/tikv/client-go/v2", - sha256 = "e4953948d2346bf26d95fcc860b612c7c1d86f07c80a754db2551067912d37c8", - strip_prefix = "github.com/tikv/client-go/v2@v2.0.8-0.20231010061802-07432ef6c031", + sha256 = "6ad31199d7498e5bd6e493790db713ee8b253276c116a414b25d67cf31e7fb5a", + strip_prefix = "github.com/tikv/client-go/v2@v2.0.8-0.20231017074406-2eaf68e0cd3d", urls = [ - "http://bazel-cache.pingcap.net:8080/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231010061802-07432ef6c031.zip", - "http://ats.apps.svc/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231010061802-07432ef6c031.zip", - "https://cache.hawkingrei.com/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231010061802-07432ef6c031.zip", - "https://storage.googleapis.com/pingcapmirror/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231010061802-07432ef6c031.zip", + "http://bazel-cache.pingcap.net:8080/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231017074406-2eaf68e0cd3d.zip", + "http://ats.apps.svc/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231017074406-2eaf68e0cd3d.zip", + "https://cache.hawkingrei.com/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231017074406-2eaf68e0cd3d.zip", + "https://storage.googleapis.com/pingcapmirror/gomod/github.com/tikv/client-go/v2/com_github_tikv_client_go_v2-v2.0.8-0.20231017074406-2eaf68e0cd3d.zip", ], ) go_repository( From 29e50b1aea93e041afaf4781bc05a2fa97f2c095 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 18 Oct 2023 14:44:17 +0800 Subject: [PATCH 4/7] address comment Signed-off-by: crazycs520 --- tests/realtikvtest/sessiontest/session_fail_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index c9c7996936c92..4a464c7a8e2e2 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -353,7 +353,6 @@ func TestBatchClientDataRace(t *testing.T) { session.ResultSetToStringSlice(ctx, tk.Session(), rs) } } - }() } wg.Wait() From 2f2c554d4eac6c1c1e3e57ea0d389008273fc182 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 18 Oct 2023 15:11:46 +0800 Subject: [PATCH 5/7] refine test Signed-off-by: crazycs520 --- tests/realtikvtest/sessiontest/session_fail_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index 4a464c7a8e2e2..7866cb047f3f7 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -332,6 +332,7 @@ func TestBatchClientDataRace(t *testing.T) { store := realtikvtest.CreateMockStoreAndSetup(t) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") + tk.MustExec("drop table if exists t") tk.MustExec("create table if not exists t (a int primary key, b int)") var wg sync.WaitGroup From 8aa4cf6a12e055e64ea777b5cb45f3ed4d169089 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 18 Oct 2023 15:26:05 +0800 Subject: [PATCH 6/7] refine test Signed-off-by: crazycs520 --- tests/realtikvtest/sessiontest/session_fail_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index 7866cb047f3f7..e6a6cff30e208 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -349,7 +349,12 @@ func TestBatchClientDataRace(t *testing.T) { time.Sleep(time.Millisecond * time.Duration(rand.Intn(10)+1)) cancel() }() - rs, _ := tk.ExecWithContext(ctx, "select * from t where a = 1") + sqls := []string{ + "select * from t where a = 1", + "select * from t where a in ( 1, 2, 3)", + "select * from t", + } + rs, _ := tk.ExecWithContext(ctx, sqls[j%len(sqls)]) if rs != nil { session.ResultSetToStringSlice(ctx, tk.Session(), rs) } From b5b720e5d74fe2f839de00a76ba39e9f1085add5 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 18 Oct 2023 16:16:47 +0800 Subject: [PATCH 7/7] fix test Signed-off-by: crazycs520 --- tests/realtikvtest/sessiontest/session_fail_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/realtikvtest/sessiontest/session_fail_test.go b/tests/realtikvtest/sessiontest/session_fail_test.go index e6a6cff30e208..61ff4c2034182 100644 --- a/tests/realtikvtest/sessiontest/session_fail_test.go +++ b/tests/realtikvtest/sessiontest/session_fail_test.go @@ -357,6 +357,7 @@ func TestBatchClientDataRace(t *testing.T) { rs, _ := tk.ExecWithContext(ctx, sqls[j%len(sqls)]) if rs != nil { session.ResultSetToStringSlice(ctx, tk.Session(), rs) + rs.Close() } } }()