Skip to content

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

Revert "refactor: GetBinlogReplayList (#2337)" (#2340)

Yilong Li98529be8ed60
diff --git a/plugin/db/mysql/restore.go b/plugin/db/mysql/restore.go
index 07f1a472d4..bd4c6b7366 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)
+ return driver.replayBinlogReadFromDir(ctx, originalDatabase, targetDatabase, startBinlogInfo, targetTs, driver.binlogDir)
}
// 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) error {
- replayBinlogPaths, err := driver.GetBinlogReplayList(startBinlogInfo)
+func (driver *Driver) replayBinlogReadFromDir(ctx context.Context, originalDatabase, targetDatabase string, startBinlogInfo api.BinlogInfo, targetTs int64, binlogDir string) error {
+ replayBinlogPaths, err := GetBinlogReplayList(startBinlogInfo, binlogDir)
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) error {
- return driver.replayBinlogReadFromDir(ctx, originDatabaseName, targetDatabaseName, startBinlogInfo, targetTs)
+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)
}
// 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 (driver *Driver) GetBinlogReplayList(startBinlogInfo api.BinlogInfo) ([]string, error) {
+func GetBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]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(driver.binlogDir)
+ binlogFiles, err := ioutil.ReadDir(binlogDir)
if err != nil {
- return nil, errors.Wrapf(err, "cannot read binlog directory %s", driver.binlogDir)
+ return nil, errors.Wrapf(err, "cannot read binlog directory %s", binlogDir)
}
var binlogFilesToReplay []BinlogFile
@@ -277,7 +277,7 @@ func (driver *Driver) GetBinlogReplayList(startBinlogInfo api.BinlogInfo) ([]str
var binlogReplayList []string
for _, binlogFile := range binlogFilesToReplaySorted {
- binlogReplayList = append(binlogReplayList, filepath.Join(driver.binlogDir, binlogFile.Name))
+ binlogReplayList = append(binlogReplayList, filepath.Join(binlogDir, binlogFile.Name))
}
return binlogReplayList, nil
diff --git a/plugin/db/mysql/restore_test.go b/plugin/db/mysql/restore_test.go
index 7ec2cfe9ab..07108e6b2f 100644
--- a/plugin/db/mysql/restore_test.go
+++ b/plugin/db/mysql/restore_test.go
@@ -262,8 +262,7 @@ func TestGetReplayBinlogPathList(t *testing.T) {
a.NoError(err)
}
- driver := Driver{binlogDir: tmpDir}
- result, err := driver.GetBinlogReplayList(test.startBinlogInfo)
+ result, err := GetBinlogReplayList(test.startBinlogInfo, tmpDir)
if test.err {
a.Error(err)
} else {
diff --git a/server/task_executor_pitr_restore.go b/server/task_executor_pitr_restore.go
index 13d9e18b59..4ad49c91d9 100644
--- a/server/task_executor_pitr_restore.go
+++ b/server/task_executor_pitr_restore.go
@@ -245,8 +245,9 @@ 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); err != nil {
+ if err := exec.updateProgress(ctx, mysqlTargetDriver, backupFile, startBinlogInfo, binlogDir); err != nil {
return nil, errors.Wrap(err, "failed to setup progress update process")
}
@@ -258,7 +259,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); err != nil {
+ if err := mysqlTargetDriver.ReplayBinlogToDatabase(ctx, task.Database.Name, *payload.DatabaseName, startBinlogInfo, targetTs, mysqlSourceDriver.GetBinlogDir()); err != nil {
log.Error("failed to perform a PITR restore in the new database",
zap.Int("issueID", issue.ID),
zap.String("databaseName", *payload.DatabaseName),
@@ -343,15 +344,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) error {
+func (exec *PITRRestoreTaskExecutor) updateProgress(ctx context.Context, driver *mysql.Driver, backupFile *os.File, startBinlogInfo api.BinlogInfo, binlogDir string) 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 := driver.GetBinlogReplayList(startBinlogInfo)
+ replayBinlogPaths, err := mysql.GetBinlogReplayList(startBinlogInfo, binlogDir)
if err != nil {
- return errors.Wrapf(err, "failed to get binlog replay list with startBinlogInfo %+v in binlog directory %q", startBinlogInfo, driver.GetBinlogDir())
+ return errors.Wrapf(err, "failed to get binlog replay list with startBinlogInfo %+v in binlog directory %q", startBinlogInfo, binlogDir)
}
totalBinlogBytes, err := common.GetFileSizeSum(replayBinlogPaths)
if err != nil {