Skip to content

Commit

Permalink
infoschema: add current txn start time in processlist table (#11491)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and jackysp committed Jul 29, 2019
1 parent 6ba79d4 commit 05b4e22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
3 changes: 2 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down
31 changes: 16 additions & 15 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"os"
"strconv"
"strings"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/parser/auth"
Expand Down Expand Up @@ -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(
Expand All @@ -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", "<nil>"),
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)),
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0 ", "<nil>"),
fmt.Sprintf("2 user-2 localhost <nil> 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(
Expand All @@ -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 <nil> Init DB 9223372036 2 %s 0", strings.Repeat("x", 101)),
fmt.Sprintf("2 user-2 localhost <nil> 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", "<nil>"),
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0 ", "<nil>"),
))
}

Expand Down
2 changes: 1 addition & 1 deletion util/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
13 changes: 11 additions & 2 deletions util/processinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 05b4e22

Please sign in to comment.