Skip to content

Commit

Permalink
*: add a column describing memory usage for table information_schema.…
Browse files Browse the repository at this point in the history
…processlist (#10837) (#12807)
  • Loading branch information
SunRunAway authored and sre-bot committed Oct 18, 2019
1 parent 1983b54 commit b96bfec
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 15 deletions.
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (e *ShowExec) fetchShowProcessList() error {
if !hasProcessPriv && pi.User != loginUser.Username {
continue
}
row := pi.ToRow(e.Full)
row := pi.ToRowForShow(e.Full)
e.appendRow(row)
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ var tableProcesslistCols = []columnInfo{
{"TIME", mysql.TypeLong, 7, mysql.NotNullFlag, 0, nil},
{"STATE", mysql.TypeVarchar, 7, 0, nil, nil},
{"INFO", mysql.TypeString, 512, 0, nil, nil},
{"MEM", mysql.TypeLonglong, 21, 0, nil, nil},
}

var tableTiDBIndexesCols = []columnInfo{
Expand Down Expand Up @@ -862,7 +863,7 @@ func dataForProcesslist(ctx sessionctx.Context) [][]types.Datum {
continue
}

rows := pi.ToRow(true)
rows := pi.ToRow()
record := types.MakeDatums(rows...)
records = append(records, record)
}
Expand Down
35 changes: 26 additions & 9 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (s *testTableSuite) TestInfoschemaFieldValue(c *C) {
User: "root",
Host: "127.0.0.1",
Command: mysql.ComQuery,
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
}
tk.Se.SetSessionManager(sm)
tk.MustQuery("SELECT user,host,command FROM information_schema.processlist;").Check(testkit.Rows("root 127.0.0.1 Query"))
Expand Down Expand Up @@ -342,19 +343,35 @@ func (s *testTableSuite) TestSomeTables(c *C) {
DB: "information_schema",
Command: byte(1),
State: 1,
Info: "do something"}
Info: "do something",
StmtCtx: tk.Se.GetSessionVars().StmtCtx,
}
sm.processInfoMap[2] = &util.ProcessInfo{
ID: 2,
User: "user-2",
Host: "localhost",
DB: "test",
Command: byte(2),
State: 2,
Info: "do something"}
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("1 user-1 localhost information_schema Quit 9223372036 1 do something",
"2 user-2 localhost test Init DB 9223372036 2 do something"))
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)),
))
tk.MustQuery("SHOW PROCESSLIST;").Sort().Check(
testkit.Rows(
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s", "do something"),
fmt.Sprintf("2 user-2 localhost test 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", "do something"),
fmt.Sprintf("2 user-2 localhost test Init DB 9223372036 2 %s", strings.Repeat("x", 101)),
))

sm = &mockSessionManager{make(map[uint64]*util.ProcessInfo, 2)}
sm.processInfoMap[1] = &util.ProcessInfo{
Expand All @@ -380,8 +397,8 @@ func (s *testTableSuite) TestSomeTables(c *C) {
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", "<nil>"),
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s", 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", strings.Repeat("x", 101)),
))
tk.MustQuery("SHOW PROCESSLIST;").Sort().Check(
testkit.Rows(
Expand All @@ -395,11 +412,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", strings.Repeat("x", 101)),
fmt.Sprintf("2 user-2 localhost <nil> Init DB 9223372036 2 %s 0", 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", "<nil>"),
fmt.Sprintf("1 user-1 localhost information_schema Quit 9223372036 1 %s 0", "<nil>"),
))
}

Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ type LogicalUnionScan struct {
conditions []expression.Expression
}

// DataSource represents a tablescan without condition push down.
// DataSource represents a tableScan without condition push down.
type DataSource struct {
logicalSchemaProducer

Expand Down
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var (
sessionExecuteParseDurationGeneral = metrics.SessionExecuteParseDuration.WithLabelValues(metrics.LblGeneral)
)

// Session context
// Session context, it is consistent with the lifecycle of a client connection.
type Session interface {
sessionctx.Context
Status() uint16 // Flag of current status, such as autocommit.
Expand Down
69 changes: 69 additions & 0 deletions util/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
package util

import (
"bytes"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/parser"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/stringutil"
"github.com/pingcap/tidb/util/testleak"
)

Expand Down Expand Up @@ -113,3 +120,65 @@ func (s *testMiscSuite) TestCompatibleParseGCTime(c *C) {
c.Assert(err, NotNil)
}
}

func (s *testMiscSuite) TestBasicFunc(c *C) {
// Test for GetStack.
b := GetStack()
c.Assert(len(b) < 4096, IsTrue)

// Test for WithRecovery.
var recover interface{}
WithRecovery(func() {
panic("test")
}, func(r interface{}) {
recover = r
})
c.Assert(recover, Equals, "test")

// Test for SyntaxError.
c.Assert(SyntaxError(nil), IsNil)
c.Assert(terror.ErrorEqual(SyntaxError(errors.New("test")), parser.ErrParse), IsTrue)
c.Assert(terror.ErrorEqual(SyntaxError(parser.ErrSyntax.GenWithStackByArgs()), parser.ErrSyntax), IsTrue)

// Test for SyntaxWarn.
c.Assert(SyntaxWarn(nil), IsNil)
c.Assert(terror.ErrorEqual(SyntaxWarn(errors.New("test")), parser.ErrParse), IsTrue)

// Test for ProcessInfo.
pi := ProcessInfo{
ID: 1,
User: "test",
Host: "www",
DB: "db",
Command: mysql.ComSleep,
Plan: nil,
Time: time.Now(),
State: 1,
Info: "test",
StmtCtx: &stmtctx.StatementContext{
MemTracker: memory.NewTracker(stringutil.StringerStr(""), -1),
},
}
row := pi.ToRowForShow(false)
row2 := pi.ToRowForShow(true)
c.Assert(row, DeepEquals, row2)
c.Assert(len(row), Equals, 8)
c.Assert(row[0], Equals, pi.ID)
c.Assert(row[1], Equals, pi.User)
c.Assert(row[2], Equals, pi.Host)
c.Assert(row[3], Equals, pi.DB)
c.Assert(row[4], Equals, "Sleep")
c.Assert(row[5], Equals, uint64(0))
c.Assert(row[6], Equals, "1")
c.Assert(row[7], Equals, "test")

row3 := pi.ToRow()
c.Assert(row3[:8], DeepEquals, row)
c.Assert(row3[8], Equals, int64(0))

// Test for RandomBuf.
buf := RandomBuf(5)
c.Assert(len(buf), Equals, 5)
c.Assert(bytes.Contains(buf, []byte("$")), IsFalse)
c.Assert(bytes.Contains(buf, []byte{0}), IsFalse)
}
10 changes: 8 additions & 2 deletions util/processinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ type ProcessInfo struct {
MaxExecutionTime uint64
}

// ToRow returns []interface{} for the row data of "show processlist" and "select * from infoschema.processlist".
func (pi *ProcessInfo) ToRow(full bool) []interface{} {
// ToRowForShow returns []interface{} for the row data of "SHOW [FULL] PROCESSLIST".
func (pi *ProcessInfo) ToRowForShow(full bool) []interface{} {
var info interface{}
if pi.Info != nil {
if full {
Expand All @@ -64,6 +64,12 @@ func (pi *ProcessInfo) ToRow(full bool) []interface{} {
}
}

// 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())
}

// SessionManager is an interface for session manage. Show processlist and
// kill statement rely on this interface.
type SessionManager interface {
Expand Down

0 comments on commit b96bfec

Please sign in to comment.