forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tablet_map.go
857 lines (743 loc) · 29.4 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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
package main
import (
"flag"
"fmt"
"io"
"strings"
"time"
log "github.com/golang/glog"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/hook"
"github.com/youtube/vitess/go/vt/key"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/mysqlctl/tmutils"
"github.com/youtube/vitess/go/vt/tabletmanager"
"github.com/youtube/vitess/go/vt/tabletmanager/tmclient"
"github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/tabletserver/querytypes"
"github.com/youtube/vitess/go/vt/tabletserver/tabletconn"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"github.com/youtube/vitess/go/vt/topotools"
"github.com/youtube/vitess/go/vt/wrangler"
querypb "github.com/youtube/vitess/go/vt/proto/query"
replicationdatapb "github.com/youtube/vitess/go/vt/proto/replicationdata"
tabletmanagerdatapb "github.com/youtube/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vschemapb "github.com/youtube/vitess/go/vt/proto/vschema"
vttestpb "github.com/youtube/vitess/go/vt/proto/vttest"
)
// tablet contains all the data for an individual tablet.
type tablet struct {
// configuration parameters
keyspace string
shard string
tabletType topodatapb.TabletType
dbname string
// objects built at construction time
qsc tabletserver.Controller
agent *tabletmanager.ActionAgent
}
// tabletMap maps the tablet uid to the tablet record
var tabletMap map[uint32]*tablet
// createTablet creates an individual tablet, with its agent, 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()
initTabletType := tabletType
if tabletType == topodatapb.TabletType_MASTER {
initTabletType = topodatapb.TabletType_REPLICA
}
agent := tabletmanager.NewComboActionAgent(ctx, ts, alias, int32(8000+uid), int32(9000+uid), controller, dbcfgs, mysqld, keyspace, shard, dbname, strings.ToLower(initTabletType.String()))
if tabletType == topodatapb.TabletType_MASTER {
if err := agent.TabletExternallyReparented(ctx, ""); err != nil {
return fmt.Errorf("TabletExternallyReparented failed on master %v: %v", topoproto.TabletAliasString(alias), err)
}
}
tabletMap[uid] = &tablet{
keyspace: keyspace,
shard: shard,
tabletType: tabletType,
dbname: dbname,
qsc: controller,
agent: agent,
}
return nil
}
// initTabletMap creates the action agents and associated data structures
// for all tablets
func initTabletMap(ts topo.Server, topology string, mysqld mysqlctl.MysqlDaemon, dbcfgs dbconfigs.DBConfigs, formal *vschemapb.SrvVSchema, mycnf *mysqlctl.Mycnf) error {
tabletMap = make(map[uint32]*tablet)
ctx := context.Background()
// disable publishing of stats from query service
flag.Set("queryserver-config-enable-publish-stats", "false")
var uid uint32 = 1
keyspaceMap := make(map[string]bool)
for _, entry := range strings.Split(topology, ",") {
slash := strings.IndexByte(entry, '/')
column := strings.IndexByte(entry, ':')
if slash == -1 || column == -1 {
return fmt.Errorf("invalid topology entry: %v", entry)
}
keyspace := entry[:slash]
shard := entry[slash+1 : column]
dbname := entry[column+1:]
dbcfgs.App.DbName = dbname
// create the keyspace if necessary, so we can set the
// ShardingColumnName and ShardingColumnType
if _, ok := keyspaceMap[keyspace]; !ok {
// only set for sharding key info for sharded keyspaces
scn := ""
sct := topodatapb.KeyspaceIdType_UNSET
if shard != "0" {
var err error
sct, err = key.ParseKeyspaceIDType(*shardingColumnType)
if err != nil {
return fmt.Errorf("parseKeyspaceIDType(%v) failed: %v", *shardingColumnType, err)
}
scn = *shardingColumnName
}
if err := ts.CreateKeyspace(ctx, keyspace, &topodatapb.Keyspace{
ShardingColumnName: scn,
ShardingColumnType: sct,
}); err != nil {
return fmt.Errorf("CreateKeyspace(%v) failed: %v", keyspace, err)
}
keyspaceMap[keyspace] = true
vs := formal.Keyspaces[keyspace]
if err := ts.SaveVSchema(ctx, keyspace, vs); err != nil {
return fmt.Errorf("SaveVSchema failed: %v", err)
}
}
// create the master
if err := createTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_MASTER, mysqld, dbcfgs); err != nil {
return err
}
uid++
// create a replica slave
if err := createTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs); err != nil {
return err
}
uid++
// create a rdonly slave
if err := createTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs); err != nil {
return err
}
uid++
}
// Rebuild the SrvKeyspace objects, so we can support range-based
// sharding queries.
wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil)
for keyspace := range keyspaceMap {
if err := wr.RebuildKeyspaceGraph(ctx, keyspace, nil); err != nil {
return fmt.Errorf("cannot rebuild %v: %v", keyspace, err)
}
}
// Register the tablet dialer for tablet server
tabletconn.RegisterDialer("internal", dialer)
*tabletconn.TabletProtocol = "internal"
// Register the tablet manager client factory for tablet manager
tmclient.RegisterTabletManagerClientFactory("internal", func() tmclient.TabletManagerClient {
return &internalTabletManagerClient{}
})
*tmclient.TabletManagerProtocol = "internal"
// run healthcheck on all vttablets
tmc := tmclient.NewTabletManagerClient()
for _, tablet := range tabletMap {
tabletInfo, err := ts.GetTablet(ctx, tablet.agent.TabletAlias)
if err != nil {
return fmt.Errorf("cannot find tablet: %+v", tablet.agent.TabletAlias)
}
tmc.RunHealthCheck(ctx, tabletInfo.Tablet)
}
return nil
}
// initTabletMapProto creates the action agents and associated data structures
// for all tablets, based on the vttest proto parameter.
func initTabletMapProto(ts topo.Server, topoProto string, mysqld mysqlctl.MysqlDaemon, dbcfgs dbconfigs.DBConfigs, formal *vschemapb.SrvVSchema, mycnf *mysqlctl.Mycnf) error {
// parse the input topology
tpb := &vttestpb.VTTestTopology{}
if err := proto.UnmarshalText(topoProto, tpb); err != nil {
return fmt.Errorf("cannot parse topology: %v", err)
}
tabletMap = make(map[uint32]*tablet)
ctx := context.Background()
// disable publishing of stats from query service
flag.Set("queryserver-config-enable-publish-stats", "false")
// iterate through the keyspaces
wr := wrangler.New(logutil.NewConsoleLogger(), ts, nil)
var uid uint32 = 1
for _, kpb := range tpb.Keyspaces {
keyspace := kpb.Name
vs := formal.Keyspaces[keyspace]
// First parse the ShardingColumnType.
// Note if it's empty, we will return 'UNSET'.
sct, err := key.ParseKeyspaceIDType(kpb.ShardingColumnType)
if err != nil {
return 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 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 fmt.Errorf("CreateKeyspace(%v) failed: %v", keyspace, err)
}
// iterate through the shards
for _, spb := range kpb.Shards {
shard := spb.Name
dbname := spb.DbNameOverride
if dbname == "" {
dbname = fmt.Sprintf("vt_%v_%v", keyspace, shard)
}
dbcfgs.App.DbName = dbname
// create the master
if err := createTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_MASTER, mysqld, dbcfgs); err != nil {
return err
}
uid++
// create a replica slave
if err := createTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_REPLICA, mysqld, dbcfgs); err != nil {
return err
}
uid++
// create a rdonly slave
if err := createTablet(ctx, ts, cell, uid, keyspace, shard, dbname, topodatapb.TabletType_RDONLY, mysqld, dbcfgs); err != nil {
return err
}
uid++
}
}
// create the vschema
if err := ts.SaveVSchema(ctx, keyspace, vs); err != nil {
return fmt.Errorf("SaveVSchema failed: %v", err)
}
// Rebuild the SrvKeyspace object, so we can support
// range-based sharding queries, and export the redirects.
if err := wr.RebuildKeyspaceGraph(ctx, keyspace, nil); err != nil {
return fmt.Errorf("cannot rebuild %v: %v", keyspace, err)
}
}
// Rebuild the SrvVSchema object
if err := topotools.RebuildVSchema(ctx, wr.Logger(), ts, []string{cell}); err != nil {
return fmt.Errorf("RebuildVSchemaGraph failed: %v", err)
}
// Register the tablet dialer for tablet server
tabletconn.RegisterDialer("internal", dialer)
*tabletconn.TabletProtocol = "internal"
// Register the tablet manager client factory for tablet manager
tmclient.RegisterTabletManagerClientFactory("internal", func() tmclient.TabletManagerClient {
return &internalTabletManagerClient{}
})
*tmclient.TabletManagerProtocol = "internal"
// run healthcheck on all vttablets
tmc := tmclient.NewTabletManagerClient()
for _, tablet := range tabletMap {
tabletInfo, err := ts.GetTablet(ctx, tablet.agent.TabletAlias)
if err != nil {
return fmt.Errorf("cannot find tablet: %+v", tablet.agent.TabletAlias)
}
tmc.RunHealthCheck(ctx, tabletInfo.Tablet)
}
return nil
}
//
// TabletConn implementation
//
// dialer is our tabletconn.Dialer
func dialer(ctx context.Context, tablet *topodatapb.Tablet, keyspace, shard string, tabletType topodatapb.TabletType, timeout time.Duration) (tabletconn.TabletConn, error) {
t, ok := tabletMap[tablet.Alias.Uid]
if !ok {
return nil, tabletconn.OperationalError("connection refused")
}
return &internalTabletConn{
tablet: t,
topoTablet: tablet,
}, nil
}
// internalTabletConn implements tabletconn.TabletConn by forwarding everything
// to the tablet
type internalTabletConn struct {
tablet *tablet
topoTablet *topodatapb.Tablet
}
// Execute is part of tabletconn.TabletConn
// We need to copy the bind variables as tablet server will change them.
func (itc *internalTabletConn) Execute(ctx context.Context, query string, bindVars map[string]interface{}, transactionID int64) (*sqltypes.Result, error) {
bv, err := querytypes.BindVariablesToProto3(bindVars)
if err != nil {
return nil, err
}
bindVars, err = querytypes.Proto3ToBindVariables(bv)
if err != nil {
return nil, err
}
reply, err := itc.tablet.qsc.QueryService().Execute(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
}, query, bindVars, transactionID)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
return reply, nil
}
// ExecuteBatch is part of tabletconn.TabletConn
// We need to copy the bind variables as tablet server will change them.
func (itc *internalTabletConn) ExecuteBatch(ctx context.Context, queries []querytypes.BoundQuery, asTransaction bool, transactionID int64) ([]sqltypes.Result, error) {
q := make([]querytypes.BoundQuery, len(queries))
for i, query := range queries {
bv, err := querytypes.BindVariablesToProto3(query.BindVariables)
if err != nil {
return nil, err
}
bindVars, err := querytypes.Proto3ToBindVariables(bv)
if err != nil {
return nil, err
}
q[i].Sql = query.Sql
q[i].BindVariables = bindVars
}
results, err := itc.tablet.qsc.QueryService().ExecuteBatch(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
}, q, asTransaction, transactionID)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
return results, nil
}
type streamExecuteAdapter struct {
c chan *sqltypes.Result
err *error
}
func (a *streamExecuteAdapter) Recv() (*sqltypes.Result, error) {
r, ok := <-a.c
if !ok {
if *a.err == nil {
return nil, io.EOF
}
return nil, *a.err
}
return r, nil
}
// StreamExecute is part of tabletconn.TabletConn
// We need to copy the bind variables as tablet server will change them.
func (itc *internalTabletConn) StreamExecute(ctx context.Context, query string, bindVars map[string]interface{}) (sqltypes.ResultStream, error) {
bv, err := querytypes.BindVariablesToProto3(bindVars)
if err != nil {
return nil, err
}
bindVars, err = querytypes.Proto3ToBindVariables(bv)
if err != nil {
return nil, err
}
result := make(chan *sqltypes.Result, 10)
var finalErr error
go func() {
finalErr = itc.tablet.qsc.QueryService().StreamExecute(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
}, query, bindVars, func(reply *sqltypes.Result) error {
// We need to deep-copy the reply before returning,
// because the underlying buffers are reused.
result <- reply.Copy()
return nil
})
// the client will only access finalErr after the
// channel is closed, and then it's already set.
close(result)
}()
return &streamExecuteAdapter{result, &finalErr}, nil
}
// Begin is part of tabletconn.TabletConn
func (itc *internalTabletConn) Begin(ctx context.Context) (int64, error) {
transactionID, err := itc.tablet.qsc.QueryService().Begin(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
})
if err != nil {
return 0, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
return transactionID, nil
}
// Commit is part of tabletconn.TabletConn
func (itc *internalTabletConn) Commit(ctx context.Context, transactionID int64) error {
err := itc.tablet.qsc.QueryService().Commit(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
}, transactionID)
return tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
// Rollback is part of tabletconn.TabletConn
func (itc *internalTabletConn) Rollback(ctx context.Context, transactionID int64) error {
err := itc.tablet.qsc.QueryService().Rollback(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
}, transactionID)
return tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
// BeginExecute is part of tabletconn.TabletConn
func (itc *internalTabletConn) BeginExecute(ctx context.Context, query string, bindVars map[string]interface{}) (*sqltypes.Result, int64, error) {
transactionID, err := itc.Begin(ctx)
if err != nil {
return nil, 0, err
}
result, err := itc.Execute(ctx, query, bindVars, transactionID)
return result, transactionID, err
}
// BeginExecuteBatch is part of tabletconn.TabletConn
func (itc *internalTabletConn) BeginExecuteBatch(ctx context.Context, queries []querytypes.BoundQuery, asTransaction bool) ([]sqltypes.Result, int64, error) {
transactionID, err := itc.Begin(ctx)
if err != nil {
return nil, 0, err
}
results, err := itc.ExecuteBatch(ctx, queries, asTransaction, transactionID)
return results, transactionID, err
}
// Close is part of tabletconn.TabletConn
func (itc *internalTabletConn) Close() {
}
// SetTarget is part of tabletconn.TabletConn
func (itc *internalTabletConn) SetTarget(keyspace, shard string, tabletType topodatapb.TabletType) error {
return nil
}
// Tablet is part of tabletconn.TabletConn
func (itc *internalTabletConn) Tablet() *topodatapb.Tablet {
return itc.topoTablet
}
// SplitQuery is part of tabletconn.TabletConn
func (itc *internalTabletConn) SplitQuery(ctx context.Context, query querytypes.BoundQuery, splitColumn string, splitCount int64) ([]querytypes.QuerySplit, error) {
splits, err := itc.tablet.qsc.QueryService().SplitQuery(ctx, &querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
}, query.Sql, query.BindVariables, splitColumn, splitCount)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
return splits, nil
}
// SplitQueryV2 is part of tabletconn.TabletConn
// TODO(erez): Rename to SplitQuery once the migration to SplitQuery V2 is done.
func (itc *internalTabletConn) SplitQueryV2(
ctx context.Context,
query querytypes.BoundQuery,
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]querytypes.QuerySplit, error) {
splits, err := itc.tablet.qsc.QueryService().SplitQueryV2(
ctx,
&querypb.Target{
Keyspace: itc.tablet.keyspace,
Shard: itc.tablet.shard,
TabletType: itc.tablet.tabletType,
},
query.Sql,
query.BindVariables,
splitColumns,
splitCount,
numRowsPerQueryPart,
algorithm)
if err != nil {
return nil, tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(err))
}
return splits, nil
}
type streamHealthReader struct {
c <-chan *querypb.StreamHealthResponse
err *error
}
// Recv implements tabletconn.StreamHealthReader.
// It returns one response from the chan.
func (r *streamHealthReader) Recv() (*querypb.StreamHealthResponse, error) {
resp, ok := <-r.c
if !ok {
return nil, *r.err
}
return resp, nil
}
// StreamHealth is part of tabletconn.TabletConn
func (itc *internalTabletConn) StreamHealth(ctx context.Context) (tabletconn.StreamHealthReader, error) {
result := make(chan *querypb.StreamHealthResponse, 10)
id, err := itc.tablet.qsc.QueryService().StreamHealthRegister(result)
if err != nil {
return nil, err
}
var finalErr error
go func() {
select {
case <-ctx.Done():
}
// We populate finalErr before closing the channel.
// The consumer first waits on the channel closure,
// then read finalErr
finalErr = itc.tablet.qsc.QueryService().StreamHealthUnregister(id)
finalErr = tabletconn.TabletErrorFromGRPC(tabletserver.ToGRPCError(finalErr))
close(result)
}()
return &streamHealthReader{
c: result,
err: &finalErr,
}, nil
}
//
// TabletManagerClient implementation
//
// internalTabletManagerClient implements tmclient.TabletManagerClient
type internalTabletManagerClient struct{}
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)
}
return t.agent.RPCWrap(ctx, tabletmanager.TabletActionPing, nil, nil, func() error {
t.agent.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)
}
var result *tabletmanagerdatapb.SchemaDefinition
if err := t.agent.RPCWrap(ctx, tabletmanager.TabletActionGetSchema, nil, nil, func() error {
sd, err := t.agent.GetSchema(ctx, tables, excludeTables, includeViews)
if err == nil {
result = sd
}
return err
}); err != nil {
return nil, err
}
return result, nil
}
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)
}
var result *tabletmanagerdatapb.Permissions
if err := t.agent.RPCWrap(ctx, tabletmanager.TabletActionGetPermissions, nil, nil, func() error {
p, err := t.agent.GetPermissions(ctx)
if err == nil {
result = p
}
return err
}); err != nil {
return nil, err
}
return result, nil
}
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 {
return fmt.Errorf("not implemented in vtcombo")
}
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)
}
return t.agent.RPCWrapLockAction(ctx, tabletmanager.TabletActionSleep, nil, nil, true, func() error {
t.agent.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.agent.RPCWrapLockAction(ctx, tabletmanager.TabletActionRefreshState, nil, nil, true, func() error {
t.agent.RefreshState(ctx)
return nil
})
}
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)
}
return t.agent.RPCWrap(ctx, tabletmanager.TabletActionRunHealthCheck, nil, nil, func() error {
t.agent.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)
}
return t.agent.RPCWrap(ctx, tabletmanager.TabletActionIgnoreHealthError, nil, nil, func() error {
t.agent.IgnoreHealthError(ctx, pattern)
return nil
})
}
func (itmc *internalTabletManagerClient) ReloadSchema(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.agent.RPCWrapLockAction(ctx, tabletmanager.TabletActionReloadSchema, nil, nil, true, func() error {
t.agent.ReloadSchema(ctx)
return nil
})
}
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)
}
var results []*tabletmanagerdatapb.SchemaChangeResult
if err := t.agent.RPCWrapLockAction(ctx, tabletmanager.TabletActionPreflightSchema, nil, nil, true, func() error {
r, err := t.agent.PreflightSchema(ctx, changes)
if err == nil {
results = r
}
return err
}); err != nil {
return nil, err
}
return results, nil
}
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)
}
var result *tabletmanagerdatapb.SchemaChangeResult
if err := t.agent.RPCWrapLockAction(ctx, tabletmanager.TabletActionApplySchema, nil, nil, true, func() error {
scr, err := t.agent.ApplySchema(ctx, change)
if err == nil {
result = scr
}
return err
}); err != nil {
return nil, err
}
return result, nil
}
func (itmc *internalTabletManagerClient) ExecuteFetchAsDba(ctx context.Context, tablet *topodatapb.Tablet, query string, maxRows int, disableBinlogs, reloadSchema bool) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) ExecuteFetchAsApp(ctx context.Context, tablet *topodatapb.Tablet, query string, maxRows int) (*querypb.QueryResult, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) SlaveStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.Status, 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) StopSlave(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StopSlaveMinimum(ctx context.Context, tablet *topodatapb.Tablet, stopPos string, waitTime time.Duration) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StartSlave(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) TabletExternallyReparented(ctx context.Context, tablet *topodatapb.Tablet, externalID string) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) GetSlaves(ctx context.Context, tablet *topodatapb.Tablet) ([]string, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) WaitBlpPosition(ctx context.Context, tablet *topodatapb.Tablet, blpPosition *tabletmanagerdatapb.BlpPosition, waitTime time.Duration) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StopBlp(ctx context.Context, tablet *topodatapb.Tablet) ([]*tabletmanagerdatapb.BlpPosition, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StartBlp(ctx context.Context, tablet *topodatapb.Tablet) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) RunBlpUntil(ctx context.Context, tablet *topodatapb.Tablet, positions []*tabletmanagerdatapb.BlpPosition, waitTime time.Duration) (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) InitSlave(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias, replicationPosition string, timeCreatedNS int64) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) DemoteMaster(ctx context.Context, tablet *topodatapb.Tablet) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) PromoteSlaveWhenCaughtUp(ctx context.Context, tablet *topodatapb.Tablet, pos string) (string, error) {
return "", fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) SlaveWasPromoted(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, forceStartSlave bool) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) SlaveWasRestarted(ctx context.Context, tablet *topodatapb.Tablet, parent *topodatapb.TabletAlias) error {
return fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) StopReplicationAndGetStatus(ctx context.Context, tablet *topodatapb.Tablet) (*replicationdatapb.Status, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}
func (itmc *internalTabletManagerClient) PromoteSlave(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) (logutil.EventStream, error) {
return nil, fmt.Errorf("not implemented in vtcombo")
}