Skip to content

Commit

Permalink
lightning: don't ignore error if check new collation failed (#31089) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Feb 17, 2022
1 parent 88470ec commit 921ca98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
5 changes: 4 additions & 1 deletion br/pkg/lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,10 @@ func (rc *Controller) setGlobalVariables(ctx context.Context) error {
return nil
}
// set new collation flag base on tidb config
enabled := ObtainNewCollationEnabled(ctx, rc.tidbGlue.GetSQLExecutor())
enabled, err := ObtainNewCollationEnabled(ctx, rc.tidbGlue.GetSQLExecutor())
if err != nil {
return err
}
// we should enable/disable new collation here since in server mode, tidb config
// may be different in different tasks
collate.SetNewCollationEnabledForTest(enabled)
Expand Down
8 changes: 6 additions & 2 deletions br/pkg/lightning/restore/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func ObtainImportantVariables(ctx context.Context, g glue.SQLExecutor, needTiDBV
return result
}

func ObtainNewCollationEnabled(ctx context.Context, g glue.SQLExecutor) bool {
func ObtainNewCollationEnabled(ctx context.Context, g glue.SQLExecutor) (bool, error) {
newCollationEnabled := false
newCollationVal, err := g.ObtainStringWithLog(
ctx,
Expand All @@ -379,9 +379,13 @@ func ObtainNewCollationEnabled(ctx context.Context, g glue.SQLExecutor) bool {
)
if err == nil && newCollationVal == "True" {
newCollationEnabled = true
} else if errors.ErrorEqual(err, sql.ErrNoRows) {
// ignore if target variable is not found, this may happen if tidb < v4.0
newCollationEnabled = false
err = nil
}

return newCollationEnabled
return newCollationEnabled, errors.Trace(err)
}

// AlterAutoIncrement rebase the table auto increment id
Expand Down
22 changes: 19 additions & 3 deletions br/pkg/lightning/restore/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package restore

import (
"context"
"database/sql"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -505,8 +506,22 @@ func (s *tidbSuite) TestObtainNewCollationEnabled(c *C) {
ctx := context.Background()

s.mockDB.
ExpectQuery("\\QSELECT variable_value FROM mysql.tidb WHERE variable_name = 'new_collation_enabled'\\E")
version := ObtainNewCollationEnabled(ctx, s.tiGlue.GetSQLExecutor())
ExpectQuery("\\QSELECT variable_value FROM mysql.tidb WHERE variable_name = 'new_collation_enabled'\\E").
WillReturnError(errors.New("mock permission deny"))
s.mockDB.
ExpectQuery("\\QSELECT variable_value FROM mysql.tidb WHERE variable_name = 'new_collation_enabled'\\E").
WillReturnError(errors.New("mock permission deny"))
s.mockDB.
ExpectQuery("\\QSELECT variable_value FROM mysql.tidb WHERE variable_name = 'new_collation_enabled'\\E").
WillReturnError(errors.New("mock permission deny"))
_, err := ObtainNewCollationEnabled(ctx, s.tiGlue.GetSQLExecutor())
c.Assert(err, ErrorMatches, "obtain new collation enabled failed: mock permission deny")

s.mockDB.
ExpectQuery("\\QSELECT variable_value FROM mysql.tidb WHERE variable_name = 'new_collation_enabled'\\E").
WillReturnRows(sqlmock.NewRows([]string{"variable_value"}).RowError(0, sql.ErrNoRows))
version, err := ObtainNewCollationEnabled(ctx, s.tiGlue.GetSQLExecutor())
c.Assert(err, IsNil)
c.Assert(version, Equals, false)

kvMap := map[string]bool{
Expand All @@ -518,7 +533,8 @@ func (s *tidbSuite) TestObtainNewCollationEnabled(c *C) {
ExpectQuery("\\QSELECT variable_value FROM mysql.tidb WHERE variable_name = 'new_collation_enabled'\\E").
WillReturnRows(sqlmock.NewRows([]string{"variable_value"}).AddRow(k))

version := ObtainNewCollationEnabled(ctx, s.tiGlue.GetSQLExecutor())
version, err = ObtainNewCollationEnabled(ctx, s.tiGlue.GetSQLExecutor())
c.Assert(err, IsNil)
c.Assert(version, Equals, v)
}
s.mockDB.
Expand Down

0 comments on commit 921ca98

Please sign in to comment.