From 05b4e2299d0ed6656114deb13550de7f1a1ad8b7 Mon Sep 17 00:00:00 2001 From: lysu Date: Mon, 29 Jul 2019 16:11:09 +0800 Subject: [PATCH] infoschema: add current txn start time in processlist table (#11491) --- infoschema/tables.go | 3 ++- infoschema/tables_test.go | 31 ++++++++++++++++--------------- util/misc_test.go | 2 +- util/processinfo.go | 13 +++++++++++-- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/infoschema/tables.go b/infoschema/tables.go index 3a447bb3264ae..e31db88b9af68 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -552,6 +552,7 @@ var tableProcesslistCols = []columnInfo{ {"STATE", mysql.TypeVarchar, 7, 0, nil, nil}, {"INFO", mysql.TypeString, 512, 0, nil, nil}, {"MEM", mysql.TypeLonglong, 21, 0, nil, nil}, + {"TxnStart", mysql.TypeVarchar, 64, mysql.NotNullFlag, "", nil}, } var tableTiDBIndexesCols = []columnInfo{ @@ -871,7 +872,7 @@ func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum { continue } - rows := pi.ToRow() + rows := pi.ToRow(ctx.GetSessionVars().StmtCtx.TimeZone) record := types.MakeDatums(rows...) records = append(records, record) } diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index 13d0999243083..c2c7c84cb1ea5 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -18,6 +18,7 @@ import ( "os" "strconv" "strings" + "time" . "github.com/pingcap/check" "github.com/pingcap/parser/auth" @@ -343,8 +344,8 @@ func (s *testTableSuite) TestSomeTables(c *C) { 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", "do something"), - fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)), + 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)), )) tk.MustQuery("SHOW PROCESSLIST;").Sort().Check( testkit.Rows( @@ -365,24 +366,24 @@ func (s *testTableSuite) TestSomeTables(c *C) { 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, + ID: 2, + User: "user-2", + Host: "localhost", + Command: byte(2), + State: 2, + Info: strings.Repeat("x", 101), + StmtCtx: tk.Se.GetSessionVars().StmtCtx, + CurTxnStartTS: 410090409861578752, } tk.Se.SetSessionManager(sm) + tk.Se.GetSessionVars().TimeZone = time.UTC 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)), + 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 07-29 03:26:05.158(410090409861578752)", strings.Repeat("x", 101)), )) tk.MustQuery("SHOW PROCESSLIST;").Sort().Check( testkit.Rows( @@ -396,11 +397,11 @@ func (s *testTableSuite) TestSomeTables(c *C) { )) 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)), + fmt.Sprintf("2 user-2 localhost Init DB 9223372036 2 %s 0 07-29 03:26:05.158(410090409861578752)", 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 0", ""), + fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0 ", ""), )) } diff --git a/util/misc_test.go b/util/misc_test.go index 7c365a98fdb5a..dba9ccd4eabf1 100644 --- a/util/misc_test.go +++ b/util/misc_test.go @@ -172,7 +172,7 @@ func (s *testMiscSuite) TestBasicFunc(c *C) { c.Assert(row[6], Equals, "1") c.Assert(row[7], Equals, "test") - row3 := pi.ToRow() + row3 := pi.ToRow(time.UTC) c.Assert(row3[:8], DeepEquals, row) c.Assert(row3[8], Equals, int64(0)) diff --git a/util/processinfo.go b/util/processinfo.go index b09edf810b184..9d95ff72b56b9 100644 --- a/util/processinfo.go +++ b/util/processinfo.go @@ -19,6 +19,7 @@ import ( "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/sessionctx/stmtctx" + "github.com/pingcap/tidb/store/tikv/oracle" ) // ProcessInfo is a struct used for show processlist statement. @@ -64,10 +65,18 @@ func (pi *ProcessInfo) ToRowForShow(full bool) []interface{} { } } +func (pi *ProcessInfo) txnStartTs(tz *time.Location) (txnStart string) { + if pi.CurTxnStartTS > 0 { + physicalTime := oracle.GetTimeFromTS(pi.CurTxnStartTS) + txnStart = fmt.Sprintf("%s(%d)", physicalTime.In(tz).Format("01-02 15:04:05.000"), pi.CurTxnStartTS) + } + return +} + // ToRow returns []interface{} for the row data of // "SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST". -func (pi *ProcessInfo) ToRow() []interface{} { - return append(pi.ToRowForShow(true), pi.StmtCtx.MemTracker.BytesConsumed()) +func (pi *ProcessInfo) ToRow(tz *time.Location) []interface{} { + return append(pi.ToRowForShow(true), pi.StmtCtx.MemTracker.BytesConsumed(), pi.txnStartTs(tz)) } // SessionManager is an interface for session manage. Show processlist and