From 3cd3b6a4230be2b13c46ae4dda5047330bad497f Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 22 Apr 2024 18:13:40 +0800 Subject: [PATCH 1/3] This is an automated cherry-pick of #52761 Signed-off-by: ti-chi-bot --- ddl/db_integration_test.go | 91 ++++++++++++++++++++++++++++++++++++++ ddl/table.go | 4 ++ meta/meta.go | 15 ++++++- meta/meta_test.go | 4 ++ 4 files changed, 112 insertions(+), 2 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index d52ad7f412f95..be3775843f256 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -26,6 +26,7 @@ import ( "time" "github.com/pingcap/errors" +<<<<<<< HEAD:ddl/db_integration_test.go _ "github.com/pingcap/tidb/autoid_service" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl/schematracker" @@ -53,6 +54,35 @@ import ( "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/dbterror" "github.com/pingcap/tidb/util/mock" +======= + _ "github.com/pingcap/tidb/pkg/autoid_service" + "github.com/pingcap/tidb/pkg/config" + "github.com/pingcap/tidb/pkg/ddl/schematracker" + ddlutil "github.com/pingcap/tidb/pkg/ddl/util" + "github.com/pingcap/tidb/pkg/ddl/util/callback" + "github.com/pingcap/tidb/pkg/domain" + "github.com/pingcap/tidb/pkg/errno" + "github.com/pingcap/tidb/pkg/infoschema" + "github.com/pingcap/tidb/pkg/kv" + "github.com/pingcap/tidb/pkg/meta" + "github.com/pingcap/tidb/pkg/parser/auth" + "github.com/pingcap/tidb/pkg/parser/charset" + "github.com/pingcap/tidb/pkg/parser/model" + "github.com/pingcap/tidb/pkg/parser/mysql" + "github.com/pingcap/tidb/pkg/parser/terror" + "github.com/pingcap/tidb/pkg/session" + "github.com/pingcap/tidb/pkg/sessionctx/stmtctx" + "github.com/pingcap/tidb/pkg/sessionctx/variable" + "github.com/pingcap/tidb/pkg/sessiontxn" + "github.com/pingcap/tidb/pkg/store/mockstore" + "github.com/pingcap/tidb/pkg/tablecodec" + "github.com/pingcap/tidb/pkg/testkit" + "github.com/pingcap/tidb/pkg/testkit/external" + "github.com/pingcap/tidb/pkg/util/collate" + "github.com/pingcap/tidb/pkg/util/dbterror" + "github.com/pingcap/tidb/pkg/util/dbterror/plannererrors" + "github.com/pingcap/tidb/pkg/util/mock" +>>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/ddl/db_integration_test.go "github.com/stretchr/testify/require" ) @@ -4418,3 +4448,64 @@ func TestReorganizePartitionWarning(t *testing.T) { tk.MustExec("alter table t reorganize partition p0 into (partition p01 values less than (10), partition p02 values less than (20));") tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 The statistics of related partitions will be outdated after reorganizing partitions. Please use 'ANALYZE TABLE' statement if you want to update it now")) } + +func TestIssue52680(t *testing.T) { + store, dom := testkit.CreateMockStoreAndDomain(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + tk.MustExec("create table issue52680 (id bigint primary key auto_increment) auto_id_cache=1;") + tk.MustExec("insert into issue52680 values(default),(default);") + tk.MustQuery("select * from issue52680").Check(testkit.Rows("1", "2")) + + is := dom.InfoSchema() + ti, err := is.TableInfoByName(model.NewCIStr("test"), model.NewCIStr("issue52680")) + require.NoError(t, err) + + ddlutil.EmulatorGCDisable() + defer ddlutil.EmulatorGCEnable() + + // For mocktikv, safe point is not initialized, we manually insert it for snapshot to use. + safePointName := "tikv_gc_safe_point" + safePointValue := "20060102-15:04:05 -0700" + safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)" + updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s') + ON DUPLICATE KEY + UPDATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment) + tk.MustExec(updateSafePoint) + + testSteps := []struct { + sql string + expect meta.AutoIDGroup + }{ + {sql: "", expect: meta.AutoIDGroup{RowID: 0, IncrementID: 4000, RandomID: 0}}, + {sql: "drop table issue52680", expect: meta.AutoIDGroup{RowID: 0, IncrementID: 0, RandomID: 0}}, + {sql: "recover table issue52680", expect: meta.AutoIDGroup{RowID: 0, IncrementID: 4000, RandomID: 0}}, + } + for _, step := range testSteps { + if step.sql != "" { + tk.MustExec(step.sql) + } + + txn, err := store.Begin() + require.NoError(t, err) + m := meta.NewMeta(txn) + idAcc := m.GetAutoIDAccessors(ti.DBID, ti.ID) + ids, err := idAcc.Get() + require.NoError(t, err) + require.Equal(t, ids, step.expect) + txn.Rollback() + } + + tk.MustQuery("show table issue52680 next_row_id").Check(testkit.Rows( + "test issue52680 id 1 _TIDB_ROWID", + "test issue52680 id 3 AUTO_INCREMENT", + )) + + is = dom.InfoSchema() + ti1, err := is.TableInfoByName(model.NewCIStr("test"), model.NewCIStr("issue52680")) + require.NoError(t, err) + require.Equal(t, ti1.ID, ti.ID) + + tk.MustExec("insert into issue52680 values(default);") + tk.MustQuery("select * from issue52680").Check(testkit.Rows("1", "2", "3")) +} diff --git a/ddl/table.go b/ddl/table.go index 191d102f60dd8..e0269ea2da11b 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -533,7 +533,11 @@ func (w *worker) recoverTable(t *meta.Meta, job *model.Job, recoverInfo *Recover tableInfo := recoverInfo.TableInfo.Clone() tableInfo.State = model.StatePublic tableInfo.UpdateTS = t.StartTS +<<<<<<< HEAD:ddl/table.go err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs.RowID, recoverInfo.AutoIDs.RandomID) +======= + err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, recoverInfo.OldSchemaName, tableInfo, recoverInfo.AutoIDs) +>>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/ddl/table.go if err != nil { return ver, errors.Trace(err) } diff --git a/meta/meta.go b/meta/meta.go index 02e99d0f106d2..fba63bcd5b156 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -761,17 +761,28 @@ func (m *Meta) GetMetadataLock() (enable bool, isNull bool, err error) { // CreateTableAndSetAutoID creates a table with tableInfo in database, // and rebases the table autoID. +<<<<<<< HEAD:meta/meta.go func (m *Meta) CreateTableAndSetAutoID(dbID int64, tableInfo *model.TableInfo, autoIncID, autoRandID int64) error { err := m.CreateTableOrView(dbID, tableInfo) +======= +func (m *Meta) CreateTableAndSetAutoID(dbID int64, dbName string, tableInfo *model.TableInfo, autoIDs AutoIDGroup) error { + err := m.CreateTableOrView(dbID, dbName, tableInfo) +>>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/meta/meta.go if err != nil { return errors.Trace(err) } - _, err = m.txn.HInc(m.dbKey(dbID), m.autoTableIDKey(tableInfo.ID), autoIncID) + _, err = m.txn.HInc(m.dbKey(dbID), m.autoTableIDKey(tableInfo.ID), autoIDs.RowID) if err != nil { return errors.Trace(err) } if tableInfo.AutoRandomBits > 0 { - _, err = m.txn.HInc(m.dbKey(dbID), m.autoRandomTableIDKey(tableInfo.ID), autoRandID) + _, err = m.txn.HInc(m.dbKey(dbID), m.autoRandomTableIDKey(tableInfo.ID), autoIDs.RandomID) + if err != nil { + return errors.Trace(err) + } + } + if tableInfo.SepAutoInc() && tableInfo.GetAutoIncrementColInfo() != nil { + _, err = m.txn.HInc(m.dbKey(dbID), m.autoIncrementIDKey(tableInfo.ID), autoIDs.IncrementID) if err != nil { return errors.Trace(err) } diff --git a/meta/meta_test.go b/meta/meta_test.go index 747d64073064c..6f326149325fe 100644 --- a/meta/meta_test.go +++ b/meta/meta_test.go @@ -394,7 +394,11 @@ func TestMeta(t *testing.T) { ID: 3, Name: model.NewCIStr("tbl3"), } +<<<<<<< HEAD:meta/meta_test.go err = m.CreateTableAndSetAutoID(1, tbInfo3, 123, 0) +======= + err = m.CreateTableAndSetAutoID(1, dbInfo.Name.L, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0}) +>>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/meta/meta_test.go require.NoError(t, err) id, err := m.GetAutoIDAccessors(1, tbInfo3.ID).RowID().Get() require.NoError(t, err) From 086d1eaac7ec059de3388c30f92ce91143089fc6 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 3 Jun 2024 20:36:29 +0800 Subject: [PATCH 2/3] resolve conflict --- ddl/db_integration_test.go | 30 ------------------------------ ddl/table.go | 6 +----- meta/meta.go | 7 +------ meta/meta_test.go | 6 +----- 4 files changed, 3 insertions(+), 46 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index be3775843f256..2d6344e6025a6 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -26,7 +26,6 @@ import ( "time" "github.com/pingcap/errors" -<<<<<<< HEAD:ddl/db_integration_test.go _ "github.com/pingcap/tidb/autoid_service" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl/schematracker" @@ -54,35 +53,6 @@ import ( "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/dbterror" "github.com/pingcap/tidb/util/mock" -======= - _ "github.com/pingcap/tidb/pkg/autoid_service" - "github.com/pingcap/tidb/pkg/config" - "github.com/pingcap/tidb/pkg/ddl/schematracker" - ddlutil "github.com/pingcap/tidb/pkg/ddl/util" - "github.com/pingcap/tidb/pkg/ddl/util/callback" - "github.com/pingcap/tidb/pkg/domain" - "github.com/pingcap/tidb/pkg/errno" - "github.com/pingcap/tidb/pkg/infoschema" - "github.com/pingcap/tidb/pkg/kv" - "github.com/pingcap/tidb/pkg/meta" - "github.com/pingcap/tidb/pkg/parser/auth" - "github.com/pingcap/tidb/pkg/parser/charset" - "github.com/pingcap/tidb/pkg/parser/model" - "github.com/pingcap/tidb/pkg/parser/mysql" - "github.com/pingcap/tidb/pkg/parser/terror" - "github.com/pingcap/tidb/pkg/session" - "github.com/pingcap/tidb/pkg/sessionctx/stmtctx" - "github.com/pingcap/tidb/pkg/sessionctx/variable" - "github.com/pingcap/tidb/pkg/sessiontxn" - "github.com/pingcap/tidb/pkg/store/mockstore" - "github.com/pingcap/tidb/pkg/tablecodec" - "github.com/pingcap/tidb/pkg/testkit" - "github.com/pingcap/tidb/pkg/testkit/external" - "github.com/pingcap/tidb/pkg/util/collate" - "github.com/pingcap/tidb/pkg/util/dbterror" - "github.com/pingcap/tidb/pkg/util/dbterror/plannererrors" - "github.com/pingcap/tidb/pkg/util/mock" ->>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/ddl/db_integration_test.go "github.com/stretchr/testify/require" ) diff --git a/ddl/table.go b/ddl/table.go index e0269ea2da11b..0ca8ab6c4a7c5 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -533,11 +533,7 @@ func (w *worker) recoverTable(t *meta.Meta, job *model.Job, recoverInfo *Recover tableInfo := recoverInfo.TableInfo.Clone() tableInfo.State = model.StatePublic tableInfo.UpdateTS = t.StartTS -<<<<<<< HEAD:ddl/table.go - err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs.RowID, recoverInfo.AutoIDs.RandomID) -======= - err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, recoverInfo.OldSchemaName, tableInfo, recoverInfo.AutoIDs) ->>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/ddl/table.go + err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs) if err != nil { return ver, errors.Trace(err) } diff --git a/meta/meta.go b/meta/meta.go index fba63bcd5b156..b5fde687bb21d 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -761,13 +761,8 @@ func (m *Meta) GetMetadataLock() (enable bool, isNull bool, err error) { // CreateTableAndSetAutoID creates a table with tableInfo in database, // and rebases the table autoID. -<<<<<<< HEAD:meta/meta.go -func (m *Meta) CreateTableAndSetAutoID(dbID int64, tableInfo *model.TableInfo, autoIncID, autoRandID int64) error { +func (m *Meta) CreateTableAndSetAutoID(dbID int64, tableInfo *model.TableInfo, autoIDs AutoIDGroup) error { err := m.CreateTableOrView(dbID, tableInfo) -======= -func (m *Meta) CreateTableAndSetAutoID(dbID int64, dbName string, tableInfo *model.TableInfo, autoIDs AutoIDGroup) error { - err := m.CreateTableOrView(dbID, dbName, tableInfo) ->>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/meta/meta.go if err != nil { return errors.Trace(err) } diff --git a/meta/meta_test.go b/meta/meta_test.go index 6f326149325fe..1db032fe87fdb 100644 --- a/meta/meta_test.go +++ b/meta/meta_test.go @@ -394,11 +394,7 @@ func TestMeta(t *testing.T) { ID: 3, Name: model.NewCIStr("tbl3"), } -<<<<<<< HEAD:meta/meta_test.go - err = m.CreateTableAndSetAutoID(1, tbInfo3, 123, 0) -======= - err = m.CreateTableAndSetAutoID(1, dbInfo.Name.L, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0}) ->>>>>>> 4b6e8ee0306 (meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761)):pkg/meta/meta_test.go + err = m.CreateTableAndSetAutoID(1, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0}) require.NoError(t, err) id, err := m.GetAutoIDAccessors(1, tbInfo3.ID).RowID().Get() require.NoError(t, err) From d39dfd01a97a06ce6443d828b16e79e96bd500c9 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 3 Jun 2024 21:03:43 +0800 Subject: [PATCH 3/3] fix test --- ddl/db_integration_test.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 2d6344e6025a6..f06a4b1c19c94 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -29,6 +29,7 @@ import ( _ "github.com/pingcap/tidb/autoid_service" "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/ddl/schematracker" + "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/ddl/util/callback" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/errno" @@ -4428,11 +4429,14 @@ func TestIssue52680(t *testing.T) { tk.MustQuery("select * from issue52680").Check(testkit.Rows("1", "2")) is := dom.InfoSchema() - ti, err := is.TableInfoByName(model.NewCIStr("test"), model.NewCIStr("issue52680")) + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("issue52680")) + ti := tbl.Meta() require.NoError(t, err) + dbInfo, ok := is.SchemaByName(model.NewCIStr("test")) + require.True(t, ok) - ddlutil.EmulatorGCDisable() - defer ddlutil.EmulatorGCEnable() + util.EmulatorGCDisable() + defer util.EmulatorGCEnable() // For mocktikv, safe point is not initialized, we manually insert it for snapshot to use. safePointName := "tikv_gc_safe_point" @@ -4459,7 +4463,7 @@ func TestIssue52680(t *testing.T) { txn, err := store.Begin() require.NoError(t, err) m := meta.NewMeta(txn) - idAcc := m.GetAutoIDAccessors(ti.DBID, ti.ID) + idAcc := m.GetAutoIDAccessors(dbInfo.ID, ti.ID) ids, err := idAcc.Get() require.NoError(t, err) require.Equal(t, ids, step.expect) @@ -4472,8 +4476,9 @@ func TestIssue52680(t *testing.T) { )) is = dom.InfoSchema() - ti1, err := is.TableInfoByName(model.NewCIStr("test"), model.NewCIStr("issue52680")) + tbl1, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("issue52680")) require.NoError(t, err) + ti1 := tbl1.Meta() require.Equal(t, ti1.ID, ti.ID) tk.MustExec("insert into issue52680 values(default);")