mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-08-21 14:46:58 +00:00
* s3: stop warming every bucket's config at startup Listing all buckets in BucketRegistry.init() made S3 gateway startup O(buckets) and pinned every bucket's metadata and config resident, which does not scale past a few hundred thousand buckets. Both caches already have lazy miss paths, so load on first access instead and let the metadata subscription refresh only entries already resident; cold buckets cost one filer round-trip on their first request. * s3: bound the per-bucket caches with LRU eviction The bucket config cache, bucket registry, and their negative caches were plain maps that only ever grew: the config cache TTL made Get miss but never evicted the entry, and the not-found sets grew on every probe of a nonexistent bucket name. Cap all four at 65536 entries with LRU eviction so a gateway keeps its hot working set and evicted buckets reload from the filer on next access. * s3: cache parsed bucket config instead of the full filer entry Each cached BucketConfig retained the whole bucket entry (extended attribute map plus raw content bytes) alongside the fields parsed from it, roughly doubling per-bucket cache cost and keeping data the read path never looks at. Parse everything up front in newBucketConfigFromEntry - now also the creator identity, tags, encryption config, and stored lifecycle XML - and drop the entry. updateBucketConfig now reads the entry fresh from the filer and diffs the mapped extended attributes against it, so the patch is computed against current state instead of a cached copy; the config clone helpers that existed for that path go away. * s3: dedup cold bucket-registry loads per bucket The registry's notFound lock doubled as the load serializer, holding one global mutex across the filer round-trip so first-touch requests for different buckets queued behind each other; the cache fill also happened after the lock was released, so two concurrent misses for the same bucket could both reach the filer. Replace it with a singleflight per bucket that fills the cache inside the flight: different buckets load concurrently, the same bucket loads once.
112 lines
4.3 KiB
Go
112 lines
4.3 KiB
Go
package s3api
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetBucketLifecycleConfigurationHandlerUsesStoredLifecycleConfig(t *testing.T) {
|
|
const bucket = "cleanup-test-net"
|
|
const lifecycleXML = `<?xml version="1.0" encoding="UTF-8"?><LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Rule><Filter></Filter><ID>rotation</ID><Expiration><Days>1</Days></Expiration><Status>Enabled</Status></Rule></LifecycleConfiguration>`
|
|
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, nil)
|
|
s3a.option = &S3ApiServerOption{BucketsPath: "/buckets"}
|
|
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
|
|
s3a.bucketConfigCache.Set(bucket, &BucketConfig{
|
|
Name: bucket,
|
|
LifecycleXML: []byte(lifecycleXML),
|
|
LifecycleTransitionMinSize: "varies_by_storage_class",
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/"+bucket+"?lifecycle", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"bucket": bucket})
|
|
resp := httptest.NewRecorder()
|
|
|
|
s3a.GetBucketLifecycleConfigurationHandler(resp, req)
|
|
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
assert.Equal(t, "varies_by_storage_class", resp.Header().Get(bucketLifecycleTransitionMinimumObjectSizeHeader))
|
|
assert.Equal(t, lifecycleXML, resp.Body.String())
|
|
}
|
|
|
|
func TestGetBucketLifecycleConfigurationHandlerDefaultsTransitionMinimumObjectSize(t *testing.T) {
|
|
const bucket = "cleanup-test-net"
|
|
const lifecycleXML = `<?xml version="1.0" encoding="UTF-8"?><LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Rule><Filter></Filter><ID>rotation</ID><Expiration><Days>1</Days></Expiration><Status>Enabled</Status></Rule></LifecycleConfiguration>`
|
|
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, nil)
|
|
s3a.option = &S3ApiServerOption{BucketsPath: "/buckets"}
|
|
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
|
|
s3a.bucketConfigCache.Set(bucket, &BucketConfig{
|
|
Name: bucket,
|
|
LifecycleXML: []byte(lifecycleXML),
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/"+bucket+"?lifecycle", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"bucket": bucket})
|
|
resp := httptest.NewRecorder()
|
|
|
|
s3a.GetBucketLifecycleConfigurationHandler(resp, req)
|
|
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
assert.Equal(t, defaultLifecycleTransitionMinimumObjectSize, resp.Header().Get(bucketLifecycleTransitionMinimumObjectSizeHeader))
|
|
assert.Equal(t, lifecycleXML, resp.Body.String())
|
|
}
|
|
|
|
func TestPutBucketLifecycleConfigurationHandlerRejectsOversizedBody(t *testing.T) {
|
|
const bucket = "cleanup-test-net"
|
|
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, nil)
|
|
s3a.option = &S3ApiServerOption{BucketsPath: "/buckets"}
|
|
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
|
|
s3a.bucketConfigCache.Set(bucket, &BucketConfig{Name: bucket})
|
|
|
|
req := httptest.NewRequest(http.MethodPut, "/"+bucket+"?lifecycle", strings.NewReader(strings.Repeat("x", maxBucketLifecycleConfigurationSize+1)))
|
|
req = mux.SetURLVars(req, map[string]string{"bucket": bucket})
|
|
resp := httptest.NewRecorder()
|
|
|
|
s3a.PutBucketLifecycleConfigurationHandler(resp, req)
|
|
|
|
require.Equal(t, s3err.GetAPIError(s3err.ErrEntityTooLarge).HTTPStatusCode, resp.Code)
|
|
assert.Contains(t, resp.Body.String(), "<Code>EntityTooLarge</Code>")
|
|
}
|
|
|
|
func TestPutBucketLifecycleConfigurationHandlerMapsReadErrorsToInvalidRequest(t *testing.T) {
|
|
const bucket = "cleanup-test-net"
|
|
|
|
s3a := newTestS3ApiServerWithMemoryIAM(t, nil)
|
|
s3a.option = &S3ApiServerOption{BucketsPath: "/buckets"}
|
|
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
|
|
s3a.bucketConfigCache.Set(bucket, &BucketConfig{Name: bucket})
|
|
|
|
req := httptest.NewRequest(http.MethodPut, "/"+bucket+"?lifecycle", nil)
|
|
req = mux.SetURLVars(req, map[string]string{"bucket": bucket})
|
|
req.Body = failingReadCloser{err: errors.New("read failed")}
|
|
resp := httptest.NewRecorder()
|
|
|
|
s3a.PutBucketLifecycleConfigurationHandler(resp, req)
|
|
|
|
require.Equal(t, s3err.GetAPIError(s3err.ErrInvalidRequest).HTTPStatusCode, resp.Code)
|
|
assert.Contains(t, resp.Body.String(), "<Code>InvalidRequest</Code>")
|
|
}
|
|
|
|
type failingReadCloser struct {
|
|
err error
|
|
}
|
|
|
|
func (f failingReadCloser) Read(_ []byte) (int, error) {
|
|
return 0, f.err
|
|
}
|
|
|
|
func (f failingReadCloser) Close() error {
|
|
return nil
|
|
}
|