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 Apr 13, 2022
1 parent 8a6e5a7 commit 13839f8
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 @@ -1628,7 +1628,10 @@ func (rc *Controller) enforceDiskQuota(ctx context.Context) {

func (rc *Controller) setGlobalVariables(ctx context.Context) error {
// 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 @@ -337,7 +337,7 @@ func ObtainImportantVariables(ctx context.Context, g glue.SQLExecutor) map[strin
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 @@ -347,9 +347,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 @@ -15,6 +15,7 @@ package restore

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

"github.com/DATA-DOG/go-sqlmock"
Expand Down Expand Up @@ -504,8 +505,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 @@ -517,7 +532,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 13839f8

Please sign in to comment.