From 997f72c59dcd755ec9505dfcbbf4a5c1452274ec Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 25 Nov 2026 23:34:36 -0800 Subject: [PATCH] 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 --- weed/mount/weedfs_nfs_export.go | 10 +++++++++- weed/mount/weedfs_nfs_export_test.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/weed/mount/weedfs_nfs_export.go b/weed/mount/weedfs_nfs_export.go index 812cdcffd7..7a564c2f7e 100644 --- a/weed/mount/weedfs_nfs_export.go +++ b/weed/mount/weedfs_nfs_export.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "errors" + "os" "sync" "syscall" "time" @@ -249,8 +250,15 @@ func (h *exportedHandles) GetPath(ctx context.Context, inode uint64) (util.FullP return registrations[len(registrations)-1].path, fuse.OK } +// newExportedHandles names the mount's registrations after its host and mount +// directory, so a mount restarted in the same place takes over, and in time +// removes, those its predecessor left. func (wfs *WFS) newExportedHandles() *exportedHandles { - return newExportedHandles(uint32(wfs.signature), func(key, value []byte) error { + owner := uint32(wfs.signature) + if hostname, err := os.Hostname(); err == nil { + owner = uint32(util.HashToInt32([]byte(hostname + ":" + wfs.option.MountDirectory))) + } + return newExportedHandles(owner, 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 { diff --git a/weed/mount/weedfs_nfs_export_test.go b/weed/mount/weedfs_nfs_export_test.go index ba4bbbcea7..4df7992c76 100644 --- a/weed/mount/weedfs_nfs_export_test.go +++ b/weed/mount/weedfs_nfs_export_test.go @@ -140,6 +140,20 @@ func TestExportedHandlesRemovalKeepsAnotherMountsRegistration(t *testing.T) { kv.waitForRegistrations(t, 8, exportedHandleRegistration{2, "/buckets/b.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