-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathonlineddl_vrepl_test.go
More file actions
1028 lines (936 loc) · 45.4 KB
/
onlineddl_vrepl_test.go
File metadata and controls
1028 lines (936 loc) · 45.4 KB
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 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vrepl
import (
"flag"
"fmt"
"os"
"path"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/test/endtoend/onlineddl"
"vitess.io/vitess/go/test/endtoend/throttler"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp"
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
vtctldatapb "vitess.io/vitess/go/vt/proto/vtctldata"
)
var (
clusterInstance *cluster.LocalProcessCluster
shards []cluster.Shard
vtParams mysql.ConnParams
normalMigrationWait = 45 * time.Second
extendedMigrationWait = 60 * time.Second
hostname = "localhost"
keyspaceName = "ks"
cell = "zone1"
schemaChangeDirectory = ""
totalTableCount = 4
createTable = `
CREATE TABLE %s (
id bigint(20) NOT NULL,
test_val bigint unsigned NOT NULL DEFAULT 0,
msg varchar(64),
PRIMARY KEY (id)
) ENGINE=InnoDB;`
// To verify non online-DDL behavior
alterTableNormalStatement = `
ALTER TABLE %s
ADD COLUMN non_online int UNSIGNED NOT NULL DEFAULT 0`
// A trivial statement which must succeed and does not change the schema
alterTableTrivialStatement = `
ALTER TABLE %s
ENGINE=InnoDB`
// The following statement is valid
alterTableSuccessfulStatement = `
ALTER TABLE %s
MODIFY id bigint UNSIGNED NOT NULL,
ADD COLUMN vrepl_col int NOT NULL DEFAULT 0,
ADD INDEX idx_msg(msg)`
// The following statement will fail because vreplication requires shared PRIMARY KEY columns
alterTableFailedStatement = `
ALTER TABLE %s
DROP PRIMARY KEY,
DROP COLUMN vrepl_col`
alterTableFailedVreplicationStatement = `
ALTER TABLE %s
ADD UNIQUE KEY test_val_uidx (test_val)`
// We will run this query while throttling vreplication
alterTableThrottlingStatement = `
ALTER TABLE %s
DROP COLUMN vrepl_col`
onlineDDLCreateTableStatement = `
CREATE TABLE %s (
id bigint NOT NULL,
test_val bigint unsigned NOT NULL DEFAULT 0,
online_ddl_create_col INT NOT NULL DEFAULT 0,
PRIMARY KEY (id)
) ENGINE=InnoDB;`
onlineDDLDropTableStatement = `
DROP TABLE %s`
onlineDDLDropTableIfExistsStatement = `
DROP TABLE IF EXISTS %s`
insertRowStatement = `
INSERT INTO %s (id, test_val) VALUES (%d, 1)
`
selectCountRowsStatement = `
SELECT COUNT(*) AS c FROM %s
`
countInserts int64
insertMutex sync.Mutex
vSchema = `
{
"sharded": true,
"vindexes": {
"hash_index": {
"type": "hash"
}
},
"tables": {
"vt_onlineddl_test_00": {
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
}
]
},
"vt_onlineddl_test_01": {
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
}
]
},
"vt_onlineddl_test_02": {
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
}
]
},
"vt_onlineddl_test_03": {
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
}
]
}
}
}
`
)
const (
customThreshold = 5
throttlerEnabledTimeout = 60 * time.Second
noCustomQuery = ""
)
func TestMain(m *testing.M) {
flag.Parse()
exitcode, err := func() (int, error) {
clusterInstance = cluster.NewCluster(cell, hostname)
schemaChangeDirectory = path.Join("/tmp", fmt.Sprintf("schema_change_dir_%d", clusterInstance.GetAndReserveTabletUID()))
defer os.RemoveAll(schemaChangeDirectory)
defer clusterInstance.Teardown()
if _, err := os.Stat(schemaChangeDirectory); os.IsNotExist(err) {
_ = os.Mkdir(schemaChangeDirectory, 0o700)
}
clusterInstance.VtctldExtraArgs = []string{
"--schema-change-dir", schemaChangeDirectory,
"--schema-change-controller", "local",
"--schema-change-check-interval", "1s",
}
clusterInstance.VtTabletExtraArgs = []string{
"--heartbeat-interval", "250ms",
"--migration-check-interval", "5s",
"--watch-replication-stream",
}
clusterInstance.VtGateExtraArgs = []string{
"--ddl-strategy", "online",
}
if err := clusterInstance.StartTopo(); err != nil {
return 1, err
}
keyspace := &cluster.Keyspace{
Name: keyspaceName,
VSchema: vSchema,
}
if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, false, clusterInstance.Cell); err != nil {
return 1, err
}
vtgateInstance := clusterInstance.NewVtgateInstance()
// Start vtgate
if err := vtgateInstance.Setup(); err != nil {
return 1, err
}
// ensure it is torn down during cluster TearDown
clusterInstance.VtgateProcess = *vtgateInstance
vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}
return m.Run(), nil
}()
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
} else {
os.Exit(exitcode)
}
}
func TestVreplSchemaChanges(t *testing.T) {
shards = clusterInstance.Keyspaces[0].Shards
require.Equal(t, 2, len(shards))
for _, shard := range shards {
require.Equal(t, 2, len(shard.Vttablets))
}
providedUUID := ""
providedMigrationContext := ""
// We execute the throttler commands via vtgate, which in turn
// executes them via vttablet. So let's wait until vtgate's view
// is updated.
err := clusterInstance.WaitForTabletsToHealthyInVtgate()
require.NoError(t, err)
t.Run("WaitForSrvKeyspace", func(t *testing.T) {
for _, ks := range clusterInstance.Keyspaces {
t.Run(ks.Name, func(t *testing.T) {
err := throttler.WaitForSrvKeyspace(clusterInstance, cell, ks.Name)
require.NoError(t, err)
})
}
})
t.Run("updating throttler config", func(t *testing.T) {
req := &vtctldatapb.UpdateThrottlerConfigRequest{Enable: true, Threshold: customThreshold}
_, err := throttler.UpdateThrottlerTopoConfig(clusterInstance, req, nil, nil)
require.NoError(t, err)
})
t.Run("checking throttler config", func(t *testing.T) {
for _, ks := range clusterInstance.Keyspaces {
t.Run(ks.Name, func(t *testing.T) {
for _, shard := range ks.Shards {
t.Run(shard.Name, func(t *testing.T) {
for _, tablet := range shard.Vttablets {
t.Run(tablet.Alias, func(t *testing.T) {
throttler.WaitForThrottlerStatusEnabled(t, &clusterInstance.VtctldClientProcess, tablet, true, &throttler.Config{Query: throttler.DefaultQuery, Threshold: customThreshold}, throttlerEnabledTimeout)
})
}
})
}
})
}
})
testWithInitialSchema(t)
t.Run("alter non_online", func(t *testing.T) {
_ = testOnlineDDLStatement(t, alterTableNormalStatement, string(schema.DDLStrategyDirect), providedUUID, providedMigrationContext, "vtctl", "non_online", "", false)
insertRows(t, 2)
testRows(t)
})
t.Run("successful online alter, vtgate", func(t *testing.T) {
insertRows(t, 2)
uuid := testOnlineDDLStatement(t, alterTableSuccessfulStatement, "online", providedUUID, providedMigrationContext, "vtgate", "vrepl_col", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
testRows(t)
testMigrationRowCount(t, uuid)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckMigrationArtifacts(t, &vtParams, shards, uuid, true)
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
retainArtifactSeconds := row.AsInt64("retain_artifacts_seconds", 0)
assert.Equal(t, int64(86400), retainArtifactSeconds)
artifacts := row.AsString("artifacts", "")
assert.NotContains(t, artifacts, "_vt_HOLD_") // _vt_HOLD table removed at cut-over time
}
onlineddl.CheckCleanupMigration(t, &vtParams, shards, uuid)
rs = onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
retainArtifactSeconds := row.AsInt64("retain_artifacts_seconds", 0)
assert.Equal(t, int64(-1), retainArtifactSeconds)
assert.False(t, row["shadow_analyzed_timestamp"].IsNull())
}
})
t.Run("successful online alter, vtctl", func(t *testing.T) {
insertRows(t, 2)
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "online", providedUUID, providedMigrationContext, "vtctl", "vrepl_col", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
testRows(t)
testMigrationRowCount(t, uuid)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckMigrationArtifacts(t, &vtParams, shards, uuid, true)
})
t.Run("successful online alter, vtctl, explicit UUID", func(t *testing.T) {
insertRows(t, 2)
providedUUID = "00000000_51c9_11ec_9cf2_0a43f95f28a3"
providedMigrationContext = "endtoend:0000-1111"
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess", providedUUID, providedMigrationContext, "vtctl", "vrepl_col", "", false)
assert.Equal(t, providedUUID, uuid)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
testRows(t)
testMigrationRowCount(t, uuid)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckMigrationArtifacts(t, &vtParams, shards, uuid, true)
})
t.Run("duplicate migration, implicitly ignored", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "online", providedUUID, providedMigrationContext, "vtctl", "vrepl_col", "", true)
assert.Equal(t, providedUUID, uuid)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
})
t.Run("fail duplicate migration with different context", func(t *testing.T) {
_ = testOnlineDDLStatement(t, alterTableTrivialStatement, "online", providedUUID, "endtoend:different-context-0000", "vtctl", "vrepl_col", "rejected", true)
})
providedUUID = ""
providedMigrationContext = ""
t.Run("successful online alter, postponed, vtgate", func(t *testing.T) {
insertRows(t, 2)
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess -postpone-completion", providedUUID, providedMigrationContext, "vtgate", "test_val", "", false)
// Should be still running!
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusRunning)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
// Issue a complete and wait for successful completion
onlineddl.CheckCompleteMigration(t, &vtParams, shards, uuid, true)
// This part may take a while, because we depend on vreplicatoin polling
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
testRows(t)
testMigrationRowCount(t, uuid)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
})
// Notes about throttling:
// In this endtoend test we test both direct tablet API for throttling, as well as VTGate queries.
// - VTGate queries (`ALTER VITESS_MIGRATION THROTTLE ALL ...`) are sent to all relevant shards/tablets via QueryExecutor
// - tablet API calls have to be sent per-shard to the primary tablet of that shard
t.Run("throttled migration", func(t *testing.T) {
// Use VTGate for throttling, issue a `ALTER VITESS_MIGRATION THROTTLE ALL ...`
insertRows(t, 2)
onlineddl.ThrottleAllMigrations(t, &vtParams)
defer onlineddl.UnthrottleAllMigrations(t, &vtParams)
uuid := testOnlineDDLStatement(t, alterTableThrottlingStatement, "online", providedUUID, providedMigrationContext, "vtgate", "vrepl_col", "", true)
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusRunning)
testRows(t)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, true)
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusFailed, schema.OnlineDDLStatusCancelled)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusCancelled)
})
t.Run("throttled and unthrottled migration", func(t *testing.T) {
insertRows(t, 2)
// Use VTGate for throttling, issue a `ALTER VITESS_MIGRATION THROTTLE ALL ...`
// begin throttling:
onlineddl.ThrottleAllMigrations(t, &vtParams)
defer onlineddl.UnthrottleAllMigrations(t, &vtParams)
onlineddl.CheckThrottledApps(t, &vtParams, throttlerapp.OnlineDDLName, true)
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess", providedUUID, providedMigrationContext, "vtgate", "test_val", "", true)
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusRunning)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
testRows(t)
// gotta give the migration a few seconds to read throttling info from _vt.vreplication and write
// to _vt.schema_migrations
row, startedTimestamp, lastThrottledTimestamp := onlineddl.WaitForThrottledTimestamp(t, &vtParams, uuid, normalMigrationWait)
require.NotNil(t, row)
// vplayer and vcopier update throttle timestamp every second, so we expect the value
// to be strictly higher than started_timestamp
assert.GreaterOrEqual(t, lastThrottledTimestamp, startedTimestamp)
component := row.AsString("component_throttled", "")
assert.Contains(t, []string{throttlerapp.VCopierName.String(), throttlerapp.VPlayerName.String()}, component)
reason := row.AsString("reason_throttled", "")
assert.Contains(t, reason, "is explicitly denied access")
// unthrottle
onlineddl.UnthrottleAllMigrations(t, &vtParams)
onlineddl.CheckThrottledApps(t, &vtParams, throttlerapp.OnlineDDLName, false)
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
})
t.Run("throttled and unthrottled migration via vstreamer", func(t *testing.T) {
insertRows(t, 2)
var uuid string
func() {
_, err := throttler.ThrottleAppAndWaitUntilTabletsConfirm(t, clusterInstance, throttlerapp.VStreamerName)
defer throttler.UnthrottleAppAndWaitUntilTabletsConfirm(t, clusterInstance, throttlerapp.VStreamerName)
require.NoError(t, err)
uuid = testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess", providedUUID, providedMigrationContext, "vtgate", "test_val", "", true)
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusRunning)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
testRows(t)
// gotta give the migration a few seconds to read throttling info from _vt.vreplication and write
// to _vt.schema_migrations
row, startedTimestamp, lastThrottledTimestamp := onlineddl.WaitForThrottledTimestamp(t, &vtParams, uuid, normalMigrationWait)
require.NotNil(t, row)
startedTime, err := time.Parse(sqltypes.TimestampFormat, startedTimestamp)
require.NoError(t, err)
lastThrottledTime, err := time.Parse(sqltypes.TimestampFormat, lastThrottledTimestamp)
require.NoError(t, err)
// rowstreamer throttle timestamp only updates once in 10 seconds, so greater or equals" is good enough here.
// Technically, lastThrottledTime has to be >= startedTime, but we allow a deviation of 1 sec due to
// clock irregularities
assert.GreaterOrEqual(t, lastThrottledTime.Add(time.Second), startedTime)
component := row.AsString("component_throttled", "")
assert.Contains(t, []string{throttlerapp.VStreamerName.String(), throttlerapp.RowStreamerName.String()}, component)
}()
// now unthrottled
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
})
t.Run("failed migration", func(t *testing.T) {
insertRows(t, 2)
uuid := testOnlineDDLStatement(t, alterTableFailedStatement, "online", providedUUID, providedMigrationContext, "vtgate", "vrepl_col", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusFailed)
testRows(t)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, true)
onlineddl.CheckMigrationArtifacts(t, &vtParams, shards, uuid, true)
// migration will fail again
})
t.Run("failed migration due to vreplication", func(t *testing.T) {
insertRows(t, 2)
uuid := testOnlineDDLStatement(t, alterTableFailedVreplicationStatement, "online", providedUUID, providedMigrationContext, "vtgate", "vrepl_col", "", false)
onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusFailed)
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
message := row["message"].ToString()
assert.Contains(t, message, "vreplication: terminal error:", "migration row: %v", row)
}
})
t.Run("cancel all migrations: nothing to cancel", func(t *testing.T) {
// no migrations pending at this time
time.Sleep(10 * time.Second)
onlineddl.CheckCancelAllMigrations(t, &vtParams, 0)
// Validate that invoking CANCEL ALL via vtctl works
onlineddl.CheckCancelAllMigrationsViaVtctld(t, &clusterInstance.VtctldClientProcess, keyspaceName)
})
t.Run("cancel all migrations: some migrations to cancel", func(t *testing.T) {
// Use VTGate for throttling, issue a `ALTER VITESS_MIGRATION THROTTLE ALL ...`
onlineddl.ThrottleAllMigrations(t, &vtParams)
defer onlineddl.UnthrottleAllMigrations(t, &vtParams)
onlineddl.CheckThrottledApps(t, &vtParams, throttlerapp.OnlineDDLName, true)
// spawn n migrations; cancel them via cancel-all
var wg sync.WaitGroup
count := 4
for range count {
wg.Go(func() {
_ = testOnlineDDLStatement(t, alterTableThrottlingStatement, "vitess", providedUUID, providedMigrationContext, "vtgate", "vrepl_col", "", false)
})
}
wg.Wait()
onlineddl.CheckCancelAllMigrations(t, &vtParams, len(shards)*count)
})
t.Run("cancel all migrations: some migrations to cancel via vtctl", func(t *testing.T) {
// Use VTGate for throttling, issue a `ALTER VITESS_MIGRATION THROTTLE ALL ...`
onlineddl.ThrottleAllMigrations(t, &vtParams)
defer onlineddl.UnthrottleAllMigrations(t, &vtParams)
onlineddl.CheckThrottledApps(t, &vtParams, throttlerapp.OnlineDDLName, true)
// spawn n migrations; cancel them via cancel-all
var wg sync.WaitGroup
count := 4
for range count {
wg.Go(func() {
_ = testOnlineDDLStatement(t, alterTableThrottlingStatement, "online", providedUUID, providedMigrationContext, "vtgate", "vrepl_col", "", false)
})
}
wg.Wait()
// cancelling via vtctl does not return values. We CANCEL ALL via vtctl, then validate via VTGate that nothing remains to be cancelled.
onlineddl.CheckCancelAllMigrationsViaVtctld(t, &clusterInstance.VtctldClientProcess, keyspaceName)
onlineddl.CheckCancelAllMigrations(t, &vtParams, 0)
})
// reparent shard -80 to replica
// and then reparent it back to original state
// (two pretty much identical tests, the point is to end up with original state)
for _, currentPrimaryTabletIndex := range []int{0, 1} {
currentPrimaryTablet := shards[0].Vttablets[currentPrimaryTabletIndex]
reparentTablet := shards[0].Vttablets[1-currentPrimaryTabletIndex]
t.Run(fmt.Sprintf("PlannedReparentShard via throttling %d/2", (currentPrimaryTabletIndex+1)), func(t *testing.T) {
insertRows(t, 2)
_, err = throttler.ThrottleAppAndWaitUntilTabletsConfirm(t, clusterInstance, throttlerapp.OnlineDDLName)
assert.NoError(t, err)
defer throttler.UnthrottleAppAndWaitUntilTabletsConfirm(t, clusterInstance, throttlerapp.OnlineDDLName)
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess", providedUUID, providedMigrationContext, "vtgate", "test_val", "", true)
t.Run("wait for migration to run", func(t *testing.T) {
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusRunning)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
})
t.Run("wait for vreplication to run on shard -80", func(t *testing.T) {
vreplStatus := onlineddl.WaitForVReplicationStatus(t, &vtParams, currentPrimaryTablet, uuid, normalMigrationWait, binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String())
require.Contains(t, []string{binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String()}, vreplStatus)
})
t.Run("wait for vreplication to run on shard 80-", func(t *testing.T) {
vreplStatus := onlineddl.WaitForVReplicationStatus(t, &vtParams, shards[1].Vttablets[0], uuid, normalMigrationWait, binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String())
require.Contains(t, []string{binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String()}, vreplStatus)
})
t.Run("check status again", func(t *testing.T) {
// again see that we're still 'running'
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
testRows(t)
})
t.Run("Check tablet", func(t *testing.T) {
// onlineddl.Executor marks this migration with its tablet alias
// reminder that onlineddl.Executor runs on the primary tablet.
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
shard := row["shard"].ToString()
tablet := row["tablet"].ToString()
switch shard {
case "-80":
require.Equal(t, currentPrimaryTablet.Alias, tablet)
case "80-":
require.Equal(t, shards[1].Vttablets[0].Alias, tablet)
default:
require.NoError(t, fmt.Errorf("unexpected shard name: %s", shard))
}
}
})
t.Run("PRS shard -80", func(t *testing.T) {
// migration has started and is throttled. We now run PRS
err := clusterInstance.VtctldClientProcess.ExecuteCommand("PlannedReparentShard", keyspaceName+"/-80", "--new-primary", reparentTablet.Alias)
require.NoError(t, err, "failed PRS: %v", err)
rs := onlineddl.VtgateExecQuery(t, &vtParams, "show vitess_tablets", "")
onlineddl.PrintQueryResult(os.Stdout, rs)
})
t.Run("unthrottle", func(t *testing.T) {
_, err = throttler.UnthrottleAppAndWaitUntilTabletsConfirm(t, clusterInstance, throttlerapp.OnlineDDLName)
assert.NoError(t, err)
})
t.Run("expect completion", func(t *testing.T) {
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
})
t.Run("Check tablet post PRS", func(t *testing.T) {
// onlineddl.Executor will find that a vrepl migration started in a different tablet.
// it will own the tablet and will update 'tablet' column in _vt.schema_migrations with its own
// (promoted primary) tablet alias.
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
shard := row["shard"].ToString()
tablet := row["tablet"].ToString()
switch shard {
case "-80":
// PRS for this tablet, we promoted tablet[1]
require.Equal(t, reparentTablet.Alias, tablet)
case "80-":
// No PRS for this tablet
require.Equal(t, shards[1].Vttablets[0].Alias, tablet)
default:
require.NoError(t, fmt.Errorf("unexpected shard name: %s", shard))
}
}
onlineddl.CheckRetryPartialMigration(t, &vtParams, uuid, 1)
// Now it should complete on the failed shard
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusComplete)
})
})
}
// reparent shard -80 to replica
// and then reparent it back to original state
// (two pretty much identical tests, the point is to end up with original state)
for _, currentPrimaryTabletIndex := range []int{0, 1} {
currentPrimaryTablet := shards[0].Vttablets[currentPrimaryTabletIndex]
reparentTablet := shards[0].Vttablets[1-currentPrimaryTabletIndex]
t.Run(fmt.Sprintf("PlannedReparentShard via postponed %d/2", (currentPrimaryTabletIndex+1)), func(t *testing.T) {
insertRows(t, 2)
uuid := testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess --postpone-completion", providedUUID, providedMigrationContext, "vtgate", "test_val", "", true)
t.Run("wait for migration to run", func(t *testing.T) {
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusRunning)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
})
t.Run("wait for vreplication to run on shard -80", func(t *testing.T) {
vreplStatus := onlineddl.WaitForVReplicationStatus(t, &vtParams, currentPrimaryTablet, uuid, normalMigrationWait, binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String())
require.Contains(t, []string{binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String()}, vreplStatus)
})
t.Run("wait for vreplication to run on shard 80-", func(t *testing.T) {
vreplStatus := onlineddl.WaitForVReplicationStatus(t, &vtParams, shards[1].Vttablets[0], uuid, normalMigrationWait, binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String())
require.Contains(t, []string{binlogdatapb.VReplicationWorkflowState_Copying.String(), binlogdatapb.VReplicationWorkflowState_Running.String()}, vreplStatus)
})
t.Run("check status again", func(t *testing.T) {
// again see that we're still 'running'
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusRunning)
testRows(t)
})
t.Run("Check tablet", func(t *testing.T) {
// onlineddl.Executor marks this migration with its tablet alias
// reminder that onlineddl.Executor runs on the primary tablet.
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
shard := row["shard"].ToString()
tablet := row["tablet"].ToString()
switch shard {
case "-80":
require.Equal(t, currentPrimaryTablet.Alias, tablet)
case "80-":
require.Equal(t, shards[1].Vttablets[0].Alias, tablet)
default:
require.NoError(t, fmt.Errorf("unexpected shard name: %s", shard))
}
}
})
t.Run("PRS shard -80", func(t *testing.T) {
// migration has started and completion is postponed. We now PRS
err := clusterInstance.VtctldClientProcess.ExecuteCommand("PlannedReparentShard", keyspaceName+"/-80", "--new-primary", reparentTablet.Alias)
require.NoError(t, err, "failed PRS: %v", err)
rs := onlineddl.VtgateExecQuery(t, &vtParams, "show vitess_tablets", "")
onlineddl.PrintQueryResult(os.Stdout, rs)
})
t.Run("complete and expect completion", func(t *testing.T) {
query := fmt.Sprintf("select * from _vt.vreplication where workflow ='%s'", uuid)
rs, err := reparentTablet.VttabletProcess.QueryTablet(query, "", true)
assert.NoError(t, err)
onlineddl.PrintQueryResult(os.Stdout, rs)
onlineddl.CheckCompleteAllMigrations(t, &vtParams, len(shards))
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
})
t.Run("Check tablet post PRS", func(t *testing.T) {
// onlineddl.Executor will find that a vrepl migration started in a different tablet.
// it will own the tablet and will update 'tablet' column in _vt.schema_migrations with its own
// (promoted primary) tablet alias.
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
shard := row["shard"].ToString()
tablet := row["tablet"].ToString()
switch shard {
case "-80":
// PRS for this tablet
require.Equal(t, reparentTablet.Alias, tablet)
case "80-":
// No PRS for this tablet
require.Equal(t, shards[1].Vttablets[0].Alias, tablet)
default:
require.NoError(t, fmt.Errorf("unexpected shard name: %s", shard))
}
}
onlineddl.CheckRetryPartialMigration(t, &vtParams, uuid, 1)
// Now it should complete on the failed shard
_ = onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusComplete)
})
})
}
t.Run("Online DROP, vtctl", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLDropTableStatement, "online", providedUUID, providedMigrationContext, "vtctl", "", "", false)
t.Run("test ready to complete", func(t *testing.T) {
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
readyToComplete := row.AsInt64("ready_to_complete", 0)
assert.Equal(t, int64(1), readyToComplete)
}
})
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
})
t.Run("Online CREATE, vtctl", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLCreateTableStatement, "vitess", providedUUID, providedMigrationContext, "vtctl", "online_ddl_create_col", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
})
t.Run("Online DROP TABLE IF EXISTS, vtgate", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLDropTableIfExistsStatement, "online ", providedUUID, providedMigrationContext, "vtgate", "", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
// this table existed
checkTables(t, schema.OnlineDDLToGCUUID(uuid), 1)
})
t.Run("Online CREATE, vtctl, extra flags", func(t *testing.T) {
// the flags are meaningless to this migration. The test just validates that they don't get in the way.
uuid := testOnlineDDLStatement(t, onlineDDLCreateTableStatement, "vitess --prefer-instant-ddl --allow-zero-in-date", providedUUID, providedMigrationContext, "vtctl", "online_ddl_create_col", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
})
t.Run("Online DROP TABLE IF EXISTS, vtgate, extra flags", func(t *testing.T) {
// the flags are meaningless to this migration. The test just validates that they don't get in the way.
uuid := testOnlineDDLStatement(t, onlineDDLDropTableIfExistsStatement, "vitess --prefer-instant-ddl --allow-zero-in-date", providedUUID, providedMigrationContext, "vtgate", "", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
// this table existed
checkTables(t, schema.OnlineDDLToGCUUID(uuid), 1)
})
t.Run("Online DROP TABLE IF EXISTS for nonexistent table, vtgate", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLDropTableIfExistsStatement, "online", providedUUID, providedMigrationContext, "vtgate", "", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
// this table did not exist
checkTables(t, schema.OnlineDDLToGCUUID(uuid), 0)
})
t.Run("Online DROP TABLE IF EXISTS for nonexistent table, postponed", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLDropTableIfExistsStatement, "vitess -postpone-completion", providedUUID, providedMigrationContext, "vtgate", "", "", false)
// Should be still queued, never promoted to 'ready'!
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusQueued)
// Issue a complete and wait for successful completion
onlineddl.CheckCompleteMigration(t, &vtParams, shards, uuid, true)
// This part may take a while, because we depend on vreplicatoin polling
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, extendedMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
// this table did not exist
checkTables(t, schema.OnlineDDLToGCUUID(uuid), 0)
})
t.Run("Online DROP TABLE for nonexistent table, expect error, vtgate", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLDropTableStatement, "online", providedUUID, providedMigrationContext, "vtgate", "", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusFailed)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, true)
})
t.Run("Online CREATE, vtctl", func(t *testing.T) {
uuid := testOnlineDDLStatement(t, onlineDDLCreateTableStatement, "vitess", providedUUID, providedMigrationContext, "vtctl", "online_ddl_create_col", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
onlineddl.CheckCancelMigration(t, &vtParams, shards, uuid, false)
onlineddl.CheckRetryMigration(t, &vtParams, shards, uuid, false)
})
// Technically the next test should belong in onlineddl_revert suite. But we're tking advantage of setup and functionality existing in this tets:
// - two shards as opposed to one
// - tablet throttling
t.Run("Revert a migration completed on one shard and cancelled on another", func(t *testing.T) {
// shard 0 will run normally, shard 1 will be postponed
var uuid string
t.Run("run migrations, expect running on both shards", func(t *testing.T) {
uuid = testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess --postpone-launch", providedUUID, providedMigrationContext, "vtgate", "test_val", "", true)
onlineddl.CheckLaunchMigration(t, &vtParams, shards[0:1], uuid, "-80", true)
{
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards[:1], uuid, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards[:1], uuid, schema.OnlineDDLStatusComplete)
}
{
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards[1:], uuid, normalMigrationWait, schema.OnlineDDLStatusQueued)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards[1:], uuid, schema.OnlineDDLStatusQueued)
}
})
t.Run("check cancel migration", func(t *testing.T) {
onlineddl.CheckCancelAllMigrations(t, &vtParams, 1)
})
t.Run("launch-all", func(t *testing.T) {
onlineddl.CheckLaunchAllMigrations(t, &vtParams, 0)
})
var revertUUID string
t.Run("issue revert migration", func(t *testing.T) {
revertQuery := fmt.Sprintf("revert vitess_migration '%s'", uuid)
rs := onlineddl.VtgateExecQuery(t, &vtParams, revertQuery, "")
require.NotNil(t, rs)
row := rs.Named().Row()
require.NotNil(t, row)
revertUUID = row.AsString("uuid", "")
assert.NotEmpty(t, revertUUID)
})
t.Run("migrations were cancelled, revert should impossible", func(t *testing.T) {
{
// shard 0 migration was complete. Revert should be successful
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards[:1], revertUUID, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards[:1], revertUUID, schema.OnlineDDLStatusFailed)
}
{
// shard 0 migration was cancelled. Revert should not be possible
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards[1:], revertUUID, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards[1:], revertUUID, schema.OnlineDDLStatusFailed)
}
})
t.Run("expect two rows in SHOW VITESS_MIGRATIONS", func(t *testing.T) {
// This validates that the shards are reflected correctly in output of SHOW VITESS_MIGRATIONS
rs := onlineddl.ReadMigrations(t, &vtParams, revertUUID)
require.NotNil(t, rs)
require.Equal(t, 2, len(rs.Rows))
for _, row := range rs.Named().Rows {
shard := row["shard"].ToString()
status := row["migration_status"].ToString()
switch shard {
case "-80":
require.Equal(t, string(schema.OnlineDDLStatusComplete), status)
case "80-":
require.Equal(t, string(schema.OnlineDDLStatusFailed), status)
default:
require.NoError(t, fmt.Errorf("unexpected shard name: %s", shard))
}
}
})
})
t.Run("Revert a migration completed on both shards", func(t *testing.T) {
var uuid string
t.Run("run migration, expect completion on both shards", func(t *testing.T) {
uuid = testOnlineDDLStatement(t, alterTableTrivialStatement, "vitess", providedUUID, providedMigrationContext, "vtgate", "test_val", "", false)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, uuid, schema.OnlineDDLStatusComplete)
})
var revertUUID string
t.Run("issue revert migration", func(t *testing.T) {
revertQuery := fmt.Sprintf("revert vitess_migration '%s'", uuid)
output, err := clusterInstance.VtctldClientProcess.ApplySchemaWithOutput(keyspaceName, revertQuery, cluster.ApplySchemaParams{DDLStrategy: "vitess"})
require.NoError(t, err)
revertUUID = strings.TrimSpace(output)
assert.NotEmpty(t, revertUUID)
})
t.Run("revert completes on both shards", func(t *testing.T) {
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, revertUUID, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, revertUUID, schema.OnlineDDLStatusComplete)
})
t.Run("validate both shards show complete in SHOW VITESS_MIGRATIONS", func(t *testing.T) {
rs := onlineddl.ReadMigrations(t, &vtParams, revertUUID)
require.NotNil(t, rs)
require.Equal(t, 2, len(rs.Rows))
for _, row := range rs.Named().Rows {
status := row["migration_status"].ToString()
assert.Equal(t, string(schema.OnlineDDLStatusComplete), status, "shard %s", row["shard"].ToString())
}
})
})
t.Run("summary: validate sequential migration IDs", func(t *testing.T) {
onlineddl.ValidateSequentialMigrationIDs(t, &vtParams, shards)
})
t.Run("summary: validate completed_timestamp", func(t *testing.T) {
onlineddl.ValidateCompletedTimestamp(t, &vtParams)
})
}
func insertRow(t *testing.T) {
insertMutex.Lock()
defer insertMutex.Unlock()
tableName := fmt.Sprintf("vt_onlineddl_test_%02d", 3)
sqlQuery := fmt.Sprintf(insertRowStatement, tableName, countInserts)
r := onlineddl.VtgateExecQuery(t, &vtParams, sqlQuery, "")
require.NotNil(t, r)
countInserts++
}
func insertRows(t *testing.T, count int) {
for range count {
insertRow(t)
}
}
func testRows(t *testing.T) {
insertMutex.Lock()
defer insertMutex.Unlock()
tableName := fmt.Sprintf("vt_onlineddl_test_%02d", 3)
sqlQuery := fmt.Sprintf(selectCountRowsStatement, tableName)
r := onlineddl.VtgateExecQuery(t, &vtParams, sqlQuery, "")
require.NotNil(t, r)
row := r.Named().Row()
require.NotNil(t, row)
require.Equal(t, countInserts, row.AsInt64("c", 0))
}
func testMigrationRowCount(t *testing.T, uuid string) {
insertMutex.Lock()
defer insertMutex.Unlock()
var totalRowsCopied uint64
// count sum of rows copied in all shards, that should be the total number of rows inserted to the table
rs := onlineddl.ReadMigrations(t, &vtParams, uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
rowsCopied := row.AsUint64("rows_copied", 0)
totalRowsCopied += rowsCopied
}
require.Equal(t, uint64(countInserts), totalRowsCopied)
}
func testWithInitialSchema(t *testing.T) {
// Create 4 tables
sqlQuery := ""
for i := range totalTableCount {
sqlQuery = fmt.Sprintf(createTable, fmt.Sprintf("vt_onlineddl_test_%02d", i))
err := clusterInstance.VtctldClientProcess.ApplySchema(keyspaceName, sqlQuery)
require.Nil(t, err)
}
// Check if 4 tables are created
checkTables(t, "", totalTableCount)
}
// testOnlineDDLStatement runs an online DDL, ALTER statement
func testOnlineDDLStatement(t *testing.T, alterStatement string, ddlStrategy string, providedUUIDList string, providedMigrationContext string, executeStrategy string, expectHint string, expectError string, skipWait bool) (uuid string) {
tableName := fmt.Sprintf("vt_onlineddl_test_%02d", 3)
sqlQuery := fmt.Sprintf(alterStatement, tableName)
if executeStrategy == "vtgate" {
row := onlineddl.VtgateExecDDL(t, &vtParams, ddlStrategy, sqlQuery, "").Named().Row()
if row != nil {
uuid = row.AsString("uuid", "")
}
} else {
params := cluster.ApplySchemaParams{DDLStrategy: ddlStrategy, UUIDs: providedUUIDList, MigrationContext: providedMigrationContext}
output, err := clusterInstance.VtctldClientProcess.ApplySchemaWithOutput(keyspaceName, sqlQuery, params)
if expectError == "" {
assert.NoError(t, err)
uuid = output
} else {
assert.Error(t, err)
assert.Contains(t, output, expectError)
}
}
uuid = strings.TrimSpace(uuid)
fmt.Println("# Generated UUID (for debug purposes):")
fmt.Printf("<%s>\n", uuid)
strategySetting, err := schema.ParseDDLStrategy(ddlStrategy)
assert.NoError(t, err)
if strategySetting.Strategy.IsDirect() {
skipWait = true
}
if !skipWait {
status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, uuid, normalMigrationWait, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
}
if expectError == "" && expectHint != "" {
checkMigratedTable(t, tableName, expectHint)
}
return uuid
}
// checkTables checks the number of tables in the first two shards.
func checkTables(t *testing.T, showTableName string, expectCount int) {
for i := range clusterInstance.Keyspaces[0].Shards {
checkTablesCount(t, clusterInstance.Keyspaces[0].Shards[i].Vttablets[0], showTableName, expectCount)
}