From a5db69027d3f478b6d6b7abd13365d53956098f9 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 27 Feb 2024 19:41:21 +0800 Subject: [PATCH] restore: correct new collation when "--check-requirements=false" (#49579) (#49959) close pingcap/tidb#49466 --- br/pkg/restore/client.go | 48 +++++++++++++++++------------------ br/pkg/restore/client_test.go | 10 ++++++-- br/pkg/task/restore.go | 2 +- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/br/pkg/restore/client.go b/br/pkg/restore/client.go index 415c588dae526..cb7cd946a8eeb 100644 --- a/br/pkg/restore/client.go +++ b/br/pkg/restore/client.go @@ -3736,43 +3736,43 @@ func CheckNewCollationEnable( g glue.Glue, storage kv.Storage, CheckRequirements bool, -) error { - if backupNewCollationEnable == "" { - if CheckRequirements { - return errors.Annotatef(berrors.ErrUnknown, - "the config 'new_collations_enabled_on_first_bootstrap' not found in backupmeta. "+ - "you can use \"show config WHERE name='new_collations_enabled_on_first_bootstrap';\" to manually check the config. "+ - "if you ensure the config 'new_collations_enabled_on_first_bootstrap' in backup cluster is as same as restore cluster, "+ - "use --check-requirements=false to skip this check") - } - log.Warn("the config 'new_collations_enabled_on_first_bootstrap' is not in backupmeta") - return nil - } - +) (bool, error) { se, err := g.CreateSession(storage) if err != nil { - return errors.Trace(err) + return false, errors.Trace(err) } newCollationEnable, err := se.GetGlobalVariable(utils.GetTidbNewCollationEnabled()) if err != nil { - return errors.Trace(err) - } - - if !strings.EqualFold(backupNewCollationEnable, newCollationEnable) { - return errors.Annotatef(berrors.ErrUnknown, - "the config 'new_collations_enabled_on_first_bootstrap' not match, upstream:%v, downstream: %v", - backupNewCollationEnable, newCollationEnable) + return false, errors.Trace(err) } - // collate.newCollationEnabled is set to 1 when the collate package is initialized, // so we need to modify this value according to the config of the cluster // before using the collate package. enabled := newCollationEnable == "True" // modify collate.newCollationEnabled according to the config of the cluster collate.SetNewCollationEnabledForTest(enabled) - log.Info("set new_collation_enabled", zap.Bool("new_collation_enabled", enabled)) - return nil + log.Info("set new_collations_enabled_on_first_bootstrap", zap.Bool("new_collation_enabled", enabled)) + + if backupNewCollationEnable == "" { + if CheckRequirements { + return enabled, errors.Annotatef(berrors.ErrUnknown, + "the value 'new_collations_enabled_on_first_bootstrap' not found in backupmeta. "+ + "you can use \"SELECT VARIABLE_VALUE FROM mysql.tidb WHERE VARIABLE_NAME='new_collations_enabled_on_first_bootstrap';\" to manually check the config. "+ + "if you ensure the value 'new_collations_enabled_on_first_bootstrap' in backup cluster is as same as restore cluster, use --check-requirements=false to skip this check", + ) + } + log.Warn("the config 'new_collations_enabled_on_first_bootstrap' is not in backupmeta") + return enabled, nil + } + + if !strings.EqualFold(backupNewCollationEnable, newCollationEnable) { + return enabled, errors.Annotatef(berrors.ErrUnknown, + "the config 'new_collations_enabled_on_first_bootstrap' not match, upstream:%v, downstream: %v", + backupNewCollationEnable, newCollationEnable) + } + + return enabled, nil } type waitTiFlashBackoffer struct { diff --git a/br/pkg/restore/client_test.go b/br/pkg/restore/client_test.go index 40b2216017b06..b48b23b470d9c 100644 --- a/br/pkg/restore/client_test.go +++ b/br/pkg/restore/client_test.go @@ -1872,19 +1872,25 @@ func TestCheckNewCollationEnable(t *testing.T) { CheckRequirements: true, isErr: true, }, + { + backupMeta: &backuppb.BackupMeta{NewCollationsEnabled: ""}, + newCollationEnableInCluster: "False", + CheckRequirements: false, + isErr: false, + }, } for i, ca := range caseList { g := &gluetidb.MockGlue{ GlobalVars: map[string]string{"new_collation_enabled": ca.newCollationEnableInCluster}, } - err := restore.CheckNewCollationEnable(ca.backupMeta.GetNewCollationsEnabled(), g, nil, ca.CheckRequirements) - + enabled, err := restore.CheckNewCollationEnable(ca.backupMeta.GetNewCollationsEnabled(), g, nil, ca.CheckRequirements) t.Logf("[%d] Got Error: %v\n", i, err) if ca.isErr { require.Error(t, err) } else { require.NoError(t, err) } + require.Equal(t, ca.newCollationEnableInCluster == "True", enabled) } } diff --git a/br/pkg/task/restore.go b/br/pkg/task/restore.go index ae33db6491cf6..f6824b5d23b31 100644 --- a/br/pkg/task/restore.go +++ b/br/pkg/task/restore.go @@ -707,7 +707,7 @@ func runRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf return errors.Trace(versionErr) } } - if err = restore.CheckNewCollationEnable(backupMeta.GetNewCollationsEnabled(), g, mgr.GetStorage(), cfg.CheckRequirements); err != nil { + if _, err = restore.CheckNewCollationEnable(backupMeta.GetNewCollationsEnabled(), g, mgr.GetStorage(), cfg.CheckRequirements); err != nil { return errors.Trace(err) }