Files
seaweedfs/weed/filer/reader_cache_destroy_budget_test.go
T
Yip LeoandClaude Opus 5.5 64cc7f5142 filer: break two reader cache budget deadlocks that wedge mount reads (#12950)
* filer: free a closed file's chunks for a download waiting on the budget

A weed mount stopped answering reads for three days. Its goroutine dump:
351 downloads waited in ReaderCacheBudget.reserve, all 31 releases sat in
SingleChunkCacher.destroy waiting for one of them, all 100 reads waited for
one of them as well, and a few goroutines spun in reserve's eviction loop.
The stuck reads filled the 128 FUSE background slots, so every read of the
mount blocked.

Closing a file runs ReaderCache.destroy, which takes the file's chunks out
of the map and then destroys them one at a time. When it reaches a download
waiting for budget first, it waits for that download, and the budget the
download needs can be held by the file's other chunks, which are out of the
map as well:
- Eviction skips a finished chunk that is out of the map. removeUnpinned
  refuses it, so reserve puts it back on the idle list and retries at once.
  When a stream is positioned in it, remove() does nothing, so reserve only
  takes it off the idle list.
- A download that fails is never evictable, and a download destroy()
  cancels fails too. remove() does nothing for one that is out of the map,
  so its reservation stays until destroy() reaches it.

Since #12616, destroy() cancels the waiting download when nothing reads
it. It does not when a stream is positioned in the download because the
read that started it gave up, for example because its process was killed:
closing a ChunkGroup does not release the streams.

Eviction now destroys a chunk that is out of its file's map instead of
skipping it. Whoever took it out destroys it too, and destroy() is safe to
run twice. A download that fails gives its reservation back at once, also
when it is out of the map.

TestReaderCacheDestroyWhileDownloadWaitsForBudget closes a file while one
of its downloads waits for budget, 30 rounds per case. When the waiting
download is a prefetch nobody read, the test passes on master. When a
stream is positioned in it, it hangs on master, both when the downloads
holding the budget are prefetches the close cancels (the release fixes
this) and when streams are positioned in them too (the eviction fixes
this). TestReaderCacheBudgetStress runs reads (some given up midway),
prefetches, failing downloads and close/reopen on four files that share a
budget of four chunks; it hangs in 10 of 10 -race runs on master.

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

* filer: do not wait for a dropped download while holding another chunk

A read registers on its new chunk and only then drops the chunk its stream
left (unpin, removeConsumed). When that chunk's download had not finished,
removeConsumed cancelled it and then waited for it in destroy(), still
holding the new chunk. The download could be evicting that very chunk for
budget: reserve falls back to a chunk a stream is positioned in, and
destroying it waits for its reads, this one included. Neither went on.

A dropped chunk whose download has ended is still destroyed at once. One
still downloading, cancelled when it was dropped, is destroyed by a
goroutine once the download gives up, so the read goes on.

TestReaderCacheBudgetStreamsStress moves eight streams between four chunks
under a budget of two and gives up a third of the reads midway. In 20 -race
runs it hangs 4 times on master and 8 times with only the previous commit,
in this cycle; with this change it does not hang.

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 20:10:59 -08:00

389 lines
12 KiB
Go

package filer
import (
"context"
"errors"
"math/rand"
"os"
"runtime/pprof"
"sync"
"sync/atomic"
"testing"
"time"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
)
// budgetTestViews returns one whole-chunk view per file id. The views lie past
// the mock chunk cache's 1 MiB cutoff, so prefetches do not land in it: the
// mock keeps the downloader's pooled buffer rather than a copy, and that buffer
// is reused once the chunk is dropped.
func budgetTestViews(chunkSize int, fids ...string) *Interval[*ChunkView] {
var views *Interval[*ChunkView]
for i := len(fids) - 1; i >= 0; i-- {
views = &Interval[*ChunkView]{Value: &ChunkView{FileId: fids[i], ViewOffset: 2<<20 + int64(i*chunkSize), ViewSize: uint64(chunkSize), ChunkSize: uint64(chunkSize)}, Next: views}
}
return views
}
// waitBudgetDrained waits until the budget holds nothing. A chunk dropped while
// its download still runs is destroyed once that download gives up.
func waitBudgetDrained(t *testing.T, b *ReaderCacheBudget) {
t.Helper()
deadline := time.Now().Add(5 * time.Second)
for {
b.Lock()
used, reserved, idle := b.used, len(b.reservations), b.idle.Len()
b.Unlock()
if used == 0 && reserved == 0 && idle == 0 {
return
}
if time.Now().After(deadline) {
t.Fatalf("closed files still reserve %d bytes (%d reservations, %d idle)", used, reserved, idle)
}
time.Sleep(10 * time.Millisecond)
}
}
// TestReaderCacheDestroyWhileDownloadWaitsForBudget closes a file while one of
// its downloads waits for budget that two other downloads of the file hold.
// destroy() takes every chunk out of the map and then destroys them one at a
// time. When it reaches the waiting download first, it waits for it, and that
// download can only get budget the file's own chunks give back.
// - prefetch: the waiting download is a prefetch nobody read, which
// destroy() cancels.
// - pinned, holders cancelled: a read started the waiting download and gave
// up (its process was killed), so its stream is still positioned in it.
// Closing a ChunkGroup does not release the streams, so destroy() cannot
// cancel it. The holders are prefetches that destroy() cancels; they end
// as failed downloads, which are never evictable, so only their
// reservations can make room.
// - pinned, holders pinned: the holders were also started by reads that gave
// up, so they are not cancelled. They finish after the close, as chunks a
// stream is positioned in that are no longer in the map, and eviction has
// to free them.
func TestReaderCacheDestroyWhileDownloadWaitsForBudget(t *testing.T) {
const chunkSize = 3 << 10 // pooled as 4 KiB, so the budget holds two
for _, tc := range []struct {
name string
pinned bool // the waiting download was started by a read that gave up
holdersPinned bool // so were the two downloads holding the budget
}{
{name: "prefetch"},
{name: "pinned, holders cancelled", pinned: true},
{name: "pinned, holders pinned", pinned: true, holdersPinned: true},
} {
t.Run(tc.name, func(t *testing.T) {
for round := 0; round < 30; round++ {
budget := NewReaderCacheBudget(8 << 10)
rc := NewReaderCache(256, newMockChunkCacheForReaderCache(), func(context.Context, string) ([]string, error) { return []string{"unused"}, nil }, nil, budget)
gate := make(chan struct{})
started := make(chan string, 3)
rc.fetchChunkDataFn = func(_ context.Context, buffer []byte, _ []string, _ []byte, _ bool, _ bool, _ int64, fileId string, _ util_http.RefreshUrlsFunc) (int, error) {
started <- fileId
<-gate
buffer[0] = 42
return len(buffer), nil
}
// giveUp starts reading fid through a stream of its own and gives
// up once wait has passed; the stream stays positioned in the chunk
giveUp := func(fid string, wait func()) {
ctx, cancel := context.WithCancel(context.Background())
readDone := make(chan error, 1)
go func() {
_, err := rc.readChunkAt(ctx, &chunkStream{}, make([]byte, 1), fid, nil, false, 0, chunkSize, false)
readDone <- err
}()
wait()
cancel()
select {
case <-readDone:
case <-time.After(5 * time.Second):
t.Fatalf("round %d: the cancelled read of %s did not return", round, fid)
}
}
holdersStarted := func() {
select {
case <-started:
case <-time.After(5 * time.Second):
t.Fatalf("round %d: a download did not start", round)
}
}
switch {
case tc.holdersPinned:
giveUp("a", holdersStarted)
giveUp("b", holdersStarted)
case tc.pinned:
rc.MaybeCache(budgetTestViews(chunkSize, "a", "b"), 2)
holdersStarted()
holdersStarted()
default:
// any two of the three take the budget and the third waits
rc.MaybeCache(budgetTestViews(chunkSize, "a", "b", "c"), 3)
holdersStarted()
holdersStarted()
}
if tc.pinned {
giveUp("c", func() { time.Sleep(20 * time.Millisecond) })
}
select {
case fid := <-started:
t.Fatalf("round %d: %s downloaded beyond the budget", round, fid)
case <-time.After(20 * time.Millisecond):
}
// the file is closed while the third download waits, then the two finish
destroyed := make(chan struct{})
go func() {
rc.destroy()
close(destroyed)
}()
time.Sleep(10 * time.Millisecond)
close(gate)
select {
case <-destroyed:
case <-time.After(3 * time.Second):
t.Fatalf("round %d: destroy is stuck: the waiting download cannot get budget the closed file's own chunks hold", round)
}
budget.Lock()
used := budget.used
budget.Unlock()
if used != 0 {
t.Fatalf("round %d: %d bytes still reserved after destroy", round, used)
}
}
})
}
}
// TestReaderCacheBudgetStress runs reads (some given up midway), prefetches,
// failing downloads and file close/reopen concurrently on several files that
// share a budget of four chunks. Run it with -race. Every read that returns
// data must return the chunk's bytes, nothing may hang, and once every file is
// closed no budget may stay reserved.
func TestReaderCacheBudgetStress(t *testing.T) {
const (
chunkSize = 3 << 10
chunks = 12
files = 4
workers = 16
)
duration := 2 * time.Second
if testing.Short() {
duration = 300 * time.Millisecond
}
budget := NewReaderCacheBudget(16 << 10)
lookup := func(context.Context, string) ([]string, error) { return []string{"unused"}, nil }
content := func(fid string) byte { return fid[1] }
fetch := func(_ context.Context, buffer []byte, _ []string, _ []byte, _ bool, _ bool, _ int64, fileId string, _ util_http.RefreshUrlsFunc) (int, error) {
time.Sleep(time.Duration(rand.Intn(1500)) * time.Microsecond)
if rand.Intn(20) == 0 {
return 0, errors.New("injected fetch failure")
}
for i := range buffer {
buffer[i] = content(fileId)
}
return len(buffer), nil
}
fids := make([]string, chunks)
for i := range fids {
fids[i] = string([]byte{'f', byte('A' + i)})
}
var mu sync.Mutex
var all []*ReaderCache
open := func() *ReaderCache {
rc := NewReaderCache(4, newMockChunkCacheForReaderCache(), lookup, nil, budget)
rc.fetchChunkDataFn = fetch
mu.Lock()
all = append(all, rc)
mu.Unlock()
return rc
}
var slots [files]atomic.Pointer[ReaderCache]
for i := range slots {
slots[i].Store(open())
}
stop := make(chan struct{})
var wrongData, reads atomic.Int64
var wg sync.WaitGroup
for w := 0; w < workers; w++ {
wg.Add(1)
go func(seed int64) {
defer wg.Done()
r := rand.New(rand.NewSource(seed))
streams := map[*ReaderCache]*chunkStream{}
for {
select {
case <-stop:
return
default:
}
rc := slots[r.Intn(files)].Load()
first := r.Intn(chunks - 2)
if r.Intn(4) == 0 {
rc.MaybeCache(budgetTestViews(chunkSize, fids[first:first+3]...), 1+r.Intn(3))
continue
}
stream := streams[rc]
if stream == nil {
stream = &chunkStream{}
streams[rc] = stream
}
ctx, cancel := context.WithCancel(context.Background())
if r.Intn(5) == 0 {
time.AfterFunc(time.Duration(r.Intn(800))*time.Microsecond, cancel)
}
offset := int64(r.Intn(chunkSize))
buf := make([]byte, 1+r.Intn(chunkSize))
n, err := rc.readChunkAt(ctx, stream, buf, fids[first], nil, false, offset, chunkSize, false)
cancel()
if err == nil && n > 0 {
reads.Add(1)
for _, b := range buf[:n] {
if b != content(fids[first]) {
wrongData.Add(1)
break
}
}
}
}
}(int64(w))
}
// close and reopen files while the workers read them
wg.Add(1)
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(99))
for {
select {
case <-stop:
return
case <-time.After(time.Duration(1+r.Intn(4)) * time.Millisecond):
}
slot := &slots[r.Intn(files)]
old := slot.Swap(open())
old.destroy()
}
}()
time.Sleep(duration)
close(stop)
finished := make(chan struct{})
go func() {
wg.Wait()
mu.Lock()
closing := append([]*ReaderCache(nil), all...)
mu.Unlock()
for _, rc := range closing {
rc.destroy()
}
close(finished)
}()
select {
case <-finished:
case <-time.After(30 * time.Second):
_ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
t.Fatal("reads, prefetches and closes are stuck")
}
if wrongData.Load() != 0 {
t.Fatalf("%d of %d reads returned bytes of another chunk", wrongData.Load(), reads.Load())
}
if reads.Load() == 0 {
t.Fatal("no read returned data")
}
waitBudgetDrained(t, budget)
t.Logf("%d reads with data", reads.Load())
}
// TestReaderCacheBudgetStreamsStress: streams moving between the chunks of one
// file under a budget of two chunks, with reads given up midway. A read that
// drops the chunk its stream left must not wait for that chunk's download
// while it holds its new chunk: the download can be evicting the new chunk
// for budget, waiting for that read. Run it with -race.
func TestReaderCacheBudgetStreamsStress(t *testing.T) {
const (
chunkSize = 3 << 10
chunks = 4
workers = 8
)
duration := 2 * time.Second
if testing.Short() {
duration = 300 * time.Millisecond
}
budget := NewReaderCacheBudget(8 << 10)
rc := NewReaderCache(chunks, newMockChunkCacheForReaderCache(), func(context.Context, string) ([]string, error) { return []string{"unused"}, nil }, nil, budget)
rc.fetchChunkDataFn = func(_ context.Context, buffer []byte, _ []string, _ []byte, _ bool, _ bool, _ int64, fileId string, _ util_http.RefreshUrlsFunc) (int, error) {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Microsecond)
for i := range buffer {
buffer[i] = fileId[1]
}
return len(buffer), nil
}
fids := make([]string, chunks)
for i := range fids {
fids[i] = string([]byte{'s', byte('A' + i)})
}
stop := make(chan struct{})
var wrongData, reads atomic.Int64
var wg sync.WaitGroup
for w := 0; w < workers; w++ {
wg.Add(1)
go func(seed int64) {
defer wg.Done()
r := rand.New(rand.NewSource(seed))
stream := &chunkStream{}
defer rc.releaseStream(stream)
for {
select {
case <-stop:
return
default:
}
fid := fids[r.Intn(chunks)]
ctx, cancel := context.WithCancel(context.Background())
if r.Intn(3) == 0 {
time.AfterFunc(time.Duration(r.Intn(500))*time.Microsecond, cancel)
}
buf := make([]byte, 1+r.Intn(chunkSize))
n, err := rc.readChunkAt(ctx, stream, buf, fid, nil, false, int64(r.Intn(chunkSize)), chunkSize, false)
cancel()
if err == nil && n > 0 {
reads.Add(1)
for _, b := range buf[:n] {
if b != fid[1] {
wrongData.Add(1)
break
}
}
}
}
}(int64(w))
}
time.Sleep(duration)
close(stop)
finished := make(chan struct{})
go func() {
wg.Wait()
rc.destroy()
close(finished)
}()
select {
case <-finished:
case <-time.After(20 * time.Second):
_ = pprof.Lookup("goroutine").WriteTo(os.Stderr, 1)
t.Fatal("streams are stuck")
}
if wrongData.Load() != 0 {
t.Fatalf("%d of %d reads returned bytes of another chunk", wrongData.Load(), reads.Load())
}
if reads.Load() == 0 {
t.Fatal("no read returned data")
}
waitBudgetDrained(t, budget)
t.Logf("%d reads with data", reads.Load())
}