forked from dolthub/vitess
/
split_clone.go
1231 lines (1099 loc) · 47.4 KB
/
split_clone.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package worker
import (
"errors"
"fmt"
"html/template"
"strings"
"sync"
"time"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/event"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/binlog/binlogplayer"
"github.com/youtube/vitess/go/vt/concurrency"
"github.com/youtube/vitess/go/vt/discovery"
"github.com/youtube/vitess/go/vt/throttler"
"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/vtgate/vindexes"
"github.com/youtube/vitess/go/vt/worker/events"
"github.com/youtube/vitess/go/vt/wrangler"
tabletmanagerdatapb "github.com/youtube/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// cloneType specifies whether it is a horizontal resharding or a vertical split.
// TODO(mberlin): Remove this once we merged both into one command.
type cloneType int
const (
horizontalResharding cloneType = iota
verticalSplit
)
// servingTypes is the list of tabletTypes which the source keyspace must be serving.
var servingTypes = []topodatapb.TabletType{topodatapb.TabletType_MASTER, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY}
// SplitCloneWorker will clone the data within a keyspace from a
// source set of shards to a destination set of shards.
type SplitCloneWorker struct {
StatusWorker
wr *wrangler.Wrangler
cloneType cloneType
cell string
destinationKeyspace string
shard string
online bool
offline bool
// verticalSplit only: List of tables which should be split out.
tables []string
// horizontalResharding only: List of tables which will be skipped.
excludeTables []string
strategy *splitStrategy
chunkCount int
minRowsPerChunk int
sourceReaderCount int
writeQueryMaxRows int
writeQueryMaxSize int
destinationWriterCount int
minHealthyRdonlyTablets int
maxTPS int64
maxReplicationLag int64
cleaner *wrangler.Cleaner
tabletTracker *TabletTracker
// populated during WorkerStateInit, read-only after that
destinationKeyspaceInfo *topo.KeyspaceInfo
sourceShards []*topo.ShardInfo
destinationShards []*topo.ShardInfo
keyspaceSchema *vindexes.KeyspaceSchema
// healthCheck is used for the destination shards to a) find out the current
// MASTER tablet, b) get the list of healthy RDONLY tablets and c) track the
// replication lag of all REPLICA tablets.
// It must be closed at the end of the command.
healthCheck discovery.HealthCheck
tsc *discovery.TabletStatsCache
// populated during WorkerStateFindTargets, read-only after that
sourceTablets []*topodatapb.Tablet
// shardWatchers contains a TopologyWatcher for each source and destination
// shard. It updates the list of tablets in the healthcheck if replicas are
// added/removed.
// Each watcher must be stopped at the end of the command.
shardWatchers []*discovery.TopologyWatcher
// destinationDbNames stores for each destination keyspace/shard the MySQL
// database name.
// Example Map Entry: test_keyspace/-80 => vt_test_keyspace
destinationDbNames map[string]string
// throttlersMu guards the fields within this group.
throttlersMu sync.Mutex
// throttlers has a throttler for each destination shard.
// Map key format: "keyspace/shard" e.g. "test_keyspace/-80"
// Throttlers will be added/removed during WorkerStateClone(Online|Offline).
throttlers map[string]*throttler.Throttler
// offlineSourceAliases has the list of tablets (per source shard) we took
// offline for the WorkerStateCloneOffline phase.
// Populated shortly before WorkerStateCloneOffline, read-only after that.
offlineSourceAliases []*topodatapb.TabletAlias
// formattedOfflineSourcesMu guards all fields in this group.
formattedOfflineSourcesMu sync.Mutex
// formattedOfflineSources is a space separated list of
// "offlineSourceAliases". It is used by the StatusAs* methods to output the
// used source tablets during the offline clone phase.
formattedOfflineSources string
// tableStatusList* holds the status for each table.
// populated during WorkerStateCloneOnline
tableStatusListOnline *tableStatusList
// populated during WorkerStateCloneOffline
tableStatusListOffline *tableStatusList
ev event.Updater
}
// newSplitCloneWorker returns a new worker object for the SplitClone command.
func newSplitCloneWorker(wr *wrangler.Wrangler, cell, keyspace, shard string, online, offline bool, excludeTables []string, strategyStr string, chunkCount, minRowsPerChunk, sourceReaderCount, writeQueryMaxRows, writeQueryMaxSize, destinationWriterCount, minHealthyRdonlyTablets int, maxTPS, maxReplicationLag int64) (Worker, error) {
return newCloneWorker(wr, horizontalResharding, cell, keyspace, shard, online, offline, nil /* tables */, excludeTables, strategyStr, chunkCount, minRowsPerChunk, sourceReaderCount, writeQueryMaxRows, writeQueryMaxSize, destinationWriterCount, minHealthyRdonlyTablets, maxTPS, maxReplicationLag)
}
// newVerticalSplitCloneWorker returns a new worker object for the
// VerticalSplitClone command.
func newVerticalSplitCloneWorker(wr *wrangler.Wrangler, cell, keyspace, shard string, online, offline bool, tables []string, strategyStr string, chunkCount, minRowsPerChunk, sourceReaderCount, writeQueryMaxRows, writeQueryMaxSize, destinationWriterCount, minHealthyRdonlyTablets int, maxTPS, maxReplicationLag int64) (Worker, error) {
return newCloneWorker(wr, verticalSplit, cell, keyspace, shard, online, offline, tables, nil /* excludeTables */, strategyStr, chunkCount, minRowsPerChunk, sourceReaderCount, writeQueryMaxRows, writeQueryMaxSize, destinationWriterCount, minHealthyRdonlyTablets, maxTPS, maxReplicationLag)
}
// newCloneWorker returns a new SplitCloneWorker object which is used both by
// the SplitClone and VerticalSplitClone command.
// TODO(mberlin): Rename SplitCloneWorker to cloneWorker.
func newCloneWorker(wr *wrangler.Wrangler, cloneType cloneType, cell, keyspace, shard string, online, offline bool, tables, excludeTables []string, strategyStr string, chunkCount, minRowsPerChunk, sourceReaderCount, writeQueryMaxRows, writeQueryMaxSize, destinationWriterCount, minHealthyRdonlyTablets int, maxTPS, maxReplicationLag int64) (Worker, error) {
if cloneType != horizontalResharding && cloneType != verticalSplit {
return nil, fmt.Errorf("unknown cloneType: %v This is a bug. Please report", cloneType)
}
// Verify user defined flags.
if !online && !offline {
return nil, errors.New("at least one clone phase (-online, -offline) must be enabled (and not set to false)")
}
if tables != nil && len(tables) == 0 {
return nil, errors.New("list of tablets to be split out must not be empty")
}
strategy, err := newSplitStrategy(wr.Logger(), strategyStr)
if err != nil {
return nil, err
}
if chunkCount <= 0 {
return nil, fmt.Errorf("chunk_count must be > 0: %v", chunkCount)
}
if minRowsPerChunk <= 0 {
return nil, fmt.Errorf("min_rows_per_chunk must be > 0: %v", minRowsPerChunk)
}
if sourceReaderCount <= 0 {
return nil, fmt.Errorf("source_reader_count must be > 0: %v", sourceReaderCount)
}
if writeQueryMaxRows <= 0 {
return nil, fmt.Errorf("write_query_max_rows must be > 0: %v", writeQueryMaxRows)
}
if writeQueryMaxSize <= 0 {
return nil, fmt.Errorf("write_query_max_size must be > 0: %v", writeQueryMaxSize)
}
if destinationWriterCount <= 0 {
return nil, fmt.Errorf("destination_writer_count must be > 0: %v", destinationWriterCount)
}
if minHealthyRdonlyTablets < 0 {
return nil, fmt.Errorf("min_healthy_rdonly_tablets must be >= 0: %v", minHealthyRdonlyTablets)
}
if maxTPS != throttler.MaxRateModuleDisabled {
wr.Logger().Infof("throttling enabled and set to a max of %v transactions/second", maxTPS)
}
if maxTPS != throttler.MaxRateModuleDisabled && maxTPS < int64(destinationWriterCount) {
return nil, fmt.Errorf("-max_tps must be >= -destination_writer_count: %v >= %v", maxTPS, destinationWriterCount)
}
if maxReplicationLag <= 0 {
return nil, fmt.Errorf("max_replication_lag must be >= 1s: %v", maxReplicationLag)
}
scw := &SplitCloneWorker{
StatusWorker: NewStatusWorker(),
wr: wr,
cloneType: cloneType,
cell: cell,
destinationKeyspace: keyspace,
shard: shard,
online: online,
offline: offline,
tables: tables,
excludeTables: excludeTables,
strategy: strategy,
chunkCount: chunkCount,
minRowsPerChunk: minRowsPerChunk,
sourceReaderCount: sourceReaderCount,
writeQueryMaxRows: writeQueryMaxRows,
writeQueryMaxSize: writeQueryMaxSize,
destinationWriterCount: destinationWriterCount,
minHealthyRdonlyTablets: minHealthyRdonlyTablets,
maxTPS: maxTPS,
maxReplicationLag: maxReplicationLag,
cleaner: &wrangler.Cleaner{},
tabletTracker: NewTabletTracker(),
throttlers: make(map[string]*throttler.Throttler),
destinationDbNames: make(map[string]string),
tableStatusListOnline: &tableStatusList{},
tableStatusListOffline: &tableStatusList{},
}
scw.initializeEventDescriptor()
return scw, nil
}
func (scw *SplitCloneWorker) initializeEventDescriptor() {
switch scw.cloneType {
case horizontalResharding:
scw.ev = &events.SplitClone{
Cell: scw.cell,
Keyspace: scw.destinationKeyspace,
Shard: scw.shard,
ExcludeTables: scw.excludeTables,
Strategy: scw.strategy.String(),
}
case verticalSplit:
scw.ev = &events.VerticalSplitClone{
Cell: scw.cell,
Keyspace: scw.destinationKeyspace,
Shard: scw.shard,
Tables: scw.tables,
Strategy: scw.strategy.String(),
}
}
}
func (scw *SplitCloneWorker) setState(state StatusWorkerState) {
scw.SetState(state)
event.DispatchUpdate(scw.ev, state.String())
}
func (scw *SplitCloneWorker) setErrorState(err error) {
scw.SetState(WorkerStateError)
event.DispatchUpdate(scw.ev, "error: "+err.Error())
}
func (scw *SplitCloneWorker) formatOnlineSources() string {
aliases := scw.tabletTracker.TabletsInUse()
if aliases == "" {
return "no online source tablets currently in use"
}
return aliases
}
func (scw *SplitCloneWorker) setFormattedOfflineSources(aliases []*topodatapb.TabletAlias) {
scw.formattedOfflineSourcesMu.Lock()
defer scw.formattedOfflineSourcesMu.Unlock()
var sources []string
for _, alias := range aliases {
sources = append(sources, topoproto.TabletAliasString(alias))
}
scw.formattedOfflineSources = strings.Join(sources, " ")
}
// FormattedOfflineSources returns a space separated list of tablets which
// are in use during the offline clone phase.
func (scw *SplitCloneWorker) FormattedOfflineSources() string {
scw.formattedOfflineSourcesMu.Lock()
defer scw.formattedOfflineSourcesMu.Unlock()
if scw.formattedOfflineSources == "" {
return "no offline source tablets currently in use"
}
return scw.formattedOfflineSources
}
// StatusAsHTML implements the Worker interface
func (scw *SplitCloneWorker) StatusAsHTML() template.HTML {
state := scw.State()
result := "<b>Working on:</b> " + scw.destinationKeyspace + "/" + scw.shard + "</br>\n"
result += "<b>State:</b> " + state.String() + "</br>\n"
switch state {
case WorkerStateCloneOnline:
result += "<b>Running:</b></br>\n"
result += "<b>Copying from:</b> " + scw.formatOnlineSources() + "</br>\n"
statuses, eta := scw.tableStatusListOnline.format()
result += "<b>ETA:</b> " + eta.String() + "</br>\n"
result += strings.Join(statuses, "</br>\n")
case WorkerStateCloneOffline:
result += "<b>Running:</b></br>\n"
result += "<b>Copying from:</b> " + scw.FormattedOfflineSources() + "</br>\n"
statuses, eta := scw.tableStatusListOffline.format()
result += "<b>ETA:</b> " + eta.String() + "</br>\n"
result += strings.Join(statuses, "</br>\n")
if scw.online {
result += "</br>\n"
result += "<b>Result from preceding Online Clone:</b></br>\n"
statuses, _ := scw.tableStatusListOnline.format()
result += strings.Join(statuses, "</br>\n")
}
case WorkerStateDone:
result += "<b>Success</b>:</br>\n"
if scw.online {
result += "</br>\n"
result += "<b>Online Clone Result:</b></br>\n"
statuses, _ := scw.tableStatusListOnline.format()
result += strings.Join(statuses, "</br>\n")
}
if scw.offline {
result += "</br>\n"
result += "<b>Offline Clone Result:</b></br>\n"
statuses, _ := scw.tableStatusListOffline.format()
result += strings.Join(statuses, "</br>\n")
}
}
if (state == WorkerStateCloneOnline || state == WorkerStateCloneOffline) && (scw.maxTPS != throttler.MaxRateModuleDisabled || scw.maxReplicationLag != throttler.ReplicationLagModuleDisabled) {
result += "</br>\n"
result += `<b>Resharding Throttler:</b> <a href="/throttlerz">see /throttlerz for details</a></br>`
}
return template.HTML(result)
}
// StatusAsText implements the Worker interface
func (scw *SplitCloneWorker) StatusAsText() string {
state := scw.State()
result := "Working on: " + scw.destinationKeyspace + "/" + scw.shard + "\n"
result += "State: " + state.String() + "\n"
switch state {
case WorkerStateCloneOnline:
result += "Running:\n"
result += "Copying from: " + scw.formatOnlineSources() + "\n"
statuses, eta := scw.tableStatusListOnline.format()
result += "ETA: " + eta.String() + "\n"
result += strings.Join(statuses, "\n")
case WorkerStateCloneOffline:
result += "Running:\n"
result += "Copying from: " + scw.FormattedOfflineSources() + "\n"
statuses, eta := scw.tableStatusListOffline.format()
result += "ETA: " + eta.String() + "\n"
result += strings.Join(statuses, "\n")
if scw.online {
result += "\n"
result += "\n"
result += "Result from preceding Online Clone:\n"
statuses, _ := scw.tableStatusListOnline.format()
result += strings.Join(statuses, "\n")
}
case WorkerStateDone:
result += "Success:"
if scw.online {
result += "\n"
result += "\n"
result += "Online Clone Result:\n"
statuses, _ := scw.tableStatusListOnline.format()
result += strings.Join(statuses, "\n")
}
if scw.offline {
result += "\n"
result += "\n"
result += "Offline Clone Result:\n"
statuses, _ := scw.tableStatusListOffline.format()
result += strings.Join(statuses, "\n")
}
}
return result
}
// Run implements the Worker interface
func (scw *SplitCloneWorker) Run(ctx context.Context) error {
resetVars()
// Run the command.
err := scw.run(ctx)
// Cleanup.
scw.setState(WorkerStateCleanUp)
// Reverse any changes e.g. setting the tablet type of a source RDONLY tablet.
cerr := scw.cleaner.CleanUp(scw.wr)
if cerr != nil {
if err != nil {
scw.wr.Logger().Errorf("CleanUp failed in addition to job error: %v", cerr)
} else {
err = cerr
}
}
// Stop watchers to prevent new tablets from getting added to the healthCheck.
for _, watcher := range scw.shardWatchers {
watcher.Stop()
}
// Stop healthCheck to make sure it stops calling our listener implementation.
if scw.healthCheck != nil {
// After Close returned, we can be sure that it won't call our listener
// implementation (method StatsUpdate) anymore.
if err := scw.healthCheck.Close(); err != nil {
scw.wr.Logger().Errorf("HealthCheck.Close() failed: %v", err)
}
}
if err != nil {
scw.setErrorState(err)
return err
}
scw.setState(WorkerStateDone)
return nil
}
func (scw *SplitCloneWorker) run(ctx context.Context) error {
// Phase 1: read what we need to do.
if err := scw.init(ctx); err != nil {
return fmt.Errorf("init() failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
// Phase 2a: Find destination master tablets.
if err := scw.findDestinationMasters(ctx); err != nil {
return fmt.Errorf("findDestinationMasters() failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
// Phase 2b: Wait for minimum number of destination tablets (required for the
// diff). Note that while we wait for the minimum number, we'll always use
// *all* available RDONLY tablets from each destination shard.
if err := scw.waitForTablets(ctx, scw.destinationShards, *waitForHealthyTabletsTimeout); err != nil {
return fmt.Errorf("waitForDestinationTablets(destinationShards) failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
// Phase 3: (optional) online clone.
if scw.online {
scw.wr.Logger().Infof("Online clone will be run now.")
// 3a: Wait for minimum number of source tablets (required for the diff).
if err := scw.waitForTablets(ctx, scw.sourceShards, *waitForHealthyTabletsTimeout); err != nil {
return fmt.Errorf("waitForDestinationTablets(sourceShards) failed: %v", err)
}
// 3b: Clone the data.
start := time.Now()
if err := scw.clone(ctx, WorkerStateCloneOnline); err != nil {
return fmt.Errorf("online clone() failed: %v", err)
}
d := time.Since(start)
if err := checkDone(ctx); err != nil {
return err
}
// TODO(mberlin): Output diff report of the online clone.
// Round duration to second granularity to make it more readable.
scw.wr.Logger().Infof("Online clone finished after %v.", time.Duration(d.Nanoseconds()/time.Second.Nanoseconds()*time.Second.Nanoseconds()))
} else {
scw.wr.Logger().Infof("Online clone skipped because --online=false was specified.")
}
// Phase 4: offline clone.
if scw.offline {
scw.wr.Logger().Infof("Offline clone will be run now.")
if scw.online {
// Wait until the inserts from the online clone were propagated
// from the destination master to the rdonly tablets.
// TODO(mberlin): Remove the sleep and get the destination master position
// instead and wait until all selected destination tablets have reached
// it.
time.Sleep(1 * time.Second)
}
// 4a: Take source tablets out of serving for an exact snapshot.
if err := scw.findOfflineSourceTablets(ctx); err != nil {
return fmt.Errorf("findSourceTablets() failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
// 4b: Clone the data.
start := time.Now()
if err := scw.clone(ctx, WorkerStateCloneOffline); err != nil {
return fmt.Errorf("offline clone() failed: %v", err)
}
d := time.Since(start)
if err := checkDone(ctx); err != nil {
return err
}
// TODO(mberlin): Output diff report of the offline clone.
// Round duration to second granularity to make it more readable.
scw.wr.Logger().Infof("Offline clone finished after %v.", time.Duration(d.Nanoseconds()/time.Second.Nanoseconds()*time.Second.Nanoseconds()))
} else {
scw.wr.Logger().Infof("Offline clone skipped because --offline=false was specified.")
}
return nil
}
// init phase:
// - read the destination keyspace, make sure it has 'servedFrom' values
func (scw *SplitCloneWorker) init(ctx context.Context) error {
scw.setState(WorkerStateInit)
// read the keyspace and validate it
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
var err error
scw.destinationKeyspaceInfo, err = scw.wr.TopoServer().GetKeyspace(shortCtx, scw.destinationKeyspace)
cancel()
if err != nil {
return fmt.Errorf("cannot read (destination) keyspace %v: %v", scw.destinationKeyspace, err)
}
// Set source and destination shard infos.
switch scw.cloneType {
case horizontalResharding:
if err := scw.initShardsForHorizontalResharding(ctx); err != nil {
return err
}
case verticalSplit:
if err := scw.initShardsForVerticalSplit(ctx); err != nil {
return err
}
}
if err := scw.sanityCheckShardInfos(); err != nil {
return err
}
if scw.cloneType == horizontalResharding {
if err := scw.loadVSchema(ctx); err != nil {
return err
}
}
// Initialize healthcheck and add destination shards to it.
scw.healthCheck = discovery.NewHealthCheck(*remoteActionsTimeout, *healthcheckRetryDelay, *healthCheckTimeout)
scw.tsc = discovery.NewTabletStatsCacheDoNotSetListener(scw.cell)
// We set sendDownEvents=true because it's required by TabletStatsCache.
scw.healthCheck.SetListener(scw, true /* sendDownEvents */)
// Start watchers to get tablets added automatically to healthCheck.
allShards := append(scw.sourceShards, scw.destinationShards...)
for _, si := range allShards {
watcher := discovery.NewShardReplicationWatcher(scw.wr.TopoServer(), scw.healthCheck,
scw.cell, si.Keyspace(), si.ShardName(),
*healthCheckTopologyRefresh, discovery.DefaultTopoReadConcurrency)
scw.shardWatchers = append(scw.shardWatchers, watcher)
}
return nil
}
func (scw *SplitCloneWorker) initShardsForHorizontalResharding(ctx context.Context) error {
// find the OverlappingShards in the keyspace
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
osList, err := topotools.FindOverlappingShards(shortCtx, scw.wr.TopoServer(), scw.destinationKeyspace)
cancel()
if err != nil {
return fmt.Errorf("cannot FindOverlappingShards in %v: %v", scw.destinationKeyspace, err)
}
// find the shard we mentioned in there, if any
os := topotools.OverlappingShardsForShard(osList, scw.shard)
if os == nil {
return fmt.Errorf("the specified shard %v/%v is not in any overlapping shard", scw.destinationKeyspace, scw.shard)
}
scw.wr.Logger().Infof("Found overlapping shards: %+v\n", os)
// one side should have served types, the other one none,
// figure out wich is which, then double check them all
if len(os.Left[0].ServedTypes) > 0 {
scw.sourceShards = os.Left
scw.destinationShards = os.Right
} else {
scw.sourceShards = os.Right
scw.destinationShards = os.Left
}
return nil
}
func (scw *SplitCloneWorker) initShardsForVerticalSplit(ctx context.Context) error {
if len(scw.destinationKeyspaceInfo.ServedFroms) == 0 {
return fmt.Errorf("destination keyspace %v has no KeyspaceServedFrom", scw.destinationKeyspace)
}
// Determine the source keyspace.
servedFrom := ""
for _, st := range servingTypes {
sf := scw.destinationKeyspaceInfo.GetServedFrom(st)
if sf == nil {
return fmt.Errorf("destination keyspace %v is serving type %v", scw.destinationKeyspace, st)
}
if servedFrom == "" {
servedFrom = sf.Keyspace
} else {
if servedFrom != sf.Keyspace {
return fmt.Errorf("destination keyspace %v is serving from multiple source keyspaces %v and %v", scw.destinationKeyspace, servedFrom, sf.Keyspace)
}
}
}
sourceKeyspace := servedFrom
// Init the source and destination shard info.
sourceShardInfo, err := scw.wr.TopoServer().GetShard(ctx, sourceKeyspace, scw.shard)
if err != nil {
return err
}
scw.sourceShards = []*topo.ShardInfo{sourceShardInfo}
destShardInfo, err := scw.wr.TopoServer().GetShard(ctx, scw.destinationKeyspace, scw.shard)
if err != nil {
return err
}
scw.destinationShards = []*topo.ShardInfo{destShardInfo}
return nil
}
func (scw *SplitCloneWorker) sanityCheckShardInfos() error {
// Verify that filtered replication is not already enabled.
for _, si := range scw.destinationShards {
if len(si.SourceShards) > 0 {
return fmt.Errorf("destination shard %v/%v has filtered replication already enabled from a previous resharding (ShardInfo is set)."+
" This requires manual intervention e.g. use vtctl SourceShardDelete to remove it",
si.Keyspace(), si.ShardName())
}
}
// Verify that the source is serving all serving types.
for _, st := range servingTypes {
for _, si := range scw.sourceShards {
if si.GetServedType(st) == nil {
return fmt.Errorf("source shard %v/%v is not serving type %v", si.Keyspace(), si.ShardName(), st)
}
}
}
switch scw.cloneType {
case horizontalResharding:
// Verify that the destination is not serving yet.
for _, si := range scw.destinationShards {
if len(si.ServedTypes) > 0 {
return fmt.Errorf("destination shard %v/%v is serving some types", si.Keyspace(), si.ShardName())
}
}
case verticalSplit:
// Verify that the destination is serving all types.
for _, st := range servingTypes {
for _, si := range scw.destinationShards {
if si.GetServedType(st) == nil {
return fmt.Errorf("source shard %v/%v is not serving type %v", si.Keyspace(), si.ShardName(), st)
}
}
}
}
return nil
}
func (scw *SplitCloneWorker) loadVSchema(ctx context.Context) error {
var keyspaceSchema *vindexes.KeyspaceSchema
if *useV3ReshardingMode {
kschema, err := scw.wr.TopoServer().GetVSchema(ctx, scw.destinationKeyspace)
if err != nil {
return fmt.Errorf("cannot load VSchema for keyspace %v: %v", scw.destinationKeyspace, err)
}
if kschema == nil {
return fmt.Errorf("no VSchema for keyspace %v", scw.destinationKeyspace)
}
keyspaceSchema, err = vindexes.BuildKeyspaceSchema(kschema, scw.destinationKeyspace)
if err != nil {
return fmt.Errorf("cannot build vschema for keyspace %v: %v", scw.destinationKeyspace, err)
}
scw.keyspaceSchema = keyspaceSchema
}
return nil
}
// findOfflineSourceTablets phase:
// - find one rdonly in the source shard
// - mark it as 'worker' pointing back to us
// - get the aliases of all the source tablets
func (scw *SplitCloneWorker) findOfflineSourceTablets(ctx context.Context) error {
scw.setState(WorkerStateFindTargets)
// find an appropriate tablet in the source shards
scw.offlineSourceAliases = make([]*topodatapb.TabletAlias, len(scw.sourceShards))
for i, si := range scw.sourceShards {
var err error
scw.offlineSourceAliases[i], err = FindWorkerTablet(ctx, scw.wr, scw.cleaner, scw.tsc, scw.cell, si.Keyspace(), si.ShardName(), scw.minHealthyRdonlyTablets)
if err != nil {
return fmt.Errorf("FindWorkerTablet() failed for %v/%v/%v: %v", scw.cell, si.Keyspace(), si.ShardName(), err)
}
scw.wr.Logger().Infof("Using tablet %v as source for %v/%v", topoproto.TabletAliasString(scw.offlineSourceAliases[i]), si.Keyspace(), si.ShardName())
}
scw.setFormattedOfflineSources(scw.offlineSourceAliases)
// get the tablet info for them, and stop their replication
scw.sourceTablets = make([]*topodatapb.Tablet, len(scw.offlineSourceAliases))
for i, alias := range scw.offlineSourceAliases {
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
ti, err := scw.wr.TopoServer().GetTablet(shortCtx, alias)
cancel()
if err != nil {
return fmt.Errorf("cannot read tablet %v: %v", topoproto.TabletAliasString(alias), err)
}
scw.sourceTablets[i] = ti.Tablet
shortCtx, cancel = context.WithTimeout(ctx, *remoteActionsTimeout)
err = scw.wr.TabletManagerClient().StopSlave(shortCtx, scw.sourceTablets[i])
cancel()
if err != nil {
return fmt.Errorf("cannot stop replication on tablet %v", topoproto.TabletAliasString(alias))
}
wrangler.RecordStartSlaveAction(scw.cleaner, scw.sourceTablets[i])
}
return nil
}
// findDestinationMasters finds for each destination shard the current master.
func (scw *SplitCloneWorker) findDestinationMasters(ctx context.Context) error {
scw.setState(WorkerStateFindTargets)
// Make sure we find a master for each destination shard and log it.
scw.wr.Logger().Infof("Finding a MASTER tablet for each destination shard...")
for _, si := range scw.destinationShards {
waitCtx, waitCancel := context.WithTimeout(ctx, *waitForHealthyTabletsTimeout)
defer waitCancel()
if err := scw.tsc.WaitForTablets(waitCtx, scw.cell, si.Keyspace(), si.ShardName(), []topodatapb.TabletType{topodatapb.TabletType_MASTER}); err != nil {
return fmt.Errorf("cannot find MASTER tablet for destination shard for %v/%v (in cell: %v): %v", si.Keyspace(), si.ShardName(), scw.cell, err)
}
masters := scw.tsc.GetHealthyTabletStats(si.Keyspace(), si.ShardName(), topodatapb.TabletType_MASTER)
if len(masters) == 0 {
return fmt.Errorf("cannot find MASTER tablet for destination shard for %v/%v (in cell: %v) in HealthCheck: empty TabletStats list", si.Keyspace(), si.ShardName(), scw.cell)
}
master := masters[0]
// Get the MySQL database name of the tablet.
keyspaceAndShard := topoproto.KeyspaceShardString(si.Keyspace(), si.ShardName())
scw.destinationDbNames[keyspaceAndShard] = topoproto.TabletDbName(master.Tablet)
// TODO(mberlin): Verify on the destination master that the
// _vt.blp_checkpoint table has the latest schema.
scw.wr.Logger().Infof("Using tablet %v as destination master for %v/%v", topoproto.TabletAliasString(master.Tablet.Alias), si.Keyspace(), si.ShardName())
}
scw.wr.Logger().Infof("NOTE: The used master of a destination shard might change over the course of the copy e.g. due to a reparent. The HealthCheck module will track and log master changes and any error message will always refer the actually used master address.")
return nil
}
// waitForTablets waits for enough serving tablets in the given
// shard (which can be used as input during the diff).
func (scw *SplitCloneWorker) waitForTablets(ctx context.Context, shardInfos []*topo.ShardInfo, timeout time.Duration) error {
var wg sync.WaitGroup
rec := concurrency.AllErrorRecorder{}
for _, si := range shardInfos {
wg.Add(1)
go func(keyspace, shard string) {
defer wg.Done()
// We wait for --min_healthy_rdonly_tablets because we will use several
// tablets per shard to spread reading the chunks of rows across as many
// tablets as possible.
if _, err := waitForHealthyRdonlyTablets(ctx, scw.wr, scw.tsc, scw.cell, keyspace, shard, scw.minHealthyRdonlyTablets, timeout); err != nil {
rec.RecordError(err)
}
}(si.Keyspace(), si.ShardName())
}
wg.Wait()
return rec.Error()
}
// copy phase:
// - copy the data from source tablets to destination masters (with replication on)
// Assumes that the schema has already been created on each destination tablet
// (probably from vtctl's CopySchemaShard)
func (scw *SplitCloneWorker) clone(ctx context.Context, state StatusWorkerState) error {
if state != WorkerStateCloneOnline && state != WorkerStateCloneOffline {
panic(fmt.Sprintf("invalid state passed to clone(): %v", state))
}
scw.setState(state)
start := time.Now()
defer func() {
statsStateDurationsNs.Set(string(state), time.Now().Sub(start).Nanoseconds())
}()
var firstSourceTablet *topodatapb.Tablet
if state == WorkerStateCloneOffline {
// Use the first source tablet which we took offline.
firstSourceTablet = scw.sourceTablets[0]
} else {
// Pick any healthy serving source tablet.
si := scw.sourceShards[0]
tablets := scw.tsc.GetHealthyTabletStats(si.Keyspace(), si.ShardName(), topodatapb.TabletType_RDONLY)
if len(tablets) == 0 {
// We fail fast on this problem and don't retry because at the start all tablets should be healthy.
return fmt.Errorf("no healthy RDONLY tablet in source shard (%v) available (required to find out the schema)", topoproto.KeyspaceShardString(si.Keyspace(), si.ShardName()))
}
firstSourceTablet = tablets[0].Tablet
}
var statsCounters []*stats.Counters
var tableStatusList *tableStatusList
switch state {
case WorkerStateCloneOnline:
statsCounters = []*stats.Counters{statsOnlineInsertsCounters, statsOnlineUpdatesCounters, statsOnlineDeletesCounters, statsOnlineEqualRowsCounters}
tableStatusList = scw.tableStatusListOnline
case WorkerStateCloneOffline:
statsCounters = []*stats.Counters{statsOfflineInsertsCounters, statsOfflineUpdatesCounters, statsOfflineDeletesCounters, statsOfflineEqualRowsCounters}
tableStatusList = scw.tableStatusListOffline
}
// The throttlers exist only for the duration of this clone() call.
// That means a SplitClone invocation with both online and offline phases
// will create throttlers for each phase.
if err := scw.createThrottlers(); err != nil {
return err
}
defer scw.closeThrottlers()
sourceSchemaDefinition, err := scw.getSourceSchema(ctx, firstSourceTablet)
if err != nil {
return err
}
scw.wr.Logger().Infof("Source tablet 0 has %v tables to copy", len(sourceSchemaDefinition.TableDefinitions))
tableStatusList.initialize(sourceSchemaDefinition)
// In parallel, setup the channels to send SQL data chunks to for each destination tablet:
//
// mu protects the context for cancelation, and firstError
mu := sync.Mutex{}
var firstError error
ctx, cancelCopy := context.WithCancel(ctx)
processError := func(format string, args ...interface{}) {
scw.wr.Logger().Errorf(format, args...)
mu.Lock()
if firstError == nil {
firstError = fmt.Errorf(format, args...)
cancelCopy()
}
mu.Unlock()
}
// NOTE: Code below this point must *not* use "return" to exit this Go routine
// early. Instead, "processError" must be called to cancel the context. This
// way all launched Go routines will terminate as well.
// (However, "return" in a new Go routine, i.e. not this one, is fine.)
// If Go routines have already been started, make sure that Wait() on the
// respective WaitGroup is called as well e.g. "destinationWaitGroup.Wait()"
// must always be reached. Waiting for the Go routines is important to avoid
// races between "defer throttler.ThreadFinished()" (must be executed first)
// and "defer scw.closeThrottlers()". Otherwise, vtworker will panic.
insertChannels := make([]chan string, len(scw.destinationShards))
destinationWaitGroup := sync.WaitGroup{}
for shardIndex, si := range scw.destinationShards {
// We create one channel per destination tablet. It is sized to have a
// buffer of a maximum of destinationWriterCount * 2 items, to hopefully
// always have data. We then have destinationWriterCount go routines reading
// from it.
insertChannels[shardIndex] = make(chan string, scw.destinationWriterCount*2)
for j := 0; j < scw.destinationWriterCount; j++ {
destinationWaitGroup.Add(1)
go func(keyspace, shard string, insertChannel chan string, throttler *throttler.Throttler, threadID int) {
defer destinationWaitGroup.Done()
defer throttler.ThreadFinished(threadID)
executor := newExecutor(scw.wr, scw.tsc, throttler, keyspace, shard, threadID)
if err := executor.fetchLoop(ctx, insertChannel); err != nil {
processError("executer.FetchLoop failed: %v", err)
}
}(si.Keyspace(), si.ShardName(), insertChannels[shardIndex], scw.getThrottler(si.Keyspace(), si.ShardName()), j)
}
}
// Now for each table, read data chunks and send them to all
// insertChannels
sourceWaitGroup := sync.WaitGroup{}
sema := sync2.NewSemaphore(scw.sourceReaderCount, 0)
for tableIndex, td := range sourceSchemaDefinition.TableDefinitions {
td = reorderColumnsPrimaryKeyFirst(td)
keyResolver, err := scw.createKeyResolver(td)
if err != nil {
processError("cannot resolve sharding keys for keyspace %v: %v", scw.destinationKeyspace, err)
break
}
// TODO(mberlin): We're going to chunk *all* source shards based on the MIN
// and MAX values of the *first* source shard. Is this going to be a problem?
chunks, err := generateChunks(ctx, scw.wr, firstSourceTablet, td, scw.chunkCount, scw.minRowsPerChunk)
if err != nil {
processError("failed to split table into chunks: %v", err)
break
}
tableStatusList.setThreadCount(tableIndex, len(chunks))
for _, c := range chunks {
sourceWaitGroup.Add(1)
go func(td *tabletmanagerdatapb.TableDefinition, tableIndex int, chunk chunk) {
defer sourceWaitGroup.Done()
errPrefix := fmt.Sprintf("table=%v chunk=%v", td.Name, chunk)
// We need our own error per Go routine to avoid races.
var err error
sema.Acquire()
defer sema.Release()
tableStatusList.threadStarted(tableIndex)
if state == WorkerStateCloneOnline {
// Wait for enough healthy tablets (they might have become unhealthy
// and their replication lag might have increased since we started.)
if err := scw.waitForTablets(ctx, scw.sourceShards, *retryDuration); err != nil {
processError("%v: No healthy source tablets found (gave up after %v): %v", errPrefix, *retryDuration, err)
return
}
}
// Set up readers for the diff. There will be one reader for every
// source and destination shard.
sourceReaders := make([]ResultReader, len(scw.sourceShards))
destReaders := make([]ResultReader, len(scw.destinationShards))
for shardIndex, si := range scw.sourceShards {
var tp tabletProvider
allowMultipleRetries := true
if state == WorkerStateCloneOffline {
tp = newSingleTabletProvider(ctx, scw.wr.TopoServer(), scw.offlineSourceAliases[shardIndex])
// allowMultipleRetries is false to avoid that we'll keep retrying
// on the same tablet alias for hours. This guards us against the
// situation that an offline tablet gets restarted and serves again.
// In that case we cannot use it because its replication is no
// longer stopped at the same point as we took it offline initially.
allowMultipleRetries = false
} else {
tp = newShardTabletProvider(scw.tsc, scw.tabletTracker, si.Keyspace(), si.ShardName())
}
sourceResultReader, err := NewRestartableResultReader(ctx, scw.wr.Logger(), tp, td, chunk, allowMultipleRetries)
if err != nil {
processError("%v: NewRestartableResultReader for source: %v failed", errPrefix, tp.description())
return
}
defer sourceResultReader.Close(ctx)
sourceReaders[shardIndex] = sourceResultReader
}
// Wait for enough healthy tablets (they might have become unhealthy
// and their replication lag might have increased due to a previous
// chunk pipeline.)
if err := scw.waitForTablets(ctx, scw.destinationShards, *retryDuration); err != nil {
processError("%v: No healthy destination tablets found (gave up after %v): ", errPrefix, *retryDuration, err)
return
}
for shardIndex, si := range scw.destinationShards {
tp := newShardTabletProvider(scw.tsc, scw.tabletTracker, si.Keyspace(), si.ShardName())
destResultReader, err := NewRestartableResultReader(ctx, scw.wr.Logger(), tp, td, chunk, true /* allowMultipleRetries */)
if err != nil {
processError("%v: NewRestartableResultReader for destination: %v failed: %v", errPrefix, tp.description(), err)
return
}
defer destResultReader.Close(ctx)
destReaders[shardIndex] = destResultReader
}
var sourceReader ResultReader
var destReader ResultReader
if len(sourceReaders) >= 2 {
sourceReader, err = NewResultMerger(sourceReaders, len(td.PrimaryKeyColumns))
if err != nil {
processError("%v: NewResultMerger for source tablets failed: %v", errPrefix, err)
return
}
} else {
sourceReader = sourceReaders[0]
}
if len(destReaders) >= 2 {
destReader, err = NewResultMerger(destReaders, len(td.PrimaryKeyColumns))
if err != nil {
processError("%v: NewResultMerger for destination tablets failed: %v", errPrefix, err)
return
}
} else {
destReader = destReaders[0]
}
dbNames := make([]string, len(scw.destinationShards))