Skip to content

Commit

Permalink
Make sure to assign the timeout on the syncer so the backup doesn't fail
Browse files Browse the repository at this point in the history
  • Loading branch information
Dylan Terry committed Oct 11, 2024
1 parent a8bcf4c commit c3188f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 17 additions & 2 deletions replication/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Du
return os.OpenFile(path.Join(backupDir, filename), os.O_CREATE|os.O_WRONLY, 0644)
})
} else {
return b.StartSynchronousBackup(p)
return b.StartSynchronousBackup(p, timeout)
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func (b *BinlogSyncer) StartBackupWithHandler(p Position, timeout time.Duration,
}

// StartSynchronousBackup starts the backup process using the SynchronousEventHandler in the BinlogSyncerConfig.
func (b *BinlogSyncer) StartSynchronousBackup(p Position) error {
func (b *BinlogSyncer) StartSynchronousBackup(p Position, timeout time.Duration) error {
if b.cfg.SynchronousEventHandler == nil {
return errors.New("SynchronousEventHandler must be set in BinlogSyncerConfig to use StartSynchronousBackup")
}
Expand All @@ -99,10 +99,25 @@ func (b *BinlogSyncer) StartSynchronousBackup(p Position) error {
return errors.Trace(err)
}

var ctx context.Context
var cancel context.CancelFunc

if timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
defer cancel()
} else {
ctx = context.Background()
}

select {
case <-ctx.Done():
// The timeout has been reached
return nil
case <-b.ctx.Done():
// The BinlogSyncer has been closed
return nil
case err := <-s.ech:
// An error occurred during streaming
return errors.Trace(err)
}
}
Expand Down
3 changes: 3 additions & 0 deletions replication/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-mysql-org/go-mysql/mysql"
)

// TestStartBackupEndInGivenTime tests the backup process completes within a given time.
func (t *testSyncerSuite) TestStartBackupEndInGivenTime() {
t.setupTest(mysql.MySQLFlavor)

Expand Down Expand Up @@ -95,6 +96,8 @@ func testBackup(t *testSyncerSuite, isSynchronous bool) {
ctx, cancel := context.WithTimeout(context.Background(), failTimeout)
defer cancel()

t.b.ctx = ctx

// Wait for the backup to complete or timeout
select {
case <-done:
Expand Down

0 comments on commit c3188f5

Please sign in to comment.