From 3cc902159f7d12da6726420bbd039084491f1db4 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Wed, 3 Jul 2019 11:18:12 +0800 Subject: [PATCH] *: fix 'db' and 'Info' column of 'show processlist' (#10985) (#11003) --- infoschema/tables.go | 2 +- infoschema/tables_test.go | 47 +++++++++++++++++++++++++++ session/session.go | 13 ++++++-- util/expensivequery/expensivequery.go | 11 ++++--- util/processinfo.go | 16 +++++---- 5 files changed, 75 insertions(+), 14 deletions(-) diff --git a/infoschema/tables.go b/infoschema/tables.go index 4a64164f69127..2ec17dc7f7b75 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -538,7 +538,7 @@ var tableProcesslistCols = []columnInfo{ {"ID", mysql.TypeLonglong, 21, mysql.NotNullFlag, 0, nil}, {"USER", mysql.TypeVarchar, 16, mysql.NotNullFlag, "", nil}, {"HOST", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil}, - {"DB", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil}, + {"DB", mysql.TypeVarchar, 64, 0, nil, nil}, {"COMMAND", mysql.TypeVarchar, 16, mysql.NotNullFlag, "", nil}, {"TIME", mysql.TypeLong, 7, mysql.NotNullFlag, 0, nil}, {"STATE", mysql.TypeVarchar, 7, 0, nil, nil}, diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 6788d585656d3..057ec4082ed25 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -17,6 +17,7 @@ import ( "fmt" "os" "strconv" + "strings" . "github.com/pingcap/check" "github.com/pingcap/parser/auth" @@ -288,6 +289,52 @@ func (s *testTableSuite) TestSomeTables(c *C) { tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Check( testkit.Rows("1 user-1 localhost information_schema Quit 9223372036 1 do something", "2 user-2 localhost test Init DB 9223372036 2 do something")) + + sm = &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2)} + sm.processInfoMap[1] = &util.ProcessInfo{ + ID: 1, + User: "user-1", + Host: "localhost", + DB: "information_schema", + Command: byte(1), + State: 1, + Info: nil, + StmtCtx: tk.Se.GetSessionVars().StmtCtx, + } + sm.processInfoMap[2] = &util.ProcessInfo{ + ID: 2, + User: "user-2", + Host: "localhost", + DB: nil, + Command: byte(2), + State: 2, + Info: strings.Repeat("x", 101), + StmtCtx: tk.Se.GetSessionVars().StmtCtx, + } + tk.Se.SetSessionManager(sm) + tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Check( + testkit.Rows( + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", ""), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s", strings.Repeat("x", 101)), + )) + tk.MustQuery("SHOW PROCESSLIST;").Sort().Check( + testkit.Rows( + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", ""), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s", strings.Repeat("x", 100)), + )) + tk.MustQuery("SHOW FULL PROCESSLIST;").Sort().Check( + testkit.Rows( + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", ""), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s", strings.Repeat("x", 101)), + )) + tk.MustQuery("select * from information_schema.PROCESSLIST where db is null;").Check( + testkit.Rows( + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s", strings.Repeat("x", 101)), + )) + tk.MustQuery("select * from information_schema.PROCESSLIST where Info is null;").Check( + testkit.Rows( + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", ""), + )) } func (s *testTableSuite) TestSchemataCharacterSet(c *C) { diff --git a/session/session.go b/session/session.go index 38c95182a3046..676ae9bc72410 100644 --- a/session/session.go +++ b/session/session.go @@ -908,14 +908,23 @@ func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) } func (s *session) SetProcessInfo(sql string, t time.Time, command byte) { + var db interface{} + if len(s.sessionVars.CurrentDB) > 0 { + db = s.sessionVars.CurrentDB + } + + var info interface{} + if len(sql) > 0 { + info = sql + } pi := util.ProcessInfo{ ID: s.sessionVars.ConnectionID, - DB: s.sessionVars.CurrentDB, + DB: db, Command: command, Plan: s.currentPlan, Time: t, State: s.Status(), - Info: sql, + Info: info, CurTxnStartTS: s.sessionVars.TxnCtx.StartTS, StmtCtx: s.sessionVars.StmtCtx, StatsInfo: plannercore.GetStatsInfo, diff --git a/util/expensivequery/expensivequery.go b/util/expensivequery/expensivequery.go index 2689eac6092a8..1b8df18ad69f5 100644 --- a/util/expensivequery/expensivequery.go +++ b/util/expensivequery/expensivequery.go @@ -60,7 +60,7 @@ func (eqh *Handle) Run() { } processInfo := eqh.sm.ShowProcessList() for _, info := range processInfo { - if len(info.Info) == 0 || info.ExceedExpensiveTimeThresh { + if info.Info == nil || info.ExceedExpensiveTimeThresh { continue } if costTime := time.Since(info.Time); costTime >= curInterval { @@ -126,8 +126,8 @@ func logExpensiveQuery(costTime time.Duration, info *util.ProcessInfo) { if len(info.User) > 0 { logFields = append(logFields, zap.String("user", info.User)) } - if len(info.DB) > 0 { - logFields = append(logFields, zap.String("database", info.DB)) + if info.DB != nil && len(info.DB.(string)) > 0 { + logFields = append(logFields, zap.String("database", info.DB.(string))) } var tableIDs, indexIDs string if len(info.StmtCtx.TableIDs) > 0 { @@ -144,7 +144,10 @@ func logExpensiveQuery(costTime time.Duration, info *util.ProcessInfo) { } const logSQLLen = 1024 * 8 - sql := info.Info + var sql string + if info.Info != nil { + sql = info.Info.(string) + } if len(sql) > logSQLLen { sql = fmt.Sprintf("%s len(%d)", sql[:logSQLLen], len(sql)) } diff --git a/util/processinfo.go b/util/processinfo.go index 956196cdfdb42..6d0fab3e59eb7 100644 --- a/util/processinfo.go +++ b/util/processinfo.go @@ -26,12 +26,12 @@ type ProcessInfo struct { ID uint64 User string Host string - DB string + DB interface{} Command byte Plan interface{} Time time.Time State uint16 - Info string + Info interface{} CurTxnStartTS uint64 StmtCtx *stmtctx.StatementContext StatsInfo func(interface{}) map[string]uint64 @@ -40,11 +40,13 @@ type ProcessInfo struct { // ToRow returns []interface{} for the row data of "show processlist" and "select * from infoschema.processlist". func (pi *ProcessInfo) ToRow(full bool) []interface{} { - var info string - if full { - info = pi.Info - } else { - info = fmt.Sprintf("%.100v", pi.Info) + var info interface{} + if pi.Info != nil { + if full { + info = pi.Info.(string) + } else { + info = fmt.Sprintf("%.100v", pi.Info.(string)) + } } t := uint64(time.Since(pi.Time) / time.Second) return []interface{}{