diff --git a/br/pkg/restore/ingestrec/ingest_recorder_test.go b/br/pkg/restore/ingestrec/ingest_recorder_test.go index 56d655a01416a..b6473f724cdbe 100644 --- a/br/pkg/restore/ingestrec/ingest_recorder_test.go +++ b/br/pkg/restore/ingestrec/ingest_recorder_test.go @@ -162,7 +162,7 @@ func TestAddIngestRecorder(t *testing.T) { }, State: model.StatePublic, } - err = m.CreateTableOrView(1, dbInfo.Name.L, tblInfo) + err = m.CreateTableOrView(1, tblInfo) require.NoError(t, err) }) dom, err := session.GetDomain(store) @@ -357,7 +357,7 @@ func TestIndexesKind(t *testing.T) { }, State: model.StatePublic, } - err = m.CreateTableOrView(1, dbInfo.Name.L, tblInfo) + err = m.CreateTableOrView(1, tblInfo) require.NoError(t, err) }) dom, err := session.GetDomain(store) @@ -454,7 +454,7 @@ func TestRewriteTableID(t *testing.T) { }, State: model.StatePublic, } - err = m.CreateTableOrView(1, dbInfo.Name.L, tblInfo) + err = m.CreateTableOrView(1, tblInfo) require.NoError(t, err) }) dom, err := session.GetDomain(store) diff --git a/lightning/pkg/importer/meta_manager_test.go b/lightning/pkg/importer/meta_manager_test.go index e0a571eb537e4..8146d64c8c83a 100644 --- a/lightning/pkg/importer/meta_manager_test.go +++ b/lightning/pkg/importer/meta_manager_test.go @@ -81,7 +81,7 @@ func newTableRestore(t *testing.T, if err := m.CreateDatabase(&model.DBInfo{ID: dbInfo.ID}); err != nil && !errors.ErrorEqual(err, meta.ErrDBExists) { return err } - return m.CreateTableOrView(dbInfo.ID, db, ti.Core) + return m.CreateTableOrView(dbInfo.ID, ti.Core) }) require.NoError(t, err) diff --git a/pkg/ddl/ddl.go b/pkg/ddl/ddl.go index c3fe93c18fc11..c0fed601acc67 100644 --- a/pkg/ddl/ddl.go +++ b/pkg/ddl/ddl.go @@ -21,18 +21,15 @@ package ddl import ( "context" "fmt" - "runtime" "strconv" "strings" "sync" - "sync/atomic" "time" "github.com/google/uuid" "github.com/ngaut/pools" "github.com/pingcap/errors" "github.com/pingcap/failpoint" - "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/pkg/config" "github.com/pingcap/tidb/pkg/ddl/ingest" "github.com/pingcap/tidb/pkg/ddl/logutil" @@ -86,7 +83,6 @@ const ( reorgWorkerCnt = 10 generalWorkerCnt = 10 - localWorkerCnt = 10 // checkFlagIndexInJobArgs is the recoverCheckFlag index used in RecoverTable/RecoverSchema job arg list. checkFlagIndexInJobArgs = 1 @@ -253,16 +249,12 @@ type ddl struct { sessPool *sess.Pool delRangeMgr delRangeManager enableTiFlashPoll *atomicutil.Bool - // used in the concurrency ddl. - localWorkerPool *workerPool // get notification if any DDL job submitted or finished. ddlJobNotifyCh chan struct{} sysTblMgr systable.Manager minJobIDRefresher *systable.MinJobIDRefresher - // localJobCh is used to delivery job in local TiDB nodes. - localJobCh chan *JobWrapper - // globalIDLocal locks global id to reduce write conflict. + // globalIDLock locks global id to reduce write conflict. globalIDLock sync.Mutex executor *executor } @@ -681,7 +673,6 @@ func newDDL(ctx context.Context, options ...Option) (*ddl, *executor) { limitJobCh: make(chan *JobWrapper, batchAddingJobs), enableTiFlashPoll: atomicutil.NewBool(true), ddlJobNotifyCh: make(chan struct{}, 100), - localJobCh: make(chan *JobWrapper, 1), } taskexecutor.RegisterTaskType(proto.Backfill, @@ -744,33 +735,6 @@ func (d *ddl) newDeleteRangeManager(mock bool) delRangeManager { return delRangeMgr } -func (d *ddl) prepareLocalModeWorkers() { - var idAllocator atomic.Uint64 - workerFactory := func(tp workerType) func() (pools.Resource, error) { - return func() (pools.Resource, error) { - wk := newWorker(d.ctx, tp, d.sessPool, d.delRangeMgr, d.ddlCtx) - sessForJob, err := d.sessPool.Get() - if err != nil { - return nil, err - } - sessForJob.GetSessionVars().SetDiskFullOpt(kvrpcpb.DiskFullOpt_AllowedOnAlmostFull) - wk.sess = sess.NewSession(sessForJob) - wk.seqAllocator = &idAllocator - metrics.DDLCounter.WithLabelValues(fmt.Sprintf("%s_%s", metrics.CreateDDL, wk.String())).Inc() - return wk, nil - } - } - // local worker count at least 2 at most 10. - localCnt := min(max(runtime.GOMAXPROCS(0)/4, 2), localWorkerCnt) - d.localWorkerPool = newDDLWorkerPool(pools.NewResourcePool(workerFactory(localWorker), localCnt, localCnt, 0), jobTypeLocal) - failpoint.Inject("NoDDLDispatchLoop", func(val failpoint.Value) { - if val.(bool) { - failpoint.Return() - } - }) - d.wg.Run(d.startLocalWorkerLoop) -} - // Start implements DDL.Start interface. func (d *ddl) Start(ctxPool *pools.ResourcePool) error { logutil.DDLLogger().Info("start DDL", zap.String("ID", d.uuid), zap.Bool("runWorker", config.GetGlobalConfig().Instance.TiDBEnableDDL.Load())) @@ -780,7 +744,7 @@ func (d *ddl) Start(ctxPool *pools.ResourcePool) error { d.sysTblMgr = systable.NewManager(d.sessPool) d.minJobIDRefresher = systable.NewMinJobIDRefresher(d.sysTblMgr) d.wg.Run(func() { - d.limitDDLJobs(d.limitJobCh, d.addBatchDDLJobsV1) + d.limitDDLJobs() }) d.wg.Run(func() { d.minJobIDRefresher.Start(d.ctx) @@ -796,8 +760,6 @@ func (d *ddl) Start(ctxPool *pools.ResourcePool) error { ddl: d, }) - d.prepareLocalModeWorkers() - if config.TableLockEnabled() { d.wg.Add(1) go d.startCleanDeadTableLock() @@ -892,9 +854,6 @@ func (d *ddl) close() { d.wg.Wait() d.ownerManager.Cancel() d.schemaSyncer.Close() - if d.localWorkerPool != nil { - d.localWorkerPool.close() - } // d.delRangeMgr using sessions from d.sessPool. // Put it before d.sessPool.close to reduce the time spent by d.sessPool.close. diff --git a/pkg/ddl/executor.go b/pkg/ddl/executor.go index 969fa928da904..df2d9c5f4b806 100644 --- a/pkg/ddl/executor.go +++ b/pkg/ddl/executor.go @@ -10057,10 +10057,11 @@ func (e *executor) delJobDoneCh(jobID int64) { e.ddlJobDoneChMap.Delete(jobID) } -func (d *ddl) limitDDLJobs(ch chan *JobWrapper, handler func([]*JobWrapper)) { +func (d *ddl) limitDDLJobs() { defer util.Recover(metrics.LabelDDL, "limitDDLJobs", nil, true) jobWs := make([]*JobWrapper, 0, batchAddingJobs) + ch := d.limitJobCh for { select { // the channel is never closed @@ -10072,7 +10073,7 @@ func (d *ddl) limitDDLJobs(ch chan *JobWrapper, handler func([]*JobWrapper)) { for i := 0; i < jobLen; i++ { jobWs = append(jobWs, <-ch) } - handler(jobWs) + d.addBatchDDLJobs(jobWs) case <-d.ctx.Done(): return } @@ -10115,8 +10116,8 @@ func (e *executor) deliverJobTask(task *JobWrapper) { e.limitJobCh <- task } -// addBatchDDLJobsV1 gets global job IDs and puts the DDL jobs in the DDL queue. -func (d *ddl) addBatchDDLJobsV1(jobWs []*JobWrapper) { +// addBatchDDLJobs gets global job IDs and puts the DDL jobs in the DDL queue. +func (d *ddl) addBatchDDLJobs(jobWs []*JobWrapper) { startTime := time.Now() var ( err error @@ -10134,7 +10135,7 @@ func (d *ddl) addBatchDDLJobsV1(jobWs []*JobWrapper) { jobWs = newWs } } - err = d.addBatchDDLJobs(jobWs) + err = d.addBatchDDLJobs2Table(jobWs) } else { err = d.addBatchDDLJobs2Queue(jobWs) } @@ -10159,24 +10160,6 @@ func (d *ddl) addBatchDDLJobsV1(jobWs []*JobWrapper) { } } -// addBatchLocalDDLJobs gets global job IDs and delivery the DDL jobs to local TiDB -func (d *ddl) addBatchLocalDDLJobs(jobWs []*JobWrapper) { - if newJobWs, err := mergeCreateTableJobs(jobWs); err == nil { - jobWs = newJobWs - } - err := d.addBatchDDLJobs(jobWs) - if err != nil { - for _, jobW := range jobWs { - jobW.NotifyResult(err) - } - logutil.DDLLogger().Error("add DDL jobs failed", zap.Bool("local_mode", true), zap.Error(err)) - } else { - logutil.DDLLogger().Info("add DDL jobs", - zap.Bool("local_mode", true), - zap.Int("batch count", len(jobWs))) - } -} - func (d *ddl) addBatchDDLJobs2Queue(jobWs []*JobWrapper) error { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL) // lock to reduce conflict @@ -10234,8 +10217,8 @@ func (*ddl) checkFlashbackJobInQueue(t *meta.Meta) error { return nil } -// addBatchDDLJobs gets global job IDs and puts the DDL jobs in the DDL job table or local worker. -func (d *ddl) addBatchDDLJobs(jobWs []*JobWrapper) error { +// addBatchDDLJobs2Table gets global job IDs and puts the DDL jobs in the DDL job table. +func (d *ddl) addBatchDDLJobs2Table(jobWs []*JobWrapper) error { var err error if len(jobWs) == 0 { @@ -10270,7 +10253,6 @@ func (d *ddl) addBatchDDLJobs(jobWs []*JobWrapper) error { } startTS = txn.StartTS() - // for localmode, we still need to check this variable if upgrading below v6.2. if variable.DDLForce2Queue.Load() { if err := d.checkFlashbackJobInQueue(t); err != nil { return err @@ -10304,7 +10286,6 @@ func (d *ddl) addBatchDDLJobs(jobWs []*JobWrapper) error { setJobStateToQueueing(job) - // currently doesn't support pause job in local mode. if d.stateSyncer.IsUpgradingState() && !hasSysDB(job) { if err = pauseRunningJob(sess.NewSession(se), job, model.AdminCommandBySystem); err != nil { logutil.DDLUpgradingLogger().Warn("pause user DDL by system failed", zap.Stringer("job", job), zap.Error(err)) diff --git a/pkg/ddl/job_scheduler.go b/pkg/ddl/job_scheduler.go index ad6cd11179244..1c3446b36704a 100644 --- a/pkg/ddl/job_scheduler.go +++ b/pkg/ddl/job_scheduler.go @@ -58,7 +58,6 @@ var ( // is a new DDL job. addingDDLJobNotifyKey = "/tidb/ddl/add_ddl_job_general" dispatchLoopWaitingDuration = 1 * time.Second - localWorkerWaitingDuration = 10 * time.Millisecond schedulerLoopRetryInterval = time.Second ) @@ -77,8 +76,6 @@ func (t jobType) String() string { return "general" case jobTypeReorg: return "reorg" - case jobTypeLocal: - return "local" } return "unknown job type: " + strconv.Itoa(int(t)) } @@ -86,7 +83,6 @@ func (t jobType) String() string { const ( jobTypeGeneral jobType = iota jobTypeReorg - jobTypeLocal ) type ownerListener struct { @@ -246,21 +242,6 @@ func (s *jobScheduler) processJobDuringUpgrade(sess *sess.Session, job *model.Jo return true, nil } -// startLocalWorkerLoop starts the local worker loop to run the DDL job of v2. -func (d *ddl) startLocalWorkerLoop() { - for { - select { - case <-d.ctx.Done(): - return - case jobW, ok := <-d.localJobCh: - if !ok { - return - } - d.delivery2LocalWorker(d.localWorkerPool, jobW) - } - } -} - func (s *jobScheduler) scheduleLoop() { const retryInterval = 3 * time.Second for { @@ -466,51 +447,6 @@ func (s *jobScheduler) mustReloadSchemas() { } } -// delivery2LocalWorker runs the DDL job of v2 in local. -// send the result to the error channels in the task. -// delivery2Localworker owns the worker, need to put it back to the pool in this function. -func (d *ddl) delivery2LocalWorker(pool *workerPool, jobW *JobWrapper) { - job := jobW.Job - wk, err := pool.get() - if err != nil { - jobW.NotifyResult(err) - return - } - for wk == nil { - select { - case <-d.ctx.Done(): - return - case <-time.After(localWorkerWaitingDuration): - } - wk, err = pool.get() - if err != nil { - jobW.NotifyResult(err) - return - } - } - d.wg.Run(func() { - metrics.DDLRunningJobCount.WithLabelValues(pool.tp().String()).Inc() - defer func() { - metrics.DDLRunningJobCount.WithLabelValues(pool.tp().String()).Dec() - }() - - for i := int64(0); i < variable.GetDDLErrorCountLimit(); i++ { - err = wk.HandleLocalDDLJob(d.ddlCtx, job) - // since local the job is not inserted into the ddl job queue, we need to add retry logic here. - if err == nil || !isRetryableError(err) { - break - } - logutil.DDLLogger().Info("handle local ddl job", zap.Int64("retry times", i), zap.Error(err)) - time.Sleep(time.Second) - } - pool.put(wk) - if err != nil { - logutil.DDLLogger().Info("handle ddl job failed", zap.Error(err), zap.Stringer("job", job)) - } - jobW.NotifyResult(err) - }) -} - // deliveryJob deliver the job to the worker to run it asynchronously. // the worker will run the job until it's finished, paused or another owner takes // over and finished it. diff --git a/pkg/ddl/job_worker.go b/pkg/ddl/job_worker.go index 43be262b7d69a..7d67467afe776 100644 --- a/pkg/ddl/job_worker.go +++ b/pkg/ddl/job_worker.go @@ -82,9 +82,6 @@ const ( generalWorker workerType = 0 // addIdxWorker is the worker who handles the operation of adding indexes. addIdxWorker workerType = 1 - // loaclWorker is the worker who handles the operation in local TiDB. - // currently it only handle CreateTable job of fast create table enabled. - localWorker workerType = 2 ) // worker is used for handling DDL jobs. @@ -94,7 +91,7 @@ type worker struct { tp workerType addingDDLJobKey string ddlJobCh chan struct{} - // for local mode worker, it's ctx of 'ddl', else it's the ctx of 'job scheduler'. + // it's the ctx of 'job scheduler'. ctx context.Context wg sync.WaitGroup @@ -164,8 +161,6 @@ func (w *worker) typeStr() string { str = "general" case addIdxWorker: str = "add index" - case localWorker: - str = "local worker" default: str = "unknown" } @@ -598,7 +593,7 @@ func (w *worker) transitOneJobStep(d *ddlCtx, job *model.Job) (int64, error) { } func (w *worker) checkBeforeCommit() error { - if !w.ddlCtx.isOwner() && w.tp != localWorker { + if !w.ddlCtx.isOwner() { // Since this TiDB instance is not a DDL owner anymore, // it should not commit any transaction. w.sess.Rollback() @@ -612,53 +607,6 @@ func (w *worker) checkBeforeCommit() error { return nil } -// HandleLocalDDLJob handles local ddl job like fast create table. -// Compare with normal ddl job: -// 1. directly insert the job to history job table(incompatible with CDC). -// 2. no need to wait schema version(only support create table now). -// 3. no register mdl info(only support create table now). -func (w *worker) HandleLocalDDLJob(d *ddlCtx, job *model.Job) (err error) { - txn, err := w.prepareTxn(job) - if err != nil { - return err - } - - t := meta.NewMeta(txn, meta.WithUpdateTableName()) - d.mu.RLock() - d.mu.hook.OnJobRunBefore(job) - d.mu.RUnlock() - - _, _, err = w.runOneJobStep(d, t, job) - defer d.unlockSchemaVersion(job.ID) - if err != nil { - return err - } - // no need to rollback for fast create table now. - if job.IsCancelling() { - job.State = model.JobStateCancelled - job.Error = dbterror.ErrCancelledDDLJob - } - if job.IsCancelled() { - w.sess.Reset() - if err = w.handleJobDone(d, job, t); err != nil { - return err - } - // return job.Error to let caller know the job is cancelled. - return job.Error - } - - d.mu.RLock() - d.mu.hook.OnJobRunAfter(job) - d.mu.RUnlock() - - writeBinlog(d.binlogCli, txn, job) - // reset the SQL digest to make topsql work right. - w.sess.GetSessionVars().StmtCtx.ResetSQLDigest(job.Query) - - job.State = model.JobStateSynced - return w.handleJobDone(d, job, t) -} - func (w *JobContext) getResourceGroupTaggerForTopSQL() tikvrpc.ResourceGroupTagger { if !topsqlstate.TopSQLEnabled() || w.cacheDigest == nil { return nil diff --git a/pkg/ddl/partition.go b/pkg/ddl/partition.go index 6da00239b7879..d86e749206cc4 100644 --- a/pkg/ddl/partition.go +++ b/pkg/ddl/partition.go @@ -2650,7 +2650,7 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo // Recreate non-partition table meta info, // by first delete it with the old table id - err = t.DropTableOrView(job.SchemaID, job.SchemaName, nt.ID, nt.Name.L) + err = t.DropTableOrView(job.SchemaID, nt.ID) if err != nil { return ver, errors.Trace(err) } @@ -2667,7 +2667,7 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo return ver, errors.Trace(err) } - err = t.CreateTableOrView(job.SchemaID, job.SchemaName, nt) + err = t.CreateTableOrView(job.SchemaID, nt) if err != nil { return ver, errors.Trace(err) } @@ -3217,7 +3217,7 @@ func (w *worker) onReorganizePartition(d *ddlCtx, t *meta.Meta, job *model.Job) job.State = model.JobStateCancelled return ver, errors.Trace(err) } - err = t.DropTableOrView(job.SchemaID, job.SchemaName, tblInfo.ID, tblInfo.Name.L) + err = t.DropTableOrView(job.SchemaID, tblInfo.ID) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -3240,7 +3240,7 @@ func (w *worker) onReorganizePartition(d *ddlCtx, t *meta.Meta, job *model.Job) return ver, errors.Trace(err) } // TODO: Add failpoint here? - err = t.CreateTableOrView(job.SchemaID, job.SchemaName, tblInfo) + err = t.CreateTableOrView(job.SchemaID, tblInfo) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) diff --git a/pkg/ddl/schema.go b/pkg/ddl/schema.go index 590b3be5e8c2a..53fc0c4c3e0ef 100644 --- a/pkg/ddl/schema.go +++ b/pkg/ddl/schema.go @@ -210,7 +210,7 @@ func onDropSchema(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) if err != nil { return ver, errors.Trace(err) } - if err = t.DropDatabase(dbInfo.ID, dbInfo.Name.L); err != nil { + if err = t.DropDatabase(dbInfo.ID); err != nil { break } diff --git a/pkg/ddl/sequence.go b/pkg/ddl/sequence.go index 8e5a0c77fad9b..a0856d2b0d509 100644 --- a/pkg/ddl/sequence.go +++ b/pkg/ddl/sequence.go @@ -75,7 +75,7 @@ func createSequenceWithCheck(t *meta.Meta, job *model.Job, tbInfo *model.TableIn } else { sequenceBase = tbInfo.Sequence.Start + 1 } - return t.CreateSequenceAndSetSeqValue(job.SchemaID, job.SchemaName, tbInfo, sequenceBase) + return t.CreateSequenceAndSetSeqValue(job.SchemaID, tbInfo, sequenceBase) default: return dbterror.ErrInvalidDDLState.GenWithStackByArgs("sequence", tbInfo.State) } diff --git a/pkg/ddl/table.go b/pkg/ddl/table.go index cce67cc81d7c4..8723dc78c8843 100644 --- a/pkg/ddl/table.go +++ b/pkg/ddl/table.go @@ -287,7 +287,7 @@ func createTableOrViewWithCheck(t *meta.Meta, job *model.Job, schemaID int64, tb job.State = model.JobStateCancelled return errors.Trace(err) } - return t.CreateTableOrView(schemaID, job.SchemaName, tbInfo) + return t.CreateTableOrView(schemaID, tbInfo) } func repairTableOrViewWithCheck(t *meta.Meta, job *model.Job, schemaID int64, tbInfo *model.TableInfo) error { @@ -338,7 +338,7 @@ func onCreateView(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) tbInfo.State = model.StatePublic tbInfo.UpdateTS = t.StartTS if oldTableID > 0 && orReplace { - err = t.DropTableOrView(schemaID, job.SchemaName, oldTableID, tbInfo.Name.L) + err = t.DropTableOrView(schemaID, oldTableID) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -401,11 +401,11 @@ func onDropTableOrView(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ er return ver, errors.Trace(err) } if tblInfo.IsSequence() { - if err = t.DropSequence(job.SchemaID, job.SchemaName, job.TableID, job.TableName); err != nil { + if err = t.DropSequence(job.SchemaID, job.TableID); err != nil { return ver, errors.Trace(err) } } else { - if err = t.DropTableOrView(job.SchemaID, job.SchemaName, job.TableID, job.TableName); err != nil { + if err = t.DropTableOrView(job.SchemaID, job.TableID); err != nil { return ver, errors.Trace(err) } if err = t.GetAutoIDAccessors(job.SchemaID, job.TableID).Del(); err != nil { @@ -571,7 +571,7 @@ func (w *worker) recoverTable(t *meta.Meta, job *model.Job, recoverInfo *Recover tableInfo := recoverInfo.TableInfo.Clone() tableInfo.State = model.StatePublic tableInfo.UpdateTS = t.StartTS - err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, recoverInfo.OldSchemaName, tableInfo, recoverInfo.AutoIDs) + err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs) if err != nil { return ver, errors.Trace(err) } @@ -752,7 +752,7 @@ func (w *worker) onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver i if err != nil { return ver, err } - err = t.DropTableOrView(schemaID, job.SchemaName, tblInfo.ID, tblInfo.Name.L) + err = t.DropTableOrView(schemaID, tblInfo.ID) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -849,7 +849,7 @@ func (w *worker) onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver i return 0, errors.Wrapf(err, "failed to notify PD the placement rules") } - err = t.CreateTableOrView(schemaID, job.SchemaName, tblInfo) + err = t.CreateTableOrView(schemaID, tblInfo) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -1126,7 +1126,7 @@ func onRenameTables(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error } func checkAndRenameTables(t *meta.Meta, job *model.Job, tblInfo *model.TableInfo, oldSchemaID, newSchemaID int64, oldSchemaName, tableName *model.CIStr) (ver int64, _ error) { - err := t.DropTableOrView(oldSchemaID, oldSchemaName.L, tblInfo.ID, tblInfo.Name.L) + err := t.DropTableOrView(oldSchemaID, tblInfo.ID) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) @@ -1161,7 +1161,7 @@ func checkAndRenameTables(t *meta.Meta, job *model.Job, tblInfo *model.TableInfo } tblInfo.Name = *tableName - err = t.CreateTableOrView(newSchemaID, job.SchemaName, tblInfo) + err = t.CreateTableOrView(newSchemaID, tblInfo) if err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) diff --git a/pkg/ddl/tests/serial/serial_test.go b/pkg/ddl/tests/serial/serial_test.go index 3edfbd6355f68..5366649eab05d 100644 --- a/pkg/ddl/tests/serial/serial_test.go +++ b/pkg/ddl/tests/serial/serial_test.go @@ -830,7 +830,7 @@ func TestCanceledJobTakeTime(t *testing.T) { if err != nil { return err } - return m.DropTableOrView(job.SchemaID, job.SchemaName, job.TableID, job.TableName) + return m.DropTableOrView(job.SchemaID, job.TableID) }) require.NoError(t, err) }) diff --git a/pkg/infoschema/infoschema_test.go b/pkg/infoschema/infoschema_test.go index 5f2d5b4608490..c8e0b7d32794d 100644 --- a/pkg/infoschema/infoschema_test.go +++ b/pkg/infoschema/infoschema_test.go @@ -376,7 +376,7 @@ func TestBuildSchemaWithGlobalTemporaryTable(t *testing.T) { createGlobalTemporaryTableChange := func(tblID int64) func(m *meta.Meta, builder *infoschema.Builder) { return func(m *meta.Meta, builder *infoschema.Builder) { - err := m.CreateTableOrView(db.ID, db.Name.L, &model.TableInfo{ + err := m.CreateTableOrView(db.ID, &model.TableInfo{ ID: tblID, TempTableType: model.TempTableGlobal, State: model.StatePublic, @@ -389,7 +389,7 @@ func TestBuildSchemaWithGlobalTemporaryTable(t *testing.T) { createNormalTableChange := func(tblID int64) func(m *meta.Meta, builder *infoschema.Builder) { return func(m *meta.Meta, builder *infoschema.Builder) { - err := m.CreateTableOrView(db.ID, db.Name.L, &model.TableInfo{ + err := m.CreateTableOrView(db.ID, &model.TableInfo{ ID: tblID, State: model.StatePublic, }) @@ -401,7 +401,7 @@ func TestBuildSchemaWithGlobalTemporaryTable(t *testing.T) { dropTableChange := func(tblID int64) func(m *meta.Meta, builder *infoschema.Builder) { return func(m *meta.Meta, builder *infoschema.Builder) { - err := m.DropTableOrView(db.ID, db.Name.L, tblID, "") + err := m.DropTableOrView(db.ID, tblID) require.NoError(t, err) _, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, SchemaID: db.ID, TableID: tblID, Version: 1}) require.NoError(t, err) @@ -410,10 +410,10 @@ func TestBuildSchemaWithGlobalTemporaryTable(t *testing.T) { truncateGlobalTemporaryTableChange := func(tblID, newTblID int64) func(m *meta.Meta, builder *infoschema.Builder) { return func(m *meta.Meta, builder *infoschema.Builder) { - err := m.DropTableOrView(db.ID, db.Name.L, tblID, "") + err := m.DropTableOrView(db.ID, tblID) require.NoError(t, err) - err = m.CreateTableOrView(db.ID, db.Name.L, &model.TableInfo{ + err = m.CreateTableOrView(db.ID, &model.TableInfo{ ID: newTblID, TempTableType: model.TempTableGlobal, State: model.StatePublic, diff --git a/pkg/infoschema/internal/testkit.go b/pkg/infoschema/internal/testkit.go index 88c6991105986..c4c60a064546e 100644 --- a/pkg/infoschema/internal/testkit.go +++ b/pkg/infoschema/internal/testkit.go @@ -223,7 +223,7 @@ func MockPolicyRefInfo(t *testing.T, store kv.Storage, policyName string) *model func AddTable(t testing.TB, store kv.Storage, dbInfo *model.DBInfo, tblInfo *model.TableInfo) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - err := meta.NewMeta(txn).CreateTableOrView(dbInfo.ID, dbInfo.Name.O, tblInfo) + err := meta.NewMeta(txn).CreateTableOrView(dbInfo.ID, tblInfo) require.NoError(t, err) return errors.Trace(err) }) @@ -245,7 +245,7 @@ func UpdateTable(t *testing.T, store kv.Storage, dbInfo *model.DBInfo, tblInfo * func DropTable(t testing.TB, store kv.Storage, dbInfo *model.DBInfo, tblID int64, tblName string) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - err := meta.NewMeta(txn).DropTableOrView(dbInfo.ID, dbInfo.Name.O, tblID, tblName) + err := meta.NewMeta(txn).DropTableOrView(dbInfo.ID, tblID) require.NoError(t, err) return errors.Trace(err) }) @@ -268,7 +268,7 @@ func DropDB(t testing.TB, store kv.Storage, dbInfo *model.DBInfo) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - err := meta.NewMeta(txn).DropDatabase(dbInfo.ID, dbInfo.Name.O) + err := meta.NewMeta(txn).DropDatabase(dbInfo.ID) require.NoError(t, err) return errors.Trace(err) }) diff --git a/pkg/lightning/common/common_test.go b/pkg/lightning/common/common_test.go index 7da0879f3f771..fdedbebfcabcc 100644 --- a/pkg/lightning/common/common_test.go +++ b/pkg/lightning/common/common_test.go @@ -51,7 +51,7 @@ func newTableInfo(t *testing.T, if err := m.CreateDatabase(&model.DBInfo{ID: dbID}); err != nil && !errors.ErrorEqual(err, meta.ErrDBExists) { return err } - return m.CreateTableOrView(dbID, "", tableInfo) + return m.CreateTableOrView(dbID, tableInfo) }) require.NoError(t, err) return tableInfo diff --git a/pkg/meta/BUILD.bazel b/pkg/meta/BUILD.bazel index d3b108a7a7b6c..141ee455b2035 100644 --- a/pkg/meta/BUILD.bazel +++ b/pkg/meta/BUILD.bazel @@ -33,7 +33,7 @@ go_test( ], embed = [":meta"], flaky = True, - shard_count = 15, + shard_count = 14, deps = [ "//pkg/ddl", "//pkg/kv", diff --git a/pkg/meta/autoid/autoid_test.go b/pkg/meta/autoid/autoid_test.go index f6fa527db7d36..a531c6ec1a5b4 100644 --- a/pkg/meta/autoid/autoid_test.go +++ b/pkg/meta/autoid/autoid_test.go @@ -65,15 +65,15 @@ func TestSignedAutoid(t *testing.T) { m := meta.NewMeta(txn) err = m.CreateDatabase(&model.DBInfo{ID: 1, Name: model.NewCIStr("a")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 3, Name: model.NewCIStr("t1")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 3, Name: model.NewCIStr("t1")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 4, Name: model.NewCIStr("t2")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 4, Name: model.NewCIStr("t2")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 5, Name: model.NewCIStr("t3")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 5, Name: model.NewCIStr("t3")}) require.NoError(t, err) return nil }) @@ -270,15 +270,15 @@ func TestUnsignedAutoid(t *testing.T) { m := meta.NewMeta(txn) err = m.CreateDatabase(&model.DBInfo{ID: 1, Name: model.NewCIStr("a")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 3, Name: model.NewCIStr("t1")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 3, Name: model.NewCIStr("t1")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 4, Name: model.NewCIStr("t2")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 4, Name: model.NewCIStr("t2")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "", &model.TableInfo{ID: 5, Name: model.NewCIStr("t3")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 5, Name: model.NewCIStr("t3")}) require.NoError(t, err) return nil }) @@ -434,7 +434,7 @@ func TestConcurrentAlloc(t *testing.T) { m := meta.NewMeta(txn) err = m.CreateDatabase(&model.DBInfo{ID: dbID, Name: model.NewCIStr("a")}) require.NoError(t, err) - err = m.CreateTableOrView(dbID, "a", &model.TableInfo{ID: tblID, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(dbID, &model.TableInfo{ID: tblID, Name: model.NewCIStr("t")}) require.NoError(t, err) return nil }) @@ -520,7 +520,7 @@ func TestRollbackAlloc(t *testing.T) { m := meta.NewMeta(txn) err = m.CreateDatabase(&model.DBInfo{ID: dbID, Name: model.NewCIStr("a")}) require.NoError(t, err) - err = m.CreateTableOrView(dbID, "a", &model.TableInfo{ID: tblID, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(dbID, &model.TableInfo{ID: tblID, Name: model.NewCIStr("t")}) require.NoError(t, err) return nil }) @@ -570,9 +570,9 @@ func TestAllocComputationIssue(t *testing.T) { m := meta.NewMeta(txn) err = m.CreateDatabase(&model.DBInfo{ID: 1, Name: model.NewCIStr("a")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "a", &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "a", &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) require.NoError(t, err) return nil }) @@ -621,7 +621,7 @@ func TestIssue40584(t *testing.T) { m := meta.NewMeta(txn) err = m.CreateDatabase(&model.DBInfo{ID: 1, Name: model.NewCIStr("a")}) require.NoError(t, err) - err = m.CreateTableOrView(1, "a", &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(1, &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) require.NoError(t, err) return nil }) diff --git a/pkg/meta/autoid/bench_test.go b/pkg/meta/autoid/bench_test.go index cd196440bb012..f2df4e67633c2 100644 --- a/pkg/meta/autoid/bench_test.go +++ b/pkg/meta/autoid/bench_test.go @@ -48,7 +48,7 @@ func BenchmarkAllocator_Alloc(b *testing.B) { if err != nil { return err } - err = m.CreateTableOrView(dbID, "a", &model.TableInfo{ID: tblID, Name: model.NewCIStr("t")}) + err = m.CreateTableOrView(dbID, &model.TableInfo{ID: tblID, Name: model.NewCIStr("t")}) if err != nil { return err } @@ -103,7 +103,7 @@ func BenchmarkAllocator_SequenceAlloc(b *testing.B) { Sequence: seq, } sequenceBase = seq.Start - 1 - err = m.CreateSequenceAndSetSeqValue(1, "a", seqTable, sequenceBase) + err = m.CreateSequenceAndSetSeqValue(1, seqTable, sequenceBase) return err }) if err != nil { diff --git a/pkg/meta/autoid/seq_autoid_test.go b/pkg/meta/autoid/seq_autoid_test.go index da3772ba9d5b6..ae0d4508a1e75 100644 --- a/pkg/meta/autoid/seq_autoid_test.go +++ b/pkg/meta/autoid/seq_autoid_test.go @@ -60,7 +60,7 @@ func TestSequenceAutoid(t *testing.T) { Sequence: seq, } sequenceBase = seq.Start - 1 - err = m.CreateSequenceAndSetSeqValue(1, "a", seqTable, sequenceBase) + err = m.CreateSequenceAndSetSeqValue(1, seqTable, sequenceBase) require.NoError(t, err) return nil }) @@ -189,7 +189,7 @@ func TestConcurrentAllocSequence(t *testing.T) { } else { sequenceBase = seq.Start + 1 } - err1 = m.CreateSequenceAndSetSeqValue(2, "a", seqTable, sequenceBase) + err1 = m.CreateSequenceAndSetSeqValue(2, seqTable, sequenceBase) require.NoError(t, err1) return nil }) diff --git a/pkg/meta/meta.go b/pkg/meta/meta.go index 70aa682293094..8900e4d59ef40 100644 --- a/pkg/meta/meta.go +++ b/pkg/meta/meta.go @@ -70,13 +70,8 @@ var ( mNextGlobalIDKey = []byte("NextGlobalID") mSchemaVersionKey = []byte("SchemaVersionKey") mDBs = []byte("DBs") - mNames = []byte("Names") - mDBNames = []byte("DBNames") - mDBNameInitialized = []byte("DBNameInitialized") - mDDLV2Initialized = []byte("DDLV2Initialized") mDBPrefix = "DB" mTablePrefix = "Table" - mNameSep = []byte("\x00") mSequencePrefix = "SID" mSeqCyclePrefix = "SequenceCycle" mTableIDPrefix = "TID" @@ -176,20 +171,11 @@ func (ver DDLTableVersion) Bytes() []byte { // Option is for Meta option. type Option func(m *Meta) -// WithUpdateTableName is for updating the name of the table. -// Only used for ddl v2. -func WithUpdateTableName() Option { - return func(m *Meta) { - m.needUpdateName = true - } -} - // Meta is for handling meta information in a transaction. type Meta struct { - txn *structure.TxStructure - StartTS uint64 // StartTS is the txn's start TS. - jobListKey JobListKeyType - needUpdateName bool + txn *structure.TxStructure + StartTS uint64 // StartTS is the txn's start TS. + jobListKey JobListKeyType } // NewMeta creates a Meta in transaction txn. @@ -681,9 +667,6 @@ func (m *Meta) CreateDatabase(dbInfo *model.DBInfo) error { if err := m.txn.HSet(mDBs, dbKey, data); err != nil { return errors.Trace(err) } - if m.needUpdateName { - return errors.Trace(m.CreateDatabaseName(dbInfo.Name.L, dbInfo.ID)) - } return nil } @@ -704,7 +687,7 @@ func (m *Meta) UpdateDatabase(dbInfo *model.DBInfo) error { } // CreateTableOrView creates a table with tableInfo in database. -func (m *Meta) CreateTableOrView(dbID int64, dbName string, tableInfo *model.TableInfo) error { +func (m *Meta) CreateTableOrView(dbID int64, tableInfo *model.TableInfo) error { // Check if db exists. dbKey := m.dbKey(dbID) if err := m.checkDBExists(dbKey); err != nil { @@ -725,9 +708,6 @@ func (m *Meta) CreateTableOrView(dbID int64, dbName string, tableInfo *model.Tab if err := m.txn.HSet(dbKey, tableKey, data); err != nil { return errors.Trace(err) } - if m.needUpdateName { - return errors.Trace(m.CreateTableName(dbName, tableInfo.Name.L, tableInfo.ID)) - } return nil } @@ -850,8 +830,8 @@ func (m *Meta) GetSchemaCacheSize() (size uint64, isNull bool, err error) { // CreateTableAndSetAutoID creates a table with tableInfo in database, // and rebases the table autoID. -func (m *Meta) CreateTableAndSetAutoID(dbID int64, dbName string, tableInfo *model.TableInfo, autoIDs AutoIDGroup) error { - err := m.CreateTableOrView(dbID, dbName, tableInfo) +func (m *Meta) CreateTableAndSetAutoID(dbID int64, tableInfo *model.TableInfo, autoIDs AutoIDGroup) error { + err := m.CreateTableOrView(dbID, tableInfo) if err != nil { return errors.Trace(err) } @@ -875,8 +855,8 @@ func (m *Meta) CreateTableAndSetAutoID(dbID int64, dbName string, tableInfo *mod } // CreateSequenceAndSetSeqValue creates sequence with tableInfo in database, and rebase the sequence seqValue. -func (m *Meta) CreateSequenceAndSetSeqValue(dbID int64, dbName string, tableInfo *model.TableInfo, seqValue int64) error { - err := m.CreateTableOrView(dbID, dbName, tableInfo) +func (m *Meta) CreateSequenceAndSetSeqValue(dbID int64, tableInfo *model.TableInfo, seqValue int64) error { + err := m.CreateTableOrView(dbID, tableInfo) if err != nil { return errors.Trace(err) } @@ -914,7 +894,7 @@ func (m *Meta) DropPolicy(policyID int64) error { } // DropDatabase drops whole database. -func (m *Meta) DropDatabase(dbID int64, dbName string) error { +func (m *Meta) DropDatabase(dbID int64) error { // Check if db exists. dbKey := m.dbKey(dbID) if err := m.txn.HClear(dbKey); err != nil { @@ -925,16 +905,13 @@ func (m *Meta) DropDatabase(dbID int64, dbName string) error { return errors.Trace(err) } - if m.needUpdateName { - return errors.Trace(m.DropDatabaseName(dbName)) - } return nil } // DropSequence drops sequence in database. // Sequence is made of table struct and kv value pair. -func (m *Meta) DropSequence(dbID int64, dbName string, tblID int64, tbName string) error { - err := m.DropTableOrView(dbID, dbName, tblID, tbName) +func (m *Meta) DropSequence(dbID int64, tblID int64) error { + err := m.DropTableOrView(dbID, tblID) if err != nil { return err } @@ -949,7 +926,7 @@ func (m *Meta) DropSequence(dbID int64, dbName string, tblID int64, tbName strin // DropTableOrView drops table in database. // If delAutoID is true, it will delete the auto_increment id key-value of the table. // For rename table, we do not need to rename auto_increment id key-value. -func (m *Meta) DropTableOrView(dbID int64, dbName string, tblID int64, tbName string) error { +func (m *Meta) DropTableOrView(dbID int64, tblID int64) error { // Check if db exists. dbKey := m.dbKey(dbID) if err := m.checkDBExists(dbKey); err != nil { @@ -965,9 +942,6 @@ func (m *Meta) DropTableOrView(dbID int64, dbName string, tblID int64, tbName st if err := m.txn.HDel(dbKey, tableKey); err != nil { return errors.Trace(err) } - if m.needUpdateName { - return errors.Trace(m.DropTableName(dbName, tbName)) - } return nil } @@ -1665,163 +1639,6 @@ func (m *Meta) SetSchemaDiff(diff *model.SchemaDiff) error { return errors.Trace(err) } -// TableNameKey constructs the key for table name. -func (*Meta) TableNameKey(dbName string, tableName string) kv.Key { - var sb strings.Builder - sb.Write(mNames) - sb.WriteByte(':') - sb.WriteString(strings.ToLower(dbName)) - sb.Write(mNameSep) - sb.WriteString(strings.ToLower(tableName)) - return kv.Key(sb.String()) -} - -// DatabaseNameKey constructs the key for database name. -func (*Meta) DatabaseNameKey(dbName string) kv.Key { - var sb strings.Builder - sb.Write(mDBNames) - sb.WriteByte(':') - sb.WriteString(strings.ToLower(dbName)) - return kv.Key(sb.String()) -} - -// CheckTableNameExists checks if the table name exists. -func (m *Meta) CheckTableNameExists(name []byte) error { - v, err := m.txn.Get(name) - if err == nil && v == nil { - err = ErrTableNotExists.FastGenByArgs(string(name)) - } - return errors.Trace(err) -} - -// CheckTableNameNotExists checks if the table name not exists. -func (m *Meta) CheckTableNameNotExists(name []byte) error { - v, err := m.txn.Get(name) - if err == nil && v != nil { - err = ErrTableExists.FastGenByArgs(string(name)) - } - return errors.Trace(err) -} - -// CheckDatabaseNameExists checks if the database name exists. -func (m *Meta) CheckDatabaseNameExists(name []byte) error { - v, err := m.txn.Get(name) - if err == nil && v == nil { - err = ErrDBNotExists.FastGenByArgs(string(name)) - } - return errors.Trace(err) -} - -// CheckDatabaseNameNotExists checks if the database name not exists. -func (m *Meta) CheckDatabaseNameNotExists(name []byte) error { - v, err := m.txn.Get(name) - if err == nil && v != nil { - err = ErrDBExists.FastGenByArgs(string(name)) - } - return errors.Trace(err) -} - -// CreateDatabaseName creates a database name. -// Used by CreateDatabase/RenameDatabase -func (m *Meta) CreateDatabaseName(dbName string, dbID int64) error { - // Check if database exists. - key := m.DatabaseNameKey(dbName) - if err := m.CheckDatabaseNameNotExists(key); err != nil { - return errors.Trace(err) - } - return m.txn.Set(key, []byte(strconv.FormatInt(dbID, 10))) -} - -// CreateTableName creates a table name. -// Used by CreateTable/RenameTable/TruncateTable/RecoverTable/RecoverSchema/CreateView... -func (m *Meta) CreateTableName(dbName string, tableName string, tableID int64) error { - // Check if table exists. - key := m.TableNameKey(dbName, tableName) - if err := m.CheckTableNameNotExists(key); err != nil { - return errors.Trace(err) - } - return m.txn.Set(key, []byte(strconv.FormatInt(tableID, 10))) -} - -// DropTableName drops a table name. -// Used by DropTable/RenameTable/TruncateTable/DropView... -func (m *Meta) DropTableName(dbName string, tableName string) error { - // Check if table exists. - key := m.TableNameKey(dbName, tableName) - if err := m.CheckTableNameExists(key); err != nil { - return errors.Trace(err) - } - return m.txn.Clear(key) -} - -// DropDatabaseName drops a database name. -// Used by DropDatabase. -func (m *Meta) DropDatabaseName(dbName string) error { - // Check if database exists. - key := m.DatabaseNameKey(dbName) - if err := m.CheckDatabaseNameExists(key); err != nil { - return errors.Trace(err) - } - if err := m.txn.Clear(key); err != nil { - return errors.Trace(err) - } - - // iterate all tables - prefix := m.TableNameKey(dbName, "") - return m.txn.Iterate(prefix, prefix.PrefixNext(), func(key []byte, _ []byte) error { - return m.txn.Clear(key) - }) -} - -// ClearAllTableNames clears all table names. -func (m *Meta) ClearAllTableNames() error { - prefix := kv.Key(fmt.Sprintf("%s:", mNames)) - return m.txn.Iterate(prefix, prefix.PrefixNext(), func(key []byte, _ []byte) error { - return m.txn.Clear(key) - }) -} - -// ClearAllDatabaseNames clears all database names. -func (m *Meta) ClearAllDatabaseNames() error { - prefix := kv.Key(fmt.Sprintf("%s:", mDBNames)) - return m.txn.Iterate(prefix, prefix.PrefixNext(), func(key []byte, _ []byte) error { - return m.txn.Clear(key) - }) -} - -// SetFastCreateTableInitialized set fast create table initialized. -func (m *Meta) SetFastCreateTableInitialized(b bool) error { - var data []byte - if b { - data = []byte("1") - } else { - data = []byte("0") - } - if err := m.txn.Set(mDDLV2Initialized, data); err != nil { - return errors.Trace(err) - } - return errors.Trace(m.txn.Set(mDBNameInitialized, data)) -} - -// GetFastCreateTableInitialized gets fast create table initialized. -func (m *Meta) GetFastCreateTableInitialized() (initialized bool, err error) { - val1, err := m.txn.Get(mDDLV2Initialized) - if err != nil { - return false, errors.Trace(err) - } - if len(val1) == 0 { - return false, nil - } - val2, err := m.txn.Get(mDBNameInitialized) - if err != nil { - return false, errors.Trace(err) - } - if len(val2) == 0 { - return false, nil - } - return bytes.Equal(val1, []byte("1")) && bytes.Equal(val2, []byte("1")), nil -} - // GroupRUStats keeps the ru consumption statistics data. type GroupRUStats struct { ID int64 `json:"id"` diff --git a/pkg/meta/meta_test.go b/pkg/meta/meta_test.go index c27ddb4b8829b..9f38a7aeaee9a 100644 --- a/pkg/meta/meta_test.go +++ b/pkg/meta/meta_test.go @@ -237,7 +237,7 @@ func TestMeta(t *testing.T) { Name: model.NewCIStr("t"), DBID: dbInfo.ID, } - err = m.CreateTableOrView(1, dbInfo.Name.L, tbInfo) + err = m.CreateTableOrView(1, tbInfo) require.NoError(t, err) n, err = m.GetAutoIDAccessors(1, 1).RowID().Inc(10) @@ -248,7 +248,7 @@ func TestMeta(t *testing.T) { require.NoError(t, err) require.Equal(t, int64(10), n) - err = m.CreateTableOrView(1, dbInfo.Name.L, tbInfo) + err = m.CreateTableOrView(1, tbInfo) require.NotNil(t, err) require.True(t, meta.ErrTableExists.Equal(err)) @@ -275,7 +275,7 @@ func TestMeta(t *testing.T) { Name: model.NewCIStr("bb"), DBID: dbInfo.ID, } - err = m.CreateTableOrView(1, dbInfo.Name.L, tbInfo2) + err = m.CreateTableOrView(1, tbInfo2) require.NoError(t, err) tblName := &model.TableNameInfo{ID: tbInfo.ID, Name: tbInfo.Name} @@ -309,7 +309,7 @@ func TestMeta(t *testing.T) { require.NoError(t, err) require.Equal(t, int64(10), n) - err = m.DropTableOrView(1, dbInfo.Name.L, tbInfo2.ID, tbInfo2.Name.L) + err = m.DropTableOrView(1, tbInfo2.ID) require.NoError(t, err) err = m.GetAutoIDAccessors(1, tbInfo2.ID).Del() require.NoError(t, err) @@ -341,7 +341,7 @@ func TestMeta(t *testing.T) { Name: model.NewCIStr("t_rename"), } // Create table. - err = m.CreateTableOrView(1, dbInfo.Name.L, tbInfo100) + err = m.CreateTableOrView(1, tbInfo100) require.NoError(t, err) // Update auto ID. currentDBID := int64(1) @@ -367,7 +367,7 @@ func TestMeta(t *testing.T) { ID: 3, Name: model.NewCIStr("tbl3"), } - err = m.CreateTableAndSetAutoID(1, dbInfo.Name.L, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0}) + err = m.CreateTableAndSetAutoID(1, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0}) require.NoError(t, err) id, err := m.GetAutoIDAccessors(1, tbInfo3.ID).RowID().Get() require.NoError(t, err) @@ -377,9 +377,9 @@ func TestMeta(t *testing.T) { require.Equal(t, []byte(strconv.FormatInt(1234, 10)), val) require.Equal(t, []byte{0x6d, 0x44, 0x42, 0x3a, 0x31, 0x0, 0x0, 0x0, 0x0, 0xfb, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x68, 0x54, 0x49, 0x44, 0x3a, 0x33, 0x0, 0x0, 0x0, 0xfc}, key) - err = m.DropDatabase(1, dbInfo.Name.L) + err = m.DropDatabase(1) require.NoError(t, err) - err = m.DropDatabase(currentDBID, dbInfo.Name.L) + err = m.DropDatabase(currentDBID) require.NoError(t, err) dbs, err = m.ListDatabases() @@ -655,111 +655,6 @@ func TestCreateMySQLDatabase(t *testing.T) { require.NoError(t, err) } -func TestName(t *testing.T) { - store, err := mockstore.NewMockStore() - require.NoError(t, err) - defer func() { - require.NoError(t, store.Close()) - }() - - txn, err := store.Begin() - require.NoError(t, err) - - // TestDatabaseNameKey - m := meta.NewMeta(txn) - key := m.DatabaseNameKey("db") - require.Equal(t, string(key), "DBNames:db") - - // TestCheckDatabaseNameExists - err = m.CheckDatabaseNameExists(m.DatabaseNameKey("db")) - require.True(t, meta.ErrDBNotExists.Equal(err)) - // TestCheckDatabaseNameNotExists - err = m.CheckDatabaseNameNotExists(m.DatabaseNameKey("db")) - require.NoError(t, err) - // TestCreateDatabase - err = m.CreateDatabaseName("db", 1) - require.NoError(t, err) - err = m.CheckDatabaseNameExists(m.DatabaseNameKey("db")) - require.NoError(t, err) - err = m.CheckDatabaseNameNotExists(m.DatabaseNameKey("db")) - require.True(t, meta.ErrDBExists.Equal(err)) - - // TestTableNameKey - key = m.TableNameKey("db", "tb") - require.Equal(t, string(key), "Names:db\x00tb") - - // TestCheckTableNameExists - err = m.CheckTableNameExists(m.TableNameKey("db", "tb")) - require.True(t, meta.ErrTableNotExists.Equal(err)) - // TestCheckTableNameNotExists - err = m.CheckTableNameNotExists(m.TableNameKey("db", "tb")) - require.NoError(t, err) - - // TestCreateTable - err = m.CreateTableName("db", "tb", 1) - require.NoError(t, err) - err = m.CheckTableNameExists(m.TableNameKey("db", "tb")) - require.NoError(t, err) - err = m.CheckTableNameNotExists(m.TableNameKey("db", "tb")) - require.True(t, meta.ErrTableExists.Equal(err)) - err = m.CreateTableName("db", "t", 2) - require.NoError(t, err) - - err = m.CreateTableName("db", "tb", 3) - require.True(t, meta.ErrTableExists.Equal(err)) - - err = m.CreateDatabaseName("d", 4) - require.NoError(t, err) - err = m.CreateTableName("d", "btb", 3) - require.NoError(t, err) - err = m.CheckTableNameExists(m.TableNameKey("d", "btb")) - require.NoError(t, err) - - // TestDropTableName - err = m.DropTableName("db1", "b") - require.True(t, meta.ErrTableNotExists.Equal(err)) - err = m.DropTableName("db", "tb") - require.NoError(t, err) - - // TestDropDatabaseName - err = m.DropDatabaseName("xx") - require.True(t, meta.ErrDBNotExists.Equal(err)) - err = m.DropDatabaseName("d") - require.NoError(t, err) - err = m.CheckTableNameNotExists(m.TableNameKey("d", "btb")) - require.NoError(t, err) - err = m.CheckTableNameExists(m.TableNameKey("db", "t")) - require.NoError(t, err) - - // TestClearAllTableNames - err = m.ClearAllTableNames() - require.NoError(t, err) - err = m.CheckTableNameNotExists(m.TableNameKey("db1", "t")) - require.NoError(t, err) - - // TestClearAllDatabaseNames - err = m.ClearAllDatabaseNames() - require.NoError(t, err) - - // TestFastCreateTableInitialized - v, err := m.GetFastCreateTableInitialized() - require.NoError(t, err) - require.Equal(t, v, false) - err = m.SetFastCreateTableInitialized(true) - require.NoError(t, err) - v, err = m.GetFastCreateTableInitialized() - require.NoError(t, err) - require.Equal(t, v, true) - err = m.SetFastCreateTableInitialized(false) - require.NoError(t, err) - v, err = m.GetFastCreateTableInitialized() - require.NoError(t, err) - require.Equal(t, v, false) - - err = txn.Rollback() - require.NoError(t, err) -} - func TestIsTableInfoMustLoad(t *testing.T) { tableInfo := &model.TableInfo{ TTLInfo: &model.TTLInfo{IntervalExprStr: "1", IntervalTimeUnit: int(ast.TimeUnitDay), JobInterval: "1h"}, diff --git a/pkg/session/session.go b/pkg/session/session.go index 3f22b1f154aae..730b5f6cf52ad 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -3256,7 +3256,7 @@ func createAndSplitTables(store kv.Storage, t *meta.Meta, dbID int64, tables []t tblInfo.State = model.StatePublic tblInfo.ID = tbl.id tblInfo.UpdateTS = t.StartTS - err = t.CreateTableOrView(dbID, "", tblInfo) + err = t.CreateTableOrView(dbID, tblInfo) if err != nil { return errors.Trace(err) } @@ -3289,7 +3289,7 @@ func InitMDLTable(store kv.Storage) error { tblInfo.State = model.StatePublic tblInfo.ID = ddl.MDLTableID tblInfo.UpdateTS = t.StartTS - err = t.CreateTableOrView(dbID, "", tblInfo) + err = t.CreateTableOrView(dbID, tblInfo) if err != nil { return errors.Trace(err) }