From 22beba4e509874ef6281bc54ee3a44ce36f71266 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Thu, 3 Dec 2026 23:59:36 -0800 Subject: [PATCH] mount: refuse writes once the shutdown flush starts A write admitted after its handle was flushed was acknowledged and then lost when the process exited. The shutdown flush now sets a flag that Write checks under the handle lock, takes every handle lock once to wait out the writes already admitted, and reports whether every flush succeeded. The test now buffers real bytes and checks they are uploaded and committed. Co-Authored-By: Claude Opus 5.5 --- weed/mount/weedfs.go | 6 ++- weed/mount/weedfs_file_write.go | 3 ++ weed/mount/weedfs_metadata_flush.go | 20 +++++++++- .../weedfs_metadata_flush_shutdown_test.go | 40 ++++++++----------- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index 272ae3445d..b95959a169 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -237,6 +237,8 @@ type WFS struct { // asyncFlushWg tracks pending background flush work items for writebackCache mode. // Must be waited on before unmount cleanup to prevent data loss. asyncFlushWg sync.WaitGroup + // shuttingDown refuses writes once the shutdown flush has begun. + shuttingDown atomic.Bool // metaFollowWg tracks the metadata subscription goroutine and // metaFollowCancel ends it. Unmount closes the meta cache the follow writes @@ -397,7 +399,9 @@ func NewSeaweedFileSystem(option *Option) *WFS { asyncDrained := true done := make(chan struct{}) go func() { - wfs.flushOpenFiles() + if !wfs.flushOpenFiles() { + glog.Warningf("some open files failed to flush before shutdown") + } wfs.asyncFlushWg.Wait() close(done) }() diff --git a/weed/mount/weedfs_file_write.go b/weed/mount/weedfs_file_write.go index 08694d11a7..d404d4dd28 100644 --- a/weed/mount/weedfs_file_write.go +++ b/weed/mount/weedfs_file_write.go @@ -62,6 +62,9 @@ func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (wr fhActiveLock := fh.wfs.fhLockTable.AcquireLock("Write", fh.fh, util.ExclusiveLock) defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock) + if wfs.shuttingDown.Load() { + return 0, fuse.EIO + } entry := fh.GetEntry() if entry == nil { diff --git a/weed/mount/weedfs_metadata_flush.go b/weed/mount/weedfs_metadata_flush.go index 2070e8c145..048b4d0f90 100644 --- a/weed/mount/weedfs_metadata_flush.go +++ b/weed/mount/weedfs_metadata_flush.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "sync" + "sync/atomic" "time" "github.com/seaweedfs/go-fuse/v2/fuse" @@ -55,13 +56,28 @@ func (wfs *WFS) flushAllDirtyMetadata() { // flushOpenFiles uploads the buffered data of every open file and commits // its metadata, as fsync would, so a terminated mount keeps the writes it -// already acknowledged. -func (wfs *WFS) flushOpenFiles() { +// already acknowledged. Writes are refused from here on, and taking each +// handle lock once waits out the writes already admitted. +func (wfs *WFS) flushOpenFiles() bool { + wfs.shuttingDown.Store(true) + wfs.fhMap.RLock() + handles := make([]*FileHandle, 0, len(wfs.fhMap.inode2fh)) + for _, fh := range wfs.fhMap.inode2fh { + handles = append(handles, fh) + } + wfs.fhMap.RUnlock() + for _, fh := range handles { + wfs.fhLockTable.ReleaseLock(fh.fh, wfs.fhLockTable.AcquireLock("flushOpenFiles", fh.fh, util.ExclusiveLock)) + } + + var failed atomic.Bool wfs.forEachDirtyHandle(func(fh *FileHandle) { if status := wfs.doFlush(context.Background(), fh, unknownCallerId, unknownCallerId, false, metadataFlushTimeout); status != fuse.OK { + failed.Store(true) glog.Warningf("failed to flush open file %s: %v", fh.FullPath(), status) } }) + return !failed.Load() } func (wfs *WFS) forEachDirtyHandle(fn func(fh *FileHandle)) { diff --git a/weed/mount/weedfs_metadata_flush_shutdown_test.go b/weed/mount/weedfs_metadata_flush_shutdown_test.go index d6af3f9223..b252a734c4 100644 --- a/weed/mount/weedfs_metadata_flush_shutdown_test.go +++ b/weed/mount/weedfs_metadata_flush_shutdown_test.go @@ -3,35 +3,27 @@ package mount import ( "testing" - "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/seaweedfs/go-fuse/v2/fuse" ) -// Shutdown commits a handle the kernel never closed, so its writes reach the -// filer instead of dying with the process. -func TestFlushOpenFilesCommitsOpenHandle(t *testing.T) { - wfs := newInvalidateTestWFS(t) - observed := flushConditionEntry(1) - live := flushConditionEntry(1, 3) - live.Chunks[1].Offset = 2 - wfs.mapPbIdFromFilerToLocal(observed) - wfs.mapPbIdFromFilerToLocal(live) - fake := &contentFlushConditionFiler{entry: flushConditionEntry(1)} - startFakeFiler(t, wfs, fake) - fh := newFileHandle(wfs, 1, 42, live) - t.Cleanup(func() { fh.dirtyPages.Destroy() }) - fh.baseEntry.Store(observed) - fh.dirtyMetadata = true - fh.RememberPath(util.FullPath("/dir/file")) - wfs.fhMap.inode2fh[42], wfs.fhMap.fh2inode[1] = fh, 42 +// Shutdown uploads the bytes an open file still buffers and commits them, then +// refuses further writes instead of acknowledging data it cannot keep. +func TestFlushOpenFilesUploadsBufferedWrites(t *testing.T) { + cluster := newProofCluster() + _, addr := newProofFiler(t, cluster, newVolumeStub(t)) + wfs := newProofMount(t, scopedMountToken(), addr) + fh := openDirtyFile(t, wfs, cluster) - wfs.flushOpenFiles() - - fake.mu.Lock() - defer fake.mu.Unlock() - if fake.calls != 1 || len(fake.entry.GetChunks()) != 2 { - t.Fatalf("filer got %d calls and %d chunks, want 1 call storing 2 chunks", fake.calls, len(fake.entry.GetChunks())) + if !wfs.flushOpenFiles() { + t.Fatal("shutdown flush failed") + } + if chunks := cluster.entry("/dir/file").GetChunks(); len(chunks) != 1 { + t.Fatalf("stored chunks = %v, want the buffered write uploaded", chunks) } if fh.dirtyMetadata { t.Fatal("handle still dirty after the shutdown flush") } + if _, code := wfs.Write(nil, &fuse.WriteIn{Fh: uint64(fh.fh), Size: 1}, []byte("x")); code != fuse.EIO { + t.Fatalf("write after the shutdown flush = %v, want EIO", code) + } }