Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnlineDDL: 'mysql' strategy, managed by the scheduler, but executed via normal MySQL statements #12027

Merged
merged 5 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/releasenotes/16_0_0_release_notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Release of Vitess v16.0.0
## Major Changes

### Online DDL

Introducing a new DDL strategy: `mysql`. This strategy is a hybrid between `direct` (which is completely non-Online) and the various online strategies.

A migration submitted with `mysql` strategy is _managed_. That is, it gets a migration UUID. The scheduler queues it, reviews it, runs it. The user may cancel or retry it, much like all Online DDL migrations. The difference is that when the scheduler runs the migration via normal MySQL `CREATE/ALTER/DROP TABLE` statements.

The user may add `ALGORITHM=INPLACE` or `ALGORITHM=INSTANT` as they please, and the scheduler will attempt to run those as is. Some migrations will be completely blocking. See the [MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html). In particular, consider that for non-`INSTANT` DDLs, replicas will accumulate substantial lag.

Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,47 @@ func TestSchemaChange(t *testing.T) {
})
})
}
// 'mysql' strategy
t.Run("mysql strategy", func(t *testing.T) {
t.Run("declarative", func(t *testing.T) {
t1uuid = testOnlineDDLStatement(t, createT1Statement, "mysql --declarative", "vtgate", "just-created", "", false)

status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t1uuid, normalWaitTime, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why we specify multiple statuses in WaitForMigrationStatus but then only a single one in CheckMigrationStatus?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that we wait till either status is seen (the function will otherwise give some 30sec for the migration to "evolve"). So there are terminal states for the function, so that it does not need to wait after meeting these states. This just reduces test time. Then, we really want to validate that we reached a particular result: typically we look for a success, ie OnlineDDLStatusComplete. But sometimes we intentionally seek a failure.

fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t1uuid, schema.OnlineDDLStatusComplete)
checkTable(t, t1Name, true)
})

t.Run("fail postpone-completion", func(t *testing.T) {
t1uuid := testOnlineDDLStatement(t, trivialAlterT1Statement, "mysql --postpone-completion", "vtgate", "", "", true)

// --postpone-completion not supported in mysql strategy
time.Sleep(ensureStateNotChangedTime)
onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t1uuid, normalWaitTime, schema.OnlineDDLStatusFailed)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t1uuid, schema.OnlineDDLStatusFailed)
})
t.Run("trivial", func(t *testing.T) {
t1uuid := testOnlineDDLStatement(t, trivialAlterT1Statement, "mysql", "vtgate", "", "", true)

status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t1uuid, normalWaitTime, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t1uuid, schema.OnlineDDLStatusComplete)

rs := onlineddl.ReadMigrations(t, &vtParams, t1uuid)
require.NotNil(t, rs)
for _, row := range rs.Named().Rows {
artifacts := row.AsString("artifacts", "-")
assert.Empty(t, artifacts)
}
})
t.Run("instant", func(t *testing.T) {
t1uuid := testOnlineDDLStatement(t, instantAlterT1Statement, "mysql", "vtgate", "", "", true)

status := onlineddl.WaitForMigrationStatus(t, &vtParams, shards, t1uuid, normalWaitTime, schema.OnlineDDLStatusComplete, schema.OnlineDDLStatusFailed)
fmt.Printf("# Migration status (for debug purposes): <%s>\n", status)
onlineddl.CheckMigrationStatus(t, &vtParams, shards, t1uuid, schema.OnlineDDLStatusComplete)
})
})
}

// testOnlineDDLStatement runs an online DDL, ALTER statement
Expand Down
8 changes: 5 additions & 3 deletions go/vt/schema/ddl_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
type DDLStrategy string

const (
// DDLStrategyDirect means not an online-ddl migration. Just a normal MySQL ALTER TABLE
// DDLStrategyDirect means not an online-ddl migration; unmanaged. Just a normal MySQL `ALTER TABLE`
DDLStrategyDirect DDLStrategy = "direct"
// DDLStrategyVitess requests vreplication to run the migration; new name for DDLStrategyOnline
DDLStrategyVitess DDLStrategy = "vitess"
Expand All @@ -56,13 +56,15 @@ const (
DDLStrategyGhost DDLStrategy = "gh-ost"
// DDLStrategyPTOSC requests pt-online-schema-change to run the migration
DDLStrategyPTOSC DDLStrategy = "pt-osc"
// DDLStrategyMySQL is a managed migration (queued and executed by the scheduler) but runs through a MySQL `ALTER TABLE`
DDLStrategyMySQL DDLStrategy = "mysql"
)

// IsDirect returns true if this strategy is a direct strategy
// A strategy is direct if it's not explciitly one of the online DDL strategies
func (s DDLStrategy) IsDirect() bool {
switch s {
case DDLStrategyVitess, DDLStrategyOnline, DDLStrategyGhost, DDLStrategyPTOSC:
case DDLStrategyVitess, DDLStrategyOnline, DDLStrategyGhost, DDLStrategyPTOSC, DDLStrategyMySQL:
return false
}
return true
Expand Down Expand Up @@ -94,7 +96,7 @@ func ParseDDLStrategy(strategyVariable string) (*DDLStrategySetting, error) {
switch strategy := DDLStrategy(strategyName); strategy {
case "": // backward compatiblity and to handle unspecified values
setting.Strategy = DDLStrategyDirect
case DDLStrategyVitess, DDLStrategyOnline, DDLStrategyGhost, DDLStrategyPTOSC, DDLStrategyDirect:
case DDLStrategyVitess, DDLStrategyOnline, DDLStrategyGhost, DDLStrategyPTOSC, DDLStrategyMySQL, DDLStrategyDirect:
setting.Strategy = strategy
default:
return nil, fmt.Errorf("Unknown online DDL strategy: '%v'", strategy)
Expand Down
5 changes: 5 additions & 0 deletions go/vt/schema/ddl_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestIsDirect(t *testing.T) {
assert.False(t, DDLStrategy("online").IsDirect())
assert.False(t, DDLStrategy("gh-ost").IsDirect())
assert.False(t, DDLStrategy("pt-osc").IsDirect())
assert.False(t, DDLStrategy("mysql").IsDirect())
assert.True(t, DDLStrategy("something").IsDirect())
}

Expand Down Expand Up @@ -73,6 +74,10 @@ func TestParseDDLStrategy(t *testing.T) {
strategyVariable: "pt-osc",
strategy: DDLStrategyPTOSC,
},
{
strategyVariable: "mysql",
strategy: DDLStrategyMySQL,
},
{
strategy: DDLStrategyDirect,
},
Expand Down
26 changes: 24 additions & 2 deletions go/vt/vttablet/onlineddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,8 @@ func (e *Executor) initMigrationSQLMode(ctx context.Context, onlineDDL *schema.O
conn.ExecuteFetch(restoreSQLModeQuery, 0, false)
}
// Change sql_mode
changeSSQLModeQuery := fmt.Sprintf("set @@session.sql_mode=REPLACE(REPLACE('%s', 'NO_ZERO_DATE', ''), 'NO_ZERO_IN_DATE', '')", sqlMode)
if _, err := conn.ExecuteFetch(changeSSQLModeQuery, 0, false); err != nil {
changeSQLModeQuery := fmt.Sprintf("set @@session.sql_mode=REPLACE(REPLACE('%s', 'NO_ZERO_DATE', ''), 'NO_ZERO_IN_DATE', '')", sqlMode)
if _, err := conn.ExecuteFetch(changeSQLModeQuery, 0, false); err != nil {
return deferFunc, err
}
return deferFunc, nil
Expand Down Expand Up @@ -2313,12 +2313,25 @@ func (e *Executor) reviewQueuedMigrations(ctx context.Context) error {
return err
}
}
// Find conditions where the migration cannot take place:
switch onlineDDL.Strategy {
case schema.DDLStrategyMySQL:
strategySetting := onlineDDL.StrategySetting()
if strategySetting.IsPostponeCompletion() {
e.failMigration(ctx, onlineDDL, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "--postpone-completion not supported in 'mysql' strategy"))
}
if strategySetting.IsAllowZeroInDateFlag() {
e.failMigration(ctx, onlineDDL, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "--allow-zero-in-date not supported in 'mysql' strategy"))
}
}

// The review is complete. We've backfilled details on the migration row. We mark
// the migration as having been reviewed. The function scheduleNextMigration() will then
// have access to this row.
if err := e.updateMigrationTimestamp(ctx, "reviewed_timestamp", uuid); err != nil {
return err
}

}
return nil
}
Expand Down Expand Up @@ -3021,6 +3034,15 @@ func (e *Executor) executeAlterDDLActionMigration(ctx context.Context, onlineDDL
failMigration(err)
}
}()
case schema.DDLStrategyMySQL:
go func() {
e.migrationMutex.Lock()
defer e.migrationMutex.Unlock()

if _, err := e.executeDirectly(ctx, onlineDDL); err != nil {
failMigration(err)
}
}()
default:
{
return failMigration(fmt.Errorf("Unsupported strategy: %+v", onlineDDL.Strategy))
Expand Down