diff --git a/br/pkg/lightning/backend/local/BUILD.bazel b/br/pkg/lightning/backend/local/BUILD.bazel index 6e471248fee84..6e2b5e9a1c43c 100644 --- a/br/pkg/lightning/backend/local/BUILD.bazel +++ b/br/pkg/lightning/backend/local/BUILD.bazel @@ -88,6 +88,7 @@ go_test( "engine_test.go", "iterator_test.go", "key_adapter_test.go", + "local_check_test.go", "local_test.go", "localhelper_test.go", ], diff --git a/br/pkg/lightning/backend/local/local.go b/br/pkg/lightning/backend/local/local.go index 85ed71fcd606c..317124d0b8d19 100644 --- a/br/pkg/lightning/backend/local/local.go +++ b/br/pkg/lightning/backend/local/local.go @@ -279,6 +279,9 @@ func checkTiDBVersion(_ context.Context, versionStr string, requiredMinVersion, var tiFlashReplicaQuery = "SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.TIFLASH_REPLICA WHERE REPLICA_COUNT > 0;" +// TiFlashReplicaQueryForTest is only used for tests. +var TiFlashReplicaQueryForTest = tiFlashReplicaQuery + type tblName struct { schema string name string @@ -299,6 +302,9 @@ func (t tblNames) String() string { return b.String() } +// CheckTiFlashVersionForTest is only used for tests. +var CheckTiFlashVersionForTest = checkTiFlashVersion + // check TiFlash replicas. // local backend doesn't support TiFlash before tidb v4.0.5 func checkTiFlashVersion(ctx context.Context, g glue.Glue, checkCtx *backend.CheckCtx, tidbVersion semver.Version) error { diff --git a/br/pkg/lightning/backend/local/local_check_test.go b/br/pkg/lightning/backend/local/local_check_test.go new file mode 100644 index 0000000000000..8c16e88e9de4d --- /dev/null +++ b/br/pkg/lightning/backend/local/local_check_test.go @@ -0,0 +1,77 @@ +// Copyright 2022 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package local_test + +import ( + "context" + "testing" + + "github.com/coreos/go-semver/semver" + "github.com/golang/mock/gomock" + "github.com/pingcap/tidb/br/pkg/lightning/backend" + "github.com/pingcap/tidb/br/pkg/lightning/backend/local" + "github.com/pingcap/tidb/br/pkg/lightning/mydump" + "github.com/pingcap/tidb/br/pkg/mock" + "github.com/stretchr/testify/require" +) + +func TestCheckRequirementsTiFlash(t *testing.T) { + controller := gomock.NewController(t) + defer controller.Finish() + glue := mock.NewMockGlue(controller) + exec := mock.NewMockSQLExecutor(controller) + ctx := context.Background() + + dbMetas := []*mydump.MDDatabaseMeta{ + { + Name: "test", + Tables: []*mydump.MDTableMeta{ + { + DB: "test", + Name: "t1", + DataFiles: []mydump.FileInfo{{}}, + }, + { + DB: "test", + Name: "tbl", + DataFiles: []mydump.FileInfo{{}}, + }, + }, + }, + { + Name: "test1", + Tables: []*mydump.MDTableMeta{ + { + DB: "test1", + Name: "t", + DataFiles: []mydump.FileInfo{{}}, + }, + { + DB: "test1", + Name: "tbl", + DataFiles: []mydump.FileInfo{{}}, + }, + }, + }, + } + checkCtx := &backend.CheckCtx{DBMetas: dbMetas} + + glue.EXPECT().GetSQLExecutor().Return(exec) + exec.EXPECT().QueryStringsWithLog(ctx, local.TiFlashReplicaQueryForTest, gomock.Any(), gomock.Any()). + Return([][]string{{"db", "tbl"}, {"test", "t1"}, {"test1", "tbl"}}, nil) + + err := local.CheckTiFlashVersionForTest(ctx, glue, checkCtx, *semver.New("4.0.2")) + require.Regexp(t, "^lightning local backend doesn't support TiFlash in this TiDB version. conflict tables: \\[`test`.`t1`, `test1`.`tbl`\\]", err.Error()) +} diff --git a/br/pkg/lightning/backend/local/local_test.go b/br/pkg/lightning/backend/local/local_test.go index 6b6b78702c68c..1a399552becf9 100644 --- a/br/pkg/lightning/backend/local/local_test.go +++ b/br/pkg/lightning/backend/local/local_test.go @@ -30,9 +30,7 @@ import ( "testing" "github.com/cockroachdb/pebble" - "github.com/coreos/go-semver/semver" "github.com/docker/go-units" - "github.com/golang/mock/gomock" "github.com/google/uuid" "github.com/pingcap/errors" "github.com/pingcap/failpoint" @@ -44,9 +42,7 @@ import ( "github.com/pingcap/tidb/br/pkg/lightning/backend/kv" "github.com/pingcap/tidb/br/pkg/lightning/common" "github.com/pingcap/tidb/br/pkg/lightning/log" - "github.com/pingcap/tidb/br/pkg/lightning/mydump" "github.com/pingcap/tidb/br/pkg/membuf" - "github.com/pingcap/tidb/br/pkg/mock" "github.com/pingcap/tidb/br/pkg/pdutil" "github.com/pingcap/tidb/br/pkg/restore/split" "github.com/pingcap/tidb/br/pkg/utils" @@ -653,55 +649,6 @@ func TestLocalIngestLoop(t *testing.T) { require.Equal(t, atomic.LoadInt32(&maxMetaSeq), f.finishedMetaSeq.Load()) } -func TestCheckRequirementsTiFlash(t *testing.T) { - controller := gomock.NewController(t) - defer controller.Finish() - glue := mock.NewMockGlue(controller) - exec := mock.NewMockSQLExecutor(controller) - ctx := context.Background() - - dbMetas := []*mydump.MDDatabaseMeta{ - { - Name: "test", - Tables: []*mydump.MDTableMeta{ - { - DB: "test", - Name: "t1", - DataFiles: []mydump.FileInfo{{}}, - }, - { - DB: "test", - Name: "tbl", - DataFiles: []mydump.FileInfo{{}}, - }, - }, - }, - { - Name: "test1", - Tables: []*mydump.MDTableMeta{ - { - DB: "test1", - Name: "t", - DataFiles: []mydump.FileInfo{{}}, - }, - { - DB: "test1", - Name: "tbl", - DataFiles: []mydump.FileInfo{{}}, - }, - }, - }, - } - checkCtx := &backend.CheckCtx{DBMetas: dbMetas} - - glue.EXPECT().GetSQLExecutor().Return(exec) - exec.EXPECT().QueryStringsWithLog(ctx, tiFlashReplicaQuery, gomock.Any(), gomock.Any()). - Return([][]string{{"db", "tbl"}, {"test", "t1"}, {"test1", "tbl"}}, nil) - - err := checkTiFlashVersion(ctx, glue, checkCtx, *semver.New("4.0.2")) - require.Regexp(t, "^lightning local backend doesn't support TiFlash in this TiDB version. conflict tables: \\[`test`.`t1`, `test1`.`tbl`\\]", err.Error()) -} - func makeRanges(input []string) []Range { ranges := make([]Range, 0, len(input)/2) for i := 0; i < len(input)-1; i += 2 {