-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
plugin_mysql_server.go
563 lines (491 loc) · 19 KB
/
plugin_mysql_server.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vtgate
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"regexp"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"context"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/callinfo"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vttls"
querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
"github.com/google/uuid"
)
var (
mysqlServerPort = flag.Int("mysql_server_port", -1, "If set, also listen for MySQL binary protocol connections on this port.")
mysqlServerBindAddress = flag.String("mysql_server_bind_address", "", "Binds on this address when listening to MySQL binary protocol. Useful to restrict listening to 'localhost' only for instance.")
mysqlServerSocketPath = flag.String("mysql_server_socket_path", "", "This option specifies the Unix socket file to use when listening for local connections. By default it will be empty and it won't listen to a unix socket")
mysqlTCPVersion = flag.String("mysql_tcp_version", "tcp", "Select tcp, tcp4, or tcp6 to control the socket type.")
mysqlAuthServerImpl = flag.String("mysql_auth_server_impl", "static", "Which auth server implementation to use. Options: none, ldap, clientcert, static, vault.")
mysqlAllowClearTextWithoutTLS = flag.Bool("mysql_allow_clear_text_without_tls", false, "If set, the server will allow the use of a clear text password over non-SSL connections.")
mysqlProxyProtocol = flag.Bool("proxy_protocol", false, "Enable HAProxy PROXY protocol on MySQL listener socket")
mysqlServerRequireSecureTransport = flag.Bool("mysql_server_require_secure_transport", false, "Reject insecure connections but only if mysql_server_ssl_cert and mysql_server_ssl_key are provided")
mysqlSslCert = flag.String("mysql_server_ssl_cert", "", "Path to the ssl cert for mysql server plugin SSL")
mysqlSslKey = flag.String("mysql_server_ssl_key", "", "Path to ssl key for mysql server plugin SSL")
mysqlSslCa = flag.String("mysql_server_ssl_ca", "", "Path to ssl CA for mysql server plugin SSL. If specified, server will require and validate client certs.")
mysqlSslServerCA = flag.String("mysql_server_ssl_server_ca", "", "path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients")
mysqlSlowConnectWarnThreshold = flag.Duration("mysql_slow_connect_warn_threshold", 0, "Warn if it takes more than the given threshold for a mysql connection to establish")
mysqlConnReadTimeout = flag.Duration("mysql_server_read_timeout", 0, "connection read timeout")
mysqlConnWriteTimeout = flag.Duration("mysql_server_write_timeout", 0, "connection write timeout")
mysqlQueryTimeout = flag.Duration("mysql_server_query_timeout", 0, "mysql query timeout")
mysqlDefaultWorkloadName = flag.String("mysql_default_workload", "OLTP", "Default session workload (OLTP, OLAP, DBA)")
mysqlDefaultWorkload int32
busyConnections int32
)
// vtgateHandler implements the Listener interface.
// It stores the Session in the ClientData of a Connection.
type vtgateHandler struct {
mu sync.Mutex
vtg *VTGate
connections map[*mysql.Conn]bool
}
func newVtgateHandler(vtg *VTGate) *vtgateHandler {
return &vtgateHandler{
vtg: vtg,
connections: make(map[*mysql.Conn]bool),
}
}
func (vh *vtgateHandler) NewConnection(c *mysql.Conn) {
vh.mu.Lock()
defer vh.mu.Unlock()
vh.connections[c] = true
}
func (vh *vtgateHandler) numConnections() int {
vh.mu.Lock()
defer vh.mu.Unlock()
return len(vh.connections)
}
func (vh *vtgateHandler) ComResetConnection(c *mysql.Conn) {
ctx := context.Background()
session := vh.session(c)
if session.InTransaction {
defer atomic.AddInt32(&busyConnections, -1)
}
err := vh.vtg.CloseSession(ctx, session)
if err != nil {
log.Errorf("Error happened in transaction rollback: %v", err)
}
}
func (vh *vtgateHandler) ConnectionClosed(c *mysql.Conn) {
// Rollback if there is an ongoing transaction. Ignore error.
defer func() {
vh.mu.Lock()
defer vh.mu.Unlock()
delete(vh.connections, c)
}()
var ctx context.Context
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), *mysqlQueryTimeout)
defer cancel()
} else {
ctx = context.Background()
}
session := vh.session(c)
if session.InTransaction {
defer atomic.AddInt32(&busyConnections, -1)
}
_ = vh.vtg.CloseSession(ctx, session)
}
// Regexp to extract parent span id over the sql query
var r = regexp.MustCompile(`/\*VT_SPAN_CONTEXT=(.*)\*/`)
// this function is here to make this logic easy to test by decoupling the logic from the `trace.NewSpan` and `trace.NewFromString` functions
func startSpanTestable(ctx context.Context, query, label string,
newSpan func(context.Context, string) (trace.Span, context.Context),
newSpanFromString func(context.Context, string, string) (trace.Span, context.Context, error)) (trace.Span, context.Context, error) {
_, comments := sqlparser.SplitMarginComments(query)
match := r.FindStringSubmatch(comments.Leading)
span, ctx := getSpan(ctx, match, newSpan, label, newSpanFromString)
trace.AnnotateSQL(span, query)
return span, ctx, nil
}
func getSpan(ctx context.Context, match []string, newSpan func(context.Context, string) (trace.Span, context.Context), label string, newSpanFromString func(context.Context, string, string) (trace.Span, context.Context, error)) (trace.Span, context.Context) {
var span trace.Span
if len(match) != 0 {
var err error
span, ctx, err = newSpanFromString(ctx, match[1], label)
if err == nil {
return span, ctx
}
log.Warningf("Unable to parse VT_SPAN_CONTEXT: %s", err.Error())
}
span, ctx = newSpan(ctx, label)
return span, ctx
}
func startSpan(ctx context.Context, query, label string) (trace.Span, context.Context, error) {
return startSpanTestable(ctx, query, label, trace.NewSpan, trace.NewFromString)
}
func (vh *vtgateHandler) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error {
ctx := context.Background()
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
ctx, cancel = context.WithTimeout(ctx, *mysqlQueryTimeout)
defer cancel()
}
span, ctx, err := startSpan(ctx, query, "vtgateHandler.ComQuery")
if err != nil {
return vterrors.Wrap(err, "failed to extract span")
}
defer span.Finish()
ctx = callinfo.MysqlCallInfo(ctx, c)
// Fill in the ImmediateCallerID with the UserData returned by
// the AuthServer plugin for that user. If nothing was
// returned, use the User. This lets the plugin map a MySQL
// user used for authentication to a Vitess User used for
// Table ACLs and Vitess authentication in general.
im := c.UserData.Get()
ef := callerid.NewEffectiveCallerID(
c.User, /* principal: who */
c.RemoteAddr().String(), /* component: running client process */
"VTGate MySQL Connector" /* subcomponent: part of the client */)
ctx = callerid.NewContext(ctx, ef, im)
session := vh.session(c)
if !session.InTransaction {
atomic.AddInt32(&busyConnections, 1)
}
defer func() {
if !session.InTransaction {
atomic.AddInt32(&busyConnections, -1)
}
}()
if session.Options.Workload == querypb.ExecuteOptions_OLAP {
err := vh.vtg.StreamExecute(ctx, session, query, make(map[string]*querypb.BindVariable), callback)
return mysql.NewSQLErrorFromError(err)
}
session, result, err := vh.vtg.Execute(ctx, session, query, make(map[string]*querypb.BindVariable))
err = mysql.NewSQLErrorFromError(err)
if err != nil {
return err
}
fillInTxStatusFlags(c, session)
return callback(result)
}
func fillInTxStatusFlags(c *mysql.Conn, session *vtgatepb.Session) {
if session.InTransaction {
c.StatusFlags |= mysql.ServerStatusInTrans
} else {
c.StatusFlags &= mysql.NoServerStatusInTrans
}
if session.Autocommit {
c.StatusFlags |= mysql.ServerStatusAutocommit
} else {
c.StatusFlags &= mysql.NoServerStatusAutocommit
}
}
// ComPrepare is the handler for command prepare.
func (vh *vtgateHandler) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*querypb.BindVariable) ([]*querypb.Field, error) {
var ctx context.Context
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), *mysqlQueryTimeout)
defer cancel()
} else {
ctx = context.Background()
}
ctx = callinfo.MysqlCallInfo(ctx, c)
// Fill in the ImmediateCallerID with the UserData returned by
// the AuthServer plugin for that user. If nothing was
// returned, use the User. This lets the plugin map a MySQL
// user used for authentication to a Vitess User used for
// Table ACLs and Vitess authentication in general.
im := c.UserData.Get()
ef := callerid.NewEffectiveCallerID(
c.User, /* principal: who */
c.RemoteAddr().String(), /* component: running client process */
"VTGate MySQL Connector" /* subcomponent: part of the client */)
ctx = callerid.NewContext(ctx, ef, im)
session := vh.session(c)
if !session.InTransaction {
atomic.AddInt32(&busyConnections, 1)
}
defer func() {
if !session.InTransaction {
atomic.AddInt32(&busyConnections, -1)
}
}()
session, fld, err := vh.vtg.Prepare(ctx, session, query, bindVars)
err = mysql.NewSQLErrorFromError(err)
if err != nil {
return nil, err
}
return fld, nil
}
func (vh *vtgateHandler) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error {
var ctx context.Context
var cancel context.CancelFunc
if *mysqlQueryTimeout != 0 {
ctx, cancel = context.WithTimeout(context.Background(), *mysqlQueryTimeout)
defer cancel()
} else {
ctx = context.Background()
}
ctx = callinfo.MysqlCallInfo(ctx, c)
// Fill in the ImmediateCallerID with the UserData returned by
// the AuthServer plugin for that user. If nothing was
// returned, use the User. This lets the plugin map a MySQL
// user used for authentication to a Vitess User used for
// Table ACLs and Vitess authentication in general.
im := c.UserData.Get()
ef := callerid.NewEffectiveCallerID(
c.User, /* principal: who */
c.RemoteAddr().String(), /* component: running client process */
"VTGate MySQL Connector" /* subcomponent: part of the client */)
ctx = callerid.NewContext(ctx, ef, im)
session := vh.session(c)
if !session.InTransaction {
atomic.AddInt32(&busyConnections, 1)
}
defer func() {
if !session.InTransaction {
atomic.AddInt32(&busyConnections, -1)
}
}()
if session.Options.Workload == querypb.ExecuteOptions_OLAP {
err := vh.vtg.StreamExecute(ctx, session, prepare.PrepareStmt, prepare.BindVars, callback)
return mysql.NewSQLErrorFromError(err)
}
_, qr, err := vh.vtg.Execute(ctx, session, prepare.PrepareStmt, prepare.BindVars)
if err != nil {
err = mysql.NewSQLErrorFromError(err)
return err
}
fillInTxStatusFlags(c, session)
return callback(qr)
}
func (vh *vtgateHandler) WarningCount(c *mysql.Conn) uint16 {
return uint16(len(vh.session(c).GetWarnings()))
}
func (vh *vtgateHandler) session(c *mysql.Conn) *vtgatepb.Session {
session, _ := c.ClientData.(*vtgatepb.Session)
if session == nil {
u, _ := uuid.NewUUID()
session = &vtgatepb.Session{
Options: &querypb.ExecuteOptions{
IncludedFields: querypb.ExecuteOptions_ALL,
Workload: querypb.ExecuteOptions_Workload(mysqlDefaultWorkload),
},
Autocommit: true,
DDLStrategy: *defaultDDLStrategy,
SessionUUID: u.String(),
EnableSystemSettings: *sysVarSetEnabled,
}
if c.Capabilities&mysql.CapabilityClientFoundRows != 0 {
session.Options.ClientFoundRows = true
}
c.ClientData = session
}
return session
}
var mysqlListener *mysql.Listener
var mysqlUnixListener *mysql.Listener
var sigChan chan os.Signal
var vtgateHandle *vtgateHandler
// initTLSConfig inits tls config for the given mysql listener
func initTLSConfig(mysqlListener *mysql.Listener, mysqlSslCert, mysqlSslKey, mysqlSslCa, mysqlSslServerCA string, mysqlServerRequireSecureTransport bool) error {
serverConfig, err := vttls.ServerConfig(mysqlSslCert, mysqlSslKey, mysqlSslCa, mysqlSslServerCA)
if err != nil {
log.Exitf("grpcutils.TLSServerConfig failed: %v", err)
return err
}
mysqlListener.TLSConfig.Store(serverConfig)
mysqlListener.RequireSecureTransport = mysqlServerRequireSecureTransport
sigChan = make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP)
go func() {
for range sigChan {
serverConfig, err := vttls.ServerConfig(mysqlSslCert, mysqlSslKey, mysqlSslCa, mysqlSslServerCA)
if err != nil {
log.Errorf("grpcutils.TLSServerConfig failed: %v", err)
} else {
log.Info("grpcutils.TLSServerConfig updated")
mysqlListener.TLSConfig.Store(serverConfig)
}
}
}()
return nil
}
// initiMySQLProtocol starts the mysql protocol.
// It should be called only once in a process.
func initMySQLProtocol() {
// Flag is not set, just return.
if *mysqlServerPort < 0 && *mysqlServerSocketPath == "" {
return
}
// If no VTGate was created, just return.
if rpcVTGate == nil {
return
}
// Initialize registered AuthServer implementations (or other plugins)
for _, initFn := range pluginInitializers {
initFn()
}
authServer := mysql.GetAuthServer(*mysqlAuthServerImpl)
// Check mysql_default_workload
var ok bool
if mysqlDefaultWorkload, ok = querypb.ExecuteOptions_Workload_value[strings.ToUpper(*mysqlDefaultWorkloadName)]; !ok {
log.Exitf("-mysql_default_workload must be one of [OLTP, OLAP, DBA, UNSPECIFIED]")
}
switch *mysqlTCPVersion {
case "tcp", "tcp4", "tcp6":
// Valid flag value.
default:
log.Exitf("-mysql_tcp_version must be one of [tcp, tcp4, tcp6]")
}
// Create a Listener.
var err error
vtgateHandle = newVtgateHandler(rpcVTGate)
if *mysqlServerPort >= 0 {
mysqlListener, err = mysql.NewListener(*mysqlTCPVersion, net.JoinHostPort(*mysqlServerBindAddress, fmt.Sprintf("%v", *mysqlServerPort)), authServer, vtgateHandle, *mysqlConnReadTimeout, *mysqlConnWriteTimeout, *mysqlProxyProtocol)
if err != nil {
log.Exitf("mysql.NewListener failed: %v", err)
}
if *servenv.MySQLServerVersion != "" {
mysqlListener.ServerVersion = *servenv.MySQLServerVersion
}
if *mysqlSslCert != "" && *mysqlSslKey != "" {
initTLSConfig(mysqlListener, *mysqlSslCert, *mysqlSslKey, *mysqlSslCa, *mysqlSslServerCA, *mysqlServerRequireSecureTransport)
}
mysqlListener.AllowClearTextWithoutTLS.Set(*mysqlAllowClearTextWithoutTLS)
// Check for the connection threshold
if *mysqlSlowConnectWarnThreshold != 0 {
log.Infof("setting mysql slow connection threshold to %v", mysqlSlowConnectWarnThreshold)
mysqlListener.SlowConnectWarnThreshold.Set(*mysqlSlowConnectWarnThreshold)
}
// Start listening for tcp
go mysqlListener.Accept()
}
if *mysqlServerSocketPath != "" {
// Let's create this unix socket with permissions to all users. In this way,
// clients can connect to vtgate mysql server without being vtgate user
oldMask := syscall.Umask(000)
mysqlUnixListener, err = newMysqlUnixSocket(*mysqlServerSocketPath, authServer, vtgateHandle)
_ = syscall.Umask(oldMask)
if err != nil {
log.Exitf("mysql.NewListener failed: %v", err)
return
}
// Listen for unix socket
go mysqlUnixListener.Accept()
}
}
// newMysqlUnixSocket creates a new unix socket mysql listener. If a socket file already exists, attempts
// to clean it up.
func newMysqlUnixSocket(address string, authServer mysql.AuthServer, handler mysql.Handler) (*mysql.Listener, error) {
listener, err := mysql.NewListener("unix", address, authServer, handler, *mysqlConnReadTimeout, *mysqlConnWriteTimeout, false)
switch err := err.(type) {
case nil:
return listener, nil
case *net.OpError:
log.Warningf("Found existent socket when trying to create new unix mysql listener: %s, attempting to clean up", address)
// err.Op should never be different from listen, just being extra careful
// in case in the future other errors are returned here
if err.Op != "listen" {
return nil, err
}
_, dialErr := net.Dial("unix", address)
if dialErr == nil {
log.Errorf("Existent socket '%s' is still accepting connections, aborting", address)
return nil, err
}
removeFileErr := os.Remove(address)
if removeFileErr != nil {
log.Errorf("Couldn't remove existent socket file: %s", address)
return nil, err
}
listener, listenerErr := mysql.NewListener("unix", address, authServer, handler, *mysqlConnReadTimeout, *mysqlConnWriteTimeout, false)
return listener, listenerErr
default:
return nil, err
}
}
func shutdownMysqlProtocolAndDrain() {
if mysqlListener != nil {
mysqlListener.Close()
mysqlListener = nil
}
if mysqlUnixListener != nil {
mysqlUnixListener.Close()
mysqlUnixListener = nil
}
if sigChan != nil {
signal.Stop(sigChan)
}
if atomic.LoadInt32(&busyConnections) > 0 {
log.Infof("Waiting for all client connections to be idle (%d active)...", atomic.LoadInt32(&busyConnections))
start := time.Now()
reported := start
for atomic.LoadInt32(&busyConnections) != 0 {
if time.Since(reported) > 2*time.Second {
log.Infof("Still waiting for client connections to be idle (%d active)...", atomic.LoadInt32(&busyConnections))
reported = time.Now()
}
time.Sleep(1 * time.Millisecond)
}
}
}
func rollbackAtShutdown() {
defer log.Flush()
// Close all open connections. If they're waiting for reads, this will cause
// them to error out, which will automatically rollback open transactions.
func() {
vtgateHandle.mu.Lock()
defer vtgateHandle.mu.Unlock()
for c := range vtgateHandle.connections {
log.Infof("Rolling back transactions associated with connection ID: %v", c.ConnectionID)
c.Close()
}
}()
// If vtgate is instead busy executing a query, the number of open conns
// will be non-zero. Give another second for those queries to finish.
for i := 0; i < 100; i++ {
if vtgateHandle.numConnections() == 0 {
log.Infof("All connections have been rolled back.")
return
}
time.Sleep(10 * time.Millisecond)
}
log.Errorf("All connections did not go idle. Shutting down anyway.")
}
func mysqlSocketPath() string {
if mysqlServerSocketPath == nil {
return ""
}
return *mysqlServerSocketPath
}
func init() {
servenv.OnRun(initMySQLProtocol)
servenv.OnTermSync(shutdownMysqlProtocolAndDrain)
servenv.OnClose(rollbackAtShutdown)
}
var pluginInitializers []func()
// RegisterPluginInitializer lets plugins register themselves to be init'ed at servenv.OnRun-time
func RegisterPluginInitializer(initializer func()) {
pluginInitializers = append(pluginInitializers, initializer)
}