Skip to content

Auto refresh iterator with snapshot - #13354

Closed
mszeszko-meta wants to merge 10 commits into
meta:mainfrom
mszeszko-meta:auto_refresh_iterator_option
Closed

mszeszko-meta wants to merge 10 commits into
meta:mainfrom
mszeszko-meta:auto_refresh_iterator_option

Conversation

@mszeszko-meta

@mszeszko-meta mszeszko-meta commented Jan 31, 2025 •

Copy link
Copy Markdown
Contributor

Problem

Once opened, iterator will preserve its' respective RocksDB snapshot for read consistency. Unless explicitly Refresh'ed, the iterator will hold on to the Init-time assigned SuperVersion throughout its lifetime. As time goes by, this might result in artificially long holdup of the obsolete memtables (potentially referenced by that superversion alone) consequently limiting the supply of the reclaimable memory on the DB instance. This behavior proved to be especially problematic in case of logical backups (outside of RocksDB BackupEngine) when scanning billions of rows artificially pinning the memtables for hours.

Solution

Building on top of the Refresh(const Snapshot* snapshot) API introduced in #10594, we're adding a new ReadOptions opt-in knob that (when enabled) will instruct the iterator to automatically refresh itself to the latest superversion - all that while retaining the originally assigned, explicit snapshot (supplied in read_options.snapshot at the time of iterator creation) for consistency. To ensure minimal performance overhead we're leveraging relaxed atomic for superversion freshness lookups.

Test plan

Correctness: New test to demonstrate the auto refresh behavior in contrast to legacy iterator: ./db_iterator_test --gtest_filter=*AutoRefreshIterator*.

Stress testing: We're adding command line parameter controlling the feature and hooking it up to as many iterator use cases in db_stress as we reasonably can with random feature on/off configuration in db_crashtest.py.

Benchmarking

The goal of this benchmark is to validate that throughput did not regress substantially. Benchmark was run on optimized build, 3-5 times for each respective category or till convergence. In addition, we configured aggressive threshold of 1 second for new Superversion creation. Experiments have been run 'in parallel' (at the same time) on separate db instances within a single host to evenly spread the potential adverse impact of noisy neighbor activities. Host specs [1].

TLDR; Baseline & new solution are practically indistinguishable from performance standpoint. Difference (positive or negative) in throughput relative to the baseline, if any, is no more than 1-2%.

Snapshot initialization approach:

This feature is only effective on iterators with well-defined snapshot passed via ReadOptions config. We modified the existing db_bench program to reflect that constraint. However, it quickly turned out that the actual Snapshot* initialization is quite expensive. Especially in case of 'tiny scans' (100 rows) contributing as much as 25-35 microseconds, which is ~20-30% of the average per/op latency unintentionally masking potentially adverse performance impact of this change. As a result, we ended up creating a single, explicit 'global' Snapshot* for all the future scans before running multiple experiments en masse. This is also a valuable data point for us to keep in mind in case of any future discussions about taking implicit snapshots - now we know what the lower bound cost could be.

"DB in memory" benchmark

DB Setup

  1. Allow a single memtable to grow large enough (~572MB) to fit in all the rows. Upon shutdown all the rows will be flushed to the WAL file (inspected 000004.log file is 541MB in size).
./db_bench -db=/tmp/testdb_in_mem -benchmarks="fillseq" -key_size=32 -value_size=512 -num=1000000 -write_buffer_size=600000000  max_write_buffer_number=2 -compression_type=none
  1. As a part of recovery in subsequent DB open, WAL will be processed to one or more SST files during the recovery. We're selecting a large block cache (cache_size parameter in db_bench script) suitable for holding the entire DB to test the “hot path” CPU overhead.
./db_bench -use_existing_db=true -db=/tmp/testdb_in_mem -statistics=false -cache_index_and_filter_blocks=true -benchmarks=seekrandom -preserve_internal_time_seconds=1 max_write_buffer_number=2 -explicit_snapshot=1 -use_direct_reads=1 -async_io=1 -num=? -seek_nexts=? -cache_size=? -write_buffer_size=? -auto_refresh_iterator_with_snapshot={0|1}
  seek_nexts=100; num=2,000,000 seek_nexts = 20,000; num=50000 seek_nexts = 400,000; num=2000
baseline 36362 (± 300) ops/sec, 928.8 (± 23) MB/s, 99.11% block cache hit 52.5 (± 0.5) ops/sec, 1402.05 (± 11.85) MB/s, 99.99% block cache hit 156.2 (± 6.3) ms / op, 1330.45 (± 54) MB/s, 99.95% block cache hit
auto refresh 35775.5 (± 537) ops/sec, 926.65 (± 13.75) MB/s, 99.11% block cache hit 53.5 (± 0.5) ops/sec, 1367.9 (± 9.5) MB/s, 99.99% block cache hit 162 (± 4.14) ms / op, 1281.35 (± 32.75) MB/s, 99.95% block cache hit

-cache_size=5000000000 -write_buffer_size=3200000000 -max_write_buffer_number=2

  seek_nexts=3,500,000; num=100
baseline 1447.5 (± 34.5) ms / op, 1255.1 (± 30) MB/s, 98.98% block cache hit
auto refresh 1473.5 (± 26.5) ms / op, 1232.6 (± 22.2) MB/s, 98.98% block cache hit

-cache_size=17680000000 -write_buffer_size=14500000000 -max_write_buffer_number=2

  seek_nexts=17,500,000; num=10
baseline 9.11 (± 0.185) s/op, 997 (± 20) MB/s
auto refresh 9.22 (± 0.1) s/op, 984 (± 11.4) MB/s

[1]

Specs

Property Value
RocksDB version 10.0.0
Date Mon Feb 3 23:21:03 2025
CPU 32 * Intel Xeon Processor (Skylake)
CPUCache 16384 KB
Keys 16 bytes each (+ 0 bytes user-defined timestamp)
Values 100 bytes each (50 bytes after compression)
Prefix 0 bytes
RawSize 5.5 MB (estimated)
FileSize 3.1 MB (estimated)
Compression Snappy
Compression sampling rate 0
Memtablerep SkipListFactory
Perf Level 1

@mszeszko-meta mszeszko-meta changed the title [EXPERIMENTAL] Auto refresh the iterator [EXPERIMENTAL] Auto refresh iterator Jan 31, 2025
@mszeszko-meta mszeszko-meta changed the title [EXPERIMENTAL] Auto refresh iterator [EXPERIMENTAL] Iterator auto refresh Jan 31, 2025
@mszeszko-meta
mszeszko-meta force-pushed the auto_refresh_iterator_option branch from 59fe197 to 777bad5 Compare January 31, 2025 03:33
@pdillinger

Copy link
Copy Markdown
Contributor

problematic in case of backups

in the case of logical backups (not BackupEngine)

@pdillinger pdillinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In addition to the performance test, I would like to see a unit test where we write some stuff, open an iterator, trigger and wait for a flush+full compaction, step the iterator, wait for background work, then verify some things like maybe size of all memtables is smaller, and all the old live sst files have been deleted. Something like that.

Comment thread db/arena_wrapped_db_iter.cc Resolved
@mszeszko-meta
mszeszko-meta force-pushed the auto_refresh_iterator_option branch 3 times, most recently from e03ba41 to e65144a Compare February 4, 2025 09:04
@mszeszko-meta mszeszko-meta changed the title [EXPERIMENTAL] Iterator auto refresh Iterator auto refresh Feb 4, 2025
@mszeszko-meta
mszeszko-meta marked this pull request as ready for review February 4, 2025 15:53
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Comment thread include/rocksdb/options.h Resolved
Comment thread include/rocksdb/options.h Resolved

@pdillinger pdillinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Otherwise, test etc. looks good

Comment thread db/db_iterator_test.cc Resolved
Comment thread db/db_iterator_test.cc Resolved
Comment thread db/db_iterator_test.cc Resolved
Comment thread db/db_iterator_test.cc Resolved
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

1 similar comment
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

@mszeszko-meta
mszeszko-meta force-pushed the auto_refresh_iterator_option branch from 4d744f6 to 57abeeb Compare February 4, 2025 23:50
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@pdillinger pdillinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Along with updating unit tests for reverse iterator stuff, then we should be good.

Comment thread db/arena_wrapped_db_iter.cc Resolved
Comment thread db/arena_wrapped_db_iter.cc Resolved
Comment thread db/arena_wrapped_db_iter.cc Resolved
Comment thread db/arena_wrapped_db_iter.cc Resolved

@pdillinger pdillinger left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Almost forgot: we also need to add this to db_stress. We should add a command line parameter controlling the feature (for differential diagnostic/debugging purposes), connect it up to as many iterator uses in db_stress as we reasonably can, and then set it randomly in db_crashtest.py. This could be another PR, but we can't forget about it as part of the same release cycle.

@mszeszko-meta

Copy link
Copy Markdown
Contributor Author

Almost forgot: we also need to add this to db_stress. We should add a command line parameter controlling the feature (for differential diagnostic/debugging purposes), connect it up to as many iterator uses in db_stress as we reasonably can, and then set it randomly in db_crashtest.py. This could be another PR, but we can't forget about it as part of the same release cycle.

Agreed. Separately, with even lesser urgency (could be different release cycle), we would probably want to add similar plumbing for db_bench.

Comment thread include/rocksdb/options.h Resolved
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

@mszeszko-meta mszeszko-meta changed the title Iterator auto refresh Auto refresh iterator with snapshot Feb 7, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@mszeszko-meta
mszeszko-meta force-pushed the auto_refresh_iterator_option branch from a89ed72 to eec3264 Compare February 12, 2025 02:11
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@mszeszko-meta

mszeszko-meta commented Feb 12, 2025 •

Copy link
Copy Markdown
Contributor Author

This PR contains complete set of changes including: implementation, unit tests, dbbench plumbing and feature onboarding to dbstress crash tests. We do not expect any followup PRs.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@mszeszko-meta merged this pull request in 8234d67.

std::unique_ptr<ManagedSnapshot> snapshot = nullptr;
if (ro.auto_refresh_iterator_with_snapshot) {
snapshot = std::make_unique<ManagedSnapshot>(db_);
ro.snapshot = snapshot->snapshot();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems to be a problem because we are acquiring a snapshot before getting pre_read_expected_values which makes the timing of the read effectively before reading pre_read_expected_values rather than between reading pre_read_expected_values and post_read_expected_values.

I'm sure other instances of creating snapshots here are similarly problematic.


// It's perfectly fine to unref the corresponding superversion
// as we rely on pinning behavior of snapshot for consistency.
DoRefresh(read_options_.snapshot, cur_sv_number);

@mszeszko-meta mszeszko-meta Feb 19, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Behind the scenes DoRefresh calls the ::~DBIter destructor, which unregisters super version handle with CleanupSuperVersionHandle callback. As a part of this cleanup, we might (and in some cases we do [1]) call the PurgeObsoleteFiles which down the road resolves to MarkAsTrash which is first class citizen for error injection. When (metadata) read error is injected in rocksdb::FaultInjectionTestFS::MaybeInjectThreadLocalError its' corresponding 'not-ok' Status is being lost as subsequent fs_->DeleteFile and OnDeleteFile on DeleteScheduler::AddFileToDeletionQueue likely succeed. And even if they don't succeed, the status is just logged as error in DBImpl::DeleteObsoleteFileImpl. All that while FaultInjectionTestFS instance increments its' injection error counter which is used in its' pure form by our db stress logic to assess if all expected errors have been properly propagated. This behavior predates the 'auto_refresh_iterator_with_snapshot' feature. However, due to significantly higher chance of exercising that new codepath (previously db stress code had to call Refresh explicitly, now it happens automatically 'from time to time'), we are exposed to failures like this [1] where error injected in MaybeAutoRefresh is not propagated upstream. I guess that failure in file deletion is not the end of the world in real life scenarios as compaction would have cleaned up those obsolete files eventually. Therefore, I'm leaning towards excluding error injection accounting for iterators running in auto_refresh_iterator_with_snapshot=true in context of rocksdb::NonBatchedOpsStressTest::TestPrefixScan test. That way we 'solve' the dbstress problem, avoid potentially risky code refactoring and, most importantly, still benefit from validating prefix iterator in auto_refresh_iterator_with_snapshot=true mode in the test main loop.

[1]

Screenshot 2025-02-18 at 7 23 58 PM

void ArenaWrappedDBIter::MaybeAutoRefresh(bool is_seek,
DBIter::Direction direction) {
if (cfh_ != nullptr && read_options_.snapshot != nullptr && allow_refresh_ &&
read_options_.auto_refresh_iterator_with_snapshot) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check is missing status().ok() at the end to properly account for read errors in Next / Prev (possibly injected in dbstress test).

@mszeszko-meta

Copy link
Copy Markdown
Contributor Author

In context of T214812411, there are at least 2 high level categories of errors: 1) likely caused by wide-scope snapshot initialization (issue found by Peter), 2) lack of proper error propagation. Addressing 1) should hopefully be straightforward. Wrt 2) - part of the problem is a real miss (we should condition auto refresh on status().ok() after calling to Next / Prev), but another part - failure in deleting files (as described in the more elaborate comment above) - should not really be asserted on in dbstress.

facebook-github-bot pushed a commit that referenced this pull request Feb 20, 2025
Summary:
This PR attempts to fix the **dbstress failures** post #13354. There are at least 2 high level categories of errors: 1) likely caused by wide-scope snapshot initialization ([issue found by Peter](#13354 (comment))), 2) lack of proper error propagation. Wrt 2), part of the problem is a real miss (we should condition auto refresh on `status().ok()` after calling to `Next` / `Prev`), but another part - [failure in propagating dbstress-injected read error](#13354 (comment)) in file deletion is expected and should not be asserted on in dbstress.

Pull Request resolved: #13408

Test Plan:
Confirmed there are no more errors after running sandcastle crashtest for each of the failing flavors:

```hcl
https://www.internalfb.com/sandcastle/workflow/252201579138859344
https://www.internalfb.com/sandcastle/workflow/3233584532458171962
https://www.internalfb.com/sandcastle/workflow/1283525893806766134
https://www.internalfb.com/sandcastle/workflow/2796735368603351293
https://www.internalfb.com/sandcastle/workflow/3792030886252148966
https://www.internalfb.com/sandcastle/workflow/67553994428973733
https://www.internalfb.com/sandcastle/workflow/3886606478427208295
https://www.internalfb.com/sandcastle/workflow/1684346260642682928
https://www.internalfb.com/sandcastle/workflow/4197354852715406516
https://www.internalfb.com/sandcastle/workflow/535928355663233170
https://www.internalfb.com/sandcastle/workflow/3409224917925569737
```

Reviewed By: cbi42

Differential Revision: D69869766

Pulled By: mszeszko-meta

fbshipit-source-id: 7a5b121218fb1dc0a37887d6fe2a5c07e2b894cf
ybtsdst pushed a commit to ybtsdst/rocksdb that referenced this pull request Apr 27, 2025
Summary:
# Problem
Once opened, iterator will preserve its' respective RocksDB snapshot for read consistency. Unless explicitly `Refresh'ed`, the iterator will hold on to the `Init`-time assigned `SuperVersion` throughout its lifetime. As time goes by, this might result in artificially long holdup of the obsolete memtables (_potentially_ referenced by that superversion alone) consequently limiting the supply of the reclaimable memory on the DB instance. This behavior proved to be especially problematic in case of _logical_ backups (outside of RocksDB `BackupEngine`).

# Solution
Building on top of the `Refresh(const Snapshot* snapshot)` API introduced in meta#10594, we're adding a new `ReadOptions` opt-in knob that (when enabled) will instruct the iterator to automatically refresh itself to the latest superversion - all that while retaining the originally assigned, explicit snapshot (supplied in `read_options.snapshot` at the time of iterator creation) for consistency. To ensure minimal performance overhead we're leveraging relaxed atomic for superversion freshness lookups.

Pull Request resolved: meta#13354

Test Plan:
**Correctness:** New test to demonstrate the auto refresh behavior in contrast to legacy iterator: `./db_iterator_test --gtest_filter=*AutoRefreshIterator*`.

**Stress testing:** We're adding command line parameter controlling the feature and hooking it up to as many iterator use cases in `db_stress` as we reasonably can with random feature on/off configuration in db_crashtest.py.

# Benchmarking

The goal of this benchmark is to validate that throughput did not regress substantially. Benchmark was run on optimized build, 3-5 times for each respective category or till convergence. In addition, we configured aggressive threshold of 1 second for new `Superversion` creation. Experiments have been run 'in parallel' (at the same time) on separate db instances within a single host to evenly spread the potential adverse impact of noisy neighbor activities. Host specs [1].

**TLDR;** Baseline & new solution are practically indistinguishable from performance standpoint. Difference (positive or negative) in throughput relative to the baseline, if any, is no more than 1-2%.

**Snapshot initialization approach:**

This feature is only effective on iterators with well-defined `snapshot` passed via `ReadOptions` config. We modified the existing `db_bench` program to reflect that constraint. However, it quickly turned out that the actual `Snapshot*` initialization is quite expensive. Especially in case of 'tiny scans' (100 rows) contributing as much as 25-35 microseconds, which is ~20-30% of the average per/op latency unintentionally masking _potentially_ adverse performance impact of this change. As a result, we ended up creating a single, explicit 'global' `Snapshot*` for all the future scans _before_ running multiple experiments en masse. This is also a valuable data point for us to keep in mind in case of any future discussions about taking implicit snapshots - now we know what the lower bound cost could be.

## "DB in memory" benchmark

**DB Setup**

1. Allow a single memtable to grow large enough (~572MB) to fit in all the rows. Upon shutdown all the rows will be flushed to the WAL file (inspected `000004.log` file is 541MB in size).

```
./db_bench -db=/tmp/testdb_in_mem -benchmarks="fillseq" -key_size=32 -value_size=512 -num=1000000 -write_buffer_size=600000000  max_write_buffer_number=2 -compression_type=none
```

2. As a part of recovery in subsequent DB open, WAL will be processed to one or more SST files during the recovery. We're selecting a large block cache (`cache_size` parameter in `db_bench` script) suitable for holding the entire DB to test the “hot path” CPU overhead.

```
./db_bench -use_existing_db=true -db=/tmp/testdb_in_mem -statistics=false -cache_index_and_filter_blocks=true -benchmarks=seekrandom -preserve_internal_time_seconds=1 max_write_buffer_number=2 -explicit_snapshot=1 -use_direct_reads=1 -async_io=1 -num=? -seek_nexts=? -cache_size=? -write_buffer_size=? -auto_refresh_iterator_with_snapshot={0|1}
```

  | seek_nexts=100; num=2,000,000 | seek_nexts = 20,000; num=50000  | seek_nexts = 400,000; num=2000
-- | -- | -- | --
baseline | 36362 (± 300) ops/sec, 928.8 (± 23) MB/s, 99.11% block cache hit  | 52.5 (± 0.5) ops/sec, 1402.05 (± 11.85) MB/s, 99.99% block cache hit | 156.2 (± 6.3) ms / op, 1330.45 (± 54) MB/s, 99.95% block cache hit
auto refresh |  35775.5 (± 537) ops/sec, 926.65 (± 13.75) MB/s, 99.11% block cache hit |  53.5 (± 0.5) ops/sec, 1367.9 (± 9.5) MB/s, 99.99% block cache hit |  162 (± 4.14) ms / op, 1281.35 (± 32.75) MB/s, 99.95% block cache hit

_-cache_size=5000000000 -write_buffer_size=3200000000 -max_write_buffer_number=2_

  | seek_nexts=3,500,000; num=100
-- | --
baseline | 1447.5 (± 34.5) ms / op, 1255.1 (± 30) MB/s, 98.98% block cache hit
auto refresh | 1473.5 (± 26.5) ms / op, 1232.6 (± 22.2) MB/s, 98.98% block cache hit

_-cache_size=17680000000 -write_buffer_size=14500000000 -max_write_buffer_number=2_

  | seek_nexts=17,500,000; num=10
-- | --
baseline | 9.11 (± 0.185) s/op, 997 (± 20) MB/s
auto refresh | 9.22 (± 0.1) s/op, 984 (± 11.4) MB/s

[1]

### Specs

  | Property | Value
-- | --
RocksDB | version 10.0.0
Date | Mon Feb  3 23:21:03 2025
CPU | 32 * Intel Xeon Processor (Skylake)
CPUCache | 16384 KB
Keys | 16 bytes each (+ 0 bytes user-defined timestamp)
Values | 100 bytes each (50 bytes after compression)
Prefix | 0 bytes
RawSize | 5.5 MB (estimated)
FileSize | 3.1 MB (estimated)
Compression | Snappy
Compression sampling rate | 0
Memtablerep | SkipListFactory
Perf Level | 1

Reviewed By: pdillinger

Differential Revision: D69122091

Pulled By: mszeszko-meta

fbshipit-source-id: 147ef7c4fe9507b6fb77f6de03415bf3bec337a8
ybtsdst pushed a commit to ybtsdst/rocksdb that referenced this pull request Apr 27, 2025
Summary:
This PR attempts to fix the **dbstress failures** post meta#13354. There are at least 2 high level categories of errors: 1) likely caused by wide-scope snapshot initialization ([issue found by Peter](meta#13354 (comment))), 2) lack of proper error propagation. Wrt 2), part of the problem is a real miss (we should condition auto refresh on `status().ok()` after calling to `Next` / `Prev`), but another part - [failure in propagating dbstress-injected read error](meta#13354 (comment)) in file deletion is expected and should not be asserted on in dbstress.

Pull Request resolved: meta#13408

Test Plan:
Confirmed there are no more errors after running sandcastle crashtest for each of the failing flavors:

```hcl
https://www.internalfb.com/sandcastle/workflow/252201579138859344
https://www.internalfb.com/sandcastle/workflow/3233584532458171962
https://www.internalfb.com/sandcastle/workflow/1283525893806766134
https://www.internalfb.com/sandcastle/workflow/2796735368603351293
https://www.internalfb.com/sandcastle/workflow/3792030886252148966
https://www.internalfb.com/sandcastle/workflow/67553994428973733
https://www.internalfb.com/sandcastle/workflow/3886606478427208295
https://www.internalfb.com/sandcastle/workflow/1684346260642682928
https://www.internalfb.com/sandcastle/workflow/4197354852715406516
https://www.internalfb.com/sandcastle/workflow/535928355663233170
https://www.internalfb.com/sandcastle/workflow/3409224917925569737
```

Reviewed By: cbi42

Differential Revision: D69869766

Pulled By: mszeszko-meta

fbshipit-source-id: 7a5b121218fb1dc0a37887d6fe2a5c07e2b894cf
Tim-Reed pushed a commit to Tim-Reed/rocksdb that referenced this pull request Oct 1, 2026
Summary:
# Problem
Once opened, iterator will preserve its' respective RocksDB snapshot for read consistency. Unless explicitly `Refresh'ed`, the iterator will hold on to the `Init`-time assigned `SuperVersion` throughout its lifetime. As time goes by, this might result in artificially long holdup of the obsolete memtables (_potentially_ referenced by that superversion alone) consequently limiting the supply of the reclaimable memory on the DB instance. This behavior proved to be especially problematic in case of _logical_ backups (outside of RocksDB `BackupEngine`).

# Solution
Building on top of the `Refresh(const Snapshot* snapshot)` API introduced in meta#10594, we're adding a new `ReadOptions` opt-in knob that (when enabled) will instruct the iterator to automatically refresh itself to the latest superversion - all that while retaining the originally assigned, explicit snapshot (supplied in `read_options.snapshot` at the time of iterator creation) for consistency. To ensure minimal performance overhead we're leveraging relaxed atomic for superversion freshness lookups.

Pull Request resolved: meta#13354

Test Plan:
**Correctness:** New test to demonstrate the auto refresh behavior in contrast to legacy iterator: `./db_iterator_test --gtest_filter=*AutoRefreshIterator*`.

**Stress testing:** We're adding command line parameter controlling the feature and hooking it up to as many iterator use cases in `db_stress` as we reasonably can with random feature on/off configuration in db_crashtest.py.

# Benchmarking

The goal of this benchmark is to validate that throughput did not regress substantially. Benchmark was run on optimized build, 3-5 times for each respective category or till convergence. In addition, we configured aggressive threshold of 1 second for new `Superversion` creation. Experiments have been run 'in parallel' (at the same time) on separate db instances within a single host to evenly spread the potential adverse impact of noisy neighbor activities. Host specs [1].

**TLDR;** Baseline & new solution are practically indistinguishable from performance standpoint. Difference (positive or negative) in throughput relative to the baseline, if any, is no more than 1-2%.

**Snapshot initialization approach:**

This feature is only effective on iterators with well-defined `snapshot` passed via `ReadOptions` config. We modified the existing `db_bench` program to reflect that constraint. However, it quickly turned out that the actual `Snapshot*` initialization is quite expensive. Especially in case of 'tiny scans' (100 rows) contributing as much as 25-35 microseconds, which is ~20-30% of the average per/op latency unintentionally masking _potentially_ adverse performance impact of this change. As a result, we ended up creating a single, explicit 'global' `Snapshot*` for all the future scans _before_ running multiple experiments en masse. This is also a valuable data point for us to keep in mind in case of any future discussions about taking implicit snapshots - now we know what the lower bound cost could be.

## "DB in memory" benchmark

**DB Setup**

1. Allow a single memtable to grow large enough (~572MB) to fit in all the rows. Upon shutdown all the rows will be flushed to the WAL file (inspected `000004.log` file is 541MB in size).

```
./db_bench -db=/tmp/testdb_in_mem -benchmarks="fillseq" -key_size=32 -value_size=512 -num=1000000 -write_buffer_size=600000000  max_write_buffer_number=2 -compression_type=none
```

2. As a part of recovery in subsequent DB open, WAL will be processed to one or more SST files during the recovery. We're selecting a large block cache (`cache_size` parameter in `db_bench` script) suitable for holding the entire DB to test the “hot path” CPU overhead.

```
./db_bench -use_existing_db=true -db=/tmp/testdb_in_mem -statistics=false -cache_index_and_filter_blocks=true -benchmarks=seekrandom -preserve_internal_time_seconds=1 max_write_buffer_number=2 -explicit_snapshot=1 -use_direct_reads=1 -async_io=1 -num=? -seek_nexts=? -cache_size=? -write_buffer_size=? -auto_refresh_iterator_with_snapshot={0|1}
```

  | seek_nexts=100; num=2,000,000 | seek_nexts = 20,000; num=50000  | seek_nexts = 400,000; num=2000
-- | -- | -- | --
baseline | 36362 (± 300) ops/sec, 928.8 (± 23) MB/s, 99.11% block cache hit  | 52.5 (± 0.5) ops/sec, 1402.05 (± 11.85) MB/s, 99.99% block cache hit | 156.2 (± 6.3) ms / op, 1330.45 (± 54) MB/s, 99.95% block cache hit
auto refresh |  35775.5 (± 537) ops/sec, 926.65 (± 13.75) MB/s, 99.11% block cache hit |  53.5 (± 0.5) ops/sec, 1367.9 (± 9.5) MB/s, 99.99% block cache hit |  162 (± 4.14) ms / op, 1281.35 (± 32.75) MB/s, 99.95% block cache hit

_-cache_size=5000000000 -write_buffer_size=3200000000 -max_write_buffer_number=2_

  | seek_nexts=3,500,000; num=100
-- | --
baseline | 1447.5 (± 34.5) ms / op, 1255.1 (± 30) MB/s, 98.98% block cache hit
auto refresh | 1473.5 (± 26.5) ms / op, 1232.6 (± 22.2) MB/s, 98.98% block cache hit

_-cache_size=17680000000 -write_buffer_size=14500000000 -max_write_buffer_number=2_

  | seek_nexts=17,500,000; num=10
-- | --
baseline | 9.11 (± 0.185) s/op, 997 (± 20) MB/s
auto refresh | 9.22 (± 0.1) s/op, 984 (± 11.4) MB/s

[1]

### Specs

  | Property | Value
-- | --
RocksDB | version 10.0.0
Date | Mon Feb  3 23:21:03 2025
CPU | 32 * Intel Xeon Processor (Skylake)
CPUCache | 16384 KB
Keys | 16 bytes each (+ 0 bytes user-defined timestamp)
Values | 100 bytes each (50 bytes after compression)
Prefix | 0 bytes
RawSize | 5.5 MB (estimated)
FileSize | 3.1 MB (estimated)
Compression | Snappy
Compression sampling rate | 0
Memtablerep | SkipListFactory
Perf Level | 1

Reviewed By: pdillinger

Differential Revision: D69122091

Pulled By: mszeszko-meta

fbshipit-source-id: 147ef7c4fe9507b6fb77f6de03415bf3bec337a8
Tim-Reed pushed a commit to Tim-Reed/rocksdb that referenced this pull request Oct 1, 2026
Summary:
This PR attempts to fix the **dbstress failures** post meta#13354. There are at least 2 high level categories of errors: 1) likely caused by wide-scope snapshot initialization ([issue found by Peter](meta#13354 (comment))), 2) lack of proper error propagation. Wrt 2), part of the problem is a real miss (we should condition auto refresh on `status().ok()` after calling to `Next` / `Prev`), but another part - [failure in propagating dbstress-injected read error](meta#13354 (comment)) in file deletion is expected and should not be asserted on in dbstress.

Pull Request resolved: meta#13408

Test Plan:
Confirmed there are no more errors after running sandcastle crashtest for each of the failing flavors:

```hcl
https://www.internalfb.com/sandcastle/workflow/252201579138859344
https://www.internalfb.com/sandcastle/workflow/3233584532458171962
https://www.internalfb.com/sandcastle/workflow/1283525893806766134
https://www.internalfb.com/sandcastle/workflow/2796735368603351293
https://www.internalfb.com/sandcastle/workflow/3792030886252148966
https://www.internalfb.com/sandcastle/workflow/67553994428973733
https://www.internalfb.com/sandcastle/workflow/3886606478427208295
https://www.internalfb.com/sandcastle/workflow/1684346260642682928
https://www.internalfb.com/sandcastle/workflow/4197354852715406516
https://www.internalfb.com/sandcastle/workflow/535928355663233170
https://www.internalfb.com/sandcastle/workflow/3409224917925569737
```

Reviewed By: cbi42

Differential Revision: D69869766

Pulled By: mszeszko-meta

fbshipit-source-id: 7a5b121218fb1dc0a37887d6fe2a5c07e2b894cf
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.

3 participants