Skip to content

Commit

Permalink
br: Fix new collaction enable check (#33500) (#33532)
Browse files Browse the repository at this point in the history
close #33422
  • Loading branch information
ti-srebot authored Apr 28, 2022
1 parent 3f7c341 commit 95fc56b
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 28 deletions.
1 change: 1 addition & 0 deletions br/pkg/glue/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Session interface {
CreateDatabase(ctx context.Context, schema *model.DBInfo) error
CreateTable(ctx context.Context, dbName model.CIStr, table *model.TableInfo) error
Close()
GetGlobalVariable(name string) (string, error)
}

// Progress is an interface recording the current execution progress.
Expand Down
5 changes: 5 additions & 0 deletions br/pkg/gluetidb/glue.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ func (gs *tidbSession) Close() {
gs.se.Close()
}

// GetGlobalVariables implements glue.Session.
func (gs *tidbSession) GetGlobalVariable(name string) (string, error) {
return gs.se.GetSessionVars().GlobalVarsAccessor.GetTiDBTableValue(name)
}

// showCreateTable shows the result of SHOW CREATE TABLE from a TableInfo.
func (gs *tidbSession) showCreateTable(tbl *model.TableInfo) (string, error) {
table := tbl.Clone()
Expand Down
11 changes: 11 additions & 0 deletions br/pkg/task/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig
statsHandle = mgr.GetDomain().StatsHandle()
}

se, err := g.CreateSession(mgr.GetStorage())
if err != nil {
return errors.Trace(err)
}
newCollationEnable, err := se.GetGlobalVariable(tidbNewCollationEnabled)
if err != nil {
return errors.Trace(err)
}
log.Info("get newCollationEnable for check during restore", zap.String("newCollationEnable", newCollationEnable))

client, err := backup.NewBackupClient(ctx, mgr)
if err != nil {
return errors.Trace(err)
Expand Down Expand Up @@ -339,6 +349,7 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig
m.ClusterId = req.ClusterId
m.ClusterVersion = clusterVersion
m.BrVersion = brVersion
m.NewCollationsEnabled = newCollationEnable
})

// nothing to backup
Expand Down
2 changes: 2 additions & 0 deletions br/pkg/task/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const (
crypterAES128KeyLen = 16
crypterAES192KeyLen = 24
crypterAES256KeyLen = 32

tidbNewCollationEnabled = "new_collation_enabled"
)

// TLSConfig is the common configuration for TLS connection.
Expand Down
35 changes: 35 additions & 0 deletions br/pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package task

import (
"context"
"strings"
"time"

"github.com/opentracing/opentracing-go"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/br/pkg/version"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/kv"
"github.com/spf13/pflag"
"go.uber.org/multierr"
"go.uber.org/zap"
Expand Down Expand Up @@ -212,6 +214,35 @@ func CheckRestoreDBAndTable(client *restore.Client, cfg *RestoreConfig) error {
return nil
}

func CheckNewCollationEnable(
backupNewCollationEnable string,
g glue.Glue,
storage kv.Storage,
) error {
if backupNewCollationEnable == "" {
log.Warn("new_collations_enabled_on_first_bootstrap not found in backupmeta. " +
"we assume that this config is as same as backup cluster")
return nil
}

se, err := g.CreateSession(storage)
if err != nil {
return errors.Trace(err)
}

newCollationEnable, err := se.GetGlobalVariable(tidbNewCollationEnabled)
if err != nil {
return errors.Trace(err)
}

if !strings.EqualFold(backupNewCollationEnable, newCollationEnable) {
return errors.Annotatef(berrors.ErrUnknown,
"newCollationEnable not match, upstream:%v, downstream: %v",
backupNewCollationEnable, newCollationEnable)
}
return nil
}

// RunRestore starts a restore task inside the current goroutine.
func RunRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConfig) error {
cfg.adjustRestoreConfig()
Expand Down Expand Up @@ -278,6 +309,10 @@ func RunRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf
return errors.Trace(versionErr)
}
}
if err = CheckNewCollationEnable(backupMeta.GetNewCollationsEnabled(), g, mgr.GetStorage()); err != nil {
return errors.Trace(err)
}

reader := metautil.NewMetaReader(backupMeta, s, &cfg.CipherInfo)
if err = client.InitBackupMeta(c, backupMeta, u, s, reader); err != nil {
return errors.Trace(err)
Expand Down
1 change: 1 addition & 0 deletions br/pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func CheckClusterVersion(ctx context.Context, client pd.Client, checker VerCheck
if err := checkTiFlashVersion(s); err != nil {
return errors.Trace(err)
}
continue
}

tikvVersionString := removeVAndHash(s.Version)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# config of tidb

# Schema lease duration
# There are lot of ddl in the tests, setting this
# to 360s to test whether BR is gracefully shutdown.
lease = "360s"

new_collations_enabled_on_first_bootstrap = false

[security]
ssl-ca = "/tmp/backup_restore_test/certs/ca.pem"
ssl-cert = "/tmp/backup_restore_test/certs/tidb.pem"
ssl-key = "/tmp/backup_restore_test/certs/tidb.key"
cluster-ssl-ca = "/tmp/backup_restore_test/certs/ca.pem"
cluster-ssl-cert = "/tmp/backup_restore_test/certs/tidb.pem"
cluster-ssl-key = "/tmp/backup_restore_test/certs/tidb.key"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# config of tidb

# Schema lease duration
# There are lot of ddl in the tests, setting this
# to 360s to test whether BR is gracefully shutdown.
lease = "360s"

new_collations_enabled_on_first_bootstrap = true

[security]
ssl-ca = "/tmp/backup_restore_test/certs/ca.pem"
ssl-cert = "/tmp/backup_restore_test/certs/tidb.pem"
ssl-key = "/tmp/backup_restore_test/certs/tidb.key"
cluster-ssl-ca = "/tmp/backup_restore_test/certs/ca.pem"
cluster-ssl-cert = "/tmp/backup_restore_test/certs/tidb.pem"
cluster-ssl-key = "/tmp/backup_restore_test/certs/tidb.key"
102 changes: 102 additions & 0 deletions br/tests/br_check_new_collocation_enable/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh
#
# Copyright 2019 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"

cur=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $cur/../_utils/run_services

PROGRESS_FILE="$TEST_DIR/progress_unit_file"
rm -rf $PROGRESS_FILE

run_sql "CREATE DATABASE $DB;"

run_sql "CREATE TABLE $DB.usertable1 ( \
YCSB_KEY varchar(64) NOT NULL, \
FIELD0 varchar(1) DEFAULT NULL, \
PRIMARY KEY (YCSB_KEY) \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"

run_sql "INSERT INTO $DB.usertable1 VALUES (\"a\", \"b\");"
run_sql "INSERT INTO $DB.usertable1 VALUES (\"aa\", \"b\");"

run_sql "CREATE TABLE $DB.usertable2 ( \
YCSB_KEY varchar(64) NOT NULL, \
FIELD0 varchar(1) DEFAULT NULL, \
PRIMARY KEY (YCSB_KEY) \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"

run_sql "INSERT INTO $DB.usertable2 VALUES (\"c\", \"d\");"

# backup db
echo "backup start ... with brv4.0.8 without NewCollactionEnable"
bin/brv4.0.8 backup db --db "$DB" -s "local://$TEST_DIR/$DB" \
--ca "$TEST_DIR/certs/ca.pem" \
--cert "$TEST_DIR/certs/br.pem" \
--key "$TEST_DIR/certs/br.key" \
--pd $PD_ADDR \
--check-requirements=false

# restore db from v4.0.8 version without `newCollationEnable`
echo "restore start ... without NewCollactionEnable in backupmeta"
restore_fail=0
error_str="new_collations_enabled_on_first_bootstrap not found in backupmeta"
test_log="new_collotion_enable_test.log"
unset BR_LOG_TO_TERM
run_br restore db --db $DB -s "local://$TEST_DIR/$DB" --pd $PD_ADDR --log-file $test_log || restore_fail=1
if [ $restore_fail -eq 1 ]; then
echo "TEST: [$TEST_NAME] test restore failed!"
exit 1
fi

if ! grep -i "$error_str" $test_log; then
echo "${error_str} not found in log"
echo "TEST: [$TEST_NAME] test restore failed!"
exit 1
fi

rm -rf "$test_log"

# backup with NewCollationEable = false
echo "Restart cluster with new_collation_enable=false"
start_services --tidb-cfg $cur/config/new_collation_enable_false.toml

echo "backup start ... witch NewCollactionEnable=false in TiDB"
run_br --pd $PD_ADDR backup db --db "$DB" -s "local://$cur/${DB}_2"

echo "Restart cluster with new_collation_enable=true"
start_services --tidb-cfg $cur/config/new_collation_enable_true.toml

echo "restore start ... with NewCollactionEnable=True in TiDB"
restore_fail=0
test_log2="new_collotion_enable_test2.log"
error_str="newCollationEnable not match"
unset BR_LOG_TO_TERM
run_br restore db --db $DB -s "local://$cur/${DB}_2" --pd $PD_ADDR --log-file $test_log2 || restore_fail=1
if [ $restore_fail -ne 1 ]; then
echo "TEST: [$TEST_NAME] test restore failed!"
exit 1
fi

if ! grep -i "$error_str" $test_log2; then
echo "${error_str} not found in log"
echo "TEST: [$TEST_NAME] test restore failed!"
exit 1
fi

rm -rf "$test_log2"
rm -rf "$cur/${DB}_2"
6 changes: 6 additions & 0 deletions br/tests/br_s3/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ for p in $(seq 2); do
exit 1
fi

target_log="get newCollationEnable for check during restore"
if ! grep -i "$target_log" $BACKUP_LOG; then
echo "${target_log} not found in log"
exit 1
fi

for i in $(seq $DB_COUNT); do
run_sql "DROP DATABASE $DB${i};"
done
Expand Down
5 changes: 5 additions & 0 deletions executor/brie.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ func (gs *tidbGlueSession) CreateTable(ctx context.Context, dbName model.CIStr,
func (gs *tidbGlueSession) Close() {
}

// GetGlobalVariables implements glue.Session.
func (gs *tidbGlueSession) GetGlobalVariable(name string) (string, error) {
return gs.se.GetSessionVars().GlobalVarsAccessor.GetTiDBTableValue(name)
}

// Open implements glue.Glue
func (gs *tidbGlueSession) Open(string, pd.SecurityOption) (kv.Storage, error) {
return gs.se.GetStore(), nil
Expand Down
25 changes: 16 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ require (
github.com/fsouza/fake-gcs-server v1.19.0
github.com/go-sql-driver/mysql v1.6.0
github.com/gogo/protobuf v1.3.2
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.3
github.com/google/btree v1.0.0
github.com/google/btree v1.0.1
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1
github.com/google/uuid v1.1.2
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
github.com/jedib0t/go-pretty/v6 v6.2.2
github.com/joho/sqltocsv v0.0.0-20210428211105-a6d6801d59df
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/ngaut/pools v0.0.0-20180318154953-b7bc8c42aac7
github.com/ngaut/sync2 v0.0.0-20141008032647-7a24ed77b2ef
github.com/opentracing/basictracer-go v1.0.0
Expand All @@ -50,48 +53,52 @@ require (
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20210316064728-7acb0f0a3dfd
github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059
github.com/pingcap/kvproto v0.0.0-20211207042851-78a55fb8e69c
github.com/pingcap/kvproto v0.0.0-20220428033740-e4924274acd8
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354
github.com/pingcap/sysutil v0.0.0-20211208032423-041a72e5860d
github.com/pingcap/tidb-tools v5.2.2-0.20211019062242-37a8bef2fa17+incompatible
github.com/pingcap/tidb/parser v0.0.0-20211011031125-9b13dc409c5e
github.com/pingcap/tipb v0.0.0-20220107024056-3b91949a18a7
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/client_golang v1.11.1
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.9.1
github.com/prometheus/common v0.26.0
github.com/shirou/gopsutil v3.21.3+incompatible
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/soheilhy/cmux v0.1.4
github.com/spf13/cobra v1.0.0
github.com/soheilhy/cmux v0.1.5
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2
github.com/tikv/client-go/v2 v2.0.0-rc.0.20220425054530-a62124d9fa75
github.com/tikv/pd v1.1.0-beta.0.20211118054146-02848d2660ee
github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 // indirect
github.com/twmb/murmur3 v1.1.3
github.com/uber/jaeger-client-go v2.22.1+incompatible
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/wangjohn/quickselect v0.0.0-20161129230411-ed8402a42d5f
github.com/xitongsys/parquet-go v1.5.5-0.20201110004701-b09c49d6d457
github.com/xitongsys/parquet-go-source v0.0.0-20200817004010-026bad9b25d0
go.etcd.io/bbolt v1.3.6 // indirect
go.etcd.io/etcd v0.5.0-alpha.5.0.20210512015243-d19fbe541bf9
go.uber.org/atomic v1.9.0
go.uber.org/automaxprocs v1.4.0
go.uber.org/goleak v1.1.12
go.uber.org/multierr v1.7.0
go.uber.org/zap v1.19.1
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/oauth2 v0.0.0-20210805134026-6f1e6394065a
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
golang.org/x/tools v0.1.8
google.golang.org/api v0.54.0
google.golang.org/grpc v1.40.0
gopkg.in/yaml.v2 v2.4.0
modernc.org/mathutil v1.4.1
sigs.k8s.io/yaml v1.2.0 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
)
Expand Down
Loading

0 comments on commit 95fc56b

Please sign in to comment.