forked from src-d/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
381 lines (324 loc) · 9.03 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package server
import (
"context"
"io"
"net"
"regexp"
"strconv"
"strings"
"sync"
"time"
sqle "github.com/src-d/go-mysql-server"
"github.com/src-d/go-mysql-server/auth"
"github.com/src-d/go-mysql-server/internal/sockstate"
"github.com/src-d/go-mysql-server/sql"
"gopkg.in/src-d/go-errors.v1"
"github.com/sirupsen/logrus"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/proto/query"
)
var regKillCmd = regexp.MustCompile(`^kill (?:(query|connection) )?(\d+)$`)
var errConnectionNotFound = errors.NewKind("connection not found: %c")
// ErrRowTimeout will be returned if the wait for the row is longer than the connection timeout
var ErrRowTimeout = errors.NewKind("row read wait bigger than connection timeout")
// ErrConnectionWasClosed will be returned if we try to use a previously closed connection
var ErrConnectionWasClosed = errors.NewKind("connection was closed")
// TODO parametrize
const rowsBatch = 100
const tcpCheckerSleepTime = 1
type conntainer struct {
MysqlConn *mysql.Conn
NetConn net.Conn
}
// Handler is a connection handler for a SQLe engine.
type Handler struct {
mu sync.Mutex
e *sqle.Engine
sm *SessionManager
c map[uint32]conntainer
readTimeout time.Duration
lc []*net.Conn
}
// NewHandler creates a new Handler given a SQLe engine.
func NewHandler(e *sqle.Engine, sm *SessionManager, rt time.Duration) *Handler {
return &Handler{
e: e,
sm: sm,
c: make(map[uint32]conntainer),
readTimeout: rt,
}
}
// AddNetConnection is used to add the net.Conn to the Handler when available (usually on the
// Listener.Accept() method)
func (h *Handler) AddNetConnection(c *net.Conn) {
h.lc = append(h.lc, c)
}
// NewConnection reports that a new connection has been established.
func (h *Handler) NewConnection(c *mysql.Conn) {
h.mu.Lock()
if _, ok := h.c[c.ConnectionID]; !ok {
// Retrieve the latest net.Conn stored by Listener.Accept(), if called, and remove it
var netConn net.Conn
if len(h.lc) > 0 {
netConn = *h.lc[len(h.lc)-1]
h.lc = h.lc[:len(h.lc)-1]
} else {
logrus.Debug("Could not find TCP socket connection after Accept(), " +
"connection checker won't run")
}
h.c[c.ConnectionID] = conntainer{c, netConn}
}
h.mu.Unlock()
logrus.Infof("NewConnection: client %v", c.ConnectionID)
}
// ConnectionClosed reports that a connection has been closed.
func (h *Handler) ConnectionClosed(c *mysql.Conn) {
h.sm.CloseConn(c)
h.mu.Lock()
delete(h.c, c.ConnectionID)
h.mu.Unlock()
// If connection was closed, kill only its associated queries.
h.e.Catalog.ProcessList.KillOnlyQueries(c.ConnectionID)
if err := h.e.Catalog.UnlockTables(nil, c.ConnectionID); err != nil {
logrus.Errorf("unable to unlock tables on session close: %s", err)
}
logrus.Infof("ConnectionClosed: client %v", c.ConnectionID)
}
// ComQuery executes a SQL query on the SQLe engine.
func (h *Handler) ComQuery(
c *mysql.Conn,
query string,
callback func(*sqltypes.Result) error,
) (err error) {
ctx := h.sm.NewContextWithQuery(c, query)
newCtx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = ctx.WithContext(newCtx)
handled, err := h.handleKill(c, query)
if err != nil {
return err
}
if handled {
return callback(&sqltypes.Result{})
}
start := time.Now()
schema, rows, err := h.e.Query(ctx, query)
defer func() {
if q, ok := h.e.Auth.(*auth.Audit); ok {
q.Query(ctx, time.Since(start), err)
}
}()
if err != nil {
return err
}
nc, ok := h.c[c.ConnectionID]
if !ok {
return ErrConnectionWasClosed.New()
}
var r *sqltypes.Result
var proccesedAtLeastOneBatch bool
// Reads rows from the row reading goroutine
rowChan := make(chan sql.Row)
// To send errors from the two goroutines to the main one
errChan := make(chan error)
// To close the goroutines
quit := make(chan struct{})
// Default waitTime is one minute if there is not timeout configured, in which case
// it will loop to iterate again unless the socket died by the OS timeout or other problems.
// If there is a timeout, it will be enforced to ensure that Vitess has a chance to
// call Handler.CloseConnection()
waitTime := 1 * time.Minute
if h.readTimeout > 0 {
waitTime = h.readTimeout
}
timer := time.NewTimer(waitTime)
defer timer.Stop()
// This goroutine will be select{}ed giving a chance to Vitess to call the
// handler.CloseConnection callback and enforcing the timeout if configured
go func() {
for {
select {
case <-quit:
return
default:
row, err := rows.Next()
if err != nil {
errChan <- err
return
}
rowChan <- row
}
}
}()
// This second goroutine will check the socket
// and try to determine if the socket is in CLOSE_WAIT state
// (because the remote client closed the connection).
go func() {
tcpConn, ok := nc.NetConn.(*net.TCPConn)
if !ok {
logrus.Debug("Connection checker exiting, connection isn't TCP")
return
}
inode, err := sockstate.GetConnInode(tcpConn)
if err != nil || inode == 0 {
if sockstate.ErrSocketCheckNotImplemented.Is(err) {
logrus.Warn("Connection checker exiting, not supported in this OS")
} else {
errChan <- err
}
return
}
t, ok := nc.NetConn.LocalAddr().(*net.TCPAddr)
if !ok {
logrus.Warn("Connection checker exiting, could not get local port")
return
}
for {
select {
case <-quit:
return
default:
}
st, err := sockstate.GetInodeSockState(t.Port, inode)
switch st {
case sockstate.Broken:
errChan <- ErrConnectionWasClosed.New()
return
case sockstate.Error:
errChan <- err
return
default: // Established
// (juanjux) this check is not free, each iteration takes about 9 milliseconds to run on my machine
// thus the small wait between checks
time.Sleep(tcpCheckerSleepTime * time.Second)
}
}
}()
rowLoop:
for {
if r == nil {
r = &sqltypes.Result{Fields: schemaToFields(schema)}
}
if r.RowsAffected == rowsBatch {
if err := callback(r); err != nil {
close(quit)
return err
}
r = nil
proccesedAtLeastOneBatch = true
continue
}
select {
case err = <-errChan:
if err == io.EOF {
break rowLoop
}
close(quit)
return err
case row := <-rowChan:
outputRow, err := rowToSQL(schema, row)
if err != nil {
close(quit)
return err
}
r.Rows = append(r.Rows, outputRow)
r.RowsAffected++
case <-timer.C:
if h.readTimeout != 0 {
// Cancel and return so Vitess can call the CloseConnection callback
close(quit)
return ErrRowTimeout.New()
}
}
timer.Reset(waitTime)
}
close(quit)
if err := rows.Close(); err != nil {
return err
}
// Even if r.RowsAffected = 0, the callback must be
// called to update the state in the go-vitess' listener
// and avoid returning errors when the query doesn't
// produce any results.
if r != nil && (r.RowsAffected == 0 && proccesedAtLeastOneBatch) {
return nil
}
return callback(r)
}
// WarningCount is called at the end of each query to obtain
// the value to be returned to the client in the EOF packet.
// Note that this will be called either in the context of the
// ComQuery callback if the result does not contain any fields,
// or after the last ComQuery call completes.
func (h *Handler) WarningCount(c *mysql.Conn) uint16 {
if sess := h.sm.session(c); sess != nil {
return sess.WarningCount()
}
return 0
}
func (h *Handler) handleKill(conn *mysql.Conn, query string) (bool, error) {
q := strings.ToLower(query)
s := regKillCmd.FindStringSubmatch(q)
if s == nil {
return false, nil
}
id, err := strconv.ParseUint(s[2], 10, 32)
if err != nil {
return false, err
}
// KILL CONNECTION and KILL should close the connection. KILL QUERY only
// cancels the query.
//
// https://dev.mysql.com/doc/refman/8.0/en/kill.html
//
// KILL [CONNECTION | QUERY] processlist_id
// - KILL QUERY terminates the statement the connection is currently executing,
// but leaves the connection itself intact.
// - KILL CONNECTION is the same as KILL with no modifier:
// It terminates the connection associated with the given processlist_id,
// after terminating any statement the connection is executing.
connID := uint32(id)
h.e.Catalog.Kill(connID)
if s[1] != "query" {
logrus.Infof("kill connection: id %d", connID)
h.mu.Lock()
c, ok := h.c[connID]
if ok {
delete(h.c, connID)
}
h.mu.Unlock()
if !ok {
return false, errConnectionNotFound.New(connID)
}
h.sm.CloseConn(c.MysqlConn)
c.MysqlConn.Close()
}
return true, nil
}
func rowToSQL(s sql.Schema, row sql.Row) ([]sqltypes.Value, error) {
o := make([]sqltypes.Value, len(row))
var err error
for i, v := range row {
o[i], err = s[i].Type.SQL(v)
if err != nil {
return nil, err
}
}
return o, nil
}
func schemaToFields(s sql.Schema) []*query.Field {
fields := make([]*query.Field, len(s))
for i, c := range s {
var charset uint32 = mysql.CharacterSetUtf8
if c.Type == sql.Blob {
charset = mysql.CharacterSetBinary
}
fields[i] = &query.Field{
Name: c.Name,
Type: c.Type.Type(),
Charset: charset,
}
}
return fields
}