Skip to content

fix(mem_wal): credit catch-up only for fragments and MemWAL the read version proves - #10140

Merged
Xuanwo merged 6 commits into
lance-format:mainfrom
hamersaw:fix/mem-wal-credit-emptied-table
Nov 25, 2026
Merged

Xuanwo merged 6 commits into
lance-format:mainfrom
hamersaw:fix/mem-wal-credit-emptied-table

Conversation

@hamersaw

@hamersaw hamersaw commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

MemWAL index catch-up coverage is derived at commit time as "the index spans every fragment of the read version". An empty fragment list was refused outright, because a manifest written before #8438 (where UpdateMemWalState published no fragments) looks identical, and there the SSTables are the last copy of the rows.

The cost of that refusal: a table emptied by deletes never credits any index. trim_eligible_generation on the WAL side then never sees catch-up for the maintained indexes, so the compacted SSTables are never trimmed, and the catch-up planner reschedules a no-op job every few seconds, indefinitely. optimize_indices returns early because mem_wal_catch_up_would_advance is false. A checkpoint waiting on the trim never returns. It un-wedges only when a later write adds a fragment. It reproduces deterministically: empty base, enable MemWAL, upsert some keys, delete them all, checkpoint. It also hits a table that started with rows once the compaction deletes the last one.

This is reachable on the normal RTE path, not only through out-of-band deletes. The compaction merge_insert applies the WAL's delete tombstones and drops fully deleted fragments in the same commit that advances compacted_sstables. The catch-up for that generation therefore always reads an empty table, even with compaction and catch-up strictly serialized.

The fix removes the empty-fragments guard, so an empty read version is trivially covered and credited. The state the guard protected requires a standalone UpdateMemWalState committed by Lance <= 10.x: the first in-repo caller (#8263) and the fix (#8438) both first shipped in v11.0.0. No production table was written that way.

An earlier revision trusted empty manifests by writer version instead. That does not work: every commit restamps writer_version, so a metadata-only commit such as UpdateConfig carries an untrusted empty fragment list past the check.

Tests: an_emptied_table_is_credited in mem_wal_index_coverage replaces the writer-version cases; cargo test -p lance-table --lib mem_wal_index_coverage (30 passed); cargo clippy --all --tests --benches -- -D warnings clean. The end-to-end repro (checkpoint returns, SSTables trim) was verified against the earlier revision, which credited the same case.

🤖 Generated with Claude Code

Index catch-up coverage is derived at commit time as "the index spans every
fragment of the read version". An empty fragment list was refused outright,
because a manifest written before lance-format#8438 (UpdateMemWalState publishing no
fragments) looks the same and its SSTables are the last copy of the rows.

The cost was that a table emptied by deletes never credited any index, so its
compacted SSTables were never trimmed, the WAL pod rescheduled a no-op
catch-up job every few seconds, and a checkpoint waiting on the trim never
returned.

Trust an empty read version when the manifest was written by a Lance release
that has the lance-format#8438 fix (12.0.0 or later); keep refusing older or unknown
writers.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

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

@github-actions github-actions Bot added the bug Something isn't working label Oct 18, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Oct 18, 2026
hamersaw and others added 2 commits November 25, 2026 02:40
The writer-version cutoff could not tell a genuinely emptied table from a
pre-lance-format#8438 one: every commit restamps the writer version, so a metadata-only
commit carried an untrusted empty fragment list past the check.

Drop the empty-fragments guard instead. Within the RTE path the table is
emptied by the compaction merge_insert itself, so its catch-up always reads an
empty table and must be credited. The pre-lance-format#8438 state requires a standalone
UpdateMemWalState from Lance <= 10.x, which no production table carries.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@hamersaw hamersaw changed the title fix(mem_wal): credit index catch-up on a genuinely emptied table fix(mem_wal): credit index catch-up on an emptied table Nov 25, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 25, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 25, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Technical blocker. On b17fddf065, a transaction that read an empty table can rebase and record catch-up for a generation the current index still misses.

I flushed a matching row to an SSTable and used merge_insert to mark generation 1 compacted while the base FTS index still missed its fragment. I deleted all rows through one handle, restored the pre-delete snapshot through another, then called update_config on the stale empty handle. After reopening, the row was present and index_catchup had advanced from None to Some(1). Base index-only FTS returned zero rows. Ordinary LSM FTS found the row through its scan fallback. The incorrect watermark can make generation 1 eligible for trimming.

I also reproduced this across MemWAL reinitialization with a new dir_id and a reused shard and generation. Please preserve the checkpoint fix for genuinely empty tables, ensure the empty-table proof still applies after rebase and to the same WAL, and cover both stale-handle cases. The empty-read rule in docs/src/format/table/mem_wal.md should describe when credit is safe.

@hamersaw hamersaw changed the title fix(mem_wal): credit index catch-up on an emptied table fix(mem_wal): credit catch-up only for fragments and MemWAL the read version proves Nov 25, 2026
…h-up credit

A stale transaction could credit index catch-up after rebasing over a
restore that brought back fragments its read version had lost, or after
the MemWAL was re-initialized with reused shard ids and generations.

Also require coverage of every live fragment no newer than the read
version's max_fragment_id, and only use the read version's compacted
progress when its dir_id matches.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 25, 2026
@github-actions github-actions Bot added A-format On-disk format: protos and format spec docs format-change A change to the format spec, which requires a vote. Remove if minor (e.g. fixing typo). labels Nov 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Important

Format specification vote

This PR modifies the Lance format specification, so it requires 3 binding +1 votes from PMC members (excluding the proposer), at least one of them on the latest commit, and a minimum 72-hour voting period, weekends excluded, before it can merge. Vote by approving this PR (+1) or requesting changes (−1, a veto). See the voting process.

Approvals carry over across pushes, so a rebase or a typo fix does not send everyone back to re-vote. Whoever approves the latest commit is vouching that nothing substantive has changed since the earlier approvals; if something has, ask for fresh votes.

Status: ❌ Blocked — 0 of 3 required approvals

Approvals none (0/3)
Latest commit approved by none — one PMC member must approve the latest commit
Vetoes none
Voting period ends Mon 2026-11-30 14:31 UTC (06:31 PST)

Updated automatically by the format-spec vote gate, which re-checks every 15 minutes — just voted? Re-check now (press Run workflow; leave the input blank to re-check every open format PR). A PMC member may apply the format-waived label to waive the vote for a trivial edit (typo, wording, formatting).

@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: request changes.

The stale-read concern is only partly addressed: rewriting restored rows defeats the new fragment-ID cutoff. Preserve empty-table catch-up, but require its emptiness proof to remain valid at publication; requiring the final fragment list to remain empty is the minimal conservative option. The earlier legacy-writer finding remains withdrawn.

Please mark this PR with the breaking-change label: lance_table::transaction::Transaction::apply_mem_wal_index_coverage now requires final_fragments, so Rust callers must pass the final fragment list.

let read_max = state.manifest.max_fragment_id();
let restored = final_fragments
.iter()
.filter(|fragment| read_max.is_some_and(|max| fragment.id <= max));

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.

A row rewrite can move restored rows above this cutoff, so an empty read still credits an index that contains none of the live rows. Starting with fragment 0 compacted through generation 5, delete all rows, restore the pre-delete version, and rewrite those rows with UpdateBuilder. They now occupy fragment 1, which this filter excludes. A metadata transaction reading the empty version then publishes index_catchup = 5 despite the index's empty coverage. That falsely authorizes retiring the generation's SSTables while its rows remain absent from the base index, and subsequent unchanged commits carry the position forward.

An allocation high-water mark does not prove row provenance. For an empty read, require the final table to remain empty, or establish that every restored row remains covered through intervening rewrites. Update the matching specification rule and extend the restore regression with this case.

Reproducer

Add this test to index::mem_wal::tests in rust/lance/src/index/mem_wal.rs, using its existing fixtures. Run cargo test -p lance --lib gate_a_restored_fragment_rewritten_after_the_read_must_not_be_credited. On this head the final assertion fails with Some(5) instead of None; the preceding assertions verify that ten rows remain live, their fragment IDs exceed the read high-water mark, and no catch-up was recorded before the stale commit. This executes the production commit path; it does not execute downstream SSTable deletion.

#[tokio::test]
async fn gate_a_restored_fragment_rewritten_after_the_read_must_not_be_credited() {
    let shard = Uuid::new_v4();
    let dataset = compacted_dataset(shard, 5).await;
    let txn = Transaction::new(
        dataset.manifest.version,
        Operation::CreateIndex {
            new_indices: vec![index_over("idx", &[])],
            removed_indices: vec![],
        },
        None,
    );
    let mut dataset = CommitBuilder::new(Arc::new(dataset))
        .execute(txn)
        .await
        .unwrap();
    let before_delete = dataset.manifest.version;
    dataset.delete("true").await.unwrap();
    let stale_read = dataset.manifest.version;
    let read_max = dataset.manifest.max_fragment_id().unwrap();
    assert!(dataset.get_fragments().is_empty());

    let mut restored = dataset.checkout_version(before_delete).await.unwrap();
    restored.restore().await.unwrap();
    let rewritten = crate::dataset::UpdateBuilder::new(Arc::new(restored))
        .set("b", "b + 1")
        .unwrap()
        .build()
        .unwrap()
        .execute()
        .await
        .unwrap()
        .new_dataset;
    assert!(rewritten.manifest.fragments.iter().all(|f| f.id > read_max));
    assert_eq!(rewritten.count_rows(None).await.unwrap(), 10);
    assert_eq!(catch_up_generation(&rewritten, "idx").await, None);

    let committed = CommitBuilder::new(rewritten)
        .execute(update_config_txn(stale_read))
        .await
        .unwrap();
    assert_eq!(committed.count_rows(None).await.unwrap(), 10);
    assert_eq!(catch_up_generation(&committed, "idx").await, None,
        "restored rows remain unindexed after they move above the read high-water mark");
}

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.

Still reproducible on c6eb2e42: the regression above records Some(5) rather than None while ten restored, rewritten rows remain live and unindexed. The new specification excludes restore, but restore_old_manifest still publishes it. Enforce the restriction at that shared publication boundary for both snapshots, as described in the current maintainer review.

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.

Fixed in 8ce4f8a7: restore_old_manifest now rejects restore before publication when either the current or target snapshot has MemWAL. This prevents the restore step required by the reproducer while preserving ordinary restore and catch-up after deleting all rows under the same WAL.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Nov 25, 2026
The fragment-id cutoff could not prove row provenance: a rewrite after a
restore moves restored rows above it. Restore never rolls back MemWAL
shard state, so it is unsupported on a table with a MemWAL; say so in the
spec and drop the cutoff and the final_fragments parameter it needed.

Keep crediting emptied tables and the dir_id check on read-version
progress.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 25, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Technical blocker. The new spec rules out restore on MemWAL tables because restoring a base snapshot cannot roll back shard state. On c6eb2e42, Dataset::restore still submits Operation::Restore, and the shared commit path through restore_old_manifest has no MemWAL check. Embedded LanceDB uses that same API. A caller can still publish the restore that the coverage rule assumes cannot happen.

Please reject the restore before publication when either the current or target snapshot has MemWAL. Cover both cases with a clear error and an unchanged dataset version. Keep ordinary restore between non-MemWAL snapshots and catch-up on a genuinely emptied table under the same WAL as positive controls.

I traced the restore path on this revision. The earlier runtime results were on ff6be397.

lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-changes Latest Gatekeeper recommendation requests changes. label Nov 25, 2026
Restore rolls back the base table and MemWAL index but not shard state,
and catch-up credit assumes it never happens. Refuse it with
NotSupported before publication when the current or target version has
a MemWAL, in restore_old_manifest so both the attached and detached
commit paths are covered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lance-gatekeeper lance-gatekeeper Bot removed the K-changes Latest Gatekeeper recommendation requests changes. label Nov 25, 2026

@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 restore blocker is fixed: the shared commit path now rejects restore whenever either snapshot has MemWAL. This closes the stale-empty-read path while preserving catch-up for genuinely emptied tables and ordinary non-MemWAL restore. The earlier legacy-writer finding remains withdrawn.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Nov 25, 2026

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for addressing the restore blocker in 8ce4f8a7. restore_old_manifest now checks both snapshots before publication. The current-only and target-only MemWAL cases both rejected restore and left the latest version unchanged. An ordinary restore advanced one version, a genuinely emptied table earned catch-up, and a stale read did not credit a reinitialized WAL. Linux test run

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@hamersaw At 8ce4f8a7, dir_id identifies the same MemWAL, and the coverage check accepts an empty fragment list. Before #8438, a standalone UpdateMemWalState could leave that list empty while the retained SSTables still held the only copy of the rows. What keeps such a manifest from earning catch-up, and could you document the reason in mem_wal.md, including whether it relies on which writers shipped?

@Xuanwo Xuanwo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

After checking again, it's not really a format change.

@Xuanwo Xuanwo added the format-waived Author attests no format vote is needed label Nov 25, 2026
@Xuanwo
Xuanwo merged commit 0944680 into lance-format:main Nov 25, 2026
48 of 56 checks passed
@hamersaw
hamersaw deleted the fix/mem-wal-credit-emptied-table branch November 25, 2026 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-format On-disk format: protos and format spec docs bug Something isn't working format-change A change to the format spec, which requires a vote. Remove if minor (e.g. fixing typo). format-waived Author attests no format vote is needed K-approved Latest Gatekeeper recommendation permits acceptance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants