Skip to content

Commit c3188f5

Browse files
author
Dylan Terry
committed
Make sure to assign the timeout on the syncer so the backup doesn't fail
1 parent a8bcf4c commit c3188f5

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

replication/backup.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (b *BinlogSyncer) StartBackup(backupDir string, p Position, timeout time.Du
2323
return os.OpenFile(path.Join(backupDir, filename), os.O_CREATE|os.O_WRONLY, 0644)
2424
})
2525
} else {
26-
return b.StartSynchronousBackup(p)
26+
return b.StartSynchronousBackup(p, timeout)
2727
}
2828
}
2929

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

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

102+
var ctx context.Context
103+
var cancel context.CancelFunc
104+
105+
if timeout > 0 {
106+
ctx, cancel = context.WithTimeout(context.Background(), timeout)
107+
defer cancel()
108+
} else {
109+
ctx = context.Background()
110+
}
111+
102112
select {
113+
case <-ctx.Done():
114+
// The timeout has been reached
115+
return nil
103116
case <-b.ctx.Done():
117+
// The BinlogSyncer has been closed
104118
return nil
105119
case err := <-s.ech:
120+
// An error occurred during streaming
106121
return errors.Trace(err)
107122
}
108123
}

replication/backup_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/go-mysql-org/go-mysql/mysql"
1313
)
1414

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

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

99+
t.b.ctx = ctx
100+
98101
// Wait for the backup to complete or timeout
99102
select {
100103
case <-done:

0 commit comments

Comments
 (0)