mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* 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>
199 lines
4.9 KiB
Go
199 lines
4.9 KiB
Go
package filer
|
|
|
|
import (
|
|
"container/list"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/mem"
|
|
)
|
|
|
|
const DefaultReaderCacheMemoryLimit = 256 << 20
|
|
|
|
type ReaderCacheBudget struct {
|
|
sync.Mutex
|
|
limit int64
|
|
used int64
|
|
reservations map[*SingleChunkCacher]int64
|
|
idle list.List
|
|
idleEntries map[*SingleChunkCacher]*list.Element
|
|
changed chan struct{}
|
|
}
|
|
|
|
func NewReaderCacheBudget(limit int64) *ReaderCacheBudget {
|
|
if limit <= 0 {
|
|
limit = DefaultReaderCacheMemoryLimit
|
|
}
|
|
return &ReaderCacheBudget{
|
|
limit: limit,
|
|
reservations: make(map[*SingleChunkCacher]int64),
|
|
idleEntries: make(map[*SingleChunkCacher]*list.Element),
|
|
changed: make(chan struct{}),
|
|
}
|
|
}
|
|
|
|
func (b *ReaderCacheBudget) reserve(s *SingleChunkCacher) error {
|
|
if s.chunkSize < 0 {
|
|
return fmt.Errorf("invalid chunk size %d", s.chunkSize)
|
|
}
|
|
if b == nil {
|
|
return nil
|
|
}
|
|
size := int64(mem.AllocationSize(s.chunkSize))
|
|
if size > b.limit {
|
|
return fmt.Errorf("chunk buffer needs %d bytes, exceeding reader cache budget %d; increase the readerCacheSizeMB budget", size, b.limit)
|
|
}
|
|
for {
|
|
// a cancelled download must not evict a chunk for itself
|
|
if err := s.ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
b.Lock()
|
|
if size <= b.limit-b.used {
|
|
b.used += size
|
|
b.reservations[s] = size
|
|
b.Unlock()
|
|
return nil
|
|
}
|
|
// Prefer evicting an idle chunk no stream is positioned in; fall back
|
|
// to the oldest pinned one so abandoned pins cannot block the budget.
|
|
var victim *SingleChunkCacher
|
|
var entry *list.Element
|
|
pinnedVictim := false
|
|
for e := b.idle.Front(); e != nil; e = e.Next() {
|
|
c := e.Value.(*SingleChunkCacher)
|
|
if atomic.LoadInt32(&c.pins) == 0 {
|
|
victim, entry = c, e
|
|
pinnedVictim = false
|
|
break
|
|
}
|
|
if victim == nil {
|
|
victim, entry = c, e
|
|
pinnedVictim = true
|
|
}
|
|
}
|
|
if entry != nil {
|
|
b.idle.Remove(entry)
|
|
delete(b.idleEntries, victim)
|
|
b.Unlock()
|
|
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.
|
|
b.Lock()
|
|
if _, ok := b.reservations[victim]; ok && b.idleEntries[victim] == nil {
|
|
b.idleEntries[victim] = b.idle.PushBack(victim)
|
|
}
|
|
b.Unlock()
|
|
}
|
|
continue
|
|
}
|
|
changed := b.changed
|
|
b.Unlock()
|
|
select {
|
|
case <-changed:
|
|
case <-s.ctx.Done():
|
|
return s.ctx.Err()
|
|
}
|
|
}
|
|
}
|
|
|
|
// canFit reports whether a whole-chunk buffer of this size can ever be
|
|
// reserved. A chunk bigger than the budget cannot be read through the
|
|
// whole-chunk path at all, so callers must fall back to range fetches.
|
|
func (b *ReaderCacheBudget) canFit(size int) bool {
|
|
return b == nil || int64(mem.AllocationSize(size)) <= b.limit
|
|
}
|
|
|
|
// TryReserveScratch takes size bytes for a buffer that a hedged chunk read
|
|
// borrows while it races the download holding the chunk's own reservation.
|
|
// Like a download it evicts idle chunks no stream is positioned in, but it
|
|
// never waits and never evicts a pinned chunk: a hedge that does not fit is
|
|
// simply not sent. A nil budget has room for anything.
|
|
func (b *ReaderCacheBudget) TryReserveScratch(size int) bool {
|
|
if b == nil {
|
|
return true
|
|
}
|
|
n := int64(mem.AllocationSize(size))
|
|
if n > b.limit {
|
|
return false
|
|
}
|
|
for {
|
|
b.Lock()
|
|
if n <= b.limit-b.used {
|
|
b.used += n
|
|
b.Unlock()
|
|
return true
|
|
}
|
|
var victim *SingleChunkCacher
|
|
var entry *list.Element
|
|
for e := b.idle.Front(); e != nil; e = e.Next() {
|
|
if c := e.Value.(*SingleChunkCacher); atomic.LoadInt32(&c.pins) == 0 {
|
|
victim, entry = c, e
|
|
break
|
|
}
|
|
}
|
|
if entry == nil {
|
|
b.Unlock()
|
|
return false
|
|
}
|
|
b.idle.Remove(entry)
|
|
delete(b.idleEntries, victim)
|
|
b.Unlock()
|
|
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 {
|
|
b.idleEntries[victim] = b.idle.PushBack(victim)
|
|
}
|
|
b.Unlock()
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
// ReleaseScratch returns what TryReserveScratch took.
|
|
func (b *ReaderCacheBudget) ReleaseScratch(size int) {
|
|
if b == nil {
|
|
return
|
|
}
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
b.used -= int64(mem.AllocationSize(size))
|
|
close(b.changed)
|
|
b.changed = make(chan struct{})
|
|
}
|
|
|
|
func (b *ReaderCacheBudget) complete(s *SingleChunkCacher) {
|
|
if b == nil {
|
|
return
|
|
}
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
if _, found := b.reservations[s]; found && b.idleEntries[s] == nil {
|
|
b.idleEntries[s] = b.idle.PushBack(s)
|
|
close(b.changed)
|
|
b.changed = make(chan struct{})
|
|
}
|
|
}
|
|
|
|
func (b *ReaderCacheBudget) release(s *SingleChunkCacher) {
|
|
if b == nil {
|
|
return
|
|
}
|
|
b.Lock()
|
|
defer b.Unlock()
|
|
if size, found := b.reservations[s]; found {
|
|
b.used -= size
|
|
delete(b.reservations, s)
|
|
if entry := b.idleEntries[s]; entry != nil {
|
|
b.idle.Remove(entry)
|
|
delete(b.idleEntries, s)
|
|
}
|
|
close(b.changed)
|
|
b.changed = make(chan struct{})
|
|
}
|
|
}
|