diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index bb98c65805..6c2ceac9b7 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -315,11 +315,6 @@ func NewSeaweedFileSystem(option *Option) *WFS { wfs.attrValidSec = 10 } - if option.NfsExport { - wfs.inodeToPath.RetainForgotten(nfsExportForgottenInodes) - wfs.exportedHandles = wfs.newExportedHandles() - } - if option.EnableDistributedLock && !option.WritebackCache && len(option.FilerAddresses) > 0 { wfs.lockClient = cluster.NewLockClient(option.GrpcDialOption, option.FilerAddresses[0]) glog.V(0).Infof("distributed lock manager enabled for mount") @@ -327,6 +322,11 @@ func NewSeaweedFileSystem(option *Option) *WFS { glog.V(0).Infof("distributed lock manager disabled: writeback cache implies single-writer mode") } + if option.NfsExport { + wfs.inodeToPath.RetainForgotten(nfsExportForgottenInodes) + wfs.exportedHandles = wfs.newExportedHandles() + } + if option.AccessReport { wfs.accessReporter = newAccessReporter(wfs.touchAccessTime, option.AccessReportInterval) wfs.accessReporter.start() diff --git a/weed/mount/weedfs_nfs_export.go b/weed/mount/weedfs_nfs_export.go index 7a564c2f7e..a8393ec352 100644 --- a/weed/mount/weedfs_nfs_export.go +++ b/weed/mount/weedfs_nfs_export.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "errors" + "fmt" "os" "sync" "syscall" @@ -52,9 +53,10 @@ type exportedHandles struct { wake chan struct{} put func(key, value []byte) error get func(ctx context.Context, key []byte) ([]byte, error) + lock func(inode uint64) (unlock func()) } -func newExportedHandles(owner uint32, 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), lock func(inode uint64) (unlock func())) *exportedHandles { h := &exportedHandles{ owner: owner, paths: make(map[uint64]util.FullPath), @@ -65,6 +67,7 @@ func newExportedHandles(owner uint32, put func(key, value []byte) error, get fun wake: make(chan struct{}, 1), put: put, get: get, + lock: lock, } go h.loop() return h @@ -110,10 +113,12 @@ func (h *exportedHandles) loop() { } // write replaces this mount's registration of the inode with path, or drops -// it when path is empty, keeping those of other mounts. Mounts that update the -// same inode at the same moment can still overwrite each other's: the filer KV -// has no compare-and-swap. +// it when path is empty, keeping those of other mounts. The filer KV has no +// compare-and-swap, so mounts take turns on a record through the lock manager +// under -dlm; without it, mounts that update the same inode at the same moment +// can still overwrite each other's. func (h *exportedHandles) write(inode uint64, path util.FullPath) error { + defer h.lock(inode)() key := exportedHandleKey(inode) value, err := h.get(context.Background(), key) if err != nil { @@ -282,5 +287,14 @@ func (wfs *WFS) newExportedHandles() *exportedHandles { return nil }) return + }, func(inode uint64) func() { + if wfs.lockClient == nil { + return func() {} + } + lock := wfs.lockClient.NewShortLivedLock(fmt.Sprintf("%s%d", exportedHandleKeyPrefix, inode), fmt.Sprintf("mount-%d", wfs.signature)) + if lock == nil { + return func() {} + } + return func() { lock.StopShortLivedLock() } }) } diff --git a/weed/mount/weedfs_nfs_export_test.go b/weed/mount/weedfs_nfs_export_test.go index 4df7992c76..c6f43b7a5c 100644 --- a/weed/mount/weedfs_nfs_export_test.go +++ b/weed/mount/weedfs_nfs_export_test.go @@ -17,8 +17,15 @@ import ( type fakeKv struct { sync.Mutex - values map[string][]byte - err error + values map[string][]byte + err error + getDelay time.Duration + turn sync.Mutex +} + +func (kv *fakeKv) lock(uint64) func() { + kv.turn.Lock() + return kv.turn.Unlock } func (kv *fakeKv) put(key, value []byte) error { @@ -34,8 +41,10 @@ func (kv *fakeKv) put(key, value []byte) error { func (kv *fakeKv) get(_ context.Context, key []byte) ([]byte, error) { kv.Lock() - defer kv.Unlock() - return kv.values[string(key)], kv.err + 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, @@ -71,7 +80,7 @@ func (kv *fakeKv) waitUntil(t *testing.T, inode uint64, want any, done func([]ex } func newExportedHandlesForTest(kv *fakeKv, owner uint32, delay time.Duration) *exportedHandles { - h := newExportedHandles(owner, kv.put, kv.get) + h := newExportedHandles(owner, kv.put, kv.get, kv.lock) h.delay = delay return h } @@ -140,6 +149,17 @@ func TestExportedHandlesRemovalKeepsAnotherMountsRegistration(t *testing.T) { 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 TestExportedHandlesRestartedMountTakesOverItsRegistration(t *testing.T) { kv := &fakeKv{values: map[string][]byte{}} newExportedHandlesForTest(kv, 1, time.Minute).AcquireHandle(7, "/buckets/a.txt")