mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* 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>
323 lines
11 KiB
Go
323 lines
11 KiB
Go
package topic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"runtime"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
|
)
|
|
|
|
// cleanupIdlePartitions used to call manager.topics.Remove from inside
|
|
// manager.topics.IterCb. IterCb holds the shard's read lock while running the
|
|
// callback, and Remove takes the same shard's write lock, so removing an
|
|
// emptied topic deadlocked the cleanup goroutine and permanently wedged the
|
|
// shard: every later ListTopicsInMemory / TopicExistsInMemory blocked behind
|
|
// the stuck writer. Observed in CI as the Kafka gateway's Metadata requests
|
|
// timing out forever once any topic went idle mid-test.
|
|
func TestCleanupIdlePartitionsRemovesEmptyTopicWithoutDeadlock(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
|
|
tp := NewTopic("test", "idle-topic")
|
|
localTopic := NewLocalTopic(tp)
|
|
localPartition := NewLocalPartition(Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}, 1, nil, nil)
|
|
localTopic.Partitions = append(localTopic.Partitions, localPartition)
|
|
manager.topics.Set(tp.String(), localTopic)
|
|
|
|
// Make the partition idle beyond any timeout.
|
|
localPartition.lastActivityTime.Store(time.Now().Add(-time.Hour).UnixNano())
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
manager.cleanupIdlePartitions(time.Minute)
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("cleanupIdlePartitions deadlocked while removing an emptied topic")
|
|
}
|
|
|
|
if manager.topics.Has(tp.String()) {
|
|
t.Errorf("emptied topic %s should have been removed", tp.String())
|
|
}
|
|
|
|
// The map must still be fully usable afterwards — with the deadlock the
|
|
// wedged shard made this call block forever.
|
|
listed := make(chan []Topic, 1)
|
|
go func() { listed <- manager.ListTopicsInMemory() }()
|
|
select {
|
|
case topics := <-listed:
|
|
if len(topics) != 0 {
|
|
t.Errorf("expected no topics in memory, got %v", topics)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("ListTopicsInMemory blocked after cleanup — shard still wedged")
|
|
}
|
|
}
|
|
|
|
// A topic that gains a partition between the cleanup scan and the removal must
|
|
// be kept: removal re-checks emptiness under the shard write lock.
|
|
func TestCleanupIdlePartitionsKeepsActiveTopic(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
|
|
tp := NewTopic("test", "active-topic")
|
|
localPartition := NewLocalPartition(Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}, 1, nil, nil)
|
|
manager.AddLocalPartition(tp, localPartition)
|
|
|
|
// Fresh activity: nothing should be cleaned up.
|
|
manager.cleanupIdlePartitions(time.Minute)
|
|
|
|
if !manager.topics.Has(tp.String()) {
|
|
t.Errorf("active topic %s should not have been removed", tp.String())
|
|
}
|
|
if manager.GetLocalPartition(tp, localPartition.Partition) == nil {
|
|
t.Errorf("active partition should still be registered")
|
|
}
|
|
}
|
|
|
|
func TestShutdownFlushesEveryPartition(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
flushed := make(chan []byte, 4)
|
|
flushFn := func(_ *log_buffer.LogBuffer, _, _ time.Time, buf []byte, _, _ int64) {
|
|
flushed <- append([]byte(nil), buf...)
|
|
}
|
|
tp := NewTopic("test", "shutdown")
|
|
// a flush interval long enough that only the shutdown can flush
|
|
p := NewLocalPartition(Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}, 3600, flushFn, nil)
|
|
manager.AddLocalPartition(tp, p)
|
|
if err := p.LogBuffer.AddDataToBuffer([]byte("k"), []byte("v"), time.Now().UnixNano()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
manager.Shutdown()
|
|
|
|
select {
|
|
case buf := <-flushed:
|
|
if len(buf) == 0 {
|
|
t.Fatal("shutdown flushed an empty buffer")
|
|
}
|
|
default:
|
|
t.Fatal("Shutdown returned before the partition flushed")
|
|
}
|
|
}
|
|
|
|
func TestShutdownRefusesNewPartitions(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
manager.Shutdown()
|
|
p := NewLocalPartition(Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}, 1, nil, nil)
|
|
defer p.Shutdown()
|
|
if manager.AddLocalPartition(NewTopic("test", "late"), p) {
|
|
t.Fatal("a partition added after Shutdown would never be flushed")
|
|
}
|
|
}
|
|
|
|
func TestShutdownWaitsForPartitionsRetiredByCleanup(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
flushed := make(chan []byte, 4)
|
|
flushFn := func(_ *log_buffer.LogBuffer, _, _ time.Time, buf []byte, _, _ int64) {
|
|
time.Sleep(100 * time.Millisecond)
|
|
flushed <- append([]byte(nil), buf...)
|
|
}
|
|
p := NewLocalPartition(Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}, 3600, flushFn, nil)
|
|
manager.AddLocalPartition(NewTopic("test", "idle"), p)
|
|
if err := p.LogBuffer.AddDataToBuffer([]byte("k"), []byte("v"), time.Now().UnixNano()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.lastActivityTime.Store(time.Now().Add(-time.Hour).UnixNano())
|
|
manager.cleanupIdlePartitions(time.Minute)
|
|
|
|
manager.Shutdown()
|
|
|
|
select {
|
|
case <-flushed:
|
|
default:
|
|
t.Fatal("Shutdown returned before a partition retired by idle cleanup flushed")
|
|
}
|
|
}
|
|
|
|
func TestShutdownWaitsForDrainedPartitions(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
flushed := make(chan []byte, 4)
|
|
flushFn := func(_ *log_buffer.LogBuffer, _, _ time.Time, buf []byte, _, _ int64) {
|
|
time.Sleep(100 * time.Millisecond)
|
|
flushed <- append([]byte(nil), buf...)
|
|
}
|
|
tp := NewTopic("test", "drained")
|
|
partition := Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}
|
|
p := NewLocalPartition(partition, 3600, flushFn, nil)
|
|
manager.AddLocalPartition(tp, p)
|
|
if err := p.LogBuffer.AddDataToBuffer([]byte("k"), []byte("v"), time.Now().UnixNano()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !manager.RemoveLocalPartition(tp, p) {
|
|
t.Fatal("partition not removed")
|
|
}
|
|
|
|
manager.Shutdown()
|
|
|
|
select {
|
|
case <-flushed:
|
|
default:
|
|
t.Fatal("Shutdown returned before a drained partition flushed")
|
|
}
|
|
}
|
|
|
|
// A removal racing Shutdown used to detach the partition after Shutdown's
|
|
// snapshot and before it was retired, so Shutdown returned without waiting for
|
|
// its flush. Once Shutdown starts, the partition stays for Shutdown to retire.
|
|
func TestRemoveAfterShutdownStarts(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
tp := NewTopic("test", "removed-while-stopping")
|
|
partition := Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}
|
|
manager.AddLocalPartition(tp, NewLocalPartition(partition, 3600, nil, nil))
|
|
|
|
manager.Shutdown()
|
|
if manager.RemoveLocalPartition(tp, manager.GetLocalPartition(tp, partition)) {
|
|
t.Fatal("removed a partition after Shutdown started")
|
|
}
|
|
}
|
|
|
|
// A partition can be shut down again, for example while a subscriber is
|
|
// still registered, without closing that subscriber's stop channel twice.
|
|
func TestPartitionShutdownTwiceWithSubscriber(t *testing.T) {
|
|
p := NewLocalPartition(Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}, 3600, nil, nil)
|
|
p.Subscribers.AddSubscriber("subscriber", NewLocalSubscriber())
|
|
p.Shutdown()
|
|
p.Shutdown()
|
|
}
|
|
|
|
// A replacement partition seeds its offsets from disk, so it must wait for the
|
|
// retired one's last flush to land.
|
|
func TestWaitRetiredWaitsForTheLastFlush(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
release := make(chan struct{})
|
|
var releaseOnce sync.Once
|
|
t.Cleanup(func() { releaseOnce.Do(func() { close(release) }) })
|
|
var flushed atomic.Bool
|
|
flushFn := func(_ *log_buffer.LogBuffer, _, _ time.Time, _ []byte, _, _ int64) {
|
|
<-release
|
|
flushed.Store(true)
|
|
}
|
|
tp := NewTopic("test", "moved")
|
|
partition := Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}
|
|
p := NewLocalPartition(partition, 3600, flushFn, nil)
|
|
manager.AddLocalPartition(tp, p)
|
|
if err := p.LogBuffer.AddDataToBuffer([]byte("k"), []byte("v"), time.Now().UnixNano()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manager.RemoveLocalPartition(tp, p)
|
|
if manager.RetiringPartition(tp, partition) == nil {
|
|
t.Fatal("a just retired partition should be pending")
|
|
}
|
|
cancelled, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if err := manager.WaitRetired(cancelled, tp, partition); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("WaitRetired with a cancelled context returned %v", err)
|
|
}
|
|
if manager.RetiringPartition(tp, partition) == nil {
|
|
t.Fatal("a cancelled wait dropped the pending retirement")
|
|
}
|
|
before := runtime.NumGoroutine()
|
|
for i := 0; i < 100; i++ {
|
|
manager.WaitRetired(cancelled, tp, partition)
|
|
}
|
|
if left := runtime.NumGoroutine() - before; left > 10 {
|
|
t.Fatalf("cancelled waits left %d goroutines behind", left)
|
|
}
|
|
|
|
releaseOnce.Do(func() { close(release) })
|
|
if err := manager.WaitRetired(context.Background(), tp, partition); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !flushed.Load() {
|
|
t.Fatal("WaitRetired returned before the last flush")
|
|
}
|
|
if manager.RetiringPartition(tp, partition) != nil {
|
|
t.Fatal("still pending after WaitRetired")
|
|
}
|
|
}
|
|
|
|
// A client of a replaced copy removing it on its way out leaves the replacement
|
|
// listed.
|
|
func TestRemovingAReplacedPartitionKeepsTheReplacement(t *testing.T) {
|
|
manager := NewLocalTopicManager()
|
|
tp := NewTopic("test", "replaced")
|
|
partition := Partition{RingSize: 2520, RangeStart: 0, RangeStop: 2520}
|
|
old := NewLocalPartition(partition, 3600, nil, nil)
|
|
manager.AddLocalPartition(tp, old)
|
|
if !manager.RemoveLocalPartition(tp, old) {
|
|
t.Fatal("partition not removed")
|
|
}
|
|
manager.WaitRetired(context.Background(), tp, partition)
|
|
replacement := NewLocalPartition(partition, 3600, nil, nil)
|
|
defer replacement.Shutdown()
|
|
manager.AddLocalPartition(tp, replacement)
|
|
|
|
if manager.RemoveLocalPartition(tp, old) {
|
|
t.Error("removed the replaced copy twice")
|
|
}
|
|
if manager.GetLocalPartition(tp, partition) != replacement {
|
|
t.Error("the replaced copy's removal took its replacement")
|
|
}
|
|
if manager.RetiringPartition(tp, partition) != nil {
|
|
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)
|
|
}
|
|
})
|
|
}
|