forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vertical_split_clone.go
588 lines (518 loc) · 19.6 KB
/
vertical_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
// 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"
"io"
"strings"
"sync"
"time"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/event"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/binlog/binlogplayer"
"github.com/youtube/vitess/go/vt/mysqlctl/tmutils"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/topo/topoproto"
"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"
)
// VerticalSplitCloneWorker will clone the data from a source keyspace/shard
// to a destination keyspace/shard.
type VerticalSplitCloneWorker struct {
StatusWorker
wr *wrangler.Wrangler
cell string
destinationKeyspace string
destinationShard string
tables []string
strategy *splitStrategy
sourceReaderCount int
destinationPackCount int
minTableSizeForSplit uint64
destinationWriterCount int
minHealthyRdonlyEndPoints int
cleaner *wrangler.Cleaner
// all subsequent fields are protected by the StatusWorker mutex
// populated during WorkerStateInit, read-only after that
sourceKeyspace string
// populated during WorkerStateFindTargets, read-only after that
sourceAlias *topodatapb.TabletAlias
sourceTablet *topo.TabletInfo
// populated during WorkerStateCopy
tableStatus []*tableStatus
startTime time.Time
// aliases of tablets that need to have their schema reloaded.
// Only populated once, read-only after that.
reloadAliases []*topodatapb.TabletAlias
reloadTablets map[topodatapb.TabletAlias]*topo.TabletInfo
ev *events.VerticalSplitClone
// Mutex to protect fields that might change when (re)resolving topology.
resolveMu sync.Mutex
destinationShardsToTablets map[string]*topo.TabletInfo
resolveTime time.Time
}
// NewVerticalSplitCloneWorker returns a new VerticalSplitCloneWorker object.
func NewVerticalSplitCloneWorker(wr *wrangler.Wrangler, cell, destinationKeyspace, destinationShard string, tables []string, strategyStr string, sourceReaderCount, destinationPackCount int, minTableSizeForSplit uint64, destinationWriterCount, minHealthyRdonlyEndPoints int) (Worker, error) {
if 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
}
return &VerticalSplitCloneWorker{
StatusWorker: NewStatusWorker(),
wr: wr,
cell: cell,
destinationKeyspace: destinationKeyspace,
destinationShard: destinationShard,
tables: tables,
strategy: strategy,
sourceReaderCount: sourceReaderCount,
destinationPackCount: destinationPackCount,
minTableSizeForSplit: minTableSizeForSplit,
destinationWriterCount: destinationWriterCount,
minHealthyRdonlyEndPoints: minHealthyRdonlyEndPoints,
cleaner: &wrangler.Cleaner{},
ev: &events.VerticalSplitClone{
Cell: cell,
Keyspace: destinationKeyspace,
Shard: destinationShard,
Tables: tables,
Strategy: strategy.String(),
},
}, nil
}
func (vscw *VerticalSplitCloneWorker) setState(state StatusWorkerState) {
vscw.SetState(state)
event.DispatchUpdate(vscw.ev, state.String())
}
func (vscw *VerticalSplitCloneWorker) setErrorState(err error) {
vscw.SetState(WorkerStateError)
event.DispatchUpdate(vscw.ev, "error: "+err.Error())
}
// StatusAsHTML implements the Worker interface
func (vscw *VerticalSplitCloneWorker) StatusAsHTML() template.HTML {
vscw.Mu.Lock()
defer vscw.Mu.Unlock()
result := "<b>Working on:</b> " + vscw.destinationKeyspace + "/" + vscw.destinationShard + "</br>\n"
result += "<b>State:</b> " + vscw.State.String() + "</br>\n"
switch vscw.State {
case WorkerStateCopy:
result += "<b>Running</b>:</br>\n"
result += "<b>Copying from</b>: " + topoproto.TabletAliasString(vscw.sourceAlias) + "</br>\n"
statuses, eta := formatTableStatuses(vscw.tableStatus, vscw.startTime)
result += "<b>ETA</b>: " + eta.String() + "</br>\n"
result += strings.Join(statuses, "</br>\n")
case WorkerStateDone:
result += "<b>Success</b>:</br>\n"
statuses, _ := formatTableStatuses(vscw.tableStatus, vscw.startTime)
result += strings.Join(statuses, "</br>\n")
}
return template.HTML(result)
}
// StatusAsText implements the Worker interface
func (vscw *VerticalSplitCloneWorker) StatusAsText() string {
vscw.Mu.Lock()
defer vscw.Mu.Unlock()
result := "Working on: " + vscw.destinationKeyspace + "/" + vscw.destinationShard + "\n"
result += "State: " + vscw.State.String() + "\n"
switch vscw.State {
case WorkerStateCopy:
result += "Running:\n"
result += "Copying from: " + topoproto.TabletAliasString(vscw.sourceAlias) + "\n"
statuses, eta := formatTableStatuses(vscw.tableStatus, vscw.startTime)
result += "ETA: " + eta.String() + "\n"
result += strings.Join(statuses, "\n")
case WorkerStateDone:
result += "Success:\n"
statuses, _ := formatTableStatuses(vscw.tableStatus, vscw.startTime)
result += strings.Join(statuses, "\n")
}
return result
}
// Run implements the Worker interface
func (vscw *VerticalSplitCloneWorker) Run(ctx context.Context) error {
resetVars()
err := vscw.run(ctx)
vscw.setState(WorkerStateCleanUp)
cerr := vscw.cleaner.CleanUp(vscw.wr)
if cerr != nil {
if err != nil {
vscw.wr.Logger().Errorf("CleanUp failed in addition to job error: %v", cerr)
} else {
err = cerr
}
}
if err != nil {
vscw.setErrorState(err)
return err
}
vscw.setState(WorkerStateDone)
return nil
}
func (vscw *VerticalSplitCloneWorker) run(ctx context.Context) error {
// first state: read what we need to do
if err := vscw.init(ctx); err != nil {
return fmt.Errorf("init() failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
// second state: find targets
if err := vscw.findTargets(ctx); err != nil {
return fmt.Errorf("findTargets() failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
// third state: copy data
if err := vscw.copy(ctx); err != nil {
return fmt.Errorf("copy() failed: %v", err)
}
if err := checkDone(ctx); err != nil {
return err
}
return nil
}
// init phase:
// - read the destination keyspace, make sure it has 'servedFrom' values
func (vscw *VerticalSplitCloneWorker) init(ctx context.Context) error {
vscw.setState(WorkerStateInit)
// read the keyspace and validate it
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
destinationKeyspaceInfo, err := vscw.wr.TopoServer().GetKeyspace(shortCtx, vscw.destinationKeyspace)
cancel()
if err != nil {
return fmt.Errorf("cannot read destination keyspace %v: %v", vscw.destinationKeyspace, err)
}
if len(destinationKeyspaceInfo.ServedFroms) == 0 {
return fmt.Errorf("destination keyspace %v has no KeyspaceServedFrom", vscw.destinationKeyspace)
}
// validate all serving types, find sourceKeyspace
servingTypes := []topodatapb.TabletType{topodatapb.TabletType_MASTER, topodatapb.TabletType_REPLICA, topodatapb.TabletType_RDONLY}
servedFrom := ""
for _, st := range servingTypes {
sf := destinationKeyspaceInfo.GetServedFrom(st)
if sf == nil {
return fmt.Errorf("destination keyspace %v is serving type %v", vscw.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", vscw.destinationKeyspace, servedFrom, sf.Keyspace)
}
}
}
vscw.sourceKeyspace = servedFrom
return nil
}
// findTargets phase:
// - find one rdonly in the source shard
// - mark it as 'worker' pointing back to us
// - get the aliases of all the targets
func (vscw *VerticalSplitCloneWorker) findTargets(ctx context.Context) error {
vscw.setState(WorkerStateFindTargets)
// find an appropriate endpoint in the source shard
var err error
vscw.sourceAlias, err = FindWorkerTablet(ctx, vscw.wr, vscw.cleaner, vscw.cell, vscw.sourceKeyspace, "0", vscw.minHealthyRdonlyEndPoints)
if err != nil {
return fmt.Errorf("FindWorkerTablet() failed for %v/%v/0: %v", vscw.cell, vscw.sourceKeyspace, err)
}
vscw.wr.Logger().Infof("Using tablet %v as the source", topoproto.TabletAliasString(vscw.sourceAlias))
// get the tablet info for it
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
vscw.sourceTablet, err = vscw.wr.TopoServer().GetTablet(shortCtx, vscw.sourceAlias)
cancel()
if err != nil {
return fmt.Errorf("cannot read tablet %v: %v", topoproto.TabletAliasString(vscw.sourceAlias), err)
}
// stop replication on it
shortCtx, cancel = context.WithTimeout(ctx, *remoteActionsTimeout)
err = vscw.wr.TabletManagerClient().StopSlave(shortCtx, vscw.sourceTablet)
cancel()
if err != nil {
return fmt.Errorf("cannot stop replication on tablet %v", topoproto.TabletAliasString(vscw.sourceAlias))
}
wrangler.RecordStartSlaveAction(vscw.cleaner, vscw.sourceTablet)
action, err := wrangler.FindChangeSlaveTypeActionByTarget(vscw.cleaner, vscw.sourceAlias)
if err != nil {
return fmt.Errorf("cannot find ChangeSlaveType action for %v: %v", topoproto.TabletAliasString(vscw.sourceAlias), err)
}
action.TabletType = topodatapb.TabletType_SPARE
return vscw.ResolveDestinationMasters(ctx)
}
// ResolveDestinationMasters implements the Resolver interface.
// It will attempt to resolve all shards and update vscw.destinationShardsToTablets;
// if it is unable to do so, it will not modify vscw.destinationShardsToTablets at all.
func (vscw *VerticalSplitCloneWorker) ResolveDestinationMasters(ctx context.Context) error {
statsDestinationAttemptedResolves.Add(1)
// Allow at most one resolution request at a time; if there are concurrent requests, only
// one of them will actualy hit the topo server.
vscw.resolveMu.Lock()
defer vscw.resolveMu.Unlock()
// If the last resolution was fresh enough, return it.
if time.Now().Sub(vscw.resolveTime) < *resolveTTL {
return nil
}
ti, err := resolveDestinationShardMaster(ctx, vscw.destinationKeyspace, vscw.destinationShard, vscw.wr)
if err != nil {
return err
}
vscw.destinationShardsToTablets = map[string]*topo.TabletInfo{vscw.destinationShard: ti}
// save the time of the last successful resolution
vscw.resolveTime = time.Now()
statsDestinationActualResolves.Add(1)
return nil
}
// GetDestinationMaster implements the Resolver interface
func (vscw *VerticalSplitCloneWorker) GetDestinationMaster(shardName string) (*topo.TabletInfo, error) {
vscw.resolveMu.Lock()
defer vscw.resolveMu.Unlock()
ti, ok := vscw.destinationShardsToTablets[shardName]
if !ok {
return nil, fmt.Errorf("no tablet found for destination shard %v", shardName)
}
return ti, nil
}
// Find all tablets on the destination shard. This should be done immediately before reloading
// the schema on these tablets, to minimize the chances of the topo changing in between.
func (vscw *VerticalSplitCloneWorker) findReloadTargets(ctx context.Context) error {
reloadAliases, reloadTablets, err := resolveReloadTabletsForShard(ctx, vscw.destinationKeyspace, vscw.destinationShard, vscw.wr)
if err != nil {
return err
}
vscw.reloadAliases, vscw.reloadTablets = reloadAliases, reloadTablets
return nil
}
// 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 (vscw *VerticalSplitCloneWorker) copy(ctx context.Context) error {
vscw.setState(WorkerStateCopy)
// get source schema
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
sourceSchemaDefinition, err := vscw.wr.GetSchema(shortCtx, vscw.sourceAlias, vscw.tables, nil, true)
cancel()
if err != nil {
return fmt.Errorf("cannot get schema from source %v: %v", topoproto.TabletAliasString(vscw.sourceAlias), err)
}
if len(sourceSchemaDefinition.TableDefinitions) == 0 {
return fmt.Errorf("no tables matching the table filter")
}
vscw.wr.Logger().Infof("Source tablet has %v tables to copy", len(sourceSchemaDefinition.TableDefinitions))
vscw.Mu.Lock()
vscw.tableStatus = make([]*tableStatus, len(sourceSchemaDefinition.TableDefinitions))
for i, td := range sourceSchemaDefinition.TableDefinitions {
vscw.tableStatus[i] = &tableStatus{
name: td.Name,
rowCount: td.RowCount,
}
}
vscw.startTime = time.Now()
vscw.Mu.Unlock()
// Count rows
for i, td := range sourceSchemaDefinition.TableDefinitions {
vscw.tableStatus[i].mu.Lock()
if td.Type == tmutils.TableBaseTable {
vscw.tableStatus[i].rowCount = td.RowCount
} else {
vscw.tableStatus[i].isView = true
}
vscw.tableStatus[i].mu.Unlock()
}
// In parallel, setup the channels to send SQL data chunks to
// for each destination tablet.
//
// mu protects firstError
mu := sync.Mutex{}
var firstError error
ctx, cancelCopy := context.WithCancel(ctx)
processError := func(format string, args ...interface{}) {
vscw.wr.Logger().Errorf(format, args...)
mu.Lock()
if firstError == nil {
firstError = fmt.Errorf(format, args...)
cancelCopy()
}
mu.Unlock()
}
destinationWaitGroup := sync.WaitGroup{}
// we create one channel for the 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.
insertChannel := make(chan string, vscw.destinationWriterCount*2)
go func(shardName string, insertChannel chan string) {
for j := 0; j < vscw.destinationWriterCount; j++ {
destinationWaitGroup.Add(1)
go func() {
defer destinationWaitGroup.Done()
if err := executeFetchLoop(ctx, vscw.wr, vscw, shardName, insertChannel); err != nil {
processError("executeFetchLoop failed: %v", err)
}
}()
}
}(vscw.destinationShard, insertChannel)
// Now for each table, read data chunks and send them to insertChannel
sourceWaitGroup := sync.WaitGroup{}
sema := sync2.NewSemaphore(vscw.sourceReaderCount, 0)
for tableIndex, td := range sourceSchemaDefinition.TableDefinitions {
if td.Type == tmutils.TableView {
continue
}
chunks, err := FindChunks(ctx, vscw.wr, vscw.sourceTablet, td, vscw.minTableSizeForSplit, vscw.sourceReaderCount)
if err != nil {
return err
}
vscw.tableStatus[tableIndex].setThreadCount(len(chunks) - 1)
for chunkIndex := 0; chunkIndex < len(chunks)-1; chunkIndex++ {
sourceWaitGroup.Add(1)
go func(td *tabletmanagerdatapb.TableDefinition, tableIndex, chunkIndex int) {
defer sourceWaitGroup.Done()
sema.Acquire()
defer sema.Release()
vscw.tableStatus[tableIndex].threadStarted()
// build the query, and start the streaming
selectSQL := buildSQLFromChunks(vscw.wr, td, chunks, chunkIndex, topoproto.TabletAliasString(vscw.sourceAlias))
qrr, err := NewQueryResultReaderForTablet(ctx, vscw.wr.TopoServer(), vscw.sourceAlias, selectSQL)
if err != nil {
processError("NewQueryResultReaderForTablet failed: %v", err)
return
}
defer qrr.Close()
// process the data
if err := vscw.processData(ctx, td, tableIndex, qrr, insertChannel, vscw.destinationPackCount); err != nil {
processError("QueryResultReader failed: %v", err)
}
vscw.tableStatus[tableIndex].threadDone()
}(td, tableIndex, chunkIndex)
}
}
sourceWaitGroup.Wait()
close(insertChannel)
destinationWaitGroup.Wait()
if firstError != nil {
return firstError
}
// then create and populate the blp_checkpoint table
if vscw.strategy.skipPopulateBlpCheckpoint {
vscw.wr.Logger().Infof("Skipping populating the blp_checkpoint table")
} else {
// get the current position from the source
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
status, err := vscw.wr.TabletManagerClient().SlaveStatus(shortCtx, vscw.sourceTablet)
cancel()
if err != nil {
return err
}
queries := make([]string, 0, 4)
queries = append(queries, binlogplayer.CreateBlpCheckpoint()...)
flags := ""
if vscw.strategy.dontStartBinlogPlayer {
flags = binlogplayer.BlpFlagDontStart
}
queries = append(queries, binlogplayer.PopulateBlpCheckpoint(0, status.Position, time.Now().Unix(), flags))
destinationWaitGroup.Add(1)
go func(shardName string) {
defer destinationWaitGroup.Done()
vscw.wr.Logger().Infof("Making and populating blp_checkpoint table")
if err := runSQLCommands(ctx, vscw.wr, vscw, shardName, queries); err != nil {
processError("blp_checkpoint queries failed: %v", err)
}
}(vscw.destinationShard)
destinationWaitGroup.Wait()
if firstError != nil {
return firstError
}
}
// Now we're done with data copy, update the shard's source info.
if vscw.strategy.skipSetSourceShards {
vscw.wr.Logger().Infof("Skipping setting SourceShard on destination shard.")
} else {
vscw.wr.Logger().Infof("Setting SourceShard on shard %v/%v", vscw.destinationKeyspace, vscw.destinationShard)
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
err := vscw.wr.SetSourceShards(shortCtx, vscw.destinationKeyspace, vscw.destinationShard, []*topodatapb.TabletAlias{vscw.sourceAlias}, vscw.tables)
cancel()
if err != nil {
return fmt.Errorf("Failed to set source shards: %v", err)
}
}
err = vscw.findReloadTargets(ctx)
if err != nil {
return fmt.Errorf("failed before reloading schema on destination tablets: %v", err)
}
// And force a schema reload on all destination tablets.
// The master tablet will end up starting filtered replication
// at this point.
for _, tabletAlias := range vscw.reloadAliases {
destinationWaitGroup.Add(1)
go func(ti *topo.TabletInfo) {
defer destinationWaitGroup.Done()
vscw.wr.Logger().Infof("Reloading schema on tablet %v", ti.AliasString())
shortCtx, cancel := context.WithTimeout(ctx, *remoteActionsTimeout)
err := vscw.wr.TabletManagerClient().ReloadSchema(shortCtx, ti)
cancel()
if err != nil {
processError("ReloadSchema failed on tablet %v: %v", ti.AliasString(), err)
}
}(vscw.reloadTablets[*tabletAlias])
}
destinationWaitGroup.Wait()
return firstError
}
// processData pumps the data out of the provided QueryResultReader.
// It returns any error the source encounters.
func (vscw *VerticalSplitCloneWorker) processData(ctx context.Context, td *tabletmanagerdatapb.TableDefinition, tableIndex int, qrr *QueryResultReader, insertChannel chan string, destinationPackCount int) error {
// process the data
baseCmd := td.Name + "(" + strings.Join(td.Columns, ", ") + ") VALUES "
var rows [][]sqltypes.Value
packCount := 0
for {
r, err := qrr.Output.Recv()
if err != nil {
// we are done, see if there was an error
if err != io.EOF {
return err
}
// send the remainder if any
if packCount > 0 {
cmd := baseCmd + makeValueString(qrr.Fields, rows)
select {
case insertChannel <- cmd:
case <-ctx.Done():
return nil
}
}
return nil
}
// add the rows to our current result
rows = append(rows, r.Rows...)
vscw.tableStatus[tableIndex].addCopiedRows(len(r.Rows))
// see if we reach the destination pack count
packCount++
if packCount < destinationPackCount {
continue
}
// send the rows to be inserted
cmd := baseCmd + makeValueString(qrr.Fields, rows)
select {
case insertChannel <- cmd:
case <-ctx.Done():
return nil
}
// and reset our row buffer
rows = nil
packCount = 0
}
}