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

[8.0] OnlineDDL bugfix: make sure schema is applied on tablet #6909

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions go/mysql/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,20 @@ func IsConnErr(err error) bool {
}
return false
}

// IsSchemaApplyError returns true when given error is a MySQL error applying schema change
func IsSchemaApplyError(err error) bool {
merr, isSQLErr := err.(*SQLError)
if !isSQLErr {
return false
}
switch merr.Num {
case
ERDupKeyName,
ERCantDropFieldOrKey,
ERTableExists,
ERDupFieldName:
return true
}
return false
}
33 changes: 27 additions & 6 deletions go/vt/vttablet/onlineddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"syscall"
"time"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/textutil"
"vitess.io/vitess/go/timer"
Expand Down Expand Up @@ -119,8 +120,9 @@ type Executor struct {
migrationRunning int64
lastMigrationUUID string

ticks *timer.Timer
isOpen bool
ticks *timer.Timer
isOpen bool
schemaInitialized bool
}

// GhostBinaryFileName returns the full path+name of the gh-ost binary
Expand Down Expand Up @@ -167,16 +169,32 @@ func (e *Executor) execQuery(ctx context.Context, query string) (result *sqltype
}

func (e *Executor) initSchema(ctx context.Context) error {
e.initMutex.Lock()
defer e.initMutex.Unlock()

if e.schemaInitialized {
return nil
}

defer e.env.LogError()

conn, err := e.pool.Get(ctx)
if err != nil {
return err
}
defer conn.Recycle()
parsed := sqlparser.BuildParsedQuery(sqlValidationQuery, "_vt")
_, err = withDDL.Exec(ctx, parsed.Query, conn.Exec)
return err

for _, ddl := range applyDDL {
_, err := conn.Exec(ctx, ddl, math.MaxInt32, false)
if mysql.IsSchemaApplyError(err) {
continue
}
if err != nil {
return err
}
}
e.schemaInitialized = true
return nil
}

// InitDBConfig initializes keysapce
Expand All @@ -194,7 +212,6 @@ func (e *Executor) Open() error {
return nil
}
e.pool.Open(e.env.Config().DB.AppWithDB(), e.env.Config().DB.DbaWithDB(), e.env.Config().DB.AppDebugWithDB())
e.initSchema(context.Background())
e.ticks.Start(e.onMigrationCheckTick)

if _, err := sqlparser.QueryMatchesTemplates("select 1 from dual", vexecUpdateTemplates); err != nil {
Expand Down Expand Up @@ -1140,6 +1157,10 @@ func (e *Executor) onMigrationCheckTick() {
}
ctx := context.Background()

if err := e.initSchema(ctx); err != nil {
log.Error(err)
return
}
if err := e.scheduleNextMigration(ctx); err != nil {
log.Error(err)
}
Expand Down
7 changes: 2 additions & 5 deletions go/vt/vttablet/onlineddl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package onlineddl

import (
"fmt"

"vitess.io/vitess/go/vt/withddl"
)

const (
Expand Down Expand Up @@ -52,7 +50,6 @@ const (
KEY status_idx (migration_status, liveness_timestamp),
KEY cleanup_status_idx (cleanup_timestamp, migration_status)
) engine=InnoDB DEFAULT CHARSET=utf8mb4`
sqlValidationQuery = `select 1 from %s.schema_migrations limit 1`
sqlScheduleSingleMigration = `UPDATE %s.schema_migrations
SET
migration_status='ready',
Expand Down Expand Up @@ -201,7 +198,7 @@ var (
sqlDropOnlineDDLUser = `DROP USER IF EXISTS %s`
)

var withDDL = withddl.New([]string{
var applyDDL = []string{
fmt.Sprintf(sqlCreateSidecarDB, "_vt"),
fmt.Sprintf(sqlCreateSchemaMigrationsTable, "_vt"),
})
}