Skip to content

Commit

Permalink
fix: resolve Shake client hang in HandShaking state due to source Red…
Browse files Browse the repository at this point in the history
…is blocking during BGSAVE (#879)
  • Loading branch information
EquentR authored Nov 11, 2024
1 parent fcab819 commit ce3c544
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion internal/reader/sync_standalone_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (r *syncStandaloneReader) supportPSYNC() bool {
return false
}
}

}
}

Expand Down Expand Up @@ -178,6 +178,28 @@ func (r *syncStandaloneReader) sendReplconfListenPort() {
}
}

// When BGSAVE is triggered by the source Redis itself, synchronization is blocked, so need to check it
func (r *syncStandaloneReader) checkBgsaveInProgress() {
for {
select {
case <-r.ctx.Done():
close(r.ch)
runtime.Goexit() // stop goroutine
default:
argv := []interface{}{"INFO", "persistence"}
r.client.Send(argv...)
receiveString := r.client.ReceiveString()
if strings.Contains(receiveString, "rdb_bgsave_in_progress:1") {
log.Warnf("[%s] source db is doing bgsave, waiting for a while.", r.stat.Name)
} else {
log.Infof("[%s] source db is not doing bgsave! continue.", r.stat.Name)
return
}
time.Sleep(1 * time.Second)
}
}
}

func (r *syncStandaloneReader) sendPSync() {
if r.opts.TryDiskless {
argv := []interface{}{"REPLCONF", "CAPA", "EOF"}
Expand All @@ -186,6 +208,7 @@ func (r *syncStandaloneReader) sendPSync() {
log.Warnf("[%s] send replconf capa eof to redis server failed. reply=[%v]", r.stat.Name, reply)
}
}
r.checkBgsaveInProgress()
// send PSync
argv := []interface{}{"PSYNC", "?", "-1"}
if config.Opt.Advanced.AwsPSync != "" {
Expand Down Expand Up @@ -226,6 +249,7 @@ func (r *syncStandaloneReader) sendSync() {
log.Warnf("[%s] send replconf capa eof to redis server failed. reply=[%v]", r.stat.Name, reply)
}
}
r.checkBgsaveInProgress()
// send SYNC
argv := []interface{}{"SYNC"}
if config.Opt.Advanced.AwsPSync != "" {
Expand Down

0 comments on commit ce3c544

Please sign in to comment.