Skip to content

Commit

Permalink
br: rebase auto random id if the table is common handle (#52256) (#52317
Browse files Browse the repository at this point in the history
)

close #52255
  • Loading branch information
ti-chi-bot authored Apr 7, 2024
1 parent 504cc18 commit 3627c71
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion br/pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
65 changes: 65 additions & 0 deletions br/tests/br_autorandom/run.sh
Original file line number Diff line number Diff line change
@@ -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;"

0 comments on commit 3627c71

Please sign in to comment.