/
tablet_map.go
831 lines (706 loc) · 32.8 KB
/
tablet_map.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
/*
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 vtcombo
import (
"flag"
"fmt"
"os"
"path"
"time"
"context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/hook"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/mysqlctl/tmutils"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/vttablet/queryservice"
"vitess.io/vitess/go/vt/vttablet/tabletconn"
"vitess.io/vitess/go/vt/vttablet/tabletmanager"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
"vitess.io/vitess/go/vt/vttablet/tmclient"
"vitess.io/vitess/go/vt/wrangler"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
replicationdatapb "vitess.io/vitess/go/vt/proto/replicationdata"
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
vttestpb "vitess.io/vitess/go/vt/proto/vttest"
)
// tablet contains all the data for an individual tablet.
type comboTablet struct {
// configuration parameters
alias *topodatapb.TabletAlias
keyspace string
shard string
tabletType topodatapb.TabletType
dbname string
uid uint32
// objects built at construction time
qsc tabletserver.Controller
tm *tabletmanager.TabletManager
}
// tabletMap maps the tablet uid to the tablet record
var tabletMap map[uint32]*comboTablet
// CreateTablet creates an individual tablet, with its tm, and adds
// it to the map. If it's a master tablet, it also issues a TER.
func CreateTablet(ctx context.Context, ts *topo.Server, cell string, uid uint32, keyspace, shard, dbname string, tabletType topodatapb.TabletType, mysqld mysqlctl.MysqlDaemon, dbcfgs *dbconfigs.DBConfigs) error {
alias := &topodatapb.TabletAlias{
Cell: cell,
Uid: uid,
}
log.Infof("Creating %v tablet %v for %v/%v", tabletType, topoproto.TabletAliasString(alias), keyspace, shard)
flag.Set("debug-url-prefix", fmt.Sprintf("/debug-%d", uid))
controller := tabletserver.NewServer(topoproto.TabletAliasString(alias), ts, alias)
initTabletType := tabletType
if tabletType == topodatapb.TabletType_MASTER {
initTabletType = topodatapb.TabletType_REPLICA
}
_, kr, err := topo.ValidateShardName(shard)
if err != nil {
return err
}
tm := &tabletmanager.TabletManager{
BatchCtx: context.Background(),
TopoServer: ts,
MysqlDaemon: mysqld,
DBConfigs: dbcfgs,
QueryServiceControl: controller,
}
tablet := &topodatapb.Tablet{
Alias: alias,
PortMap: map[string]int32{
"vt": int32(8000 + uid),
"grpc": int32(9000 + uid),
},
Keyspace: keyspace,
Shard: shard,
KeyRange: kr,
Type: initTabletType,
DbNameOverride: dbname,
}
if err := tm.Start(tablet, 0); err != nil {
return err
}
if tabletType == topodatapb.TabletType_MASTER {
if err := tm.ChangeType(ctx, topodatapb.TabletType_MASTER); err != nil {
return fmt.Errorf("TabletExternallyReparented failed on master %v: %v", topoproto.TabletAliasString(alias), err)
}
}
controller.AddStatusHeader()
controller.AddStatusPart()
tabletMap[uid] = &comboTablet{
alias: alias,
keyspace: keyspace,
shard: shard,
tabletType: tabletType,
dbname: dbname,
uid: uid,
qsc: controller,
tm: tm,
}
return nil
}
// InitTabletMap creates the action tms and associated data structures
// for all tablets, based on the vttest proto parameter.
func InitTabletMap(ts *topo.Server, tpb *vttestpb.VTTestTopology, mysqld mysqlctl.MysqlDaemon, dbcfgs *dbconfigs.DBConfigs, schemaDir string, ensureDatabase bool) (uint32, error) {
tabletMap = make(map[uint32]*comboTablet)
ctx := context.Background()
// Register the tablet manager client factory for tablet manager
// Do this before any tablets are created so that they respect the protocol,
// otherwise it defaults to grpc
tmclient.RegisterTabletManagerClientFactory("internal", func() tmclient.TabletManagerClient {
return &internalTabletManagerClient{}
})
*tmclient.TabletManagerProtocol = "internal"
// iterate through the keyspaces
wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil)
var uid uint32 = 1
for _, kpb := range tpb.Keyspaces {
var err error
uid, err = CreateKs(ctx, ts, tpb, mysqld, dbcfgs, schemaDir, kpb, ensureDatabase, uid, wr)
if err != nil {
return 0, err
}
}
// Rebuild the SrvVSchema object
if err := ts.RebuildSrvVSchema(ctx, tpb.Cells); err != nil {
return 0, fmt.Errorf("RebuildVSchemaGraph failed: %v", err)
}
// Register the tablet dialer for tablet server
tabletconn.RegisterDialer("internal", dialer)
*tabletconn.TabletProtocol = "internal"
// run healthcheck on all vttablets
tmc := tmclient.NewTabletManagerClient()
for _, tablet := range tabletMap {
tabletInfo, err := ts.GetTablet(ctx, tablet.alias)
if err != nil {
return 0, fmt.Errorf("cannot find tablet: %+v", tablet.alias)
}
tmc.RunHealthCheck(ctx, tabletInfo.Tablet)
}
return uid, nil
}
// DeleteKs deletes keyspace, shards and tablets with mysql databases
func DeleteKs(ctx context.Context, ts *topo.Server, ksName string, mysqld mysqlctl.MysqlDaemon, tpb *vttestpb.VTTestTopology) error {
for key, tablet := range tabletMap {
if tablet.keyspace == ksName {
delete(tabletMap, key)
tablet.tm.Stop()
tablet.tm.Close()
tablet.qsc.SchemaEngine().Close()
err := ts.DeleteTablet(ctx, tablet.alias)
if err != nil {
return err
}
}
}
var ks *vttestpb.Keyspace
index := 0
for _, keyspace := range tpb.Keyspaces {
if keyspace.Name == ksName {
ks = keyspace
break
}
index++
}
if ks == nil {
return fmt.Errorf("database not found")
}
conn, err := mysqld.GetDbaConnection(ctx)
if err != nil {
return err
}
defer conn.Close()
for _, shard := range ks.Shards {
q := fmt.Sprintf("DROP DATABASE IF EXISTS `vt_%s_%s`", ksName, shard.GetName())
if _, err = conn.ExecuteFetch(q, 1, false); err != nil {
return err
}
if err := ts.DeleteShard(ctx, ksName, shard.GetName()); err != nil {
return err
}
}
if err = ts.DeleteKeyspace(ctx, ksName); err != nil {
return err
}
kss := tpb.Keyspaces // to save on chars
copy(kss[index:], kss[index+1:]) // shift keyspaces to the left, overwriting the value to remove
tpb.Keyspaces = kss[:len(kss)-1] // shrink the slice by one
return nil
}
// CreateKs creates keyspace, shards and tablets with mysql database
func CreateKs(ctx context.Context, ts *topo.Server, tpb *vttestpb.VTTestTopology, mysqld mysqlctl.MysqlDaemon, dbcfgs *dbconfigs.DBConfigs, schemaDir string, kpb *vttestpb.Keyspace, ensureDatabase bool, uid uint32, wr *wrangler.Wrangler) (uint32, error) {
keyspace := kpb.Name
// First parse the ShardingColumnType.
// Note if it's empty, we will return 'UNSET'.
sct, err := key.ParseKeyspaceIDType(kpb.ShardingColumnType)
if err != nil {
return 0, fmt.Errorf("parseKeyspaceIDType(%v) failed: %v", kpb.ShardingColumnType, err)
}
if kpb.ServedFrom != "" {
// if we have a redirect, create a completely redirected
// keyspace and no tablet
if err := ts.CreateKeyspace(ctx, keyspace, &topodatapb.Keyspace{
ShardingColumnName: kpb.ShardingColumnName,
ShardingColumnType: sct,
ServedFroms: []*topodatapb.Keyspace_ServedFrom{
{
TabletType: topodatapb.TabletType_MASTER,
Keyspace: kpb.ServedFrom,
},
{
TabletType: topodatapb.TabletType_REPLICA,
Keyspace: kpb.ServedFrom,
},
{
TabletType: topodatapb.TabletType_RDONLY,
Keyspace: kpb.ServedFrom,
},
},
}); err != nil {
return 0, fmt.Errorf("CreateKeyspace(%v) failed: %v", keyspace, err)
}
} else {
// create a regular keyspace
if err := ts.CreateKeyspace(ctx, keyspace, &topodatapb.Keyspace{
ShardingColumnName: kpb.ShardingColumnName,
ShardingColumnType: sct,
}); err != nil {
return 0, fmt.Errorf("CreateKeyspace(%v) failed: %v", keyspace, err)
}
// iterate through the shards
for _, spb := range kpb.Shards {
shard := spb.Name
ts.CreateShard(ctx, keyspace, shard)
if err != nil {
return 0, fmt.Errorf("CreateShard(%v:%v) failed: %v", keyspace, shard, err)
}
for _, cell := range tpb.Cells {
dbname := spb.DbNameOverride
if dbname == "" {
dbname = fmt.Sprintf("vt_%v_%v", keyspace, shard)
}
replicas := int(kpb.ReplicaCount)
if replicas == 0 {
// 2 replicas in order to ensure the master cell has a master and a replica
replicas = 2
}
rdonlys := int(kpb.RdonlyCount)
if rdonlys == 0 {
rdonlys = 1
}
if ensureDatabase {
// Create Database if not exist
conn, err := mysqld.GetDbaConnection(context.TODO())
if err != nil {
return 0, fmt.Errorf("GetConnection failed: %v", err)
}
defer conn.Close()
_, err = conn.ExecuteFetch("CREATE DATABASE IF NOT EXISTS `"+dbname+"`", 1, false)
if err != nil {
return 0, fmt.Errorf("error ensuring database exists: %v", err)
}
}
if cell == tpb.Cells[0] {
replicas--
// create the master
if err := CreateTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_MASTER, mysqld, dbcfgs.Clone()); err != nil {
return 0, err
}
uid++
}
for i := 0; i < replicas; i++ {
// create a replica tablet
if err := CreateTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs.Clone()); err != nil {
return 0, err
}
uid++
}
for i := 0; i < rdonlys; i++ {
// create a rdonly tablet
if err := CreateTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs.Clone()); err != nil {
return 0, err
}
uid++
}
}
}
}
// vschema for the keyspace
if schemaDir != "" {
f := path.Join(schemaDir, keyspace, "vschema.json")
if _, err := os.Stat(f); err == nil {
// load the vschema
formal, err := vindexes.LoadFormalKeyspace(f)
if err != nil {
return 0, fmt.Errorf("cannot load vschema file %v for keyspace %v: %v", f, keyspace, err)
}
if err := ts.SaveVSchema(ctx, keyspace, formal); err != nil {
return 0, fmt.Errorf("SaveVSchema(%v) failed: %v", keyspace, err)
}
} else {
log.Infof("File %v doesn't exist, skipping vschema for keyspace %v", f, keyspace)
}
}
// Rebuild the SrvKeyspace object, so we can support
// range-based sharding queries, and export the redirects.
if err := wr.RebuildKeyspaceGraph(ctx, keyspace, nil, false); err != nil {
return 0, fmt.Errorf("cannot rebuild %v: %v", keyspace, err)
}
return uid, nil
}
//
// TabletConn implementation
//
// dialer is our tabletconn.Dialer
func dialer(tablet *topodatapb.Tablet, failFast grpcclient.FailFast) (queryservice.QueryService, error) {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return nil, vterrors.New(vtrpcpb.Code_UNAVAILABLE, "connection refused")
}
return &internalTabletConn{
tablet: t,
topoTablet: tablet,
}, nil
}
// internalTabletConn implements queryservice.QueryService by forwarding everything
// to the tablet
type internalTabletConn struct {
tablet *comboTablet
topoTablet *topodatapb.Tablet
}
var _ queryservice.QueryService = (*internalTabletConn)(nil)
// Execute is part of queryservice.QueryService
// We need to copy the bind variables as tablet server will change them.
func (itc *internalTabletConn) Execute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, transactionID, reservedID int64, options *querypb.ExecuteOptions) (*sqltypes.Result, error) {
bindVars = sqltypes.CopyBindVariables(bindVars)
reply, err := itc.tablet.qsc.QueryService().Execute(ctx, target, query, bindVars, transactionID, reservedID, options)
if err != nil {
return nil, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
return reply, nil
}
// ExecuteBatch is part of queryservice.QueryService
// We need to copy the bind variables as tablet server will change them.
func (itc *internalTabletConn) ExecuteBatch(ctx context.Context, target *querypb.Target, queries []*querypb.BoundQuery, asTransaction bool, transactionID int64, options *querypb.ExecuteOptions) ([]sqltypes.Result, error) {
q := make([]*querypb.BoundQuery, len(queries))
for i, query := range queries {
q[i] = &querypb.BoundQuery{
Sql: query.Sql,
BindVariables: sqltypes.CopyBindVariables(query.BindVariables),
}
}
results, err := itc.tablet.qsc.QueryService().ExecuteBatch(ctx, target, q, asTransaction, transactionID, options)
if err != nil {
return nil, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
return results, nil
}
// StreamExecute is part of queryservice.QueryService
// We need to copy the bind variables as tablet server will change them.
func (itc *internalTabletConn) StreamExecute(ctx context.Context, target *querypb.Target, query string, bindVars map[string]*querypb.BindVariable, transactionID int64, options *querypb.ExecuteOptions, callback func(*sqltypes.Result) error) error {
bindVars = sqltypes.CopyBindVariables(bindVars)
err := itc.tablet.qsc.QueryService().StreamExecute(ctx, target, query, bindVars, transactionID, options, callback)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// Begin is part of queryservice.QueryService
func (itc *internalTabletConn) Begin(ctx context.Context, target *querypb.Target, options *querypb.ExecuteOptions) (int64, *topodatapb.TabletAlias, error) {
transactionID, alias, err := itc.tablet.qsc.QueryService().Begin(ctx, target, options)
if err != nil {
return 0, nil, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
return transactionID, alias, nil
}
// Commit is part of queryservice.QueryService
func (itc *internalTabletConn) Commit(ctx context.Context, target *querypb.Target, transactionID int64) (int64, error) {
rID, err := itc.tablet.qsc.QueryService().Commit(ctx, target, transactionID)
return rID, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// Rollback is part of queryservice.QueryService
func (itc *internalTabletConn) Rollback(ctx context.Context, target *querypb.Target, transactionID int64) (int64, error) {
rID, err := itc.tablet.qsc.QueryService().Rollback(ctx, target, transactionID)
return rID, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// Prepare is part of queryservice.QueryService
func (itc *internalTabletConn) Prepare(ctx context.Context, target *querypb.Target, transactionID int64, dtid string) error {
err := itc.tablet.qsc.QueryService().Prepare(ctx, target, transactionID, dtid)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// CommitPrepared is part of queryservice.QueryService
func (itc *internalTabletConn) CommitPrepared(ctx context.Context, target *querypb.Target, dtid string) error {
err := itc.tablet.qsc.QueryService().CommitPrepared(ctx, target, dtid)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// RollbackPrepared is part of queryservice.QueryService
func (itc *internalTabletConn) RollbackPrepared(ctx context.Context, target *querypb.Target, dtid string, originalID int64) error {
err := itc.tablet.qsc.QueryService().RollbackPrepared(ctx, target, dtid, originalID)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// CreateTransaction is part of queryservice.QueryService
func (itc *internalTabletConn) CreateTransaction(ctx context.Context, target *querypb.Target, dtid string, participants []*querypb.Target) error {
err := itc.tablet.qsc.QueryService().CreateTransaction(ctx, target, dtid, participants)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// StartCommit is part of queryservice.QueryService
func (itc *internalTabletConn) StartCommit(ctx context.Context, target *querypb.Target, transactionID int64, dtid string) error {
err := itc.tablet.qsc.QueryService().StartCommit(ctx, target, transactionID, dtid)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// SetRollback is part of queryservice.QueryService
func (itc *internalTabletConn) SetRollback(ctx context.Context, target *querypb.Target, dtid string, transactionID int64) error {
err := itc.tablet.qsc.QueryService().SetRollback(ctx, target, dtid, transactionID)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// ConcludeTransaction is part of queryservice.QueryService
func (itc *internalTabletConn) ConcludeTransaction(ctx context.Context, target *querypb.Target, dtid string) error {
err := itc.tablet.qsc.QueryService().ConcludeTransaction(ctx, target, dtid)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// ReadTransaction is part of queryservice.QueryService
func (itc *internalTabletConn) ReadTransaction(ctx context.Context, target *querypb.Target, dtid string) (metadata *querypb.TransactionMetadata, err error) {
metadata, err = itc.tablet.qsc.QueryService().ReadTransaction(ctx, target, dtid)
return metadata, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// BeginExecute is part of queryservice.QueryService
func (itc *internalTabletConn) BeginExecute(ctx context.Context, target *querypb.Target, preQueries []string, query string, bindVars map[string]*querypb.BindVariable, reserveID int64, options *querypb.ExecuteOptions) (*sqltypes.Result, int64, *topodatapb.TabletAlias, error) {
bindVars = sqltypes.CopyBindVariables(bindVars)
result, transactionID, tabletAlias, err := itc.tablet.qsc.QueryService().BeginExecute(ctx, target, preQueries, query, bindVars, reserveID, options)
return result, transactionID, tabletAlias, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// BeginExecuteBatch is part of queryservice.QueryService
func (itc *internalTabletConn) BeginExecuteBatch(ctx context.Context, target *querypb.Target, queries []*querypb.BoundQuery, asTransaction bool, options *querypb.ExecuteOptions) ([]sqltypes.Result, int64, *topodatapb.TabletAlias, error) {
transactionID, alias, err := itc.Begin(ctx, target, options)
if err != nil {
return nil, 0, nil, err
}
results, err := itc.ExecuteBatch(ctx, target, queries, asTransaction, transactionID, options)
return results, transactionID, alias, err
}
// MessageStream is part of queryservice.QueryService
func (itc *internalTabletConn) MessageStream(ctx context.Context, target *querypb.Target, name string, callback func(*sqltypes.Result) error) error {
err := itc.tablet.qsc.QueryService().MessageStream(ctx, target, name, callback)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// MessageAck is part of queryservice.QueryService
func (itc *internalTabletConn) MessageAck(ctx context.Context, target *querypb.Target, name string, ids []*querypb.Value) (int64, error) {
count, err := itc.tablet.qsc.QueryService().MessageAck(ctx, target, name, ids)
return count, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// Handle panic is part of the QueryService interface.
func (itc *internalTabletConn) HandlePanic(err *error) {
}
//ReserveBeginExecute is part of the QueryService interface.
func (itc *internalTabletConn) ReserveBeginExecute(ctx context.Context, target *querypb.Target, preQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, options *querypb.ExecuteOptions) (*sqltypes.Result, int64, int64, *topodatapb.TabletAlias, error) {
bindVariables = sqltypes.CopyBindVariables(bindVariables)
res, transactionID, reservedID, alias, err := itc.tablet.qsc.QueryService().ReserveBeginExecute(ctx, target, preQueries, sql, bindVariables, options)
return res, transactionID, reservedID, alias, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
//ReserveBeginExecute is part of the QueryService interface.
func (itc *internalTabletConn) ReserveExecute(ctx context.Context, target *querypb.Target, preQueries []string, sql string, bindVariables map[string]*querypb.BindVariable, transactionID int64, options *querypb.ExecuteOptions) (*sqltypes.Result, int64, *topodatapb.TabletAlias, error) {
bindVariables = sqltypes.CopyBindVariables(bindVariables)
res, reservedID, alias, err := itc.tablet.qsc.QueryService().ReserveExecute(ctx, target, preQueries, sql, bindVariables, transactionID, options)
return res, reservedID, alias, tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
//Release is part of the QueryService interface.
func (itc *internalTabletConn) Release(ctx context.Context, target *querypb.Target, transactionID, reservedID int64) error {
err := itc.tablet.qsc.QueryService().Release(ctx, target, transactionID, reservedID)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// Close is part of queryservice.QueryService
func (itc *internalTabletConn) Close(ctx context.Context) error {
return nil
}
// Tablet is part of queryservice.QueryService
func (itc *internalTabletConn) Tablet() *topodatapb.Tablet {
return itc.topoTablet
}
// StreamHealth is part of queryservice.QueryService
func (itc *internalTabletConn) StreamHealth(ctx context.Context, callback func(*querypb.StreamHealthResponse) error) error {
err := itc.tablet.qsc.QueryService().StreamHealth(ctx, callback)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// VStream is part of queryservice.QueryService.
func (itc *internalTabletConn) VStream(ctx context.Context, target *querypb.Target, startPos string, tableLastPKs []*binlogdatapb.TableLastPK, filter *binlogdatapb.Filter, send func([]*binlogdatapb.VEvent) error) error {
err := itc.tablet.qsc.QueryService().VStream(ctx, target, startPos, tableLastPKs, filter, send)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// VStreamRows is part of the QueryService interface.
func (itc *internalTabletConn) VStreamRows(ctx context.Context, target *querypb.Target, query string, lastpk *querypb.QueryResult, send func(*binlogdatapb.VStreamRowsResponse) error) error {
err := itc.tablet.qsc.QueryService().VStreamRows(ctx, target, query, lastpk, send)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
// VStreamResults is part of the QueryService interface.
func (itc *internalTabletConn) VStreamResults(ctx context.Context, target *querypb.Target, query string, send func(*binlogdatapb.VStreamResultsResponse) error) error {
err := itc.tablet.qsc.QueryService().VStreamResults(ctx, target, query, send)
return tabletconn.ErrorFromGRPC(vterrors.ToGRPC(err))
}
//
// TabletManagerClient implementation
//
// internalTabletManagerClient implements tmclient.TabletManagerClient
type internalTabletManagerClient struct{}
func (itmc *internalTabletManagerClient) LockTables(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) UnlockTables(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) Ping(ctx context.Context, tablet *topodatapb.Tablet) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
t.tm.Ping(ctx, "payload")
return nil
}
func (itmc *internalTabletManagerClient) GetSchema(ctx context.Context, tablet *topodatapb.Tablet, tables, excludeTables []string, includeViews bool) (*tabletmanagerdatapb.SchemaDefinition, error) {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return nil, fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
return t.tm.GetSchema(ctx, tables, excludeTables, includeViews)
}
func (itmc *internalTabletManagerClient) GetPermissions(ctx context.Context, tablet *topodatapb.Tablet) (*tabletmanagerdatapb.Permissions, error) {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return nil, fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
return t.tm.GetPermissions(ctx)
}
func (itmc *internalTabletManagerClient) SetReadOnly(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) SetReadWrite(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ChangeType(ctx context.Context, tablet *topodatapb.Tablet, dbType topodatapb.TabletType) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
t.tm.ChangeType(ctx, dbType)
return nil
}
func (itmc *internalTabletManagerClient) Sleep(ctx context.Context, tablet *topodatapb.Tablet, duration time.Duration) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
t.tm.Sleep(ctx, duration)
return nil
}
func (itmc *internalTabletManagerClient) ExecuteHook(ctx context.Context, tablet *topodatapb.Tablet, hk *hook.Hook) (*hook.HookResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) RefreshState(ctx context.Context, tablet *topodatapb.Tablet) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
return t.tm.RefreshState(ctx)
}
func (itmc *internalTabletManagerClient) RunHealthCheck(ctx context.Context, tablet *topodatapb.Tablet) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
t.tm.RunHealthCheck(ctx)
return nil
}
func (itmc *internalTabletManagerClient) IgnoreHealthError(ctx context.Context, tablet *topodatapb.Tablet, pattern string) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
t.tm.IgnoreHealthError(ctx, pattern)
return nil
}
func (itmc *internalTabletManagerClient) ReloadSchema(ctx context.Context, tablet *topodatapb.Tablet, waitPosition string) error {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
return t.tm.ReloadSchema(ctx, waitPosition)
}
func (itmc *internalTabletManagerClient) PreflightSchema(ctx context.Context, tablet *topodatapb.Tablet, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error) {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return nil, fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
return t.tm.PreflightSchema(ctx, changes)
}
func (itmc *internalTabletManagerClient) ApplySchema(ctx context.Context, tablet *topodatapb.Tablet, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error) {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return nil, fmt.Errorf("tmclient: cannot find tablet %v", tablet.Alias.Uid)
}
return t.tm.ApplySchema(ctx, change)
}
func (itmc *internalTabletManagerClient) ExecuteQuery(ctx context.Context, tablet *topodatapb.Tablet, query []byte, maxrows int) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, query []byte, maxRows int, disableBinlogs, reloadSchema bool) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ExecuteFetchAsAllPrivs(ctx context.Context, tablet *topodatapb.Tablet, query []byte, maxRows int, reloadSchema bool) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ExecuteFetchAsApp(ctx context.Context, tablet *topodatapb.Tablet, usePool bool, query []byte, maxRows int) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) MasterStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.MasterStatus, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) MasterPosition(ctx context.Context, tablet *topodatapb.Tablet) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) WaitForPosition(ctx context.Context, tablet *topodatapb.Tablet, pos string) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) VExec(ctx context.Context, tablet *topodatapb.Tablet, query, workflow, keyspace string) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) VReplicationExec(ctx context.Context, tablet *topodatapb.Tablet, query string) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) VReplicationWaitForPos(ctx context.Context, tablet *topodatapb.Tablet, id int, pos string) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ResetReplication(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) InitMaster(ctx context.Context, tablet *topodatapb.Tablet) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) PopulateReparentJournal(ctx context.Context, tablet *topodatapb.Tablet, timeCreatedNS int64, actionName string, masterAlias *topodatapb.TabletAlias, pos string) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) DemoteMaster(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.MasterStatus, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) UndoDemoteMaster(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) SetMaster(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias, timeCreatedNS int64, waitPosition string, forceStartSlave bool) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StopReplicationAndGetStatus(ctx context.Context, tablet *topodatapb.Tablet, stopReplicationMode replicationdatapb.StopReplicationMode) (*replicationdatapb.Status, *replicationdatapb.StopReplicationStatus, error) {
return nil, nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) PromoteReplica(ctx context.Context, tablet *topodatapb.Tablet) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) Backup(ctx context.Context, tablet *topodatapb.Tablet, concurrency int, allowMaster bool) (logutil.EventStream, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) RestoreFromBackup(ctx context.Context, tablet *topodatapb.Tablet) (logutil.EventStream, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) Close() {
}
func (itmc *internalTabletManagerClient) ReplicationStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.Status, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StopReplication(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StopReplicationMinimum(ctx context.Context, tablet *topodatapb.Tablet, stopPos string, waitTime time.Duration) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StartReplication(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StartReplicationUntilAfter(ctx context.Context, tablet *topodatapb.Tablet, position string, duration time.Duration) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) GetReplicas(ctx context.Context, tablet *topodatapb.Tablet) ([]string, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) InitReplica(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias, replicationPosition string, timeCreatedNS int64) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ReplicaWasPromoted(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ReplicaWasRestarted(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias) error {
return fmt.Errorf("not implemented in vtcombo")
}