Skip to content

Commit f20ba79

Browse files
authored
update information_schema.processlist to correctly display status of processes and databases (#1846)
1 parent 8926ab3 commit f20ba79

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

enginetest/enginetests.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,19 @@ func TestInfoSchema(t *testing.T, h Harness) {
274274
ctx, err := p.BeginQuery(ctx, "SELECT foo")
275275
require.NoError(t, err)
276276

277-
TestQueryWithContext(t, ctx, e, h, "SELECT * FROM information_schema.processlist", []sql.Row{{uint64(1), "root", "localhost", "NULL", "Query", 0, "processlist(processlist (0/? partitions))", "SELECT foo"}}, nil, nil)
277+
p.AddConnection(2, "otherhost")
278+
sess2 := sql.NewBaseSessionWithClientServer("localhost", sql.Client{Address: "otherhost", User: "root"}, 2)
279+
sess2.SetCurrentDatabase("otherdb")
280+
p.ConnectionReady(sess2)
281+
ctx2 := sql.NewContext(context.Background(), sql.WithPid(2), sql.WithSession(sess2))
282+
ctx2, err = p.BeginQuery(ctx2, "SELECT bar")
283+
require.NoError(t, err)
284+
p.EndQuery(ctx2)
285+
286+
TestQueryWithContext(t, ctx, e, h, "SELECT * FROM information_schema.processlist ORDER BY id", []sql.Row{
287+
{uint64(1), "root", "localhost", nil, "Query", 0, "processlist(processlist (0/? partitions))", "SELECT foo"},
288+
{uint64(2), "root", "otherhost", "otherdb", "Sleep", 0, "", ""},
289+
}, nil, nil)
278290
})
279291

280292
for _, tt := range queries.SkippedInfoSchemaQueries {

processlist.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (pl *ProcessList) ConnectionReady(sess sql.Session) {
8080
Host: sess.Client().Address,
8181
User: sess.Client().User,
8282
StartedAt: time.Now(),
83+
Database: sess.GetCurrentDatabase(),
8384
}
8485
}
8586

processlist_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestProcessList(t *testing.T) {
3232
p := NewProcessList()
3333
p.AddConnection(1, clientHostOne)
3434
sess := sql.NewBaseSessionWithClientServer("0.0.0.0:3306", sql.Client{Address: clientHostOne, User: "foo"}, 1)
35+
sess.SetCurrentDatabase("test_db")
3536
p.ConnectionReady(sess)
3637
ctx := sql.NewContext(context.Background(), sql.WithPid(1), sql.WithSession(sess))
3738
ctx, err := p.BeginQuery(ctx, "SELECT foo")
@@ -55,6 +56,7 @@ func TestProcessList(t *testing.T) {
5556
Query: "SELECT foo",
5657
Command: sql.ProcessCommandQuery,
5758
StartedAt: p.procs[1].StartedAt,
59+
Database: "test_db",
5860
}
5961
require.NotNil(p.procs[1].Kill)
6062
p.procs[1].Kill = nil

sql/information_schema/information_schema.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,29 +1063,30 @@ func processListRowIter(ctx *Context, c Catalog) (RowIter, error) {
10631063
processes := ctx.ProcessList.Processes()
10641064
var rows = make([]Row, len(processes))
10651065

1066-
db := ctx.GetCurrentDatabase()
1067-
if db == "" {
1068-
db = "NULL"
1069-
}
1070-
10711066
for i, proc := range processes {
10721067
var status []string
10731068
for name, progress := range proc.Progress {
10741069
status = append(status, fmt.Sprintf("%s(%s)", name, progress))
10751070
}
1076-
if len(status) == 0 {
1071+
if len(status) == 0 && proc.Command == ProcessCommandQuery {
10771072
status = []string{"running"}
10781073
}
10791074
sort.Strings(status)
1075+
1076+
var db interface{}
1077+
if proc.Database != "" {
1078+
db = proc.Database
1079+
}
1080+
10801081
rows[i] = Row{
1081-
uint64(proc.Connection), // id
1082-
proc.User, // user
1083-
ctx.Session.Client().Address, // host
1084-
db, // db
1085-
"Query", // command
1086-
int32(proc.Seconds()), // time
1087-
strings.Join(status, ", "), // state
1088-
proc.Query, // info
1082+
uint64(proc.Connection), // id
1083+
proc.User, // user
1084+
proc.Host, // host
1085+
db, // db
1086+
string(proc.Command), // command
1087+
int32(proc.Seconds()), // time
1088+
strings.Join(status, ", "), // state
1089+
proc.Query, // info
10891090
}
10901091
}
10911092

0 commit comments

Comments
 (0)