forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vtgate.go
769 lines (685 loc) · 28.1 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
// 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 (
"errors"
"flag"
"fmt"
"math"
"strings"
"time"
log "github.com/golang/glog"
"golang.org/x/net/context"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"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/tabletserver/tabletconn"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/vterrors"
"github.com/youtube/vitess/go/vt/vtgate/planbuilder"
"github.com/youtube/vitess/go/vt/vtgate/proto"
// import vindexes implementations
_ "github.com/youtube/vitess/go/vt/vtgate/vindexes"
"github.com/youtube/vitess/go/vt/vtgate/vtgateservice"
pb "github.com/youtube/vitess/go/vt/proto/topodata"
pbg "github.com/youtube/vitess/go/vt/proto/vtgate"
"github.com/youtube/vitess/go/vt/proto/vtrpc"
)
const errDupKey = "errno 1062"
const errOutOfRange = "errno 1264"
const errTxPoolFull = "tx_pool_full"
var (
rpcVTGate *VTGate
qpsByOperation *stats.Rates
qpsByKeyspace *stats.Rates
qpsByDbType *stats.Rates
errorsByOperation *stats.Rates
errorsByKeyspace *stats.Rates
errorsByDbType *stats.Rates
errTooManyInFlight = vterrors.FromError(
vtrpc.ErrorCode_TRANSIENT_ERROR,
errors.New("request_backlog: too many requests in flight"),
)
// Error counters should be global so they can be set from anywhere
normalErrors *stats.MultiCounters
infoErrors *stats.Counters
internalErrors *stats.Counters
)
// VTGate is the rpc interface to vtgate. Only one instance
// can be created. It implements vtgateservice.VTGateService
type VTGate struct {
resolver *Resolver
router *Router
timings *stats.MultiTimings
rowsReturned *stats.MultiCounters
maxInFlight int64
inFlight sync2.AtomicInt64
// 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
}
// RegisterVTGate defines the type of registration mechanism.
type RegisterVTGate func(vtgateservice.VTGateService)
// RegisterVTGates stores register funcs for VTGate server.
var RegisterVTGates []RegisterVTGate
var (
// RPCErrorOnlyInReply informs vtgateservice(s) about how to return errors.
RPCErrorOnlyInReply = flag.Bool("rpc-error-only-in-reply", false, "if true, supported RPC calls from vtgateservice(s) will only return errors as part of the RPC server response")
)
// Init initializes VTGate server.
func Init(hc discovery.HealthCheck, topoServer topo.Server, serv SrvTopoServer, schema *planbuilder.Schema, cell string, retryDelay time.Duration, retryCount int, connTimeoutTotal, connTimeoutPerConn, connLife time.Duration, maxInFlight int, testGateway string) {
if rpcVTGate != nil {
log.Fatalf("VTGate already initialized")
}
rpcVTGate = &VTGate{
resolver: NewResolver(hc, topoServer, serv, "VttabletCall", cell, retryDelay, retryCount, connTimeoutTotal, connTimeoutPerConn, connLife, testGateway),
timings: stats.NewMultiTimings("VtgateApi", []string{"Operation", "Keyspace", "DbType"}),
rowsReturned: stats.NewMultiCounters("VtgateApiRowsReturned", []string{"Operation", "Keyspace", "DbType"}),
maxInFlight: int64(maxInFlight),
inFlight: sync2.NewAtomicInt64(0),
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),
}
// Resuse resolver's scatterConn.
rpcVTGate.router = NewRouter(serv, cell, schema, "VTGateRouter", rpcVTGate.resolver.scatterConn)
normalErrors = stats.NewMultiCounters("VtgateApiErrorCounts", []string{"Operation", "Keyspace", "DbType"})
infoErrors = stats.NewCounters("VtgateInfoErrorCounts")
internalErrors = stats.NewCounters("VtgateInternalErrorCounts")
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(normalErrors, "Operation"), 15, 1*time.Minute)
errorsByKeyspace = stats.NewRates("ErrorsByKeyspace", stats.CounterForDimension(normalErrors, "Keyspace"), 15, 1*time.Minute)
errorsByDbType = stats.NewRates("ErrorsByDbType", stats.CounterForDimension(normalErrors, "DbType"), 15, 1*time.Minute)
for _, f := range RegisterVTGates {
f(rpcVTGate)
}
}
// InitializeConnections pre-initializes VTGate by connecting to vttablets of all keyspace/shard/type.
// It is not necessary to call this function before serving queries,
// but it would reduce connection overhead when serving.
func (vtg *VTGate) InitializeConnections(ctx context.Context) (err error) {
defer vtg.HandlePanic(&err)
log.Infof("Initialize VTTablet connections")
err = vtg.resolver.InitializeConnections(ctx)
if err != nil {
log.Errorf("failed to initialize connections: %v", err)
return err
}
log.Infof("Initialize VTTablet connections completed")
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{}, tabletType pb.TabletType, session *proto.Session, notInTransaction bool, reply *proto.QueryResult) error {
startTime := time.Now()
statsKey := []string{"Execute", "Any", strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
qr, err := vtg.router.Execute(ctx, sql, bindVariables, tabletType, session, notInTransaction)
if err == nil {
reply.Result = qr
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
} else {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"TabletType": strings.ToLower(tabletType.String()),
"Session": session,
"NotInTransaction": notInTransaction,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecute).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// 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 pb.TabletType, session *proto.Session, notInTransaction bool, reply *proto.QueryResult) error {
startTime := time.Now()
statsKey := []string{"ExecuteShards", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
sql = sqlannotation.AddFilteredReplicationUnfriendlyIfDML(sql)
qr, err := vtg.resolver.Execute(
ctx,
sql,
bindVariables,
keyspace,
tabletType,
session,
func(keyspace string) (string, []string, error) {
return keyspace, shards, nil
},
notInTransaction,
)
if err == nil {
reply.Result = qr
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
} else {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"Shards": shards,
"TabletType": strings.ToLower(tabletType.String()),
"Session": session,
"NotInTransaction": notInTransaction,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecuteShards).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// 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 pb.TabletType, session *proto.Session, notInTransaction bool, reply *proto.QueryResult) error {
startTime := time.Now()
statsKey := []string{"ExecuteKeyspaceIds", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
sql = sqlannotation.AddIfDML(sql, keyspaceIds)
qr, err := vtg.resolver.ExecuteKeyspaceIds(ctx, sql, bindVariables, keyspace, keyspaceIds, tabletType, session, notInTransaction)
if err == nil {
reply.Result = qr
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
} else {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyspaceIds": keyspaceIds,
"TabletType": strings.ToLower(tabletType.String()),
"Session": session,
"NotInTransaction": notInTransaction,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecuteKeyspaceIds).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// 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 []*pb.KeyRange, tabletType pb.TabletType, session *proto.Session, notInTransaction bool, reply *proto.QueryResult) error {
startTime := time.Now()
statsKey := []string{"ExecuteKeyRanges", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
sql = sqlannotation.AddFilteredReplicationUnfriendlyIfDML(sql)
qr, err := vtg.resolver.ExecuteKeyRanges(ctx, sql, bindVariables, keyspace, keyRanges, tabletType, session, notInTransaction)
if err == nil {
reply.Result = qr
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
} else {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyRanges": keyRanges,
"TabletType": strings.ToLower(tabletType.String()),
"Session": session,
"NotInTransaction": notInTransaction,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecuteKeyRanges).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// 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 []*pbg.ExecuteEntityIdsRequest_EntityId, tabletType pb.TabletType, session *proto.Session, notInTransaction bool, reply *proto.QueryResult) error {
startTime := time.Now()
statsKey := []string{"ExecuteEntityIds", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
sql = sqlannotation.AddFilteredReplicationUnfriendlyIfDML(sql)
qr, err := vtg.resolver.ExecuteEntityIds(ctx, sql, bindVariables, keyspace, entityColumnName, entityKeyspaceIDs, tabletType, session, notInTransaction)
if err == nil {
reply.Result = qr
vtg.rowsReturned.Add(statsKey, int64(len(qr.Rows)))
} else {
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"EntityColumnName": entityColumnName,
"EntityKeyspaceIDs": entityKeyspaceIDs,
"TabletType": strings.ToLower(tabletType.String()),
"Session": session,
"NotInTransaction": notInTransaction,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecuteEntityIds).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// ExecuteBatchShards executes a group of queries on the specified shards.
func (vtg *VTGate) ExecuteBatchShards(ctx context.Context, queries []proto.BoundShardQuery, tabletType pb.TabletType, asTransaction bool, session *proto.Session, reply *proto.QueryResultList) error {
startTime := time.Now()
statsKey := []string{"ExecuteBatchShards", "", ""}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
annotateBoundShardQueriesAsUnfriendly(queries)
qrs, err := vtg.resolver.ExecuteBatch(
ctx,
tabletType,
asTransaction,
session,
func() (*scatterBatchRequest, error) {
return boundShardQueriesToScatterBatchRequest(queries), nil
})
if err == nil {
reply.List = qrs.List
var rowCount int64
for _, qr := range qrs.List {
rowCount += int64(len(qr.Rows))
}
vtg.rowsReturned.Add(statsKey, rowCount)
} else {
query := map[string]interface{}{
"Queries": queries,
"TabletType": strings.ToLower(tabletType.String()),
"AsTransaction": asTransaction,
"Session": session,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecuteBatchShards).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// ExecuteBatchKeyspaceIds executes a group of queries based on the specified keyspace ids.
func (vtg *VTGate) ExecuteBatchKeyspaceIds(ctx context.Context, queries []proto.BoundKeyspaceIdQuery, tabletType pb.TabletType, asTransaction bool, session *proto.Session, reply *proto.QueryResultList) error {
startTime := time.Now()
statsKey := []string{"ExecuteBatchKeyspaceIds", "", ""}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
annotateBoundKeyspaceIDQueries(queries)
qrs, err := vtg.resolver.ExecuteBatchKeyspaceIds(
ctx,
queries,
tabletType,
asTransaction,
session)
if err == nil {
reply.List = qrs.List
var rowCount int64
for _, qr := range qrs.List {
rowCount += int64(len(qr.Rows))
}
vtg.rowsReturned.Add(statsKey, rowCount)
} else {
query := map[string]interface{}{
"Queries": queries,
"TabletType": strings.ToLower(tabletType.String()),
"AsTransaction": asTransaction,
"Session": session,
}
reply.Error = handleExecuteError(err, statsKey, query, vtg.logExecuteBatchKeyspaceIds).Error()
reply.Err = vterrors.RPCErrFromVtError(err)
}
reply.Session = session
return nil
}
// 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{}, tabletType pb.TabletType, sendReply func(*proto.QueryResult) error) error {
startTime := time.Now()
statsKey := []string{"StreamExecute", "Any", strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
var rowCount int64
err := vtg.router.StreamExecute(
ctx,
sql,
bindVariables,
tabletType,
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
reply.Result = mreply
rowCount += int64(len(mreply.Rows))
vtg.rowsReturned.Add(statsKey, int64(len(mreply.Rows)))
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses are sent.
return sendReply(reply)
})
if err != nil {
normalErrors.Add(statsKey, 1)
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"TabletType": strings.ToLower(tabletType.String()),
}
logError(err, query, vtg.logStreamExecute)
}
return formatError(err)
}
// 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 pb.TabletType, sendReply func(*proto.QueryResult) error) error {
startTime := time.Now()
statsKey := []string{"StreamExecuteKeyspaceIds", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
var rowCount int64
err := vtg.resolver.StreamExecuteKeyspaceIds(
ctx,
sql,
bindVariables,
keyspace,
keyspaceIds,
tabletType,
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
reply.Result = mreply
rowCount += int64(len(mreply.Rows))
vtg.rowsReturned.Add(statsKey, int64(len(mreply.Rows)))
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses are sent.
return sendReply(reply)
})
if err != nil {
normalErrors.Add(statsKey, 1)
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyspaceIds": keyspaceIds,
"TabletType": strings.ToLower(tabletType.String()),
}
logError(err, query, vtg.logStreamExecuteKeyspaceIds)
}
return formatError(err)
}
// 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 []*pb.KeyRange, tabletType pb.TabletType, sendReply func(*proto.QueryResult) error) error {
startTime := time.Now()
statsKey := []string{"StreamExecuteKeyRanges", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
var rowCount int64
err := vtg.resolver.StreamExecuteKeyRanges(
ctx,
sql,
bindVariables,
keyspace,
keyRanges,
tabletType,
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
reply.Result = mreply
rowCount += int64(len(mreply.Rows))
vtg.rowsReturned.Add(statsKey, int64(len(mreply.Rows)))
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses are sent.
return sendReply(reply)
})
if err != nil {
normalErrors.Add(statsKey, 1)
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"KeyRanges": keyRanges,
"TabletType": strings.ToLower(tabletType.String()),
}
logError(err, query, vtg.logStreamExecuteKeyRanges)
}
return formatError(err)
}
// 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 pb.TabletType, sendReply func(*proto.QueryResult) error) error {
startTime := time.Now()
statsKey := []string{"StreamExecuteShards", keyspace, strings.ToLower(tabletType.String())}
defer vtg.timings.Record(statsKey, startTime)
x := vtg.inFlight.Add(1)
defer vtg.inFlight.Add(-1)
if 0 < vtg.maxInFlight && vtg.maxInFlight < x {
return errTooManyInFlight
}
var rowCount int64
err := vtg.resolver.StreamExecute(
ctx,
sql,
bindVariables,
keyspace,
tabletType,
func(keyspace string) (string, []string, error) {
return keyspace, shards, nil
},
func(mreply *mproto.QueryResult) error {
reply := new(proto.QueryResult)
reply.Result = mreply
rowCount += int64(len(mreply.Rows))
vtg.rowsReturned.Add(statsKey, int64(len(mreply.Rows)))
// Note we don't populate reply.Session here,
// as it may change incrementaly as responses are sent.
return sendReply(reply)
})
if err != nil {
normalErrors.Add(statsKey, 1)
query := map[string]interface{}{
"Sql": sql,
"BindVariables": bindVariables,
"Keyspace": keyspace,
"Shards": shards,
"TabletType": strings.ToLower(tabletType.String()),
}
logError(err, query, vtg.logStreamExecuteShards)
}
return formatError(err)
}
// Begin begins a transaction. It has to be concluded by a Commit or Rollback.
func (vtg *VTGate) Begin(ctx context.Context, outSession *proto.Session) error {
outSession.InTransaction = true
return nil
}
// Commit commits a transaction.
func (vtg *VTGate) Commit(ctx context.Context, inSession *proto.Session) error {
return formatError(vtg.resolver.Commit(ctx, inSession))
}
// Rollback rolls back a transaction.
func (vtg *VTGate) Rollback(ctx context.Context, inSession *proto.Session) error {
return formatError(vtg.resolver.Rollback(ctx, inSession))
}
// SplitQuery splits a query into sub queries by appending keyranges and
// primary key range clauses. Rows corresponding to the sub queries
// are guaranteed to be non-overlapping and will add up to the rows of
// original query. Number of sub queries will be a multiple of N that is
// greater than or equal to SplitQueryRequest.SplitCount, where N is the
// number of shards.
func (vtg *VTGate) SplitQuery(ctx context.Context, keyspace string, sql string, bindVariables map[string]interface{}, splitColumn string, splitCount int) ([]*pbg.SplitQueryResponse_Part, error) {
keyspace, srvKeyspace, shards, err := getKeyspaceShards(ctx, vtg.resolver.toposerv, vtg.resolver.cell, keyspace, pb.TabletType_RDONLY)
if err != nil {
return nil, err
}
perShardSplitCount := int(math.Ceil(float64(splitCount) / float64(len(shards))))
if srvKeyspace.ShardingColumnName != "" {
// we are using range-based sharding, so the result
// will be a list of Splits with KeyRange clauses
keyRangeByShard := make(map[string]*pb.KeyRange)
for _, shard := range shards {
keyRangeByShard[shard.Name] = shard.KeyRange
}
return vtg.resolver.scatterConn.SplitQueryKeyRange(ctx, sql, bindVariables, splitColumn, perShardSplitCount, keyRangeByShard, keyspace)
}
// we are using custom sharding, so the result
// will be a list of Splits with Shard clauses
shardNames := make([]string, len(shards))
for i, shard := range shards {
shardNames[i] = shard.Name
}
return vtg.resolver.scatterConn.SplitQueryCustomSharding(ctx, sql, bindVariables, splitColumn, perShardSplitCount, shardNames, keyspace)
}
// GetSrvKeyspace is part of the vtgate service API.
func (vtg *VTGate) GetSrvKeyspace(ctx context.Context, keyspace string) (*pb.SrvKeyspace, error) {
return vtg.resolver.toposerv.GetSrvKeyspace(ctx, vtg.resolver.cell, keyspace)
}
// GetSrvShard is part of the vtgate service API.
func (vtg *VTGate) GetSrvShard(ctx context.Context, keyspace, shard string) (*pb.SrvShard, error) {
return vtg.resolver.toposerv.GetSrvShard(ctx, vtg.resolver.cell, keyspace, shard)
}
// Any errors that are caused by VTGate dependencies (e.g, VtTablet) should be logged
// as errors in those components, but logged to Info in VTGate itself.
func logError(err error, query map[string]interface{}, logger *logutil.ThrottledLogger) {
logMethod := logger.Errorf
if isErrorCausedByVTGate(err) {
logMethod = logger.Errorf
} else {
infoErrors.Add("NonVtgateErrors", 1)
logMethod = logger.Infof
}
logMethod("%v, query: %+v", err, query)
}
// Returns true if a given error is caused entirely due to VTGate, and not any of
// the components that it depends on.
func isErrorCausedByVTGate(err error) bool {
var errQueue []error
errQueue = append(errQueue, err)
for len(errQueue) > 0 {
// pop the first item from the queue
e := errQueue[0]
errQueue = errQueue[1:]
switch e := e.(type) {
case *ScatterConnError:
errQueue = append(errQueue, e.Errs...)
case *ShardConnError:
errQueue = append(errQueue, e.Err)
case tabletconn.OperationalError:
// tabletconn.Cancelled errors are due to client behavior, not VTGate errors.
if e != tabletconn.Cancelled {
return true
}
case *tabletconn.ServerError:
break
default:
// Return true if even a single error within the error queue was
// caused by VTGate. If we're not certain what caused the error, we
// default to assuming that VTGate was at fault
return true
}
}
return false
}
func handleExecuteError(err error, statsKey []string, query map[string]interface{}, logger *logutil.ThrottledLogger) error {
s := fmt.Sprintf(", vtgate: %v", servenv.ListeningURL.String())
newErr := vterrors.WithSuffix(err, s)
errStr := newErr.Error()
if strings.Contains(errStr, errDupKey) {
infoErrors.Add("DupKey", 1)
} else if strings.Contains(errStr, errOutOfRange) {
infoErrors.Add("OutOfRange", 1)
} else if strings.Contains(errStr, errTxPoolFull) {
normalErrors.Add(statsKey, 1)
} else {
normalErrors.Add(statsKey, 1)
logError(err, query, logger)
}
return newErr
}
func formatError(err error) error {
if err == nil {
return nil
}
s := fmt.Sprintf(", vtgate: %v", servenv.ListeningURL.String())
return vterrors.WithSuffix(err, s)
}
// HandlePanic recovers from panics, and logs / increment counters
func (vtg *VTGate) HandlePanic(err *error) {
if x := recover(); x != nil {
log.Errorf("Uncaught panic:\n%v\n%s", x, tb.Stack(4))
*err = fmt.Errorf("uncaught panic: %v, vtgate: %v", x, servenv.ListeningURL.String())
internalErrors.Add("Panic", 1)
}
}
// Helper function used in ExecuteBatchKeyspaceIds
func annotateBoundKeyspaceIDQueries(queries []proto.BoundKeyspaceIdQuery) {
for i := range queries {
if len(queries[i].KeyspaceIds) == 1 {
queries[i].Sql = sqlannotation.AddKeyspaceIDIfDML(queries[i].Sql, []byte(queries[i].KeyspaceIds[0]))
} else {
queries[i].Sql = sqlannotation.AddFilteredReplicationUnfriendlyIfDML(queries[i].Sql)
}
}
}
// Helper function used in ExecuteBatchShards
func annotateBoundShardQueriesAsUnfriendly(queries []proto.BoundShardQuery) {
for i := range queries {
queries[i].Sql =
sqlannotation.AddFilteredReplicationUnfriendlyIfDML(queries[i].Sql)
}
}