filer: escalate a fenced lookup only when the remote has the object

Refusing the lazy fetch before the remote stat sent every fenced lookup that
missed under a remote mount to the exclusive lock, so concurrent lookups of
one missing key ran their stats one after another instead of sharing one.
A lookup that may not write now stats in a flight of its own and asks for
the exclusive lock only when there is an object to import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-01 15:12:51 -08:00
1 parent a1e1ed6214
commit 9612333343
2 files changed
+62 -12

No files matched your search

+21 -12
View File
@@ -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
}
@@ -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)
}
}