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 7 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 @@ -633,6 +633,20 @@ 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 {
// 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 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 = autoIDAccess.RowID().Get()
}
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
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
#
# 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:///tmp/br'";
run_sql "drop table $DB.issue46093;"
run_sql "restore table $DB.issue46093 from 'local:///tmp/br';"
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved

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"
2 changes: 1 addition & 1 deletion br/tests/run_group.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mkdir $COV_DIR
declare -A groups
groups=(
["G00"]="br_300_small_tables br_backup_empty br_backup_version br_cache_table br_case_sensitive br_charset_gbk br_check_new_collocation_enable"
["G01"]="br_crypter2 br_db br_db_online br_db_online_newkv br_db_skip br_debug_meta br_ebs br_foreign_key br_full"
["G01"]="br_autoid br_crypter2 br_db br_db_online br_db_online_newkv br_db_skip br_debug_meta br_ebs br_foreign_key br_full"
["G02"]="br_full_cluster_restore br_full_ddl br_full_index br_gcs br_history"
["G03"]='br_incompatible_tidb_config br_incremental br_incremental_ddl br_incremental_index'
["G04"]='br_incremental_only_ddl br_incremental_same_table br_insert_after_restore br_key_locked br_log_test br_move_backup br_mv_index br_other br_partition_add_index'
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