Skip to content

Commit da94cc8

Browse files
shlomi-noachclaude
andauthored
onlineddl: ALTER VITESS_MIGRATION CANCEL CONTEXT 'ctx' (#20146)
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 50e632b commit da94cc8

13 files changed

Lines changed: 10523 additions & 10516 deletions

File tree

go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,25 @@ func testScheduler(t *testing.T) {
20422042
})
20432043
testTableCompletionTimes(t, t1uuid, v1uuid)
20442044
})
2045+
t.Run("cancel migrations by context", func(t *testing.T) {
2046+
// Submit two migrations with the same explicit context, both postponed so they stay running.
2047+
t1uuid = testOnlineDDLStatement(t, &testOnlineDDLStatementParams{ddlStatement: trivialAlterT1Statement, ddlStrategy: ddlStrategy + " --allow-concurrent --postpone-completion", executeStrategy: "vtctl", migrationContext: "ctx-cancel-by-context", skipWait: true})
2048+
t2uuid = testOnlineDDLStatement(t, &testOnlineDDLStatementParams{ddlStatement: trivialAlterT2Statement, ddlStrategy: ddlStrategy + " --allow-concurrent --postpone-completion", executeStrategy: "vtctl", migrationContext: "ctx-cancel-by-context", skipWait: true})
2049+
onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t1uuid, normalWaitTime, schema.OnlineDDLStatusRunning)
2050+
onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t2uuid, normalWaitTime, schema.OnlineDDLStatusRunning)
2051+
2052+
// A non-matching context cancels nothing.
2053+
onlineddl.CheckCancelContextMigrations(t, &vtParams, "ctx-cancel-by-context-other", 0)
2054+
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t1uuid, schema.OnlineDDLStatusRunning)
2055+
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t2uuid, schema.OnlineDDLStatusRunning)
2056+
2057+
// Cancel by context: both migrations must be cancelled.
2058+
onlineddl.CheckCancelContextMigrations(t, &vtParams, "ctx-cancel-by-context", 2)
2059+
onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t1uuid, normalWaitTime, schema.OnlineDDLStatusCancelled, schema.OnlineDDLStatusFailed)
2060+
onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t2uuid, normalWaitTime, schema.OnlineDDLStatusCancelled, schema.OnlineDDLStatusFailed)
2061+
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t1uuid, schema.OnlineDDLStatusCancelled)
2062+
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t2uuid, schema.OnlineDDLStatusCancelled)
2063+
})
20452064
}
20462065

20472066
func testSingleton(t *testing.T) {

go/test/endtoend/onlineddl/vtgate_util.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ func CheckCancelAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCo
257257
}
258258
}
259259

260+
// CheckCancelContextMigrations cancels all pending migrations with a given context and expect number of affected rows
261+
// A negative value for expectCount indicates "don't care, no need to check"
262+
func CheckCancelContextMigrations(t *testing.T, vtParams *mysql.ConnParams, migrationContext string, expectCount int) {
263+
cancelQuery := fmt.Sprintf("alter vitess_migration cancel context '%s'", migrationContext)
264+
r := VtgateExecQuery(t, vtParams, cancelQuery, "")
265+
266+
if expectCount >= 0 {
267+
assert.Equal(t, expectCount, int(r.RowsAffected))
268+
}
269+
}
270+
260271
// CheckCleanupAllMigrations cleans up all applicable migrations and expect number of affected rows
261272
// A negative value for expectCount indicates "don't care, no need to check"
262273
func CheckCleanupAllMigrations(t *testing.T, vtParams *mysql.ConnParams, expectCount int) uint64 {

go/vt/sqlparser/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ type (
523523
AlterMigration struct {
524524
Type AlterMigrationType
525525
UUID string
526+
Context string
526527
Expire string
527528
Ratio *Literal
528529
Threshold string

go/vt/sqlparser/ast_equals.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/ast_format.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,11 @@ func (node *AlterMigration) Format(buf *TrackedBuffer) {
337337
case CancelMigrationType:
338338
alterType = "cancel"
339339
case CancelAllMigrationType:
340-
alterType = "cancel all"
340+
if node.Context != "" {
341+
alterType = "cancel"
342+
} else {
343+
alterType = "cancel all"
344+
}
341345
case ThrottleMigrationType:
342346
alterType = "throttle"
343347
case ThrottleAllMigrationType:
@@ -355,16 +359,19 @@ func (node *AlterMigration) Format(buf *TrackedBuffer) {
355359
}
356360
buf.astPrintf(node, " %#s", alterType)
357361
if node.Threshold != "" {
358-
buf.astPrintf(node, " '%#s'", node.Threshold)
362+
buf.astPrintf(node, " %#s", encodeSQLString(node.Threshold))
359363
}
360364
if node.Expire != "" {
361-
buf.astPrintf(node, " expire '%#s'", node.Expire)
365+
buf.astPrintf(node, " expire %#s", encodeSQLString(node.Expire))
362366
}
363367
if node.Ratio != nil {
364368
buf.astPrintf(node, " ratio %v", node.Ratio)
365369
}
370+
if node.Context != "" {
371+
buf.astPrintf(node, " context %#s", encodeSQLString(node.Context))
372+
}
366373
if node.Shards != "" {
367-
buf.astPrintf(node, " vitess_shards '%#s'", node.Shards)
374+
buf.astPrintf(node, " vitess_shards %#s", encodeSQLString(node.Shards))
368375
}
369376
}
370377

go/vt/sqlparser/ast_format_fast.go

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/cached_size.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/vt/sqlparser/keywords.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ var keywords = []keyword{
191191
{"constraint_catalog", CONSTRAINT_CATALOG},
192192
{"constraint_name", CONSTRAINT_NAME},
193193
{"constraint_schema", CONSTRAINT_SCHEMA},
194+
{"context", CONTEXT},
194195
{"continue", CONTINUE},
195196
{"convert", CONVERT},
196197
{"copy", COPY},

go/vt/sqlparser/parse_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,8 @@ var validSQL = []struct {
26892689
}, {
26902690
input: "alter vitess_migration '9748c3b7_7fdb_11eb_ac2c_f875a4d24e90' FORCE_CUTOVER",
26912691
output: "alter vitess_migration '9748c3b7_7fdb_11eb_ac2c_f875a4d24e90' force_cutover",
2692+
}, {
2693+
input: "alter vitess_migration cancel context 'some-context'",
26922694
}, {
26932695
input: "alter vitess_migration cancel all",
26942696
}, {
@@ -6488,6 +6490,9 @@ var invalidSQL = []struct {
64886490
input string
64896491
output string
64906492
}{{
6493+
input: "alter vitess_migration cancel context ''",
6494+
output: "migration context cannot be empty at position 41",
6495+
}, {
64916496
input: "select : from t",
64926497
output: "syntax error at position 9 near ':'",
64936498
}, {

0 commit comments

Comments
 (0)