Skip to content

Fix unaccessed RI key data lost after second checkpoint purge#1756

Draft
tiagonapoli wants to merge 3 commits intodevfrom
tiagonapoli/fix-rangeindex-unaccessed-purge
Draft

Fix unaccessed RI key data lost after second checkpoint purge#1756
tiagonapoli wants to merge 3 commits intodevfrom
tiagonapoli/fix-rangeindex-unaccessed-purge

Conversation

@tiagonapoli
Copy link
Copy Markdown
Collaborator

Summary

After checkpoint recovery, if an RI key is never accessed before a second checkpoint is taken, its data is permanently lost.

Root Cause

BfTree restore is lazy — trees are only restored when accessed. But PurgeOldCheckpointSnapshots (called at CheckpointCompleted) deletes all snapshot.*.bftree files that don't match the current checkpoint token. Since the unrestored tree was never registered in liveIndexes, SnapshotAllTreesForCheckpoint skips it — no new snapshot is created. The old snapshot is then purged, and no flush.bftree exists either. Next access fails with ERR range index not found.

Scenario:

  1. Create RI keys, insert data, checkpoint (token1) — creates snapshot.{token1}.bftree
  2. Recover — stubs get FlagRecovered=true, TreeHandle=0
  3. Access some keys (restored, registered in liveIndexes) — skip others
  4. Second checkpoint (token2):
    • SnapshotAllTreesForCheckpoint: only snapshots trees in liveIndexes — unaccessed trees skipped
    • PurgeOldCheckpointSnapshots: deletes snapshot.{token1}.bftree (doesn't match token2)
  5. Access unaccessed key — snapshot file gone, no flush.bftree either — data lost

Testing

  • Added RIUnaccessedKeyAfterRecoveryAndSecondCheckpointTest — currently fails with ERR range index not found, confirming the bug
  • Fix TBD

Tiago Napoli and others added 3 commits April 28, 2026 21:36
After recovery, if an RI key is never accessed before a second checkpoint:
- SnapshotAllTreesForCheckpoint skips it (not in liveIndexes)
- PurgeOldCheckpointSnapshots deletes the old checkpoint snapshot
- Next access fails with 'range index not found' — data is lost

This test is expected to FAIL (no fix included).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Verify disk file state at each stage: snapshot files after first
checkpoint, LiveIndexCount after recovery, snapshot purge after
second checkpoint, and data.bftree survival.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add file-level assertions to RIUnaccessedKeyAfterRecoveryAndSecondCheckpointTest
verifying snapshot state at each stage.

Add RIUnaccessedKeyLostAfterSecondRecoveryTest demonstrating that an
unaccessed RI key is lost after: checkpoint -> recover -> access only
other keys -> second checkpoint (purges old snapshot) -> second recover
(no snapshot to restore from).

Both tests currently fail, documenting the bug.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

1 participant