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

ddl: ddl-owner try to use memory infoSchema to check first #10170

Merged
merged 12 commits into from
Apr 23, 2019
Merged
10 changes: 5 additions & 5 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ type DDL interface {

// ddl is used to handle the statements that define the structure or schema of the database.
type ddl struct {
m sync.RWMutex
infoHandle *infoschema.Handle
quitCh chan struct{}
m sync.RWMutex
quitCh chan struct{}

*ddlCtx
workers map[workerType]*worker
Expand All @@ -299,6 +298,7 @@ type ddlCtx struct {
ddlEventCh chan<- *util.Event
lease time.Duration // lease is schema lease.
binlogCli *pumpcli.PumpsClient // binlogCli is used for Binlog.
infoHandle *infoschema.Handle

// hook may be modified.
mu struct {
Expand Down Expand Up @@ -379,12 +379,12 @@ func newDDL(ctx context.Context, etcdCli *clientv3.Client, store kv.Storage,
ownerManager: manager,
schemaSyncer: syncer,
binlogCli: binloginfo.GetPumpsClient(),
infoHandle: infoHandle,
}
ddlCtx.mu.hook = hook
ddlCtx.mu.interceptor = &BaseInterceptor{}
d := &ddl{
infoHandle: infoHandle,
ddlCtx: ddlCtx,
ddlCtx: ddlCtx,
}

d.start(ctx, ctxPool)
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64,
case model.ActionRebaseAutoID:
ver, err = onRebaseAutoID(d.store, t, job)
case model.ActionRenameTable:
ver, err = onRenameTable(t, job)
ver, err = onRenameTable(d, t, job)
case model.ActionShardRowID:
ver, err = w.onShardRowID(d, t, job)
case model.ActionModifyTableComment:
Expand Down
38 changes: 32 additions & 6 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func onCreateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error)
}

tbInfo.State = model.StateNone
err := checkTableNotExists(t, job, schemaID, tbInfo.Name.L)
err := checkTableNotExists(d, t, job, schemaID, tbInfo.Name.L)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
Expand Down Expand Up @@ -94,7 +94,7 @@ func onCreateView(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error)
return ver, errors.Trace(err)
}
tbInfo.State = model.StateNone
err := checkTableNotExists(t, job, schemaID, tbInfo.Name.L)
err := checkTableNotExists(d, t, job, schemaID, tbInfo.Name.L)
if err != nil {
if infoschema.ErrDatabaseNotExists.Equal(err) || !orReplace {
job.State = model.JobStateCancelled
Expand Down Expand Up @@ -191,7 +191,7 @@ func (w *worker) onRecoverTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver in
return ver, errors.Trace(err)
}

err = checkTableNotExists(t, job, schemaID, tblInfo.Name.L)
err = checkTableNotExists(d, t, job, schemaID, tblInfo.Name.L)
if err != nil {
return ver, errors.Trace(err)
}
Expand Down Expand Up @@ -525,7 +525,7 @@ func verifyNoOverflowShardBits(s *sessionPool, tbl table.Table, shardRowIDBits u
return nil
}

func onRenameTable(t *meta.Meta, job *model.Job) (ver int64, _ error) {
func onRenameTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) {
var oldSchemaID int64
var tableName model.CIStr
if err := job.DecodeArgs(&oldSchemaID, &tableName); err != nil {
Expand All @@ -539,7 +539,7 @@ func onRenameTable(t *meta.Meta, job *model.Job) (ver int64, _ error) {
return ver, errors.Trace(err)
}
newSchemaID := job.SchemaID
err = checkTableNotExists(t, job, newSchemaID, tableName.L)
err = checkTableNotExists(d, t, job, newSchemaID, tableName.L)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
Expand Down Expand Up @@ -658,7 +658,33 @@ func onModifyTableCharsetAndCollate(t *meta.Meta, job *model.Job) (ver int64, _
return ver, nil
}

func checkTableNotExists(t *meta.Meta, job *model.Job, schemaID int64, tableName string) error {
func checkTableNotExists(d *ddlCtx, t *meta.Meta, job *model.Job, schemaID int64, tableName string) error {
// d.infoHandle maybe nil in some test.
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
if d.infoHandle == nil {
return checkTableNotExistsFromStore(t, job, schemaID, tableName)
}
// Try to use memory schema info to check first.
currVer, err := t.GetSchemaVersion()
if err != nil {
return err
}
is := d.infoHandle.Get()
if is.SchemaMetaVersion() == currVer {
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
// Check this table's database.
schema, ok := is.SchemaByID(schemaID)
if !ok {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs("")
}
if is.TableExists(schema.Name, model.NewCIStr(tableName)) {
return infoschema.ErrTableExists.GenWithStackByArgs(tableName)
}
return nil
}

return checkTableNotExistsFromStore(t, job, schemaID, tableName)
}

func checkTableNotExistsFromStore(t *meta.Meta, job *model.Job, schemaID int64, tableName string) error {
// Check this table's database.
tables, err := t.ListTables(schemaID)
if err != nil {
Expand Down