Skip to content

Commit

Permalink
Merge pull request #6909 from planetscale/online-ddl-schema-apply-rel…
Browse files Browse the repository at this point in the history
…ease-80

[8.0] OnlineDDL bugfix: make sure schema is applied on tablet
  • Loading branch information
sougou committed Oct 22, 2020
2 parents 138b417 + e5d1fc9 commit 128718a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
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"),
})
}

0 comments on commit 128718a

Please sign in to comment.