Skip to content

Commit

Permalink
lightning: release the requirement for NewTargetInfoGetterImpl (#53651)
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 authored May 30, 2024
1 parent 22c41d0 commit afd8de1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
3 changes: 0 additions & 3 deletions lightning/pkg/importer/get_pre_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ func NewTargetInfoGetterImpl(
case config.BackendTiDB:
backendTargetInfoGetter = tidb.NewTargetInfoGetter(targetDB)
case config.BackendLocal:
if pdHTTPCli == nil {
return nil, common.ErrUnknown.GenWithStack("pd HTTP client is required when using local backend")
}
backendTargetInfoGetter = local.NewTargetInfoGetter(tls, targetDB, pdHTTPCli)
default:
return nil, common.ErrUnknownBackend.GenWithStackByArgs(cfg.TikvImporter.Backend)
Expand Down
14 changes: 10 additions & 4 deletions lightning/pkg/importer/get_pre_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,16 +753,22 @@ func TestGetPreInfoEstimateSourceSize(t *testing.T) {
}

func TestGetPreInfoIsTableEmpty(t *testing.T) {
ctx := context.TODO()
ctx := context.Background()
db, mock, err := sqlmock.New()
require.NoError(t, err)
lnConfig := config.NewConfig()
lnConfig.TikvImporter.Backend = config.BackendLocal
_, err = NewTargetInfoGetterImpl(lnConfig, db, nil)
require.ErrorContains(t, err, "pd HTTP client is required when using local backend")
lnConfig.TikvImporter.Backend = config.BackendTiDB
targetGetter, err := NewTargetInfoGetterImpl(lnConfig, db, nil)
require.NoError(t, err)
mock.ExpectQuery("SELECT version()").
WillReturnRows(sqlmock.NewRows([]string{"version()"}).AddRow("8.0.11-TiDB-v8.2.0-alpha-256-qweqweqw"))
err = targetGetter.CheckVersionRequirements(ctx)
require.ErrorContains(t, err, "pd HTTP client is required for component version check in local backend")
require.NoError(t, mock.ExpectationsWereMet())

lnConfig.TikvImporter.Backend = config.BackendTiDB
targetGetter, err = NewTargetInfoGetterImpl(lnConfig, db, nil)
require.NoError(t, err)
require.Equal(t, lnConfig, targetGetter.cfg)

mock.ExpectQuery("SELECT 1 FROM `test_db`.`test_tbl` USE INDEX\\(\\) LIMIT 1").
Expand Down
13 changes: 11 additions & 2 deletions pkg/lightning/backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,14 @@ type targetInfoGetter struct {
pdHTTPCli pdhttp.Client
}

// NewTargetInfoGetter creates an TargetInfoGetter with local backend implementation.
func NewTargetInfoGetter(tls *common.TLS, db *sql.DB, pdHTTPCli pdhttp.Client) backend.TargetInfoGetter {
// NewTargetInfoGetter creates an TargetInfoGetter with local backend
// implementation. `pdHTTPCli` should not be nil when need to check component
// versions in CheckRequirements.
func NewTargetInfoGetter(
tls *common.TLS,
db *sql.DB,
pdHTTPCli pdhttp.Client,
) backend.TargetInfoGetter {
return &targetInfoGetter{
tls: tls,
targetDB: db,
Expand Down Expand Up @@ -293,6 +299,9 @@ func (g *targetInfoGetter) CheckRequirements(ctx context.Context, checkCtx *back
if err := checkTiDBVersion(ctx, versionStr, localMinTiDBVersion, localMaxTiDBVersion); err != nil {
return err
}
if g.pdHTTPCli == nil {
return common.ErrUnknown.GenWithStack("pd HTTP client is required for component version check in local backend")
}
if err := tikv.CheckPDVersion(ctx, g.pdHTTPCli, localMinPDVersion, localMaxPDVersion); err != nil {
return err
}
Expand Down

0 comments on commit afd8de1

Please sign in to comment.