From 7ab8cabf17e0cfbe70e8b06e5990dc750f4257bf Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Wed, 5 Jul 2023 15:38:22 +0800 Subject: [PATCH 1/2] This is an automated cherry-pick of #45127 Signed-off-by: ti-chi-bot --- executor/index_merge_reader_test.go | 32 +++++++++++++++++++++++++++++ executor/jointest/BUILD.bazel | 4 ++++ session/session.go | 2 ++ util/processinfo.go | 2 ++ util/util.go | 9 ++++++++ 5 files changed, 49 insertions(+) diff --git a/executor/index_merge_reader_test.go b/executor/index_merge_reader_test.go index eb477020f623a..8842064e63fa2 100644 --- a/executor/index_merge_reader_test.go +++ b/executor/index_merge_reader_test.go @@ -20,6 +20,7 @@ import ( "regexp" "strconv" "strings" + "sync" "testing" "time" @@ -1073,3 +1074,34 @@ func TestOrderByWithLimit(t *testing.T) { } } } + +func TestProcessInfoRaceWithIndexScan(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists t1;") + tk.MustExec("create table t1(c1 int, c2 int, c3 int, c4 int, c5 int, key(c1), key(c2), key(c3), key(c4),key(c5));") + insertStr := "insert into t1 values(0, 0, 0, 0 , 0)" + for i := 1; i < 100; i++ { + insertStr += fmt.Sprintf(", (%d, %d, %d, %d, %d)", i, i, i, i, i) + } + tk.MustExec(insertStr) + + tk.Session().SetSessionManager(&testkit.MockSessionManager{ + PS: []*util.ProcessInfo{tk.Session().ShowProcess()}, + }) + + wg := sync.WaitGroup{} + wg.Add(1) + go func() { + defer wg.Done() + for i := 0; i <= 100; i++ { + ps := tk.Session().ShowProcess() + util.GenLogFields(233, ps, true) + } + }() + for i := 0; i <= 100; i++ { + tk.MustQuery("select /*+ use_index(t1, c1) */ c1 from t1 where c1 = 0 union all select /*+ use_index(t1, c2) */ c2 from t1 where c2 = 0 union all select /*+ use_index(t1, c3) */ c3 from t1 where c3 = 0 ") + } + wg.Wait() +} diff --git a/executor/jointest/BUILD.bazel b/executor/jointest/BUILD.bazel index c4b729a28137b..1cd17bdac681f 100644 --- a/executor/jointest/BUILD.bazel +++ b/executor/jointest/BUILD.bazel @@ -9,7 +9,11 @@ go_test( ], flaky = True, race = "on", +<<<<<<< HEAD:executor/jointest/BUILD.bazel shard_count = 50, +======= + shard_count = 30, +>>>>>>> af04707eda7 (*: fix data race in ProcessInfo (#45127)):executor/test/indexmergereadtest/BUILD.bazel deps = [ "//config", "//meta/autoid", diff --git a/session/session.go b/session/session.go index 3dc3126f9151e..3dc58c6d4d74d 100644 --- a/session/session.go +++ b/session/session.go @@ -1572,6 +1572,8 @@ func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecu DiskTracker: s.sessionVars.DiskTracker, StatsInfo: plannercore.GetStatsInfo, OOMAlarmVariablesInfo: s.getOomAlarmVariablesInfo(), + TableIDs: s.sessionVars.StmtCtx.TableIDs, + IndexNames: s.sessionVars.StmtCtx.IndexNames, MaxExecutionTime: maxExecutionTime, RedactSQL: s.sessionVars.EnableRedactLog, ResourceGroupName: s.sessionVars.ResourceGroupName, diff --git a/util/processinfo.go b/util/processinfo.go index d7e2e7c15ea83..0e641b132affb 100644 --- a/util/processinfo.go +++ b/util/processinfo.go @@ -58,6 +58,8 @@ type ProcessInfo struct { Port string ResourceGroupName string PlanExplainRows [][]string + TableIDs []int64 + IndexNames []string OOMAlarmVariablesInfo OOMAlarmVariablesInfo ID uint64 CurTxnStartTS uint64 diff --git a/util/util.go b/util/util.go index 9a8731cd94bb9..88c078837574d 100644 --- a/util/util.go +++ b/util/util.go @@ -153,12 +153,21 @@ func GenLogFields(costTime time.Duration, info *ProcessInfo, needTruncateSQL boo logFields = append(logFields, zap.String("database", info.DB)) } var tableIDs, indexNames string +<<<<<<< HEAD if len(info.StmtCtx.TableIDs) > 0 { tableIDs = strings.Replace(fmt.Sprintf("%v", info.StmtCtx.TableIDs), " ", ",", -1) logFields = append(logFields, zap.String("table_ids", tableIDs)) } if len(info.StmtCtx.IndexNames) > 0 { indexNames = strings.Replace(fmt.Sprintf("%v", info.StmtCtx.IndexNames), " ", ",", -1) +======= + if len(info.TableIDs) > 0 { + tableIDs = strings.ReplaceAll(fmt.Sprintf("%v", info.TableIDs), " ", ",") + logFields = append(logFields, zap.String("table_ids", tableIDs)) + } + if len(info.IndexNames) > 0 { + indexNames = strings.ReplaceAll(fmt.Sprintf("%v", info.IndexNames), " ", ",") +>>>>>>> af04707eda7 (*: fix data race in ProcessInfo (#45127)) logFields = append(logFields, zap.String("index_names", indexNames)) } logFields = append(logFields, zap.Uint64("txn_start_ts", info.CurTxnStartTS)) From 23cac286a344290bf2d0d36b2e054c8abb630338 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Fri, 7 Jul 2023 15:24:33 +0800 Subject: [PATCH 2/2] resolve conflict --- executor/jointest/BUILD.bazel | 4 ---- util/util.go | 13 ++----------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/executor/jointest/BUILD.bazel b/executor/jointest/BUILD.bazel index 1cd17bdac681f..c4b729a28137b 100644 --- a/executor/jointest/BUILD.bazel +++ b/executor/jointest/BUILD.bazel @@ -9,11 +9,7 @@ go_test( ], flaky = True, race = "on", -<<<<<<< HEAD:executor/jointest/BUILD.bazel shard_count = 50, -======= - shard_count = 30, ->>>>>>> af04707eda7 (*: fix data race in ProcessInfo (#45127)):executor/test/indexmergereadtest/BUILD.bazel deps = [ "//config", "//meta/autoid", diff --git a/util/util.go b/util/util.go index 88c078837574d..3e54054514b69 100644 --- a/util/util.go +++ b/util/util.go @@ -153,21 +153,12 @@ func GenLogFields(costTime time.Duration, info *ProcessInfo, needTruncateSQL boo logFields = append(logFields, zap.String("database", info.DB)) } var tableIDs, indexNames string -<<<<<<< HEAD - if len(info.StmtCtx.TableIDs) > 0 { - tableIDs = strings.Replace(fmt.Sprintf("%v", info.StmtCtx.TableIDs), " ", ",", -1) - logFields = append(logFields, zap.String("table_ids", tableIDs)) - } - if len(info.StmtCtx.IndexNames) > 0 { - indexNames = strings.Replace(fmt.Sprintf("%v", info.StmtCtx.IndexNames), " ", ",", -1) -======= if len(info.TableIDs) > 0 { - tableIDs = strings.ReplaceAll(fmt.Sprintf("%v", info.TableIDs), " ", ",") + tableIDs = strings.Replace(fmt.Sprintf("%v", info.TableIDs), " ", ",", -1) logFields = append(logFields, zap.String("table_ids", tableIDs)) } if len(info.IndexNames) > 0 { - indexNames = strings.ReplaceAll(fmt.Sprintf("%v", info.IndexNames), " ", ",") ->>>>>>> af04707eda7 (*: fix data race in ProcessInfo (#45127)) + indexNames = strings.Replace(fmt.Sprintf("%v", info.IndexNames), " ", ",", -1) logFields = append(logFields, zap.String("index_names", indexNames)) } logFields = append(logFields, zap.Uint64("txn_start_ts", info.CurTxnStartTS))