Browse Source
feat(filer): add lazy directory listing for remote mounts (#8615)
feat(filer): add lazy directory listing for remote mounts (#8615)
* feat(filer): add lazy directory listing for remote mounts Directory listings on remote mounts previously only queried the local filer store. With lazy mounts the listing was empty; with eager mounts it went stale over time. Add on-demand directory listing that fetches from remote and caches results with a 5-minute TTL: - Add `ListDirectory` to `RemoteStorageClient` interface (delimiter-based, single-level listing, separate from recursive `Traverse`) - Implement in S3, GCS, and Azure backends using each platform's hierarchical listing API - Add `maybeLazyListFromRemote` to filer: before each directory listing, check if the directory is under a remote mount with an expired cache, fetch from remote, persist entries to the local store, then let existing listing logic run on the populated store - Use singleflight to deduplicate concurrent requests for the same directory - Skip local-only entries (no RemoteEntry) to avoid overwriting unsynced uploads - Errors are logged and swallowed (availability over consistency) * refactor: extract xattr key to constant xattrRemoteListingSyncedAt * feat: make listing cache TTL configurable per mount via listing_cache_ttl_seconds Add listing_cache_ttl_seconds field to RemoteStorageLocation protobuf. When 0 (default), lazy directory listing is disabled for that mount. When >0, enables on-demand directory listing with the specified TTL. Expose as -listingCacheTTL flag on remote.mount command. * refactor: address review feedback for lazy directory listing - Add context.Context to ListDirectory interface and all implementations - Capture startTime before remote call for accurate TTL tracking - Simplify S3 ListDirectory using ListObjectsV2PagesWithContext - Make maybeLazyListFromRemote return void (errors always swallowed) - Remove redundant trailing-slash path manipulation in caller - Update tests to match new signatures * When an existing entry has Remote != nil, we should merge remote metadata into it rather than replacing it. * fix(gcs): wrap ListDirectory iterator error with context The raw iterator error was returned without bucket/path context, making it harder to debug. Wrap it consistently with the S3 pattern. * fix(s3): guard against nil pointer dereference in Traverse and ListDirectory Some S3-compatible backends may return nil for LastModified, Size, or ETag fields. Check for nil before dereferencing to prevent panics. * fix(filer): remove blanket 2-minute timeout from lazy listing context Individual SDK operations (S3, GCS, Azure) already have per-request timeouts and retry policies. The blanket timeout could cut off large directory listings mid-operation even though individual pages were succeeding. * fix(filer): preserve trace context in lazy listing with WithoutCancel Use context.WithoutCancel(ctx) instead of context.Background() so trace/span values from the incoming request are retained for distributed tracing, while still decoupling cancellation. * fix(filer): use Store.FindEntry for internal lookups, add Uid/Gid to files, fix updateDirectoryListingSyncedAt - Use f.Store.FindEntry instead of f.FindEntry for staleness check and child lookups to avoid unnecessary lazy-fetch overhead - Set OS_UID/OS_GID on new file entries for consistency with directories - In updateDirectoryListingSyncedAt, use Store.UpdateEntry for existing directories instead of CreateEntry to avoid deleteChunksIfNotNew and NotifyUpdateEvent side effects * fix(filer): distinguish not-found from store errors in lazy listing Previously, any error from Store.FindEntry was treated as "not found," which could cause entry recreation/overwrite on transient DB failures. Now check for filer_pb.ErrNotFound explicitly and skip entries or bail out on real store errors. * refactor(filer): use errors.Is for ErrNotFound comparisonspull/8618/head
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 687 additions and 13 deletions
-
3weed/filer/filer.go
-
208weed/filer/filer_lazy_remote_listing.go
-
271weed/filer/filer_lazy_remote_test.go
-
1weed/pb/remote.proto
-
25weed/pb/remote_pb/remote.pb.go
-
62weed/remote_storage/azure/azure_storage_client.go
-
46weed/remote_storage/gcs/gcs_storage_client.go
-
2weed/remote_storage/remote_storage.go
-
78weed/remote_storage/s3/s3_storage_client.go
-
4weed/shell/command_remote_mount.go
@ -0,0 +1,208 @@ |
|||
package filer |
|||
|
|||
import ( |
|||
"context" |
|||
"errors" |
|||
"fmt" |
|||
"os" |
|||
"strconv" |
|||
"time" |
|||
|
|||
"github.com/seaweedfs/seaweedfs/weed/glog" |
|||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" |
|||
"github.com/seaweedfs/seaweedfs/weed/util" |
|||
) |
|||
|
|||
const xattrRemoteListingSyncedAt = "remote.listing.synced_at" |
|||
|
|||
type lazyListContextKey struct{} |
|||
|
|||
// maybeLazyListFromRemote populates the local filer store with entries from the
|
|||
// remote storage backend for directory p if the following conditions hold:
|
|||
// - p is under a remote mount with listing_cache_ttl_seconds > 0
|
|||
// - the cached listing has expired (based on the per-mount TTL)
|
|||
//
|
|||
// When listing_cache_ttl_seconds is 0 (the default), lazy listing is disabled
|
|||
// for that mount.
|
|||
//
|
|||
// On success it updates the directory's xattrRemoteListingSyncedAt extended
|
|||
// attribute so subsequent calls within the TTL window are no-ops.
|
|||
//
|
|||
// Errors are logged and swallowed (availability over consistency).
|
|||
func (f *Filer) maybeLazyListFromRemote(ctx context.Context, p util.FullPath) { |
|||
// Prevent recursion: CreateEntry → FindEntry → doListDirectoryEntries → here
|
|||
if ctx.Value(lazyListContextKey{}) != nil { |
|||
return |
|||
} |
|||
// Also respect the lazy-fetch guard to prevent mutual recursion
|
|||
if ctx.Value(lazyFetchContextKey{}) != nil { |
|||
return |
|||
} |
|||
|
|||
if f.RemoteStorage == nil { |
|||
return |
|||
} |
|||
|
|||
// The ptrie stores mount rules with trailing "/". When p is exactly the
|
|||
// mount directory (e.g. "/buckets/mybucket"), we must also try matching
|
|||
// with a trailing "/" so the trie recognizes the mount root.
|
|||
lookupPath := p |
|||
mountDir, remoteLoc := f.RemoteStorage.FindMountDirectory(lookupPath) |
|||
if remoteLoc == nil { |
|||
lookupPath = util.FullPath(string(p) + "/") |
|||
mountDir, remoteLoc = f.RemoteStorage.FindMountDirectory(lookupPath) |
|||
if remoteLoc == nil { |
|||
return |
|||
} |
|||
} |
|||
|
|||
// Lazy listing is opt-in: disabled when TTL is 0
|
|||
if remoteLoc.ListingCacheTtlSeconds <= 0 { |
|||
return |
|||
} |
|||
cacheTTL := time.Duration(remoteLoc.ListingCacheTtlSeconds) * time.Second |
|||
|
|||
// Check staleness: read the directory entry's extended attributes.
|
|||
// Use Store.FindEntry directly — we only need the local xattr, not lazy-fetch.
|
|||
dirEntry, _ := f.Store.FindEntry(ctx, p) |
|||
if dirEntry != nil { |
|||
if syncedAtStr, ok := dirEntry.Extended[xattrRemoteListingSyncedAt]; ok { |
|||
if syncedAt, err := strconv.ParseInt(string(syncedAtStr), 10, 64); err == nil { |
|||
if time.Since(time.Unix(syncedAt, 0)) < cacheTTL { |
|||
return |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
client, _, found := f.RemoteStorage.FindRemoteStorageClient(lookupPath) |
|||
if !found { |
|||
return |
|||
} |
|||
|
|||
key := "list:" + string(p) |
|||
f.lazyListGroup.Do(key, func() (interface{}, error) { |
|||
startTime := time.Now() |
|||
objectLoc := MapFullPathToRemoteStorageLocation(mountDir, remoteLoc, p) |
|||
|
|||
// Decouple from the caller's cancellation/deadline while preserving
|
|||
// trace/span values for distributed tracing.
|
|||
persistCtx := context.WithValue(context.WithoutCancel(ctx), lazyListContextKey{}, true) |
|||
persistCtx = context.WithValue(persistCtx, lazyFetchContextKey{}, true) |
|||
|
|||
listErr := client.ListDirectory(persistCtx, objectLoc, func(dir string, name string, isDirectory bool, remoteEntry *filer_pb.RemoteEntry) error { |
|||
childPath := p.Child(name) |
|||
|
|||
existingEntry, findErr := f.Store.FindEntry(persistCtx, childPath) |
|||
if findErr != nil && !errors.Is(findErr, filer_pb.ErrNotFound) { |
|||
glog.Warningf("maybeLazyListFromRemote: find %s: %v", childPath, findErr) |
|||
return nil // skip this entry on transient store error
|
|||
} |
|||
|
|||
// Skip entries that exist locally without a RemoteEntry (local-only uploads)
|
|||
if existingEntry != nil && existingEntry.Remote == nil { |
|||
return nil |
|||
} |
|||
|
|||
if existingEntry != nil { |
|||
// Merge: update remote metadata while preserving local state
|
|||
// (Chunks, Extended, Uid/Gid/Mode, etc.)
|
|||
existingEntry.Remote = remoteEntry |
|||
if !isDirectory && remoteEntry != nil { |
|||
if remoteEntry.RemoteMtime > 0 { |
|||
existingEntry.Attr.Mtime = time.Unix(remoteEntry.RemoteMtime, 0) |
|||
} |
|||
existingEntry.Attr.FileSize = uint64(remoteEntry.RemoteSize) |
|||
} |
|||
if saveErr := f.Store.UpdateEntry(persistCtx, existingEntry); saveErr != nil { |
|||
glog.Warningf("maybeLazyListFromRemote: update %s: %v", childPath, saveErr) |
|||
} |
|||
} else { |
|||
// New entry not yet in local store
|
|||
var entry *Entry |
|||
if isDirectory { |
|||
now := time.Now() |
|||
entry = &Entry{ |
|||
FullPath: childPath, |
|||
Attr: Attr{ |
|||
Mtime: now, |
|||
Crtime: now, |
|||
Mode: os.ModeDir | 0755, |
|||
Uid: OS_UID, |
|||
Gid: OS_GID, |
|||
}, |
|||
} |
|||
} else { |
|||
mtime := time.Now() |
|||
if remoteEntry != nil && remoteEntry.RemoteMtime > 0 { |
|||
mtime = time.Unix(remoteEntry.RemoteMtime, 0) |
|||
} |
|||
entry = &Entry{ |
|||
FullPath: childPath, |
|||
Attr: Attr{ |
|||
Mtime: mtime, |
|||
Crtime: mtime, |
|||
Mode: 0644, |
|||
Uid: OS_UID, |
|||
Gid: OS_GID, |
|||
}, |
|||
Remote: remoteEntry, |
|||
} |
|||
if remoteEntry != nil { |
|||
entry.Attr.FileSize = uint64(remoteEntry.RemoteSize) |
|||
} |
|||
} |
|||
if saveErr := f.CreateEntry(persistCtx, entry, false, false, nil, true, f.MaxFilenameLength); saveErr != nil { |
|||
glog.Warningf("maybeLazyListFromRemote: persist %s: %v", childPath, saveErr) |
|||
} |
|||
} |
|||
return nil |
|||
}) |
|||
if listErr != nil { |
|||
glog.Warningf("maybeLazyListFromRemote: list %s: %v", p, listErr) |
|||
return nil, nil // swallow error
|
|||
} |
|||
|
|||
// Update the synced_at timestamp on the directory entry
|
|||
f.updateDirectoryListingSyncedAt(persistCtx, p, startTime) |
|||
|
|||
return nil, nil |
|||
}) |
|||
} |
|||
|
|||
func (f *Filer) updateDirectoryListingSyncedAt(ctx context.Context, p util.FullPath, syncTime time.Time) { |
|||
dirEntry, findErr := f.Store.FindEntry(ctx, p) |
|||
if findErr != nil && !errors.Is(findErr, filer_pb.ErrNotFound) { |
|||
glog.Warningf("maybeLazyListFromRemote: find dir %s: %v", p, findErr) |
|||
return |
|||
} |
|||
if errors.Is(findErr, filer_pb.ErrNotFound) { |
|||
// Directory doesn't exist yet, create it
|
|||
now := time.Now() |
|||
dirEntry = &Entry{ |
|||
FullPath: p, |
|||
Attr: Attr{ |
|||
Mtime: now, |
|||
Crtime: now, |
|||
Mode: os.ModeDir | 0755, |
|||
Uid: OS_UID, |
|||
Gid: OS_GID, |
|||
}, |
|||
} |
|||
if dirEntry.Extended == nil { |
|||
dirEntry.Extended = make(map[string][]byte) |
|||
} |
|||
dirEntry.Extended[xattrRemoteListingSyncedAt] = []byte(fmt.Sprintf("%d", syncTime.Unix())) |
|||
if saveErr := f.CreateEntry(ctx, dirEntry, false, false, nil, true, f.MaxFilenameLength); saveErr != nil { |
|||
glog.Warningf("maybeLazyListFromRemote: create dir synced_at for %s: %v", p, saveErr) |
|||
} |
|||
return |
|||
} |
|||
if dirEntry.Extended == nil { |
|||
dirEntry.Extended = make(map[string][]byte) |
|||
} |
|||
dirEntry.Extended[xattrRemoteListingSyncedAt] = []byte(fmt.Sprintf("%d", syncTime.Unix())) |
|||
if saveErr := f.Store.UpdateEntry(ctx, dirEntry); saveErr != nil { |
|||
glog.Warningf("maybeLazyListFromRemote: update synced_at for %s: %v", p, saveErr) |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue