Skip to content

Commit 0dad9f2

Browse files
authored
*: fix the duplicate entry error when using BR to restore a NONCLUSTERED AUTO_ID_CACHE=1 table (#46127)
close #46093
1 parent 6188778 commit 0dad9f2

File tree

5 files changed

+95
-9
lines changed

5 files changed

+95
-9
lines changed

br/pkg/backup/client.go

+14
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,20 @@ func BuildBackupSchemas(
633633
default:
634634
if tableInfo.SepAutoInc() {
635635
globalAutoID, err = autoIDAccess.IncrementID(tableInfo.Version).Get()
636+
// For a nonclustered table with auto_increment column, both auto_increment_id and _tidb_rowid are required.
637+
// See also https://github.com/pingcap/tidb/issues/46093
638+
if rowID, err1 := autoIDAccess.RowID().Get(); err1 == nil {
639+
tableInfo.AutoIncIDExtra = rowID + 1
640+
} else {
641+
// It is possible that the rowid meta key does not exist (i.e. table have auto_increment_id but no _rowid),
642+
// so err1 != nil might be expected.
643+
if globalAutoID == 0 {
644+
// When both auto_increment_id and _rowid are missing, it must be something wrong.
645+
return errors.Trace(err1)
646+
}
647+
// Print a warning in other scenes, should it be a INFO log?
648+
log.Warn("get rowid error", zap.Error(err1))
649+
}
636650
} else {
637651
globalAutoID, err = autoIDAccess.RowID().Get()
638652
}

br/tests/br_autoid/run.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/sh
2+
#
3+
# Copyright 2023 PingCAP, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eu
18+
DB="$TEST_NAME"
19+
20+
run_sql "create database if not exists ${DB}"
21+
run_sql "create table $DB.issue46093 (a int primary key nonclustered auto_increment, b int) auto_id_cache = 1;"
22+
run_sql "insert into $DB.issue46093 (b) values (1), (2), (3);"
23+
run_sql "show table $DB.issue46093 next_row_id;"
24+
check_contains "NEXT_GLOBAL_ROW_ID: 30001"
25+
check_contains "NEXT_GLOBAL_ROW_ID: 4"
26+
27+
run_sql "backup table $DB.issue46093 to 'local://$TEST_DIR/$DB'";
28+
run_sql "drop table $DB.issue46093;"
29+
run_sql "restore table $DB.issue46093 from 'local://$TEST_DIR/$DB';"
30+
31+
run_sql "show table $DB.issue46093 next_row_id;"
32+
check_contains "NEXT_GLOBAL_ROW_ID: 30001"
33+
check_contains "NEXT_GLOBAL_ROW_ID: 4001"
34+
run_sql "insert into $DB.issue46093 (b) values (4), (5), (6);"
35+
run_sql "insert into $DB.issue46093 (b) values (7), (8), (9);"
36+
run_sql "select * from $DB.issue46093;"
37+
check_contains "a: 1"
38+
check_contains "a: 2"
39+
check_contains "a: 3"
40+
check_contains "a: 4001"
41+
check_contains "a: 4002"
42+
check_contains "a: 4003"
43+
check_contains "a: 4004"
44+
check_contains "a: 4005"
45+
check_contains "a: 4006"
46+
check_contains "b: 4"
47+
check_contains "b: 5"
48+
check_contains "b: 6"
49+
check_contains "b: 7"
50+
check_contains "b: 8"
51+
check_contains "b: 9"

br/tests/run_group.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mkdir $COV_DIR
2121
declare -A groups
2222
groups=(
2323
["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"
24-
["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"
24+
["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"
2525
["G02"]="br_full_cluster_restore br_full_ddl br_full_index br_gcs br_history"
2626
["G03"]='br_incompatible_tidb_config br_incremental br_incremental_ddl br_incremental_index'
2727
["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'

ddl/ddl_api.go

+6
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,12 @@ func (d *ddl) createTableWithInfoPost(
26282628
return errors.Trace(err)
26292629
}
26302630
}
2631+
// For issue https://github.com/pingcap/tidb/issues/46093
2632+
if tbInfo.AutoIncIDExtra != 0 {
2633+
if err = d.handleAutoIncID(tbInfo, schemaID, tbInfo.AutoIncIDExtra-1, autoid.RowIDAllocType); err != nil {
2634+
return errors.Trace(err)
2635+
}
2636+
}
26312637
if tbInfo.AutoRandID > 1 {
26322638
// Default tableAutoRandID base is 0.
26332639
// If the first ID is expected to greater than 1, we need to do rebase.

parser/model/model.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,29 @@ type TableInfo struct {
458458
// 1 for the clustered index created > 5.0.0 RC.
459459
CommonHandleVersion uint16 `json:"common_handle_version"`
460460

461-
Comment string `json:"comment"`
462-
AutoIncID int64 `json:"auto_inc_id"`
463-
AutoIdCache int64 `json:"auto_id_cache"` //nolint:revive
464-
AutoRandID int64 `json:"auto_rand_id"`
465-
MaxColumnID int64 `json:"max_col_id"`
466-
MaxIndexID int64 `json:"max_idx_id"`
467-
MaxForeignKeyID int64 `json:"max_fk_id"`
468-
MaxConstraintID int64 `json:"max_cst_id"`
461+
Comment string `json:"comment"`
462+
AutoIncID int64 `json:"auto_inc_id"`
463+
464+
// Only used by BR when:
465+
// 1. SepAutoInc() is true
466+
// 2. The table is nonclustered and has auto_increment column.
467+
// In that case, both auto_increment_id and tidb_rowid need to be backup & recover.
468+
// See also https://github.com/pingcap/tidb/issues/46093
469+
//
470+
// It should have been named TiDBRowID, but for historial reasons, we do not use separate meta key for _tidb_rowid and auto_increment_id,
471+
// and field `AutoIncID` is used to serve both _tidb_rowid and auto_increment_id.
472+
// If we introduce a TiDBRowID here, it could make furthur misunderstanding:
473+
// in most cases, AutoIncID is _tidb_rowid and TiDBRowID is null
474+
// but in some cases, AutoIncID is auto_increment_id and TiDBRowID is _tidb_rowid
475+
// So let's just use another name AutoIncIDExtra to avoid misconception.
476+
AutoIncIDExtra int64 `json:"auto_inc_id_extra,omitempty"`
477+
478+
AutoIdCache int64 `json:"auto_id_cache"` //nolint:revive
479+
AutoRandID int64 `json:"auto_rand_id"`
480+
MaxColumnID int64 `json:"max_col_id"`
481+
MaxIndexID int64 `json:"max_idx_id"`
482+
MaxForeignKeyID int64 `json:"max_fk_id"`
483+
MaxConstraintID int64 `json:"max_cst_id"`
469484
// UpdateTS is used to record the timestamp of updating the table's schema information.
470485
// These changing schema operations don't include 'truncate table' and 'rename table'.
471486
UpdateTS uint64 `json:"update_timestamp"`

0 commit comments

Comments
 (0)