Files
seaweedfs/weed/util/mem/slot_pool.go
T
Chris Lu 4a1d65939f fix(mount): bound reader cache memory across open files (#11220)
* fix(filer): bound retained reader cache buffers by bytes

* test(filer): keep in-flight downloads during cache trimming

* feat(mem): expose pooled allocation capacity for byte reservations

* fix(mount): share a configurable reader buffer budget across files

* fix(filer): release failed prefetch slots and memory reservations

* feat(mount): expose a soft Go runtime memory limit

* docs(filer): restore shared-download rationale in startCaching

The one-line comment replacing the original context.Background() explanation was too thin for readChunkAt to cross-reference shared resource semantics. Restore a concise note on why request cancellation must not abort a download shared by concurrent readers.

* test(filer): loosen reader cache test deadlines to 5s

Three tests used 1-second deadlines that can flake on CI under load:
TestReaderCacheBudgetInFlight, TestReaderCacheEvictionDoesNotHoldCacheLock,
and TestReaderCacheFailedPrefetchReleasesBudget. Increase to 5 seconds.

* test(filer): cover re-read after reader cache eviction

Add TestReaderCacheReReadAfterEviction: reads chunk 'a', reads chunk 'b'
(evicting 'a' via budget pressure), then re-reads 'a' and asserts a
fresh download returns correct data. Verifies the core correctness
property that eviction never exposes missing or stale data to readers.
2026-09-08 10:51:28 -07:00

62 lines
1011 B
Go

package mem
import (
"sync"
)
var pools []*sync.Pool
const (
min_size = 1024
)
func bitCount(size int) (count int) {
for ; size > min_size; count++ {
size = (size + 1) >> 1
}
return
}
func init() {
// 1KB ~ 256MB
pools = make([]*sync.Pool, bitCount(1024*1024*256))
for i := 0; i < len(pools); i++ {
slotSize := 1024 << i
pools[i] = &sync.Pool{
New: func() interface{} {
buffer := make([]byte, slotSize)
return &buffer
},
}
}
}
func getSlotPool(size int) (*sync.Pool, bool) {
index := bitCount(size)
if index >= len(pools) {
return nil, false
}
return pools[index], true
}
func AllocationSize(size int) int {
if _, found := getSlotPool(size); found {
return min_size << bitCount(size)
}
return size
}
func Allocate(size int) []byte {
if pool, found := getSlotPool(size); found {
slab := *pool.Get().(*[]byte)
return slab[:size]
}
return make([]byte, size)
}
func Free(buf []byte) {
if pool, found := getSlotPool(cap(buf)); found {
pool.Put(&buf)
}
}