From 52afabbf1bc3d4a87ceb536c44714e4a10b4e5f1 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Tue, 1 Dec 2026 15:32:00 -0800 Subject: [PATCH] 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 --------- Co-authored-by: Claude Opus 5.5 --- weed/mq/topic/local_manager_test.go | 50 +++++++++++++++++++++++++++++ weed/util/log_buffer/log_buffer.go | 22 ++++++++++--- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/weed/mq/topic/local_manager_test.go b/weed/mq/topic/local_manager_test.go index 74c4ec4940..222f65c67c 100644 --- a/weed/mq/topic/local_manager_test.go +++ b/weed/mq/topic/local_manager_test.go @@ -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) + } + }) +} diff --git a/weed/util/log_buffer/log_buffer.go b/weed/util/log_buffer/log_buffer.go index 11d3c3b267..6759f9d3ae 100644 --- a/weed/util/log_buffer/log_buffer.go +++ b/weed/util/log_buffer/log_buffer.go @@ -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() }