Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: GetBinlogReplayList (#2337)

Yilong Li0a88d993888b
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.go
index bd4c6b7366..07f1a472d4 100644
--- a/plugin/db/mysql/restore.go
+++ b/plugin/db/mysql/restore.go
@@ -94,12 +94,12 @@ func newBinlogCoordinate(binlogFileName string, pos int64) (binlogCoordinate, er
// ReplayBinlog replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`, read binlog from `driver.binlogDir``.
func (driver *Driver) replayBinlog(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64) error {
- return driver.replayBinlogReadFromDir(ctx, originalDatabase, targetDatabase, startBinlogInfo, targetTs, driver.binlogDir)
+ return driver.replayBinlogReadFromDir(ctx, originalDatabase, targetDatabase, startBinlogInfo, targetTs)
}
// replayBinlogReadFromDir replays the binlog for `originDatabase` from `startBinlogInfo.Position` to `targetTs`, read binlog from `binlogDir`.
-func (driver *Driver) replayBinlogReadFromDir(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64, binlogDir string) error {
- replayBinlogPaths, err := GetBinlogReplayList(startBinlogInfo, binlogDir)
+func (driver *Driver) replayBinlogReadFromDir(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64) error {
+ replayBinlogPaths, err := driver.GetBinlogReplayList(startBinlogInfo)
if err != nil {
return err
}
@@ -201,8 +201,8 @@ func (driver *Driver) GetReplayedBinlogBytes() int64 {
}
// ReplayBinlogToDatabase replays the binlog of originDatabaseName to the targetDatabaseName.
-func (driver *Driver) ReplayBinlogToDatabase(ctx context.Context, originDatabaseName, targetDatabaseName string, startBinlogInfo api.BinlogInfo, targetTs int64, binlogDir string) error {
- return driver.replayBinlogReadFromDir(ctx, originDatabaseName, targetDatabaseName, startBinlogInfo, targetTs, binlogDir)
+func (driver *Driver) ReplayBinlogToDatabase(ctx context.Context, originDatabaseName, targetDatabaseName string, startBinlogInfo api.BinlogInfo, targetTs int64) error {
+ return driver.replayBinlogReadFromDir(ctx, originDatabaseName, targetDatabaseName, startBinlogInfo, targetTs)
}
// ReplayBinlogToPITRDatabase replays binlog to the PITR database.
@@ -235,15 +235,15 @@ func (driver *Driver) RestoreBackupToPITRDatabase(ctx context.Context, backup io
}
// GetBinlogReplayList returns the path list of the binlog that need be replayed.
-func GetBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]string, error) {
+func (driver *Driver) GetBinlogReplayList(startBinlogInfo api.BinlogInfo) ([]string, error) {
startBinlogSeq, err := GetBinlogNameSeq(startBinlogInfo.FileName)
if err != nil {
return nil, errors.Wrapf(err, "cannot parse the start binlog file name %q", startBinlogInfo.FileName)
}
- binlogFiles, err := ioutil.ReadDir(binlogDir)
+ binlogFiles, err := ioutil.ReadDir(driver.binlogDir)
if err != nil {
- return nil, errors.Wrapf(err, "cannot read binlog directory %s", binlogDir)
+ return nil, errors.Wrapf(err, "cannot read binlog directory %s", driver.binlogDir)
}
var binlogFilesToReplay []BinlogFile
@@ -277,7 +277,7 @@ func GetBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]st
var binlogReplayList []string
for _, binlogFile := range binlogFilesToReplaySorted {
- binlogReplayList = append(binlogReplayList, filepath.Join(binlogDir, binlogFile.Name))
+ binlogReplayList = append(binlogReplayList, filepath.Join(driver.binlogDir, binlogFile.Name))
}
return binlogReplayList, nil
diff --git a/plugin/db/mysql/restore_test.go b/plugin/db/mysql/restore_test.go
index 07108e6b2f..7ec2cfe9ab 100644
--- a/plugin/db/mysql/restore_test.go
+++ b/plugin/db/mysql/restore_test.go
@@ -262,7 +262,8 @@ func TestGetReplayBinlogPathList(t *testing.T) {
a.NoError(err)
}
- result, err := GetBinlogReplayList(test.startBinlogInfo, tmpDir)
+ driver := Driver{binlogDir: tmpDir}
+ result, err := driver.GetBinlogReplayList(test.startBinlogInfo)
if test.err {
a.Error(err)
} else {
diff --git a/server/task_executor_pitr_restore.go b/server/task_executor_pitr_restore.go
index 4ad49c91d9..13d9e18b59 100644
--- a/server/task_executor_pitr_restore.go
+++ b/server/task_executor_pitr_restore.go
@@ -245,9 +245,8 @@ func (exec *PITRRestoreTaskExecutor) doPITRRestore(ctx context.Context, server *
// Since it's ephemeral and will be renamed to the original database soon, we will reuse the original
// database's migration history, and append a new BRANCH migration.
startBinlogInfo := backup.Payload.BinlogInfo
- binlogDir := getBinlogAbsDir(server.profile.DataDir, task.Instance.ID)
- if err := exec.updateProgress(ctx, mysqlTargetDriver, backupFile, startBinlogInfo, binlogDir); err != nil {
+ if err := exec.updateProgress(ctx, mysqlTargetDriver, backupFile, startBinlogInfo); err != nil {
return nil, errors.Wrap(err, "failed to setup progress update process")
}
@@ -259,7 +258,7 @@ func (exec *PITRRestoreTaskExecutor) doPITRRestore(ctx context.Context, server *
zap.Error(err))
return nil, errors.Wrap(err, "failed to restore full backup in the new database")
}
- if err := mysqlTargetDriver.ReplayBinlogToDatabase(ctx, task.Database.Name, *payload.DatabaseName, startBinlogInfo, targetTs, mysqlSourceDriver.GetBinlogDir()); err != nil {
+ if err := mysqlTargetDriver.ReplayBinlogToDatabase(ctx, task.Database.Name, *payload.DatabaseName, startBinlogInfo, targetTs); err != nil {
log.Error("failed to perform a PITR restore in the new database",
zap.Int("issueID", issue.ID),
zap.String("databaseName", *payload.DatabaseName),
@@ -344,15 +343,15 @@ func (*PITRRestoreTaskExecutor) doRestoreInPlacePostgres(ctx context.Context, se
}, nil
}
-func (exec *PITRRestoreTaskExecutor) updateProgress(ctx context.Context, driver *mysql.Driver, backupFile *os.File, startBinlogInfo api.BinlogInfo, binlogDir string) error {
+func (exec *PITRRestoreTaskExecutor) updateProgress(ctx context.Context, driver *mysql.Driver, backupFile *os.File, startBinlogInfo api.BinlogInfo) error {
backupFileInfo, err := backupFile.Stat()
if err != nil {
return errors.Wrapf(err, "failed to get stat of backup file %q", backupFile.Name())
}
backupFileBytes := backupFileInfo.Size()
- replayBinlogPaths, err := mysql.GetBinlogReplayList(startBinlogInfo, binlogDir)
+ replayBinlogPaths, err := driver.GetBinlogReplayList(startBinlogInfo)
if err != nil {
- return errors.Wrapf(err, "failed to get binlog replay list with startBinlogInfo %+v in binlog directory %q", startBinlogInfo, binlogDir)
+ return errors.Wrapf(err, "failed to get binlog replay list with startBinlogInfo %+v in binlog directory %q", startBinlogInfo, driver.GetBinlogDir())
}
totalBinlogBytes, err := common.GetFileSizeSum(replayBinlogPaths)
if err != nil {