Repository navigation
Don't update in place a value that a range tombstone deletes (#15775) - #15775
xingbowang wants to merge 1 commit into
Conversation
|
@xingbowang has exported this pull request. If you are a Meta employee, you can view the originating Diff in D134285230. |
✅ clang-tidy: No findings on changed linesCompleted in 107.9s. |
✅ Claude Code ReviewAuto-triggered after CI passed — reviewing commit d7685d0 SummaryWell-designed and carefully implemented behavior change that closes a real data correctness bug. The approach of rejecting range deletions at the write path (before WAL) rather than trying to make the combination work is pragmatic and sound. The flag-based fast path ensures zero cost for DBs that don't use High-severity findings (0): No high-severity findings. Full review (click to expand)Findings🔴 HIGHNone. 🟡 MEDIUMM1. Comment added inside the min_tombstones_for_range_conversion condition --
|
| Context | Affected? | Safe? | Notes |
|---|---|---|---|
| WritePreparedTxnDB | NO | N/A | Rejects inplace_update_support at open |
| WriteUnpreparedTxnDB | NO | N/A | Rejects inplace_update_support at open |
| WriteCommittedTxnDB | YES | YES | Both skip_concurrency_control (db_impl_->Write()) and normal (CommitBatch->WriteImpl) paths go through WriteImpl |
| ReadOnly DB | NO | N/A | No writes |
| Secondary Instance | NO | N/A | No writes |
| WAL Recovery | YES | KNOWN LIMITATION | Documented. Range tombstones replayed from WAL are not checked. |
| User-defined timestamps | NO | N/A | No special interaction |
| MemPurge | NO | N/A | No interaction with range deletion check |
| Ingestion | YES | YES | Both regular ingestion and atomic_replace_range are covered |
| Read-path conversion | YES | YES | min_tombstones_for_range_conversion disabled in db_iter.cc |
Thread safety analysis:
has_inplace_update_support_cf_usesmemory_order_relaxed, matching the pattern ofis_snapshot_supported_. This is safe because:- Setting true (CreateColumnFamily): Done under
mutex_before the handle is returned. A write through the new handle can't happen until after the handle is returned, establishing happens-before. - Setting false (DropColumnFamily): Done under
mutex_. A brief window where concurrent writes see staletrueis harmless -- they just do an unnecessary check that correctly rejects nothing (the dropped CF won't be found byGetColumnFamily). - False negative (flag is false but CF has inplace_update): Impossible because the flag is set before the handle is exposed.
- Setting true (CreateColumnFamily): Done under
DB Mutex impact:
CheckRangeDeletionsSupportedacquiresmutex_briefly for CF lookups. This is on the write path but only triggers for writes with range deletions in DBs with inplace_update or row_cache. The mutex hold time is O(number of distinct CFs with range deletions in the batch), which is very small. No I/O or expensive operations under the lock.
Assumption stress test:
- "No new range tombstones" -- Verified: write path, ingestion path, and read-path conversion are all covered. WAL recovery is the only gap, documented.
- "Batch rejected before WAL" -- Verified: CheckRangeDeletionsSupported is called in WriteImpl's early validation section, before the batch enters the write group.
- "inplace_update_support cannot be changed with SetOptions" -- Verified: It's in
ImmutableCFOptions.
Positive Observations
-
Zero feature-off cost: For DBs without
inplace_update_supportand withoutrow_cache, the check isnullptr || falsewhich short-circuits to false, never callingHasDeleteRange(). Better than the old code. -
Well-chosen rejection point: Rejecting before WAL write prevents background errors that would stop all writes.
-
Comprehensive coverage: All three paths that could introduce range tombstones are covered, and the WAL recovery limitation is honestly documented.
-
Thorough testing: Tests cover basic rejection, batch rejection, WAL safety, dynamic CF create/drop, read-path conversion, ingestion, atomic_replace_range, and TransactionDB.
-
Clean separation of concerns: The flag is maintained alongside
is_snapshot_supported_in the same three places (Open, Create, Drop).
ℹ️ 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
) Summary: **Background** With `ColumnFamilyOptions::inplace_update_support`, a `Put()` to a key whose newest entry in the mutable memtable is a value of the same type, at least as large as the new value, overwrites that entry in place (`MemTable::Update()`). The entry keeps the sequence number of the value it overwrites. With `inplace_callback`, `MemTable::UpdateCallback()` passes that entry to the callback as the existing value, and the callback can update it in place too. WAL recovery inserts through the same functions with the options of the current `DB::Open()`, so it also updates in place when the DB is opened with the option, whatever options wrote the WAL. **The problem** A range tombstone hides the entries in its range that have smaller sequence numbers. It lives in the memtable's range deletion table, which in-place updates did not look at: 1. `Put(k, v1)` gets sequence number 1. 2. `DeleteRange(a, z)` gets sequence number 2. 3. `Put(k, v2)`: `v2` fits in the space of `v1`, so the entry is overwritten in place and keeps sequence number 1. `Get(k)` then returns NotFound, although the second `Put()` came after the deletion, and flush and compaction keep that result. The same three writes in a WAL written without the option lose `v2` when the DB is reopened with it: replay updates in place, and the recovery flush makes the loss permanent. With `inplace_callback`, the callback also received the deleted value as the existing value. The crash test hit the replay case when runs with and without in-place updates shared a DB; meta#15626 then made crash test series with in-place updates write no range tombstones. **Fix** `MemTable::Update()` and `MemTable::UpdateCallback()` treat an entry that a range tombstone in the same memtable deletes (`MaxCoveringTombstoneSeqnum()` above the entry's sequence number) as missing. `Update()` then adds the new value with its own sequence number. `UpdateCallback()` returns NotFound, so `MemTableInserter::PutCFImpl()` reads the existing value through the DB, which honors the tombstone, and the callback sees no existing value. Live writes and WAL replay go through the same check. Values already lost this way before this change stay lost. Checking only the active memtable is enough. Entries in immutable memtables and SST files are older than every entry in the active memtable, so a tombstone there cannot fall between an entry and its in-place update. Ingested files are the exception to that ordering, but ingestion first flushes the memtables that overlap the ingested files, so no entry that an ingested tombstone covers stays in the active memtable. Tombstones that iterators insert when converting point tombstones (`min_tombstones_for_range_conversion`) go into the active memtable, so the check sees them. **Cost** Nothing while the memtable has no range tombstones: the check returns before creating an iterator. Otherwise every `Put()` that could update in place looks up the covering tombstone. It creates an iterator over the memtable's cached fragmented tombstone list, and the first such `Put()` after a new range tombstone rebuilds that list from all tombstones in the memtable, on the write path. During WAL replay that happens under the DB mutex, which recovery holds throughout; each new range tombstone in the recovering memtable causes at most one rebuild, over all range tombstones then in that memtable. A `Put()` to a key that a tombstone deletes appends a new entry; later `Put()`s to that key update the new entry in place. With `inplace_callback`, the appending `Put()` also reads the key through the DB. Measured with `db_bench` (release build, one thread pinned to one CPU of an AMD EPYC 9D25 host): `fillseq` then `overwrite`, 200k writes each over about 100k keys, 16-byte keys, 100-byte values overwritten in place by values of the same size, WAL disabled, and a write buffer large enough that everything stays in one memtable. `db_bench` writes the range tombstones during both phases. Time per overwrite in ns, median of 7 runs with the builds alternating, minimum and maximum in parentheses: | Range tombstones | Before | With this change | |---|---|---| | None | 1126 (1032-1176) | 1155 (1044-1237) | | One per phase, each covering 1 key | 1115 (1089-1206) | 1273 (1203-1393), +14% | | One every 100 writes, each covering 1 key, 4000 by the end | 1292 (1219-1390) | 5953 (5789-6033), 4.6x | | One every 100 writes, each covering 100 keys, 4000 by the end | 1343 (1221-1367) | 8300 (8286-8444), 6.2x | Most of the cost in the last two rows is the rebuild after every new tombstone. In this write-only benchmark nothing else rebuilds the list; when reads follow each new tombstone, the first read rebuilds it without this change too (from the code, not measured). In the last row, 37% of the overwrites hit a value that a newer tombstone deletes; without this change those writes were lost. Only the combination that lost writes before pays this, and we know of no user that combines in-place updates with range deletions, so this change keeps the precise check; a TODO in `MemTable::IsDeletedByRangeTombstone()` records the cost. Skipping the lookup when the existing entry is newer than every range tombstone in the memtable would help little: an in-place update keeps the entry's sequence number, so in the last two rows almost every entry stays older than the newest tombstone. Not updating in place at all while the memtable has range tombstones would make the check one atomic load, at the cost of appending every `Put()` until the memtable is switched. **Alternative considered** Rejecting range deletions on column families with in-place updates was not done. WAL replay still updates in place, so rejecting new range deletions cannot protect a WAL written without the option, and it would make `DeleteRange()`, write batches, ingestion, `ClipColumnFamily()` and DB split/merge fail on such column families. **Crash test** Crash test series with in-place updates write range tombstones again: `delrangepercent`, `test_ingest_standalone_range_deletion_one_in` and `min_tombstones_for_range_conversion` are no longer turned off for them, and setting one of them on the command line no longer turns in-place updates off for the series. **Recovery with `inplace_callback`, unchanged** With `inplace_callback`, `DB::Open()` already fails with NotSupported when the WAL holds a callback `Put()`: during recovery `PutCFImpl()` does not read the DB, so the first replayed `Put()` of a key, which is never in the recovering memtable yet, has no existing value to pass to the callback, and WAL recovery treats the error as fatal. With `paranoid_checks=false`, or with `wal_recovery_mode=kSkipAnyCorruptedRecords`, the error is only logged and the DB opens, but each failing `Put()` and the records after it in the same write batch are dropped. All of this happens the same way before and after this change. So this change neither fixes nor adds a recovery failure with `inplace_callback`. A fix belongs in a separate change. Reviewed By: xingbowang Differential Revision: D134285230
d7685d0 to
d2f7c2c
Compare
Summary:
Background
With
ColumnFamilyOptions::inplace_update_support, aPut()to a key whose newest entry in the mutable memtable is a value of the same type, at least as large as the new value, overwrites that entry in place (MemTable::Update()). The entry keeps the sequence number of the value it overwrites. Withinplace_callback,MemTable::UpdateCallback()passes that entry to the callback as the existing value, and the callback can update it in place too. WAL recovery inserts through the same functions with the options of the currentDB::Open(), so it also updates in place when the DB is opened with the option, whatever options wrote the WAL.The problem
A range tombstone hides the entries in its range that have smaller sequence numbers. It lives in the memtable's range deletion table, which in-place updates did not look at:
Put(k, v1)gets sequence number 1.DeleteRange(a, z)gets sequence number 2.Put(k, v2):v2fits in the space ofv1, so the entry is overwritten in place and keeps sequence number 1.Get(k)then returns NotFound, although the secondPut()came after the deletion, and flush and compaction keep that result. The same three writes in a WAL written without the option losev2when the DB is reopened with it: replay updates in place, and the recovery flush makes the loss permanent. Withinplace_callback, the callback also received the deleted value as the existing value. The crash test hit the replay case when runs with and without in-place updates shared a DB; #15626 then made crash test series with in-place updates write no range tombstones.Fix
MemTable::Update()andMemTable::UpdateCallback()treat an entry that a range tombstone in the same memtable deletes (MaxCoveringTombstoneSeqnum()above the entry's sequence number) as missing.Update()then adds the new value with its own sequence number.UpdateCallback()returns NotFound, soMemTableInserter::PutCFImpl()reads the existing value through the DB, which honors the tombstone, and the callback sees no existing value. Live writes and WAL replay go through the same check. Values already lost this way before this change stay lost.Checking only the active memtable is enough. Entries in immutable memtables and SST files are older than every entry in the active memtable, so a tombstone there cannot fall between an entry and its in-place update. Ingested files are the exception to that ordering, but ingestion first flushes the memtables that overlap the ingested files, so no entry that an ingested tombstone covers stays in the active memtable. Tombstones that iterators insert when converting point tombstones (
min_tombstones_for_range_conversion) go into the active memtable, so the check sees them.Cost
Nothing while the memtable has no range tombstones: the check returns before creating an iterator. Otherwise every
Put()that could update in place looks up the covering tombstone. It creates an iterator over the memtable's cached fragmented tombstone list, and the first suchPut()after a new range tombstone rebuilds that list from all tombstones in the memtable, on the write path. During WAL replay that happens under the DB mutex, which recovery holds throughout; each new range tombstone in the recovering memtable causes at most one rebuild, over all range tombstones then in that memtable. APut()to a key that a tombstone deletes appends a new entry; laterPut()s to that key update the new entry in place. Withinplace_callback, the appendingPut()also reads the key through the DB.Measured with
db_bench(release build, one thread pinned to one CPU of an AMD EPYC 9D25 host):fillseqthenoverwrite, 200k writes each over about 100k keys, 16-byte keys, 100-byte values overwritten in place by values of the same size, WAL disabled, and a write buffer large enough that everything stays in one memtable.db_benchwrites the range tombstones during both phases. Time per overwrite in ns, median of 7 runs with the builds alternating, minimum and maximum in parentheses:Most of the cost in the last two rows is the rebuild after every new tombstone. In this write-only benchmark nothing else rebuilds the list; when reads follow each new tombstone, the first read rebuilds it without this change too (from the code, not measured). In the last row, 37% of the overwrites hit a value that a newer tombstone deletes; without this change those writes were lost.
Only the combination that lost writes before pays this, and we know of no user that combines in-place updates with range deletions, so this change keeps the precise check; a TODO in
MemTable::IsDeletedByRangeTombstone()records the cost. Skipping the lookup when the existing entry is newer than every range tombstone in the memtable would help little: an in-place update keeps the entry's sequence number, so in the last two rows almost every entry stays older than the newest tombstone. Not updating in place at all while the memtable has range tombstones would make the check one atomic load, at the cost of appending everyPut()until the memtable is switched.Alternative considered
Rejecting range deletions on column families with in-place updates was not done. WAL replay still updates in place, so rejecting new range deletions cannot protect a WAL written without the option, and it would make
DeleteRange(), write batches, ingestion,ClipColumnFamily()and DB split/merge fail on such column families.Crash test
Crash test series with in-place updates write range tombstones again:
delrangepercent,test_ingest_standalone_range_deletion_one_inandmin_tombstones_for_range_conversionare no longer turned off for them, and setting one of them on the command line no longer turns in-place updates off for the series.Recovery with
inplace_callback, unchangedWith
inplace_callback,DB::Open()already fails with NotSupported when the WAL holds a callbackPut(): during recoveryPutCFImpl()does not read the DB, so the first replayedPut()of a key, which is never in the recovering memtable yet, has no existing value to pass to the callback, and WAL recovery treats the error as fatal. Withparanoid_checks=false, or withwal_recovery_mode=kSkipAnyCorruptedRecords, the error is only logged and the DB opens, but each failingPut()and the records after it in the same write batch are dropped. All of this happens the same way before and after this change. So this change neither fixes nor adds a recovery failure withinplace_callback. A fix belongs in a separate change.Reviewed By: xingbowang
Differential Revision: D134285230