Files
seaweedfs/weed/filer/filer_list_recursive_test.go
T
Chris LuandClaude Opus 5.5 acb7728915 filer: ListEntriesRecursive RPC (#13262)
* filer: name the check that a listing expires an entry

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: StreamListRecursiveEntries lists a subtree in path order

It pages through the store's RecursiveLister until the limit, leaves out
and deletes expired entries as a directory listing does, and declines a
subtree in or holding a remote mount.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer.proto: ListEntriesRecursive

A new RPC rather than a field on ListEntries: a filer that predates it
answers Unimplemented instead of listing one directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: serve ListEntriesRecursive

Unimplemented when the store cannot list the subtree in path order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: a scoped token is refused ListEntriesRecursive

Classified with TraverseBfsMetadata: it lists a whole subtree, past the
per-directory checks a scoped ListEntries gets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK

* filer: trim a trailing slash before checking a recursive listing for remote mounts

An empty directory lists from the root.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* filer: keep the recursive listing cursor when a page comes back empty

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 12:04:27 -08:00

158 lines
5.8 KiB
Go

package filer
import (
"context"
"fmt"
"sort"
"strings"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/remote_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/stretchr/testify/require"
)
// recursiveStubStore lists the stub's entries in path order.
type recursiveStubStore struct {
*stubFilerStore
calls int
}
func (s *recursiveStubStore) ListRecursivePrefixedEntries(ctx context.Context, dirPath, startPath util.FullPath, prefix string, limit int64, eachEntryFunc ListEachEntryFunc) (lastPath util.FullPath, err error) {
s.calls++
from := strings.TrimSuffix(string(dirPath), "/") + "/" + prefix
s.mu.Lock()
var paths []string
for p := range s.entries {
if strings.HasPrefix(p, from) && p > string(startPath) {
paths = append(paths, p)
}
}
s.mu.Unlock()
sort.Strings(paths)
for _, p := range paths {
if limit <= 0 {
break
}
limit--
lastPath = util.FullPath(p)
s.mu.Lock()
entry := s.entries[p]
s.mu.Unlock()
if more, err := eachEntryFunc(entry); err != nil || !more {
return lastPath, err
}
}
return lastPath, nil
}
func TestStreamListRecursiveEntries(t *testing.T) {
ctx := context.Background()
store := &recursiveStubStore{stubFilerStore: newStubFilerStore()}
f := &Filer{Store: NewFilerStoreWrapper(store)}
now := time.Now()
var want []string
for i := 0; i < 2*PaginationSize+5; i++ {
p := fmt.Sprintf("/buckets/b/d%02d/f%05d", i%7, i)
store.entries[p] = &Entry{FullPath: util.FullPath(p), Attr: Attr{Mode: 0644, Crtime: now, Mtime: now}}
want = append(want, p)
}
expired := "/buckets/b/d00/expired"
store.entries[expired] = &Entry{FullPath: util.FullPath(expired), Attr: Attr{Mode: 0644, Crtime: now.Add(-time.Hour), Mtime: now, TtlSec: 60}}
sort.Strings(want)
list := func(t *testing.T, start, prefix string, limit int64, stopAt int) ([]string, util.FullPath) {
t.Helper()
var got []string
last, err := f.StreamListRecursiveEntries(ctx, "/buckets/b", util.FullPath(start), prefix, limit, func(e *Entry) (bool, error) {
got = append(got, string(e.FullPath))
return len(got) != stopAt, nil
})
require.NoError(t, err)
return got, last
}
t.Run("pages through the store until the limit", func(t *testing.T) {
store.calls = 0
got, last := list(t, "", "", 1<<20, -1)
require.Equal(t, want, got)
require.Equal(t, util.FullPath(want[len(want)-1]), last)
require.Equal(t, 3, store.calls, "two full pages and a short one")
_, found := store.entries[expired]
require.False(t, found, "the expired entry is deleted")
got, last = list(t, "", "", 10, -1)
require.Equal(t, want[:10], got)
require.Equal(t, util.FullPath(want[9]), last)
})
t.Run("resumes after a path, within a prefix", func(t *testing.T) {
got, _ := list(t, want[100], "d03/", 1<<20, -1)
var expect []string
for _, p := range want[101:] {
if strings.HasPrefix(p, "/buckets/b/d03/") {
expect = append(expect, p)
}
}
require.Equal(t, expect, got)
})
t.Run("a trailing slash lists the same subtree", func(t *testing.T) {
got, _ := list(t, "", "", 1<<20, -1)
var withSlash []string
_, err := f.StreamListRecursiveEntries(ctx, "/buckets/b/", "", "", 1<<20, func(e *Entry) (bool, error) {
withSlash = append(withSlash, string(e.FullPath))
return true, nil
})
require.NoError(t, err)
require.Equal(t, got, withSlash)
})
t.Run("a callback that stops ends at its entry", func(t *testing.T) {
got, last := list(t, "", "", 1<<20, 5)
require.Equal(t, want[:5], got)
require.Equal(t, util.FullPath(want[4]), last)
})
t.Run("an empty last page keeps the cursor", func(t *testing.T) {
exact := &recursiveStubStore{stubFilerStore: newStubFilerStore()}
var last string
for i := 0; i < PaginationSize; i++ {
last = fmt.Sprintf("/buckets/e/f%05d", i)
exact.entries[last] = &Entry{FullPath: util.FullPath(last), Attr: Attr{Mode: 0644, Crtime: now, Mtime: now}}
}
count := 0
lastPath, err := (&Filer{Store: NewFilerStoreWrapper(exact)}).StreamListRecursiveEntries(ctx, "/buckets/e", "", "", 1<<20, func(*Entry) (bool, error) {
count++
return true, nil
})
require.NoError(t, err)
require.Equal(t, PaginationSize, count)
require.Equal(t, 2, exact.calls)
require.Equal(t, util.FullPath(last), lastPath)
})
t.Run("declined", func(t *testing.T) {
rs := NewFilerRemoteStorage()
rs.mapDirectoryToRemoteStorage("/buckets/b/d01", &remote_pb.RemoteStorageLocation{Name: "r"})
f.RemoteStorage = rs
_, err := f.StreamListRecursiveEntries(ctx, "/buckets/b", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a remote mount below")
_, err = f.StreamListRecursiveEntries(ctx, "/buckets/b/d01/x", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "inside a remote mount")
_, err = f.StreamListRecursiveEntries(ctx, "/buckets/b/", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a remote mount below a directory with a trailing slash")
_, err = f.StreamListRecursiveEntries(ctx, "", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a remote mount below an empty directory")
f.RemoteStorage = nil
wrapper := NewFilerStoreWrapper(store)
wrapper.AddPathSpecificStore("/other/", "other", newStubFilerStore())
_, err = (&Filer{Store: wrapper}).StreamListRecursiveEntries(ctx, "/buckets/b", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "path-specific stores")
_, err = (&Filer{Store: NewFilerStoreWrapper(newStubFilerStore())}).StreamListRecursiveEntries(ctx, "/buckets/b", "", "", 10, func(*Entry) (bool, error) { return true, nil })
require.ErrorIs(t, err, ErrUnsupportedRecursiveListing, "a store without the capability")
})
}