From 7ead5e4e232a9b52cb6737e8f3b450a2f3e867a3 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 11:11:44 -0800 Subject: [PATCH] filer: persist the metadata log as soon as shutdown starts (#13386) The shutdown leaves the lock ring and drains gRPC and HTTP before Filer.Shutdown runs the final metadata log flush. Those steps can take longer than a container's termination grace period, and a kill before the final flush drops every local event since the last periodic flush. On a shared store peers keep those events only in memory, so they vanish from every filer's aggregated stream. Start a flush of the local metadata log first, in the background, so the events accepted before the signal are persisted within seconds. Co-authored-by: Claude Opus 5.5 --- weed/command/filer.go | 10 +++++-- weed/command/filer_shutdown_test.go | 46 ++++++++++++++++++++++++++--- weed/server/filer_server.go | 5 ++++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/weed/command/filer.go b/weed/command/filer.go index 0dd0972cd4..3afb4334e9 100644 --- a/weed/command/filer.go +++ b/weed/command/filer.go @@ -697,7 +697,7 @@ func (fo *FilerOptions) startFiler() { } httpS := newHttpServer(defaultHandler, tlsConfig) httpServers = append(httpServers, httpS) - shutdown := newFilerShutdown(fs.LeaveLockRing, fs.StopSubscriptions, stopGrpcServer, fs.Shutdown, httpServers...) + shutdown := newFilerShutdown(fs.FlushMetadataLog, fs.LeaveLockRing, fs.StopSubscriptions, stopGrpcServer, fs.Shutdown, httpServers...) grace.OnInterrupt(shutdown) @@ -726,7 +726,7 @@ func (fo *FilerOptions) startFiler() { } httpS := newHttpServer(defaultHandler, nil) httpServers = append(httpServers, httpS) - shutdown := newFilerShutdown(fs.LeaveLockRing, fs.StopSubscriptions, stopGrpcServer, fs.Shutdown, httpServers...) + shutdown := newFilerShutdown(fs.FlushMetadataLog, fs.LeaveLockRing, fs.StopSubscriptions, stopGrpcServer, fs.Shutdown, httpServers...) grace.OnInterrupt(shutdown) @@ -746,13 +746,17 @@ func (fo *FilerOptions) startFiler() { } // newFilerShutdown joins shutdown callers while gRPC and HTTP drain concurrently. +// It starts persisting the metadata log at once: the steps below can outlast +// the termination grace period, and a kill before the final flush would drop +// every event since the last periodic one from all aggregated streams. // The filer leaves the lock ring first: peers and S3 gateways route keys to it // until the ring changes, and would hit refused connections once gRPC stops. // Then it ends its metadata subscriptions, so gRPC GracefulStop does not wait // out its timeout on streams whose subscribers sit in this same process. -func newFilerShutdown(leaveLockRing, stopSubscriptions, stopGrpc, shutdownFiler func(), httpServers ...*http.Server) func() { +func newFilerShutdown(flushMetadataLog, leaveLockRing, stopSubscriptions, stopGrpc, shutdownFiler func(), httpServers ...*http.Server) func() { drain := newGracefulShutdown(stopGrpc, shutdownFiler, httpServers...) return sync.OnceFunc(func() { + go flushMetadataLog() leaveLockRing() stopSubscriptions() drain() diff --git a/weed/command/filer_shutdown_test.go b/weed/command/filer_shutdown_test.go index 775f6ee84e..062dbefeb2 100644 --- a/weed/command/filer_shutdown_test.go +++ b/weed/command/filer_shutdown_test.go @@ -56,7 +56,7 @@ func TestFilerShutdownJoinsServeDuringParallelDrain(t *testing.T) { grpcStopped := make(chan struct{}) filerClosed := make(chan struct{}) var closes atomic.Int32 - shutdown := newFilerShutdown(func() {}, func() {}, func() { + shutdown := newFilerShutdown(func() {}, func() {}, func() {}, func() { close(grpcStarted) <-grpcRelease close(grpcStopped) @@ -138,7 +138,7 @@ func TestFilerShutdownWaitsForHTTPAfterGrpcStops(t *testing.T) { server.Config.RegisterOnShutdown(func() { close(httpClosing) }) grpcStopped := make(chan struct{}) filerClosed := make(chan struct{}) - shutdown := newFilerShutdown(func() {}, func() {}, func() { close(grpcStopped) }, func() { close(filerClosed) }, server.Config) + shutdown := newFilerShutdown(func() {}, func() {}, func() {}, func() { close(grpcStopped) }, func() { close(filerClosed) }, server.Config) joined := make(chan struct{}) go func() { shutdown(); close(joined) }() <-httpClosing @@ -169,7 +169,7 @@ func TestFilerShutdownLeavesLockRingBeforeDraining(t *testing.T) { finishLeave := sync.OnceFunc(func() { close(releaseLeave) }) t.Cleanup(finishLeave) grpcStopping := make(chan struct{}) - shutdown := newFilerShutdown(func() { + shutdown := newFilerShutdown(func() {}, func() { close(leaving) <-releaseLeave }, func() {}, func() { close(grpcStopping) }, func() {}, server.Config) @@ -211,7 +211,7 @@ func TestFilerShutdownStopsSubscriptionsBeforeGrpc(t *testing.T) { order = append(order, step) } } - shutdown := newFilerShutdown(record("leave"), record("subscriptions"), record("grpc"), record("filer")) + shutdown := newFilerShutdown(func() {}, record("leave"), record("subscriptions"), record("grpc"), record("filer")) shutdown() want := []string{"leave", "subscriptions", "grpc", "filer"} @@ -224,3 +224,41 @@ func TestFilerShutdownStopsSubscriptionsBeforeGrpc(t *testing.T) { } } } + +// Every step after the flush can run long, and a kill before Filer.Shutdown +// reaches its final flush would lose the events of the last flush interval. +// The flush starts first but must not hold the rest of the shutdown. +func TestFilerShutdownFlushesMetadataLogFirst(t *testing.T) { + flushing := make(chan struct{}) + releaseFlush := make(chan struct{}) + finishFlush := sync.OnceFunc(func() { close(releaseFlush) }) + t.Cleanup(finishFlush) + var flushedBeforeLeave bool + filerClosed := make(chan struct{}) + shutdown := newFilerShutdown(func() { + close(flushing) + <-releaseFlush + }, func() { + select { + case <-flushing: + flushedBeforeLeave = true + case <-time.After(2 * time.Second): + } + }, func() {}, func() {}, func() { close(filerClosed) }) + joined := make(chan struct{}) + go func() { shutdown(); close(joined) }() + + select { + case <-joined: + case <-time.After(5 * time.Second): + t.Fatal("a stalled metadata log flush held the shutdown open") + } + if !flushedBeforeLeave { + t.Error("the metadata log flush did not start before leaving the lock ring") + } + select { + case <-filerClosed: + default: + t.Error("the filer was not shut down") + } +} diff --git a/weed/server/filer_server.go b/weed/server/filer_server.go index e30adc1194..9a7ca832c8 100644 --- a/weed/server/filer_server.go +++ b/weed/server/filer_server.go @@ -520,6 +520,11 @@ func (fs *FilerServer) checkWithMaster() { } } +// FlushMetadataLog persists the metadata events logged so far. +func (fs *FilerServer) FlushMetadataLog() { + fs.filer.LocalMetaLogBuffer.ForceFlush() +} + // Shutdown gracefully shuts down the filer server by waiting for in-flight uploads to complete. // This prevents data corruption when the process receives SIGTERM during active uploads. func (fs *FilerServer) Shutdown() {