mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
mount: count NFS handle opens, keep other mounts' records, retry writes
A reopen that attached a new handle before the close registered its release left a removal pending for an open file; opens are now counted. Records carry the writing mount's signature, so a delayed removal leaves a record another mount wrote since. A failed write is retried while the inode's state is unchanged, and the put runs under the attempt context. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9be2cb0b29
commit
ba6e598512
4 files changed
+103
-19
No files matched your search
@@ -124,7 +124,9 @@ func (wfs *WFS) Create(cancel <-chan struct{}, in *fuse.CreateIn, name string, o
|
||||
fileHandle.SetEntry(newEntry)
|
||||
}
|
||||
fileHandle.RememberPath(entryFullPath)
|
||||
wfs.exportedHandles.AcquireHandle(inode, entryFullPath)
|
||||
if !existed {
|
||||
wfs.exportedHandles.AcquireHandle(inode, entryFullPath)
|
||||
}
|
||||
// Mark dirty so the deferred filer create happens on Flush, even if the
|
||||
// file is closed without any writes. An eager create has already
|
||||
// persisted the entry, so its handle starts clean.
|
||||
|
||||
@@ -77,7 +77,9 @@ func (wfs *WFS) AcquireHandle(ctx context.Context, inode uint64, flags, uid, gid
|
||||
continue
|
||||
}
|
||||
fileHandle.RememberPath(path)
|
||||
wfs.exportedHandles.AcquireHandle(inode, path)
|
||||
if !existed {
|
||||
wfs.exportedHandles.AcquireHandle(inode, path)
|
||||
}
|
||||
if existed && freshLookup {
|
||||
// Another opener created the handle while our lookup was in flight;
|
||||
// install only state that provably improves on what it holds.
|
||||
|
||||
@@ -20,6 +20,8 @@ import (
|
||||
// still finds them when it comes back.
|
||||
const exportedHandleRemovalDelay = time.Minute
|
||||
|
||||
const exportedHandleRetryDelay = time.Second
|
||||
|
||||
var exportedHandleKeyPrefix = []byte("mount.nfsExport.inode.")
|
||||
|
||||
func exportedHandleKey(inode uint64) []byte {
|
||||
@@ -35,7 +37,9 @@ func exportedHandleKey(inode uint64) []byte {
|
||||
// latest state of each changed inode, so records never go back to an older one.
|
||||
type exportedHandles struct {
|
||||
mu sync.Mutex
|
||||
owner uint32
|
||||
paths map[uint64]util.FullPath
|
||||
opens map[uint64]int
|
||||
removals map[uint64]*time.Timer
|
||||
delay time.Duration
|
||||
dirty map[uint64]struct{}
|
||||
@@ -44,9 +48,11 @@ type exportedHandles struct {
|
||||
get func(ctx context.Context, key []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
func newExportedHandles(put func(key, value []byte) error, get func(ctx context.Context, key []byte) ([]byte, error)) *exportedHandles {
|
||||
func newExportedHandles(owner uint32, put func(key, value []byte) error, get func(ctx context.Context, key []byte) ([]byte, error)) *exportedHandles {
|
||||
h := &exportedHandles{
|
||||
owner: owner,
|
||||
paths: make(map[uint64]util.FullPath),
|
||||
opens: make(map[uint64]int),
|
||||
removals: make(map[uint64]*time.Timer),
|
||||
delay: exportedHandleRemovalDelay,
|
||||
dirty: make(map[uint64]struct{}),
|
||||
@@ -83,21 +89,56 @@ func (h *exportedHandles) loop() {
|
||||
delete(h.dirty, inode)
|
||||
path := h.paths[inode]
|
||||
h.mu.Unlock()
|
||||
if err := h.put(exportedHandleKey(inode), []byte(path)); err != nil {
|
||||
if err := h.write(inode, path); err != nil {
|
||||
glog.Warningf("record nfs export handle %d %q: %v", inode, path, err)
|
||||
time.AfterFunc(exportedHandleRetryDelay, func() {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if h.paths[inode] == path {
|
||||
h.markDirty(inode)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AcquireHandle records the path of an inode the mount opens, cancelling a
|
||||
// pending removal.
|
||||
// write stores path as the inode's record, owned by this mount. An empty path
|
||||
// removes the record, unless another mount has since recorded the inode.
|
||||
func (h *exportedHandles) write(inode uint64, path util.FullPath) error {
|
||||
key := exportedHandleKey(inode)
|
||||
if path == "" {
|
||||
value, err := h.get(context.Background(), key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if owner, _, ok := parseExportedHandle(value); !ok || owner != h.owner {
|
||||
return nil
|
||||
}
|
||||
return h.put(key, nil)
|
||||
}
|
||||
value := make([]byte, 4+len(path))
|
||||
binary.BigEndian.PutUint32(value, h.owner)
|
||||
copy(value[4:], path)
|
||||
return h.put(key, value)
|
||||
}
|
||||
|
||||
func parseExportedHandle(value []byte) (owner uint32, path util.FullPath, ok bool) {
|
||||
if len(value) <= 4 {
|
||||
return 0, "", false
|
||||
}
|
||||
return binary.BigEndian.Uint32(value), util.FullPath(value[4:]), true
|
||||
}
|
||||
|
||||
// AcquireHandle records the path of an inode the mount opens a new file
|
||||
// handle for, cancelling a pending removal.
|
||||
func (h *exportedHandles) AcquireHandle(inode uint64, path util.FullPath) {
|
||||
if h == nil || path == "" {
|
||||
return
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.opens[inode]++
|
||||
if t := h.removals[inode]; t != nil {
|
||||
t.Stop()
|
||||
delete(h.removals, inode)
|
||||
@@ -132,6 +173,10 @@ func (h *exportedHandles) ReleaseHandle(inode uint64) {
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if h.opens[inode]--; h.opens[inode] > 0 {
|
||||
return
|
||||
}
|
||||
delete(h.opens, inode)
|
||||
if _, recorded := h.paths[inode]; !recorded || h.removals[inode] != nil {
|
||||
return
|
||||
}
|
||||
@@ -160,16 +205,17 @@ func (h *exportedHandles) GetPath(ctx context.Context, inode uint64) (util.FullP
|
||||
glog.Warningf("read nfs export handle %d: %v", inode, err)
|
||||
return "", fuse.EIO
|
||||
}
|
||||
if len(value) == 0 {
|
||||
_, path, ok := parseExportedHandle(value)
|
||||
if !ok {
|
||||
return "", fuse.Status(syscall.ESTALE)
|
||||
}
|
||||
return util.FullPath(value), fuse.OK
|
||||
return path, fuse.OK
|
||||
}
|
||||
|
||||
func (wfs *WFS) newExportedHandles() *exportedHandles {
|
||||
return newExportedHandles(func(key, value []byte) error {
|
||||
return wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
||||
resp, err := client.KvPut(context.Background(), &filer_pb.KvPutRequest{Key: key, Value: value})
|
||||
return newExportedHandles(uint32(wfs.signature), func(key, value []byte) error {
|
||||
return wfs.withFilerClient(context.Background(), false, wfs.filerRPCWait(), func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
|
||||
resp, err := client.KvPut(ctx, &filer_pb.KvPutRequest{Key: key, Value: value})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -40,8 +40,9 @@ func (kv *fakeKv) waitFor(t *testing.T, inode uint64, want util.FullPath) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
got, _ := kv.get(context.Background(), exportedHandleKey(inode))
|
||||
if util.FullPath(got) == want {
|
||||
value, _ := kv.get(context.Background(), exportedHandleKey(inode))
|
||||
_, got, _ := parseExportedHandle(value)
|
||||
if got == want {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
@@ -51,15 +52,15 @@ func (kv *fakeKv) waitFor(t *testing.T, inode uint64, want util.FullPath) {
|
||||
}
|
||||
}
|
||||
|
||||
func newExportedHandlesForTest(kv *fakeKv, delay time.Duration) *exportedHandles {
|
||||
h := newExportedHandles(kv.put, kv.get)
|
||||
func newExportedHandlesForTest(kv *fakeKv, owner uint32, delay time.Duration) *exportedHandles {
|
||||
h := newExportedHandles(owner, kv.put, kv.get)
|
||||
h.delay = delay
|
||||
return h
|
||||
}
|
||||
|
||||
func TestExportedHandlesRecordFollowsOpenRenameAndClose(t *testing.T) {
|
||||
kv := &fakeKv{values: map[string][]byte{}}
|
||||
h := newExportedHandlesForTest(kv, 20*time.Millisecond)
|
||||
h := newExportedHandlesForTest(kv, 1, 20*time.Millisecond)
|
||||
|
||||
h.RememberPath(7, "/buckets/a.txt")
|
||||
h.AcquireHandle(8, "/buckets/c.txt")
|
||||
@@ -86,20 +87,53 @@ func TestExportedHandlesRecordFollowsOpenRenameAndClose(t *testing.T) {
|
||||
kv.waitFor(t, 7, "")
|
||||
}
|
||||
|
||||
func TestExportedHandlesReopenBeforeCloseKeepsRecord(t *testing.T) {
|
||||
kv := &fakeKv{values: map[string][]byte{}}
|
||||
h := newExportedHandlesForTest(kv, 1, 10*time.Millisecond)
|
||||
|
||||
h.AcquireHandle(7, "/buckets/a.txt")
|
||||
h.AcquireHandle(7, "/buckets/a.txt")
|
||||
h.ReleaseHandle(7)
|
||||
time.Sleep(40 * time.Millisecond)
|
||||
kv.waitFor(t, 7, "/buckets/a.txt")
|
||||
h.ReleaseHandle(7)
|
||||
kv.waitFor(t, 7, "")
|
||||
}
|
||||
|
||||
func TestExportedHandlesRemovalKeepsAnotherMountsRecord(t *testing.T) {
|
||||
kv := &fakeKv{values: map[string][]byte{}}
|
||||
first := newExportedHandlesForTest(kv, 1, 10*time.Millisecond)
|
||||
second := newExportedHandlesForTest(kv, 2, time.Minute)
|
||||
|
||||
first.AcquireHandle(7, "/buckets/a.txt")
|
||||
kv.waitFor(t, 7, "/buckets/a.txt")
|
||||
first.ReleaseHandle(7)
|
||||
second.AcquireHandle(7, "/buckets/a.txt")
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
value, _ := kv.get(context.Background(), exportedHandleKey(7))
|
||||
if owner, path, ok := parseExportedHandle(value); !ok || owner != 2 || path != "/buckets/a.txt" {
|
||||
t.Fatalf("record = %d %q %v, want the second mount's", owner, path, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDotNameFromRecordOfEarlierMount(t *testing.T) {
|
||||
kv := &fakeKv{values: map[string][]byte{}}
|
||||
file := util.FullPath("/buckets/a/b/c.txt")
|
||||
inode := file.AsInode(1)
|
||||
kv.values[string(exportedHandleKey(inode))] = []byte(file)
|
||||
kv.values[string(exportedHandleKey(inode+1))] = []byte("/elsewhere/c.txt")
|
||||
earlier := newExportedHandlesForTest(kv, 9, time.Minute)
|
||||
earlier.AcquireHandle(inode, file)
|
||||
earlier.AcquireHandle(inode+1, "/elsewhere/c.txt")
|
||||
kv.waitFor(t, inode, file)
|
||||
kv.waitFor(t, inode+1, "/elsewhere/c.txt")
|
||||
|
||||
wfs := newDotLookupTestWFS("/buckets", true)
|
||||
wfs.exportedHandles = newExportedHandlesForTest(kv, time.Minute)
|
||||
wfs.exportedHandles = newExportedHandlesForTest(kv, 1, time.Minute)
|
||||
|
||||
path, gotInode, status := wfs.resolveDotName(context.Background(), inode, ".")
|
||||
if status != fuse.OK || path != file || gotInode != inode {
|
||||
t.Fatalf("resolveDotName(.) = %q, %d, %v; want %q, %d, OK", path, gotInode, status, file, inode)
|
||||
}
|
||||
wfs.inodeToPath.RememberForgotten(inode, file)
|
||||
if !wfs.inodeToPath.LookupHandle(file, inode, false) {
|
||||
t.Fatalf("LookupHandle refused the recorded path")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user