Skip to content

Commit 0c8eb42

Browse files
authored
meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761) (#53185)
close #52680
1 parent 66a9025 commit 0c8eb42

File tree

4 files changed

+77
-5
lines changed

4 files changed

+77
-5
lines changed

ddl/db_integration_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
_ "github.com/pingcap/tidb/autoid_service"
3030
"github.com/pingcap/tidb/config"
3131
"github.com/pingcap/tidb/ddl/schematracker"
32+
"github.com/pingcap/tidb/ddl/util"
3233
"github.com/pingcap/tidb/ddl/util/callback"
3334
"github.com/pingcap/tidb/domain"
3435
"github.com/pingcap/tidb/errno"
@@ -4418,3 +4419,68 @@ func TestReorganizePartitionWarning(t *testing.T) {
44184419
tk.MustExec("alter table t reorganize partition p0 into (partition p01 values less than (10), partition p02 values less than (20));")
44194420
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"))
44204421
}
4422+
4423+
func TestIssue52680(t *testing.T) {
4424+
store, dom := testkit.CreateMockStoreAndDomain(t)
4425+
tk := testkit.NewTestKit(t, store)
4426+
tk.MustExec("use test;")
4427+
tk.MustExec("create table issue52680 (id bigint primary key auto_increment) auto_id_cache=1;")
4428+
tk.MustExec("insert into issue52680 values(default),(default);")
4429+
tk.MustQuery("select * from issue52680").Check(testkit.Rows("1", "2"))
4430+
4431+
is := dom.InfoSchema()
4432+
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("issue52680"))
4433+
ti := tbl.Meta()
4434+
require.NoError(t, err)
4435+
dbInfo, ok := is.SchemaByName(model.NewCIStr("test"))
4436+
require.True(t, ok)
4437+
4438+
util.EmulatorGCDisable()
4439+
defer util.EmulatorGCEnable()
4440+
4441+
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
4442+
safePointName := "tikv_gc_safe_point"
4443+
safePointValue := "20060102-15:04:05 -0700"
4444+
safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
4445+
updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
4446+
ON DUPLICATE KEY
4447+
UPDATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment)
4448+
tk.MustExec(updateSafePoint)
4449+
4450+
testSteps := []struct {
4451+
sql string
4452+
expect meta.AutoIDGroup
4453+
}{
4454+
{sql: "", expect: meta.AutoIDGroup{RowID: 0, IncrementID: 4000, RandomID: 0}},
4455+
{sql: "drop table issue52680", expect: meta.AutoIDGroup{RowID: 0, IncrementID: 0, RandomID: 0}},
4456+
{sql: "recover table issue52680", expect: meta.AutoIDGroup{RowID: 0, IncrementID: 4000, RandomID: 0}},
4457+
}
4458+
for _, step := range testSteps {
4459+
if step.sql != "" {
4460+
tk.MustExec(step.sql)
4461+
}
4462+
4463+
txn, err := store.Begin()
4464+
require.NoError(t, err)
4465+
m := meta.NewMeta(txn)
4466+
idAcc := m.GetAutoIDAccessors(dbInfo.ID, ti.ID)
4467+
ids, err := idAcc.Get()
4468+
require.NoError(t, err)
4469+
require.Equal(t, ids, step.expect)
4470+
txn.Rollback()
4471+
}
4472+
4473+
tk.MustQuery("show table issue52680 next_row_id").Check(testkit.Rows(
4474+
"test issue52680 id 1 _TIDB_ROWID",
4475+
"test issue52680 id 3 AUTO_INCREMENT",
4476+
))
4477+
4478+
is = dom.InfoSchema()
4479+
tbl1, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("issue52680"))
4480+
require.NoError(t, err)
4481+
ti1 := tbl1.Meta()
4482+
require.Equal(t, ti1.ID, ti.ID)
4483+
4484+
tk.MustExec("insert into issue52680 values(default);")
4485+
tk.MustQuery("select * from issue52680").Check(testkit.Rows("1", "2", "3"))
4486+
}

ddl/table.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func (w *worker) recoverTable(t *meta.Meta, job *model.Job, recoverInfo *Recover
534534
tableInfo := recoverInfo.TableInfo.Clone()
535535
tableInfo.State = model.StatePublic
536536
tableInfo.UpdateTS = t.StartTS
537-
err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs.RowID, recoverInfo.AutoIDs.RandomID)
537+
err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs)
538538
if err != nil {
539539
return ver, errors.Trace(err)
540540
}

meta/meta.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,17 +761,23 @@ func (m *Meta) GetMetadataLock() (enable bool, isNull bool, err error) {
761761

762762
// CreateTableAndSetAutoID creates a table with tableInfo in database,
763763
// and rebases the table autoID.
764-
func (m *Meta) CreateTableAndSetAutoID(dbID int64, tableInfo *model.TableInfo, autoIncID, autoRandID int64) error {
764+
func (m *Meta) CreateTableAndSetAutoID(dbID int64, tableInfo *model.TableInfo, autoIDs AutoIDGroup) error {
765765
err := m.CreateTableOrView(dbID, tableInfo)
766766
if err != nil {
767767
return errors.Trace(err)
768768
}
769-
_, err = m.txn.HInc(m.dbKey(dbID), m.autoTableIDKey(tableInfo.ID), autoIncID)
769+
_, err = m.txn.HInc(m.dbKey(dbID), m.autoTableIDKey(tableInfo.ID), autoIDs.RowID)
770770
if err != nil {
771771
return errors.Trace(err)
772772
}
773773
if tableInfo.AutoRandomBits > 0 {
774-
_, err = m.txn.HInc(m.dbKey(dbID), m.autoRandomTableIDKey(tableInfo.ID), autoRandID)
774+
_, err = m.txn.HInc(m.dbKey(dbID), m.autoRandomTableIDKey(tableInfo.ID), autoIDs.RandomID)
775+
if err != nil {
776+
return errors.Trace(err)
777+
}
778+
}
779+
if tableInfo.SepAutoInc() && tableInfo.GetAutoIncrementColInfo() != nil {
780+
_, err = m.txn.HInc(m.dbKey(dbID), m.autoIncrementIDKey(tableInfo.ID), autoIDs.IncrementID)
775781
if err != nil {
776782
return errors.Trace(err)
777783
}

meta/meta_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func TestMeta(t *testing.T) {
394394
ID: 3,
395395
Name: model.NewCIStr("tbl3"),
396396
}
397-
err = m.CreateTableAndSetAutoID(1, tbInfo3, 123, 0)
397+
err = m.CreateTableAndSetAutoID(1, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0})
398398
require.NoError(t, err)
399399
id, err := m.GetAutoIDAccessors(1, tbInfo3.ID).RowID().Get()
400400
require.NoError(t, err)

0 commit comments

Comments
 (0)