-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathvtgate_util.go
More file actions
628 lines (538 loc) · 22.8 KB
/
vtgate_util.go
File metadata and controls
628 lines (538 loc) · 22.8 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
/*
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 onlineddl
import (
"context"
"fmt"
"os"
"testing"
"time"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/schema"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vttablet/tabletserver/throttle/throttlerapp"
"vitess.io/vitess/go/test/endtoend/cluster"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
ThrottledAppsTimeout = 60 * time.Second
)
var testsStartupTime time.Time
func init() {
testsStartupTime = time.Now()
}
// VtgateExecQuery runs a query on VTGate using given query params
func VtgateExecQuery(t *testing.T, vtParams *mysql.ConnParams, query string, expectError string) *sqltypes.Result {
t.Helper()
ctx := t.Context()
conn, err := mysql.Connect(ctx, vtParams)
require.Nil(t, err)
defer conn.Close()
qr, err := conn.ExecuteFetch(query, -1, true)
if expectError == "" {
require.NoError(t, err)
} else {
require.Error(t, err, "error should not be nil")
assert.Contains(t, err.Error(), expectError, "Unexpected error")
}
return qr
}
// VtgateExecQueryInTransaction runs a query on VTGate using given query params, inside a transaction
func VtgateExecQueryInTransaction(t *testing.T, vtParams *mysql.ConnParams, query string, expectError string) *sqltypes.Result {
t.Helper()
ctx := t.Context()
conn, err := mysql.Connect(ctx, vtParams)
require.Nil(t, err)
defer conn.Close()
_, err = conn.ExecuteFetch("begin", -1, true)
require.NoError(t, err)
qr, err := conn.ExecuteFetch(query, -1, true)
if expectError == "" {
require.NoError(t, err)
} else {
require.Error(t, err, "error should not be nil")
assert.Contains(t, err.Error(), expectError, "Unexpected error")
}
_, err = conn.ExecuteFetch("commit", -1, true)
require.NoError(t, err)
return qr
}
// VtgateExecDDL executes a DDL query with given strategy
func VtgateExecDDL(t *testing.T, vtParams *mysql.ConnParams, ddlStrategy string, query string, expectError string) *sqltypes.Result {
t.Helper()
ctx := t.Context()
conn, err := mysql.Connect(ctx, vtParams)
require.Nil(t, err)
defer conn.Close()
setSession := fmt.Sprintf("set @@ddl_strategy='%s'", ddlStrategy)
_, err = conn.ExecuteFetch(setSession, 1000, true)
assert.NoError(t, err)
qr, err := conn.ExecuteFetch(query, 1000, true)
if expectError == "" {
require.NoError(t, err)
} else {
require.Error(t, err, "error should not be nil")
assert.Contains(t, err.Error(), expectError, "Unexpected error")
}
return qr
}
// CheckRetryMigration attempts to retry a migration, and expects success/failure by counting affected rows
func CheckRetryMigration(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectRetryPossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a retry",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectRetryPossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckRetryPartialMigration attempts to retry a migration where a subset of shards failed
func CheckRetryPartialMigration(t *testing.T, vtParams *mysql.ConnParams, uuid string, expectAtLeastRowsAffected uint64) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a retry",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
assert.GreaterOrEqual(t, expectAtLeastRowsAffected, r.RowsAffected)
}
// CheckCancelMigration attempts to cancel a migration, and expects success/failure by counting affected rows
func CheckCancelMigration(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectCancelPossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a cancel",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCancelPossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckCleanupMigration attempts to cleanup a migration, and expects success by counting affected rows
func CheckCleanupMigration(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a cleanup",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
assert.Equal(t, len(shards), int(r.RowsAffected))
}
// CheckCompleteMigration attempts to complete a migration, and expects success by counting affected rows
func CheckCompleteMigration(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectCompletePossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a complete",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCompletePossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckCompleteMigrationShards attempts to complete a migration for specific shards, and expects success by counting affected rows
func CheckCompleteMigrationShards(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, completeShards string, expectCompletePossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a complete vitess_shards %a",
sqltypes.StringBindVariable(uuid),
sqltypes.StringBindVariable(completeShards),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCompletePossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckPostponeCompleteMigration attempts to postpone an existing migration, and expects success by counting affected rows
func CheckPostponeCompleteMigration(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectPotponePossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a postpone complete",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectPotponePossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckLaunchMigration attempts to launch a migration, and expects success by counting affected rows
func CheckLaunchMigration(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, launchShards string, expectLaunchPossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a launch vitess_shards %a",
sqltypes.StringBindVariable(uuid),
sqltypes.StringBindVariable(launchShards),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectLaunchPossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckCompleteContextMigrations completes all pending migrations with a given context and expects number of affected rows.
// A negative value for expectCount indicates "don't care, no need to check"
func CheckCompleteContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) {
query := fmt.Sprintf("alter vitess_migration complete context '%s'", migrationContext)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckCompleteAllMigrations completes all pending migrations and expect number of affected rows
// A negative value for expectCount indicates "don't care, no need to check"
func CheckCompleteAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCount int) {
completeQuery := "alter vitess_migration complete all"
r := VtgateExecQuery(t, vtParams, completeQuery, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckPostponeCompleteContextMigrations postpones completion of all pending migrations with a given context and expects number of affected rows.
// A negative value for expectCount indicates "don't care, no need to check"
func CheckPostponeCompleteContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) {
query := fmt.Sprintf("alter vitess_migration postpone complete context '%s'", migrationContext)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckPostponeCompleteAllMigrations postpones all pending migrations and expect number of affected rows
// A negative value for expectCount indicates "don't care, no need to check"
func CheckPostponeCompleteAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCount int) {
completeQuery := "alter vitess_migration postpone complete all"
r := VtgateExecQuery(t, vtParams, completeQuery, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckCancelAllMigrations cancels all pending migrations and expect number of affected rows
// A negative value for expectCount indicates "don't care, no need to check"
func CheckCancelAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCount int) {
cancelQuery := "alter vitess_migration cancel all"
r := VtgateExecQuery(t, vtParams, cancelQuery, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckCancelContextMigrations cancels all pending migrations with a given context and expect number of affected rows
// A negative value for expectCount indicates "don't care, no need to check"
func CheckCancelContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) {
cancelQuery := fmt.Sprintf("alter vitess_migration cancel context '%s'", migrationContext)
r := VtgateExecQuery(t, vtParams, cancelQuery, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckCleanupContextMigrations cleans up terminal migrations with a given context and expects number of affected rows.
// A negative value for expectCount indicates "don't care, no need to check"
func CheckCleanupContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) uint64 {
query := fmt.Sprintf("alter vitess_migration cleanup context '%s'", migrationContext)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
return r.RowsAffected
}
// CheckCleanupAllMigrations cleans up all applicable migrations and expect number of affected rows
// A negative value for expectCount indicates "don't care, no need to check"
func CheckCleanupAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCount int) uint64 {
cleanupQuery := "alter vitess_migration cleanup all"
r := VtgateExecQuery(t, vtParams, cleanupQuery, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
return r.RowsAffected
}
// CheckLaunchContextMigrations launches all queued postponed migrations with a given context and expects number of affected rows.
// A negative value for expectCount indicates "don't care, no need to check"
func CheckLaunchContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) {
query := fmt.Sprintf("alter vitess_migration launch context '%s'", migrationContext)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckLaunchAllMigrations launches all queued posponed migrations and expect number of affected rows
// A negative value for expectCount indicates "don't care, no need to check"
func CheckLaunchAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCount int) {
completeQuery := "alter vitess_migration launch all"
r := VtgateExecQuery(t, vtParams, completeQuery, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckForceCutOverContextMigrations marks all pending migrations with a given context for forced cut-over and expects number of affected rows.
// A negative value for expectCount indicates "don't care, no need to check"
func CheckForceCutOverContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) {
query := fmt.Sprintf("alter vitess_migration force_cutover context '%s'", migrationContext)
r := VtgateExecQuery(t, vtParams, query, "")
if expectCount >= 0 {
assert.Equal(t, expectCount, int(r.RowsAffected))
}
}
// CheckForceMigrationCutOver marks a migration for forced cut-over, and expects success by counting affected rows.
func CheckForceMigrationCutOver(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectPossible bool) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a force_cutover",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
if expectPossible {
assert.Equal(t, len(shards), int(r.RowsAffected))
} else {
assert.Equal(t, int(0), int(r.RowsAffected))
}
}
// CheckSetMigrationCutOverThreshold sets the cut-over threshold for a given migration.
func CheckSetMigrationCutOverThreshold(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, threshold time.Duration, expectError string) {
query, err := sqlparser.ParseAndBind("alter vitess_migration %a cutover_threshold %a",
sqltypes.StringBindVariable(uuid),
sqltypes.StringBindVariable(threshold.String()),
)
require.NoError(t, err)
_ = VtgateExecQuery(t, vtParams, query, expectError)
}
// CheckMigrationStatus verifies that the migration indicated by given UUID has the given expected status
func CheckMigrationStatus(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectStatuses ...schema.OnlineDDLStatus) bool {
ksName := shards[0].PrimaryTablet().VttabletProcess.Keyspace
query, err := sqlparser.ParseAndBind(fmt.Sprintf("show vitess_migrations from %s like %%a", ksName),
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
fmt.Printf("# output for `%s`:\n", query)
PrintQueryResult(os.Stdout, r)
count := 0
for _, row := range r.Named().Rows {
if row["migration_uuid"].ToString() != uuid {
continue
}
for _, expectStatus := range expectStatuses {
if row["migration_status"].ToString() == string(expectStatus) {
count++
break
}
}
}
return assert.Equal(t, len(shards), count)
}
// WaitForMigrationStatus waits for a migration to reach either provided statuses (returns immediately), or eventually time out
func WaitForMigrationStatus(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, timeout time.Duration, expectStatuses ...schema.OnlineDDLStatus) schema.OnlineDDLStatus {
shardNames := map[string]bool{}
for _, shard := range shards {
shardNames[shard.Name] = true
}
query, err := sqlparser.ParseAndBind("show vitess_migrations like %a",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
statusesMap := map[string]bool{}
for _, status := range expectStatuses {
statusesMap[string(status)] = true
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
lastKnownStatus := ""
for {
countMatchedShards := 0
r := VtgateExecQuery(t, vtParams, query, "")
for _, row := range r.Named().Rows {
shardName := row["shard"].ToString()
if !shardNames[shardName] {
// irrelevant shard
continue
}
lastKnownStatus = row["migration_status"].ToString()
if row["migration_uuid"].ToString() == uuid && statusesMap[lastKnownStatus] {
countMatchedShards++
}
}
if countMatchedShards == len(shards) {
return schema.OnlineDDLStatus(lastKnownStatus)
}
select {
case <-ctx.Done():
return schema.OnlineDDLStatus(lastKnownStatus)
case <-ticker.C:
}
}
}
// CheckMigrationArtifacts verifies given migration exists, and checks if it has artifacts
func CheckMigrationArtifacts(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard, uuid string, expectArtifacts bool) {
r := ReadMigrations(t, vtParams, uuid)
assert.Equal(t, len(shards), len(r.Named().Rows))
for _, row := range r.Named().Rows {
hasArtifacts := (row["artifacts"].ToString() != "")
assert.Equal(t, expectArtifacts, hasArtifacts)
}
}
// ReadMigrations reads migration entries
func ReadMigrations(t *testing.T, vtParams *mysql.ConnParams, like string) *sqltypes.Result {
query, err := sqlparser.ParseAndBind("show vitess_migrations like %a",
sqltypes.StringBindVariable(like),
)
require.NoError(t, err)
return VtgateExecQuery(t, vtParams, query, "")
}
// ReadMigrationLogs reads migration logs for a given migration, on all shards
func ReadMigrationLogs(t *testing.T, vtParams *mysql.ConnParams, uuid string) (logs []string) {
query, err := sqlparser.ParseAndBind("show vitess_migration %a logs",
sqltypes.StringBindVariable(uuid),
)
require.NoError(t, err)
r := VtgateExecQuery(t, vtParams, query, "")
for _, row := range r.Named().Rows {
migrationLog := row["migration_log"].ToString()
logs = append(logs, migrationLog)
}
return logs
}
// ThrottleAllMigrations fully throttles online-ddl apps
func ThrottleAllMigrations(t *testing.T, vtParams *mysql.ConnParams) {
query := "alter vitess_migration throttle all expire '24h' ratio 1"
_ = VtgateExecQuery(t, vtParams, query, "")
}
// ThrottleContextMigrations throttles all pending migrations with a given context.
func ThrottleContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string) {
query := fmt.Sprintf("alter vitess_migration throttle context '%s' expire '24h' ratio 1", migrationContext)
_ = VtgateExecQuery(t, vtParams, query, "")
}
// UnthrottleAllMigrations cancels migration throttling
func UnthrottleAllMigrations(t *testing.T, vtParams *mysql.ConnParams) {
query := "alter vitess_migration unthrottle all"
_ = VtgateExecQuery(t, vtParams, query, "")
}
// UnthrottleContextMigrations unthrottles all pending migrations with a given context.
func UnthrottleContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string) {
query := fmt.Sprintf("alter vitess_migration unthrottle context '%s'", migrationContext)
_ = VtgateExecQuery(t, vtParams, query, "")
}
// CheckThrottledApps checks for existence or non-existence of an app in the throttled apps list
func CheckThrottledApps(t *testing.T, vtParams *mysql.ConnParams, throttlerApp throttlerapp.Name, expectFind bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), ThrottledAppsTimeout)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
query := "show vitess_throttled_apps"
r := VtgateExecQuery(t, vtParams, query, "")
appFound := false
for _, row := range r.Named().Rows {
if throttlerApp.Equals(row.AsString("app", "")) {
appFound = true
}
}
if appFound == expectFind {
// we're all good
return true
}
select {
case <-ctx.Done():
assert.Fail(t, "CheckThrottledApps timed out", "waiting for '%v' to be in throttled status '%v'", throttlerApp.String(), expectFind)
return false
case <-ticker.C:
}
}
}
// WaitForThrottledTimestamp waits for a migration to have a non-empty last_throttled_timestamp
func WaitForThrottledTimestamp(t *testing.T, vtParams *mysql.ConnParams, uuid string, timeout time.Duration) (
row sqltypes.RowNamedValues,
startedTimestamp string,
lastThrottledTimestamp string,
) {
startTime := time.Now()
for time.Since(startTime) < timeout {
rs := ReadMigrations(t, vtParams, uuid)
require.NotNil(t, rs)
for _, row = range rs.Named().Rows {
startedTimestamp = row.AsString("started_timestamp", "")
require.NotEmpty(t, startedTimestamp)
lastThrottledTimestamp = row.AsString("last_throttled_timestamp", "")
if lastThrottledTimestamp != "" {
// good. This is what we've been waiting for.
return row, startedTimestamp, lastThrottledTimestamp
}
}
time.Sleep(1 * time.Second)
}
t.Error("timeout waiting for last_throttled_timestamp to have nonempty value")
return
}
// ValidateSequentialMigrationIDs validates that schem_migrations.id column, which is an AUTO_INCREMENT, does
// not have gaps
func ValidateSequentialMigrationIDs(t *testing.T, vtParams *mysql.ConnParams, shards []cluster.Shard) {
r := VtgateExecQuery(t, vtParams, "show vitess_migrations", "")
shardMin := map[string]uint64{}
shardMax := map[string]uint64{}
shardCount := map[string]uint64{}
for _, row := range r.Named().Rows {
id := row.AsUint64("id", 0)
require.NotZero(t, id)
shard := row.AsString("shard", "")
require.NotEmpty(t, shard)
if _, ok := shardMin[shard]; !ok {
shardMin[shard] = id
shardMax[shard] = id
}
if id < shardMin[shard] {
shardMin[shard] = id
}
if id > shardMax[shard] {
shardMax[shard] = id
}
shardCount[shard]++
}
require.NotEmpty(t, shards)
assert.Equal(t, len(shards), len(shardMin))
assert.Equal(t, len(shards), len(shardMax))
assert.Equal(t, len(shards), len(shardCount))
for shard, count := range shardCount {
assert.NotZero(t, count)
assert.Equalf(t, count, shardMax[shard]-shardMin[shard]+1, "mismatch: shared=%v, count=%v, min=%v, max=%v", shard, count, shardMin[shard], shardMax[shard])
}
}
// ValidateCompletedTimestamp ensures that any migration in `cancelled`, `completed`, `failed` statuses
// has a non-nil and valid `completed_timestamp` value.
func ValidateCompletedTimestamp(t *testing.T, vtParams *mysql.ConnParams) {
require.False(t, testsStartupTime.IsZero())
r := VtgateExecQuery(t, vtParams, "show vitess_migrations", "")
completedTimestampNumValidations := 0
for _, row := range r.Named().Rows {
migrationStatus := row.AsString("migration_status", "")
require.NotEmpty(t, migrationStatus)
switch migrationStatus {
case string(schema.OnlineDDLStatusComplete),
string(schema.OnlineDDLStatusFailed),
string(schema.OnlineDDLStatusCancelled):
{
assert.False(t, row["completed_timestamp"].IsNull())
// Also make sure the timestamp is "real", and that it is recent.
timestamp := row.AsString("completed_timestamp", "")
completedTime, err := time.Parse(sqltypes.TimestampFormat, timestamp)
assert.NoError(t, err)
assert.Greater(t, completedTime.Unix(), testsStartupTime.Unix())
completedTimestampNumValidations++
}
}
}
assert.NotZero(t, completedTimestampNumValidations)
}