diff --git a/weed/mount/weedfs_nfs_export.go b/weed/mount/weedfs_nfs_export.go index a8393ec352..fe75331fc3 100644 --- a/weed/mount/weedfs_nfs_export.go +++ b/weed/mount/weedfs_nfs_export.go @@ -53,10 +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()) + lock func(inode uint64) (unlock func(), err error) } -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 { +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(), err error)) *exportedHandles { h := &exportedHandles{ owner: owner, paths: make(map[uint64]util.FullPath), @@ -118,7 +118,11 @@ func (h *exportedHandles) loop() { // 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)() + unlock, err := h.lock(inode) + if err != nil { + return err + } + defer unlock() key := exportedHandleKey(inode) value, err := h.get(context.Background(), key) if err != nil { @@ -287,14 +291,15 @@ func (wfs *WFS) newExportedHandles() *exportedHandles { return nil }) return - }, func(inode uint64) func() { + }, func(inode uint64) (func(), error) { if wfs.lockClient == nil { - return func() {} + return func() {}, nil } - lock := wfs.lockClient.NewShortLivedLock(fmt.Sprintf("%s%d", exportedHandleKeyPrefix, inode), fmt.Sprintf("mount-%d", wfs.signature)) + key := fmt.Sprintf("%s%d", exportedHandleKeyPrefix, inode) + lock := wfs.lockClient.NewShortLivedLock(key, fmt.Sprintf("mount-%d", wfs.signature)) if lock == nil { - return func() {} + return nil, fmt.Errorf("lock %s", key) } - return func() { lock.StopShortLivedLock() } + return func() { lock.StopShortLivedLock() }, nil }) } diff --git a/weed/mount/weedfs_nfs_export_test.go b/weed/mount/weedfs_nfs_export_test.go index c6f43b7a5c..f215cb2300 100644 --- a/weed/mount/weedfs_nfs_export_test.go +++ b/weed/mount/weedfs_nfs_export_test.go @@ -20,12 +20,19 @@ type fakeKv struct { values map[string][]byte err error getDelay time.Duration + lockErr error turn sync.Mutex } -func (kv *fakeKv) lock(uint64) func() { +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 + return kv.turn.Unlock, nil } func (kv *fakeKv) put(key, value []byte) error { @@ -160,6 +167,21 @@ func TestExportedHandlesConcurrentMountsKeepBothRegistrations(t *testing.T) { }) } +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")