Files
seaweedfs/weed/mount/weedfs_nfs_export_test.go
T
Chris LuandClaude Opus 5.5 0872953b81 mount: resolve NFS file handles across a weed mount restart (-nfsExport) (#13020)
* mount: persist a created file's entry at create time under -nfsExport

A file created over NFS gets a handle at create time. With creation
deferred to flush, a mount that stops before the flush leaves no entry for
the next mount to resolve that handle to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: resolve NFS file handles of files an earlier mount had open

With -nfsExport, the mount records on the filer the path of each file it
has open, keyed by inode, and drops the record a minute after the last
close. A "." lookup on a node id the mount does not know reads that
record, so the NFS server can reopen its files after the mount restarts or
another mount takes over. A failed read answers EIO rather than ESTALE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: record NFS handles at open, not on every path refresh

A writeback close flushes after the last release, and the flush refreshes
the handle's path. Recording there renewed the record of a file no longer
open, so it was never removed. Opens and creates now record, and a path
refresh only moves an existing record.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: leave writeback creates deferred under -nfsExport

With writeback caching an eager create runs as an async filer RPC that an
unlink does not wait for, so a quick create+unlink could bring the name
back. Writeback mounts already accept losing unflushed files on a crash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: retain a recorded handle path only once it matches the file

A path read from the filer record was retained before the lookup checked
it, so a stale one kept answering ESTALE without reading the record again.
It is now retained right before LookupHandle, and never over a forgotten
inode or path the table already holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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>

* mount: keep one NFS handle registration per mount in an inode's record

Two mounts with the same file open shared one record, so the one that
closed last removed the path the other still had open. Each mount now
replaces or drops only its own registration, and a lookup uses the newest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: name a mount's NFS handle registrations by host and mount directory

A restarted mount drew a new random signature, so it never replaced or
removed the registrations its predecessor left for files it had open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: take turns on an NFS handle record through the lock manager under -dlm

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: retry an NFS handle record update when its lock is not granted

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: renew an NFS handle record's lock for the whole update

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: resolve an NFS handle record to the newest path under the mount root

Another mount rooted elsewhere could hold the newest registration of a
hard-linked inode, hiding this mount's own valid path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: register an NFS handle at the path the mount knows now

A rename could move the inode after an opener read its path but before
the new handle was registered, so the record kept the old path; a stale
opener could also move an existing record back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: keep no NFS handle records with -filer.tokenFile

The filer refuses KvGet and KvPut to scoped tokens, so the records would
only fail: log at startup instead, and keep the in-process handle support.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 12:31:17 -08:00

314 lines
11 KiB
Go

package mount
import (
"context"
"errors"
"fmt"
"slices"
"sync"
"syscall"
"testing"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
)
type fakeKv struct {
sync.Mutex
values map[string][]byte
err error
getDelay time.Duration
lockErr error
turn sync.Mutex
}
func (kv *fakeKv) lock(uint64) (func(), error) {
kv.Lock()
err := kv.lockErr
kv.Unlock()
if err != nil {
return nil, err
}
kv.turn.Lock()
return kv.turn.Unlock, nil
}
func (kv *fakeKv) put(key, value []byte) error {
kv.Lock()
defer kv.Unlock()
if len(value) == 0 {
delete(kv.values, string(key))
} else {
kv.values[string(key)] = append([]byte(nil), value...)
}
return nil
}
func (kv *fakeKv) get(_ context.Context, key []byte) ([]byte, error) {
kv.Lock()
value, err := kv.values[string(key)], kv.err
kv.Unlock()
time.Sleep(kv.getDelay)
return value, err
}
// waitFor waits until the inode's record holds only a registration of want,
// or none when want is empty.
func (kv *fakeKv) waitFor(t *testing.T, inode uint64, want util.FullPath) {
t.Helper()
kv.waitUntil(t, inode, want, func(got []exportedHandleRegistration) bool {
return want == "" && len(got) == 0 || len(got) == 1 && got[0].path == want
})
}
func (kv *fakeKv) waitForRegistrations(t *testing.T, inode uint64, want ...exportedHandleRegistration) {
t.Helper()
kv.waitUntil(t, inode, want, func(got []exportedHandleRegistration) bool {
return slices.Equal(got, want)
})
}
func (kv *fakeKv) waitUntil(t *testing.T, inode uint64, want any, done func([]exportedHandleRegistration) bool) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for {
value, _ := kv.get(context.Background(), exportedHandleKey(inode))
got := parseExportedHandle(value)
if done(got) {
return
}
if time.Now().After(deadline) {
t.Fatalf("registrations of inode %d = %v, want %v", inode, got, want)
}
time.Sleep(time.Millisecond)
}
}
func newExportedHandlesForTest(kv *fakeKv, owner uint32, delay time.Duration) *exportedHandles {
h := newExportedHandles(owner, kv.put, kv.get, kv.lock)
h.delay = delay
return h
}
func TestExportedHandlesRecordFollowsOpenRenameAndClose(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
h := newExportedHandlesForTest(kv, 1, 20*time.Millisecond)
h.RememberPath(7, "/buckets/a.txt")
h.AcquireHandle(8, "/buckets/c.txt")
kv.waitFor(t, 8, "/buckets/c.txt")
if got, _ := kv.get(context.Background(), exportedHandleKey(7)); len(got) != 0 {
t.Fatalf("a path change recorded inode 7, which was never opened: %q", got)
}
h.AcquireHandle(7, "/buckets/a.txt")
kv.waitFor(t, 7, "/buckets/a.txt")
h.RememberPath(7, "/buckets/b.txt")
kv.waitFor(t, 7, "/buckets/b.txt")
h.ReleaseHandle(7)
h.AcquireHandle(7, "/buckets/b.txt")
time.Sleep(60 * time.Millisecond)
kv.waitFor(t, 7, "/buckets/b.txt")
h.ReleaseHandle(7)
h.RememberPath(7, "/buckets/b.txt")
kv.waitFor(t, 7, "")
h.RememberPath(7, "/buckets/b.txt")
time.Sleep(20 * time.Millisecond)
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 TestExportedHandlesRemovalKeepsAnotherMountsRegistration(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
first := newExportedHandlesForTest(kv, 1, 10*time.Millisecond)
second := newExportedHandlesForTest(kv, 2, 10*time.Millisecond)
first.AcquireHandle(7, "/buckets/a.txt")
kv.waitFor(t, 7, "/buckets/a.txt")
second.AcquireHandle(7, "/buckets/a.txt")
kv.waitForRegistrations(t, 7, exportedHandleRegistration{1, "/buckets/a.txt"}, exportedHandleRegistration{2, "/buckets/a.txt"})
second.ReleaseHandle(7)
kv.waitFor(t, 7, "/buckets/a.txt")
first.ReleaseHandle(7)
kv.waitFor(t, 7, "")
first.AcquireHandle(8, "/buckets/b.txt")
kv.waitFor(t, 8, "/buckets/b.txt")
second.AcquireHandle(8, "/buckets/b.txt")
kv.waitForRegistrations(t, 8, exportedHandleRegistration{1, "/buckets/b.txt"}, exportedHandleRegistration{2, "/buckets/b.txt"})
first.ReleaseHandle(8)
kv.waitForRegistrations(t, 8, exportedHandleRegistration{2, "/buckets/b.txt"})
}
func TestExportedHandlesConcurrentMountsKeepBothRegistrations(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}, getDelay: 20 * time.Millisecond}
first := newExportedHandlesForTest(kv, 1, time.Minute)
second := newExportedHandlesForTest(kv, 2, time.Minute)
first.AcquireHandle(7, "/buckets/a.txt")
second.AcquireHandle(7, "/buckets/a.txt")
kv.waitUntil(t, 7, "registrations of both mounts", func(got []exportedHandleRegistration) bool {
return len(got) == 2
})
}
func TestExportedHandlesWriteWaitsForTheLock(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{
string(exportedHandleKey(7)): formatExportedHandle([]exportedHandleRegistration{{2, "/buckets/a.txt"}}),
}, lockErr: errors.New("no lock server")}
h := newExportedHandlesForTest(kv, 1, time.Minute)
h.AcquireHandle(7, "/buckets/a.txt")
time.Sleep(100 * time.Millisecond)
kv.waitForRegistrations(t, 7, exportedHandleRegistration{2, "/buckets/a.txt"})
kv.Lock()
kv.lockErr = nil
kv.Unlock()
kv.waitForRegistrations(t, 7, exportedHandleRegistration{2, "/buckets/a.txt"}, exportedHandleRegistration{1, "/buckets/a.txt"})
}
func TestExportedHandlesRestartedMountTakesOverItsRegistration(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
newExportedHandlesForTest(kv, 1, time.Minute).AcquireHandle(7, "/buckets/a.txt")
kv.waitFor(t, 7, "/buckets/a.txt")
newExportedHandlesForTest(kv, 2, time.Minute).AcquireHandle(7, "/buckets/a.txt")
kv.waitForRegistrations(t, 7, exportedHandleRegistration{1, "/buckets/a.txt"}, exportedHandleRegistration{2, "/buckets/a.txt"})
restarted := newExportedHandlesForTest(kv, 1, 10*time.Millisecond)
restarted.AcquireHandle(7, "/buckets/b.txt")
kv.waitForRegistrations(t, 7, exportedHandleRegistration{2, "/buckets/a.txt"}, exportedHandleRegistration{1, "/buckets/b.txt"})
restarted.ReleaseHandle(7)
kv.waitForRegistrations(t, 7, exportedHandleRegistration{2, "/buckets/a.txt"})
}
func TestExportedHandlesRecordKeepsNewestRegistrations(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
var want []exportedHandleRegistration
for owner := uint32(1); owner <= maxExportedHandleRegistrations+2; owner++ {
path := util.FullPath(fmt.Sprintf("/buckets/%d.txt", owner))
newExportedHandlesForTest(kv, owner, time.Minute).AcquireHandle(7, path)
want = append(want, exportedHandleRegistration{owner, path})
if len(want) > maxExportedHandleRegistrations {
want = want[1:]
}
kv.waitForRegistrations(t, 7, want...)
}
wfs := newDotLookupTestWFS("/buckets", true)
wfs.exportedHandles = newExportedHandlesForTest(kv, 99, time.Minute)
if path, status := wfs.exportedHandles.GetPath(context.Background(), 7, "/buckets"); status != fuse.OK || path != want[len(want)-1].path {
t.Fatalf("GetPath = %q, %v; want the newest registration %q", path, status, want[len(want)-1].path)
}
}
func TestExportedHandlesRegisterThePathARaceRenameMoved(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
h := newExportedHandlesForTest(kv, 1, time.Minute)
var mu sync.Mutex
current := util.FullPath("/buckets/new.txt")
h.currentPath = func(uint64) util.FullPath {
mu.Lock()
defer mu.Unlock()
return current
}
// The opener read the path before a rename moved it, and the rename found
// the handle before it was registered.
h.RememberPath(7, "/buckets/new.txt")
h.AcquireHandle(7, "/buckets/old.txt")
kv.waitFor(t, 7, "/buckets/new.txt")
// A later opener that read the old path cannot move the record back.
h.RememberPath(7, "/buckets/old.txt")
mu.Lock()
current = "/buckets/renamed.txt"
mu.Unlock()
h.RememberPath(7, "/buckets/renamed.txt")
kv.waitFor(t, 7, "/buckets/renamed.txt")
mu.Lock()
current = ""
mu.Unlock()
h.RememberPath(7, "/buckets/unlinked.txt")
kv.waitFor(t, 7, "/buckets/unlinked.txt")
}
func TestExportedHandlesGetPathSkipsRegistrationsOutsideTheRoot(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
newExportedHandlesForTest(kv, 1, time.Minute).AcquireHandle(7, "/a/x.txt")
kv.waitFor(t, 7, "/a/x.txt")
newExportedHandlesForTest(kv, 2, time.Minute).AcquireHandle(7, "/b/y.txt")
kv.waitForRegistrations(t, 7, exportedHandleRegistration{1, "/a/x.txt"}, exportedHandleRegistration{2, "/b/y.txt"})
h := newExportedHandlesForTest(kv, 3, time.Minute)
for root, want := range map[util.FullPath]util.FullPath{"/a": "/a/x.txt", "/b": "/b/y.txt", "/": "/b/y.txt"} {
if path, status := h.GetPath(context.Background(), 7, root); status != fuse.OK || path != want {
t.Fatalf("GetPath under %s = %q, %v; want %q", root, path, status, want)
}
}
if _, status := h.GetPath(context.Background(), 7, "/c"); status != fuse.Status(syscall.ESTALE) {
t.Fatalf("GetPath under /c: status %v, want ESTALE", status)
}
}
func TestResolveDotNameFromRecordOfEarlierMount(t *testing.T) {
kv := &fakeKv{values: map[string][]byte{}}
file := util.FullPath("/buckets/a/b/c.txt")
inode := file.AsInode(1)
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, 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")
}
if _, _, status := wfs.resolveDotName(context.Background(), inode+1, "."); status != fuse.Status(syscall.ESTALE) {
t.Fatalf("path outside the mount: status %v, want ESTALE", status)
}
if _, _, status := wfs.resolveDotName(context.Background(), inode+2, "."); status != fuse.Status(syscall.ESTALE) {
t.Fatalf("unrecorded inode: status %v, want ESTALE", status)
}
kv.err = errors.New("store unavailable")
if _, _, status := wfs.resolveDotName(context.Background(), inode+2, "."); status != fuse.EIO {
t.Fatalf("store error: status %v, want EIO", status)
}
}
func TestNewExportedHandlesOffWithFilerTokenFile(t *testing.T) {
wfs := newDotLookupTestWFS("/buckets", true)
if wfs.newExportedHandles() == nil {
t.Fatal("a mount without a filer token keeps no NFS handle records")
}
wfs.option.FilerTokenSource = func(context.Context) (security.EncodedJwt, error) { return "", nil }
if wfs.newExportedHandles() != nil {
t.Fatal("a mount with a filer token keeps NFS handle records the filer refuses")
}
}