Skip to content

fix: preserve fragment reuse mappings across concurrent maintenance - #9620

Merged
wjones127 merged 4 commits into
lance-format:mainfrom
brendanclement:brendan/fri-preserve-mappings
Oct 5, 2026
Merged

wjones127 merged 4 commits into
lance-format:mainfrom
brendanclement:brendan/fri-preserve-mappings

Conversation

@brendanclement

@brendanclement brendanclement commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes two cases where deferred compaction could lose Fragment Reuse Index (FRI) mappings that indexes still needed, causing indexed queries to miss rows.

The FRI watermark now reflects the table version immediately before the rewrite commits. Cleanup, index remapping, and segment merging consistently treat indexes built at or before that version as needing the mappings.

Commits also check the latest FRI history before replacing it. A stale compaction or cleanup returns a retryable conflict when another writer has changed the history. Compactions can still commit over cleanup that only removed versions, preserving the remaining history. External FRI details are cached, and uncontended compactions write them once. Index maintenance also preserves the source file location when keeping an index on a shallow clone.

Validation

Tests cover concurrent compactions from separate sessions, cleanup races, commit retries, watermark boundaries, shallow clones, and cached and uncached reads. They verify row translations and indexed results after reopening the dataset.

… base_id on Keep

A fragment reuse version's dataset_version is its watermark: an index
built at or before it still holds the row addresses the rewrite moved.
The value was the committing handle's version, so an index built after
that but before the rewrite published counted as caught up, and cleanup
trimmed a mapping it still needed. Queries through that index then lost
rows.

Define the watermark as the version immediately before the rewrite
publishes, which matches the proto's own definition. finish_rewrite
restamps the rewrite's own version with it on every commit attempt of a
v0 entry, so retries restamp too; older versions keep their stamps.
Tagged histories carry no per-version watermark. Detached commits
bypass the stamp, and versions written earlier keep their old values.

Compare against the watermark inclusively everywhere staleness is
decided: is_index_remap_caught_up (cleanup), remap_index, and
fragment_reuse_affects_segment (segment merge and the NGram rebuild
checks). An index built at exactly the watermark is still stale.

RemapResult::Keep reuses the entry's files, so it now keeps the source
base_id; a shallow clone's index otherwise points at files it does not
have.
A deferred compaction's Rewrite and the reuse index cleanup each replace
the v0 fragment reuse entry wholesale. The transaction file does not
carry that entry, so a rebase that reads a concurrent Rewrite back from
its file, as a separate session always does, could not see its
mappings, and the second replacement silently discarded them.

Each replacement now records the entry of the dataset it is committed
through: the entry the compaction extended or the cleanup trimmed. The
commit loop never moves that handle between attempts, so on every
attempt the rebase compares the same base with the latest manifest's
entry:

- unchanged: publish as before, stamped as the previous commit defines;
- a compaction over a trim of its base: publish the current versions
  plus its own, which keeps every mapping the current history has;
- anything else, including creation and removal: RetryableCommitConflict.
  Commit the results again through a dataset at the latest version.

A replacement that is not its base plus one version of its own, such as
one built from an older snapshot than the dataset it is committed
through, is refused as InvalidInput.

This replaces the Rewrite-vs-Rewrite rule that depended on the
transaction cache, and both history merges (the cleanup's appended
versions and the compaction's retain), which could drop or resurrect
versions. A distributed commit through a handle newer than the tasks'
read version no longer loses a mapping recorded in between. Tagged
histories keep their validated assembly and conversion, and eager
compaction is unchanged.

A cleanup racing an ordinary index creation is a different race and is
not addressed here.
@brendanclement
brendanclement force-pushed the brendan/fri-preserve-mappings branch from 7e5f63a to bf9b96b Compare September 30, 2026 14:58
commit_compaction reserves fragment ids in a commit of its own without
moving the handle, then stamped the new v0 reuse version and its entry with
the handle's version. The Rewrite therefore always published one version
past its stamp, so finish_rewrite restamped and rebuilt the entry on every
commit: for an external history a second full details.binpb write, leaving
the first one unreferenced.

Stamp both with the version the reservation committed at, and give the
entry the fragment bitmap a rebuild or a cleanup publishes, every version's
new fragments, so an uncontended commit keeps the entry as built and
publishes the same entry a restamp would. The restamp on each attempt still
handles a commit that lands after the reservation.
Every commit attempt of a deferred compaction loads its own new entry and
the entry it was built from to check the history, and each load of an
external entry read and decoded the whole details file. Both files had just
been read or written by the same process.

Cache the decoded details of an external entry in the session index cache,
keyed by the file's store, resolved path, offset and size (a shallow clone's
entry resolves to the source dataset's file), and populate it when the file
is written, in the stamp order the encoding uses. A miss reads the file as
before, and the history check runs on the loaded details either way.
@brendanclement
brendanclement marked this pull request as ready for review October 1, 2026 19:35

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@lance-gatekeeper lance-gatekeeper 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.

✅ Gate recommendation: approve.

The watermark and history checks address the demonstrated row-loss paths. Rejecting stale v0 replacements keeps recovery explicit: reopen the latest dataset and recommit the retained compaction results.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 1, 2026
@wjones127
wjones127 merged commit 2410e22 into lance-format:main Oct 5, 2026
38 of 40 checks passed
westonpace added a commit that referenced this pull request Oct 5, 2026
A test literal still used the old conflicting_frag_reuse_indices field
name after it was renamed to frag_reuse_base in #9620, breaking the
build for every PR rebased onto main.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
LuQQiu added a commit that referenced this pull request Oct 5, 2026
…itions

A segment that fully covers all sources of a fragment-reuse transition is
remapped through the transition and its fragment bitmap restamped to the
destinations, for both ordered-compaction and stable-partition transitions
(the same semantics as the existing v1 compaction remap, extended to
stable-partition histories). A partially covered segment is skipped
cleanly; its coverage keeps deriving through the ledger and a rebuild
catches it up. A partial hop after a stable-partition restamp blocks the
remap instead of dropping coverage.

The remap streams through a batch translator instead of a materialized
map: the planned hops translate at most 64K addresses per call in ledger
order, a withdrawn or ceded contribution is dropped from the remapped file
so file and bitmap agree, and eligibility is decided from the coverage plan
before any file is read. The in-memory remap API keeps main's signatures;
remap_streaming is a provided method whose default materializes a complete
mapping under a budget that accounts for hash-table rounding, or declines
with RemapUnavailable so maintenance skips that segment only.

A segment that predates a v0 deferred compaction has a persisted bitmap
naming the compaction's destinations while its file still holds the source
addresses. The tagged planner now derives an effective provenance first,
unwinding lifted legacy transitions whose stamp is newer than the segment's
in reverse lineage order, so such a segment is remapped from the sources
its file holds instead of losing those rows once a later stable partition
consumes the destinations. The legacy watermark is the version immediately
before the rewrite committed (#9620), so the boundary is inclusive: a segment
stamped at the watermark still predates the compaction and is unwound, as
the v0 remap and cleanup rules already treat it.

Claude-Session: https://claude.ai/code/session_01DgAshYD7wVzPVdjXRuWPPs

Co-Authored-By: Claude Fable 5.1 <[email protected]>
LuQQiu added a commit that referenced this pull request Oct 5, 2026
…itions

A segment that fully covers all sources of a fragment-reuse transition is
remapped through the transition and its fragment bitmap restamped to the
destinations, for both ordered-compaction and stable-partition transitions
(the same semantics as the existing v1 compaction remap, extended to
stable-partition histories). A partially covered segment is skipped
cleanly; its coverage keeps deriving through the ledger and a rebuild
catches it up. A partial hop after a stable-partition restamp blocks the
remap instead of dropping coverage.

The remap streams through a batch translator instead of a materialized
map: the planned hops translate at most 64K addresses per call in ledger
order, a withdrawn or ceded contribution is dropped from the remapped file
so file and bitmap agree, and eligibility is decided from the coverage plan
before any file is read. The in-memory remap API keeps main's signatures;
remap_streaming is a provided method whose default materializes a complete
mapping under a budget that accounts for hash-table rounding, or declines
with RemapUnavailable so maintenance skips that segment only.

A segment that predates a v0 deferred compaction has a persisted bitmap
naming the compaction's destinations while its file still holds the source
addresses. The tagged planner now derives an effective provenance first,
unwinding lifted legacy transitions whose stamp is newer than the segment's
in reverse lineage order, so such a segment is remapped from the sources
its file holds instead of losing those rows once a later stable partition
consumes the destinations. The legacy watermark is the version immediately
before the rewrite committed (#9620), so the boundary is inclusive: a segment
stamped at the watermark still predates the compaction and is unwound, as
the v0 remap and cleanup rules already treat it.

Claude-Session: https://claude.ai/code/session_01DgAshYD7wVzPVdjXRuWPPs

Co-Authored-By: Claude Fable 5.1 <[email protected]>
westonpace pushed a commit that referenced this pull request Oct 14, 2026
…-ID tables (#9611)

Records row-address mappings during deferred compaction on stable-row-ID
tables. Supports tables with no indexes or only compatible address-based
indexes, currently ZoneMap and BloomFilter.

Every rewritten group is recorded in the Fragment Reuse Index (FRI),
including groups with no index coverage. This lets feature-engineering
jobs translate addresses from before compaction to the rows’ new
locations. Stable row IDs are preserved.

Requires defer_index_remap and remains debug-only. Depends on #9620.

Next steps
- Keep recording on subsequent compactions once enabled.
- Retain mappings for running jobs, even when indexes no longer need
them.
- Complete the remaining concurrency protections and enable release
support.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants