Skip to content

Commit 2e38652

Browse files
author
ffffwh
committed
dump: use tx for misc operations #1052-1
Before - use e.db for - getting table structures - counting rows After - use the tx (start transaction with consistent snapshot)
1 parent f87876d commit 2e38652

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

driver/mysql/extractor.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func (e *Extractor) Run() {
330330
} else { // no full copy
331331
// Will not get consistent table meta-info for an incremental only job.
332332
// https://github.com/actiontech/dtle/issues/321#issuecomment-441191534
333-
if err := e.getSchemaTablesAndMeta(); err != nil {
333+
if err := e.getSchemaTablesAndMeta(e.db); err != nil {
334334
e.onError(common.TaskStateDead, err)
335335
return
336336
}
@@ -380,9 +380,9 @@ func (e *Extractor) initiateInspector() (err error) {
380380
return nil
381381
}
382382

383-
func (e *Extractor) inspectTables() (err error) {
383+
func (e *Extractor) inspectTables(db sql.QueryAble) (err error) {
384384
// Creates a MYSQL Dump based on the options supplied through the dumper.
385-
dbsExisted, err := sql.ShowDatabases(e.db)
385+
dbsExisted, err := sql.ShowDatabases(db)
386386
if err != nil {
387387
return err
388388
}
@@ -434,11 +434,11 @@ func (e *Extractor) inspectTables() (err error) {
434434
schemaCtx.TableSchemaRename = doDb.TableSchemaRename
435435
e.replicateDoDb[doDb.TableSchema] = schemaCtx
436436

437-
schemaCtx.CreateSchemaString, err = sql.ShowCreateSchema(e.ctx, e.db, doDb.TableSchema)
437+
schemaCtx.CreateSchemaString, err = sql.ShowCreateSchema(e.ctx, db, doDb.TableSchema)
438438
if err != nil {
439439
return err
440440
}
441-
existedTables, err := sql.ShowTables(e.db, doDb.TableSchema, e.mysqlContext.ExpandSyntaxSupport)
441+
existedTables, err := sql.ShowTables(db, doDb.TableSchema, e.mysqlContext.ExpandSyntaxSupport)
442442
if err != nil {
443443
return err
444444
}
@@ -534,12 +534,12 @@ func (e *Extractor) inspectTables() (err error) {
534534
schemaCtx := common.NewSchemaContext(dbName)
535535
e.replicateDoDb[dbName] = schemaCtx
536536

537-
schemaCtx.CreateSchemaString, err = sql.ShowCreateSchema(e.ctx, e.db, dbName)
537+
schemaCtx.CreateSchemaString, err = sql.ShowCreateSchema(e.ctx, db, dbName)
538538
if err != nil {
539539
return err
540540
}
541541

542-
tbs, err := sql.ShowTables(e.db, dbName, e.mysqlContext.ExpandSyntaxSupport)
542+
tbs, err := sql.ShowTables(db, dbName, e.mysqlContext.ExpandSyntaxSupport)
543543
if err != nil {
544544
return err
545545
}
@@ -753,8 +753,8 @@ func (e *Extractor) initDBConnections() (err error) {
753753
return nil
754754
}
755755

756-
func (e *Extractor) getSchemaTablesAndMeta() error {
757-
if err := e.inspectTables(); err != nil {
756+
func (e *Extractor) getSchemaTablesAndMeta(queryable sql.QueryAble) error {
757+
if err := e.inspectTables(queryable); err != nil {
758758
return err
759759
}
760760

@@ -774,7 +774,7 @@ func (e *Extractor) getSchemaTablesAndMeta() error {
774774
continue
775775
}
776776

777-
stmt, err := base.ShowCreateTable(e.db, db.TableSchema, tb.TableName)
777+
stmt, err := base.ShowCreateTable(queryable, db.TableSchema, tb.TableName)
778778
if err != nil {
779779
e.logger.Error("error at ShowCreateTable.", "err", err)
780780
return err
@@ -856,15 +856,14 @@ func (e *Extractor) setInitialBinlogCoordinates() error {
856856
}
857857

858858
// CountTableRows counts exact number of rows on the original table
859-
func (e *Extractor) CountTableRows(table *common.Table) (int64, error) {
859+
func (e *Extractor) CountTableRows(db sql.QueryAble, table *common.Table) (int64, error) {
860860
//e.logger.Debug("As instructed, I'm issuing a SELECT COUNT(*) on the table. This may take a while")
861861

862862
var query string
863863
// It only requires select privilege on target table to select its information_schema item.
864-
query = fmt.Sprintf(`select table_rows from information_schema.tables where table_schema = '%s' and table_name = '%s'`,
865-
sql.EscapeValue(table.TableSchema), sql.EscapeValue(table.TableName))
864+
query = fmt.Sprintf(`select table_rows from information_schema.tables where table_schema = ? and table_name = ?`)
866865
var rowsEstimate int64
867-
err := e.db.QueryRow(query).Scan(&rowsEstimate)
866+
err := db.QueryRow(query, table.TableSchema, table.TableName).Scan(&rowsEstimate)
868867
if err != nil {
869868
e.logger.Error("error when getting estimated row number (using information_schema)", "err", err,
870869
"schema", table.TableSchema, "table", table.TableName)
@@ -1295,7 +1294,7 @@ func (e *Extractor) mysqlDump() (retErr error) {
12951294
// we are reading the database names from the database and not taking them from the user ...
12961295
e.logger.Info("Step: read list of available tables in each database", "n", step)
12971296

1298-
err = e.getSchemaTablesAndMeta()
1297+
err = e.getSchemaTablesAndMeta(tx)
12991298
if err != nil {
13001299
return err
13011300
}
@@ -1328,7 +1327,7 @@ func (e *Extractor) mysqlDump() (retErr error) {
13281327
// Create the tables.
13291328
for _, tbCtx := range db.TableMap {
13301329
tb := tbCtx.Table
1331-
tb.Counter, err = e.CountTableRows(tb)
1330+
tb.Counter, err = e.CountTableRows(tx, tb)
13321331
if err != nil {
13331332
return errors.Wrapf(err, "CountTableRows %v.%v", tb.TableSchema, tb.TableName)
13341333
}
@@ -1345,7 +1344,7 @@ func (e *Extractor) mysqlDump() (retErr error) {
13451344
return err
13461345
}*/
13471346
} else {
1348-
ctStmt, err := base.ShowCreateTable(e.singletonDB, tb.TableSchema, tb.TableName)
1347+
ctStmt, err := base.ShowCreateTable(tx, tb.TableSchema, tb.TableName)
13491348
if err != nil {
13501349
return err
13511350
}

driver/mysql/sql/sqlutils.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ type QueryAble interface {
240240
Prepare(query string) (*gosql.Stmt, error)
241241
Query(query string, args ...interface{}) (*gosql.Rows, error)
242242
QueryRow(query string, args ...interface{}) *gosql.Row
243+
QueryRowContext(ctx context.Context, query string, args ...interface{}) *gosql.Row
243244
}
244245

245246
func GetServerUUID(db QueryAble) (result string, err error) {
@@ -291,7 +292,7 @@ func QueryResultData(db *gosql.DB, query string, args ...interface{}) (ResultDat
291292
//INSERT INTO {{ .Name }} VALUES {{ .Values }};
292293
//UNLOCK TABLES;
293294

294-
func ShowDatabases(db *gosql.DB) ([]string, error) {
295+
func ShowDatabases(db QueryAble) ([]string, error) {
295296
dbs := make([]string, 0)
296297

297298
// Get table list
@@ -317,7 +318,7 @@ func ShowDatabases(db *gosql.DB) ([]string, error) {
317318
return dbs, rows.Err()
318319
}
319320

320-
func ShowCreateSchema(ctx context.Context, db *gosql.DB, dbName string) (r string, err error) {
321+
func ShowCreateSchema(ctx context.Context, db QueryAble, dbName string) (r string, err error) {
321322
query := fmt.Sprintf("SHOW CREATE SCHEMA IF NOT EXISTS %s", mysqlconfig.EscapeName(dbName))
322323
g.Logger.Debug("ShowCreateSchema", "query", query)
323324
row := db.QueryRowContext(ctx, query)
@@ -330,7 +331,7 @@ func ShowCreateSchema(ctx context.Context, db *gosql.DB, dbName string) (r strin
330331
return r, nil
331332
}
332333

333-
func ShowTables(db *gosql.DB, dbName string, showType bool) (tables []*common.Table, err error) {
334+
func ShowTables(db QueryAble, dbName string, showType bool) (tables []*common.Table, err error) {
334335
// Get table list
335336
var query string
336337
escapedDbName := mysqlconfig.EscapeName(dbName)

0 commit comments

Comments
 (0)