Skip to content

fix: release fragment reuse transitions whose retired destinations are settled downstream - #11290

Merged
Xuanwo merged 1 commit into
mainfrom
lu/chain-aware-frag-reuse-trim
Nov 23, 2026
Merged

Xuanwo merged 1 commit into
mainfrom
lu/chain-aware-frag-reuse-trim

Conversation

@LuQQiu

@LuQQiu LuQQiu commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

After fragments are rewritten, existing index segments may still point to the old fragment IDs. Fragment reuse history (FRI) translates those old row addresses to the current fragments, allowing the index to keep working without rebuilding it immediately.

Once an index directly covers the current fragments, those translation records may no longer be needed. Today, cleanup can retain them forever if the data was rewritten more than once.

For example, all three segments below belong to the same logical index:

Index segments:    A stores {1, 3}; B stores {2, 4}
Rewrite history:   {1, 2} → {10} → {30}
New index segment: C stores {30}
Current fragments: {3, 4, 30}

A can serve 3, B can serve 4, and C can serve 30 directly. Queries no longer need either translation. However, the old cleanup waits for direct index coverage of fragment 10, even though 10 no longer exists, and keeps both records.

With this change, cleanup removes both translation records. The index segments A, B and C are kept.

How the new check works

Start with the fragments that exist now, separately for each logical index:

  1. If an index segment directly covers a current fragment, no translation is needed to reach it.
  2. Otherwise, follow its rewrite history backwards to find old fragments stored in the index's segments. Keep the translation records needed along those paths.
  3. Also keep downstream records required by any retained record, so removing history cannot break a translation chain.

Coverage from one logical index cannot stand in for another's. A segment this build cannot read does not count as direct coverage, but its stored fragment IDs still prevent required history from being discarded. Partially indexed rewrite inputs are retained so that a later segment can complete their coverage. Existing rules for legacy history remain in place, and cleanup refuses to trim history containing transitions this build does not understand.

A related change: if a destination fragment was deleted entirely, with no remaining live descendant needing translation, it no longer keeps its history alive. If another destination still needs that translation, the record stays.

When this runs and what it costs

This is a cleanup operation, not an extra check on every index-segment commit. In the current Sophon integration, GC cleanup and the remap job's cleanup phase call cleanup_frag_reuse_index. Ordinary writes and ordinary index-segment commits do not automatically run this calculation.

Lance also exposes an explicit TrimFragReuse transaction action; a caller must choose to include it. It is not automatically added to ordinary transactions.

The walk remembers each record's result for the duration of this calculation, so a shared transition is evaluated at most once per logical index. This removes the previous repeated scanning of wide transitions. Building and traversing the history still costs work, and a cleanup rebased over a concurrent commit may need to calculate again. This PR does not promise a fixed maintenance latency.

Compatibility

Rust API change: lance_table::system_index::frag_reuse::retention::compute_tagged_retention now requires live_fragments. Callers must pass the current fragment bitmap from the same manifest as indices.

Validation

Tests cover multi-step rewrites, independent logical indices, partial coverage, unreadable segment types, legacy history, deep and wide histories, and concurrent commits. Resolution-count tests verify that shared transitions are evaluated once per logical index; 400 generated histories are compared with a separate reference calculation, which shares the existing legacy predicate.

Dataset tests perform real rewrites and deletes, run cleanup twice, reopen the dataset in a fresh session, and compare indexed queries with scans. They verify both sides: obsolete history is removed, while history needed by a surviving fragment remains.

Executed on head 2db639ed0:

  • lance-table library tests: 1,676 passed.
  • Focused Lance tests (tagged_trim, retention, frag_reuse, deleted_destination): 722 passed, 2 ignored.
  • Formatting and package-scoped clippy: passed.

These results validate the Lance change. The combined Sophon run with the final #11292 revision is separate and is not claimed here.

@LuQQiu

LuQQiu commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions github-actions Bot added the bug Something isn't working label Nov 20, 2026

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

I reviewed this PR and didn't find any bugs. A human look is still worthwhile because the change decides when fragment reuse lineage gets permanently dropped. If the settledness walk wrongly reports a chain as settled, an index loses translations it still needs.

What was reviewed:

  • IndexGroupCoverage::is_settled in retention.rs: the iterative DFS, the per-group memo, cycles treated as unsettled, and one unsettled descendant marking every frame on the stack as unsettled.
  • compute_tagged_retention now builds the node graph before the retention loop. The forward chain closure itself is unchanged.
  • Both callers pass a live set: the action-based TrimFragReuse path (self.fragments) and the maintenance trim, including its recompute on commit retry.
  • Ruled out: the new required live_fragments parameter on the pub compute_tagged_retention breaking downstream callers.
Extended reasoning...

The change rewrites the retention predicate for tagged fragment reuse history in lance-table (retention.rs, about 250 lines) and threads a live-fragment set through both trim paths. It also adds about 430 lines of rstest and dataset-level tests in frag_reuse.rs. It touches no auth, injection or data-exposure surface. However, an over-eager trim permanently drops lineage that index readers need to translate row addresses, so a mistake would break correctness. The deciding factors were the algorithmic subtlety of the memoised settledness DFS and the correctness risk of removing history, not any bug that was found.

@LuQQiu
LuQQiu force-pushed the lu/chain-aware-frag-reuse-trim branch from 0d8d843 to 2c2cc03 Compare November 20, 2026 05:11
@LuQQiu

LuQQiu commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

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

I reviewed the current push and found no bugs. A trim deletes history records for good, and this change lets it drop more of them, so a human should still confirm the new "settled" rule matches how the reader resolves coverage.

What was reviewed:

  • The iterative is_settled walk in retention.rs. Memo entries end as Settled or Unsettled on every exit path. A cycle or a retired destination with no consumer counts as unsettled, so in those cases the transition is kept.
  • Where the live set comes from. The maintenance trim uses dataset.fragment_bitmap, recomputed on the rebase retry. The action path uses the fragments the operation publishes, the same snapshot as self.indices.
  • Not checked: I did not run the new unit or integration tests, and I did not check against the reader code that direct coverage drops an older segment's rows for retired fragments.
Extended reasoning...

The change rewrites the fragment reuse trim's retention predicate in lance-table and passes live fragments to it from the maintenance trim and the action-based commit path. Tests are added at both the unit and dataset level. It touches no auth, crypto or injection surface. Approval was ruled out because the change makes a destructive, irreversible trim keep fewer records. Whether that is safe depends on reader-side direct-coverage precedence, which this diff does not change and which needs a design-level check. No CODEOWNERS entry covers the changed files.

@LuQQiu
LuQQiu marked this pull request as ready for review November 20, 2026 19:56

@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 repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 20, 2026
@LuQQiu

LuQQiu commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@LuQQiu
LuQQiu force-pushed the lu/chain-aware-frag-reuse-trim branch from 2c2cc03 to f8b9e17 Compare November 20, 2026 22:23
@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 20, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 20, 2026

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

Code review found no issues

No high-confidence issues detected in this change.

@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 20, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. and removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 20, 2026
…e settled downstream

The tagged fragment reuse trim kept a transition whenever an index held
provenance in its sources and did not directly cover every destination.
A destination that a later transition had already rewritten can never be
directly covered again, so once a chain formed (stable partition followed
by compaction, for example) the transitions of that chain were retained
forever, even after the index covered every live fragment at the end of
the chain directly.

Retention now starts from the current fragments, as the reader does. For
each logical index, every live fragment that a record produces and the
index does not cover directly is walked back through its producers,
never crossing a fragment a usable segment covers directly. A visited
transition is a seed when one of its sources is in the index's stored
provenance or an upstream producer is. Final retention is the existing
forward closure of those seeds and of the legacy versions the v0
predicate keeps.

A transition none of whose destinations leads to a live fragment outside
direct coverage is released. This includes a destination that was
deleted outright, which the previous rule kept. A fan-in with only part
of its sources in provenance stays retained. The walk is iterative,
resolves each record at most once per logical index, and takes the live
fragment set of the manifest the index metadata was read from, on both
the maintenance trim and the action-based commit path.

Co-Authored-By: Claude <[email protected]>
@LuQQiu
LuQQiu force-pushed the lu/chain-aware-frag-reuse-trim branch from f8b9e17 to 2db639e Compare November 21, 2026 00:06
@LuQQiu

LuQQiu commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@claude review

@lance-gatekeeper lance-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 21, 2026

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

Code review found no issues

No high-confidence issues detected in this change.

@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 per-transition walk removes the previous quadratic rescan. Retention follows the reader's per-index direct-coverage rule; focused tests pass for settled chains, deleted destinations, fresh-session queries, and rebasing.

Please mark this PR with the breaking-change label: lance_table::system_index::frag_reuse::retention::compute_tagged_retention now requires live_fragments, so Rust callers must pass the fragment bitmap from the same manifest as indices.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 21, 2026
@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. and removed K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 21, 2026
@Xuanwo
Xuanwo merged commit d478d29 into main Nov 23, 2026
54 of 55 checks passed
@Xuanwo
Xuanwo deleted the lu/chain-aware-frag-reuse-trim branch November 23, 2026 06:37
Xuanwo added a commit that referenced this pull request Nov 24, 2026
## Problem

Every scalar index merge repeated the same field and declaration
validation, row-domain guard, per-segment filters, destination store
allocation, source opening/downcast, and `IndexMetadata` assembly.
Bitmap also depended on btree for its empty new-data stream, and
append's merge dispatch rebuilt the same filters per arm.

## Change

Add `scalar/segment_merge.rs` to own the shared merge inputs and output:

- empty groups, keyed field/path and same-declaration checks
- per-type stable-row-id legacy guard, preserving
Bitmap/BTree/LabelList/MinHash bottom-level mixed-domain errors;
RTree/NGram keep the shared-domain guard
- per-segment filters and coverage unions
- typed source opening/downcast
- UUID/store allocation and one metadata constructor
- the empty value/address stream used by pure consolidation

All scalar merge modules now use it; Bitmap no longer reaches into
btree. `append.rs` constructs filters once for the simple merge
primitives. MinHash keeps its historical admission rule: non-stable
tables can merge disjoint v0 and v1 segments into v1, while stable
tables still reject legacy input.

No public API, persisted format, or Python wrapper changes. #11273,
#11231 and #11280 are complementary and not dependencies; their
behaviors are not pulled in here. Overlap is mechanical in the same
scalar modules and append dispatch. Current main's #11290 only changes
tagged FRI retention bookkeeping and #11391 changes commit
configuration; neither changes a caller contract used by this change.

## Validation

- `cargo fmt --all -- --check`: pass
- `cargo clippy --all --tests --benches --profile ci -j 4 -- -D
warnings`: pass (only existing lance-linalg build-script notices)
- Runtime on head `d23cbafd5`, one test executable, 49 unique tests:
- scalar MinHash module: 16 passed, including mixed v0-layout/v1
disjoint merge, commit, reopen and `fast_search` for both stable and
non-stable datasets, plus the historical-layout legacy regression
  - `index::tests::merge`: 8 passed
- append/optimize: BTree 9, Bitmap 7, NGram 1, BloomFilter 4, FTS 3,
scalar stale-row 1

No released MinHash v0 fixture exists; the v0 layout is reconstructed
from a current segment (`_rowid`, version 0) by the test helper. The
test exercises real merge, commit, reopen and search rather than mocked
metadata.

Remaining coverage: the full workspace test matrix and a Python wheel
were not run. The Python wrapper is unchanged, and the mixed historical
layout is not constructible through it.

Clean merge tree with current main
`e90fe4b8e7c8c819bb51171a43fc94d01b40e2c7`:
`bdbb1029cd3e7ab3a54ac3036d258cd4e6dcbcaa`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change 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