From 8b23a8af6cef79838332affc18129e515cb3720c Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 9 Nov 2022 10:44:42 +0800 Subject: [PATCH 01/21] save --- ddl/db_integration_test.go | 1 + ddl/db_test.go | 1 + ddl/ddl_worker.go | 3 ++- ddl/table.go | 6 +++--- executor/infoschema_reader.go | 2 +- executor/insert_common.go | 5 +++-- executor/write.go | 2 +- meta/autoid/autoid.go | 26 +++++++++++++++++++++++--- meta/autoid/memid.go | 6 +++++- meta/meta_autoid.go | 10 +++++++++- table/table.go | 5 +++-- table/tables/tables.go | 1 + 12 files changed, 53 insertions(+), 15 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index b7cb35a9f8727..c09737b7f843f 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2923,6 +2923,7 @@ func TestAutoIncrementForce(t *testing.T) { tk.MustExec("create table t (a int);") tk.MustExec("alter table t force auto_increment = 2;") tk.MustExec("insert into t values (1),(2);") + fmt.Println("6666666666666666666666666666") tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3")) // Cannot set next global ID to 0. tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed) diff --git a/ddl/db_test.go b/ddl/db_test.go index 207aad4e0f116..1b95321cccab3 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -744,6 +744,7 @@ func TestRebaseAutoID(t *testing.T) { tk.MustExec("create table tidb.test (a int auto_increment primary key, b int);") tk.MustExec("insert tidb.test values (null, 1);") tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1")) + fmt.Println("=====================================") tk.MustExec("alter table tidb.test auto_increment = 6000;") tk.MustExec("insert tidb.test values (null, 1);") tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1", "6000 1")) diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index ee7e7f3c6594e..eb982e8d27708 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -1232,7 +1232,8 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, case model.ActionTruncateTable: ver, err = onTruncateTable(d, t, job) case model.ActionRebaseAutoID: - ver, err = onRebaseRowIDType(d, t, job) + fmt.Println("yyyyyyyyyyyyyyyyyyyyyyyyyyy rebase auto id??") + ver, err = onRebaseAutoIncrementIDType(d, t, job) case model.ActionRebaseAutoRandomBase: ver, err = onRebaseAutoRandomType(d, t, job) case model.ActionRenameTable: diff --git a/ddl/table.go b/ddl/table.go index df329fd973c06..50ee0de0da0c3 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -812,8 +812,8 @@ func onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ erro return ver, nil } -func onRebaseRowIDType(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) { - return onRebaseAutoID(d, d.store, t, job, autoid.RowIDAllocType) +func onRebaseAutoIncrementIDType(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) { + return onRebaseAutoID(d, d.store, t, job, autoid.AutoIncrementType) } func onRebaseAutoRandomType(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) { @@ -862,7 +862,7 @@ func onRebaseAutoID(d *ddlCtx, store kv.Storage, t *meta.Meta, job *model.Job, t newBase = newBaseTemp } - if tp == autoid.RowIDAllocType { + if tp == autoid.AutoIncrementType { tblInfo.AutoIncID = newBase } else { tblInfo.AutoRandID = newBase diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 6d4e10c3a72a7..990eaacd9630c 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -386,7 +386,7 @@ func getAutoIncrementID(ctx sessionctx.Context, schema *model.DBInfo, tblInfo *m if err != nil { return 0, err } - return tbl.Allocators(ctx).Get(autoid.RowIDAllocType).Base() + 1, nil + return tbl.Allocators(ctx).Get(autoid.AutoIncrementType).Base() + 1, nil } func hasPriv(ctx sessionctx.Context, priv mysql.PrivilegeType) bool { diff --git a/executor/insert_common.go b/executor/insert_common.go index 5bb7feb2441da..d0146d9c3922a 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -778,7 +778,8 @@ func (e *InsertValues) lazyAdjustAutoIncrementDatum(ctx context.Context, rows [] } // Use the value if it's not null and not 0. if recordID != 0 { - err = e.Table.Allocators(e.ctx).Get(autoid.RowIDAllocType).Rebase(ctx, recordID, true) + alloc := e.Table.Allocators(e.ctx).Get(autoid.AutoIncrementType) + err = alloc.Rebase(ctx, recordID, true) if err != nil { return nil, err } @@ -871,7 +872,7 @@ func (e *InsertValues) adjustAutoIncrementDatum(ctx context.Context, d types.Dat } // Use the value if it's not null and not 0. if recordID != 0 { - err = e.Table.Allocators(e.ctx).Get(autoid.RowIDAllocType).Rebase(ctx, recordID, true) + err = e.Table.Allocators(e.ctx).Get(autoid.AutoIncrementType).Rebase(ctx, recordID, true) if err != nil { return types.Datum{}, err } diff --git a/executor/write.go b/executor/write.go index 01359b56a2571..f0c851e97a9eb 100644 --- a/executor/write.go +++ b/executor/write.go @@ -115,7 +115,7 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old if err != nil { return false, err } - if err = t.Allocators(sctx).Get(autoid.RowIDAllocType).Rebase(ctx, recordID, true); err != nil { + if err = t.Allocators(sctx).Get(autoid.AutoIncrementType).Rebase(ctx, recordID, true); err != nil { return false, err } } diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index def3245bb2da3..89e65c432d3b4 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -22,6 +22,7 @@ import ( "strconv" "sync" "time" + "runtime/debug" "github.com/opentracing/opentracing-go" "github.com/pingcap/errors" @@ -281,6 +282,7 @@ func (alloc *allocator) NextGlobalAutoID() (int64, error) { if err1 != nil { return errors.Trace(err1) } + fmt.Println("get next global auto id ==", autoID, "alloc type ==", alloc.allocType) return nil }) metrics.AutoIDHistogram.WithLabelValues(metrics.GlobalAutoID, metrics.RetLabel(err)).Observe(time.Since(startTime).Seconds()) @@ -377,6 +379,7 @@ func (alloc *allocator) rebase4Signed(ctx context.Context, requiredBase int64, a if allocatorStats != nil { txn.SetOption(kv.CollectRuntimeStats, allocatorStats.SnapshotRuntimeStats) } + fmt.Println("alloc type ===", alloc.allocType, "new base ==", requiredBase) idAcc := alloc.getIDAccessor(txn) currentEnd, err1 := idAcc.Get() if err1 != nil { @@ -455,6 +458,8 @@ func (alloc *allocator) rebase4Sequence(requiredBase int64) (int64, bool, error) // The requiredBase is the minimum base value after Rebase. // The real base may be greater than the required base. func (alloc *allocator) Rebase(ctx context.Context, requiredBase int64, allocIDs bool) error { + debug.PrintStack() + fmt.Println("in Rebase, requiredBase ==", requiredBase) alloc.mu.Lock() defer alloc.mu.Unlock() if alloc.isUnsigned { @@ -465,6 +470,7 @@ func (alloc *allocator) Rebase(ctx context.Context, requiredBase int64, allocIDs // ForceRebase implements autoid.Allocator ForceRebase interface. func (alloc *allocator) ForceRebase(requiredBase int64) error { + debug.PrintStack() if requiredBase == -1 { return ErrAutoincReadFailed.GenWithStack("Cannot force rebase the next global ID to '0'") } @@ -593,7 +599,7 @@ func NewAllocator(store kv.Storage, dbID, tbID int64, isUnsigned bool, } // Use the MySQL compatible AUTO_INCREMENT mode. - if allocType == RowIDAllocType && alloc.customStep && alloc.step == 1 { + if allocType == AutoIncrementType && alloc.customStep && alloc.step == 1 { alloc1 := newSinglePointAlloc(store, dbID, tbID, isUnsigned) if alloc1 != nil { return alloc1 @@ -619,6 +625,7 @@ func NewSequenceAllocator(store kv.Storage, dbID, tbID int64, info *model.Sequen // NewAllocatorsFromTblInfo creates an array of allocators of different types with the information of model.TableInfo. func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.TableInfo) Allocators { + // debug.PrintStack() var allocs []Allocator dbID := tblInfo.GetDBID(schemaID) idCacheOpt := CustomAutoIncCacheOption(tblInfo.AutoIdCache) @@ -626,10 +633,14 @@ func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.T hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil - if hasRowID || hasAutoIncID { + if hasRowID { alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) allocs = append(allocs, alloc) } + if hasAutoIncID { + alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt, tblVer) + allocs = append(allocs, alloc) + } hasAutoRandID := tblInfo.ContainsAutoRandomBits() if hasAutoRandID { alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt, tblVer) @@ -696,11 +707,14 @@ func CalcNeededBatchSize(base, n, increment, offset int64, isUnsigned bool) int6 nr := SeekToFirstAutoIDUnSigned(uint64(base), uint64(increment), uint64(offset)) // Calculate the total batch size needed. nr += (uint64(n) - 1) * uint64(increment) + fmt.Println("fuck is unsigned??", nr, base) return int64(nr - uint64(base)) } nr := SeekToFirstAutoIDSigned(base, increment, offset) + fmt.Println("seek to first auto signed", nr) // Calculate the total batch size needed. nr += (n - 1) * increment + fmt.Println("then xxx", nr, (n-1), increment) return nr - base } @@ -839,10 +853,12 @@ func (alloc *allocator) alloc4Signed(ctx context.Context, n uint64, increment, o var newBase, newEnd int64 startTime := time.Now() nextStep := alloc.step - if !alloc.customStep { + fmt.Println("alloc.step ==", alloc.step) + if !alloc.customStep && alloc.end > 0 { // Although it may skip a segment here, we still think it is consumed. consumeDur := startTime.Sub(alloc.lastAllocTime) nextStep = NextStep(alloc.step, consumeDur) + fmt.Println("next.step ==", nextStep, alloc.lastAllocTime) } ctx, allocatorStats, commitDetail := getAllocatorStatsFromCtx(ctx) @@ -872,17 +888,21 @@ func (alloc *allocator) alloc4Signed(ctx context.Context, n uint64, increment, o if err1 != nil { return err1 } + fmt.Println("alloc type ==", alloc.allocType, "get", newBase) // CalcNeededBatchSize calculates the total batch size needed on global base. n1 = CalcNeededBatchSize(newBase, int64(n), increment, offset, alloc.isUnsigned) // Although the step is customized by user, we still need to make sure nextStep is big enough for insert batch. if nextStep < n1 { nextStep = n1 } + fmt.Println("newBase", newBase, "nextStep", nextStep, n, increment, offset, n1) tmpStep := mathutil.Min(math.MaxInt64-newBase, nextStep) // The global rest is not enough for alloc. if tmpStep < n1 { return ErrAutoincReadFailed } + + fmt.Println("............................", tmpStep) newEnd, err1 = idAcc.Inc(tmpStep) return err1 }) diff --git a/meta/autoid/memid.go b/meta/autoid/memid.go index f3a3b50bf5275..b712dc5b70ec6 100644 --- a/meta/autoid/memid.go +++ b/meta/autoid/memid.go @@ -26,11 +26,15 @@ func NewAllocatorFromTempTblInfo(tblInfo *model.TableInfo) Allocator { hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil var alloc Allocator + allocType := RowIDAllocType + if hasAutoIncID { + allocType = AutoIncrementType + } // Temporary tables don't support auto_random and sequence. if hasRowID || hasAutoIncID { alloc = &inMemoryAllocator{ isUnsigned: tblInfo.IsAutoIncColUnsigned(), - allocType: RowIDAllocType, + allocType: allocType, } } // Rebase the allocator if the base is specified. diff --git a/meta/meta_autoid.go b/meta/meta_autoid.go index 18d384b2b25a7..e3e4d3ceda400 100644 --- a/meta/meta_autoid.go +++ b/meta/meta_autoid.go @@ -15,7 +15,9 @@ package meta import ( + "fmt" "strconv" + "runtime/debug" "github.com/pingcap/errors" "github.com/pingcap/tidb/parser/model" @@ -42,18 +44,23 @@ type autoIDAccessor struct { // Get implements the interface AutoIDAccessor. func (a *autoIDAccessor) Get() (int64, error) { m := a.m - return m.txn.HGetInt64(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID)) + ret, err := m.txn.HGetInt64(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID)) + fmt.Println("get ==", a.databaseID, a.tableID, ret) + return ret, err } // Put implements the interface AutoIDAccessor. func (a *autoIDAccessor) Put(val int64) error { m := a.m + fmt.Println("id accessor put ===", val) return m.txn.HSet(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID), []byte(strconv.FormatInt(val, 10))) } // Inc implements the interface AutoIDAccessor. func (a *autoIDAccessor) Inc(step int64) (int64, error) { m := a.m + fmt.Println("id accessor inc ===", step, a.databaseID, a.tableID) + debug.PrintStack() dbKey := m.dbKey(a.databaseID) if err := m.checkDBExists(dbKey); err != nil { return 0, errors.Trace(err) @@ -120,6 +127,7 @@ func (a *autoIDAccessors) Get() (autoIDs AutoIDGroup, err error) { // Put implements the interface AutoIDAccessors. func (a *autoIDAccessors) Put(autoIDs AutoIDGroup) error { + fmt.Println("id accessors put ===", autoIDs) if err := a.RowID().Put(autoIDs.RowID); err != nil { return err } diff --git a/table/table.go b/table/table.go index 6aef5ed4497e6..89fdfee3fcec0 100644 --- a/table/table.go +++ b/table/table.go @@ -206,7 +206,8 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte } increment := sctx.GetSessionVars().AutoIncrementIncrement offset := sctx.GetSessionVars().AutoIncrementOffset - _, max, err := t.Allocators(sctx).Get(autoid.RowIDAllocType).Alloc(ctx, uint64(1), int64(increment), int64(offset)) + alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType) + _, max, err := alloc.Alloc(ctx, uint64(1), int64(increment), int64(offset)) if err != nil { return 0, err } @@ -218,7 +219,7 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) (firstID int64, increment int64, err error) { increment = int64(sctx.GetSessionVars().AutoIncrementIncrement) offset := int64(sctx.GetSessionVars().AutoIncrementOffset) - min, max, err := t.Allocators(sctx).Get(autoid.RowIDAllocType).Alloc(ctx, uint64(N), increment, offset) + min, max, err := t.Allocators(sctx).Get(autoid.AutoIncrementType).Alloc(ctx, uint64(N), increment, offset) if err != nil { return min, max, err } diff --git a/table/tables/tables.go b/table/tables/tables.go index 1961e0cd335e8..61b49b774c2ee 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -1542,6 +1542,7 @@ func (t *TableCommon) Allocators(ctx sessionctx.Context) autoid.Allocators { // Use an independent allocator for global temporary tables. if t.meta.TempTableType == model.TempTableGlobal { if alloc := ctx.GetSessionVars().GetTemporaryTable(t.meta).GetAutoIDAllocator(); alloc != nil { + fmt.Println("global temporary table ... change auto inc to ", alloc) return autoid.Allocators{alloc} } // If the session is not in a txn, for example, in "show create table", use the original allocator. From 416e758f8b9ca7869bbdc8bc75ccca919112de31 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 9 Nov 2022 23:26:44 +0800 Subject: [PATCH 02/21] save --- br/pkg/backup/client.go | 11 ++-- br/pkg/lightning/backend/kv/allocator.go | 1 + ddl/db_integration_test.go | 1 + ddl/ddl_api.go | 4 +- ddl/table_test.go | 2 +- executor/executor.go | 2 +- executor/executor_test.go | 2 +- executor/insert_common.go | 1 + infoschema/builder.go | 19 +++--- infoschema/infoschema.go | 2 +- infoschema/tables.go | 4 +- meta/autoid/autoid.go | 79 +++++++++++++++++------- table/tables/tables.go | 16 +++-- table/temptable/ddl.go | 2 +- 14 files changed, 96 insertions(+), 50 deletions(-) diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index 6ce3d24b0e209..652fa0b0f52c2 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -380,10 +380,13 @@ func BuildBackupRangeAndSchema( zap.String("table", tableInfo.Name.O), ) - tblVer := autoid.AllocOptionTableInfoVersion(tableInfo.Version) - idAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.RowIDAllocType, tblVer) - seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.SequenceType, tblVer) - randAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.AutoRandomType, tblVer) + // tblVer := autoid.AllocOptionTableInfoVersion(tableInfo.Version) + // idAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.RowIDAllocType, tblVer) + // seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.SequenceType, tblVer) + // randAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.AutoRandomType, tblVer) + idAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.RowIDAllocType) + seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.SequenceType) + randAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.AutoRandomType) var globalAutoID int64 switch { diff --git a/br/pkg/lightning/backend/kv/allocator.go b/br/pkg/lightning/backend/kv/allocator.go index 02f46ea8c7e36..14703e1143a45 100644 --- a/br/pkg/lightning/backend/kv/allocator.go +++ b/br/pkg/lightning/backend/kv/allocator.go @@ -34,6 +34,7 @@ type panickingAllocator struct { func NewPanickingAllocators(base int64) autoid.Allocators { sharedBase := &base return autoid.NewAllocators( + false, &panickingAllocator{base: sharedBase, ty: autoid.RowIDAllocType}, &panickingAllocator{base: sharedBase, ty: autoid.AutoIncrementType}, &panickingAllocator{base: sharedBase, ty: autoid.AutoRandomType}, diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index c09737b7f843f..6eeeb3a13c6cf 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2690,6 +2690,7 @@ func TestCreateTableWithAutoIdCache(t *testing.T) { // Test both auto_increment and rowid exist. tk.MustExec("drop table if exists t;") tk.MustExec("drop table if exists t1;") + fmt.Println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") tk.MustExec("create table t(a int null, b int auto_increment unique) auto_id_cache 100") _, err = dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t")) require.NoError(t, err) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index e67c0eb001128..d864c46f6925e 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2147,7 +2147,7 @@ func checkPartitionDefinitionConstraints(ctx sessionctx.Context, tbInfo *model.T // checkTableInfoValid uses to check table info valid. This is used to validate table info. func checkTableInfoValid(tblInfo *model.TableInfo) error { - _, err := tables.TableFromMeta(nil, tblInfo) + _, err := tables.TableFromMeta(autoid.Allocators{}, tblInfo) if err != nil { return err } @@ -3445,6 +3445,8 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6 actionType = model.ActionRebaseAutoRandomBase case autoid.RowIDAllocType: actionType = model.ActionRebaseAutoID + default: + panic("fuck!") } if !force { diff --git a/ddl/table_test.go b/ddl/table_test.go index a9320e01cc781..75a0a549e06c6 100644 --- a/ddl/table_test.go +++ b/ddl/table_test.go @@ -158,7 +158,7 @@ func testGetTableWithError(store kv.Storage, schemaID, tableID int64) (table.Tab return nil, errors.New("table not found") } alloc := autoid.NewAllocator(store, schemaID, tblInfo.ID, false, autoid.RowIDAllocType) - tbl, err := table.TableFromMeta(autoid.NewAllocators(alloc), tblInfo) + tbl, err := table.TableFromMeta(autoid.NewAllocators(false, alloc), tblInfo) if err != nil { return nil, errors.Trace(err) } diff --git a/executor/executor.go b/executor/executor.go index 542734e4b1a75..c65e8b790f8bd 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -397,7 +397,7 @@ func (e *ShowNextRowIDExec) Next(ctx context.Context, req *chunk.Chunk) error { tblMeta := tbl.Meta() allocators := tbl.Allocators(e.ctx) - for _, alloc := range allocators { + for _, alloc := range allocators.Allocs { nextGlobalID, err := alloc.NextGlobalAutoID() if err != nil { return err diff --git a/executor/executor_test.go b/executor/executor_test.go index dd03c68fd4d74..0bdecf204b8e7 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1904,7 +1904,7 @@ func TestCheckIndex(t *testing.T) { tbInfo := tbl.Meta() alloc := autoid.NewAllocator(store, dbInfo.ID, tbInfo.ID, false, autoid.RowIDAllocType) - tb, err := tables.TableFromMeta(autoid.NewAllocators(alloc), tbInfo) + tb, err := tables.TableFromMeta(autoid.NewAllocators(false, alloc), tbInfo) require.NoError(t, err) _, err = se.Execute(context.Background(), "admin check index t c") diff --git a/executor/insert_common.go b/executor/insert_common.go index d0146d9c3922a..e45dfc562afc7 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -816,6 +816,7 @@ func (e *InsertValues) lazyAdjustAutoIncrementDatum(ctx context.Context, rows [] // AllocBatchAutoIncrementValue allocates batch N consecutive autoIDs. // The max value can be derived from adding the increment value to min for cnt-1 times. min, increment, err := table.AllocBatchAutoIncrementValue(ctx, e.Table, e.ctx, cnt) + fmt.Println("here min, increment ==", min, increment) if e.handleErr(col, &autoDatum, cnt, err) != nil { return nil, err } diff --git a/infoschema/builder.go b/infoschema/builder.go index 8ff50a8b50435..fc2d6673f309e 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -693,18 +693,20 @@ func (b *Builder) applyCreateTable(m *meta.Meta, dbInfo *model.DBInfo, tableID i ConvertCharsetCollateToLowerCaseIfNeed(tblInfo) ConvertOldVersionUTF8ToUTF8MB4IfNeed(tblInfo) - if len(allocs) == 0 { + if len(allocs.Allocs) == 0 { allocs = autoid.NewAllocatorsFromTblInfo(b.store, dbInfo.ID, tblInfo) } else { - tblVer := autoid.AllocOptionTableInfoVersion(tblInfo.Version) + // tblVer := autoid.AllocOptionTableInfoVersion(tblInfo.Version) switch tp { case model.ActionRebaseAutoID, model.ActionModifyTableAutoIdCache: idCacheOpt := autoid.CustomAutoIncCacheOption(tblInfo.AutoIdCache) - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, tblVer, idCacheOpt) - allocs = append(allocs, newAlloc) + // newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, tblVer, idCacheOpt) + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, idCacheOpt) + allocs = allocs.Append(newAlloc) case model.ActionRebaseAutoRandomBase: - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) - allocs = append(allocs, newAlloc) + // newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType) + allocs = allocs.Append(newAlloc) case model.ActionModifyColumn: // Change column attribute from auto_increment to auto_random. if tblInfo.ContainsAutoRandomBits() && allocs.Get(autoid.AutoRandomType) == nil { @@ -712,8 +714,9 @@ func (b *Builder) applyCreateTable(m *meta.Meta, dbInfo *model.DBInfo, tableID i allocs = allocs.Filter(func(a autoid.Allocator) bool { return a.GetType() != autoid.AutoIncrementType && a.GetType() != autoid.RowIDAllocType }) - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) - allocs = append(allocs, newAlloc) + // newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType) + allocs = allocs.Append(newAlloc) } } } diff --git a/infoschema/infoschema.go b/infoschema/infoschema.go index d024afde40d7d..18318ae3e1999 100644 --- a/infoschema/infoschema.go +++ b/infoschema/infoschema.go @@ -270,7 +270,7 @@ func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) { func (is *infoSchema) AllocByID(id int64) (autoid.Allocators, bool) { tbl, ok := is.TableByID(id) if !ok { - return nil, false + return autoid.Allocators{}, false } return tbl.Allocators(nil), true } diff --git a/infoschema/tables.go b/infoschema/tables.go index 6f8a5e6cab211..93e3425b86a65 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -2131,7 +2131,7 @@ func (it *infoschemaTable) UpdateRecord(gctx context.Context, ctx sessionctx.Con // Allocators implements table.Table Allocators interface. func (it *infoschemaTable) Allocators(_ sessionctx.Context) autoid.Allocators { - return nil + return autoid.Allocators{} } // Meta implements table.Table Meta interface. @@ -2214,7 +2214,7 @@ func (vt *VirtualTable) UpdateRecord(ctx context.Context, sctx sessionctx.Contex // Allocators implements table.Table Allocators interface. func (vt *VirtualTable) Allocators(_ sessionctx.Context) autoid.Allocators { - return nil + return autoid.Allocators{} } // Meta implements table.Table Meta interface. diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index 89e65c432d3b4..3c9cbb1d7a9f2 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -155,13 +155,13 @@ func (step CustomAutoIncCacheOption) ApplyOn(alloc *allocator) { alloc.customStep = true } -// AllocOptionTableInfoVersion is used to pass the TableInfo.Version to the allocator. -type AllocOptionTableInfoVersion uint16 +// // AllocOptionTableInfoVersion is used to pass the TableInfo.Version to the allocator. +// type AllocOptionTableInfoVersion uint16 -// ApplyOn implements the AllocOption interface. -func (v AllocOptionTableInfoVersion) ApplyOn(alloc *allocator) { - alloc.tbVersion = uint16(v) -} +// // ApplyOn implements the AllocOption interface. +// func (v AllocOptionTableInfoVersion) ApplyOn(alloc *allocator) { +// alloc.tbVersion = uint16(v) +// } // AllocOption is a interface to define allocator custom options coming in future. type AllocOption interface { @@ -206,16 +206,35 @@ type Allocator interface { } // Allocators represents a set of `Allocator`s. -type Allocators []Allocator +type Allocators struct { + SepAutoInc bool + Allocs []Allocator +} // NewAllocators packs multiple `Allocator`s into Allocators. -func NewAllocators(allocators ...Allocator) Allocators { - return allocators +func NewAllocators(sepAutoInc bool, allocators ...Allocator) Allocators { + return Allocators{ + SepAutoInc: sepAutoInc, + Allocs: allocators, + } +} + +func (all Allocators) Append(a Allocator) Allocators { + return Allocators{ + SepAutoInc: all.SepAutoInc, + Allocs: append(all.Allocs, a), + } } // Get returns the Allocator according to the AllocatorType. func (all Allocators) Get(allocType AllocatorType) Allocator { - for _, a := range all { + if !all.SepAutoInc { + if allocType == AutoIncrementType { + allocType = RowIDAllocType + } + } + + for _, a := range all.Allocs { if a.GetType() == allocType { return a } @@ -225,13 +244,16 @@ func (all Allocators) Get(allocType AllocatorType) Allocator { // Filter filters all the allocators that match pred. func (all Allocators) Filter(pred func(Allocator) bool) Allocators { - var ret Allocators - for _, a := range all { + var ret []Allocator + for _, a := range all.Allocs { if pred(a) { ret = append(ret, a) } } - return ret + return Allocators{ + SepAutoInc: all.SepAutoInc, + Allocs: ret, + } } type allocator struct { @@ -242,7 +264,8 @@ type allocator struct { // dbID is current database's ID. dbID int64 tbID int64 - tbVersion uint16 + // tbVersion uint16 + sepAutoInc bool isUnsigned bool lastAllocTime time.Time step int64 @@ -481,6 +504,7 @@ func (alloc *allocator) ForceRebase(requiredBase int64) error { err := kv.RunInNewTxn(ctx, alloc.store, true, func(ctx context.Context, txn kv.Transaction) error { idAcc := alloc.getIDAccessor(txn) currentEnd, err1 := idAcc.Get() + fmt.Println("force rebase to ==", requiredBase, "and alloc type is", alloc.allocType, alloc.sepAutoInc) if err1 != nil { return err1 } @@ -629,27 +653,28 @@ func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.T var allocs []Allocator dbID := tblInfo.GetDBID(schemaID) idCacheOpt := CustomAutoIncCacheOption(tblInfo.AutoIdCache) - tblVer := AllocOptionTableInfoVersion(tblInfo.Version) + // tblVer := AllocOptionTableInfoVersion(tblInfo.Version) hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil - if hasRowID { - alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) - allocs = append(allocs, alloc) - } - if hasAutoIncID { - alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt, tblVer) + if hasRowID || hasAutoIncID { + // alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) + alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt) allocs = append(allocs, alloc) + // alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt, tblVer) + // alloc = NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt) + // allocs = append(allocs, alloc) } hasAutoRandID := tblInfo.ContainsAutoRandomBits() if hasAutoRandID { - alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt, tblVer) + // alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt, tblVer) + alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt) allocs = append(allocs, alloc) } if tblInfo.IsSequence() { allocs = append(allocs, NewSequenceAllocator(store, dbID, tblInfo.ID, tblInfo.Sequence)) } - return NewAllocators(allocs...) + return NewAllocators(tblInfo.AutoIdCache == 1, allocs...) } // Alloc implements autoid.Allocator Alloc interface. @@ -1145,7 +1170,13 @@ func (alloc *allocator) getIDAccessor(txn kv.Transaction) meta.AutoIDAccessor { case RowIDAllocType: return acc.RowID() case AutoIncrementType: - return acc.IncrementID(alloc.tbVersion) + fmt.Println("sepAutoInc ==", alloc.sepAutoInc) + if alloc.sepAutoInc { + return acc.IncrementID(model.TableInfoVersion4 + 1) + } else { + return acc.RowID() + // return acc.IncrementID(0) + } case AutoRandomType: return acc.RandomID() case SequenceType: diff --git a/table/tables/tables.go b/table/tables/tables.go index 61b49b774c2ee..e3b5769cdaecf 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -85,7 +85,7 @@ func MockTableFromMeta(tblInfo *model.TableInfo) table.Table { } var t TableCommon - initTableCommon(&t, tblInfo, tblInfo.ID, columns, nil) + initTableCommon(&t, tblInfo, tblInfo.ID, columns, autoid.Allocators{}) if tblInfo.TableCacheStatusType != model.TableCacheStatusDisable { ret, err := newCachedTable(&t) if err != nil { @@ -1543,7 +1543,10 @@ func (t *TableCommon) Allocators(ctx sessionctx.Context) autoid.Allocators { if t.meta.TempTableType == model.TempTableGlobal { if alloc := ctx.GetSessionVars().GetTemporaryTable(t.meta).GetAutoIDAllocator(); alloc != nil { fmt.Println("global temporary table ... change auto inc to ", alloc) - return autoid.Allocators{alloc} + // return &autoid.Allocators{ + // allocs: alloc, + // } + return autoid.NewAllocators(false, alloc) } // If the session is not in a txn, for example, in "show create table", use the original allocator. // Otherwise the would be a nil pointer dereference. @@ -1553,8 +1556,9 @@ func (t *TableCommon) Allocators(ctx sessionctx.Context) autoid.Allocators { // Replace the row id allocator with the one in session variables. sessAlloc := ctx.GetSessionVars().IDAllocator - retAllocs := make([]autoid.Allocator, 0, len(t.allocs)) - copy(retAllocs, t.allocs) + allocs := t.allocs.Allocs + retAllocs := make([]autoid.Allocator, 0, len(allocs)) + copy(retAllocs, allocs) overwritten := false for i, a := range retAllocs { @@ -1567,7 +1571,7 @@ func (t *TableCommon) Allocators(ctx sessionctx.Context) autoid.Allocators { if !overwritten { retAllocs = append(retAllocs, sessAlloc) } - return retAllocs + return autoid.NewAllocators(t.allocs.SepAutoInc, retAllocs...) } // Type implements table.Table Type interface. @@ -1921,7 +1925,7 @@ func maxIndexLen(idxA, idxB *model.IndexColumn) *model.IndexColumn { } func getSequenceAllocator(allocs autoid.Allocators) (autoid.Allocator, error) { - for _, alloc := range allocs { + for _, alloc := range allocs.Allocs { if alloc.GetType() == autoid.SequenceType { return alloc, nil } diff --git a/table/temptable/ddl.go b/table/temptable/ddl.go index ccad2b7b0214c..d464cb3c48618 100644 --- a/table/temptable/ddl.go +++ b/table/temptable/ddl.go @@ -182,7 +182,7 @@ func newTemporaryTableFromTableInfo(sctx sessionctx.Context, tbInfo *model.Table if alloc != nil { allocs = append(allocs, alloc) } - return tables.TableFromMeta(allocs, tbInfo) + return tables.TableFromMeta(autoid.NewAllocators(false, allocs...), tbInfo) } // GetTemporaryTableDDL gets the temptable.TemporaryTableDDL from session context From e3cf9f36cc9e156b299ca2eb645b4357c4c1c642 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Wed, 9 Nov 2022 23:40:27 +0800 Subject: [PATCH 03/21] save again --- meta/autoid/autoid.go | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index 3c9cbb1d7a9f2..b6d4cfad4c781 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -155,13 +155,13 @@ func (step CustomAutoIncCacheOption) ApplyOn(alloc *allocator) { alloc.customStep = true } -// // AllocOptionTableInfoVersion is used to pass the TableInfo.Version to the allocator. -// type AllocOptionTableInfoVersion uint16 +// AllocOptionTableInfoVersion is used to pass the TableInfo.Version to the allocator. +type AllocOptionTableInfoVersion uint16 -// // ApplyOn implements the AllocOption interface. -// func (v AllocOptionTableInfoVersion) ApplyOn(alloc *allocator) { -// alloc.tbVersion = uint16(v) -// } +// ApplyOn implements the AllocOption interface. +func (v AllocOptionTableInfoVersion) ApplyOn(alloc *allocator) { + alloc.tbVersion = uint16(v) +} // AllocOption is a interface to define allocator custom options coming in future. type AllocOption interface { @@ -264,8 +264,7 @@ type allocator struct { // dbID is current database's ID. dbID int64 tbID int64 - // tbVersion uint16 - sepAutoInc bool + tbVersion uint16 isUnsigned bool lastAllocTime time.Time step int64 @@ -504,7 +503,6 @@ func (alloc *allocator) ForceRebase(requiredBase int64) error { err := kv.RunInNewTxn(ctx, alloc.store, true, func(ctx context.Context, txn kv.Transaction) error { idAcc := alloc.getIDAccessor(txn) currentEnd, err1 := idAcc.Get() - fmt.Println("force rebase to ==", requiredBase, "and alloc type is", alloc.allocType, alloc.sepAutoInc) if err1 != nil { return err1 } @@ -653,22 +651,17 @@ func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.T var allocs []Allocator dbID := tblInfo.GetDBID(schemaID) idCacheOpt := CustomAutoIncCacheOption(tblInfo.AutoIdCache) - // tblVer := AllocOptionTableInfoVersion(tblInfo.Version) + tblVer := AllocOptionTableInfoVersion(tblInfo.Version) hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil if hasRowID || hasAutoIncID { - // alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) - alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt) + alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) allocs = append(allocs, alloc) - // alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt, tblVer) - // alloc = NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt) - // allocs = append(allocs, alloc) } hasAutoRandID := tblInfo.ContainsAutoRandomBits() if hasAutoRandID { - // alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt, tblVer) - alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt) + alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt, tblVer) allocs = append(allocs, alloc) } if tblInfo.IsSequence() { @@ -1170,13 +1163,7 @@ func (alloc *allocator) getIDAccessor(txn kv.Transaction) meta.AutoIDAccessor { case RowIDAllocType: return acc.RowID() case AutoIncrementType: - fmt.Println("sepAutoInc ==", alloc.sepAutoInc) - if alloc.sepAutoInc { - return acc.IncrementID(model.TableInfoVersion4 + 1) - } else { - return acc.RowID() - // return acc.IncrementID(0) - } + return acc.IncrementID(alloc.tbVersion) case AutoRandomType: return acc.RandomID() case SequenceType: From 4aca589e80745ca534a883397ce91c096757cb40 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 10 Nov 2022 09:37:54 +0800 Subject: [PATCH 04/21] add tests --- autoid_service/autoid.go | 11 ++++++----- executor/executor_issue_test.go | 23 +++++++++++++++++++++++ meta/autoid/autoid.go | 10 ++++++++++ meta/autoid/autoid_service.go | 2 +- meta/autoid/memid.go | 6 +++--- table/table.go | 3 ++- 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/autoid_service/autoid.go b/autoid_service/autoid.go index 7e0edb7ea3e63..a0b0334c141a4 100644 --- a/autoid_service/autoid.go +++ b/autoid_service/autoid.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/metrics" + "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/owner" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/mathutil" @@ -73,7 +74,7 @@ func (alloc *autoIDValue) alloc4Unsigned(ctx context.Context, store kv.Storage, ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).RowID() + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) var err1 error newBase, err1 = idAcc.Get() if err1 != nil { @@ -134,7 +135,7 @@ func (alloc *autoIDValue) alloc4Signed(ctx context.Context, ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).RowID() + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) var err1 error newBase, err1 = idAcc.Get() if err1 != nil { @@ -185,7 +186,7 @@ func (alloc *autoIDValue) rebase4Unsigned(ctx context.Context, startTime := time.Now() ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).RowID() + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) currentEnd, err1 := idAcc.Get() if err1 != nil { return err1 @@ -218,7 +219,7 @@ func (alloc *autoIDValue) rebase4Signed(ctx context.Context, store kv.Storage, d var newBase, newEnd int64 ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).RowID() + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) currentEnd, err1 := idAcc.Get() if err1 != nil { return err1 @@ -418,7 +419,7 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (* func (alloc *autoIDValue) forceRebase(ctx context.Context, store kv.Storage, dbID, tblID, requiredBase int64, isUnsigned bool) error { ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).RowID() + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) currentEnd, err1 := idAcc.Get() if err1 != nil { return err1 diff --git a/executor/executor_issue_test.go b/executor/executor_issue_test.go index f0b52422c19a2..7d8a357c6fbc2 100644 --- a/executor/executor_issue_test.go +++ b/executor/executor_issue_test.go @@ -1235,3 +1235,26 @@ func TestIssue33214(t *testing.T) { } } } + +func TestIssue982(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + fmt.Println("88888888888888") + tk.MustExec("create table t (c int auto_increment, key(c)) auto_id_cache 1;") + tk.MustExec("insert into t values();") + tk.MustExec("insert into t values();") + tk.MustQuery("select * from t;").Check(testkit.Rows("1","2")) +} + +func TestIssue24627(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t;") + tk.MustExec("create table test(id float primary key AUTO_INCREMENT, col1 int) auto_id_cache 1;") + tk.MustExec("replace into test(col1) values(1);") + tk.MustExec("replace into test(col1) values(2);") + tk.MustQuery("select * from test;").Check(testkit.Rows("1 1", "2 2")) +} diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index b6d4cfad4c781..82bf10e924113 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -234,7 +234,10 @@ func (all Allocators) Get(allocType AllocatorType) Allocator { } } + fmt.Println("Get alloc ===", allocType) + for _, a := range all.Allocs { + fmt.Println("alloc a==", a, a.GetType()) if a.GetType() == allocType { return a } @@ -622,6 +625,7 @@ func NewAllocator(store kv.Storage, dbID, tbID int64, isUnsigned bool, // Use the MySQL compatible AUTO_INCREMENT mode. if allocType == AutoIncrementType && alloc.customStep && alloc.step == 1 { + fmt.Println("single point alloc ...") alloc1 := newSinglePointAlloc(store, dbID, tbID, isUnsigned) if alloc1 != nil { return alloc1 @@ -655,10 +659,16 @@ func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.T hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil + fmt.Println("hasAutoIncID ===", hasAutoIncID ) if hasRowID || hasAutoIncID { alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) allocs = append(allocs, alloc) } + if hasAutoIncID { + fmt.Println("run here ... and new ??") + alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt, tblVer) + allocs = append(allocs, alloc) + } hasAutoRandID := tblInfo.ContainsAutoRandomBits() if hasAutoRandID { alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), AutoRandomType, idCacheOpt, tblVer) diff --git a/meta/autoid/autoid_service.go b/meta/autoid/autoid_service.go index 6133dfdfc3cb2..79124a2cb4ca3 100644 --- a/meta/autoid/autoid_service.go +++ b/meta/autoid/autoid_service.go @@ -232,5 +232,5 @@ func (sp *singlePointAlloc) NextGlobalAutoID() (int64, error) { } func (*singlePointAlloc) GetType() AllocatorType { - return RowIDAllocType + return AutoIncrementType } diff --git a/meta/autoid/memid.go b/meta/autoid/memid.go index b712dc5b70ec6..23e5a94cc09d5 100644 --- a/meta/autoid/memid.go +++ b/meta/autoid/memid.go @@ -27,9 +27,9 @@ func NewAllocatorFromTempTblInfo(tblInfo *model.TableInfo) Allocator { hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil var alloc Allocator allocType := RowIDAllocType - if hasAutoIncID { - allocType = AutoIncrementType - } + // if hasAutoIncID { + // allocType = AutoIncrementType + // } // Temporary tables don't support auto_random and sequence. if hasRowID || hasAutoIncID { alloc = &inMemoryAllocator{ diff --git a/table/table.go b/table/table.go index 89fdfee3fcec0..813131df90896 100644 --- a/table/table.go +++ b/table/table.go @@ -219,7 +219,8 @@ func AllocAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Conte func AllocBatchAutoIncrementValue(ctx context.Context, t Table, sctx sessionctx.Context, N int) (firstID int64, increment int64, err error) { increment = int64(sctx.GetSessionVars().AutoIncrementIncrement) offset := int64(sctx.GetSessionVars().AutoIncrementOffset) - min, max, err := t.Allocators(sctx).Get(autoid.AutoIncrementType).Alloc(ctx, uint64(N), increment, offset) + alloc := t.Allocators(sctx).Get(autoid.AutoIncrementType) + min, max, err := alloc.Alloc(ctx, uint64(N), increment, offset) if err != nil { return min, max, err } From ccc647e9d7f784cd018fba43558e063bbce1de13 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 10 Nov 2022 09:54:35 +0800 Subject: [PATCH 05/21] update test --- executor/executor_issue_test.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/executor/executor_issue_test.go b/executor/executor_issue_test.go index 7d8a357c6fbc2..ce9c8e73368df 100644 --- a/executor/executor_issue_test.go +++ b/executor/executor_issue_test.go @@ -1252,9 +1252,30 @@ func TestIssue24627(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table test(id float primary key AUTO_INCREMENT, col1 int) auto_id_cache 1;") - tk.MustExec("replace into test(col1) values(1);") - tk.MustExec("replace into test(col1) values(2);") - tk.MustQuery("select * from test;").Check(testkit.Rows("1 1", "2 2")) + for _, sql := range []string{ + "create table test(id float primary key clustered AUTO_INCREMENT, col1 int);", + "create table test(id float primary key nonclustered AUTO_INCREMENT, col1 int) AUTO_ID_CACHE 1;", + } { + tk.MustExec("drop table if exists test;") + tk.MustExec(sql) + tk.MustExec("replace into test(col1) values(1);") + tk.MustExec("replace into test(col1) values(2);") + tk.MustQuery("select * from test;").Check(testkit.Rows("1 1", "2 2")) + tk.MustExec("drop table test") + } + + for _, sql := range []string{ + "create table test2(id double primary key clustered AUTO_INCREMENT, col1 int);", + "create table test2(id double primary key nonclustered AUTO_INCREMENT, col1 int) AUTO_ID_CACHE 1;", + }{ + tk.MustExec(sql) + tk.MustExec("replace into test2(col1) values(1);") + tk.MustExec("insert into test2(col1) values(1);") + tk.MustExec("replace into test2(col1) values(1);") + tk.MustExec("insert into test2(col1) values(1);") + tk.MustExec("replace into test2(col1) values(1);") + tk.MustExec("replace into test2(col1) values(1);") + tk.MustQuery("select * from test2").Check(testkit.Rows("1 1", "2 1", "3 1", "4 1", "5 1", "6 1")) + tk.MustExec("drop table test2") + } } From f3f14ff2525e58dd84734078c3984d9503593ed4 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Thu, 10 Nov 2022 14:24:58 +0800 Subject: [PATCH 06/21] cleanup --- autoid_service/autoid.go | 12 ++++++------ br/pkg/backup/client.go | 11 ++++------- ddl/db_integration_test.go | 2 -- ddl/db_test.go | 1 - ddl/ddl_api.go | 2 +- ddl/ddl_worker.go | 1 - executor/executor_issue_test.go | 5 ++--- executor/insert_common.go | 1 - infoschema/builder.go | 11 ++++------- meta/autoid/autoid.go | 31 +++++-------------------------- meta/autoid/memid.go | 6 +----- meta/meta_autoid.go | 8 +------- parser/model/model.go | 10 ++++++---- session/schema_amender_test.go | 5 +++-- table/tables/tables.go | 4 ---- table/tables/tables_test.go | 8 ++++---- table/temptable/main_test.go | 3 ++- 17 files changed, 39 insertions(+), 82 deletions(-) diff --git a/autoid_service/autoid.go b/autoid_service/autoid.go index a0b0334c141a4..97f3a75854d6e 100644 --- a/autoid_service/autoid.go +++ b/autoid_service/autoid.go @@ -25,8 +25,8 @@ import ( "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/metrics" - "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/owner" + "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/util/logutil" "github.com/pingcap/tidb/util/mathutil" clientv3 "go.etcd.io/etcd/client/v3" @@ -74,7 +74,7 @@ func (alloc *autoIDValue) alloc4Unsigned(ctx context.Context, store kv.Storage, ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion5) var err1 error newBase, err1 = idAcc.Get() if err1 != nil { @@ -135,7 +135,7 @@ func (alloc *autoIDValue) alloc4Signed(ctx context.Context, ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion5) var err1 error newBase, err1 = idAcc.Get() if err1 != nil { @@ -186,7 +186,7 @@ func (alloc *autoIDValue) rebase4Unsigned(ctx context.Context, startTime := time.Now() ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion5) currentEnd, err1 := idAcc.Get() if err1 != nil { return err1 @@ -219,7 +219,7 @@ func (alloc *autoIDValue) rebase4Signed(ctx context.Context, store kv.Storage, d var newBase, newEnd int64 ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion5) currentEnd, err1 := idAcc.Get() if err1 != nil { return err1 @@ -419,7 +419,7 @@ func (s *Service) allocAutoID(ctx context.Context, req *autoid.AutoIDRequest) (* func (alloc *autoIDValue) forceRebase(ctx context.Context, store kv.Storage, dbID, tblID, requiredBase int64, isUnsigned bool) error { ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnMeta) err := kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error { - idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion4 + 1) + idAcc := meta.NewMeta(txn).GetAutoIDAccessors(dbID, tblID).IncrementID(model.TableInfoVersion5) currentEnd, err1 := idAcc.Get() if err1 != nil { return err1 diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index 652fa0b0f52c2..6ce3d24b0e209 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -380,13 +380,10 @@ func BuildBackupRangeAndSchema( zap.String("table", tableInfo.Name.O), ) - // tblVer := autoid.AllocOptionTableInfoVersion(tableInfo.Version) - // idAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.RowIDAllocType, tblVer) - // seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.SequenceType, tblVer) - // randAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.AutoRandomType, tblVer) - idAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.RowIDAllocType) - seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.SequenceType) - randAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.AutoRandomType) + tblVer := autoid.AllocOptionTableInfoVersion(tableInfo.Version) + idAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.RowIDAllocType, tblVer) + seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.SequenceType, tblVer) + randAlloc := autoid.NewAllocator(storage, dbInfo.ID, tableInfo.ID, false, autoid.AutoRandomType, tblVer) var globalAutoID int64 switch { diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 6eeeb3a13c6cf..b7cb35a9f8727 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2690,7 +2690,6 @@ func TestCreateTableWithAutoIdCache(t *testing.T) { // Test both auto_increment and rowid exist. tk.MustExec("drop table if exists t;") tk.MustExec("drop table if exists t1;") - fmt.Println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") tk.MustExec("create table t(a int null, b int auto_increment unique) auto_id_cache 100") _, err = dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("t")) require.NoError(t, err) @@ -2924,7 +2923,6 @@ func TestAutoIncrementForce(t *testing.T) { tk.MustExec("create table t (a int);") tk.MustExec("alter table t force auto_increment = 2;") tk.MustExec("insert into t values (1),(2);") - fmt.Println("6666666666666666666666666666") tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3")) // Cannot set next global ID to 0. tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed) diff --git a/ddl/db_test.go b/ddl/db_test.go index 1b95321cccab3..207aad4e0f116 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -744,7 +744,6 @@ func TestRebaseAutoID(t *testing.T) { tk.MustExec("create table tidb.test (a int auto_increment primary key, b int);") tk.MustExec("insert tidb.test values (null, 1);") tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1")) - fmt.Println("=====================================") tk.MustExec("alter table tidb.test auto_increment = 6000;") tk.MustExec("insert tidb.test values (null, 1);") tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1", "6000 1")) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index d864c46f6925e..0c17425fb6f79 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -3446,7 +3446,7 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6 case autoid.RowIDAllocType: actionType = model.ActionRebaseAutoID default: - panic("fuck!") + panic(fmt.Sprintf("unimplemented rebase autoid type %s", tp)) } if !force { diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index eb982e8d27708..f24f932c00775 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -1232,7 +1232,6 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, case model.ActionTruncateTable: ver, err = onTruncateTable(d, t, job) case model.ActionRebaseAutoID: - fmt.Println("yyyyyyyyyyyyyyyyyyyyyyyyyyy rebase auto id??") ver, err = onRebaseAutoIncrementIDType(d, t, job) case model.ActionRebaseAutoRandomBase: ver, err = onRebaseAutoRandomType(d, t, job) diff --git a/executor/executor_issue_test.go b/executor/executor_issue_test.go index ce9c8e73368df..655b889b9fae8 100644 --- a/executor/executor_issue_test.go +++ b/executor/executor_issue_test.go @@ -1241,11 +1241,10 @@ func TestIssue982(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists t;") - fmt.Println("88888888888888") tk.MustExec("create table t (c int auto_increment, key(c)) auto_id_cache 1;") tk.MustExec("insert into t values();") tk.MustExec("insert into t values();") - tk.MustQuery("select * from t;").Check(testkit.Rows("1","2")) + tk.MustQuery("select * from t;").Check(testkit.Rows("1", "2")) } func TestIssue24627(t *testing.T) { @@ -1267,7 +1266,7 @@ func TestIssue24627(t *testing.T) { for _, sql := range []string{ "create table test2(id double primary key clustered AUTO_INCREMENT, col1 int);", "create table test2(id double primary key nonclustered AUTO_INCREMENT, col1 int) AUTO_ID_CACHE 1;", - }{ + } { tk.MustExec(sql) tk.MustExec("replace into test2(col1) values(1);") tk.MustExec("insert into test2(col1) values(1);") diff --git a/executor/insert_common.go b/executor/insert_common.go index e45dfc562afc7..d0146d9c3922a 100644 --- a/executor/insert_common.go +++ b/executor/insert_common.go @@ -816,7 +816,6 @@ func (e *InsertValues) lazyAdjustAutoIncrementDatum(ctx context.Context, rows [] // AllocBatchAutoIncrementValue allocates batch N consecutive autoIDs. // The max value can be derived from adding the increment value to min for cnt-1 times. min, increment, err := table.AllocBatchAutoIncrementValue(ctx, e.Table, e.ctx, cnt) - fmt.Println("here min, increment ==", min, increment) if e.handleErr(col, &autoDatum, cnt, err) != nil { return nil, err } diff --git a/infoschema/builder.go b/infoschema/builder.go index fc2d6673f309e..e708b4fef3b4f 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -696,16 +696,14 @@ func (b *Builder) applyCreateTable(m *meta.Meta, dbInfo *model.DBInfo, tableID i if len(allocs.Allocs) == 0 { allocs = autoid.NewAllocatorsFromTblInfo(b.store, dbInfo.ID, tblInfo) } else { - // tblVer := autoid.AllocOptionTableInfoVersion(tblInfo.Version) + tblVer := autoid.AllocOptionTableInfoVersion(tblInfo.Version) switch tp { case model.ActionRebaseAutoID, model.ActionModifyTableAutoIdCache: idCacheOpt := autoid.CustomAutoIncCacheOption(tblInfo.AutoIdCache) - // newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, tblVer, idCacheOpt) - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, idCacheOpt) + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, tblVer, idCacheOpt) allocs = allocs.Append(newAlloc) case model.ActionRebaseAutoRandomBase: - // newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType) + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) allocs = allocs.Append(newAlloc) case model.ActionModifyColumn: // Change column attribute from auto_increment to auto_random. @@ -714,8 +712,7 @@ func (b *Builder) applyCreateTable(m *meta.Meta, dbInfo *model.DBInfo, tableID i allocs = allocs.Filter(func(a autoid.Allocator) bool { return a.GetType() != autoid.AutoIncrementType && a.GetType() != autoid.RowIDAllocType }) - // newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType) + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) allocs = allocs.Append(newAlloc) } } diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index 82bf10e924113..ae97fec5392b9 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -22,7 +22,6 @@ import ( "strconv" "sync" "time" - "runtime/debug" "github.com/opentracing/opentracing-go" "github.com/pingcap/errors" @@ -208,21 +207,21 @@ type Allocator interface { // Allocators represents a set of `Allocator`s. type Allocators struct { SepAutoInc bool - Allocs []Allocator + Allocs []Allocator } // NewAllocators packs multiple `Allocator`s into Allocators. func NewAllocators(sepAutoInc bool, allocators ...Allocator) Allocators { return Allocators{ SepAutoInc: sepAutoInc, - Allocs: allocators, + Allocs: allocators, } } func (all Allocators) Append(a Allocator) Allocators { return Allocators{ SepAutoInc: all.SepAutoInc, - Allocs: append(all.Allocs, a), + Allocs: append(all.Allocs, a), } } @@ -234,10 +233,7 @@ func (all Allocators) Get(allocType AllocatorType) Allocator { } } - fmt.Println("Get alloc ===", allocType) - for _, a := range all.Allocs { - fmt.Println("alloc a==", a, a.GetType()) if a.GetType() == allocType { return a } @@ -255,7 +251,7 @@ func (all Allocators) Filter(pred func(Allocator) bool) Allocators { } return Allocators{ SepAutoInc: all.SepAutoInc, - Allocs: ret, + Allocs: ret, } } @@ -307,7 +303,6 @@ func (alloc *allocator) NextGlobalAutoID() (int64, error) { if err1 != nil { return errors.Trace(err1) } - fmt.Println("get next global auto id ==", autoID, "alloc type ==", alloc.allocType) return nil }) metrics.AutoIDHistogram.WithLabelValues(metrics.GlobalAutoID, metrics.RetLabel(err)).Observe(time.Since(startTime).Seconds()) @@ -404,7 +399,6 @@ func (alloc *allocator) rebase4Signed(ctx context.Context, requiredBase int64, a if allocatorStats != nil { txn.SetOption(kv.CollectRuntimeStats, allocatorStats.SnapshotRuntimeStats) } - fmt.Println("alloc type ===", alloc.allocType, "new base ==", requiredBase) idAcc := alloc.getIDAccessor(txn) currentEnd, err1 := idAcc.Get() if err1 != nil { @@ -483,8 +477,6 @@ func (alloc *allocator) rebase4Sequence(requiredBase int64) (int64, bool, error) // The requiredBase is the minimum base value after Rebase. // The real base may be greater than the required base. func (alloc *allocator) Rebase(ctx context.Context, requiredBase int64, allocIDs bool) error { - debug.PrintStack() - fmt.Println("in Rebase, requiredBase ==", requiredBase) alloc.mu.Lock() defer alloc.mu.Unlock() if alloc.isUnsigned { @@ -495,7 +487,6 @@ func (alloc *allocator) Rebase(ctx context.Context, requiredBase int64, allocIDs // ForceRebase implements autoid.Allocator ForceRebase interface. func (alloc *allocator) ForceRebase(requiredBase int64) error { - debug.PrintStack() if requiredBase == -1 { return ErrAutoincReadFailed.GenWithStack("Cannot force rebase the next global ID to '0'") } @@ -624,8 +615,7 @@ func NewAllocator(store kv.Storage, dbID, tbID int64, isUnsigned bool, } // Use the MySQL compatible AUTO_INCREMENT mode. - if allocType == AutoIncrementType && alloc.customStep && alloc.step == 1 { - fmt.Println("single point alloc ...") + if allocType == AutoIncrementType && alloc.customStep && alloc.step == 1 && tbVersion >= model.TableInfoVersion5 { alloc1 := newSinglePointAlloc(store, dbID, tbID, isUnsigned) if alloc1 != nil { return alloc1 @@ -651,7 +641,6 @@ func NewSequenceAllocator(store kv.Storage, dbID, tbID int64, info *model.Sequen // NewAllocatorsFromTblInfo creates an array of allocators of different types with the information of model.TableInfo. func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.TableInfo) Allocators { - // debug.PrintStack() var allocs []Allocator dbID := tblInfo.GetDBID(schemaID) idCacheOpt := CustomAutoIncCacheOption(tblInfo.AutoIdCache) @@ -659,13 +648,11 @@ func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.T hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil - fmt.Println("hasAutoIncID ===", hasAutoIncID ) if hasRowID || hasAutoIncID { alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), RowIDAllocType, idCacheOpt, tblVer) allocs = append(allocs, alloc) } if hasAutoIncID { - fmt.Println("run here ... and new ??") alloc := NewAllocator(store, dbID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), AutoIncrementType, idCacheOpt, tblVer) allocs = append(allocs, alloc) } @@ -735,14 +722,11 @@ func CalcNeededBatchSize(base, n, increment, offset int64, isUnsigned bool) int6 nr := SeekToFirstAutoIDUnSigned(uint64(base), uint64(increment), uint64(offset)) // Calculate the total batch size needed. nr += (uint64(n) - 1) * uint64(increment) - fmt.Println("fuck is unsigned??", nr, base) return int64(nr - uint64(base)) } nr := SeekToFirstAutoIDSigned(base, increment, offset) - fmt.Println("seek to first auto signed", nr) // Calculate the total batch size needed. nr += (n - 1) * increment - fmt.Println("then xxx", nr, (n-1), increment) return nr - base } @@ -881,12 +865,10 @@ func (alloc *allocator) alloc4Signed(ctx context.Context, n uint64, increment, o var newBase, newEnd int64 startTime := time.Now() nextStep := alloc.step - fmt.Println("alloc.step ==", alloc.step) if !alloc.customStep && alloc.end > 0 { // Although it may skip a segment here, we still think it is consumed. consumeDur := startTime.Sub(alloc.lastAllocTime) nextStep = NextStep(alloc.step, consumeDur) - fmt.Println("next.step ==", nextStep, alloc.lastAllocTime) } ctx, allocatorStats, commitDetail := getAllocatorStatsFromCtx(ctx) @@ -916,21 +898,18 @@ func (alloc *allocator) alloc4Signed(ctx context.Context, n uint64, increment, o if err1 != nil { return err1 } - fmt.Println("alloc type ==", alloc.allocType, "get", newBase) // CalcNeededBatchSize calculates the total batch size needed on global base. n1 = CalcNeededBatchSize(newBase, int64(n), increment, offset, alloc.isUnsigned) // Although the step is customized by user, we still need to make sure nextStep is big enough for insert batch. if nextStep < n1 { nextStep = n1 } - fmt.Println("newBase", newBase, "nextStep", nextStep, n, increment, offset, n1) tmpStep := mathutil.Min(math.MaxInt64-newBase, nextStep) // The global rest is not enough for alloc. if tmpStep < n1 { return ErrAutoincReadFailed } - fmt.Println("............................", tmpStep) newEnd, err1 = idAcc.Inc(tmpStep) return err1 }) diff --git a/meta/autoid/memid.go b/meta/autoid/memid.go index 23e5a94cc09d5..f3a3b50bf5275 100644 --- a/meta/autoid/memid.go +++ b/meta/autoid/memid.go @@ -26,15 +26,11 @@ func NewAllocatorFromTempTblInfo(tblInfo *model.TableInfo) Allocator { hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil var alloc Allocator - allocType := RowIDAllocType - // if hasAutoIncID { - // allocType = AutoIncrementType - // } // Temporary tables don't support auto_random and sequence. if hasRowID || hasAutoIncID { alloc = &inMemoryAllocator{ isUnsigned: tblInfo.IsAutoIncColUnsigned(), - allocType: allocType, + allocType: RowIDAllocType, } } // Rebase the allocator if the base is specified. diff --git a/meta/meta_autoid.go b/meta/meta_autoid.go index e3e4d3ceda400..f147ffcedf318 100644 --- a/meta/meta_autoid.go +++ b/meta/meta_autoid.go @@ -17,7 +17,6 @@ package meta import ( "fmt" "strconv" - "runtime/debug" "github.com/pingcap/errors" "github.com/pingcap/tidb/parser/model" @@ -45,22 +44,18 @@ type autoIDAccessor struct { func (a *autoIDAccessor) Get() (int64, error) { m := a.m ret, err := m.txn.HGetInt64(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID)) - fmt.Println("get ==", a.databaseID, a.tableID, ret) return ret, err } // Put implements the interface AutoIDAccessor. func (a *autoIDAccessor) Put(val int64) error { m := a.m - fmt.Println("id accessor put ===", val) return m.txn.HSet(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID), []byte(strconv.FormatInt(val, 10))) } // Inc implements the interface AutoIDAccessor. func (a *autoIDAccessor) Inc(step int64) (int64, error) { m := a.m - fmt.Println("id accessor inc ===", step, a.databaseID, a.tableID) - debug.PrintStack() dbKey := m.dbKey(a.databaseID) if err := m.checkDBExists(dbKey); err != nil { return 0, errors.Trace(err) @@ -109,7 +104,7 @@ type autoIDAccessors struct { access autoIDAccessor } -const sepAutoIncVer = model.TableInfoVersion4 + 1 +const sepAutoIncVer = model.TableInfoVersion5 // Get implements the interface AutoIDAccessors. func (a *autoIDAccessors) Get() (autoIDs AutoIDGroup, err error) { @@ -127,7 +122,6 @@ func (a *autoIDAccessors) Get() (autoIDs AutoIDGroup, err error) { // Put implements the interface AutoIDAccessors. func (a *autoIDAccessors) Put(autoIDs AutoIDGroup) error { - fmt.Println("id accessors put ===", autoIDs) if err := a.RowID().Put(autoIDs.RowID); err != nil { return err } diff --git a/parser/model/model.go b/parser/model/model.go index add3d59d81ba3..c8726360932b5 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -443,14 +443,16 @@ const ( // However, the convert is missed in some scenarios before v2.1.9, so for all those tables prior to TableInfoVersion3, their // charsets / collations will be converted to lower-case while loading from the storage. TableInfoVersion3 = uint16(3) - // TableInfoVersion4 indicates that the auto_increment allocator in TiDB has been separated from - // _tidb_rowid allocator. This version is introduced to preserve the compatibility of old tables: + // TableInfoVersion4 is not used. + TableInfoVersion4 = uint16(4) + // TableInfoVersion5 indicates that the auto_increment allocator in TiDB has been separated from + // _tidb_rowid allocator when AUTO_ID_CACHE is 1. This version is introduced to preserve the compatibility of old tables: // the tables with version < TableInfoVersion4 still use a single allocator for auto_increment and _tidb_rowid. // Also see https://github.com/pingcap/tidb/issues/982. - TableInfoVersion4 = uint16(4) + TableInfoVersion5 = uint16(5) // CurrLatestTableInfoVersion means the latest table info in the current TiDB. - CurrLatestTableInfoVersion = TableInfoVersion4 + CurrLatestTableInfoVersion = TableInfoVersion5 ) // ExtraHandleName is the name of ExtraHandle Column. diff --git a/session/schema_amender_test.go b/session/schema_amender_test.go index e82faba8e6e56..a6121c60441e3 100644 --- a/session/schema_amender_test.go +++ b/session/schema_amender_test.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/meta/autoid" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/planner/core" @@ -276,7 +277,7 @@ func TestAmendCollectAndGenMutations(t *testing.T) { initTblColIdxID(oldTblMeta) // Indices[0] does not exist at the start. oldTblMeta.Indices = oldTblMeta.Indices[1:] - oldTbInfo, err := table.TableFromMeta(nil, oldTblMeta) + oldTbInfo, err := table.TableFromMeta(autoid.Allocators{}, oldTblMeta) require.NoError(t, err) oldTblMeta.Indices[0].State = startState oldTblMeta.Indices[2].State = endState @@ -296,7 +297,7 @@ func TestAmendCollectAndGenMutations(t *testing.T) { // The last index "c_d_e_str_prefix is dropped. newTblMeta.Indices = newTblMeta.Indices[:len(newTblMeta.Indices)-1] newTblMeta.Indices[0].Unique = false - newTblInfo, err := table.TableFromMeta(nil, newTblMeta) + newTblInfo, err := table.TableFromMeta(autoid.Allocators{}, newTblMeta) require.NoError(t, err) newTblMeta.Indices[0].State = endState // Indices[1] is newly created. diff --git a/table/tables/tables.go b/table/tables/tables.go index e3b5769cdaecf..7854a771e3119 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -1542,10 +1542,6 @@ func (t *TableCommon) Allocators(ctx sessionctx.Context) autoid.Allocators { // Use an independent allocator for global temporary tables. if t.meta.TempTableType == model.TempTableGlobal { if alloc := ctx.GetSessionVars().GetTemporaryTable(t.meta).GetAutoIDAllocator(); alloc != nil { - fmt.Println("global temporary table ... change auto inc to ", alloc) - // return &autoid.Allocators{ - // allocs: alloc, - // } return autoid.NewAllocators(false, alloc) } // If the session is not in a txn, for example, in "show create table", use the original allocator. diff --git a/table/tables/tables_test.go b/table/tables/tables_test.go index d659f918a8168..dc8d5b1d5ac0f 100644 --- a/table/tables/tables_test.go +++ b/table/tables/tables_test.go @@ -378,18 +378,18 @@ func TestTableFromMeta(t *testing.T) { // For test coverage tbInfo.Columns[0].GeneratedExprString = "a" - _, err = tables.TableFromMeta(nil, tbInfo) + _, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) require.NoError(t, err) tbInfo.Columns[0].GeneratedExprString = "test" - _, err = tables.TableFromMeta(nil, tbInfo) + _, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) require.Error(t, err) tbInfo.Columns[0].State = model.StateNone - tb, err = tables.TableFromMeta(nil, tbInfo) + tb, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) require.Nil(t, tb) require.Error(t, err) tbInfo.State = model.StateNone - tb, err = tables.TableFromMeta(nil, tbInfo) + tb, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) require.Nil(t, tb) require.Error(t, err) diff --git a/table/temptable/main_test.go b/table/temptable/main_test.go index 7dbc5e86d37d9..1c6b5f2c75067 100644 --- a/table/temptable/main_test.go +++ b/table/temptable/main_test.go @@ -22,6 +22,7 @@ import ( "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/meta/autoid" "github.com/pingcap/tidb/parser/model" "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/table" @@ -86,7 +87,7 @@ func (is *mockedInfoSchema) TableByID(tblID int64) (table.Table, bool) { State: model.StatePublic, } - tbl, err := table.TableFromMeta(nil, tblInfo) + tbl, err := table.TableFromMeta(autoid.Allocators{}, tblInfo) require.NoError(is.t, err) return tbl, true From b5fc7b48dca3478ff5964db2bf7e3701785cbefe Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 11 Nov 2022 10:23:16 +0800 Subject: [PATCH 07/21] handle 'admin show next_row_id' --- executor/executor.go | 11 ++++++++++- meta/autoid/autoid.go | 2 +- meta/meta_autoid.go | 1 - 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index c65e8b790f8bd..6c47561dc3611 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -405,7 +405,16 @@ func (e *ShowNextRowIDExec) Next(ctx context.Context, req *chunk.Chunk) error { var colName, idType string switch alloc.GetType() { - case autoid.RowIDAllocType, autoid.AutoIncrementType: + case autoid.RowIDAllocType: + idType = "_TIDB_ROWID" + if tblMeta.PKIsHandle { + if col := tblMeta.GetAutoIncrementColInfo(); col != nil { + colName = col.Name.O + } + } else { + colName = model.ExtraHandleName.O + } + case autoid.AutoIncrementType: idType = "AUTO_INCREMENT" if tblMeta.PKIsHandle { if col := tblMeta.GetAutoIncrementColInfo(); col != nil { diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index ae97fec5392b9..446fe13efcaae 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -615,7 +615,7 @@ func NewAllocator(store kv.Storage, dbID, tbID int64, isUnsigned bool, } // Use the MySQL compatible AUTO_INCREMENT mode. - if allocType == AutoIncrementType && alloc.customStep && alloc.step == 1 && tbVersion >= model.TableInfoVersion5 { + if allocType == AutoIncrementType && alloc.customStep && alloc.step == 1 && alloc.tbVersion >= model.TableInfoVersion5 { alloc1 := newSinglePointAlloc(store, dbID, tbID, isUnsigned) if alloc1 != nil { return alloc1 diff --git a/meta/meta_autoid.go b/meta/meta_autoid.go index f147ffcedf318..c6338a65d6fda 100644 --- a/meta/meta_autoid.go +++ b/meta/meta_autoid.go @@ -15,7 +15,6 @@ package meta import ( - "fmt" "strconv" "github.com/pingcap/errors" From bd43d92f0b74e578facb01f09504981cc2bd3a1e Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 11 Nov 2022 10:41:01 +0800 Subject: [PATCH 08/21] fix case TestAutoRandomChangeFromAutoInc --- ddl/column.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ddl/column.go b/ddl/column.go index 10db5120e9351..67116096a758e 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -1524,7 +1524,8 @@ func checkAndApplyAutoRandomBits(d *ddlCtx, m *meta.Meta, dbInfo *model.DBInfo, return nil } idAcc := m.GetAutoIDAccessors(dbInfo.ID, tblInfo.ID) - err := checkNewAutoRandomBits(idAcc, oldCol, newCol, newAutoRandBits, tblInfo.AutoRandomRangeBits, tblInfo.Version) + sepAutoInc := tblInfo.Version >= model.TableInfoVersion5 && tblInfo.AutoIdCache == 1 + err := checkNewAutoRandomBits(idAcc, oldCol, newCol, newAutoRandBits, tblInfo.AutoRandomRangeBits, sepAutoInc) if err != nil { return err } @@ -1533,13 +1534,17 @@ func checkAndApplyAutoRandomBits(d *ddlCtx, m *meta.Meta, dbInfo *model.DBInfo, // checkNewAutoRandomBits checks whether the new auto_random bits number can cause overflow. func checkNewAutoRandomBits(idAccessors meta.AutoIDAccessors, oldCol *model.ColumnInfo, - newCol *model.ColumnInfo, newShardBits, newRangeBits uint64, tblVer uint16) error { + newCol *model.ColumnInfo, newShardBits, newRangeBits uint64, sepAutoInc bool) error { shardFmt := autoid.NewShardIDFormat(&newCol.FieldType, newShardBits, newRangeBits) idAcc := idAccessors.RandomID() convertedFromAutoInc := mysql.HasAutoIncrementFlag(oldCol.GetFlag()) if convertedFromAutoInc { - idAcc = idAccessors.IncrementID(tblVer) + if sepAutoInc { + idAcc = idAccessors.IncrementID(model.TableInfoVersion5) + } else { + idAcc = idAccessors.RowID() + } } // Generate a new auto ID first to prevent concurrent update in DML. _, err := idAcc.Inc(1) From 166a4c9fce2580f84b0240ac44ab8544b785721d Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Fri, 11 Nov 2022 12:09:11 +0800 Subject: [PATCH 09/21] fix TestExchangePartitionAutoID --- ddl/partition.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/partition.go b/ddl/partition.go index 5a6a5b561b146..8f3c5e0ed1c42 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -2051,7 +2051,7 @@ func (w *worker) onExchangeTablePartition(d *ddlCtx, t *meta.Meta, job *model.Jo failpoint.Return(ver, err) } sess := newSession(se) - _, err = sess.execute(context.Background(), "insert into test.pt values (40000000)", "exchange_partition_test") + _, err = sess.execute(context.Background(), "insert ignore into test.pt values (40000000)", "exchange_partition_test") if err != nil { failpoint.Return(ver, err) } From 5e3e7b3a6d3bf17f7a0ddab31b28e91c98bf99a2 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Sat, 12 Nov 2022 15:12:24 +0800 Subject: [PATCH 10/21] fix test case TestAdminShowNextID --- executor/seqtest/seq_executor_test.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/executor/seqtest/seq_executor_test.go b/executor/seqtest/seq_executor_test.go index 7f16fd68da5e7..bf78e40e7ac3d 100644 --- a/executor/seqtest/seq_executor_test.go +++ b/executor/seqtest/seq_executor_test.go @@ -773,43 +773,45 @@ func HelperTestAdminShowNextID(t *testing.T, store kv.Storage, str string) { tk.MustExec("create table t(id int, c int)") // Start handle is 1. r := tk.MustQuery(str + " t next_row_id") - r.Check(testkit.Rows("test t _tidb_rowid 1 AUTO_INCREMENT")) + r.Check(testkit.Rows("test t _tidb_rowid 1 _TIDB_ROWID")) // Row ID is step + 1. tk.MustExec("insert into t values(1, 1)") r = tk.MustQuery(str + " t next_row_id") - r.Check(testkit.Rows("test t _tidb_rowid 11 AUTO_INCREMENT")) + r.Check(testkit.Rows("test t _tidb_rowid 11 _TIDB_ROWID")) // Row ID is original + step. for i := 0; i < int(step); i++ { tk.MustExec("insert into t values(10000, 1)") } r = tk.MustQuery(str + " t next_row_id") - r.Check(testkit.Rows("test t _tidb_rowid 21 AUTO_INCREMENT")) + r.Check(testkit.Rows("test t _tidb_rowid 21 _TIDB_ROWID")) tk.MustExec("drop table t") // test for a table with the primary key tk.MustExec("create table tt(id int primary key auto_increment, c int)") // Start handle is 1. r = tk.MustQuery(str + " tt next_row_id") - r.Check(testkit.Rows("test tt id 1 AUTO_INCREMENT")) + r.Check(testkit.Rows("test tt id 1 _TIDB_ROWID", "test tt id 1 AUTO_INCREMENT")) // After rebasing auto ID, row ID is 20 + step + 1. tk.MustExec("insert into tt values(20, 1)") r = tk.MustQuery(str + " tt next_row_id") - r.Check(testkit.Rows("test tt id 31 AUTO_INCREMENT")) + r.Check(testkit.Rows("test tt id 31 _TIDB_ROWID", "test tt id 1 AUTO_INCREMENT")) // test for renaming the table tk.MustExec("drop database if exists test1") tk.MustExec("create database test1") tk.MustExec("rename table test.tt to test1.tt") tk.MustExec("use test1") r = tk.MustQuery(str + " tt next_row_id") - r.Check(testkit.Rows("test1 tt id 31 AUTO_INCREMENT")) + r.Check(testkit.Rows("test1 tt id 31 _TIDB_ROWID", "test1 tt id 1 AUTO_INCREMENT")) tk.MustExec("insert test1.tt values ()") r = tk.MustQuery(str + " tt next_row_id") - r.Check(testkit.Rows("test1 tt id 41 AUTO_INCREMENT")) + r.Check(testkit.Rows("test1 tt id 41 _TIDB_ROWID", "test1 tt id 1 AUTO_INCREMENT")) tk.MustExec("drop table tt") tk.MustExec("drop table if exists t;") tk.MustExec("create table t (a int auto_increment primary key nonclustered, b int);") - tk.MustQuery("show table t next_row_id;").Check(testkit.Rows("test1 t _tidb_rowid 1 AUTO_INCREMENT")) + tk.MustQuery("show table t next_row_id;").Check(testkit.Rows( + "test1 t _tidb_rowid 1 _TIDB_ROWID", + "test1 t _tidb_rowid 1 AUTO_INCREMENT")) tk.MustExec("set @@allow_auto_random_explicit_insert = true") @@ -830,19 +832,19 @@ func HelperTestAdminShowNextID(t *testing.T, store kv.Storage, str string) { // Test for a sequence. tk.MustExec("create sequence seq1 start 15 cache 57") r = tk.MustQuery(str + " seq1 next_row_id") - r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 AUTO_INCREMENT", "test1 seq1 15 SEQUENCE")) + r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 _TIDB_ROWID", "test1 seq1 15 SEQUENCE")) r = tk.MustQuery("select nextval(seq1)") r.Check(testkit.Rows("15")) r = tk.MustQuery(str + " seq1 next_row_id") - r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 AUTO_INCREMENT", "test1 seq1 72 SEQUENCE")) + r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 _TIDB_ROWID", "test1 seq1 72 SEQUENCE")) r = tk.MustQuery("select nextval(seq1)") r.Check(testkit.Rows("16")) r = tk.MustQuery(str + " seq1 next_row_id") - r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 AUTO_INCREMENT", "test1 seq1 72 SEQUENCE")) + r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 _TIDB_ROWID", "test1 seq1 72 SEQUENCE")) r = tk.MustQuery("select setval(seq1, 96)") r.Check(testkit.Rows("96")) r = tk.MustQuery(str + " seq1 next_row_id") - r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 AUTO_INCREMENT", "test1 seq1 97 SEQUENCE")) + r.Check(testkit.Rows("test1 seq1 _tidb_rowid 1 _TIDB_ROWID", "test1 seq1 97 SEQUENCE")) } func TestNoHistoryWhenDisableRetry(t *testing.T) { From 402ab200e0124bbc39102634f5b4fd428511ea14 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Sat, 12 Nov 2022 16:12:58 +0800 Subject: [PATCH 11/21] make bazel_prepare --- autoid_service/BUILD.bazel | 1 + meta/autoid/autoid.go | 1 + session/BUILD.bazel | 1 + table/temptable/BUILD.bazel | 1 + 4 files changed, 4 insertions(+) diff --git a/autoid_service/BUILD.bazel b/autoid_service/BUILD.bazel index df3d361d412ed..c973afd85c4e7 100644 --- a/autoid_service/BUILD.bazel +++ b/autoid_service/BUILD.bazel @@ -11,6 +11,7 @@ go_library( "//meta", "//metrics", "//owner", + "//parser/model", "//util/logutil", "//util/mathutil", "@com_github_pingcap_errors//:errors", diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index 446fe13efcaae..def60df33b7a9 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -218,6 +218,7 @@ func NewAllocators(sepAutoInc bool, allocators ...Allocator) Allocators { } } +// Append add an allocator to the allocators. func (all Allocators) Append(a Allocator) Allocators { return Allocators{ SepAutoInc: all.SepAutoInc, diff --git a/session/BUILD.bazel b/session/BUILD.bazel index 2a79502bf985e..d0d68a9142035 100644 --- a/session/BUILD.bazel +++ b/session/BUILD.bazel @@ -136,6 +136,7 @@ go_test( "//expression", "//kv", "//meta", + "//meta/autoid", "//parser/ast", "//parser/auth", "//parser/model", diff --git a/table/temptable/BUILD.bazel b/table/temptable/BUILD.bazel index 30c41bd1c55b3..8487a26533b51 100644 --- a/table/temptable/BUILD.bazel +++ b/table/temptable/BUILD.bazel @@ -41,6 +41,7 @@ go_test( deps = [ "//infoschema", "//kv", + "//meta/autoid", "//parser/model", "//parser/mysql", "//sessionctx", From 976f54bea1d5f143424ebb9678d8e0dfcb280258 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Sat, 12 Nov 2022 17:35:22 +0800 Subject: [PATCH 12/21] fix case TestAutoIncrementIDOnTemporaryTable --- ddl/db_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index 207aad4e0f116..db863c8efbe07 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -898,11 +898,11 @@ func TestAutoIncrementIDOnTemporaryTable(t *testing.T) { tk.MustExec("drop table if exists global_temp_auto_id") tk.MustExec("create global temporary table global_temp_auto_id(id int primary key auto_increment) on commit delete rows") tk.MustExec("begin") - tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 1 AUTO_INCREMENT")) + tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 1 _TIDB_ROWID")) tk.MustExec("insert into global_temp_auto_id value(null)") tk.MustQuery("select @@last_insert_id").Check(testkit.Rows("1")) tk.MustQuery("select id from global_temp_auto_id").Check(testkit.Rows("1")) - tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 2 AUTO_INCREMENT")) + tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 2 _TIDB_ROWID")) tk.MustExec("commit") tk.MustExec("drop table global_temp_auto_id") @@ -914,12 +914,12 @@ func TestAutoIncrementIDOnTemporaryTable(t *testing.T) { " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=100 ON COMMIT DELETE ROWS")) - tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 100 AUTO_INCREMENT")) + tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 100 _TIDB_ROWID")) tk.MustExec("begin") tk.MustExec("insert into global_temp_auto_id value(null)") tk.MustQuery("select @@last_insert_id").Check(testkit.Rows("100")) tk.MustQuery("select id from global_temp_auto_id").Check(testkit.Rows("100")) - tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 101 AUTO_INCREMENT")) + tk.MustQuery("show table global_temp_auto_id next_row_id").Check(testkit.Rows("test global_temp_auto_id id 101 _TIDB_ROWID")) tk.MustExec("commit") } tk.MustExec("drop table global_temp_auto_id") From 877a86d09965250591dc03a80987560b85da50ae Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 15 Nov 2022 16:53:04 +0800 Subject: [PATCH 13/21] fix mysql-test --- ddl/ddl_api.go | 8 +++++++- parser/model/model.go | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index e69a1ab12dd1a..3a555443ab5e8 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2484,7 +2484,13 @@ func (d *ddl) createTableWithInfoPost( // Default tableAutoIncID base is 0. // If the first ID is expected to greater than 1, we need to do rebase. newEnd := tbInfo.AutoIncID - 1 - if err = d.handleAutoIncID(tbInfo, schemaID, newEnd, autoid.RowIDAllocType); err != nil { + var allocType autoid.AllocatorType + if tbInfo.SepAutoInc() { + allocType = autoid.AutoIncrementType + } else { + allocType = autoid.RowIDAllocType + } + if err = d.handleAutoIncID(tbInfo, schemaID, newEnd, allocType); err != nil { return errors.Trace(err) } } diff --git a/parser/model/model.go b/parser/model/model.go index c8726360932b5..09550b59b4570 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -549,6 +549,11 @@ type TableInfo struct { ExchangePartitionInfo *ExchangePartitionInfo `json:"exchange_partition_info"` } +// SepAutoInc decides whether rowid and auto_increment id use separate allocator. +func (tblInfo *TableInfo) SepAutoInc() bool { + return tblInfo.Version >= TableInfoVersion5 && tblInfo.AutoIdCache == 1 +} + // TableCacheStatusType is the type of the table cache status type TableCacheStatusType int From a2b9cb616602b9b1b6ea533a21929fed6ddf3070 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 15 Nov 2022 21:23:30 +0800 Subject: [PATCH 14/21] fix build --- parser/model/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/model/model.go b/parser/model/model.go index c3ee2e68c5ead..f59dbb2d6be19 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -551,7 +551,7 @@ type TableInfo struct { // SepAutoInc decides whether rowid and auto_increment id use separate allocator. func (t *TableInfo) SepAutoInc() bool { - return tblInfo.Version >= TableInfoVersion5 && tblInfo.AutoIdCache == 1 + return t.Version >= TableInfoVersion5 && t.AutoIdCache == 1 } // TableCacheStatusType is the type of the table cache status From 7ca95059c2198a9f1bf78bb1ec3527a9bfcba22f Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 15 Nov 2022 21:29:42 +0800 Subject: [PATCH 15/21] make fmt --- parser/model/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser/model/model.go b/parser/model/model.go index f59dbb2d6be19..26facd6dcddc7 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -550,7 +550,7 @@ type TableInfo struct { } // SepAutoInc decides whether rowid and auto_increment id use separate allocator. -func (t *TableInfo) SepAutoInc() bool { +func (t *TableInfo) SepAutoInc() bool { return t.Version >= TableInfoVersion5 && t.AutoIdCache == 1 } From 069a858edcdfd4a25c76d4f53559fa2d82c39f12 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 28 Nov 2022 11:25:21 +0800 Subject: [PATCH 16/21] address comment --- ddl/column.go | 3 +-- ddl/ddl_api.go | 2 +- meta/autoid/autoid.go | 3 +-- meta/meta_autoid.go | 3 +-- parser/model/model.go | 4 ++-- session/schema_amender_test.go | 4 ++-- table/tables/tables.go | 2 +- table/tables/tables_test.go | 8 ++++---- table/temptable/main_test.go | 2 +- 9 files changed, 14 insertions(+), 17 deletions(-) diff --git a/ddl/column.go b/ddl/column.go index a632df40b2637..e34fcc816b123 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -1524,8 +1524,7 @@ func checkAndApplyAutoRandomBits(d *ddlCtx, m *meta.Meta, dbInfo *model.DBInfo, return nil } idAcc := m.GetAutoIDAccessors(dbInfo.ID, tblInfo.ID) - sepAutoInc := tblInfo.Version >= model.TableInfoVersion5 && tblInfo.AutoIdCache == 1 - err := checkNewAutoRandomBits(idAcc, oldCol, newCol, newAutoRandBits, tblInfo.AutoRandomRangeBits, sepAutoInc) + err := checkNewAutoRandomBits(idAcc, oldCol, newCol, newAutoRandBits, tblInfo.AutoRandomRangeBits, tblInfo.SepAutoInc()) if err != nil { return err } diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index fadf943c072bf..def64b0879042 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2147,7 +2147,7 @@ func checkPartitionDefinitionConstraints(ctx sessionctx.Context, tbInfo *model.T // checkTableInfoValid uses to check table info valid. This is used to validate table info. func checkTableInfoValid(tblInfo *model.TableInfo) error { - _, err := tables.TableFromMeta(autoid.Allocators{}, tblInfo) + _, err := tables.TableFromMeta(autoid.NewAllocators(false), tblInfo) if err != nil { return err } diff --git a/meta/autoid/autoid.go b/meta/autoid/autoid.go index def60df33b7a9..2a181a088d802 100644 --- a/meta/autoid/autoid.go +++ b/meta/autoid/autoid.go @@ -665,7 +665,7 @@ func NewAllocatorsFromTblInfo(store kv.Storage, schemaID int64, tblInfo *model.T if tblInfo.IsSequence() { allocs = append(allocs, NewSequenceAllocator(store, dbID, tblInfo.ID, tblInfo.Sequence)) } - return NewAllocators(tblInfo.AutoIdCache == 1, allocs...) + return NewAllocators(tblInfo.SepAutoInc(), allocs...) } // Alloc implements autoid.Allocator Alloc interface. @@ -910,7 +910,6 @@ func (alloc *allocator) alloc4Signed(ctx context.Context, n uint64, increment, o if tmpStep < n1 { return ErrAutoincReadFailed } - newEnd, err1 = idAcc.Inc(tmpStep) return err1 }) diff --git a/meta/meta_autoid.go b/meta/meta_autoid.go index c6338a65d6fda..5763aa268051a 100644 --- a/meta/meta_autoid.go +++ b/meta/meta_autoid.go @@ -42,8 +42,7 @@ type autoIDAccessor struct { // Get implements the interface AutoIDAccessor. func (a *autoIDAccessor) Get() (int64, error) { m := a.m - ret, err := m.txn.HGetInt64(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID)) - return ret, err + return m.txn.HGetInt64(m.dbKey(a.databaseID), a.idEncodeFn(a.tableID)) } // Put implements the interface AutoIDAccessor. diff --git a/parser/model/model.go b/parser/model/model.go index 26facd6dcddc7..8154bb99d43cc 100644 --- a/parser/model/model.go +++ b/parser/model/model.go @@ -447,7 +447,7 @@ const ( TableInfoVersion4 = uint16(4) // TableInfoVersion5 indicates that the auto_increment allocator in TiDB has been separated from // _tidb_rowid allocator when AUTO_ID_CACHE is 1. This version is introduced to preserve the compatibility of old tables: - // the tables with version < TableInfoVersion4 still use a single allocator for auto_increment and _tidb_rowid. + // the tables with version <= TableInfoVersion4 still use a single allocator for auto_increment and _tidb_rowid. // Also see https://github.com/pingcap/tidb/issues/982. TableInfoVersion5 = uint16(5) @@ -549,7 +549,7 @@ type TableInfo struct { ExchangePartitionInfo *ExchangePartitionInfo `json:"exchange_partition_info"` } -// SepAutoInc decides whether rowid and auto_increment id use separate allocator. +// SepAutoInc decides whether _rowid and auto_increment id use separate allocator. func (t *TableInfo) SepAutoInc() bool { return t.Version >= TableInfoVersion5 && t.AutoIdCache == 1 } diff --git a/session/schema_amender_test.go b/session/schema_amender_test.go index a6121c60441e3..565b4861a9400 100644 --- a/session/schema_amender_test.go +++ b/session/schema_amender_test.go @@ -277,7 +277,7 @@ func TestAmendCollectAndGenMutations(t *testing.T) { initTblColIdxID(oldTblMeta) // Indices[0] does not exist at the start. oldTblMeta.Indices = oldTblMeta.Indices[1:] - oldTbInfo, err := table.TableFromMeta(autoid.Allocators{}, oldTblMeta) + oldTbInfo, err := table.TableFromMeta(autoid.NewAllocators(false), oldTblMeta) require.NoError(t, err) oldTblMeta.Indices[0].State = startState oldTblMeta.Indices[2].State = endState @@ -297,7 +297,7 @@ func TestAmendCollectAndGenMutations(t *testing.T) { // The last index "c_d_e_str_prefix is dropped. newTblMeta.Indices = newTblMeta.Indices[:len(newTblMeta.Indices)-1] newTblMeta.Indices[0].Unique = false - newTblInfo, err := table.TableFromMeta(autoid.Allocators{}, newTblMeta) + newTblInfo, err := table.TableFromMeta(autoid.NewAllocators(false), newTblMeta) require.NoError(t, err) newTblMeta.Indices[0].State = endState // Indices[1] is newly created. diff --git a/table/tables/tables.go b/table/tables/tables.go index a6c4bb78d02a4..2cb7b6e32fd01 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -85,7 +85,7 @@ func MockTableFromMeta(tblInfo *model.TableInfo) table.Table { } var t TableCommon - initTableCommon(&t, tblInfo, tblInfo.ID, columns, autoid.Allocators{}) + initTableCommon(&t, tblInfo, tblInfo.ID, columns, autoid.NewAllocators(false)) if tblInfo.TableCacheStatusType != model.TableCacheStatusDisable { ret, err := newCachedTable(&t) if err != nil { diff --git a/table/tables/tables_test.go b/table/tables/tables_test.go index dc8d5b1d5ac0f..661770b868383 100644 --- a/table/tables/tables_test.go +++ b/table/tables/tables_test.go @@ -378,18 +378,18 @@ func TestTableFromMeta(t *testing.T) { // For test coverage tbInfo.Columns[0].GeneratedExprString = "a" - _, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) + _, err = tables.TableFromMeta(autoid.NewAllocators(false), tbInfo) require.NoError(t, err) tbInfo.Columns[0].GeneratedExprString = "test" - _, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) + _, err = tables.TableFromMeta(autoid.NewAllocators(false), tbInfo) require.Error(t, err) tbInfo.Columns[0].State = model.StateNone - tb, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) + tb, err = tables.TableFromMeta(autoid.NewAllocators(false), tbInfo) require.Nil(t, tb) require.Error(t, err) tbInfo.State = model.StateNone - tb, err = tables.TableFromMeta(autoid.Allocators{}, tbInfo) + tb, err = tables.TableFromMeta(autoid.NewAllocators(false), tbInfo) require.Nil(t, tb) require.Error(t, err) diff --git a/table/temptable/main_test.go b/table/temptable/main_test.go index 1c6b5f2c75067..8c5c4f557e1ae 100644 --- a/table/temptable/main_test.go +++ b/table/temptable/main_test.go @@ -87,7 +87,7 @@ func (is *mockedInfoSchema) TableByID(tblID int64) (table.Table, bool) { State: model.StatePublic, } - tbl, err := table.TableFromMeta(autoid.Allocators{}, tblInfo) + tbl, err := table.TableFromMeta(autoid.NewAllocators(false), tblInfo) require.NoError(is.t, err) return tbl, true From 14b7bf920393baa6db9dd4ad511190c96fbd4eb7 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 28 Nov 2022 23:23:50 +0800 Subject: [PATCH 17/21] fix CI --- ddl/db_integration_test.go | 295 ++++++++++++++++++++++++++----------- ddl/ddl_api.go | 4 +- infoschema/builder.go | 9 +- 3 files changed, 223 insertions(+), 85 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index b25aa87f2fce8..6d2e5cf39c468 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -2926,94 +2926,84 @@ func TestAutoIncrementForce(t *testing.T) { return gid } - for _, str := range []string{"", " AUTO_ID_CACHE 1"} { - // Rebase _tidb_row_id. - tk.MustExec("create table t (a int)" + str) - tk.MustExec("alter table t force auto_increment = 2;") - tk.MustExec("insert into t values (1),(2);") - tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3")) - // Cannot set next global ID to 0. - tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed) - tk.MustExec("alter table t force auto_increment = 1;") - require.Equal(t, uint64(1), getNextGlobalID()) - // inserting new rows can overwrite the existing data. - tk.MustExec("insert into t values (3);") - require.Equal(t, "[kv:1062]Duplicate entry '2' for key 't.PRIMARY'", tk.ExecToErr("insert into t values (3);").Error()) - tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "1 2", "2 3")) - tk.MustExec("drop table if exists t;") - } + // Rebase _tidb_row_id. + tk.MustExec("create table t (a int)") + tk.MustExec("alter table t force auto_increment = 2;") + tk.MustExec("insert into t values (1),(2);") + tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 2", "2 3")) + // Cannot set next global ID to 0. + tk.MustGetErrCode("alter table t force auto_increment = 0;", errno.ErrAutoincReadFailed) + tk.MustExec("alter table t force auto_increment = 1;") + require.Equal(t, uint64(1), getNextGlobalID()) + // inserting new rows can overwrite the existing data. + tk.MustExec("insert into t values (3);") + require.Equal(t, "[kv:1062]Duplicate entry '2' for key 't.PRIMARY'", tk.ExecToErr("insert into t values (3);").Error()) + tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("3 1", "1 2", "2 3")) + tk.MustExec("drop table if exists t;") - for _, str := range []string{"", " AUTO_ID_CACHE 1"} { - // Rebase auto_increment. - tk.MustExec("create table t (a int primary key auto_increment, b int)" + str) - tk.MustExec("insert into t values (1, 1);") - tk.MustExec("insert into t values (100000000, 1);") - tk.MustExec("delete from t where a = 100000000;") - require.Greater(t, getNextGlobalID(), uint64(100000000)) - // Cannot set next global ID to 0. - tk.MustGetErrCode("alter table t /*T![force_inc] force */ auto_increment = 0;", errno.ErrAutoincReadFailed) - tk.MustExec("alter table t /*T![force_inc] force */ auto_increment = 2;") - require.Equal(t, uint64(2), getNextGlobalID()) - tk.MustExec("insert into t(b) values (2);") - tk.MustQuery("select a, b from t;").Check(testkit.Rows("1 1", "2 2")) - tk.MustExec("drop table if exists t;") - } + // Rebase auto_increment. + tk.MustExec("create table t (a int primary key auto_increment, b int)") + tk.MustExec("insert into t values (1, 1);") + tk.MustExec("insert into t values (100000000, 1);") + tk.MustExec("delete from t where a = 100000000;") + require.Greater(t, getNextGlobalID(), uint64(100000000)) + // Cannot set next global ID to 0. + tk.MustGetErrCode("alter table t /*T![force_inc] force */ auto_increment = 0;", errno.ErrAutoincReadFailed) + tk.MustExec("alter table t /*T![force_inc] force */ auto_increment = 2;") + require.Equal(t, uint64(2), getNextGlobalID()) + tk.MustExec("insert into t(b) values (2);") + tk.MustQuery("select a, b from t;").Check(testkit.Rows("1 1", "2 2")) + tk.MustExec("drop table if exists t;") - for _, str := range []string{"", " AUTO_ID_CACHE 1"} { - // Rebase auto_random. - tk.MustExec("create table t (a bigint primary key auto_random(5))" + str) - tk.MustExec("insert into t values ();") - tk.MustExec("set @@allow_auto_random_explicit_insert = true") - tk.MustExec("insert into t values (100000000);") - tk.MustExec("delete from t where a = 100000000;") - require.Greater(t, getNextGlobalID(), uint64(100000000)) - // Cannot set next global ID to 0. - tk.MustGetErrCode("alter table t force auto_random_base = 0;", errno.ErrAutoincReadFailed) - tk.MustExec("alter table t force auto_random_base = 2;") - require.Equal(t, uint64(2), getNextGlobalID()) - tk.MustExec("insert into t values ();") - tk.MustQuery("select (a & 3) from t order by 1;").Check(testkit.Rows("1", "2")) - tk.MustExec("drop table if exists t;") - } + // Rebase auto_random. + tk.MustExec("create table t (a bigint primary key auto_random(5))") + tk.MustExec("insert into t values ();") + tk.MustExec("set @@allow_auto_random_explicit_insert = true") + tk.MustExec("insert into t values (100000000);") + tk.MustExec("delete from t where a = 100000000;") + require.Greater(t, getNextGlobalID(), uint64(100000000)) + // Cannot set next global ID to 0. + tk.MustGetErrCode("alter table t force auto_random_base = 0;", errno.ErrAutoincReadFailed) + tk.MustExec("alter table t force auto_random_base = 2;") + require.Equal(t, uint64(2), getNextGlobalID()) + tk.MustExec("insert into t values ();") + tk.MustQuery("select (a & 3) from t order by 1;").Check(testkit.Rows("1", "2")) + tk.MustExec("drop table if exists t;") - for _, str := range []string{"", " AUTO_ID_CACHE 1"} { - // Change next global ID. - tk.MustExec("create table t (a bigint primary key auto_increment)" + str) - tk.MustExec("insert into t values (1);") - bases := []uint64{1, 65535, 10, math.MaxUint64, math.MaxInt64 + 1, 1, math.MaxUint64, math.MaxInt64, 2} - lastBase := fmt.Sprintf("%d", bases[len(bases)-1]) - for _, b := range bases { - fmt.Println("execute alter table force increment to ==", b) - tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b)) - require.Equal(t, b, getNextGlobalID()) - } + // Change next global ID. + tk.MustExec("create table t (a bigint primary key auto_increment)") + tk.MustExec("insert into t values (1);") + bases := []uint64{1, 65535, 10, math.MaxUint64, math.MaxInt64 + 1, 1, math.MaxUint64, math.MaxInt64, 2} + lastBase := fmt.Sprintf("%d", bases[len(bases)-1]) + for _, b := range bases { + fmt.Println("execute alter table force increment to ==", b) + tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b)) + require.Equal(t, b, getNextGlobalID()) + } + tk.MustExec("insert into t values ();") + tk.MustQuery("select a from t;").Check(testkit.Rows("1", lastBase)) + // Force alter unsigned int auto_increment column. + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (a bigint unsigned primary key auto_increment)") + for _, b := range bases { + tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b)) + require.Equal(t, b, getNextGlobalID()) tk.MustExec("insert into t values ();") - tk.MustQuery("select a from t;").Check(testkit.Rows("1", lastBase)) - // Force alter unsigned int auto_increment column. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a bigint unsigned primary key auto_increment)" + str) - for _, b := range bases { - tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b)) - require.Equal(t, b, getNextGlobalID()) - tk.MustExec("insert into t values ();") - tk.MustQuery("select a from t;").Check(testkit.Rows(fmt.Sprintf("%d", b))) - tk.MustExec("delete from t;") - } - tk.MustExec("drop table if exists t;") + tk.MustQuery("select a from t;").Check(testkit.Rows(fmt.Sprintf("%d", b))) + tk.MustExec("delete from t;") } + tk.MustExec("drop table if exists t;") - for _, str := range []string{"", " AUTO_ID_CACHE 1"} { - // Force alter with @@auto_increment_increment and @@auto_increment_offset. - tk.MustExec("create table t(a int key auto_increment)" + str) - tk.MustExec("set @@auto_increment_offset=2;") - tk.MustExec("set @@auto_increment_increment = 11;") - tk.MustExec("insert into t values (500);") - tk.MustExec("alter table t force auto_increment=100;") - tk.MustExec("insert into t values (), ();") - tk.MustQuery("select * from t;").Check(testkit.Rows("101", "112", "500")) - tk.MustQuery("select * from t order by a;").Check(testkit.Rows("101", "112", "500")) - tk.MustExec("drop table if exists t;") - } + // Force alter with @@auto_increment_increment and @@auto_increment_offset. + tk.MustExec("create table t(a int key auto_increment)") + tk.MustExec("set @@auto_increment_offset=2;") + tk.MustExec("set @@auto_increment_increment = 11;") + tk.MustExec("insert into t values (500);") + tk.MustExec("alter table t force auto_increment=100;") + tk.MustExec("insert into t values (), ();") + tk.MustQuery("select * from t;").Check(testkit.Rows("101", "112", "500")) + tk.MustQuery("select * from t order by a;").Check(testkit.Rows("101", "112", "500")) + tk.MustExec("drop table if exists t;") // Check for warning in case we can't set the auto_increment to the desired value tk.MustExec("create table t(a int primary key auto_increment)") @@ -3033,6 +3023,147 @@ func TestAutoIncrementForce(t *testing.T) { tk.MustExec("drop table t") } +func TestAutoIncrementForceAutoIDCache(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("drop database if exists auto_inc_force;") + tk.MustExec("create database auto_inc_force;") + tk.MustExec("use auto_inc_force;") + getNextGlobalID := func() uint64 { + gidStr := tk.MustQuery("show table t next_row_id").Rows()[0][3] + gid, err := strconv.ParseUint(gidStr.(string), 10, 64) + require.NoError(t, err) + return gid + } + + // Rebase _tidb_row_id. + tk.MustExec("create table t (a int) AUTO_ID_CACHE 1") + tk.MustExec("alter table t force auto_increment = 2;") + tk.MustExec("insert into t values (1),(2);") + tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 1", "2 2")) + // Cannot set next global ID to 0. + tk.MustExec("alter table t force auto_increment = 0;") + tk.MustExec("alter table t force auto_increment = 1;") + require.Equal(t, uint64(3), getNextGlobalID()) + // inserting new rows can overwrite the existing data. + tk.MustExec("insert into t values (3);") + tk.MustExec("insert into t values (3);") + tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 1", "2 2", "3 3", "3 4")) + tk.MustExec("drop table if exists t;") + + // When AUTO_ID_CACHE is 1, row id and auto increment id use separate allocator, so the behaviour differs. + // "Alter table t force auto_increment" has no effect on row id. + tk.MustExec("create table t (a int) AUTO_ID_CACHE 1") + tk.MustExec("alter table t force auto_increment = 2;") + tk.MustExec("insert into t values (1),(2);") + tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 1", "2 2")) + // Cannot set next global ID to 0. + tk.MustExec("alter table t force auto_increment = 0;") + tk.MustExec("alter table t force auto_increment = 1;") + require.Equal(t, uint64(3), getNextGlobalID()) + // inserting new rows can overwrite the existing data. + tk.MustExec("insert into t values (3);") + tk.MustExec("insert into t values (3);") + tk.MustQuery("select a, _tidb_rowid from t;").Check(testkit.Rows("1 1", "2 2", "3 3", "3 4")) + tk.MustExec("drop table if exists t;") + + // Rebase auto_increment. + tk.MustExec("create table t (a int primary key auto_increment, b int) AUTO_ID_CACHE 1") + tk.MustExec("insert into t values (1, 1);") + tk.MustExec("insert into t values (100000000, 1);") + tk.MustExec("delete from t where a = 100000000;") + tk.MustQuery("show table t next_row_id").Check(testkit.Rows( + "auto_inc_force t a 1 _TIDB_ROWID", + "auto_inc_force t a 100000001 AUTO_INCREMENT", + )) + // Cannot set next global ID to 0. + tk.MustGetErrCode("alter table t /*T![force_inc] force */ auto_increment = 0;", errno.ErrAutoincReadFailed) + tk.MustExec("alter table t /*T![force_inc] force */ auto_increment = 2;") + tk.MustQuery("show table t next_row_id").Check(testkit.Rows( + "auto_inc_force t a 1 _TIDB_ROWID", + "auto_inc_force t a 2 AUTO_INCREMENT", + )) + + tk.MustExec("insert into t(b) values (2);") + tk.MustQuery("select a, b from t;").Check(testkit.Rows("1 1", "2 2")) + tk.MustExec("drop table if exists t;") + + // Rebase auto_random. + tk.MustExec("create table t (a bigint primary key auto_random(5)) AUTO_ID_CACHE 1") + tk.MustExec("insert into t values ();") + tk.MustExec("set @@allow_auto_random_explicit_insert = true") + tk.MustExec("insert into t values (100000000);") + tk.MustExec("delete from t where a = 100000000;") + require.Greater(t, getNextGlobalID(), uint64(100000000)) + // Cannot set next global ID to 0. + tk.MustGetErrCode("alter table t force auto_random_base = 0;", errno.ErrAutoincReadFailed) + tk.MustExec("alter table t force auto_random_base = 2;") + require.Equal(t, uint64(2), getNextGlobalID()) + tk.MustExec("insert into t values ();") + tk.MustQuery("select (a & 3) from t order by 1;").Check(testkit.Rows("1", "2")) + tk.MustExec("drop table if exists t;") + + // Change next global ID. + tk.MustExec("create table t (a bigint primary key auto_increment) AUTO_ID_CACHE 1") + tk.MustExec("insert into t values (1);") + bases := []uint64{1, 65535, 10, math.MaxUint64, math.MaxInt64 + 1, 1, math.MaxUint64, math.MaxInt64, 2} + lastBase := fmt.Sprintf("%d", bases[len(bases)-1]) + for _, b := range bases { + fmt.Println("execute alter table force increment to ==", b) + tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b)) + tk.MustQuery("show table t next_row_id").Check(testkit.Rows( + "auto_inc_force t a 1 _TIDB_ROWID", + fmt.Sprintf("auto_inc_force t a %d AUTO_INCREMENT", b), + )) + } + tk.MustExec("insert into t values ();") + tk.MustQuery("select a from t;").Check(testkit.Rows("1", lastBase)) + // Force alter unsigned int auto_increment column. + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t (a bigint unsigned primary key auto_increment) AUTO_ID_CACHE 1") + for _, b := range bases { + tk.MustExec(fmt.Sprintf("alter table t force auto_increment = %d;", b)) + tk.MustQuery("show table t next_row_id").Check(testkit.Rows( + "auto_inc_force t a 1 _TIDB_ROWID", + fmt.Sprintf("auto_inc_force t a %d AUTO_INCREMENT", b), + )) + tk.MustExec("insert into t values ();") + tk.MustQuery("select a from t;").Check(testkit.Rows(fmt.Sprintf("%d", b))) + tk.MustExec("delete from t;") + } + tk.MustExec("drop table if exists t;") + + // Force alter with @@auto_increment_increment and @@auto_increment_offset. + tk.MustExec("create table t(a int key auto_increment) AUTO_ID_CACHE 1") + tk.MustExec("set @@auto_increment_offset=2;") + tk.MustExec("set @@auto_increment_increment = 11;") + tk.MustExec("insert into t values (500);") + tk.MustExec("alter table t force auto_increment=100;") + tk.MustExec("insert into t values (), ();") + tk.MustQuery("select * from t;").Check(testkit.Rows("101", "112", "500")) + tk.MustQuery("select * from t order by a;").Check(testkit.Rows("101", "112", "500")) + tk.MustExec("drop table if exists t;") + + // Check for warning in case we can't set the auto_increment to the desired value + tk.MustExec("create table t(a int primary key auto_increment) AUTO_ID_CACHE 1") + tk.MustExec("insert into t values (200)") + tk.MustQuery("show create table t").Check(testkit.Rows( + "t CREATE TABLE `t` (\n" + + " `a` int(11) NOT NULL AUTO_INCREMENT,\n" + + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=1 */")) + tk.MustExec("alter table t auto_increment=100;") + tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 Can't reset AUTO_INCREMENT to 100 without FORCE option, using 201 instead")) + tk.MustExec("insert into t values ()") + tk.MustQuery("select * from t").Check(testkit.Rows("200", "211")) + tk.MustQuery("show create table t").Check(testkit.Rows( + "t CREATE TABLE `t` (\n" + + " `a` int(11) NOT NULL AUTO_INCREMENT,\n" + + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=1 */")) + tk.MustExec("drop table t") +} + func TestIssue20490(t *testing.T) { store := testkit.CreateMockStore(t, mockstore.WithDDLChecker()) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 744a59d151086..0ff381ce6bc51 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -3367,7 +3367,7 @@ func (d *ddl) AlterTable(ctx context.Context, sctx sessionctx.Context, stmt *ast } err = d.ShardRowID(sctx, ident, opt.UintValue) case ast.TableOptionAutoIncrement: - err = d.RebaseAutoID(sctx, ident, int64(opt.UintValue), autoid.RowIDAllocType, opt.BoolValue) + err = d.RebaseAutoID(sctx, ident, int64(opt.UintValue), autoid.AutoIncrementType, opt.BoolValue) case ast.TableOptionAutoIdCache: if opt.UintValue > uint64(math.MaxInt64) { // TODO: Refine this error. @@ -3498,6 +3498,8 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6 actionType = model.ActionRebaseAutoRandomBase case autoid.RowIDAllocType: actionType = model.ActionRebaseAutoID + case autoid.AutoIncrementType: + actionType = model.ActionRebaseAutoID default: panic(fmt.Sprintf("unimplemented rebase autoid type %s", tp)) } diff --git a/infoschema/builder.go b/infoschema/builder.go index e708b4fef3b4f..67153fcc5f3bd 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -700,8 +700,13 @@ func (b *Builder) applyCreateTable(m *meta.Meta, dbInfo *model.DBInfo, tableID i switch tp { case model.ActionRebaseAutoID, model.ActionModifyTableAutoIdCache: idCacheOpt := autoid.CustomAutoIncCacheOption(tblInfo.AutoIdCache) - newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), autoid.RowIDAllocType, tblVer, idCacheOpt) - allocs = allocs.Append(newAlloc) + // If the allocator type might be AutoIncrementType, create both AutoIncrementType + // and RowIDAllocType allocator for it. Because auto id and row id could share the same allocator. + // Allocate auto id may route to allocate row id, if row id allocator is nil, the program panic! + for _, tp := range [2]autoid.AllocatorType{autoid.AutoIncrementType, autoid.RowIDAllocType} { + newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoIncColUnsigned(), tp, tblVer, idCacheOpt) + allocs = allocs.Append(newAlloc) + } case model.ActionRebaseAutoRandomBase: newAlloc := autoid.NewAllocator(b.store, dbInfo.ID, tblInfo.ID, tblInfo.IsAutoRandomBitColUnsigned(), autoid.AutoRandomType, tblVer) allocs = allocs.Append(newAlloc) From d11e237a6f24b57745f2b031c374af52d9b65729 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 29 Nov 2022 10:34:48 +0800 Subject: [PATCH 18/21] update test case for the changes --- ddl/ddl_api.go | 5 +++ executor/autoidtest/autoid_test.go | 53 +++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 0ff381ce6bc51..1816e0d65891d 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -5213,6 +5213,11 @@ func (d *ddl) AlterTableAutoIDCache(ctx sessionctx.Context, ident ast.Ident, new if err != nil { return errors.Trace(err) } + tbInfo := tb.Meta() + if (newCache == 1 && tbInfo.AutoIdCache != 1) || + (newCache != 1 && tbInfo.AutoIdCache == 1) { + return fmt.Errorf("Can't Alter AUTO_ID_CACHE between 1 and non-1, the underlying implementation is different") + } job := &model.Job{ SchemaID: schema.ID, diff --git a/executor/autoidtest/autoid_test.go b/executor/autoidtest/autoid_test.go index 269f3966f8a01..4bc2d7181ece4 100644 --- a/executor/autoidtest/autoid_test.go +++ b/executor/autoidtest/autoid_test.go @@ -642,14 +642,23 @@ func TestAutoIDIncrementAndOffset(t *testing.T) { tk.MustExec(`insert into io(b) values (null),(null),(null)`) // AutoID allocation will take increment and offset into consideration. tk.MustQuery(`select b from io`).Check(testkit.Rows("10", "12", "14")) - // HandleID allocation will ignore the increment and offset. - tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("15", "16", "17")) + if str == "" { + // HandleID allocation will ignore the increment and offset. + tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("15", "16", "17")) + } else { + // Separate row id and auto inc id, increment and offset works on auto inc id + tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("1", "2", "3")) + } tk.MustExec(`delete from io`) tk.Session().GetSessionVars().AutoIncrementIncrement = 10 tk.MustExec(`insert into io(b) values (null),(null),(null)`) tk.MustQuery(`select b from io`).Check(testkit.Rows("20", "30", "40")) - tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("41", "42", "43")) + if str == "" { + tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("41", "42", "43")) + } else { + tk.MustQuery(`select _tidb_rowid from io`).Check(testkit.Rows("4", "5", "6")) + } // Test invalid value. tk.Session().GetSessionVars().AutoIncrementIncrement = -1 @@ -711,27 +720,49 @@ func TestAlterTableAutoIDCache(t *testing.T) { require.NoError(t, err2) tk.MustExec("alter table t_473 auto_id_cache = 100") - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows(fmt.Sprintf("test t_473 id %d AUTO_INCREMENT", val))) + tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( + fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val), + "test t_473 id 1 AUTO_INCREMENT", + )) tk.MustExec("insert into t_473 values ()") tk.MustQuery("select * from t_473").Check(testkit.Rows("1", fmt.Sprintf("%d", val))) - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows(fmt.Sprintf("test t_473 id %d AUTO_INCREMENT", val+100))) + tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( + fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+100), + "test t_473 id 1 AUTO_INCREMENT", + )) + + // Note that auto_id_cache=1 use a different implementation, switch between them is not allowed. + // TODO: relax this restriction and update the test case. + _, err = tk.Exec("alter table t_473 auto_id_cache = 1") + require.Error(t, err) + return - // Note that auto_id_cache=1 use a different implementation. - tk.MustExec("alter table t_473 auto_id_cache = 1") - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows(fmt.Sprintf("test t_473 id %d AUTO_INCREMENT", val+100))) + tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( + fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+100), + "test t_473 id 1 AUTO_INCREMENT", + )) tk.MustExec("insert into t_473 values ()") tk.MustQuery("select * from t_473").Check(testkit.Rows("1", fmt.Sprintf("%d", val), fmt.Sprintf("%d", val+100))) - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows(fmt.Sprintf("test t_473 id %d AUTO_INCREMENT", val+101))) + tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( + fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+101), + "test t_473 id 1 AUTO_INCREMENT", + )) // alter table from auto_id_cache=1 to default will discard the IDs cached by the autoid service. // This is because they are two component and TiDB can't tell the autoid service to "save position and exit". tk.MustExec("alter table t_473 auto_id_cache = 20000") - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows(fmt.Sprintf("test t_473 id %d AUTO_INCREMENT", val+4100))) + tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( + fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+4100), + "test t_473 id 1 AUTO_INCREMENT", + )) tk.MustExec("insert into t_473 values ()") tk.MustQuery("select * from t_473").Check(testkit.Rows("1", fmt.Sprintf("%d", val), fmt.Sprintf("%d", val+100), fmt.Sprintf("%d", val+4100))) - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows(fmt.Sprintf("test t_473 id %d AUTO_INCREMENT", val+24100))) + tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( + fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+24100), + "test t_473 id 1 AUTO_INCREMENT", + )) } From b08b9fbc05f2e5eb705c88ef21ff6868b1d38115 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 29 Nov 2022 11:34:46 +0800 Subject: [PATCH 19/21] fix build --- executor/autoidtest/autoid_test.go | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/executor/autoidtest/autoid_test.go b/executor/autoidtest/autoid_test.go index 4bc2d7181ece4..357439676d2b2 100644 --- a/executor/autoidtest/autoid_test.go +++ b/executor/autoidtest/autoid_test.go @@ -736,33 +736,4 @@ func TestAlterTableAutoIDCache(t *testing.T) { _, err = tk.Exec("alter table t_473 auto_id_cache = 1") require.Error(t, err) return - - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( - fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+100), - "test t_473 id 1 AUTO_INCREMENT", - )) - tk.MustExec("insert into t_473 values ()") - tk.MustQuery("select * from t_473").Check(testkit.Rows("1", fmt.Sprintf("%d", val), fmt.Sprintf("%d", val+100))) - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( - fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+101), - "test t_473 id 1 AUTO_INCREMENT", - )) - - // alter table from auto_id_cache=1 to default will discard the IDs cached by the autoid service. - // This is because they are two component and TiDB can't tell the autoid service to "save position and exit". - tk.MustExec("alter table t_473 auto_id_cache = 20000") - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( - fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+4100), - "test t_473 id 1 AUTO_INCREMENT", - )) - - tk.MustExec("insert into t_473 values ()") - tk.MustQuery("select * from t_473").Check(testkit.Rows("1", - fmt.Sprintf("%d", val), - fmt.Sprintf("%d", val+100), - fmt.Sprintf("%d", val+4100))) - tk.MustQuery("show table t_473 next_row_id").Check(testkit.Rows( - fmt.Sprintf("test t_473 id %d _TIDB_ROWID", val+24100), - "test t_473 id 1 AUTO_INCREMENT", - )) } From fded827c14c8c7dfdcd50de4047567c70d058ec6 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 29 Nov 2022 11:45:49 +0800 Subject: [PATCH 20/21] fix build --- executor/autoidtest/autoid_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/executor/autoidtest/autoid_test.go b/executor/autoidtest/autoid_test.go index 357439676d2b2..f52f63b9c59a2 100644 --- a/executor/autoidtest/autoid_test.go +++ b/executor/autoidtest/autoid_test.go @@ -735,5 +735,4 @@ func TestAlterTableAutoIDCache(t *testing.T) { // TODO: relax this restriction and update the test case. _, err = tk.Exec("alter table t_473 auto_id_cache = 1") require.Error(t, err) - return } From 90ab5fae4ad6d6aaa867312b15b73e6858b23ab2 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 29 Nov 2022 12:35:17 +0800 Subject: [PATCH 21/21] fix CI --- ddl/multi_schema_change_test.go | 2 +- telemetry/data_feature_usage_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ddl/multi_schema_change_test.go b/ddl/multi_schema_change_test.go index f302737d60c3b..0f8aeca87802c 100644 --- a/ddl/multi_schema_change_test.go +++ b/ddl/multi_schema_change_test.go @@ -1177,7 +1177,7 @@ func TestMultiSchemaChangeUnsupportedType(t *testing.T) { tk.MustExec("use test;") tk.MustExec("create table t (a int, b int);") - tk.MustGetErrMsg("alter table t add column c int, auto_id_cache = 1;", + tk.MustGetErrMsg("alter table t add column c int, auto_id_cache = 10;", "[ddl:8200]Unsupported multi schema change for modify auto id cache") } diff --git a/telemetry/data_feature_usage_test.go b/telemetry/data_feature_usage_test.go index 770f4a7d3d08e..8e20ac09ff4d1 100644 --- a/telemetry/data_feature_usage_test.go +++ b/telemetry/data_feature_usage_test.go @@ -143,7 +143,7 @@ func TestAutoIDNoCache(t *testing.T) { usage, err = telemetry.GetFeatureUsage(tk.Session()) require.NoError(t, err) require.True(t, usage.AutoIDNoCache) - tk.MustExec("alter table tele_autoid auto_id_cache=0") + tk.MustExec("drop table tele_autoid") usage, err = telemetry.GetFeatureUsage(tk.Session()) require.NoError(t, err) require.False(t, usage.AutoIDNoCache)