mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
perf(s3tables): read bucket policy from the listing (#12949)
* s3tables: read bucket policy from the listing ListTableBuckets looked up every listed bucket again to read its policy, which is already on the listed entry: one extra filer lookup per bucket. Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com * s3tables: cancel the list test helper's context The handler stops reading the filer listing once a page is full, so a call made with a context that is never cancelled leaves that stream open until the connection closes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3tables: drop the list tests' note about master Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
4 files changed
+218
-10
No files matched your search
@@ -192,16 +192,9 @@ func (h *S3TablesHandler) handleListTableBuckets(w http.ResponseWriter, r *http.
|
||||
continue
|
||||
}
|
||||
|
||||
current, err := filer_pb.LookupEntry(r.Context(), client, &filer_pb.LookupDirectoryEntryRequest{
|
||||
Directory: TablesPath,
|
||||
Name: entry.Entry.Name,
|
||||
})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
bucketPolicy := string(current.Entry.Extended[ExtendedKeyPolicy])
|
||||
bucketPolicy := string(entry.Entry.Extended[ExtendedKeyPolicy])
|
||||
var bucketTags map[string]string
|
||||
if tagData, ok := current.Entry.Extended[ExtendedKeyTags]; ok && bucketPolicy != "" {
|
||||
if tagData, ok := entry.Entry.Extended[ExtendedKeyTags]; ok && bucketPolicy != "" {
|
||||
if err := json.Unmarshal(tagData, &bucketTags); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package s3tables
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// ListTableBuckets checks every listed bucket against its own policy. The
|
||||
// policy is stored on the bucket entry the listing already returns, so the
|
||||
// check needs no further filer round trip per bucket.
|
||||
|
||||
const (
|
||||
listPerfReader = "reader"
|
||||
listPerfOther = "stranger"
|
||||
)
|
||||
|
||||
var listPerfAllowReader = fmt.Sprintf(`{"Statement":[{"Effect":"Allow","Principal":%q,"Action":"s3tables:*","Resource":"*"}]}`, listPerfReader)
|
||||
|
||||
// seedListPerfBuckets stores n table buckets owned by DefaultAccountID, each
|
||||
// with a policy that grants listPerfReader access.
|
||||
func seedListPerfBuckets(tb testing.TB, fs *s3tablestest.MemFiler, n int) {
|
||||
tb.Helper()
|
||||
for i := 0; i < n; i++ {
|
||||
name := fmt.Sprintf("perf-%05d", i)
|
||||
meta, err := json.Marshal(tableBucketMetadata{Name: name, OwnerAccountID: DefaultAccountID, CreatedAt: time.Unix(1700000000, 0)})
|
||||
require.NoError(tb, err)
|
||||
fs.Put(TablesPath, name, map[string][]byte{
|
||||
ExtendedKeyTableBucket: []byte("{}"),
|
||||
ExtendedKeyMetadata: meta,
|
||||
ExtendedKeyPolicy: []byte(listPerfAllowReader),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func listTableBucketsAs(tb testing.TB, m *Manager, fs *s3tablestest.MemFiler, caller string, maxBuckets int) ListTableBucketsResponse {
|
||||
tb.Helper()
|
||||
// The handler stops reading the filer's listing once the page is full;
|
||||
// cancelling releases that unfinished stream after each call.
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
var resp ListTableBucketsResponse
|
||||
require.NoError(tb, m.Execute(ctx, NewManagerClient(fs.Client), "ListTableBuckets",
|
||||
&ListTableBucketsRequest{MaxBuckets: maxBuckets}, &resp, caller))
|
||||
return resp
|
||||
}
|
||||
|
||||
// TestListTableBucketsLooksUpNoBucket: a page of 100 buckets is served by the
|
||||
// listing alone, not by one more filer lookup per bucket.
|
||||
func TestListTableBucketsLooksUpNoBucket(t *testing.T) {
|
||||
fs := s3tablestest.Start(t)
|
||||
seedListPerfBuckets(t, fs, 100)
|
||||
m := NewManager()
|
||||
|
||||
var lookups atomic.Int64
|
||||
fs.OnLookup = func(string, string) { lookups.Add(1) }
|
||||
resp := listTableBucketsAs(t, m, fs, listPerfReader, 100)
|
||||
fs.OnLookup = nil
|
||||
|
||||
require.Len(t, resp.TableBuckets, 100)
|
||||
require.Zero(t, lookups.Load(), "filer lookups while listing one page of 100 buckets")
|
||||
}
|
||||
|
||||
// BenchmarkListTableBuckets lists one page of 100 buckets out of n through
|
||||
// the in-memory filer over gRPC. lookupDelay is added to every
|
||||
// LookupDirectoryEntry to stand for a filer whose store is a network hop away
|
||||
// (a SQL or KV backend). lookups/op is the number of filer lookups one
|
||||
// listing makes.
|
||||
func BenchmarkListTableBuckets(b *testing.B) {
|
||||
for _, n := range []int{100, 1000} {
|
||||
for _, lookupDelay := range []time.Duration{0, 500 * time.Microsecond} {
|
||||
b.Run(fmt.Sprintf("buckets=%d/lookupDelay=%s", n, lookupDelay), func(b *testing.B) {
|
||||
fs := s3tablestest.Start(b)
|
||||
seedListPerfBuckets(b, fs, n)
|
||||
m := NewManager()
|
||||
var lookups atomic.Int64
|
||||
fs.OnLookup = func(string, string) {
|
||||
lookups.Add(1)
|
||||
if lookupDelay > 0 {
|
||||
time.Sleep(lookupDelay)
|
||||
}
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if resp := listTableBucketsAs(b, m, fs, listPerfReader, 100); len(resp.TableBuckets) != 100 {
|
||||
b.Fatalf("listed %d buckets, want 100", len(resp.TableBuckets))
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
b.ReportMetric(float64(lookups.Load())/float64(b.N), "lookups/op")
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package s3tables
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func listedBucketNames(resp ListTableBucketsResponse) []string {
|
||||
names := make([]string, 0, len(resp.TableBuckets))
|
||||
for _, b := range resp.TableBuckets {
|
||||
names = append(names, b.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// TestListTableBucketsStillAppliesEachBucketPolicy: the owner sees every
|
||||
// bucket but one its policy denies, a grantee sees the buckets whose policy
|
||||
// grants it, and an unrelated account sees none.
|
||||
func TestListTableBucketsStillAppliesEachBucketPolicy(t *testing.T) {
|
||||
fs := s3tablestest.Start(t)
|
||||
seedListPerfBuckets(t, fs, 3)
|
||||
m := NewManager()
|
||||
m.SetDefaultAllow(false)
|
||||
|
||||
deny := `{"Statement":[{"Effect":"Deny","Principal":"*","Action":"s3tables:GetTableBucket","Resource":"*"}]}`
|
||||
fs.Get(TablesPath, "perf-00001").Extended[ExtendedKeyPolicy] = []byte(deny)
|
||||
delete(fs.Get(TablesPath, "perf-00002").Extended, ExtendedKeyPolicy)
|
||||
|
||||
names := func(caller string) []string {
|
||||
return listedBucketNames(listTableBucketsAs(t, m, fs, caller, 100))
|
||||
}
|
||||
require.Equal(t, []string{"perf-00000", "perf-00002"}, names(DefaultAccountID))
|
||||
require.Equal(t, []string{"perf-00000"}, names(listPerfReader))
|
||||
require.Empty(t, names(listPerfOther))
|
||||
}
|
||||
|
||||
// TestListTableBucketsPagesThroughAllBuckets: continuation tokens walk every
|
||||
// bucket once, in name order, a full page at a time.
|
||||
func TestListTableBucketsPagesThroughAllBuckets(t *testing.T) {
|
||||
fs := s3tablestest.Start(t)
|
||||
seedListPerfBuckets(t, fs, 250)
|
||||
m := NewManager()
|
||||
|
||||
var all []string
|
||||
var pages []int
|
||||
token := ""
|
||||
for {
|
||||
var resp ListTableBucketsResponse
|
||||
require.NoError(t, m.Execute(context.Background(), NewManagerClient(fs.Client), "ListTableBuckets",
|
||||
&ListTableBucketsRequest{MaxBuckets: 100, ContinuationToken: token}, &resp, listPerfReader))
|
||||
all = append(all, listedBucketNames(resp)...)
|
||||
pages = append(pages, len(resp.TableBuckets))
|
||||
token = resp.ContinuationToken
|
||||
if token == "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
require.Equal(t, []int{100, 100, 50}, pages)
|
||||
require.Len(t, all, 250)
|
||||
for i, name := range all {
|
||||
require.Equal(t, fmt.Sprintf("perf-%05d", i), name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListTableBucketsAppliesPrefix: only names starting with the prefix are
|
||||
// listed.
|
||||
func TestListTableBucketsAppliesPrefix(t *testing.T) {
|
||||
fs := s3tablestest.Start(t)
|
||||
seedListPerfBuckets(t, fs, 25)
|
||||
m := NewManager()
|
||||
|
||||
var resp ListTableBucketsResponse
|
||||
require.NoError(t, m.Execute(context.Background(), NewManagerClient(fs.Client), "ListTableBuckets",
|
||||
&ListTableBucketsRequest{Prefix: "perf-0001"}, &resp, DefaultAccountID))
|
||||
var want []string
|
||||
for i := 10; i < 20; i++ {
|
||||
want = append(want, fmt.Sprintf("perf-%05d", i))
|
||||
}
|
||||
require.Equal(t, want, listedBucketNames(resp))
|
||||
}
|
||||
|
||||
// TestListTableBucketsSkipsWhatIsNotABucket: hidden directories, directories
|
||||
// without the table bucket marker, files and buckets whose metadata does not
|
||||
// decode are left out.
|
||||
func TestListTableBucketsSkipsWhatIsNotABucket(t *testing.T) {
|
||||
fs := s3tablestest.Start(t)
|
||||
seedListPerfBuckets(t, fs, 1)
|
||||
valid := fs.Get(TablesPath, "perf-00000").Extended
|
||||
fs.Put(TablesPath, ".hidden", valid)
|
||||
fs.Put(TablesPath, "plain-dir", map[string][]byte{ExtendedKeyMetadata: valid[ExtendedKeyMetadata]})
|
||||
fs.PutFile(TablesPath, "a-file", time.Now())
|
||||
fs.Put(TablesPath, "bad-metadata", map[string][]byte{
|
||||
ExtendedKeyTableBucket: []byte("{}"),
|
||||
ExtendedKeyMetadata: []byte("{not json"),
|
||||
ExtendedKeyPolicy: []byte(listPerfAllowReader),
|
||||
})
|
||||
m := NewManager()
|
||||
|
||||
require.Equal(t, []string{"perf-00000"}, listedBucketNames(listTableBucketsAs(t, m, fs, DefaultAccountID, 100)))
|
||||
}
|
||||
|
||||
// TestListTableBucketsRejectsTooManyBuckets: MaxBuckets above 1000 is a
|
||||
// client error.
|
||||
func TestListTableBucketsRejectsTooManyBuckets(t *testing.T) {
|
||||
fs := s3tablestest.Start(t)
|
||||
m := NewManager()
|
||||
err := m.Execute(context.Background(), NewManagerClient(fs.Client), "ListTableBuckets",
|
||||
&ListTableBucketsRequest{MaxBuckets: 1001}, nil, DefaultAccountID)
|
||||
requireS3TablesError(t, err, ErrCodeInvalidRequest)
|
||||
}
|
||||
@@ -250,7 +250,7 @@ func (f *MemFiler) Ping(_ context.Context, _ *filer_pb.PingRequest) (*filer_pb.P
|
||||
return &filer_pb.PingResponse{StartTimeNs: now, RemoteTimeNs: now, StopTimeNs: now}, nil
|
||||
}
|
||||
|
||||
func Start(t *testing.T) *MemFiler {
|
||||
func Start(t testing.TB) *MemFiler {
|
||||
t.Helper()
|
||||
fs := newMemFiler()
|
||||
|
||||
|
||||
Reference in new issue
Block a user