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

*: fix 'db' and 'Info' column of 'show processlist' #10985

Merged
merged 3 commits into from
Jul 1, 2019
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
2 changes: 1 addition & 1 deletion infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,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},
Expand Down
46 changes: 46 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,52 @@ func (s *testTableSuite) TestSomeTables(c *C) {
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{
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;").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)),
))
tk.MustQuery("SHOW PROCESSLIST;").Sort().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", 100)),
))
tk.MustQuery("SHOW FULL PROCESSLIST;").Sort().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)),
))
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)),
))
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>"),
))
}

func (s *testTableSuite) TestSchemataCharacterSet(c *C) {
Expand Down
13 changes: 11 additions & 2 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,14 +965,23 @@ 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{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about not changing the type of Info from string to interface{}. We can do it this way:

info := "NULL"
if len(sql) > 0 {
	info = sql
}

Copy link
Member

@zz-jason zz-jason Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. Could you add a test like:

select * from information_schema.processlist where db is null;
select * from information_schema.processlist where info is null;

if len(sql) > 0 {
info = sql
}
pi := util.ProcessInfo{
ID: s.sessionVars.ConnectionID,
DB: s.sessionVars.CurrentDB,
DB: db,
Command: command,
Plan: s.currentPlan,
Time: t,
State: s.Status(),
Info: sql,
Info: info,
CurTxnStartTS: s.sessionVars.TxnCtx.StartTS,
StmtCtx: s.sessionVars.StmtCtx,
StatsInfo: plannercore.GetStatsInfo,
Expand Down
11 changes: 7 additions & 4 deletions util/expensivequery/expensivequery.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (eqh *Handle) Run() {
case <-ticker.C:
processInfo := eqh.sm.ShowProcessList()
for _, info := range processInfo {
if len(info.Info) == 0 || info.ExceedExpensiveTimeThresh {
if info.Info == nil || info.ExceedExpensiveTimeThresh {
continue
}
costTime := time.Since(info.Time)
Expand Down Expand Up @@ -124,8 +124,8 @@ func logExpensiveQuery(costTime time.Duration, info *util.ProcessInfo) {
if len(info.User) > 0 {
logFields = append(logFields, zap.String("user", info.User))
}
if len(info.DB) > 0 {
logFields = append(logFields, zap.String("database", info.DB))
if info.DB != nil && len(info.DB.(string)) > 0 {
XuHuaiyu marked this conversation as resolved.
Show resolved Hide resolved
logFields = append(logFields, zap.String("database", info.DB.(string)))
}
var tableIDs, indexIDs string
if len(info.StmtCtx.TableIDs) > 0 {
Expand All @@ -142,7 +142,10 @@ func logExpensiveQuery(costTime time.Duration, info *util.ProcessInfo) {
}

const logSQLLen = 1024 * 8
sql := info.Info
var sql string
if info.Info != nil {
sql = info.Info.(string)
}
if len(sql) > logSQLLen {
sql = fmt.Sprintf("%s len(%d)", sql[:logSQLLen], len(sql))
}
Expand Down
16 changes: 9 additions & 7 deletions util/processinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ type ProcessInfo struct {
ID uint64
User string
Host string
DB string
DB interface{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use interface{} here is a bad idea

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any better idea? @tiancaiamao

Command byte
Plan interface{}
Time time.Time
State uint16
Info string
Info interface{}
CurTxnStartTS uint64
StmtCtx *stmtctx.StatementContext
StatsInfo func(interface{}) map[string]uint64
Expand All @@ -43,11 +43,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{}{
Expand Down