Skip to content

perf(fileservice): keep IVF-Flat entries objects in the index-file cache - #32697

Merged
mergify[bot] merged 85 commits into
matrixorigin:mainfrom
cpegeric:ivf_pin_entries
Nov 14, 2026
Merged

mergify[bot] merged 85 commits into
matrixorigin:mainfrom
cpegeric:ivf_pin_entries

Conversation

@cpegeric

@cpegeric cpegeric commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #32696

What this PR does / why we need it:

Depends on #32382. This branch is built on it, so until #32382 merges the diff here also shows its commits. The changes of this PR alone are git diff fileservice_range_cost...ivf_pin_entries: about 460 lines of code, 470 of tests and a design doc.

A CN with an index-file cache keeps the objects of IVF-Flat entries tables whole in it, beside HNSW and fulltext index files and apart from the shared disk cache. Design: docs/design/20261111-index-objects-in-index-file-cache.md.

  • Loading. The IVF-Flat search's scan of the entries table opens the objects of its blocks before reading them, eight at once, and holds them until the scan ends (indexfile.OpenObject). An object missing from the cache is downloaded whole, as two ranges at once when it is at least 16 MiB and fewer than disk-prefetch-object-split-downloads (default 8) whole-object downloads are in flight. A distributed search loads only each CN's own objects. An object that cannot be loaded is read the normal way.
  • Reading. Reads of an entries table carry a new policy bit, IndexFileObject: its table scans (by table type) and the search's own scan. S3FS.Read serves such a read from the index-file cache when the cache holds the object; the decoded data still enters the memory cache. A prefetch of an object the cache holds is not downloaded into the disk cache.
  • Eviction and delete. An object's entry is an index file like any other, evicted by the cache's LRU. Deleting an object through the file service deletes its entry; a load that read the object before the delete publishes nothing.
  • Always on. No setting: any CN with an index-file cache does this. The index-file cache's idle-TTL default changes from 1 h to 0 (idle collection off; LRU and free-space collection still apply).
  • Known limit. The index-file cache is assumed to be sized for the indexes it serves, as for HNSW and fulltext. When the objects one search holds exceed it, each further object is read whole, evicted at once, and its ranges are read again from object storage.

Validation

Swap benchmark: wiki_all 1M IVF-Flat (2.9 GB of entries objects) alternating with TPC-H SF10, single CN, 4 GB disk cache, MinIO behind a proxy adding 5 ms per request, both caches empty at start, 2 runs per build interleaved (#32382 vs this PR):

Phase #32382 this PR
2nd TPC-H round 31.4 s / 23.7 s, 10.9k / 8.9k S3 GETs 10.2 s / 9.8 s, 0 S3 GETs
2nd wiki filtered, 100 queries 8.41 s / 4.27 s, p95 352 / 156 ms 2.64 s / 2.57 s, p95 ~50 ms
1st (cold) wiki filtered 4.78 s / 4.41 s 6.06 s / 5.79 s
wiki unfiltered / projected ~2.0 s ~2.1 s

The first cold filtered search is slower because it waits for whole objects to load. Query results were identical; no panics.

Unit tests cover serving reads from the cache, misses, prefetch skipping, delete, split downloads, concurrent opens, entries of the wrong size, and the scan policy. The 15 focused tests passed 100 runs each under -race; fileservice, indexfile, ivfflat, sql/compile and objectio/ioutil passed under -race.

🤖 Generated with Claude Code

cpegeric and others added 30 commits November 7, 2026 07:36
… read share honest

- A background download keeps its reader's deadline and ends when the disk
  cache closes.
- A read that captured its bytes does not fail when its download fails later,
  also when no background slot was free.
- The download rate is fed only by downloads from object storage; content
  already in memory is cached without tracking it as a download.
- Range-read cost is timed only for reads the file service drains to the end.
- Bytes read from a downloading object count toward its cached file's read share.
- The LRU disk cache inserts an item larger than its capacity as oldest, and
  does not evict an item before its insert notification returns.
- Design v3 records these contracts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s at publication

- A whole-object fill from memory is tracked as a download again, so readers
  share it, Update skips its ranges and the room check records its size; only
  downloads from object storage feed the download rate.
- Bytes read from a download are counted before the read and credited to the
  file when it is published, so a file evicted at once, or a read still running
  at publication, keeps them.
- The LRU disk cache neither evicts an item nor counts it toward eviction until
  its insert notification returns, so an oversized item no longer empties the
  cache while it is inserted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…zed items oldest

- The read that starts a whole-object download counts the bytes it captures
  toward the file's read share, as other readers of the download do, so an
  object read only by its starter is not taken as unread.
- A use of an item larger than the LRU capacity does not make it newer, so a
  read while it is inserted no longer evicts warm items before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ns it

A disk-cache read that misses its exact range file, the object's whole file and
its download now reads from the smallest cached range file of the object that
contains it. Range files are indexed by object as they enter the LRU, including
files found at startup, and forgotten as they leave; a read passes over and
forgets an indexed file the cache no longer holds. The size in a range file's
name runs into the object name, so it is the prefix that equals the file's
length, and only files in their object's shard directory are indexed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s cached ranges

A range read with disk-prefetch-range-min or disk-prefetch-range-auto set fetches
the aligned units its entries touch, at least the bandwidth-delay product of range
reads with auto, and caches them as ranges that serve later reads. Units are split
into blocks; a read takes each block it needs from a cached range, from the read
under way that ends first, or from a new read of each run of missing blocks, so no
block is read twice. The bytes between entries in other units are not read.

DiskCache.Read serves an entry from cached ranges that hold it together when no
single range contains it.

The whole-object preload switch compares the cost of range reads with the cost of
whole objects, with tunable margins, charge window and half life.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… cost model

Prefetch flights read the body in 64KiB chunks and wake readers as their
bytes arrive, drain the body to EOF so the connection is reused, and run
on a context that outlives the reader that started them. Prefetch skips
paths with a separator, samples request latency, fetches only the units
entries touch, and counts its S3 reads. The preload cost model and its
disk-preload-* knobs are removed; it changed no preload decision.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…inserts and deletes

An insert charges its bytes as pending before used, and settles under the
evictor lock. A removal subtracts its bytes before the item leaves its
shard. Eviction checks the budget again before removing each item. Without
these, a concurrent eviction could count a pending or deleted item and
remove a warm one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h by default

A run of missing blocks that holds a needed block is read from the first
needed block, and the blocks before it in a range of their own, so a read
does not wait for the bytes before its own. disk-prefetch-range-auto
defaults to true; false turns prefetch ranges off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… items

An item larger than the capacity takes a stamp below every SetOldest stamp,
and SetOldest places its item ahead of the oversized items at the back of
its shard, so a file admitted oldest while an oversized insertion is pending
is not evicted before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fetch for a done read

disk-prefetch-range-aligned (default true) turns aligned prefetch ranges on
or off, sized by the bandwidth-delay product of range reads; it replaces
disk-prefetch-range-auto and disk-prefetch-range-min. A read whose context
is done starts no prefetch range. The publication test that warms a range
turns prefetch ranges off, since the prefetched range would serve its
leader read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A whole-object download of an object of at least disk-prefetch-object-split-min
(16 MiB) reads disk-prefetch-object-split (2, at most 4) 64KiB-aligned ranges at
once into the one cache file, while fewer than 4 downloads are in flight. The
object's size comes from a Stat made only when a split is possible. A read of the
object waits only for the parts holding its bytes; a failed part fails the
download, which publishes nothing.

The download's progress is tracked per part, and the time its bytes arrive is
estimated from the time to a part's first byte plus its bytes left at the part's
own rate, measured from 256 KiB, or the recent downloads' rate, which each part
samples.

The HTTP pool allows 128 connections per host, up from 100, which TPC-H reads
exceeded.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Table scans prefetch whole objects with their known size and the time
  the scan reaches them; a prefetcher downloads the soonest due first,
  never queues an object queued, downloading or cached, and bounds the
  queued bytes by min(cache/2, download rate x workers x 30s).
- A read of a queued object moves it to the front and reads its ranges.
- A read-started whole-object download splits at the read's first
  needed block; object size comes from the prefetch, no Stat call.
- Download parts reserve HTTP connections (half of max conns per host,
  pool 128); long connection waits are logged.
- Full-object merger waits are removed.
- Counters for dropped prefetches, skipped downloads and what reads find.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ended prefetches

- A read that reaches a queued object drops its download and reads its
  ranges, so the bytes are not fetched twice.
- Scans pass the blocks of each object they read and its block count;
  the queue downloads first the objects read in the largest share, then
  the soonest due.
- Each scan's prefetches carry a token ended on Close; queued downloads
  whose scans all ended are dropped.
- mo_fs_prefetch_object_total{result} counts queued, merged, cached,
  downloaded, failed, missed, ended and dropped prefetches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cher

- Scans hand object prefetches to the file service's prefetcher instead
  of through the IO pipeline; the pipeline is back to main's.
- Scans prefetch the object they read first too, so its read follows
  the prefetch download or reads its ranges like any other.
- PrefetchObject only queues; it never downloads in the caller.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…wnloads by count, bound prefetcher close

- A prefetch of a cached object marks it used, so the disk cache evicts unrequested objects first.
- The object-split gate counts downloads in flight, not connections.
- Close cancels running prefetch downloads; the disk cache closing cancels them too.
- The last part of a whole-object download reads to the object's end, so a wrong size fails the download.
- A full prefetch queue drops ended entries first; an oversized request drops nothing.
- Only full-object reads clear the policy in the read merge key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…east-read group switches first

- A narrow read's prefetch of a cached object checks it without marking it used, so
  barely read objects age out instead of the files other reads use.
- A group switches to range reads only after the groups read less than it that are
  evicting; when one switches, the other groups' evictions start again.
- Design doc for the object prefetcher; the thrash-aware design records the switching rule.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…reads

The narrow join-back scan of an IVF-Flat post-filter search now prefetches
each read column's object-wide extent as a range instead of the whole object,
and reads exactly its entries rather than aligned prefetch units. Range
requests merge by union, a whole request takes over, and a range request of a
cached object does not change its eviction order. Other scans are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every vector (IVF-Flat, HNSW, IVF-PQ, CAGRA) and fulltext (fulltext,
fulltext2) join back to the base table marks its scan narrow, so it
prefetches the ranges of the columns it reads. When those columns are at
least half of an object, or the object records no column ranges, the scan
prefetches the whole object instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ns satisfy

A row fetch that falls back to a whole-object prefetch passes the ranges of
its columns; the request is skipped while cached ranges cover them. A request
merged into a queued download is skipped when the cache satisfies it, and does
not widen the download past the queue's bytes. Columns an object does not
store need no prefetch. A column prefetch skips the columns already cached and
reads adjacent ones in one read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…on close

Deleting an object deletes its indexed range files, so no read after the
delete is served from them. Closing the disk cache cancels the prefetch range
reads under way, which outlive the reads that start them, before waiting.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…w-only setting

Range reads always fetch and cache the aligned ranges around them, so
overlapping reads fetch and store each block once. disk-prefetch-range-aligned
and disk-prefetch-range-narrow-only are removed; only tests turn prefetch
ranges off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…fore deleting its ranges

A prefetch range read outlives the read that starts it, so it could cache its range
after the object was deleted. Deleting an object now cancels its prefetch range reads
and waits for them and their cache writes, then deletes its indexed range files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mergify mergify Bot added the queued label Nov 14, 2026
@mergify

mergify Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request branch update failed

1 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Sat 14 Nov, 10:26 · rule main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue 10:27 · b12bb0d

Reason

The pull request can't be updated

GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/entrypoint.yaml without workflows permission
GitHub only lets Mergify write to .github/workflows/ with the workflows permission. An organization owner can accept Mergify's pending permissions at https://dashboard.mergify.com/orgs/matrixorigin/repositories.

To get back in the queue

  • Update or rebase the branch yourself: the pull request is queued again once its queue conditions match.
  • If the failure is a flake, tick the box below to queue it again as it is.

Requeue requested — the merge queue status continues in this comment ↓.

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot added dequeued and removed queued labels Nov 14, 2026
cpegeric and others added 3 commits November 14, 2026 10:50
A read that opens a cache file the cache does not index reads and indexes it
only if its object has no delete counted since the cache was created, as the
startup load does. A delete during an asynchronous load misses range files
the load has not indexed yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mergify

mergify Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

✅ Merged into main as b465f86 at 12:05 UTC

Less than a minute in the queue, with no time running CI.

So far

  • ✅ Entered the queue Sat 14 Nov, 12:05 · rule main · triggered by @cpegeric with the merge queue checkbox
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged 12:05 · b465f86 · squash
3 merge conditions met
  • #review-threads-unresolved = 0 · required by GitHub branch protection
  • github-review-approved · required by GitHub branch protection
  • any of · required by GitHub branch protection:
    • check-success = CI Required
    • check-neutral = CI Required
    • check-skipped = CI Required

Times in UTC · dashboard · how the merge queue works

@mergify mergify Bot added the queued label Nov 14, 2026
@mergify
mergify Bot merged commit b465f86 into matrixorigin:main Nov 14, 2026
@mergify mergify Bot removed the queued label Nov 14, 2026
@cpegeric
cpegeric deleted the ivf_pin_entries branch November 14, 2026 12:44
XuPeng-SH pushed a commit that referenced this pull request Nov 16, 2026
…-file cache (#32697)" (#32809)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #32796 #32760 #32792

## What this PR does / why we need it:

Reverts #32697 (squash commit b465f86). That commit also carried the
fileservice range-read and prefetch changes of #32382, so both are
reverted.

It caused three regressions on main:

- **#32796**: distributed SSB scans are 8–11x slower. A scan of some of
a table's columns skips whole-object preloads and relies on column-range
prefetch. Only the local data source has that prefetch;
`RemoteDataSource` has none, so remote CNs read each block's columns one
range at a time.
- **#32760**: Wiki IVF 10M falls to ~2 QPS. Its entries objects (about
30 GB) do not fit the 8 GiB index-file cache, so searches keep
downloading whole objects again.
- **#32792**: reads fail with `fileservice: no bytes from the source
within the idle timeout`. The new 20 s idle timer also counts the wait
for a free pooled connection. A whole-object cache fill that waits that
long fails the query.

Root causes and the planned fixes are in the issue comments. The changes
will be re-landed with those fixes.

Conflict resolution with later commits, both from #32782:

- `pkg/fileservice/policy.go` keeps `ReplayDataForDiskCache` and drops
`ColumnPrefetch`, `ExactRead` and `IndexFileObject`.
- The replay write in `S3FS.write` calls `fillFile` without the
object-layout argument that #32697 added.

Validation: the build and `go vet` pass, and the tests pass in every
package the revert touches (`fileservice/...`, `objectio`,
`objectio/ioutil`, `vm/engine/readutil`, `vm/engine/disttae`,
`disttae/logtailreplay`, `tae/tables`, `tae/txn/txnimpl`, `sql/plan`,
`sql/compile`, `vectorindex/indexfile/...`, `vectorindex/ivfflat/...`,
`util/metric/v2`).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
cpegeric added a commit to cpegeric/matrixone that referenced this pull request Nov 16, 2026

This branch was previously deployed

1 inactive deployment
ci — b29ebc06 Deployed Nov 14, 2026 by cpegeric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants