Skip to content

Commit

Permalink
parser, executor: truncate info field for show processlist and show f…
Browse files Browse the repository at this point in the history
…ull processlist support (#4739)
  • Loading branch information
darren authored and zz-jason committed Oct 26, 2017
1 parent 5f74127 commit e40ad67
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
11 changes: 10 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ func (e *ShowExec) fetchShowProcessList() error {
if len(pi.Info) != 0 {
t = uint64(time.Since(pi.Time) / time.Second)
}

var info string
if e.Full {
info = pi.Info
} else {
info = fmt.Sprintf("%.100v", pi.Info)
}

row := []types.Datum{
types.NewUintDatum(pi.ID),
types.NewStringDatum(pi.User),
Expand All @@ -185,10 +193,11 @@ func (e *ShowExec) fetchShowProcessList() error {
types.NewStringDatum(pi.Command),
types.NewUintDatum(t),
types.NewStringDatum(fmt.Sprintf("%d", pi.State)),
types.NewStringDatum(pi.Info),
types.NewStringDatum(info),
}
e.rows = append(e.rows, row)
}

return nil
}

Expand Down
33 changes: 33 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/privilege/privileges"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/auth"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
Expand Down Expand Up @@ -138,6 +139,7 @@ func (s *testSuite) TestShow(c *C) {
tk.MustQuery("SHOW PROCEDURE STATUS WHERE Db='test'").Check(testkit.Rows())
tk.MustQuery("SHOW TRIGGERS WHERE `Trigger` ='test'").Check(testkit.Rows())
tk.MustQuery("SHOW PROCESSLIST;").Check(testkit.Rows())
tk.MustQuery("SHOW FULL PROCESSLIST;").Check(testkit.Rows())
tk.MustQuery("SHOW EVENTS WHERE Db = 'test'").Check(testkit.Rows())
tk.MustQuery("SHOW PLUGINS").Check(testkit.Rows())
tk.MustQuery("SHOW PROFILES").Check(testkit.Rows())
Expand Down Expand Up @@ -307,6 +309,37 @@ func (s *testSuite) TestShowVisibility(c *C) {
tk.MustExec("drop database showdatabase")
}

// mockSessionManager is a mocked session manager that wraps one session
// it returns only this session's current proccess info as processlist for test.
type mockSessionManager struct {
tidb.Session
}

// ShowProcessList implements the SessionManager.ShowProcessList interface.
func (msm *mockSessionManager) ShowProcessList() []util.ProcessInfo {
return []util.ProcessInfo{msm.ShowProcess()}
}

// Kill implements the SessionManager.Kill interface.
func (msm *mockSessionManager) Kill(cid uint64, query bool) {
}

func (s *testSuite) TestShowFullProcessList(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("select 1") // for tk.Se init

se := tk.Se
se.SetSessionManager(&mockSessionManager{se})

fullSQL := "show full processlist"
simpSQL := "show processlist"

tk.MustQuery(fullSQL).Check(testutil.RowsWithSep("|", "218| Query|0|2|"+fullSQL))
tk.MustQuery(simpSQL).Check(testutil.RowsWithSep("|", "218| Query|0|2|"+simpSQL[:100]))

se.SetSessionManager(nil) // reset sm so other tests won't use this
}

type stats struct {
}

Expand Down
3 changes: 2 additions & 1 deletion parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -4524,10 +4524,11 @@ ShowStmt:
User: $4.(*auth.UserIdentity),
}
}
| "SHOW" "PROCESSLIST"
| "SHOW" OptFull "PROCESSLIST"
{
$$ = &ast.ShowStmt{
Tp: ast.ShowProcessList,
Full: $2.(bool),
}
}
| "SHOW" "STATS_META" ShowLikeOrWhereOpt
Expand Down
1 change: 1 addition & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,7 @@ func (s *testParserSuite) TestSessionManage(c *C) {
{"kill tidb connection 23123", true},
{"kill tidb query 23123", true},
{"show processlist", true},
{"show full processlist", true},
}
s.RunTest(c, table)
}
Expand Down

0 comments on commit e40ad67

Please sign in to comment.