Skip to content

fix(cache): serve cached entries only to the file they came from - #526

Merged
anoop-narang merged 1 commit into
datafusion-contrib:mainfrom
hotdata-dev:upstream/file-id-aliasing
Oct 16, 2026
Merged

anoop-narang merged 1 commit into
datafusion-contrib:mainfrom
hotdata-dev:upstream/file-id-aliasing

Conversation

@anoop-narang

@anoop-narang anoop-narang commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

After a process has read 65,536 distinct parquet files, the next file is served the first file's cached data.

Why

A cache key keeps only 16 bits of the file id, but file ids come from a counter that only climbs (register_or_get_file). File 65,537 gets id 65536, which narrows to 0, so its keys are the first file's keys. Its reads then return the first file's arrays: wrong rows if the column types match, a panic in RecordBatch::try_new if they don't. The only guard is a debug_assert!, so release builds hit this silently.

Repro

files_whose_keys_collide_do_not_share_entries caches a batch for one file, registers 65,535 more, and reads the same batch through the 65,537th. On main it fails in release with "a file was served another file's cached data" (in debug, at the debug_assert!).

Fix

Keep the full file id with every cached entry and check it on every read. A mismatch is a cache miss, so the reader falls back to parquet. A write replaces whatever held the key.

  • Memory: each index slot stores the file id next to the entry.
  • Disk: each object gets an 8-byte file-id trailer, and the disk budget counts it. Disk keys don't change, so a late eviction or flush that overwrites a newer file's object just causes a miss for that file.

The key stays 64 bits because congee supports only 8-byte keys.

API change

insert, get, eval_predicate, prefetch, try_read_liquid, contains, read_entry and eval_predicate_on_entry on LiquidCache take an identity: u64. Callers with unique keys can pass any constant (the README and examples pass 0).

Cost: 8 bytes per entry and per disk object, plus one comparison per read.

Tests

  • New: the repro, plus core tests for memory and disk mismatches.
  • test_cache_entry_id_new_panic_file_id required the debug_assert! panic. It is replaced by a test that file ids keep their low 16 bits.

Follow-ups

  1. Replacing a disk entry (by hydration, or now by another file) doesn't release its disk budget. This predates this PR; I'll send a separate fix.
  2. The file-id map keeps every file ever read. With this PR, ids can safely be reused, so the map can drop a file once none of its data is cached. That also stops two active files that share a 16-bit id from repeatedly evicting each other.

A cache key keeps 16 bits of the file id, while file ids come from a
counter that only climbs. The 65,537th distinct file a process registers
therefore computes the same keys as the first, and reads the first
file's cached data: silently wrong rows when the column types match, a
panic in `RecordBatch::try_new` when they do not. The narrowing is
guarded by `debug_assert!` only, so release builds carry on.

Record the full file id ("identity") with every cached entry, in the
index and as an 8-byte trailer on every object written to disk, and
compare it on every lookup. A mismatch reads as a miss, so the caller
falls back to parquet and gets correct data. An insert replaces whatever
held the key, so the newer file can still cache its own data.

The trailer is what makes the disk side safe without changing how
objects are keyed: an eviction or flush that lands after the key changed
hands leaves bytes that name the old identity, and the new owner's read
of them misses. It follows the payload so the payload keeps its
alignment, and the disk budget is charged for it.

`LiquidCache::insert`, `get`, `eval_predicate`, `prefetch`,
`try_read_liquid`, `contains`, `read_entry` and `eval_predicate_on_entry`
take the identity as a new argument. Callers whose keys are already
unique can pass any constant.

The `should_panic` test for an oversized file id pinned the debug-only
guard; it is replaced by one pinning the narrowing the identity check
relies on.
@codecov-commenter

codecov-commenter commented Oct 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.83378% with 23 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@c946ea4). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/core/src/cache/core.rs 90.33% 10 Missing and 13 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #526   +/-   ##
=======================================
  Coverage        ?   81.38%           
=======================================
  Files           ?       80           
  Lines           ?    13666           
  Branches        ?    13666           
=======================================
  Hits            ?    11122           
  Misses          ?     2216           
  Partials        ?      328           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anoop-narang

Copy link
Copy Markdown
Collaborator Author

@XiangpengHao I am merging this as a fix as current main is giving wrong results on long running processes. Let me know your thoughts on this. There is more to be done to make liquidcache better for long running processes.

@anoop-narang
anoop-narang merged commit 25358ff into datafusion-contrib:main Oct 16, 2026
12 checks passed
@XiangpengHao

Copy link
Copy Markdown
Collaborator

LGTM thank you @anoop-narang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants