diff --git a/br/pkg/restore/client.go b/br/pkg/restore/client.go index d832bc9056b9e..39716ce052bb7 100644 --- a/br/pkg/restore/client.go +++ b/br/pkg/restore/client.go @@ -3676,43 +3676,43 @@ func CheckNewCollationEnable( g glue.Glue, storage kv.Storage, CheckRequirements bool, -) error { +) (bool, error) { + se, err := g.CreateSession(storage) + if err != nil { + return false, errors.Trace(err) + } + + newCollationEnable, err := se.GetGlobalVariable(utils.GetTidbNewCollationEnabled()) + if err != nil { + 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(fmt.Sprintf("set %s", utils.TidbNewCollationEnabled), zap.Bool("new_collation_enabled", enabled)) + if backupNewCollationEnable == "" { if CheckRequirements { - return errors.Annotatef(berrors.ErrUnknown, + return enabled, errors.Annotatef(berrors.ErrUnknown, "the value '%s' not found in backupmeta. "+ "you can use \"SELECT VARIABLE_VALUE FROM mysql.tidb WHERE VARIABLE_NAME='%s';\" to manually check the config. "+ "if you ensure the value '%s' in backup cluster is as same as restore cluster, use --check-requirements=false to skip this check", utils.TidbNewCollationEnabled, utils.TidbNewCollationEnabled, utils.TidbNewCollationEnabled) } log.Warn(fmt.Sprintf("the config '%s' is not in backupmeta", utils.TidbNewCollationEnabled)) - return nil - } - - se, err := g.CreateSession(storage) - if err != nil { - return errors.Trace(err) - } - - newCollationEnable, err := se.GetGlobalVariable(utils.GetTidbNewCollationEnabled()) - if err != nil { - return errors.Trace(err) + return enabled, nil } if !strings.EqualFold(backupNewCollationEnable, newCollationEnable) { - return errors.Annotatef(berrors.ErrUnknown, + return enabled, errors.Annotatef(berrors.ErrUnknown, "the config '%s' not match, upstream:%v, downstream: %v", utils.TidbNewCollationEnabled, backupNewCollationEnable, newCollationEnable) } - // 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(fmt.Sprintf("set %s", utils.TidbNewCollationEnabled), zap.Bool("new_collation_enabled", enabled)) - return nil + return enabled, nil } type waitTiFlashBackoffer struct { diff --git a/br/pkg/restore/client_test.go b/br/pkg/restore/client_test.go index cd15bde9ba50e..4407a90d07b35 100644 --- a/br/pkg/restore/client_test.go +++ b/br/pkg/restore/client_test.go @@ -1951,19 +1951,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 b76e41001a5d1..befff0d6c5468 100644 --- a/br/pkg/task/restore.go +++ b/br/pkg/task/restore.go @@ -713,7 +713,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) }