From 918df7d3ef43d18d4497653f0e8482c59aa4edc5 Mon Sep 17 00:00:00 2001 From: Qiannan Lyu Date: Thu, 12 Mar 2020 23:26:21 +1300 Subject: [PATCH] ucp: migrate dataForProcesslist & dataForClusterProcesslist --- executor/builder.go | 2 ++ executor/infoschema_reader.go | 45 +++++++++++++++++++++++++++++++++++ infoschema/cluster.go | 10 ++------ infoschema/tables.go | 44 ++++------------------------------ 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index e27290f01fdb9..e504085581151 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -1432,6 +1432,8 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo strings.ToLower(infoschema.TableUserPrivileges), strings.ToLower(infoschema.TableMetricTables), strings.ToLower(infoschema.TableCollationCharacterSetApplicability), + strings.ToLower(infoschema.TableProcesslist), + strings.ToLower(infoschema.ClusterTableProcesslist), strings.ToLower(infoschema.TableTiKVRegionPeers), strings.ToLower(infoschema.TableTiDBHotRegions), strings.ToLower(infoschema.TableSessionVar), diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index e9d225aef9f3f..14fd87bb7fd3e 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -88,6 +88,10 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex e.setDataForMetricTables(sctx) case infoschema.TableCollationCharacterSetApplicability: e.dataForCollationCharacterSetApplicability() + case infoschema.TableProcesslist: + e.setDataForProcessList(sctx) + case infoschema.ClusterTableProcesslist: + err = e.setDataForClusterProcessList(sctx) case infoschema.TableUserPrivileges: e.setDataFromUserPrivileges(sctx) case infoschema.TableTiKVRegionPeers: @@ -793,6 +797,47 @@ func (e *memtableRetriever) setDataFromKeyColumnUsage(ctx sessionctx.Context, sc e.rows = rows } +func (e *memtableRetriever) setDataForClusterProcessList(ctx sessionctx.Context) error { + e.setDataForProcessList(ctx) + rows, err := infoschema.AppendHostInfoToRows(e.rows) + if err != nil { + return err + } + e.rows = rows + return nil +} + +func (e *memtableRetriever) setDataForProcessList(ctx sessionctx.Context) { + sm := ctx.GetSessionManager() + if sm == nil { + return + } + + loginUser := ctx.GetSessionVars().User + var hasProcessPriv bool + if pm := privilege.GetPrivilegeManager(ctx); pm != nil { + if pm.RequestVerification(ctx.GetSessionVars().ActiveRoles, "", "", "", mysql.ProcessPriv) { + hasProcessPriv = true + } + } + + pl := sm.ShowProcessList() + + records := make([][]types.Datum, 0, len(pl)) + for _, pi := range pl { + // If you have the PROCESS privilege, you can see all threads. + // Otherwise, you can see only your own threads. + if !hasProcessPriv && loginUser != nil && pi.User != loginUser.Username { + continue + } + + rows := pi.ToRow(ctx.GetSessionVars().StmtCtx.TimeZone) + record := types.MakeDatums(rows...) + records = append(records, record) + } + e.rows = records +} + func (e *memtableRetriever) setDataFromUserPrivileges(ctx sessionctx.Context) { pm := privilege.GetPrivilegeManager(ctx) e.rows = pm.UserPrivilegesTable() diff --git a/infoschema/cluster.go b/infoschema/cluster.go index 2bb5505ed76e6..097cfb0c12447 100644 --- a/infoschema/cluster.go +++ b/infoschema/cluster.go @@ -19,7 +19,6 @@ import ( "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/domain/infosync" - "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util" ) @@ -30,13 +29,13 @@ import ( const ( // ClusterTableSlowLog is the string constant of cluster slow query memory table. ClusterTableSlowLog = "CLUSTER_SLOW_QUERY" - clusterTableProcesslist = "CLUSTER_PROCESSLIST" + ClusterTableProcesslist = "CLUSTER_PROCESSLIST" ) // memTableToClusterTables means add memory table to cluster table. var memTableToClusterTables = map[string]string{ TableSlowQuery: ClusterTableSlowLog, - tableProcesslist: clusterTableProcesslist, + TableProcesslist: ClusterTableProcesslist, } func init() { @@ -72,11 +71,6 @@ func isClusterTableByName(dbName, tableName string) bool { return false } -func dataForClusterProcesslist(ctx sessionctx.Context) (rows [][]types.Datum, err error) { - rows = dataForProcesslist(ctx) - return AppendHostInfoToRows(rows) -} - // AppendHostInfoToRows appends host info to the rows. func AppendHostInfoToRows(rows [][]types.Datum) ([][]types.Datum, error) { serverInfo, err := infosync.GetServerInfo() diff --git a/infoschema/tables.go b/infoschema/tables.go index f0e073ee1f427..a23791569755e 100755 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -93,7 +93,8 @@ const ( tableTableSpaces = "TABLESPACES" // TableCollationCharacterSetApplicability is the string constant of infoschema memory table. TableCollationCharacterSetApplicability = "COLLATION_CHARACTER_SET_APPLICABILITY" - tableProcesslist = "PROCESSLIST" + // TableProcesslist is the string constant of infoschema table. + TableProcesslist = "PROCESSLIST" // TableTiDBIndexes is the string constant of infoschema table TableTiDBIndexes = "TIDB_INDEXES" // TableTiDBHotRegions is the string constant of infoschema table @@ -166,7 +167,7 @@ var tableIDMap = map[string]int64{ tableOptimizerTrace: autoid.InformationSchemaDBID + 30, tableTableSpaces: autoid.InformationSchemaDBID + 31, TableCollationCharacterSetApplicability: autoid.InformationSchemaDBID + 32, - tableProcesslist: autoid.InformationSchemaDBID + 33, + TableProcesslist: autoid.InformationSchemaDBID + 33, TableTiDBIndexes: autoid.InformationSchemaDBID + 34, TableSlowQuery: autoid.InformationSchemaDBID + 35, TableTiDBHotRegions: autoid.InformationSchemaDBID + 36, @@ -180,7 +181,7 @@ var tableIDMap = map[string]int64{ TableClusterLoad: autoid.InformationSchemaDBID + 44, tableTiFlashReplica: autoid.InformationSchemaDBID + 45, ClusterTableSlowLog: autoid.InformationSchemaDBID + 46, - clusterTableProcesslist: autoid.InformationSchemaDBID + 47, + ClusterTableProcesslist: autoid.InformationSchemaDBID + 47, TableClusterLog: autoid.InformationSchemaDBID + 48, TableClusterHardware: autoid.InformationSchemaDBID + 49, TableClusterSystemInfo: autoid.InformationSchemaDBID + 50, @@ -1080,36 +1081,6 @@ func dataForTiKVStoreStatus(ctx sessionctx.Context) (records [][]types.Datum, er return records, nil } -func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum { - sm := ctx.GetSessionManager() - if sm == nil { - return nil - } - - loginUser := ctx.GetSessionVars().User - var hasProcessPriv bool - if pm := privilege.GetPrivilegeManager(ctx); pm != nil { - if pm.RequestVerification(ctx.GetSessionVars().ActiveRoles, "", "", "", mysql.ProcessPriv) { - hasProcessPriv = true - } - } - - pl := sm.ShowProcessList() - records := make([][]types.Datum, 0, len(pl)) - for _, pi := range pl { - // If you have the PROCESS privilege, you can see all threads. - // Otherwise, you can see only your own threads. - if !hasProcessPriv && loginUser != nil && pi.User != loginUser.Username { - continue - } - - rows := pi.ToRow(ctx.GetSessionVars().StmtCtx.TimeZone) - record := types.MakeDatums(rows...) - records = append(records, record) - } - return records -} - // GetShardingInfo returns a nil or description string for the sharding information of given TableInfo. // The returned description string may be: // - "NOT_SHARDED": for tables that SHARD_ROW_ID_BITS is not specified. @@ -1556,7 +1527,7 @@ var tableNameToColumns = map[string][]columnInfo{ tableOptimizerTrace: tableOptimizerTraceCols, tableTableSpaces: tableTableSpacesCols, TableCollationCharacterSetApplicability: tableCollationCharacterSetApplicabilityCols, - tableProcesslist: tableProcesslistCols, + TableProcesslist: tableProcesslistCols, TableTiDBIndexes: tableTiDBIndexesCols, TableSlowQuery: slowQueryCols, TableTiDBHotRegions: TableTiDBHotRegionsCols, @@ -1639,8 +1610,6 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column) case tableSessionStatus: case tableOptimizerTrace: case tableTableSpaces: - case tableProcesslist: - fullRows = dataForProcesslist(ctx) case tableTiKVStoreStatus: fullRows, err = dataForTiKVStoreStatus(ctx) case tableAnalyzeStatus: @@ -1651,9 +1620,6 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column) fullRows, err = dataForServersInfo() case tableTiFlashReplica: fullRows = dataForTableTiFlashReplica(ctx, dbs) - // Data for cluster processlist memory table. - case clusterTableProcesslist: - fullRows, err = dataForClusterProcesslist(ctx) } if err != nil { return nil, err