diff --git a/weed/filer/filer.go b/weed/filer/filer.go index 9a76b8d491..6b49ed83f9 100644 --- a/weed/filer/filer.go +++ b/weed/filer/filer.go @@ -1106,6 +1106,19 @@ var ( } ) +type lookupWritesContextKey struct{} + +// ErrLookupNeedsWrite is what a lookup made with WithoutLookupWrites returns +// in place of a write it would make: deleting an expired S3 object, or +// importing an object from a remote mount. +var ErrLookupNeedsWrite = errors.New("lookup needs a write") + +// WithoutLookupWrites makes FindEntry report a write it would make instead of +// making it, so a caller can redo the lookup under a lock that covers writes. +func WithoutLookupWrites(ctx context.Context) context.Context { + return context.WithValue(ctx, lookupWritesContextKey{}, true) +} + // FindEntry looks up the entry at p, serving a path the store does not hold // from a remote mount when one covers it. func (f *Filer) FindEntry(ctx context.Context, p util.FullPath) (entry *Entry, err error) { @@ -1134,6 +1147,9 @@ func (f *Filer) findEntry(ctx context.Context, p util.FullPath) (entry *Entry, f // lookup outside a transaction removes it. gone := false if ctx.Value(storeTransactionKey{}) == nil { + if ctx.Value(lookupWritesContextKey{}) != nil { + return nil, false, ErrLookupNeedsWrite + } if delErr := f.doDeleteEntryMetaAndData(ctx, entry, true, false, nil); delErr != nil { glog.ErrorfCtx(ctx, "FindEntry doDeleteEntryMetaAndData %s failed: %v", entry.FullPath, delErr) } else { @@ -1154,7 +1170,9 @@ func (f *Filer) findEntry(ctx context.Context, p util.FullPath) (entry *Entry, f } if entry == nil && (err == nil || errors.Is(err, filer_pb.ErrNotFound)) { - if lazy, lazyErr := f.maybeLazyFetchFromRemote(ctx, p); lazyErr != nil { + if lazy, lazyErr := f.maybeLazyFetchFromRemote(ctx, p); errors.Is(lazyErr, ErrLookupNeedsWrite) { + return nil, false, lazyErr + } else if lazyErr != nil { glog.V(1).InfofCtx(ctx, "FindEntry lazy fetch %s: %v", p, lazyErr) } else if lazy != nil { return lazy, false, nil diff --git a/weed/filer/filer_lazy_remote.go b/weed/filer/filer_lazy_remote.go index a1e3ccc1fd..66a18bd68f 100644 --- a/weed/filer/filer_lazy_remote.go +++ b/weed/filer/filer_lazy_remote.go @@ -69,6 +69,9 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( if !found { return nil, nil } + if ctx.Value(lookupWritesContextKey{}) != nil { + return nil, ErrLookupNeedsWrite + } relPath := strings.TrimPrefix(string(p), string(mountDir)) if relPath != "" && !strings.HasPrefix(relPath, "/") { diff --git a/weed/server/filer_grpc_server.go b/weed/server/filer_grpc_server.go index 9b7b89d7b9..2d9c16da4e 100644 --- a/weed/server/filer_grpc_server.go +++ b/weed/server/filer_grpc_server.go @@ -35,7 +35,14 @@ import ( // Any read whose result a client versions must go through here. func (fs *FilerServer) fencedFindEntry(ctx context.Context, path util.FullPath) (entry *filer.Entry, logTsNs int64, err error) { pathLock := fs.entryLockTable.AcquireLock("fencedFindEntry", path, util.SharedLock) - entry, err = fs.filer.FindEntry(ctx, path) + entry, err = fs.filer.FindEntry(filer.WithoutLookupWrites(ctx), path) + if errors.Is(err, filer.ErrLookupNeedsWrite) { + // A write under a shared lock could log its event between another + // reader's read and its fence; take the path to ourselves for it. + fs.entryLockTable.ReleaseLock(path, pathLock) + pathLock = fs.entryLockTable.AcquireLock("fencedFindEntry", path, util.ExclusiveLock) + entry, err = fs.filer.FindEntry(ctx, path) + } // After the read: it may itself log an event (a remote import, an expiry // delete) that the returned entry reflects. logTsNs = fs.filer.LogFenceTsNs() diff --git a/weed/server/filer_grpc_server_lookup_fence_test.go b/weed/server/filer_grpc_server_lookup_fence_test.go new file mode 100644 index 0000000000..bfc1c7b6f7 --- /dev/null +++ b/weed/server/filer_grpc_server_lookup_fence_test.go @@ -0,0 +1,78 @@ +package weed_server + +import ( + "context" + "errors" + "sync/atomic" + "testing" + "time" + + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" + "github.com/seaweedfs/seaweedfs/weed/util" +) + +// One lookup reads an object just before it expires; a second finds it +// expired and deletes it. The first must not return the object under a fence +// above that delete, or a client skips the delete as already reflected. +func TestLookupFenceNotAboveAConcurrentExpiryDelete(t *testing.T) { + fs, store := newTxnTestServer(t, nil) + const path = "/buckets/bkt/obj" + live := newFileEntry(path, 1) + live.TtlSec = 3600 + live.Mtime = time.Now() + live.Extended = map[string][]byte{s3_constants.SeaweedFSExpiresS3: []byte("true")} + store.entries[path] = live + + var first atomic.Bool + firstRead := make(chan struct{}) + release := make(chan struct{}) + store.afterFind = func(p util.FullPath) { + if p != path || !first.CompareAndSwap(false, true) { + return + } + // the object expires right after this read + store.mu.Lock() + expired := live.ShallowClone() + expired.Mtime = time.Now().Add(-2 * time.Hour) + store.entries[path] = expired + store.mu.Unlock() + close(firstRead) + select { + case <-release: + case <-time.After(time.Second): + } + } + + lookup := func() (*filer_pb.LookupDirectoryEntryResponse, error) { + return fs.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{Directory: "/buckets/bkt", Name: "obj"}) + } + type result struct { + resp *filer_pb.LookupDirectoryEntryResponse + err error + } + firstDone := make(chan result, 1) + go func() { + resp, err := lookup() + firstDone <- result{resp, err} + }() + <-firstRead + secondDone := make(chan result, 1) + go func() { + resp, err := lookup() + close(release) + secondDone <- result{resp, err} + }() + a, b := <-firstDone, <-secondDone + + if !errors.Is(b.err, filer_pb.ErrNotFound) { + t.Fatalf("second lookup: %v, want the expired object deleted", b.err) + } + if _, found := store.entries[path]; found { + t.Fatal("expired object still stored") + } + deletePos := fs.filer.LocalMetaLogBuffer.LastTsNs.Load() + if a.err == nil && a.resp.Entry != nil && a.resp.LogTsNs >= deletePos { + t.Fatalf("first lookup returned the object under fence %d, at or above its delete at %d", a.resp.LogTsNs, deletePos) + } +}