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) #46217

Merged
Merged
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
14 changes: 14 additions & 0 deletions br/pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ func BuildBackupRangeAndSchema(
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 {
tableInfo.AutoIncIDExtra = rowID + 1
} else {
// It is possible that the rowid meta key does not exist (i.e. table have auto_increment_id but no _rowid),
// so err1 != nil might be expected.
if globalAutoID == 0 {
// When both auto_increment_id and _rowid are missing, it must be something wrong.
return nil, nil, nil, errors.Trace(err1)
}
// Print a warning in other scenes, should it be a INFO log?
log.Warn("get rowid error", zap.Error(err1))
}
} else {
globalAutoID, err = idAlloc.NextGlobalAutoID()
}
Expand Down
51 changes: 51 additions & 0 deletions br/tests/br_autoid/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh
#
# Copyright 2023 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
DB="$TEST_NAME"

run_sql "create database if not exists ${DB}"
run_sql "create table $DB.issue46093 (a int primary key nonclustered auto_increment, b int) auto_id_cache = 1;"
run_sql "insert into $DB.issue46093 (b) values (1), (2), (3);"
run_sql "show table $DB.issue46093 next_row_id;"
check_contains "NEXT_GLOBAL_ROW_ID: 30001"
check_contains "NEXT_GLOBAL_ROW_ID: 4"

run_sql "backup table $DB.issue46093 to 'local://$TEST_DIR/$DB'";
run_sql "drop table $DB.issue46093;"
run_sql "restore table $DB.issue46093 from 'local://$TEST_DIR/$DB';"

run_sql "show table $DB.issue46093 next_row_id;"
check_contains "NEXT_GLOBAL_ROW_ID: 30001"
check_contains "NEXT_GLOBAL_ROW_ID: 4001"
run_sql "insert into $DB.issue46093 (b) values (4), (5), (6);"
run_sql "insert into $DB.issue46093 (b) values (7), (8), (9);"
run_sql "select * from $DB.issue46093;"
check_contains "a: 1"
check_contains "a: 2"
check_contains "a: 3"
check_contains "a: 4001"
check_contains "a: 4002"
check_contains "a: 4003"
check_contains "a: 4004"
check_contains "a: 4005"
check_contains "a: 4006"
check_contains "b: 4"
check_contains "b: 5"
check_contains "b: 6"
check_contains "b: 7"
check_contains "b: 8"
check_contains "b: 9"
6 changes: 6 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2504,6 +2504,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 @@ -489,14 +489,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