diff --git a/weed/filer/filer_lazy_remote.go b/weed/filer/filer_lazy_remote.go index 66a18bd68f..4171e3d525 100644 --- a/weed/filer/filer_lazy_remote.go +++ b/weed/filer/filer_lazy_remote.go @@ -69,10 +69,6 @@ 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, "/") { relPath = "/" + relPath @@ -87,16 +83,23 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( } type lazyFetchResult struct { - entry *Entry + entry *Entry + needsWrite bool } + // A caller that may not write only stats, in a flight of its own, so a + // miss on the remote is still answered once for all concurrent lookups. + noWrites := ctx.Value(lookupWritesContextKey{}) != nil key := string(p) + if noWrites { + key = "stat:" + key + } val, err, _ := f.lazyFetchGroup.Do(key, func() (interface{}, error) { buildCtx := context.WithoutCancel(ctx) client, clientErr := f.buildRemoteStorageClient(buildCtx, remoteConf) if clientErr != nil { glog.V(1).InfofCtx(ctx, "maybeLazyFetchFromRemote: reject %s: %v", p, clientErr) - return lazyFetchResult{nil}, nil + return lazyFetchResult{}, nil } remoteEntry, statErr := client.StatFile(objectLoc) if statErr != nil { @@ -105,11 +108,14 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( } else { glog.Warningf("maybeLazyFetchFromRemote: stat %s failed: %v", p, statErr) } - return lazyFetchResult{nil}, nil + return lazyFetchResult{}, nil } if remoteEntry == nil { glog.V(3).InfofCtx(ctx, "maybeLazyFetchFromRemote: %s StatFile returned nil entry", p) - return lazyFetchResult{nil}, nil + return lazyFetchResult{}, nil + } + if noWrites { + return lazyFetchResult{needsWrite: true}, nil } mtime := time.Unix(remoteEntry.RemoteMtime, 0) @@ -132,7 +138,7 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( // the fetched object cannot resurrect a path whose delete is pending. if f.isRemoteDeletionPending(persistCtx, p, mountDir) { glog.V(2).InfofCtx(ctx, "maybeLazyFetchFromRemote: %s deleted during remote stat", p) - return lazyFetchResult{nil}, nil + return lazyFetchResult{}, nil } if ctx.Value(lazyParentsContextKey{}) == nil { f.fetchMissingRemoteParents(persistBaseCtx, mountDir, p) @@ -141,7 +147,7 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( if saveErr != nil { glog.Warningf("maybeLazyFetchFromRemote: failed to persist filer entry for %s: %v", p, saveErr) f.lazyFetchGroup.Forget(key) - return lazyFetchResult{entry}, nil + return lazyFetchResult{entry: entry}, nil } // A delete records its tombstone before removing the entry, so a @@ -151,10 +157,10 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( glog.V(2).InfofCtx(ctx, "maybeLazyFetchFromRemote: %s deleted while persisting", p) f.lazyFetchGroup.Forget(key) f.retractLazyRemoteEntry(persistCtx, entry) - return lazyFetchResult{nil}, nil + return lazyFetchResult{}, nil } - return lazyFetchResult{entry}, nil + return lazyFetchResult{entry: entry}, nil }) if err != nil { return nil, err @@ -164,6 +170,9 @@ func (f *Filer) maybeLazyFetchFromRemote(ctx context.Context, p util.FullPath) ( if !ok { return nil, fmt.Errorf("maybeLazyFetchFromRemote: unexpected singleflight result type %T for %s", val, p) } + if result.needsWrite { + return nil, ErrLookupNeedsWrite + } return result.entry, nil } diff --git a/weed/server/filer_grpc_server_lookup_fence_test.go b/weed/server/filer_grpc_server_lookup_fence_test.go index bfc1c7b6f7..f873dd9eaf 100644 --- a/weed/server/filer_grpc_server_lookup_fence_test.go +++ b/weed/server/filer_grpc_server_lookup_fence_test.go @@ -3,11 +3,14 @@ package weed_server import ( "context" "errors" + "strings" "sync/atomic" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb" + "github.com/seaweedfs/seaweedfs/weed/remote_storage" "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" "github.com/seaweedfs/seaweedfs/weed/util" ) @@ -76,3 +79,41 @@ func TestLookupFenceNotAboveAConcurrentExpiryDelete(t *testing.T) { t.Fatalf("first lookup returned the object under fence %d, at or above its delete at %d", a.resp.LogTsNs, deletePos) } } + +// slowMissingRemoteClient answers every stat after a delay with not found. +type slowMissingRemoteClient struct { + remote_storage.RemoteStorageClient + stats atomic.Int32 +} + +func (c *slowMissingRemoteClient) StatFile(*remote_pb.RemoteStorageLocation) (*filer_pb.RemoteEntry, error) { + c.stats.Add(1) + time.Sleep(300 * time.Millisecond) + return nil, remote_storage.ErrRemoteObjectNotFound +} + +// Concurrent lookups of a key missing both locally and on the remote mount +// share one remote stat: nothing is imported, so none needs the path to itself. +func TestLookupsOfRemoteMissShareOneStat(t *testing.T) { + fs, _, _ := newRemoteMountTestServer(t) + client := &slowMissingRemoteClient{} + fs.filer.BuildGuardedRemoteClient = func(context.Context, *remote_pb.RemoteConf, bool) (remote_storage.RemoteStorageClient, error) { + return client, nil + } + const lookups = 8 + errs := make(chan error, lookups) + for i := 0; i < lookups; i++ { + go func() { + _, err := fs.LookupDirectoryEntry(context.Background(), &filer_pb.LookupDirectoryEntryRequest{Directory: "/buckets/b", Name: "missing.bin"}) + errs <- err + }() + } + for i := 0; i < lookups; i++ { + if err := <-errs; err == nil || !strings.Contains(err.Error(), filer_pb.ErrNotFound.Error()) { + t.Fatalf("lookup: %v, want not found", err) + } + } + if got := client.stats.Load(); got != 1 { + t.Fatalf("%d remote stats for %d concurrent lookups of one missing key, want 1", got, lookups) + } +}