Skip to content

Commit 16c342c

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

File tree

5 files changed

+170
-8
lines changed

5 files changed

+170
-8
lines changed

br/pkg/backup/client.go

+14
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,20 @@ func BuildBackupSchemas(
643643
default:
644644
if tableInfo.SepAutoInc() {
645645
globalAutoID, err = autoIDAccess.IncrementID(tableInfo.Version).Get()
646+
// For a nonclustered table with auto_increment column, both auto_increment_id and _tidb_rowid are required.
647+
// See also https://github.com/pingcap/tidb/issues/46093
648+
if rowID, err1 := autoIDAccess.RowID().Get(); err1 == nil {
649+
tableInfo.AutoIncIDExtra = rowID + 1
650+
} else {
651+
// It is possible that the rowid meta key does not exist (i.e. table have auto_increment_id but no _rowid),
652+
// so err1 != nil might be expected.
653+
if globalAutoID == 0 {
654+
// When both auto_increment_id and _rowid are missing, it must be something wrong.
655+
return errors.Trace(err1)
656+
}
657+
// Print a warning in other scenes, should it be a INFO log?
658+
log.Warn("get rowid error", zap.Error(err1))
659+
}
646660
} else {
647661
globalAutoID, err = autoIDAccess.RowID().Get()
648662
}

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

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
3+
# This script split the integration tests into 16 groups to support parallel group tests execution.
4+
# all the integration tests are located in br/tests directory. only the directories
5+
# containing run.sh will be considered as integration tests. the script will print the total # # # number
6+
7+
set -eo pipefail
8+
9+
# Step 1
10+
CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
11+
group=$1
12+
export COV_DIR="/tmp/group_cover"
13+
rm -rf COV_DIR
14+
mkdir $COV_DIR
15+
16+
# Define groups
17+
# Note: If new group is added, the group name must also be added to CI
18+
# * https://github.com/PingCAP-QE/ci/blob/main/pipelines/pingcap/tidb/latest/pull_br_integration_test.groovy
19+
# Each group of tests consumes as much time as possible, thus reducing CI waiting time.
20+
# Putting multiple light tests together and heavy tests in a separate group.
21+
declare -A groups
22+
groups=(
23+
["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_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"
25+
["G02"]="br_full_cluster_restore br_full_ddl br_full_index br_gcs br_history"
26+
["G03"]='br_incompatible_tidb_config br_incremental br_incremental_ddl br_incremental_index'
27+
["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'
28+
["G05"]='br_range br_rawkv br_replica_read br_restore_TDE_enable br_restore_log_task_enable br_s3 br_shuffle_leader br_shuffle_region br_single_table'
29+
["G06"]='br_skip_checksum br_small_batch_size br_split_region_fail br_systables br_table_filter br_txn'
30+
["G07"]='br_clustered_index br_crypter br_table_partition br_tidb_placement_policy br_tiflash br_tikv_outage'
31+
["G08"]='br_tikv_outage2 br_ttl br_views_and_sequences br_z_gc_safepoint lightning_add_index lightning_alter_random lightning_auto_columns'
32+
["G09"]='lightning_auto_random_default lightning_bom_file lightning_character_sets lightning_check_partial_imported lightning_checkpoint lightning_checkpoint_chunks lightning_checkpoint_columns lightning_checkpoint_dirty_tableid'
33+
["G10"]='lightning_checkpoint_engines lightning_checkpoint_engines_order lightning_checkpoint_error_destroy lightning_checkpoint_parquet lightning_checkpoint_timestamp lightning_checksum_mismatch lightning_cmdline_override lightning_column_permutation lightning_common_handle'
34+
["G11"]='lightning_compress lightning_concurrent-restore lightning_config_max_error lightning_config_skip_csv_header lightning_csv lightning_default-columns lightning_disable_scheduler_by_key_range lightning_disk_quota lightning_distributed_import'
35+
["G12"]='lightning_drop_other_tables_halfway lightning_duplicate_detection lightning_duplicate_detection_new lightning_duplicate_resolution lightning_duplicate_resolution_incremental lightning_error_summary lightning_examples lightning_exotic_filenames lightning_extend_routes lightning_fail_fast'
36+
["G13"]='lightning_fail_fast_on_nonretry_err lightning_file_routing lightning_foreign_key lightning_gcs lightning_generated_columns lightning_ignore_columns lightning_import_compress lightning_incremental lightning_issue_282'
37+
["G14"]='lightning_issue_40657 lightning_issue_410 lightning_issue_519 lightning_local_backend lightning_max_incr lightning_max_random lightning_multi_valued_index lightning_new_collation lightning_no_schema'
38+
["G15"]='lightning_parquet lightning_partition_incremental lightning_partitioned-table lightning_record_network lightning_reload_cert lightning_restore lightning_routes lightning_routes_panic lightning_row-format-v2 lightning_s3'
39+
["G16"]='lightning_shard_rowid lightning_source_linkfile lightning_sqlmode lightning_tidb_duplicate_data lightning_tidb_rowid lightning_tiflash lightning_tikv_multi_rocksdb lightning_too_many_columns lightning_tool_135'
40+
["G17"]='lightning_tool_1420 lightning_tool_1472 lightning_tool_241 lightning_ttl lightning_unused_config_keys lightning_various_types lightning_view lightning_write_batch lightning_write_limit'
41+
)
42+
43+
# Get other cases not in groups, to avoid missing any case
44+
others=()
45+
for script in "$CUR"/*/run.sh; do
46+
test_name="$(basename "$(dirname "$script")")"
47+
# shellcheck disable=SC2076
48+
if [[ ! " ${groups[*]} " =~ " ${test_name} " ]]; then
49+
others=("${others[@]} ${test_name}")
50+
fi
51+
done
52+
53+
if [[ "$group" == "others" ]]; then
54+
if [[ -z $others ]]; then
55+
echo "All br&lightning integration test cases have been added to groups"
56+
exit 0
57+
fi
58+
echo "Error: "$others" is not added to any group in br/tests/run_group.sh"
59+
exit 1
60+
elif [[ " ${!groups[*]} " =~ " ${group} " ]]; then
61+
test_names="${groups[${group}]}"
62+
# Run test cases
63+
if [[ -n $test_names ]]; then
64+
echo ""
65+
echo "Run cases: ${test_names}"
66+
for case_name in $test_names; do
67+
echo "Run cases: ${case_name}"
68+
rm -rf /tmp/backup_restore_test
69+
mkdir -p /tmp/backup_restore_test
70+
TEST_NAME=${case_name} ${CUR}/run.sh
71+
done
72+
fi
73+
else
74+
echo "Error: invalid group name: ${group}"
75+
exit 1
76+
fi

ddl/ddl_api.go

+6
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,12 @@ func (d *ddl) createTableWithInfoPost(
25142514
return errors.Trace(err)
25152515
}
25162516
}
2517+
// For issue https://github.com/pingcap/tidb/issues/46093
2518+
if tbInfo.AutoIncIDExtra != 0 {
2519+
if err = d.handleAutoIncID(tbInfo, schemaID, tbInfo.AutoIncIDExtra-1, autoid.RowIDAllocType); err != nil {
2520+
return errors.Trace(err)
2521+
}
2522+
}
25172523
if tbInfo.AutoRandID > 1 {
25182524
// Default tableAutoRandID base is 0.
25192525
// 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)