diff --git a/weed/filer/reader_cache.go b/weed/filer/reader_cache.go index 94eaad9499..39911b905d 100644 --- a/weed/filer/reader_cache.go +++ b/weed/filer/reader_cache.go @@ -354,21 +354,29 @@ func (rc *ReaderCache) remove(downloader *SingleChunkCacher) { } } -// removeUnpinned drops a cacher only while no stream is positioned in it. -// Budget eviction picks its victim under the budget lock, so the pin check -// and the map removal must happen together under the ReaderCache lock. -func (rc *ReaderCache) removeUnpinned(downloader *SingleChunkCacher) (removed bool) { +// evictForBudget destroys a finished chunk the budget picked to make room. +// Unless force is set it keeps a chunk a stream is positioned in: the budget +// picks its victim under the budget lock, so the pin check and the map removal +// must happen together under the ReaderCache lock. +// +// A chunk no longer in the map was taken out by a removal that has not +// destroyed it yet, such as ReaderCache.destroy(), which destroys a closed +// file's chunks one at a time and can be waiting for a download that needs +// this very budget. Such a chunk is destroyed here rather than skipped: +// skipping it would leave both waiting forever. destroy() is safe to run twice. +func (rc *ReaderCache) evictForBudget(downloader *SingleChunkCacher, force bool) (evicted bool) { rc.Lock() - removed = rc.downloaders[downloader.chunkFileId] == downloader && - atomic.LoadInt32(&downloader.pins) == 0 - if removed { + inMap := rc.downloaders[downloader.chunkFileId] == downloader + if inMap && !force && atomic.LoadInt32(&downloader.pins) != 0 { + rc.Unlock() + return false + } + if inMap { delete(rc.downloaders, downloader.chunkFileId) } rc.Unlock() - if removed { - downloader.destroy() - } - return + downloader.destroy() + return true } // consumable reports whether the cacher could be dropped: its buffer was @@ -394,7 +402,21 @@ func (rc *ReaderCache) removeConsumed(downloader *SingleChunkCacher) { removed := rc.removeConsumedLocked(downloader) rc.Unlock() if removed { - downloader.destroy() + downloader.destroyWhenDone() + } +} + +// destroyWhenDone destroys a cacher dropped as consumed: at once when its +// download has ended, otherwise once the download, cancelled when the cacher +// was dropped, gives up. A read dropping the chunk its stream left must not +// wait for that download while it holds its new chunk: the download can be in +// a budget eviction of the new chunk, waiting for that very read to finish. +func (s *SingleChunkCacher) destroyWhenDone() { + select { + case <-s.done: + s.destroy() + default: + go s.destroy() } } @@ -456,6 +478,12 @@ func (s *SingleChunkCacher) startCaching() { s.wg.Done() if s.hasCompletedError() { s.parent.remove(s) + // remove() does nothing for a download no longer in the map: + // whoever took it out destroys it, possibly a ReaderCache.destroy() + // that waits for a download needing this very budget. A failed + // download holds no buffer, so give its reservation back now; + // release is idempotent. + s.parent.budget.release(s) } else { s.parent.budget.complete(s) } diff --git a/weed/filer/reader_cache_budget.go b/weed/filer/reader_cache_budget.go index bd6c379ba0..41bd0988c0 100644 --- a/weed/filer/reader_cache_budget.go +++ b/weed/filer/reader_cache_budget.go @@ -78,9 +78,7 @@ func (b *ReaderCacheBudget) reserve(s *SingleChunkCacher) error { b.idle.Remove(entry) delete(b.idleEntries, victim) b.Unlock() - if pinnedVictim { - victim.parent.remove(victim) - } else if !victim.parent.removeUnpinned(victim) { + if !victim.parent.evictForBudget(victim, pinnedVictim) { // The victim was pinned between selection and removal: keep it // evictable so a pin abandoned in that gap cannot wedge the // budget, then retry the selection. @@ -144,7 +142,7 @@ func (b *ReaderCacheBudget) TryReserveScratch(size int) bool { b.idle.Remove(entry) delete(b.idleEntries, victim) b.Unlock() - if !victim.parent.removeUnpinned(victim) { + if !victim.parent.evictForBudget(victim, false) { // pinned meanwhile: keep it evictable, as reserve does, and give up b.Lock() if _, ok := b.reservations[victim]; ok && b.idleEntries[victim] == nil { diff --git a/weed/filer/reader_cache_destroy_budget_test.go b/weed/filer/reader_cache_destroy_budget_test.go new file mode 100644 index 0000000000..4d529ce872 --- /dev/null +++ b/weed/filer/reader_cache_destroy_budget_test.go @@ -0,0 +1,388 @@ +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()) +}