Files
seaweedfs/test/postgres_flat/recursive_listing_test.go
T
Chris LuandClaude Opus 5.5 f599f870a8 s3: list without a delimiter from one range of the filer store (#13263)
* s3: -s3.listRecursiveFromStore, on by default

Lets an operator keep recursive listings on the directory walk.

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

* s3: list without a delimiter from one range of the filer store

A bucket that never had versioning is listed with ListEntriesRecursive:
one read of the bucket's path range per page instead of one directory
listing per directory. A folder object's "<dir>/" key waits for its place in
key order, and the folder objects whose rows sort before the range (the one
the prefix names, those the marker ends inside of) are looked up. .uploads
is skipped; a .versions folder hands the page to the walk, as does a filer
that answers Unimplemented, which is then not asked again for a minute.

The test pages through both ways over the same tree, for prefixes, markers
and page sizes, and expects the same keys, sizes, ETags and markers.

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

* test/postgres_flat: a range-read gateway lists what the walking one does

A second S3 gateway with -listRecursiveFromStore=false runs on the same
filer. ListObjectsV2 and V1 pages agree for prefixes, markers and page sizes
over folder objects, a prefix object, an unfinished upload and +/-/0
neighbours. Listing 60 keys five directories deep scans the bucket table 2
times from the range read and 302 times from the walk.

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

* s3: range-read listings without the -s3.listRecursiveFromStore flag

A filer whose store cannot list a range answers Unimplemented and the
gateway already walks that bucket's directories instead, so the flag only
switched off a path that falls back by itself. The unit test forces the
walk through the decline cache, and the postgres_flat test checks pages
against the keys in byte order rather than against a second gateway.

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

* s3: step a range-read listing over the bucket's upload parts

The walk skips the bucket's .uploads folder as one entry, but a range read
from the bucket root streamed every part of every unfinished upload and
dropped them, one 1024-row window after another, on each first page.
Every path under .uploads/ sorts before ".uploads0", so the read resumes
there and looks up the one key with that name.

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

* s3: list a waiting folder object once a row passes its key

A range-read page queued every childless folder object until a file row
arrived, so max-keys=1 over a bucket of empty folders read the whole
bucket before answering.

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

* s3: forget a deleted bucket's recursive-listing decline

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

---------

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

116 lines
4.4 KiB
Go

package postgres_flat
import (
"context"
"fmt"
"sort"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/stretchr/testify/require"
)
// TestS3RecursiveListing lists through a gateway that reads one path range of
// the bucket's table per page, and expects the pages S3 returns: the keys under
// the prefix and past the marker, in byte order.
func TestS3RecursiveListing(t *testing.T) {
c := startCluster(t)
fast := c.s3Client()
ctx := context.Background()
bucket := "pgflat-recursive"
b := aws.String(bucket)
_, err := fast.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: b})
require.NoError(t, err)
keys := []string{"a+b", "a-b/x", "a/x", "a/y/z", "a0", "日本/x", "sp ace/%_!", "z", "m/", "m/c", "m+/c", "mk/", "p", "p/q"}
for i := 0; i < 60; i++ {
keys = append(keys, fmt.Sprintf("deep/acct-%03d/Contract/rec-%03d/2026/10/doc.pdf", i, i))
}
for _, k := range keys {
_, err := fast.PutObject(ctx, &s3.PutObjectInput{Bucket: b, Key: aws.String(k), Body: strings.NewReader("body of " + k)})
require.NoError(t, err, "put %s", k)
}
_, err = fast.CreateMultipartUpload(ctx, &s3.CreateMultipartUploadInput{Bucket: b, Key: aws.String("up/unfinished")})
require.NoError(t, err)
sorted := append([]string(nil), keys...)
sort.Strings(sorted)
want := func(prefix, startAfter string, maxKeys int) (pages [][]string) {
var page []string
for _, k := range sorted {
if !strings.HasPrefix(k, prefix) || k <= startAfter {
continue
}
if len(page) == maxKeys {
pages, page = append(pages, page), nil
}
page = append(page, k)
}
return append(pages, page)
}
listV2 := func(t *testing.T, client *s3.Client, prefix, startAfter string, maxKeys int32) (pages [][]string) {
t.Helper()
in := &s3.ListObjectsV2Input{Bucket: b, Prefix: aws.String(prefix), StartAfter: aws.String(startAfter), MaxKeys: aws.Int32(maxKeys)}
for {
require.Less(t, len(pages), 500, "listing did not terminate")
out, err := client.ListObjectsV2(ctx, in)
require.NoError(t, err)
var page []string
for _, o := range out.Contents {
page = append(page, aws.ToString(o.Key))
}
pages = append(pages, page)
if !aws.ToBool(out.IsTruncated) {
return pages
}
in.ContinuationToken = out.NextContinuationToken
}
}
listV1 := func(t *testing.T, client *s3.Client, prefix, marker string, maxKeys int32) (keys []string, truncated bool) {
t.Helper()
out, err := client.ListObjects(ctx, &s3.ListObjectsInput{Bucket: b, Prefix: aws.String(prefix), Marker: aws.String(marker), MaxKeys: aws.Int32(maxKeys)})
require.NoError(t, err)
for _, o := range out.Contents {
keys = append(keys, aws.ToString(o.Key))
}
return keys, aws.ToBool(out.IsTruncated)
}
all := listV2(t, fast, "", "", 1000)
require.Len(t, all, 1)
require.Len(t, all[0], len(keys), "every key once, no upload parts: %v", all[0])
for _, prefix := range []string{"", "a", "a/", "deep/", "deep/acct-01", "m", "m/", "p", "p/", "日", "sp ace/%_", "up/", "zz"} {
for _, maxKeys := range []int32{1, 7, 1000} {
require.Equal(t, want(prefix, "", int(maxKeys)), listV2(t, fast, prefix, "", maxKeys), "prefix %q max %d", prefix, maxKeys)
}
}
for _, marker := range []string{"a", "a+", "a+b", "a/", "a/y", "m", "m+", "m/", "mk", "p", "p/", "deep/acct-010", "日本"} {
for _, prefix := range []string{"", "m", "deep/"} {
wantPages := want(prefix, marker, 3)
require.Equal(t, wantPages, listV2(t, fast, prefix, marker, 3), "prefix %q start-after %q", prefix, marker)
gotKeys, gotTruncated := listV1(t, fast, prefix, marker, 3)
require.Equal(t, wantPages[0], gotKeys, "prefix %q marker %q", prefix, marker)
require.Equal(t, len(wantPages) > 1, gotTruncated, "prefix %q marker %q", prefix, marker)
}
}
t.Run("a page reads the bucket table a few times, not once per directory", func(t *testing.T) {
// A backend reports its table statistics when idle, at least every 10s.
scans := func() int {
time.Sleep(11 * time.Second)
_, err := c.db.Exec(`SELECT pg_stat_clear_snapshot()`)
require.NoError(t, err)
return c.count(t, `SELECT coalesce(idx_scan, 0) + coalesce(seq_scan, 0) FROM pg_stat_user_tables WHERE schemaname = current_schema() AND relname = $1`, bucket)
}
before := scans()
require.Len(t, listV2(t, fast, "deep/", "", 1000)[0], 60)
after := scans()
t.Logf("60 keys in 300 directories under deep/: %d scans", after-before)
require.LessOrEqual(t, after-before, 5)
})
}