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 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-12-04 11:11:44 -08:00
1 parent ac12fc4683
commit 7ead5e4e23
3 files changed
+54 -7

No files matched your search

+7 -3
View File
@@ -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()
+42 -4
View File
@@ -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")
}
}
+5
View File
@@ -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() {