diff --git a/executor/autoidtest/BUILD.bazel b/executor/autoidtest/BUILD.bazel index 0f5bf6c434f91..34be3469b78ad 100644 --- a/executor/autoidtest/BUILD.bazel +++ b/executor/autoidtest/BUILD.bazel @@ -8,6 +8,7 @@ go_test( ], flaky = True, race = "on", + shard_count = 30, deps = [ "//config", "//ddl/testutil", diff --git a/executor/autoidtest/main_test.go b/executor/autoidtest/main_test.go index f87db4afe1371..1dc33b2085dd4 100644 --- a/executor/autoidtest/main_test.go +++ b/executor/autoidtest/main_test.go @@ -39,6 +39,7 @@ func TestMain(m *testing.M) { goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"), goleak.IgnoreTopFunction("github.com/tikv/client-go/v2/txnkv/transaction.keepAlive"), + goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"), } goleak.VerifyTestMain(m, opts...) } diff --git a/executor/index_merge_reader_test.go b/executor/index_merge_reader_test.go index 46a1206460074..f6be67d455414 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" @@ -937,3 +938,34 @@ func TestIndexMergeCoprGoroutinesLeak(t *testing.T) { require.Contains(t, err.Error(), "testIndexMergePartialIndexWorkerCoprLeak") require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/executor/testIndexMergePartialIndexWorkerCoprLeak")) } + +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/session/session.go b/session/session.go index b60a5f1782b65..91d26bdb56a9f 100644 --- a/session/session.go +++ b/session/session.go @@ -1617,6 +1617,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, } diff --git a/util/processinfo.go b/util/processinfo.go index 579d1ae08b18e..0144f14eaf547 100644 --- a/util/processinfo.go +++ b/util/processinfo.go @@ -57,6 +57,8 @@ type ProcessInfo struct { Info string Port string PlanExplainRows [][]string + TableIDs []int64 + IndexNames []string OOMAlarmVariablesInfo OOMAlarmVariablesInfo ID uint64 CurTxnStartTS uint64 diff --git a/util/util.go b/util/util.go index 5edb010ce6c52..4dc22ddcd1d65 100644 --- a/util/util.go +++ b/util/util.go @@ -148,12 +148,12 @@ func GenLogFields(costTime time.Duration, info *ProcessInfo, needTruncateSQL boo logFields = append(logFields, zap.String("database", info.DB)) } var tableIDs, indexNames string - if len(info.StmtCtx.TableIDs) > 0 { - tableIDs = strings.Replace(fmt.Sprintf("%v", info.StmtCtx.TableIDs), " ", ",", -1) + if len(info.TableIDs) > 0 { + tableIDs = strings.Replace(fmt.Sprintf("%v", info.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.IndexNames) > 0 { + 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))