filer: always range-fetch a chunk too big for the reader budget (#13201)

* filer: always range-fetch a chunk too big for the reader budget

A whole chunk view bigger than the reader cache budget only fell back to
a range fetch in random mode. A sequential read, or a read across a
chunk boundary, took the whole-chunk path and failed with "chunk buffer
needs N bytes, exceeding reader cache budget". Prefetch also started
such chunks, which took a slot in the cache and then failed.

readsRange now decides for both read paths, and a chunk that can't fit
the budget is always a range fetch. Prefetch skips it.

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

* filer: test that prefetch skips a chunk over the reader budget

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-12-01 10:55:20 -08:00
1 parent f9c08ad379
commit 19204293c2
3 files changed
+127 -19

No files matched your search

+20 -13
View File
@@ -351,19 +351,26 @@ func (c *ChunkReadAt) doReadAt(ctx context.Context, p []byte, offset int64) (n i
return
}
func (c *ChunkReadAt) readChunkSliceAt(ctx context.Context, buffer []byte, chunkView *ChunkView, nextChunkViews *Interval[*ChunkView], offset uint64) (n int, err error) {
// readsRange reports whether chunkView is fetched as a range rather than
// through the shared whole-chunk download.
//
// A view clipped to part of its chunk (e.g. the edge of a ranged GET,
// whose views ViewFromVisibleIntervals clips to the request) only ever
// needs that part: fetch it as a range no matter the detected pattern.
// Fetching the chunk whole would multiply volume-server reads. Ciphered
// and compressed chunks are the exception: the volume server reads the
// whole blob to serve a range, so they take the shared whole-chunk path
// where one download serves every buffer. A chunk that cannot fit the
// reader budget is always fetched as a range, as nothing else can serve it.
func (c *ChunkReadAt) readsRange(chunkView *ChunkView) bool {
if !c.readerCache.budget.canFit(int(chunkView.ChunkSize)) {
return true
}
return chunkView.CanRangeFetch() && (!chunkView.IsFullChunk() || c.readerPattern.IsRandomMode())
}
// A view clipped to part of its chunk (e.g. the edge of a ranged GET,
// whose views ViewFromVisibleIntervals clips to the request) only ever
// needs that part: fetch it as a range no matter the detected pattern.
// Fetching the chunk whole would multiply volume-server reads. Ciphered
// and compressed chunks are the exception: the volume server reads the
// whole blob to serve a range, so they take the shared whole-chunk path
// where one download serves every buffer — unless the whole chunk cannot
// even fit the reader budget, in which case a range fetch is the only
// way to serve the request.
rangeFetch := chunkView.CanRangeFetch() || !c.readerCache.budget.canFit(int(chunkView.ChunkSize))
if rangeFetch && (!chunkView.IsFullChunk() || c.readerPattern.IsRandomMode()) {
func (c *ChunkReadAt) readChunkSliceAt(ctx context.Context, buffer []byte, chunkView *ChunkView, nextChunkViews *Interval[*ChunkView], offset uint64) (n int, err error) {
if c.readsRange(chunkView) {
c.readerCache.releaseStream(&c.stream)
n, err := c.readerCache.chunkCache.ReadChunkAt(buffer, chunkView.FileId, offset)
if n > 0 {
@@ -393,7 +400,7 @@ func (c *ChunkReadAt) readChunkSliceAt(ctx context.Context, buffer []byte, chunk
// readChunkSliceAtForParallel is a simplified version for parallel chunk fetching
// It doesn't update lastChunkFid or trigger prefetch (handled by the caller)
func (c *ChunkReadAt) readChunkSliceAtForParallel(ctx context.Context, buffer []byte, chunkView *ChunkView, offset uint64) (n int, err error) {
if (chunkView.CanRangeFetch() || !c.readerCache.budget.canFit(int(chunkView.ChunkSize))) && !chunkView.IsFullChunk() {
if c.readsRange(chunkView) {
n, err = c.readerCache.chunkCache.ReadChunkAt(buffer, chunkView.FileId, offset)
if n > 0 {
return n, err
+101
View File
@@ -403,6 +403,107 @@ func TestChunkReadAtOversizedCompressedChunkFallsBackToRange(t *testing.T) {
}
}
// A whole chunk larger than the reader cache budget is range-fetched in a
// sequential read too, as the budget can't hold it whole.
func TestChunkReadAtOversizedFullChunkFallsBackToRange(t *testing.T) {
const chunkSize = 1 << 20
const sliceSize = 16 << 10
budget := NewReaderCacheBudget(64 << 10)
rc := NewReaderCache(64, (*chunk_cache.TieredChunkCache)(nil), func(context.Context, string) ([]string, error) {
return []string{"unused"}, nil
}, nil, budget)
defer rc.destroy()
fetches := fetchRecorder(rc)
views := NewIntervalList[*ChunkView]()
views.AppendInterval(&Interval[*ChunkView]{
StartOffset: 0,
StopOffset: chunkSize,
Value: &ChunkView{FileId: "chunk0", ViewSize: chunkSize, ChunkSize: chunkSize},
})
reader := NewChunkReaderAtFromClient(context.Background(), rc, views, chunkSize, 0)
defer reader.ReleaseStream()
if n, err := reader.ReadAt(make([]byte, sliceSize), 0); err != nil || n != sliceSize {
t.Fatalf("read: n=%d err=%v", n, err)
}
if len(*fetches) != 1 {
t.Fatalf("got %d fetches, want 1 range fetch: %+v", len(*fetches), *fetches)
}
if f := (*fetches)[0]; f.isFullChunk || f.offset != 0 || f.size != sliceSize {
t.Fatalf("fetch = %+v, want range fetch offset=0 size=%d", f, sliceSize)
}
}
// A read across chunks bigger than the budget range-fetches each of them, as
// the parallel path can't download them whole either.
func TestChunkReadAtOversizedFullChunksAcrossABoundaryFallBackToRange(t *testing.T) {
const chunkSize = 1 << 20
const sliceSize = 16 << 10
budget := NewReaderCacheBudget(64 << 10)
rc := NewReaderCache(64, (*chunk_cache.TieredChunkCache)(nil), func(context.Context, string) ([]string, error) {
return []string{"unused"}, nil
}, nil, budget)
defer rc.destroy()
fetches := fetchRecorder(rc)
views := NewIntervalList[*ChunkView]()
for i := int64(0); i < 2; i++ {
views.AppendInterval(&Interval[*ChunkView]{
StartOffset: i * chunkSize,
StopOffset: (i + 1) * chunkSize,
Value: &ChunkView{FileId: fmt.Sprintf("chunk%d", i), ViewOffset: i * chunkSize, ViewSize: chunkSize, ChunkSize: chunkSize},
})
}
reader := NewChunkReaderAtFromClient(context.Background(), rc, views, 2*chunkSize, 0)
defer reader.ReleaseStream()
if n, err := reader.ReadAt(make([]byte, sliceSize), chunkSize-sliceSize/2); err != nil || n != sliceSize {
t.Fatalf("read: n=%d err=%v", n, err)
}
if len(*fetches) != 2 {
t.Fatalf("got %d fetches, want 2 range fetches: %+v", len(*fetches), *fetches)
}
for _, f := range *fetches {
if f.isFullChunk || f.size != sliceSize/2 {
t.Fatalf("fetch = %+v, want a range fetch of size %d", f, sliceSize/2)
}
}
}
// Prefetch skips a chunk that cannot fit the reader budget: only a range
// fetch can serve it, so a whole-chunk prefetch would take a download slot
// the chunks after it could use, and then fail.
func TestReaderCacheMaybeCacheSkipsOversizedChunk(t *testing.T) {
const oversized = 1 << 20
const small = 4 << 10
rc := NewReaderCache(1, (*chunk_cache.TieredChunkCache)(nil), func(context.Context, string) ([]string, error) {
return []string{"unused"}, nil
}, nil, NewReaderCacheBudget(64<<10))
defer rc.destroy()
fetchRecorder(rc)
views := NewIntervalList[*ChunkView]()
views.AppendInterval(&Interval[*ChunkView]{StartOffset: 0, StopOffset: oversized,
Value: &ChunkView{FileId: "chunk0", ViewSize: oversized, ChunkSize: oversized}})
views.AppendInterval(&Interval[*ChunkView]{StartOffset: oversized, StopOffset: oversized + small,
Value: &ChunkView{FileId: "chunk1", ViewOffset: oversized, ViewSize: small, ChunkSize: small}})
rc.MaybeCache(views.Front(), 2)
rc.Lock()
_, prefetched := rc.downloaders["chunk1"]
rc.Unlock()
if !prefetched {
t.Fatal("the chunk after one over the budget was not prefetched")
}
}
// A chunk a stream is positioned in must outlast downloader-limit eviction:
// otherwise a busy cache drops the buffer mid-stream and forces a refetch.
func TestChunkReadAtPinnedChunkSurvivesEviction(t *testing.T) {
+6 -6
View File
@@ -114,12 +114,12 @@ func (rc *ReaderCache) MaybeCache(chunkViews *Interval[*ChunkView], count int) {
// abort when slots are filled
return
}
if (chunkView.CanRangeFetch() || !rc.budget.canFit(int(chunkView.ChunkSize))) && !chunkView.IsFullChunk() {
// the view is clipped to part of the chunk and will be
// range-fetched, so prefetching it whole would download bytes
// nobody needs; a ciphered or compressed partial view needs
// the whole blob anyway and is worth prefetching, but not when
// it cannot fit the budget at all
if !rc.budget.canFit(int(chunkView.ChunkSize)) || (chunkView.CanRangeFetch() && !chunkView.IsFullChunk()) {
// a chunk that cannot fit the budget is only ever range-fetched,
// and so is a view clipped to part of the chunk, so prefetching
// it whole would download bytes nobody needs; a ciphered or
// compressed partial view needs the whole blob anyway and is
// worth prefetching
continue
}