Skip to content

Commit

Permalink
*: fix 'duplicate entry' error when using br to restore AUTO_ID_CACHE…
Browse files Browse the repository at this point in the history
…=1 tables (#44743) (#44816)

ref #38442, close #44716
  • Loading branch information
ti-chi-bot authored Aug 2, 2023
1 parent cb6e7cb commit df3fa99
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion br/pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ func BuildBackupRangeAndSchema(
zap.String("table", tableInfo.Name.O),
)

autoIDAccess := m.GetAutoIDAccessors(dbInfo.ID, tableInfo.ID)
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)
Expand All @@ -575,7 +576,11 @@ func BuildBackupRangeAndSchema(
case tableInfo.IsView() || !utils.NeedAutoID(tableInfo):
// no auto ID for views or table without either rowID nor auto_increment ID.
default:
globalAutoID, err = idAlloc.NextGlobalAutoID()
if tableInfo.SepAutoInc() {
globalAutoID, err = autoIDAccess.IncrementID(tableInfo.Version).Get()
} else {
globalAutoID, err = idAlloc.NextGlobalAutoID()
}
}
if err != nil {
return nil, nil, nil, errors.Trace(err)
Expand Down
4 changes: 2 additions & 2 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3139,7 +3139,7 @@ func TestAutoIncrementForceAutoIDCache(t *testing.T) {
"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 */"))
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=201 /*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 ()")
Expand All @@ -3148,7 +3148,7 @@ func TestAutoIncrementForceAutoIDCache(t *testing.T) {
"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 */"))
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=212 /*T![auto_id_cache] AUTO_ID_CACHE=1 */"))
tk.MustExec("drop table t")
}

Expand Down
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T
fmt.Fprintf(buf, " COMPRESSION='%s'", tableInfo.Compression)
}

incrementAllocator := allocators.Get(autoid.RowIDAllocType)
incrementAllocator := allocators.Get(autoid.AutoIncrementType)
if hasAutoIncID && incrementAllocator != nil {
autoIncID, err := incrementAllocator.NextGlobalAutoID()
if err != nil {
Expand Down

0 comments on commit df3fa99

Please sign in to comment.