Skip to content

Disable read-path range tombstone conversion with unordered_write (#15741) - #15741

Open
xingbowang wants to merge 2 commits into
meta:mainfrom
xingbowang:export-D133500312
Open

xingbowang wants to merge 2 commits into
meta:mainfrom
xingbowang:export-D133500312

Conversation

@xingbowang

@xingbowang xingbowang commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Background: with min_tombstones_for_range_conversion, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With DBOptions::unordered_write, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that unordered_write documents.

New behavior: the DBIter constructor disables read-path conversion when unordered_write is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under unordered_write never insert converted range tombstones.

Implementation:

  • db/db_iter.cc: add !ioptions.unordered_write to the condition that sets min_tombstones_for_range_conversion_, and explain why in the existing comment.
  • include/rocksdb/advanced_options.h: list unordered_write among the configurations where conversion is disabled (comment only).
  • unreleased_history/bug_fixes/unordered_write_range_conversion.md: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and two_write_queues without unordered_write publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses unordered_write (which requires two_write_queues), although the existing guard could make it safe there. A WritePrepared iterator carries min_uncommitted, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through GetBaseDB()) is not registered, so the guard does not cover it. So unordered_write turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both unordered_write and min_tombstones_for_range_conversion do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312

@meta-cla meta-cla Bot added the CLA Signed label Nov 20, 2026
@meta-codesync

meta-codesync Bot commented Nov 20, 2026

Copy link
Copy Markdown

@xingbowang has exported this pull request. If you are a Meta employee, you can view the originating Diff in D133500312.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

✅ clang-tidy: No findings on changed lines

Completed in 600.2s.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Claude Code Review - OBSOLETE

Superseded by a newer AI review. Expand to see the original review.

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit fc76825


Summary

Well-scoped, correct bug fix for a real data loss scenario. The one-line guard (!ioptions.unordered_write) is the right approach: simple, conservative, and consistent with the existing pattern of disabling conversion at iterator construction time. The test is well-constructed and deterministic.

High-severity findings (0):

No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. Test timeout too short for CI — db/db_range_del_test.cc (diff line)
  • Issue: The test uses wait_for(std::chrono::seconds(10)) to wait for the writer thread to reach the sync point. Per CLAUDE.md testing guidelines: "make the timeout several minutes, even if the event normally takes milliseconds. CI machines can stall, and RocksDB CI does not retry failed tests." A 10-second timeout may cause flaky failures on loaded CI machines.
  • Suggested fix: Increase the timeout to std::chrono::minutes(2) or similar multi-minute value.

🟢 LOW / NIT

L1. Conservative over-disabling for WritePrepared + two_write_queues + unordered_write
  • Issue: The PR acknowledges that WritePrepared + two_write_queues + unordered_write would already be guarded by the read_callback_ check in MaybeInsertRangeTombstone() (db/db_iter.cc:1905-1908), so disabling conversion entirely is conservative. This means users of this specific configuration lose the optimization benefit.
  • Assessment: Acceptable tradeoff. The PR description documents this explicitly, and the lost benefit is minimal because the read_callback_ guard already restricts most conversions in this configuration.
L2. No reverse-scan-specific test for unordered_write
  • Issue: The new test only exercises forward iteration (SeekToFirst + Next). The existing WritePreparedTransactionTest::RangeTombstoneInsertionWithWritePrepared tests both forward and reverse, but the new standalone test does not.
  • Assessment: Low risk because the guard applies at iterator construction (all scan directions equally). The WritePrepared test parametrization with kUnorderedWrite covers both directions.
L3. EXPECT_EQ to ASSERT_EQ change is correct — write_prepared_transaction_test.cc:4406
  • Observation: The change from EXPECT_EQ to ASSERT_EQ for inserted_ranges.size() is a good fix — the subsequent code accesses inserted_ranges[0], which would be undefined behavior on an empty vector. ASSERT_EQ prevents this.

Cross-Component Analysis

Context Does code execute? Assumptions hold? Action needed?
WritePreparedTxnDB + two_write_queues + unordered_write YES YES (conservatively disabled) None
WriteUnpreparedTxnDB NO — incompatible with unordered_write N/A None
WriteCommittedTxnDB NO — incompatible with unordered_write N/A None
ReadOnly DB / Secondary N/A — no writes, active_mem null N/A None
Pipelined writes NO — mutually exclusive with unordered_write N/A None
User-defined timestamps YES YES — orthogonal guard None
Dynamic SetOptions N/A — unordered_write is ImmutableDBOptions N/A None

DB Mutex Analysis: The change is in the DBIter constructor initializer list, outside DBImpl::mutex_. Simple bool read on immutable struct — zero overhead, no locking concerns.

Pipelined Write Safety: Verified pipelined writes do NOT have the same race. In pipelined writes, the reader-visible LastSequence is published only AFTER memtable insertion (db_impl_write.cc:1761). Only unordered_write publishes sequence numbers to readers before memtable insertion (db_impl_write.cc:1094-1106).

Invariant Verification:

  • min_tombstones_for_range_conversion_ is only set in the constructor initializer list; no subsequent assignments exist. Guard is permanent for iterator lifetime.
  • ImmutableDBOptions::unordered_write cannot change after DB open.
  • No other code path inserts "converted" range tombstones outside MaybeInsertRangeTombstone.

Positive Observations

  1. Correct placement: Guard added at the same level as existing guards (table_filter, read_tier, prefix, timestamp) — consistent pattern.
  2. Good test design: Uses sync points (not sleeps), std::future/std::promise, Defer for cleanup, and verifies both negative (no conversion) and positive (key visible after writer completes) cases.
  3. Clear documentation: Comment in db_iter.cc explains the race, advanced_options.h lists the disabled configuration, release note is concise.
  4. ASSERT_EQ fix: Prevents potential UB in WritePrepared test.

ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/ci_review_prompt.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 24, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: `min_tombstones_for_range_conversion` lets a forward or reverse scan replace a series of adjacent point tombstones, with no live key between them, by one range tombstone `[first_tombstone_key, next_live_key)`. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that series that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. A scan can therefore read at a snapshot that includes a write whose key is not in the memtable yet.

Previous behavior: such a scan could convert the point tombstones around the missing key. The converted range tombstone has a larger sequence number than the pending write. When the write finished, its key was covered by the range tombstone and was lost for every later read, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing uncommitted-write guard would make it safe there, as long as every write goes through the transaction DB. We expect the lost benefit to be small: with `two_write_queues`, that guard already refuses a conversion unless the main write queue has moved past the scan's snapshot, for example while a prepared transaction is open.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
@meta-codesync meta-codesync Bot changed the title Disable read-path range tombstone conversion with unordered_write Disable read-path range tombstone conversion with unordered_write (#15741) Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Claude Code Review - OBSOLETE

Superseded by a newer AI review. Expand to see the original review.

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit 4c23a0d


Summary

This is a well-targeted bug fix that correctly disables read-path range tombstone conversion when unordered_write is true, preventing permanent data loss from a race where sequence publication precedes memtable insertion. The code change is minimal, correctly placed, well-documented, and accompanied by a solid regression test. The conservative approach (disabling entirely rather than attempting dynamic detection) is appropriate given the severity of the bug (permanent data loss).

High-severity findings (0):
No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. Pipelined writes may have a similar (less severe) vulnerability — db/db_impl/db_impl_write.cc:1680
  • Issue: In PipelinedWriteImpl, the sequence is published at line 1680 (write_thread_.UpdateLastSequence(current_sequence + total_count - 1)) BEFORE the memtable write happens at line 1746+. The WAL group leader exits at line 1737 and the memtable write occurs later in a separate pipeline stage. While pipelined writes use write-thread coordination that should prevent new snapshots from observing the gap, the same conceptual window (sequence published, memtable not yet written) exists.
  • Root cause: Pipelined writes deliberately separate WAL and memtable phases. However, enable_pipelined_write is validated as incompatible with unordered_write (line 980-983), and the write thread's state machine (STATE_MEMTABLE_WRITER_LEADER) coordinates the memtable phase before making the sequence visible to new readers. The SetLastSequence at line 1680 is for the write thread's internal tracking, while the actual published sequence for new snapshots goes through versions_->SetLastSequence() only after memtable write completes (line 1761).
  • Assessment: After deeper analysis, pipelined writes appear safe because versions_->SetLastSequence() is called only after memtable insertion completes (line 1761 in the serial path, line 1164 in the parallel path). The intermediate write_thread_.UpdateLastSequence() at line 1680 is internal to write thread coordination and does not make the sequence visible to snapshot acquisition. This is NOT a bug but worth documenting as a confirmed safe case.
  • Suggested action: No code change needed, but a brief note in the PR description or the code comment confirming pipelined writes are safe (and why) would strengthen the analysis.

🟢 LOW / NIT

L1. std::future usage in test is uncommon in RocksDB tests — db/db_range_del_test.cc:3335
  • Issue: The new test uses #include <future> and std::promise/std::shared_future. Only 4 test files in db/ currently use std::future. Most RocksDB tests use sync points and port::Thread for synchronization.
  • Assessment: The usage here is legitimate and arguably cleaner than alternatives — the std::promise/std::future pair provides a clean signal mechanism between the writer thread and the test thread, and the wait_for with timeout prevents test hangs. The Defer for cleanup is good practice. This is a stylistic observation, not a correctness issue.
L2. No reverse iteration test coverage — db/db_range_del_test.cc
  • Issue: The new test UnorderedWriteRangeConversionKeepsPendingWrite only tests forward iteration (SeekToFirst + Next). The range tombstone conversion also operates during reverse iteration (tracked via PrevInternal at db/db_iter.cc:1083-1149). Since the fix is at the constructor level (setting min_tombstones_for_range_conversion_ to 0), reverse iteration is implicitly covered, but explicit test coverage would strengthen confidence.
  • Suggested fix: Consider adding a reverse iteration variant (e.g., SeekToLast + Prev) to the test, similar to how RangeTombstoneInsertionWithWritePrepared tests both forward and reverse.
L3. Comment placement in advanced_options.h — include/rocksdb/advanced_options.h:1449-1450
  • Issue: The PR adds the unordered_write bullet after the read_tier bullet in the "disabled for configurations that may not expose all interior live keys" section. However, unordered_write doesn't hide keys from the scan — it creates a window where keys are not yet IN the memtable. The existing comment framing ("configurations that may not expose all interior live keys") is technically accurate for unordered_write (the scan cannot see a key not yet in the memtable), and the added sub-bullet "because a write can be in the read snapshot before it is in the memtable" correctly explains the distinction. This is fine.
L4. EXPECT_EQ changed to ASSERT_EQ — utilities/transactions/write_prepared_transaction_test.cc:4406
  • Issue: EXPECT_EQ(inserted_ranges.size(), expected_insertions) was changed to ASSERT_EQ(inserted_ranges.size(), expected_insertions). This is correct — it prevents the subsequent inserted_ranges[0] access from being an out-of-bounds read when the size doesn't match. Good improvement.

Cross-Component Analysis

Context Affected? Safe? Notes
Default write path No Yes Sequence published after memtable insert
unordered_write=true Yes Yes (with fix) Conversion disabled
enable_pipelined_write No Yes versions_->SetLastSequence() after memtable insert
WritePreparedTxnDB + unordered_write Yes Yes Both min_uncommitted guard AND new guard apply (conservative)
WritePreparedTxnDB without unordered_write No Yes Existing min_uncommitted guard sufficient
ReadOnly DB No N/A active_mem is null, conversion already disabled
Iterator Refresh Yes Yes Re-creates DBIter, re-evaluates condition
two_write_queues without unordered_write No Yes Sequence published after memtable insert in this mode

Assumption stress-test results:

  1. Claim: "sequence published before memtable insert with unordered_write" — Confirmed at db/db_impl/db_impl_write.cc:1093-1107. WriteImplWALOnly with kDoPublishLastSeq publishes via versions_->SetLastSequence() (line 2065), then BeforeUnorderedWriteMemtable sync point fires, then UnorderedWriteMemtable() does the insert.

  2. Claim: "converted tombstone has larger seqno than pending write" — Confirmed. The tombstone is inserted at sequence_ (the iterator's read sequence, db/db_iter.cc:1902), which must be >= the pending write's sequence (since the write's sequence was already published and the iterator was created after).

  3. Claim: "other write modes are safe" — Confirmed for default path (memtable insert happens within the write group before SetLastSequence). Confirmed for pipelined writes (SetLastSequence at line 1761 after memtable insert). enable_pipelined_write is also incompatible with unordered_write (line 980-983).

Positive Observations

  • Minimal, surgical fix: The change adds a single boolean check in exactly the right place (the constructor guard that determines if conversion is enabled for the iterator's lifetime). No new code paths, no new data members.
  • Well-documented: The code comment explains WHY unordered_write is unsafe, not just WHAT the check does. The PR description includes a thorough safety analysis.
  • Known limitation documented: The PR honestly acknowledges that WritePrepared+unordered_write loses conversion benefit unnecessarily, and explains why the conservative approach is acceptable.
  • Good test: The test correctly simulates the race condition using an existing sync point, uses Defer for cleanup, and has a timeout to prevent hangs. The final verification checks both point reads and iteration.
  • ASSERT_EQ upgrade: Changing EXPECT_EQ to ASSERT_EQ for inserted_ranges.size() prevents potential out-of-bounds access — a subtle but important improvement.

ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/ci_review_prompt.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

@meta-codesync meta-codesync Bot changed the title Disable read-path range tombstone conversion with unordered_write (#15741) Disable read-path range tombstone conversion with unordered_write Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Claude Code Review - OBSOLETE

Superseded by a newer AI review. Expand to see the original review.

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit 32b951f


Summary

This is a well-targeted bug fix for a real data-loss scenario. The fix is conservative (disables an optimization) rather than adding complex logic, and the approach is correct. The test is well-designed, using sync points to deterministically reproduce the race condition. The comment and documentation updates are accurate and well-placed.

High-severity findings (0):
No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. Test comment says "two deletes on each side" but only deletes b1, b2, b4, b5 — db/db_range_del_test.cc
  • Issue: The comment says min_tombstones_for_range_conversion = 2 and "the two deletes on each side of 'b3' are enough for a conversion." However, the threshold means 2 contiguous tombstones trigger a conversion. With b1, b2 on one side and b4, b5 on the other, a forward scan would see b1, b2 as 2 contiguous tombstones, then hit the live gap at b3 (missing due to unordered_write), then b4, b5 as another run of 2. Without the fix, either run alone is enough to trigger conversion. The comment is slightly imprecise — it implies the two sides combine, but they would actually trigger independently. This is a minor documentation issue in test code; the test logic itself is correct.
  • Suggested fix: Consider clarifying that either run of 2 tombstones independently meets the threshold, not that they combine across the gap.
M2. No reverse-scan variant of the new test — db/db_range_del_test.cc
  • Issue: The existing RangeTombstoneInsertionWithWritePrepared test iterates both forward and reverse (for (bool forward : {true, false})). The new UnorderedWriteRangeConversionKeepsPendingWrite test only tests forward iteration (SeekToFirst + Next). The reverse path in PrevInternal() has its own tombstone tracking logic (lines 1080-1144 of db_iter.cc), and while the guard is applied at construction time (so it covers both directions), a reverse test would increase confidence.
  • Suggested fix: Consider adding a reverse iteration variant (SeekToLast + Prev) to verify the guard works in both directions, similar to the WritePrepared test pattern.

🟢 LOW / NIT

L1. The EXPECT_EQ to ASSERT_EQ upgrade is a good fix — write_prepared_transaction_test.cc:4406
  • Observation: Changing EXPECT_EQ(inserted_ranges.size(), expected_insertions) to ASSERT_EQ prevents out-of-bounds access on inserted_ranges[0] when the size doesn't match expectations. This is a correct defensive improvement.
L2. Known limitation is well-documented — PR description
  • Observation: The PR description explicitly notes that WritePrepared + unordered_write + two_write_queues loses the optimization even though the read_callback guard would suffice. This conservative approach is appropriate for a bug fix — it avoids adding complexity to distinguish cases.
L3. Comment placement in advanced_options.h is correct
  • Observation: The new unordered_write bullet is correctly placed in the "disabled for iterator configurations" section (constructor-time gates), not in the "discarded" section (runtime guards in MaybeInsertRangeTombstone). This matches the implementation.

Cross-Component Analysis

Context Affected? Analysis
Plain DB + unordered_write YES The fix correctly disables conversion. No data loss.
WritePreparedTxnDB + ordered_write NO unordered_write is false; existing read_callback guard handles this.
WritePreparedTxnDB + unordered_write + two_write_queues YES Conservatively disabled. The read_callback guard would also catch it, but double protection is safe.
WriteUnpreparedTxnDB NO Incompatible with unordered_write (rejected at open time).
ReadOnly DB NO active_mem_ is nullptr; conversion already disabled.
User-defined timestamps Independent Separate guard (HasFullTimestampVisibility) handles this.
Concurrent memtable writes NO impact The guard is in the constructor; unordered_write is an ImmutableDBOption set at open time.
Iterator Refresh Safe DoRefresh re-constructs DBIter with the same ioptions, so the guard is reapplied.

Assumption stress test:

  • Claim: "With unordered_write, a scan can miss a key that its snapshot includes."

    • Precondition 1: Writer publishes seqno before memtable insert → TRUE by design (db_impl_write.cc:1097 kDoPublishLastSeq before line 1106-1108 memtable insert).
    • Precondition 2: Another writer can advance published seq while first is pending → TRUE (concurrent writers).
    • Precondition 3: Scanner at advanced seq misses the pending key → TRUE (key not in memtable).
    • All preconditions verified. The bug scenario is real.
  • Claim: "Disabling conversion prevents data loss."

    • If min_tombstones_for_range_conversion_ is 0, TrackContiguousTombstone and MaybeInsertRangeTombstone are never reached (guarded by min_tombstones_for_range_conversion_ > 0 checks at lines 653, 980, 1083, 1139, 1877). No range tombstone is inserted. The pending write is not covered. Verified correct.

DB Mutex analysis: This change does not affect any code path that runs under DBImpl::mutex_. The guard is evaluated in the DBIter constructor, which runs outside the DB mutex. No mutex-related concerns.

Positive Observations

  • The fix is minimal and surgical — one boolean check added to an existing guard chain.
  • The sync point BeforeUnorderedWriteMemtable already exists in the codebase and is the correct point to pause the writer between WAL publish and memtable insert.
  • The test uses Defer for cleanup, ensuring the writer thread is always joined even when assertions fail — good test hygiene.
  • The std::future::wait_for with 10-second timeout ensures the test fails rather than hangs if the sync point is never reached.
  • The EXPECT_EQ → ASSERT_EQ upgrade in the WritePrepared test prevents potential crashes on test failure.
  • The PR description's "Safety and unsupported cases" and "Known limitations" sections demonstrate thorough analysis of the fix's scope and trade-offs.

ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/ci_review_prompt.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 25, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. A scan can therefore read at a snapshot that includes a write whose key is not in the memtable yet.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was covered by the range tombstone and was lost for every later read, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard would make it safe there, as long as every write goes through the transaction DB. That guard refuses a conversion when the scan's read sequence number is at or above the `min_uncommitted` bound saved with the scan's snapshot, which is a conservative bound for the oldest uncommitted write. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 25, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. A scan can therefore read at a snapshot that includes a write whose key is not in the memtable yet.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was covered by the range tombstone and was lost for every later read, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard would make it safe there, as long as every write goes through the transaction DB. That guard refuses a conversion when the scan's read sequence number is at or above the `min_uncommitted` bound saved with the scan's snapshot, which is a conservative bound for the oldest uncommitted write. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
@github-actions

Copy link
Copy Markdown

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit 4d3ea63


Summary

Well-scoped, correct bug fix for a real data-loss scenario with unordered_write + min_tombstones_for_range_conversion. The approach follows the existing pattern of constructor-time guards in DBIter, the analysis of the write-path race is sound, and the test effectively reproduces the timing window. The fix is conservative (also disables conversion for WritePrepared + two_write_queues + unordered_write where the existing read_callback guard would suffice), which is the right trade-off for a data-integrity fix.

High-severity findings (0):
No high-severity findings.

Full review (click to expand)

Findings

🔴 HIGH

None.

🟡 MEDIUM

None.

🟢 LOW / NIT

L1. Guard placement in short-circuit chain — db/db_iter.cc:137
  • Issue: !ioptions.unordered_write is placed second in the && chain, after active_mem != nullptr. When unordered_write is true, the remaining checks (HasTableFilter, read_tier, prefix, timestamp) are still skipped. However, active_mem != nullptr is a common short-circuit (null for read-only/secondary instances), so the current placement is reasonable.
  • Suggested fix: No change needed. Current placement is acceptable — active_mem check first is the existing convention and has good short-circuit coverage.
L2. No reverse-scan variant in the new test — db/db_range_del_test.cc
  • Issue: The new test UnorderedWriteRangeConversionKeepsPendingWrite only exercises forward iteration (SeekToFirst / Next). The existing range conversion also has a reverse-scan path (tracked in PrevInternal via TrackContiguousTombstone with always_update_first_key=true). Since the fix disables conversion entirely (at construction time), a reverse-scan variant would not test a different code path, but it would add defense-in-depth regression coverage.
  • Suggested fix: Consider adding a SCOPED_TRACE(forward/reverse) loop, mirroring the pattern in RangeTombstoneInsertionWithWritePrepared.
L3. EXPECT_EQ → ASSERT_EQ for inserted_ranges.size() is a correctness improvement — write_prepared_transaction_test.cc:4406
  • Issue: The original code used EXPECT_EQ(inserted_ranges.size(), expected_insertions) followed by if (options.two_write_queues) { EXPECT_EQ(inserted_ranges[0]... }. If the size check failed, the subsequent access to inserted_ranges[0]would be undefined behavior. The change toASSERT_EQ` correctly stops the test on failure. This is a good fix bundled with the main change.
  • Suggested fix: None — this is already correct.
L4. Test uses std::future/std::promise which is fine but SyncPoint is the conventional RocksDB pattern — db/db_range_del_test.cc
  • Issue: The test uses std::promise/std::future for the published signal and std::shared_future for the scan_done signal. This is correct and the 5-minute timeout follows CLAUDE.md guidelines. The SyncPoint callback correctly gates only on the writer thread via paused_writer. The Defer ensures cleanup on assertion failure. Overall the test is well-constructed.
  • Suggested fix: None — the approach is sound. The SyncPoint callback combined with std::promise is a reasonable hybrid pattern already used elsewhere in the codebase (e.g., column_family_test.cc:3659).

Cross-Component Analysis

Context Affected? Analysis
Regular DB + unordered_write YES Fix correctly disables conversion. The only production caller of AddLogicallyRedundantRangeTombstone is DBIter::MaybeInsertRangeTombstone (db/db_iter.cc:1932).
WritePrepared + two_write_queues + unordered_write YES Conservatively disabled. The existing read_callback_ guard (db/db_iter.cc:1905) would prevent the bug, but the fix disables conversion earlier. PR documents this as a known limitation with unmeasured benefit loss.
WritePrepared + one_write_queue (no unordered_write) NO unordered_write is false, so the new guard doesn't trigger. Existing read_callback_ guard handles this case.
WriteUnprepared + unordered_write YES Same as WritePrepared — conservatively disabled.
ReadOnly DB / SecondaryInstance NO active_mem is null, so conversion is already disabled.
PipelinedWrite NO Incompatible with unordered_write (db/db_impl/db_impl_write.cc:980-983).
Dynamic option change N/A unordered_write is an ImmutableDBOptions field (options/db_options.h:72), cannot change after DB open. The constructor-time guard is therefore correct.

Assumption Stress Test

Claim: "disabling at construction time is sufficient."

  • Precondition: unordered_write cannot change after DB open.
  • Verified: It's in ImmutableDBOptions (options/db_options.h:72), set from DBOptions at DB open (options/db_options.cc:850). No SetDBOptions path modifies it.
  • Result: Valid. Constructor-time is sufficient.

Claim: "the fix covers all insertion paths."

  • Precondition: MaybeInsertRangeTombstone is the only production caller of AddLogicallyRedundantRangeTombstone.
  • Verified: Grep confirms only db/db_iter.cc:1932 calls it in production. db/db_flush_test.cc:4436 is test-only.
  • Result: Valid. The fix covers the only path.

Claim: "other write modes are not changed."

  • Verified: The check !ioptions.unordered_write only triggers when unordered_write is true. Default write path, pipelined writes, and concurrent memtable writes are unaffected.
  • Result: Valid.

Positive Observations

  1. Correct root cause analysis. The PR description accurately identifies the race: seqno publication before memtable insertion allows a scan to convert tombstones at a seqno that covers the pending write. This is permanent data loss, not the relaxed-snapshot behavior unordered_write documents.

  2. Follows existing guard pattern. Adding to the constructor's boolean condition chain is exactly how the other guards (table_filter, read_tier, prefix, timestamp) work. This is consistent and maintainable.

  3. Good test construction. The new test correctly uses sync points to pause the writer between WAL write and memtable insert, advances the published sequence, and verifies both that the scan cannot see the pending key and that the key survives after the write completes. The Defer pattern ensures cleanup even on assertion failure.

  4. ASSERT_EQ improvement. Changing EXPECT_EQ to ASSERT_EQ for inserted_ranges.size() prevents undefined behavior in the subsequent element access.

  5. Clear, accurate documentation. The inline comment in db/db_iter.cc explains the race precisely. The comment in advanced_options.h and the release note are appropriately concise.

  6. Explicit "Changes visible to existing users" section. The PR description clearly states that DBs with both options set will lose the scan optimization, which is the correct trade-off for data integrity.


ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/ci_review_prompt.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

@xingbowang
xingbowang force-pushed the export-D133500312 branch 2 times, most recently from c96e29f to 70d7ac3 Compare November 26, 2026 12:53
…eta#15798)

Summary:
Pull Request resolved: meta#15798

Background: with `min_tombstones_for_range_conversion`, a scan that steps over enough adjacent point tombstones inserts one range tombstone over them into the active memtable, at the scan's read sequence number. Later reads at that sequence number or higher see the range tombstone.

A SuperVersion references the memtables and the Version (the list of SST files) of a column family. Reads that pick their own sequence number use one of two orders:
- Iterator creation, single column family `MultiGet()`, and a `Refresh()` that rebuilds the iterator reference their SuperVersion first and read the last sequence number after that. This is the pin-first rule: the referenced SuperVersion keeps every key version that a later sequence number can need, so a flush or a compaction cannot drop data that the read needs.
- A multi column family read (multi-CF `MultiGet()`, `NewIterators()`) reads one sequence number first and then references the SuperVersions of the group. It retries the whole group if a memtable is newer than that sequence number.

A file ingestion publishes the ingested files' sequence number S before it installs the SuperVersion that has the files. So a read can get the old SuperVersion and a sequence number at or above S. If the ingestion does not flush the active memtable, the old and the new SuperVersion share it.

Previous behavior: without conversion, such a read sees the state from before the ingestion, which is a consistent view. With conversion, a newer iterator that sees the ingested files can convert a run that a delete in an ingested file completes, at S, into the shared active memtable. The old read then sees that range tombstone, but not the other keys of the ingested file. For example:
- An SST file holds `b`. The memtable deletes `a` and `c`, and `d` is live.
- An ingested file puts `ax` and deletes `b`, at S.
- A read references the old SuperVersion and then reads S.
- A newer iterator sees `b` and `c` deleted and converts [`b`, `d`) at S.
- The old read sees neither `ax` nor `b`. Before the ingestion, it would see `b`; after it, `ax`.

A single-key `Get()` cannot show the problem: the range tombstone hides a key only if the key is deleted at S, so the result is the key's value at S. So `GetImpl()` is not changed, and the fix has four call sites.

New behavior: such a read uses the SuperVersion that has the ingested files, so it sees the whole ingestion.

Implementation:
- `ColumnFamilyData::last_ingest_version_number_` holds the number of the Version that installed the latest successful ingestion, also one that assigns no new sequence number. The ingestion stores it under the DB mutex after `LogAndApply()` and before it publishes its sequence numbers. It is separate from the memtable conversion barrier, which serves scans that try to insert a converted range tombstone. The sequence number store has release ordering and the read's load has acquire ordering, so a read that sees an ingested sequence number also sees this value.
- `DBImpl::SuperVersionMissesIngestion()` returns true if the SuperVersion's Version number is older than that value. A read calls it after it has its sequence number.
- `DBImpl::ReplaceSuperVersionMissingIngestion()` takes the DB mutex, references the current SuperVersion and releases the old one. The ingestion holds the DB mutex from before it publishes its sequence numbers until it installs its SuperVersion, so the new SuperVersion has every ingestion whose sequence number was published before the call.
- After the replacement, the read reads its sequence number again, so the pin-first rule holds for the new SuperVersion. Without this, a write and a flush between the first sequence number and the new reference could drop a key version that the old sequence number needs, and the read would miss a live key. The read repeats the check, because another ingestion can publish its sequence number between the new reference and the new read. Each repeat needs a new ingestion.
- The checks that a read runs on its first SuperVersion also run on each new one, with the same errors: a read with `ReadOptions::timestamp` checks it against `full_history_ts_low`, because the new SuperVersion can have files whose older history was already collapsed; an iterator with `ReadOptions::table_filter` checks that conversion is off, because `SetOptions()` can turn it on in between.
- Entry points that replace the SuperVersion (pin-first):
  - `NewIterator()` without a snapshot reads its sequence number in the new `DBImpl::ReadIteratorSequence()`, which runs the loop and the checks and can return an error. `NewIteratorImpl()` then builds the iterator with the sequence number that its caller passes. Its other callers already pass one: the WritePrepared and WriteUnprepared transaction DBs take a snapshot first, and multi column family `NewIterators()` gets its sequence number from `MultiCFSnapshot()`. The sync points `DBImpl::NewIterator:1` to `:4` moved with the sequence number read, so they run only for `NewIterator()` without a snapshot.
  - The single column family path of `MultiCFSnapshot()` (`MultiGet()`, `MultiGetEntity()`).
  - `ArenaWrappedDBIter::DoRefresh()`, the `Refresh()` path that rebuilds the iterator.
- The multi column family retry loop of `MultiCFSnapshot()` (multi-CF `MultiGet()`, `NewIterators()`, multi-CF iterators) reads one sequence number first, so it does not replace a SuperVersion: it treats a missing ingestion as one more reason to try the whole group again. Its last try holds the DB mutex while it references the SuperVersions. That try now reads the sequence number after it takes the mutex, when the read has no snapshot. Reading it before the mutex left a window in which a flush could drop a version that the sequence number needs. This window existed before this diff, but the extra retries for an ingestion make the last try easier to reach.
- Reads with a snapshot that the caller passes skip the check. Such a snapshot already existed when the SuperVersion was referenced, and a snapshot at or above S can be created only after the ingestion installs its SuperVersion.
- The in-place path of `Refresh()`, which keeps its iterator tree and only moves the sequence number, needs no check here. It refreshes its memtable range tombstones before it checks the SuperVersion number again, and if it finds range tombstones that its tree has no slot for, it rebuilds the tree at once. A conversion that depends on an ingested file can only happen after the column family installs the ingestion's SuperVersion, so a refresh that sees that conversion also sees the new SuperVersion number and rebuilds.

Cost:
Each `NewIterator()`, `MultiGet()` and `Refresh()` without a snapshot does two loads (the column family value and the Version number of the SuperVersion) and one compare. The replacement runs only when the compare finds the SuperVersion's Version older than the Version of the latest ingestion, that is, when an ingestion installed its Version after the read referenced its SuperVersion and before the compare. An ingestion that assigns no new sequence number also counts. Each replacement takes the DB mutex once; the loop repeats only for another such ingestion. Releasing the old SuperVersion can take the DB mutex again if that was its last reference.

Measured with release db_bench, base vs this change, on an AMD EPYC 9D85 host: a DB of 1M keys (16-byte keys, 100-byte values) after a full compaction, 1 GB block cache, one thread pinned to one CPU, 300,000 operations per run, 3 rounds with the two builds swapped between two CPUs in each round.

| Benchmark | Instructions per op, base | Change | Time per op, base | Time per op, this change |
|---|---|---|---|---|
| seekrandom (a new iterator and one Seek per op) | about 16,600 | +44 to +50 (+0.3%) | 3.05 to 3.18 us | 3.01 to 3.13 us |
| multireadrandom (MultiGet of 8 keys) | about 9,900 | within +-3 | 1.89 to 2.07 us | 1.88 to 2.01 us |

The time per op does not change beyond the spread between runs. The cause of the extra instructions in seekrandom was not found; moving the retry into a function that is never inlined did not change it.

How to read this diff:
1. db/column_family.h: `last_ingest_version_number_` and its accessors; db/db_impl/db_impl.cc `IngestExternalFiles()`, where the ingestion records it before it publishes its sequence numbers.
2. db/db_impl/db_impl.h: the overview at `SuperVersionMissesIngestion()` (the pin-first rule, the problem, the fix), then `ReplaceSuperVersionMissingIngestion()` and `ReadIteratorSequence()`.
3. db/db_impl/db_impl.cc: `NewIterator()` and `ReadIteratorSequence()`, then `MultiCFSnapshot()` (the single column family loop, then the multi column family retry and its last try).
4. db/arena_wrapped_db_iter.cc: `DoRefresh()`.
5. db/db_iterator_test.cc: the `ReadPathRangeTombstoneTest` cases listed in the Test Plan.

Changes visible to existing users: the check runs for every user, also with conversion off. An iterator, `MultiGet()` or `Refresh()` that gets a SuperVersion from before an ingestion together with a sequence number from after it now takes the DB mutex, gets the current SuperVersion, reads its sequence number again, and sees the ingested files. Before, it saw the view from before the ingestion. The existing test `IngestedFileSurvivesIteratorWithOlderSuperVersion` (db/db_iterator_test.cc) changes accordingly: its iterator now sees the ingested keys and converts nothing.

Differential Revision: D134584125
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
@meta-codesync meta-codesync Bot changed the title Disable read-path range tombstone conversion with unordered_write Disable read-path range tombstone conversion with unordered_write (#15741) Nov 26, 2026
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 26, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 26, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 26, 2026
…ta#15741)

Summary:

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 26, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 26, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 26, 2026
…ta#15741)

Summary:
Pull Request resolved: meta#15741

Background: with `min_tombstones_for_range_conversion`, a forward or reverse scan that steps over enough adjacent point tombstones, with no live key between them, adds one range tombstone over the keys it found deleted. The point tombstones stay; the range tombstone is redundant, and later scans use it to seek past the covered keys in older memtables and SST files. The scan inserts the range tombstone into the active memtable at the scan's read sequence number. This is safe only if the scan saw every key in that range that its snapshot includes.

With `DBOptions::unordered_write`, a write publishes its sequence number before it inserts its keys into the memtable. An iterator can therefore get a read sequence number at or above a pending write's sequence number while that write's key is still absent from the memtable.

Previous behavior: such a scan could convert the point tombstones around the missing key. While the first write was pending, a later write could publish a higher sequence number. An iterator opened at that higher sequence number could miss the pending key, and the converted range tombstone, inserted at the read sequence number, is newer than the pending write. When the write finished, its key was still covered by the range tombstone for every read at the scan's sequence number or higher, also after flush and compaction. This is permanent data loss, not only the relaxed snapshot guarantee that `unordered_write` documents.

New behavior: the `DBIter` constructor disables read-path conversion when `unordered_write` is true, next to the existing gates for table filters, read tiers, prefix mode and timestamp visibility. Scans under `unordered_write` never insert converted range tombstones.

Implementation:
- `db/db_iter.cc`: add `!ioptions.unordered_write` to the condition that sets `min_tombstones_for_range_conversion_`, and explain why in the existing comment.
- `include/rocksdb/advanced_options.h`: list `unordered_write` among the configurations where conversion is disabled (comment only).
- `unreleased_history/bug_fixes/unordered_write_range_conversion.md`: release note.

Safety and unsupported cases: other write modes are not changed. The default write path, pipelined writes, concurrent memtable writes and `two_write_queues` without `unordered_write` publish a sequence number only after the memtable insert finishes, so a scan cannot miss a key that its snapshot includes. WritePrepared and WriteUnprepared transactions keep their existing guard: a scan does not convert when its snapshot is at or above the oldest uncommitted sequence number.

Known limitations: conversion is also disabled for a WritePrepared transaction DB that uses `unordered_write` (which requires `two_write_queues`), although the existing guard could make it safe there. A WritePrepared iterator carries `min_uncommitted`, a conservative lower bound on the sequence number of the oldest pending write that the transaction DB tracks, and conversion refuses a scan whose sequence number reaches that bound. This guard covers a pending write only if the write goes through the transaction DB, which registers its sequence number. A write to the base DB under the transaction DB (for example through `GetBaseDB()`) is not registered, so the guard does not cover it. So `unordered_write` turns conversion off for the whole configuration, also for a transaction DB. The lost benefit was not measured.

Changes visible to existing users: DBs, including transaction DBs, that set both `unordered_write` and `min_tombstones_for_range_conversion` do not get read-path range tombstones, so their scans over long tombstone runs do not get faster.

Differential Revision: D133500312

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant