Skip to content

perf(fileservice): prefetch whole objects for narrow scans only while the disk cache keeps them - #32034

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
cpegeric:issue_32033_prefetch
Nov 2, 2026
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
cpegeric:issue_32033_prefetch

Conversation

@cpegeric

@cpegeric cpegeric commented Nov 2, 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 #32033

What this PR does / why we need it:

A table scan prefetches every object it is about to read into the CN disk cache as a whole object. That suits scans that read most of each object. For scans that read a few rows from each object, it fails whenever the touched objects do not fit the disk cache:

  • the IVFFLAT join back to the base table, which runs when a query filters or projects a column outside the index;
  • the IVFFLAT entries search.

In that case every query downloads whole objects that are evicted before they are used again. On wiki_all 1M with a 4 GB disk cache, 91 objects were downloaded 2,017 times for 750 queries, and a filtered query took 500–800 ms instead of 17 ms. At 88M vectors the cache can never hold these objects.

Never prefetching for these scans is not the answer either: when the objects fit, it makes the first pass about three times slower (cold p95 760 ms against 150 ms). This PR prefetches only while the disk cache keeps what it fetches.

Design: docs/design/20261102-thrash-aware-prefetch.md

  • Signal. Each disk cache measures the share of every whole-object file that is read before the file is evicted. Below 0.4 on average, after at least 8 evictions, it reports thrashing. It stops after 30 s with no evictions. Explicit deletes (object GC) do not count. The exit uses eviction pressure rather than read share, because the signal's own effect raises the read share of the files still being evicted; a read-share exit alternated on and off in testing.
  • Narrow reads. A new policy bit, NarrowRead. While the cache reports thrashing, a narrow read skips whole-object prefetch and caches the ranges it reads on disk. Otherwise it behaves exactly as before.
  • Which reads are narrow.
    • plan.Node.narrow_scan (field 97) marks the base-table scan of the IVFFLAT join back. The scope passes it to the table_scan operator as NarrowRead.
    • The IVFFLAT entries search reads with NarrowRead.
    • Filtered entries searches keep SkipFullFilePreloads, as before.
  • Other scans are unchanged.

Measurements

wiki_all 1M (768-dim vecf32), IVFFLAT lists=1000, probe_limit=8. Filtered query: select id from w where cat < 10 order by l2_distance(emb, q) limit 10. Single CN on MinIO; the disk cache is empty at the start of each run. Milliseconds, average (p95).

Memory / disk main: cold warm never prefetch narrow: cold warm this PR: cold warm
0.5 / 1.5 GB 2632 (5692) 2443 497 (1528) 320 516 (1161) 149
1 / 4 GB 629 (1721) 571 125 (757) 17 214 (602) 55
1 / 8 GB 49 (153) 17 124 (755) 17 102 (310) 16
  • The "this PR" column is this branch's build, 50 + 50 queries per row. The other columns are 100 + 100.
  • A prototype of the same logic, with 100 + 100 queries, gave 53 (147) cold and 17 warm at 1 / 8 GB, and 40 (133) / 3.5 at 4 / 16 GB: the same as main when the cache fits.
  • At 1 / 8 GB the signal never turned on. It turned on once and stayed on at 1 / 4 GB and 0.5 / 1.5 GB.
  • TPC-H SF10 scans are not narrow and do not change.

Alternatives measured and rejected are listed in the design note:

  • readers that do not wait for an in-flight prefetch: 15–30% of the gain;
  • a per-object block-share rule: no effect;
  • eviction-policy changes: about 1.5× against 13–20× for the fill unit.

Tests

  • pkg/fileservice/disk_cache_preload_test.go covers the signal:

    • turning on after the minimum samples;
    • staying off for well-read files;
    • the read share is capped at 1;
    • staying on while evictions continue;
    • turning off after the quiet period;
    • explicit deletes are not evictions;
    • range files are ignored;
    • nil safety;
    • FullFilePreloadsSkipped.

    It also includes a real disk cache that reports thrashing, and an S3FS narrow read that switches to range caching while thrashing.

  • Planner, scope and operator tests check that the join back is marked narrow, the index-only path is not, the mark survives a deep copy, and the reader receives NarrowRead.

  • Each targeted test fails with its production change removed.

  • go vet passes, as do the full suites of pkg/fileservice, pkg/sql/colexec/table_scan, pkg/sql/compile, pkg/sql/plan, pkg/vectorindex/ivfflat, pkg/vm/engine/readutil and pkg/pb/plan, and the pkg/vm/engine/disttae datasource tests.

🤖 Generated with Claude Code

…ect preloads (matrixorigin#32033)

An IVFFLAT query that filters or projects a column outside the index joins the
candidates back to the base table. That scan ran with the default policy, so the
local data source prefetched every touched object whole into the disk cache to
read a few rows; with entries and base objects larger than the disk cache, they
evicted each other on every query.

Plan nodes gain skip_full_file_preloads. The IVFFLAT rewrite sets it on the base
table scan of the join back, the scope passes it to the table scan operator, and
the operator adds SkipFullFilePreloads to its read context, which the data
sources already honor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 0e36a09fb66f20f85f3888bc46c881b27039ed8a

Review completed. Read the assessment.

@cpegeric
cpegeric marked this pull request as draft November 2, 2026 16:12

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 0e36a09fb66f20f85f3888bc46c881b27039ed8a

Assessment: approve

The bounded IVFFLAT cache-policy fix is correctly scoped and propagated through local, parallel and remote scans. Caller policy bits and unaffected scan paths are preserved. No actionable findings remain.

Model: gpt-6-astra / xhigh

Session: 01a1869d-999c-7481-b340-ae4a63e8cee4 · slot 1 · generation 2

Validation:

  • Reviewed all 13 changed files, storage consumers, adaptive fallback isolation, operator lifecycle and regression-test oracles.

  • Static descriptor audit passed: field 97 is the sole descriptor change; frozen source is clean and git diff --check passed. Receipt: checks/1793636061561863019-2.json; output: descriptor-audit.log.

  • Frozen-head CI reports successful build, UT, static checks and both active BVT groups. Reviewed author-reported regression tests and controlled performance comparisons.

  • Saved review-plan.md, complete discussion-assessment.json and validation-evidence.json.

Limitations:

  • No fresh Go tests or benchmarks were run; supplied CI lacks individual test logs, and author-reported measurements were not independently reproduced.

  • Performance evidence covers single-CN MinIO; real S3 latency and multi-CN performance remain unmeasured.

…hile the disk cache thrashes (matrixorigin#32033)

Replaces the fixed rule of the previous commit. Never prefetching whole objects
for the IVFFLAT join back fixed the thrash with a small disk cache, but made the
first pass about three times slower whenever the objects fit the cache.

Each disk cache now measures the share of every whole-object file read before
the file is evicted. When that share stays low it reports thrashing, and reads
marked NarrowRead skip whole-object prefetch and cache the ranges they read; it
stops after a quiet period with no evictions. The plan marks the IVFFLAT join
back as a narrow scan (renamed from skip_full_file_preloads), and the IVFFLAT
entries search reads as narrow. Other scans are unchanged.

Design: docs/design/20261102-thrash-aware-prefetch.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cpegeric cpegeric changed the title perf(ivfflat): read the join back to the base table without whole-object preloads perf(fileservice): prefetch whole objects for narrow scans only while the disk cache keeps them Nov 2, 2026
@matrix-meow matrix-meow added size/L Denotes a PR that changes [500,999] lines and removed size/M Denotes a PR that changes [100,499] lines labels Nov 2, 2026
@cpegeric
cpegeric marked this pull request as ready for review November 2, 2026 19:01
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

✅ Merged into main as 0f149a2 at 20:04 UTC

1 h 2 min in the queue, 1 h 2 min of it CI.

So far

  • ✅ Entered the queue Mon 2 Nov, 19:01 · rule main · triggered by rule Automatic queue on approval for main
  • ✅ Passed · in place
  • ✅ Merged 20:04 · 0f149a2 · 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 merged commit 0f149a2 into matrixorigin:main Nov 2, 2026
@mergify mergify Bot removed the queued label Nov 2, 2026
gouhongshen added a commit to gouhongshen/matrixone that referenced this pull request Nov 2, 2026
…stream claimed 97 as narrow_scan in matrixorigin#32034); take upstream narrow-scan prefetch, virtual-timestamp reads, temporal integer casts, vector shuffle ownership, JSON concat and logtail append-range changes

GitHub reported the PR CONFLICTING (DIRTY): upstream matrixorigin#32034 added
`plan.Node.narrow_scan = 97` while this branch's unmerged additive
`ScanReadRole scan_read_role` sat on field 97.

Semantic resolution, both sides kept:
- upstream keeps `narrow_scan = 97` (shipped on main;
  `plan.pb.go` regenerated from the merged proto with the repository
  protoc/gogofast pipeline plus both postprocessors; toolchain verified
  byte-identical against upstream's committed bindings before use);
- this branch's additive `scan_read_role` moves 97 -> 98; nothing ever
  persisted 97 as scan_read_role (unmerged field), provenance comment
  updated in proto/plan.proto;
- `merged_descriptor_test.go` (upstream's merge-collision contract
  guard) extended to pin Node.narrow_scan=97 / Node.scan_read_role=98
  so a future regeneration cannot silently renumber or drop either;
- all other overlapping files auto-merged with both sides' hunks intact
  (upstream: table_scan/narrow-scan scope+operator plumbing, plan binder
  temporal/int-arg changes, disttae datasource, query_builder/types/
  deepcopy/base_binder/apply_indices).

Upstream batch: matrixorigin#31268 (virtual timestamp reads), matrixorigin#32001 (temporal
integer casts), matrixorigin#32020 (vector shuffle ownership), matrixorigin#32031 (logtail
append ranges), matrixorigin#32038 (python JSON-string concatenation; upstream's
own change, no PR-side code), matrixorigin#32034 (narrow-scan prefetch).
mergify Bot pushed a commit that referenced this pull request Nov 3, 2026
…able (#32040)

## What type of PR is this?

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

## Which issue(s) this PR fixes:

issue #32033

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

Follow-up to #32034. #32034 keeps one thrash signal per disk cache: when one table thrashes, every narrow read switches to range reads, including tables that would fit. This PR keeps one signal per table.

**Design** (`docs/design/20261102-thrash-aware-prefetch.md`, updated):
- Each narrow read tells the disk cache which table it reads for; the cache maps the objects narrow reads touch to their table and tracks the read share of each table's evicted whole-object files.
- A table switches to range reads when that share averages below 0.1 over 4 evictions, or below 0.4 over 16. Of two tables evicting each other, the one using its whole objects least switches first, and the other usually fits after that.
- A table switches back after 30 s with no eviction of its own whole-object files. The period doubles, up to 10 min, when it thrashes again soon after.
- The IVFFLAT pre-filter join back (`JOIN(base, SEMI(search, filter scan))`) is now marked narrow like the plain join back; the filter scan copied from the base scan is not.
- The decision is made in `S3FS.Read` for every narrow read, from the table in the read context; reads of no table are never skipped.
- Scans read with their table id: taken from the table definition, or from the plan node's object reference when the rewritten table definition has none.

**Measurements** (wiki_all 1M, IVFFLAT lists=1000, probe_limit=8, filtered query, MinIO, empty disk cache; 50 cold, 50 warm, 35 s pause, 50 more; avg ms, p95 in brackets):

| Memory / disk | #32034: cold / warm / after pause | this PR: cold / warm / after pause |
|---|---|---|
| 1 / 4 GB (4 runs) | 162 (401) / 46 (223) / 68 | 122–194 (364–604) / 22–31 (60–127) / 45–64 |
| 0.5 / 1.5 GB (3 runs) | 515 / 149 / 136 | 485 / 138 / 197 |
| 1 / 8 GB | 102 / 16 | 82 / 16 |

- At 1 / 4 GB only the base table switches; the entries search keeps whole objects.
- At 0.5 / 1.5 GB both tables switch; cold is better and warm is the same. After the pause two of three runs were worse (210 and 233 vs 129 ms) and one the same (147 vs 142).
- TPC-H SF10 at 1 / 4 GB, two rounds: 44.7 s / 53.4 s, 3,255 S3 requests, against 46.3 s / 51.2 s, 3,362 for #32034. No TPC-H table switches.

**Not in this PR:** marking reads narrow by the share of an object's blocks they select (point and primary-key lookups without a plan mark). Tried and rejected: the TPC-H joins with selective runtime filters became narrow, and TPC-H round one took 64 s with 9,678 S3 requests.

**Tests:** per-table signal unit tests (separation, tiered trip, own quiet period, doubling, deletes, bounds), a real disk cache reporting a group, S3FS per-group decision, the prefetch gate and both data sources, table id reaching the read context from the scan operator, the scope fallback to the object reference, the IVFFLAT entries search, and the pre-filter join back plan mark.

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


Approved-by: XuPeng-SH <[email protected]>
Approved-by: gouhongshen <[email protected]>
Approved-by: fengttt <[email protected]>

This branch was previously deployed

1 inactive deployment
ci — 80bd5fa0 Deployed Nov 2, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants