Skip to content

Refresh reused DataFusion source caches when files are replaced - #12315

Open
lwwmanning wants to merge 9 commits into
developfrom
wm/fix-df-source-replacement-caches
Open

lwwmanning wants to merge 9 commits into
developfrom
wm/fix-df-source-replacement-caches

Conversation

@lwwmanning

@lwwmanning lwwmanning commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

This fixes stale per-file caches in VortexSource in vortex-datafusion when an object is replaced at the same path. The caches are the natural splits, physical schemas, layout readers, segment sources and scan caches that partitions and executions share. The PR also changes when the opener and VortexFormat reuse footers from the DataFusion file metadata cache.

On develop, these per-file caches use the path alone as the key. If a reused source scans a path after the object there was replaced, it can use the old file's row ranges or physical schema. That gives wrong rows, or nulls for columns that the new file added. The footer cache uses the DataFusion check of size and last_modified, which misses a replacement of the same size within the same timestamp when the store provides an e_tag or version.

With this PR, each cache entry stores an ObjectGeneration next to the path: size, last_modified, e_tag, and version. A lookup uses an entry only when all four fields match, and a new generation overwrites the old entry so each cache still holds one entry per path. The footer cache extends DataFusion's is_valid_for check with e_tag and version equality while continuing to cache unconditionally (including for PartitionedFile::new and stores without e_tag/version). The PR also merges the three helpers for the weak-reference caches into one generic shared_weak_entry.

Assisted by Antigravity.

Store an ObjectGeneration derived from ObjectMeta (size, last_modified,
e_tag, and version) alongside each Path-keyed entry in VortexSource and
VortexOpener caches (natural_splits, file_schemas, layout_readers,
segment_sources, and scan_caches). Cache hits verify the generation
under a read lock without allocating, and replacements overwrite the
path entry so memory stays bounded to distinct paths.

When ObjectMeta lacks a reliable generation token, recompute rather than
populating or reading either the per-file caches or DataFusion's
FileMetadataCache, and verify e_tag and version alongside DataFusion's
CachedFileMetadataEntry::is_valid_for check.

Signed-off-by: Will Manning <will@willmanning.io>
@lwwmanning lwwmanning added the changelog/fix A bug fix label Nov 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T13:56:41.429580Z b8e9f5b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@vortex-roborev-bot

vortex-roborev-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

roborev: Combined Review (84c9ebe)

Verdict: No issues found.


Reviewers: claude-code | Synthesis: claude-code, 0s | Total: 1m0s, cost partial ~$0.47

vortex-roborev-bot[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude Opus 5.5 on Will's behalf.

I agree with roborev's first finding, and I think it blocks the merge. PartitionedFile::new(path, size) gives an epoch mtime and no e_tag. So the new reliability check turns off the footer cache and the three shared_* caches for that constructor. The crate's own examples in source.rs and access_plan.rs use it, so each partition of a scan would fetch the footer again. Please keep the path-keyed Weak caches when the metadata is unreliable, and skip only the caches that live longer than one scan.

That change may also fix the clippy failure: shared_layout_reader now has a cognitive complexity of 31, and the limit is 25. The three shared_* helpers are near copies, so one generic helper would remove most of the new branching.

…ersion

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed in f2c17dbfa:

  • Kept the three path-keyed Weak caches (layout_readers, segment_sources, scan_caches) active for unreliable metadata (Option<ObjectGeneration> = None) so concurrent partitions of a scan still share them while a strong reference is alive, skipping only the long-lived caches (file_metadata_cache, file_schemas, natural_splits).
  • Unified the three shared_* helpers into a single generic shared_weak_entry helper, resolving the clippy::cognitive_complexity failure on shared_layout_reader.
  • Also addressed both Codex findings (requiring e_tag or version for reliable generations, and comparing complete arrays via assert_arrays_eq! in test_reused_source_byte_partitions_after_object_replacement).

devin-ai-integration[bot]

This comment was marked as resolved.

…e weak invalidation

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
devin-ai-integration[bot]

This comment was marked as resolved.

…placement test

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
vortex-roborev-bot[bot]

This comment was marked as resolved.

…fied

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
devin-ai-integration[bot]

This comment was marked as resolved.

@codspeed

codspeed Bot commented Nov 29, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 14.36%

Warning

vortex-data has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed.

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

❌ 1 regressed benchmark
✅ 2176 untouched benchmarks
⏩ 164 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Walltime scalar_subtract_neon 12.4 µs 14.4 µs -14.36%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing wm/fix-df-source-replacement-caches (012f014) with develop (4525dfa)

Open in CodSpeed

Footnotes

  1. 164 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review by Gemini 4 Carbon on behalf of @lwwmanning.

Keying the per-file caches on VortexSource and FileMetadataCache by (size, last_modified, e_tag, version) fixes a real stale-read bug on file replacement (and the scalar_subtract_neon walltime blip on CodSpeed is unrelated vortex-array noise). Two issues need to be fixed before merging:

  1. ObjectGeneration::has_reliable_metadata breaks footer, schema, and natural_splits caching for PartitionedFile::new and ETag-less stores:
    • Requiring a non-empty e_tag or version completely disables file_metadata_cache, file_schemas, and natural_splits whenever both are None. Every programmatic caller using PartitionedFile::new(path, size) (including our own doc examples and the two existing opener.rs footer-cache tests this PR had to rewrite from PartitionedFile::new to object_store.head), plus any ObjectStore backend without ETags, now re-fetches and re-parses the footer on every partition of the same scan and re-walks the layout tree per partition in compute_natural_splits.
    • Meanwhile, has_reliable_metadata does not even prevent same-size, same-timestamp in-place overwrite collisions on local disk (LocalFileSystem synthesizes e_tag from "{inode:x}-{mtime:x}-{size:x}", where inode == 0 on Windows and inode is unchanged on Unix in-place overwrite).
    • DataFusion's own CachedFileMetadataEntry::is_valid_for contract (used by ParquetOpener) caches unconditionally on (size, last_modified). Extending that check with e_tag and version equality invalidates same-size, same-timestamp replacements whenever a store supplies e_tag/version, without breaking caching when they are None.
  2. Test suite bloat (+911/-134):
    • Removing the has_reliable_metadata / from_meta_unverified split makes the 175-line test_unreliable_generation_recomputes_instead_of_caching obsolete, and test_replacement_overwrites_bounded_per_file_caches in opener.rs duplicates test_reused_source_byte_partitions_after_object_replacement just to check .len() == 1.

Remediation Plan

  1. vortex-datafusion/src/persistent/cache.rs:
    • Simplify ObjectGeneration to hold (size: u64, last_modified: DateTime<Utc>, e_tag: Option<String>, version: Option<String>) (DateTime<Utc> already implements Eq + Hash), with a single infallible ObjectGeneration::from_meta(meta: &ObjectMeta) -> Self constructor and matches_meta(&self, meta: &ObjectMeta) -> bool. Delete has_reliable_metadata, from_object_meta, and from_meta_unverified.
    • Simplify is_cached_metadata_valid to:
      pub(crate) fn is_cached_metadata_valid(entry: &CachedFileMetadataEntry, meta: &ObjectMeta) -> bool {
          entry.is_valid_for(meta)
              && entry.meta.e_tag == meta.e_tag
              && entry.meta.version == meta.version
      }
    • Trim cache::tests to test_object_generation_distinguishes_all_identity_fields and test_is_cached_metadata_valid (including None e_tag/version matching when (size, last_modified) match).
  2. vortex-datafusion/src/persistent/format.rs & vortex-datafusion/src/persistent/opener.rs:
    • Cache unconditionally in VortexFormat::infer_schema, VortexFormat::infer_stats, VortexOpener::open (file_metadata_cache and file_schemas), and natural_splits_for_file using ObjectGeneration::from_meta, invalidating whenever matches_meta / == is false.
    • Revert the two existing tests in opener.rs back to PartitionedFile::new(file_path.to_string(), data_size) so they continue verifying that PartitionedFile::new populates file_metadata_cache.
    • Delete test_unreliable_generation_recomputes_instead_of_caching and fold the opener.*.len() == 1 assertions from test_replacement_overwrites_bounded_per_file_caches into test_reused_source_byte_partitions_after_object_replacement and test_reused_source_schema_after_object_replacement.
  3. PR description:
    • Remove the claim that files without e_tag or version (such as PartitionedFile::new) are no longer cached.

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed in a746cadcf:

  • Removed has_reliable_metadata / from_object_meta / from_meta_unverified in favor of a single infallible ObjectGeneration::from_meta, and simplified is_cached_metadata_valid to entry.is_valid_for(meta) && entry.meta.e_tag == meta.e_tag && entry.meta.version == meta.version.
  • Restored unconditional caching across VortexFormat::{infer_schema, infer_stats}, VortexOpener::open, and natural_splits_for_file, and reverted the two existing opener.rs tests back to PartitionedFile::new.
  • Deleted test_unreliable_generation_recomputes_instead_of_caching and folded the .len() == 1 bounded-cache assertions from test_replacement_overwrites_bounded_per_file_caches into test_reused_source_byte_partitions_after_object_replacement and test_reused_source_schema_after_object_replacement.
  • Updated the PR description to remove the claim that PartitionedFile::new / tokenless files skip caching.

@robert3005 robert3005 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.

Generation matching fixes replacements within one store. I found a second identity component missing when a VortexSource is reused across stores.

— Codex (OpenAI)

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>

@robert3005 robert3005 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.

I verified the five per-source caches now follow the object-store URL. The normal DataFusion plan also installs a shared footer cache, and that path still has the original collision.

— Codex (OpenAI)

devin-ai-integration[bot]

This comment was marked as resolved.

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Posted by Claude Opus 5.5 on Will's behalf.

The two new commits fix both of Robert's cross-store findings. The Weak store handle is a sound identity key. It keeps the allocation alive, so a dropped store's address cannot be reused, and upgrade fails once the store is gone. DataFusion's default registry returns the same Arc for each lookup, so planned scans still hit the footer cache.

One test gap remains. Nothing checks that the footer cache still hits for the same store. test_open_populates_file_metadata_cache says the second open hits the cache, but it only checks the rows. If matches_store always returned false, every open would read the footer again and the test would still pass. Please assert that the cached entry after the second open is the same Arc as after the first. A miss puts a new entry, so the assertion would fail.

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Added Arc::ptr_eq(&entry.file_metadata, &second_entry.file_metadata) in test_open_populates_file_metadata_cache in 84c9ebe56 to verify the second open hits the same-store cached footer rather than rewriting it.

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 1 new potential issue.

Devin Review

This branch has not been deployed

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

Labels

changelog/fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants