From 03847a8ded0cce027e90f1c3f8a389ea7e392232 Mon Sep 17 00:00:00 2001 From: ClSlaid Date: Thu, 17 Jun 2021 21:02:38 +0800 Subject: [PATCH] executor, infoschema: Add cluster_statements_summary_evicted table to TiDB (#25418) --- executor/builder.go | 1 + executor/infoschema_reader.go | 16 ++++++++-------- infoschema/cluster.go | 3 +++ infoschema/tables.go | 1 + infoschema/tables_test.go | 34 ++++++++++++++++++++++++++++++++-- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index f9a3c0746ebe0..b3bbdc579d6cb 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -1563,6 +1563,7 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo strings.ToLower(infoschema.TableStatementsSummaryEvicted), strings.ToLower(infoschema.ClusterTableStatementsSummary), strings.ToLower(infoschema.ClusterTableStatementsSummaryHistory), + strings.ToLower(infoschema.ClusterTableStatementsSummaryEvicted), strings.ToLower(infoschema.TablePlacementPolicy), strings.ToLower(infoschema.TableClientErrorsSummaryGlobal), strings.ToLower(infoschema.TableClientErrorsSummaryByUser), diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index b01972726991a..ef39c9cc96d35 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -145,11 +145,11 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex err = e.dataForTiKVStoreStatus(sctx) case infoschema.TableStatementsSummary, infoschema.TableStatementsSummaryHistory, + infoschema.TableStatementsSummaryEvicted, infoschema.ClusterTableStatementsSummary, - infoschema.ClusterTableStatementsSummaryHistory: + infoschema.ClusterTableStatementsSummaryHistory, + infoschema.ClusterTableStatementsSummaryEvicted: err = e.setDataForStatementsSummary(sctx, e.table.Name.O) - case infoschema.TableStatementsSummaryEvicted: - e.setDataForStatementsSummaryEvicted(sctx) case infoschema.TablePlacementPolicy: err = e.setDataForPlacementPolicy(sctx) case infoschema.TableClientErrorsSummaryGlobal, @@ -1912,10 +1912,14 @@ func (e *memtableRetriever) setDataForStatementsSummary(ctx sessionctx.Context, case infoschema.TableStatementsSummaryHistory, infoschema.ClusterTableStatementsSummaryHistory: e.rows = stmtsummary.StmtSummaryByDigestMap.ToHistoryDatum(user, isSuper) + case infoschema.TableStatementsSummaryEvicted, + infoschema.ClusterTableStatementsSummaryEvicted: + e.rows = stmtsummary.StmtSummaryByDigestMap.ToEvictedCountDatum() } switch tableName { case infoschema.ClusterTableStatementsSummary, - infoschema.ClusterTableStatementsSummaryHistory: + infoschema.ClusterTableStatementsSummaryHistory, + infoschema.ClusterTableStatementsSummaryEvicted: rows, err := infoschema.AppendHostInfoToRows(ctx, e.rows) if err != nil { return err @@ -2101,10 +2105,6 @@ func (e *memtableRetriever) setDataForClusterDeadlock(ctx sessionctx.Context) er return nil } -func (e *memtableRetriever) setDataForStatementsSummaryEvicted(ctx sessionctx.Context) { - e.rows = stmtsummary.StmtSummaryByDigestMap.ToEvictedCountDatum() -} - type hugeMemTableRetriever struct { dummyCloser table *model.TableInfo diff --git a/infoschema/cluster.go b/infoschema/cluster.go index 20589ad7a0c67..e4e14e195826f 100644 --- a/infoschema/cluster.go +++ b/infoschema/cluster.go @@ -37,6 +37,8 @@ const ( ClusterTableStatementsSummary = "CLUSTER_STATEMENTS_SUMMARY" // ClusterTableStatementsSummaryHistory is the string constant of cluster statement summary history table. ClusterTableStatementsSummaryHistory = "CLUSTER_STATEMENTS_SUMMARY_HISTORY" + // ClusterTableStatementsSummaryEvicted is the string constant of cluster statement summary evict table. + ClusterTableStatementsSummaryEvicted = "CLUSTER_STATEMENTS_SUMMARY_EVICTED" // ClusterTableTiDBTrx is the string constant of cluster transaction running table. ClusterTableTiDBTrx = "CLUSTER_TIDB_TRX" // ClusterTableDeadlocks is the string constant of cluster dead lock table. @@ -49,6 +51,7 @@ var memTableToClusterTables = map[string]string{ TableProcesslist: ClusterTableProcesslist, TableStatementsSummary: ClusterTableStatementsSummary, TableStatementsSummaryHistory: ClusterTableStatementsSummaryHistory, + TableStatementsSummaryEvicted: ClusterTableStatementsSummaryEvicted, TableTiDBTrx: ClusterTableTiDBTrx, TableDeadlocks: ClusterTableDeadlocks, } diff --git a/infoschema/tables.go b/infoschema/tables.go index a1404ac8c717b..cc66f0afd6cd0 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -249,6 +249,7 @@ var tableIDMap = map[string]int64{ ClusterTableDeadlocks: autoid.InformationSchemaDBID + 73, TableDataLockWaits: autoid.InformationSchemaDBID + 74, TableStatementsSummaryEvicted: autoid.InformationSchemaDBID + 75, + ClusterTableStatementsSummaryEvicted: autoid.InformationSchemaDBID + 76, } type columnInfo struct { diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 6cf0acfb68921..38857e40b9563 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -1374,12 +1374,18 @@ func (s *testTableSuite) TestStmtSummarySensitiveQuery(c *C) { )) } +// test stmtSummaryEvictedCount func (s *testTableSuite) TestSimpleStmtSummaryEvictedCount(c *C) { now := time.Now().Unix() interval := int64(1800) beginTimeForCurInterval := now - now%interval tk := s.newTestKitWithPlanCache(c) tk.MustExec(fmt.Sprintf("set global tidb_stmt_summary_refresh_interval = %v", interval)) + + // clean up side effects + defer tk.MustExec("set global tidb_stmt_summary_max_stmt_count = 100") + defer tk.MustExec("set global tidb_stmt_summary_refresh_interval = 1800") + tk.MustExec("set global tidb_enable_stmt_summary = 0") tk.MustExec("set global tidb_enable_stmt_summary = 1") // first sql @@ -1395,10 +1401,34 @@ func (s *testTableSuite) TestSimpleStmtSummaryEvictedCount(c *C) { int64(2)), )) // TODO: Add more tests. +} +// test stmtSummaryEvictedCount cluster table +func (s *testClusterTableSuite) TestStmtSummaryEvictedCountTable(c *C) { + tk := s.newTestKitWithRoot(c) + // disable refreshing + tk.MustExec("set global tidb_stmt_summary_refresh_interval=9999") + // set information_schema.statements_summary's size to 1 + tk.MustExec("set global tidb_stmt_summary_max_stmt_count = 1") // clean up side effects - tk.MustExec("set global tidb_stmt_summary_max_stmt_count = 100") - tk.MustExec("set global tidb_stmt_summary_refresh_interval = 1800") + defer tk.MustExec("set global tidb_stmt_summary_max_stmt_count = 100") + defer tk.MustExec("set global tidb_stmt_summary_refresh_interval = 1800") + // clear information_schema.statements_summary + tk.MustExec("set global tidb_enable_stmt_summary=0") + tk.MustExec("set global tidb_enable_stmt_summary=1") + + // make a new session for test... + tk = s.newTestKitWithRoot(c) + // first sql + tk.MustExec("show databases;") + // second sql, evict former sql from stmt_summary + tk.MustQuery("select evicted_count from information_schema.cluster_statements_summary_evicted;"). + Check(testkit.Rows("1")) + // after executed the sql above + tk.MustQuery("select evicted_count from information_schema.cluster_statements_summary_evicted;"). + Check(testkit.Rows("2")) + // TODO: Add more tests. + } func (s *testTableSuite) TestStmtSummaryTableOther(c *C) {