From 9785fd225f2a1888ed1077c5084472976e26b5eb Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Fri, 28 Jun 2019 18:42:49 +0800 Subject: [PATCH 1/3] *: fix 'db' and 'Info' column of 'show processlist' --- infoschema/tables.go | 2 +- infoschema/tables_test.go | 38 +++++++++++++++++++++++++++ session/session.go | 13 +++++++-- util/expensivequery/expensivequery.go | 2 +- util/processinfo.go | 16 ++++++----- 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/infoschema/tables.go b/infoschema/tables.go index 1587ac85bee80..7f74a48c1d0c9 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -526,7 +526,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 b4a3125e643dd..6f9698e796c90 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -334,6 +334,44 @@ func (s *testTableSuite) TestSomeTables(c *C) { fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", "do something"), fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)), )) + + 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;").Sort().Check( + testkit.Rows( + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", ""), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s 0", 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)), + )) } func (s *testSuite) TestSchemataCharacterSet(c *C) { diff --git a/session/session.go b/session/session.go index 244f3974f907a..1d5deb97284ab 100644 --- a/session/session.go +++ b/session/session.go @@ -792,13 +792,22 @@ func (s *session) ParseSQL(ctx context.Context, sql, charset, collation string) } func (s *session) SetProcessInfo(sql string, t time.Time, command byte, maxExecutionTime uint64) { + 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, Time: t, State: s.Status(), - Info: sql, + Info: info, StmtCtx: s.sessionVars.StmtCtx, MaxExecutionTime: maxExecutionTime, diff --git a/util/expensivequery/expensivequery.go b/util/expensivequery/expensivequery.go index 6a164d98f757c..2df481c112bac 100644 --- a/util/expensivequery/expensivequery.go +++ b/util/expensivequery/expensivequery.go @@ -49,7 +49,7 @@ func (eqh *Handle) Run() { case <-ticker.C: processInfo := eqh.sm.ShowProcessList() for _, info := range processInfo { - if len(info.Info) == 0 { + if info.Info == nil { continue } costTime := time.Since(info.Time) diff --git a/util/processinfo.go b/util/processinfo.go index c1e178b09771a..9f3a3f65d7e3b 100644 --- a/util/processinfo.go +++ b/util/processinfo.go @@ -26,11 +26,11 @@ type ProcessInfo struct { ID uint64 User string Host string - DB string + DB interface{} Command byte Time time.Time State uint16 - Info string + Info interface{} StmtCtx *stmtctx.StatementContext // MaxExecutionTime is the timeout for select statement, in milliseconds. // If the query takes too long, kill it. @@ -39,11 +39,13 @@ type ProcessInfo struct { // ToRowForShow returns []interface{} for the row data of "SHOW [FULL] PROCESSLIST". func (pi *ProcessInfo) ToRowForShow(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{}{ From 5f2a91dedf3254b0c2dc9a7903e557495ba4c465 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Mon, 1 Jul 2019 11:20:55 +0800 Subject: [PATCH 2/3] add test cases --- infoschema/tables_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 6f9698e796c90..549ebed7d22fd 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -372,6 +372,14 @@ func (s *testTableSuite) TestSomeTables(c *C) { 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;").Sort().Check( + testkit.Rows( + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)), + )) + tk.MustQuery("select * from information_schema.PROCESSLIST where Info is null;").Sort().Check( + testkit.Rows( + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", ""), + )) } func (s *testSuite) TestSchemataCharacterSet(c *C) { From 5ccced3aeb59758ccb6edf8d73aa1af8631c5f97 Mon Sep 17 00:00:00 2001 From: wshwsh12 <793703860@qq.com> Date: Mon, 1 Jul 2019 14:40:29 +0800 Subject: [PATCH 3/3] fix test cases --- infoschema/tables_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 549ebed7d22fd..76b34497a548c 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -357,26 +357,26 @@ func (s *testTableSuite) TestSomeTables(c *C) { StmtCtx: tk.Se.GetSessionVars().StmtCtx, } tk.Se.SetSessionManager(sm) - tk.MustQuery("select * from information_schema.PROCESSLIST order by ID;").Sort().Check( + 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 0", ""), fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s 0", 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)), + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", ""), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s 0", 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)), + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", ""), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)), )) - tk.MustQuery("select * from information_schema.PROCESSLIST where db is null;").Sort().Check( + 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 0", strings.Repeat("x", 101)), )) - tk.MustQuery("select * from information_schema.PROCESSLIST where Info is null;").Sort().Check( + 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 0", ""), ))