-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
stream_migrator.go
776 lines (630 loc) · 22.7 KB
/
stream_migrator.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
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package workflow
import (
"context"
"fmt"
"strings"
"sync"
"text/template"
"google.golang.org/protobuf/encoding/prototext"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/binlog/binlogplayer"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/vindexes"
"vitess.io/vitess/go/vt/vttablet/tabletmanager/vreplication"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
)
// StreamType is an enum representing the kind of stream.
//
// (TODO:@ajm188) This should be made package-private once the last references
// in package wrangler are removed.
type StreamType int
// StreamType values.
const (
StreamTypeUnknown = StreamType(iota)
StreamTypeSharded
StreamTypeReference
)
type StreamMigrator struct {
streams map[string][]*VReplicationStream
workflows []string
templates []*VReplicationStream
ts ITrafficSwitcher
logger logutil.Logger
}
// BuildStreamMigrator creates a new StreamMigrator based on the given
// TrafficSwitcher.
func BuildStreamMigrator(ctx context.Context, ts ITrafficSwitcher, cancelMigrate bool) (*StreamMigrator, error) {
sm := &StreamMigrator{
ts: ts,
logger: ts.Logger(),
}
if sm.ts.MigrationType() == binlogdatapb.MigrationType_TABLES {
// Source streams should be stopped only for shard migrations.
return sm, nil
}
var err error
sm.streams, err = sm.readSourceStreams(ctx, cancelMigrate)
if err != nil {
return nil, err
}
// Loop executes only once.
for _, tabletStreams := range sm.streams {
tmpl, err := sm.templatize(ctx, tabletStreams)
if err != nil {
return nil, err
}
sm.workflows = VReplicationStreams(tmpl).Workflows()
break
}
return sm, nil
}
// StreamMigratorFinalize finalizes the stream migration.
//
// (TODO:@ajm88) in the original implementation, "it's a standalone function
// because it does not use the streamMigrater state". That's still true, but
// moving this to a method on StreamMigrator would provide a cleaner namespacing
// in package workflow. But, we would have to update more callers in order to
// do that (*wrangler.switcher's streamMigrateFinalize would need to change its
// signature to also take a *workflow.StreamMigrator), so we will do that in a
// second PR.
func StreamMigratorFinalize(ctx context.Context, ts ITrafficSwitcher, workflows []string) error {
if len(workflows) == 0 {
return nil
}
workflowList := stringListify(workflows)
err := ts.ForAllSources(func(source *MigrationSource) error {
query := fmt.Sprintf("delete from _vt.vreplication where db_name=%s and workflow in (%s)", encodeString(source.GetPrimary().DbName()), workflowList)
_, err := ts.VReplicationExec(ctx, source.GetPrimary().Alias, query)
return err
})
if err != nil {
return err
}
err = ts.ForAllTargets(func(target *MigrationTarget) error {
query := fmt.Sprintf("update _vt.vreplication set state='Running' where db_name=%s and workflow in (%s)", encodeString(target.GetPrimary().DbName()), workflowList)
_, err := ts.VReplicationExec(ctx, target.GetPrimary().Alias, query)
return err
})
return err
}
// Streams returns a deep-copy of the StreamMigrator's streams map.
func (sm *StreamMigrator) Streams() map[string][]*VReplicationStream {
streams := make(map[string][]*VReplicationStream, len(sm.streams))
for k, v := range sm.streams {
streams[k] = VReplicationStreams(v).Copy().ToSlice()
}
return streams
}
// Templates returns a copy of the StreamMigrator's template streams.
func (sm *StreamMigrator) Templates() []*VReplicationStream {
return VReplicationStreams(sm.templates).Copy().ToSlice()
}
func (sm *StreamMigrator) CancelMigration(ctx context.Context) {
if sm.streams == nil {
return
}
_ = sm.deleteTargetStreams(ctx)
err := sm.ts.ForAllSources(func(source *MigrationSource) error {
query := fmt.Sprintf("update _vt.vreplication set state='Running', stop_pos=null, message='' where db_name=%s and workflow != %s", encodeString(source.GetPrimary().DbName()), encodeString(sm.ts.ReverseWorkflowName()))
_, err := sm.ts.VReplicationExec(ctx, source.GetPrimary().Alias, query)
return err
})
if err != nil {
sm.logger.Errorf("Cancel migration failed: could not restart source streams: %v", err)
}
}
func (sm *StreamMigrator) MigrateStreams(ctx context.Context) error {
if sm.streams == nil {
return nil
}
if err := sm.deleteTargetStreams(ctx); err != nil {
return err
}
return sm.createTargetStreams(ctx, sm.templates)
}
func (sm *StreamMigrator) StopStreams(ctx context.Context) ([]string, error) {
if sm.streams == nil {
return nil, nil
}
if err := sm.stopSourceStreams(ctx); err != nil {
return nil, err
}
positions, err := sm.syncSourceStreams(ctx)
if err != nil {
return nil, err
}
return sm.verifyStreamPositions(ctx, positions)
}
/* tablet streams */
func (sm *StreamMigrator) readTabletStreams(ctx context.Context, ti *topo.TabletInfo, constraint string) ([]*VReplicationStream, error) {
var query string
switch constraint {
case "":
query = fmt.Sprintf("select id, workflow, source, pos from _vt.vreplication where db_name=%s and workflow != %s", encodeString(ti.DbName()), encodeString(sm.ts.ReverseWorkflowName()))
default:
query = fmt.Sprintf("select id, workflow, source, pos from _vt.vreplication where db_name=%s and workflow != %s and %s", encodeString(ti.DbName()), encodeString(sm.ts.ReverseWorkflowName()), constraint)
}
p3qr, err := sm.ts.TabletManagerClient().VReplicationExec(ctx, ti.Tablet, query)
if err != nil {
return nil, err
}
qr := sqltypes.Proto3ToResult(p3qr)
tabletStreams := make([]*VReplicationStream, 0, len(qr.Rows))
for _, row := range qr.Rows {
id, err := evalengine.ToInt64(row[0])
if err != nil {
return nil, err
}
workflowName := row[1].ToString()
switch workflowName {
case "":
return nil, fmt.Errorf("VReplication streams must have named workflows for migration: shard: %s:%s, stream: %d", ti.Keyspace, ti.Shard, id)
case sm.ts.WorkflowName():
return nil, fmt.Errorf("VReplication stream has the same workflow name as the resharding workflow: shard: %s:%s, stream: %d", ti.Keyspace, ti.Shard, id)
}
var bls binlogdatapb.BinlogSource
rowBytes, err := row[2].ToBytes()
if err != nil {
return nil, err
}
if err := prototext.Unmarshal(rowBytes, &bls); err != nil {
return nil, err
}
isReference, err := sm.blsIsReference(&bls)
if err != nil {
return nil, vterrors.Wrap(err, "blsIsReference")
}
if isReference {
sm.ts.Logger().Infof("readTabletStreams: ignoring reference table %+v", &bls)
continue
}
pos, err := mysql.DecodePosition(row[3].ToString())
if err != nil {
return nil, err
}
tabletStreams = append(tabletStreams, &VReplicationStream{
ID: uint32(id),
Workflow: workflowName,
BinlogSource: &bls,
Position: pos,
})
}
return tabletStreams, nil
}
/* source streams */
func (sm *StreamMigrator) readSourceStreams(ctx context.Context, cancelMigrate bool) (map[string][]*VReplicationStream, error) {
var (
mu sync.Mutex
streams = make(map[string][]*VReplicationStream)
)
err := sm.ts.ForAllSources(func(source *MigrationSource) error {
if !cancelMigrate {
// This flow protects us from the following scenario: When we create streams,
// we always do it in two phases. We start them off as Stopped, and then
// update them to Running. If such an operation fails, we may be left with
// lingering Stopped streams. They should actually be cleaned up by the user.
// In the current workflow, we stop streams and restart them.
// Once existing streams are stopped, there will be confusion about which of
// them can be restarted because they will be no different from the lingering streams.
// To prevent this confusion, we first check if there are any stopped streams.
// If so, we request the operator to clean them up, or restart them before going ahead.
// This allows us to assume that all stopped streams can be safely restarted
// if we cancel the operation.
stoppedStreams, err := sm.readTabletStreams(ctx, source.GetPrimary(), "state = 'Stopped' and message != 'FROZEN'")
if err != nil {
return err
}
if len(stoppedStreams) != 0 {
return fmt.Errorf("cannot migrate until all streams are running: %s: %d", source.GetShard().ShardName(), source.GetPrimary().Alias.Uid)
}
}
tabletStreams, err := sm.readTabletStreams(ctx, source.GetPrimary(), "")
if err != nil {
return err
}
if len(tabletStreams) == 0 {
// No VReplication is running. So, we have no work to do.
return nil
}
query := fmt.Sprintf("select vrepl_id from _vt.copy_state where vrepl_id in %s", VReplicationStreams(tabletStreams).Values())
p3qr, err := sm.ts.TabletManagerClient().VReplicationExec(ctx, source.GetPrimary().Tablet, query)
switch {
case err != nil:
return err
case len(p3qr.Rows) != 0:
return fmt.Errorf("cannot migrate while vreplication streams in source shards are still copying: %s", source.GetShard().ShardName())
}
mu.Lock()
defer mu.Unlock()
streams[source.GetShard().ShardName()] = tabletStreams
return nil
})
if err != nil {
return nil, err
}
// Validate that streams match across source shards.
var (
reference []*VReplicationStream
refshard string
streams2 = make(map[string][]*VReplicationStream)
)
for k, v := range streams {
if reference == nil {
refshard = k
reference = v
continue
}
streams2[k] = append([]*VReplicationStream(nil), v...)
}
for shard, tabletStreams := range streams2 {
for _, refStream := range reference {
err := func() error {
for i := 0; i < len(tabletStreams); i++ {
vrs := tabletStreams[i]
if refStream.Workflow == vrs.Workflow &&
refStream.BinlogSource.Keyspace == vrs.BinlogSource.Keyspace &&
refStream.BinlogSource.Shard == vrs.BinlogSource.Shard {
// Delete the matched item and scan for the next stream.
tabletStreams = append(tabletStreams[:i], tabletStreams[i+1:]...)
return nil
}
}
return fmt.Errorf("streams are mismatched across source shards: %s vs %s", refshard, shard)
}()
if err != nil {
return nil, err
}
}
if len(tabletStreams) != 0 {
return nil, fmt.Errorf("streams are mismatched across source shards: %s vs %s", refshard, shard)
}
}
return streams, nil
}
func (sm *StreamMigrator) stopSourceStreams(ctx context.Context) error {
var (
mu sync.Mutex
stoppedStreams = make(map[string][]*VReplicationStream)
)
err := sm.ts.ForAllSources(func(source *MigrationSource) error {
tabletStreams := sm.streams[source.GetShard().ShardName()]
if len(tabletStreams) == 0 {
return nil
}
query := fmt.Sprintf("update _vt.vreplication set state='Stopped', message='for cutover' where id in %s", VReplicationStreams(tabletStreams).Values())
_, err := sm.ts.TabletManagerClient().VReplicationExec(ctx, source.GetPrimary().Tablet, query)
if err != nil {
return err
}
tabletStreams, err = sm.readTabletStreams(ctx, source.GetPrimary(), fmt.Sprintf("id in %s", VReplicationStreams(tabletStreams).Values()))
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
stoppedStreams[source.GetShard().ShardName()] = tabletStreams
return nil
})
if err != nil {
return err
}
sm.streams = stoppedStreams
return nil
}
func (sm *StreamMigrator) syncSourceStreams(ctx context.Context) (map[string]mysql.Position, error) {
stopPositions := make(map[string]mysql.Position)
for _, tabletStreams := range sm.streams {
for _, vrs := range tabletStreams {
key := fmt.Sprintf("%s:%s", vrs.BinlogSource.Keyspace, vrs.BinlogSource.Shard)
if pos, ok := stopPositions[key]; !ok || vrs.Position.AtLeast(pos) {
sm.ts.Logger().Infof("syncSourceStreams setting stopPositions +%s %+v %d", key, vrs.Position, vrs.ID)
stopPositions[key] = vrs.Position
}
}
}
var (
wg sync.WaitGroup
allErrors concurrency.AllErrorRecorder
)
for shard, tabletStreams := range sm.streams {
for _, vrs := range tabletStreams {
key := fmt.Sprintf("%s:%s", vrs.BinlogSource.Keyspace, vrs.BinlogSource.Shard)
pos := stopPositions[key]
sm.ts.Logger().Infof("syncSourceStreams before go func +%s %+v %d", key, pos, vrs.ID)
if vrs.Position.Equal(pos) {
continue
}
wg.Add(1)
go func(vrs *VReplicationStream, shard string, pos mysql.Position) {
defer wg.Done()
sm.ts.Logger().Infof("syncSourceStreams beginning of go func %s %s %+v %d", shard, vrs.BinlogSource.Shard, pos, vrs.ID)
si, err := sm.ts.TopoServer().GetShard(ctx, sm.ts.SourceKeyspaceName(), shard)
if err != nil {
allErrors.RecordError(err)
return
}
primary, err := sm.ts.TopoServer().GetTablet(ctx, si.PrimaryAlias)
if err != nil {
allErrors.RecordError(err)
return
}
query := fmt.Sprintf("update _vt.vreplication set state='Running', stop_pos='%s', message='synchronizing for cutover' where id=%d", mysql.EncodePosition(pos), vrs.ID)
if _, err := sm.ts.TabletManagerClient().VReplicationExec(ctx, primary.Tablet, query); err != nil {
allErrors.RecordError(err)
return
}
sm.ts.Logger().Infof("Waiting for keyspace:shard: %v:%v, position %v", sm.ts.SourceKeyspaceName(), shard, pos)
if err := sm.ts.TabletManagerClient().VReplicationWaitForPos(ctx, primary.Tablet, int(vrs.ID), mysql.EncodePosition(pos)); err != nil {
allErrors.RecordError(err)
return
}
sm.ts.Logger().Infof("Position for keyspace:shard: %v:%v reached", sm.ts.SourceKeyspaceName(), shard)
}(vrs, shard, pos)
}
}
wg.Wait()
return stopPositions, allErrors.AggrError(vterrors.Aggregate)
}
func (sm *StreamMigrator) verifyStreamPositions(ctx context.Context, stopPositions map[string]mysql.Position) ([]string, error) {
var (
mu sync.Mutex
stoppedStreams = make(map[string][]*VReplicationStream)
)
err := sm.ts.ForAllSources(func(source *MigrationSource) error {
tabletStreams := sm.streams[source.GetShard().ShardName()]
if len(tabletStreams) == 0 {
return nil
}
tabletStreams, err := sm.readTabletStreams(ctx, source.GetPrimary(), fmt.Sprintf("id in %s", VReplicationStreams(tabletStreams).Values()))
if err != nil {
return err
}
mu.Lock()
defer mu.Unlock()
stoppedStreams[source.GetShard().ShardName()] = tabletStreams
return nil
})
if err != nil {
return nil, err
}
// This is not really required because it's not used later.
// But we keep it up-to-date for good measure.
sm.streams = stoppedStreams
var (
oneSet []*VReplicationStream
allErrors concurrency.AllErrorRecorder
)
for _, tabletStreams := range stoppedStreams {
if oneSet == nil {
oneSet = tabletStreams
}
for _, vrs := range tabletStreams {
key := fmt.Sprintf("%s:%s", vrs.BinlogSource.Keyspace, vrs.BinlogSource.Shard)
if pos := stopPositions[key]; !vrs.Position.Equal(pos) {
allErrors.RecordError(fmt.Errorf("%s: stream %d position: %s does not match %s", key, vrs.ID, mysql.EncodePosition(vrs.Position), mysql.EncodePosition(pos)))
}
}
}
if allErrors.HasErrors() {
return nil, allErrors.AggrError(vterrors.Aggregate)
}
sm.templates, err = sm.templatize(ctx, oneSet)
if err != nil {
// Unreachable: we've already templatized this before.
return nil, err
}
return VReplicationStreams(sm.templates).Workflows(), allErrors.AggrError(vterrors.Aggregate)
}
/* target streams */
func (sm *StreamMigrator) createTargetStreams(ctx context.Context, tmpl []*VReplicationStream) error {
if len(tmpl) == 0 {
return nil
}
return sm.ts.ForAllTargets(func(target *MigrationTarget) error {
ig := vreplication.NewInsertGenerator(binlogplayer.BlpStopped, target.GetPrimary().DbName())
tabletStreams := VReplicationStreams(tmpl).Copy().ToSlice()
for _, vrs := range tabletStreams {
for _, rule := range vrs.BinlogSource.Filter.Rules {
buf := &strings.Builder{}
t := template.Must(template.New("").Parse(rule.Filter))
if err := t.Execute(buf, key.KeyRangeString(target.GetShard().KeyRange)); err != nil {
return err
}
rule.Filter = buf.String()
}
ig.AddRow(vrs.Workflow, vrs.BinlogSource, mysql.EncodePosition(vrs.Position), "", "")
}
_, err := sm.ts.VReplicationExec(ctx, target.GetPrimary().GetAlias(), ig.String())
return err
})
}
func (sm *StreamMigrator) deleteTargetStreams(ctx context.Context) error {
if len(sm.workflows) == 0 {
return nil
}
workflows := stringListify(sm.workflows)
err := sm.ts.ForAllTargets(func(target *MigrationTarget) error {
query := fmt.Sprintf("delete from _vt.vreplication where db_name=%s and workflow in (%s)", encodeString(target.GetPrimary().DbName()), workflows)
_, err := sm.ts.VReplicationExec(ctx, target.GetPrimary().Alias, query)
return err
})
if err != nil {
sm.logger.Warningf("Could not delete migrated streams: %v", err)
}
return err
}
/* templatizing */
func (sm *StreamMigrator) templatize(ctx context.Context, tabletStreams []*VReplicationStream) ([]*VReplicationStream, error) {
var shardedStreams []*VReplicationStream
tabletStreams = VReplicationStreams(tabletStreams).Copy().ToSlice()
for _, vrs := range tabletStreams {
streamType := StreamTypeUnknown
for _, rule := range vrs.BinlogSource.Filter.Rules {
typ, err := sm.templatizeRule(ctx, rule)
if err != nil {
return nil, err
}
switch typ {
case StreamTypeSharded:
if streamType == StreamTypeReference {
return nil, fmt.Errorf("cannot migrate streams with a mix of reference and sharded tables: %v", vrs.BinlogSource)
}
streamType = StreamTypeSharded
case StreamTypeReference:
if streamType == StreamTypeSharded {
return nil, fmt.Errorf("cannot migrate streams with a mix of reference and sharded tables: %v", vrs.BinlogSource)
}
streamType = StreamTypeReference
}
}
if streamType == StreamTypeSharded {
shardedStreams = append(shardedStreams, vrs)
}
}
return shardedStreams, nil
}
// templatizeRule replaces keyrange values with {{.}}.
// This can then be used by go's template package to substitute other keyrange values.
func (sm *StreamMigrator) templatizeRule(ctx context.Context, rule *binlogdatapb.Rule) (StreamType, error) {
vtable, ok := sm.ts.SourceKeyspaceSchema().Tables[rule.Match]
if !ok {
return StreamTypeUnknown, fmt.Errorf("table %v not found in vschema", rule.Match)
}
if vtable.Type == vindexes.TypeReference {
return StreamTypeReference, nil
}
switch {
case rule.Filter == "":
return StreamTypeUnknown, fmt.Errorf("rule %v does not have a select expression in vreplication", rule)
case key.IsKeyRange(rule.Filter):
rule.Filter = "{{.}}"
return StreamTypeSharded, nil
case rule.Filter == vreplication.ExcludeStr:
return StreamTypeUnknown, fmt.Errorf("unexpected rule in vreplication: %v", rule)
default:
if err := sm.templatizeKeyRange(ctx, rule); err != nil {
return StreamTypeUnknown, err
}
return StreamTypeSharded, nil
}
}
func (sm *StreamMigrator) templatizeKeyRange(ctx context.Context, rule *binlogdatapb.Rule) error {
statement, err := sqlparser.Parse(rule.Filter)
if err != nil {
return err
}
sel, ok := statement.(*sqlparser.Select)
if !ok {
return fmt.Errorf("unexpected query: %v", rule.Filter)
}
var expr sqlparser.Expr
if sel.Where != nil {
expr = sel.Where.Expr
}
exprs := sqlparser.SplitAndExpression(nil, expr)
for _, subexpr := range exprs {
funcExpr, ok := subexpr.(*sqlparser.FuncExpr)
if !ok || !funcExpr.Name.EqualString("in_keyrange") {
continue
}
var krExpr sqlparser.SelectExpr
switch len(funcExpr.Exprs) {
case 1:
krExpr = funcExpr.Exprs[0]
case 3:
krExpr = funcExpr.Exprs[2]
default:
return fmt.Errorf("unexpected in_keyrange parameters: %v", sqlparser.String(funcExpr))
}
aliased, ok := krExpr.(*sqlparser.AliasedExpr)
if !ok {
return fmt.Errorf("unexpected in_keyrange parameters: %v", sqlparser.String(funcExpr))
}
val, ok := aliased.Expr.(*sqlparser.Literal)
if !ok {
return fmt.Errorf("unexpected in_keyrange parameters: %v", sqlparser.String(funcExpr))
}
if strings.Contains(rule.Filter, "{{") {
return fmt.Errorf("cannot migrate queries that contain '{{' in their string: %s", rule.Filter)
}
val.Val = "{{.}}"
rule.Filter = sqlparser.String(statement)
return nil
}
// There was no in_keyrange expression. Create a new one.
vtable := sm.ts.SourceKeyspaceSchema().Tables[rule.Match]
inkr := &sqlparser.FuncExpr{
Name: sqlparser.NewColIdent("in_keyrange"),
Exprs: sqlparser.SelectExprs{
&sqlparser.AliasedExpr{Expr: &sqlparser.ColName{Name: vtable.ColumnVindexes[0].Columns[0]}},
&sqlparser.AliasedExpr{Expr: sqlparser.NewStrLiteral(vtable.ColumnVindexes[0].Type)},
&sqlparser.AliasedExpr{Expr: sqlparser.NewStrLiteral("{{.}}")},
},
}
sel.AddWhere(inkr)
rule.Filter = sqlparser.String(statement)
return nil
}
/* misc */
func (sm *StreamMigrator) blsIsReference(bls *binlogdatapb.BinlogSource) (bool, error) {
streamType := StreamTypeUnknown
for _, rule := range bls.Filter.Rules {
typ, err := sm.identifyRuleType(rule)
if err != nil {
return false, err
}
switch typ {
case StreamTypeSharded:
if streamType == StreamTypeReference {
return false, fmt.Errorf("cannot reshard streams with a mix of reference and sharded tables: %v", bls)
}
streamType = StreamTypeSharded
case StreamTypeReference:
if streamType == StreamTypeSharded {
return false, fmt.Errorf("cannot reshard streams with a mix of reference and sharded tables: %v", bls)
}
streamType = StreamTypeReference
}
}
return streamType == StreamTypeReference, nil
}
func (sm *StreamMigrator) identifyRuleType(rule *binlogdatapb.Rule) (StreamType, error) {
vtable, ok := sm.ts.SourceKeyspaceSchema().Tables[rule.Match]
if !ok {
return 0, fmt.Errorf("table %v not found in vschema", rule.Match)
}
if vtable.Type == vindexes.TypeReference {
return StreamTypeReference, nil
}
// In this case, 'sharded' means that it's not a reference
// table. We don't care about any other subtleties.
return StreamTypeSharded, nil
}
func stringListify(ss []string) string {
var buf strings.Builder
prefix := ""
for _, s := range ss {
fmt.Fprintf(&buf, "%s%s", prefix, encodeString(s))
prefix = ", "
}
return buf.String()
}