Skip to content

Commit

Permalink
infoschema/perfschema: add cluster statement summary table (#14259)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Dec 29, 2019
1 parent e5d5a61 commit ca9ecf9
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 71 deletions.
12 changes: 8 additions & 4 deletions infoschema/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func init() {
continue
}
cols := make([]columnInfo, 0, len(memTableCols)+1)
cols = append(cols, memTableCols...)
cols = append(cols, addrCol)
cols = append(cols, memTableCols...)
tableNameToColumns[clusterMemTableName] = cols
}
}
Expand Down Expand Up @@ -85,17 +85,21 @@ func getClusterMemTableRows(ctx sessionctx.Context, tableName string) (rows [][]
if err != nil {
return nil, err
}
return appendHostInfoToRows(rows)
return AppendHostInfoToRows(rows)
}

func appendHostInfoToRows(rows [][]types.Datum) ([][]types.Datum, error) {
// AppendHostInfoToRows appends host info to the rows.
func AppendHostInfoToRows(rows [][]types.Datum) ([][]types.Datum, error) {
serverInfo, err := infosync.GetServerInfo()
if err != nil {
return nil, err
}
addr := serverInfo.IP + ":" + strconv.FormatUint(uint64(serverInfo.StatusPort), 10)
for i := range rows {
rows[i] = append(rows[i], types.NewStringDatum(addr))
row := make([]types.Datum, 0, len(rows[i])+1)
row = append(row, types.NewStringDatum(addr))
row = append(row, rows[i]...)
rows[i] = row
}
return rows, nil
}
19 changes: 15 additions & 4 deletions infoschema/perfschema/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var perfSchemaTables = []string{
tableStagesHistoryLong,
tableEventsStatementsSummaryByDigest,
tableEventsStatementsSummaryByDigestHistory,
tableClusterEventsStatementsSummaryByDigest,
tableClusterEventsStatementsSummaryByDigestHistory,
tableTiDBProfileCPU,
tableTiDBProfileMemory,
tableTiDBProfileMutex,
Expand Down Expand Up @@ -383,8 +385,7 @@ const tableStagesHistoryLong = "CREATE TABLE if not exists performance_schema."
"NESTING_EVENT_TYPE ENUM('TRANSACTION','STATEMENT','STAGE'));"

// Fields in `events_statements_summary_by_digest` and `events_statements_summary_by_digest_history` are the same.
const fieldsInEventsStatementsSummary = " (" +
"SUMMARY_BEGIN_TIME TIMESTAMP(6) NOT NULL," +
const fieldsInEventsStatementsSummary = "SUMMARY_BEGIN_TIME TIMESTAMP(6) NOT NULL," +
"SUMMARY_END_TIME TIMESTAMP(6) NOT NULL," +
"STMT_TYPE VARCHAR(64) NOT NULL," +
"SCHEMA_NAME VARCHAR(64) DEFAULT NULL," +
Expand Down Expand Up @@ -454,12 +455,22 @@ const fieldsInEventsStatementsSummary = " (" +
// tableEventsStatementsSummaryByDigest contains the column name definitions for table
// events_statements_summary_by_digest, same as MySQL.
const tableEventsStatementsSummaryByDigest = "CREATE TABLE if not exists " + tableNameEventsStatementsSummaryByDigest +
fieldsInEventsStatementsSummary
"(" + fieldsInEventsStatementsSummary

// tableEventsStatementsSummaryByDigestHistory contains the column name definitions for table
// events_statements_summary_by_digest_history.
const tableEventsStatementsSummaryByDigestHistory = "CREATE TABLE if not exists " + tableNameEventsStatementsSummaryByDigestHistory +
fieldsInEventsStatementsSummary
"(" + fieldsInEventsStatementsSummary

// tableClusterEventsStatementsSummaryByDigest contains the column name definitions for table
// cluster_events_statements_summary_by_digest, same as MySQL.
const tableClusterEventsStatementsSummaryByDigest = "CREATE TABLE if not exists " + tableNameClusterEventsStatementsSummaryByDigest +
"(ADDRESS VARCHAR(64) DEFAULT NULL," + fieldsInEventsStatementsSummary

// tableClusterEventsStatementsSummaryByDigestHistory contains the column name definitions for table
// cluster_events_statements_summary_by_digest_history.
const tableClusterEventsStatementsSummaryByDigestHistory = "CREATE TABLE if not exists " + tableNameClusterEventsStatementsSummaryByDigestHistory +
"(ADDRESS VARCHAR(64) DEFAULT NULL," + fieldsInEventsStatementsSummary

// tableTiDBProfileCPU contains the columns name definitions for table tidb_profile_cpu
const tableTiDBProfileCPU = "CREATE TABLE IF NOT EXISTS " + tableNameTiDBProfileCPU + " (" +
Expand Down
159 changes: 97 additions & 62 deletions infoschema/perfschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,78 +37,83 @@ import (
)

const (
tableNameGlobalStatus = "global_status"
tableNameSessionStatus = "session_status"
tableNameSetupActors = "setup_actors"
tableNameSetupObjects = "setup_objects"
tableNameSetupInstruments = "setup_instruments"
tableNameSetupConsumers = "setup_consumers"
tableNameEventsStatementsCurrent = "events_statements_current"
tableNameEventsStatementsHistory = "events_statements_history"
tableNameEventsStatementsHistoryLong = "events_statements_history_long"
tableNamePreparedStatementsInstances = "prepared_statements_instances"
tableNameEventsTransactionsCurrent = "events_transactions_current"
tableNameEventsTransactionsHistory = "events_transactions_history"
tableNameEventsTransactionsHistoryLong = "events_transactions_history_long"
tableNameEventsStagesCurrent = "events_stages_current"
tableNameEventsStagesHistory = "events_stages_history"
tableNameEventsStagesHistoryLong = "events_stages_history_long"
tableNameEventsStatementsSummaryByDigest = "events_statements_summary_by_digest"
tableNameEventsStatementsSummaryByDigestHistory = "events_statements_summary_by_digest_history"
tableNameTiDBProfileCPU = "tidb_profile_cpu"
tableNameTiDBProfileMemory = "tidb_profile_memory"
tableNameTiDBProfileMutex = "tidb_profile_mutex"
tableNameTiDBProfileAllocs = "tidb_profile_allocs"
tableNameTiDBProfileBlock = "tidb_profile_block"
tableNameTiDBProfileGoroutines = "tidb_profile_goroutines"
tableNameTiKVProfileCPU = "tikv_profile_cpu"
tableNamePDProfileCPU = "pd_profile_cpu"
tableNamePDProfileMemory = "pd_profile_memory"
tableNamePDProfileMutex = "pd_profile_mutex"
tableNamePDProfileAllocs = "pd_profile_allocs"
tableNamePDProfileBlock = "pd_profile_block"
tableNamePDProfileGoroutines = "pd_profile_goroutines"
tableNameGlobalStatus = "global_status"
tableNameSessionStatus = "session_status"
tableNameSetupActors = "setup_actors"
tableNameSetupObjects = "setup_objects"
tableNameSetupInstruments = "setup_instruments"
tableNameSetupConsumers = "setup_consumers"
tableNameEventsStatementsCurrent = "events_statements_current"
tableNameEventsStatementsHistory = "events_statements_history"
tableNameEventsStatementsHistoryLong = "events_statements_history_long"
tableNamePreparedStatementsInstances = "prepared_statements_instances"
tableNameEventsTransactionsCurrent = "events_transactions_current"
tableNameEventsTransactionsHistory = "events_transactions_history"
tableNameEventsTransactionsHistoryLong = "events_transactions_history_long"
tableNameEventsStagesCurrent = "events_stages_current"
tableNameEventsStagesHistory = "events_stages_history"
tableNameEventsStagesHistoryLong = "events_stages_history_long"
tableNameEventsStatementsSummaryByDigest = "events_statements_summary_by_digest"
tableNameEventsStatementsSummaryByDigestHistory = "events_statements_summary_by_digest_history"
tableNameClusterEventsStatementsSummaryByDigest = "cluster_events_statements_summary_by_digest"
tableNameClusterEventsStatementsSummaryByDigestHistory = "cluster_events_statements_summary_by_digest_history"
tableNameTiDBProfileCPU = "tidb_profile_cpu"
tableNameTiDBProfileMemory = "tidb_profile_memory"
tableNameTiDBProfileMutex = "tidb_profile_mutex"
tableNameTiDBProfileAllocs = "tidb_profile_allocs"
tableNameTiDBProfileBlock = "tidb_profile_block"
tableNameTiDBProfileGoroutines = "tidb_profile_goroutines"
tableNameTiKVProfileCPU = "tikv_profile_cpu"
tableNamePDProfileCPU = "pd_profile_cpu"
tableNamePDProfileMemory = "pd_profile_memory"
tableNamePDProfileMutex = "pd_profile_mutex"
tableNamePDProfileAllocs = "pd_profile_allocs"
tableNamePDProfileBlock = "pd_profile_block"
tableNamePDProfileGoroutines = "pd_profile_goroutines"
)

var tableIDMap = map[string]int64{
tableNameGlobalStatus: autoid.PerformanceSchemaDBID + 1,
tableNameSessionStatus: autoid.PerformanceSchemaDBID + 2,
tableNameSetupActors: autoid.PerformanceSchemaDBID + 3,
tableNameSetupObjects: autoid.PerformanceSchemaDBID + 4,
tableNameSetupInstruments: autoid.PerformanceSchemaDBID + 5,
tableNameSetupConsumers: autoid.PerformanceSchemaDBID + 6,
tableNameEventsStatementsCurrent: autoid.PerformanceSchemaDBID + 7,
tableNameEventsStatementsHistory: autoid.PerformanceSchemaDBID + 8,
tableNameEventsStatementsHistoryLong: autoid.PerformanceSchemaDBID + 9,
tableNamePreparedStatementsInstances: autoid.PerformanceSchemaDBID + 10,
tableNameEventsTransactionsCurrent: autoid.PerformanceSchemaDBID + 11,
tableNameEventsTransactionsHistory: autoid.PerformanceSchemaDBID + 12,
tableNameEventsTransactionsHistoryLong: autoid.PerformanceSchemaDBID + 13,
tableNameEventsStagesCurrent: autoid.PerformanceSchemaDBID + 14,
tableNameEventsStagesHistory: autoid.PerformanceSchemaDBID + 15,
tableNameEventsStagesHistoryLong: autoid.PerformanceSchemaDBID + 16,
tableNameEventsStatementsSummaryByDigest: autoid.PerformanceSchemaDBID + 17,
tableNameTiDBProfileCPU: autoid.PerformanceSchemaDBID + 18,
tableNameTiDBProfileMemory: autoid.PerformanceSchemaDBID + 19,
tableNameTiDBProfileMutex: autoid.PerformanceSchemaDBID + 20,
tableNameTiDBProfileAllocs: autoid.PerformanceSchemaDBID + 21,
tableNameTiDBProfileBlock: autoid.PerformanceSchemaDBID + 22,
tableNameTiDBProfileGoroutines: autoid.PerformanceSchemaDBID + 23,
tableNameTiKVProfileCPU: autoid.PerformanceSchemaDBID + 24,
tableNamePDProfileCPU: autoid.PerformanceSchemaDBID + 25,
tableNamePDProfileMemory: autoid.PerformanceSchemaDBID + 26,
tableNamePDProfileMutex: autoid.PerformanceSchemaDBID + 27,
tableNamePDProfileAllocs: autoid.PerformanceSchemaDBID + 28,
tableNamePDProfileBlock: autoid.PerformanceSchemaDBID + 29,
tableNamePDProfileGoroutines: autoid.PerformanceSchemaDBID + 30,
tableNameEventsStatementsSummaryByDigestHistory: autoid.PerformanceSchemaDBID + 31,
tableNameGlobalStatus: autoid.PerformanceSchemaDBID + 1,
tableNameSessionStatus: autoid.PerformanceSchemaDBID + 2,
tableNameSetupActors: autoid.PerformanceSchemaDBID + 3,
tableNameSetupObjects: autoid.PerformanceSchemaDBID + 4,
tableNameSetupInstruments: autoid.PerformanceSchemaDBID + 5,
tableNameSetupConsumers: autoid.PerformanceSchemaDBID + 6,
tableNameEventsStatementsCurrent: autoid.PerformanceSchemaDBID + 7,
tableNameEventsStatementsHistory: autoid.PerformanceSchemaDBID + 8,
tableNameEventsStatementsHistoryLong: autoid.PerformanceSchemaDBID + 9,
tableNamePreparedStatementsInstances: autoid.PerformanceSchemaDBID + 10,
tableNameEventsTransactionsCurrent: autoid.PerformanceSchemaDBID + 11,
tableNameEventsTransactionsHistory: autoid.PerformanceSchemaDBID + 12,
tableNameEventsTransactionsHistoryLong: autoid.PerformanceSchemaDBID + 13,
tableNameEventsStagesCurrent: autoid.PerformanceSchemaDBID + 14,
tableNameEventsStagesHistory: autoid.PerformanceSchemaDBID + 15,
tableNameEventsStagesHistoryLong: autoid.PerformanceSchemaDBID + 16,
tableNameEventsStatementsSummaryByDigest: autoid.PerformanceSchemaDBID + 17,
tableNameTiDBProfileCPU: autoid.PerformanceSchemaDBID + 18,
tableNameTiDBProfileMemory: autoid.PerformanceSchemaDBID + 19,
tableNameTiDBProfileMutex: autoid.PerformanceSchemaDBID + 20,
tableNameTiDBProfileAllocs: autoid.PerformanceSchemaDBID + 21,
tableNameTiDBProfileBlock: autoid.PerformanceSchemaDBID + 22,
tableNameTiDBProfileGoroutines: autoid.PerformanceSchemaDBID + 23,
tableNameTiKVProfileCPU: autoid.PerformanceSchemaDBID + 24,
tableNamePDProfileCPU: autoid.PerformanceSchemaDBID + 25,
tableNamePDProfileMemory: autoid.PerformanceSchemaDBID + 26,
tableNamePDProfileMutex: autoid.PerformanceSchemaDBID + 27,
tableNamePDProfileAllocs: autoid.PerformanceSchemaDBID + 28,
tableNamePDProfileBlock: autoid.PerformanceSchemaDBID + 29,
tableNamePDProfileGoroutines: autoid.PerformanceSchemaDBID + 30,
tableNameEventsStatementsSummaryByDigestHistory: autoid.PerformanceSchemaDBID + 31,
tableNameClusterEventsStatementsSummaryByDigest: autoid.PerformanceSchemaDBID + 32,
tableNameClusterEventsStatementsSummaryByDigestHistory: autoid.PerformanceSchemaDBID + 33,
}

// perfSchemaTable stands for the fake table all its data is in the memory.
type perfSchemaTable struct {
infoschema.VirtualTable
meta *model.TableInfo
cols []*table.Column
tp table.Type
}

var pluginTable = make(map[string]func(autoid.Allocators, *model.TableInfo) (table.Table, error))
Expand All @@ -135,9 +140,15 @@ func createPerfSchemaTable(meta *model.TableInfo) *perfSchemaTable {
col := table.ToColumn(colInfo)
columns = append(columns, col)
}
tp := table.VirtualTable
switch meta.Name.L {
case tableNameClusterEventsStatementsSummaryByDigest, tableNameClusterEventsStatementsSummaryByDigestHistory:
tp = table.ClusterTable
}
t := &perfSchemaTable{
meta: meta,
cols: columns,
tp: tp,
}
return t
}
Expand Down Expand Up @@ -172,6 +183,11 @@ func (vt *perfSchemaTable) Meta() *model.TableInfo {
return vt.meta
}

// Type implements table.Table Type interface.
func (vt *perfSchemaTable) Type() table.Type {
return vt.tp
}

func (vt *perfSchemaTable) getRows(ctx sessionctx.Context, cols []*table.Column) (fullRows [][]types.Datum, err error) {
switch vt.meta.Name.O {
case tableNameEventsStatementsSummaryByDigest:
Expand Down Expand Up @@ -206,6 +222,10 @@ func (vt *perfSchemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/block", false)
case tableNamePDProfileGoroutines:
fullRows, err = dataForRemoteProfile(ctx, "pd", "/pd/api/v1/debug/pprof/goroutine?debug=2", true)
// Data for cluster memory table.
case tableNameClusterEventsStatementsSummaryByDigest, tableNameClusterEventsStatementsSummaryByDigestHistory:
fullRows, err = getClusterMemTableRows(ctx, vt.meta.Name.L)

}
if err != nil {
return
Expand All @@ -224,6 +244,21 @@ func (vt *perfSchemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
return rows, nil
}

func getClusterMemTableRows(ctx sessionctx.Context, tableName string) (rows [][]types.Datum, err error) {
switch tableName {
case tableNameClusterEventsStatementsSummaryByDigest:
rows = stmtsummary.StmtSummaryByDigestMap.ToCurrentDatum()
case tableNameClusterEventsStatementsSummaryByDigestHistory:
rows = stmtsummary.StmtSummaryByDigestMap.ToHistoryDatum()
default:
err = errors.Errorf("unknown cluster table: %v", tableName)
}
if err != nil {
return nil, err
}
return infoschema.AppendHostInfoToRows(rows)
}

// IterRecords implements table.Table IterRecords interface.
func (vt *perfSchemaTable) IterRecords(ctx sessionctx.Context, startKey kv.Key, cols []*table.Column,
fn table.RecordIterFunc) error {
Expand Down
17 changes: 16 additions & 1 deletion infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,13 +1032,28 @@ func (s *testClusterTableSuite) TestSelectClusterTable(c *C) {
prepareSlowLogfile(c, slowLogFileName)
defer os.Remove(slowLogFileName)
tk.MustExec("use information_schema")
tk.MustExec("set @@global.tidb_enable_stmt_summary=1")
tk.MustQuery("select count(*) from `CLUSTER_SLOW_QUERY`").Check(testkit.Rows("1"))
tk.MustQuery("select count(*) from `CLUSTER_PROCESSLIST`").Check(testkit.Rows("1"))
tk.MustQuery("select * from `CLUSTER_PROCESSLIST`").Check(testkit.Rows("1 root 127.0.0.1 <nil> Query 9223372036 0 <nil> 0 :10080"))
tk.MustQuery("select * from `CLUSTER_PROCESSLIST`").Check(testkit.Rows(":10080 1 root 127.0.0.1 <nil> Query 9223372036 0 <nil> 0 "))
tk.MustQuery("select query_time, conn_id from `CLUSTER_SLOW_QUERY` order by time limit 1").Check(testkit.Rows("4.895492 6"))
tk.MustQuery("select count(*) from `CLUSTER_SLOW_QUERY` group by digest").Check(testkit.Rows("1"))
tk.MustQuery("select digest, count(*) from `CLUSTER_SLOW_QUERY` group by digest").Check(testkit.Rows("42a1c8aae6f133e934d4bf0147491709a8812ea05ff8819ec522780fe657b772 1"))
tk.MustQuery("select count(*) from `CLUSTER_SLOW_QUERY` where time > now() group by digest").Check(testkit.Rows())
tk.MustExec("use performance_schema")
re := tk.MustQuery("select * from `CLUSTER_events_statements_summary_by_digest`")
c.Assert(re, NotNil)
c.Assert(len(re.Rows()) > 0, IsTrue)
tk.MustQuery("select * from `CLUSTER_events_statements_summary_by_digest_history`")
c.Assert(re, NotNil)
c.Assert(len(re.Rows()) > 0, IsTrue)
tk.MustExec("set @@global.tidb_enable_stmt_summary=0")
re = tk.MustQuery("select * from `CLUSTER_events_statements_summary_by_digest`")
c.Assert(re, NotNil)
c.Assert(len(re.Rows()) == 0, IsTrue)
tk.MustQuery("select * from `CLUSTER_events_statements_summary_by_digest_history`")
c.Assert(re, NotNil)
c.Assert(len(re.Rows()) == 0, IsTrue)
}

func (s *testTableSuite) TestSelectHiddenColumn(c *C) {
Expand Down

0 comments on commit ca9ecf9

Please sign in to comment.