-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vtgate.go
1059 lines (946 loc) · 38 KB
/
vtgate.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package vtgate provides query routing rpc services
// for vttablets.
package vtgate
import (
"flag"
"fmt"
"math"
"net/http"
"sync"
"time"
log "github.com/golang/glog"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/tb"
"github.com/youtube/vitess/go/vt/discovery"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/servenv"
"github.com/youtube/vitess/go/vt/sqlannotation"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"github.com/youtube/vitess/go/vt/vterrors"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/querytypes"
"github.com/youtube/vitess/go/vt/vtgate/gateway"
"github.com/youtube/vitess/go/vt/vtgate/vtgateservice"
"strings"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vtgatepb "github.com/youtube/vitess/go/vt/proto/vtgate"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
var (
transactionMode = flag.String("transaction_mode", "multi", "single: disallow multi-db transactions, multi: allow multi-db transactions with best effort commit, twopc: allow multi-db transactions with 2pc commit")
normalizeQueries = flag.Bool("normalize_queries", true, "Rewrite queries with bind vars. Turn this off if the app itself sends normalized queries with bind vars.")
)
// Transaction modes. The value specifies what's allowed.
const (
TxSingle = iota
TxMulti
TxTwoPC
)
func getTxMode() int {
txMode := TxMulti
switch *transactionMode {
case "single":
log.Infof("Transaction mode: '%s'", *transactionMode)
txMode = TxSingle
case "multi":
log.Infof("Transaction mode: '%s'", *transactionMode)
case "twopc":
log.Infof("Transaction mode: '%s'", *transactionMode)
txMode = TxTwoPC
default:
log.Warningf("Unrecognized transactionMode '%s'. Continuing with default 'multi'", *transactionMode)
}
return txMode
}
var (
rpcVTGate *VTGate
qpsByOperation *stats.Rates
qpsByKeyspace *stats.Rates
qpsByDbType *stats.Rates
vschemaCounters *stats.Counters
errorsByOperation *stats.Rates
errorsByKeyspace *stats.Rates
errorsByDbType *stats.Rates
errorsByCode *stats.Rates
// Error counters should be global so they can be set from anywhere
errorCounts *stats.MultiCounters
)
// VTGate is the rpc interface to vtgate. Only one instance
// can be created. It implements vtgateservice.VTGateService
type VTGate struct {
transactionMode int
// router and resolver are top-level objects
// that make routing decisions.
router *Router
resolver *Resolver
// scatterConn and txConn are mid-level objects
// that execute requests.
scatterConn *ScatterConn
txConn *TxConn
// gateway is the low-level outgoing connection
// object to vttablet or l2vtgate.
gateway gateway.Gateway
// stats objects.
// TODO(sougou): This needs to be cleaned up. There
// are global vars that depend on this member var.
timings *stats.MultiTimings
rowsReturned *stats.MultiCounters
// the throttled loggers for all errors, one per API entry
logExecute *logutil.ThrottledLogger
logExecuteShards *logutil.ThrottledLogger
logExecuteKeyspaceIds *logutil.ThrottledLogger
logExecuteKeyRanges *logutil.ThrottledLogger
logExecuteEntityIds *logutil.ThrottledLogger
logExecuteBatchShards *logutil.ThrottledLogger
logExecuteBatchKeyspaceIds *logutil.ThrottledLogger
logStreamExecute *logutil.ThrottledLogger
logStreamExecuteKeyspaceIds *logutil.ThrottledLogger
logStreamExecuteKeyRanges *logutil.ThrottledLogger
logStreamExecuteShards *logutil.ThrottledLogger
logUpdateStream *logutil.ThrottledLogger
logMessageStream *logutil.ThrottledLogger
}
// RegisterVTGate defines the type of registration mechanism.
type RegisterVTGate func(vtgateservice.VTGateService)
// RegisterVTGates stores register funcs for VTGate server.
var RegisterVTGates []RegisterVTGate
var vtgateOnce sync.Once
// Init initializes VTGate server.
func Init(ctx context.Context, hc discovery.HealthCheck, topoServer topo.Server, serv topo.SrvTopoServer, cell string, retryCount int, tabletTypesToWait []topodatapb.TabletType) *VTGate {
if rpcVTGate != nil {
log.Fatalf("VTGate already initialized")
}
// vschemaCounters needs to be initialized before planner to
// catch the initial load stats.
vschemaCounters = stats.NewCounters("VtgateVSchemaCounts")
// Build objects from low to high level.
gw := gateway.GetCreator()(hc, topoServer, serv, cell, retryCount)
gateway.WaitForTablets(gw, tabletTypesToWait)
tc := NewTxConn(gw)
// ScatterConn depends on TxConn to perform forced rollbacks.
sc := NewScatterConn("VttabletCall", tc, gw)
rpcVTGate = &VTGate{
transactionMode: getTxMode(),
router: NewRouter(ctx, serv, cell, "VTGateRouter", sc, *normalizeQueries),
resolver: NewResolver(serv, cell, sc),
scatterConn: sc,
txConn: tc,
gateway: gw,
timings: stats.NewMultiTimings("VtgateApi", []string{"Operation", "Keyspace", "DbType"}),
rowsReturned: stats.NewMultiCounters("VtgateApiRowsReturned", []string{"Operation", "Keyspace", "DbType"}),
logExecute: logutil.NewThrottledLogger("Execute", 5*time.Second),
logExecuteShards: logutil.NewThrottledLogger("ExecuteShards", 5*time.Second),
logExecuteKeyspaceIds: logutil.NewThrottledLogger("ExecuteKeyspaceIds", 5*time.Second),
logExecuteKeyRanges: logutil.NewThrottledLogger("ExecuteKeyRanges", 5*time.Second),
logExecuteEntityIds: logutil.NewThrottledLogger("ExecuteEntityIds", 5*time.Second),
logExecuteBatchShards: logutil.NewThrottledLogger("ExecuteBatchShards", 5*time.Second),
logExecuteBatchKeyspaceIds: logutil.NewThrottledLogger("ExecuteBatchKeyspaceIds", 5*time.Second),
logStreamExecute: logutil.NewThrottledLogger("StreamExecute", 5*time.Second),
logStreamExecuteKeyspaceIds: logutil.NewThrottledLogger("StreamExecuteKeyspaceIds", 5*time.Second),
logStreamExecuteKeyRanges: logutil.NewThrottledLogger("StreamExecuteKeyRanges", 5*time.Second),
logStreamExecuteShards: logutil.NewThrottledLogger("StreamExecuteShards", 5*time.Second),
logUpdateStream: logutil.NewThrottledLogger("UpdateStream", 5*time.Second),
logMessageStream: logutil.NewThrottledLogger("MessageStream", 5*time.Second),
}
errorCounts = stats.NewMultiCounters("VtgateApiErrorCounts", []string{"Operation", "Keyspace", "DbType", "Code"})
qpsByOperation = stats.NewRates("QPSByOperation", stats.CounterForDimension(rpcVTGate.timings, "Operation"), 15, 1*time.Minute)
qpsByKeyspace = stats.NewRates("QPSByKeyspace", stats.CounterForDimension(rpcVTGate.timings, "Keyspace"), 15, 1*time.Minute)
qpsByDbType = stats.NewRates("QPSByDbType", stats.CounterForDimension(rpcVTGate.timings, "DbType"), 15, 1*time.Minute)
errorsByOperation = stats.NewRates("ErrorsByOperation", stats.CounterForDimension(errorCounts, "Operation"), 15, 1*time.Minute)
errorsByKeyspace = stats.NewRates("ErrorsByKeyspace", stats.CounterForDimension(errorCounts, "Keyspace"), 15, 1*time.Minute)
errorsByDbType = stats.NewRates("ErrorsByDbType", stats.CounterForDimension(errorCounts, "DbType"), 15, 1*time.Minute)
errorsByCode = stats.NewRates("ErrorsByCode", stats.CounterForDimension(errorCounts, "Code"), 15, 1*time.Minute)
servenv.OnRun(func() {
for _, f := range RegisterVTGates {
f(rpcVTGate)
}
})
vtgateOnce.Do(rpcVTGate.registerDebugHealthHandler)
return rpcVTGate
}
func (vtg *VTGate) registerDebugHealthHandler() {
http.HandleFunc("/debug/health", func(w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.MONITORING); err != nil {
acl.SendError(w, err)
return
}
w.Header().Set("Content-Type", "text/plain")
if err := vtg.IsHealthy(); err != nil {
w.Write([]byte("not ok"))
return
}
w.Write([]byte("ok"))
})
}
// IsHealthy returns nil if server is healthy.
// Otherwise, it returns an error indicating the reason.
func (vtg *VTGate) IsHealthy() error {
return nil
}
// Execute executes a non-streaming query by routing based on the values in the query.
func (vtg *VTGate) Execute(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspaceShard string, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool, options *querypb.ExecuteOptions) (newSession *vtgatepb.Session, qr *sqltypes.Result, err error) {
// We'll always return a non-nil session.
if session == nil {
session = &vtgatepb.Session{}
}
session, intercepted, err := vtg.intercept(ctx, sql, session)
if err != nil {
return session, nil, err
}
if intercepted {
return session, &sqltypes.Result{}, nil
}
// Normal processing.
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"Execute", "Any", ltt}
defer vtg.timings.Record(statsKey, startTime)
// Autocommit handling
autocommit := false
if session.Autocommit && !session.InTransaction && sqlparser.IsDML(sql) {
autocommit = true
vtg.localBegin(session)
}
keyspace, shard := parseKeyspaceOptionalShard(keyspaceShard)
if shard != "" {
sql = sqlannotation.AnnotateIfDML(sql, nil)
f := func(keyspace string) (string, []string, error) {
return keyspace, []string{shard}, nil
}
qr, err = vtg.resolver.Execute(ctx, sql, bindVariables, keyspace, tabletType, session, f, notInTransaction, options)
} else {
qr, err = vtg.router.Execute(ctx, sql, bindVariables, keyspace, tabletType, session, notInTransaction, options)
}
if err == nil && autocommit {
// Set the error if commit fails.
err = vtg.Commit(ctx, vtg.transactionMode == TxTwoPC, session)
}
if err == nil {
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
return session, qr, nil
}
// Error handling: Execute or Commit failed.
if autocommit {
// Rollback the transaction and ignore errors.
vtg.Rollback(ctx, session)
}
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"KeyspaceShard": keyspaceShard,
"TabletType": ltt,
"Session": session,
"NotInTransaction": notInTransaction,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecute)
return session, nil, err
}
// intercept checks for transactional or set statements. If they match then it performs the
// necessary operation and returns a new session and true indicating that it's intercepted
// the call. If so, the caller (Execute) should just return without proceeding further.
func (vtg *VTGate) intercept(ctx context.Context, sql string, session *vtgatepb.Session) (*vtgatepb.Session, bool, error) {
switch {
case sqlparser.IsStatement(sql, "begin"), sqlparser.IsStatement(sql, "start transaction"):
if session.InTransaction {
// If we're in a transaction, commit and start a new one.
if err := vtg.Commit(ctx, vtg.transactionMode == TxTwoPC, session); err != nil {
return session, true, err
}
}
vtg.localBegin(session)
return session, true, nil
case sqlparser.IsStatement(sql, "commit"):
if !session.InTransaction {
return session, true, nil
}
err := vtg.Commit(ctx, vtg.transactionMode == TxTwoPC, session)
return session, true, err
case sqlparser.IsStatement(sql, "rollback"):
if !session.InTransaction {
return session, true, nil
}
err := vtg.Rollback(ctx, session)
return session, true, err
case sqlparser.HasPrefix(sql, "set"):
vals, err := sqlparser.ExtractSetNums(sql)
if err != nil {
return session, true, vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, err.Error())
}
if len(vals) != 1 {
return session, true, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "too many set values: %s", sql)
}
val, ok := vals["autocommit"]
if !ok {
return session, true, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "unsupported construct: %s", sql)
}
if val != 0 {
session.Autocommit = true
} else {
session.Autocommit = false
}
return session, true, nil
}
return session, false, nil
}
// localBegin starts a transaction using the default settings of VTGate.
// This is different from the exported Begin because there is no explicit
// transaction mode when we implicitly begin a transaction.
func (vtg *VTGate) localBegin(session *vtgatepb.Session) {
session.InTransaction = true
session.SingleDb = vtg.transactionMode == TxSingle
}
// ExecuteShards executes a non-streaming query on the specified shards.
func (vtg *VTGate) ExecuteShards(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, shards []string, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool, options *querypb.ExecuteOptions) (*sqltypes.Result, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteShards", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
sql = sqlannotation.AnnotateIfDML(sql, nil)
qr, err := vtg.resolver.Execute(
ctx,
sql,
bindVariables,
keyspace,
tabletType,
session,
func(keyspace string) (string, []string, error) {
return keyspace, shards, nil
},
notInTransaction,
options,
)
if err == nil {
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
return qr, nil
}
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"Shards": shards,
"TabletType": ltt,
"Session": session,
"NotInTransaction": notInTransaction,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecuteShards)
return nil, err
}
// ExecuteKeyspaceIds executes a non-streaming query based on the specified keyspace ids.
func (vtg *VTGate) ExecuteKeyspaceIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyspaceIds [][]byte, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool, options *querypb.ExecuteOptions) (*sqltypes.Result, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteKeyspaceIds", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
sql = sqlannotation.AnnotateIfDML(sql, keyspaceIds)
qr, err := vtg.resolver.ExecuteKeyspaceIds(ctx, sql, bindVariables, keyspace, keyspaceIds, tabletType, session, notInTransaction, options)
if err == nil {
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
return qr, nil
}
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyspaceIds": keyspaceIds,
"TabletType": ltt,
"Session": session,
"NotInTransaction": notInTransaction,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecuteKeyspaceIds)
return nil, err
}
// ExecuteKeyRanges executes a non-streaming query based on the specified keyranges.
func (vtg *VTGate) ExecuteKeyRanges(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyRanges []*topodatapb.KeyRange, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool, options *querypb.ExecuteOptions) (*sqltypes.Result, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteKeyRanges", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
sql = sqlannotation.AnnotateIfDML(sql, nil)
qr, err := vtg.resolver.ExecuteKeyRanges(ctx, sql, bindVariables, keyspace, keyRanges, tabletType, session, notInTransaction, options)
if err == nil {
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
return qr, nil
}
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyRanges": keyRanges,
"TabletType": ltt,
"Session": session,
"NotInTransaction": notInTransaction,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecuteKeyRanges)
return nil, err
}
// ExecuteEntityIds excutes a non-streaming query based on given KeyspaceId map.
func (vtg *VTGate) ExecuteEntityIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, entityColumnName string, entityKeyspaceIDs []*vtgatepb.ExecuteEntityIdsRequest_EntityId, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool, options *querypb.ExecuteOptions) (*sqltypes.Result, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteEntityIds", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
sql = sqlannotation.AnnotateIfDML(sql, nil)
qr, err := vtg.resolver.ExecuteEntityIds(ctx, sql, bindVariables, keyspace, entityColumnName, entityKeyspaceIDs, tabletType, session, notInTransaction, options)
if err == nil {
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
return qr, nil
}
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"EntityColumnName": entityColumnName,
"EntityKeyspaceIDs": entityKeyspaceIDs,
"TabletType": ltt,
"Session": session,
"NotInTransaction": notInTransaction,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecuteEntityIds)
return nil, err
}
// ExecuteBatch executes a non-streaming queries by routing based on the values in the query.
func (vtg *VTGate) ExecuteBatch(ctx context.Context, sqlList []string, bindVariablesList []map[string]interface{}, keyspaceShard string, tabletType topodatapb.TabletType, session *vtgatepb.Session, options *querypb.ExecuteOptions) (*vtgatepb.Session, []sqltypes.QueryResponse, error) {
// We'll always return a non-nil session.
if session == nil {
session = &vtgatepb.Session{}
}
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteBatch", "Any", ltt}
defer vtg.timings.Record(statsKey, startTime)
qrl := make([]sqltypes.QueryResponse, len(sqlList))
for i, sql := range sqlList {
var bv map[string]interface{}
if len(bindVariablesList) != 0 {
bv = bindVariablesList[i]
}
session, qrl[i].QueryResult, qrl[i].QueryError = vtg.Execute(ctx, sql, bv, keyspaceShard, tabletType, session, false, options)
if qr := qrl[i].QueryResult; qr != nil {
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
}
}
return session, qrl, nil
}
// ExecuteBatchShards executes a group of queries on the specified shards.
func (vtg *VTGate) ExecuteBatchShards(ctx context.Context, queries []*vtgatepb.BoundShardQuery, tabletType topodatapb.TabletType, asTransaction bool, session *vtgatepb.Session, options *querypb.ExecuteOptions) ([]sqltypes.Result, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteBatchShards", "", ltt}
defer vtg.timings.Record(statsKey, startTime)
annotateBoundShardQueriesAsUnfriendly(queries)
qrs, err := vtg.resolver.ExecuteBatch(
ctx,
tabletType,
asTransaction,
session,
options,
func() (*scatterBatchRequest, error) {
return boundShardQueriesToScatterBatchRequest(queries)
})
if err == nil {
var rowCount int64
for _, qr := range qrs {
rowCount += int64(len(qr.Rows))
}
vtg.rowsReturned.Add(statsKey, rowCount)
return qrs, nil
}
query := map[string]interface{}{
"Queries": queries,
"TabletType": ltt,
"AsTransaction": asTransaction,
"Session": session,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecuteBatchShards)
return nil, err
}
// ExecuteBatchKeyspaceIds executes a group of queries based on the specified keyspace ids.
func (vtg *VTGate) ExecuteBatchKeyspaceIds(ctx context.Context, queries []*vtgatepb.BoundKeyspaceIdQuery, tabletType topodatapb.TabletType, asTransaction bool, session *vtgatepb.Session, options *querypb.ExecuteOptions) ([]sqltypes.Result, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"ExecuteBatchKeyspaceIds", "", ltt}
defer vtg.timings.Record(statsKey, startTime)
annotateBoundKeyspaceIDQueries(queries)
qrs, err := vtg.resolver.ExecuteBatchKeyspaceIds(
ctx,
queries,
tabletType,
asTransaction,
session,
options)
if err == nil {
var rowCount int64
for _, qr := range qrs {
rowCount += int64(len(qr.Rows))
}
vtg.rowsReturned.Add(statsKey, rowCount)
return qrs, nil
}
query := map[string]interface{}{
"Queries": queries,
"TabletType": ltt,
"AsTransaction": asTransaction,
"Session": session,
"Options": options,
}
err = recordAndAnnotateError(err, statsKey, query, vtg.logExecuteBatchKeyspaceIds)
return nil, err
}
// StreamExecute executes a streaming query by routing based on the values in the query.
func (vtg *VTGate) StreamExecute(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspaceShard string, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"StreamExecute", "Any", ltt}
defer vtg.timings.Record(statsKey, startTime)
keyspace, shard := parseKeyspaceOptionalShard(keyspaceShard)
var err error
if shard != "" {
err = vtg.resolver.streamExecute(
ctx,
sql,
bindVariables,
keyspace,
tabletType,
func(keyspace string) (string, []string, error) {
return keyspace, []string{shard}, nil
},
options,
func(reply *sqltypes.Result) error {
vtg.rowsReturned.Add(statsKey, int64(len(reply.Rows)))
return callback(reply)
})
} else {
err = vtg.router.StreamExecute(
ctx,
sql,
bindVariables,
keyspace,
tabletType,
options,
func(reply *sqltypes.Result) error {
vtg.rowsReturned.Add(statsKey, int64(len(reply.Rows)))
return callback(reply)
})
}
if err != nil {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"KeyspaceShard": keyspaceShard,
"TabletType": ltt,
"Options": options,
}
return recordAndAnnotateError(err, statsKey, query, vtg.logStreamExecute)
}
return nil
}
// StreamExecuteKeyspaceIds executes a streaming query on the specified KeyspaceIds.
// The KeyspaceIds are resolved to shards using the serving graph.
// This function currently temporarily enforces the restriction of executing on
// one shard since it cannot merge-sort the results to guarantee ordering of
// response which is needed for checkpointing.
// The api supports supplying multiple KeyspaceIds to make it future proof.
func (vtg *VTGate) StreamExecuteKeyspaceIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyspaceIds [][]byte, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"StreamExecuteKeyspaceIds", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
err := vtg.resolver.StreamExecuteKeyspaceIds(
ctx,
sql,
bindVariables,
keyspace,
keyspaceIds,
tabletType,
options,
func(reply *sqltypes.Result) error {
vtg.rowsReturned.Add(statsKey, int64(len(reply.Rows)))
return callback(reply)
})
if err != nil {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyspaceIds": keyspaceIds,
"TabletType": ltt,
"Options": options,
}
return recordAndAnnotateError(err, statsKey, query, vtg.logStreamExecuteKeyspaceIds)
}
return nil
}
// StreamExecuteKeyRanges executes a streaming query on the specified KeyRanges.
// The KeyRanges are resolved to shards using the serving graph.
// This function currently temporarily enforces the restriction of executing on
// one shard since it cannot merge-sort the results to guarantee ordering of
// response which is needed for checkpointing.
// The api supports supplying multiple keyranges to make it future proof.
func (vtg *VTGate) StreamExecuteKeyRanges(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyRanges []*topodatapb.KeyRange, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"StreamExecuteKeyRanges", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
err := vtg.resolver.StreamExecuteKeyRanges(
ctx,
sql,
bindVariables,
keyspace,
keyRanges,
tabletType,
options,
func(reply *sqltypes.Result) error {
vtg.rowsReturned.Add(statsKey, int64(len(reply.Rows)))
return callback(reply)
})
if err != nil {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyRanges": keyRanges,
"TabletType": ltt,
"Options": options,
}
return recordAndAnnotateError(err, statsKey, query, vtg.logStreamExecuteKeyRanges)
}
return nil
}
// StreamExecuteShards executes a streaming query on the specified shards.
func (vtg *VTGate) StreamExecuteShards(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, shards []string, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"StreamExecuteShards", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
err := vtg.resolver.streamExecute(
ctx,
sql,
bindVariables,
keyspace,
tabletType,
func(keyspace string) (string, []string, error) {
return keyspace, shards, nil
},
options,
func(reply *sqltypes.Result) error {
vtg.rowsReturned.Add(statsKey, int64(len(reply.Rows)))
return callback(reply)
})
if err != nil {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"Shards": shards,
"TabletType": ltt,
"Options": options,
}
return recordAndAnnotateError(err, statsKey, query, vtg.logStreamExecuteShards)
}
return nil
}
// Begin begins a transaction. It has to be concluded by a Commit or Rollback.
func (vtg *VTGate) Begin(ctx context.Context, singledb bool) (*vtgatepb.Session, error) {
if !singledb && vtg.transactionMode == TxSingle {
return nil, vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "multi-db transaction disallowed")
}
return &vtgatepb.Session{
InTransaction: true,
SingleDb: singledb,
}, nil
}
// Commit commits a transaction.
func (vtg *VTGate) Commit(ctx context.Context, twopc bool, session *vtgatepb.Session) error {
if twopc && vtg.transactionMode != TxTwoPC {
// Rollback the transaction to prevent future deadlocks.
vtg.txConn.Rollback(ctx, NewSafeSession(session))
return vterrors.New(vtrpcpb.Code_INVALID_ARGUMENT, "2pc transaction disallowed")
}
return formatError(vtg.txConn.Commit(ctx, twopc, NewSafeSession(session)))
}
// Rollback rolls back a transaction.
func (vtg *VTGate) Rollback(ctx context.Context, session *vtgatepb.Session) error {
return formatError(vtg.txConn.Rollback(ctx, NewSafeSession(session)))
}
// ResolveTransaction resolves the specified 2PC transaction.
func (vtg *VTGate) ResolveTransaction(ctx context.Context, dtid string) error {
return formatError(vtg.txConn.Resolve(ctx, dtid))
}
// isKeyspaceRangeBasedSharded returns true if a keyspace is sharded
// by range. This is true when there is a ShardingColumnType defined
// in the SrvKeyspace (that is using the range-based sharding with the
// client specifying the sharding key), or when the VSchema for the
// keyspace is Sharded.
func (vtg *VTGate) isKeyspaceRangeBasedSharded(keyspace string, srvKeyspace *topodatapb.SrvKeyspace) bool {
if srvKeyspace.ShardingColumnType != topodatapb.KeyspaceIdType_UNSET {
// We are using range based sharding with the application
// providing the sharding key value.
return true
}
if vtg.router.IsKeyspaceRangeBasedSharded(keyspace) {
// We are using range based sharding with the VSchema
// poviding the routing information
return true
}
// Not range based sharded, might be un-sharded or custom sharded.
return false
}
// SplitQuery implements the SplitQuery RPC. This is the new version that
// supports multiple split-columns and multiple splitting algorithms.
// See the documentation of SplitQueryRequest in "proto/vtgate.proto" for more
// information.
func (vtg *VTGate) SplitQuery(
ctx context.Context,
keyspace string,
sql string,
bindVariables map[string]interface{},
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]*vtgatepb.SplitQueryResponse_Part, error) {
// TODO(erez): Add validation of SplitQuery parameters.
keyspace, srvKeyspace, shardRefs, err := getKeyspaceShards(
ctx, vtg.resolver.toposerv, vtg.resolver.cell, keyspace, topodatapb.TabletType_RDONLY)
if err != nil {
return nil, err
}
// If the caller specified a splitCount (vs. specifying 'numRowsPerQueryPart') scale it by the
// number of shards (otherwise it stays 0).
perShardSplitCount := int64(math.Ceil(float64(splitCount) / float64(len(shardRefs))))
// Determine whether to return SplitQueryResponse_KeyRangeParts or SplitQueryResponse_ShardParts.
// We return 'KeyRangeParts' for sharded keyspaces that are not custom sharded. If the
// keyspace is custom sharded or unsharded we return 'ShardParts'.
var querySplitToQueryPartFunc func(
querySplit *querytypes.QuerySplit, shard string) (*vtgatepb.SplitQueryResponse_Part, error)
if vtg.isKeyspaceRangeBasedSharded(keyspace, srvKeyspace) {
// Index the shard references in 'shardRefs' by shard name.
shardRefByName := make(map[string]*topodatapb.ShardReference, len(shardRefs))
for _, shardRef := range shardRefs {
shardRefByName[shardRef.Name] = shardRef
}
querySplitToQueryPartFunc = getQuerySplitToKeyRangePartFunc(keyspace, shardRefByName)
} else {
// Keyspace is either unsharded or custom-sharded.
querySplitToQueryPartFunc = getQuerySplitToShardPartFunc(keyspace)
}
// Collect all shard names into a slice.
shardNames := make([]string, 0, len(shardRefs))
for _, shardRef := range shardRefs {
shardNames = append(shardNames, shardRef.Name)
}
return vtg.resolver.scatterConn.SplitQuery(
ctx,
sql,
bindVariables,
splitColumns,
perShardSplitCount,
numRowsPerQueryPart,
algorithm,
shardNames,
querySplitToQueryPartFunc,
keyspace)
}
// getQuerySplitToKeyRangePartFunc returns a function to use with scatterConn.SplitQuery
// that converts the given QuerySplit to a SplitQueryResponse_Part message whose KeyRangePart field
// is set.
func getQuerySplitToKeyRangePartFunc(
keyspace string,
shardReferenceByName map[string]*topodatapb.ShardReference) func(
querySplit *querytypes.QuerySplit, shard string) (*vtgatepb.SplitQueryResponse_Part, error) {
return func(
querySplit *querytypes.QuerySplit, shard string) (*vtgatepb.SplitQueryResponse_Part, error) {
// TODO(erez): Assert that shardReferenceByName contains an entry for 'shard'.
// Keyrange can be nil for the shard (e.g. for single-sharded keyspaces during resharding).
// In this case we append an empty keyrange that represents the entire keyspace.
keyranges := []*topodatapb.KeyRange{{Start: []byte{}, End: []byte{}}}
if shardReferenceByName[shard].KeyRange != nil {
keyranges = []*topodatapb.KeyRange{shardReferenceByName[shard].KeyRange}
}
bindVars, err := querytypes.BindVariablesToProto3(querySplit.BindVariables)
if err != nil {
return nil, err
}
return &vtgatepb.SplitQueryResponse_Part{
Query: &querypb.BoundQuery{
Sql: querySplit.Sql,
BindVariables: bindVars,
},
KeyRangePart: &vtgatepb.SplitQueryResponse_KeyRangePart{
Keyspace: keyspace,
KeyRanges: keyranges,
},
Size: querySplit.RowCount,
}, nil
}
}
// getQuerySplitToShardPartFunc returns a function to use with scatterConn.SplitQuery
// that converts the given QuerySplit to a SplitQueryResponse_Part message whose ShardPart field
// is set.
func getQuerySplitToShardPartFunc(keyspace string) func(
querySplit *querytypes.QuerySplit, shard string) (*vtgatepb.SplitQueryResponse_Part, error) {
return func(
querySplit *querytypes.QuerySplit, shard string) (*vtgatepb.SplitQueryResponse_Part, error) {
bindVars, err := querytypes.BindVariablesToProto3(querySplit.BindVariables)
if err != nil {
return nil, err
}
return &vtgatepb.SplitQueryResponse_Part{
Query: &querypb.BoundQuery{
Sql: querySplit.Sql,
BindVariables: bindVars,
},
ShardPart: &vtgatepb.SplitQueryResponse_ShardPart{
Keyspace: keyspace,
Shards: []string{shard},
},
Size: querySplit.RowCount,
}, nil
}
}
// GetSrvKeyspace is part of the vtgate service API.
func (vtg *VTGate) GetSrvKeyspace(ctx context.Context, keyspace string) (*topodatapb.SrvKeyspace, error) {
return vtg.resolver.toposerv.GetSrvKeyspace(ctx, vtg.resolver.cell, keyspace)
}
// MessageStream is part of the vtgate service API. This is a V2 level API that's sent
// to the Resolver.
func (vtg *VTGate) MessageStream(ctx context.Context, keyspace string, shard string, keyRange *topodatapb.KeyRange, name string, callback func(*sqltypes.Result) error) error {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(topodatapb.TabletType_MASTER)
statsKey := []string{"MessageStream", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
err := vtg.resolver.MessageStream(
ctx,
keyspace,
shard,
keyRange,
name,
callback,
)
if err != nil {
request := map[string]interface{}{
"Keyspace": keyspace,
"Shard": shard,
"KeyRange": keyRange,
"TabletType": ltt,
"MessageName": name,
}
recordAndAnnotateError(err, statsKey, request, vtg.logMessageStream)
}
return formatError(err)
}
// MessageAck is part of the vtgate service API. This is a V3 level API that's sent
// to the Router. The table name will be resolved using V3 rules, and the routing
// will make use of vindexes for sharded keyspaces.
func (vtg *VTGate) MessageAck(ctx context.Context, keyspace string, name string, ids []*querypb.Value) (int64, error) {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(topodatapb.TabletType_MASTER)
statsKey := []string{"MessageAck", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
count, err := vtg.router.MessageAck(ctx, keyspace, name, ids)
return count, formatError(err)
}
// UpdateStream is part of the vtgate service API.
func (vtg *VTGate) UpdateStream(ctx context.Context, keyspace string, shard string, keyRange *topodatapb.KeyRange, tabletType topodatapb.TabletType, timestamp int64, event *querypb.EventToken, callback func(*querypb.StreamEvent, int64) error) error {
startTime := time.Now()
ltt := topoproto.TabletTypeLString(tabletType)
statsKey := []string{"UpdateStream", keyspace, ltt}
defer vtg.timings.Record(statsKey, startTime)
err := vtg.resolver.UpdateStream(
ctx,
keyspace,
shard,
keyRange,
tabletType,
timestamp,
event,
callback,
)
if err != nil {
request := map[string]interface{}{
"Keyspace": keyspace,
"Shard": shard,
"KeyRange": keyRange,
"TabletType": ltt,
"Timestamp": timestamp,
}
recordAndAnnotateError(err, statsKey, request, vtg.logUpdateStream)
}
return formatError(err)
}
// GetGatewayCacheStatus returns a displayable version of the Gateway cache.
func (vtg *VTGate) GetGatewayCacheStatus() gateway.TabletCacheStatusList {
return vtg.resolver.GetGatewayCacheStatus()
}
// VSchemaStats returns the loaded vschema stats.
func (vtg *VTGate) VSchemaStats() *VSchemaStats {
return vtg.router.planner.VSchemaStats()
}
func recordAndAnnotateError(err error, statsKey []string, request map[string]interface{}, logger *logutil.ThrottledLogger) error {