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

*: fix the duplicate entry error when using BR to restore a NONCLUSTERED AUTO_ID_CACHE=1 table #46127

Merged
merged 8 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions br/pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ func BuildBackupSchemas(
default:
if tableInfo.SepAutoInc() {
globalAutoID, err = autoIDAccess.IncrementID(tableInfo.Version).Get()
// For a nonclustered table with auto_increment column, both auto_increment_id and _tidb_rowid are required.
// See also https://github.com/pingcap/tidb/issues/46093
if rowID, err1 := autoIDAccess.RowID().Get(); err1 == nil {
3pointer marked this conversation as resolved.
Show resolved Hide resolved
tableInfo.AutoIncIDExtra = rowID + 1
}
} else {
globalAutoID, err = autoIDAccess.RowID().Get()
}
Expand Down
6 changes: 6 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,12 @@ func (d *ddl) createTableWithInfoPost(
return errors.Trace(err)
}
}
// For issue https://github.com/pingcap/tidb/issues/46093
if tbInfo.AutoIncIDExtra != 0 {
if err = d.handleAutoIncID(tbInfo, schemaID, tbInfo.AutoIncIDExtra-1, autoid.RowIDAllocType); err != nil {
return errors.Trace(err)
}
}
if tbInfo.AutoRandID > 1 {
// Default tableAutoRandID base is 0.
// If the first ID is expected to greater than 1, we need to do rebase.
Expand Down
31 changes: 23 additions & 8 deletions parser/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,29 @@ type TableInfo struct {
// 1 for the clustered index created > 5.0.0 RC.
CommonHandleVersion uint16 `json:"common_handle_version"`

Comment string `json:"comment"`
AutoIncID int64 `json:"auto_inc_id"`
AutoIdCache int64 `json:"auto_id_cache"` //nolint:revive
AutoRandID int64 `json:"auto_rand_id"`
MaxColumnID int64 `json:"max_col_id"`
MaxIndexID int64 `json:"max_idx_id"`
MaxForeignKeyID int64 `json:"max_fk_id"`
MaxConstraintID int64 `json:"max_cst_id"`
Comment string `json:"comment"`
AutoIncID int64 `json:"auto_inc_id"`

// Only used by BR when:
// 1. SepAutoInc() is true
// 2. The table is nonclustered and has auto_increment column.
// In that case, both auto_increment_id and tidb_rowid need to be backup & recover.
// See also https://github.com/pingcap/tidb/issues/46093
//
// It should have been named TiDBRowID, but for historial reasons, we do not use separate meta key for _tidb_rowid and auto_increment_id,
// and field `AutoIncID` is used to serve both _tidb_rowid and auto_increment_id.
// If we introduce a TiDBRowID here, it could make furthur misunderstanding:
// in most cases, AutoIncID is _tidb_rowid and TiDBRowID is null
// but in some cases, AutoIncID is auto_increment_id and TiDBRowID is _tidb_rowid
// So let's just use another name AutoIncIDExtra to avoid misconception.
AutoIncIDExtra int64 `json:"auto_inc_id_extra,omitempty"`

AutoIdCache int64 `json:"auto_id_cache"` //nolint:revive
AutoRandID int64 `json:"auto_rand_id"`
MaxColumnID int64 `json:"max_col_id"`
MaxIndexID int64 `json:"max_idx_id"`
MaxForeignKeyID int64 `json:"max_fk_id"`
MaxConstraintID int64 `json:"max_cst_id"`
// UpdateTS is used to record the timestamp of updating the table's schema information.
// These changing schema operations don't include 'truncate table' and 'rename table'.
UpdateTS uint64 `json:"update_timestamp"`
Expand Down