Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightning: don't ignore error if check new collation failed #31089

Merged
merged 7 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion br/pkg/lightning/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to set it.

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