mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-12 09:17:44 +02:00
* filer: bound metadata log flush retries during shutdown On SIGTERM the filer could hang forever in Shutdown: the final LocalMetaLogBuffer flush retries appendToFile indefinitely, and with the master already down each AssignVolume attempt just kept failing. WaitForShutdown never returned, the interrupt hook never reached os.Exit, and the half-dead filer kept its ports bound. Thread a context through appendToFile/assignAndUpload and switch to a 15s-bounded context once the filer is stopping: the flush abandons with a log line instead of retrying forever. Normal operation keeps the unbounded retry so no metadata is dropped while running. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: share one shutdown deadline across all pending meta log flushes Review feedback on the per-flush timeout: a deadline armed at flush start could already be expired when shutdown arrived, the first append of a flush still ran unbounded, each queued window got a fresh budget (16 windows * 15s), and an abandoned window still advanced the flushed watermark as if it had landed. Rework to a single shared flush context on the Filer, cancelled once by Shutdown via AfterFunc. Every append - the in-flight one and every queued window - observes the same deadline, so the whole drain is bounded at 15s. A flush that gives up reports its dropped bytes through the new LogBuffer.NoteFlushDropped, and loopFlush then skips the offset/timestamp advance and subscriber notifications for that window. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: bound append attempts and commit uploaded pieces detached Store operations check then drop request cancellation, so a stalled backend could still hold flushFn past the shutdown deadline; run each append attempt on its own goroutine and give up on it at the deadline. Once a piece is uploaded, commit its entry on a detached context so the expired deadline cannot strand the chunk. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * filer: keep shutdown flushes synchronous Detaching the append attempt let flushFn return while the goroutine still held the pooled flush buffer and could commit after the metadata store closed; abandonment is only safe for the cancelable assign/upload phase, which the shared flush context already bounds. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
package filer
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
|
)
|
|
|
|
type shutdownStore struct {
|
|
VirtualFilerStore
|
|
closed chan struct{}
|
|
}
|
|
|
|
func (s *shutdownStore) Shutdown() {
|
|
close(s.closed)
|
|
}
|
|
|
|
func TestShutdownKeepsStoreOpenUntilMetadataIsFlushed(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
store := &shutdownStore{closed: make(chan struct{})}
|
|
releaseFlush := make(chan struct{})
|
|
flushed := false
|
|
lb := log_buffer.NewLogBuffer("filer shutdown", time.Hour,
|
|
func(_ *log_buffer.LogBuffer, _, _ time.Time, _ []byte, _, _ int64) {
|
|
<-releaseFlush
|
|
select {
|
|
case <-store.closed:
|
|
t.Error("metadata store closed before the pending log write")
|
|
default:
|
|
flushed = true
|
|
}
|
|
}, nil, nil)
|
|
f := &Filer{Store: store, LocalMetaLogBuffer: lb, deletionQuit: make(chan struct{})}
|
|
if err := lb.AddDataToBuffer(nil, []byte("last metadata event"), 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
f.Shutdown()
|
|
close(done)
|
|
}()
|
|
synctest.Wait()
|
|
select {
|
|
case <-store.closed:
|
|
t.Error("metadata store closed while the log write was blocked")
|
|
default:
|
|
}
|
|
|
|
close(releaseFlush)
|
|
<-done
|
|
lb.WaitForShutdown()
|
|
if !flushed {
|
|
t.Error("shutdown returned without persisting the pending metadata")
|
|
}
|
|
select {
|
|
case <-store.closed:
|
|
default:
|
|
t.Error("metadata store was not closed after the log write finished")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestShutdownBoundsBlockedMetadataFlush(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
store := &shutdownStore{closed: make(chan struct{})}
|
|
f := &Filer{Store: store, deletionQuit: make(chan struct{})}
|
|
f.flushCtx, f.flushCancel = context.WithCancel(context.Background())
|
|
|
|
var flushStarted atomic.Bool
|
|
lb := log_buffer.NewLogBuffer("blocked flush", time.Hour,
|
|
func(lb *log_buffer.LogBuffer, _, _ time.Time, buf []byte, _, _ int64) {
|
|
flushStarted.Store(true)
|
|
// A dead cluster makes append retries hang here; the shutdown
|
|
// deadline cancels the shared flush context to unblock it.
|
|
<-f.flushCtx.Done()
|
|
lb.NoteFlushDropped(len(buf))
|
|
}, nil, nil)
|
|
f.LocalMetaLogBuffer = lb
|
|
if err := lb.AddDataToBuffer(nil, []byte("last metadata event"), 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
f.Shutdown()
|
|
close(done)
|
|
}()
|
|
|
|
<-done
|
|
if !flushStarted.Load() {
|
|
t.Error("shutdown finished without running the pending flush")
|
|
}
|
|
if ts := lb.GetLastFlushTsNs(); ts != 0 {
|
|
t.Errorf("dropped flush advanced the flushed watermark to %d", ts)
|
|
}
|
|
select {
|
|
case <-store.closed:
|
|
default:
|
|
t.Error("metadata store was not closed after the bounded wait")
|
|
}
|
|
})
|
|
}
|