Files
upstream-seaweedfs/weed/filer/filechunk_manifest_cache_test.go
upstream-mirror 89f5be005d
Some checks failed
go: build dev binaries / cleanup (push) Successful in 4s
Codespell / Check for spelling errors (push) Failing after 3s
docker: build dev containers / build-rust-binaries (amd64, x86_64-unknown-linux-musl) (push) Failing after 3s
docker: build dev containers / build-dev-containers (push) Has been skipped
docker: build dev containers / build-rust-binaries (arm64, true, aarch64-unknown-linux-musl) (push) Failing after 3s
End to End / FUSE Mount (push) Failing after 2s
EC Integration Tests / EC Integration Tests (push) Failing after 30s
FUSE DLM Integration Tests / FUSE DLM Integration Tests (push) Failing after 3s
FUSE Volume Server Failover Tests / FUSE Volume Server Failover (push) Failing after 3s
FUSE Integration Tests / FUSE Integration Testing (push) Failing after 2s
FUSE P2P Peer Chunk Sharing Integration Tests / FUSE P2P Peer Chunk Sharing (push) Failing after 3s
go: build binary / Go Vet (push) Failing after 2s
go: build binary / Go Vet 32-bit (push) Failing after 3s
go: build binary / Build (push) Failing after 4s
go: build binary / Build cross-platform (freebsd/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/amd64) (push) Failing after 3s
go: build binary / Build cross-platform (darwin/arm64) (push) Failing after 3s
go: build binary / Build cross-platform (windows/arm64) (push) Failing after 3s
go: build binary / Test (push) Failing after 2s
go: build binary / Test 32-bit (push) Failing after 3s
helm: lint and test charts / lint-test (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (11) (push) Failing after 3s
Java Client Integration Tests / Java Integration Tests (17) (push) Failing after 3s
go: build dev binaries / build_dev_linux_windows (amd64, linux) (push) Failing after 2s
go: build dev binaries / build_dev_linux_windows (amd64, windows) (push) Failing after 3s
go: build dev binaries / build_dev_darwin (amd64, darwin) (push) Has been skipped
go: build dev binaries / build_dev_darwin (arm64, darwin) (push) Has been skipped
Java Client Unit Tests / Java Unit Tests (11) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (17) (push) Failing after 2s
Java Client Unit Tests / Java Unit Tests (21) (push) Failing after 3s
Java Client Unit Tests / Java Unit Tests (8) (push) Failing after 3s
Kafka Quick Test (Load Test with Schema Registry) / Kafka Client Load Test (Quick) (push) Failing after 3s
Kafka Gateway Tests / Kafka Unit Tests (unit-tests-1) (push) Failing after 11s
Kafka Gateway Tests / Kafka Integration Tests (Critical) (integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka End-to-End Tests (with SMQ) (e2e-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Consumer Group Tests (Highly Isolated) (consumer-group-1) (push) Failing after 2s
Kafka Gateway Tests / Kafka Client Compatibility (with SMQ) (client-compat-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka SMQ Integration Tests (Full Stack) (smq-integration-1) (push) Failing after 3s
Kafka Gateway Tests / Kafka Protocol Tests (Isolated) (protocol-1) (push) Failing after 3s
KMS Tests / KMS Provider Integration Tests (push) Failing after 2s
KMS Tests / S3 KMS End-to-End Tests (push) Failing after 3s
Master Cold Start Tests / Master Cold Start Tests (push) Failing after 4s
Metadata Subscribe Integration Tests / Metadata Subscribe Integration Tests (push) Failing after 3s
Multi-Master Tests / Multi-Master Failover Tests (push) Failing after 3s
Performance / CPU and Heap Profile (push) Failing after 2s
Performance / S3 Read/Write Benchmark (rust volume) (push) Failing after 3s
pjdfstest POSIX Compliance / pjdfstest (push) Failing after 2s
Plugin Worker Integration Tests / Plugin Worker: erasure_coding (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: vacuum (push) Failing after 3s
Rust Volume Server Tests / Rust Integration Tests (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 1) (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 1) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 2) (push) Failing after 3s
Rust Volume Server Tests / Go Tests with Rust Volume (grpc - Shard 3) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Distributed Tests (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Performance Tests (push) Failing after 3s
Performance / Throughput Benchmark (go volume) (push) Failing after 3s
Performance / Throughput Benchmark (rust volume) (push) Failing after 2s
Performance / S3 Read/Write Benchmark (go volume) (push) Failing after 3s
Plugin Worker Integration Tests / Plugin Worker: volume_balance (push) Failing after 3s
PostgreSQL Gateway Tests / PostgreSQL Basic Tests (push) Failing after 3s
Rust Volume Server Tests / Rust Unit Tests (push) Failing after 2s
Rust Volume Server Tests / Go Tests with Rust Volume (http - Shard 3) (push) Failing after 3s
Rust Plugin Worker Tests / Rust Plugin Worker Build and Unit Tests (push) Failing after 3s
rust: build dev volume server binaries / cleanup (push) Successful in 3s
S3 ETag and ACL Tests / S3 ETag + ACL Integration Tests (push) Failing after 3s
S3 IAM Integration Tests / IAM Unit Tests (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (advanced) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (basic) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (group) (push) Failing after 3s
S3 IAM Integration Tests / S3 IAM Integration Tests (policy-enforcement) (push) Failing after 2s
S3 IAM Integration Tests / S3 IAM Integration Tests (sts) (push) Failing after 4s
S3 Keycloak Integration Tests / S3 Keycloak Integration Tests (push) Failing after 3s
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.11) (push) Failing after 3s
S3 PyArrow Parquet Tests / Go Unit Tests (Implicit Directory) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.12) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-amd64, x86_64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-darwin (darwin-arm64, aarch64-apple-darwin) (push) Has been cancelled
rust: build dev volume server binaries / build-rust-volume-dev-linux (linux-amd64, x86_64-unknown-linux-gnu) (push) Has been cancelled
S3 PyArrow Parquet Tests / PyArrow Parquet Tests (Python 3.9) (push) Has been cancelled
S3 Proxy Signature Tests / S3 Proxy Signature Verification Tests (push) Has been cancelled
S3 SDK V2 Route Disambiguation Tests / S3 SDK V2 Routing Tests (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (quick) (push) Has been cancelled
S3 SSE Tests / S3 SSE Compatibility Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Metadata Persistence Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Copy Operations Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Multipart Upload Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Performance Test (push) Has been cancelled
S3 SSE Tests / S3 Volume Encryption Test (push) Has been cancelled
S3 SSE Tests / S3 SSE Integration Tests (comprehensive) (push) Has been cancelled
Spark Integration Tests / Spark Integration Tests (push) Failing after 2s
Telemetry Integration Tests / telemetry-integration-test (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 1) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 1) (push) Failing after 3s
Ceph S3 tests / Basic S3 tests (KV store) (push) Has been cancelled
Ceph S3 tests / S3 Versioning & Object Lock tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Unit Tests (push) Failing after 3s
Ceph S3 tests / S3 CORS tests (push) Has been cancelled
Ceph S3 tests / SeaweedFS Custom S3 Copy tests (push) Has been cancelled
Ceph S3 tests / Basic S3 tests (SQL store) (push) Has been cancelled
test s3 over https using aws-cli / awscli-tests (push) Has been cancelled
TLS Rotation Integration Tests / TLS Rotation Integration Tests (push) Has been cancelled
Vacuum Integration Tests / Vacuum Integration Tests (push) Has been cancelled
S3 Policy Integration Tests / S3 Policy Variables Integration Tests (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (advanced-policy) (push) Failing after 3s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (basic-policy) (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Enforcement Integration Tests (contextual-policy) (push) Failing after 4s
S3 Policy Integration Tests / Trusted Proxy Detection Tests (push) Failing after 2s
S3 Policy Integration Tests / S3 Policy Shell Integration Tests (push) Failing after 3s
Samba on FUSE Integration / samba-integration (push) Failing after 2s
SFTP Integration Tests / SFTP Integration Testing (push) Failing after 3s
terraform: validate and test modules / fmt, validate, plan-level tests (push) Failing after 3s
terraform: validate and test modules / local cluster smoke test (real weed) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 3) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (grpc - Shard 2) (push) Failing after 3s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 2) (push) Failing after 2s
Volume Server Integration Tests / Volume Server Integration Tests (http - Shard 3) (push) Failing after 5s
mount: windows conformance / WinFsp conformance (push) Has been cancelled
mount: windows / Mount on Windows (push) Has been cancelled
snapshot of https://github.com/seaweedfs/seaweedfs.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 03:23:42 +05:00

306 lines
11 KiB
Go

package filer
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)
func newManifestCacheTestChunk(fileID string) *filer_pb.FileChunk {
return resolveTestManifest(fileID, 0)
}
func newTestManifestCache(t testing.TB) *ChunkManifestCache {
t.Helper()
cache := NewChunkManifestCache(MaxMountChunkManifestCacheEntries, MaxMountChunkManifestCacheBytes)
t.Cleanup(cache.clear)
return cache
}
func TestChunkGroupManifestResolutionCachesRepeatedOpens(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-repeated-open"
fixture := newManifestReadFixture(t, map[string][]*filer_pb.FileChunk{
manifestID: {resolveTestData("cached-data", 0)},
}, nil)
var lookups atomic.Int32
lookup := func(ctx context.Context, fileID string) ([]string, error) {
lookups.Add(1)
return fixture.lookup(ctx, fileID)
}
for i := 0; i < 3; i++ {
_, err := NewChunkGroup(lookup, nil, []*filer_pb.FileChunk{
newManifestCacheTestChunk(manifestID),
}, 1, nil, cache)
require.NoError(t, err)
}
require.Equal(t, int32(1), lookups.Load(), "repeated Mount opens should reuse the Manifest lookup")
require.Equal(t, int32(1), fixture.loads.Load(), "repeated Mount opens should reuse the Manifest fetch")
}
func TestChunkGroupManifestResolutionDoesNotCacheFailedReads(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-failed-read"
fixture := newManifestReadFixture(t, nil, nil)
var lookups atomic.Int32
lookup := func(ctx context.Context, fileID string) ([]string, error) {
lookups.Add(1)
return fixture.lookup(ctx, fileID)
}
chunk := newManifestCacheTestChunk(manifestID)
_, err := NewChunkGroup(lookup, nil, []*filer_pb.FileChunk{chunk}, 1, nil, cache)
require.Error(t, err)
fixture.manifests[manifestID] = manifestBytes(t, resolveTestData("retried-data", 0))
_, err = NewChunkGroup(lookup, nil, []*filer_pb.FileChunk{chunk}, 1, nil, cache)
require.NoError(t, err)
require.Equal(t, int32(2), lookups.Load(), "a failed lookup must not poison later Mount opens")
require.Equal(t, int32(2), fixture.loads.Load(), "a failed fetch must not poison later Mount opens")
}
func TestChunkGroupManifestResolutionConcurrentWarmOpensReuseFetch(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-concurrent-warm-open"
fixture := newManifestReadFixture(t, map[string][]*filer_pb.FileChunk{
manifestID: {resolveTestData("concurrent-data", 0)},
}, nil)
var lookups atomic.Int32
lookup := func(ctx context.Context, fileID string) ([]string, error) {
lookups.Add(1)
return fixture.lookup(ctx, fileID)
}
_, err := NewChunkGroup(lookup, nil, []*filer_pb.FileChunk{
newManifestCacheTestChunk(manifestID),
}, 1, nil, cache)
require.NoError(t, err)
const concurrentOpens = 8
var wg sync.WaitGroup
errs := make(chan error, concurrentOpens)
for i := 0; i < concurrentOpens; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, openErr := NewChunkGroup(lookup, nil, []*filer_pb.FileChunk{
newManifestCacheTestChunk(manifestID),
}, 1, nil, cache)
errs <- openErr
}()
}
wg.Wait()
close(errs)
for openErr := range errs {
require.NoError(t, openErr)
}
require.Equal(t, int32(1), lookups.Load(), "warm concurrent Mount opens should not repeat the lookup")
require.Equal(t, int32(1), fixture.loads.Load(), "warm concurrent Mount opens should not repeat the fetch")
}
func TestChunkGroupManifestResolutionCoalescesColdMisses(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-cold-miss"
// Delay the manifest response so the leader's fetch is still in flight
// when the other concurrent opens arrive and join the singleflight.
fixture := newManifestReadFixture(t, map[string][]*filer_pb.FileChunk{
manifestID: {resolveTestData("cold-data", 0)},
}, map[string]time.Duration{manifestID: 50 * time.Millisecond})
var lookups atomic.Int32
lookup := func(ctx context.Context, fileID string) ([]string, error) {
lookups.Add(1)
return fixture.lookup(ctx, fileID)
}
const concurrentOpens = 8
var wg sync.WaitGroup
errs := make(chan error, concurrentOpens)
for i := 0; i < concurrentOpens; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, openErr := NewChunkGroup(lookup, nil, []*filer_pb.FileChunk{
newManifestCacheTestChunk(manifestID),
}, 1, nil, cache)
errs <- openErr
}()
}
wg.Wait()
close(errs)
for openErr := range errs {
require.NoError(t, openErr)
}
require.Equal(t, int32(1), lookups.Load(), "concurrent cold opens should coalesce into a single lookup")
require.Equal(t, int32(1), fixture.loads.Load(), "concurrent cold opens should coalesce into a single fetch")
}
func TestResolveOneChunkManifestDoesNotUseMountCache(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-public-resolver"
fixture := newManifestReadFixture(t, map[string][]*filer_pb.FileChunk{
manifestID: {resolveTestData("public-resolver-data", 0)},
}, nil)
var lookups atomic.Int32
lookup := func(ctx context.Context, fileID string) ([]string, error) {
lookups.Add(1)
return fixture.lookup(ctx, fileID)
}
chunk := newManifestCacheTestChunk(manifestID)
for i := 0; i < 2; i++ {
_, err := ResolveOneChunkManifest(context.Background(), lookup, chunk, nil)
require.NoError(t, err)
}
require.Equal(t, int32(2), lookups.Load(), "the general resolver must retain its uncached behavior")
require.Equal(t, int32(2), fixture.loads.Load(), "the general resolver must not use the Mount cache")
// The shared cache must remain empty: a nil-cache caller must not pollute
// a mount-owned cache, and vice versa.
_, found := cache.get(chunkManifestCacheKey{fileID: manifestID})
require.False(t, found, "a nil-cache resolve must not populate an unrelated cache")
}
func TestChunkGroupManifestResolutionDoesNotCacheMalformedManifest(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-malformed-manifest"
fixture := newManifestReadFixture(t, map[string][]*filer_pb.FileChunk{
manifestID: nil,
}, nil)
fixture.manifests[manifestID] = []byte("malformed manifest")
chunk := newManifestCacheTestChunk(manifestID)
_, err := NewChunkGroup(fixture.lookup, nil, []*filer_pb.FileChunk{chunk}, 1, nil, cache)
require.Error(t, err)
fixture.manifests[manifestID] = manifestBytes(t, resolveTestData("after-malformed-data", 0))
_, err = NewChunkGroup(fixture.lookup, nil, []*filer_pb.FileChunk{chunk}, 1, nil, cache)
require.NoError(t, err)
require.Equal(t, int32(2), fixture.loads.Load(), "a malformed manifest must not poison later Mount opens")
}
func TestChunkManifestCacheSeparatesReadParameters(t *testing.T) {
cache := NewChunkManifestCache(4, 1024)
base := chunkManifestCacheKey{fileID: "same-file"}
cache.put(base, []byte("manifest"))
_, found := cache.get(chunkManifestCacheKey{fileID: "same-file", cipherKey: "key"})
require.False(t, found, "cipher keys must be part of the cache key")
_, found = cache.get(chunkManifestCacheKey{fileID: "same-file", isCompressed: true})
require.False(t, found, "compression settings must be part of the cache key")
_, found = cache.get(chunkManifestCacheKey{fileID: "different-file"})
require.False(t, found, "FileIds must be part of the cache key")
}
func TestChunkManifestCacheCopiesData(t *testing.T) {
cache := NewChunkManifestCache(1, 1024)
key := chunkManifestCacheKey{fileID: "copy-data"}
original := []byte("manifest")
cache.put(key, original)
original[0] = 'X'
data, found := cache.get(key)
require.True(t, found)
require.Equal(t, []byte("manifest"), data)
data[0] = 'Y'
data, found = cache.get(key)
require.True(t, found)
require.Equal(t, []byte("manifest"), data)
}
func TestChunkManifestCacheEvictsLeastRecentlyUsedAndOversizedEntries(t *testing.T) {
cache := NewChunkManifestCache(2, 6)
first := chunkManifestCacheKey{fileID: "first"}
second := chunkManifestCacheKey{fileID: "second"}
third := chunkManifestCacheKey{fileID: "third"}
oversized := chunkManifestCacheKey{fileID: "oversized"}
cache.put(first, []byte("one"))
cache.put(second, []byte("two"))
_, found := cache.get(first)
require.True(t, found, "the first entry should be present before eviction")
cache.put(third, []byte("tri"))
_, found = cache.get(first)
require.True(t, found, "a recent entry should survive LRU eviction")
_, found = cache.get(second)
require.False(t, found, "the least recently used entry should be evicted")
_, found = cache.get(third)
require.True(t, found)
cache.put(oversized, []byte("too large"))
_, found = cache.get(oversized)
require.False(t, found, "an entry over the byte limit must not be cached")
}
func TestResolveOneChunkManifestHonorsCanceledContextOnCacheHit(t *testing.T) {
cache := NewChunkManifestCache(1, 1024)
chunk := newManifestCacheTestChunk("cache-canceled-hit")
cache.put(chunkManifestCacheKey{fileID: chunk.GetFileIdString()}, manifestBytes(t, resolveTestData("canceled-data", 0)))
ctx, cancel := context.WithCancel(context.Background())
cancel()
lookupCalled := false
lookup := func(context.Context, string) ([]string, error) {
lookupCalled = true
return nil, errors.New("lookup should not be called")
}
_, err := resolveOneChunkManifest(ctx, lookup, chunk, nil, cache)
require.ErrorIs(t, err, context.Canceled)
require.False(t, lookupCalled, "a canceled cache hit must not issue a lookup")
}
func TestResolveOneChunkManifestCanceledWaiterReturnsDuringCoalescedMiss(t *testing.T) {
cache := newTestManifestCache(t)
const manifestID = "cache-canceled-waiter"
// Delay the manifest response so the leader's fetch is still in flight
// when the waiter arrives and cancels.
fixture := newManifestReadFixture(t, map[string][]*filer_pb.FileChunk{
manifestID: {resolveTestData("canceled-waiter-data", 0)},
}, map[string]time.Duration{manifestID: 100 * time.Millisecond})
chunk := newManifestCacheTestChunk(manifestID)
// Leader starts the fetch with a live context.
leaderCtx, leaderCancel := context.WithCancel(context.Background())
defer leaderCancel()
leaderDone := make(chan error, 1)
go func() {
_, err := resolveOneChunkManifest(leaderCtx, fixture.lookup, chunk, nil, cache)
leaderDone <- err
}()
// Waiter cancels its own context while the leader's fetch is still in
// flight; it must return context.Canceled promptly instead of blocking
// for the leader's result.
waiterCtx, waiterCancel := context.WithCancel(context.Background())
waiterCancel()
_, err := resolveOneChunkManifest(waiterCtx, fixture.lookup, chunk, nil, cache)
require.ErrorIs(t, err, context.Canceled)
// The leader must still complete successfully and populate the cache.
require.NoError(t, <-leaderDone)
data, found := cache.get(chunkManifestCacheKey{fileID: manifestID})
require.True(t, found, "the leader's successful fetch must populate the cache")
require.NotEmpty(t, data)
}
func manifestBytes(t testing.TB, chunks ...*filer_pb.FileChunk) []byte {
t.Helper()
data, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: chunks})
require.NoError(t, err)
return data
}