From 3627c71359ea8f8e471f289246f48e717d81c4b0 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Sun, 7 Apr 2024 10:33:20 +0800 Subject: [PATCH] br: rebase auto random id if the table is common handle (#52256) (#52317) close pingcap/tidb#52255 --- br/pkg/backup/client.go | 2 +- br/pkg/restore/db.go | 2 +- br/tests/br_autorandom/run.sh | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 br/tests/br_autorandom/run.sh diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index 427c941203504..904e236eec928 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -380,7 +380,7 @@ func BuildBackupRangeAndSchema( // Treat cached table as normal table. tableInfo.TableCacheStatusType = model.TableCacheStatusDisable - if tableInfo.PKIsHandle && tableInfo.ContainsAutoRandomBits() { + if tableInfo.ContainsAutoRandomBits() { // this table has auto_random id, we need backup and rebase in restoration var globalAutoRandID int64 globalAutoRandID, err = randAlloc.NextGlobalAutoID() diff --git a/br/pkg/restore/db.go b/br/pkg/restore/db.go index e6e0a0c300e55..03b38202d93dc 100644 --- a/br/pkg/restore/db.go +++ b/br/pkg/restore/db.go @@ -246,7 +246,7 @@ func (db *DB) CreateTablePostRestore(ctx context.Context, table *metautil.Table, utils.EncloseName(table.DB.Name.O), utils.EncloseName(table.Info.Name.O), table.Info.AutoIncID) - } else if table.Info.PKIsHandle && table.Info.ContainsAutoRandomBits() { + } else if table.Info.ContainsAutoRandomBits() { restoreMetaSQL = fmt.Sprintf( "alter table %s.%s auto_random_base = %d", utils.EncloseName(table.DB.Name.O), diff --git a/br/tests/br_autorandom/run.sh b/br/tests/br_autorandom/run.sh new file mode 100644 index 0000000000000..9dafdac4e6141 --- /dev/null +++ b/br/tests/br_autorandom/run.sh @@ -0,0 +1,65 @@ +#!/bin/bash +# +# Copyright 2024 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" + +# prepare the data +echo "prepare the data" +run_sql "CREATE DATABASE ${DB};" +run_sql "CREATE TABLE ${DB}.common (a BIGINT UNSIGNED AUTO_RANDOM(1), b VARCHAR(255), uid INT, c VARCHAR(255) DEFAULT 'c', PRIMARY KEY (a, b) clustered, UNIQUE INDEX (uid));" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 1, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 2, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 3, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 4, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 5, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 6, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 7, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 8, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 9, 'a');" +run_sql "INSERT INTO ${DB}.common (b, uid, c) values ('a', 10, 'a');" + +run_sql "CREATE TABLE ${DB}.pk (a BIGINT UNSIGNED AUTO_RANDOM(1), uid INT, c VARCHAR(255) DEFAULT 'c', PRIMARY KEY (a), UNIQUE INDEX (uid));" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (1, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (2, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (3, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (4, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (5, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (6, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (7, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (8, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (9, 'a');" +run_sql "INSERT INTO ${DB}.pk (uid, c) values (10, 'a');" + +# backup & restore +run_br --pd $PD_ADDR backup full -s "local://$TEST_DIR/$DB-full" +run_sql "DROP DATABASE $DB;" +run_br --pd $PD_ADDR restore full -s "local://$TEST_DIR/$DB-full" + +# new workload +for i in `seq 1 9`; do + run_sql "INSERT INTO ${DB}.common (b, uid) values ('a', 10) on duplicate key update c = 'b';" + run_sql "INSERT INTO ${DB}.pk (uid) values (10) on duplicate key update c = 'b';" +done + +# check consistency +run_sql "SELECT COUNT(*) AS RESCNT FROM ${DB}.common WHERE uid < 10 AND c = 'b';" +check_contains "RESCNT: 0" +run_sql "SELECT COUNT(*) AS RESCNT FROM ${DB}.pk WHERE uid < 10 AND c = 'b';" +check_contains "RESCNT: 0" + +# clean the data +run_sql "DROP DATABASE $DB;"