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) + } }