Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta,ddl: fix duplicate entry error when insert after drop and recover table (#52761) #53185

Open
wants to merge 3 commits into
base: release-7.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -4418,3 +4419,68 @@ 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()
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)

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"
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(dbInfo.ID, 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()
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);")
tk.MustQuery("select * from issue52680").Check(testkit.Rows("1", "2", "3"))
}
2 changes: 1 addition & 1 deletion ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +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
err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs.RowID, recoverInfo.AutoIDs.RandomID)
err = t.CreateTableAndSetAutoID(recoverInfo.SchemaID, tableInfo, recoverInfo.AutoIDs)
if err != nil {
return ver, errors.Trace(err)
}
Expand Down
12 changes: 9 additions & 3 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,17 +761,23 @@

// CreateTableAndSetAutoID creates a table with tableInfo in database,
// and rebases the table autoID.
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)
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)
}

Check warning on line 777 in meta/meta.go

View check run for this annotation

Codecov / codecov/patch

meta/meta.go#L776-L777

Added lines #L776 - L777 were not covered by tests
}
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)
}
Expand Down
2 changes: 1 addition & 1 deletion meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func TestMeta(t *testing.T) {
ID: 3,
Name: model.NewCIStr("tbl3"),
}
err = m.CreateTableAndSetAutoID(1, tbInfo3, 123, 0)
err = m.CreateTableAndSetAutoID(1, tbInfo3, meta.AutoIDGroup{RowID: 123, IncrementID: 0})
require.NoError(t, err)
id, err := m.GetAutoIDAccessors(1, tbInfo3.ID).RowID().Get()
require.NoError(t, err)
Expand Down