This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore: add
glue.Glue
interface and other function (#456)
* save my work * add notes * save work * save work * fix unit test * remove tidbMgr in RestoreController * remove some comments * remove some comments * change logger in SQLWithRetry * revert replace log.Logger to *zap.Logger * replace tab to space * try another port to fix CI * remove some comment * *: more glue * report info to host TiDB * fix CI * address comment * address comment * rename a method in interface * save work * try fix CI * could work * change ctx usage * try fix CI * try fix CI * refine function interface * refine some fucntion interface * debug CI * address comment * remove debug log * address comment
- Loading branch information
Showing
14 changed files
with
367 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2020 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package glue | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/pingcap/parser" | ||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/parser/mysql" | ||
"github.com/pingcap/tidb-lightning/lightning/checkpoints" | ||
"github.com/pingcap/tidb-lightning/lightning/common" | ||
"github.com/pingcap/tidb-lightning/lightning/config" | ||
"github.com/pingcap/tidb-lightning/lightning/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Glue interface { | ||
OwnsSQLExecutor() bool | ||
GetSQLExecutor() SQLExecutor | ||
GetDB() (*sql.DB, error) | ||
GetParser() *parser.Parser | ||
GetTables(context.Context, string) ([]*model.TableInfo, error) | ||
OpenCheckpointsDB(context.Context, *config.Config) (checkpoints.CheckpointsDB, error) | ||
// Record is used to report some information (key, value) to host TiDB, including progress, stage currently | ||
Record(string, uint64) | ||
} | ||
|
||
type SQLExecutor interface { | ||
ExecuteWithLog(ctx context.Context, query string, purpose string, logger *zap.Logger) error | ||
ObtainStringWithLog(ctx context.Context, query string, purpose string, logger *zap.Logger) (string, error) | ||
Close() | ||
} | ||
|
||
type ExternalTiDBGlue struct { | ||
db *sql.DB | ||
parser *parser.Parser | ||
} | ||
|
||
func NewExternalTiDBGlue(db *sql.DB, sqlMode mysql.SQLMode) *ExternalTiDBGlue { | ||
p := parser.New() | ||
p.SetSQLMode(sqlMode) | ||
|
||
return &ExternalTiDBGlue{db: db, parser: p} | ||
} | ||
|
||
func (e *ExternalTiDBGlue) GetSQLExecutor() SQLExecutor { | ||
return e | ||
} | ||
|
||
func (e *ExternalTiDBGlue) ExecuteWithLog(ctx context.Context, query string, purpose string, logger *zap.Logger) error { | ||
sql := common.SQLWithRetry{ | ||
DB: e.db, | ||
Logger: log.Logger{Logger: logger}, | ||
} | ||
return sql.Exec(ctx, purpose, query) | ||
} | ||
|
||
func (e *ExternalTiDBGlue) ObtainStringWithLog(ctx context.Context, query string, purpose string, logger *zap.Logger) (string, error) { | ||
var s string | ||
err := common.SQLWithRetry{ | ||
DB: e.db, | ||
Logger: log.Logger{Logger: logger}, | ||
}.QueryRow(ctx, purpose, query, &s) | ||
return s, err | ||
} | ||
|
||
func (e *ExternalTiDBGlue) GetDB() (*sql.DB, error) { | ||
return e.db, nil | ||
} | ||
|
||
func (e *ExternalTiDBGlue) GetParser() *parser.Parser { | ||
return e.parser | ||
} | ||
|
||
func (e *ExternalTiDBGlue) GetTables(context.Context, string) ([]*model.TableInfo, error) { | ||
return nil, nil | ||
} | ||
|
||
func (e *ExternalTiDBGlue) OpenCheckpointsDB(ctx context.Context, cfg *config.Config) (checkpoints.CheckpointsDB, error) { | ||
return checkpoints.OpenCheckpointsDB(ctx, cfg) | ||
} | ||
|
||
func (e *ExternalTiDBGlue) OwnsSQLExecutor() bool { | ||
return true | ||
} | ||
|
||
func (e *ExternalTiDBGlue) Close() { | ||
e.db.Close() | ||
} | ||
|
||
func (e *ExternalTiDBGlue) Record(string, uint64) { | ||
} | ||
|
||
const ( | ||
RecordEstimatedChunk = "EstimatedChunk" | ||
RecordFinishedChunk = "FinishedChunk" | ||
) |
Oops, something went wrong.