/
vcursor_impl.go
812 lines (704 loc) · 27.8 KB
/
vcursor_impl.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
/*
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 (
"context"
"fmt"
"sort"
"strings"
"sync/atomic"
"time"
"golang.org/x/sync/errgroup"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/discovery"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vschemapb "vitess.io/vitess/go/vt/proto/vschema"
vtgatepb "vitess.io/vitess/go/vt/proto/vtgate"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/srvtopo"
"vitess.io/vitess/go/vt/topo"
topoprotopb "vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/vtgate/vschemaacl"
)
var _ engine.VCursor = (*vcursorImpl)(nil)
var _ planbuilder.ContextVSchema = (*vcursorImpl)(nil)
var _ iExecute = (*Executor)(nil)
var _ vindexes.VCursor = (*vcursorImpl)(nil)
// vcursor_impl needs these facilities to be able to be able to execute queries for vindexes
type iExecute interface {
Execute(ctx context.Context, method string, session *SafeSession, s string, vars map[string]*querypb.BindVariable) (*sqltypes.Result, error)
ExecuteMultiShard(ctx context.Context, rss []*srvtopo.ResolvedShard, queries []*querypb.BoundQuery, session *SafeSession, autocommit bool, ignoreMaxMemoryRows bool) (qr *sqltypes.Result, errs []error)
StreamExecuteMulti(ctx context.Context, s string, rss []*srvtopo.ResolvedShard, vars []map[string]*querypb.BindVariable, options *querypb.ExecuteOptions, callback func(reply *sqltypes.Result) error) []error
ExecuteLock(ctx context.Context, rs *srvtopo.ResolvedShard, query *querypb.BoundQuery, session *SafeSession) (*sqltypes.Result, error)
Commit(ctx context.Context, safeSession *SafeSession) error
ExecuteMessageStream(ctx context.Context, rss []*srvtopo.ResolvedShard, name string, callback func(*sqltypes.Result) error) error
ExecuteVStream(ctx context.Context, rss []*srvtopo.ResolvedShard, filter *binlogdatapb.Filter, gtid string, callback func(evs []*binlogdatapb.VEvent) error) error
// TODO: remove when resolver is gone
ParseDestinationTarget(targetString string) (string, topodatapb.TabletType, key.Destination, error)
VSchema() *vindexes.VSchema
}
//VSchemaOperator is an interface to Vschema Operations
type VSchemaOperator interface {
GetCurrentSrvVschema() *vschemapb.SrvVSchema
UpdateVSchema(ctx context.Context, ksName string, vschema *vschemapb.SrvVSchema) error
}
// vcursorImpl implements the VCursor functionality used by dependent
// packages to call back into VTGate.
type vcursorImpl struct {
ctx context.Context
safeSession *SafeSession
keyspace string
tabletType topodatapb.TabletType
destination key.Destination
marginComments sqlparser.MarginComments
executor iExecute
resolver *srvtopo.Resolver
topoServer *topo.Server
logStats *LogStats
// rollbackOnPartialExec is set to true if any DML was successfully
// executed. If there was a subsequent failure, the transaction
// must be forced to rollback.
rollbackOnPartialExec bool
ignoreMaxMemoryRows bool
vschema *vindexes.VSchema
vm VSchemaOperator
semTable *semantics.SemTable
warnShardedOnly bool // when using sharded only features, a warning will be warnings field
warnings []*querypb.QueryWarning // any warnings that are accumulated during the planning phase are stored here
}
// newVcursorImpl creates a vcursorImpl. Before creating this object, you have to separate out any marginComments that came with
// the query and supply it here. Trailing comments are typically sent by the application for various reasons,
// including as identifying markers. So, they have to be added back to all queries that are executed
// on behalf of the original query.
func newVCursorImpl(
ctx context.Context,
safeSession *SafeSession,
marginComments sqlparser.MarginComments,
executor *Executor,
logStats *LogStats,
vm VSchemaOperator,
vschema *vindexes.VSchema,
resolver *srvtopo.Resolver,
serv srvtopo.Server,
warnShardedOnly bool,
) (*vcursorImpl, error) {
keyspace, tabletType, destination, err := parseDestinationTarget(safeSession.TargetString, vschema)
if err != nil {
return nil, err
}
// With DiscoveryGateway transactions are only allowed on master.
if UsingLegacyGateway() && safeSession.InTransaction() && tabletType != topodatapb.TabletType_MASTER {
return nil, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "transaction is supported only for master tablet type, current type: %v", tabletType)
}
var ts *topo.Server
// We don't have access to the underlying TopoServer if this vtgate is
// filtering keyspaces because we don't have an accurate view of the topo.
if serv != nil && !discovery.FilteringKeyspaces() {
ts, err = serv.GetTopoServer()
if err != nil {
return nil, err
}
}
return &vcursorImpl{
ctx: ctx,
safeSession: safeSession,
keyspace: keyspace,
tabletType: tabletType,
destination: destination,
marginComments: marginComments,
executor: executor,
logStats: logStats,
resolver: resolver,
vschema: vschema,
vm: vm,
topoServer: ts,
warnShardedOnly: warnShardedOnly,
}, nil
}
// Context returns the current Context.
func (vc *vcursorImpl) Context() context.Context {
return vc.ctx
}
// MaxMemoryRows returns the maxMemoryRows flag value.
func (vc *vcursorImpl) MaxMemoryRows() int {
return *maxMemoryRows
}
// ExceedsMaxMemoryRows returns a boolean indicating whether the maxMemoryRows value has been exceeded.
// Returns false if the max memory rows override directive is set to true.
func (vc *vcursorImpl) ExceedsMaxMemoryRows(numRows int) bool {
return !vc.ignoreMaxMemoryRows && numRows > *maxMemoryRows
}
// SetIgnoreMaxMemoryRows sets the ignoreMaxMemoryRows value.
func (vc *vcursorImpl) SetIgnoreMaxMemoryRows(ignoreMaxMemoryRows bool) {
vc.ignoreMaxMemoryRows = ignoreMaxMemoryRows
}
// SetContextTimeout updates context and sets a timeout.
func (vc *vcursorImpl) SetContextTimeout(timeout time.Duration) context.CancelFunc {
ctx, cancel := context.WithTimeout(vc.ctx, timeout)
vc.ctx = ctx
return cancel
}
// ErrorGroupCancellableContext updates context that can be cancelled.
func (vc *vcursorImpl) ErrorGroupCancellableContext() (*errgroup.Group, func()) {
origCtx := vc.ctx
g, ctx := errgroup.WithContext(vc.ctx)
vc.ctx = ctx
return g, func() {
vc.ctx = origCtx
}
}
// RecordWarning stores the given warning in the current session
func (vc *vcursorImpl) RecordWarning(warning *querypb.QueryWarning) {
vc.safeSession.RecordWarning(warning)
}
// FindTable finds the specified table. If the keyspace what specified in the input, it gets used as qualifier.
// Otherwise, the keyspace from the request is used, if one was provided.
func (vc *vcursorImpl) FindTable(name sqlparser.TableName) (*vindexes.Table, string, topodatapb.TabletType, key.Destination, error) {
destKeyspace, destTabletType, dest, err := vc.executor.ParseDestinationTarget(name.Qualifier.String())
if err != nil {
return nil, "", destTabletType, nil, err
}
if destKeyspace == "" {
destKeyspace = vc.keyspace
}
table, err := vc.vschema.FindTable(destKeyspace, name.Name.String())
if err != nil {
return nil, "", destTabletType, nil, err
}
return table, destKeyspace, destTabletType, dest, err
}
func (vc *vcursorImpl) FindRoutedTable(name sqlparser.TableName) (*vindexes.Table, error) {
destKeyspace, destTabletType, _, err := vc.executor.ParseDestinationTarget(name.Qualifier.String())
if err != nil {
return nil, err
}
if destKeyspace == "" {
destKeyspace = vc.keyspace
}
table, err := vc.vschema.FindRoutedTable(destKeyspace, name.Name.String(), destTabletType)
if err != nil {
return nil, err
}
return table, nil
}
// FindTableOrVindex finds the specified table or vindex.
func (vc *vcursorImpl) FindTableOrVindex(name sqlparser.TableName) (*vindexes.Table, vindexes.Vindex, string, topodatapb.TabletType, key.Destination, error) {
destKeyspace, destTabletType, dest, err := vc.executor.ParseDestinationTarget(name.Qualifier.String())
if err != nil {
return nil, nil, "", destTabletType, nil, err
}
if destKeyspace == "" {
destKeyspace = vc.getActualKeyspace()
}
table, vindex, err := vc.vschema.FindTableOrVindex(destKeyspace, name.Name.String(), vc.tabletType)
if err != nil {
return nil, nil, "", destTabletType, nil, err
}
return table, vindex, destKeyspace, destTabletType, dest, nil
}
func (vc *vcursorImpl) getActualKeyspace() string {
if !sqlparser.SystemSchema(vc.keyspace) {
return vc.keyspace
}
ks, err := vc.AnyKeyspace()
if err != nil {
return ""
}
return ks.Name
}
// DefaultKeyspace returns the default keyspace of the current request
// if there is one. If the keyspace specified in the target cannot be
// identified, it returns an error.
func (vc *vcursorImpl) DefaultKeyspace() (*vindexes.Keyspace, error) {
if ignoreKeyspace(vc.keyspace) {
return nil, errNoKeyspace
}
ks, ok := vc.vschema.Keyspaces[vc.keyspace]
if !ok {
return nil, vterrors.NewErrorf(vtrpcpb.Code_NOT_FOUND, vterrors.BadDb, "Unknown database '%s' in vschema", vc.keyspace)
}
return ks.Keyspace, nil
}
var errNoDbAvailable = vterrors.NewErrorf(vtrpcpb.Code_FAILED_PRECONDITION, vterrors.NoDB, "no database available")
func (vc *vcursorImpl) AnyKeyspace() (*vindexes.Keyspace, error) {
keyspace, err := vc.DefaultKeyspace()
if err == nil {
return keyspace, nil
}
if err != errNoKeyspace {
return nil, err
}
if len(vc.vschema.Keyspaces) == 0 {
return nil, errNoDbAvailable
}
// Looks for any sharded keyspace if present, otherwise take any keyspace.
for _, ks := range vc.vschema.Keyspaces {
keyspace = ks.Keyspace
if keyspace.Sharded {
return keyspace, nil
}
}
return keyspace, nil
}
func (vc *vcursorImpl) FirstSortedKeyspace() (*vindexes.Keyspace, error) {
if len(vc.vschema.Keyspaces) == 0 {
return nil, errNoDbAvailable
}
kss := vc.vschema.Keyspaces
keys := make([]string, 0, len(kss))
for ks := range kss {
keys = append(keys, ks)
}
sort.Strings(keys)
return kss[keys[0]].Keyspace, nil
}
// SysVarSetEnabled implements the ContextVSchema interface
func (vc *vcursorImpl) SysVarSetEnabled() bool {
return vc.GetSessionEnableSystemSettings()
}
// KeyspaceExists provides whether the keyspace exists or not.
func (vc *vcursorImpl) KeyspaceExists(ks string) bool {
return vc.vschema.Keyspaces[ks] != nil
}
// AllKeyspace implements the ContextVSchema interface
func (vc *vcursorImpl) AllKeyspace() ([]*vindexes.Keyspace, error) {
if len(vc.vschema.Keyspaces) == 0 {
return nil, errNoDbAvailable
}
var kss []*vindexes.Keyspace
for _, ks := range vc.vschema.Keyspaces {
kss = append(kss, ks.Keyspace)
}
return kss, nil
}
// Planner implements the ContextVSchema interface
func (vc *vcursorImpl) Planner() planbuilder.PlannerVersion {
if vc.safeSession.Options != nil &&
vc.safeSession.Options.PlannerVersion != querypb.ExecuteOptions_DEFAULT_PLANNER {
return vc.safeSession.Options.PlannerVersion
}
switch strings.ToLower(*plannerVersion) {
case "v3":
return planbuilder.V3
case "gen4":
return planbuilder.Gen4
case "gen4greedy", "greedy":
return planbuilder.Gen4GreedyOnly
case "left2right":
return planbuilder.Gen4Left2Right
case "gen4fallback":
return planbuilder.Gen4WithFallback
}
log.Warning("unknown planner version configured. using the default")
return planbuilder.V3
}
// GetSemTable implements the ContextVSchema interface
func (vc *vcursorImpl) GetSemTable() *semantics.SemTable {
return vc.semTable
}
// TargetString returns the current TargetString of the session.
func (vc *vcursorImpl) TargetString() string {
return vc.safeSession.TargetString
}
// Execute is part of the engine.VCursor interface.
func (vc *vcursorImpl) Execute(method string, query string, bindVars map[string]*querypb.BindVariable, rollbackOnError bool, co vtgatepb.CommitOrder) (*sqltypes.Result, error) {
session := vc.safeSession
if co == vtgatepb.CommitOrder_AUTOCOMMIT {
// For autocommit, we have to create an independent session.
session = NewAutocommitSession(vc.safeSession.Session)
} else {
session.SetCommitOrder(co)
defer session.SetCommitOrder(vtgatepb.CommitOrder_NORMAL)
}
qr, err := vc.executor.Execute(vc.ctx, method, session, vc.marginComments.Leading+query+vc.marginComments.Trailing, bindVars)
if err == nil && rollbackOnError {
vc.rollbackOnPartialExec = true
}
return qr, err
}
// ExecuteMultiShard is part of the engine.VCursor interface.
func (vc *vcursorImpl) ExecuteMultiShard(rss []*srvtopo.ResolvedShard, queries []*querypb.BoundQuery, rollbackOnError, autocommit bool) (*sqltypes.Result, []error) {
atomic.AddUint64(&vc.logStats.ShardQueries, uint64(len(queries)))
qr, errs := vc.executor.ExecuteMultiShard(vc.ctx, rss, commentedShardQueries(queries, vc.marginComments), vc.safeSession, autocommit, vc.ignoreMaxMemoryRows)
if errs == nil && rollbackOnError {
vc.rollbackOnPartialExec = true
}
return qr, errs
}
func (vc *vcursorImpl) InTransactionAndIsDML() bool {
if !vc.safeSession.InTransaction() {
return false
}
switch vc.logStats.StmtType {
case "INSERT", "REPLACE", "UPDATE", "DELETE":
return true
}
return false
}
func (vc *vcursorImpl) LookupRowLockShardSession() vtgatepb.CommitOrder {
switch vc.logStats.StmtType {
case "DELETE", "UPDATE":
return vtgatepb.CommitOrder_POST
}
return vtgatepb.CommitOrder_PRE
}
func (vc *vcursorImpl) ExecuteLock(rs *srvtopo.ResolvedShard, query *querypb.BoundQuery) (*sqltypes.Result, error) {
query.Sql = vc.marginComments.Leading + query.Sql + vc.marginComments.Trailing
return vc.executor.ExecuteLock(vc.ctx, rs, query, vc.safeSession)
}
// AutocommitApproval is part of the engine.VCursor interface.
func (vc *vcursorImpl) AutocommitApproval() bool {
return vc.safeSession.AutocommitApproval()
}
// ExecuteStandalone is part of the engine.VCursor interface.
func (vc *vcursorImpl) ExecuteStandalone(query string, bindVars map[string]*querypb.BindVariable, rs *srvtopo.ResolvedShard) (*sqltypes.Result, error) {
rss := []*srvtopo.ResolvedShard{rs}
bqs := []*querypb.BoundQuery{
{
Sql: vc.marginComments.Leading + query + vc.marginComments.Trailing,
BindVariables: bindVars,
},
}
// The autocommit flag is always set to false because we currently don't
// execute DMLs through ExecuteStandalone.
qr, errs := vc.executor.ExecuteMultiShard(vc.ctx, rss, bqs, NewAutocommitSession(vc.safeSession.Session), false /* autocommit */, vc.ignoreMaxMemoryRows)
return qr, vterrors.Aggregate(errs)
}
// StreamExeculteMulti is the streaming version of ExecuteMultiShard.
func (vc *vcursorImpl) StreamExecuteMulti(query string, rss []*srvtopo.ResolvedShard, bindVars []map[string]*querypb.BindVariable, callback func(reply *sqltypes.Result) error) []error {
atomic.AddUint64(&vc.logStats.ShardQueries, uint64(len(rss)))
return vc.executor.StreamExecuteMulti(vc.ctx, vc.marginComments.Leading+query+vc.marginComments.Trailing, rss, bindVars, vc.safeSession.Options, callback)
}
// ExecuteKeyspaceID is part of the engine.VCursor interface.
func (vc *vcursorImpl) ExecuteKeyspaceID(keyspace string, ksid []byte, query string, bindVars map[string]*querypb.BindVariable, rollbackOnError, autocommit bool) (*sqltypes.Result, error) {
atomic.AddUint64(&vc.logStats.ShardQueries, 1)
rss, _, err := vc.ResolveDestinations(keyspace, nil, []key.Destination{key.DestinationKeyspaceID(ksid)})
if err != nil {
return nil, err
}
queries := []*querypb.BoundQuery{{
Sql: query,
BindVariables: bindVars,
}}
qr, errs := vc.ExecuteMultiShard(rss, queries, rollbackOnError, autocommit)
if len(errs) == 0 {
if rollbackOnError {
vc.rollbackOnPartialExec = true
}
return qr, nil
}
return nil, errs[0]
}
func (vc *vcursorImpl) ResolveDestinations(keyspace string, ids []*querypb.Value, destinations []key.Destination) ([]*srvtopo.ResolvedShard, [][]*querypb.Value, error) {
return vc.resolver.ResolveDestinations(vc.ctx, keyspace, vc.tabletType, ids, destinations)
}
func (vc *vcursorImpl) Session() engine.SessionActions {
return vc
}
func (vc *vcursorImpl) SetTarget(target string) error {
keyspace, tabletType, _, err := topoprotopb.ParseDestination(target, defaultTabletType)
if err != nil {
return err
}
if _, ok := vc.vschema.Keyspaces[keyspace]; !ignoreKeyspace(keyspace) && !ok {
return vterrors.NewErrorf(vtrpcpb.Code_NOT_FOUND, vterrors.BadDb, "unknown database '%s'", keyspace)
}
if vc.safeSession.InTransaction() && tabletType != topodatapb.TabletType_MASTER {
return vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.LockOrActiveTransaction, "can't execute the given command because you have an active transaction")
}
vc.safeSession.SetTargetString(target)
return nil
}
func ignoreKeyspace(keyspace string) bool {
return keyspace == "" || sqlparser.SystemSchema(keyspace)
}
func (vc *vcursorImpl) SetUDV(key string, value interface{}) error {
bindValue, err := sqltypes.BuildBindVariable(value)
if err != nil {
return err
}
vc.safeSession.SetUserDefinedVariable(key, bindValue)
return nil
}
func (vc *vcursorImpl) SetSysVar(name string, expr string) {
vc.safeSession.SetSystemVariable(name, expr)
}
//NeedsReservedConn implements the SessionActions interface
func (vc *vcursorImpl) NeedsReservedConn() {
vc.safeSession.SetReservedConn(true)
}
func (vc *vcursorImpl) InReservedConn() bool {
return vc.safeSession.InReservedConn()
}
func (vc *vcursorImpl) ShardSession() []*srvtopo.ResolvedShard {
ss := vc.safeSession.GetShardSessions()
if len(ss) == 0 {
return nil
}
rss := make([]*srvtopo.ResolvedShard, len(ss))
for i, shardSession := range ss {
rss[i] = &srvtopo.ResolvedShard{
Target: shardSession.Target,
Gateway: vc.resolver.GetGateway(),
}
}
return rss
}
// Destination implements the ContextVSchema interface
func (vc *vcursorImpl) Destination() key.Destination {
return vc.destination
}
// TabletType implements the ContextVSchema interface
func (vc *vcursorImpl) TabletType() topodatapb.TabletType {
return vc.tabletType
}
// SubmitOnlineDDL implements the VCursor interface
func (vc *vcursorImpl) SubmitOnlineDDL(onlineDDl *schema.OnlineDDL) error {
if vc.topoServer == nil {
return vterrors.New(vtrpcpb.Code_INTERNAL, "Unable to apply DDL toposerver unavailable, ensure this vtgate is not using filtered keyspaces")
}
conn, err := vc.topoServer.ConnForCell(vc.ctx, topo.GlobalCell)
if err != nil {
return err
}
// Submit an online schema change by writing a migration request in topo
return onlineDDl.WriteTopo(vc.ctx, conn, schema.MigrationRequestsPath())
}
func commentedShardQueries(shardQueries []*querypb.BoundQuery, marginComments sqlparser.MarginComments) []*querypb.BoundQuery {
if marginComments.Leading == "" && marginComments.Trailing == "" {
return shardQueries
}
newQueries := make([]*querypb.BoundQuery, len(shardQueries))
for i, v := range shardQueries {
newQueries[i] = &querypb.BoundQuery{
Sql: marginComments.Leading + v.Sql + marginComments.Trailing,
BindVariables: v.BindVariables,
}
}
return newQueries
}
// TargetDestination implements the ContextVSchema interface
func (vc *vcursorImpl) TargetDestination(qualifier string) (key.Destination, *vindexes.Keyspace, topodatapb.TabletType, error) {
keyspaceName := vc.keyspace
if vc.destination == nil && qualifier != "" {
keyspaceName = qualifier
}
if keyspaceName == "" {
return nil, nil, 0, errNoKeyspace
}
keyspace := vc.vschema.Keyspaces[keyspaceName]
if keyspace == nil {
return nil, nil, 0, vterrors.NewErrorf(vtrpcpb.Code_NOT_FOUND, vterrors.BadDb, "Unknown database '%s' in vschema", keyspaceName)
}
return vc.destination, keyspace.Keyspace, vc.tabletType, nil
}
// SetAutocommit implements the SessionActions interface
func (vc *vcursorImpl) SetAutocommit(autocommit bool) error {
if autocommit && vc.safeSession.InTransaction() {
if err := vc.executor.Commit(vc.ctx, vc.safeSession); err != nil {
return err
}
}
vc.safeSession.Autocommit = autocommit
return nil
}
// SetClientFoundRows implements the SessionActions interface
func (vc *vcursorImpl) SetClientFoundRows(clientFoundRows bool) error {
vc.safeSession.GetOrCreateOptions().ClientFoundRows = clientFoundRows
return nil
}
// SetSkipQueryPlanCache implements the SessionActions interface
func (vc *vcursorImpl) SetSkipQueryPlanCache(skipQueryPlanCache bool) error {
vc.safeSession.GetOrCreateOptions().SkipQueryPlanCache = skipQueryPlanCache
return nil
}
// SetSkipQueryPlanCache implements the SessionActions interface
func (vc *vcursorImpl) SetSQLSelectLimit(limit int64) error {
vc.safeSession.GetOrCreateOptions().SqlSelectLimit = limit
return nil
}
// SetSkipQueryPlanCache implements the SessionActions interface
func (vc *vcursorImpl) SetTransactionMode(mode vtgatepb.TransactionMode) {
vc.safeSession.TransactionMode = mode
}
// SetWorkload implements the SessionActions interface
func (vc *vcursorImpl) SetWorkload(workload querypb.ExecuteOptions_Workload) {
vc.safeSession.GetOrCreateOptions().Workload = workload
}
// SetPlannerVersion implements the SessionActions interface
func (vc *vcursorImpl) SetPlannerVersion(v planbuilder.PlannerVersion) {
vc.safeSession.GetOrCreateOptions().PlannerVersion = v
}
// SetFoundRows implements the SessionActions interface
func (vc *vcursorImpl) SetFoundRows(foundRows uint64) {
vc.safeSession.FoundRows = foundRows
vc.safeSession.foundRowsHandled = true
}
// SetReadAfterWriteGTID implements the SessionActions interface
func (vc *vcursorImpl) SetDDLStrategy(strategy string) {
vc.safeSession.SetDDLStrategy(strategy)
}
// SetReadAfterWriteGTID implements the SessionActions interface
func (vc *vcursorImpl) GetDDLStrategy() string {
return vc.safeSession.GetDDLStrategy()
}
// GetSessionUUID implements the SessionActions interface
func (vc *vcursorImpl) GetSessionUUID() string {
return vc.safeSession.GetSessionUUID()
}
// SetSessionEnableSystemSettings implements the SessionActions interface
func (vc *vcursorImpl) SetSessionEnableSystemSettings(allow bool) error {
vc.safeSession.SetSessionEnableSystemSettings(allow)
return nil
}
// GetSessionEnableSystemSettings implements the SessionActions interface
func (vc *vcursorImpl) GetSessionEnableSystemSettings() bool {
return vc.safeSession.GetSessionEnableSystemSettings()
}
// SetReadAfterWriteGTID implements the SessionActions interface
func (vc *vcursorImpl) SetReadAfterWriteGTID(vtgtid string) {
vc.safeSession.SetReadAfterWriteGTID(vtgtid)
}
// SetReadAfterWriteTimeout implements the SessionActions interface
func (vc *vcursorImpl) SetReadAfterWriteTimeout(timeout float64) {
vc.safeSession.SetReadAfterWriteTimeout(timeout)
}
// SetSessionTrackGTIDs implements the SessionActions interface
func (vc *vcursorImpl) SetSessionTrackGTIDs(enable bool) {
vc.safeSession.SetSessionTrackGtids(enable)
}
// HasCreatedTempTable implements the SessionActions interface
func (vc *vcursorImpl) HasCreatedTempTable() {
vc.safeSession.GetOrCreateOptions().HasCreatedTempTables = true
}
// GetWarnings implements the SessionActions interface
func (vc *vcursorImpl) GetWarnings() []*querypb.QueryWarning {
return vc.safeSession.GetWarnings()
}
// GetDBDDLPluginName implements the VCursor interface
func (vc *vcursorImpl) GetDBDDLPluginName() string {
return *dbDDLPlugin
}
// KeyspaceAvailable implements the VCursor interface
func (vc *vcursorImpl) KeyspaceAvailable(ks string) bool {
_, exists := vc.executor.VSchema().Keyspaces[ks]
return exists
}
// ErrorIfShardedF implements the VCursor interface
func (vc *vcursorImpl) ErrorIfShardedF(ks *vindexes.Keyspace, warn, errFormat string, params ...interface{}) error {
if ks.Sharded {
return vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, errFormat, params...)
}
vc.WarnUnshardedOnly("'%s' not supported in sharded mode", warn)
return nil
}
// WarnUnshardedOnly implements the VCursor interface
func (vc *vcursorImpl) WarnUnshardedOnly(format string, params ...interface{}) {
if vc.warnShardedOnly {
vc.warnings = append(vc.warnings, &querypb.QueryWarning{
Code: mysql.ERNotSupportedYet,
Message: fmt.Sprintf(format, params...),
})
}
}
// ForeignKey implements the VCursor interface
func (vc *vcursorImpl) ForeignKeyMode() string {
if foreignKeyMode == nil {
return ""
}
return strings.ToLower(*foreignKeyMode)
}
// ParseDestinationTarget parses destination target string and sets default keyspace if possible.
func parseDestinationTarget(targetString string, vschema *vindexes.VSchema) (string, topodatapb.TabletType, key.Destination, error) {
destKeyspace, destTabletType, dest, err := topoprotopb.ParseDestination(targetString, defaultTabletType)
// Set default keyspace
if destKeyspace == "" && len(vschema.Keyspaces) == 1 {
for k := range vschema.Keyspaces {
destKeyspace = k
}
}
return destKeyspace, destTabletType, dest, err
}
func (vc *vcursorImpl) planPrefixKey() string {
if vc.destination != nil {
switch vc.destination.(type) {
case key.DestinationKeyspaceID, key.DestinationKeyspaceIDs:
resolved, _, err := vc.ResolveDestinations(vc.keyspace, nil, []key.Destination{vc.destination})
if err == nil && len(resolved) > 0 {
shards := make([]string, len(resolved))
for i := 0; i < len(shards); i++ {
shards[i] = resolved[i].Target.GetShard()
}
sort.Strings(shards)
return fmt.Sprintf("%s%sKsIDsResolved(%s)", vc.keyspace, vindexes.TabletTypeSuffix[vc.tabletType], strings.Join(shards, ","))
}
default:
// use destination string (out of the switch)
}
return fmt.Sprintf("%s%s%s", vc.keyspace, vindexes.TabletTypeSuffix[vc.tabletType], vc.destination.String())
}
return fmt.Sprintf("%s%s", vc.keyspace, vindexes.TabletTypeSuffix[vc.tabletType])
}
func (vc *vcursorImpl) GetKeyspace() string {
return vc.keyspace
}
func (vc *vcursorImpl) ExecuteVSchema(keyspace string, vschemaDDL *sqlparser.AlterVschema) error {
srvVschema := vc.vm.GetCurrentSrvVschema()
if srvVschema == nil {
return vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vschema not loaded")
}
user := callerid.ImmediateCallerIDFromContext(vc.ctx)
allowed := vschemaacl.Authorized(user)
if !allowed {
return vterrors.NewErrorf(vtrpcpb.Code_PERMISSION_DENIED, vterrors.AccessDeniedError, "User '%s' is not authorized to perform vschema operations", user.GetUsername())
}
// Resolve the keyspace either from the table qualifier or the target keyspace
var ksName string
if !vschemaDDL.Table.IsEmpty() {
ksName = vschemaDDL.Table.Qualifier.String()
}
if ksName == "" {
ksName = keyspace
}
if ksName == "" {
return errNoKeyspace
}
ks := srvVschema.Keyspaces[ksName]
ks, err := topotools.ApplyVSchemaDDL(ksName, ks, vschemaDDL)
if err != nil {
return err
}
srvVschema.Keyspaces[ksName] = ks
return vc.vm.UpdateVSchema(vc.ctx, ksName, srvVschema)
}
func (vc *vcursorImpl) MessageStream(rss []*srvtopo.ResolvedShard, tableName string, callback func(*sqltypes.Result) error) error {
atomic.AddUint64(&vc.logStats.ShardQueries, uint64(len(rss)))
return vc.executor.ExecuteMessageStream(vc.ctx, rss, tableName, callback)
}
func (vc *vcursorImpl) VStream(rss []*srvtopo.ResolvedShard, filter *binlogdatapb.Filter, gtid string, callback func(evs []*binlogdatapb.VEvent) error) error {
return vc.executor.ExecuteVStream(vc.ctx, rss, filter, gtid, callback)
}