-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathdriver.go
605 lines (504 loc) · 15.7 KB
/
driver.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
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 vitessdriver
import (
"context"
"database/sql"
"database/sql/driver"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"google.golang.org/grpc"
"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
"vitess.io/vitess/go/vt/vtgate/grpcvtgateconn"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
)
var (
errNoIntermixing = errors.New("named and positional arguments intermixing disallowed")
errIsolationUnsupported = errors.New("isolation levels are not supported")
)
// Type-check interfaces.
var (
_ interface {
driver.Connector
} = &connector{}
_ interface {
driver.Driver
driver.DriverContext
} = drv{}
_ interface {
driver.Conn
driver.ConnBeginTx
driver.ConnPrepareContext
driver.ExecerContext
driver.Pinger
driver.QueryerContext
driver.Tx
} = &conn{}
_ interface {
driver.Stmt
driver.StmtExecContext
driver.StmtQueryContext
} = &stmt{}
)
func init() {
sql.Register("vitess", drv{})
}
// Open is a Vitess helper function for sql.Open().
//
// It opens a database connection to vtgate running at "address".
func Open(address, target string) (*sql.DB, error) {
c := Configuration{
Address: address,
Target: target,
}
return OpenWithConfiguration(c)
}
// OpenForStreaming is the same as Open() but uses streaming RPCs to retrieve
// the results.
//
// The streaming mode is recommended for large results.
func OpenForStreaming(address, target string) (*sql.DB, error) {
c := Configuration{
Address: address,
Target: target,
Streaming: true,
}
return OpenWithConfiguration(c)
}
// OpenWithConfiguration is the generic Vitess helper function for sql.Open().
//
// It allows to pass in a Configuration struct to control all possible
// settings of the Vitess Go SQL driver.
func OpenWithConfiguration(c Configuration) (*sql.DB, error) {
c.setDefaults()
json, err := c.toJSON()
if err != nil {
return nil, err
}
if len(c.GRPCDialOptions) != 0 {
vtgateconn.RegisterDialer(c.Protocol, grpcvtgateconn.Dial(c.GRPCDialOptions...))
}
return sql.Open(c.DriverName, json)
}
type drv struct{}
// Open implements the database/sql/driver.Driver interface.
//
// For "name", the Vitess driver requires that a JSON object is passed in.
//
// Instead of using this call and passing in a hand-crafted JSON string, it's
// recommended to use the public Vitess helper functions like
// Open(), OpenShard() or OpenWithConfiguration() instead. These will generate
// the required JSON string behind the scenes for you.
//
// Example for a JSON string:
//
// {"protocol": "grpc", "address": "localhost:1111", "target": "@primary"}
//
// For a description of the available fields, see the Configuration struct.
func (d drv) Open(name string) (driver.Conn, error) {
conn, err := d.OpenConnector(name)
if err != nil {
return nil, err
}
return conn.Connect(context.Background())
}
// OpenConnector implements the database/sql/driver.DriverContext interface.
//
// See the documentation of Open for details on the format of name.
func (d drv) OpenConnector(name string) (driver.Connector, error) {
var cfg Configuration
if err := json.Unmarshal([]byte(name), &cfg); err != nil {
return nil, err
}
cfg.setDefaults()
return d.newConnector(cfg)
}
// A connector holds immutable state for the creation of additional conns via
// the Connect method.
type connector struct {
drv drv
cfg Configuration
convert *converter
}
func (d drv) newConnector(cfg Configuration) (driver.Connector, error) {
convert, err := newConverter(&cfg)
if err != nil {
return nil, err
}
return &connector{
drv: d,
cfg: cfg,
convert: convert,
}, nil
}
// Connect implements the database/sql/driver.Connector interface.
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
conn := &conn{
cfg: c.cfg,
convert: c.convert,
}
if err := conn.dial(ctx); err != nil {
return nil, err
}
return conn, nil
}
// Driver implements the database/sql/driver.Connector interface.
func (c *connector) Driver() driver.Driver { return c.drv }
// Configuration holds all Vitess driver settings.
//
// Fields with documented default values do not have to be set explicitly.
type Configuration struct {
// Protocol is the name of the vtgate RPC client implementation.
// Note: In open-source "grpc" is the recommended implementation.
//
// Default: "grpc"
Protocol string
// Address must point to a vtgate instance.
//
// Format: hostname:port
Address string
// Target specifies the default target.
Target string
// Streaming is true when streaming RPCs are used.
// Recommended for large results.
// Default: false
Streaming bool
// DefaultLocation is the timezone string that will be used
// when converting DATETIME and DATE into time.Time.
// This setting has no effect if ConvertDatetime is not set.
// Default: UTC
DefaultLocation string
// GRPCDialOptions registers a new vtgateconn dialer with these dial options using the
// protocol as the key. This may overwrite the default grpcvtgateconn dial option
// if a custom one hasn't been specified in the config.
//
// Default: none
GRPCDialOptions []grpc.DialOption `json:"-"`
// Driver is the name registered with the database/sql package. This override
// is here in case you have wrapped the driver for stats or other interceptors.
//
// Default: "vitess"
DriverName string `json:"-"`
// SessionToken is a protobuf encoded vtgatepb.Session represented as base64, which
// can be used to distribute a transaction over the wire.
SessionToken string
}
// toJSON converts Configuration to the JSON string which is required by the
// Vitess driver. Default values for empty fields will be set.
func (c Configuration) toJSON() (string, error) {
jsonBytes, err := json.Marshal(c)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
// setDefaults sets the default values for empty fields.
func (c *Configuration) setDefaults() {
// if no protocol is provided default to grpc so the driver is in control
// of the connection protocol and not the flag vtgateconn.VtgateProtocol
if c.Protocol == "" {
c.Protocol = "grpc"
}
if c.DriverName == "" {
c.DriverName = "vitess"
}
}
type conn struct {
cfg Configuration
convert *converter
conn *vtgateconn.VTGateConn
session *vtgateconn.VTGateSession
}
func (c *conn) dial(ctx context.Context) error {
var err error
c.conn, err = vtgateconn.DialProtocol(ctx, c.cfg.Protocol, c.cfg.Address)
if err != nil {
return err
}
if c.cfg.SessionToken != "" {
sessionFromToken, err := sessionTokenToSession(c.cfg.SessionToken)
if err != nil {
return err
}
c.session = c.conn.SessionFromPb(sessionFromToken)
} else {
c.session = c.conn.Session(c.cfg.Target, nil)
}
return nil
}
func (c *conn) Ping(ctx context.Context) error {
if c.cfg.Streaming {
return errors.New("Ping not allowed for streaming connections")
}
_, err := c.ExecContext(ctx, "select 1", nil)
return err
}
func (c *conn) Prepare(query string) (driver.Stmt, error) {
return &stmt{c: c, query: query}, nil
}
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return c.Prepare(query)
}
func (c *conn) Close() error {
c.conn.Close()
return nil
}
// DistributedTxFromSessionToken allows users to send serialized sessions over the wire and
// reconnect to an existing transaction. Setting the sessionToken and address on the
// supplied configuration is the minimum required
// WARNING: the original Tx must already have already done work on all shards to be affected,
// otherwise the ShardSessions will not be sent through in the session token, and thus will
// never be committed in the source. The returned validation function checks to make sure that
// the new transaction work has not added any new ShardSessions.
func DistributedTxFromSessionToken(ctx context.Context, c Configuration) (*sql.Tx, func() error, error) {
if c.SessionToken == "" {
return nil, nil, errors.New("c.SessionToken is required")
}
session, err := sessionTokenToSession(c.SessionToken)
if err != nil {
return nil, nil, err
}
// if there isn't 1 or more shards already referenced, no work in this Tx can be committed
originalShardSessionCount := len(session.ShardSessions)
if originalShardSessionCount == 0 {
return nil, nil, errors.New("there must be at least 1 ShardSession")
}
db, err := OpenWithConfiguration(c)
if err != nil {
return nil, nil, err
}
// this should return the only connection associated with the db
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return nil, nil, err
}
// this is designed to be run after all new work has been done in the tx, similar to
// where you would traditionally run a tx.Commit, to help prevent you from silently
// losing transactional data.
validationFunc := func() error {
var sessionToken string
sessionToken, err = SessionTokenFromTx(ctx, tx)
if err != nil {
return err
}
session, err = sessionTokenToSession(sessionToken)
if err != nil {
return err
}
if len(session.ShardSessions) > originalShardSessionCount {
return fmt.Errorf("mismatched ShardSession count: originally %d, now %d",
originalShardSessionCount, len(session.ShardSessions),
)
}
return nil
}
return tx, validationFunc, nil
}
// SessionTokenFromTx serializes the sessionFromToken on the tx, which can be reconstituted
// into a *sql.Tx using DistributedTxFromSessionToken
func SessionTokenFromTx(ctx context.Context, tx *sql.Tx) (string, error) {
var sessionToken string
err := tx.QueryRowContext(ctx, "vt_session_token").Scan(&sessionToken)
if err != nil {
return "", err
}
session, err := sessionTokenToSession(sessionToken)
if err != nil {
return "", err
}
// if there isn't 1 or more shards already referenced, no work in this Tx can be committed
originalShardSessionCount := len(session.ShardSessions)
if originalShardSessionCount == 0 {
return "", errors.New("there must be at least 1 ShardSession")
}
return sessionToken, nil
}
func newSessionTokenRow(session *vtgatepb.Session, c *converter) (driver.Rows, error) {
sessionToken, err := sessionToSessionToken(session)
if err != nil {
return nil, err
}
qr := sqltypes.Result{
Fields: []*querypb.Field{{
Name: "vt_session_token",
Type: sqltypes.VarBinary,
}},
Rows: [][]sqltypes.Value{{
sqltypes.NewVarBinary(sessionToken),
}},
}
return newRows(&qr, c), nil
}
func sessionToSessionToken(session *vtgatepb.Session) (string, error) {
b, err := session.MarshalVT()
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
func sessionTokenToSession(sessionToken string) (*vtgatepb.Session, error) {
b, err := base64.StdEncoding.DecodeString(sessionToken)
if err != nil {
return nil, err
}
session := &vtgatepb.Session{}
err = session.UnmarshalVT(b)
if err != nil {
return nil, err
}
return session, nil
}
func (c *conn) Begin() (driver.Tx, error) {
// if we're loading from an existing session, we need to avoid starting a new transaction
if c.cfg.SessionToken != "" {
return c, nil
}
if _, err := c.Exec("begin", nil); err != nil {
return nil, err
}
return c, nil
}
func (c *conn) BeginTx(_ context.Context, opts driver.TxOptions) (driver.Tx, error) {
// We don't use the context. The function signature accepts the context
// to signal to the driver that it's allowed to call Rollback on Cancel.
if opts.Isolation != driver.IsolationLevel(0) || opts.ReadOnly {
return nil, errIsolationUnsupported
}
return c.Begin()
}
func (c *conn) Commit() error {
// if we're loading from an existing session, disallow committing/rolling back the transaction
// this isn't a technical limitation, but is enforced to prevent misuse, so that only
// the original creator of the transaction can commit/rollback
if c.cfg.SessionToken != "" {
return errors.New("calling Commit from a distributed tx is not allowed")
}
_, err := c.Exec("commit", nil)
return err
}
func (c *conn) Rollback() error {
// if we're loading from an existing session, disallow committing/rolling back the transaction
// this isn't a technical limitation, but is enforced to prevent misuse, so that only
// the original creator of the transaction can commit/rollback
if c.cfg.SessionToken != "" {
return errors.New("calling Rollback from a distributed tx is not allowed")
}
_, err := c.Exec("rollback", nil)
return err
}
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
ctx := context.TODO()
if c.cfg.Streaming {
return nil, errors.New("Exec not allowed for streaming connections")
}
bindVars, err := c.convert.buildBindVars(args)
if err != nil {
return nil, err
}
qr, err := c.session.Execute(ctx, query, bindVars)
if err != nil {
return nil, err
}
return result{int64(qr.InsertID), int64(qr.RowsAffected)}, nil
}
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
if c.cfg.Streaming {
return nil, errors.New("Exec not allowed for streaming connections")
}
bv, err := c.convert.bindVarsFromNamedValues(args)
if err != nil {
return nil, err
}
qr, err := c.session.Execute(ctx, query, bv)
if err != nil {
return nil, err
}
return result{int64(qr.InsertID), int64(qr.RowsAffected)}, nil
}
func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
ctx := context.TODO()
bindVars, err := c.convert.buildBindVars(args)
if err != nil {
return nil, err
}
if c.cfg.Streaming {
stream, err := c.session.StreamExecute(ctx, query, bindVars)
if err != nil {
return nil, err
}
return newStreamingRows(stream, c.convert), nil
}
qr, err := c.session.Execute(ctx, query, bindVars)
if err != nil {
return nil, err
}
return newRows(qr, c.convert), nil
}
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
// special case for serializing the current sessionFromToken state
if query == "vt_session_token" {
return newSessionTokenRow(c.session.SessionPb(), c.convert)
}
bv, err := c.convert.bindVarsFromNamedValues(args)
if err != nil {
return nil, err
}
if c.cfg.Streaming {
stream, err := c.session.StreamExecute(ctx, query, bv)
if err != nil {
return nil, err
}
return newStreamingRows(stream, c.convert), nil
}
qr, err := c.session.Execute(ctx, query, bv)
if err != nil {
return nil, err
}
return newRows(qr, c.convert), nil
}
type stmt struct {
c *conn
query string
}
func (s *stmt) Close() error {
return nil
}
func (s *stmt) NumInput() int {
// -1 = Golang sql won't sanity check argument counts before Exec or Query.
return -1
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return s.c.Exec(s.query, args)
}
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return s.c.ExecContext(ctx, s.query, args)
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
return s.c.Query(s.query, args)
}
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return s.c.QueryContext(ctx, s.query, args)
}
type result struct {
insertid, rowsaffected int64
}
func (r result) LastInsertId() (int64, error) {
return r.insertid, nil
}
func (r result) RowsAffected() (int64, error) {
return r.rowsaffected, nil
}