mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
397f127f6b
commit
22beba4e50
4 files changed
+42
-27
No files matched your search
@@ -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)
|
||||
}()
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user