mq: avoid a deadlock when retiring a partition with a full flush queue (#13225)

* mq: let partition retirement drain a full flush queue outside broker locks

* log_buffer: queue the final flush from ShutdownLogBuffer itself

Every ShutdownLogBuffer caller already waits afterwards with WaitForShutdown
or IsAllFlushed, so queueing the final flush in the background changes no
caller's outcome and lets every caller holding a lock a flush callback needs
avoid the full-queue deadlock, without a second shutdown entry point.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-12-01 15:32:00 -08:00
1 parent 45458336e1
commit 52afabbf1b
2 files changed
+67 -5

No files matched your search

+50
View File
@@ -7,6 +7,7 @@ import (
"sync"
"sync/atomic"
"testing"
"testing/synctest"
"time"
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
@@ -270,3 +271,52 @@ func TestRemovingAReplacedPartitionKeepsTheReplacement(t *testing.T) {
t.Error("the replacement is pending retirement")
}
}
func TestRetirePartitionWithFullFlushQueue(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
manager := NewLocalTopicManager()
tp := NewTopic("test", "retire")
releaseFlush := make(chan struct{})
flushed := 0
p := NewLocalPartition(Partition{RingSize: 2520, RangeStop: 2520}, 3600,
func(_ *log_buffer.LogBuffer, _, _ time.Time, _ []byte, _, _ int64) {
<-releaseFlush
flushed++
}, nil)
manager.AddLocalPartition(tp, p)
// Each record starts a new window. Wait for the producer to block
// handing off a window behind the stalled flush.
writesDone := make(chan struct{})
accepted := 0
go func() {
defer close(writesDone)
for {
ts := time.Now().Add(time.Duration(accepted) * 2 * time.Hour)
err := p.LogBuffer.AddDataToBuffer(nil, []byte("v"), ts.UnixNano())
if errors.Is(err, log_buffer.ErrBufferStopped) {
return
}
if err != nil {
t.Error(err)
return
}
accepted++
}
}()
synctest.Wait()
// A flush callback can need the caller's broker or topic lock, so
// removal must return before the callback is allowed to proceed.
manager.RemoveLocalPartition(tp, p)
if !p.LogBuffer.IsStopping() {
t.Error("retired partition still accepts writes")
}
close(releaseFlush)
<-writesDone
manager.Shutdown()
if flushed == 0 || flushed != accepted {
t.Errorf("flushed %d windows after accepting %d", flushed, accepted)
}
})
}
+17 -5
View File
@@ -756,11 +756,19 @@ func (logBuffer *LogBuffer) beginWrite() bool {
return true
}
// ShutdownLogBuffer flushes the buffer and stops the log buffer
// ShutdownLogBuffer stops writes before returning and queues every accepted
// window for flushing in the background, so a caller holding a lock a flush
// callback needs cannot deadlock on a full queue. WaitForShutdown waits for
// the flushes to land.
func (logBuffer *LogBuffer) ShutdownLogBuffer() {
isAlreadyStopped := logBuffer.isStopping.Swap(true)
if isAlreadyStopped {
return
if logBuffer.stopWrites() {
go logBuffer.queueFinalFlush()
}
}
func (logBuffer *LogBuffer) stopWrites() bool {
if logBuffer.isStopping.Swap(true) {
return false
}
// Wake any subscribers blocked in awaitNotificationOrTimeout so they can
// notice IsStopping() and exit promptly, even on an idle buffer where no
@@ -769,6 +777,10 @@ func (logBuffer *LogBuffer) ShutdownLogBuffer() {
// Wake oversized writers waiting for room; they will observe shutdown
// before appending. Already admitted writes must finish their handoff.
logBuffer.flushBudget.close()
return true
}
func (logBuffer *LogBuffer) queueFinalFlush() {
logBuffer.writeMu.Lock()
defer logBuffer.writeMu.Unlock()
logBuffer.Lock()
@@ -783,7 +795,7 @@ func (logBuffer *LogBuffer) ShutdownLogBuffer() {
}
// WaitForShutdown waits for pending flushes and background loops to finish.
// Call ShutdownLogBuffer first, after stopping producers.
// Call ShutdownLogBuffer first.
func (logBuffer *LogBuffer) WaitForShutdown() {
logBuffer.loopsDone.Wait()
}