Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 65150c0

Browse files
committed
sql: process list now shows client address
Previously it was showing server address but should be the client: https://dev.mysql.com/doc/refman/8.0/en/processlist-table.html Also merged client user and address as Client struct in session. Signed-off-by: Javi Fontan <jfontan@gmail.com>
1 parent a5bdcc8 commit 65150c0

File tree

12 files changed

+45
-24
lines changed

12 files changed

+45
-24
lines changed

auth/common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func testAuthorization(
153153
t.Run(fmt.Sprintf("%s-%s", c.user, c.query), func(t *testing.T) {
154154
req := require.New(t)
155155

156-
session := sql.NewSession("localhost", c.user, uint32(i))
156+
session := sql.NewSession("localhost", "client", c.user, uint32(i))
157157
ctx := sql.NewContext(context.TODO(),
158158
sql.WithSession(session),
159159
sql.WithPid(uint64(i)))

engine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,7 @@ func insertRows(t *testing.T, table sql.Inserter, rows ...sql.Row) {
16741674
var pid uint64
16751675

16761676
func newCtx() *sql.Context {
1677-
session := sql.NewSession("address", "user", 1)
1677+
session := sql.NewSession("address", "client", "user", 1)
16781678
return sql.NewContext(
16791679
context.Background(),
16801680
sql.WithPid(atomic.AddUint64(&pid, 1)),

server/context.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type DoneFunc func()
1818

1919
// DefaultSessionBuilder is a SessionBuilder that returns a base session.
2020
func DefaultSessionBuilder(c *mysql.Conn, addr string) sql.Session {
21-
return sql.NewSession(addr, c.User, c.ConnectionID)
21+
client := c.RemoteAddr().String()
22+
return sql.NewSession(addr, client, c.User, c.ConnectionID)
2223
}
2324

2425
// SessionManager is in charge of creating new sessions for the given

server/handler_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,16 @@ func setupMemDB(require *require.Assertions) *sqle.Engine {
3636
}
3737

3838
func TestHandlerOutput(t *testing.T) {
39+
// This session builder is used as dummy mysql Conn is not complete and
40+
// causes panic when accessing remote address.
41+
testSessionBuilder := func(c *mysql.Conn, addr string) sql.Session {
42+
client := "127.0.0.1:34567"
43+
return sql.NewSession(addr, client, c.User, c.ConnectionID)
44+
}
45+
3946
e := setupMemDB(require.New(t))
4047
dummyConn := &mysql.Conn{ConnectionID: 1}
41-
handler := NewHandler(e, NewSessionManager(DefaultSessionBuilder, opentracing.NoopTracer{}, "foo"))
48+
handler := NewHandler(e, NewSessionManager(testSessionBuilder, opentracing.NoopTracer{}, "foo"))
4249
handler.NewConnection(dummyConn)
4350

4451
type exptectedValues struct {

sql/analyzer/check_auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func CheckAuthorization(au auth.Auth) RuleFunc {
2121
perm = auth.ReadPerm
2222
}
2323

24-
err := au.Allowed(ctx.User(), perm)
24+
err := au.Allowed(ctx.Client().User, perm)
2525
if err != nil {
2626
return nil, err
2727
}

sql/expression/function/connection_id_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func TestConnectionID(t *testing.T) {
1212
require := require.New(t)
1313

14-
session := sql.NewSession("", "", 2)
14+
session := sql.NewSession("", "", "", 2)
1515
ctx := sql.NewContext(context.Background(), sql.WithSession(session))
1616

1717
f := NewConnectionID()

sql/plan/processlist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (p *ShowProcessList) RowIter(ctx *sql.Context) (sql.RowIter, error) {
9494
time: int64(proc.Seconds()),
9595
state: strings.Join(status, ", "),
9696
command: proc.Type.String(),
97-
host: ctx.Session.Address(),
97+
host: ctx.Session.Client().Address,
9898
info: proc.Query,
9999
db: p.Database,
100100
}.toRow()

sql/plan/processlist_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
func TestShowProcessList(t *testing.T) {
1212
require := require.New(t)
1313

14+
addr := "127.0.0.1:34567"
15+
1416
n := NewShowProcessList()
1517
p := sql.NewProcessList()
16-
sess := sql.NewSession("0.0.0.0:1234", "foo", 1)
18+
sess := sql.NewSession("0.0.0.0:3306", addr, "foo", 1)
1719
ctx := sql.NewContext(context.Background(), sql.WithPid(1), sql.WithSession(sess))
1820

1921
ctx, err := p.AddProcess(ctx, sql.QueryProcess, "SELECT foo")
@@ -42,8 +44,8 @@ func TestShowProcessList(t *testing.T) {
4244
require.NoError(err)
4345

4446
expected := []sql.Row{
45-
{int64(1), "foo", "0.0.0.0:1234", "foo", "query", int64(0), "a(4/5), b(2/6)", "SELECT foo"},
46-
{int64(2), "foo", "0.0.0.0:1234", "foo", "create_index", int64(0), "foo(1/2)", "SELECT bar"},
47+
{int64(1), "foo", addr, "foo", "query", int64(0), "a(4/5), b(2/6)", "SELECT foo"},
48+
{int64(2), "foo", addr, "foo", "create_index", int64(0), "foo(1/2)", "SELECT bar"},
4749
}
4850

4951
require.ElementsMatch(expected, rows)

sql/processlist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (pl *ProcessList) AddProcess(
108108
Type: typ,
109109
Query: query,
110110
Progress: make(map[string]Progress),
111-
User: ctx.Session.User(),
111+
User: ctx.Session.Client().User,
112112
StartedAt: time.Now(),
113113
Kill: cancel,
114114
}
@@ -136,7 +136,7 @@ func (pl *ProcessList) UpdateProgress(pid uint64, name string, delta int64) {
136136
p.Progress[name] = progress
137137
}
138138

139-
// AddProgressItem adds a new item to track progress from to the proces with
139+
// AddProgressItem adds a new item to track progress from to the process with
140140
// the given pid. If the pid does not exist, it will do nothing.
141141
func (pl *ProcessList) AddProgressItem(pid uint64, name string, total int64) {
142142
pl.mu.Lock()

sql/processlist_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestProcessList(t *testing.T) {
1212
require := require.New(t)
1313

1414
p := NewProcessList()
15-
sess := NewSession("0.0.0.0:1234", "foo", 1)
15+
sess := NewSession("0.0.0.0:3306", "127.0.0.1:34567", "foo", 1)
1616
ctx := NewContext(context.Background(), WithPid(1), WithSession(sess))
1717
ctx, err := p.AddProcess(ctx, QueryProcess, "SELECT foo")
1818
require.NoError(err)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy