Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema: add current txn start time in processlist table (#11491) #16160

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,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 @@ -863,7 +864,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 @@ -19,6 +19,7 @@ import (
"os"
"strconv"
"strings"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/parser/auth"
Expand Down Expand Up @@ -364,8 +365,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 @@ -386,24 +387,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 @@ -417,11 +418,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 @@ -190,7 +190,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 @@ -20,6 +20,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 @@ -65,10 +66,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