Compare commits

..
Author SHA1 Message Date
Jay Huh 10dfc6acb8 Update version and HISTORY.md for 11.5.2 release 2026-06-25 14:52:45 -07:00
Jay Huh d46d4b7f82 Revert reverse-comparator handling in range tree lock manager (#14887)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14887

This reverts two changes to the range-lock endpoint comparator and restores the prior, direction-agnostic behavior:
- https://github.com/facebook/rocksdb/pull/14831 ("Fix range lock manager crash with reverse comparator"), which made RangeTreeLockManager::CompareDbtEndpoints reverse-aware, and
- https://github.com/facebook/rocksdb/pull/14880 (a follow-up that only corrected the equal-length / point-range branch).

Why: #14831 taught the endpoint comparator about reverse-ordered column families by flipping the suffix tie-break based on comparator direction. However, callers that use range locking on a reverse-ordered CF already flip the start/end endpoints themselves before calling lock_range() -- for example, MyRocks does this in ha_rocksdb::set_range_lock() (see its "RangeFlagsShouldBeFlippedForRevCF" comment). The two corrections stack and produce left > right again, which:
(a) trips paranoid_invariant(compare(left, right) <= 0) in toku::locktree::try_acquire_lock() and aborts on equality/point locks, and
(b) for range scans, mis-orders the endpoints and produces an incorrect lock range, leading to silent correctness issues under concurrency.
#14880 only addressed (a) (the equal-length / point-range branch), not (b), so it was an incomplete fix at the wrong layer.

Decision: keep CompareDbtEndpoints direction-agnostic and make it an explicit caller requirement to pass a flipped (swapped start/end, with negated infimum/supremum flags) range for range locking to work on a reverse-ordered column family. A function-level comment documenting this contract is added so reverse-awareness is not reintroduced here.

Removes the RangeLockWithReverseComparator unit test added by #14831 (it asserts the reverted direction-flipped ordering). Keeps a regression test that a point range lock [infimum(K), supremum(K)] works on a reverse-comparator column family.

Reviewed By: xingbowang

Differential Revision: D109713989

fbshipit-source-id: 4ea0999c92645e1adc9cd1114d924cad142f2001
2026-06-25 14:26:31 -07:00
Jay Huh a127d1533b Update version and HISTORY.md for 11.5.1 release 2026-06-24 16:25:47 -07:00
Josh Kang 928ebd5b8b Do not allow read only DBs to delete obsolete files (#14881)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14881

Read-only DB instances can share a directory with a live writer, so their view of live files is only a snapshot from the time they opened. After read-only DB open began setting `opened_successfully_` for its intended open-success semantics, that state had an unintended side effect: the common close path treated a successful read-only open like a read-write open and ran obsolete-file cleanup. If the writer created or made files live after the read-only handle opened, that cleanup could use the stale read-only live set and delete files still needed by the writer.

This change records whether a `DBImpl` was opened read-only and skips close-time obsolete-file cleanup for read-only DBs instead of simply keeping `opened_successfully_=false`, which could be confusing and easily mistaken in the future again.

For completeness I also updated other callsites of `opened_successfully_`, but those should not be real bugs as ReadOnly DBs do not run flushes/compactions or write to WALs.

Reviewed By: xingbowang

Differential Revision: D109622445

fbshipit-source-id: d7be4b20fce86ccb218a63ac6f5b707b316aac79
2026-06-24 16:23:13 -07:00
Jay Huh afcf52a996 Fix range lock manager assertion crash on reverse-comparator CF point locks (#14880)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14880

The RocksDB 11.5.0 reverse-comparator range-lock fix (the HISTORY.md entry "Fixed a bug where the range lock manager would crash with an assertion failure when using a reverse comparator column family") made `RangeTreeLockManager::CompareDbtEndpoints` reverse-aware by wrapping the endpoint-suffix tie-break results in an `is_reverse` flip. The length-disparity (different user key) branches need that flip, but the equal-length / equal-user-key branch was flipped too, and that is incorrect.

Range-lock endpoints are encoded as `[suffix_byte][user_key]` where the suffix is `SUFFIX_INFIMUM` (0x0) or `SUFFIX_SUPREMUM` (0x1). When two endpoints share the same user key and differ only in the suffix byte, the suffix is a positional boundary marker for that key, not key content, so the infimum endpoint must sort before the supremum endpoint in any total order, regardless of the column family sort direction. Flipping it for a reverse comparator makes `m_cmp(infimum(K), supremum(K)) > 0`.

MyRocks issues an equality lock (e.g. `UPDATE t1 SET a=123 WHERE a=35`) as the range `[infimum(K), supremum(K)]` over the same user key K. On a reverse (`rev:`) column family — which uses the stock `ReverseBytewiseComparator()`, so `is_reverse` is true — the flipped comparator reports `left > right`, tripping `paranoid_invariant(m_cmp(left_key, right_key) <= 0)` in `toku::locktree::try_acquire_lock` and aborting the server with SIGABRT. This broke many MyRocks `---range_locking` MTR tests (partial_index*, bypass_select_range_sk*, alter_table_online_debug, allow_no_primary_key_with_sk) after RocksDB was bumped to 11.5.0 in D108936924.

Fix: in the equal-length / equal-user-key branch of `CompareDbtEndpoints`, compare the suffix bytes without the `is_reverse` flip so infimum always precedes supremum. The length-disparity branches keep the reverse-aware flip (still covered by `RangeLockWithReverseComparator`).

Note: this lands in `internal_repo_rocksdb/repo`, the RocksDB development tree; it reaches `fbcode/rocksdb/src` (what MyRocks/ZippyDB link) through the RocksDB release sync. Tracked in T276980728.

Reviewed By: xingbowang

Differential Revision: D109591908

fbshipit-source-id: 078b16e6e51f4f365b4a8d539ac5b968a7461729
2026-06-24 16:23:04 -07:00
anand1976 e688e7b235 Update HISTORY.md for 11.5.0 release 2026-06-17 14:24:03 -07:00
Josh Kang e292aa95b3 Allow skipping lmax index and filter block prefetches for external file ingestion
Summary:
Bulk external file ingestion can spend significant commit time prefetching index/filter blocks for large Lmax files. This adds `IngestExternalFileOptions::prefetch_lmax_index_and_filter_blocks` (default true) so bulk-load callers can defer that metadata prefetch when the cache doesn't need immediate warming. The option threads through `FileMetaData::skip_index_and_filter_blocks_prefetch` to drive the existing `LoadTableHandlers()` prefetch logic. Benchmarks show ~40% commit latency reduction (3459 → 2112 μs) and ~55% lower IO time (2560 → 1145 μs) when disabled.

Benchmarks:

```
db_bench --benchmarks=ingestexternalfile --num=2200000 --ingest_external_file_num_batches=1 --ingest_external_file_batch_size=1 --ingest_external_file_use_file_info=true --ingest_external_file_fill_cache=true --cache_index_and_filter_blocks=true --bloom_bits=10 --statistics=true --stats_level=3 --ingest_external_file_prefetch_lmax_index_and_filter_blocks=<true|false>
```

| `ingest_external_file_prefetch_lmax_index_and_filter_blocks` | `rocksdb.ingest.external.file.run.micros` | `rocksdb.table.open.io.micros` | index/filter cache adds | last-level read bytes |
| `true` | `3459 us` | `2560 us` | `1 / 1` | `3551559` |
| `false` | `2112 us` | `1145 us` | `0 / 0` | `1333` |

Reviewed By: xingbowang

Differential Revision: D108678511

fbshipit-source-id: e8951e3aad36cc0accffb33ecc3cc1b4aeb89459
2026-06-16 11:20:07 -07:00
Peter Dillinger 123030ffee Graceful shutdown on fatal benchmark errors (#14855)
Summary:
db_bench worker operations historically responded to a fatal error (failed Get/Put/Merge/Delete, checksum failure, etc.) by calling db_bench_exit() -> exit() in place. When that happened on a worker thread while sibling workers and background (compaction/flush) threads were still running, exit() began static/global destruction underneath those live threads. A thread mid-call into a user callback (e.g. a custom Env/ FileSystem) could then touch a global/singleton that had already been destroyed -> cross-thread use-after-free. This is the same class of bug fixed for db_stress in https://github.com/facebook/rocksdb/issues/14850, but it also matters for db_bench's secondary role as an *integration validation* tool: we want production-like processes to shut down cleanly after an error (so a custom FileSystem can log the adverse condition and run its teardown) rather than crash or _exit at the first sign of trouble.

This change makes fatal errors on benchmark worker threads unwind instead of exiting in place, and performs an ordered shutdown on the main thread:

  * SharedState gains a cooperative fatal flag + first-error Status/message and SetFatal(). A worker that hits a fatal error records it and returns out of its operation (releasing its DbUseGuard) instead of exiting.
  * Op loops stop promptly via Duration: the loop-termination helper now consults the run's fatal flag at its existing throttled check boundary (~every FLAGS_ops_between_duration_checks ops), so peers wind down with no added per-key cost.
  * After RunBenchmark joins all workers, the (guard-free) main thread runs the ordered shutdown -- DeleteDBs() closes all DBs, joining background threads and running user Env/FileSystem teardown -- and only then exits nonzero. No static destructors run while RocksDB threads or user callbacks are still live.
  * The worker fatal sites that previously called db_bench_exit() (BGWriter, DoDelete, RandomWithVerify, UpdateRandom, XORUpdateRandom, MergeRandom, ReadRandomMergeRandom, VerifyChecksum, VerifyFileChecksums, RandomReplaceKeys, Replay, DoDeterministicCompact) now SetFatal + return. Flush (main-thread) routes to ErrorExit() which closes DBs first.

Supporting changes:

  * Duration is now a nested member type of SharedState, constructible only via SharedState::MakeDuration() (movable, non-copyable). This binds every Duration to its run's fatal flag by construction and removes the previous mutable global (g_cooperative_abort_flag) and its set/clear lifecycle. A `using Duration = SharedState::Duration;` alias keeps call sites tidy.
  * New test-only flag --fault_injection_read_after_reads: wraps the FileSystem so random-access reads start returning a non-retryable IOError after N reads, to exercise the fatal/shutdown path on demand.
  * New flag --fatal_shutdown_watchdog_seconds (default 180, 0 disables): arms a one-shot detached watchdog on the first fatal error (and before ErrorExit's cleanup) that forces port::ImmediateExit() if the careful shutdown itself hangs (e.g. a background thread or misbehaving Env that never returns). This is a backstop, not the path under test.
  * Added an "Error handling background" comment documenting the rationale and the machinery, and an invariant comment at db_bench_exit covering pre-Open vs worker vs main-thread exit rules.

Existing _Exit()/abort() sites are intentionally left unchanged.

Future work:
  * Worker ops that still abort() on error (ReadRandom, ReadRandomFast, ReadToRowCache, MultiReadRandom, MixGraph, AppendRandom, UpdateRandom's read path, BGScan, RandomTransaction, CreateNewCf) are not the use-after-free class (abort() skips static destruction) but are not graceful either; converting them would extend clean-shutdown coverage. RandomTransaction needs a rollback decision; MultiReadRandom needs a structured break-out.
  * Post-Open main-thread db_bench_exit() sites in Open()/Run()/ VerifyDBFromDB() are the harder, https://github.com/facebook/rocksdb/issues/14850-class case: Open() is reachable both under and outside a DbStateMutationGuard, so it cannot unconditionally call ErrorExit() without deadlock. Needs guard-aware handling or restructuring Open() to return Status.
  * Pre-Open setup exits (option/flag parsing, cache setup) correctly stay plain exit() -- no live threads -- and are out of scope.
  * Fault injection currently covers read faults only; verifying graceful shutdown for write/open/transaction error paths would need write-fault, open-fault, and conflict injection.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14855

Test Plan:
Built debug db_bench (make db_bench) and validated manually:

  * Correctness of loop termination (unchanged by the Duration refactor):
    - Count-bounded is exact: `db_bench --benchmarks=fillrandom --num=300` reports "300 operations".
    - Time-bounded honors duration and is not capped by max_ops: `db_bench --benchmarks=readrandom --use_existing_db --duration=5 --reads=3000` ran 5.002 s / ~2.0-2.7M operations.

  * Graceful shutdown on a fatal error (multi-threaded, peers live): `db_bench --benchmarks=xorupdaterandom --use_existing_db --threads=8 --duration=30 --fault_injection_read_after_reads=5000` -> exit code 1, ZERO "Received signal", prints 'Fatal error during "xorupdaterandom"' after the ordered shutdown.

  * Confirmed this is a real fix, not just masking: rebuilt pristine main + only the fault-injection flag (graceful-shutdown changes reverted) and ran the same command -> SIGSEGV (rc=139), crash stack in __run_exit_handlers -> jemalloc free, triggered from XORUpdateRandom -> db_bench_exit() -> exit() with sibling threads still live. With the fix in place the same workload is graceful (above).

  * Watchdog:
    - Default (180 s): graceful runs above complete with no watchdog message (no spurious firing).
    - --fatal_shutdown_watchdog_seconds=0 disables it (still graceful).
    - Positive: temporarily inserted a 60 s sleep before DeleteDBs in the fatal path, ran with --fatal_shutdown_watchdog_seconds=3 -> process exited at ~3 s (not 60 s) with the watchdog message and rc=1; reverted the temporary sleep and rebuilt clean.

  * No benchmark performance impact (normal, error-free runs):
    - Structural: the cooperative-abort check adds no per-key work. It is evaluated only at Duration's pre-existing throttled boundary (~every FLAGS_ops_between_duration_checks ops, same site as the existing time check); there is no atomic load on the per-op hot path. Fatal recording (SetFatal) lives only in cold error branches. A Duration is constructed once per benchmark via the factory (returned by value, elided), not per op.
    - Observed: error-free fillrandom / fillseq / readrandom throughput (e.g. fillrandom ~300K ops/s, fillseq ~800K-870K ops/s) was in line with pre-change runs; no regression observed.

  * make format-auto applied; debug build is clean.

Reviewed By: anand1976

Differential Revision: D108690137

Pulled By: pdillinger

fbshipit-source-id: 7ffdf4abc665b4c2172b718670c5c2fa1da58500
2026-06-15 19:53:56 -07:00
Josh Kang 5f66923d1e External file ingestion, support fast prepare path from DB generated files (#14854)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14854

External SST ingestion can already skip the open-and-scan metadata path when ingesting files produced by `SstFileWriter`. This extends that fast path to live SST files produced by a DB, so applications that move DB-generated files between DBs can request prepared metadata directly from the source DB and pass it through ingestion.

The API is intentionally limited to live table files whose metadata is already available from the DB and cached table reader. Callers remain responsible for keeping the source file alive and unchanged until ingestion completes.

Reviewed By: xingbowang

Differential Revision: D108440584

fbshipit-source-id: e732278ea194aac71f046b54234238b35c6fe0e5
2026-06-15 17:41:57 -07:00
Josh Kang 7785264e5a Make file opens multi-threaded for commit external sst file ingestion (#14853)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14853

`IngestExternalFileOptions` now exposes `file_opening_threads` so external file ingestion can open table readers for newly committed SSTs in parallel. The requested thread count is carried from each ingestion job into commit and then into manifest `LogAndApply`, where `VersionBuilder::LoadTableHandlers()` uses it while installing the ingested files.

This is intended for workloads that ingest many external SSTs in one call, where serial table-reader opens can dominate commit latency. The ingestexternalfile benchmark now has a flag for this option, and `db_stress` now creates multiple data SSTs per ingest operation so stress runs cover multi-file ingestion, file-info ingestion, and the parallel file-opening path together.

Benchmarks

Ran `db_bench --benchmarks=ingestexternalfile --use_existing_db=0 --num=2200000 --compression_type=none --statistics --use_direct_reads=true --ingest_external_file_batch_size=200 --ingest_external_file_num_batches=1 --ingest_external_file_use_file_info=true --ingest_external_file_fill_cache=false` while varying `--ingest_external_file_file_opening_threads`. The generated SST files were about 256 MiB each.

| `file_opening_threads` | `rocksdb.ingest.external.file.prepare.micros` | `rocksdb.ingest.external.file.run.micros` | File opens |
| 1 | 16,150 us | 327,136 us | 200 |
| 32 | 16,665 us | 38,349 us | 200 |

At the same ~256 MiB SST size, 2,000 files is roughly 500 GiB of SST data. A straight 10x extrapolation of the 200-file ingest histograms puts prepare+run at about 3.43s with `file_opening_threads=1` and 0.55s with `file_opening_threads=32`; run alone is about 3.27s vs. 0.38s.

Reviewed By: pdillinger

Differential Revision: D108347952

fbshipit-source-id: 49efa48524df64cace010b881030f63f771cbb95
2026-06-15 14:45:29 -07:00
Josh Kang ad43a5fbe8 Pass file metadata to IngestExternalFile to improve ingestion latency (#14837)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14837

External file ingestion (`DB::IngestExternalFile[s]`) re-opens every SST file and scans it -- footer, properties, index, filter, and the first/last data blocks -- to recompute the boundary keys, sequence-number bounds, and table properties before committing. For cold, I/O-bound files this scan dominates ingest latency, even when the file is moved/linked rather than copied.

This change lets a caller skip that work when it already has the file's metadata. `SstFileWriter::Finish()` now returns a `PreparedFileInfo` with the file size, table properties, and prepared internal `smallest`/`largest` bounds, and a new `IngestExternalFileArg::file_infos` field carries one `PreparedFileInfo` per file into `IngestExternalFiles()`. When set, ingestion reuses that metadata instead of re-opening and scanning each file. The file is still copied/linked, and the checksum is still verified when `verify_checksums_before_ingest` is set (the fast path opens the file only for that). Point-key and range-deletion bounds are folded into the same prepared bound pair, and user-defined-timestamp files (including the "UDT in Memtables only" format, whose boundary keys carry no timestamp) are handled.

Internally, ingestion-job metadata acquisition was split into `GetIngestedFileInfoFromFileInfo` (reuse caller metadata) and `GetIngestedFileInfoFromFile` (open + scan). Prepared boundary updates use RocksDB comparators, and the timestamp-stripping path pads prepared bounds back to the internal timestamp shape before installing the file.

The `ingestexternalfile` `db_bench` benchmark now also exposes `--ingest_external_file_fill_cache` so ingestion-read block-cache effects can be controlled independently while comparing the file-info fast path against the normal open-and-scan path.

A future PR will support creating `PreparedFileInfo` from a existing DB-generated file, so this optimization is not just limited to SstFileWriter.

## Benchmark Results

Existing `db_bench ingestexternalfile` benchmark, release build, using an XFS flash-backed filesystem. Files are linked (`move_files`) so the measurement isolates the ingest path rather than file-copy throughput. These numbers used direct reads and `fill_cache=false` to avoid warming the block cache while comparing the file-info fast path against the normal open-and-scan path.

Each run used 1M keys/SST, one ingest call, and `db_bench` reported a 62.9 MB estimated file size. The comparison varied the number of files in that ingest call across 10, 30, and 50 files.

| Files/batch | Config | Prepare P50 | Run P50 | Run P50/file | Total ingestion P50 | Total P50 drop |
| --- | --- | --- | --- | --- | --- | --- |
| 10 | Baseline | 10.909 ms | 7.593 ms | 0.759 ms/file | 18.502 ms | -- |
| 10 | `file_info=true` | 1.127 ms | 7.541 ms | 0.754 ms/file | 8.668 ms | 53.2% |
| 30 | Baseline | 29.049 ms | 23.161 ms | 0.772 ms/file | 52.210 ms | -- |
| 30 | `file_info=true` | 2.734 ms | 23.384 ms | 0.779 ms/file | 26.118 ms | 50.0% |
| 50 | Baseline | 49.036 ms | 39.224 ms | 0.784 ms/file | 88.260 ms | -- |
| 50 | `file_info=true` | 4.133 ms | 39.417 ms | 0.788 ms/file | 43.550 ms | 50.7% |

With `fill_cache=false` and direct reads, the metadata fast path cuts `Prepare()` by roughly 90-92%. `Run()` is roughly 0.75-0.79 ms per file across these runs. End-to-end `db_bench` micros/op is almost unchanged because this benchmark includes SST generation and compaction work; use `rocksdb.ingest.external.file.prepare.micros` and `rocksdb.ingest.external.file.run.micros` to isolate ingestion.

Benchmark args: `--benchmarks=ingestexternalfile --use_existing_db=0 --num=1000000 --compression_type=none --statistics --use_direct_reads=true --ingest_external_file_batch_size=<10|30|50> --ingest_external_file_num_batches=1 --ingest_external_file_use_file_info=<false|true> --ingest_external_file_fill_cache=false`.

Reviewed By: xingbowang

Differential Revision: D107721261

fbshipit-source-id: b06ecb7b35f260ec02acf837e7986057bc23cdbf
2026-06-15 11:14:42 -07:00
Mateo Correa f7677f0b6b add rocksdb_set_db_options for dynamic DB option updates (#14615)
Summary:
# Context

The C API lacks a SetDBOptions wrapper, so callers using the C bindings cannot dynamically reconfigure DB-level options at runtime without dropping down to the C++ API.

# Changes
  - Add `rocksdb_set_db_options` to the C API as a wrapper around `DB::SetDBOptions`
  - Adds new tests in `c_test.c` covering both valid and invalid option updates

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14615

Reviewed By: joshkang97

Differential Revision: D101010593

Pulled By: xingbowang

fbshipit-source-id: 3feac94bd0e3b7d6a4913ce9bc014e1233292309
2026-06-15 10:32:13 -07:00
Xingbo Wang c9252b9ac0 Clarify wide column memory ownership in public APIs (#14626)
Summary:
We have seen multiple coding error with the usage of WideColumn API. Hence, improving the documentation to reduce the chance of this happening again.

Clarify that `WideColumn` and `WideColumns` are non-owning views over caller-managed memory.

Update the public `PutEntity()` API documentation to state that column name and value storage must remain valid until the call returns.

Add safe and unsafe usage examples in `wide_columns.h` to make lifetime requirements explicit.

## Testing

Not run. Documentation-only change.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14626

Reviewed By: archang19

Differential Revision: D101280295

Pulled By: xingbowang

fbshipit-source-id: 076103321f1d54103dd1c2772aa9e84affdfd2ad
2026-06-13 07:06:04 -07:00
Josh Kang 4d30cfd1fc Split ingest external files into prepare and commit APIs (#14849)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14849

This change splits ingestion into two public calls: `PrepareFileIngestion()` performs all of the off-mutex work and returns an opaque `FileIngestionHandle`, and `CommitFileIngestionHandle()` makes the prepared files visible under the mutex.

Even though the "Prepare" phase was off the DB mutex, the application layer may still have application logic that depends on the completion of `IngestExternalFile`.

`IngestExternalFiles(args)` is also just `PrepareFileIngestion(args)` followed by `CommitFileIngestionHandle()`, so its behavior is unchanged.

`CommitFileIngestionHandles()` also supports committing multiple handles atomically. However, it is possible that a CF may be present in multiple handles. In this case, we merge the ingestion jobs together. This allows applications to prepare file ingestions at different times, but still provide a single atomic commit.

Reviewed By: xingbowang

Differential Revision: D108225105

fbshipit-source-id: 082e149f13983a4b14a8fd711d2113728485421f
2026-06-12 17:51:36 -07:00
Xingbo Wang 61c3e93a74 Wire external table reads into RocksDB metrics (#14839)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14839

Wrap the filesystem passed to external table readers when statistics or file IO listeners are configured so reads performed by implementations such as Nimble update the same RocksDB metrics as regular SST reads. The wrapper records `SST_READ_MICROS`, activity-specific `FILE_READ_*` histograms, last/non-last-level and temperature read tickers, and forwards `OnFileReadFinish`/`OnIOError` to configured listeners so ZippyDB file-read counters observe external-table IO. `MultiRead` records the elapsed read histogram once and applies byte/listener accounting to each completed request.

Add a table test that makes a dummy external table reader read through `ExternalTableOptions::fs` and verifies both `Statistics` counters/histograms and file-read listener updates. Add an unreleased-history entry for the new external table read metrics coverage. This revision also applies the RocksDB `make check-format` clang-format output for `external_table.cc`.

Reviewed By: pdillinger

Differential Revision: D108012449

fbshipit-source-id: 6b6c993c0aa99479e6e043171124bd48f0416bec
2026-06-12 11:56:42 -07:00
Xingbo Wang 521cd8b31d Add parallel gtest flake reproduction runner (#14827)
Summary:
- Add `tools/gtest_parallel_repro.py` to reproduce gtest flaky tests that depend on process-level CPU contention and scheduler delays.
- Run many fresh gtest processes concurrently, isolate each process with its own `TEST_TMPDIR`, and optionally pin the workload to a smaller CPU set with `taskset`.
- Record per-run logs, write `failures.jsonl`, summarize failure keys, support stopping on first failed batch, and optionally rebuild with `COERCE_CONTEXT_SWITCH=1`.
- Document when to use the runner in `CLAUDE.md` for CI-style flaky tests that single-process `--gtest_repeat` or coerce mode alone does not reproduce.
- Using the new runner, we found and fixed one flaky test and improved c_test harness
  - Flaky test: EnvPosixTest.ReadAsyncQueueFull simulated a full io_uring submission queue by overwriting the SQE pointer after io_uring_get_sqe() had already consumed a submission slot. That left stale state in the thread-local ring, so later AbortIO tests could process an unexpected completion and crash. Add a skip_io_uring_get_sqe syncpoint before the io_uring_get_sqe() call and use it from the test so the Busy path is exercised without mutating the ring.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14827

Test Plan: - CI

Reviewed By: pdillinger

Differential Revision: D107758097

Pulled By: xingbowang

fbshipit-source-id: 3256612ae1ae98b66901593371743b3de7e5bb53
2026-06-12 09:42:58 -07:00
Peter Dillinger 77d9ed7f63 Use _exit(1) instead of exit(1) after DB is open to avoid UAF (#14850)
Summary:
When `FinishInitDb()` or `Open()` calls `exit(1)` after the DB has been opened, background compaction/flush threads are still running. `exit()` triggers static object destruction (including the `KillPoint` singleton and its `rocksdb_kill_exclude_prefixes` vector) while those threads are still accessing them via `TestKillRandom()`, causing a heap-use-after-free detected by ASAN.

This became more likely to trigger after the multi-DB support commit (3d0d60101e7f) which runs `RunStressTestImpl` on worker threads, making the race window larger when one DB fails initialization while other DBs background threads are active.

The fix replaces `exit(1)` in all error paths that fire after the DB has been opened with a wrapper around `_exit(1)`. `_exit()` terminates immediately without running atexit handlers or destroying static objects, avoiding the race with background threads.

Also updated CLAUDE.md to help get cross-platform compatibility right the first time.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14850

Reviewed By: hx235

Differential Revision: D108298839

Pulled By: pdillinger

fbshipit-source-id: 8e87fcb259c273e2be5eb26b4eaf6009f5b998f1
2026-06-11 22:32:11 -07:00
Peter Dillinger 6d4a8144e0 Unify LZ4 and LZ4HC compression levels (#14819)
Summary:
kLZ4Compression and kLZ4HCCompression share the same on-disk format and decompressor, but historically kLZ4Compression only honored negative (acceleration) levels while kLZ4HCCompression only honored positive levels. This unifies them so `compression_opts.level` alone selects the variant: level <= 0 uses LZ4 fast (acceleration = -level) and level >= 1 uses LZ4HC (1..12), regardless of which of the two types is configured.

The configured type now only determines the default compression level (LZ4: acceleration 1, equivalent to level -1; LZ4HC: level 9).

For code simplicity, the recorded per-block type comes from the compression type derived from the level, which could differ from the configured type. To preserve the originally configured choice for debugging/tracking, it is recorded as a `_type=<decimal>` pseudo-option in the rocksdb.compression_options SST table property.

Out-of-range non-default levels are clamped to the nearest effective value (LZ4 acceleration capped at 65537, which also avoids signed overflow negating INT_MIN; LZ4HC level capped at 12). The cost-aware (auto-tune) compressor's LZ4 level grid is changed to negative accelerations so it actually exercises fast LZ4 (positive levels now route to LZ4HC).

Related inclusion: The ZSTD library has a discontinuity at level=0, which maps to level 3, which is more aggressive than levels 1 and 2, which are more aggressive than levels -1, -2, etc. For better friendliness to auto-tuning (etc.), we now map level 0 to be the same as level -1, so that increasing compression level numbers have non-decreasing aggressiveness.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14819

Test Plan:
New unit tests in compression_test.cc:
- UnifiedLZ4LZ4HCLevels: for a representative set of non-default levels, both configured types produce identical output and the same recorded type (selected by the level), levels that clamp to the same effective parameter compress identically, and each round-trips; plus per-type default-level behavior.
- ConfiguredCompressionTypeRecordedInProperties: the `_type=` pseudo- option appears in the SST table property for each configured type.
- ZSTDLevelZeroMapsToMinusOne: level 0 behaves like -1, not like 3.

Reviewed By: joshkang97

Differential Revision: D107536580

Pulled By: pdillinger

fbshipit-source-id: a1281956f70a75d0620cb73d0bfb9ad76c52cca3
2026-06-11 14:10:21 -07:00
Laurynas Biveinis 4026c3cc08 Fix range lock manager crash with reverse comparator (#14831)
Summary:
Fixed a bug where the range lock manager would crash with an assertion failure when using a reverse comparator column family. The CompareDbtEndpoints function used bytewise ordering for length-disparity comparisons, ignoring the user comparator direction.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14831

Test Plan:
- Added unit test RangeLockWithReverseComparator
- Ran make check (4552 tests, 0 failures)
- Verified range_locking_test passes (9/9)

Reviewed By: xingbowang

Differential Revision: D107880089

Pulled By: laurynas-biveinis

fbshipit-source-id: 77f2f2f58197ab5104d4e42f005b0aab1132f1bb
2026-06-11 11:09:10 -07:00
Steph Pontikes e18d41e08c lazily intialize iterators (#14772)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14772

Updated the iterator creation scheme to happen lazily (on request) as oppsed to eagerly. this allows us to prune the iterator tree structure at the time of requesting iterator preparation as opposed to creation, and allows pruning to become an implementation detail. Version now skips non-overlapping SST levels and files before adding children to the iterator tree, returns direct table iterators when a level has a single matching file, and uses pruned LevelIterator instances when multiple files in one non-L0 level match. The overload no longer prepares iterators during creation; callers that need prepared multiscan execution still call Prepare explicitly after construction, and MultiScan does that itself.

Benchmark: ran `db_bench` in opt mode for the base revision and this diff, with `fillseq,compact,levelstats,multiscanrandom`, `--num=1000000`, `--reads=10000000`, single thread, fixed seeds, `--multiscan_use_async_io=false`, and `--use_multiscan=true`. Both A and B had exactly one SST file and no memtable/L0 data (`L0: 0 files`, `L1: 1 file, 61 MB`). `multiscanrandom` creates `MultiScanArgs` and calls `NewMultiScan(...)`, which reaches the new `NewIterator(..., scan_opts)` pruning path in this diff.

```
seed     base A      pruning B    delta
424242   21824.333   17693.333    -18.9%
424243   24042.014   19424.056    -19.2%
424244   22424.974   17636.910    -21.4%
424245   22404.213   18612.840    -16.9%
```

Average: base `22673.9 us/op`, pruning `18341.8 us/op`, about `19.1%` faster.

Reviewed By: xingbowang

Differential Revision: D104904298

fbshipit-source-id: a742106a1d5813fb795a39eeeb35f8cddc02e886
2026-06-10 17:02:47 -07:00
Xingbo Wang 4975341aef Add async I/O release UAF regression coverage (#14844)
Summary:
- Add a controlled async filesystem regression test for released direct I/O async reads, verifying released handles are aborted before a later `Poll()` can complete them.
- Add a MultiScan-style regression test for skipping a pending range remainder before seeking into a later async read.
- Extend db_stress/crashtest coverage so MultiScan can stop early after `--num_iterations` results and randomize `--multiscan_use_async_io` again.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14844

Test Plan: CI

Reviewed By: archang19, anand1976

Differential Revision: D108191370

Pulled By: xingbowang

fbshipit-source-id: 5b56099644cf918ea11071521875d0d059f3a69e
2026-06-10 14:18:40 -07:00
Maciej Szeszko 8d33fe7c70 fix use-after-free: drain background purge in CloseHelper (#14842)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14842

### Problem

A process using RocksDB can hit an ASAN `heap-use-after-free` in `rocksdb::InstrumentedMutex::Lock()` when a `DBImpl` is closed while obsolete-file purge work is still in flight. This can happen during close storms with `avoid_unnecessary_blocking_io=true` on a shared Env, where a queued or late handoff purge can run after `~DBImpl` has destroyed `mutex_`.

### Root Cause

`CloseHelper()` has several mutex-unlocked windows late in shutdown. A dropped column-family handle or SuperVersion cleanup can run in one of those windows and start obsolete-file purge work. `FindObsoleteFiles()` marks that work by incrementing `pending_purge_obsolete_files_`, but `PurgeObsoleteFiles(..., true)` can then spend time outside the DB mutex before transferring the work to `bg_purge_scheduled_` via `SchedulePurge()`. During that handoff, `bg_purge_scheduled_` is still zero even though purge work is pending.

### Fix

Make the final `CloseHelper()` drain wait for both `pending_purge_obsolete_files_` and `bg_purge_scheduled_` before destroying `versions_` / `DBImpl`. This covers both the pending handoff and the scheduled `BGWorkPurge` state. Also move `TEST_VerifyNoObsoleteFilesCached()` and `table_cache_->EraseUnRefEntries()` after the final drain, so debug/ASAN table-cache verification runs only after close-time purge work has settled. The regression test parks a dropped-CF cleanup in the pending handoff window and verifies `CloseHelper()` blocks in the final drain until that handoff is released.

Reviewed By: xingbowang

Differential Revision: D107920881

fbshipit-source-id: f73cd79afe50fc30e0f5d14889dd4285bf350a58
2026-06-10 13:04:36 -07:00
Peter Dillinger 01084e7e8e Disable parallel compression for fast built-in compressors (#14841)
Summary:
Use Compressor::GetRecommendedParallelThreads() overrides to disable parallel compression for Snappy, LZ4 (accelerated, not LZ4HC), and for accelerated levels of ZSTD (level < 0). These do not generally benefit from parallel compression.

Also add --verify_compression option to sst_dump for some basic "recompress" benchmarking with that option.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14841

Test Plan:
unit test updated.

In planning the scope of this change, I manually tested some production SST files with release build sst_dump --command=recompress and various settings for --compression_parallel_threads, --compression_types, and --compression_level, while I had the CPUs mostly busy using cache_bench in the background. Here's an example, before the change to override parallel_threads:

```
$ for PT in 1 8; do /usr/bin/time ./sst_dump --command=recompress --compression_parallel_threads=$PT --block_size=16384 --compression_types=kLZ4Compression --verify_compression=1 test.sst; done
...
Compression: kLZ4Compression          Block Size: 16384  Threads: 1
Cx level: 32767 Cx size:  168501634 Uncx size:  791721664 Ratio:   4.698599 Write usec:    2345894  Read usec:     429022  Cx count:  48661 (100.0%) Not cx for ratio:      0 (  0.0%) Not cx otherwise:      0 (  0.0%)
2.54user 0.29system 0:02.84elapsed 99%CPU (0avgtext+0avgdata 460816maxresident)k
...
Compression: kLZ4Compression          Block Size: 16384  Threads: 8
Cx level: 32767 Cx size:  168501634 Uncx size:  791721664 Ratio:   4.698599 Write usec:    2476459  Read usec:     439464  Cx count:  48661 (100.0%) Not cx for ratio:      0 (  0.0%) Not cx otherwise:      0 (  0.0%)
3.95user 0.33system 0:02.98elapsed 143%CPU (0avgtext+0avgdata 455504maxresident)k
```

Here as in many of the cases I'm changing, it actually takes longer to compress with parallel, despite the added parallel opportunity of verify_compression. And overall CPU is much higher from 2.54 `CPU*s` to 3.95 `CPU*s`.

The difference disappears with the change, because both use single-threaded SST construction.

Reviewed By: joshkang97

Differential Revision: D108075938

Pulled By: pdillinger

fbshipit-source-id: 5d1fc77ddbccf9f3b24a4f0b20b2b3c43074e89d
2026-06-10 12:58:47 -07:00
zaidoon 7affaee1c4 Add use_direct_io_for_compaction_reads option (#14743)
Summary:
Adds a new `DBOption use_direct_io_for_compaction_reads` (default false). When on, compaction-input SST files are opened with `O_DIRECT` so the sequential read-once data from compaction doesn't pollute the OS page cache and evict the hot user-read working set. User reads keep going through the buffered fast path. This protects user-read tail latency on write-heavy workloads without forcing user reads onto the existing global `use_direct_reads` knob (which pays in throughput and P50 — see the bench below).

The interesting bit is that just flipping the FileOptions returned by `FileSystem::OptimizeForCompactionTableRead` doesn't actually trigger `O_DIRECT` at the kernel level. The TableCache (and `FileMetaData::pinned_reader`) is already holding buffered handles opened at flush time or at `DB::Open` via `LoadTableHandlers`. When compaction asks for an iterator, it gets back the cached buffered handle and the kernel never sees the `O_DIRECT` flag.

So this PR also adds a small bypass path:

- `TableCache::FindTable` / `NewIterator` learn a `open_ephemeral_table_reader` mode. When set, the pinned-reader fast path and the shared cache are skipped, `GetTableReader` is called directly with the caller's FileOptions, and ownership of the freshly opened TableReader is handed back via a `unique_ptr`. The iterator takes ownership via `RegisterCleanup` and frees the reader on destruction.
- `VersionSet::MakeInputIterator` and `LevelIterator` plumb the flag through both L0 and L1+ compaction-input paths.
- `CompactionJob::ProcessKeyValueCompaction` turns the bypass on when `use_direct_io_for_compaction_reads` is set, the global `use_direct_reads` is off, and `OptimizeForCompactionTableRead` produced `use_direct_reads=true` in the compaction-read FileOptions.

The option is opt-in: when off, nothing changes for existing users. When on, only the compaction-input opens take the bypass path; user reads keep hitting the TableCache and the buffered fast path normally.

There's also a small db_bench helper in the same PR: a new `--bgwriter_num` flag that lets the writer thread in `readwhilewriting` (and the other "while writing" variants) spread its puts across `[0, bgwriter_num)` instead of `[0, num)`. Without this the readers and writer share a key range and you can't have both a hot read subset and meaningful compaction work — this lets you have both.

### Benchmark

Setup: Ubuntu 24.04 (kernel 7.0.5, OrbStack Linux VM on Apple Silicon), 14 vCPUs, virtio-blk disk, btrfs. MGLRU disabled (`echo 0 > /sys/kernel/mm/lru_gen/enabled`) so the kernel uses the classic active/inactive LRU. 14 GB DB (3.5M keys × 4 KB values), no compression. Each measurement run is pinned to a 1 GB cgroup via `systemd-run --scope -p MemoryMax=1G -p MemorySwapMax=0`. Page cache is dropped between configs. db_bench is Release build.

Workload: `readwhilewriting` for 120s. 4 reader threads doing random reads over a hot key subset, plus 1 writer thread spreading overwrites across the full 3.5M-key keyspace (via `--bgwriter_num=3500000`) throttled at 200 MB/s, so there's continuous compaction running while the readers go.

The size of the hot reader subset relative to available page cache controls how visible the optimization is. The Cassandra blog ([Lightfoot 2026](https://lightfoot.dev/direct-i-o-for-cassandra-compaction-cutting-p99-read-latency-by-5x/)) documented the same thing: biggest wins when the hot set is big enough to actually compete for cache, smaller wins when the hot set trivially fits, neutral when the hot set is way bigger than cache. So I ran two hot-set sizes.

#### Small hot set: ~30 MB (~3% of the 1 GB cgroup) — N=5 iterations, mean (CV)

`--num=7500`. The hot set is small enough that the page cache holds it without much trouble even under compaction, so the wins here are real but on the modest side.

| Config | Throughput (ops/s) | Read P50 (µs) | Read P99 (µs) | Read P99.9 (µs) | Read P99.99 (µs) |
|---|---|---|---|---|---|
| buffered (default) | 233,477 (8.2%) | 16.09 | 82.24 | 721.0 | 2,102.5 |
| direct_compaction_writes_only (existing knob alone) | 287,405 (2.8%) — **+23.1%** | 13.00 (−19.2%) | **66.77 (−18.8%)** | 553.9 (−23.2%) | 1,787.6 (−15.0%) |
| direct_compaction_read_only (new knob alone) | 250,669 (2.4%) — +7.4% | 14.16 (−12.0%) | 102.99 (+25.2%) | 689.8 (−4.3%) | 1,801.3 (−14.3%) |
| direct_compaction_read_write (new + existing, recommended) | 277,920 (3.3%) — **+19.0%** | **12.99 (−19.3%)** | 84.23 (+2.4%) | 613.4 (−14.9%) | **1,738.2 (−17.3%)** |
| use_direct_reads=true (existing global) + write-side | 249,014 (2.5%) — +6.7% | 15.95 (−0.9%) | 68.78 (−16.4%) | **450.8 (−37.5%)** | 1,814.5 (−13.7%) |

CV is 2.4–3.3% on the optimized configs (8.2% on buffered), so the deltas are real. With a hot set this small, the existing `use_direct_io_for_flush_and_compaction` knob is already doing most of the work — the new flag's main extra contribution here is P99.99 (combined wins it by ~2 points vs writes-only-alone). Worth noting: the new flag *alone* (without the existing write-side flag) improves P99.99 but regresses P99 by 25% on this small-hot-set workload, because direct compaction reads lose kernel readahead and compaction-output writes are still hitting the page cache. That regression goes away once you combine with the existing write-side flag, or once the hot set is bigger (see next table). So if you're using just one knob, use the existing one. If you're using this PR's flag, pair it with `use_direct_io_for_flush_and_compaction=true`.

#### Larger hot set: ~400 MB (~40% of cache) — N=5 iterations, mean (CV)

`--num=100000`. This is the case the Cassandra blog calls out — hot set big enough to actually fight compaction for cache. Their analogous setup (1M hot partitions, ~33% hot/cache) reported 1.93× p99 improvement. Numbers here are the headline:

| Config | Throughput (ops/s) | Read P50 (µs) | Read P99 (µs) | Read P99.9 (µs) | Read P99.99 (µs) |
|---|---|---|---|---|---|
| buffered (default) | 68,959 (7.7%) | 44.81 | 541.22 | 2,225.2 | 11,334.5 |
| direct_compaction_writes_only (existing knob alone) | 73,973 (10.3%) — +7.3% | 42.22 (−5.8%) | 456.27 (−15.7%) | 2,016.9 (−9.4%) | 9,190.0 (−18.9%) |
| direct_compaction_read_only (new knob alone) | 84,337 (2.3%) — +22.3% | 38.66 (−13.7%) | 386.97 (−28.5%) | 1,644.8 (−26.1%) | 4,837.9 (−57.3%, 2.34×) |
| direct_compaction_read_write (new + existing, recommended) | **104,923 (8.4%) — +52.2%** | **34.26 (−23.5%)** | **290.97 (−46.2%)** | **1,143.4 (−48.6%)** | **3,080.3 (−72.8%, 3.68×)** |
| use_direct_reads=true (existing global) + write-side | 71,598 (9.1%) — +3.8% | 51.33 (+14.5%) | 297.91 (−45.0%) | 1,663.6 (−25.2%) | 6,530.0 (−42.4%) |

Combined config gets a 3.68× p99.99 win, 1.86× p99, p50 down 23%, throughput up 52%. Same shape as the Cassandra blog's 1.93× p99 result — the improvement just lands at deeper percentiles for us because RocksDB's baseline data path is roughly 40× faster than Cassandra's (their buffered p99 was 35 ms, ours is 0.54 ms), so the cache-miss tail is further out.

A few things worth calling out from this table:

- The new flag is doing real work on top of the existing write-side flag here, not just shifting things around. Combined throughput is +42% over `direct_compaction_writes_only` alone, and combined p99.99 is 3× better. The existing knob alone gives a fairly modest +7% throughput / -19% p99.99 in this case — there's a clear gap that the new flag fills.
- The new flag *alone* (no existing write-side flag) is also a real improvement here: +22% throughput, p99.99 down 57%. The P99 regression we saw in the small-hot-set case is gone, because the cache-protection effect now dominates the lost-readahead cost.
- `use_direct_reads=true` (the existing global flag) actually regresses P50 by 14.5% in this workload — taking user reads off the page cache hurts you when the hot data could have been cached. It also gets the worst throughput of any direct config. It's not an equivalent way to get these gains.

### `compaction_readahead_size` matters when this flag is on

Direct I/O bypasses kernel readahead, so RocksDB's own `DBOptions::compaction_readahead_size` becomes the only prefetch the iterator has. The default of 2 MB is enough and real users will get it automatically. **But `db_bench`'s `--compaction_readahead_size` CLI default is 0**, which defeats prefetch and makes direct compaction look slower than it actually is. If you're reproducing the numbers above, pass `--compaction_readahead_size=2097152` (or larger).

- Recommended production config is `use_direct_io_for_compaction_reads=true` + `use_direct_io_for_flush_and_compaction=true`. Strongest configuration at every percentile and throughput in both benches.
- The new flag is the read-side counterpart to `use_direct_io_for_flush_and_compaction`, which handles compaction-write cache pollution. They address different sources of pollution and compose. The gap between "combined" and "writes-only-alone" is 17 percentage points on p99.99 in the small-hot-set bench and 54 points in the larger one, so the new flag is contributing real value, especially as the hot set grows.
- The new flag alone is also a real improvement when the hot set is big enough to compete with cache (+22% throughput, 2.34× p99.99 in the larger-hot-set bench). On a very small hot set it improves p99.99 but regresses p99, so pairing with the existing write-side flag is safer.
- The benefit is workload-dependent. Small hot sets get modest tail-latency wins. Hot sets sized to actually compete for cache get the big multi-percentile wins shown above. Hot sets bigger than cache (not benched here but covered in the Cassandra blog) see no change either way — every read misses regardless.

### Reproducing

Any Linux host (or a Linux VM on macOS via OrbStack / Multipass / lima):

```bash
sudo apt-get install -y build-essential clang cmake git pkg-config \
  libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev

cmake -DCMAKE_BUILD_TYPE=Release -DPORTABLE=1 -DWITH_GFLAGS=1 -DWITH_TESTS=0 ..
make -j db_bench

echo 0 | sudo tee /sys/kernel/mm/lru_gen/enabled
```

Build the source DB once, unrestricted memory:

```bash
./db_bench --benchmarks=fillrandom,compact,waitforcompaction,stats \
  --db=/path/to/source_db --num=3500000 --key_size=16 --value_size=4096 \
  --write_buffer_size=16777216 --target_file_size_base=16777216 \
  --max_background_jobs=4 --compression_type=none --cache_size=4194304 \
  --max_bytes_for_level_base=67108864 --disable_wal=1 --sync=0
```

For each config, copy `source_db -> scratch_db`, run `sync && echo 3 > /proc/sys/vm/drop_caches`, then:

```bash
sudo systemd-run --scope -p MemoryMax=1G -p MemorySwapMax=0 \
  ./db_bench --use_existing_db=1 \
    --benchmarks=readwhilewriting,stats --db=/path/to/scratch_db \
    --threads=5 --duration=120 --statistics=true --histogram=1 \
    --num=7500 --bgwriter_num=3500000 \
    --key_size=16 --value_size=4096 \
    --write_buffer_size=16777216 --target_file_size_base=16777216 \
    --max_background_jobs=4 --compression_type=none \
    --cache_size=4194304 --open_files=200 \
    --skip_stats_update_on_db_open=true \
    --max_bytes_for_level_base=67108864 \
    --benchmark_write_rate_limit=209715200 \
    --compaction_readahead_size=2097152 \
    --rate_limiter_bytes_per_sec=0 \
    --use_direct_reads={true|false} \
    --use_direct_io_for_compaction_reads={true|false} \
    --use_direct_io_for_flush_and_compaction={true|false}
```

For the larger hot-set table, change `--num=7500` to `--num=100000`.

The five configs in the tables:
- `buffered`: all three flags false.
- `direct_compaction_writes_only`: `use_direct_io_for_flush_and_compaction=true`, the other two false. This is what users have today without this PR.
- `direct_compaction_read_only`: `use_direct_io_for_compaction_reads=true`, the other two false.
- `direct_compaction_read_write`: `use_direct_io_for_compaction_reads=true`, `use_direct_io_for_flush_and_compaction=true`, `use_direct_reads=false`. **Recommended.**
- `direct_all`: `use_direct_reads=true`, `use_direct_io_for_flush_and_compaction=true`, `use_direct_io_for_compaction_reads=false`.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14743

Reviewed By: pdillinger

Differential Revision: D108017601

Pulled By: xingbowang

fbshipit-source-id: 4039d490d7e77b476db7a477a2f3d24738db6336
2026-06-09 17:02:53 -07:00
Anand Ananthabhotla 828f6d189e Mark remote filtered input counts as inaccurate (#14838)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14838

Remote compaction can pre-filter whole input files when universal compaction sees standalone range tombstones. In that case the worker's iterator count no longer describes the full original input set, so returning `has_accurate_num_input_records=true` can trigger a false-positive input-record verification on the primary. Mark the remote count inaccurate whenever the reconstructed compaction filtered input files before iteration, and add a regression test covering that path.

Reviewed By: jaykorean

Differential Revision: D107961493

fbshipit-source-id: 0c302081964ab1d894b34bddd2c55f5a4f06752f
2026-06-09 13:38:51 -07:00
Josh Kang 1ede57e5b0 Add file ingestion histograms (#14836)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14836

Adds a public `Statistics` histogram, `INGEST_EXTERNAL_FILE_TIME` (`"rocksdb.ingest.external.file.micros"`), recording the end-to-end latency in microseconds of each `IngestExternalFile(s)` call. Ingestion timing was previously only available through per-thread `perf_context` counters, which require setting a `PerfLevel` and are not aggregated, so there was no process-wide latency distribution (p50/p99/max) for dashboards.

It is recorded with an RAII `StopWatch` at the top of `DBImpl::IngestExternalFiles` -- one sample per call (not per column family), covering all return paths. It is null-safe and self-gating on the stats level, so there is no cost when statistics are off, and ingestion is not a hot path. Java bindings are kept in sync per the `statistics.h` requirement; the C API needs no change.

Reviewed By: xingbowang

Differential Revision: D107721260

fbshipit-source-id: 0705f9e0f7392329a7bcbdbd9f3afd34594d20bb
2026-06-08 18:13:38 -07:00
Xingbo Wang 82085868e2 Add read-scoped block buffers for scan reads (#14806)
Summary:
Add read-scoped block buffers for scan reads. Introduce an experimental read-scoped block buffer provider API, configured through ReadOptions::read_scoped_block_buffer_provider, so supported block-based table iterator scans and MultiScan data-block reads can use caller-provided read-scoped storage for final data-block contents.

When configured, supported provider-backed scan data-block reads bypass the data-block cache while preserving normal index/filter block-cache behavior. Known-uncompressed reads can attach provider cleanup to provider-backed read buffers without copying. Compressed reads decompress directly into provider-backed output, while maybe-compressed reads that turn out to be uncompressed copy once into provider-backed final contents. mmap reads ignore the provider.

Extend AlignedBuffer and RandomAccessFileReader direct-I/O paths to support external aligned allocations, then use that support for read-scoped iterator, async I/O, and MultiRead scratch buffers. Centralize read-scoped I/O policy, keep coalesced async reads safe when blocks are released before completion, and validate provider lease contracts.

Add focused coverage for read-scoped ownership, compressed and uncompressed blocks, direct I/O, data-block cache bypass behavior, invalid provider leases, async release handling, and stress-test provider invariants. Add public API release notes for read-scoped block buffers.

Bonus change: Fixed a flaky test in ReserveThread

## Testing

- CI

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14806

Reviewed By: anand1976

Differential Revision: D106999951

Pulled By: xingbowang

fbshipit-source-id: b1f23d4bab6318b6373ba2ca99a5c4d6a842dc5a
2026-06-08 14:26:46 -07:00
Raj Suvariya 0493154a73 Expose BackupEngine C API additions: StopBackup and rate limiters (#14722)
Summary:
Extends RocksDB's C API with two `BackupEngine` capabilities needed by
language bindings (e.g. Rust via librocksdb-sys) that consume the C API:

- **StopBackup**: Add `rocksdb_backup_engine_stop_backup()` to allow cancelling
  an in-progress backup.

- **Rate limiters**: Add
  `rocksdb_backup_engine_options_set_backup_rate_limiter()` and
  `rocksdb_backup_engine_options_set_restore_rate_limiter()` to expose the
  `shared_ptr<RateLimiter>` fields on `BackupEngineOptions`. The existing
  `uint64_t` setters only throttle writes; these expose the richer `RateLimiter`
  object that supports read+write throttling (e.g. `kAllIo` mode).

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14722

Test Plan:
- [x] New tests in `db/c_test.c` cover `StopBackup` and rate limiter
  setter/getter roundtrips, plus opening a real backup engine with rate
  limiters set and running a backup end-to-end
- [x] `make check` passes with no regressions

Reviewed By: pdillinger

Differential Revision: D107654882

Pulled By: xingbowang

fbshipit-source-id: f50c3989779e6a099113fec203231d47b9480cb9
2026-06-08 11:52:53 -07:00
anand76 a214d3f1f8 Update version to 11.5.0 for 11.4 release (#14820)
Summary:
- Bump version.h from 11.4.0 to 11.5.0
- Add `11.4.fb` to `check_format_compatible.sh`
- Cherry-pick HISTORY.md from `11.4.fb`
- Delete `unreleased_history/` note files

Part of 11.4 release workflow.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14820

Reviewed By: joshkang97

Differential Revision: D107681545

Pulled By: anand1976

fbshipit-source-id: ce34d01f3e7fb336cc7ebf3450ccbe97a8628eca
2026-06-05 23:09:42 -07:00
Erin Gao 2b9e8dc925 Create a C shim for setting TransactionDB write policy (#14810)
Summary:
`TransactionDB` supports different write policies (e.g. `WritePrepared`), but this functionality is not currently accessible via the C API. Creating a C shim to expose the functionality for setting the write policy.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14810

Reviewed By: pdillinger

Differential Revision: D107654915

Pulled By: xingbowang

fbshipit-source-id: b0d915a3420057de5236fe9f6cb47d291294788c
2026-06-05 16:26:37 -07:00
Xingbo Wang 8c3bd2c3e9 Fix stale no-reopen tracking in fault injection FS (#14822)
Summary:
PR https://github.com/facebook/rocksdb/issues/14585 added FileOpenContract enforcement to FaultInjectionTestFS. The no-reopen-for-write check recorded contracts by path but did not clear a stale contract after the file was deleted. A later SST create that reused the same path could be rejected as a forbidden reopen, causing DBWALTest.WALWithChecksumHandoff to fail with "NewWritableFile violates no-reopen-for-write contract". If the ASSERT exited the test early, the local Env stack object could also be destroyed before DB fixture teardown closed the DB, producing the follow-on TSAN heap-use-after-free.

When opening a file for write, drop stale no-reopen tracking if the target file no longer exists, while still rejecting writes through reopened handles. Also close the DB before the WAL test's local Env unwinds on assertion failure.

A separate TSAN report in DBBlobBasicIOErrorTest.GetEntityMergeWithBlobBaseIOError exposed unsynchronized access to FaultInjectionTestEnv::error_: SetFilesystemActive() updated the stored error under mutex_, while background purge work read it through GetError() without that mutex. Copy injected errors under the same mutex and read the no-space state in GetFreeSpace() while protected. Apply the same synchronization to FaultInjectionTestFS.

Bonus fix: Fix another TSAN data race in FaultInjectionTestEnv GetError not synchronized under mutex_

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14822

Test Plan: CI

Reviewed By: mszeszko-meta

Differential Revision: D107689662

Pulled By: xingbowang

fbshipit-source-id: 6d4f8fdc8b898d3ddcad7816e385f3b7f20c4727
2026-06-05 15:17:12 -07:00
Xingbo Wang 76b8cce2f7 Fix spelling of memtable_verify_per_key_checksum_on_seek (#14811)
Summary:
- Rename the misspelled `memtable_veirfy_per_key_checksum_on_seek` option and related flags/config keys to `memtable_verify_per_key_checksum_on_seek`.
- Update memtable option plumbing, options serialization/logging, db_bench/db_stress/crash-test flags, tests, and the option-addition guide to use the corrected name.
- Keep the checksum-on-seek behavior unchanged.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14811

Test Plan: CI

Reviewed By: pdillinger

Differential Revision: D107382276

Pulled By: xingbowang

fbshipit-source-id: 7621dc718d61503b982a7e3f65cc9293a1ad085b
2026-06-05 10:18:47 -07:00
Xingbo Wang 10e4f4745e Add contract boundary guidance to CLAUDE.md (#14821)
Summary:
- Add a contract-boundaries section to the RocksDB code review checklist in `CLAUDE.md`.
- Document common review prompts for keeping caller-specific policy out of reusable lower layers.
- Add "Contract Boundary Leaks" to the common review feedback patterns.

## Testing
CI

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14821

Reviewed By: pdillinger

Differential Revision: D107652555

Pulled By: xingbowang

fbshipit-source-id: 383bd78fec6e3ecc251c752d30f9de3ea44a10de
2026-06-05 10:02:13 -07:00
Xingbo Wang 2d68f30425 Support remote file system for blob direct write file (#14585)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14585

Adds typed file-open contracts and open-file size handling needed for blob direct-write files on remote or SHM-backed file systems.

This diff:

- Adds typed `FileOpenContract` semantics to RocksDB `FileOptions`, including `kNoReopenForWrite`, `kNoReadersWhileOpenForWrite`, bitwise helpers, and constructor propagation from `EnvOptions` to `FileOptions`.
- Marks SST/table output creation paths with both `kNoReopenForWrite` and `kNoReadersWhileOpenForWrite`, including builder output, compaction output, and external `SstFileWriter` output.
- Marks blob write paths with the appropriate contracts: regular blob file builders use both `kNoReopenForWrite` and `kNoReadersWhileOpenForWrite`, while blob direct-write partition output uses `kNoReopenForWrite` so active readers can still observe SHM-backed data through the read path.
- Adds `GetFileSizeFromOpenFileOrPath` so readers can prefer an already-open `FSRandomAccessFile::GetFileSize` result and fall back to path-level `FileSystem::GetFileSize` according to the caller's fallback policy.
- Updates blob file reader and table footer reader size checks to use the new open-file-or-path size helper, preserving malformed-file validation while supporting file systems where path-level size is not the only valid source.
- Extends `FaultInjectionTestFS` to track file-open contracts, reject readers while a no-readers writer is open, reject reopened data writes for `kNoReopenForWrite`, allow `SyncFile` to use a reopened handle, and preserve contract state across create, reopen, reuse, rename, link, reset, and delete paths.
- Keeps `IOStatus` return paths simple by returning status locals directly in the file-size helper and fault-injection contract validation paths.
- Adds clang-format-compliant tests covering file-open contract enforcement, `SyncFile` being allowed while reopened data writes are rejected, blob direct-write behavior, blob reader behavior, and the new file-size fallback helper behavior.
- Leaves WAL and MANIFEST contract assignment for follow-up work because live WAL/MANIFEST readers and `reuse_manifest_on_open` need separate handling before RocksDB can safely claim stronger file-open contracts for those file classes.

Reviewed By: anand1976, pdillinger

Differential Revision: D100002686

fbshipit-source-id: 26b4021bc13333ede1077f0ff8cbd71335c5f294
2026-06-05 04:59:40 -07:00
Xingbo Wang b5922eab11 Add FileSystem SyncFile API (#14739)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14739

Adds public path-level file sync APIs to RocksDB so callers can ask `Env` or `FileSystem` to sync a named file without hand-rolled reopen logic.

This diff:

- Adds public `Env::SyncFile` and `FileSystem::SyncFile` APIs for syncing or fsyncing a file by name without requiring callers to reopen it directly.
- Documents the `SyncFile` contract: filesystems may override it as a no-op when flush/close already provides durability, and RocksDB callers should use it instead of hand-rolled `ReopenWritableFile()+Sync()/Fsync()` so filesystems can reject post-close data-write reopen paths.
- Implements the default `Env` and `FileSystem` behavior by reopening the file as writable, calling `Sync` or `Fsync`, closing the handle, returning the sync/fsync error ahead of a close error when both fail, and explicitly consuming the close status on the sync-failure path.
- Wires `SyncFile` through the Env/FileSystem bridge and wrapper layers, including `EnvWrapper`, `FileSystemWrapper`, `CompositeEnvWrapper`, `EncryptedFileSystem`, `RemapFileSystem`, read-only file systems, mock file systems, and fault-injection wrappers.
- Keeps fault-injection `SyncFile` overrides on the base default implementation so the operation still exercises the wrapper's `ReopenWritableFile`, wrapped-file `Sync`/`Fsync`, and `Close` hooks instead of bypassing them through target forwarding.
- Updates external SST ingestion to call `FileSystem::SyncFile` instead of manually reopening an ingested file as writable, while preserving the `NotSupported` skip behavior and failure logging.
- Adds release-note coverage for the new public API and unit coverage for default `Env`/`FileSystem` success, reopen failure, close failure, sync-versus-close failure precedence, and external SST sync behavior.

There was an earlier version of this API which got reverted (PR #13987) due to internal change broken. Re-apply the change. The internal issue will be resolved in next release.

Reviewed By: pdillinger

Differential Revision: D104918547

fbshipit-source-id: 3f8d2d127fc962b68b423bbb90de551fe6706224
2026-06-05 04:41:42 -07:00
Peter Dillinger 8053b9414f Change default compression from Snappy to LZ4 (#14818)
Summary:
The historical default block compression `kSnappyCompression` dates to when Snappy was the obvious fast/cheap choice. On modern server CPUs LZ4 matches or beats Snappy on compression ratio while decompressing substantially cheaper, so it is a better default. This changes the default `ColumnFamilyOptions::compression` to `kLZ4Compression`, with a runtime fallback of LZ4 -> Snappy -> none depending on what is compiled into the binary (new `GetDefaultCompressionType()` in util/compression.h). Only column families that do not explicitly set `compression` are affected (including compaction output when
`CompactionOptions::compression == kDisableCompressionOption`), and only newly written SST files; existing data is read as before. Doc comments, the Java bindings, and the sorted_run_builder example are updated to describe the new default.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14818

Test Plan:
Adjusted two unit tests that implicitly depended on the old Snappy default to pin `compression = kSnappyCompression`: db_iterator_test's ReadAhead (readahead byte thresholds assume Snappy-sized files) and compaction_service_test's CustomFileChecksum (LSM shape after auto-compaction determined whether a manual CompactRange had work to do).

This change is primarily validated by performance testing. Added a `compressreject` db_bench benchmark (output buffer sized just below the predicted compressed size) to measure the cost of attempting then declining compression, alongside `compress`/`uncompress`. Both db_bench and sst_dump compress/decompress a modest block at a time, as in a real workload.

sst_dump on SST files from production workloads (4 files, 16KB blocks, single thread) on recent server-class AMD, Intel, and ARM CPUs, LZ4 vs Snappy:
  - Compression ratio: comparable; LZ4 is slightly smaller on the more compressible files (up to ~8%) and within ~2% on the rest.
  - Compression (write) CPU: a wash, within ~2% either direction.
  - Decompression (read) CPU: the clear win -- Snappy costs ~1.2x-1.5x as much as LZ4, i.e. LZ4 saves ~25-30% read CPU, consistently across AMD, Intel, and ARM.

db_bench synthetic workload (100-byte values), at 1 / 12 / 160 threads, LZ4 vs Snappy:
  - Compression throughput: LZ4 ~10-30% higher.
  - Decompression throughput: LZ4 much higher, and the advantage grows with core count -- from ~+12% at 12 threads to ~+45-50% at 160 threads, i.e. better multi-core scaling.
  - Rejection (insufficient ratio) path: comparable; LZ4 ~15-18% faster on compressible-but-rejected blocks and Snappy within ~10% on barely-compressible blocks. No meaningful regression, confirming incompressible data is still efficiently detected and stored raw.

Reviewed By: xingbowang

Differential Revision: D107536490

Pulled By: pdillinger

fbshipit-source-id: f8abaee630d782674778338148e36ed0c84e3661
2026-06-04 10:48:27 -07:00
Hui Xiao 5177a8e6c2 Add remote compaction format compatibility test to check_format_compatible.sh (#14798)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14798

Add cross-version format compatibility testing for remote compaction to `check_format_compatible.sh` as primary and worker RocksDB instances can run different versions.

Two new ldb commands coordinate via local files:
- `remote_compaction_primary`: opens an existing DB and runs `CompactRange()` through `LocalFileCompactionService`, which writes `input.bin` via `Schedule()` and polls for `result.bin` via `Wait()`.
- `remote_compaction_worker`: polls for `input.bin`, calls `OpenAndCompact()`, writes `result.bin`.

The test script creates a DB using `generate_random_db.sh` with the primary's ldb binary (new optional 3rd argument) so the OPTIONS file matches the primary's version. An overlap key is written to ensure `CompactRange` triggers a real compaction (not a trivial move). For each old ref in `db_forward_with_options_refs`, the script tests both directions -- current primary + old worker and old primary + current worker -- to catch wire-format incompatibilities in `CompactionServiceInput`/`CompactionServiceResult`. Old refs lacking the commands are skipped gracefully.

Reviewed By: pdillinger

Differential Revision: D106321150

fbshipit-source-id: e0341b57c1b12e1fa5296609f0463f77484c1a6e
2026-06-03 14:03:47 -07:00
Peter Dillinger 3883a8d05e Rename db_test2 -> db_etc2_test (#14218)
Summary:
When searching/grepping through unit tests, it's convenient to use test.cc suffix to match all unit tests, or to exclude test.cc when excluding unit tests from a code search. (I've even seen my AI assistant grep through `*test.cc`.) So I'm renaming db_test2.cc (as planned in https://github.com/facebook/rocksdb/issues/14076)

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14218

Test Plan: existing tests + CI

Reviewed By: xingbowang

Differential Revision: D90140624

Pulled By: pdillinger

fbshipit-source-id: 78aa099290670bbb4093b2c7c02bc47ab3bf5f8e
2026-06-02 16:18:05 -07:00
Josh Kang 768de6e50d Fix false-positive corruption error with remote compaction (#14808)
Summary:
Fix a false-positive compaction corruption error in the remote compaction path. Remote workers can mark `CompactionJobStats::num_input_records` as unreliable when input iteration uses seek/skip behavior, but the remote result serialization was dropping `has_accurate_num_input_records`. The primary then deserialized the flag as its default `true`, trusted an unreliable zero input-record count, and raised `Compaction number of input keys does not match number of keys processed`.

This change serializes the accuracy flag with the rest of `CompactionJobStats` so the primary preserves the remote worker's "do not verify this count" signal. It also adds a targeted regression test and a release note for the bug fix.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14808

Test Plan: New regression test that triggers corruption error without the fix by modifying `has_accurate_num_input_records=false`

Reviewed By: xingbowang

Differential Revision: D107128357

Pulled By: joshkang97

fbshipit-source-id: 3243c9c2ab18534652737f7410fe3c51724db549
2026-06-02 11:13:54 -07:00
Peter Dillinger 62f05627be Reduce manifest rotation for foreground metadata ops (#14797)
Summary:
Async WAL precreation in https://github.com/facebook/rocksdb/pull/14738 / D105020559 was motivated by slow file creation time on remote storage. MANIFEST does not need the same precreation treatment as WAL because most MANIFEST writes come from background flush and compaction work, but user-facing metadata operations can still pay MANIFEST rotation file creation latency inline. File ingestion performance is a particular concern to some Meta users.

Relax the effective MANIFEST rotation limit by 25% for MANIFEST write batches containing any foreground VersionEdit, while keeping background-only flush/compaction batches on the configured or auto-tuned limit. This covers column family manipulation, external file ingestion and import, and DeleteFilesInRange(s). SetOptions remains expected to avoid MANIFEST writes; the test keeps a regression guard for that behavior.

The relaxation is intentionally bounded. It reduces the chance that foreground metadata operations create a new MANIFEST inline, while still allowing foreground operations to rotate once the current MANIFEST is beyond the relaxed threshold. Heavier blocking operations like manual Flush or CompactRange already trigger additional file creation and do not get this treatment here, though that could be reconsidered later.

This should reduce a potential latency hazard of manifest file size auto-tuning: more frequent MANIFEST rotations. With this change, rotation latency is shifted toward background-only MANIFEST batches when possible.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14797

Test Plan:
Expanded DBEtc3Test.AutoTuneManifestSize to cover the foreground threshold behavior and the original auto-tuning behavior in separate phases:
- verifies foreground-only CreateColumnFamily writes get only bounded 25% headroom by asserting the first four large-CF additions do not rotate and the fifth does;
- verifies auto-tuned background thresholds still prevent excessive rotation;
- verifies foreground operations stay below the relaxed threshold for CreateColumnFamily, IngestExternalFile, CreateColumnFamilyWithImport, and DeleteFilesInRanges;
- verifies SetOptions still does not write to MANIFEST;
- verifies a following background flush still rotates at the normal threshold;
- preserves the persisted compacted manifest size close/reopen coverage.

Reviewed By: xingbowang

Differential Revision: D106578771

Pulled By: pdillinger

fbshipit-source-id: f8e274032cd9e7f50e95b685c949242f95351498
2026-06-01 18:13:29 -07:00
Peter Dillinger 9ef369c606 Make StringToMap entries self-contained for direct round-trip (#14805)
Summary:
StringToMap previously stripped the outer braces from nested values,
making single-entry round-trip via `key=value;` (e.g. SetOptions)
silently corrupt values that contain ';'. For example, a filter_policy
with sub-options serialized as
  filter_policy={id=ribbonfilter:10:-1;bloom_before_level=-1;}
parsed to map[filter_policy] = "id=ribbonfilter:10:-1;bloom_before_level=-1;",
and embedding that map entry directly into another `key=value;` string
re-exposed the inner ';' as a top-level delimiter.

This change is a step toward the "essential view": the outer `{...}` of
a nested value is part of the value's identity. StringToMap preserves it, so
each map entry is in self-contained form (a simple value or a single
balanced `{...}` block) and can be embedded directly without further
escaping by the caller. However some permissiveness is kept for
compatibility: list-style typed parsers (ParseVector, ParseArray,
kStringMap, the listener parser) and scalar dispatch in
OptionTypeInfo::Parse accept either the bare or the wrapped form via a
new OptionTypeInfo::StripOuterBraces helper that peels at most one
outer-pair-matched layer. So braced scalar input like `key={42};` and
`key={true};` still parses, and `key={};` denotes an empty list,
distinct from `key={{}};` which denotes a one-element list with an
empty element.

A new public MapToString is added to convenience.h as the symmetric
inverse of StringToMap: a trivial `key=value;` joiner that relies on
each value already being self-contained (which StringToMap guarantees).

Also fixed and refactored our trim() function because it would do the wrong
thing for a single space. This problem was detected by expanding the
StringToMapRandomTest based on code review feedback, and should only
improve existing callers to trim().

The OPTIONS-file serializer code is untouched, so the on-disk format
is byte-for-byte identical and format-compatibility is preserved.

Bonus: fixes / clarifications to CLAUDE.md's build-system note.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14805

Test Plan:
db_bloom_filter_test extends MutableFilterPolicy with the OPTIONS-file
format for filter_policy to confirm that form round-trips through
SetOptions.

options_test updates StringToMapTest expectations for the new
brace-preserving behavior, and adds:
- MapToStringTest covering the new joiner.
- StringToMapMapToStringRoundTripTest including a single-entry
  pull-and-embed scenario that previously corrupted on round-trip.
- FullOptionsStringToMapRoundTripTest that drives a populated
  DBOptions and CFOptions through GetStringFromXxx -> StringToMap ->
  per-entry single round-trip -> MapToString -> GetXxxFromString ->
  VerifyXxxOptions, catching any custom serializer that emits a
  value with ';' without enclosing it in '{}'.
- EmptyBracedVectorAndBracedScalarTest covering `key={};` (empty
  list) and braced scalar input like `key={42};`, `key={true};`.
- Expanded StringToMapRandomTest with round-tripping.

Some explicit trim() tests added to string_util_test.cc

Format compatibility verified with
  SHORT_TEST=1 tools/check_format_compatible.sh

Other existing tests in options_test, customizable_test, options_settable_test,
listener_test, options_file_test, options_util_test, db_options_test,
and compaction_service_test have extensive coverage of serialization /
deserialization logic.

Reviewed By: hx235, xingbowang

Differential Revision: D106855466

Pulled By: pdillinger

fbshipit-source-id: 872aac8d819c7b90c807b92bab85569b48fa2aa0
2026-06-01 17:07:11 -07:00
Xingbo Wang 30dba7f41a Fix compaction abort rescheduling before queue pick (#14800)
Summary:
- AbortAllCompactions can race with an already-scheduled automatic compaction before the background worker calls PickCompactionFromQueue(). MaybeScheduleFlushOrCompaction() has already consumed one unscheduled_compactions_ credit when it scheduled the worker, but the CF is still present in compaction_queue_ with queued_for_compaction=true. If the worker returns kCompactionAborted before popping the CF, ResumeAllCompactions() can see unscheduled_compactions_ == 0 and fail to schedule the still-queued work, leaving compaction permanently stalled until DB restart.

- Restore the unscheduled compaction credit in the non-prepicked abort path, matching the existing BG-work-stopped handling for the same scheduled-before-pick state. Prepicked/manual compactions are not adjusted because they do not represent an unpopped automatic compaction_queue_ entry whose scheduling credit was consumed.

- Add AbortScheduledAutomaticCompactionBeforePick to deterministically reproduce the lost-credit race with a sync point at BackgroundCallCompaction:0. The test verifies that ResumeAllCompactions() schedules the still-queued automatic compaction by checking COMPACT_WRITE_BYTES advances and L0 file count drops.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14800

Test Plan: - Without the implementation fix, `DBCompactionAbortTest.AbortScheduledAutomaticCompactionBeforePick` fails because `COMPACT_WRITE_BYTES` remains unchanged after `ResumeAllCompactions()`.

Reviewed By: pdillinger

Differential Revision: D106684155

Pulled By: xingbowang

fbshipit-source-id: 6dacea473ef481eb3122eb15e296e5b69635413f
2026-06-01 16:11:22 -07:00
Xingbo Wang 023fbb074a Optimize MultiScan dispatch for sorted blocks (#14783)
Summary:
- Propagate validated, sorted MultiScan range state from `DBIter::Prepare()` through `MultiScanArgs`.
- Mark block-based table IO jobs as already sorted when the public MultiScan ranges have been validated.
- Keep `IODispatcher::SubmitJob()` as the normalization boundary for unsorted callers, while allowing sorted callers to skip the defensive block-handle sort.
- Update private dispatcher coalescing helpers to consume sorted block indices and add debug assertions for that precondition.

## Testing
CI, new unit test

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14783

Reviewed By: anand1976

Differential Revision: D106301516

Pulled By: xingbowang

fbshipit-source-id: 99b7ffcaecbf27cb79f15feb4af8680ff1e422d9
2026-06-01 15:36:38 -07:00
Josh Kang 26a501b5d1 Fix tiered compaction incorrectly moving range tombstones upwards (#14795)
Summary:
Fixes an off-by-one bug in how sequence numbers are handled when splitting range tombstones across output levels in per-key-placement (tiered) compaction. The bug was either introduced or propagated in https://github.com/facebook/rocksdb/issues/13256.

Point entries move to proximal output only when their sequence number is strictly greater than `proximal_after_seqno_`, but range tombstones were previously split using an inclusive lower bound at that same seqno. That allowed a range tombstone at the boundary to be emitted to the proximal level while point keys at the same seqno stayed in the last level, which could create overlapping files in the proximal level (caught by `force_consistency_checks` as `L<n> has overlapping ranges`).

This matters when `proximal_output_range_type_` is `kNonLastRange`: the compaction only owns the selected proximal-level input range, so existing last-level data at the split boundary must stay in the last level. In `kFullRange`, the compaction owns the relevant proximal-level range, so newer last-level data can be safely emitted to proximal output.

The fix splits range tombstones at `proximal_after_seqno_ + 1` (saturating at `kMaxSequenceNumber`), so the half-open `[lower, upper)` tombstone filter lands on the same boundary as the strict `seqno > proximal_after_seqno_` rule used for point keys.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14795

Test Plan:
New regression test `PrecludeLastLevelTestBase.RangeDelAtProximalSeqnoBoundaryStaysInLastLevel` uses a targeted manual compaction to exercise the `kNonLastRange` case directly, verifying a boundary range tombstone stays in the last level instead of widening the proximal output range.

Worked example:

```text
Initial state:

  L6 (last level):   Put(Key 2)s1,  Put(Key 12)s2,  RangeDel[Key 2, Key 12)s3
  L5 (proximal):     file A [Key 0 .. Key 4]s4-s5     file B [Key 5 .. Key 9]s6-s7

  preclude_last_level_min_seqno is forced to 0 via sync point.

Manual CompactFiles selects only L5 file B + the L6 file (output level 6).
Only part of the proximal level is selected, so this is the kNonLastRange case:

  max_last_level_seqno  = 3
  proximal_after_seqno_ = max(0, 3) = 3

  Point keys Key 5 (s6) and Key 9 (s7): both seqno > 3 -> proximal output (L5)   OK

  Range tombstone s3:
    Old (buggy):  proximal keep range [3, MAX) includes s3, so the tombstone is
                  emitted to the proximal output. The output is built from L5 input
                  file B [Key 5 .. Key 9], but the tombstone covers [Key 2, Key 12),
                  so the proximal output file starts at Key 2 and spills past Key 5
                  into the existing, untouched L5 file A [Key 0 .. Key 4].
                  Two overlapping files in L5 -> Corruption.

    New (fixed):  split at proximal_after_seqno_ + 1 = 4.
                  proximal keep [4, MAX) excludes s3; last-level keep [0, 4) includes
                  s3 -> tombstone stays in the last level (L6).   OK
```

Verification (debug build, `make -j64 tiered_compaction_test`):

- Without the fix, the test fails at the `CompactFiles` call:
  ```
  tiered_compaction_test.cc:2940: Failure
  Corruption: force_consistency_checks(DEBUG): VersionBuilder: L5 has overlapping ranges:
    file https://github.com/facebook/rocksdb/issues/11 largest key: Key(4) seq:5, type:1 (Put) vs.
    file https://github.com/facebook/rocksdb/issues/17 smallest key: Key(2) seq:3, type:15 (range deletion)
  ```
- With the fix, the test passes (range deletions absent from L5, still present in L6).

Reviewed By: pdillinger

Differential Revision: D106528471

Pulled By: joshkang97

fbshipit-source-id: a5f99b426d3a7a6253bc1972cf8cb60d1cb85089
2026-05-29 13:10:21 -07:00
Xingbo Wang e492562651 Prune MultiScan blocks using first internal key (#14784)
Summary:
- Use `IndexValue::first_internal_key` from `kBinarySearchWithFirstKey` index entries to decide whether the final bounded MultiScan candidate block starts at or beyond the scan limit.
- Skip that block when its first user key compares greater than or equal to the range limit with `CompareWithoutTimestamp`.
- Preserve existing conservative behavior for unbounded ranges, index entries without first-key metadata, and normal `kBinarySearch` indexes.
- Add parameterized coverage for boundary limits, in-block limits, bytewise and reverse comparators, and stripped/persisted user-defined timestamp modes.

## Testing

CI

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14784

Reviewed By: pdillinger

Differential Revision: D106302205

Pulled By: xingbowang

fbshipit-source-id: 1acebdf48bf7c18d35a781ca41c7bfd5c4ab8f47
2026-05-29 05:01:54 -07:00
Hui Xiao a0db079fdd Fall back to local compaction and report kUseLocal on remote result parse failure (#14799)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14799

When `CompactionService::Wait()` returns `kSuccess` but `CompactionServiceResult::Read()` fails before the primary renames any remote output file from `CompactionServiceResult::output_path` into the DB directory, fall back to local compaction for the same job and notify the service with `OnInstallation(..., kUseLocal)`.

At that point the remote SSTs are still in the service-managed output directory recorded in `CompactionServiceResult::output_path`, and the primary has not installed any of them into the DB yet.

Update `CompactionServiceTest.InvalidResultFallsBackToLocal` to verify the fallback completes successfully, preserves the data, and invokes `OnInstallation()` exactly once with `kUseLocal`.

Reviewed By: jaykorean

Differential Revision: D106321319

fbshipit-source-id: 39d9206f0e3f62612a52c03462bd1bee69020b80
2026-05-28 19:38:58 -07:00
Michael Huang 9270dc8149 Add blob_cache_read_byte perf context counter (#14792)
Summary:
Added blob_cache_read_byte in the rocksdb::PerContextBase to expose the blob cache read bytes when blob cache is enabled.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14792

Reviewed By: xingbowang

Differential Revision: D106528572

Pulled By: mikechuangmeta

fbshipit-source-id: 555b2f01785bb819e62ed834ee45f0436dfb2875
2026-05-28 17:46:45 -07:00
xingbowang 638354e766 Fix getdeps fallback mirror downloads (#14763)
Summary:
- parse folly getdeps manifests with bare package entries so fallback prefetching actually runs
- validate and remove bad cached/downloaded archives before trying fallback mirrors
- download through temporary files and include libiberty in the GNU toolchain fallback set

Context:

Nightly test failed with dependency download failure in folly.

```
Assessing autoconf...
Download with https://ftpmirror.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz -> /tmp/fbcode_builder_getdeps-Z__wZrocksdbZrocksdbZthird-partyZfollyZbuildZfbcode_builder-root/downloads/autoconf-autoconf-2.69.tar.gz ...
 [Complete in 136.616022 seconds]
    raise Exception(
Exception: https://ftpmirror.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz: expected sha256 954bd69b391edc12d6a4a51a2dd1476543da5c6bbf05a95b59dc0dd6fd4c2969 but got e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
make: *** [folly.mk:152: build_folly] Error 1
##[error]Process completed with exit code 2.
```

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14763

Test Plan:
1. Connection failure:
      - Forced first mirror to http://127.0.0.1:1/...
      - It logged connection refused.
      - It then tried https://mirrors.kernel.org/gnu/...
      - Download succeeded, size 1927468, SHA matched 954bd69b...
  2. Empty file / bad hash:
      - Ran a local HTTP server returning a zero-byte autoconf-2.69.tar.gz
      - Script logged mismatch with actual=e3b0c442... size=0
      - It removed the bad download and fell back to mirrors.kernel.org
      - Download succeeded with the expected SHA.
  3. Existing zero-byte cache:
      - Seeded cache with an empty tarball.
      - Script removed invalid cache and downloaded a verified copy.

Reviewed By: mszeszko-meta

Differential Revision: D105859558

Pulled By: xingbowang

fbshipit-source-id: ff1f20f87debad561610271ce99b8b8de2d4264f
2026-05-28 07:19:20 -07:00
Hui Xiao c724aeb67e Add multi-DB stress testing support (--num_dbs flag) (#14749)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14749

Add `--num_dbs` flag to run N independent DB instances in parallel. Each `StressTest` instance has its own DB with isolated fault injection (from D104959945). `db_crashtest.py` defaults to `num_dbs=1`.

For `num_dbs=1`: `--db` and `--expected_values_dir` are paths used as-is.
For `num_dbs>1`: they are parent directories; C++ creates `db_0/`, `db_1/`, ... subdirs underneath.

Path ownership: C++ owns DB and secondary dir creation (supports remote env). Python owns EV dir creation (always local). C++ also creates EV dirs as fallback for direct CLI usage. `DestroyAllDbs` cleans up subdirs and the parent dir.

Per-DB: `threads`, `max_key`, `ops_per_thread`, `reopen`, `column_families`, and all DB options.
Shared: background env threads (compaction, flush pool), `block_cache`, `write_buffer_manager`, `compressed_secondary_cache`, `rate_limiter`, `compaction_thread_pool_adjust_interval`.

Reviewed By: anand1976

Differential Revision: D104959942

fbshipit-source-id: 3d0d60101e7f2e600306e5a9c4018686bf649658
2026-05-27 11:53:11 -07:00
Xingbo Wang 364eb88151 Keep prepared transactions rollbackable after commit write failure (#14778)
Summary:
- Restore prepared transactions to `PREPARED` state when writing the commit marker fails, so callers can still roll them back.
- Preserve `PREPARED` state when rollback of a prepared transaction hits a retryable write error, allowing rollback to be retried after `DB::Resume()`.
- Update `db_stress` to clean up prepared transactions after failed commits and report detailed rollback cleanup failure diagnostics.
- Add a WritePrepared regression test covering retryable commit write failure, retryable rollback write failure, successful rollback retry, and DB reopen.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14778

Test Plan: CI

Reviewed By: pdillinger

Differential Revision: D106202437

Pulled By: xingbowang

fbshipit-source-id: b0b52e1d14f39b023b9692dd8fc44060fa35c446
2026-05-27 05:25:59 -07:00
Xingbo Wang d7afd3bb3b Sync recovery SST directory before reused MANIFEST append (#14780)
Summary:
- When `reuse_manifest_on_open` reuses the current MANIFEST, `DB::Open` recovery can flush WAL data into a new L0 SST and append the corresponding `VersionEdit` to that already-current MANIFEST.
- If open later fails and the process crashes, the MANIFEST edit can be durable while the recovered SST directory entry is not, leaving the DB pointing at a missing SST.
- Fsync the recovered SST's data directory before adding the file to the recovery edit when appending to a reused MANIFEST.
- Add a regression test that injects failure after MANIFEST sync, simulates crash cleanup of files created after the last directory sync, and verifies the recovered key remains readable.

## Task
- T272584339

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14780

Test Plan: CI

Reviewed By: anand1976

Differential Revision: D106201774

Pulled By: xingbowang

fbshipit-source-id: a44a7d1263d5bc1d82b995c90eef1a825eab4182
2026-05-27 04:42:27 -07:00
Xingbo Wang ae30c71c6b Fix secondary WAL tailing with precreated future WAL (#14781)
Summary:
- Fix secondary catch-up WAL discovery to retain existing WAL readers until MANIFEST replay advances min_log_number_to_keep past them.
- Do not treat a higher-number WAL appearing in the directory as proof that a lower-number current WAL is obsolete; async WAL precreation can expose that shape while the lower-number WAL is still growing.
- Continue scanning from the smallest retained reader so later appends to the current WAL are replayed, and add a regression test that precreates an empty future WAL before secondary catch-up.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14781

Test Plan:
- make -j128 db_secondary_test
- ./db_secondary_test --gtest_filter=DBSecondaryTest.CatchUpTailsCurrentWalWhenFutureWalExists
- ./db_secondary_test
- make check-sources

Reviewed By: hx235

Differential Revision: D106296411

Pulled By: xingbowang

fbshipit-source-id: acc850a177c02968372981d1407721540bc164f5
2026-05-26 11:55:32 -07:00
zaidoon 91b31112ed Expose AdvancedColumnFamilyOptions::memtable_batch_lookup_optimization in the C API (#14776)
Summary:
`AdvancedColumnFamilyOptions::memtable_batch_lookup_optimization` ([advanced_options.h](https://github.com/facebook/rocksdb/blob/main/include/rocksdb/advanced_options.h)) gates the skip-list memtable's batch-lookup optimization for `MultiGet`. When enabled, the search path is cached between consecutive keys, reducing per-key cost from `O(log N)` to `O(log d)` where `d` is the distance between consecutive keys.

The C++ field exists; the C API setter does not. This PR adds the missing pair, mirroring the existing `rocksdb_options_{set,get}_memtable_huge_page_size` shape exactly — the closest sibling on both axes:

- C API: adjacent memtable knob, same `rocksdb_options_t*` receiver.
- C++: same `AdvancedColumnFamilyOptions` parent struct, same immutability semantics.

## Motivation

Without this setter, C API consumers and downstream bindings cannot opt into the batch-lookup optimization. Non-skip-list memtable implementations fall back to per-key lookups, so the flag is a no-op for them.

The field is immutable on the C++ side, so calling the setter on options that are already in use by an open DB has no effect on that DB — same constraint as the underlying C++ field. This matches the behavior of every other immutable-options setter in the C API.

No change to the C++ API.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14776

Reviewed By: joshkang97

Differential Revision: D106364224

Pulled By: xingbowang

fbshipit-source-id: 90946af498fba51581a1e7d493c9e5c9b98472a2
2026-05-26 09:11:35 -07:00
zaidoon 7f32e2cabc Expose BlockBasedTableOptions::uniform_cv_threshold and BlockSearchType::kAuto in the C API (#14775)
Summary:
The block-based table format gained an "auto" index-block search mode ([table.h](https://github.com/facebook/rocksdb/blob/main/include/rocksdb/table.h)) that selects binary vs interpolation search per index block based on key uniformity. The C++ surface exposes this as two coupled knobs:

- `BlockSearchType::kAuto = 0x02` selects the per-block adaptive search at read time.
- `BlockBasedTableOptions::uniform_cv_threshold` (default `-1`, i.e. disabled) is the coefficient-of-variation threshold checked on the write path to set the per-block `is_uniform` footer bit that `kAuto` reads.

This PR adds the missing C API coverage for both:

1. **`rocksdb_block_based_table_index_block_search_type_auto = 2`** enum constant. The existing setter `rocksdb_block_based_options_set_index_block_search_type` already does `static_cast<BlockSearchType>(v)`, so `kAuto = 2` was reachable today by passing the raw int — only the named constant was missing.

2. **`rocksdb_block_based_options_set_uniform_cv_threshold(...)` setter**. The field had no C wrapper, so C/binding users could select `kAuto` but the `is_uniform` bit was never set on the write path, making `kAuto` degenerate to `kBinary`.

No getter is added: the surrounding `rocksdb_block_based_options_set_*` functions in `c.h` do not expose getters either, so adding one only here would be inconsistent with the local style.

## Motivation

Without both pieces, `kAuto` is effectively unreachable from C. This matters for binding consumers (Rust, Go, Java-via-JNI shim, etc.) who want to opt index-block search into the per-block adaptive mode.

The setter mirrors the existing `rocksdb_block_based_options_set_data_block_hash_ratio` shape exactly (same struct, same `double` payload, same naming pattern), so review surface is minimal.

No change to the C++ API.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14775

Reviewed By: joshkang97

Differential Revision: D106364288

Pulled By: xingbowang

fbshipit-source-id: bb532eac6d4c04d032a7235f25ac29ab74f636f2
2026-05-26 09:09:24 -07:00
Xingbo Wang 81fa943ca0 Avoid retryable io_uring wait_cqe stderr in crash tests (#14779)
Summary:
- Suppress retryable `io_uring_wait_cqe()` errors in `PosixFileSystem::Poll()` and `AbortIO()` before they reach stderr during crash-test SIGTERM timeout handling.
- Keep terminal `wait_cqe` failures logged and fatal.
- Extend the `db_crashtest.py` SIGTERM stderr filter only for retryable `Poll`/`AbortIO` wait_cqe errors and add regression coverage.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14779

Test Plan:
CI

## Related
- T272682963

Reviewed By: hx235

Differential Revision: D106201579

Pulled By: xingbowang

fbshipit-source-id: 2331d5cdca064ad901f6af7341b4e8f15b418663
2026-05-25 05:30:49 -07:00
zaidoon a3ba3e8a6e Expose ReadOptions::optimize_multiget_for_io in the C API (#14752)
Summary:
The async MultiGet support introduced two `ReadOptions` flags: `async_io` and `optimize_multiget_for_io`. The setter/getter for `async_io` was exposed in the C API in [ff04fb154](https://github.com/facebook/rocksdb/commit/ff04fb154bd74fd0681baa83b478095207e2719d); the corresponding pair for `optimize_multiget_for_io` was not.

This PR adds `rocksdb_readoptions_set_optimize_multiget_for_io` and `rocksdb_readoptions_get_optimize_multiget_for_io`, mirroring the existing `async_io` pattern exactly.

## Motivation

The flag is consulted in `db/version_set.cc` only inside the `#if USE_COROUTINES` guard, so this setter has no behavioral effect in non-coroutine builds. It matters for:

1. **API parity** with the C++ surface and with the existing `async_io` C API. The two flags were introduced together as part of the async MultiGet feature; only exposing one is an oversight.

2. **CPU/latency tuning in `USE_COROUTINES` builds.** Per the [Asynchronous IO in RocksDB blog post](https://rocksdb.org/blog/2022/10/07/asynchronous-io-in-rocksdb.html), `async_io=true` with `optimize_multiget_for_io=false` (single-level parallel reads) ran 775 μs/op vs 508 μs/op with `optimize_multiget_for_io=true` (multi-level), with the latter incurring additional CPU overhead from coroutine scheduling. Without this setter, coroutine-enabled builds cannot reach the single-level configuration from C.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14752

Reviewed By: mszeszko-meta

Differential Revision: D106080144

Pulled By: xingbowang

fbshipit-source-id: 28f12f07f29660392ba6ef7840b22804dab3567b
2026-05-23 04:38:23 -07:00
Peter Dillinger e82af29adb Makefile fix and speed up 'clean' (#14767)
Summary:
* Fix Makefile default target (was ordered after a folly target)
* Improved the speed of `make clean` by using just one `find` and by pruning "hidden" .* and third-party directories that should not be modified anyway.
* Reduce excessive output from `make clean`

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14767

Test Plan: manual

Reviewed By: mszeszko-meta

Differential Revision: D105972958

Pulled By: pdillinger

fbshipit-source-id: 2c0f6097c74c3129b815450f23c19ef07bfbe656
2026-05-22 17:34:53 -07:00
Peter Dillinger acfa68ccff Fix flaky preallocation tests on btrfs, zfs, tmpfs, and overlayfs (#14744)
Summary:
Fix flaky test failures in EnvPosixTestWithParam.AllocateTest and DBWALTest.TruncateLastLogAfterRecoverWithFlush that occur when running on filesystems where preallocated space is not reliably reflected in st_blocks.

The tests use stat() to check st_blocks and verify that fallocate with FALLOC_FL_KEEP_SIZE actually preallocates disk space. However, on certain filesystems, this check is unreliable:

1. btrfs and zfs: Copy-on-write filesystems where preallocated extents are not reliably reflected in st_blocks, especially under load.

2. tmpfs: Memory filesystem that may not report preallocated blocks in st_blocks.

3. overlayfs: Union filesystem common in containers that may not pass through fallocate properly or report preallocated space.

4. Any filesystem where FALLOC_FL_KEEP_SIZE is not supported: The preallocation will fail silently (error ignored with PermitUncheckedError), leaving only the written data in st_blocks.

Changes made:

env/env_test.cc:
- Added filesystem magic number definitions for TMPFS_MAGIC, OVERLAYFS_SUPER_MAGIC, and ZFS_SUPER_MAGIC
- Extended the AllocateTest to skip block count checks on zfs, tmpfs, and overlayfs in addition to btrfs
- Added runtime fallback: if st_blocks is less than expected, print a warning and skip the check instead of failing. This handles unknown filesystems or configurations where preallocation isn't supported.

db/db_wal_test.cc:
- Added includes and filesystem magic number definitions
- Added ShouldSkipAllocationCheck() helper function to detect problematic filesystems
- Modified TruncateLastLogAfterRecoverWithoutFlush, TruncateLastLogAfterRecoverWithFlush, TruncateLastLogAfterRecoverWALEmpty, and ReadOnlyRecoveryNoTruncate tests to skip allocation checks on problematic filesystems
- Added runtime fallback checks similar to env_test.cc

These changes make the tests robust against filesystem differences while still validating preallocation behavior on filesystems where it works correctly (ext4, xfs).

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14744

Test Plan: Many local 'make -j100 check' runs that would previously fail with good probability.

Reviewed By: hx235

Differential Revision: D105331256

Pulled By: pdillinger

fbshipit-source-id: 862a1512da1466cb037af15342404939b677c02a
2026-05-22 14:43:23 -07:00
Xingbo Wang 8bf167a194 Notify listeners before DB shutdown begins (#14769)
Summary:
### Notify listeners before DB shutdown begins

db_stress listener bookkeeping correlates compaction callbacks with file deletion callbacks. DBImpl intentionally skips some compaction listener callbacks once shutdown starts, but file deletion callbacks can still be delivered. That mismatch is normally tolerable for production listeners, but db_stress uses listener-local tracking to detect callback consistency and can report a false positive when shutdown interrupts a compaction callback sequence.

The failed-open case is the important gap. DB::Open() can create a DBImpl, recover or flush files, schedule background compaction, and then fail during late open work such as persisting OPTIONS or waiting for open-time compaction under fault injection. At that point DB::Open() tears down the internal DBImpl before returning an error to StressTest::Open(). If OnCompactionBegin already recorded an input file or job in DbStressListener, DBImpl shutdown can suppress the later OnCompactionPreCommit/OnCompactionCompleted callbacks that would normally clear that state. Since control has not returned to the stress harness yet, StressTest::CleanUp()/Reopen() cannot notify the listener in time. A later shutdown-time callback, or the next open retry reusing the same listener, can then observe stale tracking and abort even though RocksDB did not compact the same SST concurrently.

Add EventListener::OnDBShutdownBegin and fire it once from DBImpl::CancelAllBackgroundWork() before publishing shutting_down_. The callback also covers cleanup of a failed DB::Open() attempt, where the DB pointer refers to the internal DBImpl that was never returned to the caller. Track shutdown_notification_sent_ separately from shutting_down_ because listeners are invoked with mutex_ released, and a concurrent or reentrant cancellation must not deliver the callback twice.

Update DbStressListener to consume the DBImpl-driven shutdown notification instead of relying on StressTest::CleanUp()/Reopen() to manually notify it. This lets db_stress mark itself as shutting down before DBImpl starts skipping shutdown-sensitive compaction notifications, including during failed-open cleanup.

### Also keep listener state scoped to one db_stress open attempt.

Initialize listeners at the top of each non-transactional open retry before enabling open fault injection, so retry attempts get fresh listener state without widening open fault injection to listener construction. Factor the open fault setup into a small helper to keep the retry loop readable. The transaction open path still initializes listeners once because it does not use this open-fault retry loop.

### Also fix multi-ops transaction listener checks during shutdown

A TSAN race was reported where MultiOpsTxnsStressListener::OnCompactionCompleted called VerifyPkSkFast on a background compaction thread while the main thread was destroying the transaction DB wrapper in StressTest::CleanUp(). The reported read was a virtual call through the DB object and the write was the WritePreparedTxnDB/WriteUnpreparedTxnDB destructor updating the vptr.

The vulnerable ordering is that DBImpl can already be inside NotifyOnCompactionCompleted before shutdown is requested. It unlocks db mutex while iterating listeners; another listener such as DbStressListener can spend time in its callback, giving the main thread time to enter CleanUp()/Close(). The DB object is still shutting down, but MultiOpsTxnsStressListener may be invoked later in the same callback iteration and call VerifyPkSkFast through stress_test_->db_aptr_, racing with DB wrapper destruction.

With DBImpl-owned EventListener::OnDBShutdownBegin callback, we have MultiOpsTxnsStressListener consume that callback directly. Once DBImpl begins shutdown, the listener skips both flush-completed and compaction-completed verification callbacks, avoiding DB access during teardown. This also covers failed-open cleanup without name-based downcasts in StressTest.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14769

Test Plan: CI

Reviewed By: pdillinger

Differential Revision: D106011452

Pulled By: xingbowang

fbshipit-source-id: 768838ddcd9910de5d1b5204c990a4d88dbc850c
2026-05-22 12:32:11 -07:00
Hui Xiao 97551b72d7 Per-StressTest fault injection with env/fs cleanup (#14757)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14757

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14750

Prerequisite for multi-DB stress test support where each StressTest instance owns one DB. Moves fault injection from a single global to per-StressTest instance so each DB gets isolated fault injection; errors injected into one DB do not leak to others.

FS/Env architecture change:

  Before (upstream):
    raw_fs → FaultInjectionTestFS (global) → DbStressFSWrapper → db_stress_env → DB::Open

  After (this diff):
    Global: raw_env (no wrappers)
      Used outside StressTest: non-FS ops (threads, time, sleep), test framework FS setup (dirs), DB destruction
      Used inside StressTest: cleanup ops that must succeed (external file delete)

    Per StressTest:
      raw_fs → DbStressFSWrapper (db_stress_fs_, always)
                → FaultInjectionTestFS (db_fault_injection_fs_, optional)
                      → CompositeEnvWrapper (db_env_, always) → DB::Open

    Expected values state: Env::Default() (always local PosixEnv even when raw_env is remote)

FS layer order swapped (DbStressFSWrapper now innermost). Safe because:
- Error injection returns early; inner wrapper never executes (same behavior)
- DbStressFSWrapper assertions do not modify data (checksum validation, IOActivity checks)
- MANIFEST rename tracking slightly better for crash simulation in new order

Stored members (per StressTest):
- db_stress_fs_: DbStressFSWrapper. Always active regardless of fault injection flags.
- db_fault_injection_fs_: FaultInjectionTestFS. Only when fault injection flags set. Direct access for Enable/Disable/SetThreadLocal.
- db_env_: CompositeEnvWrapper. Always present. THE env for all DB I/O (options_.env).

Eliminated globals: db_stress_env → renamed to raw_env (no wrappers, DbStressFSWrapper moved to per-StressTest); db_stress_listener_env, db_stress_raw_fs removed; fault_fs_guard, fault_env_guard moved to per-StressTest.

Other changes:
- CleanupOutputDirectory simplified (uses raw_env, no disable/enable needed)
- SstFileManager recreated with per-StressTest env in Open()
- Remote compaction override env uses options.env directly (fixes pre-existing silent bug)
- Comments added: Env::Default() always local, DbStressDestroyDb MANIFEST explanation, fault injection log path in TEST_TMPDIR
- TestFSWritableFile::Close() now mirrors the production FSWritableFile close boundary. After the first Close() attempt, later explicit or destructor Close() calls are wrapper-level no-ops, while FaultInjectionTestFS still records the first close attempt for crash/recovery simulation.
- Added targeted fault_injection_fs_test coverage for injected metadata-close failures to ensure FaultInjectionTestFS does not retry the inner Close() path.

Reviewed By: anand1976

Differential Revision: D104959945

fbshipit-source-id: 7cf9bb494dec2b372528d5f119c023b6d392ffca
2026-05-21 18:39:18 -07:00
Maciej Szeszko d1ab4bf12c Guard db_bench DB lifetime with RWMutex against shutdown race (#14754)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14754

**Problem**

When db_bench shuts down (fatal IO error, unknown benchmark name, etc.), ErrorExit() or ~Benchmark() destroys db_/multi_dbs_ while worker threads may still be inside benchmark methods holding raw DBWithColumnFamilies pointers returned by SelectDBWithCfh(). In single-DB mode, db_.db becomes nullptr and multi_dbs_ is empty, so workers evaluate rand_int % 0 and SIGFPE. The cascade across all workers masks whatever originally triggered the shutdown. Observed in the reliable_volumes crash-fault test at ~5 SIGFPE events per hour.

**Approach**

Guard DB lifetime at the worker-thread boundary with a reader-writer lock (port::RWMutex): workers acquire a read lock for the duration of their benchmark method; mutators acquire the write lock (which waits for active readers to drain) before destroying DBs. This ensures raw pointers returned by SelectDBWithCfh() remain valid for their entire usage scope.

**Fix**

- Workers hold a DbUseGuard (RAII wrapper over a read lock on db_lifecycle_rwlock_) for the entire benchmark method in ThreadBody. Cost: one rdlock/unlock pair per worker lifetime, zero hot-path overhead.

- Every DB mutation path (ErrorExit, ~Benchmark, fresh-DB reopen) takes DbStateMutationGuard, which acquires the write lock and then stops the secondary update thread before mutation may proceed.

- ErrorExit() routes to std::_Exit(1) when called from any thread that cannot safely run the mutation cleanup path: a DbUseGuard holder (would self-wait on the write lock) or the secondary update thread (would self-join via StopSecondaryUpdateThread). Tracked via two thread-locals: holds_db_use_guard_ and is_secondary_update_thread_. Main thread takes DbStateMutationGuard, cleans up, dispatches through db_bench_exit() / ToolHooks::Exit.

- StopSecondaryUpdateThread() resets secondary_update_stopped_ to 0 after joining, so a replacement thread created by a subsequent Open() is not immediately killed.

- Protocol is mechanically enforced in debug builds:
    * SelectDBWithCfh asserts holds_db_use_guard_  (read-side ownership)
    * DeleteDBs        asserts holds_db_state_mutation_guard_ (write-side ownership)
    * DbUseGuard and DbStateMutationGuard ctor/dtor assert correct imbalance (no nested or stray release).
    * DbStateMutationGuard ctor additionally asserts !holds_db_use_guard_ and !is_secondary_update_thread_, catching the new deadlock modes the single-lock design makes reachable (WriteLock while holding ReadLock; secondary thread self-joining via StopSecondaryUpdateThread).
  These convert "trust me" invariants into "the assert fires if you break it." Direct field accesses (e.g. db_.db->NewIterator inside a benchmark method, multi_dbs_.clear() in the reopen branch) are protected by the enclosing guard scope but are not per-site asserted.

**Caveat**

port::RWMutex is pthread_rwlock_t with default attrs -> reader-preferred on Linux/glibc. The current call graph has no concurrent worker spawn during mutator wait (mutator paths run only from the main thread, not concurrently with RunBenchmark spawning workers), so writer starvation is not reachable. Documented as a constraint; revisit if that invariant changes. The port layer doesn't expose pthread_rwlockattr_setkind_np cross-platform.

**Alternatives considered**

- std::_Exit(1) on every shutdown path, skip cleanup entirely. Loses ToolHooks::Exit dispatch, flushed traces, and end-of-run stats -- those matter for the RV crash-fault test image which consumes db_bench output. Rejected.

- shared_ptr<DBWithColumnFamilies> from SelectDBWithCfh, let DB lifetime extend naturally to the last reader. Adds a per-call atomic refcount bump in the hot path; the RWMutex approach is per-method-call instead of per-op, making the hot-path cost zero. Rejected for hot-path neutrality.

Reviewed By: xingbowang

Differential Revision: D104974784

fbshipit-source-id: 3a04d9e1c0b5042436d690f573cf369de6b4c9df
2026-05-21 11:49:59 -07:00
Xingbo Wang e42af37ea7 Avoid reusing db_stress listeners across open retries (#14765)
Summary:
- Rebuild db_stress event listeners through a shared `InitializeListenersForOpen()` helper.
- Reinitialize listeners before retrying `DB::Open()` after injected open or open-compaction failures.

## Context
- A stress test failed with "Concurrent compaction of SST file detected".
- Root cause: StressTest::Open() built DbStressListener once before its DB::Open() retry loop. With open fault injection, a DB::Open() attempt can create a DBImpl, schedule background compaction, and then fail during late open work such as persisting OPTIONS. During teardown of that failed DBImpl, DBImpl's shutdown flag can suppress later compaction callbacks, leaving listener-local compaction bookkeeping stale.
- Diagnosis: Sandcastle DB LOGs showed file 16821 flushed, then a failed open attempt with an injected read error and a background compaction picking 16821. The crash was in DbStressListener::OnCompactionBegin, so this was stale db_stress listener state across open attempts rather than DBImpl allowing a real concurrent compaction of the same SST.
- Fix: factor listener construction into InitializeListenersForOpen() and call it before each DB::Open() attempt, including the retry path after open/open-compaction failure. Each DBImpl open attempt now gets fresh listener state.
- Verification: make clean; make db_stress -j192; make check-sources; git diff --check.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14765

Test Plan:
- `make clean`
- `make db_stress -j192`
- `make check-sources`
- `git diff --check`

Reviewed By: pdillinger

Differential Revision: D105969381

Pulled By: xingbowang

fbshipit-source-id: 759e2be7e1215a498ed449ab36f13e8c7975f4a4
2026-05-21 10:21:02 -07:00
xingbowang 54d13c0af9 Fix DBTest cleanup for alternate log dirs (#14764)
Summary:
- Clean DBTestBase's fixture-owned alternate WAL and db_log_dir paths during setup and teardown.
- Prevent kDBLogDir option tests from leaving dbname_/db_log_dir behind and polluting later DBTest cases in the same gtest shard.
- Preserve production DestroyDB() behavior while making the test fixture cleanup complete.

Context:
- The ARM nightly failure was exposed by the 32-shard db_test layout running DBTest.GetPicksCorrectFile before DBTest.PurgeInfoLogs in the same process.
- GetPicksCorrectFile can use kDBLogDir, which creates logs under dbname_/db_log_dir. DestroyDB() intentionally ignores DeleteDir() failures when unknown children remain, so the next fixture could observe dbname_ still existing.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14764

Test Plan:
- make -j14 db_test
- TEST_TMPDIR=/tmp/rocksdb_arm_fix_test ./db_test --gtest_filter=DBTest.GetPicksCorrectFile:DBTest.PurgeInfoLogs
- TEST_TMPDIR=/tmp/rocksdb_arm_fix_test_shard GTEST_TOTAL_SHARDS=32 GTEST_SHARD_INDEX=18 ./db_test

Reviewed By: mszeszko-meta

Differential Revision: D105860476

Pulled By: xingbowang

fbshipit-source-id: b2685063ca6c4eedec589697b439fe4aee4eda1a
2026-05-21 09:09:17 -07:00
Hui Xiao 2904bc64dc Encapsulate path access in StressTest via accessors (#14756)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14756

Pure mechanical refactor: replace all direct FLAGS_db / FLAGS_expected_values_dir / FLAGS_secondaries_base reads with accessor methods on StressTest. No new flags, no parameters, no behavior change. Prepares for multi-DB stress test where each StressTest instance has its own DB.

Changes:
- GetDbPath(), GetExpectedValuesDir(), GetSecondariesBase() accessors return the corresponding FLAGS values directly
- Replace ~15 FLAGS_db references with GetDbPath() in db_stress_test_base.cc
- Move SharedState constructor from .h to .cc (needs full StressTest type for GetExpectedValuesDir())
- Move DbStressListener constructor from .h to .cc (same reason)
- Replace FLAGS_db / FLAGS_expected_values_dir in db_stress_driver.cc with accessor calls
- NO changes to db_crashtest.py or db_stress_gflags.cc

Reviewed By: anand1976

Differential Revision: D104959943

fbshipit-source-id: d7ef6a39d4c2ed467b2960417629c09f3988faf5
2026-05-20 16:52:40 -07:00
Anand Ananthabhotla 88d7d2df75 Enable MANIFEST optimization options in db_crashtest.py and add db_stress verification (#14742)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14742

This change enables testing of the two new MANIFEST optimization options introduced in D103568447:
1. optimize_manifest_for_recovery - Skips unnecessary MANIFEST edits during recovery
2. reuse_manifest_on_open - Reuses existing MANIFEST file on DB open

Changes:
- tools/db_crashtest.py: Add both options with 20% probability to default_params
- db_stress_tool/db_stress_test_base.h: Add ManifestVerifyMode enum and member variables for tracking MANIFEST state
- db_stress_tool/db_stress_test_base.cc: Implement RecordManifestStateBeforeReopen() and VerifyManifestNotRewritten() methods to validate MANIFEST reuse on DB reopen

The verification logic handles 4 combinations:
- Both disabled: No verification (baseline)
- Only optimize enabled: No verification (hard to measure without sync points)
- Only reuse enabled: Verify MANIFEST file is reused
- Both enabled: Verify MANIFEST reused AND CURRENT unchanged

Verification is warning-only (not fatal) to account for legitimate fallback cases like corruption or size limits.

Reviewed By: hx235

Differential Revision: D105224068

fbshipit-source-id: 5baa65680fdd639674d87ff1e9187b743e691bc1
2026-05-20 11:27:27 -07:00
Hui Xiao 367f2b0fdf Resumable Remote Compaction Blog Post (#14759)
Summary:
**Summary:** as titled

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14759

Test Plan:
local server rendering test
<img width="914" height="764" alt="Screenshot 2026-05-19 at 2 18 41 PM" src="https://github.com/user-attachments/assets/0e5353d4-df41-476c-8902-28d7975330d6" />
<img width="914" height="764" alt="Screenshot 2026-05-19 at 2 18 59 PM" src="https://github.com/user-attachments/assets/a0f8692d-6ca8-4c67-b7a0-b47146080c57" />
<img width="895" height="477" alt="Screenshot 2026-05-19 at 2 19 20 PM" src="https://github.com/user-attachments/assets/7479dd54-5bc4-41da-99f3-f081a714ba7c" />

Reviewed By: jaykorean

Differential Revision: D105665739

Pulled By: hx235

fbshipit-source-id: 0701a627eb2b18b9bfd3dd22397aeae9553f1903
2026-05-19 14:35:43 -07:00
Peter Dillinger 07a5a0a804 Fix "too many open files" failures in GitHub CI (#14755)
Summary:
seen several times in the build-linux-mini-crashtest job. Raise ulimit in container spec.

Task: T271298423

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14755

Test Plan: look at reported ulimits, watch CI

Reviewed By: hx235

Differential Revision: D105664569

Pulled By: pdillinger

fbshipit-source-id: 6f7f3976bd73ecc86509ac955d5e190316e98ba3
2026-05-18 23:15:12 -07:00
Peter Dillinger 01d8c7720f Fix crash test failures in OnCompactionPreCommit listener checks (#14753)
Summary:
NotifyOnCompactionPreCommit initially omitted the shutting_down_ guard to avoid a false positive abort in the db_stress OnTableFileDeleted check, where stale compacting_files_ entries from skipped notifications would be mistaken for a bug. The better fix is to add the shutting_down_ guard for consistency with Begin and Completed, and instead make OnTableFileDeleted tolerate stale tracking during shutdown by checking a new atomic bool in the listener intended to track DBImpl's shutting_down_.

Also fix lint: release mutex before RandomSleep() in PreCommit; use char literal for find_last_of; avoid unnecessary string copy.

Document WART in listener.h: all three compaction callbacks are skipped during DB shutdown, so a committed compaction may go unobserved.

Bonus: update CLAUDE.md with instructions on avoiding non-ASCII characters

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14753

Test Plan: manually trigger many crash test runs

Reviewed By: hx235

Differential Revision: D105591913

Pulled By: pdillinger

fbshipit-source-id: 32029fea4c2571d88f645eb325db2e25a94e0d26
2026-05-18 22:31:46 -07:00
Hui Xiao cba33621bd Fix BackgroundJobPressure flakiness with TEST_WaitForBackgroundWork (#14708)
Summary:
BackgroundJobPressure test was flaky because Phase 3 used TEST_WaitForCompact() which waits for bg_compaction_scheduled_ but NOT bg_pressure_callback_in_progress_. The final "healthy" pressure callback could still be in-flight when the test checked snapshots.back(), causing compaction_scheduled=1 instead of 0.

Fix: replace TEST_WaitForCompact() with TEST_WaitForBackgroundWork() which explicitly checks bg_pressure_callback_in_progress_ (db_impl.cc:478-484). Also add TEST_WaitForBackgroundWork() before Phase 1 and Phase 2 snapshot checks for consistency, ensuring all pressure callbacks are delivered before assertions.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14708

Reviewed By: jaykorean

Differential Revision: D103784101

fbshipit-source-id: 275802b5bb70094af62486bde26b599a292e71fa
2026-05-18 17:05:57 -07:00
Hui Xiao 554a123295 Clean up failed regression test workdirs (#14751)
Summary:
Pull Request resolved: https://github.com/facebook/rocksdb/pull/14751

`regression_test.sh` already cleans the current run's `TEST_PATH` on normal success and on the early-exit path when a recent `db_bench` is still running. But a hard benchmark failure goes through `exit_on_error`, which exits before the end-of-`main()` cleanup runs.

This change adds an `EXIT` trap and a single-shot finalizer so the current invocation still cleans its own `TEST_PATH` on hard failure. It does not reintroduce any sibling-directory cleanup, and it preserves the existing success-path cleanup, early-exit cleanup, and debug preservation behavior.

| Scenario | Previous code | New code |
| --- | --- | --- |
| Normal success | Cleans current `TEST_PATH` at end of `main()` | Still cleans current `TEST_PATH` |
| Hard benchmark failure via `exit_on_error` | Can leave current `TEST_PATH` behind | `EXIT` trap cleans current `TEST_PATH` |
| Early exit because recent `db_bench` exists | Cleans current `TEST_PATH`, exits `2` | Same behavior |
| Debug mode / `DELETE_TEST_PATH=0` | Preserves artifacts | Same behavior |

Reviewed By: jaykorean

Differential Revision: D105411220

fbshipit-source-id: 37a335b87faaeee86d44ef2e24bebf1b7b9626d6
2026-05-18 16:24:56 -07:00
Maciej Szeszko 459236341c Start development 11.4 (#14747)
Summary:
* Release notes from 11.3 branch
* Update version.h
* Add [11.3.fb](https://github.com/facebook/rocksdb/tree/11.3.fb) (to check_format_compatible.sh)
* Update folly commit hash to: https://github.com/facebook/folly/releases/tag/v2026.05.11.00 (see [comment](https://github.com/facebook/rocksdb/pull/14747#issuecomment-4480217173) for more)
* Copyright headers refresh
* Decouple 11.1.fb from 11.2.fb in `db_forward_with_options_refs`
* Add validation step in `check_format_compatible.sh` to prevent gluey release strings

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14747

Reviewed By: xingbowang

Differential Revision: D105443941

Pulled By: mszeszko-meta

fbshipit-source-id: 8499b18cc2ff118d805b3865463ad2a999868de4
2026-05-18 13:35:11 -07:00
Peter Dillinger c48b020e92 Speed up parallel 'make check' scheduling (#14745)
Summary:
Reduce wall-clock time of parallel 'make check' by improving the scheduling and granularity of slow test binaries.

Three Makefile changes:

1) Refresh slow_test_regexp with current observed bottlenecks. Adds
   binaries (point_lock_manager_stress_test, compaction_service_test,
   corruption_test, comparator_db_test, external_sst_file_basic_test,
   rate_limiter_test, db_compaction_test, db_merge_operator_test,
   db_dynamic_level_test, db_bloom_filter_test, error_handler_fs_test,
   merge_helper_test, db_kv_checksum_test, inlineskiplist_test) whose
   shards take >=15s but were not being front-loaded for early
   queueing. Also drops stale FIXME comments that no longer apply
   and adds tier annotations + a maintenance recipe.

2) Add SHARD_SIZE_OVERRIDES, a per-binary override of GTEST_SHARD_SIZE,
   so binaries with slow individual tests (e.g.
   point_lock_manager_stress_test where each test is ~10s) can be
   chopped into more, smaller shards. The default of 10 stays for
   everything else. Each shard's effective size is reported in the
   'Generating ... shards for ...' line.

3) Add 'make suggest-slow-tests' to print a per-binary aggregation of
   the most recent LOG (max single-shard time, total time, shard
   count) for any binary worth attention. Used to maintain the regex
   and override list above.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14745

Test Plan:
Two runs each of 'make -j166 check', before and after this change (all compilation already finished):
Before: 197s and 198s
After: 123s and 125s
Reduction: 37%

Reviewed By: xingbowang

Differential Revision: D105332444

Pulled By: pdillinger

fbshipit-source-id: 1d1c2f89a32647e6651e2ffeb72da9d51bcc004f
2026-05-18 09:07:08 -07:00
Maciej Szeszko 3daabe2db3 Fix SeekForPrev key pinning regression in FindValueForCurrentKeyUsingSeek (#14746)
Summary:
In PR https://github.com/facebook/rocksdb/issues/13531, we added a `saved_key_.SetUserKey(ikey.user_key)` call in `FindValueForCurrentKeyUsingSeek` to fix unprepared-value reverse iteration. The default `copy`=`true` parameter unconditionally copies the key into the internal buffer, breaking is-key-pinned when `pin_data`=`true`. This path triggers only when a key has more versions than `max_sequential_skip_in_iterations` (default 8 ), making the bug rare and hard to repro deterministically.

**Fix:** pass `!pin_thru_lifetime_ || !iter_.iter()->IsKeyPinned()` as the copy parameter, matching every other `SetUserKey` call site in `DBIter`.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14746

Test Plan: New test `SeekForPrevKeyPinnedWithManyVersions`: writes 20 versions of the same key, confirms `FindValueForCurrentKeyUsingSeek` is taken via `NUMBER_OF_RESEEKS_IN_ITERATION`, asserts is-key-pinned == "1" after `SeekForPrev`. Fails without fix, passes with.

Reviewed By: xingbowang

Differential Revision: D105397906

Pulled By: mszeszko-meta

fbshipit-source-id: 5581105e9d929bb4c582c52dd6a7ae3e8dd9da72
2026-05-16 11:17:05 -07:00
Peter Dillinger 805a476a5f Add OnCompactionPreCommit listener callback (#14740)
Summary:
Adds a new `EventListener::OnCompactionPreCommit` callback that fires after a compaction job's output files are written but *before* the manifest write commits the new Version. At that point input files still have `FileMetaData::being_compacted == true`, so listeners that maintain bookkeeping of "files currently being compacted" can clean up that state without racing the compaction picker.

A check implemented by a Meta-internal RocksDB user crashes when the same file appears as input to two concurrent compactions. Tracking that set in `OnCompactionBegin` / `OnCompactionCompleted` produces false positives because `Compaction::ReleaseCompactionFiles()` flips `being_compacted` back to false before `OnCompactionCompleted` fires, so another thread can pick the same file and trigger `OnCompactionBegin` before the previous compaction's `Completed` callback runs. Doing the cleanup in `OnCompactionPreCommit` closes that race. Default implementation is a no-op, so no API break.

A trivial refactoring to split PerformTrivialMove ensures data is populated for the new callback, while calling back before the trivial move compaction is committed.

Bonus: `CLAUDE.md` update for when to call `make clean` as I've recently had it get thoroughly confused TWICE mixing build modes.

Task: T269479969

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14740

Test Plan:
- New unit test in `db/listener_test.cc` checks that `OnCompactionPreCommit` fires strictly between `OnCompactionBegin` and `OnCompactionCompleted` for the same compaction, and that input files still have `being_compacted == true` at the time it fires.
- Crash test: adds a concurrent-compaction sanity check to `db_stress`'s listener resembling the Meta-internal intended usage: tracks input file numbers from `OnCompactionBegin` to `OnCompactionPreCommit`, aborting if the same file appears as input to two concurrent compactions. Also checks other ordering constraints and checks for "leaks" from possible failure to call OnCompactionPreCommit(). Exercises all compaction styles and trivial-move/FIFO paths under load.

Reviewed By: hx235

Differential Revision: D105065326

Pulled By: pdillinger

fbshipit-source-id: 8f606a7c0fac899574e7340c600b71fed902394a
2026-05-15 16:08:23 -07:00
Xingbo Wang 21723bbbef Add async WAL precreation
Summary:
- Add experimental immutable `DBOptions::async_wal_precreate` to reserve and open one future WAL on a background HIGH-priority task, with sanitization that disables the optimization when WAL recycling is configured.
- Split WAL creation into open/preallocate and start phases so `SwitchMemtable()` can consume a prepared WAL after writing normal WAL metadata, wait for in-flight precreation, fall back to synchronous creation, and delete an unstarted prepared WAL on start failure.
- Keep WAL numbering, close, recovery, and read-only open safe for empty future WAL files left by async precreation; `error_if_wal_file_exists=true` now rejects non-empty WALs while tolerating empty WALs.
- Add public option plumbing for the C API, options parsing/stringification, random option testing, `db_bench`, `db_stress`, and crash-test configuration.
- Add WAL precreate statistics counters plus Java `TickerType`/JNI mappings, and update C++, C, and Java read-only-open documentation for the empty-WAL behavior.
- Add focused WAL/option/C/Java tests for async precreate ready/wait/failure/recovery paths, read-only WAL detection, option sanitization, and API plumbing, plus write-flow docs and unreleased history entries for the new feature and behavior change.

PR https://github.com/facebook/rocksdb/pull/14738

Reviewed By: pdillinger

Differential Revision: D105020559

fbshipit-source-id: 5059b424702e021abb8de65ceeb6d3b975280ffc
2026-05-15 10:59:47 -07:00
Hui Xiao a2c96df7d7 Add listener_uri stress test flag for pluggable EventListener (#14741)
Summary:
**Summary:**
Adds a --listener_uri flag to db_stress that creates an EventListener via ObjectLibrary from the given URI and attaches it to the DB options.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14741

Test Plan:
- Compilation
- e2e test will be done next internally with a customized listener

Reviewed By: anand1976

Differential Revision: D104750476

Pulled By: hx235

fbshipit-source-id: cdc00191de6b7434e4b373db30769ff34d99b80d
2026-05-14 17:33:29 -07:00
Peter Dillinger 87c554b492 Persist compacted manifest size for auto-tuning across DB::Open (#14725)
Summary:
last_compacted_manifest_file_size_ drives TuneMaxManifestFileSize() to compute the manifest rotation threshold, but it started at 0 on every DB::Open and was only populated after the first manifest rotation. This is really only a problem with reuse_manifest_on_open, because no fresh manifest is created on open.

Add a new forward-compatible (safe-to-ignore) MANIFEST tag kLastCompactedManifestFileSize that records the approximate compacted manifest size at the end of WriteCurrentStateToManifest. During recovery, the value is loaded and used to immediately tune the rotation threshold.

The record includes a rough estimate of its own overhead (~15 bytes) and must be the last record written by WriteCurrentStateToManifest for accurate estimation.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/14725

Test Plan:
Extended AutoTuneManifestSize in db_etc3_test to close and reopen with reuse_manifest_on_open after establishing a known auto-tuning state. Verifies that the manifest file number is preserved (no spurious rotation) and that subsequent CF additions don't trigger rotation -- proving the persisted compacted size keeps the tuned threshold correct. Verified the test fails when the recovery loading is disabled.

Relax a fragile Java test that was dependent on the exact size of the manifest file.

SHORT_TEST=1 ./tools/check_format_compatible.sh

Reviewed By: anand1976

Differential Revision: D104464522

Pulled By: pdillinger

fbshipit-source-id: 4f5d22d2e149bd40a523ee11780e5e3344803c19
2026-05-13 18:31:49 -07:00
245 changed files with 17847 additions and 2498 deletions

No files matched your search

+7
View File
@@ -2,6 +2,13 @@ name: pre-steps
runs:
using: composite
steps:
- name: Dump ulimits for diagnostics
run: |
echo "=== ulimits (soft) ==="
ulimit -a -S || true
echo "=== ulimits (hard) ==="
ulimit -a -H || true
shell: bash
- name: Install lld linker for faster builds
run: apt-get update -y && apt-get install -y lld 2>/dev/null || true
shell: bash
@@ -3,7 +3,7 @@ inputs:
suite-run:
description: Comma-separated list of test suites to run (empty to skip C++ tests)
required: false
default: arena_test,db_basic_test,db_test,db_test2,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test
default: arena_test,db_basic_test,db_test,db_etc2_test,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test
run-java:
description: Whether to run Java tests
required: false
+10 -2
View File
@@ -714,6 +714,8 @@ jobs:
CODEX_HOME: /tmp/codex-home
CODEX_MODEL: ${{ inputs.default_model }}
CODEX_REASONING_EFFORT: ${{ steps.budget_codex.outputs.effort }}
BASE_SHA: ${{ steps.pr_info.outputs.base_sha }}
PR_TITLE: ${{ steps.pr_info.outputs.pr_title }}
run: |
if [ -z "${OPENAI_API_KEY}" ]; then
echo "OPENAI_API_KEY not configured, skipping Codex review"
@@ -721,7 +723,9 @@ jobs:
fi
mkdir -p "${CODEX_HOME}"
set +e
codex exec \
codex exec review \
--base "${BASE_SHA}" \
--title "${PR_TITLE}" \
-m "${CODEX_MODEL}" \
-c model_reasoning_effort="${CODEX_REASONING_EFFORT}" \
--dangerously-bypass-approvals-and-sandbox \
@@ -1336,6 +1340,8 @@ jobs:
CODEX_HOME: /tmp/codex-home
CODEX_MODEL: ${{ inputs.selected_model != '' && inputs.selected_model || inputs.default_model }}
CODEX_REASONING_EFFORT: ${{ steps.manual_budget_codex.outputs.effort }}
BASE_SHA: ${{ steps.pr_info.outputs.base_sha }}
PR_TITLE: ${{ steps.pr_info.outputs.pr_title }}
COMMAND_TYPE: ${{ steps.request.outputs.type }}
run: |
mkdir -p "${CODEX_HOME}"
@@ -1348,7 +1354,9 @@ jobs:
--output-last-message codex-review-output.md \
- < /tmp/prompt.txt > codex-review-execution.log 2>&1
else
codex exec \
codex exec review \
--base "${BASE_SHA}" \
--title "${PR_TITLE}" \
-m "${CODEX_MODEL}" \
-c model_reasoning_effort="${CODEX_REASONING_EFFORT}" \
--dangerously-bypass-approvals-and-sandbox \
+6 -3
View File
@@ -373,14 +373,17 @@ jobs:
crash_duration: 240
container:
image: ghcr.io/facebook/rocksdb_ubuntu:22.1
options: --shm-size=16gb
options: --shm-size=16gb --ulimit nofile=1048576:1048576
steps:
- uses: actions/checkout@v4.1.0
- uses: "./.github/actions/pre-steps"
- uses: "./.github/actions/setup-ccache"
with:
cache-key-prefix: mini-crashtest
- run: ulimit -S -n `ulimit -H -n` && make V=1 -j8 CRASH_TEST_EXT_ARGS='--duration=${{ matrix.crash_duration }} --max_key=2500000' ${{ matrix.crash_test_target }}
- run: |
ulimit -S -n $(ulimit -Hn) # Raise open files soft limit to hard limit
echo "Updated ulimit -Hn: $(ulimit -Hn) -Sn: $(ulimit -Sn)"
make V=1 -j8 CRASH_TEST_EXT_ARGS='--duration=${{ matrix.crash_duration }} --max_key=2500000' ${{ matrix.crash_test_target }}
- uses: "./.github/actions/teardown-ccache"
if: always()
- uses: "./.github/actions/post-steps"
@@ -534,7 +537,7 @@ jobs:
suite_run: db_test
run_java: "false"
- test_shard: other
suite_run: arena_test,db_basic_test,db_test2,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test
suite_run: arena_test,db_basic_test,db_etc2_test,db_merge_operand_test,bloom_test,c_test,coding_test,crc32c_test,dynamic_bloom_test,env_basic_test,env_test,hash_test,random_test
run_java: "false"
- test_shard: java
suite_run: ""
+1 -1
View File
@@ -52,7 +52,7 @@ GRTAGS
GTAGS
rocksdb_dump
rocksdb_undump
db_test2
db_etc2_test
trace_analyzer
block_cache_trace_analyzer
io_tracer_parser
+6 -6
View File
@@ -4868,6 +4868,12 @@ cpp_unittest_wrapper(name="db_encryption_test",
extra_compiler_flags=[])
cpp_unittest_wrapper(name="db_etc2_test",
srcs=["db/db_etc2_test.cc"],
deps=[":rocksdb_test_lib"],
extra_compiler_flags=[])
cpp_unittest_wrapper(name="db_etc3_test",
srcs=["db/db_etc3_test.cc"],
deps=[":rocksdb_test_lib"],
@@ -5024,12 +5030,6 @@ cpp_unittest_wrapper(name="db_test",
extra_compiler_flags=[])
cpp_unittest_wrapper(name="db_test2",
srcs=["db/db_test2.cc"],
deps=[":rocksdb_test_lib"],
extra_compiler_flags=[])
cpp_unittest_wrapper(name="db_universal_compaction_test",
srcs=["db/db_universal_compaction_test.cc"],
deps=[":rocksdb_test_lib"],
+91 -3
View File
@@ -149,6 +149,26 @@ Cache management is critical for RocksDB's performance.
When reviewing RocksDB code (or preparing code for review), use this checklist:
### Contract Boundaries
- [ ] Is each behavior owned by the right layer? High-level policy (for example,
"compaction wants this I/O mode") should live at the caller/policy layer, while
lower layers should expose generic mechanisms (for example, "open a fresh
reader", "skip shared cache insertion", or "use these FileOptions").
- [ ] Do comments and names describe local contracts rather than leaking a
specific caller's rationale into reusable APIs? Generic code should not need to
know about one current use case unless the API itself is intentionally
use-case-specific.
- [ ] Does each flag or parameter control one coherent behavior? If one boolean
starts implying ownership, cache policy, I/O mode, prefetching, and caller
identity, split it into explicit flags or an options struct.
- [ ] Could a future caller use this lower-level API without accidentally
inheriting assumptions from compaction, backup, user reads, or a particular
table format? If not, tighten the contract with assertions, clearer names, or
a narrower API.
- [ ] Are implementation details not being used as policy signals? Prefer an
explicit contract over inferring behavior from incidental fields such as file
options, cache handles, or current table-reader state.
### Correctness
- [ ] Does the change preserve database semantics (e.g., snapshot isolation, key ordering)?
- [ ] Are all error cases handled appropriately?
@@ -204,19 +224,84 @@ The following patterns emerged as frequent sources of review feedback:
10. **Platform Compatibility:** Ensure changes work correctly on all supported platforms (Linux, Windows, macOS) and with all supported compilers (GCC, Clang, MSVC).
11. **Contract Boundary Leaks:** When a change plumbs a new option or use-case
specific behavior through multiple subsystems, review the call chain for
contract leaks. Caller-specific rationale belongs at the call site or public API
documentation; reusable layers should expose precise, layer-local capabilities.
Watch especially for comments mentioning one caller in generic code, booleans
that silently bundle several behaviors, and downstream code inferring policy
from an implementation detail instead of an explicit option.
---
## Important tips
### Build system
* There are 3 build system. Make, CMake, BUCK(meta internal).
* There are 3 build system. Make for git clones, BUCK (meta internal) for hg
clones, and CMake for some special cases.
* When a new .cc file is added, update Makefile, CMakeLists.txt, src.mk, BUCK.
* Don't manually edit BUCK file, after updating src.mk, run
/usr/local/bin/python3 buckifier/buckify_rocksdb.py to update it
* Use make to build and run the test. CMake and BUCK are not used locally.
* Use `make dbg` command to build all of the unit test in debug mode.
* For -j in make command, use the number of CPU cores to decide it.
### When to run `make clean` (avoid mixing build modes)
The Makefile does **not** track build mode, so object files from a prior
build are silently reused even when compiled with different flags, leading
to confusing linker errors, sanitizer false negatives, ODR violations, or
"phantom" bugs.
Run `make clean` before switching any of these:
* **`ASSERT_STATUS_CHECKED=1` ↔ unset** — changes the `Status` class layout (ABI break).
* **Sanitizer builds** — toggling any of `COMPILE_WITH_ASAN=1`,
`COMPILE_WITH_UBSAN=1`, `COMPILE_WITH_TSAN=1` on/off.
* `DEBUG_LEVEL=0` (release) ↔ `DEBUG_LEVEL=1` (debug, default for `make dbg`).
* Different compilers, `OPT` levels, or other flags affecting codegen/ABI.
**Notable exception:** `DEBUG_LEVEL=2` can be safely mixed with
`DEBUG_LEVEL=1` — rebuild a subset of files with `DEBUG_LEVEL=2` to get
extra/more accurate runtime checks for those files without a full clean.
When in doubt, `make clean` is cheap insurance compared to chasing a
phantom bug.
### Source checks
* Run `make check-sources` before committing. This catches non-ASCII
characters in source files and other source-level issues that CI will
reject. In particular, **do not use Unicode characters** (em dashes,
smart quotes, etc.) in comments or strings -- use ASCII equivalents
(`--` instead of em dash, `'` instead of smart quote, etc.).
### RTTI and dynamic_cast
* Production code and `db_stress` must build in **release mode
(`-fno-rtti`)**. Do not use `dynamic_cast` anywhere except unit tests.
Use `static_cast_with_check` from `util/cast_util.h` (validates with
`dynamic_cast` in debug builds, plain `static_cast` in release).
* Unit tests (`*_test.cc`) are built in debug mode with RTTI enabled.
### Cross-platform / portability
Local `make` only exercises Linux with GCC/Clang, but CI
(`.github/workflows/pr-jobs.yml` and `nightly.yml`) gates on a much wider
matrix, so portability breaks are invisible locally until CI fails. Code must
build (and where noted, run tests) across:
| Axis | Must support |
|------|--------------|
| OS | Linux (x86_64 + ARM), macOS, Windows |
| Compiler | GCC, Clang (libstdc++ **and** libc++), AppleClang, **MSVC (VS2022)**, MinGW (Linux cross-compile, build-only, no gflags) |
| Build system | Make, CMake, and BUCK (internal) -- keep all in sync (see "Build system" above) |
| Config | release (`-fno-rtti`), `ASSERT_STATUS_CHECKED`, ASAN/UBSAN/TSAN, folly, unity build, JNI/Java |
Treat these as constraints to satisfy and infer the specifics from them before
adding any system header, libc call, or compiler-specific construct. The most
common trap: anything that compiles under GCC/Clang on Linux but not under
**MSVC/MinGW** -- e.g. unguarded POSIX-only headers/functions (`<unistd.h>`,
`<sys/*.h>`, `getpid`, `_exit`, ...) or GCC/Clang extensions
(`__attribute__`, `__builtin_*`, VLAs, `alloca`). Prefer the `port::`/`Env`
abstractions; otherwise guard with `#ifdef OS_WIN` (POSIX `<unistd.h>` ->
Windows `<process.h>`). Because libc++ is also tested, include what you use
rather than relying on libstdc++ transitive includes.
### Unit Test
* After all of the unit tests are added, review them and try to extract common
reusable utility functions to reduce code duplication due to copy past between
@@ -232,6 +317,9 @@ The following patterns emerged as frequent sources of review feedback:
COERCE_CONTEXT_SWITCH=1 make {test_binary}
./{test_binary} --gtest_filter="*YourTestName*" --gtest_repeat=5
```
* For CI-style flaky tests that do not reproduce with `gtest_parallel.py`,
`--gtest_repeat`, or normal coerce-mode runs, inspect
`tools/gtest_parallel_repro.py --help`.
### Unit test dedup guidelines
* Extract helper functions for repeated patterns such as object
+39 -1
View File
@@ -654,6 +654,8 @@ if(USE_FOLLY)
FMT_INST_PATH)
exec_program(ls ARGS -d ${FOLLY_INST_PATH}/../gflags* OUTPUT_VARIABLE
GFLAGS_INST_PATH)
exec_program(ls ARGS -d ${FOLLY_INST_PATH}/../glog* OUTPUT_VARIABLE
GLOG_INST_PATH)
set(Boost_DIR ${BOOST_INST_PATH}/lib/cmake/Boost-1.83.0)
if(EXISTS ${FMT_INST_PATH}/lib64)
set(fmt_DIR ${FMT_INST_PATH}/lib64/cmake/fmt)
@@ -661,6 +663,42 @@ if(USE_FOLLY)
set(fmt_DIR ${FMT_INST_PATH}/lib/cmake/fmt)
endif()
set(gflags_DIR ${GFLAGS_INST_PATH}/lib/cmake/gflags)
if(EXISTS ${GLOG_INST_PATH}/lib64/cmake/glog)
set(GLOG_CMAKE_DIR ${GLOG_INST_PATH}/lib64/cmake/glog)
elseif(EXISTS ${GLOG_INST_PATH}/lib/cmake/glog)
set(GLOG_CMAKE_DIR ${GLOG_INST_PATH}/lib/cmake/glog)
endif()
if(NOT GLOG_CMAKE_DIR)
find_library(GETDEPS_GLOG_LIBRARY NAMES glogd glog
PATHS ${GLOG_INST_PATH}/lib64 ${GLOG_INST_PATH}/lib
NO_DEFAULT_PATH)
if(NOT GETDEPS_GLOG_LIBRARY OR
NOT EXISTS ${GLOG_INST_PATH}/include/glog/logging.h)
message(FATAL_ERROR "Could not find getdeps glog under "
"${GLOG_INST_PATH}")
endif()
set(GLOG_CMAKE_DIR ${CMAKE_CURRENT_BINARY_DIR}/getdeps-glog-cmake)
file(MAKE_DIRECTORY ${GLOG_CMAKE_DIR})
file(WRITE ${GLOG_CMAKE_DIR}/glog-config.cmake
"if(NOT TARGET glog::glog)\n"
" add_library(glog::glog UNKNOWN IMPORTED)\n"
" set_target_properties(glog::glog PROPERTIES\n"
" IMPORTED_LOCATION \"${GETDEPS_GLOG_LIBRARY}\"\n"
" INTERFACE_INCLUDE_DIRECTORIES \"${GLOG_INST_PATH}/include\")\n"
"endif()\n"
"set(glog_FOUND TRUE)\n"
"set(Glog_FOUND TRUE)\n"
"set(GLOG_FOUND TRUE)\n"
"set(GLOG_LIBRARY \"${GETDEPS_GLOG_LIBRARY}\")\n"
"set(GLOG_LIBRARIES \"${GETDEPS_GLOG_LIBRARY}\")\n"
"set(GLOG_INCLUDE_DIR \"${GLOG_INST_PATH}/include\")\n")
endif()
set(glog_DIR ${GLOG_CMAKE_DIR})
set(Glog_DIR ${GLOG_CMAKE_DIR})
# Unlike gflags, glog may also be installed system-wide. Pre-resolve the
# getdeps copy so folly-config.cmake cannot fall back to another version.
find_package(glog CONFIG REQUIRED PATHS ${GLOG_CMAKE_DIR}
NO_DEFAULT_PATH)
exec_program(sed ARGS -i 's/gflags_shared//g'
${FOLLY_INST_PATH}/lib/cmake/folly/folly-targets.cmake)
@@ -1453,6 +1491,7 @@ if(WITH_TESTS)
db/db_clip_test.cc
db/db_dynamic_level_test.cc
db/db_encryption_test.cc
db/db_etc2_test.cc
db/db_etc3_test.cc
db/db_flush_test.cc
db/db_inplace_update_test.cc
@@ -1476,7 +1515,6 @@ if(WITH_TESTS)
db/db_table_properties_test.cc
db/db_tailing_iter_test.cc
db/db_test.cc
db/db_test2.cc
db/db_logical_block_size_cache_test.cc
db/db_universal_compaction_test.cc
db/db_wal_test.cc
+80
View File
@@ -1,6 +1,86 @@
# Rocksdb Change Log
> NOTE: Entries for next release do not go here. Follow instructions in `unreleased_history/README.txt`
## 11.5.2 (06/25/2026)
### Bug Fixes
* Reverted PR14831 that made range_lock_manager aware of reverse-order CF
## 11.5.1 (06/24/2026)
### Bug Fixes
* Fixed a bug where closing a read-only DB instance could delete live SST files created by a concurrent read-write DB sharing the same directory.
## 11.5.0 (06/16/2026)
### New Features
* External table readers that open files through `ExternalTableOptions::fs` now update RocksDB SST/file-read statistics and file IO listener callbacks, making external file IO activity visible in existing read metrics.
* Added `DB::PrepareFileIngestion()`, a two-phase form of `IngestExternalFile`/`IngestExternalFiles`. `PrepareFileIngestion()` performs all of the work that does not require the DB mutex (validating the arguments, reading each external file's metadata, and linking/copying the files into the DB) and returns an opaque `FileIngestionHandle`. `DB::CommitFileIngestionHandle()` (or `DB::CommitFileIngestionHandles()` for several handles at once) then makes the prepared files visible; multiple handles are committed atomically in a single MANIFEST write, or none are. `FileIngestionHandle::Abort()` (or simply destroying the handle) cancels a prepared ingestion and rolls it back. This gives flexibility to the application to Prepare a file separately from when its committed, and may shorten an applications critical path on `IngestExternalFile`.
* Added two stats histograms reporting the per-call latency (in microseconds) of each successful `IngestExternalFile`/`IngestExternalFiles` call, split by phase: `rocksdb.ingest.external.file.prepare.micros` (argument validation and reading/validating/linking the external files, which does not block live writes) and `rocksdb.ingest.external.file.run.micros` (the ingestion performed under the DB mutex while live writes are blocked). Failed ingestions are not recorded.
* Added `DBOptions::use_direct_io_for_compaction_reads` (default false). When enabled, compaction-input SST reads use `O_DIRECT` while user reads remain buffered, avoiding page-cache eviction of hot user-read data by sequential compaction scans. Pair with `use_direct_io_for_flush_and_compaction = true` on write-heavy workloads for direct I/O on both compaction inputs and outputs. Rejected at Open when combined with `allow_mmap_reads = true`. No-op when `use_direct_reads = true` is already set (since all reads are already direct).
### Public API Changes
* `DB::GetPreparedFileInfoForExternalSstIngestion()` can now prepare metadata for a live DB-generated SST file so it can be passed to `IngestExternalFileArg::file_infos`. The input path must exactly match a live table file owned by the source DB, and the returned metadata handle must outlive the ingestion.
* `ExternalSstFileInfo` gained a `prepared_file_info` member produced by `SstFileWriter::Finish`, and `IngestExternalFileArg` gained a `file_infos` field: a vector of borrowed `const PreparedFileInfo*` pointers, parallel to `external_files`. The owning `ExternalSstFileInfo::prepared_file_info` handle must outlive the ingestion. When set, `IngestExternalFiles()` reuses the supplied per-file metadata instead of re-opening and scanning each file to recompute it, avoiding that extra I/O.
* Corrected the public C++ option spelling from `memtable_veirfy_per_key_checksum_on_seek` to `memtable_verify_per_key_checksum_on_seek`. RocksDB continues to accept the old misspelled OPTIONS-file key for compatibility, while newly serialized OPTIONS files use the corrected key.
* Added experimental read-scoped block buffer provider API, configured through `ReadOptions::read_scoped_block_buffer_provider`, for supported block-based table iterator scans and MultiScan reads to use caller-provided read-scoped storage for final data-block contents. When configured, supported provider-backed scan data-block reads bypass the data-block cache. The provider is ignored when mmap reads are enabled. RocksDB may still use ordinary temporary scratch for serialized block bytes, such as when a block may be compressed. Get and MultiGet do not currently provide API guarantees for this provider.
* Added `FileSystem::SyncFile()` and `Env::SyncFile()` public APIs for syncing or fsyncing a file by name without requiring callers to reopen it as writable.
### Behavior Changes
* The default `compression` for column families is now `kLZ4Compression` rather than `kSnappyCompression`. This affects only column families that do not explicitly set `compression`, and only newly-written SST files. The change is fully compatible: existing data remains readable with no migration needed, as RocksDB selects the decompressor per block. LZ4 offers slightly better compression ratios and decompression CPU efficiency vs. Snappy and similar compression CPU, tested across various server CPUs. When support is not compiled in, the fallback path for default compression is LZ4 -> Snappy -> NoCompression.
* Disable parallel compression (ignore `CompressionOptions::parallel_threads`) for fast built-in compressors: Snappy, LZ4 (accelerated, not LZ4HC), and accelerated levels of ZSTD (level < 0). These do not generally benefit from parallel compression. This behavior can be overridden with a custom Compressor from a custom CompressionManager.
* `PosixFileSystem::OptimizeForCompactionTableRead` now delegates to the base `FileSystem::OptimizeForCompactionTableRead` implementation before applying its Linux-only compaction-readahead clamp (added for #12038). The returned `FileOptions::use_direct_reads` therefore reflects `DBOptions::use_direct_reads`, consistent with the base class and with other `FileSystem` implementations. Previously the override ignored the base implementation and keyed both the returned options and the Linux readahead clamp off the incoming `FileOptions` rather than `DBOptions`. At the in-tree call site the incoming `FileOptions::use_direct_reads` already matches `DBOptions::use_direct_reads`, so this is effectively a no-op for existing configurations; it removes a latent inconsistency where a caller passing `FileOptions` whose `use_direct_reads` disagreed with `DBOptions` would have had the global flag silently ignored for compaction-input reads on Linux. (Note: the `#12038` readahead clamp still keys off the global `use_direct_reads`; it is not skipped for the compaction-only `use_direct_io_for_compaction_reads` path, since that flag enables O_DIRECT after this hook runs.)
* Brought more unity to `kLZ4Compression` and `kLZ4HCCompression` by giving each access to the other's compression levels. Negative (fast) values previously only available to LZ4 and positive (slow) values previously only available to LZ4HC are now available to both, so configuring a non-default `compression_opts.level` now selects the LZ4 compressor variant. This is a behavior change for previously tolerated but dubious configurations such as positive compression level with `kLZ4Compression`. `kLZ4Compression` and `kLZ4HCCompression` keep their effective default levels, equivalent to -1 and 9 respectively.
* Removed a discontinuity in `kZSTD` compression levels at `compression_opts.level == 0` by mapping that setting to `level = -1` rather than to `level = 3` as the ZSTD library does internally. This improves the compression auto-tuning landscape.
### Bug Fixes
* Fixed a bug where the range lock manager would crash with an assertion failure when using a reverse comparator column family.
### Performance Improvements
* Reduced commit latency when ingesting many external files by allowing `IngestExternalFileOptions::file_opening_threads` to open table readers for committed ingested files using multiple threads.
* Reduced commit latency for large external file ingestions into the last level by adding `IngestExternalFileOptions::prefetch_lmax_index_and_filter_blocks`, which can skip commit-time index and filter block prefetching for cache-backed table metadata.
## 11.4.0 (06/02/2026)
### Public API Changes
* Added `rocksdb_options_set_memtable_batch_lookup_optimization()` and `rocksdb_options_get_memtable_batch_lookup_optimization()` to the C API, exposing the existing `AdvancedColumnFamilyOptions::memtable_batch_lookup_optimization` field. This allows C API users (and downstream language bindings) to enable the skip-list memtable's batch-lookup optimization for `MultiGet`, which caches the search path between consecutive keys and reduces per-key cost from O(log N) to O(log d) where d is the distance between consecutive keys.
* Added `rocksdb_readoptions_set_optimize_multiget_for_io()` and `rocksdb_readoptions_get_optimize_multiget_for_io()` to the C API, exposing the existing `ReadOptions::optimize_multiget_for_io` field. This allows C API users (and downstream language bindings) to opt out of the multi-level parallel MultiGet path, which only takes effect when the library is built with `USE_COROUTINES`.
* Added `rocksdb_block_based_table_index_block_search_type_auto` enum constant and the `rocksdb_block_based_options_set_uniform_cv_threshold()` setter to the C API. The constant exposes `BlockSearchType::kAuto`, and the setter exposes `BlockBasedTableOptions::uniform_cv_threshold`. Both are required for `kAuto` index-block search to take effect from the C API: without setting `uniform_cv_threshold >= 0`, the per-block `is_uniform` footer bit is never written, and `kAuto` falls back to binary search at read time.
* Added `EventListener::OnDBShutdownBegin`, a callback that fires once when a DB begins shutdown, including when RocksDB cleans up a failed `DB::Open()` attempt.
* Added a new PerfContext counter `blob_cache_read_byte` for blob cache bytes read.
### Behavior Changes
* Remote compaction now falls back to local compaction when the primary cannot deserialize a successful `CompactionServiceResult` before installing any remote output files.
* StringToMap (rocksdb/convenience.h) now preserves the outer braces of nested values so each map entry is in self-contained form and can be embedded directly into another `key=value;` string (e.g. SetOptions) without losing inner ';' delimiters. A new symmetric MapToString utility is provided.
### Bug Fixes
* Fixed a bug where `AbortAllCompactions()` could leave automatic compaction work unscheduled when aborting before the background worker picked it, causing compaction and write stalls until DB restart.
* Fix a bug where db had a false positive compaction corruption error due to remote compaction service result with `has_accurate_num_input_records=false` not being serialized.
* Fixed WritePrepared TransactionDB cleanup after retryable commit or rollback write failures so prepared transactions remain rollbackable instead of leaving unresolved prepared state.
* Fix a rare corruption bug for tiered compaction that incorrectly moved last level range tombstones into proximal level. This corruption error is surfaced when force_consistency_checks is enabled.
### Performance Improvements
* Reduce the likelihood of user-facing metadata operations blocking on MANIFEST rotation when file creation is slow, such as on remote storage. MANIFEST write batches containing foreground edits from `CreateColumnFamily()`, `DropColumnFamily()`, `CreateColumnFamilyWithImport()`, `IngestExternalFile()`, and `DeleteFilesInRanges()` now get a relaxed file size threshold for triggering MANIFEST rotation; background-only MANIFEST write batches, such as compaction and flush, continue to use the normal threshold. This change should make auto-tuning manifest file size more attractive (see `max_manifest_file_size` and `max_manifest_space_amp_pct` options).
## 11.3.0 (05/15/2026)
### New Features
* Add experimental DB option `async_wal_precreate` to precreate the next WAL file in a background thread and reduce foreground WAL rotation latency. The option is sanitized to false when WAL recycling is enabled.
* Added a new `EventListener::OnCompactionPreCommit` callback that fires after a compaction job finishes but before its input files are released (i.e. while `FileMetaData::being_compacted` is still true). Listeners that maintain bookkeeping of which files are currently being compacted can clean up such state in this new callback to avoid races with concurrent compaction picking, where another thread might pick up the same files for a new compaction immediately after `being_compacted` is flipped back to false but before `OnCompactionCompleted` fires. The default implementation is a no-op so this is not a breaking change.
* Add mutable DBOption `optimize_manifest_for_recovery` (default false). When enabled, RocksDB can reduce recovery work after a clean shutdown, which may lower DB::Open latency on warm reopens.
* Added public utility APIs `ParseCompressionNameForDisplay()` to convert `TableProperties::compression_name` into a human-readable compression name for both legacy and format_version 7+ SST metadata, including custom `CompressionManager`-provided display names for custom compression types.
* Add `reuse_manifest_on_open` DBOption (default false). When enabled, DB::Open reuses the existing MANIFEST file for append instead of creating a fresh one, avoiding the cost of serializing the entire database state into a new MANIFEST on the first post-open write. To prevent this feature from interfering with manifest file size auto-tuning, an extra forward-compatible field is now always added to the MANIFEST (to track the last "compacted" size).
### Behavior Changes
* Read-only open with `error_if_wal_file_exists=true` now tolerates empty WAL files so empty precreated WALs do not prevent inspection.
* WriteCommitted TransactionDB now matches WritePrepared and WriteUnprepared compaction filtering in both single- and two-write-queue modes: a compaction filter's FilterMergeOperand will not be invoked on merge operands at or below the latest published sequence number.
### Bug Fixes
* Fixed blob-backed wide-column merge reads to preserve correct status
propagation and resolution across memtable, read-only, and secondary DB
paths.
* Fixed a bug where `DB::GetCreationTimeOfOldestFile()` could return inaccurate results instead of the real creation time when called shortly after opening a legacy DB (one whose manifest lacks `file_creation_time`) with `open_files_async = true`. The API now waits for background SST file loading to complete only when needed; modern DBs are unaffected.
* Fixed merge reads against wide-column/blob-backed base values to preserve precise failure statuses, including `GetMergeOperands()` and direct-write memtable reads.
* Fix bug in range tombstone synthesis that covers live keys added during an IngestExternalFile
* Reject the empty string as a column family name in `DB::CreateColumnFamily` / `DB::CreateColumnFamilies`. Previously such calls returned OK and a usable handle, but the column family was not persisted in the manifest, so any data written to it was silently lost on DB reopen.
* Fixed a bug where a WriteCommitted TransactionDB using commit-bypass WBWI ingestion could drop an entry that is still visible at the published sequence boundary.
## 11.2.0 (04/18/2026)
### New Features
* Added experimental `DBOptions::fast_sst_open` option. When enabled, RocksDB retrieves opaque file system metadata for SST files after flush, compaction, and external file ingestion, persists it in the MANIFEST, and passes it back to the file system on subsequent file opens to accelerate DB open time.
+90 -31
View File
@@ -327,6 +327,9 @@ missing_make_config_paths := $(shell \
$(foreach path, $(missing_make_config_paths), \
$(warning Warning: $(path) does not exist))
# This (the first rule) must depend on "all".
default: all
ifeq ($(PLATFORM), OS_AIX)
# no debug info
else ifneq ($(PLATFORM), IOS)
@@ -485,9 +488,6 @@ ifdef ROCKSDB_MODIFY_NPHASH
PLATFORM_CXXFLAGS += -DROCKSDB_MODIFY_NPHASH=1
endif
# This (the first rule) must depend on "all".
default: all
WARNING_FLAGS = -W -Wextra -Wall -Wsign-compare -Wshadow \
-Wunused-parameter
@@ -887,23 +887,43 @@ coverage: clean
parallel_tests = $(patsubst %,parallel_%,$(PARALLEL_TEST))
.PHONY: gen_parallel_tests $(parallel_tests)
# Shard size controls how many test cases run per process. The actual number
# of shards per binary is: min(ceil(test_count / GTEST_SHARD_SIZE), NCORES * 8)
# of shards per binary is: min(ceil(test_count / shard_size), NCORES * 8)
# This adapts to machine size: many small shards on beefy machines, fewer
# larger shards on CI (typically 2-4 cores).
GTEST_SHARD_SIZE ?= 10
NCORES ?= $(shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
# Per-binary overrides for GTEST_SHARD_SIZE, used to chop up binaries whose
# individual test cases are slow enough that the default of 10 leaves one
# shard on the critical path of "make check". Format: whitespace-separated
# tokens "BINARY:SIZE". Sizes were tuned from `make suggest-slow-tests`
# output to keep max shard time under ~20s on a beefy box. Reducing too far
# adds per-shard fixed overhead, so don't use 1 unless individual tests are
# >5s.
SHARD_SIZE_OVERRIDES ?= \
point_lock_manager_stress_test:1 \
external_sst_file_test:1 \
external_sst_file_basic_test:2 \
db_test:3 \
compaction_service_test:1
$(parallel_tests):
$(AM_V_at)TEST_BINARY=$(patsubst parallel_%,%,$@); \
TEST_COUNT=` \
(./$$TEST_BINARY --gtest_list_tests 2>/dev/null || echo " list_failure") \
| grep -c '^ '`; \
if [ "$$TEST_COUNT" -le 0 ]; then TEST_COUNT=1; fi; \
SHARD_SIZE=$(GTEST_SHARD_SIZE); \
for o in $(SHARD_SIZE_OVERRIDES); do \
case "$$o" in \
"$$TEST_BINARY":*) SHARD_SIZE=$${o#*:} ;; \
esac; \
done; \
MAX_SHARDS=$$(( $(NCORES) * 8 )); \
NUM_SHARDS=$$(( (TEST_COUNT + $(GTEST_SHARD_SIZE) - 1) / $(GTEST_SHARD_SIZE) )); \
NUM_SHARDS=$$(( (TEST_COUNT + SHARD_SIZE - 1) / SHARD_SIZE )); \
if [ "$$NUM_SHARDS" -gt "$$MAX_SHARDS" ]; then NUM_SHARDS=$$MAX_SHARDS; fi; \
if [ "$$NUM_SHARDS" -le 0 ]; then NUM_SHARDS=1; fi; \
echo " Generating $$NUM_SHARDS shards for $$TEST_BINARY ($$TEST_COUNT tests)"; \
echo " Generating $$NUM_SHARDS shards for $$TEST_BINARY ($$TEST_COUNT tests, shard_size=$$SHARD_SIZE)"; \
SHARD_IDX=0; \
while [ "$$SHARD_IDX" -lt "$$NUM_SHARDS" ]; do \
if [ -n "$(CI_TOTAL_SHARDS)" ] && [ $$(( $$SHARD_IDX % $(CI_TOTAL_SHARDS) )) -ne $(CI_SHARD_INDEX) ]; then \
@@ -929,31 +949,64 @@ gen_parallel_tests:
$(AM_V_at)$(FIND) t -type f -name 'run-*' -exec rm -f {} \;
$(MAKE) $(parallel_tests)
# Reorder input lines (which are one per test) so that the
# longest-running tests appear first in the output.
# Do this by prefixing each selected name with its duration,
# sort the resulting names, and remove the leading numbers.
# FIXME: the "100" we prepend is a fake time, for now.
# FIXME: squirrel away timings from each run and use them
# (when present) on subsequent runs to order these tests.
# Reorder input lines (one per test or per shard) so the longest-running
# tests/shards start first under "make check" parallel scheduling. The trick
# is to prefix matched names with a fake duration (100), sort numerically
# descending, then strip the prefix. Slow-matched tests therefore come ahead
# of all unmatched tests; ordering within each group is unspecified.
#
# Without this reordering, these two tests would happen to start only
# after almost all other tests had completed, thus adding 100 seconds
# to the duration of parallel "make check". That's the difference
# between 4 minutes (old) and 2m20s (new).
# Why this matters: gnu_parallel hands out jobs in input order. If a slow
# test starts only after most others have finished, the run is bottlenecked
# on its tail. Front-loading slow tests overlaps them with the bulk of
# faster ones for much better wall-clock time.
#
# 152.120 PASS t/DBTest.FileCreationRandomFailure
# 107.816 PASS t/DBTest.EncodeDecompressedBlockSizeTest
# Maintenance:
# 1) Run `make check` so LOG is up-to-date.
# 2) Run `make suggest-slow-tests` to see candidate slow binaries.
# 3) Add binaries with high per-shard time (slow critical path) or high
# total time across many shards (need early queueing for fan-out).
# Each alternative is wrapped in `^.*NAME.*$$` so the *whole input line* is
# captured into $$1; then `s,(...),100 $$1,` prepends "100 " to the line.
#
# With sharded test execution, prioritize binaries known to be slow.
# These generate many shards and should start early for good load balancing.
# Tiers below are based on observed timings (see suggest-slow-tests).
# Tier 1: max single-shard time >= 30s (critical-path bottlenecks).
# Tier 2: max single-shard time 15-30s.
# Tier 3: huge total time across many tiny shards; front-loading them keeps
# the tail of the run busy while big shards finish.
slow_test_regexp = \
^.*block_based_table_reader_test.*$$|^.*table_test.*$$|^.*block_test.*$$|^.*write_prepared_transaction_test.*$$|^.*transaction_test.*$$|^.*external_sst_file_test.*$$|^.*db_wal_test.*$$|^.*db_with_timestamp_basic_test.*$$|^.*db_test-.*$$
^.*point_lock_manager_stress_test.*$$|^.*db_test.*$$|^.*external_sst_file_test.*$$|^.*compaction_service_test.*$$|^.*corruption_test.*$$|^.*comparator_db_test.*$$|^.*external_sst_file_basic_test.*$$|^.*rate_limiter_test.*$$|^.*db_compaction_test.*$$|^.*write_prepared_transaction_test.*$$|^.*db_merge_operator_test.*$$|\
^.*db_dynamic_level_test.*$$|^.*db_bloom_filter_test.*$$|^.*error_handler_fs_test.*$$|^.*merge_helper_test.*$$|^.*transaction_test.*$$|^.*db_kv_checksum_test.*$$|^.*inlineskiplist_test.*$$|\
^.*db_with_timestamp_basic_test.*$$|^.*table_test.*$$|^.*db_wal_test.*$$|^.*block_based_table_reader_test.*$$|^.*block_test.*$$
prioritize_long_running_tests = \
perl -pe 's,($(slow_test_regexp)),100 $$1,' \
| sort -k1,1gr \
| sed 's/^[.0-9]* //'
# Helper: print binaries observed to be slow in the last `make check` run.
# Use this to decide whether `slow_test_regexp` above needs updating.
# Aggregates per-binary across shards; flags any binary whose max single-shard
# time is >= 20s OR whose total time is >= 200s.
.PHONY: suggest-slow-tests
suggest-slow-tests:
@if [ ! -s LOG ]; then \
echo "No (or empty) LOG file. Run 'make check' first." >&2; exit 1; \
fi
@bash -c '$(quoted_perl_command)' < LOG | awk '\
{ \
t=$$1; name=$$3; \
sub(/^t\/run-/, "", name); \
sub(/-shard-[0-9]+$$/, "", name); \
total[name] += t; \
if (t > max[name]) max[name] = t; \
count[name]++; \
} \
END { \
printf "%8s %8s %5s %s\n", "TOTAL_s", "MAX_s", "SHRDS", "BINARY"; \
for (n in total) \
if (max[n] >= 20 || total[n] >= 200) \
printf "%8.1f %8.1f %5d %s\n", total[n], max[n], count[n], n; \
}' | (read header; echo "$$header"; sort -k2,2gr)
# "make check" uses
# Run with "make J=1 check" to disable parallelism in "make check".
# Run with "make J=200% check" to run two parallel jobs per core.
@@ -990,7 +1043,7 @@ check_0:
awk -v s=$(CI_SHARD_INDEX) -v n=$(CI_TOTAL_SHARDS) '(NR-1)%n==s'; \
else cat; fi; \
fi; \
find t -name 'run-*' -print; \
$(FIND) t -name 'run-*' -print; \
} \
| $(prioritize_long_running_tests) \
| grep -E '$(tests-regexp)' \
@@ -1012,7 +1065,7 @@ valgrind_check_0:
' run "make watch-log" in a separate window' ''; \
{ \
printf './%s\n' $(filter-out $(PARALLEL_TEST) %skiplist_test options_settable_test, $(TESTS)); \
find t -name 'run-*' -print; \
$(FIND) t -name 'run-*' -print; \
} \
| $(prioritize_long_running_tests) \
| grep -E '$(tests-regexp)' \
@@ -1230,11 +1283,17 @@ clean-not-downloaded: clean-ext-libraries-bin clean-rocks clean-not-downloaded-r
clean-rocks:
# Not practical to exactly match all versions/variants in naming (e.g. debug or not)
rm -f ${LIBNAME}*.so* ${LIBNAME}*.a
rm -f $(BENCHMARKS) $(TOOLS) $(TESTS) $(PARALLEL_TEST) $(MICROBENCHS)
rm -rf $(CLEAN_FILES) ios-x86 ios-arm scan_build_report
$(FIND) . -name "*.[oda]" -exec rm -f {} \;
$(FIND) . -type f \( -name "*.gcda" -o -name "*.gcno" \) -exec rm -f {} \;
@echo Removing link targets
@rm -f ${LIBNAME}*.so* ${LIBNAME}*.a $(BENCHMARKS) $(TOOLS) $(TESTS) $(PARALLEL_TEST) $(MICROBENCHS)
@echo Finding and cleaning other files
@rm -rf $(CLEAN_FILES) ios-x86 ios-arm scan_build_report
@if $(FIND) Makefile -regextype awk &> /dev/null; then \
$(FIND) . -regextype awk \
-regex '.*/([.].*|third-party)' -prune -o \
-type f -regex '.*[.]([oda]|gcda|gcno)' -exec rm -f {} \; ; \
else \
$(FIND) . -name "*.[oda]" -exec rm -f {} \; ; \
fi
clean-rocksjava: clean-rocks
rm -rf jl jls
@@ -1247,7 +1306,7 @@ clean-ext-libraries-all:
rm -rf bzip2* snappy* zlib* lz4* zstd*
clean-ext-libraries-bin:
find . -maxdepth 1 -type d \( -name bzip2\* -or -name snappy\* -or -name zlib\* -or -name lz4\* -or -name zstd\* \) -prune -exec rm -rf {} \;
$(FIND) . -maxdepth 1 -type d \( -name bzip2\* -or -name snappy\* -or -name zlib\* -or -name lz4\* -or -name zstd\* \) -prune -exec rm -rf {} \;
tags:
ctags -R .
@@ -1525,7 +1584,7 @@ db_open_with_config_test: $(OBJ_DIR)/db/db_open_with_config_test.o $(TEST_LIBRAR
db_test: $(OBJ_DIR)/db/db_test.o $(TEST_LIBRARY) $(LIBRARY)
$(AM_LINK)
db_test2: $(OBJ_DIR)/db/db_test2.o $(TEST_LIBRARY) $(LIBRARY)
db_etc2_test: $(OBJ_DIR)/db/db_etc2_test.o $(TEST_LIBRARY) $(LIBRARY)
$(AM_LINK)
db_etc3_test: $(OBJ_DIR)/db/db_etc3_test.o $(TEST_LIBRARY) $(LIBRARY)
+178 -73
View File
@@ -1,62 +1,188 @@
#!/usr/bin/env python3
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This source code is licensed under both the GPLv2 (found in the COPYING file in the root directory)
# and the Apache 2.0 License (found in the LICENSE.Apache file in the root directory).
"""
Pre-download packages with unreliable mirrors using fallback mirrors.
Reads package info from folly's getdeps manifest files.
"""
import sys
import os
import hashlib
import subprocess
import configparser
import hashlib
import os
import shutil
import sys
import urllib.request
DOWNLOAD_TIMEOUT_SECONDS = 120
DOWNLOAD_CHUNK_BYTES = 64 * 1024
MAX_DOWNLOAD_BYTES = 50 * 1024 * 1024
MIRROR_FALLBACKS = {
"ftpmirror.gnu.org/gnu/": [
"https://mirrors.kernel.org/gnu/",
"https://ftpmirror.gnu.org/gnu/",
"https://ftp.gnu.org/gnu/",
],
"ftp.gnu.org/gnu/": [
"https://mirrors.kernel.org/gnu/",
"https://ftpmirror.gnu.org/gnu/",
"https://ftp.gnu.org/gnu/",
],
}
# These packages must have URLs matching MIRROR_FALLBACKS; other packages are
# left for getdeps.py's normal download path.
PACKAGES_TO_CHECK = ("autoconf", "automake", "libtool", "libiberty")
def sha256_file(path):
"""Calculate SHA256 hash of a file."""
h = hashlib.sha256()
try:
with open(path, 'rb') as f:
for chunk in iter(lambda: f.read(65536), b''):
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(65536), b""):
h.update(chunk)
return h.hexdigest()
except Exception:
return None
def parse_manifest(manifest_path):
"""Parse a getdeps manifest file to extract download info."""
config = configparser.ConfigParser()
# folly manifests can contain bare keys in sections unrelated to downloads.
config = configparser.ConfigParser(allow_no_value=True, interpolation=None)
try:
config.read(manifest_path)
if 'download' in config:
return {
'url': config['download'].get('url', ''),
'sha256': config['download'].get('sha256', ''),
}
except Exception:
pass
with open(manifest_path, encoding="utf-8") as manifest_file:
config.read_file(manifest_file)
except Exception as ex:
print(f" {os.path.basename(manifest_path)}: WARNING - parse failed: {ex}")
return None
if "download" in config:
return {
"url": config["download"].get("url", ""),
"sha256": config["download"].get("sha256", ""),
}
return None
def file_size(path):
try:
return os.path.getsize(path)
except Exception:
return None
def get_fallback_mirrors(url):
"""Get fallback mirror URLs for a given URL."""
# Fallback mirror patterns for known unreliable hosts
mirror_fallbacks = {
"ftp.gnu.org/gnu/": [
"https://mirrors.kernel.org/gnu/",
"https://ftpmirror.gnu.org/gnu/",
"https://ftp.gnu.org/gnu/",
],
"ftpmirror.gnu.org/gnu/": [
"https://mirrors.kernel.org/gnu/",
"https://ftpmirror.gnu.org/gnu/",
"https://ftp.gnu.org/gnu/",
],
}
for pattern, mirrors in mirror_fallbacks.items():
for pattern, mirrors in MIRROR_FALLBACKS.items():
if pattern in url:
# Extract the path after the pattern
path_start = url.find(pattern) + len(pattern)
path = url[path_start:]
return [mirror + path for mirror in mirrors]
return [url] # No fallback, use original
return []
def download_url(url, filepath):
"""Download URL to filepath without leaving partial files behind."""
tmp_filepath = filepath + ".tmp"
if os.path.exists(tmp_filepath):
os.remove(tmp_filepath)
request = urllib.request.Request(
url, headers={"User-Agent": "rocksdb-getdeps-fallback/1.0"}
)
try:
with urllib.request.urlopen(
request, timeout=DOWNLOAD_TIMEOUT_SECONDS
) as response, open(tmp_filepath, "wb") as output:
copied = 0
while True:
chunk = response.read(DOWNLOAD_CHUNK_BYTES)
if not chunk:
break
copied += len(chunk)
if copied > MAX_DOWNLOAD_BYTES:
raise Exception(
f"download exceeds {MAX_DOWNLOAD_BYTES} bytes"
)
output.write(chunk)
os.replace(tmp_filepath, filepath)
finally:
if os.path.exists(tmp_filepath):
os.remove(tmp_filepath)
def prepare_download(package, info, download_dir, cache_dir):
url = info["url"]
expected_sha256 = info["sha256"]
mirrors = get_fallback_mirrors(url)
if not mirrors:
return False
if not expected_sha256:
print(f" {package}: WARNING - skipped fallback without sha256")
return False
# getdeps uses format: {package}-{filename}
filename = f"{package}-{os.path.basename(url)}"
filepath = os.path.join(download_dir, filename)
cache_path = os.path.join(cache_dir, filename)
# Check if already valid.
actual_sha256 = sha256_file(filepath) if os.path.exists(filepath) else None
if actual_sha256 == expected_sha256:
print(f" {filename}: OK (already downloaded)")
return True
if actual_sha256 is not None:
print(
f" {filename}: WARNING - removing invalid download "
f"sha256={actual_sha256}"
)
os.remove(filepath)
# The cache is only an opportunistic single-build accelerator; callers
# should not share it across concurrent builds without external locking.
actual_sha256 = sha256_file(cache_path) if os.path.exists(cache_path) else None
if actual_sha256 == expected_sha256:
print(f" {filename}: OK (from cache)")
shutil.copy2(cache_path, filepath)
return True
if actual_sha256 is not None:
print(
f" {filename}: WARNING - removing invalid cache "
f"sha256={actual_sha256}"
)
os.remove(cache_path)
# Try fallback mirrors.
for mirror_url in mirrors:
print(f" {filename}: trying {mirror_url}...")
try:
download_url(mirror_url, filepath)
except Exception as ex:
print(f" {filename}: WARNING - download failed: {ex}")
continue
actual_sha256 = sha256_file(filepath)
if actual_sha256 == expected_sha256:
size = file_size(filepath)
print(f" {filename}: OK (downloaded, {size} bytes)")
shutil.copy2(filepath, cache_path)
return True
size = file_size(filepath)
print(
f" {filename}: WARNING - sha256 mismatch from {mirror_url}: "
f"expected={expected_sha256} actual={actual_sha256} size={size}"
)
os.remove(filepath)
print(f" {filename}: WARNING - all mirrors failed")
return False
def main():
if len(sys.argv) != 4:
@@ -64,60 +190,39 @@ def main():
sys.exit(1)
download_dir, cache_dir, manifests_dir = sys.argv[1], sys.argv[2], sys.argv[3]
os.makedirs(download_dir, exist_ok=True)
os.makedirs(cache_dir, exist_ok=True)
# Packages known to have unreliable mirrors
packages_to_check = ["autoconf", "automake", "libtool"]
for package in packages_to_check:
checked = 0
ready = 0
for package in PACKAGES_TO_CHECK:
manifest_path = os.path.join(manifests_dir, package)
if not os.path.exists(manifest_path):
if not os.path.isfile(manifest_path):
continue
info = parse_manifest(manifest_path)
if not info or not info['url'] or not info['sha256']:
if not info or not info["url"]:
continue
# Determine filename from URL
url = info['url']
expected_sha256 = info['sha256']
url_filename = os.path.basename(url)
# getdeps uses format: {package}-{filename}
filename = f"{package}-{url_filename}"
filepath = os.path.join(download_dir, filename)
cache_path = os.path.join(cache_dir, filename)
# Check if already valid
if os.path.exists(filepath) and sha256_file(filepath) == expected_sha256:
print(f" {filename}: OK (already downloaded)")
if not info["sha256"]:
print(f" {package}: WARNING - skipped fallback without sha256")
continue
# Check cache
if os.path.exists(cache_path) and sha256_file(cache_path) == expected_sha256:
print(f" {filename}: OK (from cache)")
subprocess.run(['cp', cache_path, filepath], check=True)
if not get_fallback_mirrors(info["url"]):
print(
f" {package}: WARNING - skipped fallback without known mirror "
f"for {info['url']}"
)
continue
# Try fallback mirrors
mirrors = get_fallback_mirrors(url)
downloaded = False
for mirror_url in mirrors:
print(f" {filename}: trying {mirror_url}...")
try:
subprocess.run(['wget', '-q', '-O', filepath, mirror_url], check=True, timeout=120)
if sha256_file(filepath) == expected_sha256:
print(f" {filename}: OK (downloaded)")
subprocess.run(['cp', filepath, cache_path], check=False)
downloaded = True
break
else:
os.remove(filepath)
except Exception:
if os.path.exists(filepath):
os.remove(filepath)
checked += 1
try:
if prepare_download(package, info, download_dir, cache_dir):
ready += 1
except Exception as ex:
print(f" {package}: WARNING - fallback preparation failed: {ex}")
if not downloaded:
print(f" {filename}: WARNING - all mirrors failed")
print(f" fallback mirror downloads ready: {ready}/{checked}")
if __name__ == "__main__":
main()
+26 -26
View File
@@ -46,7 +46,7 @@ This document provides guidance on how to add new options to RocksDB's public AP
## Pattern 1: Adding a Standard Column Family Option
Example reference: commit `94e65a2e0b4f817aa4bfa4c96cdf867e7980d7bc` (memtable_veirfy_per_key_checksum_on_seek)
Example reference: commit `94e65a2e0b4f817aa4bfa4c96cdf867e7980d7bc` (memtable_verify_per_key_checksum_on_seek)
### Step 1: Define the Option in Public Header
@@ -63,7 +63,7 @@ Add the option with documentation in `AdvancedColumnFamilyOptions` struct:
// operation.
// This option depends on memtable_protection_bytes_per_key to be non zero.
// If memtable_protection_bytes_per_key is zero, no validation is performed.
bool memtable_veirfy_per_key_checksum_on_seek = false;
bool memtable_verify_per_key_checksum_on_seek = false;
```
### Step 2: Add to Internal Options Structs
@@ -74,14 +74,14 @@ Add to `MutableCFOptions` struct (or `ImmutableCFOptions` for immutable options)
```cpp
// In MutableCFOptions constructor from Options:
memtable_veirfy_per_key_checksum_on_seek(
options.memtable_veirfy_per_key_checksum_on_seek),
memtable_verify_per_key_checksum_on_seek(
options.memtable_verify_per_key_checksum_on_seek),
// In MutableCFOptions default constructor:
memtable_veirfy_per_key_checksum_on_seek(false),
memtable_verify_per_key_checksum_on_seek(false),
// In MutableCFOptions struct member declarations:
bool memtable_veirfy_per_key_checksum_on_seek;
bool memtable_verify_per_key_checksum_on_seek;
```
### Step 3: Register for Serialization/Deserialization
@@ -91,9 +91,9 @@ bool memtable_veirfy_per_key_checksum_on_seek;
Add to the options type info map for serialization:
```cpp
{"memtable_veirfy_per_key_checksum_on_seek",
{"memtable_verify_per_key_checksum_on_seek",
{offsetof(struct MutableCFOptions,
memtable_veirfy_per_key_checksum_on_seek),
memtable_verify_per_key_checksum_on_seek),
OptionType::kBoolean, OptionVerificationType::kNormal,
OptionTypeFlags::kMutable}},
```
@@ -101,8 +101,8 @@ Add to the options type info map for serialization:
Add logging in `MutableCFOptions::Dump()`:
```cpp
ROCKS_LOG_INFO(log, "memtable_veirfy_per_key_checksum_on_seek: %d",
memtable_veirfy_per_key_checksum_on_seek);
ROCKS_LOG_INFO(log, "memtable_verify_per_key_checksum_on_seek: %d",
memtable_verify_per_key_checksum_on_seek);
```
### Step 4: Update Options Helper
@@ -112,8 +112,8 @@ ROCKS_LOG_INFO(log, "memtable_veirfy_per_key_checksum_on_seek: %d",
Add to `UpdateColumnFamilyOptions()`:
```cpp
cf_opts->memtable_veirfy_per_key_checksum_on_seek =
moptions.memtable_veirfy_per_key_checksum_on_seek;
cf_opts->memtable_verify_per_key_checksum_on_seek =
moptions.memtable_verify_per_key_checksum_on_seek;
```
### Step 5: Add to Options Settable Test
@@ -123,7 +123,7 @@ cf_opts->memtable_veirfy_per_key_checksum_on_seek =
Add to the test string in `ColumnFamilyOptionsAllFieldsSettable`:
```cpp
"memtable_veirfy_per_key_checksum_on_seek=1;"
"memtable_verify_per_key_checksum_on_seek=1;"
```
### Step 6: Add db_stress Support
@@ -131,23 +131,23 @@ Add to the test string in `ColumnFamilyOptionsAllFieldsSettable`:
**File: `db_stress_tool/db_stress_common.h`**
```cpp
DECLARE_bool(memtable_veirfy_per_key_checksum_on_seek);
DECLARE_bool(memtable_verify_per_key_checksum_on_seek);
```
**File: `db_stress_tool/db_stress_gflags.cc`**
```cpp
DEFINE_bool(
memtable_veirfy_per_key_checksum_on_seek,
ROCKSDB_NAMESPACE::Options().memtable_veirfy_per_key_checksum_on_seek,
"Sets CF option memtable_veirfy_per_key_checksum_on_seek.");
memtable_verify_per_key_checksum_on_seek,
ROCKSDB_NAMESPACE::Options().memtable_verify_per_key_checksum_on_seek,
"Sets CF option memtable_verify_per_key_checksum_on_seek.");
```
**File: `db_stress_tool/db_stress_test_base.cc`**
```cpp
options.memtable_veirfy_per_key_checksum_on_seek =
FLAGS_memtable_veirfy_per_key_checksum_on_seek;
options.memtable_verify_per_key_checksum_on_seek =
FLAGS_memtable_verify_per_key_checksum_on_seek;
```
### Step 7: Add db_bench Support
@@ -156,12 +156,12 @@ options.memtable_veirfy_per_key_checksum_on_seek =
```cpp
// Flag definition (near related flags):
DEFINE_bool(memtable_veirfy_per_key_checksum_on_seek, false,
"Sets CF option memtable_veirfy_per_key_checksum_on_seek");
DEFINE_bool(memtable_verify_per_key_checksum_on_seek, false,
"Sets CF option memtable_verify_per_key_checksum_on_seek");
// Apply flag to options (in InitializeOptionsFromFlags or similar):
options.memtable_veirfy_per_key_checksum_on_seek =
FLAGS_memtable_veirfy_per_key_checksum_on_seek;
options.memtable_verify_per_key_checksum_on_seek =
FLAGS_memtable_verify_per_key_checksum_on_seek;
```
### Step 8: Add Crash Test Support
@@ -169,7 +169,7 @@ options.memtable_veirfy_per_key_checksum_on_seek =
**File: `tools/db_crashtest.py`**
```python
"memtable_veirfy_per_key_checksum_on_seek": lambda: random.choice([0] * 7 + [1]),
"memtable_verify_per_key_checksum_on_seek": lambda: random.choice([0] * 7 + [1]),
```
Also add constraint handling in `finalize_and_sanitize()` if needed:
@@ -177,7 +177,7 @@ Also add constraint handling in `finalize_and_sanitize()` if needed:
```python
# only skip list memtable representation supports paranoid memory checks
if dest_params.get("memtablerep") != "skip_list":
dest_params["memtable_veirfy_per_key_checksum_on_seek"] = 0
dest_params["memtable_verify_per_key_checksum_on_seek"] = 0
```
### Step 9: Add Release Note
@@ -185,7 +185,7 @@ if dest_params.get("memtablerep") != "skip_list":
**File: `unreleased_history/new_features/<descriptive_name>.md`**
```markdown
A new flag memtable_veirfy_per_key_checksum_on_seek is added to AdvancedColumnFamilyOptions. When it is enabled, it will validate key checksum along the binary search path on skiplist based memtable during seek operation.
A new flag memtable_verify_per_key_checksum_on_seek is added to AdvancedColumnFamilyOptions. When it is enabled, it will validate key checksum along the binary search path on skiplist based memtable during seek operation.
```
---
+132 -26
View File
@@ -30,13 +30,78 @@ inline static SequenceNumber GetSeqNum(const DBImpl* db, const Snapshot* s) {
Status ArenaWrappedDBIter::GetProperty(std::string prop_name,
std::string* prop) {
if (prop_name == "rocksdb.iterator.super-version-number") {
if (!internal_iter_initialized_) {
*prop = std::to_string(sv_number_);
return Status::OK();
}
// First try to pass the value returned from inner iterator.
if (!db_iter_->GetProperty(prop_name, prop).ok()) {
*prop = std::to_string(sv_number_);
}
return Status::OK();
}
return db_iter_->GetProperty(prop_name, prop);
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return db_iter_->status();
}
return db_iter_->GetProperty(std::move(prop_name), prop);
}
void ArenaWrappedDBIter::CleanupDeferredSuperVersion() {
if (deferred_sv_ != nullptr) {
assert(db_impl_ != nullptr);
db_impl_->CleanupIteratorSuperVersion(
deferred_sv_, read_options_.background_purge_on_iterator_cleanup);
deferred_sv_ = nullptr;
}
}
void ArenaWrappedDBIter::DestroyDBIter() {
db_iter_->~DBIter();
CleanupDeferredSuperVersion();
}
void ArenaWrappedDBIter::ColumnFamilyDataUnrefDeleter::operator()(
ColumnFamilyData* cfd) const {
if (cfd == nullptr) {
return;
}
assert(db_impl != nullptr);
InstrumentedMutexLock lock(db_impl->mutex());
cfd->UnrefAndTryDelete();
}
void ArenaWrappedDBIter::DestroyDBIterAndArena() {
DestroyDBIter();
arena_.~Arena();
}
Status ArenaWrappedDBIter::EnsureInternalIteratorInitialized(
const MultiScanArgs* scan_opts) {
if (internal_iter_initialized_) {
return Status::OK();
}
if (db_impl_ == nullptr || deferred_cfd_ == nullptr ||
deferred_sv_ == nullptr) {
Status s = Status::InvalidArgument(
"Internal iterator cannot be initialized without deferred DB state");
db_iter_->set_status(s);
db_iter_->set_valid(false);
return s;
}
const MultiScanArgs* pruning_scan_opts =
scan_opts != nullptr && scan_opts->HasBoundedScanRanges() ? scan_opts
: nullptr;
child_read_options_ = read_options_;
child_read_options_.snapshot = nullptr;
InternalIterator* internal_iter = db_impl_->NewInternalIterator(
child_read_options_, deferred_cfd_, deferred_sv_, &arena_, sequence_,
/*allow_unprepared_value=*/true, this, pruning_scan_opts);
deferred_cfd_ = nullptr;
deferred_sv_ = nullptr;
SetIterUnderDBIterImpl(internal_iter);
return Status::OK();
}
void ArenaWrappedDBIter::Init(
@@ -44,18 +109,26 @@ void ArenaWrappedDBIter::Init(
const MutableCFOptions& mutable_cf_options, const Version* version,
const SequenceNumber& sequence, uint64_t version_number,
ReadCallback* read_callback, ColumnFamilyHandleImpl* cfh,
bool expose_blob_index, bool allow_refresh, ReadOnlyMemTable* active_mem) {
bool expose_blob_index, bool allow_refresh, ReadOnlyMemTable* active_mem,
DBImpl* db_impl, ColumnFamilyData* cfd) {
read_options_ = read_options;
child_read_options_ = read_options;
if (!CheckFSFeatureSupport(env->GetFileSystem().get(),
FSSupportedOps::kAsyncIO)) {
read_options_.async_io = false;
}
read_options_.total_order_seek |= ioptions.prefix_seek_opt_in_only;
db_iter_ = DBIter::NewIter(
env, read_options_, ioptions, mutable_cf_options,
ioptions.user_comparator, /*internal_iter=*/nullptr, version, sequence,
read_callback, active_mem, cfh, expose_blob_index, &arena_);
if (cfh != nullptr) {
db_impl = cfh->db();
cfd = cfh->cfd();
}
db_iter_ = DBIter::NewIter(env, read_options_, ioptions, mutable_cf_options,
ioptions.user_comparator,
/*internal_iter=*/nullptr, version, sequence,
read_callback, active_mem, /*cfh=*/nullptr,
expose_blob_index, &arena_, db_impl, cfd);
sv_number_ = version_number;
allow_refresh_ = allow_refresh;
@@ -65,13 +138,13 @@ void ArenaWrappedDBIter::Init(
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) {
if (cfd_ref_ != nullptr && read_options_.snapshot != nullptr &&
allow_refresh_ && read_options_.auto_refresh_iterator_with_snapshot) {
// The intent here is to capture the superversion number change
// reasonably soon from the time it actually happened. As such,
// we're fine with weaker synchronization / ordering guarantees
// provided by relaxed atomic (in favor of less CPU / mem overhead).
uint64_t cur_sv_number = cfh_->cfd()->GetSuperVersionNumberRelaxed();
uint64_t cur_sv_number = cfd_ref_->GetSuperVersionNumberRelaxed();
if ((sv_number_ != cur_sv_number) && status().ok()) {
// Changing iterators' direction is pretty heavy-weight operation and
// could have unintended consequences when it comes to prefix seek.
@@ -150,13 +223,13 @@ void ArenaWrappedDBIter::DoRefresh(const Snapshot* snapshot,
// present in the error log, but won't be reflected in the iterator status.
// This is by design as we expect compaction to clean up those obsolete files
// eventually.
db_iter_->~DBIter();
arena_.~Arena();
DestroyDBIterAndArena();
new (&arena_) Arena();
auto cfd = cfh_->cfd();
auto db_impl = cfh_->db();
auto cfd = cfd_ref_.get();
auto db_impl = db_impl_;
assert(cfd != nullptr);
assert(db_impl != nullptr);
SuperVersion* sv = cfd->GetReferencedSuperVersion(db_impl);
assert(sv->version_number >= sv_number);
@@ -164,23 +237,27 @@ void ArenaWrappedDBIter::DoRefresh(const Snapshot* snapshot,
if (read_callback_) {
read_callback_->Refresh(read_seq);
}
// TODO: Preserve Prepare() scan options across Refresh() so a refreshed
// MultiScan iterator can rebuild the same pruned tree.
Init(env, read_options_, cfd->ioptions(), sv->mutable_cf_options, sv->current,
read_seq, sv->version_number, read_callback_, cfh_, expose_blob_index_,
allow_refresh_, allow_mark_memtable_for_flush_ ? sv->mem : nullptr);
read_seq, sv->version_number, read_callback_, nullptr,
expose_blob_index_, allow_refresh_,
allow_mark_memtable_for_flush_ ? sv->mem : nullptr, db_impl, cfd);
InternalIterator* internal_iter = db_impl->NewInternalIterator(
read_options_, cfd, sv, &arena_, read_seq,
/* allow_unprepared_value */ true, /* db_iter */ this);
SetIterUnderDBIter(internal_iter);
internal_iter_initialized_ = true;
}
Status ArenaWrappedDBIter::Refresh(const Snapshot* snapshot) {
if (cfh_ == nullptr || !allow_refresh_) {
if (cfd_ref_ == nullptr || db_impl_ == nullptr || !allow_refresh_) {
return Status::NotSupported("Creating renew iterator is not allowed.");
}
assert(db_iter_ != nullptr);
auto cfd = cfh_->cfd();
auto db_impl = cfh_->db();
auto cfd = cfd_ref_.get();
auto db_impl = db_impl_;
// TODO(yiwu): For last_seq_same_as_publish_seq_==false, this is not the
// correct behavior. Will be corrected automatically when we take a snapshot
@@ -193,6 +270,13 @@ Status ArenaWrappedDBIter::Refresh(const Snapshot* snapshot) {
TEST_SYNC_POINT("ArenaWrappedDBIter::Refresh:1");
TEST_SYNC_POINT("ArenaWrappedDBIter::Refresh:2");
if (!internal_iter_initialized_) {
Status s = EnsureInternalIteratorInitialized(nullptr);
if (!s.ok()) {
return s;
}
}
while (true) {
if (sv_number_ != cur_sv_number) {
DoRefresh(snapshot, cur_sv_number);
@@ -250,25 +334,47 @@ Status ArenaWrappedDBIter::Refresh(const Snapshot* snapshot) {
return Status::OK();
}
void ArenaWrappedDBIter::Prepare(const MultiScanArgs& scan_opts) {
if (prepare_called_) {
db_iter_->set_status(Status::InvalidArgument(
"Prepare called more than once on the same iterator"));
db_iter_->set_valid(false);
return;
}
prepare_called_ = true;
Status s = db_iter_->SetScanOptionsForPrepare(scan_opts);
if (!s.ok()) {
return;
}
const MultiScanArgs* pruning_scan_opts =
scan_opts.HasBoundedScanRanges() ? &scan_opts : nullptr;
s = EnsureInternalIteratorInitialized(pruning_scan_opts);
if (!s.ok()) {
return;
}
db_iter_->PrepareInternalChildren();
}
ArenaWrappedDBIter* NewArenaWrappedDbIterator(
Env* env, const ReadOptions& read_options, ColumnFamilyHandleImpl* cfh,
SuperVersion* sv, const SequenceNumber& sequence,
ReadCallback* read_callback, DBImpl* db_impl, bool expose_blob_index,
bool allow_refresh, bool allow_mark_memtable_for_flush) {
ArenaWrappedDBIter* db_iter = new ArenaWrappedDBIter();
ColumnFamilyData* cfd = cfh->cfd();
db_iter->Init(env, read_options, cfh->cfd()->ioptions(),
sv->mutable_cf_options, sv->current, sequence,
sv->version_number, read_callback, cfh, expose_blob_index,
allow_refresh,
allow_mark_memtable_for_flush ? sv->mem : nullptr);
if (cfh != nullptr && allow_refresh) {
db_iter->StoreRefreshInfo(cfh, read_callback, expose_blob_index);
if (allow_refresh && cfd != nullptr) {
db_iter->StoreRefreshInfo(read_callback, expose_blob_index);
}
InternalIterator* internal_iter = db_impl->NewInternalIterator(
db_iter->GetReadOptions(), cfh->cfd(), sv, db_iter->GetArena(), sequence,
/*allow_unprepared_value=*/true, db_iter);
db_iter->SetIterUnderDBIter(internal_iter);
db_iter->StoreDeferredInitInfo(db_impl, cfd, sv, sequence,
allow_mark_memtable_for_flush);
return db_iter;
}
+74 -17
View File
@@ -10,6 +10,7 @@
#pragma once
#include <stdint.h>
#include <memory>
#include <string>
#include "db/db_impl/db_impl.h"
@@ -33,10 +34,17 @@ class Version;
// When using the class's Iterator interface, the behavior is exactly
// the same as the inner DBIter.
class ArenaWrappedDBIter : public Iterator {
struct ColumnFamilyDataUnrefDeleter {
DBImpl* db_impl = nullptr;
void operator()(ColumnFamilyData* cfd) const;
};
using ColumnFamilyDataRef =
std::unique_ptr<ColumnFamilyData, ColumnFamilyDataUnrefDeleter>;
public:
~ArenaWrappedDBIter() override {
if (db_iter_ != nullptr) {
db_iter_->~DBIter();
DestroyDBIter();
} else {
assert(false);
}
@@ -51,7 +59,7 @@ class ArenaWrappedDBIter : public Iterator {
// Set the internal iterator wrapped inside the DB Iterator. Usually it is
// a merging iterator.
virtual void SetIterUnderDBIter(InternalIterator* iter) {
db_iter_->SetIter(iter);
SetIterUnderDBIterImpl(iter);
}
void SetMemtableRangetombstoneIter(
@@ -60,26 +68,48 @@ class ArenaWrappedDBIter : public Iterator {
}
bool Valid() const override { return db_iter_->Valid(); }
void SeekToFirst() override { db_iter_->SeekToFirst(); }
void SeekToLast() override { db_iter_->SeekToLast(); }
void SeekToFirst() override {
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return;
}
db_iter_->SeekToFirst();
}
void SeekToLast() override {
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return;
}
db_iter_->SeekToLast();
}
// 'target' does not contain timestamp, even if user timestamp feature is
// enabled.
void Seek(const Slice& target) override {
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return;
}
MaybeAutoRefresh(true /* is_seek */, DBIter::kForward);
db_iter_->Seek(target);
}
void SeekForPrev(const Slice& target) override {
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return;
}
MaybeAutoRefresh(true /* is_seek */, DBIter::kReverse);
db_iter_->SeekForPrev(target);
}
void Next() override {
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return;
}
db_iter_->Next();
MaybeAutoRefresh(false /* is_seek */, DBIter::kForward);
}
void Prev() override {
if (!EnsureInternalIteratorInitialized(nullptr).ok()) {
return;
}
db_iter_->Prev();
MaybeAutoRefresh(false /* is_seek */, DBIter::kReverse);
}
@@ -96,12 +126,13 @@ class ArenaWrappedDBIter : public Iterator {
Status Refresh() override;
Status Refresh(const Snapshot*) override;
bool PrepareValue() override { return db_iter_->PrepareValue(); }
void Prepare(const MultiScanArgs& scan_opts) override {
db_iter_->Prepare(scan_opts);
bool PrepareValue() override {
return EnsureInternalIteratorInitialized(nullptr).ok() &&
db_iter_->PrepareValue();
}
void Prepare(const MultiScanArgs& scan_opts) override;
// FIXME: we could just pass SV in for mutable cf option, version and version
// number, but this is used by SstFileReader which does not have a SV.
void Init(Env* env, const ReadOptions& read_options,
@@ -110,27 +141,53 @@ class ArenaWrappedDBIter : public Iterator {
const SequenceNumber& sequence, uint64_t version_number,
ReadCallback* read_callback, ColumnFamilyHandleImpl* cfh,
bool expose_blob_index, bool allow_refresh,
ReadOnlyMemTable* active_mem);
ReadOnlyMemTable* active_mem, DBImpl* db_impl = nullptr,
ColumnFamilyData* cfd = nullptr);
// Store some parameters so we can refresh the iterator at a later point
// with these same params
void StoreRefreshInfo(ColumnFamilyHandleImpl* cfh,
ReadCallback* read_callback, bool expose_blob_index) {
cfh_ = cfh;
// Store parameters used only by explicit/auto-refresh.
void StoreRefreshInfo(ReadCallback* read_callback, bool expose_blob_index) {
read_callback_ = read_callback;
expose_blob_index_ = expose_blob_index;
}
void StoreDeferredInitInfo(DBImpl* db_impl, ColumnFamilyData* cfd,
SuperVersion* sv, const SequenceNumber& sequence,
bool allow_mark_memtable_for_flush) {
assert(cfd != nullptr);
db_impl_ = db_impl;
cfd->Ref();
cfd_ref_ = ColumnFamilyDataRef(cfd, ColumnFamilyDataUnrefDeleter{db_impl});
deferred_cfd_ = cfd;
deferred_sv_ = sv;
sequence_ = sequence;
allow_mark_memtable_for_flush_ = allow_mark_memtable_for_flush;
}
private:
Status EnsureInternalIteratorInitialized(const MultiScanArgs* scan_opts);
void SetIterUnderDBIterImpl(InternalIterator* iter) {
db_iter_->SetIter(iter);
internal_iter_initialized_ = true;
}
void CleanupDeferredSuperVersion();
void DestroyDBIter();
void DestroyDBIterAndArena();
void DoRefresh(const Snapshot* snapshot, uint64_t sv_number);
void MaybeAutoRefresh(bool is_seek, DBIter::Direction direction);
DBIter* db_iter_ = nullptr;
Arena arena_;
uint64_t sv_number_;
ColumnFamilyHandleImpl* cfh_ = nullptr;
uint64_t sv_number_ = 0;
DBImpl* db_impl_ = nullptr;
ColumnFamilyDataRef cfd_ref_{nullptr, ColumnFamilyDataUnrefDeleter{}};
ColumnFamilyData* deferred_cfd_ = nullptr;
SuperVersion* deferred_sv_ = nullptr;
SequenceNumber sequence_ = kMaxSequenceNumber;
bool internal_iter_initialized_ = false;
bool prepare_called_ = false;
ReadOptions read_options_;
ReadCallback* read_callback_;
ReadOptions child_read_options_;
ReadCallback* read_callback_ = nullptr;
bool expose_blob_index_ = false;
bool allow_refresh_ = true;
bool allow_mark_memtable_for_flush_ = true;
+3
View File
@@ -22,6 +22,7 @@
#include "logging/logging.h"
#include "options/cf_options.h"
#include "options/options_helper.h"
#include "rocksdb/file_system.h"
#include "rocksdb/slice.h"
#include "rocksdb/status.h"
#include "test_util/sync_point.h"
@@ -200,6 +201,8 @@ Status BlobFileBuilder::OpenBlobFileIfNeeded() {
{
assert(file_options_);
fo_copy = *file_options_;
fo_copy.open_contract = FileOpenContract::kNoReopenForWrite |
FileOpenContract::kNoReadersWhileOpenForWrite;
fo_copy.write_hint = write_hint_;
Status s = NewWritableFile(fs_, blob_file_path, &file, fo_copy);
+1
View File
@@ -123,6 +123,7 @@ Status BlobFileCache::OpenBlobFileReaderUncached(
Statistics* const statistics = immutable_options_->stats;
RecordTick(statistics, NO_FILE_OPENS);
assert(file_options_);
Status s = BlobFileReader::Create(
*immutable_options_, read_options, *file_options_, column_family_id_,
blob_file_read_hist_, blob_file_number, io_tracer_,
+5 -2
View File
@@ -24,6 +24,7 @@
#include "file/read_write_util.h"
#include "file/writable_file_writer.h"
#include "logging/logging.h"
#include "rocksdb/file_system.h"
#include "util/aligned_buffer.h"
#include "util/compression.h"
#include "util/mutexlock.h"
@@ -199,7 +200,9 @@ Status BlobFilePartitionManager::OpenNewBlobFile(Partition* partition,
}
std::unique_ptr<FSWritableFile> file;
Status s = NewWritableFile(fs_, blob_file_path, &file, file_options_);
FileOptions writer_file_options = file_options_;
writer_file_options.open_contract = FileOpenContract::kNoReopenForWrite;
Status s = NewWritableFile(fs_, blob_file_path, &file, writer_file_options);
if (!s.ok()) {
RemoveFilePartitionMapping(blob_file_number);
return s;
@@ -208,7 +211,7 @@ Status BlobFilePartitionManager::OpenNewBlobFile(Partition* partition,
const bool perform_data_verification =
checksum_handoff_file_types_.Contains(FileType::kBlobFile);
auto file_writer = std::make_unique<WritableFileWriter>(
std::move(file), blob_file_path, file_options_, clock_, io_tracer_,
std::move(file), blob_file_path, writer_file_options, clock_, io_tracer_,
statistics_, Histograms::BLOB_DB_BLOB_FILE_WRITE_MICROS, listeners_,
file_checksum_gen_factory_, perform_data_verification);
+35 -31
View File
@@ -12,6 +12,7 @@
#include "db/blob/blob_log_format.h"
#include "file/file_prefetch_buffer.h"
#include "file/filename.h"
#include "file/read_write_util.h"
#include "monitoring/statistics_impl.h"
#include "options/cf_options.h"
#include "rocksdb/file_system.h"
@@ -20,6 +21,7 @@
#include "table/format.h"
#include "table/multiget_context.h"
#include "test_util/sync_point.h"
#include "util/aligned_buffer.h"
#include "util/compression.h"
#include "util/stop_watch.h"
@@ -105,24 +107,6 @@ Status BlobFileReader::OpenFile(
constexpr IODebugContext* dbg = nullptr;
{
TEST_SYNC_POINT("BlobFileReader::OpenFile:GetFileSize");
const Status s =
fs->GetFileSize(blob_file_path, IOOptions(), file_size, dbg);
if (!s.ok()) {
return s;
}
}
if (!skip_footer_size_check &&
*file_size < BlobLogHeader::kSize + BlobLogFooter::kSize) {
return Status::Corruption("Malformed blob file");
}
if (skip_footer_size_check && *file_size < BlobLogHeader::kSize) {
return Status::Corruption("Malformed blob file");
}
std::unique_ptr<FSRandomAccessFile> file;
FileOptions reader_file_opts = file_opts;
@@ -142,6 +126,24 @@ Status BlobFileReader::OpenFile(
assert(file);
{
Status s = GetFileSizeFromOpenFileOrPath(
file.get(), fs, blob_file_path, file_size, dbg,
FileSizeFallback::kNotSupportedOnly,
[]() { TEST_SYNC_POINT("BlobFileReader::OpenFile:GetFileSize"); });
if (!s.ok()) {
return s;
}
}
if (!skip_footer_size_check &&
*file_size < BlobLogHeader::kSize + BlobLogFooter::kSize) {
return Status::Corruption("Malformed blob file");
}
if (skip_footer_size_check && *file_size < BlobLogHeader::kSize) {
return Status::Corruption("Malformed blob file");
}
if (immutable_options.advise_random_on_open) {
file->Hint(FSRandomAccessFile::kRandom);
}
@@ -165,7 +167,7 @@ Status BlobFileReader::ReadHeader(const RandomAccessFileReader* file_reader,
Slice header_slice;
Buffer buf;
AlignedBuf aligned_buf;
AlignedBuffer direct_io_buffer;
{
TEST_SYNC_POINT("BlobFileReader::ReadHeader:ReadFromFile");
@@ -175,7 +177,7 @@ Status BlobFileReader::ReadHeader(const RandomAccessFileReader* file_reader,
const Status s =
ReadFromFile(file_reader, read_options, read_offset, read_size,
statistics, &header_slice, &buf, &aligned_buf);
statistics, &header_slice, &buf, &direct_io_buffer);
if (!s.ok()) {
return s;
}
@@ -216,7 +218,7 @@ Status BlobFileReader::ReadFooter(const RandomAccessFileReader* file_reader,
Slice footer_slice;
Buffer buf;
AlignedBuf aligned_buf;
AlignedBuffer direct_io_buffer;
{
TEST_SYNC_POINT("BlobFileReader::ReadFooter:ReadFromFile");
@@ -226,7 +228,7 @@ Status BlobFileReader::ReadFooter(const RandomAccessFileReader* file_reader,
const Status s =
ReadFromFile(file_reader, read_options, read_offset, read_size,
statistics, &footer_slice, &buf, &aligned_buf);
statistics, &footer_slice, &buf, &direct_io_buffer);
if (!s.ok()) {
return s;
}
@@ -257,10 +259,11 @@ Status BlobFileReader::ReadFromFile(const RandomAccessFileReader* file_reader,
const ReadOptions& read_options,
uint64_t read_offset, size_t read_size,
Statistics* statistics, Slice* slice,
Buffer* buf, AlignedBuf* aligned_buf) {
Buffer* buf,
AlignedBuffer* direct_io_buffer) {
assert(slice);
assert(buf);
assert(aligned_buf);
assert(direct_io_buffer);
assert(file_reader);
@@ -277,15 +280,15 @@ Status BlobFileReader::ReadFromFile(const RandomAccessFileReader* file_reader,
if (file_reader->use_direct_io()) {
constexpr char* scratch = nullptr;
AlignedBufferAllocationContext direct_io_context{direct_io_buffer};
s = file_reader->Read(io_options, read_offset, read_size, slice, scratch,
aligned_buf, &dbg);
&direct_io_context, &dbg);
} else {
buf->reset(new char[read_size]);
constexpr AlignedBuf* aligned_scratch = nullptr;
s = file_reader->Read(io_options, read_offset, read_size, slice, buf->get(),
aligned_scratch, &dbg);
nullptr, &dbg);
}
if (!s.ok()) {
@@ -349,7 +352,7 @@ Status BlobFileReader::GetBlob(
Slice record_slice;
Buffer buf;
AlignedBuf aligned_buf;
AlignedBuffer direct_io_buffer;
bool prefetched = false;
@@ -379,7 +382,7 @@ Status BlobFileReader::GetBlob(
const Status s =
ReadFromFile(file_reader_.get(), read_options, record_offset,
static_cast<size_t>(record_size), statistics_,
&record_slice, &buf, &aligned_buf);
&record_slice, &buf, &direct_io_buffer);
if (!s.ok()) {
return s;
}
@@ -477,7 +480,7 @@ void BlobFileReader::MultiGetBlob(
}
Buffer buf;
AlignedBuf aligned_buf;
AlignedBuffer direct_io_buffer;
Status s;
bool direct_io = file_reader_->use_direct_io();
@@ -500,8 +503,9 @@ void BlobFileReader::MultiGetBlob(
IODebugContext dbg;
s = file_reader_->PrepareIOOptions(read_options, opts, &dbg);
if (s.ok()) {
AlignedBufferAllocationContext direct_io_context{&direct_io_buffer};
s = file_reader_->MultiRead(opts, read_reqs.data(), read_reqs.size(),
direct_io ? &aligned_buf : nullptr, &dbg);
&direct_io_context, &dbg);
}
if (!s.ok()) {
for (auto& req : read_reqs) {
+1 -1
View File
@@ -108,7 +108,7 @@ class BlobFileReader {
const ReadOptions& read_options,
uint64_t read_offset, size_t read_size,
Statistics* statistics, Slice* slice, Buffer* buf,
AlignedBuf* aligned_buf);
AlignedBuffer* direct_io_buffer);
static Status VerifyBlob(const Slice& record_slice, const Slice& user_key,
uint64_t value_size);
+368 -1
View File
@@ -7,10 +7,12 @@
#include <cassert>
#include <string>
#include <utility>
#include "db/blob/blob_contents.h"
#include "db/blob/blob_log_format.h"
#include "db/blob/blob_log_writer.h"
#include "env/composite_env_wrapper.h"
#include "env/mock_env.h"
#include "file/filename.h"
#include "file/read_write_util.h"
@@ -136,6 +138,167 @@ class BlobFileReaderTest : public testing::Test {
std::unique_ptr<Env> mock_env_;
};
namespace {
// Returns a stale path-level size for one target blob file while leaving the
// opened file handle untouched. This lets the test verify
// BlobFileReader::Create prefers the opened handle's size when the path stat
// lags behind.
class StalePathSizeFileSystem : public FileSystemWrapper {
public:
static const char* kClassName() { return "StalePathSizeFileSystem"; }
StalePathSizeFileSystem(const std::shared_ptr<FileSystem>& target,
std::string stale_path)
: FileSystemWrapper(target), stale_path_(std::move(stale_path)) {}
const char* Name() const override { return kClassName(); }
IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
uint64_t* file_size, IODebugContext* dbg) override {
if (fname == stale_path_) {
*file_size = 0;
return IOStatus::OK();
}
return FileSystemWrapper::GetFileSize(fname, options, file_size, dbg);
}
private:
const std::string stale_path_;
};
// Makes opened blob file handles fail GetFileSize() with a hard error so the
// reader path can verify that such errors are propagated directly.
class FailingOpenFileSizeRandomAccessFile
: public FSRandomAccessFileOwnerWrapper {
public:
explicit FailingOpenFileSizeRandomAccessFile(
std::unique_ptr<FSRandomAccessFile>&& target)
: FSRandomAccessFileOwnerWrapper(std::move(target)) {}
IOStatus GetFileSize(uint64_t* /*result*/) override {
return IOStatus::IOError("open file size failed");
}
};
// Makes opened blob file handles report GetFileSize() as unsupported so the
// reader path exercises its path-level fallback logic.
class NotSupportedOpenFileSizeRandomAccessFile
: public FSRandomAccessFileOwnerWrapper {
public:
explicit NotSupportedOpenFileSizeRandomAccessFile(
std::unique_ptr<FSRandomAccessFile>&& target)
: FSRandomAccessFileOwnerWrapper(std::move(target)) {}
IOStatus GetFileSize(uint64_t* /*result*/) override {
return IOStatus::NotSupported("open file size not supported");
}
};
class OpenFileSizeFileSystem : public FileSystemWrapper {
public:
OpenFileSizeFileSystem(const std::shared_ptr<FileSystem>& target,
std::string target_path)
: FileSystemWrapper(target), target_path_(std::move(target_path)) {}
IOStatus NewRandomAccessFile(const std::string& fname,
const FileOptions& options,
std::unique_ptr<FSRandomAccessFile>* result,
IODebugContext* dbg) override {
std::unique_ptr<FSRandomAccessFile> file;
IOStatus s =
FileSystemWrapper::NewRandomAccessFile(fname, options, &file, dbg);
if (!s.ok()) {
return IOStatus(std::move(s));
}
if (fname == target_path_) {
*result = WrapTargetFile(std::move(file));
} else {
*result = std::move(file);
}
return IOStatus::OK();
}
protected:
virtual std::unique_ptr<FSRandomAccessFile> WrapTargetFile(
std::unique_ptr<FSRandomAccessFile>&& file) = 0;
private:
const std::string target_path_;
};
// Wraps the target blob file in FailingOpenFileSizeRandomAccessFile while
// leaving all other files alone.
class FailingOpenFileSizeFileSystem : public OpenFileSizeFileSystem {
public:
static const char* kClassName() { return "FailingOpenFileSizeFileSystem"; }
FailingOpenFileSizeFileSystem(const std::shared_ptr<FileSystem>& target,
std::string target_path)
: OpenFileSizeFileSystem(target, std::move(target_path)) {}
const char* Name() const override { return kClassName(); }
private:
std::unique_ptr<FSRandomAccessFile> WrapTargetFile(
std::unique_ptr<FSRandomAccessFile>&& file) override {
return std::unique_ptr<FSRandomAccessFile>(
new FailingOpenFileSizeRandomAccessFile(std::move(file)));
}
};
// Wraps the target blob file in NotSupportedOpenFileSizeRandomAccessFile while
// leaving all other files alone.
class NotSupportedOpenFileSizeFileSystem : public OpenFileSizeFileSystem {
public:
static const char* kClassName() {
return "NotSupportedOpenFileSizeFileSystem";
}
NotSupportedOpenFileSizeFileSystem(const std::shared_ptr<FileSystem>& target,
std::string target_path)
: OpenFileSizeFileSystem(target, std::move(target_path)) {}
const char* Name() const override { return kClassName(); }
private:
std::unique_ptr<FSRandomAccessFile> WrapTargetFile(
std::unique_ptr<FSRandomAccessFile>&& file) override {
return std::unique_ptr<FSRandomAccessFile>(
new NotSupportedOpenFileSizeRandomAccessFile(std::move(file)));
}
};
// Forces the target blob file onto the NotSupported open-handle-size branch and
// then fails the fallback path-level GetFileSize() call.
class FailingFallbackPathSizeFileSystem
: public NotSupportedOpenFileSizeFileSystem {
public:
static const char* kClassName() {
return "FailingFallbackPathSizeFileSystem";
}
FailingFallbackPathSizeFileSystem(const std::shared_ptr<FileSystem>& target,
std::string target_path)
: NotSupportedOpenFileSizeFileSystem(target, target_path),
target_path_(std::move(target_path)) {}
const char* Name() const override { return kClassName(); }
IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
uint64_t* file_size, IODebugContext* dbg) override {
if (fname == target_path_) {
return IOStatus::IOError("fallback path size failed");
}
return FileSystemWrapper::GetFileSize(fname, options, file_size, dbg);
}
private:
const std::string target_path_;
};
} // namespace
TEST_F(BlobFileReaderTest, CreateReaderAndGetBlob) {
Options options;
options.env = mock_env_.get();
@@ -428,6 +591,211 @@ TEST_F(BlobFileReaderTest, CreateReaderAndGetBlob) {
}
}
// Verifies Create() prefers the file size reported by the opened handle over a
// stale path-level size query by returning a readable reader even when the path
// stat intentionally reports 0 for the blob file.
TEST_F(BlobFileReaderTest, CreateReaderUsesOpenedFileSizeWhenPathSizeIsStale) {
const std::string db_path = test::PerThreadDBPath(
mock_env_.get(),
"BlobFileReaderTest_CreateReaderUsesOpenedFileSizeWhenPathSizeIsStale");
constexpr uint64_t blob_file_number = 1;
const std::string blob_file_path = BlobFileName(db_path, blob_file_number);
auto stale_size_fs = std::make_shared<StalePathSizeFileSystem>(
mock_env_->GetFileSystem(), blob_file_path);
CompositeEnvWrapper stale_size_env(mock_env_.get(), stale_size_fs);
Options options;
options.env = &stale_size_env;
options.cf_paths.emplace_back(db_path, 0);
options.enable_blob_files = true;
ImmutableOptions immutable_options(options);
constexpr uint32_t column_family_id = 1;
constexpr bool has_ttl = false;
constexpr ExpirationRange expiration_range;
constexpr char key[] = "key";
constexpr char blob[] = "blob";
uint64_t blob_offset = 0;
uint64_t blob_size = 0;
WriteBlobFile(immutable_options, column_family_id, has_ttl, expiration_range,
expiration_range, blob_file_number, key, blob, kNoCompression,
&blob_offset, &blob_size);
std::unique_ptr<BlobFileReader> reader;
ReadOptions read_options;
read_options.verify_checksums = false;
ASSERT_OK(BlobFileReader::Create(
immutable_options, read_options, FileOptions(), column_family_id,
/*blob_file_read_hist=*/nullptr, blob_file_number, nullptr /*IOTracer*/,
&reader));
ASSERT_NE(reader, nullptr);
std::unique_ptr<BlobContents> value;
uint64_t bytes_read = 0;
ASSERT_OK(reader->GetBlob(read_options, key, blob_offset, blob_size,
kNoCompression,
/*prefetch_buffer=*/nullptr,
/*allocator=*/nullptr, &value, &bytes_read));
ASSERT_NE(value, nullptr);
ASSERT_EQ(value->data(), Slice(blob));
ASSERT_EQ(bytes_read, blob_size);
}
// Verifies Create() falls back to the path-level GetFileSize() call when the
// opened file handle reports that size queries are unsupported.
TEST_F(BlobFileReaderTest,
CreateReaderFallsBackToPathSizeWhenOpenedFileSizeNotSupported) {
const std::string db_path = test::PerThreadDBPath(
mock_env_.get(),
"BlobFileReaderTest_"
"CreateReaderFallsBackToPathSizeWhenOpenedFileSizeNotSupported");
constexpr uint64_t blob_file_number = 1;
const std::string blob_file_path = BlobFileName(db_path, blob_file_number);
auto not_supported_open_file_size_fs =
std::make_shared<NotSupportedOpenFileSizeFileSystem>(
mock_env_->GetFileSystem(), blob_file_path);
CompositeEnvWrapper not_supported_open_file_size_env(
mock_env_.get(), not_supported_open_file_size_fs);
Options options;
options.env = &not_supported_open_file_size_env;
options.cf_paths.emplace_back(db_path, 0);
options.enable_blob_files = true;
ImmutableOptions immutable_options(options);
constexpr uint32_t column_family_id = 1;
constexpr bool has_ttl = false;
constexpr ExpirationRange expiration_range;
constexpr char key[] = "key";
constexpr char blob[] = "blob";
uint64_t blob_offset = 0;
uint64_t blob_size = 0;
WriteBlobFile(immutable_options, column_family_id, has_ttl, expiration_range,
expiration_range, blob_file_number, key, blob, kNoCompression,
&blob_offset, &blob_size);
std::unique_ptr<BlobFileReader> reader;
ReadOptions read_options;
read_options.verify_checksums = false;
ASSERT_OK(BlobFileReader::Create(
immutable_options, read_options, FileOptions(), column_family_id,
/*blob_file_read_hist=*/nullptr, blob_file_number, nullptr /*IOTracer*/,
&reader));
ASSERT_NE(reader, nullptr);
std::unique_ptr<BlobContents> value;
uint64_t bytes_read = 0;
ASSERT_OK(reader->GetBlob(read_options, key, blob_offset, blob_size,
kNoCompression,
/*prefetch_buffer=*/nullptr,
/*allocator=*/nullptr, &value, &bytes_read));
ASSERT_NE(value, nullptr);
ASSERT_EQ(value->data(), Slice(blob));
ASSERT_EQ(bytes_read, blob_size);
}
// Verifies Create() propagates a real error from the opened file handle's
// GetFileSize() rather than masking it with any path-level fallback.
TEST_F(BlobFileReaderTest, CreateReaderPropagatesOpenedFileSizeError) {
const std::string db_path = test::PerThreadDBPath(
mock_env_.get(),
"BlobFileReaderTest_CreateReaderPropagatesOpenedFileSizeError");
constexpr uint64_t blob_file_number = 1;
const std::string blob_file_path = BlobFileName(db_path, blob_file_number);
auto failing_open_file_size_fs =
std::make_shared<FailingOpenFileSizeFileSystem>(
mock_env_->GetFileSystem(), blob_file_path);
CompositeEnvWrapper failing_open_file_size_env(mock_env_.get(),
failing_open_file_size_fs);
Options options;
options.env = &failing_open_file_size_env;
options.cf_paths.emplace_back(db_path, 0);
options.enable_blob_files = true;
ImmutableOptions immutable_options(options);
constexpr uint32_t column_family_id = 1;
constexpr bool has_ttl = false;
constexpr ExpirationRange expiration_range;
constexpr char key[] = "key";
constexpr char blob[] = "blob";
uint64_t blob_offset = 0;
uint64_t blob_size = 0;
WriteBlobFile(immutable_options, column_family_id, has_ttl, expiration_range,
expiration_range, blob_file_number, key, blob, kNoCompression,
&blob_offset, &blob_size);
std::unique_ptr<BlobFileReader> reader;
ReadOptions read_options;
Status s = BlobFileReader::Create(
immutable_options, read_options, FileOptions(), column_family_id,
/*blob_file_read_hist=*/nullptr, blob_file_number, nullptr /*IOTracer*/,
&reader);
ASSERT_TRUE(s.IsIOError());
ASSERT_EQ(reader, nullptr);
}
// Verifies Create() also propagates errors from the path-level fallback size
// query when the opened file handle only reports NotSupported for GetFileSize.
TEST_F(
BlobFileReaderTest,
CreateReaderPropagatesFallbackPathSizeErrorWhenOpenedFileSizeNotSupported) {
const std::string db_path =
test::PerThreadDBPath(mock_env_.get(),
"BlobFileReaderTest_"
"CreateReaderPropagatesFallbackPathSizeErrorWhenOpe"
"nedFileSizeNotSupported");
constexpr uint64_t blob_file_number = 1;
const std::string blob_file_path = BlobFileName(db_path, blob_file_number);
auto failing_fallback_path_size_fs =
std::make_shared<FailingFallbackPathSizeFileSystem>(
mock_env_->GetFileSystem(), blob_file_path);
CompositeEnvWrapper failing_fallback_path_size_env(
mock_env_.get(), failing_fallback_path_size_fs);
Options options;
options.env = &failing_fallback_path_size_env;
options.cf_paths.emplace_back(db_path, 0);
options.enable_blob_files = true;
ImmutableOptions immutable_options(options);
constexpr uint32_t column_family_id = 1;
constexpr bool has_ttl = false;
constexpr ExpirationRange expiration_range;
constexpr char key[] = "key";
constexpr char blob[] = "blob";
uint64_t blob_offset = 0;
uint64_t blob_size = 0;
WriteBlobFile(immutable_options, column_family_id, has_ttl, expiration_range,
expiration_range, blob_file_number, key, blob, kNoCompression,
&blob_offset, &blob_size);
std::unique_ptr<BlobFileReader> reader;
ReadOptions read_options;
Status s = BlobFileReader::Create(
immutable_options, read_options, FileOptions(), column_family_id,
/*blob_file_read_hist=*/nullptr, blob_file_number, nullptr /*IOTracer*/,
&reader);
ASSERT_TRUE(s.IsIOError());
ASSERT_EQ(reader, nullptr);
}
TEST_F(BlobFileReaderTest, Malformed) {
// Write a blob file consisting of nothing but a header, and make sure we
// detect the error when we open it for reading
@@ -849,7 +1217,6 @@ class BlobFileReaderIOErrorTest
INSTANTIATE_TEST_CASE_P(BlobFileReaderTest, BlobFileReaderIOErrorTest,
::testing::ValuesIn(std::vector<std::string>{
"BlobFileReader::OpenFile:GetFileSize",
"BlobFileReader::OpenFile:NewRandomAccessFile",
"BlobFileReader::ReadHeader:ReadFromFile",
"BlobFileReader::ReadFooter:ReadFromFile",
+1
View File
@@ -74,6 +74,7 @@ Status BlobSource::GetBlobFromCache(
assert(cached_blob->GetValue());
PERF_COUNTER_ADD(blob_cache_hit_count, 1);
PERF_COUNTER_ADD(blob_cache_read_byte, cached_blob->GetValue()->size());
RecordTick(statistics_, BLOB_DB_CACHE_HIT);
RecordTick(statistics_, BLOB_DB_CACHE_BYTES_READ,
cached_blob->GetValue()->size());
+10
View File
@@ -229,6 +229,7 @@ TEST_F(BlobSourceTest, GetBlobsFromCache) {
// Retrieved the blob cache num_blobs * 3 times via TEST_BlobInCache,
// GetBlob, and TEST_BlobInCache.
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count, 0);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte, 0);
ASSERT_EQ((int)get_perf_context()->blob_read_count, num_blobs);
ASSERT_EQ((int)get_perf_context()->blob_read_byte, total_bytes);
ASSERT_GE((int)get_perf_context()->blob_checksum_time, 0);
@@ -262,6 +263,8 @@ TEST_F(BlobSourceTest, GetBlobsFromCache) {
blob_bytes += blob_sizes[i];
total_bytes += bytes_read;
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count, i);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte,
blob_bytes - blob_sizes[i]);
ASSERT_EQ((int)get_perf_context()->blob_read_count, i + 1);
ASSERT_EQ((int)get_perf_context()->blob_read_byte, total_bytes);
@@ -269,11 +272,13 @@ TEST_F(BlobSourceTest, GetBlobsFromCache) {
blob_offsets[i]));
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count, i + 1);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte, blob_bytes);
ASSERT_EQ((int)get_perf_context()->blob_read_count, i + 1);
ASSERT_EQ((int)get_perf_context()->blob_read_byte, total_bytes);
}
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count, num_blobs);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte, blob_bytes);
ASSERT_EQ((int)get_perf_context()->blob_read_count, num_blobs);
ASSERT_EQ((int)get_perf_context()->blob_read_byte, total_bytes);
@@ -312,6 +317,7 @@ TEST_F(BlobSourceTest, GetBlobsFromCache) {
// Retrieved the blob cache num_blobs * 3 times via TEST_BlobInCache,
// GetBlob, and TEST_BlobInCache.
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count, num_blobs * 3);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte, blob_bytes * 3);
ASSERT_EQ((int)get_perf_context()->blob_read_count, 0); // without i/o
ASSERT_EQ((int)get_perf_context()->blob_read_byte, 0); // without i/o
@@ -690,6 +696,8 @@ TEST_F(BlobSourceTest, MultiGetBlobsFromMultiFiles) {
// TEST_BlobInCache.
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count,
num_blobs * blob_files);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte,
blob_value_bytes * blob_files);
ASSERT_EQ((int)get_perf_context()->blob_read_count,
num_blobs * blob_files); // blocking i/o
ASSERT_EQ((int)get_perf_context()->blob_read_byte,
@@ -750,6 +758,8 @@ TEST_F(BlobSourceTest, MultiGetBlobsFromMultiFiles) {
// via MultiGetBlob and TEST_BlobInCache.
ASSERT_EQ((int)get_perf_context()->blob_cache_hit_count,
num_blobs * blob_files * 2);
ASSERT_EQ((int)get_perf_context()->blob_cache_read_byte,
blob_value_bytes * blob_files * 2);
ASSERT_EQ((int)get_perf_context()->blob_read_count,
0); // blocking i/o
ASSERT_EQ((int)get_perf_context()->blob_read_byte,
+229
View File
@@ -10,11 +10,14 @@
#include <algorithm>
#include <array>
#include <functional>
#include <limits>
#include <mutex>
#include <set>
#include <sstream>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "cache/compressed_secondary_cache.h"
@@ -26,10 +29,12 @@
#include "db/db_test_util.h"
#include "db/db_with_timestamp_test_util.h"
#include "db/wide/wide_column_test_util.h"
#include "env/composite_env_wrapper.h"
#include "file/filename.h"
#include "file/random_access_file_reader.h"
#include "port/stack_trace.h"
#include "rocksdb/convenience.h"
#include "rocksdb/file_system.h"
#include "rocksdb/trace_reader_writer.h"
#include "rocksdb/trace_record.h"
#include "rocksdb/utilities/replayer.h"
@@ -101,6 +106,206 @@ class RecordingBlobDirectWritePartitionStrategy
std::vector<Call> calls_;
};
namespace {
// Matches BlobDB file names so the test filesystem can special-case only active
// blob files without changing unrelated file opens.
bool IsBlobFilePath(const std::string& fname) {
const size_t basename_pos = fname.find_last_of("/\\");
const std::string basename = basename_pos == std::string::npos
? fname
: fname.substr(basename_pos + 1);
uint64_t file_number = 0;
FileType file_type = kWalFile;
return ParseFileName(basename, &file_number, &file_type) &&
file_type == kBlobFile;
}
// Keeps track of when an active blob file stops being writer-visible so the
// test filesystem can model current-size visibility only while the file remains
// active.
class ActiveBlobVisibilityWritableFile : public FSWritableFileOwnerWrapper {
public:
ActiveBlobVisibilityWritableFile(std::unique_ptr<FSWritableFile>&& target,
std::function<void()> on_close)
: FSWritableFileOwnerWrapper(std::move(target)),
on_close_(std::move(on_close)) {}
~ActiveBlobVisibilityWritableFile() override { DeactivateOnce(); }
IOStatus Close(const IOOptions& options, IODebugContext* dbg) override {
IOStatus s = target()->Close(options, dbg);
DeactivateOnce();
return s;
}
private:
void DeactivateOnce() {
if (on_close_) {
on_close_();
on_close_ = nullptr;
}
}
std::function<void()> on_close_;
};
// Simulates a remote readable file whose GetFileSize() either exposes the live
// on-disk size for default-contract active blobs or hides it for
// append-only-no-readers files.
class ActiveBlobVisibilityRandomAccessFile
: public FSRandomAccessFileOwnerWrapper {
public:
ActiveBlobVisibilityRandomAccessFile(
std::unique_ptr<FSRandomAccessFile>&& target, bool expose_current_size,
std::shared_ptr<FileSystem> underlying_fs, std::string fname)
: FSRandomAccessFileOwnerWrapper(std::move(target)),
expose_current_size_(expose_current_size),
underlying_fs_(std::move(underlying_fs)),
fname_(std::move(fname)) {}
IOStatus GetFileSize(uint64_t* result) override {
if (!expose_current_size_) {
*result = 0;
return IOStatus::OK();
}
// Delegate to the underlying FS path-level GetFileSize (bypassing the
// wrapper that reports 0 for active blobs) to simulate a remote FS that
// exposes the current file size through the open handle.
return underlying_fs_->GetFileSize(fname_, IOOptions(), result,
/*dbg=*/nullptr);
}
private:
const bool expose_current_size_;
std::shared_ptr<FileSystem> underlying_fs_;
std::string fname_;
};
// Models a remote filesystem where active direct-write blobs are only
// reader-visible when the writer does not promise no concurrent readers.
class RemoteBlobVisibilityFileSystem : public FileSystemWrapper {
public:
explicit RemoteBlobVisibilityFileSystem(const std::shared_ptr<FileSystem>& fs)
: FileSystemWrapper(fs) {}
static const char* kClassName() { return "RemoteBlobVisibilityFileSystem"; }
const char* Name() const override { return kClassName(); }
IOStatus NewWritableFile(const std::string& fname, const FileOptions& opts,
std::unique_ptr<FSWritableFile>* result,
IODebugContext* dbg) override {
std::unique_ptr<FSWritableFile> file;
IOStatus s = target()->NewWritableFile(fname, opts, &file, dbg);
if (!s.ok() || !IsBlobFilePath(fname)) {
*result = std::move(file);
return IOStatus(std::move(s));
}
const bool has_no_reopen_for_write_contract = HasFileOpenContract(
opts.open_contract, FileOpenContract::kNoReopenForWrite);
const bool has_no_readers_while_open_for_write_contract =
HasFileOpenContract(opts.open_contract,
FileOpenContract::kNoReadersWhileOpenForWrite);
{
std::lock_guard<std::mutex> lock(mu_);
saw_no_reopen_for_write_writer_contract_ |=
has_no_reopen_for_write_contract;
saw_no_readers_while_open_for_write_writer_contract_ |=
has_no_readers_while_open_for_write_contract;
if (!has_no_readers_while_open_for_write_contract) {
active_blob_paths_.insert(fname);
}
}
if (has_no_readers_while_open_for_write_contract) {
*result = std::move(file);
return IOStatus::OK();
}
result->reset(new ActiveBlobVisibilityWritableFile(
std::move(file), [this, tracked_path = std::string(fname)]() {
DeactivateBlobPath(tracked_path);
}));
return IOStatus::OK();
}
IOStatus NewRandomAccessFile(const std::string& fname,
const FileOptions& opts,
std::unique_ptr<FSRandomAccessFile>* result,
IODebugContext* dbg) override {
std::unique_ptr<FSRandomAccessFile> file;
IOStatus s = target()->NewRandomAccessFile(fname, opts, &file, dbg);
if (!s.ok() || !IsBlobFilePath(fname)) {
*result = std::move(file);
return IOStatus(std::move(s));
}
const bool has_no_readers_while_open_for_write_contract =
HasFileOpenContract(opts.open_contract,
FileOpenContract::kNoReadersWhileOpenForWrite);
const bool active_blob = IsActiveBlobPath(fname);
{
std::lock_guard<std::mutex> lock(mu_);
saw_no_readers_while_open_for_write_reader_contract_ |=
has_no_readers_while_open_for_write_contract;
}
if (!active_blob) {
*result = std::move(file);
return IOStatus::OK();
}
result->reset(new ActiveBlobVisibilityRandomAccessFile(
std::move(file), !has_no_readers_while_open_for_write_contract, target_,
fname));
return IOStatus::OK();
}
IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
uint64_t* file_size, IODebugContext* dbg) override {
if (IsActiveBlobPath(fname)) {
*file_size = 0;
return IOStatus::OK();
}
return target()->GetFileSize(fname, options, file_size, dbg);
}
bool SawNoReopenForWriteWriterContract() const {
std::lock_guard<std::mutex> lock(mu_);
return saw_no_reopen_for_write_writer_contract_;
}
bool SawNoReadersWhileOpenForWriteWriterContract() const {
std::lock_guard<std::mutex> lock(mu_);
return saw_no_readers_while_open_for_write_writer_contract_;
}
bool SawNoReadersWhileOpenForWriteReaderContract() const {
std::lock_guard<std::mutex> lock(mu_);
return saw_no_readers_while_open_for_write_reader_contract_;
}
private:
void DeactivateBlobPath(const std::string& fname) {
std::lock_guard<std::mutex> lock(mu_);
active_blob_paths_.erase(fname);
}
bool IsActiveBlobPath(const std::string& fname) const {
std::lock_guard<std::mutex> lock(mu_);
return active_blob_paths_.find(fname) != active_blob_paths_.end();
}
mutable std::mutex mu_;
bool saw_no_reopen_for_write_writer_contract_ = false;
bool saw_no_readers_while_open_for_write_writer_contract_ = false;
bool saw_no_readers_while_open_for_write_reader_contract_ = false;
std::unordered_set<std::string> active_blob_paths_;
};
} // namespace
class DBBlobDirectWriteTest : public DBTestBase {
protected:
DBBlobDirectWriteTest()
@@ -816,6 +1021,30 @@ TEST_F(DBBlobDirectWriteTest, DirectWriteImmutableMemtableRead) {
verify_reads();
}
// Verifies active blob direct-write files promise no write reopen without also
// promising no concurrent readers.
TEST_F(DBBlobDirectWriteTest, DirectWriteUsesReadableContractForRemoteFile) {
auto remote_fs =
std::make_shared<RemoteBlobVisibilityFileSystem>(env_->GetFileSystem());
std::unique_ptr<Env> remote_env(new CompositeEnvWrapper(env_, remote_fs));
Options options = GetDirectWriteOptions();
options.env = remote_env.get();
Reopen(options);
const std::string key = "remote_blob_key";
const std::string value(128, 'w');
ASSERT_OK(Put(key, value));
ASSERT_TRUE(remote_fs->SawNoReopenForWriteWriterContract());
ASSERT_FALSE(remote_fs->SawNoReadersWhileOpenForWriteWriterContract());
ASSERT_EQ(Get(key), value);
ASSERT_FALSE(remote_fs->SawNoReadersWhileOpenForWriteReaderContract());
Close();
}
TEST_F(DBBlobDirectWriteTest, DirectWriteRefreshesReaderAfterFlush) {
Options options = GetDirectWriteOptions();
options.blob_direct_write_partitions = 1;
+2
View File
@@ -179,6 +179,8 @@ Status BuildTable(
TEST_SYNC_POINT_CALLBACK("BuildTable:create_file", &use_direct_writes);
#endif // !NDEBUG
FileOptions fo_copy = file_options;
fo_copy.open_contract = FileOpenContract::kNoReopenForWrite |
FileOpenContract::kNoReadersWhileOpenForWrite;
fo_copy.write_hint = write_hint;
IOStatus io_s = NewWritableFile(fs, fname, &file, fo_copy);
assert(s.ok());
+79
View File
@@ -148,6 +148,7 @@ using ROCKSDB_NAMESPACE::TransactionDB;
using ROCKSDB_NAMESPACE::TransactionDBOptions;
using ROCKSDB_NAMESPACE::TransactionLogIterator;
using ROCKSDB_NAMESPACE::TransactionOptions;
using ROCKSDB_NAMESPACE::TxnDBWritePolicy;
using ROCKSDB_NAMESPACE::WaitForCompactOptions;
using ROCKSDB_NAMESPACE::WALRecoveryMode;
using ROCKSDB_NAMESPACE::WritableFile;
@@ -1384,6 +1385,10 @@ void rocksdb_backup_engine_info_destroy(
delete info;
}
void rocksdb_backup_engine_stop_backup(rocksdb_backup_engine_t* be) {
be->rep->StopBackup();
}
void rocksdb_backup_engine_close(rocksdb_backup_engine_t* be) {
delete be->rep;
delete be;
@@ -1465,6 +1470,20 @@ uint64_t rocksdb_backup_engine_options_get_restore_rate_limit(
return options->rep.restore_rate_limit;
}
void rocksdb_backup_engine_options_set_backup_rate_limiter(
rocksdb_backup_engine_options_t* options, rocksdb_ratelimiter_t* limiter) {
if (limiter) {
options->rep.backup_rate_limiter = limiter->rep;
}
}
void rocksdb_backup_engine_options_set_restore_rate_limiter(
rocksdb_backup_engine_options_t* options, rocksdb_ratelimiter_t* limiter) {
if (limiter) {
options->rep.restore_rate_limiter = limiter->rep;
}
}
void rocksdb_backup_engine_options_set_max_background_operations(
rocksdb_backup_engine_options_t* options, int val) {
options->rep.max_background_operations = val;
@@ -3771,6 +3790,11 @@ void rocksdb_block_based_options_set_index_block_search_type(
static_cast<BlockBasedTableOptions::BlockSearchType>(v);
}
void rocksdb_block_based_options_set_uniform_cv_threshold(
rocksdb_block_based_table_options_t* options, double v) {
options->rep.uniform_cv_threshold = v;
}
void rocksdb_block_based_options_set_data_block_hash_ratio(
rocksdb_block_based_table_options_t* options, double v) {
options->rep.data_block_hash_table_util_ratio = v;
@@ -4242,6 +4266,15 @@ void rocksdb_set_options(rocksdb_t* db, int count, const char* const keys[],
SaveError(errptr, db->rep->SetOptions(options_map));
}
void rocksdb_set_db_options(rocksdb_t* db, int count, const char* const keys[],
const char* const values[], char** errptr) {
std::unordered_map<std::string, std::string> options_map;
for (int i = 0; i < count; i++) {
options_map[keys[i]] = values[i];
}
SaveError(errptr, db->rep->SetDBOptions(options_map));
}
void rocksdb_set_options_cf(rocksdb_t* db,
rocksdb_column_family_handle_t* handle, int count,
const char* const keys[],
@@ -5038,6 +5071,16 @@ unsigned char rocksdb_options_get_use_direct_io_for_flush_and_compaction(
return opt->rep.use_direct_io_for_flush_and_compaction;
}
void rocksdb_options_set_use_direct_io_for_compaction_reads(
rocksdb_options_t* opt, unsigned char v) {
opt->rep.use_direct_io_for_compaction_reads = v;
}
unsigned char rocksdb_options_get_use_direct_io_for_compaction_reads(
rocksdb_options_t* opt) {
return opt->rep.use_direct_io_for_compaction_reads;
}
void rocksdb_options_set_allow_mmap_reads(rocksdb_options_t* opt,
unsigned char v) {
opt->rep.allow_mmap_reads = v;
@@ -5286,6 +5329,15 @@ size_t rocksdb_options_get_recycle_log_file_num(rocksdb_options_t* opt) {
return opt->rep.recycle_log_file_num;
}
void rocksdb_options_set_async_wal_precreate(rocksdb_options_t* opt,
unsigned char v) {
opt->rep.async_wal_precreate = v;
}
unsigned char rocksdb_options_get_async_wal_precreate(rocksdb_options_t* opt) {
return opt->rep.async_wal_precreate;
}
void rocksdb_options_set_soft_pending_compaction_bytes_limit(
rocksdb_options_t* opt, size_t v) {
opt->rep.soft_pending_compaction_bytes_limit = v;
@@ -5389,6 +5441,16 @@ size_t rocksdb_options_get_memtable_huge_page_size(rocksdb_options_t* opt) {
return opt->rep.memtable_huge_page_size;
}
void rocksdb_options_set_memtable_batch_lookup_optimization(
rocksdb_options_t* opt, unsigned char v) {
opt->rep.memtable_batch_lookup_optimization = v;
}
unsigned char rocksdb_options_get_memtable_batch_lookup_optimization(
rocksdb_options_t* opt) {
return opt->rep.memtable_batch_lookup_optimization;
}
void rocksdb_options_set_hash_skip_list_rep(rocksdb_options_t* opt,
size_t bucket_count,
int32_t skiplist_height,
@@ -5820,6 +5882,8 @@ uint64_t rocksdb_perfcontext_metric(rocksdb_perfcontext_t* context,
return rep->number_async_seek;
case rocksdb_blob_cache_hit_count:
return rep->blob_cache_hit_count;
case rocksdb_blob_cache_read_byte:
return rep->blob_cache_read_byte;
case rocksdb_blob_read_count:
return rep->blob_read_count;
case rocksdb_blob_read_byte:
@@ -6238,6 +6302,16 @@ unsigned char rocksdb_readoptions_get_async_io(rocksdb_readoptions_t* opt) {
return opt->rep.async_io;
}
void rocksdb_readoptions_set_optimize_multiget_for_io(
rocksdb_readoptions_t* opt, unsigned char v) {
opt->rep.optimize_multiget_for_io = v;
}
unsigned char rocksdb_readoptions_get_optimize_multiget_for_io(
rocksdb_readoptions_t* opt) {
return opt->rep.optimize_multiget_for_io;
}
void rocksdb_readoptions_set_timestamp(rocksdb_readoptions_t* opt,
const char* ts, size_t tslen) {
if (ts == nullptr) {
@@ -7491,6 +7565,11 @@ void rocksdb_transactiondb_options_set_default_lock_timeout(
opt->rep.default_lock_timeout = default_lock_timeout;
}
void rocksdb_transactiondb_options_set_write_policy(
rocksdb_transactiondb_options_t* opt, int write_policy) {
opt->rep.write_policy = static_cast<TxnDBWritePolicy>(write_policy);
}
void rocksdb_transactiondb_options_set_use_per_key_point_lock_mgr(
rocksdb_transactiondb_options_t* opt, int use_per_key_point_lock_mgr) {
opt->rep.use_per_key_point_lock_mgr = use_per_key_point_lock_mgr;
+171 -16
View File
@@ -6,6 +6,7 @@
#include "rocksdb/c.h"
#include <assert.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -14,7 +15,6 @@
#ifndef OS_WIN
#include <unistd.h>
#endif
#include <inttypes.h>
// Can not use port/port.h macros as this is a c file
#ifdef OS_WIN
@@ -32,6 +32,14 @@ int geteuid() {
#endif
static uint64_t GetTestId(void) {
#ifdef OS_WIN
return (uint64_t)geteuid();
#else
return ((uint64_t)geteuid() << 32) ^ (uint64_t)getpid();
#endif
}
const char* phase = "";
static char dbname[200];
static char sstfilename[200];
@@ -848,20 +856,21 @@ int main(int argc, char** argv) {
char* err = NULL;
int run = -1;
snprintf(dbname, sizeof(dbname), "%s/rocksdb_c_test-%d", GetTempDir(),
((int)geteuid()));
snprintf(dbname, sizeof(dbname), "%s/rocksdb_c_test-%" PRIu64, GetTempDir(),
GetTestId());
snprintf(dbbackupname, sizeof(dbbackupname), "%s/rocksdb_c_test-%d-backup",
GetTempDir(), ((int)geteuid()));
snprintf(dbbackupname, sizeof(dbbackupname),
"%s/rocksdb_c_test-%" PRIu64 "-backup", GetTempDir(), GetTestId());
snprintf(dbcheckpointname, sizeof(dbcheckpointname),
"%s/rocksdb_c_test-%d-checkpoint", GetTempDir(), ((int)geteuid()));
"%s/rocksdb_c_test-%" PRIu64 "-checkpoint", GetTempDir(),
GetTestId());
snprintf(sstfilename, sizeof(sstfilename), "%s/rocksdb_c_test-%d-sst",
GetTempDir(), ((int)geteuid()));
snprintf(sstfilename, sizeof(sstfilename),
"%s/rocksdb_c_test-%" PRIu64 "-sst", GetTempDir(), GetTestId());
snprintf(dbpathname, sizeof(dbpathname), "%s/rocksdb_c_test-%d-dbpath",
GetTempDir(), ((int)geteuid()));
snprintf(dbpathname, sizeof(dbpathname),
"%s/rocksdb_c_test-%" PRIu64 "-dbpath", GetTempDir(), GetTestId());
StartPhase("create_objects");
cmp = rocksdb_comparator_create(NULL, CmpDestroy, CmpCompare, CmpName);
@@ -888,6 +897,9 @@ int main(int argc, char** argv) {
rocksdb_block_based_options_set_block_cache(table_options, cache);
rocksdb_block_based_options_set_data_block_index_type(table_options, 1);
rocksdb_block_based_options_set_data_block_hash_ratio(table_options, 0.75);
rocksdb_block_based_options_set_index_block_search_type(
table_options, rocksdb_block_based_table_index_block_search_type_auto);
rocksdb_block_based_options_set_uniform_cv_threshold(table_options, 0.2);
rocksdb_block_based_options_set_top_level_index_pinning_tier(table_options,
1);
rocksdb_block_based_options_set_partition_pinning_tier(table_options, 2);
@@ -948,6 +960,19 @@ int main(int argc, char** argv) {
CheckNoError(err);
CheckGet(db, roptions, "foo", "hello");
StartPhase("set_db_options");
{
const char* keys[] = {"stats_dump_period_sec"};
const char* values[] = {"10"};
rocksdb_set_db_options(db, 1, keys, values, &err);
CheckNoError(err);
keys[0] = "not_a_db_option";
rocksdb_set_db_options(db, 1, keys, values, &err);
CheckCondition(err != NULL);
Free(&err);
}
StartPhase("backup_and_restore");
{
rocksdb_destroy_db(options, dbbackupname, &err);
@@ -1015,6 +1040,12 @@ int main(int argc, char** argv) {
Free(&before_db_id);
Free(&after_db_id);
rocksdb_backup_engine_stop_backup(be);
rocksdb_backup_engine_create_new_backup(be, db, &err);
CheckCondition(err != NULL);
free(err);
err = NULL;
rocksdb_backup_engine_close(be);
}
@@ -1144,9 +1175,11 @@ int main(int argc, char** argv) {
static char cf_export_path[200];
static char db_import_path[200];
snprintf(cf_export_path, sizeof(cf_export_path),
"%s/rocksdb_c_test-%d-cf_export", GetTempDir(), ((int)geteuid()));
"%s/rocksdb_c_test-%" PRIu64 "-cf_export", GetTempDir(),
GetTestId());
snprintf(db_import_path, sizeof(db_import_path),
"%s/rocksdb_c_test-%d-db_import", GetTempDir(), ((int)geteuid()));
"%s/rocksdb_c_test-%" PRIu64 "-db_import", GetTempDir(),
GetTestId());
rocksdb_options_t* db_options = rocksdb_options_create();
rocksdb_column_family_handle_t* cf_export =
@@ -2055,8 +2088,9 @@ int main(int argc, char** argv) {
// use dbpathname2 as the cf_path for "cf1"
rocksdb_dbpath_t* dbpath2;
char dbpathname2[200];
snprintf(dbpathname2, sizeof(dbpathname2), "%s/rocksdb_c_test-%d-dbpath2",
GetTempDir(), ((int)geteuid()));
snprintf(dbpathname2, sizeof(dbpathname2),
"%s/rocksdb_c_test-%" PRIu64 "-dbpath2", GetTempDir(),
GetTestId());
dbpath2 = rocksdb_dbpath_create(dbpathname2, 1024 * 1024);
const rocksdb_dbpath_t* cf_paths[1] = {dbpath2};
rocksdb_options_set_cf_paths(cf_options_2, cf_paths, 1);
@@ -2757,6 +2791,10 @@ int main(int argc, char** argv) {
CheckCondition(
1 == rocksdb_options_get_use_direct_io_for_flush_and_compaction(o));
rocksdb_options_set_use_direct_io_for_compaction_reads(o, 1);
CheckCondition(1 ==
rocksdb_options_get_use_direct_io_for_compaction_reads(o));
rocksdb_options_set_is_fd_close_on_exec(o, 1);
CheckCondition(1 == rocksdb_options_get_is_fd_close_on_exec(o));
@@ -2812,6 +2850,15 @@ int main(int argc, char** argv) {
rocksdb_options_set_memtable_huge_page_size(o, 25);
CheckCondition(25 == rocksdb_options_get_memtable_huge_page_size(o));
// memtable_batch_lookup_optimization defaults to 0; flip to 1 to verify
// the setter is wired through to the underlying C++ ColumnFamilyOptions
// field.
CheckCondition(0 ==
rocksdb_options_get_memtable_batch_lookup_optimization(o));
rocksdb_options_set_memtable_batch_lookup_optimization(o, 1);
CheckCondition(1 ==
rocksdb_options_get_memtable_batch_lookup_optimization(o));
rocksdb_options_set_max_successive_merges(o, 26);
CheckCondition(26 == rocksdb_options_get_max_successive_merges(o));
@@ -2869,6 +2916,9 @@ int main(int argc, char** argv) {
rocksdb_options_set_track_and_verify_wals_in_manifest(o, 42);
CheckCondition(1 ==
rocksdb_options_get_track_and_verify_wals_in_manifest(o));
CheckCondition(0 == rocksdb_options_get_async_wal_precreate(o));
rocksdb_options_set_async_wal_precreate(o, 1);
CheckCondition(1 == rocksdb_options_get_async_wal_precreate(o));
/* Blob Options */
rocksdb_options_set_enable_blob_files(o, 1);
@@ -2972,6 +3022,8 @@ int main(int argc, char** argv) {
CheckCondition(1 == rocksdb_options_get_use_direct_reads(copy));
CheckCondition(
1 == rocksdb_options_get_use_direct_io_for_flush_and_compaction(copy));
CheckCondition(
1 == rocksdb_options_get_use_direct_io_for_compaction_reads(copy));
CheckCondition(1 == rocksdb_options_get_is_fd_close_on_exec(copy));
CheckCondition(18 == rocksdb_options_get_stats_dump_period_sec(copy));
CheckCondition(5 == rocksdb_options_get_stats_persist_period_sec(copy));
@@ -2995,6 +3047,8 @@ int main(int argc, char** argv) {
rocksdb_options_get_memtable_prefix_bloom_size_ratio(copy));
CheckCondition(24 == rocksdb_options_get_max_compaction_bytes(copy));
CheckCondition(25 == rocksdb_options_get_memtable_huge_page_size(copy));
CheckCondition(
1 == rocksdb_options_get_memtable_batch_lookup_optimization(copy));
CheckCondition(26 == rocksdb_options_get_max_successive_merges(copy));
CheckCondition(27 == rocksdb_options_get_bloom_locality(copy));
CheckCondition(1 == rocksdb_options_get_inplace_update_support(copy));
@@ -3248,6 +3302,12 @@ int main(int argc, char** argv) {
CheckCondition(
1 == rocksdb_options_get_use_direct_io_for_flush_and_compaction(o));
rocksdb_options_set_use_direct_io_for_compaction_reads(copy, 0);
CheckCondition(
0 == rocksdb_options_get_use_direct_io_for_compaction_reads(copy));
CheckCondition(1 ==
rocksdb_options_get_use_direct_io_for_compaction_reads(o));
rocksdb_options_set_is_fd_close_on_exec(copy, 0);
CheckCondition(0 == rocksdb_options_get_is_fd_close_on_exec(copy));
CheckCondition(1 == rocksdb_options_get_is_fd_close_on_exec(o));
@@ -3429,6 +3489,12 @@ int main(int argc, char** argv) {
rocksdb_readoptions_set_async_io(ro, 1);
CheckCondition(1 == rocksdb_readoptions_get_async_io(ro));
// optimize_multiget_for_io defaults to true (1); flip to 0 to verify the
// setter is wired through to the underlying C++ ReadOptions field.
CheckCondition(1 == rocksdb_readoptions_get_optimize_multiget_for_io(ro));
rocksdb_readoptions_set_optimize_multiget_for_io(ro, 0);
CheckCondition(0 == rocksdb_readoptions_get_optimize_multiget_for_io(ro));
rocksdb_readoptions_destroy(ro);
}
@@ -3674,6 +3740,52 @@ int main(int argc, char** argv) {
CheckCondition(37 ==
rocksdb_backup_engine_options_get_restore_rate_limit(bdo));
{
rocksdb_ratelimiter_t* limiter = rocksdb_ratelimiter_create_with_mode(
1024 * 1024, 100 * 1000, 10, 2, 0);
rocksdb_backup_engine_options_set_backup_rate_limiter(bdo, limiter);
rocksdb_backup_engine_options_set_restore_rate_limiter(bdo, limiter);
rocksdb_ratelimiter_destroy(limiter);
rocksdb_backup_engine_options_t* rate_beo =
rocksdb_backup_engine_options_create(dbbackupname);
rocksdb_ratelimiter_t* backup_limiter =
rocksdb_ratelimiter_create_with_mode(1024 * 1024, 100 * 1000, 10, 2,
0);
rocksdb_ratelimiter_t* restore_limiter =
rocksdb_ratelimiter_create_with_mode(1024 * 1024, 100 * 1000, 10, 2,
0);
rocksdb_backup_engine_options_set_backup_rate_limiter(rate_beo,
backup_limiter);
rocksdb_backup_engine_options_set_restore_rate_limiter(rate_beo,
restore_limiter);
rocksdb_ratelimiter_destroy(backup_limiter);
rocksdb_ratelimiter_destroy(restore_limiter);
rocksdb_env_t* rate_benv = rocksdb_create_default_env();
rocksdb_backup_engine_t* rate_be =
rocksdb_backup_engine_open_opts(rate_beo, rate_benv, &err);
rocksdb_backup_engine_options_destroy(rate_beo);
rocksdb_env_destroy(rate_benv);
CheckNoError(err);
rocksdb_backup_engine_create_new_backup(rate_be, db, &err);
CheckNoError(err);
{
char rate_restore_path[220];
snprintf(rate_restore_path, sizeof(rate_restore_path),
"%s.rate_restore", dbname);
rocksdb_restore_options_t* rate_restore_opts =
rocksdb_restore_options_create();
rocksdb_backup_engine_restore_db_from_latest_backup(
rate_be, rate_restore_path, rate_restore_path, rate_restore_opts,
&err);
CheckNoError(err);
rocksdb_restore_options_destroy(rate_restore_opts);
rocksdb_destroy_db(options, rate_restore_path, &err);
err = NULL;
}
rocksdb_backup_engine_close(rate_be);
}
rocksdb_backup_engine_options_set_max_background_operations(bdo, 20);
CheckCondition(
20 == rocksdb_backup_engine_options_get_max_background_operations(bdo));
@@ -3785,6 +3897,43 @@ int main(int argc, char** argv) {
}
}
StartPhase("transactiondb_set_write_policy_prepared");
{
char wp_dbname[200];
rocksdb_transactiondb_t* wp_txn_db;
rocksdb_transactiondb_options_t* wp_txn_opts;
snprintf(wp_dbname, sizeof(wp_dbname), "%s/rocksdb_c_test-txnwp-%" PRIu64,
GetTempDir(), GetTestId());
rocksdb_destroy_db(options, wp_dbname, &err);
Free(&err);
wp_txn_opts = rocksdb_transactiondb_options_create();
rocksdb_transactiondb_options_set_write_policy(
wp_txn_opts, rocksdb_txndb_write_policy_write_prepared);
rocksdb_options_set_create_if_missing(options, 1);
// create new TransactionDB with write policy WRITE_PREPARED
wp_txn_db =
rocksdb_transactiondb_open(options, wp_txn_opts, wp_dbname, &err);
CheckNoError(err);
{
rocksdb_transaction_options_t* wp_txn_options =
rocksdb_transaction_options_create();
rocksdb_transaction_t* wp_txn =
rocksdb_transaction_begin(wp_txn_db, woptions, wp_txn_options, NULL);
// write one record within a transaction
rocksdb_transaction_put(wp_txn, "k", 1, "v", 1, &err);
CheckNoError(err);
rocksdb_transaction_commit(wp_txn, &err);
CheckNoError(err);
rocksdb_transaction_destroy(wp_txn);
rocksdb_transaction_options_destroy(wp_txn_options);
}
CheckTxnDBGet(wp_txn_db, roptions, "k", "v");
rocksdb_transactiondb_close(wp_txn_db);
rocksdb_transactiondb_options_destroy(wp_txn_opts);
rocksdb_destroy_db(options, wp_dbname, &err);
CheckNoError(err);
}
StartPhase("transactions");
{
rocksdb_close(db);
@@ -4422,7 +4571,7 @@ int main(int argc, char** argv) {
rocksdb_options_set_max_open_files(opts, -1);
rocksdb_options_set_create_if_missing(opts, 1);
snprintf(secondary_path, sizeof(secondary_path),
"%s/rocksdb_c_test_secondary-%d", GetTempDir(), ((int)geteuid()));
"%s/rocksdb_c_test_secondary-%" PRIu64, GetTempDir(), GetTestId());
db1 = rocksdb_open_as_secondary(opts, dbname, secondary_path, &err);
CheckNoError(err);
@@ -4881,7 +5030,13 @@ int main(int argc, char** argv) {
rocksdb_options_set_create_if_missing(null_opts, 1);
rocksdb_options_set_compaction_service(null_opts, null_service);
const char* null_db = "rocksdb_c_test_null_service";
char null_db[200];
int null_db_len = snprintf(null_db, sizeof(null_db),
"%s/rocksdb_c_test_null_service-%" PRIu64,
GetTempDir(), GetTestId());
if (null_db_len <= 0 || (size_t)null_db_len >= sizeof(null_db)) {
abort();
}
rocksdb_t* null_db_handle = rocksdb_open(null_opts, null_db, &err);
CheckNoError(err);
+18
View File
@@ -454,6 +454,18 @@ class Compaction {
return notify_on_compaction_completion_;
}
// Called by DBImpl::NotifyOnCompactionPreCommit to ensure the
// OnCompactionPreCommit listener callback fires at most once per
// compaction lifecycle, even though the notify helper is invoked at multiple
// potential ReleaseCompactionFiles() sites for safety.
void SetNotifyOnCompactionPreCommitCalled() {
notify_on_compaction_pre_commit_called_ = true;
}
bool WasNotifyOnCompactionPreCommitCalled() const {
return notify_on_compaction_pre_commit_called_;
}
static constexpr int kInvalidLevel = -1;
// Evaluate proximal output level. If the compaction supports
@@ -626,6 +638,12 @@ class Compaction {
// begin.
bool notify_on_compaction_completion_;
// Tracks whether OnCompactionPreCommit has already fired for this
// compaction. The notify helper is called from multiple potential
// ReleaseCompactionFiles() call sites; this flag guarantees that listeners
// observe the callback exactly once per compaction.
bool notify_on_compaction_pre_commit_called_ = false;
// Enable/disable GC collection for blobs during compaction.
bool enable_blob_garbage_collection_;
+29 -3
View File
@@ -173,6 +173,7 @@ CompactionJob::CompactionJob(
fs_(db_options.fs, io_tracer),
file_options_for_read_(
fs_->OptimizeForCompactionTableRead(file_options, db_options_)),
file_options_for_compaction_input_read_(file_options_for_read_),
versions_(versions),
shutting_down_(shutting_down),
manual_compaction_canceled_(manual_compaction_canceled),
@@ -203,6 +204,11 @@ CompactionJob::CompactionJob(
assert(job_context);
assert(job_context->snapshot_context_initialized);
if (db_options_.use_direct_io_for_compaction_reads &&
!db_options_.use_direct_reads) {
file_options_for_compaction_input_read_.use_direct_reads = true;
}
const auto* cfd = compact_->compaction->column_family_data();
ThreadStatusUtil::SetEnableTracking(db_options_.enable_thread_tracking);
ThreadStatusUtil::SetColumnFamily(cfd);
@@ -1536,10 +1542,20 @@ InternalIterator* CompactionJob::CreateInputIterator(
// Although the v2 aggregator is what the level iterator(s) know about,
// the AddTombstones calls will be propagated down to the v1 aggregator.
const bool open_ephemeral_table_reader =
db_options_.use_direct_io_for_compaction_reads &&
!db_options_.use_direct_reads;
FileOptions& input_file_options =
open_ephemeral_table_reader ? file_options_for_compaction_input_read_
: file_options_for_read_;
TEST_SYNC_POINT_CALLBACK(
"CompactionJob::CreateInputIterator:InputFileOptions",
&input_file_options);
iterators.raw_input =
std::unique_ptr<InternalIterator>(versions_->MakeInputIterator(
read_options, sub_compact->compaction, sub_compact->RangeDelAgg(),
file_options_for_read_, boundaries.start, boundaries.end));
input_file_options, boundaries.start, boundaries.end,
open_ephemeral_table_reader));
InternalIterator* input = iterators.raw_input.get();
if (boundaries.start.has_value() || boundaries.end.has_value()) {
@@ -2065,10 +2081,18 @@ Status CompactionJob::FinishCompactionOutputFile(
std::pair<SequenceNumber, SequenceNumber> keep_seqno_range{
0, kMaxSequenceNumber};
if (sub_compact->compaction->SupportsPerKeyPlacement()) {
// Point entries are routed to proximal output only when their seqno is
// strictly greater than `proximal_after_seqno_`. Range tombstones use a
// [lower, upper) filter, so split them at the next seqno to preserve the
// same boundary.
SequenceNumber range_del_split_seqno = proximal_after_seqno_;
if (range_del_split_seqno < kMaxSequenceNumber) {
range_del_split_seqno++;
}
if (outputs.IsProximalLevel()) {
keep_seqno_range.first = proximal_after_seqno_;
keep_seqno_range.first = range_del_split_seqno;
} else {
keep_seqno_range.second = proximal_after_seqno_;
keep_seqno_range.second = range_del_split_seqno;
}
}
CompactionIterationStats range_del_out_stats;
@@ -2462,6 +2486,8 @@ Status CompactionJob::OpenCompactionOutputFile(SubcompactionState* sub_compact,
auto temperature =
sub_compact->compaction->GetOutputTemperature(outputs.IsProximalLevel());
fo_copy.temperature = temperature;
fo_copy.open_contract = FileOpenContract::kNoReopenForWrite |
FileOpenContract::kNoReadersWhileOpenForWrite;
fo_copy.write_hint = write_hint_;
Status s;
+2
View File
@@ -462,6 +462,8 @@ class CompactionJob {
FileSystemPtr fs_;
// env_option optimized for compaction table reads
FileOptions file_options_for_read_;
// file_options_for_read_ plus compaction-input-only overrides.
FileOptions file_options_for_compaction_input_read_;
VersionSet* versions_;
const std::atomic<bool>* shutting_down_;
const std::atomic<bool>& manual_compaction_canceled_;
+27 -5
View File
@@ -189,14 +189,24 @@ CompactionJob::ProcessKeyValueCompactionWithCompactionService(
return compaction_status;
}
// CompactionServiceJobStatus::kSuccess was returned, but somehow we failed to
// read the result. Consider this as an installation failure
if (!s.ok()) {
sub_compact->status = s;
// Wait() returned kSuccess, but the primary host could not deserialize the
// remote result before importing any output files into this DB. The remote
// output is still isolated in the service-managed staging directory, so it
// is safe to fall back to local compaction for the same job and notify the
// service via OnInstallation(kUseLocal).
assert(sub_compact->status.ok());
assert(sub_compact->GetMutableCompactionOutputs().empty());
assert(sub_compact->GetMutableProximalOutputs().empty());
ROCKS_LOG_WARN(db_options_.info_log,
"[%s] [JOB %d] Failed to parse remote compaction result "
"(%s), falling back to local compaction",
compaction->column_family_data()->GetName().c_str(), job_id_,
s.ToString().c_str());
compaction_result.status.PermitUncheckedError();
db_options_.compaction_service->OnInstallation(
response.scheduled_job_id, CompactionServiceJobStatus::kFailure);
return CompactionServiceJobStatus::kFailure;
response.scheduled_job_id, CompactionServiceJobStatus::kUseLocal);
return CompactionServiceJobStatus::kUseLocal;
}
sub_compact->status = compaction_result.status;
@@ -421,6 +431,14 @@ Status CompactionServiceCompactionJob::Run() {
// Please note that input stats will be updated by primary host when all
// subcompactions are finished
UpdateCompactionJobOutputStatsFromInternalStats(status, internal_stats_);
// Whole-file filtering changes which keys are fed to the iterator, so the
// iterator-based input record count should not be treated as exact.
for (size_t level = 1; level < c->num_input_levels(); ++level) {
if (!c->filtered_input_levels(level).empty()) {
compaction_result_->stats.has_accurate_num_input_records = false;
break;
}
}
// and set fields that are not propagated as part of the update
compaction_result_->stats.is_manual_compaction = c->is_manual_compaction();
compaction_result_->stats.is_full_compaction = c->is_full_compaction();
@@ -641,6 +659,10 @@ static std::unordered_map<std::string, OptionTypeInfo>
{"cpu_micros",
{offsetof(struct CompactionJobStats, cpu_micros), OptionType::kUInt64T,
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
{"has_accurate_num_input_records",
{offsetof(struct CompactionJobStats, has_accurate_num_input_records),
OptionType::kBoolean, OptionVerificationType::kNormal,
OptionTypeFlags::kNone}},
{"num_input_records",
{offsetof(struct CompactionJobStats, num_input_records),
OptionType::kUInt64T, OptionVerificationType::kNormal,
+144 -3
View File
@@ -127,6 +127,7 @@ class MyTestCompactionService : public CompactionService {
void OnInstallation(const std::string& /*scheduled_job_id*/,
CompactionServiceJobStatus status) override {
installation_callback_count_.fetch_add(1);
final_updated_status_ = status;
}
@@ -167,6 +168,8 @@ class MyTestCompactionService : public CompactionService {
return final_updated_status_.load();
}
int GetOnInstallationCount() { return installation_callback_count_.load(); }
protected:
InstrumentedMutex mutex_;
const std::string db_path_;
@@ -195,6 +198,7 @@ class MyTestCompactionService : public CompactionService {
std::vector<std::shared_ptr<EventListener>> listeners_;
std::vector<std::shared_ptr<TablePropertiesCollectorFactory>>
table_properties_collector_factories_;
std::atomic_int installation_callback_count_{0};
std::atomic<CompactionServiceJobStatus> final_updated_status_{
CompactionServiceJobStatus::kUseLocal};
@@ -607,6 +611,101 @@ TEST_F(CompactionServiceTest, StandaloneDeleteRangeTombstoneOptimization) {
SyncPoint::GetInstance()->DisableProcessing();
}
TEST_F(CompactionServiceTest,
StandaloneDeleteRangeTombstoneMakesInputCountInaccurate) {
Options options = CurrentOptions();
options.compaction_style = CompactionStyle::kCompactionStyleUniversal;
options.compaction_verify_record_count = true;
ReopenWithCompactionService(&options);
std::vector<std::string> files;
{
SstFileWriter sst_file_writer(EnvOptions(), options);
std::string file1 = dbname_ + "file1.sst";
ASSERT_OK(sst_file_writer.Open(file1));
ASSERT_OK(sst_file_writer.Put("a", "a1"));
ASSERT_OK(sst_file_writer.Put("b", "b1"));
ExternalSstFileInfo file1_info;
ASSERT_OK(sst_file_writer.Finish(&file1_info));
files.push_back(std::move(file1));
std::string file2 = dbname_ + "file2.sst";
ASSERT_OK(sst_file_writer.Open(file2));
ASSERT_OK(sst_file_writer.Put("x", "x1"));
ASSERT_OK(sst_file_writer.Put("y", "y1"));
ExternalSstFileInfo file2_info;
ASSERT_OK(sst_file_writer.Finish(&file2_info));
files.push_back(std::move(file2));
}
IngestExternalFileOptions ifo;
ASSERT_OK(db_->IngestExternalFile(files, ifo));
ASSERT_EQ(Get("a"), "a1");
ASSERT_EQ(Get("b"), "b1");
ASSERT_EQ(Get("x"), "x1");
ASSERT_EQ(Get("y"), "y1");
ASSERT_EQ(2, NumTableFilesAtLevel(6));
auto my_cs = GetCompactionService();
uint64_t comp_num = my_cs->GetCompactionNum();
size_t num_files_after_filtered = 0;
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"VersionSet::MakeInputIterator:NewCompactionMergingIterator",
[&](void* arg) {
num_files_after_filtered = *static_cast<size_t*>(arg);
});
SyncPoint::GetInstance()->EnableProcessing();
{
// The standalone range tombstone can fully cover the old versioned files,
// so universal compaction may filter those files before iteration.
files.clear();
SstFileWriter sst_file_writer(EnvOptions(), options);
std::string file2 = dbname_ + "file2.sst";
ASSERT_OK(sst_file_writer.Open(file2));
ASSERT_OK(sst_file_writer.DeleteRange("a", "z"));
ExternalSstFileInfo file2_info;
ASSERT_OK(sst_file_writer.Finish(&file2_info));
files.push_back(std::move(file2));
std::string file3 = dbname_ + "file3.sst";
ASSERT_OK(sst_file_writer.Open(file3));
ASSERT_OK(sst_file_writer.Put("a", "a2"));
ASSERT_OK(sst_file_writer.Put("b", "b2"));
ExternalSstFileInfo file3_info;
ASSERT_OK(sst_file_writer.Finish(&file3_info));
files.push_back(std::move(file3));
std::string file4 = dbname_ + "file4.sst";
ASSERT_OK(sst_file_writer.Open(file4));
ASSERT_OK(sst_file_writer.Put("x", "x2"));
ASSERT_OK(sst_file_writer.Put("y", "y2"));
ExternalSstFileInfo file4_info;
ASSERT_OK(sst_file_writer.Finish(&file4_info));
files.push_back(std::move(file4));
}
ASSERT_OK(db_->IngestExternalFile(files, ifo));
ASSERT_OK(db_->WaitForCompact(WaitForCompactOptions()));
ASSERT_GE(my_cs->GetCompactionNum(), comp_num + 1);
CompactionServiceResult result;
my_cs->GetResult(&result);
ASSERT_OK(result.status);
ASSERT_TRUE(result.stats.is_remote_compaction);
ASSERT_FALSE(result.stats.has_accurate_num_input_records);
ASSERT_EQ(1, num_files_after_filtered);
ASSERT_EQ(Get("a"), "a2");
ASSERT_EQ(Get("b"), "b2");
ASSERT_EQ(Get("x"), "x2");
ASSERT_EQ(Get("y"), "y2");
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(CompactionServiceTest, CompactionOutputFileIOError) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
@@ -939,6 +1038,37 @@ TEST_F(CompactionServiceTest, VerifyInputRecordCount) {
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(CompactionServiceTest, InaccurateInputRecordCount) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
ReopenWithCompactionService(&options);
GenerateTestData();
auto my_cs = GetCompactionService();
std::string start_str = Key(15);
std::string end_str = Key(45);
Slice start(start_str);
Slice end(end_str);
uint64_t comp_num = my_cs->GetCompactionNum();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"CompactionServiceCompactionJob::Run:0", [&](void* arg) {
CompactionServiceResult* compaction_result =
*(static_cast<CompactionServiceResult**>(arg));
ASSERT_TRUE(compaction_result != nullptr);
compaction_result->stats.has_accurate_num_input_records = false;
compaction_result->stats.num_input_records = 0;
});
SyncPoint::GetInstance()->EnableProcessing();
ASSERT_OK(db_->CompactRange(CompactRangeOptions(), &start, &end));
ASSERT_GE(my_cs->GetCompactionNum(), comp_num + 1);
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(CompactionServiceTest, EmptyResult) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
@@ -1281,6 +1411,10 @@ TEST_F(CompactionServiceTest, TruncatedOutput) {
TEST_F(CompactionServiceTest, CustomFileChecksum) {
Options options = CurrentOptions();
// Pin compression so the auto-compacted LSM shape (and thus whether the
// manual CompactRange below schedules a remote compaction) doesn't depend on
// the default compression type. kNoCompression is always available.
options.compression = kNoCompression;
options.file_checksum_gen_factory = GetFileChecksumGenCrc32cFactory();
ReopenWithCompactionService(&options);
GenerateTestData();
@@ -1448,7 +1582,7 @@ TEST_F(CompactionServiceTest, FailedToStart) {
ASSERT_TRUE(s.IsIncomplete());
}
TEST_F(CompactionServiceTest, InvalidResult) {
TEST_F(CompactionServiceTest, InvalidResultFallsBackToLocal) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
ReopenWithCompactionService(&options);
@@ -1456,15 +1590,22 @@ TEST_F(CompactionServiceTest, InvalidResult) {
GenerateTestData();
auto my_cs = GetCompactionService();
ASSERT_EQ(0, my_cs->GetOnInstallationCount());
my_cs->OverrideWaitResult("Invalid Str");
int compaction_num_before = my_cs->GetCompactionNum();
std::string start_str = Key(15);
std::string end_str = Key(45);
Slice start(start_str);
Slice end(end_str);
// The remote result is malformed before any installation starts, so the
// primary should recover by rerunning the compaction locally for this job.
Status s = db_->CompactRange(CompactRangeOptions(), &start, &end);
ASSERT_FALSE(s.ok());
ASSERT_EQ(CompactionServiceJobStatus::kFailure,
ASSERT_OK(s);
ASSERT_GE(my_cs->GetCompactionNum(), compaction_num_before + 1);
VerifyTestData();
ASSERT_EQ(1, my_cs->GetOnInstallationCount());
ASSERT_EQ(CompactionServiceJobStatus::kUseLocal,
my_cs->GetFinalCompactionServiceJobStatus());
}
+55
View File
@@ -2897,6 +2897,61 @@ TEST_P(PrecludeLastLevelTest, RangeDelsCauseFileEndpointsToOverlap) {
Close();
}
TEST_F(PrecludeLastLevelTestBase,
RangeDelAtProximalSeqnoBoundaryStaysInLastLevel) {
constexpr int kNumLevels = 7;
Options options = CurrentOptions();
options.env = mock_env_.get();
options.num_levels = kNumLevels;
options.disable_auto_compactions = true;
DestroyAndReopen(options);
Defer close_db([&] { Close(); });
ASSERT_OK(Put(Key(2), "old"));
ASSERT_OK(Put(Key(12), "old"));
ManagedSnapshot snapshot(db_.get());
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(), Key(2),
Key(12)));
ASSERT_OK(Flush());
MoveFilesToLevel(6);
const int l5_file_keys[][2] = {{0, 4}, {5, 9}};
for (const auto& l5_file : l5_file_keys) {
ASSERT_OK(Put(Key(l5_file[0]), "hot"));
ASSERT_OK(Put(Key(l5_file[1]), "hot"));
ASSERT_OK(Flush());
MoveFilesToLevel(5);
}
ASSERT_EQ("0,0,0,0,0,2,1", FilesPerLevel());
SyncPoint::GetInstance()->SetCallBack(
"CompactionJob::PrepareTimes():preclude_last_level_min_seqno",
[](void* arg) { *static_cast<SequenceNumber*>(arg) = 0; });
SyncPoint::GetInstance()->EnableProcessing();
ASSERT_OK(db_->SetOptions({{"preclude_last_level_data_seconds", "10000"}}));
auto l5_files = GetLevelFileMetadatas(5);
auto l6_files = GetLevelFileMetadatas(6);
ASSERT_EQ(2, l5_files.size());
ASSERT_EQ(1, l6_files.size());
ASSERT_OK(db_->CompactFiles(CompactionOptions(),
{MakeTableFileName(l5_files[1]->fd.GetNumber()),
MakeTableFileName(l6_files[0]->fd.GetNumber())},
6));
uint64_t l6_range_deletions = 0;
for (auto* f : GetLevelFileMetadatas(5)) {
ASSERT_EQ(0, f->num_range_deletions);
}
for (auto* f : GetLevelFileMetadatas(6)) {
l6_range_deletions += f->num_range_deletions;
}
ASSERT_GT(l6_range_deletions, 0);
}
// Tests DBIter::GetProperty("rocksdb.iterator.write-time") return a data's
// approximate write unix time.
class IteratorWriteTimeTest
+106 -1
View File
@@ -761,6 +761,111 @@ TEST_F(DBBasicTest, ReuseManifestOnOpenFullStackComposition) {
EXPECT_EQ("v", Get("k"));
}
// Regression test for WAL recovery while publishing a fresh MANIFEST. The test
// stores SSTs in a separate DB path, injects failure after CURRENT points at
// the new MANIFEST, and simulates crash cleanup; the recovered SST must survive
// because the synced MANIFEST references it.
TEST_F(DBBasicTest, RecoverySstDirSyncedBeforeFreshManifestPublish) {
auto fault_fs = std::make_shared<FaultInjectionTestFS>(env_->GetFileSystem());
std::unique_ptr<Env> fault_env(NewCompositeEnv(fault_fs));
Options options = CurrentOptions();
options.create_if_missing = true;
options.disable_auto_compactions = true;
options.env = fault_env.get();
options.reuse_manifest_on_open = false;
options.db_paths.emplace_back(dbname_ + "_2", 1ULL << 30);
DestroyAndReopen(options);
ASSERT_OK(Put("base", "value"));
ASSERT_OK(Flush());
ASSERT_OK(Put("recovered", "value"));
ASSERT_OK(db_->FlushWAL(true));
Close();
fault_fs->ResetState();
std::atomic<bool> fail_after_current_publish{true};
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"VersionSet::ProcessManifestWrites:AfterSetCurrentFile", [&](void* arg) {
if (fail_after_current_publish.exchange(false)) {
ASSERT_NE(nullptr, arg);
IOStatus* io_s = static_cast<IOStatus*>(arg);
ASSERT_OK(*io_s);
*io_s = IOStatus::IOError("injected current publish aftermath");
}
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
Status s = TryReopen(options);
ASSERT_TRUE(s.IsIOError()) << s.ToString();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
ASSERT_OK(fault_fs->DeleteFilesCreatedAfterLastDirSync(IOOptions(), nullptr));
s = TryReopen(options);
ASSERT_OK(s);
ASSERT_EQ("value", Get("base"));
ASSERT_EQ("value", Get("recovered"));
Close();
}
// Regression test for WAL recovery while appending to a reused MANIFEST. The
// first reopen forces recovery to create an SST and then fail after MANIFEST
// sync. The simulated crash cleanup deletes files without a prior directory
// sync; the recovered SST must survive because the synced MANIFEST references
// it.
TEST_F(DBBasicTest,
ReuseManifestOnOpenSyncsRecoverySstDirBeforeManifestAppend) {
auto fault_fs = std::make_shared<FaultInjectionTestFS>(env_->GetFileSystem());
std::unique_ptr<Env> fault_env(NewCompositeEnv(fault_fs));
Options options = CurrentOptions();
options.create_if_missing = true;
options.disable_auto_compactions = true;
options.env = fault_env.get();
options.reuse_manifest_on_open = true;
DestroyAndReopen(options);
ASSERT_OK(Put("base", "value"));
ASSERT_OK(Flush());
ASSERT_OK(Put("recovered", "value"));
ASSERT_OK(db_->FlushWAL(true));
Close();
fault_fs->ResetState();
std::atomic<bool> fail_after_manifest_sync{true};
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"VersionSet::ProcessManifestWrites:AfterSyncManifest", [&](void* arg) {
if (fail_after_manifest_sync.exchange(false)) {
ASSERT_NE(nullptr, arg);
IOStatus* io_s = static_cast<IOStatus*>(arg);
ASSERT_OK(*io_s);
*io_s = IOStatus::IOError("injected manifest sync aftermath");
}
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
Status s = TryReopen(options);
ASSERT_TRUE(s.IsIOError()) << s.ToString();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
ASSERT_OK(fault_fs->DeleteFilesCreatedAfterLastDirSync(IOOptions(), nullptr));
s = TryReopen(options);
ASSERT_OK(s);
ASSERT_EQ("value", Get("base"));
ASSERT_EQ("value", Get("recovered"));
Close();
}
// Direct unit test for WritableFileWriter's initial_file_size parameter:
// verifies the visible size accessors report the existing on-disk size
// immediately, rather than the constructor's zero default.
@@ -6226,7 +6331,7 @@ TEST_F(DBBasicTest, DisallowMemtableWrite) {
Options options_disallow = options_allow;
options_disallow.disallow_memtable_writes = true;
options_disallow.paranoid_memory_checks = true;
options_disallow.memtable_veirfy_per_key_checksum_on_seek = true;
options_disallow.memtable_verify_per_key_checksum_on_seek = true;
DestroyAndReopen(options_allow);
// CFs allowing and disallowing memtable write
+3
View File
@@ -1482,10 +1482,13 @@ TEST_P(DBBlockCacheTypeTest, AddRedundantStats) {
// Access just data, forcing redundant load+insert
ReadOptions read_options;
std::unique_ptr<Iterator> iter{db_->NewIterator(read_options)};
ASSERT_OK(iter->Refresh());
cache->SetNthLookupNotFound(1);
iter->SeekToFirst();
ASSERT_TRUE(iter->Valid());
ASSERT_EQ(iter->key(), "bar");
ASSERT_EQ(iter->value(), "value");
EXPECT_EQ(2, TestGetTickerCount(options, BLOCK_CACHE_INDEX_ADD));
EXPECT_EQ(2, TestGetTickerCount(options, BLOCK_CACHE_FILTER_ADD));
+5 -1
View File
@@ -2022,7 +2022,11 @@ TEST_F(DBBloomFilterTest, MutableFilterPolicy) {
double expected_bpk = 10.0;
// Other configs to try
std::vector<std::pair<std::string, double>> configs = {
{"ribbonfilter:10:-1", 7.0}, {"bloomfilter:5", 5.0}, {"nullptr", 0.0}};
{"ribbonfilter:10:-1", 7.0},
{"bloomfilter:5", 5.0},
{"nullptr", 0.0},
// As serialized in OPTIONS file
{"{id=ribbonfilter:12:-1;bloom_before_level=-1;}", 8.4}};
table_options.cache_index_and_filter_blocks = true;
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
+32
View File
@@ -427,6 +427,38 @@ TEST_F(DBCompactionAbortTest, AbortAutomaticCompaction) {
}
}
TEST_F(DBCompactionAbortTest, AbortScheduledAutomaticCompactionBeforePick) {
// The goal is to cover an automatic compaction that has been scheduled, but
// aborts before the worker picks and removes a column family from the
// compaction queue. This pins the scheduler bookkeeping invariant that
// ResumeAllCompactions() must be able to schedule that still-queued work.
constexpr int kCompactionTrigger = 4;
constexpr int kNumL0Files = kCompactionTrigger + 1;
Options options = GetOptionsWithStats();
options.level0_file_num_compaction_trigger = kCompactionTrigger;
options.max_background_compactions = 1;
options.max_subcompactions = 1;
options.disable_auto_compactions = false;
Reopen(options);
SyncPointAbortHelper helper("BackgroundCallCompaction:0");
helper.Setup(dbfull());
PopulateData(/*num_files=*/kNumL0Files, /*keys_per_file=*/100,
/*value_size=*/1000);
helper.CleanupAndWait();
const uint64_t compact_write_bytes_before_resume =
stats_->getTickerCount(COMPACT_WRITE_BYTES);
dbfull()->ResumeAllCompactions();
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_GT(stats_->getTickerCount(COMPACT_WRITE_BYTES),
compact_write_bytes_before_resume);
ASSERT_LT(NumTableFilesAtLevel(0), kNumL0Files);
VerifyDataIntegrity(/*num_keys=*/100);
}
TEST_F(DBCompactionAbortTest, AbortAndVerifyNoOutputFiles) {
Options options = CurrentOptions();
options.level0_file_num_compaction_trigger = 4;
+568
View File
@@ -8,11 +8,13 @@
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include <tuple>
#include <utility>
#include "compaction/compaction_picker_universal.h"
#include "db/blob/blob_index.h"
#include "db/db_test_util.h"
#include "db/dbformat.h"
#include "db/table_cache.h"
#include "env/mock_env.h"
#include "file/filename.h"
#include "port/port.h"
@@ -6651,6 +6653,572 @@ TEST_P(DBCompactionDirectIOTest, DirectIO) {
INSTANTIATE_TEST_CASE_P(DBCompactionDirectIOTest, DBCompactionDirectIOTest,
testing::Bool());
// With use_direct_io_for_compaction_reads OFF, compaction reads must stay
// buffered: neither the compaction-input FileOptions nor a kernel O_DIRECT
// open should fire. Runs on every platform (the sync points just don't fire
// where O_DIRECT isn't reachable). Pairs with
// UseDirectIoForCompactionReadsEndToEnd for the on case.
TEST_F(DBCompactionTest, UseDirectIoForCompactionReadsOffStaysBuffered) {
Options options = CurrentOptions();
Destroy(options);
options.create_if_missing = true;
options.disable_auto_compactions = true;
options.use_direct_reads = false;
options.use_direct_io_for_compaction_reads = false;
options.use_direct_io_for_flush_and_compaction = false;
std::atomic<bool> observed_direct_compaction_read{false};
std::atomic<int> observed_callbacks{0};
std::atomic<int> observed_odirect_opens{0};
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"CompactionJob::CreateInputIterator:InputFileOptions", [&](void* arg) {
const auto* fo = static_cast<const FileOptions*>(arg);
observed_callbacks.fetch_add(1, std::memory_order_relaxed);
if (fo->use_direct_reads) {
observed_direct_compaction_read.store(true,
std::memory_order_relaxed);
}
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"NewRandomAccessFile:O_DIRECT", [&](void* /*arg*/) {
observed_odirect_opens.fetch_add(1, std::memory_order_relaxed);
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
ASSERT_OK(TryReopen(options));
const std::string value(4096, 'v');
for (int i = 0; i < 64; ++i) {
ASSERT_OK(Put(Key(i), value));
}
ASSERT_OK(Flush());
for (int i = 0; i < 64; ++i) {
ASSERT_OK(Put(Key(i), value));
}
ASSERT_OK(Flush());
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_GT(observed_callbacks.load(), 0);
ASSERT_FALSE(observed_direct_compaction_read.load());
ASSERT_EQ(0, observed_odirect_opens.load());
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
Destroy(options);
}
TEST_F(DBCompactionTest,
UseDirectIoForCompactionReadsUsesBoundedEphemeralReaders) {
auto fs = std::make_shared<MockFileSystem>(Env::Default()->GetSystemClock(),
/*supports_direct_io=*/true);
std::unique_ptr<Env> mock_env = NewCompositeEnv(fs);
Options options = CurrentOptions();
options.env = mock_env.get();
Destroy(options);
options.create_if_missing = true;
options.disable_auto_compactions = true;
options.use_direct_reads = false;
options.use_direct_io_for_compaction_reads = true;
options.use_direct_io_for_flush_and_compaction = false;
options.max_subcompactions = 3;
options.statistics = CreateDBStatistics();
BlockBasedTableOptions table_options;
table_options.block_cache = NewLRUCache(64 << 20);
table_options.cache_index_and_filter_blocks = true;
table_options.pin_l0_filter_and_index_blocks_in_cache = true;
table_options.filter_policy.reset(NewBloomFilterPolicy(10));
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
std::atomic<int> fresh_reader_opens{0};
std::atomic<int> malformed_fresh_reader_options{0};
std::atomic<int> avoid_shared_metadata_cache_opens{0};
std::atomic<int> shared_metadata_cache_uses{0};
std::atomic<size_t> input_iterator_file_bound{0};
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"TableCache::FindTable:FreshTableReader", [&](void* arg) {
const auto* open_options = static_cast<TableCacheOpenOptions*>(arg);
fresh_reader_opens.fetch_add(1, std::memory_order_relaxed);
if (!open_options->open_ephemeral_table_reader ||
!open_options->avoid_shared_metadata_cache ||
!open_options->skip_filters) {
malformed_fresh_reader_options.fetch_add(1,
std::memory_order_relaxed);
}
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"BlockBasedTable::PrefetchIndexAndFilterBlocks:SharedMetadataCache",
[&](void* arg) {
const auto* context = static_cast<std::pair<bool, bool>*>(arg);
const bool avoid_shared_metadata_cache = context->first;
const bool use_cache = context->second;
if (avoid_shared_metadata_cache) {
avoid_shared_metadata_cache_opens.fetch_add(
1, std::memory_order_relaxed);
if (use_cache) {
shared_metadata_cache_uses.fetch_add(1, std::memory_order_relaxed);
}
}
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"VersionSet::MakeInputIterator:NewCompactionMergingIterator",
[&](void* arg) {
input_iterator_file_bound.fetch_add(*static_cast<size_t*>(arg),
std::memory_order_relaxed);
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
ASSERT_OK(TryReopen(options));
const std::string value(4096, 'v');
for (int i = 0; i < 64; ++i) {
ASSERT_OK(Put(Key(i), value));
}
ASSERT_OK(Flush());
for (int i = 0; i < 64; ++i) {
ASSERT_OK(Put(Key(i), value));
}
ASSERT_OK(Flush());
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_GT(fresh_reader_opens.load(), 0);
EXPECT_EQ(0, malformed_fresh_reader_options.load());
ASSERT_GT(avoid_shared_metadata_cache_opens.load(), 0);
EXPECT_EQ(0, shared_metadata_cache_uses.load());
ASSERT_GT(input_iterator_file_bound.load(), 0);
EXPECT_LE(static_cast<size_t>(fresh_reader_opens.load()),
input_iterator_file_bound.load());
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
Destroy(options);
}
// End-to-end check that use_direct_io_for_compaction_reads opens compaction
// inputs with O_DIRECT while use_direct_reads stays off (user reads buffered).
// The NewRandomAccessFile:O_DIRECT sync point in env/fs_posix.cc fires once
// per fresh open with the O_DIRECT flag, so this proves the kernel path, not
// just the FileOptions. Only runs on platforms that take the O_DIRECT path.
#if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS) && \
!defined(OS_WIN)
TEST_F(DBCompactionTest, UseDirectIoForCompactionReadsEndToEnd) {
if (!IsDirectIOSupported()) {
ROCKSDB_GTEST_BYPASS("Direct IO not supported");
return;
}
Options options = CurrentOptions();
Destroy(options);
options.create_if_missing = true;
options.disable_auto_compactions = true;
// User reads stay buffered, compaction reads should switch to O_DIRECT.
options.use_direct_reads = false;
options.use_direct_io_for_compaction_reads = true;
// Isolate the read-side change; leave the compaction write path buffered.
options.use_direct_io_for_flush_and_compaction = false;
// Sync-point callbacks fire on compaction threads while the test thread
// reads these counters, so use atomics to avoid a data race.
std::atomic<int> observed_run_starts{0};
std::atomic<int> observed_odirect_opens{0};
std::atomic<bool> observed_direct_compaction_read{false};
std::atomic<int> observed_callbacks{0};
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency({});
// Plumbing-level probe: the compaction-input FileOptions should carry
// use_direct_reads = true when the new flag is enabled.
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"CompactionJob::CreateInputIterator:InputFileOptions", [&](void* arg) {
const auto* fo = static_cast<const FileOptions*>(arg);
observed_callbacks.fetch_add(1, std::memory_order_relaxed);
if (fo != nullptr && fo->use_direct_reads) {
observed_direct_compaction_read.store(true,
std::memory_order_relaxed);
}
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"CompactionJob::Run():Start", [&](void* /*arg*/) {
observed_run_starts.fetch_add(1, std::memory_order_relaxed);
});
// Kernel-level probe: this fires only when open() is issued with O_DIRECT,
// proving we change the actual cache mode, not just the FileOptions struct.
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"NewRandomAccessFile:O_DIRECT", [&](void* /*arg*/) {
observed_odirect_opens.fetch_add(1, std::memory_order_relaxed);
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
Status s = TryReopen(options);
if (s.IsNotSupported() || s.IsInvalidArgument()) {
ROCKSDB_GTEST_BYPASS(
"Direct IO reads not supported in this test environment");
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
return;
}
ASSERT_OK(s);
// Produce two L0 files with OVERLAPPING key ranges so that CompactRange has
// actual merge work to do (otherwise RocksDB performs a trivial file move
// and never constructs a CompactionJob).
const std::string value(4096, 'v');
for (int i = 0; i < 64; ++i) {
ASSERT_OK(Put(Key(i), value));
}
ASSERT_OK(Flush());
for (int i = 0; i < 64; ++i) {
ASSERT_OK(Put(Key(i), value));
}
ASSERT_OK(Flush());
// User reads should still go through the buffered path. Confirm that the
// option does not silently flip use_direct_reads for user reads.
for (int i = 0; i < 8; ++i) {
std::string actual;
ASSERT_OK(db_->Get(ReadOptions(), Key(i), &actual));
ASSERT_EQ(value, actual);
}
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
// Wait for compaction to complete and CompactionJob to be constructed.
ASSERT_OK(dbfull()->TEST_WaitForCompact());
// Confirm the compaction actually ran; otherwise the missing sync-point hits
// would be a test-setup problem, not an option regression.
ASSERT_GT(observed_run_starts.load(), 0)
<< "CompactionJob::Run():Start never fired; CompactRange did not "
"schedule a compaction.";
ASSERT_GT(observed_callbacks.load(), 0);
ASSERT_TRUE(observed_direct_compaction_read.load());
// At least one compaction-input open went through O_DIRECT. Without the
// TableCache bypass this would be zero, since compaction would reuse the
// buffered handles cached for user reads.
EXPECT_GT(observed_odirect_opens.load(), 0)
<< "no compaction-input opens went through O_DIRECT; "
"observed_odirect_opens="
<< observed_odirect_opens.load();
// Quick sanity sweep after compaction to confirm data is intact.
for (int i = 0; i < 64; ++i) {
std::string actual;
ASSERT_OK(db_->Get(ReadOptions(), Key(i), &actual));
ASSERT_EQ(value, actual);
}
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
Destroy(options);
}
// Exercise the LevelIterator bypass path (L1+ compactions) with range
// tombstones, where the ephemeral TableReader's lifetime is coupled to the
// range_tombstone_iter the file iterator returns. The end-to-end test above
// only makes two L0 files, which take the direct NewIterator path and never
// hit LevelIterator. Here we build L1/L2 with tombstones and compact L1->L2 so
// LevelIterator::NewFileIterator drives the bypass; a wrong reader/iterator
// lifetime would crash or trip sanitizers as LevelIterator switches files.
// Correctness is checked by computing the expected state of every key and
// asserting Get() matches: wave-2 puts beat wave-1, and each key's most-recent
// covering tombstone shadows older puts.
TEST_F(DBCompactionTest,
UseDirectIoForCompactionReadsLevelIteratorWithTombstones) {
if (!IsDirectIOSupported()) {
ROCKSDB_GTEST_BYPASS("Direct IO not supported");
return;
}
Options options = CurrentOptions();
Destroy(options);
options.create_if_missing = true;
options.disable_auto_compactions = true;
options.use_direct_reads = false;
options.use_direct_io_for_compaction_reads = true;
options.use_direct_io_for_flush_and_compaction = false;
// Small files / small level base so we can pack data into L1 and L2 with
// a few flushes and CompactRange calls instead of needing millions of keys.
options.write_buffer_size = 64 * 1024;
options.target_file_size_base = 64 * 1024;
options.max_bytes_for_level_base = 256 * 1024;
options.level0_file_num_compaction_trigger = 100; // never auto-trigger
std::atomic<int> observed_odirect_opens{0};
std::atomic<int> observed_run_starts{0};
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"NewRandomAccessFile:O_DIRECT", [&](void* /*arg*/) {
observed_odirect_opens.fetch_add(1, std::memory_order_relaxed);
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"CompactionJob::Run():Start", [&](void* /*arg*/) {
observed_run_starts.fetch_add(1, std::memory_order_relaxed);
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
Status s = TryReopen(options);
if (s.IsNotSupported() || s.IsInvalidArgument()) {
ROCKSDB_GTEST_BYPASS(
"Direct IO reads not supported in this test environment");
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
return;
}
ASSERT_OK(s);
// Use distinguishable values per wave so we can verify which wave's put
// won the merge for each key, not just "some put won".
const std::string wave1_value(1024, '1');
const std::string wave2_value(1024, '2');
auto write_batch = [&](int begin, int end, const std::string& value,
bool with_range_tombstone) {
for (int i = begin; i < end; ++i) {
ASSERT_OK(Put(Key(i), value));
}
if (with_range_tombstone) {
// Drop a slice in the middle of the just-written range. The
// DeleteRange follows the puts in this batch, so its sequence number
// is higher and it shadows the puts on [del_lo, del_hi) within this
// SST.
ASSERT_OK(db_->DeleteRange(WriteOptions(), db_->DefaultColumnFamily(),
Key(begin + (end - begin) / 4),
Key(begin + 3 * (end - begin) / 4)));
}
ASSERT_OK(Flush());
};
// Wave 1: four flushed SSTs covering [0, 800), each with a tombstone
// covering the middle half of its own range. Then compact down so the
// next phase exercises LevelIterator over L1+ files.
constexpr int kWave1Begin = 0;
constexpr int kWave1End = 800;
constexpr int kWave1BatchSize = 200;
for (int batch_begin = kWave1Begin; batch_begin < kWave1End;
batch_begin += kWave1BatchSize) {
write_batch(batch_begin, batch_begin + kWave1BatchSize, wave1_value,
/*with_range_tombstone=*/true);
}
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
ASSERT_OK(dbfull()->TEST_WaitForCompact());
// Wave 2: two more flushed SSTs at L0 that overlap wave 1, each with their
// own range tombstone. The puts in wave 2 have higher seqno than wave 1,
// so they win when not tombstoned.
struct Wave2Range {
int begin;
int end;
};
const Wave2Range wave2_ranges[] = {{50, 250}, {350, 550}};
for (const auto& r : wave2_ranges) {
write_batch(r.begin, r.end, wave2_value, /*with_range_tombstone=*/true);
}
const int run_starts_before = observed_run_starts.load();
const int odirect_before = observed_odirect_opens.load();
// Compact everything together, forcing a LevelIterator over the lower-level
// files on the bypass path. Wrong ephemeral reader / tombstone-iter
// lifetimes should trip sanitizers here.
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_GT(observed_run_starts.load(), run_starts_before)
<< "expected at least one compaction to run during the L1+ phase";
EXPECT_GT(observed_odirect_opens.load(), odirect_before)
<< "no compaction-input opens went through O_DIRECT during L1+ "
"compaction; LevelIterator bypass path may be broken";
// Compute the precise expected state per key from the test parameters.
// For each k in the wave-1 range:
// - If k is inside a wave-2 batch: wave-2 wins. NotFound iff k is in
// that batch's tombstone range; otherwise present with wave2_value.
// - Else: wave 1 wins. NotFound iff k is in its batch's tombstone
// range; otherwise present with wave1_value.
enum class Expectation { kAbsent, kWave1Value, kWave2Value };
auto classify = [&](int k) -> Expectation {
// Wave 2 first (it shadows wave 1 wherever it covers).
for (const auto& r : wave2_ranges) {
if (k >= r.begin && k < r.end) {
const int width = r.end - r.begin;
const int del_lo = r.begin + width / 4;
const int del_hi = r.begin + 3 * width / 4;
if (k >= del_lo && k < del_hi) {
return Expectation::kAbsent;
}
return Expectation::kWave2Value;
}
}
// Fall back to wave 1.
const int batch = (k - kWave1Begin) / kWave1BatchSize;
const int batch_begin = kWave1Begin + batch * kWave1BatchSize;
const int del_lo = batch_begin + kWave1BatchSize / 4;
const int del_hi = batch_begin + 3 * kWave1BatchSize / 4;
if (k >= del_lo && k < del_hi) {
return Expectation::kAbsent;
}
return Expectation::kWave1Value;
};
int present_w1 = 0;
int present_w2 = 0;
int absent = 0;
std::string actual;
for (int k = kWave1Begin; k < kWave1End; ++k) {
const Status get_s = db_->Get(ReadOptions(), Key(k), &actual);
const Expectation exp = classify(k);
switch (exp) {
case Expectation::kAbsent:
EXPECT_TRUE(get_s.IsNotFound())
<< "key " << k << " expected NotFound (covered by tombstone); "
<< "got status=" << get_s.ToString()
<< " value_len=" << (get_s.ok() ? actual.size() : 0);
++absent;
break;
case Expectation::kWave1Value:
ASSERT_OK(get_s) << "key " << k << " expected wave-1 value";
EXPECT_EQ(wave1_value, actual)
<< "key " << k << " expected wave-1 value but got a different one";
++present_w1;
break;
case Expectation::kWave2Value:
ASSERT_OK(get_s) << "key " << k << " expected wave-2 value";
EXPECT_EQ(wave2_value, actual)
<< "key " << k << " expected wave-2 value but got a different one";
++present_w2;
break;
}
}
// All three buckets should be non-empty, so the test really exercises both
// tombstone paths and the wave-2-wins path. A zero here means the setup
// drifted and the setup (not these checks) should be fixed.
EXPECT_GT(present_w1, 0);
EXPECT_GT(present_w2, 0);
EXPECT_GT(absent, 0);
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
Destroy(options);
}
// With the option enabled, run reader threads against the live DB while manual
// compactions run in the background. Each compaction-input file is open through
// an ephemeral O_DIRECT handle while the shared TableCache still serves user
// reads through a buffered handle, so the same SST is open in two cache modes
// at once. This stresses that coexistence under TSAN/ASAN/UBSAN; it asserts
// reads stay consistent and final values match the last writes, not anything
// about timing.
TEST_F(DBCompactionTest, UseDirectIoForCompactionReadsConcurrentReadStress) {
if (!IsDirectIOSupported()) {
ROCKSDB_GTEST_BYPASS("Direct IO not supported");
return;
}
Options options = CurrentOptions();
Destroy(options);
options.create_if_missing = true;
options.disable_auto_compactions = true;
options.use_direct_reads = false;
options.use_direct_io_for_compaction_reads = true;
options.use_direct_io_for_flush_and_compaction = false;
options.write_buffer_size = 64 * 1024;
options.target_file_size_base = 64 * 1024;
options.max_bytes_for_level_base = 256 * 1024;
options.level0_file_num_compaction_trigger = 100; // never auto-trigger
Status s = TryReopen(options);
if (s.IsNotSupported() || s.IsInvalidArgument()) {
ROCKSDB_GTEST_BYPASS(
"Direct IO reads not supported in this test environment");
return;
}
ASSERT_OK(s);
constexpr int kNumKeys = 512;
constexpr int kValueSize = 256;
const std::string base_value(kValueSize, 'b');
// Initial population, flushed into a few L0 SSTs.
for (int i = 0; i < kNumKeys; ++i) {
ASSERT_OK(Put(Key(i), base_value));
if (i > 0 && i % 128 == 0) {
ASSERT_OK(Flush());
}
}
ASSERT_OK(Flush());
std::atomic<bool> stop{false};
std::atomic<int64_t> reads_observed{0};
std::atomic<int64_t> read_errors{0};
// Reader threads loop until stop is set, recording any status that isn't OK
// or NotFound. The point is to surface Corruption/IOError/use-after-free, so
// the main thread can fail the test.
constexpr int kNumReaders = 4;
std::vector<port::Thread> readers;
readers.reserve(kNumReaders);
for (int t = 0; t < kNumReaders; ++t) {
readers.emplace_back([&, t]() {
std::string value;
while (!stop.load(std::memory_order_acquire)) {
const int k =
(t * 7919 +
static_cast<int>(reads_observed.load(std::memory_order_relaxed))) %
kNumKeys;
Status read_s = db_->Get(ReadOptions(), Key(k), &value);
if (!read_s.ok() && !read_s.IsNotFound()) {
read_errors.fetch_add(1, std::memory_order_relaxed);
}
reads_observed.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Drive a few rounds of writes and compactions on the main thread while
// the readers hammer Get(). Each round overwrites every key with a
// round-tagged value and then compacts.
constexpr int kRounds = 4;
std::string last_value = base_value;
for (int round = 0; round < kRounds; ++round) {
const std::string round_value(kValueSize,
static_cast<char>('A' + (round % 26)));
for (int i = 0; i < kNumKeys; ++i) {
ASSERT_OK(Put(Key(i), round_value));
if (i > 0 && i % 64 == 0) {
ASSERT_OK(Flush());
}
}
ASSERT_OK(Flush());
ASSERT_OK(dbfull()->CompactRange(CompactRangeOptions(), nullptr, nullptr));
ASSERT_OK(dbfull()->TEST_WaitForCompact());
last_value = round_value;
}
stop.store(true, std::memory_order_release);
for (auto& reader : readers) {
reader.join();
}
// Every key must now hold the final round's value. Catches wholesale
// corruption the sampling readers might miss, and confirms compaction
// finished under the bypass path.
std::string value;
for (int i = 0; i < kNumKeys; ++i) {
ASSERT_OK(db_->Get(ReadOptions(), Key(i), &value));
EXPECT_EQ(last_value, value);
}
EXPECT_GT(reads_observed.load(), 0)
<< "reader threads never observed a single read; test was a no-op";
EXPECT_EQ(0, read_errors.load())
<< "concurrent reads against compaction with bypass-path saw "
<< read_errors.load() << " non-OK/non-NotFound status returns";
Destroy(options);
}
#endif // !defined(OS_MACOSX) && !defined(OS_OPENBSD) && ...
class CompactionPriTest : public DBTestBase,
public testing::WithParamInterface<uint32_t> {
public:
+1 -1
View File
@@ -35,7 +35,7 @@ namespace ROCKSDB_NAMESPACE {
class DBTest2 : public DBTestBase {
public:
DBTest2() : DBTestBase("db_test2", /*env_do_fsync=*/true) {}
DBTest2() : DBTestBase("db_etc2_test", /*env_do_fsync=*/true) {}
};
TEST_F(DBTest2, OpenForReadOnly) {
+253 -34
View File
@@ -1,9 +1,12 @@
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// Copyright (c) Meta Platforms, Inc. and affiliates.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#include "db/db_test_util.h"
#include "rocksdb/convenience.h"
#include "rocksdb/metadata.h"
#include "rocksdb/sst_file_writer.h"
namespace ROCKSDB_NAMESPACE {
@@ -43,50 +46,98 @@ TEST_F(DBEtc3Test, AutoTuneManifestSize) {
ASSERT_EQ(DBOptions{}.max_manifest_space_amp_pct, 500);
Options options = CurrentOptions();
ASSERT_OK(db_->SetOptions({{"level0_file_num_compaction_trigger", "20"}}));
// Test setup: create many flushed files. Keep level compaction semantics so
// DeleteFilesInRanges can remove non-L0 files, but prevent automatic
// compactions and write stalls from adding unrelated behavior.
options.disable_auto_compactions = true;
options.level0_slowdown_writes_trigger = 100;
options.level0_stop_writes_trigger = 200;
// Use large column family names to essentially control the amount of payload
// data needed for the manifest file. Drop manifest entries don't include the
// CF name so are small.
// Test strategy: use large column family names to control the rough amount
// of payload added to the MANIFEST. Drop manifest entries do not include the
// CF name, so they are small.
//
// Most CF helper calls piggy-back a background manifest write so the main
// auto-tuning phases continue to test the unrelaxed background threshold
// even though CF manipulation itself is foreground. Phase-specific foreground
// checks disable that piggy-backed background write.
uint64_t prev_manifest_num = 0, cur_manifest_num = 0;
std::deque<ColumnFamilyHandle*> handles;
int counter = 5;
auto AddCfFn = [&]() {
auto UpdateManifestNumsFrom = [&](uint64_t before_manifest_num) {
prev_manifest_num = before_manifest_num;
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
};
auto BackgroundManifestWriteFn = [&]() {
uint64_t before_manifest_num = cur_manifest_num;
ASSERT_OK(Put("x", std::to_string(counter++)));
ASSERT_OK(Flush());
UpdateManifestNumsFrom(before_manifest_num);
};
auto AddCfFn = [&](bool include_background_manifest_write = true) {
uint64_t before_manifest_num = cur_manifest_num;
std::string name = "cf" + std::to_string(counter++);
name.resize(1000, 'a');
ASSERT_OK(db_->CreateColumnFamily(options, name, &handles.emplace_back()));
prev_manifest_num = cur_manifest_num;
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
if (include_background_manifest_write) {
BackgroundManifestWriteFn();
}
UpdateManifestNumsFrom(before_manifest_num);
};
auto DropCfFn = [&]() {
auto DropCfFn = [&](bool include_background_manifest_write = true) {
uint64_t before_manifest_num = cur_manifest_num;
ASSERT_OK(db_->DropColumnFamily(handles.front()));
ASSERT_OK(db_->DestroyColumnFamilyHandle(handles.front()));
handles.pop_front();
prev_manifest_num = cur_manifest_num;
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
};
auto TrivialManifestWriteFn = [&]() {
ASSERT_OK(Put("x", std::to_string(counter++)));
ASSERT_OK(Flush());
prev_manifest_num = cur_manifest_num;
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
if (include_background_manifest_write) {
BackgroundManifestWriteFn();
}
UpdateManifestNumsFrom(before_manifest_num);
};
// ---- Phase 1: foreground threshold relaxation is bounded ----
//
// Foreground operations should only get about 25% extra headroom, not an
// unbounded threshold. With 1000-byte CF names and a 3000-byte normal limit,
// the relaxed limit should allow the first four foreground-only CF additions
// but require rotation on the fifth.
options.max_manifest_file_size = 3000;
options.max_manifest_space_amp_pct = 0;
DestroyAndReopen(options);
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
prev_manifest_num = cur_manifest_num;
for (int i = 1; i <= 4; ++i) {
AddCfFn(/*include_background_manifest_write=*/false);
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
}
AddCfFn(/*include_background_manifest_write=*/false);
ASSERT_LT(prev_manifest_num, cur_manifest_num);
while (!handles.empty()) {
ASSERT_OK(db_->DestroyColumnFamilyHandle(handles.front()));
handles.pop_front();
}
// ---- Phase 2: no auto-tuning means frequent rotation ----
//
options.max_manifest_file_size = 1000000;
options.max_manifest_space_amp_pct = 0; // no auto-tuning yet
DestroyAndReopen(options);
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
prev_manifest_num = cur_manifest_num;
// With the generous (minimum) maximum manifest size, should not be rotated
// With the generous minimum manifest size, should not be rotated.
AddCfFn();
AddCfFn();
AddCfFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
// Change options for small max and (still) no auto-tuning
// Lower the minimum while still disabling auto-tuning.
ASSERT_OK(db_->SetDBOptions({{"max_manifest_file_size", "3000"}}));
// Takes effect on the next manifest write
TrivialManifestWriteFn();
BackgroundManifestWriteFn();
ASSERT_LT(prev_manifest_num, cur_manifest_num);
// Now we have to rewrite the whole manifest on each write because the
@@ -97,28 +148,31 @@ TEST_F(DBEtc3Test, AutoTuneManifestSize) {
ASSERT_LT(prev_manifest_num, cur_manifest_num);
AddCfFn();
ASSERT_LT(prev_manifest_num, cur_manifest_num);
TrivialManifestWriteFn();
BackgroundManifestWriteFn();
ASSERT_LT(prev_manifest_num, cur_manifest_num);
// ---- Phase 3: auto-tuning raises the background threshold ----
//
// Enabling auto-tuning should fix this, immediately for next manifest writes.
// This will allow up to double-ish the size of the compacted manifest,
// which last should have been 4000 + some bytes.
// This will allow up to roughly double the size of the compacted manifest,
// which now includes CF entries plus the piggy-backed background writes.
ASSERT_EQ(handles.size(), 4U);
ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "105"}}));
ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "95"}}));
// After 9 CF names should be enough to rotate the manifest
for (int i = 1; i <= 5; ++i) {
// Auto-tuning lets several more CF names accumulate in the MANIFEST before
// the piggy-backed background write crosses the threshold.
for (int i = 1; i <= 3; ++i) {
if ((i % 2) == 1) {
DropCfFn();
}
AddCfFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
}
TrivialManifestWriteFn();
AddCfFn();
ASSERT_LT(prev_manifest_num, cur_manifest_num);
// We now have a different last compacted manifest size, should be
// able to go beyond 9 CFs named in manifest this time.
// We now have a different last compacted manifest size, so the next
// threshold should be based on the newly compacted MANIFEST.
ASSERT_EQ(handles.size(), 6U);
DropCfFn();
@@ -128,11 +182,10 @@ TEST_F(DBEtc3Test, AutoTuneManifestSize) {
AddCfFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
}
// We've written 10 named CFs to the manifest. We should be able to
// dynamically change the auto-tuning still based on the last "compacted"
// manifest size of 7000 + some bytes.
// We should be able to dynamically change the auto-tuning still based on
// the last "compacted" manifest size.
ASSERT_OK(db_->SetDBOptions({{"max_manifest_space_amp_pct", "51"}}));
TrivialManifestWriteFn();
BackgroundManifestWriteFn();
ASSERT_LT(prev_manifest_num, cur_manifest_num);
// And the "compacted" manifest size has reset again, so should be changed
// again sooner.
@@ -141,13 +194,179 @@ TEST_F(DBEtc3Test, AutoTuneManifestSize) {
AddCfFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
}
// Enough for manifest change
AddCfFn();
// ---- Phase 4: foreground operations use relaxed threshold ----
//
// The current MANIFEST is now large enough for the next background manifest
// write to rotate it, but still small enough for foreground operations to use
// their 25% extra headroom. Assert that each foreground operation stays on
// the same MANIFEST, then verify the next background write rotates.
const std::string external_files_dir = dbname_ + "/external_files";
ASSERT_OK(env_->CreateDirIfMissing(external_files_dir));
auto WriteExternalSstFile = [&](const std::string& file_name,
const std::string& key,
const std::string& value) {
const std::string file_path = external_files_dir + "/" + file_name;
Status ignored = env_->DeleteFile(file_path);
ignored.PermitUncheckedError();
SstFileWriter sst_file_writer(EnvOptions(), options);
ASSERT_OK(sst_file_writer.Open(file_path));
ASSERT_OK(sst_file_writer.Put(key, value));
ASSERT_OK(sst_file_writer.Finish());
};
auto IngestExternalFileForegroundManifestWriteFn =
[&](std::string* ingested_key) {
uint64_t before_manifest_num = cur_manifest_num;
const std::string key = "z" + std::to_string(counter++);
const std::string value = "v" + std::to_string(counter++);
const std::string file_name =
"ingest_" + std::to_string(counter++) + ".sst";
const std::string file_path = external_files_dir + "/" + file_name;
WriteExternalSstFile(file_name, key, value);
ASSERT_OK(
db_->IngestExternalFile({file_path}, IngestExternalFileOptions()));
ASSERT_EQ(value, Get(key));
*ingested_key = key;
Status ignored = env_->DeleteFile(file_path);
ignored.PermitUncheckedError();
UpdateManifestNumsFrom(before_manifest_num);
};
auto CreateColumnFamilyWithImportForegroundManifestWriteFn = [&]() {
uint64_t before_manifest_num = cur_manifest_num;
const std::string cf_name = "import_cf" + std::to_string(counter++);
const std::string key = "import" + std::to_string(counter++);
const std::string value = "v" + std::to_string(counter++);
const std::string file_name =
"import_" + std::to_string(counter++) + ".sst";
const std::string file_path = external_files_dir + "/" + file_name;
WriteExternalSstFile(file_name, key, value);
LiveFileMetaData file_metadata;
file_metadata.name = file_name;
file_metadata.db_path = external_files_dir;
file_metadata.smallest_seqno = 0;
file_metadata.largest_seqno = 0;
file_metadata.level = 0;
ExportImportFilesMetaData metadata;
metadata.files.push_back(file_metadata);
metadata.db_comparator_name = options.comparator->Name();
ColumnFamilyHandle* import_handle = nullptr;
ASSERT_OK(db_->CreateColumnFamilyWithImport(options, cf_name,
ImportColumnFamilyOptions(),
metadata, &import_handle));
ASSERT_NE(import_handle, nullptr);
handles.push_back(import_handle);
std::string result;
ASSERT_OK(db_->Get(ReadOptions(), import_handle, key, &result));
ASSERT_EQ(value, result);
Status ignored = env_->DeleteFile(file_path);
ignored.PermitUncheckedError();
UpdateManifestNumsFrom(before_manifest_num);
};
auto DeleteFilesInRangesForegroundManifestWriteFn =
[&](const std::string& key) {
uint64_t before_manifest_num = cur_manifest_num;
const std::string limit = key + "\xff";
std::vector<RangeOpt> ranges;
ranges.emplace_back(key, limit);
ASSERT_OK(DeleteFilesInRanges(db_.get(), db_->DefaultColumnFamily(),
ranges.data(), ranges.size(),
/*include_end=*/false));
std::string result;
ASSERT_TRUE(db_->Get(ReadOptions(), key, &result).IsNotFound());
UpdateManifestNumsFrom(before_manifest_num);
};
// Column family manipulation.
AddCfFn(/*include_background_manifest_write=*/false);
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
// SetOptions should not write to the MANIFEST. If that regresses, the write
// should be treated as a background write and rotate here.
{
ASSERT_OK(db_->SetOptions({{"level0_slowdown_writes_trigger", "101"}}));
UpdateManifestNumsFrom(cur_manifest_num);
}
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
// External file ingestion.
std::string ingested_key;
IngestExternalFileForegroundManifestWriteFn(&ingested_key);
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
// Imported column family creation.
CreateColumnFamilyWithImportForegroundManifestWriteFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
// Physical file deletion by range.
DeleteFilesInRangesForegroundManifestWriteFn(ingested_key);
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
// Background flush.
BackgroundManifestWriteFn();
ASSERT_LT(prev_manifest_num, cur_manifest_num);
// ---- Phase 5: persisted compacted manifest size survives close/reopen ----
// Close with CFs still live. Reopen with reuse_manifest_on_open so the
// manifest is NOT rewritten from scratch. The persisted compacted size
// should be loaded and used for auto-tuning.
// At this point we have 8 CF handles plus default.
ASSERT_EQ(handles.size(), 8U);
// Collect CF names for reopen, then release handles (Close needs this)
std::vector<std::string> cf_names = {"default"};
for (auto* h : handles) {
cf_names.push_back(h->GetName());
}
for (auto* h : handles) {
ASSERT_OK(db_->DestroyColumnFamilyHandle(h));
}
handles.clear();
Close();
// Use a max_manifest_file_size below the reused manifest size. Auto-tuning
// with the persisted compacted size at 200% amp keeps the tuned threshold
// high enough. Without persistence, the threshold would be
// max(max_manifest_file_size, 0 * anything) = max_manifest_file_size.
//
// With max_manifest_file_size set to 3000, missing persisted compacted size
// would keep tuned = 3000 and the first AddCf would rotate because the
// reused manifest is already larger. With persisted compacted size loaded,
// the tuned threshold is based on the compacted size, so no rotation until
// the manifest grows well beyond the minimum.
options.max_manifest_file_size = 3000;
options.max_manifest_space_amp_pct = 200;
options.reuse_manifest_on_open = true;
uint64_t manifest_num_before_reopen = cur_manifest_num;
ReopenWithColumnFamilies(cf_names, options);
cur_manifest_num = dbfull()->TEST_Current_Manifest_FileNo();
// With persistence: the manifest file number should NOT have changed
// during reopen, because the persisted compacted size keeps the tuned
// threshold high enough. Without persistence, last_compacted = 0, so
// tuned = max_manifest_file_size = 3000, and the first LogAndApply
// during Open rotates the manifest because it is already larger than 3000.
ASSERT_EQ(manifest_num_before_reopen, cur_manifest_num);
// Adding CFs should still not trigger rotation because the tuned threshold
// from the persisted compacted size exceeds the current manifest size.
for (int i = 1; i <= 4; ++i) {
AddCfFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
}
for (int i = 1; i <= 4; ++i) {
AddCfFn();
ASSERT_EQ(prev_manifest_num, cur_manifest_num);
}
// Wrap up
while (!handles.empty()) {
DropCfFn();
DropCfFn(/*include_background_manifest_write=*/false);
}
}
+480 -136
View File
@@ -102,6 +102,7 @@
#include "table/get_context.h"
#include "table/merging_iterator.h"
#include "table/multiget_context.h"
#include "table/prepared_file_info.h"
#include "table/sst_file_dumper.h"
#include "table/table_builder.h"
#include "table/two_level_iterator.h"
@@ -175,6 +176,7 @@ DBImpl::DBImpl(const DBOptions& options, const std::string& dbname,
const bool seq_per_batch, const bool batch_per_txn,
bool read_only)
: dbname_(dbname),
read_only_(read_only),
own_info_log_(options.info_log == nullptr),
initial_db_options_(SanitizeOptions(dbname, options, read_only,
&init_logger_creation_s_)),
@@ -495,6 +497,21 @@ void DBImpl::WaitForAsyncFileOpen() {
}
}
void DBImpl::NotifyOnDBShutdownBegin() {
mutex_.AssertHeld();
if (shutdown_notification_sent_ || immutable_db_options_.listeners.empty()) {
return;
}
shutdown_notification_sent_ = true;
// release lock while notifying events
mutex_.Unlock();
for (const auto& listener : immutable_db_options_.listeners) {
listener->OnDBShutdownBegin(this);
}
mutex_.Lock();
}
// Will lock the mutex_, will wait for completion if wait is true
void DBImpl::CancelAllBackgroundWork(bool wait) {
ROCKS_LOG_INFO(immutable_db_options_.info_log,
@@ -541,6 +558,9 @@ void DBImpl::CancelAllBackgroundWork(bool wait) {
immutable_db_options_.compaction_service->CancelAwaitingJobs();
}
if (!already_shutting_down) {
NotifyOnDBShutdownBegin();
}
shutting_down_.store(true, std::memory_order_release);
bg_cv_.SignalAll();
if (!wait) {
@@ -653,7 +673,8 @@ void DBImpl::UnregisterBlobDirectWriteColumnFamily() {
Status DBImpl::MaybeWriteWalMarkersToManifestOnClose() {
mutex_.AssertHeld();
if (!mutable_db_options_.optimize_manifest_for_recovery ||
!opened_successfully_ || versions_ == nullptr || logs_.empty()) {
!opened_successfully_ || read_only_ || versions_ == nullptr ||
logs_.empty()) {
return Status::OK();
}
@@ -789,16 +810,35 @@ Status DBImpl::CloseHelper() {
bg_flush_scheduled_ || bg_purge_scheduled_ ||
bg_pressure_callback_in_progress_ ||
bg_async_file_open_state_ == AsyncFileOpenState::kScheduled ||
async_wal_precreate_state_ == AsyncWALPrecreateState::kScheduled ||
pending_purge_obsolete_files_ ||
error_handler_.IsRecoveryInProgress()) {
TEST_SYNC_POINT("DBImpl::~DBImpl:WaitJob");
bg_cv_.Wait();
}
// Release any opened-but-unpublished WAL writer after the in-flight worker
// has published its result. Clear the DB-owned async slot while holding
// mutex_, but destroy the detached writer after dropping mutex_ because
// log::Writer / WritableFileWriter destruction can flush and close the file.
// The file itself can be left behind as an empty future WAL; recovery already
// tolerates it and marks its file number used if observed.
UnpublishedWAL unused_async_wal = std::move(async_wal_precreate_wal_);
async_wal_precreate_state_ = AsyncWALPrecreateState::kNotScheduled;
if (unused_async_wal.writer) {
mutex_.Unlock();
unused_async_wal.Reset();
mutex_.Lock();
}
// Ensure subclasses don't forget to schedule async file opening
assert(!immutable_db_options_.open_files_async || !opened_successfully_ ||
bg_async_file_open_state_ != AsyncFileOpenState::kNotScheduled);
// No FileIngestionHandle from PrepareFileIngestion() may still be
// outstanding
assert(num_outstanding_prepared_ingestions_.load() == 0);
TEST_SYNC_POINT_CALLBACK("DBImpl::CloseHelper:PendingPurgeFinished",
&files_grabbed_for_purge_);
EraseThreadStatusDbInfo();
@@ -820,6 +860,8 @@ Status DBImpl::CloseHelper() {
if (default_cf_handle_ != nullptr || persist_stats_cf_handle_ != nullptr) {
// we need to delete handle outside of lock because it does its own locking
mutex_.Unlock();
TEST_SYNC_POINT("DBImpl::CloseHelper:CFHandleCleanupUnlocked");
TEST_SYNC_POINT("DBImpl::CloseHelper:CFHandleCleanupAllowed");
if (default_cf_handle_) {
delete default_cf_handle_;
default_cf_handle_ = nullptr;
@@ -840,7 +882,7 @@ Status DBImpl::CloseHelper() {
// manifest file), it is not able to identify live files correctly. As a
// result, all "live" files can get deleted by accident. However, corrupted
// manifest is recoverable by RepairDB().
if (opened_successfully_) {
if (opened_successfully_ && !read_only_) {
JobContext job_context(next_job_id_.fetch_add(1));
FindObsoleteFiles(&job_context, true);
@@ -887,25 +929,6 @@ Status DBImpl::CloseHelper() {
logs_.clear();
}
// Table cache may have table handles holding blocks from the block cache.
// We need to release them before the block cache is destroyed. The block
// cache may be destroyed inside versions_.reset(), when column family data
// list is destroyed, so leaving handles in table cache after
// versions_.reset() may cause issues. Here we clean all unreferenced handles
// in table cache, and (for certain builds/conditions) assert that no obsolete
// files are hanging around unreferenced (leak) in the table/blob file cache.
// Now we assume all user queries have finished, so only version set itself
// can possibly hold the blocks from block cache. After releasing unreferenced
// handles here, only handles held by version set left and inside
// versions_.reset(), we will release them. There, we need to make sure every
// time a handle is released, we erase it from the cache too. By doing that,
// we can guarantee that after versions_.reset(), table cache is empty
// so the cache can be safely destroyed.
#ifndef NDEBUG
TEST_VerifyNoObsoleteFilesCached(/*db_mutex_already_held=*/true);
#endif // !NDEBUG
table_cache_->EraseUnRefEntries();
for (auto& txn_entry : recovered_transactions_) {
delete txn_entry.second;
}
@@ -931,6 +954,42 @@ Status DBImpl::CloseHelper() {
}
}
// Drain obsolete-file purge work started AFTER the early CloseHelper wait
// near the top of this method. A late SuperVersion cleanup -- a dropped
// column family handle, or an in-flight iterator/Get whose ReadOptions or
// immutable_db_options_.avoid_unnecessary_blocking_io selected background
// purge -- can run in one of the mutex-unlocked windows above and enter the
// FindObsoleteFiles() -> PurgeObsoleteFiles(..., true) handoff. During that
// handoff pending_purge_obsolete_files_ is already nonzero, but
// bg_purge_scheduled_ may still be zero until SchedulePurge() runs at the end
// of PurgeObsoleteFiles(). Wait for both states while mutex_/this are still
// alive; bg_cv_.Wait() releases mutex_ so pending and scheduled purges can
// finish and signal.
TEST_SYNC_POINT("DBImpl::CloseHelper:BeforeFinalPurgeDrain");
while (pending_purge_obsolete_files_ || bg_purge_scheduled_) {
TEST_SYNC_POINT("DBImpl::CloseHelper:FinalPurgeDrainWait");
bg_cv_.Wait();
}
// Table cache may have table handles holding blocks from the block cache.
// We need to release them before the block cache is destroyed. The block
// cache may be destroyed inside versions_.reset(), when column family data
// list is destroyed, so leaving handles in table cache after
// versions_.reset() may cause issues. Here we clean all unreferenced handles
// in table cache, and (for certain builds/conditions) assert that no obsolete
// files are hanging around unreferenced (leak) in the table/blob file cache.
// Now we assume all user queries have finished, and close-time purge work has
// settled, so only version set itself can possibly hold the blocks from block
// cache. After releasing unreferenced handles here, only handles held by
// version set left and inside versions_.reset(), we will release them. There,
// we need to make sure every time a handle is released, we erase it from the
// cache too. By doing that, we can guarantee that after versions_.reset(),
// table cache is empty so the cache can be safely destroyed.
#ifndef NDEBUG
TEST_VerifyNoObsoleteFilesCached(/*db_mutex_already_held=*/true);
#endif // !NDEBUG
table_cache_->EraseUnRefEntries();
versions_.reset();
mutex_.Unlock();
if (db_lock_ != nullptr) {
@@ -1809,6 +1868,8 @@ Status DBImpl::SetDBOptions(
// TODO(xiez): clarify why apply optimize for read to write options
file_options_for_compaction_ = fs_->OptimizeForCompactionTableRead(
file_options_for_compaction_, immutable_db_options_);
TEST_SYNC_POINT_CALLBACK("DBImpl::SetDBOptions:FileOptionsForCompaction",
&file_options_for_compaction_);
if (wal_other_option_changed || wal_size_option_changed) {
WriteThread::Writer w;
write_thread_.EnterUnbatched(&w, &mutex_);
@@ -2440,30 +2501,8 @@ struct SuperVersionHandle {
static void CleanupSuperVersionHandle(void* arg1, void* /*arg2*/) {
SuperVersionHandle* sv_handle = static_cast<SuperVersionHandle*>(arg1);
if (sv_handle->super_version->Unref()) {
// Job id == 0 means that this is not our background process, but rather
// user thread
JobContext job_context(0);
sv_handle->mu->Lock();
sv_handle->super_version->Cleanup();
sv_handle->db->FindObsoleteFiles(&job_context, false, true);
if (sv_handle->background_purge) {
sv_handle->db->ScheduleBgLogWriterClose(&job_context);
sv_handle->db->AddSuperVersionsToFreeQueue(sv_handle->super_version);
sv_handle->db->SchedulePurge();
}
sv_handle->mu->Unlock();
if (!sv_handle->background_purge) {
delete sv_handle->super_version;
}
if (job_context.HaveSomethingToDelete()) {
sv_handle->db->PurgeObsoleteFiles(job_context,
sv_handle->background_purge);
}
job_context.Clean();
}
sv_handle->db->CleanupIteratorSuperVersion(sv_handle->super_version,
sv_handle->background_purge);
delete sv_handle;
}
@@ -2485,7 +2524,8 @@ static void CleanupGetMergeOperandsState(void* arg1, void* /*arg2*/) {
InternalIterator* DBImpl::NewInternalIterator(
const ReadOptions& read_options, ColumnFamilyData* cfd,
SuperVersion* super_version, Arena* arena, SequenceNumber sequence,
bool allow_unprepared_value, ArenaWrappedDBIter* db_iter) {
bool allow_unprepared_value, ArenaWrappedDBIter* db_iter,
const MultiScanArgs* scan_opts) {
InternalIterator* internal_iter;
assert(arena != nullptr);
auto prefix_extractor =
@@ -2497,47 +2537,62 @@ InternalIterator* DBImpl::NewInternalIterator(
// here, and no unit test cares about the value provided here.
!read_options.total_order_seek && prefix_extractor != nullptr,
read_options.iterate_upper_bound);
// Collect iterator for mutable memtable
auto mem_iter = super_version->mem->NewIterator(
read_options, super_version->GetSeqnoToTimeMapping(), arena,
super_version->mutable_cf_options.prefix_extractor.get(),
/*for_flush=*/false);
Status s;
if (!read_options.ignore_range_deletions) {
std::unique_ptr<TruncatedRangeDelIterator> mem_tombstone_iter;
auto range_del_iter = super_version->mem->NewRangeTombstoneIterator(
read_options, sequence, false /* immutable_memtable */);
if (range_del_iter == nullptr || range_del_iter->empty()) {
delete range_del_iter;
const Comparator* user_comparator = cfd->user_comparator();
const bool mem_intersects =
!super_version->mem->IsEmpty() &&
MultiScanIntersectsMemTable(super_version->mem, read_options,
super_version->GetSeqnoToTimeMapping(),
prefix_extractor, scan_opts, user_comparator);
if (scan_opts == nullptr || mem_intersects) {
// Collect iterator for mutable memtable
auto mem_iter = super_version->mem->NewIterator(
read_options, super_version->GetSeqnoToTimeMapping(), arena,
super_version->mutable_cf_options.prefix_extractor.get(),
/*for_flush=*/false);
if (!read_options.ignore_range_deletions) {
std::unique_ptr<TruncatedRangeDelIterator> mem_tombstone_iter;
std::unique_ptr<FragmentedRangeTombstoneIterator> range_del_iter(
super_version->mem->NewRangeTombstoneIterator(
read_options, sequence, false /* immutable_memtable */));
if (range_del_iter != nullptr && !range_del_iter->empty()) {
mem_tombstone_iter = std::make_unique<TruncatedRangeDelIterator>(
std::move(range_del_iter), &cfd->ioptions().internal_comparator,
nullptr /* smallest */, nullptr /* largest */);
}
merge_iter_builder.AddPointAndTombstoneIterator(
mem_iter, std::move(mem_tombstone_iter));
} else {
mem_tombstone_iter = std::make_unique<TruncatedRangeDelIterator>(
std::unique_ptr<FragmentedRangeTombstoneIterator>(range_del_iter),
&cfd->ioptions().internal_comparator, nullptr /* smallest */,
nullptr /* largest */);
merge_iter_builder.AddIterator(mem_iter);
}
merge_iter_builder.AddPointAndTombstoneIterator(
mem_iter, std::move(mem_tombstone_iter));
} else {
merge_iter_builder.AddIterator(mem_iter);
} else if (scan_opts != nullptr) {
merge_iter_builder.SetMemtablePruned(true);
}
// Collect all needed child iterators for immutable memtables
if (s.ok()) {
if (s.ok() &&
(scan_opts == nullptr || super_version->imm->GetTotalNumEntries() > 0)) {
// Collect all needed child iterators for immutable memtables. When scan
// ranges are provided, AddIterators prunes each immutable memtable
// individually.
super_version->imm->AddIterators(
read_options, super_version->GetSeqnoToTimeMapping(),
super_version->mutable_cf_options.prefix_extractor.get(),
&merge_iter_builder, !read_options.ignore_range_deletions);
&merge_iter_builder, !read_options.ignore_range_deletions, sequence,
scan_opts, user_comparator);
}
TEST_SYNC_POINT_CALLBACK("DBImpl::NewInternalIterator:StatusCallback", &s);
if (s.ok()) {
// Collect iterators for files in L0 - Ln
if (read_options.read_tier != kMemtableTier) {
super_version->current->AddIterators(read_options, file_options_,
&merge_iter_builder,
allow_unprepared_value);
super_version->current->AddIterators(
read_options, file_options_, &merge_iter_builder,
allow_unprepared_value, sequence, scan_opts);
}
internal_iter = merge_iter_builder.Finish(
read_options.ignore_range_deletions ? nullptr : db_iter);
if (internal_iter == nullptr) {
internal_iter = NewEmptyInternalIterator<Slice>(arena);
}
SuperVersionHandle* cleanup = new SuperVersionHandle(
this, &mutex_, super_version,
read_options.background_purge_on_iterator_cleanup ||
@@ -2551,6 +2606,35 @@ InternalIterator* DBImpl::NewInternalIterator(
return NewErrorInternalIterator<Slice>(s, arena);
}
void DBImpl::CleanupIteratorSuperVersion(SuperVersion* super_version,
bool background_purge) {
background_purge =
background_purge || immutable_db_options_.avoid_unnecessary_blocking_io;
if (super_version->Unref()) {
// Job id == 0 means that this is not our background process, but rather
// user thread
JobContext job_context(0);
mutex_.Lock();
super_version->Cleanup();
FindObsoleteFiles(&job_context, false, true);
if (background_purge) {
ScheduleBgLogWriterClose(&job_context);
AddSuperVersionsToFreeQueue(super_version);
SchedulePurge();
}
mutex_.Unlock();
if (!background_purge) {
delete super_version;
}
if (job_context.HaveSomethingToDelete()) {
PurgeObsoleteFiles(job_context, background_purge);
}
job_context.Clean();
}
}
ColumnFamilyHandle* DBImpl::DefaultColumnFamily() const {
return default_cf_handle_;
}
@@ -5820,6 +5904,7 @@ Status DBImpl::DeleteFilesInRanges(ColumnFamilyHandle* column_family,
}
VersionEdit edit;
edit.MarkForegroundOperation();
std::set<FileMetaData*> deleted_files;
JobContext job_context(next_job_id_.fetch_add(1), true);
{
@@ -5917,6 +6002,79 @@ Status DBImpl::GetLiveFilesChecksumInfo(FileChecksumList* checksum_list) {
return versions_->GetLiveFilesChecksumInfo(checksum_list);
}
Status DBImpl::GetPreparedFileInfoForExternalSstIngestion(
const std::string& file_path,
std::shared_ptr<const PreparedFileInfo>* file_info) {
if (file_info == nullptr) {
return Status::InvalidArgument("file_info must not be null");
}
file_info->reset();
const size_t file_name_pos = file_path.find_last_of("/\\");
const std::string file_name = file_name_pos == std::string::npos
? file_path
: file_path.substr(file_name_pos + 1);
uint64_t file_number = 0;
FileType file_type;
if (!ParseFileName(file_name, &file_number, &file_type) ||
file_type != kTableFile || file_number == 0) {
return Status::InvalidArgument("Invalid table file name: " + file_path);
}
int file_level = -1;
FileMetaData* file_meta = nullptr;
ColumnFamilyData* file_cfd = nullptr;
Version* file_version = nullptr;
std::shared_ptr<const TableProperties> table_properties;
ReadOptions read_options;
{
InstrumentedMutexLock l(&mutex_);
Status s = versions_->GetMetadataForFile(file_number, &file_level,
&file_meta, &file_cfd);
if (!s.ok()) {
return s;
}
const std::string expected_file_path = TableFileName(
file_cfd->ioptions().cf_paths, file_number, file_meta->fd.GetPathId());
if (file_path != expected_file_path) {
return Status::InvalidArgument("Path does not match live table file: " +
file_path);
}
file_cfd->Ref();
file_version = file_cfd->current();
file_version->Ref();
}
const Defer cleanup_refs([&]() {
InstrumentedMutexLock l(&mutex_);
file_version->Unref();
file_cfd->UnrefAndTryDelete();
});
Status s = file_cfd->table_cache()->GetTableProperties(
file_options_, read_options, file_cfd->internal_comparator(), *file_meta,
&table_properties, file_version->GetMutableCFOptions(),
false /* no_io */);
if (!s.ok()) {
return s;
}
assert(table_properties != nullptr);
auto prepared_file_info = std::make_shared<PreparedFileInfo>();
prepared_file_info->file_size = file_meta->fd.GetFileSize();
prepared_file_info->smallest = file_meta->smallest;
prepared_file_info->largest = file_meta->largest;
prepared_file_info->table_properties = *table_properties;
prepared_file_info->table_properties.key_largest_seqno =
file_meta->fd.largest_seqno;
prepared_file_info->table_properties.key_smallest_seqno =
file_meta->fd.smallest_seqno;
*file_info = std::move(prepared_file_info);
return s;
}
void DBImpl::GetColumnFamilyMetaData(ColumnFamilyHandle* column_family,
ColumnFamilyMetaData* cf_meta) {
assert(column_family);
@@ -6639,11 +6797,76 @@ Status DBImpl::IngestExternalFile(
return IngestExternalFiles({arg});
}
Status DBImpl::IngestExternalFiles(
const std::vector<IngestExternalFileArg>& args) {
PERF_TIMER_GUARD(file_ingestion_nanos);
// TODO: plumb Env::IOActivity, Env::IOPriority
const WriteOptions write_options;
class FileIngestionHandleImpl : public FileIngestionHandle {
public:
explicit FileIngestionHandleImpl(DBImpl* db) : db_(db) {}
~FileIngestionHandleImpl() override;
FileIngestionHandleImpl(const FileIngestionHandleImpl&) = delete;
FileIngestionHandleImpl& operator=(const FileIngestionHandleImpl&) = delete;
FileIngestionHandleImpl(FileIngestionHandleImpl&&) = delete;
FileIngestionHandleImpl& operator=(FileIngestionHandleImpl&&) = delete;
Status Abort() override;
DBImpl* const db_;
std::vector<ExternalSstFileIngestionJob> jobs_;
std::unique_ptr<std::list<uint64_t>::iterator> pending_output_elem_;
bool fill_cache_ = true;
// Set true once committed or aborted, so the destructor does not roll back.
bool consumed_ = false;
};
FileIngestionHandleImpl::~FileIngestionHandleImpl() {
if (consumed_ || db_ == nullptr) {
return;
}
// Dropped without commit or abort: roll back as a safety net.
db_->RollbackPreparedFileIngestion(this);
ROCKS_LOG_WARN(
db_->immutable_db_options_.info_log,
"[%zu CF(s)] File ingestion handle destroyed without commit or "
"abort; prepared files were rolled back.",
jobs_.size());
}
void DBImpl::RollbackPreparedFileIngestion(FileIngestionHandleImpl* const h) {
// Delete the staged internal files and release the reserved file numbers /
// pending-output protection, leaving the DB unchanged.
const Status rollback_status = Status::Incomplete("file ingestion aborted");
for (auto& job : h->jobs_) {
job.Cleanup(rollback_status);
}
{
InstrumentedMutexLock l(&mutex_);
ReleaseFileNumberFromPendingOutputs(h->pending_output_elem_);
}
h->consumed_ = true;
num_outstanding_prepared_ingestions_.fetch_sub(1);
}
Status FileIngestionHandleImpl::Abort() {
if (consumed_) {
return Status::InvalidArgument(
"file ingestion handle has already been committed or aborted");
}
db_->RollbackPreparedFileIngestion(this);
return Status::OK();
}
Status DBImpl::PrepareFileIngestion(
const std::vector<IngestExternalFileArg>& args,
std::unique_ptr<FileIngestionHandle>* handle) {
if (handle == nullptr) {
return Status::InvalidArgument("file ingestion handle output is null");
}
handle->reset();
// Recorded as INGEST_EXTERNAL_FILE_PREPARE_TIME on success below.
const bool record_ingest_micros =
stats_ != nullptr &&
stats_->get_stats_level() > StatsLevel::kExceptTimers;
const uint64_t prepare_start_micros =
record_ingest_micros ? immutable_db_options_.clock->NowMicros() : 0;
if (args.empty()) {
return Status::InvalidArgument("ingestion arg list is empty");
@@ -6668,6 +6891,17 @@ Status DBImpl::IngestExternalFiles(
"external_files[" + std::to_string(i) + "] is empty";
return Status::InvalidArgument(err_msg);
}
if (!args[i].file_infos.empty()) {
if (args[i].file_infos.size() != args[i].external_files.size()) {
return Status::InvalidArgument("file_infos[" + std::to_string(i) +
"] size must match external_files[" +
std::to_string(i) + "] size");
}
if (args[i].options.write_global_seqno) {
return Status::InvalidArgument(
"write_global_seqno is not supported when file_infos is set");
}
}
if (i && args[i].options.fill_cache != args[i - 1].options.fill_cache) {
return Status::InvalidArgument(
"fill_cache should be the same across ingestion options.");
@@ -6744,6 +6978,7 @@ Status DBImpl::IngestExternalFiles(
}
std::vector<ExternalSstFileIngestionJob> ingestion_jobs;
ingestion_jobs.reserve(num_cfs);
for (const auto& arg : args) {
auto* cfd = static_cast<ColumnFamilyHandleImpl*>(arg.column_family)->cfd();
ingestion_jobs.emplace_back(versions_.get(), cfd, immutable_db_options_,
@@ -6761,8 +6996,9 @@ Status DBImpl::IngestExternalFiles(
this);
Status es = ingestion_jobs[i].Prepare(
args[i].external_files, args[i].files_checksums,
args[i].files_checksum_func_names, args[i].atomic_replace_range,
args[i].file_temperature, start_file_number, super_version);
args[i].files_checksum_func_names, args[i].file_infos,
args[i].atomic_replace_range, args[i].file_temperature,
start_file_number, super_version);
// capture first error only
if (!es.ok() && status.ok()) {
status = es;
@@ -6777,8 +7013,9 @@ Status DBImpl::IngestExternalFiles(
this);
Status es = ingestion_jobs[0].Prepare(
args[0].external_files, args[0].files_checksums,
args[0].files_checksum_func_names, args[0].atomic_replace_range,
args[0].file_temperature, next_file_number, super_version);
args[0].files_checksum_func_names, args[0].file_infos,
args[0].atomic_replace_range, args[0].file_temperature,
next_file_number, super_version);
if (!es.ok()) {
status = es;
}
@@ -6793,8 +7030,86 @@ Status DBImpl::IngestExternalFiles(
return status;
}
auto handle_impl = std::make_unique<FileIngestionHandleImpl>(this);
handle_impl->jobs_ = std::move(ingestion_jobs);
handle_impl->pending_output_elem_ = std::move(pending_output_elem);
handle_impl->fill_cache_ = args[0].options.fill_cache;
if (record_ingest_micros) {
RecordTimeToHistogram(
stats_, INGEST_EXTERNAL_FILE_PREPARE_TIME,
immutable_db_options_.clock->NowMicros() - prepare_start_micros);
}
num_outstanding_prepared_ingestions_.fetch_add(1);
*handle = std::move(handle_impl);
return Status::OK();
}
Status DBImpl::CommitFileIngestionHandles(
std::vector<std::unique_ptr<FileIngestionHandle>> handles) {
if (handles.empty()) {
return Status::InvalidArgument("no file ingestion handles to commit");
}
// Validate the handles and group their jobs by column family, merging same-CF
// jobs into one so each column family commits via a single atomic version
// edit and one SuperVersion install.
Status status;
std::vector<FileIngestionHandleImpl*> hs;
hs.reserve(handles.size());
std::vector<ExternalSstFileIngestionJob*> ingestion_jobs;
const bool fill_cache =
static_cast<FileIngestionHandleImpl*>(handles[0].get())->fill_cache_;
int max_file_opening_threads = 1;
{
UnorderedMap<ColumnFamilyData*, ExternalSstFileIngestionJob*>
primary_job_for_cfd;
for (const auto& handle : handles) {
assert(handle);
auto* h = static_cast<FileIngestionHandleImpl*>(handle.get());
assert(h->db_ == this);
if (h->consumed_) {
status = Status::InvalidArgument(
"file ingestion handle has already been committed or aborted");
}
if (h->fill_cache_ != fill_cache) {
status = Status::InvalidArgument(
"fill cache arg must be consistent across all handles");
}
if (!status.ok()) {
return status;
}
hs.push_back(h);
for (auto& job : h->jobs_) {
max_file_opening_threads =
std::max(max_file_opening_threads, job.file_opening_threads());
auto [it, inserted] =
primary_job_for_cfd.try_emplace(job.GetColumnFamilyData(), &job);
if (inserted) {
ingestion_jobs.push_back(&job);
} else {
status = it->second->MergeForSameColumnFamily(&job);
if (!status.ok()) {
return status;
}
}
}
}
}
const size_t num_jobs = ingestion_jobs.size();
// TODO: plumb Env::IOActivity, Env::IOPriority
const WriteOptions write_options;
// Decided here (not in Prepare) so the histograms reflect the stats level at
// commit time; recorded only on success below.
const bool record_ingest_micros =
stats_ != nullptr &&
stats_->get_stats_level() > StatsLevel::kExceptTimers;
const uint64_t run_start_micros =
record_ingest_micros ? immutable_db_options_.clock->NowMicros() : 0;
std::vector<SuperVersionContext> sv_ctxs;
for (size_t i = 0; i != num_cfs; ++i) {
for (size_t i = 0; i != num_jobs; ++i) {
sv_ctxs.emplace_back(true /* create_superversion */);
}
TEST_SYNC_POINT("DBImpl::IngestExternalFiles:BeforeJobsRun:0");
@@ -6805,9 +7120,9 @@ Status DBImpl::IngestExternalFiles(
// mutex so the lock-acquisition order is ingest_sst_lock -> DB mutex
// throughout. Use readlock so we still allow concurrent ingestions.
std::vector<std::unique_ptr<ReadLock>> ingest_read_locks;
ingest_read_locks.reserve(num_cfs);
for (size_t i = 0; i != num_cfs; ++i) {
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
ingest_read_locks.reserve(num_jobs);
for (auto* job : ingestion_jobs) {
auto* cfd = job->GetColumnFamilyData();
if (!cfd->IsDropped()) {
ingest_read_locks.emplace_back(
std::make_unique<ReadLock>(&cfd->GetIngestSstLock()));
@@ -6833,14 +7148,14 @@ Status DBImpl::IngestExternalFiles(
// So wait here to ensure there is no pending write to memtable.
WaitForPendingWrites();
num_running_ingest_file_ += static_cast<int>(num_cfs);
num_running_ingest_file_ += static_cast<int>(num_jobs);
TEST_SYNC_POINT("DBImpl::IngestExternalFile:AfterIncIngestFileCounter");
TEST_SYNC_POINT("DBImpl::IngestExternalFile:AfterIncIngestFileCounter:2");
bool at_least_one_cf_need_flush = false;
std::vector<bool> need_flush(num_cfs, false);
for (size_t i = 0; i != num_cfs; ++i) {
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
std::vector<bool> need_flush(num_jobs, false);
for (size_t i = 0; i != num_jobs; ++i) {
auto* cfd = ingestion_jobs[i]->GetColumnFamilyData();
if (cfd->IsDropped()) {
// TODO (yanqin) investigate whether we should abort ingestion or
// proceed with other non-dropped column families.
@@ -6849,7 +7164,7 @@ Status DBImpl::IngestExternalFiles(
break;
}
bool tmp = false;
status = ingestion_jobs[i].NeedsFlush(&tmp, cfd->GetSuperVersion());
status = ingestion_jobs[i]->NeedsFlush(&tmp, cfd->GetSuperVersion());
need_flush[i] = tmp;
at_least_one_cf_need_flush = (at_least_one_cf_need_flush || tmp);
if (!status.ok()) {
@@ -6869,11 +7184,11 @@ Status DBImpl::IngestExternalFiles(
{} /* provided_candidate_cfds */, true /* entered_write_thread */);
mutex_.Lock();
} else {
for (size_t i = 0; i != num_cfs; ++i) {
for (size_t i = 0; i != num_jobs; ++i) {
if (need_flush[i]) {
mutex_.Unlock();
status =
FlushMemTable(ingestion_jobs[i].GetColumnFamilyData(),
FlushMemTable(ingestion_jobs[i]->GetColumnFamilyData(),
flush_opts, FlushReason::kExternalFileIngestion,
true /* entered_write_thread */);
mutex_.Lock();
@@ -6884,22 +7199,22 @@ Status DBImpl::IngestExternalFiles(
}
}
if (status.ok()) {
for (size_t i = 0; i != num_cfs; ++i) {
for (size_t i = 0; i != num_jobs; ++i) {
if (immutable_db_options_.atomic_flush || need_flush[i]) {
ingestion_jobs[i].SetFlushedBeforeRun();
ingestion_jobs[i]->SetFlushedBeforeRun();
}
}
}
}
// Run ingestion jobs.
if (status.ok()) {
for (size_t i = 0; i != num_cfs; ++i) {
for (size_t i = 0; i != num_jobs; ++i) {
mutex_.AssertHeld();
status = ingestion_jobs[i].Run();
status = ingestion_jobs[i]->Run();
if (!status.ok()) {
break;
}
ingestion_jobs[i].RegisterRange();
ingestion_jobs[i]->RegisterRange();
}
}
// Now that Run() has assigned the actual seqno for each ingested file,
@@ -6911,9 +7226,9 @@ Status DBImpl::IngestExternalFiles(
// next conversion observes the new barrier and refuses any insert
// with insert_seq < assigned.
if (status.ok()) {
for (size_t i = 0; i != num_cfs; ++i) {
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
SequenceNumber assigned = ingestion_jobs[i].MaxAssignedSequenceNumber();
for (auto* job : ingestion_jobs) {
auto* cfd = job->GetColumnFamilyData();
SequenceNumber assigned = job->MaxAssignedSequenceNumber();
if (assigned > 0) {
cfd->mem()->BumpIngestSeqnoBarrier(assigned);
}
@@ -6921,16 +7236,18 @@ Status DBImpl::IngestExternalFiles(
}
if (status.ok()) {
ReadOptions read_options;
read_options.fill_cache = args[0].options.fill_cache;
read_options.fill_cache = fill_cache;
autovector<ColumnFamilyData*> cfds_to_commit;
autovector<autovector<VersionEdit*>> edit_lists;
uint32_t num_entries = 0;
for (size_t i = 0; i != num_cfs; ++i) {
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
for (auto* job : ingestion_jobs) {
auto* cfd = job->GetColumnFamilyData();
assert(!cfd->IsDropped());
cfds_to_commit.push_back(cfd);
autovector<VersionEdit*> edit_list;
edit_list.push_back(ingestion_jobs[i].edit());
auto* edit = job->edit();
edit->MarkForegroundOperation();
edit_list.push_back(edit);
edit_lists.push_back(edit_list);
++num_entries;
}
@@ -6943,10 +7260,11 @@ Status DBImpl::IngestExternalFiles(
}
assert(0 == num_entries);
}
status =
versions_->LogAndApply(cfds_to_commit, read_options, write_options,
edit_lists, &mutex_, directories_.GetDbDir());
status = versions_->LogAndApply(
cfds_to_commit, read_options, write_options, edit_lists, &mutex_,
directories_.GetDbDir(), false /* new_descriptor_log */,
nullptr /* new_cf_options */, {} /* manifest_wcbs */, {} /* pre_cb */,
max_file_opening_threads);
// It is safe to update VersionSet last seqno here after LogAndApply since
// LogAndApply persists last sequence number from VersionEdits,
// which are from file's largest seqno and not from VersionSet.
@@ -6955,11 +7273,10 @@ Status DBImpl::IngestExternalFiles(
// mutex when persisting MANIFEST file, and the snapshots taken during
// that period will not be stable if VersionSet last seqno is updated
// before LogAndApply.
SequenceNumber max_assigned_seqno =
ingestion_jobs[0].MaxAssignedSequenceNumber();
for (size_t i = 1; i != num_cfs; ++i) {
max_assigned_seqno = std::max(
max_assigned_seqno, ingestion_jobs[i].MaxAssignedSequenceNumber());
SequenceNumber max_assigned_seqno = 0;
for (auto* job : ingestion_jobs) {
max_assigned_seqno =
std::max(max_assigned_seqno, job->MaxAssignedSequenceNumber());
}
if (max_assigned_seqno > 0) {
const SequenceNumber last_seqno = versions_->LastSequence();
@@ -6971,17 +7288,17 @@ Status DBImpl::IngestExternalFiles(
}
}
for (auto& job : ingestion_jobs) {
job.UnregisterRange();
for (auto* job : ingestion_jobs) {
job->UnregisterRange();
}
if (status.ok()) {
for (size_t i = 0; i != num_cfs; ++i) {
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
for (size_t i = 0; i != num_jobs; ++i) {
auto* cfd = ingestion_jobs[i]->GetColumnFamilyData();
assert(!cfd->IsDropped());
InstallSuperVersionAndScheduleWork(cfd, &sv_ctxs[i]);
#ifndef NDEBUG
if (0 == i && num_cfs > 1) {
if (0 == i && num_jobs > 1) {
TEST_SYNC_POINT("DBImpl::IngestExternalFiles:InstallSVForFirstCF:0");
TEST_SYNC_POINT("DBImpl::IngestExternalFiles:InstallSVForFirstCF:1");
}
@@ -7006,12 +7323,14 @@ Status DBImpl::IngestExternalFiles(
PERF_TIMER_STOP(file_ingestion_blocking_live_writes_nanos);
if (status.ok()) {
for (auto& job : ingestion_jobs) {
job.UpdateStats();
for (auto* job : ingestion_jobs) {
job->UpdateStats();
}
for (auto* h : hs) {
ReleaseFileNumberFromPendingOutputs(h->pending_output_elem_);
}
}
ReleaseFileNumberFromPendingOutputs(pending_output_elem);
num_running_ingest_file_ -= static_cast<int>(num_cfs);
num_running_ingest_file_ -= static_cast<int>(num_jobs);
if (0 == num_running_ingest_file_) {
bg_cv_.SignalAll();
}
@@ -7019,24 +7338,46 @@ Status DBImpl::IngestExternalFiles(
}
// mutex_ is unlocked here
// Cleanup
for (size_t i = 0; i != num_cfs; ++i) {
sv_ctxs[i].Clean();
// This may rollback jobs that have completed successfully. This is
// intended for atomicity.
ingestion_jobs[i].Cleanup(status);
for (auto& sv_ctx : sv_ctxs) {
sv_ctx.Clean();
}
if (status.ok()) {
for (size_t i = 0; i != num_cfs; ++i) {
auto* cfd = ingestion_jobs[i].GetColumnFamilyData();
if (!cfd->IsDropped()) {
NotifyOnExternalFileIngested(cfd, ingestion_jobs[i]);
}
if (!status.ok()) {
// The atomic commit failed; nothing was made visible. The handles are NOT
// consumed, so each one's destructor rolls it back
return status;
}
for (auto* job : ingestion_jobs) {
job->Cleanup(status);
auto* cfd = job->GetColumnFamilyData();
if (!cfd->IsDropped()) {
NotifyOnExternalFileIngested(cfd, *job);
}
}
for (auto* h : hs) {
h->consumed_ = true;
num_outstanding_prepared_ingestions_.fetch_sub(1);
}
// Record commit latency.
if (record_ingest_micros) {
RecordTimeToHistogram(
stats_, INGEST_EXTERNAL_FILE_RUN_TIME,
immutable_db_options_.clock->NowMicros() - run_start_micros);
}
return status;
}
Status DBImpl::IngestExternalFiles(
const std::vector<IngestExternalFileArg>& args) {
PERF_TIMER_GUARD(file_ingestion_nanos);
std::unique_ptr<FileIngestionHandle> handle;
Status status = PrepareFileIngestion(args, &handle);
if (!status.ok()) {
return status;
}
return CommitFileIngestionHandle(std::move(handle));
}
Status DBImpl::CreateColumnFamilyWithImport(
const ColumnFamilyOptions& options, const std::string& column_family_name,
const ImportColumnFamilyOptions& import_options,
@@ -7078,6 +7419,7 @@ Status DBImpl::CreateColumnFamilyWithImport(
SuperVersionContext dummy_sv_ctx(/* create_superversion */ true);
VersionEdit dummy_edit;
dummy_edit.MarkForegroundOperation();
uint64_t next_file_number = 0;
std::unique_ptr<std::list<uint64_t>::iterator> pending_output_elem;
{
@@ -7135,9 +7477,10 @@ Status DBImpl::CreateColumnFamilyWithImport(
// Install job edit [Mutex will be unlocked here]
if (status.ok()) {
status = versions_->LogAndApply(cfd, read_options, write_options,
import_job.edit(), &mutex_,
directories_.GetDbDir());
auto* edit = import_job.edit();
edit->MarkForegroundOperation();
status = versions_->LogAndApply(cfd, read_options, write_options, edit,
&mutex_, directories_.GetDbDir());
if (status.ok()) {
InstallSuperVersionForConfigChange(cfd, &sv_context);
}
@@ -7568,6 +7911,7 @@ Status DBImpl::ReserveFileNumbersBeforeIngestion(
// reuse the file number that has already assigned to the internal file,
// and this will overwrite the external file. To protect the external
// file, we have to make sure the file number will never being reused.
dummy_edit.MarkForegroundOperation();
s = versions_->LogAndApply(cfd, read_options, write_options, &dummy_edit,
&mutex_, directories_.GetDbDir());
if (s.ok()) {
+151 -11
View File
@@ -15,6 +15,8 @@
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <unordered_map>
@@ -74,6 +76,7 @@ namespace ROCKSDB_NAMESPACE {
class Arena;
class ArenaWrappedDBIter;
class FileIngestionHandleImpl;
class InMemoryStatsHistoryIterator;
class MemTable;
class PersistentStatsHistoryIterator;
@@ -573,6 +576,10 @@ class DBImpl : public DB {
const LiveFilesStorageInfoOptions& opts,
std::vector<LiveFileStorageInfo>* files) override;
Status GetPreparedFileInfoForExternalSstIngestion(
const std::string& file_path,
std::shared_ptr<const PreparedFileInfo>* file_info) override;
// Obtains the meta data of the specified column family of the DB.
// TODO(yhchiang): output parameter is placed in the end in this codebase.
void GetColumnFamilyMetaData(ColumnFamilyHandle* column_family,
@@ -602,6 +609,14 @@ class DBImpl : public DB {
Status IngestExternalFiles(
const std::vector<IngestExternalFileArg>& args) override;
using DB::PrepareFileIngestion;
Status PrepareFileIngestion(
const std::vector<IngestExternalFileArg>& args,
std::unique_ptr<FileIngestionHandle>* handle) override;
Status CommitFileIngestionHandles(
std::vector<std::unique_ptr<FileIngestionHandle>> handles) override;
using DB::CreateColumnFamilyWithImport;
Status CreateColumnFamilyWithImport(
const ColumnFamilyOptions& options, const std::string& column_family_name,
@@ -855,12 +870,22 @@ class DBImpl : public DB {
// memtable range tombstone iterator used by the underlying merging iterator.
// This range tombstone iterator can be refreshed later by db_iter.
// @param read_options Must outlive the returned iterator.
InternalIterator* NewInternalIterator(const ReadOptions& read_options,
ColumnFamilyData* cfd,
SuperVersion* super_version,
Arena* arena, SequenceNumber sequence,
bool allow_unprepared_value,
ArenaWrappedDBIter* db_iter = nullptr);
// @param sequence The snapshot sequence captured when the DB iterator was
// created. Child iterators must use this instead of dereferencing
// read_options.snapshot, which may be released before lazy initialization.
// @param scan_opts Optional bounded scan ranges used only to prune the
// iterator tree during lazy Prepare() initialization.
InternalIterator* NewInternalIterator(
const ReadOptions& read_options, ColumnFamilyData* cfd,
SuperVersion* super_version, Arena* arena, SequenceNumber sequence,
bool allow_unprepared_value, ArenaWrappedDBIter* db_iter = nullptr,
const MultiScanArgs* scan_opts = nullptr);
// Release a SuperVersion held by an iterator. This preserves the cleanup
// behavior used by materialized internal iterators even when the DB iterator
// never needed to lazily build its child iterator tree.
void CleanupIteratorSuperVersion(SuperVersion* super_version,
bool background_purge);
LogsWithPrepTracker* logs_with_prep_tracker() {
return &logs_with_prep_tracker_;
@@ -1372,6 +1397,7 @@ class DBImpl : public DB {
protected:
const std::string dbname_;
const bool read_only_;
// TODO(peterd): unify with VersionSet::db_id_
std::string db_id_;
// db_session_id_ is an identifier that gets reset
@@ -1453,6 +1479,13 @@ class DBImpl : public DB {
std::atomic<int> next_job_id_ = 1;
std::atomic<bool> shutting_down_ = false;
// Protected by mutex_. This is separate from shutting_down_ because
// OnDBShutdownBegin must fire before shutting_down_ is published, and the
// notification releases mutex_ while invoking listeners. Marking that the
// notification has started prevents a concurrent or reentrant
// CancelAllBackgroundWork() call from firing it again during that unlocked
// callback window.
bool shutdown_notification_sent_ = false;
// No new background jobs can be queued if true. This is used to prevent new
// background jobs from being queued after WaitForCompact() completes waiting
@@ -1527,6 +1560,16 @@ class DBImpl : public DB {
const Status& st,
const CompactionJobStats& job_stats,
int job_id);
// Fires the OnCompactionPreCommit listener callback. Mirrors
// NotifyOnCompactionCompleted but is invoked before ReleaseCompactionFiles()
// (i.e. while input files still have being_compacted == true). Idempotent:
// safe to call from multiple potential release sites; only fires once per
// compaction, and only if NotifyOnCompactionBegin previously fired.
void NotifyOnCompactionPreCommit(ColumnFamilyData* cfd, Compaction* c,
const Status& st,
const CompactionJobStats& job_stats,
int job_id);
void NotifyOnDBShutdownBegin();
void NotifyOnMemTableSealed(ColumnFamilyData* cfd,
const MemTableInfo& mem_table_info);
@@ -1887,6 +1930,7 @@ class DBImpl : public DB {
friend class WriteBatchWithIndex;
friend class WriteUnpreparedTxnDB;
friend class WriteUnpreparedTxn;
friend class FileIngestionHandleImpl;
friend class ForwardIterator;
friend struct SuperVersion;
@@ -2224,6 +2268,10 @@ class DBImpl : public DB {
void ReleaseFileNumberFromPendingOutputs(
std::unique_ptr<std::list<uint64_t>::iterator>& v);
// Rolls back one prepared file ingestion (delete its staged files, release
// the reserved file numbers)
void RollbackPreparedFileIngestion(FileIngestionHandleImpl* const h);
// Similar to pending_outputs, preserve OPTIONS file. Used for remote
// compaction.
std::list<uint64_t>::iterator CaptureOptionsFileNumber();
@@ -2574,10 +2622,15 @@ class DBImpl : public DB {
// Helper function to perform trivial move by updating manifest metadata
// without rewriting data files. This is called when IsTrivialMove() is true.
// REQUIRES: mutex held
// Returns: Status of the trivial move operation
Status PerformTrivialMove(Compaction& c, LogBuffer* log_buffer,
bool& compaction_released, size_t& moved_files,
size_t& moved_bytes);
// Populates the Compaction's edit with DeleteFile/AddFile entries for
// a trivial move (no data rewriting). Does not commit the edit.
void PrepareTrivialMoveEdit(Compaction& c, LogBuffer* log_buffer,
size_t& moved_files, size_t& moved_bytes);
// REQUIRES: mutex held, PrepareTrivialMoveEdit already called
// Commits the trivial move by calling LogAndApply on the prepared edit.
// Returns: Status of the manifest write
Status CommitTrivialMove(Compaction& c, bool& compaction_released);
// REQUIRES: mutex unlocked
void TrackOrUntrackFiles(const std::vector<std::string>& existing_data_files,
@@ -2810,7 +2863,68 @@ class DBImpl : public DB {
size_t GetWalPreallocateBlockSize(uint64_t write_buffer_size) const;
Env::WriteLifeTimeHint CalculateWALWriteHint() { return Env::WLTH_SHORT; }
IOStatus CreateWAL(const WriteOptions& write_options, uint64_t log_file_num,
// Returns true when async WAL precreation is enabled and compatible with the
// active WAL strategy. WAL recycling already avoids file creation latency, so
// precreation is disabled when recycle_log_file_num is non-zero.
bool AsyncWALPrecreateEnabled() const;
// A WAL file that has a reserved file number and may have an opened writer,
// but has not been added to DBImpl's in-memory logical WAL tracking lists
// (logs_ and alive_wal_files_).
struct UnpublishedWAL {
uint64_t log_number = 0;
std::unique_ptr<log::Writer> writer;
UnpublishedWAL() = default;
UnpublishedWAL(const UnpublishedWAL&) = delete;
UnpublishedWAL& operator=(const UnpublishedWAL&) = delete;
UnpublishedWAL(UnpublishedWAL&& other) noexcept {
*this = std::move(other);
}
UnpublishedWAL& operator=(UnpublishedWAL&& other) noexcept {
if (this != &other) {
log_number = other.log_number;
writer = std::move(other.writer);
other.Reset();
}
return *this;
}
void Reset() {
log_number = 0;
writer.reset();
}
};
// Reserves the next WAL file number and schedules a HIGH-priority background
// task to precreate that WAL file. A precreated WAL is not a logical WAL
// until a foreground WAL rotation consumes it.
void MaybeScheduleAsyncWALPrecreate(size_t preallocate_block_size);
// Background task for opening the reserved future WAL and publishing the
// result under mutex_.
static void BGWorkAsyncWALPrecreate(void* arg);
// Waits for an in-flight async WAL precreation and returns a prepared WAL if
// one is available. If precreation failed, returns an empty WAL and lets the
// foreground rotation create the WAL synchronously. Caller must hold mutex_.
UnpublishedWAL WaitForAsyncWALPrecreate();
// Opens and preallocates a WAL writer without writing logical WAL records.
// Used by async WAL precreation and by synchronous WAL creation.
IOStatus CreateWALWriter(const DBOptions& db_options, uint64_t log_file_num,
uint64_t recycle_log_number,
size_t preallocate_block_size,
UnpublishedWAL* new_wal);
// Starts an opened WAL file by writing the initial records required before it
// can be installed as the current WAL for foreground writes.
IOStatus StartWALFile(const WriteOptions& write_options,
const PredecessorWALInfo& predecessor_wal_info,
log::Writer* new_log);
IOStatus CreateWAL(const DBOptions& db_options,
const WriteOptions& write_options, uint64_t log_file_num,
uint64_t recycle_log_number, size_t preallocate_block_size,
const PredecessorWALInfo& predecessor_wal_info,
log::Writer** new_log);
@@ -3306,6 +3420,28 @@ class DBImpl : public DB {
AsyncFileOpenState bg_async_file_open_state_ =
AsyncFileOpenState::kNotScheduled;
// State machine for the single async WAL precreation slot protected by
// mutex_. Background precreation failure returns to kNotScheduled; foreground
// rotation handles it the same as no prepared WAL and creates one
// synchronously. kScheduled owns a reserved file number; kReady owns an
// opened writer that has not been started or added to logical WAL tracking.
enum class AsyncWALPrecreateState : uint8_t {
kNotScheduled = 0, // No WAL precreate work is in-flight or ready.
kScheduled, // Background task owns creation of the reserved WAL.
kReady, // Reserved WAL writer is open but not logically live.
};
// Protected by mutex_. Tracks at most one background precreated WAL. A
// precreated WAL is only reserved empty storage until SwitchMemtable()
// consumes it and installs it in DBImpl's in-memory logical WAL tracking
// lists (logs_ and alive_wal_files_).
AsyncWALPrecreateState async_wal_precreate_state_ =
AsyncWALPrecreateState::kNotScheduled;
// Reserved in-flight/ready precreated WAL. The writer is populated only while
// state is kReady.
UnpublishedWAL async_wal_precreate_wal_;
std::deque<ManualCompactionState*> manual_compaction_dequeue_;
// shall we disable deletion of obsolete files
@@ -3355,6 +3491,10 @@ class DBImpl : public DB {
// REQUIRES: mutex held
int num_running_ingest_file_ = 0;
// Number of FileIngestionHandle objects produced by PrepareFileIngestion()
// that have not been committed or destroyed yet.
std::atomic<uint32_t> num_outstanding_prepared_ingestions_{0};
WalManager wal_manager_;
// A value of > 0 temporarily disables scheduling of background work
+86 -12
View File
@@ -1581,9 +1581,8 @@ Status DBImpl::CompactFiles(const CompactionOptions& compact_options,
return s;
}
Status DBImpl::PerformTrivialMove(Compaction& c, LogBuffer* log_buffer,
bool& compaction_released,
size_t& moved_files, size_t& moved_bytes) {
void DBImpl::PrepareTrivialMoveEdit(Compaction& c, LogBuffer* log_buffer,
size_t& moved_files, size_t& moved_bytes) {
mutex_.AssertHeld();
ROCKS_LOG_BUFFER(log_buffer, "[%s] Moving %d files to level-%d\n",
@@ -1616,6 +1615,10 @@ Status DBImpl::PerformTrivialMove(Compaction& c, LogBuffer* log_buffer,
}
moved_files += c.num_input_files(l);
}
}
Status DBImpl::CommitTrivialMove(Compaction& c, bool& compaction_released) {
mutex_.AssertHeld();
// Install the new version
const ReadOptions read_options(Env::IOActivity::kCompaction);
@@ -1740,8 +1743,8 @@ Status DBImpl::CompactFilesImpl(
bool compaction_released = false;
size_t moved_files = 0;
size_t moved_bytes = 0;
Status status = PerformTrivialMove(
*c.get(), log_buffer, compaction_released, moved_files, moved_bytes);
PrepareTrivialMoveEdit(*c.get(), log_buffer, moved_files, moved_bytes);
Status status = CommitTrivialMove(*c.get(), compaction_released);
if (status.ok()) {
InstallSuperVersionAndScheduleWork(
@@ -2009,6 +2012,46 @@ void DBImpl::NotifyOnCompactionCompleted(
// flush process.
}
void DBImpl::NotifyOnCompactionPreCommit(
ColumnFamilyData* cfd, Compaction* c, const Status& st,
const CompactionJobStats& compaction_job_stats, const int job_id) {
if (immutable_db_options_.listeners.size() == 0U) {
return;
}
mutex_.AssertHeld();
if (shutting_down_.load(std::memory_order_acquire)) {
return;
}
// Only fire if OnCompactionBegin has fired for this compaction.
if (c->ShouldNotifyOnCompactionCompleted() == false) {
return;
}
// Idempotency: ensure listeners observe at most one
// OnCompactionPreCommit per compaction lifecycle, even though the
// notify helper is invoked at multiple potential ReleaseCompactionFiles()
// sites for safety.
if (c->WasNotifyOnCompactionPreCommitCalled()) {
return;
}
c->SetNotifyOnCompactionPreCommitCalled();
int num_l0_files = cfd->current()->storage_info()->NumLevelFiles(0);
// release lock while notifying events
mutex_.Unlock();
TEST_SYNC_POINT("DBImpl::NotifyOnCompactionPreCommit::UnlockMutex");
{
CompactionJobInfo info{};
info.num_l0_files = num_l0_files;
BuildCompactionJobInfo(cfd, c, st, compaction_job_stats, job_id, &info);
for (const auto& listener : immutable_db_options_.listeners) {
listener->OnCompactionPreCommit(this, info);
}
info.status.PermitUncheckedError();
}
mutex_.Lock();
}
// REQUIREMENT: block all background work by calling PauseBackgroundWork()
// before calling this function
// TODO (hx235): Replace Status::NotSupported() with Status::Aborted() for
@@ -3180,7 +3223,7 @@ void DBImpl::ResumeAllCompactions() {
void DBImpl::MaybeScheduleFlushOrCompaction() {
mutex_.AssertHeld();
TEST_SYNC_POINT("DBImpl::MaybeScheduleFlushOrCompaction:Start");
if (!opened_successfully_) {
if (!opened_successfully_ || read_only_) {
// Compaction may introduce data race to DB open
return;
}
@@ -4024,6 +4067,13 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
status = Status::ShutdownInProgress();
} else if (compaction_aborted_.load(std::memory_order_acquire) > 0) {
status = Status::Incomplete(Status::SubCode::kCompactionAborted);
if (!is_prepicked) {
// This automatic compaction was scheduled from compaction_queue_, but
// abort happened before PickCompactionFromQueue() could remove the
// queued CF. Restore the unscheduled count so ResumeAllCompactions()
// can schedule the still-queued work.
unscheduled_compactions_++;
}
} else if (is_manual &&
manual_compaction->canceled.load(std::memory_order_acquire)) {
status = Status::Incomplete(Status::SubCode::kManualCompactionPaused);
@@ -4031,10 +4081,14 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
} else {
status = error_handler_.GetBGError();
// If we get here, it means a hard error happened after this compaction
// was scheduled by MaybeScheduleFlushOrCompaction(), but before it got
// a chance to execute. Since we didn't pop a cfd from the compaction
// queue, increment unscheduled_compactions_
unscheduled_compactions_++;
// was scheduled by MaybeScheduleFlushOrCompaction(), but before it got a
// chance to execute. Since non-prepicked work consumed an unscheduled
// compaction credit without popping a cfd from the compaction queue,
// restore the credit here. Prepicked work was scheduled directly and did
// not consume unscheduled_compactions_.
if (!is_prepicked) {
unscheduled_compactions_++;
}
}
if (!status.ok()) {
@@ -4255,6 +4309,8 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
for (const auto& f : *c->inputs(0)) {
c->edit()->DeleteFile(c->level(), f->fd.GetNumber());
}
NotifyOnCompactionPreCommit(c->column_family_data(), c.get(), status,
compaction_job_stats, job_context->job_id);
status = versions_->LogAndApply(
c->column_family_data(), read_options, write_options, c->edit(),
&mutex_, directories_.GetDbDir(),
@@ -4473,6 +4529,8 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
++out_file_metadata_it;
}
NotifyOnCompactionPreCommit(c->column_family_data(), c.get(), status,
compaction_job_stats, job_context->job_id);
status = versions_->LogAndApply(
c->column_family_data(), read_options, write_options, c->edit(),
&mutex_, directories_.GetDbDir(),
@@ -4549,8 +4607,12 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
// Perform the trivial move
size_t moved_files = 0;
size_t moved_bytes = 0;
status = PerformTrivialMove(*c.get(), log_buffer, compaction_released,
moved_files, moved_bytes);
// Populate the edit first so that CompactionJobInfo has output file
// info when OnCompactionPreCommit fires.
PrepareTrivialMoveEdit(*c.get(), log_buffer, moved_files, moved_bytes);
NotifyOnCompactionPreCommit(c->column_family_data(), c.get(), status,
compaction_job_stats, job_context->job_id);
status = CommitTrivialMove(*c.get(), compaction_released);
io_s = versions_->io_status();
InstallSuperVersionAndScheduleWork(
c->column_family_data(), job_context->superversion_contexts.data());
@@ -4673,6 +4735,10 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
ReleaseOptionsFileNumber(min_options_file_number_elem);
}
// Fire OnCompactionPreCommit before compaction_job.Install runs
// ReleaseCompactionFiles in its manifest write callback.
NotifyOnCompactionPreCommit(c->column_family_data(), c.get(), status,
compaction_job_stats, job_context->job_id);
status = compaction_job.Install(&compaction_released);
io_s = compaction_job.io_status();
if (status.ok()) {
@@ -4692,6 +4758,14 @@ Status DBImpl::BackgroundCompaction(bool* made_progress,
if (c != nullptr) {
if (!compaction_released) {
// Safety net: if any of the paths above did not fire the
// OnCompactionPreCommit notification (e.g. status was not OK and
// an early-out skipped the LogAndApply / Install), still fire the
// notify here while files are about to be released. The notify is
// idempotent and is a no-op if it has already fired (or if Begin never
// fired).
NotifyOnCompactionPreCommit(c->column_family_data(), c.get(), status,
compaction_job_stats, job_context->job_id);
c->ReleaseCompactionFiles(status);
} else {
#ifndef NDEBUG
+1
View File
@@ -833,6 +833,7 @@ void DBImpl::PurgeObsoleteFiles(JobContext& state, bool schedule_only) {
}
wal_manager_.PurgeObsoleteWALFiles();
LogFlush(immutable_db_options_.info_log);
TEST_SYNC_POINT("DBImpl::PurgeObsoleteFiles:BeforePendingPurgeFinished");
InstrumentedMutexLock l(&mutex_);
--pending_purge_obsolete_files_;
assert(pending_purge_obsolete_files_ >= 0);
+212 -23
View File
@@ -21,6 +21,7 @@
#include "file/writable_file_writer.h"
#include "logging/logging.h"
#include "monitoring/persistent_stats_history.h"
#include "monitoring/statistics_impl.h"
#include "monitoring/thread_status_util.h"
#include "options/options_helper.h"
#include "rocksdb/options.h"
@@ -122,6 +123,16 @@ DBOptions SanitizeOptions(const std::string& dbname, const DBOptions& src,
result.recycle_log_file_num = 0;
}
if (result.async_wal_precreate && result.recycle_log_file_num != 0) {
// Async WAL precreation reserves a future log number, while WAL recycling
// chooses from old WAL files. Keep the recycling behavior and disable only
// the async optimization.
result.async_wal_precreate = false;
ROCKS_LOG_WARN(result.info_log,
"async_wal_precreate is disabled since "
"recycle_log_file_num is non-zero");
}
if (result.db_paths.size() == 0) {
result.db_paths.emplace_back(dbname, std::numeric_limits<uint64_t>::max());
} else if (result.wal_dir.empty()) {
@@ -244,6 +255,17 @@ Status DBImpl::ValidateOptions(const DBOptions& db_options) {
"then direct I/O reads (use_direct_reads) must be disabled. ");
}
if (db_options.allow_mmap_reads &&
db_options.use_direct_io_for_compaction_reads) {
// mmap reads and direct I/O share the same EnvOptions field, so enabling
// both would try to mmap and O_DIRECT the same reads. Reject it here rather
// than tripping a lower-level assert.
return Status::NotSupported(
"If memory mapped reads (allow_mmap_reads) are enabled "
"then compaction-only direct I/O reads "
"(use_direct_io_for_compaction_reads) must be disabled. ");
}
if (db_options.allow_mmap_writes &&
db_options.use_direct_io_for_flush_and_compaction) {
return Status::NotSupported(
@@ -495,7 +517,8 @@ Status DBImpl::Recover(
std::unique_ptr<FSRandomAccessFile> idfile;
FileOptions customized_fs(file_options_);
customized_fs.use_direct_reads |=
immutable_db_options_.use_direct_io_for_flush_and_compaction;
immutable_db_options_.use_direct_io_for_flush_and_compaction ||
immutable_db_options_.use_direct_io_for_compaction_reads;
const std::string& fname =
manifest_path.empty() ? current_fname : manifest_path;
s = fs_->NewRandomAccessFile(fname, customized_fs, &idfile, nullptr);
@@ -799,9 +822,19 @@ Status DBImpl::Recover(
if (!wal_files.empty()) {
if (error_if_wal_file_exists) {
return Status::Corruption(
"The db was opened in readonly mode with error_if_wal_file_exists"
"flag but a WAL file already exists");
for (const auto& wal_file : wal_files) {
uint64_t bytes;
s = env_->GetFileSize(wal_file.second, &bytes);
if (!s.ok()) {
return s;
}
if (bytes > 0) {
return Status::Corruption(
"The db was opened in readonly mode with "
"error_if_wal_file_exists flag but a non-empty WAL file "
"already exists");
}
}
} else if (error_if_data_exists_in_wals) {
for (auto& wal_file : wal_files) {
uint64_t bytes;
@@ -2213,6 +2246,16 @@ Status DBImpl::WriteLevel0TableForRecovery(int job_id, ColumnFamilyData* cfd,
constexpr int level = 0;
if (s.ok() && has_output) {
// Before publishing any recovery edit, make the new SST's directory entry
// durable. Reused MANIFESTs have no CURRENT update, and fresh MANIFEST
// publication should not be relied on to order the recovered SST entry.
s = GetDataDir(cfd, meta.fd.GetPathId())
->FsyncWithDirOptions(
IOOptions(), nullptr,
DirFsyncOptions(DirFsyncOptions::FsyncReason::kNewFileSynced));
}
if (s.ok() && has_output) {
edit->AddFile(level, meta.fd.GetNumber(), meta.fd.GetPathId(),
meta.fd.GetFileSize(), meta.smallest, meta.largest,
@@ -2377,16 +2420,17 @@ Status DB::OpenAndTrimHistory(
return s;
}
IOStatus DBImpl::CreateWAL(const WriteOptions& write_options,
uint64_t log_file_num, uint64_t recycle_log_number,
size_t preallocate_block_size,
const PredecessorWALInfo& predecessor_wal_info,
log::Writer** new_log) {
IOStatus DBImpl::CreateWALWriter(const DBOptions& db_options,
uint64_t log_file_num,
uint64_t recycle_log_number,
size_t preallocate_block_size,
UnpublishedWAL* new_wal) {
assert(new_wal);
assert(new_wal->log_number == 0);
assert(new_wal->writer == nullptr);
IOStatus io_s;
std::unique_ptr<FSWritableFile> lfile;
DBOptions db_options =
BuildDBOptions(immutable_db_options_, mutable_db_options_);
FileOptions opt_file_options =
fs_->OptimizeForLogWrite(file_options_, db_options);
opt_file_options.write_hint = CalculateWALWriteHint();
@@ -2424,21 +2468,162 @@ IOStatus DBImpl::CreateWAL(const WriteOptions& write_options,
Histograms::HISTOGRAM_ENUM_MAX /* hist_type */, listeners, nullptr,
tmp_set.Contains(FileType::kWalFile),
tmp_set.Contains(FileType::kWalFile)));
*new_log = new log::Writer(std::move(file_writer), log_file_num,
immutable_db_options_.recycle_log_file_num > 0,
immutable_db_options_.manual_wal_flush,
immutable_db_options_.wal_compression,
immutable_db_options_.track_and_verify_wals);
io_s = (*new_log)->AddCompressionTypeRecord(write_options);
if (io_s.ok()) {
io_s = (*new_log)->MaybeAddPredecessorWALInfo(write_options,
predecessor_wal_info);
}
new_wal->log_number = log_file_num;
new_wal->writer = std::make_unique<log::Writer>(
std::move(file_writer), log_file_num,
immutable_db_options_.recycle_log_file_num > 0,
immutable_db_options_.manual_wal_flush,
immutable_db_options_.wal_compression,
immutable_db_options_.track_and_verify_wals);
}
return io_s;
}
IOStatus DBImpl::StartWALFile(const WriteOptions& write_options,
const PredecessorWALInfo& predecessor_wal_info,
log::Writer* new_log) {
assert(new_log);
IOStatus io_s = new_log->AddCompressionTypeRecord(write_options);
TEST_SYNC_POINT_CALLBACK("DBImpl::StartWALFile:AfterCompressionTypeRecord",
&io_s);
if (io_s.ok()) {
io_s = new_log->MaybeAddPredecessorWALInfo(write_options,
predecessor_wal_info);
}
return io_s;
}
IOStatus DBImpl::CreateWAL(const DBOptions& db_options,
const WriteOptions& write_options,
uint64_t log_file_num, uint64_t recycle_log_number,
size_t preallocate_block_size,
const PredecessorWALInfo& predecessor_wal_info,
log::Writer** new_log) {
assert(new_log);
assert(*new_log == nullptr);
UnpublishedWAL wal;
IOStatus io_s = CreateWALWriter(db_options, log_file_num, recycle_log_number,
preallocate_block_size, &wal);
if (io_s.ok()) {
io_s = StartWALFile(write_options, predecessor_wal_info, wal.writer.get());
}
if (io_s.ok()) {
*new_log = wal.writer.release();
}
return io_s;
}
bool DBImpl::AsyncWALPrecreateEnabled() const {
return immutable_db_options_.async_wal_precreate &&
immutable_db_options_.recycle_log_file_num == 0;
}
struct AsyncWALPrecreateContext {
DBImpl* db = nullptr;
DBOptions db_options;
uint64_t log_number = 0;
size_t preallocate_block_size = 0;
};
void DBImpl::MaybeScheduleAsyncWALPrecreate(size_t preallocate_block_size) {
mutex_.AssertHeld();
if (!AsyncWALPrecreateEnabled() ||
shutting_down_.load(std::memory_order_acquire) ||
error_handler_.IsBGWorkStopped() ||
async_wal_precreate_state_ != AsyncWALPrecreateState::kNotScheduled) {
return;
}
async_wal_precreate_wal_.Reset();
async_wal_precreate_wal_.log_number = versions_->NewFileNumber();
async_wal_precreate_state_ = AsyncWALPrecreateState::kScheduled;
auto* ctx = new AsyncWALPrecreateContext();
ctx->db = this;
ctx->db_options = BuildDBOptions(immutable_db_options_, mutable_db_options_);
ctx->log_number = async_wal_precreate_wal_.log_number;
ctx->preallocate_block_size = preallocate_block_size;
env_->Schedule(&DBImpl::BGWorkAsyncWALPrecreate, ctx, Env::Priority::HIGH,
nullptr);
}
DBImpl::UnpublishedWAL DBImpl::WaitForAsyncWALPrecreate() {
mutex_.AssertHeld();
UnpublishedWAL result;
if (!AsyncWALPrecreateEnabled()) {
return result;
}
TEST_SYNC_POINT("DBImpl::WaitForAsyncWALPrecreate:Begin");
bool waited = false;
uint64_t wait_start_micros = 0;
while (async_wal_precreate_state_ == AsyncWALPrecreateState::kScheduled) {
TEST_SYNC_POINT("DBImpl::WaitForAsyncWALPrecreate:BeforeWait");
if (!waited) {
waited = true;
wait_start_micros = immutable_db_options_.clock->NowMicros();
RecordTick(stats_, WAL_PRECREATE_WAITED);
}
bg_cv_.Wait();
}
if (waited) {
RecordTick(stats_, WAL_PRECREATE_WAIT_MICROS,
immutable_db_options_.clock->NowMicros() - wait_start_micros);
}
if (async_wal_precreate_state_ == AsyncWALPrecreateState::kReady) {
result = std::move(async_wal_precreate_wal_);
async_wal_precreate_state_ = AsyncWALPrecreateState::kNotScheduled;
RecordTick(stats_, WAL_PRECREATE_HIT);
return result;
}
assert(async_wal_precreate_state_ == AsyncWALPrecreateState::kNotScheduled);
RecordTick(stats_, WAL_PRECREATE_MISS);
return result;
}
void DBImpl::BGWorkAsyncWALPrecreate(void* arg) {
TEST_SYNC_POINT("DBImpl::BGWorkAsyncWALPrecreate:Start");
std::unique_ptr<AsyncWALPrecreateContext> ctx(
static_cast<AsyncWALPrecreateContext*>(arg));
DBImpl* db = ctx->db;
UnpublishedWAL new_wal;
IOStatus io_s = db->CreateWALWriter(ctx->db_options, ctx->log_number,
0 /* recycle_log_number */,
ctx->preallocate_block_size, &new_wal);
if (!io_s.ok()) {
RecordTick(db->stats_, WAL_PRECREATE_FAILED);
ROCKS_LOG_WARN(db->immutable_db_options_.info_log,
"Async WAL precreate failed for #%" PRIu64 ": %s",
ctx->log_number, io_s.ToString().c_str());
}
TEST_SYNC_POINT("DBImpl::BGWorkAsyncWALPrecreate:BeforePublish");
TEST_SYNC_POINT("DBImpl::BGWorkAsyncWALPrecreate:Publish");
{
InstrumentedMutexLock l(&db->mutex_);
if (db->async_wal_precreate_state_ == AsyncWALPrecreateState::kScheduled &&
db->async_wal_precreate_wal_.log_number == ctx->log_number) {
if (io_s.ok()) {
db->async_wal_precreate_wal_ = std::move(new_wal);
db->async_wal_precreate_state_ = AsyncWALPrecreateState::kReady;
} else {
// Async WAL precreation is best-effort. Clear the slot so foreground
// rotation falls back to normal synchronous WAL creation.
db->async_wal_precreate_wal_.Reset();
db->async_wal_precreate_state_ = AsyncWALPrecreateState::kNotScheduled;
}
}
db->bg_cv_.SignalAll();
}
TEST_SYNC_POINT("DBImpl::BGWorkAsyncWALPrecreate:Done");
}
void DBImpl::TrackExistingDataFiles(
const std::vector<std::string>& existing_data_files) {
TrackOrUntrackFiles(existing_data_files, /*track=*/true);
@@ -2534,8 +2719,10 @@ Status DBImpl::Open(const DBOptions& db_options, const std::string& dbname,
// created during DB open with predecessor WALs from previous DB session due
// to `avoid_flush_during_recovery == true`. This can protect the last WAL
// recovered.
s = impl->CreateWAL(write_options, new_log_number, 0 /*recycle_log_number*/,
preallocate_block_size,
const DBOptions db_options_snapshot =
BuildDBOptions(impl->immutable_db_options_, impl->mutable_db_options_);
s = impl->CreateWAL(db_options_snapshot, write_options, new_log_number,
0 /*recycle_log_number*/, preallocate_block_size,
PredecessorWALInfo() /* predecessor_wal_info */,
&new_log);
if (s.ok()) {
@@ -2740,6 +2927,8 @@ Status DBImpl::Open(const DBOptions& db_options, const std::string& dbname,
if (impl->immutable_db_options_.open_files_async) {
impl->ScheduleAsyncFileOpening();
}
impl->MaybeScheduleAsyncWALPrecreate(
impl->GetWalPreallocateBlockSize(max_write_buffer_size));
impl->mutex_.Unlock();
}
+13
View File
@@ -134,6 +134,19 @@ class DBImplReadOnly : public DBImpl {
return Status::NotSupported("Not supported operation in read only mode.");
}
using DB::PrepareFileIngestion;
Status PrepareFileIngestion(
const std::vector<IngestExternalFileArg>& /*args*/,
std::unique_ptr<FileIngestionHandle>* /*handle*/) override {
return Status::NotSupported("Not supported operation in read only mode.");
}
using DB::CommitFileIngestionHandles;
Status CommitFileIngestionHandles(
std::vector<std::unique_ptr<FileIngestionHandle>> /*handles*/) override {
return Status::NotSupported("Not supported operation in read only mode.");
}
using DB::CreateColumnFamilyWithImport;
Status CreateColumnFamilyWithImport(
const ColumnFamilyOptions& /*options*/,
+16 -10
View File
@@ -105,10 +105,22 @@ Status DBImplSecondary::FindNewLogNumbers(std::vector<uint64_t>* logs) {
return s;
}
// if log_readers_ is non-empty, it means we have applied all logs with log
// numbers smaller than the smallest log in log_readers_, so there is no
// need to pass these logs to RecoverLogFiles
uint64_t log_number_min = 0;
// Drop readers only after MANIFEST replay says the WAL is obsolete. The
// primary can create a higher-number empty WAL before it stops appending to a
// lower-number current WAL, so seeing a higher-number WAL is not sufficient.
const uint64_t min_log_number_to_keep = versions_->min_log_number_to_keep();
for (auto iter = log_readers_.begin(); iter != log_readers_.end();) {
if (iter->first < min_log_number_to_keep) {
iter = log_readers_.erase(iter);
} else {
break;
}
}
// If log_readers_ is non-empty, it means we have applied all logs with log
// numbers smaller than the smallest retained log reader. Otherwise, manifest
// recovery tells us which older logs can be skipped.
uint64_t log_number_min = min_log_number_to_keep;
if (!log_readers_.empty()) {
log_number_min = log_readers_.begin()->first;
}
@@ -321,12 +333,6 @@ Status DBImplSecondary::RecoverLogFiles(
return status;
}
}
// remove logreaders from map after successfully recovering the WAL
if (log_readers_.size() > 1) {
auto erase_iter = log_readers_.begin();
std::advance(erase_iter, log_readers_.size() - 1);
log_readers_.erase(log_readers_.begin(), erase_iter);
}
return status;
}
+13
View File
@@ -239,6 +239,19 @@ class DBImplSecondary : public DBImpl {
return Status::NotSupported("Not supported operation in secondary mode.");
}
using DB::PrepareFileIngestion;
Status PrepareFileIngestion(
const std::vector<IngestExternalFileArg>& /*args*/,
std::unique_ptr<FileIngestionHandle>* /*handle*/) override {
return Status::NotSupported("Not supported operation in secondary mode.");
}
using DB::CommitFileIngestionHandles;
Status CommitFileIngestionHandles(
std::vector<std::unique_ptr<FileIngestionHandle>> /*handles*/) override {
return Status::NotSupported("Not supported operation in secondary mode.");
}
// Try to catch up with the primary by reading as much as possible from the
// log files until there is nothing more to read or encounters an error. If
// the amount of information in the log files to process is huge, this
+39 -4
View File
@@ -16,6 +16,7 @@
#include "db/error_handler.h"
#include "db/event_helpers.h"
#include "db/wide/wide_columns_helper.h"
#include "file/filename.h"
#include "logging/logging.h"
#include "memtable/wbwi_memtable.h"
#include "monitoring/perf_context_imp.h"
@@ -3107,6 +3108,7 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
const WriteOptions write_options;
log::Writer* new_log = nullptr;
UnpublishedWAL prepared_wal;
MemTable* new_mem = nullptr;
IOStatus io_s;
@@ -3134,8 +3136,16 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
!wal_recycle_files_.empty() && IsFileDeletionsEnabled()) {
recycle_log_number = wal_recycle_files_.front();
}
uint64_t new_log_number =
creating_new_log ? versions_->NewFileNumber() : cur_wal_number_;
uint64_t new_log_number = cur_wal_number_;
if (creating_new_log) {
prepared_wal = WaitForAsyncWALPrecreate();
if (prepared_wal.writer) {
new_log_number = prepared_wal.log_number;
} else {
assert(prepared_wal.log_number == 0);
new_log_number = versions_->NewFileNumber();
}
}
// For use outside of holding DB mutex
const MutableCFOptions mutable_cf_options_copy =
cfd->GetLatestMutableCFOptions();
@@ -3156,6 +3166,8 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
// Log this later after lock release. It may be outdated, e.g., if background
// flush happens before logging, but that should be ok.
int num_imm_unflushed = cfd->imm()->NumNotFlushed();
const DBOptions db_options_snapshot =
BuildDBOptions(immutable_db_options_, mutable_db_options_);
const auto preallocate_block_size =
GetWalPreallocateBlockSize(mutable_cf_options_copy.write_buffer_size);
mutex_.Unlock();
@@ -3171,13 +3183,35 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
wal_write_mutex_.Unlock();
// TODO: Write buffer size passed in should be max of all CF's instead
// of mutable_cf_options.write_buffer_size.
io_s = CreateWAL(write_options, new_log_number, recycle_log_number,
preallocate_block_size, info, &new_log);
if (prepared_wal.writer) {
io_s = StartWALFile(write_options, info, prepared_wal.writer.get());
if (io_s.ok()) {
new_log = prepared_wal.writer.release();
}
} else {
io_s =
CreateWAL(db_options_snapshot, write_options, new_log_number,
recycle_log_number, preallocate_block_size, info, &new_log);
}
TEST_SYNC_POINT_CALLBACK("DBImpl::SwitchMemtable:AfterCreateWAL", &io_s);
if (s.ok()) {
s = io_s;
}
}
if (!s.ok() && prepared_wal.writer) {
const uint64_t prepared_wal_number = prepared_wal.log_number;
const std::string prepared_wal_fname =
LogFileName(immutable_db_options_.GetWalDir(), prepared_wal_number);
prepared_wal.Reset();
Status delete_s = env_->DeleteFile(prepared_wal_fname);
if (!delete_s.ok() && !delete_s.IsNotFound()) {
ROCKS_LOG_WARN(immutable_db_options_.info_log,
"Failed to delete unstarted async precreated WAL #%" PRIu64
": %s",
prepared_wal_number, delete_s.ToString().c_str());
}
delete_s.PermitUncheckedError();
}
if (s.ok()) {
// FIXME: from the comment for GetEarliestSequenceNumber(), any key with
// seqno >= earliest_seqno should be in this or later memtable. This means
@@ -3359,6 +3393,7 @@ Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
new_mem->Ref();
cfd->SetMemtable(new_mem);
InstallSuperVersionAndScheduleWork(cfd, &context->superversion_context);
MaybeScheduleAsyncWALPrecreate(preallocate_block_size);
// Notify client that memtable is sealed, now that we have successfully
// installed a new memtable
+43 -28
View File
@@ -67,8 +67,9 @@ DBIter::DBIter(Env* _env, const ReadOptions& read_options,
const MutableCFOptions& mutable_cf_options,
const Comparator* cmp, InternalIterator* iter,
const Version* version, SequenceNumber s, bool arena_mode,
ReadCallback* read_callback, ColumnFamilyHandleImpl* cfh,
bool expose_blob_index, ReadOnlyMemTable* active_mem)
ReadCallback* read_callback, DBImpl* db_impl,
ColumnFamilyData* cfd, bool expose_blob_index,
ReadOnlyMemTable* active_mem)
: prefix_extractor_(mutable_cf_options.prefix_extractor.get()),
env_(_env),
clock_(ioptions.clock),
@@ -76,21 +77,26 @@ DBIter::DBIter(Env* _env, const ReadOptions& read_options,
user_comparator_(cmp),
merge_operator_(ioptions.merge_operator.get()),
iter_(iter),
blob_state_(
version, read_options.read_tier, read_options.verify_checksums,
read_options.fill_cache, read_options.io_activity,
cfh ? cfh->cfd()->blob_file_cache() : nullptr,
cfh != nullptr && cfh->cfd()->blob_partition_manager() != nullptr),
blob_state_(version, read_options.read_tier,
read_options.verify_checksums, read_options.fill_cache,
read_options.io_activity,
cfd ? cfd->blob_file_cache() : nullptr,
cfd != nullptr && cfd->blob_partition_manager() != nullptr),
read_callback_(read_callback),
sequence_(s),
value_columns_state_(version, read_options, cfh),
value_columns_state_(version, read_options, cfd),
statistics_(ioptions.stats),
max_skip_(mutable_cf_options.max_sequential_skip_in_iterations),
max_skippable_internal_keys_(read_options.max_skippable_internal_keys),
num_internal_keys_skipped_(0),
iterate_lower_bound_(read_options.iterate_lower_bound),
iterate_upper_bound_(read_options.iterate_upper_bound),
cfh_(cfh),
trace_db_(db_impl),
trace_cf_id_(cfd != nullptr ? cfd->GetID() : 0),
has_trace_state_(db_impl != nullptr && cfd != nullptr),
allow_blob_write_path_fallback_(cfd != nullptr &&
cfd->blob_partition_manager() != nullptr),
ingest_sst_lock_(cfd != nullptr ? &cfd->GetIngestSstLock() : nullptr),
timestamp_ub_(read_options.timestamp),
timestamp_lb_(read_options.iter_start_ts),
timestamp_size_(timestamp_ub_ ? timestamp_ub_->size() : 0),
@@ -305,10 +311,8 @@ bool DBIter::SetValueAndColumnsFromBlobImpl(const Slice& user_key,
// Keep the non-BDW iterator path on the pre-existing Version::GetBlob()
// fast path. Only enable the direct-write fallback when this CF actually
// has a write-path partition manager.
const bool allow_write_path_fallback =
cfh_ != nullptr && cfh_->cfd()->blob_partition_manager() != nullptr;
const Status s = blob_state_.mut()->reader.RetrieveAndSetBlobValue(
user_key, blob_index, allow_write_path_fallback);
user_key, blob_index, allow_blob_write_path_fallback_);
if (!s.ok()) {
status_ = s;
valid_ = false;
@@ -1480,7 +1484,9 @@ bool DBIter::FindValueForCurrentKeyUsingSeek() {
if (timestamp_lb_ != nullptr) {
saved_key_.SetInternalKey(ikey);
} else {
saved_key_.SetUserKey(ikey.user_key);
saved_key_.SetUserKey(
ikey.user_key,
!pin_thru_lifetime_ || !iter_.iter()->IsKeyPinned() /* copy */);
}
valid_ = true;
@@ -1615,10 +1621,8 @@ bool DBIter::MergeWithBlobBaseValue(const Slice& blob_index,
return false;
}
const bool allow_write_path_fallback =
cfh_ != nullptr && cfh_->cfd()->blob_partition_manager() != nullptr;
const Status s = blob_state_.mut()->reader.RetrieveAndSetBlobValue(
user_key, blob_index, allow_write_path_fallback);
user_key, blob_index, allow_blob_write_path_fallback_);
if (!s.ok()) {
status_ = s;
valid_ = false;
@@ -1814,10 +1818,10 @@ void DBIter::MaybeInsertRangeTombstone(const Slice& end_key) {
}
}
assert(cfh_ != nullptr);
assert(ingest_sst_lock_ != nullptr);
if (active_mem_->AddLogicallyRedundantRangeTombstone(
insert_seq, range_tomb_first_key_.GetUserKey(), end_key,
cfh_->cfd()->GetIngestSstLock())) {
*ingest_sst_lock_)) {
RecordTick(statistics_, READ_PATH_RANGE_TOMBSTONES_INSERTED);
ROCKS_LOG_DEBUG(logger_,
"Inserted range tombstone [%s, %s) @ seq %" PRIu64
@@ -1957,10 +1961,10 @@ Status DBIter::ValidateScanOptions(const MultiScanArgs& multiscan_opts) const {
return Status::OK();
}
void DBIter::Prepare(const MultiScanArgs& scan_opts) {
Status DBIter::SetScanOptionsForPrepare(const MultiScanArgs& scan_opts) {
status_ = ValidateScanOptions(scan_opts);
if (!status_.ok()) {
return;
return status_;
}
std::optional<MultiScanArgs> new_scan_opts;
new_scan_opts.emplace(scan_opts);
@@ -1973,14 +1977,27 @@ void DBIter::Prepare(const MultiScanArgs& scan_opts) {
if (!scan_opts_.value().io_dispatcher) {
scan_opts_->io_dispatcher.reset(NewIODispatcher());
}
return Status::OK();
}
if (!scan_opts.empty()) {
void DBIter::PrepareInternalChildren() {
if (!scan_opts_.has_value() || !status_.ok()) {
return;
}
if (scan_opts_.value().HasBoundedScanRanges()) {
iter_.Prepare(&scan_opts_.value());
} else {
iter_.Prepare(nullptr);
}
}
void DBIter::Prepare(const MultiScanArgs& scan_opts) {
if (SetScanOptionsForPrepare(scan_opts).ok()) {
PrepareInternalChildren();
}
}
void DBIter::Seek(const Slice& target) {
PERF_COUNTER_ADD(iter_seek_count, 1);
PERF_CPU_TIMER_GUARD(iter_seek_cpu_nanos, clock_);
@@ -2027,7 +2044,7 @@ void DBIter::Seek(const Slice& target) {
scan_index_++;
}
if (cfh_ != nullptr) {
if (has_trace_state_) {
// TODO: What do we do if this returns an error?
Slice lower_bound, upper_bound;
if (iterate_lower_bound_ != nullptr) {
@@ -2040,9 +2057,7 @@ void DBIter::Seek(const Slice& target) {
} else {
upper_bound = Slice("");
}
cfh_->db()
->TraceIteratorSeek(cfh_->cfd()->GetID(), target, lower_bound,
upper_bound)
trace_db_->TraceIteratorSeek(trace_cf_id_, target, lower_bound, upper_bound)
.PermitUncheckedError();
}
@@ -2095,7 +2110,7 @@ void DBIter::SeekForPrev(const Slice& target) {
PERF_CPU_TIMER_GUARD(iter_seek_cpu_nanos, clock_);
StopWatch sw(clock_, statistics_, DB_SEEK);
if (cfh_ != nullptr) {
if (has_trace_state_) {
// TODO: What do we do if this returns an error?
Slice lower_bound, upper_bound;
if (iterate_lower_bound_ != nullptr) {
@@ -2108,8 +2123,8 @@ void DBIter::SeekForPrev(const Slice& target) {
} else {
upper_bound = Slice("");
}
cfh_->db()
->TraceIteratorSeekForPrev(cfh_->cfd()->GetID(), target, lower_bound,
trace_db_
->TraceIteratorSeekForPrev(trace_cf_id_, target, lower_bound,
upper_bound)
.PermitUncheckedError();
}
+25 -11
View File
@@ -32,6 +32,9 @@
namespace ROCKSDB_NAMESPACE {
class BlobFileCache;
class Version;
namespace port {
class RWMutex;
}
// This file declares the factory functions of DBIter, in its original form
// or a wrapped form with class ArenaWrappedDBIter, which is defined here.
@@ -83,14 +86,19 @@ class DBIter final : public Iterator {
ReadCallback* read_callback,
ReadOnlyMemTable* active_mem,
ColumnFamilyHandleImpl* cfh = nullptr,
bool expose_blob_index = false,
Arena* arena = nullptr) {
bool expose_blob_index = false, Arena* arena = nullptr,
DBImpl* db_impl = nullptr,
ColumnFamilyData* cfd = nullptr) {
if (cfh != nullptr) {
db_impl = cfh->db();
cfd = cfh->cfd();
}
void* mem = arena ? arena->AllocateAligned(sizeof(DBIter))
: operator new(sizeof(DBIter));
DBIter* db_iter = new (mem)
DBIter(env, read_options, ioptions, mutable_cf_options,
user_key_comparator, internal_iter, version, sequence, arena,
read_callback, cfh, expose_blob_index, active_mem);
read_callback, db_impl, cfd, expose_blob_index, active_mem);
return db_iter;
}
@@ -207,7 +215,7 @@ class DBIter final : public Iterator {
}
Status status() const override {
if (status_.ok()) {
if (status_.ok() && iter_.iter() != nullptr) {
return iter_.status();
} else {
assert(!valid_);
@@ -248,19 +256,22 @@ class DBIter final : public Iterator {
iter_.SetRangeDelReadSeqno(s);
}
void set_valid(bool v) { valid_ = v; }
void set_status(Status s) { status_ = std::move(s); }
bool PrepareValue() override;
void Prepare(const MultiScanArgs& scan_opts) override;
Status ValidateScanOptions(const MultiScanArgs& multiscan_opts) const;
Status SetScanOptionsForPrepare(const MultiScanArgs& scan_opts);
void PrepareInternalChildren();
private:
DBIter(Env* _env, const ReadOptions& read_options,
const ImmutableOptions& ioptions,
const MutableCFOptions& mutable_cf_options, const Comparator* cmp,
InternalIterator* iter, const Version* version, SequenceNumber s,
bool arena_mode, ReadCallback* read_callback,
ColumnFamilyHandleImpl* cfh, bool expose_blob_index,
bool arena_mode, ReadCallback* read_callback, DBImpl* db_impl,
ColumnFamilyData* cfd, bool expose_blob_index,
ReadOnlyMemTable* active_mem);
class BlobReader {
@@ -318,13 +329,12 @@ class DBIter final : public Iterator {
class ValueColumnsState {
public:
ValueColumnsState(const Version* version, const ReadOptions& read_options,
ColumnFamilyHandleImpl* cfh)
ColumnFamilyData* cfd)
: entity_blob_resolver_(
version, read_options.read_tier, read_options.verify_checksums,
read_options.fill_cache, read_options.io_activity,
cfh ? cfh->cfd()->blob_file_cache() : nullptr,
cfh != nullptr &&
cfh->cfd()->blob_partition_manager() != nullptr) {}
cfd ? cfd->blob_file_cache() : nullptr,
cfd != nullptr && cfd->blob_partition_manager() != nullptr) {}
Slice& value() { return value_; }
const Slice& value() const { return value_; }
@@ -706,7 +716,11 @@ class DBIter final : public Iterator {
MergeContext merge_context_;
LocalStatistics local_stats_;
PinnedIteratorsManager pinned_iters_mgr_;
ColumnFamilyHandleImpl* cfh_;
DBImpl* trace_db_;
uint32_t trace_cf_id_;
bool has_trace_state_;
bool allow_blob_write_path_fallback_;
port::RWMutex* ingest_sst_lock_;
const Slice* const timestamp_ub_;
const Slice* const timestamp_lb_;
const size_t timestamp_size_;
+54
View File
@@ -216,12 +216,19 @@ class TestIterator : public InternalIterator {
bool IsKeyPinned() const override { return true; }
bool IsValuePinned() const override { return true; }
void Prepare(const MultiScanArgs* /*scan_opts*/) override {
++prepare_call_count_;
}
size_t prepare_call_count() const { return prepare_call_count_; }
private:
bool initialized_;
bool valid_;
size_t sequence_number_;
size_t iter_;
size_t steps_ = 0;
size_t prepare_call_count_ = 0;
InternalKeyComparator cmp;
std::vector<std::pair<std::string, std::string>> data_;
@@ -243,6 +250,53 @@ class DBIteratorTest : public testing::Test {
DBIteratorTest() : env_(Env::Default()) {}
};
TEST_F(DBIteratorTest, PrepareForwardsValidatedScanRanges) {
Options options;
ImmutableOptions ioptions = ImmutableOptions(options);
MutableCFOptions mutable_cf_options = MutableCFOptions(options);
TestIterator* internal_iter = new TestIterator(BytewiseComparator());
internal_iter->AddPut("a", "val_a");
internal_iter->AddPut("b", "val_b");
internal_iter->Finish();
ReadOptions ro;
std::unique_ptr<Iterator> db_iter(DBIter::NewIter(
env_, ro, ioptions, mutable_cf_options, BytewiseComparator(),
internal_iter, nullptr /* version */, 10 /* sequence */,
nullptr /* read_callback */, /*active_mem=*/nullptr));
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert("a", "c");
db_iter->Prepare(scan_opts);
ASSERT_OK(db_iter->status());
EXPECT_EQ(internal_iter->prepare_call_count(), 1);
}
TEST_F(DBIteratorTest, PrepareDoesNotForwardInvalidScanRanges) {
Options options;
ImmutableOptions ioptions = ImmutableOptions(options);
MutableCFOptions mutable_cf_options = MutableCFOptions(options);
TestIterator* internal_iter = new TestIterator(BytewiseComparator());
internal_iter->AddPut("a", "val_a");
internal_iter->AddPut("b", "val_b");
internal_iter->Finish();
ReadOptions ro;
std::unique_ptr<Iterator> db_iter(DBIter::NewIter(
env_, ro, ioptions, mutable_cf_options, BytewiseComparator(),
internal_iter, nullptr /* version */, 10 /* sequence */,
nullptr /* read_callback */, /*active_mem=*/nullptr));
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert("b", "d");
scan_opts.insert("a", "c");
db_iter->Prepare(scan_opts);
EXPECT_TRUE(db_iter->status().IsInvalidArgument());
EXPECT_EQ(internal_iter->prepare_call_count(), 0);
}
TEST_F(DBIteratorTest, DBIteratorPrevNext) {
Options options;
ImmutableOptions ioptions = ImmutableOptions(options);
+592
View File
@@ -10,6 +10,9 @@
#include <functional>
#include <iomanip>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include "db/arena_wrapped_db_iter.h"
#include "db/db_iter.h"
@@ -87,6 +90,392 @@ TEST_F(DBIteratorBaseTest, APICallsWithPerfContext) {
delete iter;
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanPrunesNonIntersectingFiles) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Flush());
ASSERT_OK(Put("z", "vz"));
ASSERT_OK(Flush());
ASSERT_EQ(2, NumTableFilesAtLevel(0));
int table_iterators_created = 0;
int files_added = 0;
int block_based_iterators = 0;
int level_iterators = 0;
SyncPoint::GetInstance()->SetCallBack(
"TableCache::NewIterator::BeforeFindTable",
[&](void* /*arg*/) { ++table_iterators_created; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:AddedFile",
[&](void* /*arg*/) { ++files_added; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:IteratorType", [&](void* arg) {
auto* iterator_type = static_cast<std::pair<bool, bool>*>(arg);
if (iterator_type->first) {
++block_based_iterators;
}
if (iterator_type->second) {
++level_iterators;
}
});
SyncPoint::GetInstance()->EnableProcessing();
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("a"), Slice("b"));
Slice upper_bound("b");
ReadOptions read_options;
read_options.iterate_upper_bound = &upper_bound;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
ASSERT_EQ(0, table_iterators_created);
ASSERT_EQ(0, files_added);
ASSERT_EQ(0, block_based_iterators);
ASSERT_EQ(0, level_iterators);
iter->Prepare(scan_opts);
ASSERT_EQ(1, table_iterators_created);
ASSERT_EQ(1, files_added);
ASSERT_EQ(1, block_based_iterators);
ASSERT_EQ(0, level_iterators);
std::vector<std::string> keys;
for (iter->Seek("a"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_EQ(keys, std::vector<std::string>({"a"}));
ASSERT_FALSE(iter->Valid());
ASSERT_OK(iter->status());
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanPrunesNonIntersectingLevels) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("z", "vz"));
ASSERT_OK(Flush());
MoveFilesToLevel(2);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Flush());
MoveFilesToLevel(1);
ASSERT_OK(Put("m", "vm"));
ASSERT_OK(Flush());
MoveFilesToLevel(1);
int table_iterators_created = 0;
int files_added = 0;
int block_based_iterators = 0;
int level_iterators = 0;
SyncPoint::GetInstance()->SetCallBack(
"TableCache::NewIterator::BeforeFindTable",
[&](void* /*arg*/) { ++table_iterators_created; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:AddedFile",
[&](void* /*arg*/) { ++files_added; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:IteratorType", [&](void* arg) {
auto* iterator_type = static_cast<std::pair<bool, bool>*>(arg);
if (iterator_type->first) {
++block_based_iterators;
}
if (iterator_type->second) {
++level_iterators;
}
});
SyncPoint::GetInstance()->EnableProcessing();
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("a"), Slice("n"));
Slice upper_bound("n");
ReadOptions read_options;
read_options.iterate_upper_bound = &upper_bound;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
ASSERT_EQ(0, table_iterators_created);
ASSERT_EQ(0, files_added);
ASSERT_EQ(0, block_based_iterators);
ASSERT_EQ(0, level_iterators);
iter->Prepare(scan_opts);
ASSERT_EQ(0, table_iterators_created);
ASSERT_EQ(2, files_added);
ASSERT_EQ(0, block_based_iterators);
ASSERT_EQ(1, level_iterators);
std::vector<std::string> keys;
for (iter->Seek("a"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_EQ(keys, std::vector<std::string>({"a", "m"}));
ASSERT_FALSE(iter->Valid());
ASSERT_OK(iter->status());
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanAllowsSingleUnboundedRange) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Put("b", "vb"));
ASSERT_OK(Put("c", "vc"));
ASSERT_OK(Flush());
MoveFilesToLevel(1);
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("b"));
ReadOptions read_options;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
iter->Prepare(scan_opts);
std::vector<std::string> keys;
for (iter->Seek("b"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_EQ(keys, std::vector<std::string>({"b", "c"}));
ASSERT_FALSE(iter->Valid());
ASSERT_OK(iter->status());
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanRejectsRepeatedPrepare) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Flush());
MoveFilesToLevel(1);
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("a"), Slice("b"));
Slice upper_bound("b");
ReadOptions read_options;
read_options.iterate_upper_bound = &upper_bound;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
iter->Prepare(scan_opts);
ASSERT_OK(iter->status());
iter->Prepare(scan_opts);
ASSERT_NOK(iter->status());
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanDedupsMultipleRangesInSameFile) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Put("m", "vm"));
ASSERT_OK(Put("z", "vz"));
ASSERT_OK(Flush());
MoveFilesToLevel(1);
int table_iterators_created = 0;
int files_added = 0;
int block_based_iterators = 0;
int level_iterators = 0;
SyncPoint::GetInstance()->SetCallBack(
"TableCache::NewIterator::BeforeFindTable",
[&](void* /*arg*/) { ++table_iterators_created; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:AddedFile",
[&](void* /*arg*/) { ++files_added; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:IteratorType", [&](void* arg) {
auto* iterator_type = static_cast<std::pair<bool, bool>*>(arg);
if (iterator_type->first) {
++block_based_iterators;
}
if (iterator_type->second) {
++level_iterators;
}
});
SyncPoint::GetInstance()->EnableProcessing();
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("a"), Slice("b"));
scan_opts.insert(Slice("m"), Slice("n"));
Slice upper_bound("b");
ReadOptions read_options;
read_options.iterate_upper_bound = &upper_bound;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
ASSERT_EQ(0, table_iterators_created);
ASSERT_EQ(0, files_added);
ASSERT_EQ(0, block_based_iterators);
ASSERT_EQ(0, level_iterators);
iter->Prepare(scan_opts);
ASSERT_EQ(1, table_iterators_created);
ASSERT_EQ(1, files_added);
ASSERT_EQ(1, block_based_iterators);
ASSERT_EQ(0, level_iterators);
std::vector<std::string> keys;
for (iter->Seek("a"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_OK(iter->status());
ASSERT_EQ(keys, std::vector<std::string>({"a"}));
upper_bound = "n";
for (iter->Seek("m"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_EQ(keys, std::vector<std::string>({"a", "m"}));
ASSERT_FALSE(iter->Valid());
ASSERT_OK(iter->status());
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanPrunesOverlappingL0Files) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Flush());
ASSERT_OK(Put("m", "vm"));
ASSERT_OK(Flush());
ASSERT_EQ(2, NumTableFilesAtLevel(0));
int table_iterators_created = 0;
int files_added = 0;
int block_based_iterators = 0;
int level_iterators = 0;
SyncPoint::GetInstance()->SetCallBack(
"TableCache::NewIterator::BeforeFindTable",
[&](void* /*arg*/) { ++table_iterators_created; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:AddedFile",
[&](void* /*arg*/) { ++files_added; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:IteratorType", [&](void* arg) {
auto* iterator_type = static_cast<std::pair<bool, bool>*>(arg);
if (iterator_type->first) {
++block_based_iterators;
}
if (iterator_type->second) {
++level_iterators;
}
});
SyncPoint::GetInstance()->EnableProcessing();
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("a"), Slice("n"));
Slice upper_bound("n");
ReadOptions read_options;
read_options.iterate_upper_bound = &upper_bound;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
ASSERT_EQ(0, table_iterators_created);
ASSERT_EQ(0, files_added);
ASSERT_EQ(0, block_based_iterators);
ASSERT_EQ(0, level_iterators);
iter->Prepare(scan_opts);
ASSERT_EQ(2, table_iterators_created);
ASSERT_EQ(2, files_added);
ASSERT_EQ(2, block_based_iterators);
ASSERT_EQ(0, level_iterators);
std::vector<std::string> keys;
for (iter->Seek("a"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_EQ(keys, std::vector<std::string>({"a", "m"}));
ASSERT_FALSE(iter->Valid());
ASSERT_OK(iter->status());
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(DBIteratorBaseTest, PrepareWithMultiScanPrunesNonIntersectingMemTables) {
Options options = CurrentOptions();
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("a", "va"));
ASSERT_OK(Flush());
MoveFilesToLevel(1);
ASSERT_OK(db_->PauseBackgroundWork());
ASSERT_OK(Put("z_imm", "vz"));
ASSERT_OK(dbfull()->TEST_SwitchMemtable());
ASSERT_OK(Put("z_mem", "vz"));
int table_iterators_created = 0;
int files_added = 0;
int block_based_iterators = 0;
int level_iterators = 0;
int merging_iterators = 0;
SyncPoint::GetInstance()->SetCallBack(
"TableCache::NewIterator::BeforeFindTable",
[&](void* /*arg*/) { ++table_iterators_created; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:AddedFile",
[&](void* /*arg*/) { ++files_added; });
SyncPoint::GetInstance()->SetCallBack(
"Version::AddIteratorsForLevel:IteratorType", [&](void* arg) {
auto* iterator_type = static_cast<std::pair<bool, bool>*>(arg);
if (iterator_type->first) {
++block_based_iterators;
}
if (iterator_type->second) {
++level_iterators;
}
});
SyncPoint::GetInstance()->SetCallBack(
"MergeIteratorBuilder::Finish:UseMergingIterator", [&](void* arg) {
if (*static_cast<bool*>(arg)) {
++merging_iterators;
}
});
SyncPoint::GetInstance()->EnableProcessing();
MultiScanArgs scan_opts(BytewiseComparator());
scan_opts.insert(Slice("a"), Slice("b"));
Slice upper_bound("b");
ReadOptions read_options;
read_options.iterate_upper_bound = &upper_bound;
std::unique_ptr<Iterator> iter(
db_->NewIterator(read_options, db_->DefaultColumnFamily()));
ASSERT_EQ(0, table_iterators_created);
ASSERT_EQ(0, files_added);
ASSERT_EQ(0, block_based_iterators);
ASSERT_EQ(0, level_iterators);
ASSERT_EQ(0, merging_iterators);
iter->Prepare(scan_opts);
ASSERT_EQ(1, table_iterators_created);
ASSERT_EQ(1, files_added);
ASSERT_EQ(1, block_based_iterators);
ASSERT_EQ(0, level_iterators);
ASSERT_EQ(0, merging_iterators);
std::vector<std::string> keys;
for (iter->Seek("a"); iter->Valid(); iter->Next()) {
keys.push_back(iter->key().ToString());
}
ASSERT_EQ(keys, std::vector<std::string>({"a"}));
ASSERT_FALSE(iter->Valid());
ASSERT_OK(iter->status());
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
ASSERT_OK(db_->ContinueBackgroundWork());
}
// Test param:
// bool: whether to pass read_callback to NewIterator().
class DBIteratorTest : public DBIteratorBaseTest,
@@ -945,6 +1334,68 @@ TEST_P(DBIteratorTest, IteratorDeleteAfterCfDelete) {
delete iter;
}
TEST_P(DBIteratorTest, IteratorSeekAfterCfDelete) {
CreateAndReopenWithCF({"pikachu"}, CurrentOptions());
ASSERT_OK(Put(1, "foo", "delete-cf-then-seek-iter"));
ASSERT_OK(Put(1, "hello", "value2"));
ColumnFamilyHandle* cf = handles_[1];
ReadOptions ro;
auto* iter = db_->NewIterator(ro, cf);
// Delete the CF handle before the lazy iterator tree is materialized.
EXPECT_OK(db_->DestroyColumnFamilyHandle(cf));
handles_.erase(std::begin(handles_) + 1);
iter->Seek("foo");
ASSERT_EQ(IterStatus(iter), "foo->delete-cf-then-seek-iter");
iter->SeekForPrev("hello");
ASSERT_EQ(IterStatus(iter), "hello->value2");
iter->SeekToFirst();
ASSERT_EQ(IterStatus(iter), "foo->delete-cf-then-seek-iter");
iter->Next();
ASSERT_EQ(IterStatus(iter), "hello->value2");
delete iter;
}
TEST_P(DBIteratorTest, IteratorAutoRefreshAfterCfDeleteBeforeLazyInit) {
CreateAndReopenWithCF({"pikachu"}, CurrentOptions());
ASSERT_OK(Put(1, "foo", "delete-cf-then-auto-refresh"));
ASSERT_OK(Flush(1));
ColumnFamilyHandle* cf = handles_[1];
const Snapshot* snapshot = db_->GetSnapshot();
ReadOptions ro;
ro.snapshot = snapshot;
ro.auto_refresh_iterator_with_snapshot = true;
auto* iter = db_->NewIterator(ro, cf);
ASSERT_OK(Put(1, "zzz", "after-snapshot"));
ASSERT_OK(Flush(1));
// Delete the CF handle before the lazy iterator tree is materialized. The
// following operations force auto-refresh to acquire a newer SuperVersion.
EXPECT_OK(db_->DestroyColumnFamilyHandle(cf));
handles_.erase(std::begin(handles_) + 1);
iter->Seek("foo");
ASSERT_EQ(IterStatus(iter), "foo->delete-cf-then-auto-refresh");
ASSERT_OK(iter->status());
iter->SeekForPrev("foo");
ASSERT_EQ(IterStatus(iter), "foo->delete-cf-then-auto-refresh");
ASSERT_OK(iter->status());
iter->Next();
ASSERT_OK(iter->status());
delete iter;
db_->ReleaseSnapshot(snapshot);
}
TEST_P(DBIteratorTest, IteratorDeleteAfterCfDrop) {
CreateAndReopenWithCF({"pikachu"}, CurrentOptions());
@@ -966,6 +1417,30 @@ TEST_P(DBIteratorTest, IteratorDeleteAfterCfDrop) {
delete iter;
}
TEST_P(DBIteratorTest, IteratorSeekAfterCfDrop) {
CreateAndReopenWithCF({"pikachu"}, CurrentOptions());
ASSERT_OK(Put(1, "foo", "drop-cf-then-seek-iter"));
ReadOptions ro;
ColumnFamilyHandle* cf = handles_[1];
auto* iter = db_->NewIterator(ro, cf);
// Drop and delete the CF before the lazy iterator tree is materialized.
EXPECT_OK(db_->DropColumnFamily(cf));
EXPECT_OK(db_->DestroyColumnFamilyHandle(cf));
handles_.erase(std::begin(handles_) + 1);
iter->Seek("foo");
ASSERT_EQ(IterStatus(iter), "foo->drop-cf-then-seek-iter");
iter->SeekForPrev("foo");
ASSERT_EQ(IterStatus(iter), "foo->drop-cf-then-seek-iter");
iter->SeekToFirst();
ASSERT_EQ(IterStatus(iter), "foo->drop-cf-then-seek-iter");
delete iter;
}
// SetOptions not defined in ROCKSDB LITE
TEST_P(DBIteratorTest, DBIteratorBoundTest) {
Options options = CurrentOptions();
@@ -2277,6 +2752,10 @@ TEST_P(DBIteratorTest, ReadAhead) {
options.env = env_;
options.disable_auto_compactions = true;
options.write_buffer_size = 4 << 20;
// Pin compression so the readahead byte thresholds below don't depend on the
// default compression type. kNoCompression keeps the SST files large enough
// to exercise readahead and is always available.
options.compression = kNoCompression;
options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
BlockBasedTableOptions table_options;
table_options.block_size = 1024;
@@ -2693,6 +3172,8 @@ TEST_P(DBIteratorTest, CreationFailure) {
Iterator* iter = NewIterator(ReadOptions());
ASSERT_FALSE(iter->Valid());
iter->SeekToFirst();
ASSERT_FALSE(iter->Valid());
ASSERT_TRUE(iter->status().IsCorruption());
delete iter;
}
@@ -4395,6 +4876,71 @@ TEST_P(DBMultiScanIteratorTest, RangeAcrossFiles) {
iter.reset();
}
TEST_P(DBMultiScanIteratorTest, SortedRangesSkipIODispatcherSort) {
auto options = CurrentOptions();
options.target_file_size_base = 100 << 10;
options.compaction_style = kCompactionStyleUniversal;
options.num_levels = 50;
options.compression = kNoCompression;
DestroyAndReopen(options);
auto key = [](int i) {
std::stringstream ss;
ss << "k" << std::setw(5) << std::setfill('0') << i;
return ss.str();
};
auto rnd = Random::GetTLSInstance();
for (int i = 0; i < 100; ++i) {
ASSERT_OK(Put(key(i), rnd->RandomString(2 << 10)));
}
ASSERT_OK(Flush());
ASSERT_OK(db_->CompactRange({}, nullptr, nullptr));
ASSERT_EQ(2, NumTableFilesAtLevel(49));
int sort_count = 0;
SyncPoint::GetInstance()->SetCallBack(
"IODispatcherImpl::SubmitJob:SortBlockHandles",
[&](void* /*arg*/) { ++sort_count; });
SyncPoint::GetInstance()->EnableProcessing();
auto tracking_dispatcher = std::make_shared<TrackingIODispatcher>();
std::vector<std::string> key_ranges({key(10), key(90)});
MultiScanArgs scan_options(BytewiseComparator());
scan_options.io_dispatcher = tracking_dispatcher;
scan_options.use_async_io = false;
scan_options.insert(key_ranges[0], key_ranges[1]);
ReadOptions ro;
ro.fill_cache = GetParam();
ColumnFamilyHandle* cfh = dbfull()->DefaultColumnFamily();
std::unique_ptr<MultiScan> iter =
dbfull()->NewMultiScan(ro, cfh, scan_options);
try {
int i = 10;
for (auto range : *iter) {
for (auto it : range) {
ASSERT_EQ(it.first.ToString(), key(i));
++i;
}
}
ASSERT_EQ(i, 90);
} catch (MultiScanException& ex) {
ASSERT_NOK(ex.status());
std::cerr << "Iterator returned status " << ex.what();
abort();
} catch (std::logic_error& ex) {
std::cerr << "Iterator returned logic error " << ex.what();
abort();
}
ASSERT_GT(tracking_dispatcher->GetReadSets().size(), 0);
EXPECT_EQ(sort_count, 0);
iter.reset();
}
TEST_P(DBMultiScanIteratorTest, FailureTest) {
auto options = CurrentOptions();
options.compression = kNoCompression;
@@ -7181,6 +7727,52 @@ TEST_P(ReadPathRangeTombstoneTest,
db_->ReleaseSnapshot(snap);
}
// SeekForPrev uses the reverse path: PrevInternal -> FindValueForCurrentKey.
// When a key has more versions than max_sequential_skip_in_iterations,
// FindValueForCurrentKey delegates to FindValueForCurrentKeyUsingSeek which
// re-seeks forward to the newest visible version. That function must preserve
// key pinning by passing the correct `copy` parameter to
// saved_key_.SetUserKey().
TEST_P(DBIteratorTest, SeekForPrevKeyPinnedWithManyVersions) {
Options options = CurrentOptions();
options.max_sequential_skip_in_iterations = 8;
options.statistics = CreateDBStatistics();
DestroyAndReopen(options);
// 20 versions > max_sequential_skip (8), forcing the reseek path.
// No flush: keeps all versions alive in the memtable.
for (int i = 0; i < 20; i++) {
ASSERT_OK(Put("key1", "value" + std::to_string(i)));
}
ReadOptions ro;
ro.pin_data = true;
std::unique_ptr<Iterator> iter(NewIterator(ro));
std::string prop_value;
// Seek (forward) correctly reports key as pinned.
iter->Seek("key1");
ASSERT_TRUE(iter->Valid());
ASSERT_OK(iter->GetProperty("rocksdb.iterator.is-key-pinned", &prop_value));
ASSERT_EQ("1", prop_value);
uint64_t reseeks_before =
options.statistics->getTickerCount(NUMBER_OF_RESEEKS_IN_ITERATION);
iter->SeekForPrev("key1");
ASSERT_TRUE(iter->Valid());
ASSERT_EQ("key1", iter->key().ToString());
// Confirm FindValueForCurrentKeyUsingSeek was actually taken.
uint64_t reseeks_after =
options.statistics->getTickerCount(NUMBER_OF_RESEEKS_IN_ITERATION);
ASSERT_GT(reseeks_after, reseeks_before);
ASSERT_OK(iter->GetProperty("rocksdb.iterator.is-key-pinned", &prop_value));
ASSERT_EQ("1", prop_value);
}
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {
+3 -3
View File
@@ -359,7 +359,7 @@ class DBMemTableTestForSeek : public DBMemTableTest,
TEST_P(DBMemTableTestForSeek, IntegrityChecks) {
// Validate key corruption could be detected during seek.
// We insert many keys into skiplist. Then we corrupt the each key one at a
// time. With memtable_veirfy_per_key_checksum_on_seek enabled, when the
// time. With memtable_verify_per_key_checksum_on_seek enabled, when the
// corrupted key is searched, the checksum of every key visited during the
// seek is validated. It will report data corruption. Otherwise seek returns
// not found.
@@ -367,7 +367,7 @@ TEST_P(DBMemTableTestForSeek, IntegrityChecks) {
Options options = CurrentOptions();
options.allow_data_in_errors = allow_data_in_error;
options.paranoid_memory_checks = std::get<1>(GetParam());
options.memtable_veirfy_per_key_checksum_on_seek = std::get<2>(GetParam());
options.memtable_verify_per_key_checksum_on_seek = std::get<2>(GetParam());
options.memtable_protection_bytes_per_key = 8;
DestroyAndReopen(options);
@@ -410,7 +410,7 @@ TEST_P(DBMemTableTestForSeek, IntegrityChecks) {
ASSERT_EQ(raw_data_pointer.size(), key_count);
bool enable_key_validation_on_seek =
options.memtable_veirfy_per_key_checksum_on_seek;
options.memtable_verify_per_key_checksum_on_seek;
// For each key, corrupt it, validate corruption is detected correctly, then
// revert it.
+2 -2
View File
@@ -151,8 +151,8 @@ TEST_F(DBOpenWithConfigTest, OpenWithComprehensiveConfig) {
// memtable_op_scan_flush_trigger=0
options.memtable_op_scan_flush_trigger = 0;
// memtable_veirfy_per_key_checksum_on_seek=false
options.memtable_veirfy_per_key_checksum_on_seek = false;
// memtable_verify_per_key_checksum_on_seek=false
options.memtable_verify_per_key_checksum_on_seek = false;
// Level and compaction settings
// num_levels=50
+200
View File
@@ -13,6 +13,7 @@
#include "db/column_family.h"
#include "db/db_impl/db_impl.h"
#include "db/db_test_util.h"
#include "env/mock_env.h"
#include "options/options_helper.h"
#include "port/stack_trace.h"
#include "rocksdb/cache.h"
@@ -102,6 +103,40 @@ TEST_F(DBOptionsTest, ImmutableTrackAndVerifyWalsInManifest) {
ASSERT_FALSE(s.ok());
}
TEST_F(DBOptionsTest, ImmutableAsyncWalPrecreate) {
Options options;
options.env = env_;
options.async_wal_precreate = true;
ImmutableDBOptions db_options(options);
ASSERT_TRUE(db_options.async_wal_precreate);
Reopen(options);
ASSERT_TRUE(dbfull()->GetDBOptions().async_wal_precreate);
Status s = dbfull()->SetDBOptions({{"async_wal_precreate", "false"}});
ASSERT_FALSE(s.ok());
}
TEST_F(DBOptionsTest, SanitizeAsyncWalPrecreateWithWalRecycle) {
// This checks the option-level contract for the incompatible WAL paths:
// SanitizeOptions() preserves WAL recycling and disables only the
// opportunistic async precreation path, so the immutable options reflect
// effective behavior.
Options options;
options.env = env_;
options.async_wal_precreate = true;
options.recycle_log_file_num = 1;
Options sanitized_options = SanitizeOptions(dbname_, options);
ASSERT_FALSE(sanitized_options.async_wal_precreate);
ASSERT_EQ(1U, sanitized_options.recycle_log_file_num);
options.recycle_log_file_num = 0;
sanitized_options = SanitizeOptions(dbname_, options);
ASSERT_TRUE(sanitized_options.async_wal_precreate);
}
TEST_F(DBOptionsTest, ImmutableVerifySstUniqueIdInManifest) {
Options options;
options.env = env_;
@@ -1729,6 +1764,171 @@ TEST_F(DBOptionsTest, SetOptionsMultipleColumnFamilies) {
ASSERT_TRUE(dbfull()->GetOptions(handles_[2]).disable_auto_compactions);
}
// Confirms the default value and serialization/parse round-trip of the new
// option. No DB open required; exercises only the options-metadata layer.
TEST_F(DBOptionsTest, UseDirectIoForCompactionReadsRoundTrip) {
// Default value must remain false to preserve existing semantics.
ASSERT_FALSE(DBOptions().use_direct_io_for_compaction_reads);
DBOptions parsed;
ConfigOptions config_options;
ASSERT_OK(GetDBOptionsFromString(config_options, DBOptions(),
"use_direct_io_for_compaction_reads=true",
&parsed));
ASSERT_TRUE(parsed.use_direct_io_for_compaction_reads);
ASSERT_OK(GetDBOptionsFromString(config_options, DBOptions(),
"use_direct_io_for_compaction_reads=false",
&parsed));
ASSERT_FALSE(parsed.use_direct_io_for_compaction_reads);
}
// Validates that Open rejects the documented incompatible combination.
TEST_F(DBOptionsTest, UseDirectIoForCompactionReadsValidation) {
// mmap_reads + use_direct_io_for_compaction_reads is rejected at Open
// time, the same way mmap_reads + use_direct_reads has always been
// rejected.
Options bad_options = CurrentOptions();
bad_options.create_if_missing = true;
bad_options.allow_mmap_reads = true;
bad_options.use_direct_io_for_compaction_reads = true;
Status bad_status = TryReopen(bad_options);
ASSERT_TRUE(bad_status.IsNotSupported()) << bad_status.ToString();
}
// Confirms the option is plumbed all the way to the live DB's options API:
// after opening with the flag set, GetDBOptions() reports it back. Only runs
// when the environment supports direct I/O.
TEST_F(DBOptionsTest, UseDirectIoForCompactionReadsLiveReopen) {
Options options = CurrentOptions();
options.create_if_missing = true;
options.use_direct_io_for_compaction_reads = true;
// Use a buffered user-read setup so the new flag is the one doing the work.
options.use_direct_reads = false;
options.use_direct_io_for_flush_and_compaction = false;
Status s = TryReopen(options);
if (s.IsNotSupported() || s.IsInvalidArgument()) {
ROCKSDB_GTEST_BYPASS(
"Direct I/O not supported in this test environment; live reopen "
"cannot be exercised.");
return;
}
ASSERT_OK(s);
ASSERT_TRUE(dbfull()->GetDBOptions().use_direct_io_for_compaction_reads);
Close();
}
TEST_F(DBOptionsTest, UseDirectIoForCompactionReadsUnsupportedFileSystem) {
auto fs = std::make_shared<MockFileSystem>(Env::Default()->GetSystemClock(),
/*supports_direct_io=*/false);
std::unique_ptr<Env> mock_env = NewCompositeEnv(fs);
Options options = CurrentOptions();
options.env = mock_env.get();
options.create_if_missing = true;
options.use_direct_reads = false;
options.use_direct_io_for_flush_and_compaction = false;
options.use_direct_io_for_compaction_reads = true;
Status s = TryReopen(options);
ASSERT_TRUE(s.IsInvalidArgument()) << s.ToString();
ASSERT_NE(s.ToString().find("Direct I/O is not supported"), std::string::npos)
<< s.ToString();
}
TEST_F(
DBOptionsTest,
UseDirectIoForCompactionReadsSetDBOptionsKeepsVerificationReadsBuffered) {
auto fs = std::make_shared<MockFileSystem>(Env::Default()->GetSystemClock(),
/*supports_direct_io=*/true);
std::unique_ptr<Env> mock_env = NewCompositeEnv(fs);
Options options = CurrentOptions();
options.env = mock_env.get();
options.create_if_missing = true;
options.use_direct_reads = false;
options.use_direct_io_for_flush_and_compaction = false;
options.use_direct_io_for_compaction_reads = true;
ASSERT_OK(TryReopen(options));
bool observed = false;
FileOptions observed_file_options;
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
"DBImpl::SetDBOptions:FileOptionsForCompaction", [&](void* arg) {
observed = true;
observed_file_options = *static_cast<FileOptions*>(arg);
});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
ASSERT_OK(dbfull()->SetDBOptions({{"bytes_per_sync", "1024"}}));
ASSERT_TRUE(observed);
ASSERT_FALSE(observed_file_options.use_direct_reads);
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
Close();
}
// Exercises the FileSystem::OptimizeForCompactionTableRead and
// OptimizeForBlobFileRead helpers directly, asserting that the compaction-only
// flag does not affect shared FileSystem hooks.
TEST_F(DBOptionsTest, OptimizeForCompactionTableReadUsesGlobalDirectReadsOnly) {
FileOptions in_opts;
in_opts.use_direct_reads = false;
{
Options check_options;
check_options.use_direct_reads = false;
check_options.use_direct_io_for_compaction_reads = true;
check_options.use_direct_io_for_flush_and_compaction = false;
ImmutableDBOptions immutable(check_options);
FileOptions sst_read =
env_->GetFileSystem()->OptimizeForCompactionTableRead(in_opts,
immutable);
FileOptions blob_read =
env_->GetFileSystem()->OptimizeForBlobFileRead(in_opts, immutable);
EXPECT_FALSE(sst_read.use_direct_reads);
EXPECT_FALSE(blob_read.use_direct_reads);
EXPECT_FALSE(sst_read.use_direct_writes);
}
{
Options off_options;
off_options.use_direct_reads = false;
off_options.use_direct_io_for_compaction_reads = false;
off_options.use_direct_io_for_flush_and_compaction = false;
ImmutableDBOptions immutable_off(off_options);
FileOptions sst_read_off =
env_->GetFileSystem()->OptimizeForCompactionTableRead(in_opts,
immutable_off);
EXPECT_FALSE(sst_read_off.use_direct_reads);
}
{
Options global_on_options;
global_on_options.use_direct_reads = true;
global_on_options.use_direct_io_for_compaction_reads = false;
global_on_options.use_direct_io_for_flush_and_compaction = false;
ImmutableDBOptions immutable_global(global_on_options);
FileOptions sst_read_global =
env_->GetFileSystem()->OptimizeForCompactionTableRead(in_opts,
immutable_global);
EXPECT_TRUE(sst_read_global.use_direct_reads);
}
{
Options both_on;
both_on.use_direct_reads = true;
both_on.use_direct_io_for_compaction_reads = true;
both_on.use_direct_io_for_flush_and_compaction = false;
ImmutableDBOptions immutable_both(both_on);
FileOptions sst_read_both =
env_->GetFileSystem()->OptimizeForCompactionTableRead(in_opts,
immutable_both);
EXPECT_TRUE(sst_read_both.use_direct_reads);
}
}
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {
+43
View File
@@ -13,6 +13,7 @@
#include "db/db_with_timestamp_test_util.h"
#include "db/wide/wide_column_test_util.h"
#include "db/write_batch_internal.h"
#include "file/filename.h"
#include "port/stack_trace.h"
#include "rocksdb/utilities/transaction_db.h"
#include "test_util/sync_point.h"
@@ -922,6 +923,48 @@ TEST_F(DBSecondaryTest, OpenAsSecondaryWALTailing) {
verify_db_func("new_foo_value_1", "new_bar_value");
}
TEST_F(DBSecondaryTest, CatchUpTailsCurrentWalWhenFutureWalExists) {
// Goal: secondary catch-up must keep tailing the current WAL even when a
// higher-number empty WAL is present. This reproduces the async WAL
// precreation shape by creating a future WAL before opening the secondary,
// then appending another write to the current WAL and catching up again.
Options options;
options.env = env_;
Reopen(options);
ASSERT_OK(Put("foo", "value0"));
ASSERT_OK(Flush());
ASSERT_OK(Put("foo", "value1"));
ASSERT_OK(db_->FlushWAL(/*sync=*/true));
std::unique_ptr<WalFile> current_wal;
ASSERT_OK(db_->GetCurrentWalFile(&current_wal));
ASSERT_NE(nullptr, current_wal);
const uint64_t future_wal_number = current_wal->LogNumber() + 1000;
std::unique_ptr<WritableFile> future_wal_file;
ASSERT_OK(env_->NewWritableFile(LogFileName(dbname_, future_wal_number),
&future_wal_file, EnvOptions()));
ASSERT_OK(future_wal_file->Close());
Options secondary_options;
secondary_options.env = env_;
secondary_options.max_open_files = -1;
OpenSecondary(secondary_options);
ReadOptions read_options;
std::string value;
ASSERT_OK(db_secondary_->Get(read_options, "foo", &value));
ASSERT_EQ("value1", value);
ASSERT_OK(Put("foo", "value2"));
ASSERT_OK(db_->FlushWAL(/*sync=*/true));
ASSERT_OK(db_secondary_->TryCatchUpWithPrimary());
ASSERT_OK(db_secondary_->Get(read_options, "foo", &value));
ASSERT_EQ("value2", value);
}
TEST_F(DBSecondaryTest, SecondaryTailingBug_ISSUE_8467) {
Options options;
options.env = env_;
+53
View File
@@ -3516,6 +3516,18 @@ class ModelDB : public DB {
return Status::NotSupported("Not implemented");
}
using DB::PrepareFileIngestion;
Status PrepareFileIngestion(
const std::vector<IngestExternalFileArg>& /*args*/,
std::unique_ptr<FileIngestionHandle>* /*handle*/) override {
return Status::NotSupported("Not implemented.");
}
Status CommitFileIngestionHandles(
std::vector<std::unique_ptr<FileIngestionHandle>> /*handles*/) override {
return Status::NotSupported("Not implemented.");
}
using DB::CreateColumnFamilyWithImport;
Status CreateColumnFamilyWithImport(
const ColumnFamilyOptions& /*options*/,
@@ -3773,6 +3785,12 @@ class ModelDB : public DB {
return Status::OK();
}
Status GetPreparedFileInfoForExternalSstIngestion(
const std::string& /*file_path*/,
std::shared_ptr<const PreparedFileInfo>* /*file_info*/) override {
return Status::NotSupported();
}
Status GetSortedWalFiles(VectorLogPtr& /*files*/) override {
return Status::OK();
}
@@ -8071,6 +8089,41 @@ INSTANTIATE_TEST_CASE_P(OpenFilesAsync, OpenFilesAsyncTest,
::testing::Values(-1, 10),
::testing::Bool()));
TEST_F(DBTest, ReadOnlyCloseDoesNotDeleteWriterFiles) {
Options options = CurrentOptions();
options.create_if_missing = true;
options.disable_auto_compactions = true;
DestroyAndReopen(options);
ASSERT_OK(Put("before", "value"));
ASSERT_OK(Flush());
std::unique_ptr<DB> read_only_db;
ASSERT_OK(DB::OpenForReadOnly(options, dbname_, &read_only_db));
ASSERT_OK(Put("after", "value"));
ASSERT_OK(Flush());
std::vector<LiveFileMetaData> live_files;
db_->GetLiveFilesMetaData(&live_files);
ASSERT_GE(live_files.size(), 2);
std::string newest_sst_path;
uint64_t newest_file_number = 0;
for (const auto& live_file : live_files) {
if (live_file.file_number > newest_file_number) {
newest_file_number = live_file.file_number;
newest_sst_path = live_file.directory + "/" + live_file.relative_filename;
}
}
ASSERT_FALSE(newest_sst_path.empty());
ASSERT_OK(env_->FileExists(newest_sst_path));
read_only_db.reset();
ASSERT_OK(env_->FileExists(newest_sst_path));
}
// Test mix of races with async file open, reads, compactions
TEST_P(OpenFilesAsyncTest, ConcurrentFileAccess) {
Options options = CurrentOptions();
+5
View File
@@ -13,6 +13,7 @@
#include "db/forward_iterator.h"
#include "env/fs_readonly.h"
#include "env/mock_env.h"
#include "file/file_util.h"
#include "port/lang.h"
#include "rocksdb/cache.h"
#include "rocksdb/convenience.h"
@@ -90,6 +91,8 @@ DBTestBase::DBTestBase(const std::string path, bool env_do_fsync)
dbname_ = test::PerThreadDBPath(env_, path);
alternative_wal_dir_ = dbname_ + "/wal";
alternative_db_log_dir_ = dbname_ + "/db_log_dir";
EXPECT_OK(DestroyDir(env_, alternative_wal_dir_));
EXPECT_OK(DestroyDir(env_, alternative_db_log_dir_));
auto options = CurrentOptions();
options.env = env_;
auto delete_options = options;
@@ -117,6 +120,8 @@ DBTestBase::~DBTestBase() {
if (getenv("KEEP_DB")) {
printf("DB is still at %s\n", dbname_.c_str());
} else {
EXPECT_OK(DestroyDir(env_, alternative_wal_dir_));
EXPECT_OK(DestroyDir(env_, alternative_db_log_dir_));
EXPECT_OK(DestroyDB(dbname_, options));
}
// Ensure SstFileManager (and its DeleteScheduler background thread) is
+445 -14
View File
@@ -7,6 +7,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include <algorithm>
#include "db/db_test_util.h"
#include "db/db_with_timestamp_test_util.h"
#include "options/options_helper.h"
@@ -19,12 +21,62 @@
#include "utilities/fault_injection_env.h"
#include "utilities/fault_injection_fs.h"
#if defined(OS_LINUX)
#include <sys/statfs.h>
#endif
#if defined(OS_LINUX) && !defined(BTRFS_SUPER_MAGIC)
#define BTRFS_SUPER_MAGIC 0x9123683E
#endif
#if defined(OS_LINUX) && !defined(TMPFS_MAGIC)
#define TMPFS_MAGIC 0x01021994
#endif
#if defined(OS_LINUX) && !defined(OVERLAYFS_SUPER_MAGIC)
#define OVERLAYFS_SUPER_MAGIC 0x794c7630
#endif
#if defined(OS_LINUX) && !defined(ZFS_SUPER_MAGIC)
#define ZFS_SUPER_MAGIC 0x2fc12fc1
#endif
namespace ROCKSDB_NAMESPACE {
class DBWALTestBase : public DBTestBase {
protected:
explicit DBWALTestBase(const std::string& dir_name)
: DBTestBase(dir_name, /*env_do_fsync=*/true) {}
std::vector<uint64_t> ListWalNumbers() {
std::vector<uint64_t> wal_numbers;
std::vector<std::string> files;
EXPECT_OK(env_->GetChildren(dbname_, &files));
for (const auto& file : files) {
uint64_t number = 0;
FileType type = kWalFile;
if (ParseFileName(file, &number, &type) && type == kWalFile) {
wal_numbers.push_back(number);
}
}
std::sort(wal_numbers.begin(), wal_numbers.end());
return wal_numbers;
}
void CreateEmptyWal(uint64_t wal_number) {
std::unique_ptr<WritableFile> file;
ASSERT_OK(env_->NewWritableFile(LogFileName(dbname_, wal_number), &file,
EnvOptions()));
ASSERT_OK(file->Close());
}
void CreateWalWithContents(uint64_t wal_number, const Slice& contents) {
std::unique_ptr<WritableFile> file;
ASSERT_OK(env_->NewWritableFile(LogFileName(dbname_, wal_number), &file,
EnvOptions()));
ASSERT_OK(file->Append(contents));
ASSERT_OK(file->Close());
}
#if defined(ROCKSDB_PLATFORM_POSIX)
public:
#if defined(ROCKSDB_FALLOCATE_PRESENT)
@@ -57,6 +109,31 @@ class DBWALTestBase : public DBTestBase {
assert(err == 0);
return sbuf.st_blocks * 512;
}
#if defined(ROCKSDB_FALLOCATE_PRESENT)
bool ShouldSkipAllocationCheck(const std::string& file_name) {
(void)file_name;
#if defined(OS_LINUX)
struct statfs fs_stat;
if (statfs(file_name.c_str(), &fs_stat) == 0) {
if (fs_stat.f_type ==
static_cast<decltype(fs_stat.f_type)>(BTRFS_SUPER_MAGIC)) {
return true;
} else if (fs_stat.f_type ==
static_cast<decltype(fs_stat.f_type)>(ZFS_SUPER_MAGIC)) {
return true;
} else if (fs_stat.f_type ==
static_cast<decltype(fs_stat.f_type)>(TMPFS_MAGIC)) {
return true;
} else if (fs_stat.f_type ==
static_cast<decltype(fs_stat.f_type)>(OVERLAYFS_SUPER_MAGIC)) {
return true;
}
}
#endif
return false;
}
#endif // ROCKSDB_FALLOCATE_PRESENT
#endif // ROCKSDB_PLATFORM_POSIX
};
@@ -783,6 +860,8 @@ TEST_F(DBWALTest, WALWithChecksumHandoff) {
std::shared_ptr<FaultInjectionTestFS> fault_fs(
new FaultInjectionTestFS(FileSystem::Default()));
std::unique_ptr<Env> fault_fs_env(NewCompositeEnv(fault_fs));
// Close the DB before the local Env is destroyed if an ASSERT exits early.
Defer close_db_on_exit([this]() { Close(); });
do {
Options options = CurrentOptions();
@@ -2752,16 +2831,35 @@ TEST_F(DBWALTest, TruncateLastLogAfterRecoverWithoutFlush) {
auto& file_before = log_files_before[0];
ASSERT_LT(file_before->SizeFileBytes(), 1 * kKB);
// The log file has preallocated space.
ASSERT_GE(GetAllocatedFileSize(dbname_ + file_before->PathName()),
preallocated_size);
{
std::string fname = dbname_ + file_before->PathName();
uint64_t allocated = GetAllocatedFileSize(fname);
if (ShouldSkipAllocationCheck(fname)) {
fprintf(stderr,
"Skipping preallocation check on this filesystem for %s\n",
fname.c_str());
} else if (allocated < preallocated_size) {
fprintf(stderr,
"Warning: allocated size (%lu) less than preallocated size (%lu) "
"for %s, skipping check. This may indicate the filesystem does "
"not support preallocation or does not report it.\n",
allocated, preallocated_size, fname.c_str());
} else {
ASSERT_GE(allocated, preallocated_size);
}
}
Reopen(options);
VectorLogPtr log_files_after;
ASSERT_OK(dbfull()->GetSortedWalFiles(log_files_after));
ASSERT_EQ(1, log_files_after.size());
ASSERT_LT(log_files_after[0]->SizeFileBytes(), 1 * kKB);
// The preallocated space should be truncated.
ASSERT_LT(GetAllocatedFileSize(dbname_ + file_before->PathName()),
preallocated_size);
{
std::string fname = dbname_ + file_before->PathName();
if (!ShouldSkipAllocationCheck(fname)) {
ASSERT_LT(GetAllocatedFileSize(fname), preallocated_size);
}
}
}
// Tests that we will truncate the preallocated space of the last log from
// previous.
@@ -2788,8 +2886,23 @@ TEST_F(DBWALTest, TruncateLastLogAfterRecoverWithFlush) {
ASSERT_EQ(1, log_files_before.size());
auto& file_before = log_files_before[0];
ASSERT_LT(file_before->SizeFileBytes(), 1 * kKB);
ASSERT_GE(GetAllocatedFileSize(dbname_ + file_before->PathName()),
preallocated_size);
{
std::string fname = dbname_ + file_before->PathName();
uint64_t allocated = GetAllocatedFileSize(fname);
if (ShouldSkipAllocationCheck(fname)) {
fprintf(stderr,
"Skipping preallocation check on this filesystem for %s\n",
fname.c_str());
} else if (allocated < preallocated_size) {
fprintf(stderr,
"Warning: allocated size (%lu) less than preallocated size (%lu) "
"for %s, skipping check. This may indicate the filesystem does "
"not support preallocation or does not report it.\n",
allocated, preallocated_size, fname.c_str());
} else {
ASSERT_GE(allocated, preallocated_size);
}
}
// The log file has preallocated space.
Close();
@@ -2807,8 +2920,12 @@ TEST_F(DBWALTest, TruncateLastLogAfterRecoverWithFlush) {
// if the process is in a crash loop, the log file may not get
// deleted and thte preallocated space will keep accumulating. So we need
// to ensure it gets trtuncated.
EXPECT_LT(GetAllocatedFileSize(dbname_ + file_before->PathName()),
preallocated_size);
{
std::string fname = dbname_ + file_before->PathName();
if (!ShouldSkipAllocationCheck(fname)) {
EXPECT_LT(GetAllocatedFileSize(fname), preallocated_size);
}
}
TEST_SYNC_POINT(
"DBWALTest::TruncateLastLogAfterRecoverWithFlush:AfterTruncate");
reopen_thread.join();
@@ -2863,14 +2980,31 @@ TEST_F(DBWALTest, TruncateLastLogAfterRecoverWALEmpty) {
log_file->PrepareWrite(0, 4096);
log_file.reset();
ASSERT_GE(GetAllocatedFileSize(last_log), preallocated_size);
{
uint64_t allocated = GetAllocatedFileSize(last_log);
if (ShouldSkipAllocationCheck(last_log)) {
fprintf(stderr,
"Skipping preallocation check on this filesystem for %s\n",
last_log.c_str());
} else if (allocated < preallocated_size) {
fprintf(stderr,
"Warning: allocated size (%lu) less than preallocated size (%lu) "
"for %s, skipping check. This may indicate the filesystem does "
"not support preallocation or does not report it.\n",
allocated, preallocated_size, last_log.c_str());
} else {
ASSERT_GE(allocated, preallocated_size);
}
}
port::Thread reopen_thread([&]() { Reopen(options); });
TEST_SYNC_POINT(
"DBWALTest::TruncateLastLogAfterRecoverWithFlush:AfterRecover");
// The preallocated space should be truncated.
EXPECT_LT(GetAllocatedFileSize(last_log), preallocated_size);
if (!ShouldSkipAllocationCheck(last_log)) {
EXPECT_LT(GetAllocatedFileSize(last_log), preallocated_size);
}
TEST_SYNC_POINT(
"DBWALTest::TruncateLastLogAfterRecoverWithFlush:AfterTruncate");
reopen_thread.join();
@@ -2912,8 +3046,23 @@ TEST_F(DBWALTest, ReadOnlyRecoveryNoTruncate) {
auto& file_before = log_files_before[0];
ASSERT_LT(file_before->SizeFileBytes(), 1 * kKB);
// The log file has preallocated space.
auto db_size = GetAllocatedFileSize(dbname_ + file_before->PathName());
ASSERT_GE(db_size, preallocated_size);
std::string fname = dbname_ + file_before->PathName();
auto db_size = GetAllocatedFileSize(fname);
{
if (ShouldSkipAllocationCheck(fname)) {
fprintf(stderr,
"Skipping preallocation check on this filesystem for %s\n",
fname.c_str());
} else if (db_size < preallocated_size) {
fprintf(stderr,
"Warning: allocated size (%lu) less than preallocated size (%lu) "
"for %s, skipping check. This may indicate the filesystem does "
"not support preallocation or does not report it.\n",
db_size, preallocated_size, fname.c_str());
} else {
ASSERT_GE(db_size, preallocated_size);
}
}
Close();
// enable truncate and open DB as readonly, the file should not be truncated
@@ -2927,8 +3076,12 @@ TEST_F(DBWALTest, ReadOnlyRecoveryNoTruncate) {
ASSERT_EQ(log_files_after[0]->PathName(), file_before->PathName());
// The preallocated space should NOT be truncated.
// the DB size is almost the same.
ASSERT_NEAR(GetAllocatedFileSize(dbname_ + file_before->PathName()), db_size,
db_size / 100);
{
std::string fname2 = dbname_ + file_before->PathName();
if (!ShouldSkipAllocationCheck(fname2)) {
ASSERT_NEAR(GetAllocatedFileSize(fname2), db_size, db_size / 100);
}
}
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
@@ -3071,6 +3224,284 @@ TEST_F(DBWALTest, EmptyWalReopenTest) {
}
}
TEST_F(DBWALTest, AsyncWalPrecreateWaitsForPendingFile) {
Options options = CurrentOptions();
options.env = env_;
options.statistics = CreateDBStatistics();
options.async_wal_precreate = true;
options.recycle_log_file_num = 0;
options.write_buffer_size = 1024;
options.max_write_buffer_number = 4;
// Goal: cover the race where foreground WAL rotation reaches the switch
// before the background precreate publishes its result. The sync points hold
// the async worker after it has created the file but before it marks it
// ready, then verify the foreground writer waits instead of allocating a
// higher WAL number.
SyncPoint::GetInstance()->LoadDependency({
{"DBImpl::BGWorkAsyncWALPrecreate:BeforePublish",
"DBWALTest::AsyncWalPrecreateWaitsForPendingFile:WorkerBlocked"},
{"DBWALTest::AsyncWalPrecreateWaitsForPendingFile:AllowPublish",
"DBImpl::BGWorkAsyncWALPrecreate:Publish"},
{"DBImpl::WaitForAsyncWALPrecreate:BeforeWait",
"DBWALTest::AsyncWalPrecreateWaitsForPendingFile:ForegroundWaiting"},
});
SyncPoint::GetInstance()->EnableProcessing();
Defer cleanup_sync_point([] {
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
});
Reopen(options);
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateWaitsForPendingFile:WorkerBlocked");
ASSERT_OK(Put("fill", "value"));
Status writer_status;
port::Thread writer(
[&]() { writer_status = dbfull()->TEST_SwitchMemtable(); });
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateWaitsForPendingFile:ForegroundWaiting");
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateWaitsForPendingFile:AllowPublish");
writer.join();
ASSERT_OK(writer_status);
ASSERT_OK(Put("trigger", "value"));
ASSERT_EQ("value", Get("trigger"));
ASSERT_EQ(1, options.statistics->getTickerCount(WAL_PRECREATE_HIT));
ASSERT_EQ(1, options.statistics->getTickerCount(WAL_PRECREATE_WAITED));
ASSERT_EQ(0, options.statistics->getTickerCount(WAL_PRECREATE_MISS));
}
TEST_F(DBWALTest, AsyncWalPrecreateConsumesReadyFile) {
Options options = CurrentOptions();
options.env = env_;
options.statistics = CreateDBStatistics();
options.async_wal_precreate = true;
options.recycle_log_file_num = 0;
options.write_buffer_size = 1024;
options.max_write_buffer_number = 4;
// Goal: exercise the no-wait consumption path. The background task publishes
// a prepared future WAL before the foreground memtable switch, and the switch
// must install exactly that reserved WAL number instead of allocating
// another.
SyncPoint::GetInstance()->LoadDependency({
{"DBImpl::BGWorkAsyncWALPrecreate:Done",
"DBWALTest::AsyncWalPrecreateConsumesReadyFile:PrecreateDone"},
});
SyncPoint::GetInstance()->EnableProcessing();
Defer cleanup_sync_point([] {
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
});
Reopen(options);
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateConsumesReadyFile:PrecreateDone");
const std::vector<uint64_t> wal_numbers = ListWalNumbers();
ASSERT_GE(wal_numbers.size(), 2);
const uint64_t prepared_wal_number = wal_numbers.back();
ASSERT_NE(prepared_wal_number, dbfull()->TEST_GetCurrentLogNumber());
ASSERT_OK(Put("fill", "value"));
ASSERT_OK(dbfull()->TEST_SwitchMemtable());
ASSERT_EQ(prepared_wal_number, dbfull()->TEST_GetCurrentLogNumber());
ASSERT_EQ(1, options.statistics->getTickerCount(WAL_PRECREATE_HIT));
ASSERT_EQ(0, options.statistics->getTickerCount(WAL_PRECREATE_MISS));
ASSERT_EQ(0, options.statistics->getTickerCount(WAL_PRECREATE_WAITED));
}
TEST_F(DBWALTest, AsyncWalPrecreateRecoveryToleratesActualFutureWal) {
Options options = CurrentOptions();
options.env = env_;
options.async_wal_precreate = true;
options.recycle_log_file_num = 0;
options.track_and_verify_wals = true;
options.track_and_verify_wals_in_manifest = true;
options.avoid_flush_during_shutdown = true;
options.avoid_flush_during_recovery = true;
// Goal: cover the actual async precreation path, not a hand-forged empty WAL.
// The background task opens the future WAL, clean close releases the
// unpublished writer and leaves the empty future WAL behind, and recovery
// must tolerate that file while replaying the preceding WAL with real user
// data.
SyncPoint::GetInstance()->LoadDependency({
{"DBImpl::BGWorkAsyncWALPrecreate:Done",
"DBWALTest::AsyncWalPrecreateRecoveryToleratesActualFutureWal:"
"PrecreateDone"},
});
SyncPoint::GetInstance()->EnableProcessing();
Defer cleanup_sync_point([] {
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
});
DestroyAndReopen(options);
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateRecoveryToleratesActualFutureWal:"
"PrecreateDone");
const std::vector<uint64_t> wal_numbers = ListWalNumbers();
ASSERT_GE(wal_numbers.size(), 2);
const uint64_t future_wal_number = wal_numbers.back();
uint64_t future_wal_size = 0;
ASSERT_OK(env_->GetFileSize(LogFileName(dbname_, future_wal_number),
&future_wal_size));
ASSERT_EQ(0, future_wal_size);
ASSERT_OK(Put("key", "value"));
Close();
ASSERT_OK(TryReopen(options));
ASSERT_EQ("value", Get("key"));
ASSERT_OK(Put("after-recovery", "value2"));
ASSERT_EQ("value2", Get("after-recovery"));
}
TEST_F(DBWALTest, AsyncWalPrecreateDeletesFileOnStartFailure) {
Options options = CurrentOptions();
options.env = env_;
options.statistics = CreateDBStatistics();
options.async_wal_precreate = true;
options.recycle_log_file_num = 0;
options.write_buffer_size = 1024;
options.max_write_buffer_number = 4;
// Goal: force failure after consuming a prepared WAL but before it becomes a
// logical WAL. The failed, unpublished file must be deleted and its writer
// destroyed outside mutex_.
SyncPoint::GetInstance()->LoadDependency({
{"DBImpl::BGWorkAsyncWALPrecreate:Done",
"DBWALTest::AsyncWalPrecreateDeletesFileOnStartFailure:PrecreateDone"},
});
SyncPoint::GetInstance()->EnableProcessing();
Defer cleanup_sync_point([] {
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
});
Reopen(options);
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateDeletesFileOnStartFailure:PrecreateDone");
SyncPoint::GetInstance()->SetCallBack(
"DBImpl::StartWALFile:AfterCompressionTypeRecord", [](void* arg) {
*static_cast<IOStatus*>(arg) =
IOStatus::IOError("injected async WAL start failure");
});
const std::vector<uint64_t> wal_numbers = ListWalNumbers();
ASSERT_GE(wal_numbers.size(), 2);
const uint64_t prepared_wal_number = wal_numbers.back();
ASSERT_OK(Put("fill", "value"));
ASSERT_NOK(dbfull()->TEST_SwitchMemtable());
ASSERT_TRUE(
env_->FileExists(LogFileName(dbname_, prepared_wal_number)).IsNotFound());
ASSERT_EQ(1, options.statistics->getTickerCount(WAL_PRECREATE_HIT));
}
TEST_F(DBWALTest, AsyncWalPrecreateFailureFallsBackToSyncCreation) {
Options options = CurrentOptions();
options.env = env_;
options.statistics = CreateDBStatistics();
options.async_wal_precreate = true;
options.recycle_log_file_num = 0;
options.write_buffer_size = 1024;
options.max_write_buffer_number = 4;
// Goal: fail the background precreate file open after normal DB open has
// created the current WAL. The foreground switch should observe no prepared
// WAL, create one synchronously, and expose both failure and miss counters.
SyncPoint::GetInstance()->LoadDependency({
{"DBImpl::BGWorkAsyncWALPrecreate:Done",
"DBWALTest::AsyncWalPrecreateFailureFallsBackToSyncCreation:"
"PrecreateDone"},
});
SyncPoint::GetInstance()->SetCallBack(
"DBImpl::Open:AfterDeleteFiles",
[&](void*) { env_->non_writable_count_ = 1; });
SyncPoint::GetInstance()->EnableProcessing();
Defer cleanup_sync_point([&] {
env_->non_writable_count_ = 0;
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
});
Reopen(options);
TEST_SYNC_POINT(
"DBWALTest::AsyncWalPrecreateFailureFallsBackToSyncCreation:"
"PrecreateDone");
ASSERT_EQ(1, options.statistics->getTickerCount(WAL_PRECREATE_FAILED));
ASSERT_OK(Put("fill", "value"));
ASSERT_OK(dbfull()->TEST_SwitchMemtable());
ASSERT_EQ(1, options.statistics->getTickerCount(WAL_PRECREATE_MISS));
}
TEST_F(DBWALTest, ReadOnlyErrorIfWalFileExistsIgnoresEmptyWals) {
Options options = CurrentOptions();
options.env = env_;
DestroyAndReopen(options);
Close();
// Goal: preserve error_if_wal_file_exists for WALs containing data while
// allowing empty future WALs left by async precreation. This creates empty
// WAL files directly so the test isolates the read-only open contract.
const std::vector<uint64_t> existing_wal_numbers = ListWalNumbers();
const uint64_t empty_wal_number =
existing_wal_numbers.empty() ? 1 : existing_wal_numbers.back() + 1;
CreateEmptyWal(empty_wal_number);
DBOptions db_options(options);
std::vector<ColumnFamilyDescriptor> column_families;
column_families.emplace_back(kDefaultColumnFamilyName,
ColumnFamilyOptions(options));
std::vector<ColumnFamilyHandle*> handles;
std::unique_ptr<DB> read_only_db;
ASSERT_OK(DB::OpenForReadOnly(db_options, dbname_, column_families, &handles,
&read_only_db,
true /* error_if_wal_file_exists */));
for (ColumnFamilyHandle* handle : handles) {
ASSERT_OK(read_only_db->DestroyColumnFamilyHandle(handle));
}
read_only_db.reset();
handles.clear();
CreateWalWithContents(empty_wal_number + 1, Slice("not empty"));
ASSERT_NOK(DB::OpenForReadOnly(db_options, dbname_, column_families, &handles,
&read_only_db,
true /* error_if_wal_file_exists */));
}
TEST_F(DBWALTest, RecoveryToleratesEmptyFutureWal) {
Options options = CurrentOptions();
options.env = env_;
options.track_and_verify_wals = true;
options.track_and_verify_wals_in_manifest = true;
options.avoid_flush_during_recovery = true;
DestroyAndReopen(options);
ASSERT_OK(Put("key", "value"));
Close();
const std::vector<uint64_t> wal_numbers = ListWalNumbers();
ASSERT_FALSE(wal_numbers.empty());
const uint64_t future_wal_number = wal_numbers.back() + 1;
CreateEmptyWal(future_wal_number);
// Goal: model a crash after async WAL precreation but before consumption.
// Recovery should process the zero-byte future WAL, mark its file number
// used, and keep replaying the real preceding WAL without reporting a WAL
// chain or MANIFEST-tracking error.
ASSERT_OK(TryReopen(options));
ASSERT_EQ("value", Get("key"));
ASSERT_OK(Put("after-recovery", "value2"));
ASSERT_EQ("value2", Get("after-recovery"));
}
TEST_F(DBWALTest, RecoveryFlushSwitchWALOnEmptyMemtable) {
Options options = CurrentOptions();
auto fault_fs = std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
+103
View File
@@ -7,6 +7,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
#include <atomic>
#include <cstdlib>
#include <map>
#include <string>
@@ -258,6 +259,108 @@ TEST_F(DeleteFileTest, BackgroundPurgeIteratorTest) {
CheckFileTypeCounts(dbname_, 0, 1, 1);
}
// Regression test for a use-after-free where an obsolete-file purge started
// DURING DB close -- after CloseHelper's early purge drain -- continued using
// DBImpl after ~DBImpl destroyed mutex_. Seen as a vhost-user-blk SIGABRT when
// many RocksDB instances closed concurrently under a Warm Storage fault, with
// avoid_unnecessary_blocking_io=true on a shared, process-wide Env.
//
// Here we make the straggler deterministic: keep a dropped column family handle
// alive, delete it only after CloseHelper has passed its early purge drain,
// then park that cleanup after FindObsoleteFiles() has incremented
// pending_purge_obsolete_files_ but before PurgeObsoleteFiles(..., true)
// schedules BGWorkPurge. Without the fix, the final drain sees
// bg_purge_scheduled_ == 0 and can destroy DBImpl while the pending cleanup is
// still about to lock/use it. With the fix, CloseHelper waits through the
// pending handoff and then through the scheduled purge.
TEST_F(DeleteFileTest, CloseWaitsForPurgeScheduledDuringClose) {
Options options = CurrentOptions();
SetOptions(&options);
Destroy(options);
options.create_if_missing = true;
// Route obsolete-file deletion through the background purge queue, matching
// the production config that hit the bug.
options.avoid_unnecessary_blocking_io = true;
Reopen(options);
// A dropped CF whose handle we keep alive: deleting the handle schedules a
// *background* purge (the dropped-CF teardown path).
ColumnFamilyHandle* cfh = nullptr;
ASSERT_OK(db_->CreateColumnFamily(ColumnFamilyOptions(), "dropme", &cfh));
ASSERT_OK(db_->Put(WriteOptions(), cfh, "key", "value"));
ASSERT_OK(db_->Flush(FlushOptions(), cfh));
ASSERT_OK(db_->DropColumnFamily(cfh));
ASSERT_OK(dbfull()->TEST_WaitForPurge()); // settle setup purges
std::atomic<bool> block_pending_handoff{false};
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
SyncPoint::GetInstance()->SetCallBack(
"DBImpl::PurgeObsoleteFiles:BeforePendingPurgeFinished", [&](void*) {
if (!block_pending_handoff.exchange(false)) {
return;
}
TEST_SYNC_POINT(
"DeleteFileTest::CloseWaitsForPurge:PendingHandoffReady");
TEST_SYNC_POINT(
"DeleteFileTest::CloseWaitsForPurge:PendingHandoffBlocked");
});
SyncPoint::GetInstance()->LoadDependency({
// Delete the dropped CF handle only after CloseHelper has passed its
// early purge drain and entered a mutex-unlocked close window.
{"DBImpl::CloseHelper:CFHandleCleanupUnlocked",
"DeleteFileTest::CloseWaitsForPurge:DropDuringCloseUnlock"},
// Let the test thread observe that the deleter is parked in the pending
// handoff window.
{"DeleteFileTest::CloseWaitsForPurge:PendingHandoffReady",
"DeleteFileTest::CloseWaitsForPurge:WaitForPendingHandoff"},
// Keep CloseHelper in the mutex-unlocked close window until the
// dropped-CF cleanup reaches the pending-to-scheduled handoff.
{"DeleteFileTest::CloseWaitsForPurge:PendingHandoffReady",
"DBImpl::CloseHelper:CFHandleCleanupAllowed"},
// Observe that CloseHelper is actually waiting in the final purge drain
// while the dropped-CF cleanup is still parked in the pending handoff.
{"DBImpl::CloseHelper:FinalPurgeDrainWait",
"DeleteFileTest::CloseWaitsForPurge:WaitForFinalPurgeDrain"},
// Keep the dropped-CF cleanup parked in the pending handoff window until
// the test releases it.
{"DeleteFileTest::CloseWaitsForPurge:ReleasePendingHandoff",
"DeleteFileTest::CloseWaitsForPurge:PendingHandoffBlocked"},
// Park the scheduled BGWorkPurge before it locks mutex_ until we release
// it, so CloseHelper also has to wait for bg_purge_scheduled_.
{"DeleteFileTest::CloseWaitsForPurge:ReleaseBackgroundPurge",
"DBImpl::BackgroundCallPurge:beforeMutexLock"},
});
SyncPoint::GetInstance()->EnableProcessing();
port::Thread closer([&]() { db_.reset(); });
// Unblocks once CloseHelper is in a mutex-unlocked close window; then
// deleting the dropped CF handle starts purge work the early drain missed.
TEST_SYNC_POINT("DeleteFileTest::CloseWaitsForPurge:DropDuringCloseUnlock");
block_pending_handoff.store(true);
ColumnFamilyHandle* dropped_cfh = cfh;
cfh = nullptr;
port::Thread dropped_cf_deleter([dropped_cfh]() { delete dropped_cfh; });
TEST_SYNC_POINT("DeleteFileTest::CloseWaitsForPurge:WaitForPendingHandoff");
// Wait until CloseHelper is blocked in the final drain with no scheduled
// purge yet. Without the fix, it would proceed while the dropped-CF cleanup
// is still pending. With the fix, it waits for pending_purge_obsolete_files_.
TEST_SYNC_POINT("DeleteFileTest::CloseWaitsForPurge:WaitForFinalPurgeDrain");
// Release the pending cleanup, then the BGWorkPurge it schedules. With the
// fix, the closer waits for both transitions and completes cleanly.
TEST_SYNC_POINT("DeleteFileTest::CloseWaitsForPurge:ReleasePendingHandoff");
dropped_cf_deleter.join();
TEST_SYNC_POINT("DeleteFileTest::CloseWaitsForPurge:ReleaseBackgroundPurge");
closer.join();
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(DeleteFileTest, PurgeDuringOpen) {
Options options = CurrentOptions();
CheckFileTypeCounts(dbname_, -1, 0, -1);
+4 -4
View File
@@ -1311,7 +1311,7 @@ TEST_F(ExternalSSTFileBasicTest, SyncFailure) {
});
if (i == 0) {
SyncPoint::GetInstance()->SetCallBack(
"ExternalSstFileIngestionJob::Prepare:Reopen", [&](void* s) {
"ExternalSstFileIngestionJob::CheckSyncReturnCode", [&](void* s) {
Status* status = static_cast<Status*>(s);
if (status->IsNotSupported()) {
no_sync = true;
@@ -1367,13 +1367,13 @@ TEST_F(ExternalSSTFileBasicTest, SyncFailure) {
}
}
TEST_F(ExternalSSTFileBasicTest, ReopenNotSupported) {
TEST_F(ExternalSSTFileBasicTest, SyncFileNotSupported) {
Options options;
options.create_if_missing = true;
options.env = env_;
SyncPoint::GetInstance()->SetCallBack(
"ExternalSstFileIngestionJob::Prepare:Reopen", [&](void* arg) {
"ExternalSstFileIngestionJob::CheckSyncReturnCode", [&](void* arg) {
Status* s = static_cast<Status*>(arg);
*s = Status::NotSupported();
});
@@ -1386,7 +1386,7 @@ TEST_F(ExternalSSTFileBasicTest, ReopenNotSupported) {
std::unique_ptr<SstFileWriter> sst_file_writer(
new SstFileWriter(EnvOptions(), sst_file_writer_options));
std::string file_name =
sst_files_dir_ + "reopen_not_supported_test_" + ".sst";
sst_files_dir_ + "sync_file_not_supported_test_" + ".sst";
ASSERT_OK(sst_file_writer->Open(file_name));
ASSERT_OK(sst_file_writer->Put("bar", "v2"));
ASSERT_OK(sst_file_writer->Finish());
+289 -110
View File
@@ -19,6 +19,7 @@
#include "logging/logging.h"
#include "monitoring/statistics_impl.h"
#include "table/merging_iterator.h"
#include "table/prepared_file_info.h"
#include "table/sst_file_writer_collectors.h"
#include "table/table_builder.h"
#include "table/unique_id_impl.h"
@@ -31,18 +32,24 @@ Status ExternalSstFileIngestionJob::Prepare(
const std::vector<std::string>& external_files_paths,
const std::vector<std::string>& files_checksums,
const std::vector<std::string>& files_checksum_func_names,
const std::vector<const PreparedFileInfo*>& file_infos,
const std::optional<RangeOpt>& atomic_replace_range,
const Temperature& file_temperature, uint64_t next_file_number,
SuperVersion* sv) {
Status status;
// Read the information of files we are ingesting
for (const std::string& file_path : external_files_paths) {
// Read the information of files we are ingesting.
for (size_t i = 0; i < external_files_paths.size(); i++) {
const std::string& file_path = external_files_paths[i];
IngestedFileInfo file_to_ingest;
// For temperature, first assume it matches provided hint
file_to_ingest.file_temperature = file_temperature;
status =
GetIngestedFileInfo(file_path, next_file_number++, &file_to_ingest, sv);
file_to_ingest.prefetch_lmax_index_and_filter_blocks =
ingestion_options_.prefetch_lmax_index_and_filter_blocks;
const PreparedFileInfo* prepared_file_info =
file_infos.empty() ? nullptr : file_infos[i];
status = GetIngestedFileInfo(file_path, next_file_number++,
prepared_file_info, &file_to_ingest, sv);
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to get ingested file info: %s: %s",
@@ -76,23 +83,10 @@ Status ExternalSstFileIngestionJob::Prepare(
auto num_files = files_to_ingest_.size();
if (num_files == 0) {
return Status::InvalidArgument("The list of files is empty");
} else if (num_files > 1) {
// Verify that passed files don't have overlapping ranges
autovector<const IngestedFileInfo*> sorted_files;
for (size_t i = 0; i < num_files; i++) {
sorted_files.push_back(&files_to_ingest_[i]);
}
std::sort(sorted_files.begin(), sorted_files.end(), file_range_checker_);
for (size_t i = 0; i + 1 < num_files; i++) {
if (file_range_checker_.Overlaps(*sorted_files[i], *sorted_files[i + 1],
/* known_sorted= */ true)) {
files_overlap_ = true;
break;
}
}
}
// Detect whether the input files overlap one another; this drives how they
// are divided into batches below.
files_overlap_ = ComputeFilesOverlap(files_to_ingest_);
if (atomic_replace_range.has_value()) {
atomic_replace_range_.emplace();
@@ -165,35 +159,21 @@ Status ExternalSstFileIngestionJob::Prepare(
// It is unsafe to assume application had sync the file and file
// directory before ingest the file. For integrity of RocksDB we need
// to sync the file.
// TODO(xingbo), We should in general be moving away from production
// uses of ReuseWritableFile (except explicitly for WAL recycling),
// ReopenWritableFile, and NewRandomRWFile. We should create a
// FileSystem::SyncFile/FsyncFile API that by default does the
// re-open+sync+close combo but can (a) be reused easily, and (b) be
// overridden to do that more cleanly, e.g. in EncryptedEnv.
// https://github.com/facebook/rocksdb/issues/13741
std::unique_ptr<FSWritableFile> file_to_sync;
Status s = fs_->ReopenWritableFile(path_inside_db, env_options_,
&file_to_sync, nullptr);
TEST_SYNC_POINT_CALLBACK("ExternalSstFileIngestionJob::Prepare:Reopen",
&s);
TEST_SYNC_POINT("ExternalSstFileIngestionJob::BeforeSyncIngestedFile");
Status s = fs_->SyncFile(path_inside_db, env_options_, IOOptions(),
db_options_.use_fsync, nullptr);
TEST_SYNC_POINT("ExternalSstFileIngestionJob::AfterSyncIngestedFile");
TEST_SYNC_POINT_CALLBACK(
"ExternalSstFileIngestionJob::CheckSyncReturnCode", &s);
// Some file systems (especially remote/distributed) don't support
// reopening a file for writing and don't require reopening and
// syncing the file. Ignore the NotSupported error in that case.
// explicitly syncing the file and don't require it. Ignore the
// NotSupported error in that case.
if (!s.IsNotSupported()) {
status = s;
if (status.ok()) {
TEST_SYNC_POINT(
"ExternalSstFileIngestionJob::BeforeSyncIngestedFile");
status = SyncIngestedFile(file_to_sync.get());
TEST_SYNC_POINT(
"ExternalSstFileIngestionJob::AfterSyncIngestedFile");
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to sync ingested file %s: %s",
path_inside_db.c_str(), status.ToString().c_str());
}
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to sync ingested file %s: %s",
path_inside_db.c_str(), status.ToString().c_str());
}
}
} else if (status.IsNotSupported() &&
@@ -447,6 +427,60 @@ void ExternalSstFileIngestionJob::DivideInputFilesIntoBatches() {
}
}
bool ExternalSstFileIngestionJob::ComputeFilesOverlap(
const autovector<IngestedFileInfo>& files) const {
const size_t num_files = files.size();
if (num_files <= 1) {
return false;
}
// Verify whether the files have overlapping ranges by sorting copies of the
// file ranges and checking adjacent pairs.
autovector<const IngestedFileInfo*> sorted_files;
for (size_t i = 0; i < num_files; i++) {
sorted_files.push_back(&files[i]);
}
std::sort(sorted_files.begin(), sorted_files.end(), file_range_checker_);
for (size_t i = 0; i + 1 < num_files; i++) {
if (file_range_checker_.Overlaps(*sorted_files[i], *sorted_files[i + 1],
/* known_sorted= */ true)) {
return true;
}
}
return false;
}
Status ExternalSstFileIngestionJob::MergeForSameColumnFamily(
ExternalSstFileIngestionJob* other) {
assert(other != nullptr);
assert(other != this);
assert(cfd_ == other->cfd_);
if (atomic_replace_range_.has_value() ||
other->atomic_replace_range_.has_value()) {
return Status::NotSupported(
"cannot merge file ingestion handles for the same column family when "
"atomic_replace_range is used");
}
if (!(ingestion_options_ == other->ingestion_options_)) {
return Status::InvalidArgument(
"file ingestion handles for the same column family must be prepared "
"with the same IngestExternalFileOptions");
}
// Append the other job's prepared files after this job's so that, for any
// overlapping keys, the other job's data wins via a higher assigned sequence
// number -- the same semantics as passing all the files to a single ingestion
// call in this order. Recompute overlap and rebuild the batches over the
// union.
for (IngestedFileInfo& file : other->files_to_ingest_) {
files_to_ingest_.push_back(std::move(file));
}
other->files_to_ingest_.clear();
other->file_batches_to_ingest_.clear();
files_overlap_ = ComputeFilesOverlap(files_to_ingest_);
file_batches_to_ingest_.clear();
DivideInputFilesIntoBatches();
return Status::OK();
}
Status ExternalSstFileIngestionJob::NeedsFlush(bool* flush_needed,
SuperVersion* super_version) {
Status status;
@@ -716,6 +750,9 @@ Status ExternalSstFileIngestionJob::AssignLevelsForOneBatch(
tail_size, file->user_defined_timestamps_persisted, "", "");
f_metadata.temperature = file->file_temperature;
f_metadata.marked_for_compaction = marked_for_compaction;
f_metadata.skip_index_and_filter_blocks_prefetch =
!file->prefetch_lmax_index_and_filter_blocks &&
file->picked_level == cfd_->NumberLevels() - 1;
// Extract min/max timestamps from table properties for UDT support.
// This ensures ingested files have proper timestamp ranges in FileMetaData,
// similar to files created by flush and compaction.
@@ -970,18 +1007,14 @@ Status ExternalSstFileIngestionJob::ResetTableReader(
}
Status ExternalSstFileIngestionJob::SanityCheckTableProperties(
const std::string& external_file, uint64_t new_file_number,
SuperVersion* sv, IngestedFileInfo* file_to_ingest,
std::unique_ptr<TableReader>* table_reader) {
// Get the external file properties
auto props = table_reader->get()->GetTableProperties();
assert(props.get());
const auto& uprops = props->user_collected_properties;
const std::string& external_file, const TableProperties& props,
IngestedFileInfo* file_to_ingest) {
const auto& uprops = props.user_collected_properties;
// Get table version
auto version_iter = uprops.find(ExternalSstFilePropertyNames::kVersion);
if (version_iter == uprops.end()) {
assert(!SstFileWriter::CreatedBySstFileWriter(*props));
assert(!SstFileWriter::CreatedBySstFileWriter(props));
if (!ingestion_options_.allow_db_generated_files) {
return Status::Corruption("External file version not found");
} else {
@@ -990,7 +1023,7 @@ Status ExternalSstFileIngestionJob::SanityCheckTableProperties(
file_to_ingest->version = 0;
}
} else {
assert(SstFileWriter::CreatedBySstFileWriter(*props));
assert(SstFileWriter::CreatedBySstFileWriter(props));
file_to_ingest->version = DecodeFixed32(version_iter->second.c_str());
}
@@ -1004,12 +1037,17 @@ Status ExternalSstFileIngestionJob::SanityCheckTableProperties(
// Set the global sequence number
file_to_ingest->original_seqno = DecodeFixed64(seqno_iter->second.c_str());
if (props->external_sst_file_global_seqno_offset == 0) {
file_to_ingest->global_seqno_offset = 0;
file_to_ingest->global_seqno_offset =
static_cast<size_t>(props.external_sst_file_global_seqno_offset);
// The on-disk offset is only needed if we will write the global seqno back
// into the file (write_global_seqno). The metadata fast-path does not open
// the file, and its in-memory table properties do not carry the offset
// (it is only computed while reading the file back); that is fine as long
// as write_global_seqno is not requested.
if (ingestion_options_.write_global_seqno &&
file_to_ingest->global_seqno_offset == 0) {
return Status::Corruption("Was not able to find file global seqno field");
}
file_to_ingest->global_seqno_offset =
static_cast<size_t>(props->external_sst_file_global_seqno_offset);
} else if (file_to_ingest->version == 1) {
// SST file V1 should not have global seqno field
assert(seqno_iter == uprops.end());
@@ -1030,23 +1068,20 @@ Status ExternalSstFileIngestionJob::SanityCheckTableProperties(
" is not supported");
}
file_to_ingest->cf_id = static_cast<uint32_t>(props->column_family_id);
// This assignment works fine even though `table_reader` may later be reset,
// since that will not affect how table properties are parsed, and this
// assignment is making a copy.
file_to_ingest->table_properties = *props;
file_to_ingest->cf_id = static_cast<uint32_t>(props.column_family_id);
file_to_ingest->table_properties = props;
// Get number of entries in table
file_to_ingest->num_entries = props->num_entries;
file_to_ingest->num_range_deletions = props->num_range_deletions;
file_to_ingest->num_entries = props.num_entries;
file_to_ingest->num_range_deletions = props.num_range_deletions;
// Validate table properties related to comparator name and user defined
// timestamps persisted flag.
file_to_ingest->user_defined_timestamps_persisted =
static_cast<bool>(props->user_defined_timestamps_persisted);
static_cast<bool>(props.user_defined_timestamps_persisted);
bool mark_sst_file_has_no_udt = false;
Status s = ValidateUserDefinedTimestampsOptions(
cfd_->user_comparator(), props->comparator_name,
cfd_->user_comparator(), props.comparator_name,
cfd_->ioptions().persist_user_defined_timestamps,
file_to_ingest->user_defined_timestamps_persisted,
&mark_sst_file_has_no_udt);
@@ -1054,7 +1089,9 @@ Status ExternalSstFileIngestionJob::SanityCheckTableProperties(
// A column family that enables user-defined timestamps in Memtable only
// feature can also ingest external files created by a setting that disables
// user-defined timestamps. In that case, we need to re-mark the
// user_defined_timestamps_persisted flag for the file.
// user_defined_timestamps_persisted flag for the file. The open-and-scan
// caller is then responsible for reopening its `TableReader` with the
// updated flag.
file_to_ingest->user_defined_timestamps_persisted = false;
} else if (!s.ok()) {
ROCKS_LOG_WARN(
@@ -1064,22 +1101,84 @@ Status ExternalSstFileIngestionJob::SanityCheckTableProperties(
return s;
}
// `TableReader` is initialized with `user_defined_timestamps_persisted` flag
// to be true. If its value changed to false after this sanity check, we
// need to reset the `TableReader`.
if (ucmp_->timestamp_size() > 0 &&
!file_to_ingest->user_defined_timestamps_persisted) {
s = ResetTableReader(external_file, new_file_number,
file_to_ingest->user_defined_timestamps_persisted, sv,
file_to_ingest, table_reader);
}
return s;
}
Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
Status ExternalSstFileIngestionJob::GetIngestedFileInfoFromFileInfo(
const std::string& external_file,
const PreparedFileInfo& prepared_file_info,
IngestedFileInfo* file_to_ingest) {
// Boundaries, size, and table properties were obtained without opening the
// file (e.g. produced by SstFileWriter::Finish). We reuse them directly.
file_to_ingest->file_size = prepared_file_info.file_size;
Status status = SanityCheckTableProperties(
external_file, prepared_file_info.table_properties, file_to_ingest);
if (!status.ok()) {
ROCKS_LOG_WARN(
db_options_.info_log,
"Failed to sanity check table properties for external file %s: %s",
external_file.c_str(), status.ToString().c_str());
return status;
}
const size_t ts_sz = ucmp_->timestamp_size();
if (ts_sz > 0 && !file_to_ingest->user_defined_timestamps_persisted) {
auto pad_timestamp = [ts_sz](std::string* result, const Slice& key) {
assert(result->empty());
if (ExtractValueType(key) == kTypeRangeDeletion) {
PadInternalKeyWithMaxTimestamp(result, key, ts_sz);
} else {
PadInternalKeyWithMinTimestamp(result, key, ts_sz);
}
};
pad_timestamp(file_to_ingest->smallest_internal_key.rep(),
prepared_file_info.smallest.Encode());
pad_timestamp(file_to_ingest->largest_internal_key.rep(),
prepared_file_info.largest.Encode());
} else {
file_to_ingest->smallest_internal_key = prepared_file_info.smallest;
file_to_ingest->largest_internal_key = prepared_file_info.largest;
}
if (ingestion_options_.allow_db_generated_files) {
// Sequence numbers are preserved (not reassigned), so the bounds must be
// known before we skip the GetSeqnoBoundaryForFile scan.
if (!file_to_ingest->table_properties.HasKeyLargestSeqno()) {
return Status::Corruption("Unknown largest seqno for db generated file.");
}
file_to_ingest->largest_seqno =
file_to_ingest->table_properties.key_largest_seqno;
if (file_to_ingest->largest_seqno == 0) {
file_to_ingest->smallest_seqno = 0;
} else {
if (!file_to_ingest->table_properties.HasKeySmallestSeqno()) {
return Status::Corruption(
"Unknown smallest seqno for db generated file.");
}
file_to_ingest->smallest_seqno =
file_to_ingest->table_properties.key_smallest_seqno;
}
} else {
// Normal ingestion reassigns a global sequence number later, so the file's
// keys must currently be at seqno 0 (mirror of the open-and-scan check).
SequenceNumber largest_seqno =
file_to_ingest->table_properties.key_largest_seqno;
// UINT64_MAX means unknown and the file is generated before table property
// `key_largest_seqno` is introduced.
if (largest_seqno != UINT64_MAX && largest_seqno > 0) {
return Status::Corruption(
"External file has non zero largest sequence number " +
std::to_string(largest_seqno));
}
}
return Status::OK();
}
Status ExternalSstFileIngestionJob::GetIngestedFileInfoFromFile(
const std::string& external_file, uint64_t new_file_number,
IngestedFileInfo* file_to_ingest, SuperVersion* sv) {
file_to_ingest->external_file_path = external_file;
IngestedFileInfo* file_to_ingest, SuperVersion* sv,
std::unique_ptr<TableReader>* out_table_reader) {
TEST_SYNC_POINT("ExternalSstFileIngestionJob::GetIngestedFileInfo:ReadPath");
// Get external file size
Status status = fs_->GetFileSize(external_file, IOOptions(),
@@ -1091,15 +1190,11 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
return status;
}
// Assign FD with number
file_to_ingest->fd =
FileDescriptor(new_file_number, 0, file_to_ingest->file_size);
// Create TableReader for external file
// Create TableReader for external file.
std::unique_ptr<TableReader> table_reader;
// Initially create the `TableReader` with flag
// `user_defined_timestamps_persisted` to be true since that's the most common
// case
// `user_defined_timestamps_persisted` to be true since that's the most
// common case
status = ResetTableReader(external_file, new_file_number,
/*user_defined_timestamps_persisted=*/true, sv,
file_to_ingest, &table_reader);
@@ -1110,8 +1205,8 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
return status;
}
status = SanityCheckTableProperties(external_file, new_file_number, sv,
file_to_ingest, &table_reader);
status = SanityCheckTableProperties(
external_file, *table_reader->GetTableProperties(), file_to_ingest);
if (!status.ok()) {
ROCKS_LOG_WARN(
db_options_.info_log,
@@ -1120,6 +1215,23 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
return status;
}
// The `TableReader` above was opened with `user_defined_timestamps_persisted`
// assumed true. If the sanity check determined the file has no persisted
// timestamps (UDT-in-Memtable-only feature), reopen it with the corrected
// flag so keys are parsed properly by the scan below.
if (ucmp_->timestamp_size() > 0 &&
!file_to_ingest->user_defined_timestamps_persisted) {
status = ResetTableReader(external_file, new_file_number,
file_to_ingest->user_defined_timestamps_persisted,
sv, file_to_ingest, &table_reader);
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to reset table reader for external file %s: %s",
external_file.c_str(), status.ToString().c_str());
return status;
}
}
const bool allow_data_in_errors = db_options_.allow_data_in_errors;
ParsedInternalKey key;
if (ingestion_options_.allow_db_generated_files) {
@@ -1141,8 +1253,8 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
} else {
SequenceNumber largest_seqno =
table_reader.get()->GetTableProperties()->key_largest_seqno;
// UINT64_MAX means unknown and the file is generated before table property
// `key_largest_seqno` is introduced.
// UINT64_MAX means unknown and the file is generated before table
// property `key_largest_seqno` is introduced.
if (largest_seqno != UINT64_MAX && largest_seqno > 0) {
return Status::Corruption(
"External file has non zero largest sequence number " +
@@ -1150,24 +1262,6 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
}
}
if (ingestion_options_.verify_checksums_before_ingest) {
// If customized readahead size is needed, we can pass a user option
// all the way to here. Right now we just rely on the default readahead
// to keep things simple.
// TODO: plumb Env::IOActivity, Env::IOPriority
ReadOptions ro;
ro.readahead_size = ingestion_options_.verify_checksums_readahead_size;
ro.fill_cache = ingestion_options_.fill_cache;
status = table_reader->VerifyChecksum(
ro, TableReaderCaller::kExternalSSTIngestion);
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to verify checksum for table reader: %s",
status.ToString().c_str());
return status;
}
}
// TODO: plumb Env::IOActivity, Env::IOPriority
ReadOptions ro;
ro.fill_cache = ingestion_options_.fill_cache;
@@ -1247,6 +1341,22 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
"External file has a range deletion with non zero sequence "
"number.");
}
#ifndef NDEBUG
// To keep aligned with the fast path that expects range deletion keys to
// always use max ts.
const size_t ts_sz = ucmp_->timestamp_size();
if (ts_sz > 0) {
const std::string max_ts(ts_sz, '\xff');
assert(key.user_key.size() >= ts_sz);
assert(ucmp_->CompareTimestamp(
ExtractTimestampFromUserKey(key.user_key, ts_sz), max_ts) ==
0);
assert(range_del_iter->value().size() >= ts_sz);
assert(ucmp_->CompareTimestamp(
ExtractTimestampFromUserKey(range_del_iter->value(), ts_sz),
max_ts) == 0);
}
#endif
RangeTombstone tombstone(key, range_del_iter->value());
file_range_checker_.MaybeUpdateRange(tombstone.SerializeKey(),
tombstone.SerializeEndKey(),
@@ -1254,6 +1364,75 @@ Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
}
}
*out_table_reader = std::move(table_reader);
return Status::OK();
}
Status ExternalSstFileIngestionJob::GetIngestedFileInfo(
const std::string& external_file, uint64_t new_file_number,
const PreparedFileInfo* prepared_file_info,
IngestedFileInfo* file_to_ingest, SuperVersion* sv) {
file_to_ingest->external_file_path = external_file;
std::unique_ptr<TableReader> table_reader;
Status status =
prepared_file_info != nullptr
? GetIngestedFileInfoFromFileInfo(external_file, *prepared_file_info,
file_to_ingest)
: GetIngestedFileInfoFromFile(external_file, new_file_number,
file_to_ingest, sv, &table_reader);
if (!status.ok()) {
return status;
}
assert(file_to_ingest->file_size > 0);
assert(!file_to_ingest->unset());
assert(file_to_ingest->table_properties.num_entries > 0 ||
file_to_ingest->table_properties.num_range_deletions > 0);
if (ingestion_options_.allow_db_generated_files) {
// These files keep their original sequence numbers (derived, not
// reassigned), so the bounds must be valid here.
assert(file_to_ingest->smallest_seqno <= file_to_ingest->largest_seqno);
assert(file_to_ingest->largest_seqno < kMaxSequenceNumber);
}
// Verify the file checksum if requested. The open-and-scan path already has a
// `TableReader` open; the fast-path opens one here only when verification is
// requested -- otherwise it performs no file I/O.
if (ingestion_options_.verify_checksums_before_ingest) {
if (table_reader == nullptr) {
status =
ResetTableReader(external_file, new_file_number,
file_to_ingest->user_defined_timestamps_persisted,
sv, file_to_ingest, &table_reader);
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to reset table reader for external file %s: %s",
external_file.c_str(), status.ToString().c_str());
return status;
}
}
// If customized readahead size is needed, we can pass a user option all the
// way to here. Right now we just rely on the default readahead to keep
// things simple.
// TODO: plumb Env::IOActivity, Env::IOPriority
ReadOptions ro;
ro.readahead_size = ingestion_options_.verify_checksums_readahead_size;
ro.fill_cache = ingestion_options_.fill_cache;
status = table_reader->VerifyChecksum(
ro, TableReaderCaller::kExternalSSTIngestion);
if (!status.ok()) {
ROCKS_LOG_WARN(db_options_.info_log,
"Failed to verify checksum for external file %s: %s",
external_file.c_str(), status.ToString().c_str());
return status;
}
}
// Assign FD with number.
file_to_ingest->fd =
FileDescriptor(new_file_number, 0, file_to_ingest->file_size);
const size_t ts_sz = ucmp_->timestamp_size();
Slice smallest = file_to_ingest->smallest_internal_key.user_key();
Slice largest = file_to_ingest->largest_internal_key.user_key();
+47 -5
View File
@@ -181,6 +181,9 @@ struct IngestedFileInfo : public KeyRangeInfo {
// setting.
bool user_defined_timestamps_persisted = true;
// Whether Lmax commit-time table opening should prefetch index/filter blocks.
bool prefetch_lmax_index_and_filter_blocks = true;
SequenceNumber largest_seqno = kMaxSequenceNumber;
SequenceNumber smallest_seqno = kMaxSequenceNumber;
};
@@ -248,6 +251,7 @@ class ExternalSstFileIngestionJob {
Status Prepare(const std::vector<std::string>& external_files_paths,
const std::vector<std::string>& files_checksums,
const std::vector<std::string>& files_checksum_func_names,
const std::vector<const PreparedFileInfo*>& file_infos,
const std::optional<RangeOpt>& atomic_replace_range,
const Temperature& file_temperature, uint64_t next_file_number,
SuperVersion* sv);
@@ -301,6 +305,20 @@ class ExternalSstFileIngestionJob {
return max_assigned_seqno_;
}
// Max threads requested for opening this job's files during commit, from the
// per-CF IngestExternalFileOptions.
int file_opening_threads() const {
return ingestion_options_.file_opening_threads;
}
// Merge another already-Prepare()d job for the SAME column family into this
// one so both sets of files are committed by a single Run(). The other job's
// files are appended after this job's, so for any overlapping keys the other
// job's data wins via a higher assigned sequence number -- the same semantics
// as passing all the files to a single ingestion call in this order. The
// other job is left empty.
Status MergeForSameColumnFamily(ExternalSstFileIngestionJob* other);
private:
Status ResetTableReader(const std::string& external_file,
uint64_t new_file_number,
@@ -315,17 +333,37 @@ class ExternalSstFileIngestionJob {
// different options. For example: when external file does not contain
// timestamps while column family enables UDT in Memtables only feature.
Status SanityCheckTableProperties(const std::string& external_file,
uint64_t new_file_number, SuperVersion* sv,
IngestedFileInfo* file_to_ingest,
std::unique_ptr<TableReader>* table_reader);
const TableProperties& props,
IngestedFileInfo* file_to_ingest);
// Open the external file and populate `file_to_ingest` with all the
// external information we need to ingest this file.
// external information we need to ingest this file. When
// `prepared_file_info` is non-null, its caller-supplied metadata is reused
// instead of opening and scanning the file.
Status GetIngestedFileInfo(const std::string& external_file,
uint64_t new_file_number,
const PreparedFileInfo* prepared_file_info,
IngestedFileInfo* file_to_ingest,
SuperVersion* sv);
// Acquire the per-file metadata from the caller-supplied opaque
// `PreparedFileInfo` (produced by SstFileWriter::Finish) instead of opening
// the file.
Status GetIngestedFileInfoFromFileInfo(
const std::string& external_file,
const PreparedFileInfo& prepared_file_info,
IngestedFileInfo* file_to_ingest);
// Acquire the per-file metadata by opening the external file and scanning it
// (table properties, sequence number bounds, and boundary keys including any
// range-tombstone extensions). Used when no file_info is available. The
// opened `TableReader` is returned via `*table_reader` so the caller can
// reuse it (e.g. to verify the file checksum) without re-opening the file.
Status GetIngestedFileInfoFromFile(
const std::string& external_file, uint64_t new_file_number,
IngestedFileInfo* file_to_ingest, SuperVersion* sv,
std::unique_ptr<TableReader>* out_table_reader);
// If the input files' key range overlaps themselves, this function divides
// them in the user specified order into multiple batches. Where the files
// within a batch do not overlap with each other, but key range could overlap
@@ -334,6 +372,10 @@ class ExternalSstFileIngestionJob {
// make one batch.
void DivideInputFilesIntoBatches();
// Returns whether any two files in `files` have overlapping key ranges, by
// sorting the file ranges and checking adjacent pairs.
bool ComputeFilesOverlap(const autovector<IngestedFileInfo>& files) const;
// Assign level for the files in one batch. The files within one batch are not
// overlapping, and we assign level to each file one after another.
// If `prev_batch_uppermost_level` is specified, all files in this batch will
@@ -407,7 +449,7 @@ class ExternalSstFileIngestionJob {
SnapshotList* db_snapshots_;
autovector<IngestedFileInfo> files_to_ingest_;
std::vector<FileBatchInfo> file_batches_to_ingest_;
const IngestExternalFileOptions& ingestion_options_;
const IngestExternalFileOptions ingestion_options_;
std::optional<KeyRangeInfo> atomic_replace_range_;
Directories* directories_;
EventLogger* event_logger_;
+733 -6
View File
@@ -5,6 +5,7 @@
#include <table/block_based/block_based_table_factory.h>
#include <atomic>
#include <functional>
#include <memory>
#include <sstream>
@@ -17,6 +18,7 @@
#include "port/stack_trace.h"
#include "rocksdb/sst_file_reader.h"
#include "rocksdb/sst_file_writer.h"
#include "table/prepared_file_info.h"
#include "test_util/testutil.h"
#include "util/random.h"
#include "util/thread_guard.h"
@@ -92,10 +94,17 @@ class ExternSSTFileLinkFailFallbackTest
class ExternalSSTFileTest
: public ExternalSSTFileTestBase,
public ::testing::WithParamInterface<std::tuple<bool, bool>> {
public ::testing::WithParamInterface<std::tuple<bool, bool, bool>> {
public:
ExternalSSTFileTest() = default;
void SetUp() override {
const auto* info = ::testing::UnitTest::GetInstance()->current_test_info();
if (info != nullptr && info->value_param() != nullptr) {
two_phase_ingest_ = std::get<2>(GetParam());
}
}
Status GenerateOneExternalFile(
const Options& options, ColumnFamilyHandle* cfh,
std::vector<std::pair<std::string, std::string>>& data, int file_id,
@@ -152,7 +161,8 @@ class ExternalSSTFileTest
bool verify_checksums_before_ingest = true, bool ingest_behind = false,
bool sort_data = false,
std::map<std::string, std::string>* true_data = nullptr,
ColumnFamilyHandle* cfh = nullptr, bool fill_cache = false) {
ColumnFamilyHandle* cfh = nullptr, bool fill_cache = false,
bool ingest_with_file_info = false) {
// Generate a file id if not provided
if (file_id == -1) {
file_id = last_file_id_ + 1;
@@ -188,7 +198,12 @@ class ExternalSSTFileTest
return s;
}
}
s = sst_file_writer.Finish();
ExternalSstFileInfo file_info;
if (ingest_with_file_info) {
s = sst_file_writer.Finish(&file_info);
} else {
s = sst_file_writer.Finish();
}
if (s.ok()) {
IngestExternalFileOptions ifo;
@@ -197,7 +212,21 @@ class ExternalSSTFileTest
ifo.verify_checksums_before_ingest = verify_checksums_before_ingest;
ifo.ingest_behind = ingest_behind;
ifo.fill_cache = fill_cache;
if (cfh) {
if (two_phase_ingest_) {
std::unique_ptr<FileIngestionHandle> handle;
s = db_->PrepareFileIngestion(cfh ? cfh : db_->DefaultColumnFamily(),
{file_path}, ifo, &handle);
if (s.ok()) {
s = db_->CommitFileIngestionHandle(std::move(handle));
}
} else if (ingest_with_file_info) {
IngestExternalFileArg arg;
arg.column_family = cfh ? cfh : db_->DefaultColumnFamily();
arg.external_files = {file_path};
arg.file_infos = {file_info.prepared_file_info.get()};
arg.options = ifo;
s = db_->IngestExternalFiles({arg});
} else if (cfh) {
s = db_->IngestExternalFile(cfh, {file_path}, ifo);
} else {
s = db_->IngestExternalFile({file_path}, ifo);
@@ -243,6 +272,14 @@ class ExternalSSTFileTest
args[i].external_files.push_back(external_file_path);
args[i].options = ifos[i];
}
if (two_phase_ingest_) {
std::unique_ptr<FileIngestionHandle> handle;
Status s = db_->PrepareFileIngestion(args, &handle);
if (!s.ok()) {
return s;
}
return db_->CommitFileIngestionHandle(std::move(handle));
}
return db_->IngestExternalFiles(args);
}
@@ -294,10 +331,619 @@ class ExternalSSTFileTest
return db_->IngestExternalFile(files, opts);
}
// Writes `data` (sorted by user key) into a new external SST and returns its
// path in *file_path WITHOUT ingesting it, so a test can drive the two-phase
// PrepareFileIngestion()/CommitFileIngestion() API directly.
Status GenerateExternalFileOnly(
const Options& options,
std::vector<std::pair<std::string, std::string>> data,
std::string* file_path) {
std::sort(data.begin(), data.end(),
[&](const std::pair<std::string, std::string>& e1,
const std::pair<std::string, std::string>& e2) {
return options.comparator->Compare(e1.first, e2.first) < 0;
});
*file_path = sst_files_dir_ + "only_" + std::to_string(++last_file_id_);
SstFileWriter sst_file_writer(EnvOptions(), options);
Status s = sst_file_writer.Open(*file_path);
if (!s.ok()) {
return s;
}
for (const auto& entry : data) {
s = sst_file_writer.Put(entry.first, entry.second);
if (!s.ok()) {
sst_file_writer.Finish().PermitUncheckedError();
return s;
}
}
return sst_file_writer.Finish();
}
protected:
int last_file_id_ = 0;
bool two_phase_ingest_ = false;
};
TEST_F(ExternalSSTFileTest, IngestionTimingHistogram) {
Options options = CurrentOptions();
options.statistics = CreateDBStatistics();
options.statistics->set_stats_level(StatsLevel::kAll); // enable timers
DestroyAndReopen(options);
ASSERT_OK(GenerateAndAddExternalFile(
options, {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}, /*file_id=*/-1,
/*allow_global_seqno=*/true, /*write_global_seqno=*/false,
/*verify_checksums_before_ingest=*/true, /*ingest_behind=*/false,
/*sort_data=*/true));
auto hist = [&](Histograms h) {
HistogramData hd;
options.statistics->histogramData(h, &hd);
return hd;
};
// One sample per call (not per CF) in each phase histogram.
HistogramData prepare_hd = hist(INGEST_EXTERNAL_FILE_PREPARE_TIME);
HistogramData run_hd = hist(INGEST_EXTERNAL_FILE_RUN_TIME);
ASSERT_EQ(1, prepare_hd.count);
ASSERT_EQ(1, run_hd.count);
ASSERT_GT(prepare_hd.max, 0.0);
ASSERT_GT(run_hd.max, 0.0);
// A second call adds exactly one more sample to each histogram.
ASSERT_OK(GenerateAndAddExternalFile(
options, {{"k4", "v4"}, {"k5", "v5"}}, /*file_id=*/-1,
/*allow_global_seqno=*/true, /*write_global_seqno=*/false,
/*verify_checksums_before_ingest=*/true, /*ingest_behind=*/false,
/*sort_data=*/true));
ASSERT_EQ(2, hist(INGEST_EXTERNAL_FILE_PREPARE_TIME).count);
ASSERT_EQ(2, hist(INGEST_EXTERNAL_FILE_RUN_TIME).count);
// A failed ingestion records nothing (latency is logged only on success).
ASSERT_NOK(db_->IngestExternalFile({"/path/does/not/exist.sst"},
IngestExternalFileOptions()));
ASSERT_EQ(2, hist(INGEST_EXTERNAL_FILE_PREPARE_TIME).count);
ASSERT_EQ(2, hist(INGEST_EXTERNAL_FILE_RUN_TIME).count);
}
TEST_F(ExternalSSTFileTest, PrepareThenCommit) {
Options options = CurrentOptions();
DestroyAndReopen(options);
std::string file_path;
ASSERT_OK(GenerateExternalFileOnly(
options, {{"k1", "v1"}, {"k2", "v2"}, {"k3", "v3"}}, &file_path));
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {file_path};
std::unique_ptr<FileIngestionHandle> handle;
ASSERT_OK(db_->PrepareFileIngestion({arg}, &handle));
ASSERT_TRUE(handle != nullptr);
// The prepared file is staged in the DB but not yet visible.
ASSERT_EQ("NOT_FOUND", Get("k1"));
ASSERT_OK(db_->CommitFileIngestionHandle(std::move(handle)));
ASSERT_EQ("v1", Get("k1"));
ASSERT_EQ("v2", Get("k2"));
ASSERT_EQ("v3", Get("k3"));
}
TEST_F(ExternalSSTFileTest, ParallelFileOpenWithFileOpeningThreads) {
// Ingest many non-overlapping files with file_opening_threads > 1 so commit
// opens table readers with multiple threads. max_open_files == -1 makes
// commit open every new file.
Options options = CurrentOptions();
options.max_open_files = -1;
DestroyAndReopen(options);
constexpr int kNumFiles = 8;
constexpr int kKeysPerFile = 50;
std::vector<std::string> files;
std::map<std::string, std::string> true_data;
for (int f = 0; f < kNumFiles; f++) {
std::vector<std::pair<std::string, std::string>> data;
for (int k = 0; k < kKeysPerFile; k++) {
const int key = f * kKeysPerFile + k;
const std::string value = "val" + std::to_string(key);
data.emplace_back(Key(key), value);
true_data[Key(key)] = value;
}
std::string file_path;
ASSERT_OK(GenerateExternalFileOnly(options, data, &file_path));
files.push_back(file_path);
}
IngestExternalFileOptions ifo;
ifo.file_opening_threads = 4;
ASSERT_OK(db_->IngestExternalFile(files, ifo));
for (const auto& [key, value] : true_data) {
ASSERT_EQ(value, Get(key));
}
}
TEST_F(ExternalSSTFileTest, LmaxPrefetchSkipDoesNotDisableL0Prefetch) {
LRUCacheOptions co;
co.capacity = 32 << 20;
std::shared_ptr<Cache> cache = NewLRUCache(co);
BlockBasedTableOptions table_options;
table_options.block_cache = cache;
table_options.cache_index_and_filter_blocks = true;
table_options.filter_policy.reset(NewBloomFilterPolicy(10));
Options options = CurrentOptions();
options.disable_auto_compactions = true;
options.max_open_files = -1;
options.num_levels = 2;
options.optimize_filters_for_hits = false;
options.statistics = CreateDBStatistics();
options.table_factory.reset(NewBlockBasedTableFactory(table_options));
DestroyAndReopen(options);
const auto write_file = [&](const std::string& file_path,
const std::string& value,
ExternalSstFileInfo* file_info) {
SstFileWriter writer(EnvOptions(), options);
ASSERT_OK(writer.Open(file_path));
ASSERT_OK(writer.Put(Key(10), value));
ASSERT_OK(writer.Finish(file_info));
};
const auto ingest_file = [&](const std::string& file_path,
const ExternalSstFileInfo& file_info,
bool fail_if_not_bottommost_level) {
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {file_path};
arg.file_infos = {file_info.prepared_file_info.get()};
arg.options.fail_if_not_bottommost_level = fail_if_not_bottommost_level;
arg.options.verify_checksums_before_ingest = false;
arg.options.prefetch_lmax_index_and_filter_blocks = false;
ASSERT_OK(db_->IngestExternalFiles({arg}));
};
const std::string lmax_file_path = sst_files_dir_ + "lazy_lmax.sst";
ExternalSstFileInfo lmax_file_info;
write_file(lmax_file_path, "v10", &lmax_file_info);
ASSERT_EQ(0, options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_INDEX_ADD));
ASSERT_EQ(0, options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_FILTER_ADD));
ingest_file(lmax_file_path, lmax_file_info,
true /* fail_if_not_bottommost_level */);
ASSERT_EQ("0,1", FilesPerLevel());
ASSERT_EQ(0, options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_INDEX_ADD));
ASSERT_EQ(0, options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_FILTER_ADD));
const std::string l0_file_path = sst_files_dir_ + "l0.sst";
ExternalSstFileInfo l0_file_info;
write_file(l0_file_path, "v11", &l0_file_info);
ASSERT_EQ(0, options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_INDEX_ADD));
ASSERT_EQ(0, options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_FILTER_ADD));
ingest_file(l0_file_path, l0_file_info,
false /* fail_if_not_bottommost_level */);
ASSERT_EQ("1,1", FilesPerLevel());
EXPECT_GT(options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_INDEX_ADD),
0);
EXPECT_GT(options.statistics->getAndResetTickerCount(
Tickers::BLOCK_CACHE_FILTER_ADD),
0);
}
TEST_F(ExternalSSTFileTest, AbortPreparedIngestion) {
Options options = CurrentOptions();
DestroyAndReopen(options);
std::string file_path;
ASSERT_OK(GenerateExternalFileOnly(options, {{"x", "1"}}, &file_path));
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {file_path};
std::unique_ptr<FileIngestionHandle> handle;
ASSERT_OK(db_->PrepareFileIngestion({arg}, &handle));
ASSERT_OK(handle->Abort());
ASSERT_EQ("NOT_FOUND", Get("x"));
// Committing an already-aborted handle returns an error.
ASSERT_TRUE(
db_->CommitFileIngestionHandle(std::move(handle)).IsInvalidArgument());
// The DB remains healthy after an abort: a fresh prepare + commit works.
std::unique_ptr<FileIngestionHandle> handle2;
ASSERT_OK(db_->PrepareFileIngestion({arg}, &handle2));
ASSERT_OK(db_->CommitFileIngestionHandle(std::move(handle2)));
ASSERT_EQ("1", Get("x"));
}
TEST_F(ExternalSSTFileTest, MultiHandleAtomicCommit) {
Options options = CurrentOptions();
CreateAndReopenWithCF({"cf1", "cf2", "cf3"}, options);
// Two handles whose column families overlap on cf2:
// handle 1 -> cf1, cf2 handle 2 -> cf2, cf3
// Committing them together installs cf1 and cf3 each from one handle, while
// cf2 receives files from BOTH handles, merged into a single per-CF commit.
std::string f_cf1;
std::string f_cf2_h1;
std::string f_cf2_h2;
std::string f_cf3;
ASSERT_OK(GenerateExternalFileOnly(options, {{"a", "1"}}, &f_cf1));
// The cf2 files share key "b": handle 2 is committed later, so its value must
// win via a higher assigned sequence number.
ASSERT_OK(GenerateExternalFileOnly(options, {{"b", "h1"}, {"b1", "10"}},
&f_cf2_h1));
ASSERT_OK(GenerateExternalFileOnly(options, {{"b", "h2"}, {"b2", "20"}},
&f_cf2_h2));
ASSERT_OK(GenerateExternalFileOnly(options, {{"c", "3"}}, &f_cf3));
std::vector<IngestExternalFileArg> args1(2);
args1[0].column_family = handles_[1]; // cf1
args1[0].external_files = {f_cf1};
args1[1].column_family = handles_[2]; // cf2
args1[1].external_files = {f_cf2_h1};
std::vector<IngestExternalFileArg> args2(2);
args2[0].column_family =
handles_[2]; // cf2 -> merges with handle 1's cf2 job
args2[0].external_files = {f_cf2_h2};
args2[1].column_family = handles_[3]; // cf3
args2[1].external_files = {f_cf3};
std::unique_ptr<FileIngestionHandle> h1;
std::unique_ptr<FileIngestionHandle> h2;
ASSERT_OK(db_->PrepareFileIngestion(args1, &h1));
ASSERT_OK(db_->PrepareFileIngestion(args2, &h2));
ASSERT_EQ("NOT_FOUND", Get(1, "a"));
ASSERT_EQ("NOT_FOUND", Get(2, "b1"));
ASSERT_EQ("NOT_FOUND", Get(3, "c"));
// Commit both handles atomically.
std::vector<std::unique_ptr<FileIngestionHandle>> batch;
batch.push_back(std::move(h1));
batch.push_back(std::move(h2));
ASSERT_OK(db_->CommitFileIngestionHandles(std::move(batch)));
ASSERT_EQ("1", Get(1, "a"));
ASSERT_EQ("h2", Get(2, "b")); // overlapping key: later handle (h2) wins
ASSERT_EQ("10", Get(2, "b1")); // cf2 from handle 1
ASSERT_EQ("20", Get(2, "b2")); // cf2 from handle 2 (merged)
ASSERT_EQ("3", Get(3, "c"));
}
TEST_F(ExternalSSTFileTest, MultiHandleSameColumnFamilyIncompatibleOptions) {
Options options = CurrentOptions();
DestroyAndReopen(options);
std::string f1;
std::string f2;
ASSERT_OK(GenerateExternalFileOnly(options, {{"a", "1"}}, &f1));
ASSERT_OK(GenerateExternalFileOnly(options, {{"b", "2"}}, &f2));
// Same column family but with options the merged Run() would consult set
// differently across the two handles.
IngestExternalFileOptions opts_fill;
IngestExternalFileOptions opts_no_fill;
opts_no_fill.fill_cache = false;
std::unique_ptr<FileIngestionHandle> h1;
std::unique_ptr<FileIngestionHandle> h2;
ASSERT_OK(db_->PrepareFileIngestion(db_->DefaultColumnFamily(), {f1},
opts_fill, &h1));
ASSERT_OK(db_->PrepareFileIngestion(db_->DefaultColumnFamily(), {f2},
opts_no_fill, &h2));
std::vector<std::unique_ptr<FileIngestionHandle>> batch;
batch.push_back(std::move(h1));
batch.push_back(std::move(h2));
ASSERT_TRUE(
db_->CommitFileIngestionHandles(std::move(batch)).IsInvalidArgument());
// The rejected commit rolled both handles back; the CF is still usable.
ASSERT_EQ("NOT_FOUND", Get("a"));
ASSERT_EQ("NOT_FOUND", Get("b"));
std::unique_ptr<FileIngestionHandle> h3;
ASSERT_OK(db_->PrepareFileIngestion(db_->DefaultColumnFamily(), {f1},
IngestExternalFileOptions(), &h3));
ASSERT_OK(db_->CommitFileIngestionHandle(std::move(h3)));
ASSERT_EQ("1", Get("a"));
}
TEST_F(ExternalSSTFileTest, PrepareFileIngestionValidationErrors) {
Options options = CurrentOptions();
DestroyAndReopen(options);
std::unique_ptr<FileIngestionHandle> handle;
ASSERT_TRUE(
db_->PrepareFileIngestion(std::vector<IngestExternalFileArg>{}, nullptr)
.IsInvalidArgument());
// Empty arg list.
ASSERT_TRUE(
db_->PrepareFileIngestion(std::vector<IngestExternalFileArg>{}, &handle)
.IsInvalidArgument());
ASSERT_TRUE(handle == nullptr);
// move_files and link_files are mutually exclusive.
std::string file_path;
ASSERT_OK(GenerateExternalFileOnly(options, {{"k", "v"}}, &file_path));
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {file_path};
arg.options.move_files = true;
arg.options.link_files = true;
ASSERT_TRUE(db_->PrepareFileIngestion({arg}, &handle).IsInvalidArgument());
ASSERT_TRUE(handle == nullptr);
}
// Ingestion that reuses the SstFileWriter's metadata via
// IngestExternalFileArg::file_infos skips the open-and-scan path and still
// produces correct data, including overlapping data that is reassigned a new
// global sequence number.
TEST_F(ExternalSSTFileTest, IngestWithFileInfo) {
Options options = CurrentOptions();
DestroyAndReopen(options);
std::map<std::string, std::string> true_data;
// Count entries into the open-and-scan path so we can assert it is skipped.
std::atomic<int> read_path_count{0};
SyncPoint::GetInstance()->SetCallBack(
"ExternalSstFileIngestionJob::GetIngestedFileInfo:ReadPath",
[&](void*) { read_path_count.fetch_add(1); });
SyncPoint::GetInstance()->EnableProcessing();
// Reuse the writer's metadata, with the normal ingestion options: a global
// sequence number is assigned (allow_db_generated_files is NOT set) and the
// checksum is verified (the fast-path opens the file only to verify it, which
// is not a metadata scan).
auto ingest_fi = [&](std::vector<std::pair<std::string, std::string>> data) {
return GenerateAndAddExternalFile(
options, std::move(data), /*file_id=*/-1, /*allow_global_seqno=*/true,
/*write_global_seqno=*/false, /*verify_checksums_before_ingest=*/true,
/*ingest_behind=*/false, /*sort_data=*/true, &true_data,
/*cfh=*/nullptr, /*fill_cache=*/false, /*ingest_with_file_info=*/true);
};
// Non-overlapping ingest: data reads back and the file is not scanned.
ASSERT_OK(ingest_fi({{Key(1), "a1"}, {Key(2), "a2"}, {Key(3), "a3"}}));
ASSERT_EQ(read_path_count.load(), 0);
// Overlap existing (flushed) data so a non-zero global seqno is assigned and
// the newer values win -- the normal seqno-reassignment path through
// file_infos.
ASSERT_OK(db_->Put(WriteOptions(), Key(2), "old2"));
true_data[Key(2)] = "old2";
ASSERT_OK(Flush());
ASSERT_OK(ingest_fi({{Key(2), "new2"}, {Key(4), "new4"}}));
ASSERT_EQ(read_path_count.load(), 0);
// Control: ingesting the same way but WITHOUT file_infos does enter the scan.
ASSERT_OK(GenerateAndAddExternalFile(
options, {{Key(8), "c8"}}, /*file_id=*/-1, /*allow_global_seqno=*/true,
/*write_global_seqno=*/false, /*verify_checksums_before_ingest=*/true,
/*ingest_behind=*/false, /*sort_data=*/true, &true_data));
ASSERT_GT(read_path_count.load(), 0);
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
size_t kcnt = 0;
VerifyDBFromMap(true_data, &kcnt, false);
}
// Range deletions are honored on the file_infos fast-path: the writer's
// range-del bounds are carried in the metadata, so a DeleteRange covering
// existing keys still shadows them.
TEST_F(ExternalSSTFileTest, IngestWithFileInfoRangeDeletion) {
Options options = CurrentOptions();
DestroyAndReopen(options);
ASSERT_OK(db_->Put(WriteOptions(), Key(20), "live20"));
ASSERT_OK(db_->Put(WriteOptions(), Key(25), "live25"));
ASSERT_OK(Flush());
const std::string f = sst_files_dir_ + "rangedel.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
ASSERT_OK(w.Put(Key(30), "v30"));
ASSERT_OK(w.DeleteRange(Key(20), Key(26))); // covers keys 20..25
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {f};
arg.file_infos = {file_info.prepared_file_info.get()};
arg.options.allow_global_seqno = true;
ASSERT_OK(db_->IngestExternalFiles({arg}));
ReadOptions ro;
std::string val;
ASSERT_TRUE(db_->Get(ro, Key(20), &val).IsNotFound());
ASSERT_TRUE(db_->Get(ro, Key(25), &val).IsNotFound());
ASSERT_OK(db_->Get(ro, Key(30), &val));
ASSERT_EQ(val, "v30");
}
// verify_checksums_before_ingest is honored on the file_infos fast-path: it
// opens the file just to verify, so corruption is still detected.
TEST_F(ExternalSSTFileTest, IngestWithFileInfoVerifiesChecksum) {
Options options = CurrentOptions();
DestroyAndReopen(options);
const std::string f = sst_files_dir_ + "corrupt.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
for (int k = 0; k < 100; k++) {
ASSERT_OK(w.Put(Key(k), "v" + Key(k)));
}
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
ASSERT_OK(test::CorruptFile(options.env, f, /*offset=*/64,
/*bytes_to_corrupt=*/8));
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {f};
arg.file_infos = {file_info.prepared_file_info.get()};
arg.options.verify_checksums_before_ingest = true;
ASSERT_TRUE(db_->IngestExternalFiles({arg}).IsCorruption());
}
// write_global_seqno is incompatible with file_infos: the file is not opened,
// so the seqno cannot be written back into it.
TEST_F(ExternalSSTFileTest, IngestWithFileInfoRejectsWriteGlobalSeqno) {
Options options = CurrentOptions();
DestroyAndReopen(options);
const std::string f = sst_files_dir_ + "wgs.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
ASSERT_OK(w.Put(Key(1), "v1"));
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {f};
arg.file_infos = {file_info.prepared_file_info.get()};
arg.options.write_global_seqno = true;
ASSERT_TRUE(db_->IngestExternalFiles({arg}).IsInvalidArgument());
}
TEST_F(ExternalSSTFileTest, GetPreparedFileInfoForExternalSstIngestion) {
Options options = CurrentOptions();
DestroyAndReopen(options);
const std::string source_dbname = dbname_ + "_source";
ASSERT_OK(DestroyDB(source_dbname, options));
std::unique_ptr<DB> source_db;
ASSERT_OK(DB::Open(options, source_dbname, &source_db));
ASSERT_OK(source_db->Put(WriteOptions(), Key(10), "v10"));
ASSERT_OK(source_db->Flush(FlushOptions()));
std::vector<LiveFileMetaData> live_meta;
source_db->GetLiveFilesMetaData(&live_meta);
ASSERT_EQ(live_meta.size(), 1);
ASSERT_GT(live_meta[0].largest_seqno, 0);
const std::string file_path =
live_meta[0].directory + "/" + live_meta[0].relative_filename;
std::shared_ptr<const PreparedFileInfo> prepared_file_info;
ASSERT_TRUE(source_db
->GetPreparedFileInfoForExternalSstIngestion(
live_meta[0].relative_filename, &prepared_file_info)
.IsInvalidArgument());
ASSERT_EQ(prepared_file_info, nullptr);
prepared_file_info = std::make_shared<PreparedFileInfo>();
ASSERT_TRUE(source_db
->GetPreparedFileInfoForExternalSstIngestion(
dbname_ + "_wrong/" + live_meta[0].relative_filename,
&prepared_file_info)
.IsInvalidArgument());
ASSERT_EQ(prepared_file_info, nullptr);
ASSERT_OK(source_db->GetPreparedFileInfoForExternalSstIngestion(
file_path, &prepared_file_info));
ASSERT_TRUE(prepared_file_info != nullptr);
ASSERT_EQ(prepared_file_info->file_size, live_meta[0].size);
ASSERT_EQ(prepared_file_info->table_properties.key_largest_seqno,
live_meta[0].largest_seqno);
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {file_path};
arg.file_infos = {prepared_file_info.get()};
arg.options.allow_db_generated_files = true;
arg.options.snapshot_consistency = false;
SyncPoint::GetInstance()->SetCallBack(
"ExternalSstFileIngestionJob::GetIngestedFileInfo:ReadPath",
[](void*) { FAIL() << "Prepared file info should skip the read path"; });
SyncPoint::GetInstance()->EnableProcessing();
ASSERT_OK(db_->IngestExternalFiles({arg}));
ASSERT_EQ(Get(Key(10)), "v10");
ASSERT_OK(source_db->Close());
source_db.reset();
ASSERT_OK(DestroyDB(source_dbname, options));
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
TEST_F(ExternalSSTFileTest,
IngestWithFileInfoRejectsMissingDbGeneratedSeqnoBounds) {
Options options = CurrentOptions();
DestroyAndReopen(options);
const std::string f = sst_files_dir_ + "missing_seqno_bounds.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
ASSERT_OK(w.Put(Key(1), "v1"));
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
auto prepared_file_info = file_info.prepared_file_info;
auto missing_seqno_bounds =
std::make_shared<PreparedFileInfo>(*prepared_file_info);
missing_seqno_bounds->table_properties.key_smallest_seqno = UINT64_MAX;
missing_seqno_bounds->table_properties.key_largest_seqno = UINT64_MAX;
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {f};
arg.file_infos = {missing_seqno_bounds.get()};
arg.options.allow_db_generated_files = true;
arg.options.snapshot_consistency = false;
Status s = db_->IngestExternalFiles({arg});
ASSERT_TRUE(s.IsCorruption());
ASSERT_NE(s.ToString().find("Unknown largest seqno"), std::string::npos);
}
TEST_F(ExternalSSTFileTest, IngestWithFileInfoPreparedKeyBoundTypes) {
Options options = CurrentOptions();
DestroyAndReopen(options);
const std::string f = sst_files_dir_ + "prepared_key_bound_types.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
ASSERT_OK(w.Put(Key(10), "v10"));
ASSERT_OK(w.DeleteRange(Key(20), Key(30)));
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
auto prepared_file_info = file_info.prepared_file_info;
ParsedInternalKey smallest;
ASSERT_OK(ParseInternalKey(prepared_file_info->smallest.Encode(), &smallest,
false /* log_err_key */));
ASSERT_EQ(0, smallest.sequence);
ASSERT_EQ(kTypeValue, smallest.type);
ASSERT_EQ(Key(10), smallest.user_key.ToString());
ParsedInternalKey largest;
ASSERT_OK(ParseInternalKey(prepared_file_info->largest.Encode(), &largest,
false /* log_err_key */));
ASSERT_EQ(kMaxSequenceNumber, largest.sequence);
ASSERT_EQ(kTypeRangeDeletion, largest.type);
ASSERT_EQ(Key(30), largest.user_key.ToString());
}
TEST_F(ExternalSSTFileTest, ComparatorMismatch) {
Options options = CurrentOptions();
Options options_diff_ucmp = options;
@@ -1816,7 +2462,8 @@ TEST_P(ExternalSSTFileTest, IngestFileWithGlobalSeqnoRandomized) {
for (int i = 0; i < 500; i++) {
std::vector<std::pair<std::string, std::string>> random_data;
for (int j = 0; j < 100; j++) {
std::string k = rnd.RandomString(rnd.Next() % 20);
std::string k =
rnd.RandomString(rnd.Next() % 20 + 1); // requires non-empty keys
std::string v = rnd.RandomString(rnd.Next() % 50);
random_data.emplace_back(k, v);
}
@@ -3247,6 +3894,20 @@ class ExternalSSTFileWithTimestampTest : public ExternalSSTFileTest {
return dst;
}
std::string MaxTimestamp() {
Slice ts = MaxU64Ts();
return ts.ToString();
}
Options UDTMemtableOnlyOptions() {
Options options = CurrentOptions();
options.comparator = test::BytewiseComparatorWithU64TsWrapper();
options.persist_user_defined_timestamps = false;
options.allow_concurrent_memtable_write = false;
options.disable_auto_compactions = true;
return options;
}
Status IngestExternalUDTFile(const std::vector<std::string>& files,
bool allow_global_seqno = true) {
IngestExternalFileOptions opts;
@@ -3815,8 +4476,74 @@ TEST_F(ExternalSSTFileWithTimestampTest, TimestampsNotPersistedBasic) {
kRangeDelSkipConfigs));
}
TEST_F(ExternalSSTFileWithTimestampTest,
TimestampsPersistedIngestWithFileInfoRangeDeletionBounds) {
Options options = CurrentOptions();
options.comparator = test::BytewiseComparatorWithU64TsWrapper();
options.persist_user_defined_timestamps = true;
options.disable_auto_compactions = true;
DestroyAndReopen(options);
const std::string ts = EncodeAsUint64(10);
const std::string f = sst_files_dir_ + "udt_persisted_rangedel_bounds.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
ASSERT_OK(w.Put(Key(30), ts, "v30"));
ASSERT_OK(w.DeleteRange(Key(20), Key(26), ts));
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
auto prepared_file_info = file_info.prepared_file_info;
ASSERT_EQ(Key(20) + MaxTimestamp(),
prepared_file_info->smallest.user_key().ToString());
ASSERT_EQ(Key(30) + ts, prepared_file_info->largest.user_key().ToString());
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {f};
arg.file_infos = {file_info.prepared_file_info.get()};
ASSERT_OK(db_->IngestExternalFiles({arg}));
std::vector<LiveFileMetaData> metadata;
db_->GetLiveFilesMetaData(&metadata);
ASSERT_EQ(1, metadata.size());
ASSERT_EQ(Key(20) + MaxTimestamp(), metadata[0].smallestkey);
ASSERT_EQ(Key(30) + ts, metadata[0].largestkey);
}
TEST_F(ExternalSSTFileWithTimestampTest,
TimestampsNotPersistedIngestWithFileInfoRangeDeletionBounds) {
Options options = UDTMemtableOnlyOptions();
DestroyAndReopen(options);
const std::string f = sst_files_dir_ + "udt_rangedel_bounds.sst";
SstFileWriter w(EnvOptions(), options);
ASSERT_OK(w.Open(f));
ASSERT_OK(w.Put(Key(30), EncodeAsUint64(0), "v30"));
ASSERT_OK(w.DeleteRange(Key(20), Key(26), EncodeAsUint64(0)));
ExternalSstFileInfo file_info;
ASSERT_OK(w.Finish(&file_info));
auto prepared_file_info = file_info.prepared_file_info;
ASSERT_EQ(Key(20), prepared_file_info->smallest.user_key().ToString());
ASSERT_EQ(Key(30), prepared_file_info->largest.user_key().ToString());
IngestExternalFileArg arg;
arg.column_family = db_->DefaultColumnFamily();
arg.external_files = {f};
arg.file_infos = {file_info.prepared_file_info.get()};
ASSERT_OK(db_->IngestExternalFiles({arg}));
std::vector<LiveFileMetaData> metadata;
db_->GetLiveFilesMetaData(&metadata);
ASSERT_EQ(1, metadata.size());
ASSERT_EQ(Key(20) + MaxTimestamp(), metadata[0].smallestkey);
ASSERT_EQ(Key(30) + EncodeAsUint64(0), metadata[0].largestkey);
}
INSTANTIATE_TEST_CASE_P(ExternalSSTFileTest, ExternalSSTFileTest,
testing::Combine(testing::Bool(), testing::Bool()));
testing::Combine(testing::Bool(), testing::Bool(),
testing::Bool()));
class IngestDBGeneratedFileTest
: public ExternalSSTFileTestBase,
+72
View File
@@ -646,6 +646,78 @@ TEST(FaultInjectionFSTest, ReadUnsyncedData) {
ASSERT_EQ(0, sl.compare(Slice(data.data() + first_read_len, sl.size())));
}
}
TEST(FaultInjectionFSTest, FileOpenContractRejectsReadersWhileOpenForWrite) {
std::shared_ptr<FaultInjectionTestFS> fault_fs =
std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
const std::string fname = test::PerThreadDBPath(
"file_open_contract_no_readers_while_open_for_write");
FileOptions writer_options;
writer_options.open_contract = FileOpenContract::kNoReadersWhileOpenForWrite;
std::unique_ptr<FSWritableFile> writer;
ASSERT_OK(fault_fs->NewWritableFile(fname, writer_options, &writer, nullptr));
std::unique_ptr<FSRandomAccessFile> reader;
IOStatus s =
fault_fs->NewRandomAccessFile(fname, FileOptions(), &reader, nullptr);
ASSERT_TRUE(s.IsNotSupported());
ASSERT_OK(writer->Close(IOOptions(), nullptr));
ASSERT_OK(
fault_fs->NewRandomAccessFile(fname, FileOptions(), &reader, nullptr));
}
TEST(FaultInjectionFSTest,
FileOpenContractAllowsSyncFileButRejectsReopenedWrite) {
std::shared_ptr<FaultInjectionTestFS> fault_fs =
std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
const std::string fname =
test::PerThreadDBPath("file_open_contract_no_reopen_for_write");
FileOptions writer_options;
writer_options.open_contract = FileOpenContract::kNoReopenForWrite;
std::unique_ptr<FSWritableFile> writer;
ASSERT_OK(fault_fs->NewWritableFile(fname, writer_options, &writer, nullptr));
ASSERT_OK(writer->Append("abc", IOOptions(), nullptr));
ASSERT_OK(writer->Close(IOOptions(), nullptr));
ASSERT_OK(fault_fs->SyncFile(fname, FileOptions(), IOOptions(),
/*use_fsync=*/false, nullptr));
std::unique_ptr<FSWritableFile> reopened_writer;
ASSERT_OK(fault_fs->ReopenWritableFile(fname, FileOptions(), &reopened_writer,
nullptr));
IOStatus s = reopened_writer->Append("d", IOOptions(), nullptr);
ASSERT_TRUE(s.IsNotSupported());
ASSERT_OK(reopened_writer->Close(IOOptions(), nullptr));
}
TEST(FaultInjectionFSTest, FileOpenContractAllowsCreateAfterDelete) {
std::shared_ptr<FaultInjectionTestFS> fault_fs =
std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
const std::string fname =
test::PerThreadDBPath("file_open_contract_recreate_after_delete");
FileOptions writer_options;
writer_options.open_contract = FileOpenContract::kNoReopenForWrite;
std::unique_ptr<FSWritableFile> writer;
ASSERT_OK(fault_fs->NewWritableFile(fname, writer_options, &writer, nullptr));
ASSERT_OK(writer->Append("abc", IOOptions(), nullptr));
ASSERT_OK(writer->Close(IOOptions(), nullptr));
ASSERT_OK(FileSystem::Default()->DeleteFile(fname, IOOptions(), nullptr));
std::unique_ptr<FSWritableFile> recreated_writer;
ASSERT_OK(fault_fs->NewWritableFile(fname, writer_options, &recreated_writer,
nullptr));
ASSERT_OK(recreated_writer->Append("def", IOOptions(), nullptr));
ASSERT_OK(recreated_writer->Close(IOOptions(), nullptr));
ASSERT_OK(fault_fs->DeleteFile(fname, IOOptions(), nullptr));
}
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {
+190 -1
View File
@@ -31,6 +31,7 @@
#include "util/mutexlock.h"
#include "util/rate_limiter_impl.h"
#include "util/string_util.h"
#include "utilities/fault_injection_fs.h"
#include "utilities/merge_operators.h"
namespace ROCKSDB_NAMESPACE {
@@ -201,6 +202,194 @@ TEST_F(EventListenerTest, OnSingleDBCompactionTest) {
}
}
// Listener that asserts OnCompactionPreCommit fires strictly between
// OnCompactionBegin and OnCompactionCompleted, and that input files'
// being_compacted flag is still true at that point.
class TestCompactionPreCommitListener : public EventListener {
public:
explicit TestCompactionPreCommitListener(EventListenerTest* test)
: test_(test) {}
void OnCompactionBegin(DB* /*db*/, const CompactionJobInfo& ci) override {
std::lock_guard<std::mutex> lock(mutex_);
++begin_count_;
last_begin_job_id_ = ci.job_id;
EXPECT_EQ(begin_count_, pre_commit_count_ + 1);
EXPECT_EQ(begin_count_, completed_count_ + 1);
}
void OnCompactionPreCommit(DB* db, const CompactionJobInfo& ci) override {
std::lock_guard<std::mutex> lock(mutex_);
++pre_commit_count_;
// Must fire after Begin and before Completed for this compaction.
EXPECT_EQ(pre_commit_count_, begin_count_);
EXPECT_EQ(pre_commit_count_, completed_count_ + 1);
EXPECT_EQ(ci.job_id, last_begin_job_id_);
EXPECT_GT(ci.input_files.size(), 0U);
// Verify input files are still marked being_compacted.
std::vector<std::vector<FileMetaData>> files_by_level;
test_->dbfull()->TEST_GetFilesMetaData(test_->handles_[ci.cf_id],
&files_by_level);
EXPECT_EQ(test_->db_.get(), db);
for (const auto& info : ci.input_file_infos) {
bool found = false;
for (const auto& level_files : files_by_level) {
for (const auto& meta : level_files) {
if (meta.fd.GetNumber() == info.file_number) {
found = true;
EXPECT_TRUE(meta.being_compacted)
<< "input file " << info.file_number
<< " should still be being_compacted in "
"OnCompactionPreCommit";
}
}
}
EXPECT_TRUE(found) << "input file " << info.file_number
<< " not found in DB";
}
}
void OnCompactionCompleted(DB* /*db*/, const CompactionJobInfo& ci) override {
std::lock_guard<std::mutex> lock(mutex_);
++completed_count_;
// Must fire after the matching PreCommit.
EXPECT_EQ(completed_count_, pre_commit_count_);
EXPECT_EQ(completed_count_, begin_count_);
EXPECT_EQ(ci.job_id, last_begin_job_id_);
}
size_t BeginCount() {
std::lock_guard<std::mutex> lock(mutex_);
return begin_count_;
}
size_t PreCommitCount() {
std::lock_guard<std::mutex> lock(mutex_);
return pre_commit_count_;
}
size_t CompletedCount() {
std::lock_guard<std::mutex> lock(mutex_);
return completed_count_;
}
private:
EventListenerTest* test_;
std::mutex mutex_;
size_t begin_count_ = 0;
size_t pre_commit_count_ = 0;
size_t completed_count_ = 0;
int last_begin_job_id_ = -1;
};
TEST_F(EventListenerTest, OnCompactionPreCommitOrdering) {
Options options = CurrentOptions();
options.create_if_missing = true;
options.compaction_style = kCompactionStyleLevel;
options.compression = kNoCompression;
options.level0_file_num_compaction_trigger = 4;
auto* listener = new TestCompactionPreCommitListener(this);
options.listeners.emplace_back(listener);
// Verify sync-point ordering: Begin -> PreCommit -> Completed.
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
{{"DBImpl::NotifyOnCompactionBegin::UnlockMutex",
"DBImpl::NotifyOnCompactionPreCommit::UnlockMutex"},
{"DBImpl::NotifyOnCompactionPreCommit::UnlockMutex",
"DBImpl::NotifyOnCompactionCompleted::UnlockMutex"}});
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
CreateAndReopenWithCF({"pikachu"}, options);
Random rnd(301);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) {
ASSERT_OK(Put(1, rnd.RandomString(10), rnd.RandomString(10)));
}
ASSERT_OK(Flush(1));
}
ASSERT_OK(dbfull()->TEST_WaitForCompact());
EXPECT_GT(listener->BeginCount(), 0U);
EXPECT_EQ(listener->PreCommitCount(), listener->BeginCount());
EXPECT_EQ(listener->CompletedCount(), listener->BeginCount());
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
}
class TestDBShutdownBeginListener : public EventListener {
public:
void OnDBShutdownBegin(DB* db) override {
std::lock_guard<std::mutex> lock(mutex_);
++shutdown_count_;
last_db_ = db;
}
int ShutdownCount() const {
std::lock_guard<std::mutex> lock(mutex_);
return shutdown_count_;
}
DB* LastDB() const {
std::lock_guard<std::mutex> lock(mutex_);
return last_db_;
}
private:
mutable std::mutex mutex_;
int shutdown_count_ = 0;
DB* last_db_ = nullptr;
};
TEST_F(EventListenerTest, OnDBShutdownBeginOnceForCancelAndClose) {
Options options = CurrentOptions();
options.create_if_missing = true;
auto listener = std::make_shared<TestDBShutdownBeginListener>();
options.listeners.emplace_back(listener);
DestroyAndReopen(options);
DB* db = db_.get();
dbfull()->CancelAllBackgroundWork(false);
EXPECT_EQ(1, listener->ShutdownCount());
EXPECT_EQ(db, listener->LastDB());
Close();
EXPECT_EQ(1, listener->ShutdownCount());
}
TEST_F(EventListenerTest, OnDBShutdownBeginOnFailedOpen) {
Close();
std::shared_ptr<FaultInjectionTestFS> fs(
new FaultInjectionTestFS(env_->GetFileSystem()));
std::unique_ptr<Env> env(NewCompositeEnv(fs));
Options options = CurrentOptions();
options.env = env.get();
options.create_if_missing = true;
auto listener = std::make_shared<TestDBShutdownBeginListener>();
options.listeners.emplace_back(listener);
SyncPoint::GetInstance()->SetCallBack(
"PersistRocksDBOptions:create",
[&](void* /*arg*/) { fs->SetFilesystemActive(false); });
SyncPoint::GetInstance()->SetCallBack(
"PersistRocksDBOptions:written",
[&](void* /*arg*/) { fs->SetFilesystemActive(true); });
SyncPoint::GetInstance()->EnableProcessing();
std::unique_ptr<DB> db;
Status s = DB::Open(options, dbname_, &db);
ASSERT_TRUE(s.IsIOError()) << s.ToString();
EXPECT_EQ(1, listener->ShutdownCount());
EXPECT_NE(nullptr, listener->LastDB());
EXPECT_EQ(nullptr, db.get());
fs->SetFilesystemActive(true);
SyncPoint::GetInstance()->DisableProcessing();
SyncPoint::GetInstance()->ClearAllCallBacks();
}
// This simple Listener can only handle one flush at a time.
class TestFlushListener : public EventListener {
public:
@@ -1754,7 +1943,7 @@ TEST_F(EventListenerTest, BackgroundJobPressure) {
listener->Reset();
sleeping_task.WakeUp();
sleeping_task.WaitUntilDone();
ASSERT_OK(dbfull()->TEST_WaitForCompact());
ASSERT_OK(dbfull()->TEST_WaitForBackgroundWork());
snapshots = listener->GetSnapshots();
CheckInvariants(snapshots);
+6 -6
View File
@@ -159,8 +159,8 @@ ImmutableMemTableOptions::ImmutableMemTableOptions(
mutable_cf_options.memtable_protection_bytes_per_key),
allow_data_in_errors(ioptions.allow_data_in_errors),
paranoid_memory_checks(mutable_cf_options.paranoid_memory_checks),
memtable_veirfy_per_key_checksum_on_seek(
mutable_cf_options.memtable_veirfy_per_key_checksum_on_seek),
memtable_verify_per_key_checksum_on_seek(
mutable_cf_options.memtable_verify_per_key_checksum_on_seek),
memtable_batch_lookup_optimization(
ioptions.memtable_batch_lookup_optimization) {}
@@ -256,7 +256,7 @@ MemTable::MemTable(const InternalKeyComparator& cmp,
mutable_cf_options.memtable_max_range_deletions),
key_validation_callback_(
(moptions_.protection_bytes_per_key != 0 &&
moptions_.memtable_veirfy_per_key_checksum_on_seek)
moptions_.memtable_verify_per_key_checksum_on_seek)
? std::bind(&MemTable::ValidateKey, this, std::placeholders::_1,
std::placeholders::_2)
: std::function<Status(const char*, bool)>(nullptr)) {
@@ -528,7 +528,7 @@ class MemTableIterator : public InternalIterator {
paranoid_memory_checks_(mem.moptions_.paranoid_memory_checks),
validate_on_seek_(
mem.moptions_.paranoid_memory_checks ||
mem.moptions_.memtable_veirfy_per_key_checksum_on_seek),
mem.moptions_.memtable_verify_per_key_checksum_on_seek),
allow_data_in_error_(mem.moptions_.allow_data_in_errors),
key_validation_callback_(mem.key_validation_callback_) {
if (kind == kRangeDelEntries) {
@@ -1682,7 +1682,7 @@ void MemTable::GetFromTable(const LookupKey& key,
saver.protection_bytes_per_key = moptions_.protection_bytes_per_key;
if (!moptions_.paranoid_memory_checks &&
!moptions_.memtable_veirfy_per_key_checksum_on_seek) {
!moptions_.memtable_verify_per_key_checksum_on_seek) {
table_->Get(key, &saver, SaveValue);
} else {
Status check_s = table_->GetAndValidate(
@@ -1750,7 +1750,7 @@ void MemTable::MultiGet(const ReadOptions& read_options, MultiGetRange* range,
// Use batch lookup optimization when enabled
bool use_batch_optimization = moptions_.memtable_batch_lookup_optimization;
bool validate = moptions_.paranoid_memory_checks ||
moptions_.memtable_veirfy_per_key_checksum_on_seek;
moptions_.memtable_verify_per_key_checksum_on_seek;
if (use_batch_optimization) {
// Phase 1: Handle range tombstones and set up Savers for batched lookup
+1 -1
View File
@@ -68,7 +68,7 @@ struct ImmutableMemTableOptions {
uint32_t protection_bytes_per_key;
bool allow_data_in_errors;
bool paranoid_memory_checks;
bool memtable_veirfy_per_key_checksum_on_seek;
bool memtable_verify_per_key_checksum_on_seek;
bool memtable_batch_lookup_optimization;
};
+98 -16
View File
@@ -17,6 +17,7 @@
#include "db/version_set.h"
#include "logging/log_buffer.h"
#include "logging/logging.h"
#include "memory/arena.h"
#include "monitoring/thread_status_util.h"
#include "rocksdb/db.h"
#include "rocksdb/env.h"
@@ -31,6 +32,78 @@ class InternalKeyComparator;
class Mutex;
class VersionSet;
bool MultiScanOverlapsUserKeyRange(const MultiScanArgs* scan_opts,
const Comparator* user_comparator,
const Slice& smallest_user_key,
const Slice& largest_user_key) {
if (scan_opts == nullptr || !scan_opts->HasBoundedScanRanges()) {
return true;
}
for (const ScanOptions& scan_range : scan_opts->GetScanRanges()) {
if (user_comparator->CompareWithoutTimestamp(
scan_range.range.limit.value(), /*a_has_ts=*/false,
smallest_user_key, /*b_has_ts=*/true) <= 0) {
continue;
}
if (user_comparator->CompareWithoutTimestamp(
scan_range.range.start.value(), /*a_has_ts=*/false,
largest_user_key, /*b_has_ts=*/true) > 0) {
continue;
}
return true;
}
return false;
}
bool MultiScanIteratorOverlapsUserKeyRange(InternalIterator* iter,
const MultiScanArgs* scan_opts,
const Comparator* user_comparator) {
iter->SeekToFirst();
if (!iter->status().ok()) {
return true;
}
if (!iter->Valid()) {
return true;
}
const std::string smallest_user_key = ExtractUserKey(iter->key()).ToString();
iter->SeekToLast();
if (!iter->status().ok()) {
return true;
}
if (!iter->Valid()) {
return true;
}
const std::string largest_user_key = ExtractUserKey(iter->key()).ToString();
return MultiScanOverlapsUserKeyRange(scan_opts, user_comparator,
smallest_user_key, largest_user_key);
}
bool MultiScanIntersectsMemTable(
ReadOnlyMemTable* memtable, const ReadOptions& read_options,
UnownedPtr<const SeqnoToTimeMapping> seqno_to_time_mapping,
const SliceTransform* prefix_extractor, const MultiScanArgs* scan_opts,
const Comparator* user_comparator) {
if (scan_opts == nullptr || !scan_opts->HasBoundedScanRanges()) {
return true;
}
if (memtable->NumRangeDeletion() > 0) {
return true;
}
Arena arena;
ReadOptions intersect_read_options(read_options);
intersect_read_options.total_order_seek = true;
intersect_read_options.iterate_lower_bound = nullptr;
intersect_read_options.iterate_upper_bound = nullptr;
InternalIterator* iter =
memtable->NewIterator(intersect_read_options, seqno_to_time_mapping,
&arena, prefix_extractor, /*for_flush=*/false);
ScopedArenaPtr<InternalIterator> iter_guard(iter);
return MultiScanIteratorOverlapsUserKeyRange(iter, scan_opts,
user_comparator);
}
void MemTableListVersion::AddMemTable(ReadOnlyMemTable* m) {
if (!memlist_.empty()) {
// ID can be equal for MemPurge
@@ -229,33 +302,42 @@ void MemTableListVersion::AddIterators(
const ReadOptions& options,
UnownedPtr<const SeqnoToTimeMapping> seqno_to_time_mapping,
const SliceTransform* prefix_extractor,
MergeIteratorBuilder* merge_iter_builder, bool add_range_tombstone_iter) {
MergeIteratorBuilder* merge_iter_builder, bool add_range_tombstone_iter,
SequenceNumber read_seq, const MultiScanArgs* scan_opts,
const Comparator* user_comparator) {
for (auto& m : memlist_) {
const bool should_probe_scan_intersection =
scan_opts != nullptr && scan_opts->HasBoundedScanRanges() &&
m->NumRangeDeletion() == 0;
if (should_probe_scan_intersection &&
!MultiScanIntersectsMemTable(m, options, seqno_to_time_mapping,
prefix_extractor, scan_opts,
user_comparator)) {
continue;
}
auto mem_iter =
m->NewIterator(options, seqno_to_time_mapping,
merge_iter_builder->GetArena(), prefix_extractor,
/*for_flush=*/false);
ScopedArenaPtr<InternalIterator> mem_iter_guard(mem_iter);
if (!add_range_tombstone_iter || options.ignore_range_deletions) {
merge_iter_builder->AddIterator(mem_iter);
merge_iter_builder->AddIterator(mem_iter_guard.release());
} else {
// Except for snapshot read, using kMaxSequenceNumber is OK because these
// are immutable memtables.
SequenceNumber read_seq = options.snapshot != nullptr
? options.snapshot->GetSequenceNumber()
: kMaxSequenceNumber;
const SequenceNumber range_del_read_seq =
read_seq != kMaxSequenceNumber || options.snapshot == nullptr
? read_seq
: options.snapshot->GetSequenceNumber();
std::unique_ptr<TruncatedRangeDelIterator> mem_tombstone_iter;
auto range_del_iter = m->NewRangeTombstoneIterator(
options, read_seq, true /* immutale_memtable */);
if (range_del_iter == nullptr || range_del_iter->empty()) {
delete range_del_iter;
} else {
std::unique_ptr<FragmentedRangeTombstoneIterator> range_del_iter(
m->NewRangeTombstoneIterator(options, range_del_read_seq,
true /* immutale_memtable */));
if (range_del_iter != nullptr && !range_del_iter->empty()) {
mem_tombstone_iter = std::make_unique<TruncatedRangeDelIterator>(
std::unique_ptr<FragmentedRangeTombstoneIterator>(range_del_iter),
&m->GetInternalKeyComparator(), nullptr /* smallest */,
nullptr /* largest */);
std::move(range_del_iter), &m->GetInternalKeyComparator(),
nullptr /* smallest */, nullptr /* largest */);
}
merge_iter_builder->AddPointAndTombstoneIterator(
mem_iter, std::move(mem_tombstone_iter));
mem_iter_guard.release(), std::move(mem_tombstone_iter));
}
}
}
+22 -1
View File
@@ -34,6 +34,21 @@ class MemTableList;
struct FlushJobInfo;
// Returns true when at least one bounded scan range overlaps the user-key
// range. Unbounded scan options conservatively overlap.
bool MultiScanOverlapsUserKeyRange(const MultiScanArgs* scan_opts,
const Comparator* user_comparator,
const Slice& smallest_user_key,
const Slice& largest_user_key);
// Returns true when the memtable may contain keys or range deletions relevant
// to the scan ranges. Iterator/status errors conservatively overlap.
bool MultiScanIntersectsMemTable(
ReadOnlyMemTable* memtable, const ReadOptions& read_options,
UnownedPtr<const SeqnoToTimeMapping> seqno_to_time_mapping,
const SliceTransform* prefix_extractor, const MultiScanArgs* scan_opts,
const Comparator* user_comparator);
// keeps a list of immutable memtables (ReadOnlyMemtable*) in a vector.
// The list is immutable if refcount is bigger than one. It is used as
// a state for Get() and iterator code paths.
@@ -122,11 +137,17 @@ class MemTableListVersion {
std::vector<InternalIterator*>* iterator_list,
Arena* arena);
// read_seq controls range tombstone visibility. It may be captured before
// lazy iterator initialization. scan_opts can prune immutable memtables that
// cannot intersect the requested scan ranges.
void AddIterators(const ReadOptions& options,
UnownedPtr<const SeqnoToTimeMapping> seqno_to_time_mapping,
const SliceTransform* prefix_extractor,
MergeIteratorBuilder* merge_iter_builder,
bool add_range_tombstone_iter);
bool add_range_tombstone_iter,
SequenceNumber read_seq = kMaxSequenceNumber,
const MultiScanArgs* scan_opts = nullptr,
const Comparator* user_comparator = nullptr);
uint64_t GetTotalNumEntries() const;
+3
View File
@@ -497,6 +497,9 @@ FragmentedRangeTombstoneIterator::SplitBySnapshot(
splits;
SequenceNumber lower = 0;
SequenceNumber upper;
// Safe to dereference icmp_ below: SplitBySnapshot is construction-time work
// (called synchronously from the range-del aggregator while the reader that
// owns *icmp_ is alive). See the icmp_ note in the header.
for (size_t i = 0; i <= snapshots.size(); i++) {
if (i >= snapshots.size()) {
upper = kMaxSequenceNumber;
+9 -1
View File
@@ -199,7 +199,9 @@ class FragmentedRangeTombstoneIterator : public InternalIterator {
RangeTombstone Tombstone() const {
assert(Valid());
if (icmp_->user_comparator()->timestamp_size()) {
// Use ucmp_ (long-lived; see the icmp_ note below) rather than
// icmp_->user_comparator(). They are equal for all constructors.
if (ucmp_->timestamp_size()) {
return RangeTombstone(start_key(), end_key(), seq(), timestamp());
}
return RangeTombstone(start_key(), end_key(), seq());
@@ -316,6 +318,12 @@ class FragmentedRangeTombstoneIterator : public InternalIterator {
const RangeTombstoneStackStartComparator tombstone_start_cmp_;
const RangeTombstoneStackEndComparator tombstone_end_cmp_;
// icmp_ is a borrowed pointer and is not guaranteed to outlive this iterator:
// it may be built from a comparator with a shorter lifetime than an iterator
// some owner keeps alive. Only dereference it during construction-time work
// (e.g. SplitBySnapshot), when the referent is known alive. Post-construction
// navigation must use ucmp_ (the long-lived user comparator) and tombstones_;
// do not add new icmp_ dereferences in Next()/Seek()/Tombstone().
const InternalKeyComparator* icmp_;
const Comparator* ucmp_;
std::shared_ptr<FragmentedRangeTombstoneList> tombstones_ref_;
+97 -9
View File
@@ -98,7 +98,7 @@ Status TableCache::GetTableReader(
const MutableCFOptions& mutable_cf_options, bool skip_filters, int level,
bool prefetch_index_and_filter_in_cache,
size_t max_file_size_for_l0_meta_pin, Temperature file_temperature,
std::string* file_open_metadata) {
std::string* file_open_metadata, bool avoid_shared_metadata_cache) {
std::string fname = TableFileName(
ioptions_.cf_paths, file_meta.fd.GetNumber(), file_meta.fd.GetPathId());
std::unique_ptr<FSRandomAccessFile> file;
@@ -190,7 +190,8 @@ Status TableCache::GetTableReader(
block_cache_tracer_, max_file_size_for_l0_meta_pin, db_session_id_,
file_meta.fd.GetNumber(), expected_unique_id,
file_meta.fd.largest_seqno, file_meta.tail_size,
file_meta.user_defined_timestamps_persisted),
file_meta.user_defined_timestamps_persisted,
avoid_shared_metadata_cache),
std::move(file_reader), file_meta.fd.GetFileSize(), table_reader,
prefetch_index_and_filter_in_cache);
TEST_SYNC_POINT("TableCache::GetTableReader:0");
@@ -213,11 +214,53 @@ Status TableCache::FindTable(
const bool no_io, HistogramImpl* file_read_hist, bool skip_filters,
int level, bool prefetch_index_and_filter_in_cache,
size_t max_file_size_for_l0_meta_pin, Temperature file_temperature,
bool pin_table_handle, std::string* file_open_metadata) {
bool pin_table_handle, std::string* file_open_metadata,
std::unique_ptr<TableReader>* fresh_table_reader_owner,
const TableCacheOpenOptions& open_options) {
assert(out_table_reader != nullptr && *out_table_reader == nullptr);
assert(handle != nullptr && *handle == nullptr);
// open_ephemeral_table_reader requests a fresh reader;
// fresh_table_reader_owner is where we return it. The two must agree.
assert(open_options.open_ephemeral_table_reader ==
(fresh_table_reader_owner != nullptr));
PERF_TIMER_GUARD_WITH_CLOCK(find_table_nanos, ioptions_.clock);
// Bypass path: open a fresh TableReader, skipping the pinned-reader fast path
// and the shared cache. The caller takes ownership via
// fresh_table_reader_owner. no_io is not allowed here since opening a new
// reader always needs I/O.
if (fresh_table_reader_owner != nullptr) {
assert(!no_io);
if (no_io) {
// Defensive in release builds: the caller violated the contract.
return Status::Incomplete(
"fresh TableReader requested but no_io is set; cannot open file");
}
std::unique_ptr<TableReader> table_reader;
const bool effective_skip_filters =
skip_filters || open_options.skip_filters;
TEST_SYNC_POINT_CALLBACK("TableCache::FindTable:FreshTableReader",
const_cast<TableCacheOpenOptions*>(&open_options));
Status s = GetTableReader(
ro, file_options, internal_comparator, file_meta,
false /* sequential mode */, file_read_hist, &table_reader,
mutable_cf_options, effective_skip_filters, level,
prefetch_index_and_filter_in_cache &&
!open_options.avoid_shared_metadata_cache,
max_file_size_for_l0_meta_pin, file_temperature, file_open_metadata,
open_options.avoid_shared_metadata_cache);
if (!s.ok()) {
assert(table_reader == nullptr);
RecordTick(ioptions_.stats, NO_FILE_ERRORS);
IGNORE_STATUS_IF_ERROR(s);
return s;
}
*out_table_reader = table_reader.get();
*fresh_table_reader_owner = std::move(table_reader);
*handle = nullptr;
return s;
}
// Fast path: if table reader is already pinned, return it directly without a
// cache lookup.
auto pinned_reader = file_meta.fd.pinned_reader.Get();
@@ -313,15 +356,23 @@ InternalIterator* TableCache::NewIterator(
const InternalKey* largest_compaction_key, bool allow_unprepared_value,
const SequenceNumber* read_seqno,
std::unique_ptr<TruncatedRangeDelIterator>* range_del_iter,
bool maybe_pin_table_handle, std::string* file_open_metadata) {
bool maybe_pin_table_handle, std::string* file_open_metadata,
const TableCacheOpenOptions& open_options) {
PERF_TIMER_GUARD(new_table_iterator_nanos);
Status s;
TableReader* table_reader = nullptr;
TypedHandle* handle = nullptr;
assert(!open_options.open_ephemeral_table_reader ||
table_reader_ptr == nullptr);
// Holds ownership of a freshly-opened TableReader when the caller asked us
// to bypass the shared cache. When non-empty, the iterator we hand back must
// arrange to free it on destruction.
std::unique_ptr<TableReader> ephemeral_reader;
if (table_reader_ptr != nullptr) {
*table_reader_ptr = nullptr;
}
const bool effective_skip_filters = skip_filters || open_options.skip_filters;
bool for_compaction = caller == TableReaderCaller::kCompaction;
TEST_SYNC_POINT_CALLBACK("TableCache::NewIterator::BeforeFindTable",
const_cast<FileDescriptor*>(&file_meta.fd));
@@ -329,9 +380,13 @@ InternalIterator* TableCache::NewIterator(
options, file_options, icomparator, file_meta, &handle,
mutable_cf_options, &table_reader,
options.read_tier == kBlockCacheTier /* no_io */, file_read_hist,
skip_filters, level, true /* prefetch_index_and_filter_in_cache */,
max_file_size_for_l0_meta_pin, file_meta.temperature,
maybe_pin_table_handle && should_pin_table_handles_, file_open_metadata);
effective_skip_filters, level,
/*prefetch_index_and_filter_in_cache=*/
!open_options.avoid_shared_metadata_cache, max_file_size_for_l0_meta_pin,
file_meta.temperature,
maybe_pin_table_handle && should_pin_table_handles_, file_open_metadata,
open_options.open_ephemeral_table_reader ? &ephemeral_reader : nullptr,
open_options);
InternalIterator* result = nullptr;
if (s.ok()) {
if (options.table_filter &&
@@ -340,13 +395,17 @@ InternalIterator* TableCache::NewIterator(
} else {
result = table_reader->NewIterator(
options, mutable_cf_options.prefix_extractor.get(), arena,
skip_filters, caller, file_options.compaction_readahead_size,
allow_unprepared_value);
effective_skip_filters, caller,
file_options.compaction_readahead_size, allow_unprepared_value);
}
if (handle != nullptr) {
cache_.RegisterReleaseAsCleanup(handle, *result);
handle = nullptr; // prevent from releasing below
}
// Don't hand ephemeral_reader to result's cleanup yet: range-del
// processing below can set s to non-OK, in which case result is replaced
// by an error iterator at function end. Transfer ownership only once we
// know s stays OK; otherwise ephemeral_reader's destructor frees it.
if (for_compaction) {
table_reader->SetupForCompaction();
@@ -398,8 +457,37 @@ InternalIterator* TableCache::NewIterator(
if (handle != nullptr) {
cache_.Release(handle);
}
// Range-del processing is done and s is final. Hand the ephemeral reader's
// lifetime to the returned iterator; if s is non-OK, leave it for
// ephemeral_reader's destructor. RegisterCleanup gets the raw pointer before
// release(), so if its allocation throws the reader is still owned by
// ephemeral_reader and freed on unwind.
if (s.ok() && ephemeral_reader && result != nullptr) {
TableReader* raw = ephemeral_reader.get();
result->RegisterCleanup(
[](void* arg1, void* /*arg2*/) {
delete static_cast<TableReader*>(arg1);
},
raw, nullptr);
// release() returns raw, which the cleanup now owns; drop the unique_ptr's
// ownership so the reader isn't freed twice.
[[maybe_unused]] TableReader* released = ephemeral_reader.release();
assert(released == raw);
}
if (!s.ok()) {
// Today result is always null here: it is only set when s was OK, and the
// only later change to s, new_range_del_iter->status(), is always OK for a
// FragmentedRangeTombstoneIterator. The assert documents that; the cleanup
// still disposes of any stray result before the error iterator replaces it.
assert(result == nullptr);
if (result != nullptr) {
if (arena != nullptr) {
result->~InternalIterator();
} else {
delete result;
}
result = nullptr;
}
result = NewErrorInternalIterator<Slice>(s, arena);
}
return result;
+49 -17
View File
@@ -35,6 +35,18 @@ struct FileDescriptor;
class GetContext;
class HistogramImpl;
struct TableCacheOpenOptions {
// Open a new TableReader owned by the returned iterator instead of reusing
// the pinned reader or shared TableCache entry.
bool open_ephemeral_table_reader = false;
// Avoid inserting open-time metadata blocks into the shared block cache.
bool avoid_shared_metadata_cache = false;
// Disable loading/accessing filter blocks for this open/iterator.
bool skip_filters = false;
};
// Manages caching for TableReader objects for a column family. The actual
// cache is allocated separately and passed to the constructor. TableCache
// wraps around the underlying SST file readers by providing Get(),
@@ -73,7 +85,9 @@ class TableCache {
// underlying the returned iterator, or nullptr if no Table object underlies
// the returned iterator. The returned "*table_reader_ptr" object is owned
// by the cache and should not be deleted, and is valid for as long as the
// returned iterator is live.
// returned iterator is live. `table_reader_ptr` must be nullptr when
// `open_options.open_ephemeral_table_reader` is set because that reader is
// owned by iterator cleanup rather than by the cache.
// If !options.ignore_range_deletions, and range_del_iter is non-nullptr,
// then range_del_iter is set to a TruncatedRangeDelIterator for range
// tombstones in the SST file corresponding to the specified file number. The
@@ -82,12 +96,16 @@ class TableCache {
// @param options Must outlive the returned iterator.
// @param range_del_agg If non-nullptr, adds range deletions to the
// aggregator. If an error occurs, returns it in a NewErrorInternalIterator
// @param for_compaction If true, a new TableReader may be allocated (but
// not cached), depending on the CF options
// @param caller Identifies the high-level caller for table-reader behavior
// such as compaction preparation.
// @param skip_filters Disables loading/accessing the filter block
// @param level The level this table is at, -1 for "not set / don't know"
// @param range_del_read_seqno If non-nullptr, will be used to create
// *range_del_iter.
// @param open_options Optional table-open policy overrides. The caller must
// not combine `open_ephemeral_table_reader` with
// `ReadOptions::read_tier = kBlockCacheTier` (no_io);
// opening an ephemeral reader inherently requires I/O.
InternalIterator* NewIterator(
const ReadOptions& options, const FileOptions& toptions,
const InternalKeyComparator& internal_comparator,
@@ -103,7 +121,8 @@ class TableCache {
bool maybe_pin_table_handle = false,
// If non-null, and the table reader is newly opened (not cached),
// retrieves file open metadata via GetFileOpenMetadata().
std::string* file_open_metadata = nullptr);
std::string* file_open_metadata = nullptr,
const TableCacheOpenOptions& open_options = TableCacheOpenOptions());
// If a seek to internal key "k" in specified file finds an entry,
// call get_context->SaveValue() repeatedly until
@@ -190,18 +209,30 @@ class TableCache {
// @param pin_table_handle If true, pins the table reader on file_meta so
// future lookups bypass the cache. *handle is set to nullptr
// on return in this case.
Status FindTable(const ReadOptions& ro, const FileOptions& toptions,
const InternalKeyComparator& internal_comparator,
const FileMetaData& file_meta, TypedHandle**,
const MutableCFOptions& mutable_cf_options,
TableReader** table_reader, const bool no_io = false,
HistogramImpl* file_read_hist = nullptr,
bool skip_filters = false, int level = -1,
bool prefetch_index_and_filter_in_cache = true,
size_t max_file_size_for_l0_meta_pin = 0,
Temperature file_temperature = Temperature::kUnknown,
bool pin_table_handle = false,
std::string* file_open_metadata = nullptr);
// @param fresh_table_reader_owner If non-null, FindTable always opens a new
// TableReader (skipping the pinned-reader fast path and the
// shared cache) and writes ownership into this unique_ptr.
// `*handle` will be nullptr on return and `*table_reader` will
// point to the freshly allocated reader. Callers that pass this
// are responsible for keeping the unique_ptr alive for the
// lifetime of any iterators built on top.
// @param open_options Optional table-open policy overrides (e.g.
// avoid_shared_metadata_cache, skip_filters).
// `open_options.open_ephemeral_table_reader` must be set if and
// only if `fresh_table_reader_owner` is non-null.
Status FindTable(
const ReadOptions& ro, const FileOptions& toptions,
const InternalKeyComparator& internal_comparator,
const FileMetaData& file_meta, TypedHandle**,
const MutableCFOptions& mutable_cf_options, TableReader** table_reader,
const bool no_io = false, HistogramImpl* file_read_hist = nullptr,
bool skip_filters = false, int level = -1,
bool prefetch_index_and_filter_in_cache = true,
size_t max_file_size_for_l0_meta_pin = 0,
Temperature file_temperature = Temperature::kUnknown,
bool pin_table_handle = false, std::string* file_open_metadata = nullptr,
std::unique_ptr<TableReader>* fresh_table_reader_owner = nullptr,
const TableCacheOpenOptions& open_options = TableCacheOpenOptions());
// Get the table properties of a given table.
// @no_io: indicates if we should load table to the cache if it is not present
@@ -286,7 +317,8 @@ class TableCache {
bool prefetch_index_and_filter_in_cache = true,
size_t max_file_size_for_l0_meta_pin = 0,
Temperature file_temperature = Temperature::kUnknown,
std::string* file_open_metadata = nullptr);
std::string* file_open_metadata = nullptr,
bool avoid_shared_metadata_cache = false);
// Update the max_covering_tombstone_seq in the GetContext for each key based
// on the range deletions in the table
+26
View File
@@ -240,6 +240,13 @@ bool VersionEdit::EncodeTo(std::string* dst,
PutLengthPrefixedSlice(dst, progress_data);
}
if (has_last_compacted_manifest_file_size_) {
PutVarint32(dst, kLastCompactedManifestFileSize);
std::string varint_size;
PutVarint64(&varint_size, last_compacted_manifest_file_size_);
PutLengthPrefixedSlice(dst, Slice(varint_size));
}
return true;
}
@@ -890,6 +897,17 @@ Status VersionEdit::DecodeFrom(const Slice& src) {
break;
}
case kLastCompactedManifestFileSize: {
Slice encoded;
if (GetLengthPrefixedSlice(&input, &encoded) &&
GetVarint64(&encoded, &last_compacted_manifest_file_size_)) {
has_last_compacted_manifest_file_size_ = true;
} else {
msg = "last compacted manifest file size";
}
break;
}
default:
if (tag & kTagSafeIgnoreMask) {
// Tag from future which can be safely ignored.
@@ -1064,6 +1082,10 @@ std::string VersionEdit::DebugString(bool hex_key) const {
r.append("\n SubcompactionProgress: ");
r.append(subcompaction_progress_.ToString());
}
if (has_last_compacted_manifest_file_size_) {
r.append("\n LastCompactedManifestFileSize: ");
AppendNumberTo(&r, last_compacted_manifest_file_size_);
}
r.append("\n}\n");
return r;
}
@@ -1220,6 +1242,10 @@ std::string VersionEdit::DebugJSON(int edit_num, bool hex_key) const {
jw << "SubcompactionProgress" << subcompaction_progress_.ToString();
}
if (has_last_compacted_manifest_file_size_) {
jw << "LastCompactedManifestFileSize" << last_compacted_manifest_file_size_;
}
jw.EndObject();
return jw.Get();
+26
View File
@@ -74,6 +74,7 @@ enum Tag : uint32_t {
kWalDeletion2,
kPersistUserDefinedTimestamps,
kSubcompactionProgress,
kLastCompactedManifestFileSize,
};
enum SubcompactionProgressPerLevelCustomTag : uint32_t {
@@ -336,6 +337,11 @@ struct FileMetaData {
// via FileOptions::file_metadata on subsequent opens. Empty if not available.
std::string file_open_metadata;
// Skips prefetching index and filter blocks into block cache on file open,
// not persisted in MANIFEST. NOTE: false does not guarantee prefetching
// either (e.g. when index and filter blocks are not cached in block cache).
bool skip_index_and_filter_blocks_prefetch = false;
FileMetaData() = default;
FileMetaData(uint64_t file, uint32_t file_path_id, uint64_t file_size,
@@ -977,6 +983,11 @@ class VersionEdit {
bool IsColumnFamilyDrop() const { return is_column_family_drop_; }
void MarkForegroundOperation() { is_foreground_operation_ = true; }
bool IsForegroundOperation() const {
return is_foreground_operation_ || IsColumnFamilyManipulation();
}
void MarkNoManifestWriteDummy() { is_no_manifest_write_dummy_ = true; }
bool IsNoManifestWriteDummy() const { return is_no_manifest_write_dummy_; }
@@ -1016,6 +1027,17 @@ class VersionEdit {
subcompaction_progress_.Clear();
}
void SetLastCompactedManifestFileSize(uint64_t size) {
has_last_compacted_manifest_file_size_ = true;
last_compacted_manifest_file_size_ = size;
}
bool HasLastCompactedManifestFileSize() const {
return has_last_compacted_manifest_file_size_;
}
uint64_t GetLastCompactedManifestFileSize() const {
return last_compacted_manifest_file_size_;
}
// Recovery-time per-column-family edits only need to be written when they
// advance the CF's log number or carry some other manifest state.
bool ShouldEmitPerColumnFamilyRecoveryEdit(uint64_t current_log_number) const;
@@ -1099,6 +1121,7 @@ class VersionEdit {
// it also includes column family name.
bool is_column_family_drop_ = false;
bool is_column_family_add_ = false;
bool is_foreground_operation_ = false;
std::string column_family_name_;
uint32_t remaining_entries_ = 0;
@@ -1111,6 +1134,9 @@ class VersionEdit {
bool has_subcompaction_progress_ = false;
SubcompactionProgress subcompaction_progress_;
bool has_last_compacted_manifest_file_size_ = false;
uint64_t last_compacted_manifest_file_size_ = 0;
// Newly created table files and blob files are eligible for deletion if they
// are not registered as live files after the background jobs creating them
// have finished. In case committing the VersionEdit containing such changes
+5
View File
@@ -642,6 +642,11 @@ Status VersionEditHandler::ExtractInfoFromVersionEdit(ColumnFamilyData* cfd,
version_edit_params_.GetLastSequence() <= edit.GetLastSequence());
version_edit_params_.SetLastSequence(edit.GetLastSequence());
}
if (edit.HasLastCompactedManifestFileSize()) {
version_set_->last_compacted_manifest_file_size_ =
edit.GetLastCompactedManifestFileSize();
version_set_->TuneMaxManifestFileSize();
}
if (!version_edit_params_.HasPrevLogNumber()) {
version_edit_params_.SetPrevLogNumber(0);
}
+401 -136
View File
@@ -18,7 +18,9 @@
#include <set>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "db/blob/blob_fetcher.h"
@@ -69,7 +71,6 @@
#include "table/multiget_context.h"
#include "table/plain/plain_table_factory.h"
#include "table/table_reader.h"
#include "table/two_level_iterator.h"
#include "table/unique_id_impl.h"
#include "test_util/sync_point.h"
#include "util/cast_util.h"
@@ -110,6 +111,181 @@ int FindFileInRange(const InternalKeyComparator& icmp,
return static_cast<int>(std::lower_bound(b + left, b + right, key, cmp) - b);
}
InternalKey MultiScanInternalKey(const Slice& user_key,
const Comparator* user_comparator) {
const size_t timestamp_size = user_comparator->timestamp_size();
if (timestamp_size == 0) {
return InternalKey(user_key, kMaxSequenceNumber, kValueTypeForSeek);
}
std::string key_with_ts;
AppendKeyWithMaxTimestamp(&key_with_ts, user_key, timestamp_size);
return InternalKey(key_with_ts, kMaxSequenceNumber, kValueTypeForSeek);
}
bool MultiScanRangeOverlapsFile(const UserComparatorWrapper& user_comparator,
const ScanOptions& scan_opts,
const FdWithKeyRange& file) {
assert(scan_opts.range.start.has_value());
assert(scan_opts.range.limit.has_value());
if (user_comparator.CompareWithoutTimestamp(
scan_opts.range.limit.value(), /*a_has_ts=*/false,
ExtractUserKey(file.smallest_key), /*b_has_ts=*/true) <= 0) {
return false;
}
if (user_comparator.CompareWithoutTimestamp(
scan_opts.range.start.value(), /*a_has_ts=*/false,
ExtractUserKey(file.largest_key), /*b_has_ts=*/true) > 0) {
return false;
}
return true;
}
void AddMultiScanFileIndex(std::vector<size_t>* file_indexes,
std::vector<char>* selected, size_t file_index) {
if ((*selected)[file_index]) {
return;
}
(*selected)[file_index] = true;
file_indexes->push_back(file_index);
}
template <typename OnOverlappingFile>
bool NotifyMultiScanOverlappingFile(OnOverlappingFile& on_overlapping_file,
size_t file_index,
const ScanOptions& scan_range) {
if constexpr (std::is_same_v<std::invoke_result_t<OnOverlappingFile, size_t,
const ScanOptions&>,
bool>) {
return on_overlapping_file(file_index, scan_range);
} else {
on_overlapping_file(file_index, scan_range);
return true;
}
}
template <typename OnOverlappingFile>
void ForEachMultiScanOverlappingFile(const InternalKeyComparator& icmp,
const UserComparatorWrapper& ucmp,
const LevelFilesBrief& file_level,
int level, const MultiScanArgs& scan_opts,
OnOverlappingFile on_overlapping_file) {
assert(scan_opts.HasBoundedScanRanges());
if (file_level.num_files == 0) {
return;
}
if (level == 0) {
for (size_t i = 0; i < file_level.num_files; ++i) {
for (const ScanOptions& scan_range : scan_opts.GetScanRanges()) {
if (MultiScanRangeOverlapsFile(ucmp, scan_range, file_level.files[i])) {
if (!NotifyMultiScanOverlappingFile(on_overlapping_file, i,
scan_range)) {
return;
}
}
if (ucmp.CompareWithoutTimestamp(
scan_range.range.start.value(), /*a_has_ts=*/false,
ExtractUserKey(file_level.files[i].largest_key),
/*b_has_ts=*/true) > 0) {
break;
}
}
}
return;
}
for (const ScanOptions& scan_range : scan_opts.GetScanRanges()) {
const InternalKey start_key = MultiScanInternalKey(
scan_range.range.start.value(), ucmp.user_comparator());
size_t first_file = FindFile(icmp, file_level, start_key.Encode());
if (first_file >= file_level.num_files) {
continue;
}
const InternalKey limit_key = MultiScanInternalKey(
scan_range.range.limit.value(), ucmp.user_comparator());
size_t last_file = FindFile(icmp, file_level, limit_key.Encode());
if (last_file >= file_level.num_files) {
last_file = file_level.num_files - 1;
}
for (size_t i = first_file; i <= last_file; ++i) {
if (MultiScanRangeOverlapsFile(ucmp, scan_range, file_level.files[i])) {
if (!NotifyMultiScanOverlappingFile(on_overlapping_file, i,
scan_range)) {
return;
}
}
}
}
}
std::vector<size_t> GetMultiScanOverlappingFiles(
const InternalKeyComparator& icmp, const UserComparatorWrapper& ucmp,
const LevelFilesBrief& file_level, int level,
const MultiScanArgs& scan_opts,
size_t max_file_indexes = std::numeric_limits<size_t>::max()) {
std::vector<size_t> file_indexes;
if (!scan_opts.HasBoundedScanRanges() || file_level.num_files == 0) {
return file_indexes;
}
file_indexes.reserve(
std::min(static_cast<size_t>(file_level.num_files), max_file_indexes));
std::vector<char> selected(file_level.num_files, false);
ForEachMultiScanOverlappingFile(
icmp, ucmp, file_level, level, scan_opts,
[&](size_t file_index, const ScanOptions& /*scan_range*/) {
if (file_indexes.size() >= max_file_indexes) {
return false;
}
AddMultiScanFileIndex(&file_indexes, &selected, file_index);
return file_indexes.size() < max_file_indexes;
});
return file_indexes;
}
void AddTableIteratorForLevel(
ColumnFamilyData* cfd, const ReadOptions& read_options,
const FileOptions& soptions, const MutableCFOptions& mutable_cf_options,
MergeIteratorBuilder* merge_iter_builder, Arena* arena,
const LevelFilesBrief& file_level, int level, size_t file_index,
bool skip_filters, size_t max_file_size_for_l0_meta_pin,
bool allow_unprepared_value, SequenceNumber read_seq) {
std::unique_ptr<TruncatedRangeDelIterator> tombstone_iter = nullptr;
const auto& file = file_level.files[file_index];
auto table_iter = cfd->table_cache()->NewIterator(
read_options, soptions, cfd->internal_comparator(), *file.file_metadata,
/*range_del_agg=*/nullptr, mutable_cf_options, nullptr,
cfd->internal_stats()->GetFileReadHist(level),
TableReaderCaller::kUserIterator, arena, skip_filters, level,
max_file_size_for_l0_meta_pin,
/*smallest_compaction_key=*/nullptr,
/*largest_compaction_key=*/nullptr, allow_unprepared_value, &read_seq,
&tombstone_iter,
/*maybe_pin_table_handle=*/true);
#ifndef NDEBUG
TEST_SYNC_POINT_CALLBACK("Version::AddIteratorsForLevel:AddedFile",
file.file_metadata);
std::pair<bool, bool> iterator_type(true /* is_block_based_table_iterator */,
false /* is_level_iterator */);
TEST_SYNC_POINT_CALLBACK("Version::AddIteratorsForLevel:IteratorType",
&iterator_type);
#endif
if (read_options.ignore_range_deletions) {
merge_iter_builder->AddIterator(table_iter);
} else {
merge_iter_builder->AddPointAndTombstoneIterator(table_iter,
std::move(tombstone_iter));
}
}
Status OverlapWithIterator(const Comparator* ucmp,
const Slice& smallest_user_key,
const Slice& largest_user_key,
@@ -966,6 +1142,15 @@ bool SomeFileOverlapsRange(const InternalKeyComparator& icmp,
namespace {
TableCacheOpenOptions GetCompactionTableCacheOpenOptions(
bool open_ephemeral_table_reader) {
TableCacheOpenOptions options;
options.open_ephemeral_table_reader = open_ephemeral_table_reader;
options.avoid_shared_metadata_cache = open_ephemeral_table_reader;
options.skip_filters = open_ephemeral_table_reader;
return options;
}
class LevelIterator final : public InternalIterator {
public:
// NOTE: many of the const& parameters are saved in this object (so
@@ -981,7 +1166,9 @@ class LevelIterator final : public InternalIterator {
bool allow_unprepared_value = false,
std::unique_ptr<TruncatedRangeDelIterator>*** range_tombstone_iter_ptr_ =
nullptr,
Statistics* db_statistics = nullptr, SystemClock* clock = nullptr)
SequenceNumber read_seq = kMaxSequenceNumber,
Statistics* db_statistics = nullptr, SystemClock* clock = nullptr,
bool open_ephemeral_table_reader = false)
: table_cache_(table_cache),
read_options_(read_options),
file_options_(file_options),
@@ -997,9 +1184,11 @@ class LevelIterator final : public InternalIterator {
pinned_iters_mgr_(nullptr),
compaction_boundaries_(compaction_boundaries),
range_tombstone_iter_(nullptr),
read_seq_(read_options.snapshot
? read_options.snapshot->GetSequenceNumber()
: kMaxSequenceNumber),
range_tombstone_iter_required_(range_tombstone_iter_ptr_ != nullptr),
read_seq_(read_seq != kMaxSequenceNumber ||
read_options.snapshot == nullptr
? read_seq
: read_options.snapshot->GetSequenceNumber()),
level_(level),
skip_filters_(skip_filters),
allow_unprepared_value_(allow_unprepared_value),
@@ -1007,7 +1196,9 @@ class LevelIterator final : public InternalIterator {
to_return_sentinel_(false),
scan_opts_(nullptr),
db_statistics_(db_statistics),
clock_(clock) {
clock_(clock),
table_cache_open_options_(
GetCompactionTableCacheOpenOptions(open_ephemeral_table_reader)) {
// Empty level is not supported.
assert(flevel_ != nullptr && flevel_->num_files > 0);
if (range_tombstone_iter_ptr_) {
@@ -1146,69 +1337,18 @@ class LevelIterator final : public InternalIterator {
assert(so->GetComparator() == user_comparator_.user_comparator());
file_to_scan_opts_ = std::make_unique<ScanOptionsMap>();
for (size_t k = 0; k < scan_opts_->size(); k++) {
const ScanOptions& opt = scan_opts_->GetScanRanges().at(k);
auto start = opt.range.start;
auto end = opt.range.limit;
if (!start.has_value()) {
continue;
}
// We can capture this case in the future, but for now lets skip this.
if (!end.has_value()) {
continue;
}
const size_t timestamp_size =
user_comparator_.user_comparator()->timestamp_size();
InternalKey istart, iend;
if (timestamp_size == 0) {
istart =
InternalKey(start.value(), kMaxSequenceNumber, kValueTypeForSeek);
// end key is exclusive for multiscan
iend = InternalKey(end.value(), kMaxSequenceNumber, kValueTypeForSeek);
} else {
std::string start_key_with_ts, end_key_with_ts;
AppendKeyWithMaxTimestamp(&start_key_with_ts, start.value(),
timestamp_size);
AppendKeyWithMaxTimestamp(&end_key_with_ts, end.value(),
timestamp_size);
istart = InternalKey(start_key_with_ts, kMaxSequenceNumber,
kValueTypeForSeek);
// end key is exclusive for multiscan
iend =
InternalKey(end_key_with_ts, kMaxSequenceNumber, kValueTypeForSeek);
}
// TODO: This needs to be optimized, right now we iterate twice, which
// we dont need to. We can do this in N rather than 2N.
size_t fstart = FindFile(icomparator_, *flevel_, istart.Encode());
size_t fend = FindFile(icomparator_, *flevel_, iend.Encode());
// We need to check the relevant cases
// Cases:
// 1. [ S E ]
// 2. [ S ] [ E ]
// 3. [ S ] ...... [ E ]
for (auto i = fstart; i <= fend; i++) {
if (i < flevel_->num_files) {
// FindFile only compares against the largest_key, so we need this
// additional check to ensure the scan range overlaps the file
if (icomparator_.InternalKeyComparator::Compare(
iend.Encode(), flevel_->files[i].smallest_key) < 0) {
continue;
}
auto const metadata = flevel_->files[i].file_metadata;
ForEachMultiScanOverlappingFile(
icomparator_, user_comparator_, *flevel_, level_, *scan_opts_,
[&](size_t file_index, const ScanOptions& scan_range) {
auto const metadata = flevel_->files[file_index].file_metadata;
if (metadata->FileIsStandAloneRangeTombstone()) {
// Skip stand alone range deletion files.
continue;
return;
}
auto& args = GetMultiScanArgForFile(i);
args.insert(start.value(), end.value(), opt.property_bag);
}
}
}
auto& args = GetMultiScanArgForFile(file_index);
args.insert(scan_range.range.start.value(),
scan_range.range.limit.value(), scan_range.property_bag);
});
StopWatch timer(clock_, db_statistics_, MULTISCAN_PREPARE_ITERATORS);
@@ -1221,7 +1361,8 @@ class LevelIterator final : public InternalIterator {
file_to_arg.first));
}
if (so->use_async_io) {
if (so->use_async_io &&
(!range_tombstone_iter_required_ || range_tombstone_iter_ != nullptr)) {
auto before = file_index_;
// Pre-create and prepare only relevant file iterators
for (auto& file_to_arg : *file_to_scan_opts_) {
@@ -1314,7 +1455,9 @@ class LevelIterator final : public InternalIterator {
/*max_file_size_for_l0_meta_pin=*/0, smallest_compaction_key,
largest_compaction_key, allow_unprepared_value_, &read_seq_,
range_tombstone_iter_,
/*maybe_pin_table_handle=*/true);
/*maybe_pin_table_handle=*/
!table_cache_open_options_.open_ephemeral_table_reader,
/*file_open_metadata=*/nullptr, table_cache_open_options_);
}
// Check if current file being fully within iterate_lower_bound.
@@ -1371,6 +1514,7 @@ class LevelIterator final : public InternalIterator {
//
// *range_tombstone_iter_ points to range tombstones of the current SST file
std::unique_ptr<TruncatedRangeDelIterator>* range_tombstone_iter_;
bool range_tombstone_iter_required_;
// The sentinel key to be returned
Slice sentinel_;
@@ -1391,6 +1535,7 @@ class LevelIterator final : public InternalIterator {
Statistics* db_statistics_ = nullptr;
SystemClock* clock_ = nullptr;
TableCacheOpenOptions table_cache_open_options_;
// Our stored scan_opts for each prefix
std::unique_ptr<ScanOptionsMap> file_to_scan_opts_ = nullptr;
@@ -1680,22 +1825,20 @@ bool LevelIterator::SkipEmptyFileForward() {
if (scan_opts_ && FileHasMultiScanArg(file_index_)) {
const ScanOptions& opts =
GetMultiScanArgForFile(file_index_).GetScanRanges().front();
if (opts.range.start.has_value()) {
InternalKey target;
const size_t ts_size =
user_comparator_.user_comparator()->timestamp_size();
if (ts_size == 0) {
target = InternalKey(opts.range.start.value(), kMaxSequenceNumber,
kValueTypeForSeek);
} else {
std::string seek_key;
AppendKeyWithMaxTimestamp(&seek_key, opts.range.start.value(),
ts_size);
target =
InternalKey(seek_key, kMaxSequenceNumber, kValueTypeForSeek);
}
file_iter_.Seek(target.Encode());
assert(opts.range.start.has_value());
InternalKey target;
const size_t ts_size =
user_comparator_.user_comparator()->timestamp_size();
if (ts_size == 0) {
target = InternalKey(opts.range.start.value(), kMaxSequenceNumber,
kValueTypeForSeek);
} else {
std::string seek_key;
AppendKeyWithMaxTimestamp(&seek_key, opts.range.start.value(),
ts_size);
target = InternalKey(seek_key, kMaxSequenceNumber, kValueTypeForSeek);
}
file_iter_.Seek(target.Encode());
} else {
file_iter_.SeekToFirst();
}
@@ -1742,14 +1885,8 @@ void LevelIterator::SkipEmptyFileBackward() {
#ifndef NDEBUG
bool LevelIterator::OverlapRange(const ScanOptions& opts, size_t file_index) {
return (user_comparator_.CompareWithoutTimestamp(
opts.range.start.value(), /*a_has_ts=*/false,
ExtractUserKey(flevel_->files[file_index].largest_key),
/*b_has_ts=*/true) <= 0 &&
user_comparator_.CompareWithoutTimestamp(
opts.range.limit.value(), /*a_has_ts=*/false,
ExtractUserKey(flevel_->files[file_index].smallest_key),
/*b_has_ts=*/true) > 0);
return MultiScanRangeOverlapsFile(user_comparator_, opts,
flevel_->files[file_index]);
}
#endif
@@ -2271,7 +2408,9 @@ InternalIterator* Version::TEST_GetLevelIterator(
mutable_cf_options_, cfd_->internal_stats()->GetFileReadHist(level),
TableReaderCaller::kUserIterator, IsFilterSkipped(level), level,
nullptr /* range_del_agg */, nullptr /* compaction_boundaries */,
allow_unprepared_value, &tombstone_iter_ptr, db_statistics_, clock_);
allow_unprepared_value,
read_options.ignore_range_deletions ? nullptr : &tombstone_iter_ptr,
kMaxSequenceNumber, db_statistics_, clock_);
if (read_options.ignore_range_deletions) {
merge_iter_builder->AddIterator(level_iter);
} else {
@@ -2342,19 +2481,22 @@ double VersionStorageInfo::GetEstimatedCompressionRatioAtLevel(
void Version::AddIterators(const ReadOptions& read_options,
const FileOptions& soptions,
MergeIteratorBuilder* merge_iter_builder,
bool allow_unprepared_value) {
bool allow_unprepared_value, SequenceNumber read_seq,
const MultiScanArgs* scan_opts) {
assert(storage_info_.finalized_);
for (int level = 0; level < storage_info_.num_non_empty_levels(); level++) {
AddIteratorsForLevel(read_options, soptions, merge_iter_builder, level,
allow_unprepared_value);
allow_unprepared_value, read_seq, scan_opts);
}
}
void Version::AddIteratorsForLevel(const ReadOptions& read_options,
const FileOptions& soptions,
MergeIteratorBuilder* merge_iter_builder,
int level, bool allow_unprepared_value) {
int level, bool allow_unprepared_value,
SequenceNumber read_seq,
const MultiScanArgs* scan_opts) {
assert(storage_info_.finalized_);
if (level >= storage_info_.num_non_empty_levels()) {
// This is an empty level
@@ -2365,37 +2507,95 @@ void Version::AddIteratorsForLevel(const ReadOptions& read_options,
}
auto* arena = merge_iter_builder->GetArena();
const LevelFilesBrief& file_level = storage_info_.LevelFilesBrief(level);
const UserComparatorWrapper user_comparator(
cfd_->internal_comparator().user_comparator());
if (scan_opts != nullptr) {
const std::vector<size_t> file_indexes = GetMultiScanOverlappingFiles(
cfd_->internal_comparator(), user_comparator, file_level, level,
*scan_opts, level == 0 ? std::numeric_limits<size_t>::max() : 2);
if (file_indexes.empty()) {
return;
}
if (level == 0) {
const bool should_sample = should_sample_file_read();
for (size_t file_index : file_indexes) {
AddTableIteratorForLevel(
cfd_, read_options, soptions, mutable_cf_options_,
merge_iter_builder, arena, file_level, level, file_index,
/*skip_filters=*/false, max_file_size_for_l0_meta_pin_,
allow_unprepared_value, read_seq);
if (should_sample) {
sample_file_read_inc(file_level.files[file_index].file_metadata);
}
}
return;
}
if (file_indexes.size() == 1) {
AddTableIteratorForLevel(cfd_, read_options, soptions,
mutable_cf_options_, merge_iter_builder, arena,
file_level, level, file_indexes.front(),
IsFilterSkipped(level),
/*max_file_size_for_l0_meta_pin=*/0,
allow_unprepared_value, read_seq);
return;
}
#ifndef NDEBUG
for (size_t file_index = 0; file_index < file_level.num_files;
++file_index) {
TEST_SYNC_POINT_CALLBACK("Version::AddIteratorsForLevel:AddedFile",
file_level.files[file_index].file_metadata);
}
#endif
auto* mem = arena->AllocateAligned(sizeof(LevelIterator));
std::unique_ptr<TruncatedRangeDelIterator>** tombstone_iter_ptr = nullptr;
auto level_iter = new (mem) LevelIterator(
cfd_->table_cache(), read_options, soptions,
cfd_->internal_comparator(), &file_level, mutable_cf_options_,
cfd_->internal_stats()->GetFileReadHist(level),
TableReaderCaller::kUserIterator, IsFilterSkipped(level), level,
/*range_del_agg=*/nullptr,
/*compaction_boundaries=*/nullptr, allow_unprepared_value,
read_options.ignore_range_deletions ? nullptr : &tombstone_iter_ptr,
read_seq, db_statistics_, clock_);
#ifndef NDEBUG
std::pair<bool, bool> iterator_type(
false /* is_block_based_table_iterator */,
true /* is_level_iterator */);
TEST_SYNC_POINT_CALLBACK("Version::AddIteratorsForLevel:IteratorType",
&iterator_type);
#endif
if (read_options.ignore_range_deletions) {
merge_iter_builder->AddIterator(level_iter);
} else {
assert(tombstone_iter_ptr);
merge_iter_builder->AddPointAndTombstoneIterator(
level_iter, nullptr /* tombstone_iter */, tombstone_iter_ptr);
}
return;
}
if (level == 0) {
// Merge all level zero files together since they may overlap
std::unique_ptr<TruncatedRangeDelIterator> tombstone_iter = nullptr;
for (size_t i = 0; i < storage_info_.LevelFilesBrief(0).num_files; i++) {
const auto& file = storage_info_.LevelFilesBrief(0).files[i];
auto table_iter = cfd_->table_cache()->NewIterator(
read_options, soptions, cfd_->internal_comparator(),
*file.file_metadata, /*range_del_agg=*/nullptr, mutable_cf_options_,
nullptr, cfd_->internal_stats()->GetFileReadHist(0),
TableReaderCaller::kUserIterator, arena,
/*skip_filters=*/false, /*level=*/0, max_file_size_for_l0_meta_pin_,
/*smallest_compaction_key=*/nullptr,
/*largest_compaction_key=*/nullptr, allow_unprepared_value,
/*range_del_read_seqno=*/nullptr, &tombstone_iter,
/*maybe_pin_table_handle=*/true);
if (read_options.ignore_range_deletions) {
merge_iter_builder->AddIterator(table_iter);
} else {
merge_iter_builder->AddPointAndTombstoneIterator(
table_iter, std::move(tombstone_iter));
const bool should_sample = should_sample_file_read();
for (size_t i = 0; i < file_level.num_files; i++) {
AddTableIteratorForLevel(
cfd_, read_options, soptions, mutable_cf_options_, merge_iter_builder,
arena, file_level, level, i,
/*skip_filters=*/false, max_file_size_for_l0_meta_pin_,
allow_unprepared_value, read_seq);
if (should_sample) {
// Count once for every L0 file. This is done per iterator creation
// rather than Seek(), while files in other levels are sampled on
// seek/next/prev.
sample_file_read_inc(file_level.files[i].file_metadata);
}
}
if (should_sample_file_read()) {
// Count ones for every L0 files. This is done per iterator creation
// rather than Seek(), while files in other levels are sampled on
// seek/next/prev.
for (FileMetaData* meta : storage_info_.LevelFiles(0)) {
sample_file_read_inc(meta);
}
}
} else if (storage_info_.LevelFilesBrief(level).num_files > 0) {
} else if (file_level.num_files > 0) {
// For levels > 0, we can use a concatenating iterator that sequentially
// walks through the non-overlapping files in the level, opening them
// lazily.
@@ -2403,12 +2603,13 @@ void Version::AddIteratorsForLevel(const ReadOptions& read_options,
std::unique_ptr<TruncatedRangeDelIterator>** tombstone_iter_ptr = nullptr;
auto level_iter = new (mem) LevelIterator(
cfd_->table_cache(), read_options, soptions,
cfd_->internal_comparator(), &storage_info_.LevelFilesBrief(level),
mutable_cf_options_, cfd_->internal_stats()->GetFileReadHist(level),
cfd_->internal_comparator(), &file_level, mutable_cf_options_,
cfd_->internal_stats()->GetFileReadHist(level),
TableReaderCaller::kUserIterator, IsFilterSkipped(level), level,
/*range_del_agg=*/nullptr,
/*compaction_boundaries=*/nullptr, allow_unprepared_value,
&tombstone_iter_ptr, db_statistics_, clock_);
read_options.ignore_range_deletions ? nullptr : &tombstone_iter_ptr,
read_seq, db_statistics_, clock_);
if (read_options.ignore_range_deletions) {
merge_iter_builder->AddIterator(level_iter);
} else {
@@ -2464,7 +2665,8 @@ Status Version::OverlapWithLevelIterator(const ReadOptions& read_options,
cfd_->internal_comparator(), &storage_info_.LevelFilesBrief(level),
mutable_cf_options_, cfd_->internal_stats()->GetFileReadHist(level),
TableReaderCaller::kUserIterator, IsFilterSkipped(level), level,
&range_del_agg, nullptr, false, nullptr, db_statistics_, clock_));
&range_del_agg, nullptr, false, nullptr, kMaxSequenceNumber,
db_statistics_, clock_));
status = OverlapWithIterator(ucmp, smallest_user_key, largest_user_key,
iter.get(), overlap);
}
@@ -5520,16 +5722,19 @@ struct VersionSet::ManifestWriter {
ColumnFamilyData* cfd;
const autovector<VersionEdit*>& edit_list;
const std::function<void(const Status&)> manifest_write_callback;
int max_file_opening_threads;
explicit ManifestWriter(
InstrumentedMutex* mu, ColumnFamilyData* _cfd,
const autovector<VersionEdit*>& e,
const std::function<void(const Status&)>& manifest_wcb)
const std::function<void(const Status&)>& manifest_wcb,
int _max_file_opening_threads = 1)
: done(false),
cv(mu),
cfd(_cfd),
edit_list(e),
manifest_write_callback(manifest_wcb) {}
manifest_write_callback(manifest_wcb),
max_file_opening_threads(_max_file_opening_threads) {}
~ManifestWriter() { status.PermitUncheckedError(); }
bool IsAllWalEdits() const {
@@ -5904,6 +6109,10 @@ Status VersionSet::ProcessManifestWrites(
ManifestWriter& first_writer = writers.front();
ManifestWriter* last_writer = &first_writer;
// Supports opening table readers with multiple threads, mostly useful for
// batched external sst file ingestion.
int batch_max_file_opening_threads = 1;
assert(!manifest_writers_.empty());
assert(manifest_writers_.front() == &first_writer);
@@ -5940,6 +6149,9 @@ Status VersionSet::ProcessManifestWrites(
for (;;) {
assert(!(*it)->edit_list.front()->IsColumnFamilyManipulation());
last_writer = *it;
batch_max_file_opening_threads =
std::max(batch_max_file_opening_threads,
last_writer->max_file_opening_threads);
assert(last_writer != nullptr);
assert(last_writer->cfd != nullptr);
if (last_writer->cfd->IsDropped()) {
@@ -6111,9 +6323,32 @@ Status VersionSet::ProcessManifestWrites(
uint64_t prev_manifest_file_size = manifest_file_size_;
assert(pending_manifest_file_number_ == 0);
bool has_foreground_operation = false;
for (const VersionEdit* e : batch_edits) {
if (e->IsForegroundOperation()) {
has_foreground_operation = true;
break;
}
}
// For MANIFEST write batches with any foreground operation (external file
// ingestion/import, DeleteFilesInRange(s), and column family manipulations
// like CreateColumnFamily and DropColumnFamily), relax the size limit by 25%
// to reduce the likelihood of a user operation blocking on MANIFEST rotation.
// Background-only batches (flush/compaction) still rotate at the normal
// threshold.
// TODO/future: for workloads like atomic-replace ingestion-only, with zero
// or few flushes and compactions, it might be nice to trigger background
// manifest rotation if we are beyond the soft limit. But the vast majority
// of workloads should have plenty of background manifest ops to avoid
// foreground rotation.
uint64_t enforced_limit = tuned_max_manifest_file_size_;
if (has_foreground_operation) {
uint64_t new_limit = enforced_limit + enforced_limit / 4;
// don't keep in case of overflow
enforced_limit = std::max(enforced_limit, new_limit);
}
if (!skip_manifest_write &&
(!descriptor_log_ ||
prev_manifest_file_size >= tuned_max_manifest_file_size_)) {
(!descriptor_log_ || prev_manifest_file_size >= enforced_limit)) {
TEST_SYNC_POINT("VersionSet::ProcessManifestWrites:BeforeNewManifest");
new_descriptor_log = true;
} else {
@@ -6175,7 +6410,7 @@ Status VersionSet::ProcessManifestWrites(
builder_guards.size() == versions.size());
ColumnFamilyData* cfd = versions[i]->cfd_;
s = builder_guards[i]->version_builder()->LoadTableHandlers(
cfd->internal_stats(), 1 /* max_threads */,
cfd->internal_stats(), batch_max_file_opening_threads,
true /* prefetch_index_and_filter_in_cache */,
false /* is_initial_load */, versions[i]->GetMutableCFOptions(),
MaxFileSizeForL0MetaPin(versions[i]->GetMutableCFOptions()),
@@ -6284,6 +6519,8 @@ Status VersionSet::ProcessManifestWrites(
io_s = SetCurrentFile(
write_options, fs_.get(), dbname_, pending_manifest_file_number_,
file_options_.temperature, dir_contains_current_file);
TEST_SYNC_POINT_CALLBACK(
"VersionSet::ProcessManifestWrites:AfterSetCurrentFile", &io_s);
if (!io_s.ok()) {
s = io_s;
// Quarantine old manifest file in case new manifest file's CURRENT
@@ -6536,7 +6773,7 @@ Status VersionSet::LogAndApply(
InstrumentedMutex* mu, FSDirectory* dir_contains_current_file,
bool new_descriptor_log, const ColumnFamilyOptions* new_cf_options,
const std::vector<std::function<void(const Status&)>>& manifest_wcbs,
const std::function<Status()>& pre_cb) {
const std::function<Status()>& pre_cb, int max_file_opening_threads) {
mu->AssertHeld();
int num_edits = 0;
for (const auto& elist : edit_lists) {
@@ -6568,7 +6805,8 @@ Status VersionSet::LogAndApply(
for (int i = 0; i < num_cfds; ++i) {
const auto wcb =
manifest_wcbs.empty() ? [](const Status&) {} : manifest_wcbs[i];
writers.emplace_back(mu, column_family_datas[i], edit_lists[i], wcb);
writers.emplace_back(mu, column_family_datas[i], edit_lists[i], wcb,
max_file_opening_threads);
manifest_writers_.push_back(&writers[i]);
}
assert(!writers.empty());
@@ -7496,6 +7734,28 @@ Status VersionSet::WriteCurrentStateToManifest(
}
}
}
// Record the approximate compacted manifest size so it's available at
// recovery time for TuneMaxManifestFileSize(). This record must come last
// in WriteCurrentStateToManifest for an accurate size estimate.
{
// Include a rough estimate of this record's own size (~15 bytes for the
// VersionEdit payload + log record header).
constexpr uint64_t kEstimatedRecordOverhead = 15;
VersionEdit edit;
edit.SetLastCompactedManifestFileSize(log->file()->GetFileSize() +
kEstimatedRecordOverhead);
std::string record;
if (!edit.EncodeTo(&record)) {
return Status::Corruption("Unable to Encode VersionEdit:" +
edit.DebugString(true));
}
io_s = log->AddRecord(write_options, record);
if (!io_s.ok()) {
return io_s;
}
}
return Status::OK();
}
@@ -7812,7 +8072,7 @@ InternalIterator* VersionSet::MakeInputIterator(
RangeDelAggregator* range_del_agg,
const FileOptions& file_options_compactions,
const std::optional<const Slice>& start,
const std::optional<const Slice>& end) {
const std::optional<const Slice>& end, bool open_ephemeral_table_reader) {
auto cfd = c->column_family_data();
// Level-0 files have to be merged together. For other levels,
// we will make a concatenating iterator per level.
@@ -7831,6 +8091,8 @@ InternalIterator* VersionSet::MakeInputIterator(
range_tombstones;
size_t num = 0;
[[maybe_unused]] size_t num_input_files = 0;
const TableCacheOpenOptions table_cache_open_options =
GetCompactionTableCacheOpenOptions(open_ephemeral_table_reader);
for (size_t which = 0; which < c->num_input_levels(); which++) {
const LevelFilesBrief* flevel = c->input_levels(which);
num_input_files += flevel->num_files;
@@ -7867,7 +8129,9 @@ InternalIterator* VersionSet::MakeInputIterator(
/*largest_compaction_key=*/nullptr,
/*allow_unprepared_value=*/false,
/*range_del_read_seqno=*/nullptr,
/*range_del_iter=*/&range_tombstone_iter);
/*range_del_iter=*/&range_tombstone_iter,
/*maybe_pin_table_handle=*/false,
/*file_open_metadata=*/nullptr, table_cache_open_options);
range_tombstones.emplace_back(std::move(range_tombstone_iter),
nullptr);
}
@@ -7882,7 +8146,8 @@ InternalIterator* VersionSet::MakeInputIterator(
TableReaderCaller::kCompaction, /*skip_filters=*/false,
/*level=*/static_cast<int>(c->level(which)), range_del_agg,
c->boundaries(which), false, &tombstone_iter_ptr,
db_options_->statistics.get(), clock_);
kMaxSequenceNumber, db_options_->statistics.get(), clock_,
open_ephemeral_table_reader);
range_tombstones.emplace_back(nullptr, tombstone_iter_ptr);
}
}
+22 -5
View File
@@ -85,6 +85,7 @@ class MergeIteratorBuilder;
class SystemClock;
class ManifestTailer;
class FilePickerMultiGet;
class MultiScanArgs;
// VersionEdit is always supposed to be valid and it is used to point at
// entries in Manifest. Ideally it should not be used as a container to
@@ -916,17 +917,27 @@ class Version {
// yield the contents of this Version when merged together.
// @param read_options Must outlive any iterator built by
// `merger_iter_builder`.
// @param read_seq Snapshot sequence to use for range tombstone visibility.
// This is passed separately because lazy iterator initialization may happen
// after read_options.snapshot has been released by the caller.
// @param scan_opts Optional bounded scan ranges used to prune levels/files
// while building the iterator tree.
void AddIterators(const ReadOptions& read_options,
const FileOptions& soptions,
MergeIteratorBuilder* merger_iter_builder,
bool allow_unprepared_value);
bool allow_unprepared_value, SequenceNumber read_seq,
const MultiScanArgs* scan_opts = nullptr);
// @param read_options Must outlive any iterator built by
// `merger_iter_builder`.
// @param read_seq Snapshot sequence to use for range tombstone visibility.
// @param scan_opts Optional bounded scan ranges used to prune this level.
void AddIteratorsForLevel(const ReadOptions& read_options,
const FileOptions& soptions,
MergeIteratorBuilder* merger_iter_builder,
int level, bool allow_unprepared_value);
int level, bool allow_unprepared_value,
SequenceNumber read_seq,
const MultiScanArgs* scan_opts = nullptr);
Status OverlapWithLevelIterator(const ReadOptions&, const FileOptions&,
const Slice& smallest_user_key,
@@ -1319,7 +1330,8 @@ class VersionSet {
bool new_descriptor_log = false,
const ColumnFamilyOptions* new_cf_options = nullptr,
const std::vector<std::function<void(const Status&)>>& manifest_wcbs = {},
const std::function<Status()>& pre_cb = {});
const std::function<Status()>& pre_cb = {},
int max_file_opening_threads = 1);
void WakeUpWaitingManifestWriters();
@@ -1558,12 +1570,16 @@ class VersionSet {
// The caller should delete the iterator when no longer needed.
// @param read_options Must outlive the returned iterator.
// @param start, end indicates compaction range
// @param open_ephemeral_table_reader When true, the per-file iterators
// bypass the shared TableCache and open fresh TableReaders
// using `file_options_compactions`.
InternalIterator* MakeInputIterator(
const ReadOptions& read_options, const Compaction* c,
RangeDelAggregator* range_del_agg,
const FileOptions& file_options_compactions,
const std::optional<const Slice>& start,
const std::optional<const Slice>& end);
const std::optional<const Slice>& end,
bool open_ephemeral_table_reader = false);
// Add all files listed in any live version to *live_table_files and
// *live_blob_files. Note that these lists may contain duplicates.
@@ -1950,7 +1966,8 @@ class ReactiveVersionSet : public VersionSet {
InstrumentedMutex* /*mu*/, FSDirectory* /*dir_contains_current_file*/,
bool /*new_descriptor_log*/, const ColumnFamilyOptions* /*new_cf_option*/,
const std::vector<std::function<void(const Status&)>>& /*manifest_wcbs*/,
const std::function<Status()>& /*pre_cb*/) override {
const std::function<Status()>& /*pre_cb*/,
int /*max_file_opening_threads*/) override {
return Status::NotSupported("not supported in reactive mode");
}
+4 -1
View File
@@ -62,11 +62,14 @@ Status LoadTableHandlersHelper(
TableCache::TypedHandle* handle = nullptr;
TableReader* table_reader = nullptr;
const bool prefetch_index_and_filter_for_file =
prefetch_index_and_filter_in_cache &&
!file_meta->skip_index_and_filter_blocks_prefetch;
auto status = table_cache->FindTable(
read_options, file_options, internal_comparator, *file_meta, &handle,
mutable_cf_options, &table_reader, false /* no_io */,
internal_stats->GetFileReadHist(level), false /* skip_filters */,
level, prefetch_index_and_filter_in_cache,
level, prefetch_index_and_filter_for_file,
max_file_size_for_l0_meta_pin, file_meta->temperature,
true /* pin_table_handle */);
+8 -2
View File
@@ -13,7 +13,9 @@
namespace ROCKSDB_NAMESPACE {
class BatchedOpsStressTest : public StressTest {
public:
BatchedOpsStressTest() = default;
BatchedOpsStressTest(int db_index, const std::string& db_path,
const std::string& ev_path, const std::string& sec_path)
: StressTest(db_index, db_path, ev_path, sec_path) {}
virtual ~BatchedOpsStressTest() = default;
bool IsStateTracked() const override { return false; }
@@ -722,7 +724,11 @@ class BatchedOpsStressTest : public StressTest {
}
};
StressTest* CreateBatchedOpsStressTest() { return new BatchedOpsStressTest(); }
StressTest* CreateBatchedOpsStressTest(int db_index, const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path) {
return new BatchedOpsStressTest(db_index, db_path, ev_path, sec_path);
}
} // namespace ROCKSDB_NAMESPACE
#endif // GFLAGS
+21 -15
View File
@@ -21,7 +21,10 @@
namespace ROCKSDB_NAMESPACE {
class CfConsistencyStressTest : public StressTest {
public:
CfConsistencyStressTest() : batch_id_(0) {}
CfConsistencyStressTest(int db_index, const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path)
: StressTest(db_index, db_path, ev_path, sec_path), batch_id_(0) {}
~CfConsistencyStressTest() override = default;
@@ -227,10 +230,10 @@ class CfConsistencyStressTest : public StressTest {
key, &value0);
// Temporarily disable error injection for verification
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
@@ -279,10 +282,10 @@ class CfConsistencyStressTest : public StressTest {
}
// Enable back error injection disabled for verification
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
db_->ReleaseSnapshot(snapshot);
@@ -397,10 +400,10 @@ class CfConsistencyStressTest : public StressTest {
&cmp_result);
// Temporarily disable error injection for verification
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
@@ -559,10 +562,10 @@ class CfConsistencyStressTest : public StressTest {
}
// Enable back error injection disabled for verification
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
}
@@ -1543,8 +1546,11 @@ class CfConsistencyStressTest : public StressTest {
std::deque<DebugEvent> recent_debug_events_;
};
StressTest* CreateCfConsistencyStressTest() {
return new CfConsistencyStressTest();
StressTest* CreateCfConsistencyStressTest(int db_index,
const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path) {
return new CfConsistencyStressTest(db_index, db_path, ev_path, sec_path);
}
} // namespace ROCKSDB_NAMESPACE
+39 -45
View File
@@ -13,16 +13,17 @@
#include <cmath>
#include "db_stress_tool/db_stress_test_base.h"
#include "file/file_util.h"
#include "rocksdb/secondary_cache.h"
#include "util/file_checksum_helper.h"
#include "util/xxhash.h"
ROCKSDB_NAMESPACE::Env* db_stress_listener_env = nullptr;
ROCKSDB_NAMESPACE::Env* db_stress_env = nullptr;
std::shared_ptr<ROCKSDB_NAMESPACE::FaultInjectionTestFS> fault_fs_guard;
ROCKSDB_NAMESPACE::Env* raw_env = nullptr;
std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache> compressed_secondary_cache;
std::shared_ptr<ROCKSDB_NAMESPACE::Cache> block_cache;
std::shared_ptr<ROCKSDB_NAMESPACE::WriteBufferManager> wbm;
std::shared_ptr<ROCKSDB_NAMESPACE::RateLimiter> rate_limiter;
enum ROCKSDB_NAMESPACE::CompressionType compression_type_e =
ROCKSDB_NAMESPACE::kSnappyCompression;
enum ROCKSDB_NAMESPACE::CompressionType bottommost_compression_type_e =
@@ -114,10 +115,10 @@ void PoolSizeChangeThread(void* v) {
if (new_thread_pool_size < 1) {
new_thread_pool_size = 1;
}
db_stress_env->SetBackgroundThreads(new_thread_pool_size,
ROCKSDB_NAMESPACE::Env::Priority::LOW);
raw_env->SetBackgroundThreads(new_thread_pool_size,
ROCKSDB_NAMESPACE::Env::Priority::LOW);
// Sleep up to 3 seconds
db_stress_env->SleepForMicroseconds(
raw_env->SleepForMicroseconds(
thread->rand.Next() % FLAGS_compaction_thread_pool_adjust_interval *
1000 +
1);
@@ -144,7 +145,7 @@ void DbVerificationThread(void* v) {
if (!shared->HasVerificationFailedYet()) {
stress_test->ContinuouslyVerifyDb(thread);
}
db_stress_env->SleepForMicroseconds(
raw_env->SleepForMicroseconds(
thread->rand.Next() % FLAGS_continuous_verification_interval * 1000 +
1);
}
@@ -166,7 +167,7 @@ void CompressedCacheSetCapacityThread(void* v) {
return;
}
}
db_stress_env->SleepForMicroseconds(FLAGS_secondary_cache_update_interval);
raw_env->SleepForMicroseconds(FLAGS_secondary_cache_update_interval);
if (FLAGS_compressed_secondary_cache_size > 0) {
Status s = compressed_secondary_cache->SetCapacity(0);
size_t capacity;
@@ -174,7 +175,7 @@ void CompressedCacheSetCapacityThread(void* v) {
s = compressed_secondary_cache->GetCapacity(capacity);
assert(capacity == 0);
}
db_stress_env->SleepForMicroseconds(10 * 1000 * 1000);
raw_env->SleepForMicroseconds(10 * 1000 * 1000);
if (s.ok()) {
s = compressed_secondary_cache->SetCapacity(
FLAGS_compressed_secondary_cache_size);
@@ -201,7 +202,7 @@ void CompressedCacheSetCapacityThread(void* v) {
block_cache->SetCapacity(capacity - adjustment);
fprintf(stdout, "New cache capacity = %zu\n",
block_cache->GetCapacity());
db_stress_env->SleepForMicroseconds(10 * 1000 * 1000);
raw_env->SleepForMicroseconds(10 * 1000 * 1000);
block_cache->SetCapacity(capacity);
} else {
Status s;
@@ -214,7 +215,7 @@ void CompressedCacheSetCapacityThread(void* v) {
s = UpdateTieredCache(block_cache, /*capacity*/ -1,
new_comp_cache_ratio);
if (s.ok()) {
db_stress_env->SleepForMicroseconds(10 * 1000 * 1000);
raw_env->SleepForMicroseconds(10 * 1000 * 1000);
}
if (s.ok()) {
s = UpdateTieredCache(block_cache, /*capacity*/ -1,
@@ -231,36 +232,37 @@ void CompressedCacheSetCapacityThread(void* v) {
#ifndef NDEBUG
static void SetupFaultInjectionForRemoteCompaction(SharedState* shared) {
if (!fault_fs_guard) {
auto fault_fs = shared->GetStressTest()->GetDbFaultInjectionFs();
if (!fault_fs) {
return;
}
fault_fs_guard->SetThreadLocalErrorContext(
fault_fs->SetThreadLocalErrorContext(
FaultInjectionIOType::kRead, shared->GetSeed(), FLAGS_read_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(FaultInjectionIOType::kRead);
fault_fs->EnableThreadLocalErrorInjection(FaultInjectionIOType::kRead);
fault_fs_guard->SetThreadLocalErrorContext(
fault_fs->SetThreadLocalErrorContext(
FaultInjectionIOType::kWrite, shared->GetSeed(), FLAGS_write_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(FaultInjectionIOType::kWrite);
fault_fs->EnableThreadLocalErrorInjection(FaultInjectionIOType::kWrite);
fault_fs_guard->SetThreadLocalErrorContext(
fault_fs->SetThreadLocalErrorContext(
FaultInjectionIOType::kMetadataRead, shared->GetSeed(),
FLAGS_metadata_read_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
fault_fs->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
fault_fs_guard->SetThreadLocalErrorContext(
fault_fs->SetThreadLocalErrorContext(
FaultInjectionIOType::kMetadataWrite, shared->GetSeed(),
FLAGS_metadata_write_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
fault_fs->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
#endif // NDEBUG
@@ -268,7 +270,7 @@ static void SetupFaultInjectionForRemoteCompaction(SharedState* shared) {
static CompactionServiceOptionsOverride CreateOverrideOptions(
const Options& options, const CompactionServiceJobInfo& job_info) {
CompactionServiceOptionsOverride override_options{
.env = db_stress_env,
.env = options.env,
.file_checksum_gen_factory = options.file_checksum_gen_factory,
.merge_operator = options.merge_operator,
.compaction_filter = options.compaction_filter,
@@ -311,14 +313,8 @@ static CompactionServiceOptionsOverride CreateOverrideOptions(
}
static Status CleanupOutputDirectory(const std::string& output_directory) {
#ifndef NDEBUG
// Temporarily disable fault injection to ensure deletion always succeeds
if (fault_fs_guard) {
fault_fs_guard->DisableAllThreadLocalErrorInjection();
}
#endif // NDEBUG
Status s = DestroyDir(db_stress_env, output_directory);
// Uses raw_env (no fault injection); cleanup must always succeed.
Status s = DestroyDir(raw_env, output_directory);
if (!s.ok()) {
fprintf(stderr,
"Failed to destroy output directory %s when allow_resumption is "
@@ -327,7 +323,7 @@ static Status CleanupOutputDirectory(const std::string& output_directory) {
}
if (s.ok()) {
s = db_stress_env->CreateDir(output_directory);
s = raw_env->CreateDir(output_directory);
if (!s.ok()) {
fprintf(stderr,
"Failed to recreate output directory %s when allow_resumption is "
@@ -336,13 +332,6 @@ static Status CleanupOutputDirectory(const std::string& output_directory) {
}
}
#ifndef NDEBUG
// Re-enable fault injection after deletion
if (fault_fs_guard) {
fault_fs_guard->EnableAllThreadLocalErrorInjection();
}
#endif // NDEBUG
return s;
}
@@ -497,7 +486,7 @@ void RemoteCompactionWorkerThread(void* v) {
shared, stress_test, rand, successful_compaction_end_to_end_micros);
}
db_stress_env->SleepForMicroseconds(
raw_env->SleepForMicroseconds(
thread->rand.Next() % FLAGS_remote_compaction_worker_interval * 1000 +
1);
}
@@ -679,7 +668,7 @@ bool VerifyIteratorAttributeGroups(
}
std::string GetNowNanos() {
uint64_t t = db_stress_env->NowNanos();
uint64_t t = raw_env->NowNanos();
std::string ret;
PutFixed64(&ret, t);
return ret;
@@ -691,7 +680,7 @@ uint64_t GetWriteUnixTime(ThreadState* thread) {
FLAGS_preclude_last_level_data_seconds);
static uint64_t kFallbackTime = std::numeric_limits<uint64_t>::max();
int64_t write_time = 0;
Status s = db_stress_env->GetCurrentTime(&write_time);
Status s = raw_env->GetCurrentTime(&write_time);
uint32_t write_time_mode = thread->rand.Uniform(3);
if (write_time_mode == 0 || !s.ok()) {
return kFallbackTime;
@@ -806,6 +795,8 @@ std::shared_ptr<FileChecksumGenFactory> GetFileChecksumImpl(
return std::make_shared<DbStressChecksumGenFactory>(internal_name);
}
// Expected values state files are always on local filesystem (Python owns
// this dir), so use Env::Default() (PosixEnv) even when raw_env is remote.
Status DeleteFilesInDirectory(const std::string& dirname) {
std::vector<std::string> filenames;
Status s = Env::Default()->GetChildren(dirname, &filenames);
@@ -880,8 +871,12 @@ Status DestroyUnverifiedSubdir(const std::string& dirname) {
Status DbStressDestroyDb(const std::string& db_path) {
Status s;
Options options;
// NOTE: using db_stress_listener_env in order to see obsolete MANIFEST files
options.env = db_stress_listener_env;
// Use raw_env (no DbStressFSWrapper) because DbStressFSWrapper renames
// MANIFEST files to MANIFEST-xxx_renamed_ instead of deleting them during
// normal DB operation (to preserve history for debugging). Using raw_env
// here ensures DestroyDB actually deletes these files. DestroyDir below
// catches any remaining files not known to DestroyDB.
options.env = raw_env;
// Remove DB files in a principled way to avoid issues
if (FLAGS_use_blob_db) {
s = blob_db::DestroyBlobDB(db_path, options, blob_db::BlobDBOptions());
@@ -891,9 +886,8 @@ Status DbStressDestroyDb(const std::string& db_path) {
if (!s.ok()) {
return s;
}
// Remove everything else recursively, only reporting success if able to
// delete everything
return DestroyDir(db_stress_listener_env, db_path);
// Remove everything else recursively (catches MANIFEST_renamed_ files)
return DestroyDir(raw_env, db_path);
}
} // namespace ROCKSDB_NAMESPACE
+33 -9
View File
@@ -49,6 +49,7 @@
#include "rocksdb/advanced_options.h"
#include "rocksdb/cache.h"
#include "rocksdb/env.h"
#include "rocksdb/rate_limiter.h"
#include "rocksdb/slice.h"
#include "rocksdb/slice_transform.h"
#include "rocksdb/statistics.h"
@@ -61,6 +62,7 @@
#include "rocksdb/utilities/transaction.h"
#include "rocksdb/utilities/transaction_db.h"
#include "rocksdb/write_batch.h"
#include "rocksdb/write_buffer_manager.h"
#include "test_util/testutil.h"
#include "util/coding.h"
#include "util/compression.h"
@@ -114,6 +116,7 @@ DECLARE_double(memtable_prefix_bloom_size_ratio);
DECLARE_bool(memtable_whole_key_filtering);
DECLARE_int32(open_files);
DECLARE_bool(open_files_async);
DECLARE_bool(async_wal_precreate);
DECLARE_uint64(compressed_secondary_cache_size);
DECLARE_int32(compressed_secondary_cache_numshardbits);
DECLARE_int32(secondary_cache_update_interval);
@@ -190,6 +193,7 @@ DECLARE_string(db);
DECLARE_string(secondaries_base);
DECLARE_bool(test_secondary);
DECLARE_string(expected_values_dir);
DECLARE_int32(num_dbs);
DECLARE_bool(expected_state_trace_debug);
DECLARE_int64(expected_state_trace_debug_key);
DECLARE_int32(expected_state_trace_debug_max_logs);
@@ -197,6 +201,7 @@ DECLARE_bool(verify_checksum);
DECLARE_bool(mmap_read);
DECLARE_bool(mmap_write);
DECLARE_bool(use_direct_reads);
DECLARE_bool(use_direct_io_for_compaction_reads);
DECLARE_bool(use_direct_io_for_flush_and_compaction);
DECLARE_bool(mock_direct_io);
DECLARE_bool(statistics);
@@ -224,6 +229,8 @@ DECLARE_uint64(backup_max_size);
DECLARE_int32(checkpoint_one_in);
DECLARE_int32(ingest_external_file_one_in);
DECLARE_int32(ingest_external_file_width);
DECLARE_int32(ingest_external_file_prepare_commit_one_in);
DECLARE_int32(ingest_external_file_use_file_info_one_in);
DECLARE_int32(compact_files_one_in);
DECLARE_int32(compact_range_one_in);
DECLARE_int32(promote_l0_one_in);
@@ -299,7 +306,7 @@ DECLARE_string(default_write_temperature);
DECLARE_string(default_temperature);
DECLARE_uint32(verify_output_flags);
DECLARE_bool(paranoid_memory_checks);
DECLARE_bool(memtable_veirfy_per_key_checksum_on_seek);
DECLARE_bool(memtable_verify_per_key_checksum_on_seek);
DECLARE_bool(memtable_batch_lookup_optimization);
// Options for transaction dbs.
@@ -462,6 +469,7 @@ DECLARE_uint32(ingest_wbwi_one_in);
DECLARE_bool(universal_reduce_file_locking);
DECLARE_bool(use_multiscan);
DECLARE_bool(multiscan_use_async_io);
DECLARE_bool(read_scoped_block_buffer_provider);
DECLARE_uint64(multiscan_max_prefetch_memory_bytes);
// Compaction deletion trigger declarations for stress testing
@@ -472,18 +480,23 @@ DECLARE_int32(compaction_on_deletion_window_size);
DECLARE_double(compaction_on_deletion_ratio);
DECLARE_double(read_triggered_compaction_threshold);
DECLARE_string(listener_uri);
constexpr long KB = 1024;
constexpr int kRandomValueMaxFactor = 3;
constexpr int kValueMaxLen = 100;
constexpr uint32_t kLargePrimeForCommonFactorSkew = 1872439133;
// wrapped posix environment
extern ROCKSDB_NAMESPACE::Env* db_stress_env;
extern ROCKSDB_NAMESPACE::Env* db_stress_listener_env;
extern std::shared_ptr<ROCKSDB_NAMESPACE::FaultInjectionTestFS> fault_fs_guard;
// Base env from --env_uri/--fs_uri. No wrappers (no DbStressFSWrapper, no
// fault injection). Could be PosixEnv or remote. Used for infrastructure ops
// (threads, time, dirs, cleanup). Per-StressTest env adds fault injection +
// DbStressFSWrapper on top for DB I/O.
extern ROCKSDB_NAMESPACE::Env* raw_env;
extern std::shared_ptr<ROCKSDB_NAMESPACE::SecondaryCache>
compressed_secondary_cache;
extern std::shared_ptr<ROCKSDB_NAMESPACE::Cache> block_cache;
extern std::shared_ptr<ROCKSDB_NAMESPACE::WriteBufferManager> wbm;
extern std::shared_ptr<ROCKSDB_NAMESPACE::RateLimiter> rate_limiter;
extern enum ROCKSDB_NAMESPACE::CompressionType compression_type_e;
extern enum ROCKSDB_NAMESPACE::CompressionType bottommost_compression_type_e;
@@ -828,10 +841,21 @@ AttributeGroups GenerateAttributeGroups(
const std::vector<ColumnFamilyHandle*>& cfhs, uint32_t value_base,
const Slice& slice);
StressTest* CreateCfConsistencyStressTest();
StressTest* CreateBatchedOpsStressTest();
StressTest* CreateNonBatchedOpsStressTest();
StressTest* CreateMultiOpsTxnsStressTest();
StressTest* CreateCfConsistencyStressTest(int db_index,
const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path);
StressTest* CreateBatchedOpsStressTest(int db_index, const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path);
StressTest* CreateNonBatchedOpsStressTest(int db_index,
const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path);
StressTest* CreateMultiOpsTxnsStressTest(int db_index,
const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path);
void CheckAndSetOptionsForMultiOpsTxnStressTest();
void InitializeHotKeyGenerator(double alpha);
int64_t GetOneHotKeyID(double rand_seed, int64_t max_key);
@@ -1,4 +1,4 @@
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// Copyright (c) Meta Platforms, Inc. and affiliates.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
+70 -49
View File
@@ -10,12 +10,14 @@
#include "db_stress_tool/db_stress_shared_state.h"
#ifdef GFLAGS
#include <mutex>
#include "db_stress_tool/db_stress_common.h"
#include "utilities/fault_injection_fs.h"
namespace ROCKSDB_NAMESPACE {
void ThreadBody(void* v) {
ThreadStatusUtil::RegisterThread(db_stress_env, ThreadStatus::USER);
ThreadStatusUtil::RegisterThread(raw_env, ThreadStatus::USER);
ThreadState* thread = static_cast<ThreadState*>(v);
SharedState* shared = thread->shared;
@@ -64,17 +66,21 @@ void ThreadBody(void* v) {
ThreadStatusUtil::UnregisterThread();
}
bool RunStressTestImpl(SharedState* shared) {
SystemClock* clock = db_stress_env->GetSystemClock().get();
SystemClock* clock = raw_env->GetSystemClock().get();
StressTest* stress = shared->GetStressTest();
const std::string db_label =
(FLAGS_num_dbs > 1) ? "[" + stress->GetDbLabel() + "] " : "";
if (shared->ShouldVerifyAtBeginning() && FLAGS_preserve_unverified_changes) {
Status s = InitUnverifiedSubdir(FLAGS_db);
if (s.ok() && !FLAGS_expected_values_dir.empty()) {
s = InitUnverifiedSubdir(FLAGS_expected_values_dir);
const auto& db_path = stress->GetDbPath();
const auto& ev_dir = stress->GetExpectedValuesDir();
Status s = InitUnverifiedSubdir(db_path);
if (s.ok() && !ev_dir.empty()) {
s = InitUnverifiedSubdir(ev_dir);
}
if (!s.ok()) {
fprintf(stderr, "Failed to setup unverified state dir: %s\n",
s.ToString().c_str());
fprintf(stderr, "%sFailed to setup unverified state dir: %s\n",
db_label.c_str(), s.ToString().c_str());
exit(1);
}
}
@@ -84,24 +90,15 @@ bool RunStressTestImpl(SharedState* shared) {
uint32_t n = FLAGS_threads;
uint64_t now = clock->NowMicros();
fprintf(stdout, "%s Initializing worker threads\n",
clock->TimeToString(now / 1000000).c_str());
fprintf(stdout, "%s %sInitializing worker threads\n",
clock->TimeToString(now / 1000000).c_str(), db_label.c_str());
shared->SetThreads(n);
if (FLAGS_compaction_thread_pool_adjust_interval > 0) {
shared->IncBgThreads();
}
if (FLAGS_continuous_verification_interval > 0) {
shared->IncBgThreads();
}
if (FLAGS_compressed_secondary_cache_size > 0 ||
FLAGS_compressed_secondary_cache_ratio > 0.0) {
shared->IncBgThreads();
}
uint32_t remote_compaction_worker_thread_count =
FLAGS_remote_compaction_worker_threads;
if (remote_compaction_worker_thread_count > 0) {
@@ -113,25 +110,39 @@ bool RunStressTestImpl(SharedState* shared) {
std::vector<ThreadState*> threads(n);
for (uint32_t i = 0; i < n; i++) {
threads[i] = new ThreadState(i, shared);
db_stress_env->StartThread(ThreadBody, threads[i]);
raw_env->StartThread(ThreadBody, threads[i]);
}
// Spawn at most one PoolSizeChangeThread globally. All DBs share the
// same raw_env, so one thread resizing the pool is sufficient.
// IncBgThreads inside call_once so only the launching DB's SharedState
// tracks this thread (avoids deadlock for non-first DBs).
ThreadState bg_thread(0, shared);
static std::once_flag pool_size_thread_flag;
if (FLAGS_compaction_thread_pool_adjust_interval > 0) {
db_stress_env->StartThread(PoolSizeChangeThread, &bg_thread);
std::call_once(pool_size_thread_flag, [&]() {
shared->IncBgThreads();
raw_env->StartThread(PoolSizeChangeThread, &bg_thread);
});
}
ThreadState continuous_verification_thread(0, shared);
if (FLAGS_continuous_verification_interval > 0) {
db_stress_env->StartThread(DbVerificationThread,
&continuous_verification_thread);
raw_env->StartThread(DbVerificationThread, &continuous_verification_thread);
}
// Spawn at most one CompressedCacheSetCapacityThread globally. The cache
// is shared across all DBs, and the thread's SetCapacity(0)/assert(==0)
// sequence would race if multiple DBs each spawned their own copy.
ThreadState compressed_cache_set_capacity_thread(0, shared);
static std::once_flag compressed_cache_thread_flag;
if (FLAGS_compressed_secondary_cache_size > 0 ||
FLAGS_compressed_secondary_cache_ratio > 0.0) {
db_stress_env->StartThread(CompressedCacheSetCapacityThread,
&compressed_cache_set_capacity_thread);
std::call_once(compressed_cache_thread_flag, [&]() {
shared->IncBgThreads();
raw_env->StartThread(CompressedCacheSetCapacityThread,
&compressed_cache_set_capacity_thread);
});
}
std::vector<ThreadState*> remote_compaction_worker_threads;
@@ -141,7 +152,7 @@ bool RunStressTestImpl(SharedState* shared) {
for (uint32_t i = 0; i < remote_compaction_worker_thread_count; i++) {
ThreadState* ts = new ThreadState(i, shared);
remote_compaction_worker_threads.push_back(ts);
db_stress_env->StartThread(RemoteCompactionWorkerThread, ts);
raw_env->StartThread(RemoteCompactionWorkerThread, ts);
}
}
@@ -156,16 +167,19 @@ bool RunStressTestImpl(SharedState* shared) {
}
if (shared->ShouldVerifyAtBeginning()) {
if (shared->HasVerificationFailedYet()) {
fprintf(stderr, "Crash-recovery verification failed :(\n");
fprintf(stderr, "%sCrash-recovery verification failed :(\n",
db_label.c_str());
} else {
fprintf(stdout, "Crash-recovery verification passed :)\n");
Status s = DestroyUnverifiedSubdir(FLAGS_db);
if (s.ok() && !FLAGS_expected_values_dir.empty()) {
s = DestroyUnverifiedSubdir(FLAGS_expected_values_dir);
fprintf(stdout, "%sCrash-recovery verification passed :)\n",
db_label.c_str());
const auto& ev_dir = stress->GetExpectedValuesDir();
Status s = DestroyUnverifiedSubdir(stress->GetDbPath());
if (s.ok() && !ev_dir.empty()) {
s = DestroyUnverifiedSubdir(ev_dir);
}
if (!s.ok()) {
fprintf(stderr, "Failed to cleanup unverified state dir: %s\n",
s.ToString().c_str());
fprintf(stderr, "%sFailed to cleanup unverified state dir: %s\n",
db_label.c_str(), s.ToString().c_str());
exit(1);
}
}
@@ -174,15 +188,16 @@ bool RunStressTestImpl(SharedState* shared) {
if (!FLAGS_verification_only) {
// This is after the verification step to avoid making all those `Get()`s
// and `MultiGet()`s contend on the DB-wide trace mutex.
if (!FLAGS_expected_values_dir.empty()) {
if (!stress->GetExpectedValuesDir().empty()) {
stress->TrackExpectedState(shared);
}
if (FLAGS_sync_fault_injection || FLAGS_write_fault_one_in > 0) {
fault_fs_guard->SetFilesystemDirectWritable(false);
fault_fs_guard->SetInjectUnsyncedDataLoss(FLAGS_sync_fault_injection);
auto fault_fs = stress->GetDbFaultInjectionFs();
fault_fs->SetFilesystemDirectWritable(false);
fault_fs->SetInjectUnsyncedDataLoss(FLAGS_sync_fault_injection);
if (FLAGS_exclude_wal_from_write_fault_injection) {
fault_fs_guard->SetFileTypesExcludedFromWriteFaultInjection(
fault_fs->SetFileTypesExcludedFromWriteFaultInjection(
{FileType::kWalFile});
}
}
@@ -190,8 +205,8 @@ bool RunStressTestImpl(SharedState* shared) {
Status s = stress->EnableAutoCompaction();
assert(s.ok());
}
fprintf(stdout, "%s Starting database operations\n",
clock->TimeToString(now / 1000000).c_str());
fprintf(stdout, "%s %sStarting database operations\n",
clock->TimeToString(now / 1000000).c_str(), db_label.c_str());
shared->SetStart();
shared->GetCondVar()->SignalAll();
@@ -201,14 +216,17 @@ bool RunStressTestImpl(SharedState* shared) {
now = clock->NowMicros();
if (FLAGS_test_batches_snapshots) {
fprintf(stdout, "%s Limited verification already done during gets\n",
clock->TimeToString((uint64_t)now / 1000000).c_str());
fprintf(stdout, "%s %sLimited verification already done during gets\n",
clock->TimeToString((uint64_t)now / 1000000).c_str(),
db_label.c_str());
} else if (FLAGS_skip_verifydb) {
fprintf(stdout, "%s Verification skipped\n",
clock->TimeToString((uint64_t)now / 1000000).c_str());
fprintf(stdout, "%s %sVerification skipped\n",
clock->TimeToString((uint64_t)now / 1000000).c_str(),
db_label.c_str());
} else {
fprintf(stdout, "%s Starting verification\n",
clock->TimeToString((uint64_t)now / 1000000).c_str());
fprintf(stdout, "%s %sStarting verification\n",
clock->TimeToString((uint64_t)now / 1000000).c_str(),
db_label.c_str());
}
shared->SetStartVerify();
@@ -230,7 +248,7 @@ bool RunStressTestImpl(SharedState* shared) {
for (unsigned int i = 1; i < n; i++) {
threads[0]->stats.Merge(threads[i]->stats);
}
threads[0]->stats.Report("Stress Test");
threads[0]->stats.Report((db_label + "Stress Test").c_str());
}
for (unsigned int i = 0; i < n; i++) {
@@ -241,8 +259,8 @@ bool RunStressTestImpl(SharedState* shared) {
now = clock->NowMicros();
if (!FLAGS_skip_verifydb && !FLAGS_test_batches_snapshots &&
!shared->HasVerificationFailedYet()) {
fprintf(stdout, "%s Verification successful\n",
clock->TimeToString(now / 1000000).c_str());
fprintf(stdout, "%s %sVerification successful\n",
clock->TimeToString(now / 1000000).c_str(), db_label.c_str());
}
if (!FLAGS_verification_only) {
@@ -260,6 +278,9 @@ bool RunStressTestImpl(SharedState* shared) {
shared->GetCondVar()->Wait();
}
}
now = clock->NowMicros();
fprintf(stdout, "%s %sStress test bg threads finished\n",
clock->TimeToString(now / 1000000).c_str(), db_label.c_str());
assert(remote_compaction_worker_threads.size() ==
remote_compaction_worker_thread_count);
@@ -271,13 +292,13 @@ bool RunStressTestImpl(SharedState* shared) {
}
if (shared->HasVerificationFailedYet()) {
fprintf(stderr, "Verification failed :(\n");
fprintf(stderr, "%sVerification failed :(\n", db_label.c_str());
return false;
}
return true;
}
bool RunStressTest(SharedState* shared) {
ThreadStatusUtil::RegisterThread(db_stress_env, ThreadStatus::USER);
ThreadStatusUtil::RegisterThread(raw_env, ThreadStatus::USER);
bool result = RunStressTestImpl(shared);
ThreadStatusUtil::UnregisterThread();
return result;
+48 -4
View File
@@ -206,6 +206,10 @@ DEFINE_int32(open_files, ROCKSDB_NAMESPACE::Options().max_open_files,
DEFINE_bool(open_files_async, ROCKSDB_NAMESPACE::Options().open_files_async,
"Options.open_files_async");
DEFINE_bool(async_wal_precreate,
ROCKSDB_NAMESPACE::Options().async_wal_precreate,
"Options.async_wal_precreate");
DEFINE_uint64(compressed_secondary_cache_size, 0,
"Number of bytes to use as a cache of compressed data."
" 0 means use default settings.");
@@ -728,6 +732,11 @@ DEFINE_bool(mmap_write, ROCKSDB_NAMESPACE::Options().allow_mmap_writes,
DEFINE_bool(use_direct_reads, ROCKSDB_NAMESPACE::Options().use_direct_reads,
"Use O_DIRECT for reading data");
DEFINE_bool(use_direct_io_for_compaction_reads,
ROCKSDB_NAMESPACE::Options().use_direct_io_for_compaction_reads,
"Use O_DIRECT for compaction-input SST reads only, while keeping "
"user reads buffered");
DEFINE_bool(use_direct_io_for_flush_and_compaction,
ROCKSDB_NAMESPACE::Options().use_direct_io_for_flush_and_compaction,
"Use O_DIRECT for writing data");
@@ -858,6 +867,19 @@ DEFINE_int32(ingest_external_file_one_in, 0,
DEFINE_int32(ingest_external_file_width, 100,
"The width of the ingested external files.");
DEFINE_int32(ingest_external_file_prepare_commit_one_in, 0,
"If non-zero, an ingestion that would call IngestExternalFile() "
"instead uses the two-phase PrepareFileIngestion()/"
"CommitFileIngestion() API once for every N such ingestions on "
"average, occasionally dropping the prepared handle without "
"committing to exercise the rollback path. 0 disables it.");
DEFINE_int32(ingest_external_file_use_file_info_one_in, 0,
"If non-zero, the ingestexternalfile flow reuses each file's "
"metadata via IngestExternalFileArg::file_infos (from "
"SstFileWriter::Finish) once every N ingestions on average, so "
"ingestion skips re-opening and scanning the files.");
DEFINE_int32(compact_files_one_in, 0,
"If non-zero, then CompactFiles() will be called once for every N "
"operations on average. 0 indicates CompactFiles() is disabled.");
@@ -1017,7 +1039,8 @@ DEFINE_int32(iterpercent, 10,
static const bool FLAGS_iterpercent_dummy __attribute__((__unused__)) =
RegisterFlagValidator(&FLAGS_iterpercent, &ValidateInt32Percent);
DEFINE_uint64(num_iterations, 10, "Number of iterations per MultiIterate run");
DEFINE_uint64(num_iterations, 10,
"Number of iterations per iterator or MultiScan run");
static const bool FLAGS_num_iterations_dummy __attribute__((__unused__)) =
RegisterFlagValidator(&FLAGS_num_iterations, &ValidateUint32Range);
@@ -1626,9 +1649,9 @@ DEFINE_bool(paranoid_memory_checks,
"Sets CF option paranoid_memory_checks.");
DEFINE_bool(
memtable_veirfy_per_key_checksum_on_seek,
ROCKSDB_NAMESPACE::Options().memtable_veirfy_per_key_checksum_on_seek,
"Sets CF option memtable_veirfy_per_key_checksum_on_seek.");
memtable_verify_per_key_checksum_on_seek,
ROCKSDB_NAMESPACE::Options().memtable_verify_per_key_checksum_on_seek,
"Sets CF option memtable_verify_per_key_checksum_on_seek.");
DEFINE_bool(memtable_batch_lookup_optimization,
ROCKSDB_NAMESPACE::Options().memtable_batch_lookup_optimization,
@@ -1664,6 +1687,10 @@ DEFINE_double(read_triggered_compaction_threshold,
ROCKSDB_NAMESPACE::Options().read_triggered_compaction_threshold,
"Sets CF option read_triggered_compaction_threshold.");
DEFINE_string(listener_uri, "",
"URI for an additional EventListener to attach (e.g. "
"auto_tuner://). Empty means none.");
DEFINE_bool(
auto_refresh_iterator_with_snapshot,
ROCKSDB_NAMESPACE::ReadOptions().auto_refresh_iterator_with_snapshot,
@@ -1697,10 +1724,27 @@ DEFINE_bool(use_multiscan, false,
DEFINE_bool(multiscan_use_async_io, false,
"If set, enable async_io for MultiScan operations.");
DEFINE_bool(read_scoped_block_buffer_provider, false,
"If set, configure ReadOptions::read_scoped_block_buffer_provider "
"with a stress-test provider for supported scan reads.");
DEFINE_uint64(multiscan_max_prefetch_memory_bytes, 0,
"If non-zero, sets the max_prefetch_memory_bytes on the "
"IODispatcher used for MultiScan. This limits the total memory "
"used for prefetching data blocks across all concurrent "
"MultiScan ReadSets.");
DEFINE_int32(num_dbs, 1,
"Number of DB instances to run in parallel. "
"For num_dbs=1: --db is the DB path directly (default). "
"For num_dbs>1: --db is a parent directory; db_stress "
"auto-creates db_0/, db_1/, ... subdirs underneath "
"(same for --expected_values_dir and secondaries). "
"Per-DB flags: threads, max_key, ops_per_thread, reopen, "
"column_families, and all DB options. "
"Shared across all instances: background env threads "
"(compaction, flush pool), cache, write_buffer_manager, "
"compressed_secondary_cache, rate_limiter, and "
"compaction_thread_pool_adjust_interval.");
#endif // GFLAGS
+16
View File
@@ -7,6 +7,7 @@
#include <cstdint>
#include "db_stress_tool/db_stress_test_base.h"
#include "file/file_util.h"
#include "rocksdb/file_system.h"
#include "util/coding_lean.h"
@@ -15,6 +16,21 @@ namespace ROCKSDB_NAMESPACE {
#ifdef GFLAGS
DbStressListener::DbStressListener(
const std::string& db_name, const std::vector<DbPath>& db_paths,
const std::vector<ColumnFamilyDescriptor>& column_families,
SharedState* shared)
: db_name_(db_name),
db_paths_(db_paths),
column_families_(column_families),
num_pending_file_creations_(0),
unique_ids_(shared->GetStressTest()->GetExpectedValuesDir().empty()
? db_name
: shared->GetStressTest()->GetExpectedValuesDir()),
shared_(shared),
db_fault_injection_fs_(shared->GetStressTest()->GetDbFaultInjectionFs()) {
}
UniqueIdVerifier::UniqueIdVerifier(const std::string& dir)
: path_(dir + "/.unique_ids") {
// We expect such a small number of files generated during this test
+174 -41
View File
@@ -6,7 +6,9 @@
#ifdef GFLAGS
#pragma once
#include <cinttypes>
#include <mutex>
#include <unordered_map>
#include <unordered_set>
#include "db_stress_tool/db_stress_compaction_service.h"
@@ -19,13 +21,12 @@
#include "rocksdb/listener.h"
#include "rocksdb/table_properties.h"
#include "rocksdb/unique_id.h"
#include "util/atomic.h"
#include "util/gflags_compat.h"
#include "util/random.h"
#include "utilities/fault_injection_fs.h"
DECLARE_int32(compact_files_one_in);
extern std::shared_ptr<ROCKSDB_NAMESPACE::FaultInjectionTestFS> fault_fs_guard;
namespace ROCKSDB_NAMESPACE {
// Verify across process executions that all seen IDs are unique
@@ -55,27 +56,22 @@ class DbStressListener : public EventListener {
DbStressListener(const std::string& db_name,
const std::vector<DbPath>& db_paths,
const std::vector<ColumnFamilyDescriptor>& column_families,
SharedState* shared)
: db_name_(db_name),
db_paths_(db_paths),
column_families_(column_families),
num_pending_file_creations_(0),
unique_ids_(FLAGS_expected_values_dir.empty()
? db_name
: FLAGS_expected_values_dir),
shared_(shared) {}
SharedState* shared);
const char* Name() const override { return kClassName(); }
static const char* kClassName() { return "DBStressListener"; }
~DbStressListener() override { assert(num_pending_file_creations_ == 0); }
void OnDBShutdownBegin(DB* /*db*/) override { shutting_down_.Store(true); }
void OnFlushCompleted(DB* /*db*/, const FlushJobInfo& info) override {
assert(IsValidColumnFamilyName(info.cf_name));
VerifyFilePath(info.file_path);
// pretending doing some work here
RandomSleep();
if (fault_fs_guard) {
fault_fs_guard->DisableAllThreadLocalErrorInjection();
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableAllThreadLocalErrorInjection();
}
shared_->SetPersistedSeqno(info.largest_seqno);
}
@@ -83,46 +79,118 @@ class DbStressListener : public EventListener {
void OnFlushBegin(DB* /*db*/,
const FlushJobInfo& /*flush_job_info*/) override {
RandomSleep();
if (fault_fs_guard) {
fault_fs_guard->SetThreadLocalErrorContext(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kRead, static_cast<uint32_t>(FLAGS_seed),
FLAGS_read_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->SetThreadLocalErrorContext(
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kWrite, static_cast<uint32_t>(FLAGS_seed),
FLAGS_write_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kWrite);
fault_fs_guard->SetThreadLocalErrorContext(
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kMetadataRead,
static_cast<uint32_t>(FLAGS_seed), FLAGS_metadata_read_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
fault_fs_guard->SetThreadLocalErrorContext(
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kMetadataWrite,
static_cast<uint32_t>(FLAGS_seed), FLAGS_metadata_write_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
}
void OnTableFileDeleted(const TableFileDeletionInfo& /*info*/) override {
void OnTableFileDeleted(const TableFileDeletionInfo& info) override {
// A file must not be deleted while it is still between
// OnCompactionBegin and OnCompactionPreCommit (i.e. actively being
// compacted with being_compacted == true). (Perhaps more realistically,
// this is checking for failure to call OnCompactionPreCommit.)
//
// During shutdown, compaction notifications are skipped so tracking
// state may be stale -- skip the check.
if (!shutting_down_.Load()) {
std::lock_guard<std::mutex> lock(compacting_files_mu_);
uint64_t file_number = FileNumberFromPath(info.file_path);
if (file_number != 0 &&
compacting_files_.find(file_number) != compacting_files_.end()) {
fprintf(stderr,
"OnTableFileDeleted for file tracked as being compacted "
"(between Begin and PreCommit): file_number=%" PRIu64 "\n",
file_number);
fflush(stderr);
std::abort();
}
}
RandomSleep();
}
void OnCompactionBegin(DB* /*db*/, const CompactionJobInfo& /*ci*/) override {
void OnCompactionBegin(DB* /*db*/, const CompactionJobInfo& ci) override {
// Sanity check inspired by a Meta-internal check: the same input file must
// not be in two concurrent compactions. Inserting input file numbers into a
// shared set on Begin and removing them on PreCommit (rather than on
// Completed) is what exercises the new OnCompactionPreCommit
// callback. Removing on Completed would race with another picker that
// grabs the same file as soon as ReleaseCompactionFiles flips
// FileMetaData::being_compacted back to false; PreCommit runs
// while being_compacted is still true and so closes that race.
{
std::lock_guard<std::mutex> lock(compacting_files_mu_);
for (const auto& info : ci.input_file_infos) {
auto [_, inserted] = compacting_files_.insert(info.file_number);
if (!inserted) {
fprintf(stderr,
"Concurrent compaction of SST file detected: cf=%s "
"file_number=%" PRIu64 "\n",
ci.cf_name.c_str(), info.file_number);
fflush(stderr);
std::abort();
}
}
}
RandomSleep();
}
void OnCompactionPreCommit(DB* /*db*/, const CompactionJobInfo& ci) override {
// Pair with OnCompactionBegin's bookkeeping: move files from
// compacting_files_ and record the job for Completed verification.
{
std::lock_guard<std::mutex> lock(compacting_files_mu_);
std::unordered_set<uint64_t> job_files;
for (const auto& info : ci.input_file_infos) {
size_t erased = compacting_files_.erase(info.file_number);
if (erased != 1) {
fprintf(stderr,
"OnCompactionPreCommit for file not in tracking set: "
"cf=%s file_number=%" PRIu64 "\n",
ci.cf_name.c_str(), info.file_number);
fflush(stderr);
std::abort();
}
job_files.insert(info.file_number);
}
auto [_, inserted] =
precommitted_jobs_.emplace(ci.job_id, std::move(job_files));
if (!inserted) {
fprintf(stderr, "OnCompactionPreCommit: duplicate job_id %d\n",
ci.job_id);
fflush(stderr);
std::abort();
}
}
RandomSleep();
}
@@ -135,49 +203,77 @@ class DbStressListener : public EventListener {
for (const auto& file_path : ci.output_files) {
VerifyFilePath(file_path);
}
// Verify that OnCompactionPreCommit fired before OnCompactionCompleted
// for the same job, with matching input files.
{
std::lock_guard<std::mutex> lock(compacting_files_mu_);
auto it = precommitted_jobs_.find(ci.job_id);
if (it == precommitted_jobs_.end()) {
fprintf(stderr,
"OnCompactionCompleted without prior OnCompactionPreCommit: "
"cf=%s job_id=%d\n",
ci.cf_name.c_str(), ci.job_id);
fflush(stderr);
std::abort();
}
// Verify input file sets match between PreCommit and Completed.
for (const auto& info : ci.input_file_infos) {
if (it->second.find(info.file_number) == it->second.end()) {
fprintf(stderr,
"OnCompactionCompleted: input file %" PRIu64
" not in PreCommit set for job_id=%d\n",
info.file_number, ci.job_id);
fflush(stderr);
std::abort();
}
}
precommitted_jobs_.erase(it);
}
// pretending doing some work here
RandomSleep();
}
void OnSubcompactionBegin(const SubcompactionJobInfo& /* si */) override {
if (fault_fs_guard) {
fault_fs_guard->SetThreadLocalErrorContext(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kRead, static_cast<uint32_t>(FLAGS_seed),
FLAGS_read_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->SetThreadLocalErrorContext(
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kWrite, static_cast<uint32_t>(FLAGS_seed),
FLAGS_write_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kWrite);
fault_fs_guard->SetThreadLocalErrorContext(
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kMetadataRead,
static_cast<uint32_t>(FLAGS_seed), FLAGS_metadata_read_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
fault_fs_guard->SetThreadLocalErrorContext(
db_fault_injection_fs_->SetThreadLocalErrorContext(
FaultInjectionIOType::kMetadataWrite,
static_cast<uint32_t>(FLAGS_seed), FLAGS_metadata_write_fault_one_in,
FLAGS_inject_error_severity == 1 /* retryable */,
FLAGS_inject_error_severity == 2 /* has_data_loss*/);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
}
void OnSubcompactionCompleted(const SubcompactionJobInfo& /* si */) override {
if (fault_fs_guard) {
fault_fs_guard->DisableAllThreadLocalErrorInjection();
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableAllThreadLocalErrorInjection();
}
}
@@ -270,11 +366,12 @@ class DbStressListener : public EventListener {
Status /* bg_error */,
bool* /* auto_recovery */) override {
RandomSleep();
if (FLAGS_error_recovery_with_no_fault_injection && fault_fs_guard) {
fault_fs_guard->DisableAllThreadLocalErrorInjection();
if (FLAGS_error_recovery_with_no_fault_injection &&
db_fault_injection_fs_) {
db_fault_injection_fs_->DisableAllThreadLocalErrorInjection();
// TODO(hx235): only exempt the flush thread during error recovery instead
// of all the flush threads from error injection
fault_fs_guard->SetIOActivitiesExcludedFromFaultInjection(
db_fault_injection_fs_->SetIOActivitiesExcludedFromFaultInjection(
{Env::IOActivity::kFlush});
}
}
@@ -282,9 +379,10 @@ class DbStressListener : public EventListener {
void OnErrorRecoveryEnd(
const BackgroundErrorRecoveryInfo& /*info*/) override {
RandomSleep();
if (FLAGS_error_recovery_with_no_fault_injection && fault_fs_guard) {
fault_fs_guard->EnableAllThreadLocalErrorInjection();
fault_fs_guard->SetIOActivitiesExcludedFromFaultInjection({});
if (FLAGS_error_recovery_with_no_fault_injection &&
db_fault_injection_fs_) {
db_fault_injection_fs_->EnableAllThreadLocalErrorInjection();
db_fault_injection_fs_->SetIOActivitiesExcludedFromFaultInjection({});
}
}
@@ -375,8 +473,43 @@ class DbStressListener : public EventListener {
std::atomic<int> num_pending_file_creations_;
UniqueIdVerifier unique_ids_;
SharedState* shared_;
std::shared_ptr<FaultInjectionTestFS> db_fault_injection_fs_;
mutable std::mutex bg_pressure_mu_;
BackgroundJobPressure last_bg_pressure_;
// Files (by file_number) currently in flight from OnCompactionBegin to
// OnCompactionPreCommit. Used to detect concurrent compaction of
// the same SST file -- the bug fixed by the OnCompactionPreCommit
// callback. Protected by compacting_files_mu_.
std::mutex compacting_files_mu_;
std::unordered_set<uint64_t> compacting_files_;
// Set when DBImpl begins shutdown to suppress false positives from stale
// tracking when compaction callbacks are skipped during shutdown.
Atomic<bool> shutting_down_{false};
// Jobs that have passed OnCompactionPreCommit but not yet
// OnCompactionCompleted. Maps job_id -> input file numbers.
// Used to verify Begin -> PreCommit -> Completed ordering per job.
// Protected by compacting_files_mu_.
std::unordered_map<int, std::unordered_set<uint64_t>> precommitted_jobs_;
// Extract file number from a file path like "/path/to/000123.sst".
// Returns 0 if the path cannot be parsed.
static uint64_t FileNumberFromPath(const std::string& file_path) {
size_t pos = file_path.find_last_of('/');
// Avoid copying file_path when no '/' separator is found
std::string file_name_buf;
if (pos != std::string::npos) {
file_name_buf = file_path.substr(pos + 1);
}
const std::string& file_name =
(pos == std::string::npos) ? file_path : file_name_buf;
uint64_t file_number = 0;
FileType file_type;
if (ParseFileName(file_name, &file_number, &file_type) &&
file_type == kTableFile) {
return file_number;
}
return 0;
}
};
} // namespace ROCKSDB_NAMESPACE
#endif // GFLAGS
+102
View File
@@ -11,7 +11,109 @@
#ifdef GFLAGS
#include "db_stress_tool/db_stress_shared_state.h"
#include "db_stress_tool/db_stress_test_base.h"
namespace ROCKSDB_NAMESPACE {
thread_local bool SharedState::ignore_read_error;
SharedState::SharedState(Env* /*env*/, StressTest* stress_test)
: cv_(&mu_),
seed_(static_cast<uint32_t>(FLAGS_seed)),
max_key_(FLAGS_max_key),
log2_keys_per_lock_(static_cast<uint32_t>(FLAGS_log2_keys_per_lock)),
num_threads_(0),
num_initialized_(0),
num_populated_(0),
vote_reopen_(0),
num_done_(0),
start_(false),
start_verify_(false),
num_bg_threads_(0),
should_stop_bg_thread_(false),
bg_thread_finished_(0),
stress_test_(stress_test),
verification_failure_(false),
should_stop_test_(false),
no_overwrite_ids_(GenerateNoOverwriteIds()),
expected_state_manager_(nullptr),
printing_verification_results_(false),
start_timestamp_(Env::Default()->NowNanos()) {
Status status;
// TODO: We should introduce a way to explicitly disable verification
// during shutdown. When that is disabled and FLAGS_expected_values_dir
// is empty (disabling verification at startup), we can skip tracking
// expected state. Only then should we permit bypassing the below feature
// compatibility checks.
const auto& expected_values_dir = stress_test_->GetExpectedValuesDir();
if (!expected_values_dir.empty()) {
if (!std::atomic<uint32_t>{}.is_lock_free() ||
!std::atomic<uint64_t>{}.is_lock_free()) {
std::ostringstream status_s;
status_s << "Cannot use --expected_values_dir on platforms without "
"lock-free "
<< (!std::atomic<uint32_t>{}.is_lock_free()
? "std::atomic<uint32_t>"
: "std::atomic<uint64_t>");
status = Status::InvalidArgument(status_s.str());
}
if (status.ok() && FLAGS_clear_column_family_one_in > 0) {
status = Status::InvalidArgument(
"Cannot use --expected_values_dir on when "
"--clear_column_family_one_in is greater than zero.");
}
}
if (status.ok()) {
if (expected_values_dir.empty()) {
expected_state_manager_.reset(
new AnonExpectedStateManager(FLAGS_max_key, FLAGS_column_families));
} else {
expected_state_manager_.reset(new FileExpectedStateManager(
FLAGS_max_key, FLAGS_column_families, expected_values_dir));
}
status = expected_state_manager_->Open();
}
if (!status.ok()) {
fprintf(stderr, "Failed setting up expected state with error: %s\n",
status.ToString().c_str());
exit(1); // NOLINT(concurrency-mt-unsafe)
}
if (FLAGS_test_batches_snapshots) {
fprintf(stdout, "No lock creation because test_batches_snapshots set\n");
return;
}
long num_locks = static_cast<long>(max_key_ >> log2_keys_per_lock_);
if (max_key_ & ((1 << log2_keys_per_lock_) - 1)) {
num_locks++;
}
fprintf(stdout, "Creating %ld locks\n", num_locks * FLAGS_column_families);
key_locks_.resize(FLAGS_column_families);
for (int i = 0; i < FLAGS_column_families; ++i) {
key_locks_[i].reset(new port::Mutex[num_locks]);
}
if (FLAGS_read_fault_one_in || FLAGS_metadata_read_fault_one_in) {
#ifdef NDEBUG
// Unsupported in release mode because it relies on
// `IGNORE_STATUS_IF_ERROR` to distinguish faults not expected to lead to
// failure.
fprintf(stderr,
"Cannot set nonzero value for --read_fault_one_in in "
"release mode.");
exit(1); // NOLINT(concurrency-mt-unsafe)
#else // NDEBUG
SyncPoint::GetInstance()->SetCallBack("FaultInjectionIgnoreError",
IgnoreReadErrorCallback);
SyncPoint::GetInstance()->EnableProcessing();
#endif // NDEBUG
}
}
bool SharedState::ShouldVerifyAtBeginning() const {
return !stress_test_->GetExpectedValuesDir().empty();
}
} // namespace ROCKSDB_NAMESPACE
#endif // GFLAGS
+2 -97
View File
@@ -24,7 +24,6 @@ DECLARE_uint64(log2_keys_per_lock);
DECLARE_int32(threads);
DECLARE_int32(column_families);
DECLARE_int32(nooverwritepercent);
DECLARE_string(expected_values_dir);
DECLARE_int32(clear_column_family_one_in);
DECLARE_bool(test_batches_snapshots);
DECLARE_int32(compaction_thread_pool_adjust_interval);
@@ -78,99 +77,7 @@ class SharedState {
// for those calls
static thread_local bool ignore_read_error;
SharedState(Env* /*env*/, StressTest* stress_test)
: cv_(&mu_),
seed_(static_cast<uint32_t>(FLAGS_seed)),
max_key_(FLAGS_max_key),
log2_keys_per_lock_(static_cast<uint32_t>(FLAGS_log2_keys_per_lock)),
num_threads_(0),
num_initialized_(0),
num_populated_(0),
vote_reopen_(0),
num_done_(0),
start_(false),
start_verify_(false),
num_bg_threads_(0),
should_stop_bg_thread_(false),
bg_thread_finished_(0),
stress_test_(stress_test),
verification_failure_(false),
should_stop_test_(false),
no_overwrite_ids_(GenerateNoOverwriteIds()),
expected_state_manager_(nullptr),
printing_verification_results_(false),
start_timestamp_(Env::Default()->NowNanos()) {
Status status;
// TODO: We should introduce a way to explicitly disable verification
// during shutdown. When that is disabled and FLAGS_expected_values_dir
// is empty (disabling verification at startup), we can skip tracking
// expected state. Only then should we permit bypassing the below feature
// compatibility checks.
if (!FLAGS_expected_values_dir.empty()) {
if (!std::atomic<uint32_t>{}.is_lock_free() ||
!std::atomic<uint64_t>{}.is_lock_free()) {
std::ostringstream status_s;
status_s << "Cannot use --expected_values_dir on platforms without "
"lock-free "
<< (!std::atomic<uint32_t>{}.is_lock_free()
? "std::atomic<uint32_t>"
: "std::atomic<uint64_t>");
status = Status::InvalidArgument(status_s.str());
}
if (status.ok() && FLAGS_clear_column_family_one_in > 0) {
status = Status::InvalidArgument(
"Cannot use --expected_values_dir on when "
"--clear_column_family_one_in is greater than zero.");
}
}
if (status.ok()) {
if (FLAGS_expected_values_dir.empty()) {
expected_state_manager_.reset(
new AnonExpectedStateManager(FLAGS_max_key, FLAGS_column_families));
} else {
expected_state_manager_.reset(new FileExpectedStateManager(
FLAGS_max_key, FLAGS_column_families, FLAGS_expected_values_dir));
}
status = expected_state_manager_->Open();
}
if (!status.ok()) {
fprintf(stderr, "Failed setting up expected state with error: %s\n",
status.ToString().c_str());
exit(1);
}
if (FLAGS_test_batches_snapshots) {
fprintf(stdout, "No lock creation because test_batches_snapshots set\n");
return;
}
long num_locks = static_cast<long>(max_key_ >> log2_keys_per_lock_);
if (max_key_ & ((1 << log2_keys_per_lock_) - 1)) {
num_locks++;
}
fprintf(stdout, "Creating %ld locks\n", num_locks * FLAGS_column_families);
key_locks_.resize(FLAGS_column_families);
for (int i = 0; i < FLAGS_column_families; ++i) {
key_locks_[i].reset(new port::Mutex[num_locks]);
}
if (FLAGS_read_fault_one_in || FLAGS_metadata_read_fault_one_in) {
#ifdef NDEBUG
// Unsupported in release mode because it relies on
// `IGNORE_STATUS_IF_ERROR` to distinguish faults not expected to lead to
// failure.
fprintf(stderr,
"Cannot set nonzero value for --read_fault_one_in in "
"release mode.");
exit(1);
#else // NDEBUG
SyncPoint::GetInstance()->SetCallBack("FaultInjectionIgnoreError",
IgnoreReadErrorCallback);
SyncPoint::GetInstance()->EnableProcessing();
#endif // NDEBUG
}
}
SharedState(Env* env, StressTest* stress_test);
~SharedState() {
#ifndef NDEBUG
@@ -430,9 +337,7 @@ class SharedState {
return bg_thread_finished_ == num_bg_threads_;
}
bool ShouldVerifyAtBeginning() const {
return !FLAGS_expected_values_dir.empty();
}
bool ShouldVerifyAtBeginning() const;
bool PrintingVerificationResults() {
bool tmp = false;
+4
View File
@@ -15,6 +15,7 @@
#include "rocksdb/statistics.h"
#include "rocksdb/system_clock.h"
#include "util/gflags_compat.h"
#include "util/mutexlock.h"
#include "util/random.h"
DECLARE_bool(histogram);
@@ -181,6 +182,9 @@ class Stats {
double rate = bytes_mb / elapsed;
double throughput = (double)done_ / elapsed;
// Lock stdout so multi-DB reports don't interleave.
static port::Mutex report_mutex;
MutexLock l(&report_mutex);
fprintf(stdout, "%-12s: ", name);
fprintf(stdout, "%.3f micros/op %ld ops/sec\n", seconds_ * 1e6 / done_,
(long)throughput);
File diff suppressed because it is too large. Load diff
+79 -14
View File
@@ -13,6 +13,7 @@
#include "db_stress_tool/db_stress_common.h"
#include "db_stress_tool/db_stress_shared_state.h"
#include "env/composite_env_wrapper.h"
#include "rocksdb/experimental.h"
#include "rocksdb/user_defined_index.h"
#include "utilities/fault_injection_fs.h"
@@ -22,6 +23,7 @@ class SystemClock;
class Transaction;
class TransactionDB;
class OptimisticTransactionDB;
class DbStressFSWrapper;
struct TransactionDBOptions;
using experimental::SstQueryFilterConfigsManager;
@@ -40,11 +42,26 @@ class StressTest {
// from optimistic transactions when conflict detection retries are exhausted.
static bool IsExpectedTxnError(const Status& s);
StressTest();
StressTest(int db_index, const std::string& db_path,
const std::string& ev_path, const std::string& sec_path);
virtual ~StressTest() {}
std::shared_ptr<Cache> NewCache(size_t capacity, int32_t num_shard_bits);
const std::string& GetDbLabel() const;
const std::string& GetDbPath() const;
const std::string& GetExpectedValuesDir() const;
const std::string& GetSecondariesBase() const;
// See db_fault_injection_fs_ member.
std::shared_ptr<FaultInjectionTestFS> GetDbFaultInjectionFs() const {
return db_fault_injection_fs_;
}
// See db_env_ member.
Env* GetDbEnv() const;
static std::shared_ptr<Cache> NewCache(size_t capacity,
int32_t num_shard_bits);
static std::vector<std::string> GetBlobCompressionTags();
@@ -71,6 +88,11 @@ class StressTest {
Options GetOptions(int cf_id);
void CleanUp();
private:
void InitializeListenersForOpen(
SharedState* shared,
const std::vector<ColumnFamilyDescriptor>& cf_descriptors);
protected:
static int GetMinInjectedErrorCount(int error_count_1, int error_count_2) {
if (error_count_1 > 0 && error_count_2 > 0) {
@@ -84,39 +106,34 @@ class StressTest {
}
}
void UpdateIfInitialWriteFails(Env* db_stress_env, const Status& write_s,
void UpdateIfInitialWriteFails(Env* env, const Status& write_s,
Status* initial_write_s,
bool* initial_wal_write_may_succeed,
uint64_t* wait_for_recover_start_time,
bool commit_bypass_memtable = false) {
assert(db_stress_env && initial_write_s && initial_wal_write_may_succeed &&
assert(env && initial_write_s && initial_wal_write_may_succeed &&
wait_for_recover_start_time);
// Only update `initial_write_s`, `initial_wal_write_may_succeed` when the
// first write fails
if (!write_s.ok() && (*initial_write_s).ok()) {
*initial_write_s = write_s;
// With commit_bypass_memtable, we create a new WAL after WAL write
// succeeds, that wal creation may fail due to injected error. So the
// initial wal write may succeed even if status is failed to write to wal
*initial_wal_write_may_succeed =
commit_bypass_memtable ||
!FaultInjectionTestFS::IsFailedToWriteToWALError(*initial_write_s);
*wait_for_recover_start_time = db_stress_env->NowMicros();
*wait_for_recover_start_time = env->NowMicros();
}
}
void PrintWriteRecoveryWaitTimeIfNeeded(Env* db_stress_env,
void PrintWriteRecoveryWaitTimeIfNeeded(Env* env,
const Status& initial_write_s,
bool initial_wal_write_may_succeed,
uint64_t wait_for_recover_start_time,
const std::string& thread_name) {
assert(db_stress_env);
assert(env);
bool waited_for_recovery = !initial_write_s.ok() &&
IsErrorInjectedAndRetryable(initial_write_s) &&
initial_wal_write_may_succeed;
if (waited_for_recovery) {
uint64_t elapsed_sec =
(db_stress_env->NowMicros() - wait_for_recover_start_time) / 1000000;
(env->NowMicros() - wait_for_recover_start_time) / 1000000;
if (elapsed_sec > 10) {
fprintf(stdout,
"%s thread slept to wait for write recovery for "
@@ -303,6 +320,35 @@ class StressTest {
kLastOpSeekToLast
};
// Enum used to track MANIFEST verification mode during DB reopen
enum ManifestVerifyMode {
MANIFEST_VERIFY_NONE,
// MANIFEST file should be reused (same file number), CURRENT should not
// change. Used when reuse_manifest_on_open=1. Warnings are logged on
// failure but test continues.
MANIFEST_VERIFY_REUSE,
// MANIFEST file should be reused AND no recovery writes should occur.
// Used when both reuse_manifest_on_open=1 and
// optimize_manifest_for_recovery=1. Warnings are logged on failure but test
// continues.
MANIFEST_VERIFY_NO_WRITE,
// Strict verification: MANIFEST file must be reused with ZERO writes
// (complete avoidance). Used when ALL conditions for complete avoidance
// are met. Failures are FATAL and will terminate the test.
// Conditions for STRICT mode:
// - Both reuse_manifest_on_open=1 and optimize_manifest_for_recovery=1
// - Not in best_efforts_recovery mode
// - avoid_flush_during_recovery=true (no flush during recovery)
// - write_dbid_to_manifest=0 (no DB_ID write on open)
// - metadata_write_fault_one_in=0 (no fault injection)
// - open_metadata_write_fault_one_in=0 (no fault injection)
// - MANIFEST not corrupted, not at size limit
// Note: avoid_flush_during_shutdown is NOT required. If it leaves data
// in WAL but avoid_flush_during_recovery=true prevents flushing it,
// MANIFEST still won't be written.
MANIFEST_VERIFY_STRICT
};
// Compare the two iterator, iter and cmp_iter are in the same position,
// unless iter might be made invalidate or undefined because of
// upper or lower bounds, or prefix extractor.
@@ -413,7 +459,20 @@ class StressTest {
void CleanUpColumnFamilies();
std::shared_ptr<Cache> cache_;
void RecordManifestStateBeforeReopen();
void VerifyManifestNotRewritten();
int db_index_;
std::string db_label_;
std::string db_path_;
std::string expected_values_path_;
std::string secondaries_path_;
// Wraps raw_env->GetFileSystem(). See DbStressFSWrapper.
std::shared_ptr<DbStressFSWrapper> db_stress_fs_;
// Wraps db_stress_fs_ when NeedsFaultInjection(). Null otherwise.
std::shared_ptr<FaultInjectionTestFS> db_fault_injection_fs_;
// Wraps the outermost FS above. Set as options_.env for all DB I/O.
std::unique_ptr<CompositeEnvWrapper> db_env_;
std::shared_ptr<Cache> compressed_cache_;
std::shared_ptr<const FilterPolicy> filter_policy_;
std::unique_ptr<DB> db_owner_;
@@ -439,6 +498,12 @@ class StressTest {
std::unique_ptr<DB> secondary_db_;
std::vector<ColumnFamilyHandle*> secondary_cfhs_;
bool is_db_stopped_;
// MANIFEST verification state for reopen
ManifestVerifyMode manifest_verify_mode_;
uint64_t manifest_file_number_before_reopen_;
uint64_t manifest_file_size_before_reopen_;
std::string current_file_content_before_reopen_;
};
// Load options from OPTIONS file and populate `options`.
+213 -106
View File
@@ -22,6 +22,7 @@
#ifdef GFLAGS
#include <iostream>
#include <thread>
#include "db_stress_tool/db_stress_common.h"
#include "db_stress_tool/db_stress_driver.h"
@@ -33,11 +34,79 @@
namespace ROCKSDB_NAMESPACE {
namespace {
static std::shared_ptr<ROCKSDB_NAMESPACE::Env> env_guard;
static std::shared_ptr<ROCKSDB_NAMESPACE::Env> env_wrapper_guard;
static std::shared_ptr<ROCKSDB_NAMESPACE::Env> legacy_env_wrapper_guard;
static std::shared_ptr<ROCKSDB_NAMESPACE::CompositeEnvWrapper>
dbsl_env_wrapper_guard;
static std::shared_ptr<CompositeEnvWrapper> fault_env_guard;
// Raw pointers for signal-safe crash callback. Signal handlers can only
// access file-static/global variables; can't capture StressTest instances.
static std::vector<ROCKSDB_NAMESPACE::FaultInjectionTestFS*>
fault_fs_for_crash_report;
int ValidateNumDbsFlags() {
if (FLAGS_num_dbs < 1) {
fprintf(stderr, "Error: --num_dbs must be >= 1\n");
return 1;
}
if (FLAGS_num_dbs > 1) {
if (FLAGS_clear_column_family_one_in > 0) {
fprintf(stderr,
"Error: --num_dbs > 1 incompatible with "
"--clear_column_family_one_in\n");
return 1;
}
if (FLAGS_test_multi_ops_txns) {
fprintf(stderr,
"Error: --num_dbs > 1 incompatible with "
"--test_multi_ops_txns\n");
return 1;
}
}
return 0;
}
int DestroyAllDbs() {
bool all_ok = true;
const int num_dbs = FLAGS_num_dbs;
auto destroy_one = [&](const std::string& db_path) {
Status s = DbStressDestroyDb(db_path);
if (s.ok()) {
fprintf(stdout, "Successfully destroyed db at %s\n", db_path.c_str());
} else {
fprintf(stderr, "Failed to destroy db at %s: %s\n", db_path.c_str(),
s.ToString().c_str());
all_ok = false;
}
};
if (num_dbs == 1) {
destroy_one(FLAGS_db);
} else {
for (int i = 0; i < num_dbs; i++) {
destroy_one(FLAGS_db + "/db_" + std::to_string(i));
}
DestroyDir(raw_env, FLAGS_db);
}
return all_ok ? 0 : 1;
}
void RegisterCrashCallbacks(
const std::vector<std::unique_ptr<StressTest>>& stress_tests, int num_dbs) {
fault_fs_for_crash_report.resize(num_dbs, nullptr);
bool any_fault_fs = false;
for (int i = 0; i < num_dbs; i++) {
fault_fs_for_crash_report[i] =
stress_tests[i]->GetDbFaultInjectionFs().get();
if (fault_fs_for_crash_report[i]) {
any_fault_fs = true;
}
}
if (any_fault_fs) {
port::RegisterCrashCallback([]() {
for (auto* fs : fault_fs_for_crash_report) {
if (fs) {
fs->PrintRecentInjectedErrors();
}
}
});
}
}
int ReturnFlagValidationError(const char* message) {
std::cerr << "Error: " << message << '\n';
@@ -67,8 +136,6 @@ int db_stress_tool(int argc, char** argv) {
StringToCompressionType(FLAGS_bottommost_compression_type.c_str());
checksum_type_e = StringToChecksumType(FLAGS_checksum_type.c_str());
Env* raw_env;
int env_opts = !FLAGS_env_uri.empty() + !FLAGS_fs_uri.empty();
if (env_opts > 1) {
fprintf(stderr, "Error: --env_uri and --fs_uri are mutually exclusive\n");
@@ -82,57 +149,10 @@ int db_stress_tool(int argc, char** argv) {
s.ToString().c_str());
exit(1);
}
dbsl_env_wrapper_guard = std::make_shared<CompositeEnvWrapper>(raw_env);
db_stress_listener_env = dbsl_env_wrapper_guard.get();
if (FLAGS_open_metadata_read_fault_one_in ||
FLAGS_open_metadata_write_fault_one_in || FLAGS_open_read_fault_one_in ||
FLAGS_open_write_fault_one_in || FLAGS_metadata_read_fault_one_in ||
FLAGS_metadata_write_fault_one_in || FLAGS_read_fault_one_in ||
FLAGS_write_fault_one_in || FLAGS_sync_fault_injection) {
FaultInjectionTestFS* fs =
new FaultInjectionTestFS(raw_env->GetFileSystem());
fault_fs_guard.reset(fs);
// Info logs are debugging artifacts, so exclude them from fault injection
// and keep error accounting focused on DB data and metadata.
fault_fs_guard->SetFileTypesExcludedFromFaultInjection(
{FileType::kInfoLogFile});
// Set it to direct writable here to initially bypass any fault injection
// during DB open This will correspondingly be overwritten in
// StressTest::Open() for open fault injection and in RunStressTestImpl()
// for proper fault injection setup.
fault_fs_guard->SetFilesystemDirectWritable(true);
fault_env_guard =
std::make_shared<CompositeEnvWrapper>(raw_env, fault_fs_guard);
raw_env = fault_env_guard.get();
// Register a crash callback so that recently injected errors are
// printed to stderr when the process crashes (SIGABRT, SIGSEGV, etc.).
// This helps diagnose stress test failures caused by fault injection.
port::RegisterCrashCallback([]() {
if (fault_fs_guard) {
fault_fs_guard->PrintRecentInjectedErrors();
}
});
}
auto db_stress_fs =
std::make_shared<DbStressFSWrapper>(raw_env->GetFileSystem());
env_wrapper_guard =
std::make_shared<CompositeEnvWrapper>(raw_env, db_stress_fs);
db_stress_env = env_wrapper_guard.get();
// Handle --destroy_db_and_exit early, before other option validation
// Handle --destroy_db_and_exit early
if (FLAGS_destroy_db_and_exit) {
s = DbStressDestroyDb(FLAGS_db);
if (s.ok()) {
fprintf(stdout, "Successfully destroyed db at %s\n", FLAGS_db.c_str());
return 0;
} else {
fprintf(stderr, "Failed to destroy db at %s: %s\n", FLAGS_db.c_str(),
s.ToString().c_str());
return 1;
}
return DestroyAllDbs();
}
// Handle --delete_dir_and_exit early, before other option validation
@@ -149,14 +169,21 @@ int db_stress_tool(int argc, char** argv) {
}
}
{
int rc = ValidateNumDbsFlags();
if (rc != 0) {
return rc;
}
}
FLAGS_rep_factory = StringToRepFactory(FLAGS_memtablerep.c_str());
// The number of background threads should be at least as much the
// max number of concurrent compactions.
db_stress_env->SetBackgroundThreads(FLAGS_max_background_compactions,
ROCKSDB_NAMESPACE::Env::Priority::LOW);
db_stress_env->SetBackgroundThreads(FLAGS_num_bottom_pri_threads,
ROCKSDB_NAMESPACE::Env::Priority::BOTTOM);
raw_env->SetBackgroundThreads(FLAGS_max_background_compactions,
ROCKSDB_NAMESPACE::Env::Priority::LOW);
raw_env->SetBackgroundThreads(FLAGS_num_bottom_pri_threads,
ROCKSDB_NAMESPACE::Env::Priority::BOTTOM);
if (FLAGS_prefixpercent > 0 && FLAGS_prefix_size < 0) {
fprintf(stderr,
"Error: prefixpercent is non-zero while prefix_size is "
@@ -356,42 +383,75 @@ int db_stress_tool(int argc, char** argv) {
// Choose a location for the test database if none given with --db=<path>
if (FLAGS_db.empty()) {
std::string default_db_path;
db_stress_env->GetTestDirectory(&default_db_path);
raw_env->GetTestDirectory(&default_db_path);
default_db_path += "/dbstress";
FLAGS_db = default_db_path;
}
// Now that FLAGS_db is resolved, set the fault injection log file path
// so that PrintAll() writes to a file instead of stderr (signal-safe).
// Store the log in TEST_TMPDIR (outside the DB directory) so it survives
// DB reopen (which cleans untracked files) and gets included in the
// sandcastle db.tar.gz artifact for post-failure analysis.
if (fault_fs_guard) {
std::string log_dir;
const char* test_tmpdir = getenv("TEST_TMPDIR");
if (test_tmpdir && test_tmpdir[0] != '\0') {
log_dir = test_tmpdir;
} else {
log_dir = "/tmp";
}
std::string log_path = log_dir + "/fault_injection_" +
std::to_string(getpid()) + "_" +
std::to_string(time(nullptr)) + ".log";
fault_fs_guard->SetInjectedErrorLogPath(log_path);
}
if ((FLAGS_test_secondary || FLAGS_continuous_verification_interval > 0) &&
FLAGS_secondaries_base.empty()) {
std::string default_secondaries_path;
db_stress_env->GetTestDirectory(&default_secondaries_path);
default_secondaries_path += "/dbstress_secondaries";
s = db_stress_env->CreateDirIfMissing(default_secondaries_path);
// For num_dbs=1: --db and --expected_values_dir are paths used as-is.
// For num_dbs>1: they are parent directories; C++ creates db_0/, db_1/, ...
// subdirs underneath. DB::Open(create_if_missing) creates each DB dir.
// Python (db_crashtest.py) also creates EV dirs; C++ creates them here as a
// fallback for direct CLI usage.
const int num_dbs = FLAGS_num_dbs;
if (num_dbs > 1) {
s = raw_env->CreateDirIfMissing(FLAGS_db);
if (!s.ok()) {
fprintf(stderr, "Failed to create directory %s: %s\n",
default_secondaries_path.c_str(), s.ToString().c_str());
fprintf(stderr, "Failed to create directory %s: %s\n", FLAGS_db.c_str(),
s.ToString().c_str());
exit(1);
}
FLAGS_secondaries_base = default_secondaries_path;
}
std::vector<std::string> db_paths;
std::vector<std::string> ev_paths;
for (int i = 0; i < num_dbs; i++) {
std::string suffix = (num_dbs == 1) ? "" : "/db_" + std::to_string(i);
db_paths.push_back(FLAGS_db + suffix);
if (!FLAGS_expected_values_dir.empty()) {
std::string ep = FLAGS_expected_values_dir + suffix;
s = Env::Default()->CreateDirIfMissing(ep);
if (!s.ok()) {
fprintf(stderr, "Failed to create directory %s: %s\n", ep.c_str(),
s.ToString().c_str());
exit(1);
}
ev_paths.push_back(std::move(ep));
}
}
if (ev_paths.empty()) {
ev_paths.resize(num_dbs);
}
// Secondary paths: C++ owns creation entirely.
std::vector<std::string> sec_paths;
if (FLAGS_test_secondary || FLAGS_continuous_verification_interval > 0) {
std::string sec_parent;
if (!FLAGS_secondaries_base.empty()) {
sec_parent = FLAGS_secondaries_base;
} else {
raw_env->GetTestDirectory(&sec_parent);
sec_parent += "/dbstress_secondaries";
}
s = raw_env->CreateDirIfMissing(sec_parent);
if (!s.ok()) {
fprintf(stderr, "Failed to create directory %s: %s\n", sec_parent.c_str(),
s.ToString().c_str());
exit(1);
}
for (int i = 0; i < num_dbs; i++) {
std::string suffix = (num_dbs == 1) ? "" : "/db_" + std::to_string(i);
std::string sec_path = sec_parent + suffix;
s = raw_env->CreateDirIfMissing(sec_path);
if (!s.ok()) {
fprintf(stderr, "Failed to create directory %s: %s\n", sec_path.c_str(),
s.ToString().c_str());
exit(1);
}
sec_paths.push_back(std::move(sec_path));
}
}
if (sec_paths.empty()) {
sec_paths.resize(num_dbs);
}
if (FLAGS_best_efforts_recovery &&
@@ -511,25 +571,72 @@ int db_stress_tool(int argc, char** argv) {
key_gen_ctx.weights.emplace_back(key_gen_ctx.window -
keys_per_level * (levels - 1));
}
std::unique_ptr<ROCKSDB_NAMESPACE::SharedState> shared;
std::unique_ptr<ROCKSDB_NAMESPACE::StressTest> stress;
if (FLAGS_test_cf_consistency) {
stress.reset(CreateCfConsistencyStressTest());
} else if (FLAGS_test_batches_snapshots) {
stress.reset(CreateBatchedOpsStressTest());
} else if (FLAGS_test_multi_ops_txns) {
stress.reset(CreateMultiOpsTxnsStressTest());
} else {
stress.reset(CreateNonBatchedOpsStressTest());
}
// Initialize the Zipfian pre-calculated array
// Initialize shared resources (hot-key generator, block cache, WBM,
// rate limiter) once so that all DB instances share them.
InitializeHotKeyGenerator(FLAGS_hot_key_alpha);
shared.reset(new SharedState(db_stress_env, stress.get()));
bool run_stress_test = RunStressTest(shared.get());
// Close DB in CleanUp() before destructor to prevent race between destructor
// and operations in listener callbacks (e.g. MultiOpsTxnsStressListener).
stress->CleanUp();
return run_stress_test ? 0 : 1;
block_cache =
StressTest::NewCache(FLAGS_cache_size, FLAGS_cache_numshardbits);
if (FLAGS_use_write_buffer_manager) {
wbm = std::make_shared<WriteBufferManager>(FLAGS_db_write_buffer_size,
block_cache);
}
if (FLAGS_rate_limiter_bytes_per_sec > 0) {
rate_limiter.reset(NewGenericRateLimiter(
FLAGS_rate_limiter_bytes_per_sec, 1000 /* refill_period_us */,
10 /* fairness */,
FLAGS_rate_limit_bg_reads ? RateLimiter::Mode::kReadsOnly
: RateLimiter::Mode::kWritesOnly));
}
// Phase 1: Create StressTest instances (one per DB).
std::vector<std::unique_ptr<StressTest>> stress_tests(num_dbs);
for (int i = 0; i < num_dbs; i++) {
if (FLAGS_test_cf_consistency) {
stress_tests[i].reset(CreateCfConsistencyStressTest(
i, db_paths[i], ev_paths[i], sec_paths[i]));
} else if (FLAGS_test_batches_snapshots) {
stress_tests[i].reset(CreateBatchedOpsStressTest(
i, db_paths[i], ev_paths[i], sec_paths[i]));
} else if (FLAGS_test_multi_ops_txns) {
stress_tests[i].reset(CreateMultiOpsTxnsStressTest(
i, db_paths[i], ev_paths[i], sec_paths[i]));
} else {
stress_tests[i].reset(CreateNonBatchedOpsStressTest(
i, db_paths[i], ev_paths[i], sec_paths[i]));
}
}
RegisterCrashCallbacks(stress_tests, num_dbs);
// Phase 2: Create SharedState for every DB before any worker thread starts.
std::vector<std::unique_ptr<SharedState>> shared_states(num_dbs);
for (int i = 0; i < num_dbs; i++) {
shared_states[i].reset(new SharedState(raw_env, stress_tests[i].get()));
}
// Phase 3: Launch each DB's stress test on its own thread.
std::vector<int> results(num_dbs, 0);
std::vector<std::thread> stress_test_runners;
stress_test_runners.reserve(num_dbs);
for (int i = 0; i < num_dbs; i++) {
stress_test_runners.emplace_back([i, &results, &shared_states]() {
results[i] = RunStressTest(shared_states[i].get()) ? 1 : 0;
});
}
// Phase 4: Wait for all DB threads, collect results, clean up.
bool all_passed = true;
for (int i = 0; i < num_dbs; i++) {
stress_test_runners[i].join();
if (!results[i]) {
all_passed = false;
}
stress_tests[i]->CleanUp();
}
for (auto& fs : fault_fs_for_crash_report) {
fs = nullptr;
}
return all_passed ? 0 : 1;
}
} // namespace ROCKSDB_NAMESPACE
+8 -5
View File
@@ -40,7 +40,7 @@ DEFINE_int32(clear_wp_commit_cache_one_in, 0,
"write-unprepared transactions.");
extern "C" bool rocksdb_write_prepared_TEST_ShouldClearCommitCache(void) {
static Random rand(static_cast<uint32_t>(db_stress_env->NowMicros()));
static Random rand(static_cast<uint32_t>(raw_env->NowMicros()));
return FLAGS_clear_wp_commit_cache_one_in > 0 &&
rand.OneIn(FLAGS_clear_wp_commit_cache_one_in);
}
@@ -1413,7 +1413,7 @@ Status MultiOpsTxnsStressTest::CommitAndCreateTimestampedSnapshotIfNeeded(
Status s;
if (FLAGS_create_timestamped_snapshot_one_in > 0 &&
thread->rand.OneInOpt(FLAGS_create_timestamped_snapshot_one_in)) {
uint64_t ts = db_stress_env->NowNanos();
uint64_t ts = raw_env->NowNanos();
std::shared_ptr<const Snapshot> snapshot;
s = txn.CommitAndTryCreateSnapshot(/*notifier=*/nullptr, ts, &snapshot);
} else {
@@ -1428,7 +1428,7 @@ Status MultiOpsTxnsStressTest::CommitAndCreateTimestampedSnapshotIfNeeded(
assert(txn_db_);
if (FLAGS_create_timestamped_snapshot_one_in > 0 &&
thread->rand.OneInOpt(50000)) {
uint64_t now = db_stress_env->NowNanos();
uint64_t now = raw_env->NowNanos();
constexpr uint64_t time_diff = static_cast<uint64_t>(1000) * 1000 * 1000;
txn_db_->ReleaseTimestampedSnapshotsOlderThan(now - time_diff);
}
@@ -1767,8 +1767,11 @@ void MultiOpsTxnsStressTest::ScanExistingDb(SharedState* shared, int threads) {
}
}
StressTest* CreateMultiOpsTxnsStressTest() {
return new MultiOpsTxnsStressTest();
StressTest* CreateMultiOpsTxnsStressTest(int db_index,
const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path) {
return new MultiOpsTxnsStressTest(db_index, db_path, ev_path, sec_path);
}
void CheckAndSetOptionsForMultiOpsTxnStressTest() {
+14 -1
View File
@@ -9,6 +9,7 @@
#ifdef GFLAGS
#include "db_stress_tool/db_stress_common.h"
#include "util/atomic.h"
namespace ROCKSDB_NAMESPACE {
@@ -191,7 +192,10 @@ class MultiOpsTxnsStressTest : public StressTest {
uint32_t c_{0};
};
MultiOpsTxnsStressTest() {}
MultiOpsTxnsStressTest(int db_index, const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path)
: StressTest(db_index, db_path, ev_path, sec_path) {}
~MultiOpsTxnsStressTest() override {}
@@ -423,11 +427,16 @@ class MultiOpsTxnsStressListener : public EventListener {
~MultiOpsTxnsStressListener() override {}
void OnDBShutdownBegin(DB* /*db*/) override { shutting_down_.Store(true); }
void OnFlushCompleted(DB* db, const FlushJobInfo& info) override {
assert(db);
#ifdef NDEBUG
(void)db;
#endif
if (shutting_down_.Load()) {
return;
}
assert(info.cf_id == 0);
const ReadOptions read_options(Env::IOActivity::kFlush);
stress_test_->VerifyPkSkFast(read_options, info.job_id);
@@ -438,6 +447,9 @@ class MultiOpsTxnsStressListener : public EventListener {
#ifdef NDEBUG
(void)db;
#endif
if (shutting_down_.Load()) {
return;
}
assert(info.cf_id == 0);
const ReadOptions read_options(Env::IOActivity::kCompaction);
stress_test_->VerifyPkSkFast(read_options, info.job_id);
@@ -445,6 +457,7 @@ class MultiOpsTxnsStressListener : public EventListener {
private:
MultiOpsTxnsStressTest* const stress_test_ = nullptr;
Atomic<bool> shutting_down_{false};
};
} // namespace ROCKSDB_NAMESPACE
+251 -151
View File
@@ -14,6 +14,7 @@
#include "rocksdb/status.h"
#ifdef GFLAGS
#include <cinttypes>
#include <deque>
#include <unordered_map>
#include "db/wide/wide_columns_helper.h"
@@ -24,7 +25,10 @@
namespace ROCKSDB_NAMESPACE {
class NonBatchedOpsStressTest : public StressTest {
public:
NonBatchedOpsStressTest() = default;
NonBatchedOpsStressTest(int db_index, const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path)
: StressTest(db_index, db_path, ev_path, sec_path) {}
virtual ~NonBatchedOpsStressTest() = default;
@@ -265,20 +269,20 @@ class NonBatchedOpsStressTest : public StressTest {
std::string from_db;
// Temporarily disable error injection to verify the secondary
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
s = secondary_db_->Get(options, secondary_cfhs_[cf], key, &from_db);
// Re-enable error injection after verifying the secondary
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
@@ -688,10 +692,10 @@ class NonBatchedOpsStressTest : public StressTest {
bool read_older_ts = MaybeUseOlderTimestampForPointLookup(
thread, read_ts_str, read_ts_slice, read_opts_copy);
if (fault_fs_guard) {
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead);
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead);
SharedState::ignore_read_error = false;
}
@@ -703,11 +707,11 @@ class NonBatchedOpsStressTest : public StressTest {
thread->shared->Get(rand_column_families[0], rand_keys[0]);
int injected_error_count = 0;
if (fault_fs_guard) {
if (db_fault_injection_fs_) {
injected_error_count = GetMinInjectedErrorCount(
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead),
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead));
if (!SharedState::ignore_read_error && injected_error_count > 0 &&
(s.ok() || s.IsNotFound())) {
@@ -716,9 +720,9 @@ class NonBatchedOpsStressTest : public StressTest {
MutexLock l(thread->shared->GetMutex());
fprintf(stderr, "Didn't get expected error from Get\n");
fprintf(stderr, "Callstack that injected the fault\n");
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kRead);
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kMetadataRead);
std::terminate();
}
@@ -821,10 +825,10 @@ class NonBatchedOpsStressTest : public StressTest {
std::unique_ptr<Transaction> txn;
if (use_txn) {
// TODO(hx235): test fault injection with MultiGet() with transactions
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
WriteOptions wo;
@@ -850,20 +854,20 @@ class NonBatchedOpsStressTest : public StressTest {
int injected_error_count = 0;
if (!use_txn) {
if (fault_fs_guard) {
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead);
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead);
SharedState::ignore_read_error = false;
}
db_->MultiGet(readoptionscopy, cfh, num_keys, keys.data(), values.data(),
statuses.data());
if (fault_fs_guard) {
if (db_fault_injection_fs_) {
injected_error_count = GetMinInjectedErrorCount(
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead),
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead));
if (injected_error_count > 0) {
@@ -883,9 +887,9 @@ class NonBatchedOpsStressTest : public StressTest {
"num_keys %zu Expected %d errors, seen at least %d\n",
num_keys, injected_error_count, stat_nok_nfound);
fprintf(stderr, "Callstack that injected the fault\n");
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kRead);
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kMetadataRead);
std::terminate();
}
@@ -952,10 +956,10 @@ class NonBatchedOpsStressTest : public StressTest {
const Status& s,
const std::optional<ExpectedValue>& ryw_expected_value) -> bool {
// Temporarily disable error injection for verification
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
@@ -1049,10 +1053,10 @@ class NonBatchedOpsStressTest : public StressTest {
}
// Enable back error injection disbled for checking results
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
return check_multiget_res;
@@ -1091,10 +1095,10 @@ class NonBatchedOpsStressTest : public StressTest {
if (use_txn) {
txn->Rollback().PermitUncheckedError();
// Enable back error injection disbled for transactions
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
}
@@ -1141,10 +1145,10 @@ class NonBatchedOpsStressTest : public StressTest {
const ExpectedValue pre_read_expected_value =
thread->shared->Get(column_family, key);
if (fault_fs_guard) {
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead);
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead);
SharedState::ignore_read_error = false;
}
@@ -1164,11 +1168,11 @@ class NonBatchedOpsStressTest : public StressTest {
thread->shared->Get(column_family, key);
int injected_error_count = 0;
if (fault_fs_guard) {
if (db_fault_injection_fs_) {
injected_error_count = GetMinInjectedErrorCount(
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead),
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead));
if (!SharedState::ignore_read_error && injected_error_count > 0 &&
(s.ok() || s.IsNotFound())) {
@@ -1177,9 +1181,9 @@ class NonBatchedOpsStressTest : public StressTest {
MutexLock l(thread->shared->GetMutex());
fprintf(stderr, "Didn't get expected error from GetEntity\n");
fprintf(stderr, "Callstack that injected the fault\n");
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kRead);
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kMetadataRead);
std::terminate();
}
@@ -1281,10 +1285,10 @@ class NonBatchedOpsStressTest : public StressTest {
if (FLAGS_use_txn) {
// TODO(hx235): test fault injection with MultiGetEntity() with
// transactions
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
WriteOptions write_options;
@@ -1318,11 +1322,11 @@ class NonBatchedOpsStressTest : public StressTest {
int injected_error_count = 0;
auto verify_expected_errors = [&](auto get_status) {
assert(fault_fs_guard);
assert(db_fault_injection_fs_);
injected_error_count = GetMinInjectedErrorCount(
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead),
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead));
if (injected_error_count) {
int stat_nok_nfound = 0;
@@ -1344,9 +1348,9 @@ class NonBatchedOpsStressTest : public StressTest {
fprintf(stderr, "num_keys %zu Expected %d errors, seen %d\n",
num_keys, injected_error_count, stat_nok_nfound);
fprintf(stderr, "Call stack that injected the fault\n");
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kRead);
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kMetadataRead);
std::terminate();
}
@@ -1356,10 +1360,10 @@ class NonBatchedOpsStressTest : public StressTest {
auto check_results = [&](auto get_columns, auto get_status,
auto do_extra_check, auto call_get_entity) {
// Temporarily disable error injection for checking results
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
const bool check_get_entity =
@@ -1538,10 +1542,10 @@ class NonBatchedOpsStressTest : public StressTest {
}
}
// Enable back error injection disbled for checking results
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
};
@@ -1624,19 +1628,19 @@ class NonBatchedOpsStressTest : public StressTest {
txn->Rollback().PermitUncheckedError();
// Enable back error injection disbled for transactions
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
} else if (FLAGS_use_attribute_group) {
// AttributeGroup MultiGetEntity verification
if (fault_fs_guard) {
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead);
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead);
SharedState::ignore_read_error = false;
}
@@ -1652,7 +1656,7 @@ class NonBatchedOpsStressTest : public StressTest {
db_->MultiGetEntity(read_opts_copy, num_keys, key_slices.data(),
results.data());
if (fault_fs_guard) {
if (db_fault_injection_fs_) {
verify_expected_errors(
[&](size_t i) { return results[i][0].status(); });
}
@@ -1668,10 +1672,10 @@ class NonBatchedOpsStressTest : public StressTest {
} else {
// Non-AttributeGroup MultiGetEntity verification
if (fault_fs_guard) {
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead);
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead);
SharedState::ignore_read_error = false;
}
@@ -1682,7 +1686,7 @@ class NonBatchedOpsStressTest : public StressTest {
db_->MultiGetEntity(read_opts_copy, cfh, num_keys, key_slices.data(),
results.data(), statuses.data());
if (fault_fs_guard) {
if (db_fault_injection_fs_) {
verify_expected_errors([&](size_t i) { return statuses[i]; });
}
@@ -1733,10 +1737,10 @@ class NonBatchedOpsStressTest : public StressTest {
MaybeUseOlderTimestampForRangeScan(thread, read_ts_str, read_ts_slice,
ro_copy);
if (fault_fs_guard) {
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead);
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead);
SharedState::ignore_read_error = false;
}
@@ -1798,11 +1802,11 @@ class NonBatchedOpsStressTest : public StressTest {
}
int injected_error_count = 0;
if (fault_fs_guard) {
if (db_fault_injection_fs_) {
injected_error_count = GetMinInjectedErrorCount(
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kRead),
fault_fs_guard->GetAndResetInjectedThreadLocalErrorCount(
db_fault_injection_fs_->GetAndResetInjectedThreadLocalErrorCount(
FaultInjectionIOType::kMetadataRead));
if (!SharedState::ignore_read_error && injected_error_count > 0 &&
s.ok()) {
@@ -1811,9 +1815,9 @@ class NonBatchedOpsStressTest : public StressTest {
MutexLock l(thread->shared->GetMutex());
fprintf(stderr, "Didn't get expected error from PrefixScan\n");
fprintf(stderr, "Callstack that injected the fault\n");
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kRead);
fault_fs_guard->PrintInjectedThreadLocalErrorBacktrace(
db_fault_injection_fs_->PrintInjectedThreadLocalErrorBacktrace(
FaultInjectionIOType::kMetadataRead);
std::terminate();
}
@@ -1880,10 +1884,10 @@ class NonBatchedOpsStressTest : public StressTest {
if (FLAGS_verify_before_write) {
// Temporarily disable error injection for preparation
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
@@ -1894,10 +1898,10 @@ class NonBatchedOpsStressTest : public StressTest {
/* msg_prefix */ "Pre-Put Get verification", from_db, s);
// Enable back error injection disabled for preparation
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
}
if (!res) {
@@ -2000,7 +2004,7 @@ class NonBatchedOpsStressTest : public StressTest {
&commit_bypass_memtable);
}
}
UpdateIfInitialWriteFails(db_stress_env, s, &initial_write_s,
UpdateIfInitialWriteFails(raw_env, s, &initial_write_s,
&initial_wal_write_may_succeed,
&wait_for_recover_start_time);
@@ -2031,7 +2035,7 @@ class NonBatchedOpsStressTest : public StressTest {
}
} else {
PrintWriteRecoveryWaitTimeIfNeeded(
db_stress_env, initial_write_s, initial_wal_write_may_succeed,
raw_env, initial_write_s, initial_wal_write_may_succeed,
wait_for_recover_start_time, "TestPut");
pending_expected_value.Commit();
thread->stats.AddBytesForWrites(1, sz);
@@ -2107,7 +2111,7 @@ class NonBatchedOpsStressTest : public StressTest {
&commit_bypass_memtable);
}
UpdateIfInitialWriteFails(
db_stress_env, s, &initial_write_s, &initial_wal_write_may_succeed,
raw_env, s, &initial_write_s, &initial_wal_write_may_succeed,
&wait_for_recover_start_time, commit_bypass_memtable);
} while (!s.ok() && IsErrorInjectedAndRetryable(s) &&
initial_wal_write_may_succeed);
@@ -2137,7 +2141,7 @@ class NonBatchedOpsStressTest : public StressTest {
}
} else {
PrintWriteRecoveryWaitTimeIfNeeded(
db_stress_env, initial_write_s, initial_wal_write_may_succeed,
raw_env, initial_write_s, initial_wal_write_may_succeed,
wait_for_recover_start_time, "TestDelete");
pending_expected_value.Commit();
thread->stats.AddDeletes(1);
@@ -2179,7 +2183,7 @@ class NonBatchedOpsStressTest : public StressTest {
&commit_bypass_memtable);
}
UpdateIfInitialWriteFails(
db_stress_env, s, &initial_write_s, &initial_wal_write_may_succeed,
raw_env, s, &initial_write_s, &initial_wal_write_may_succeed,
&wait_for_recover_start_time, commit_bypass_memtable);
} while (!s.ok() && IsErrorInjectedAndRetryable(s) &&
initial_wal_write_may_succeed);
@@ -2209,7 +2213,7 @@ class NonBatchedOpsStressTest : public StressTest {
}
} else {
PrintWriteRecoveryWaitTimeIfNeeded(
db_stress_env, initial_write_s, initial_wal_write_may_succeed,
raw_env, initial_write_s, initial_wal_write_may_succeed,
wait_for_recover_start_time, "TestDelete");
pending_expected_value.Commit();
thread->stats.AddSingleDeletes(1);
@@ -2274,7 +2278,7 @@ class NonBatchedOpsStressTest : public StressTest {
} else {
s = db_->DeleteRange(write_opts, cfh, key, end_key);
}
UpdateIfInitialWriteFails(db_stress_env, s, &initial_write_s,
UpdateIfInitialWriteFails(raw_env, s, &initial_write_s,
&initial_wal_write_may_succeed,
&wait_for_recover_start_time);
} while (!s.ok() && IsErrorInjectedAndRetryable(s) &&
@@ -2302,7 +2306,7 @@ class NonBatchedOpsStressTest : public StressTest {
}
} else {
PrintWriteRecoveryWaitTimeIfNeeded(
db_stress_env, initial_write_s, initial_wal_write_may_succeed,
raw_env, initial_write_s, initial_wal_write_may_succeed,
wait_for_recover_start_time, "TestDeleteRange");
for (PendingExpectedValue& pending_expected_value :
pending_expected_values) {
@@ -2323,60 +2327,16 @@ class NonBatchedOpsStressTest : public StressTest {
// deletion file's compaction input optimization.
bool test_standalone_range_deletion = thread->rand.OneInOpt(
FLAGS_test_ingest_standalone_range_deletion_one_in);
std::vector<std::string> external_files;
const std::string sst_filename =
FLAGS_db + "/." + std::to_string(thread->tid) + ".sst";
external_files.push_back(sst_filename);
std::string standalone_rangedel_filename;
if (test_standalone_range_deletion) {
standalone_rangedel_filename = FLAGS_db + "/." +
std::to_string(thread->tid) +
"_standalone_rangedel.sst";
external_files.push_back(standalone_rangedel_filename);
}
// When true, reuse the writer's metadata via IngestExternalFileArg's
// file_infos so ingestion skips re-opening and scanning the file. Not
// combined with the standalone range deletion mode (a range-del-only file).
bool use_file_info =
!test_standalone_range_deletion &&
thread->rand.OneInOpt(FLAGS_ingest_external_file_use_file_info_one_in);
Status s;
std::ostringstream ingest_options_oss;
// Temporarily disable error injection for preparation
if (fault_fs_guard) {
fault_fs_guard->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
fault_fs_guard->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
for (const auto& filename : external_files) {
if (db_stress_env->FileExists(filename).ok()) {
// Maybe we terminated abnormally before, so cleanup to give this file
// ingestion a clean slate
s = db_stress_env->DeleteFile(filename);
}
if (!s.ok()) {
return;
}
}
if (fault_fs_guard) {
fault_fs_guard->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
fault_fs_guard->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
SstFileWriter sst_file_writer(EnvOptions(options_), options_);
SstFileWriter standalone_rangedel_sst_file_writer(EnvOptions(options_),
options_);
if (s.ok()) {
s = sst_file_writer.Open(sst_filename);
}
if (s.ok() && test_standalone_range_deletion) {
s = standalone_rangedel_sst_file_writer.Open(
standalone_rangedel_filename);
}
if (!s.ok()) {
return;
}
int64_t key_base = rand_keys[0];
int column_family = rand_column_families[0];
std::vector<std::unique_ptr<MutexLock>> range_locks;
@@ -2425,13 +2385,76 @@ class NonBatchedOpsStressTest : public StressTest {
}
}
if (s.ok() && keys.empty()) {
if (keys.empty()) {
return;
}
size_t total_keys = keys.size();
const size_t data_file_count =
std::min<size_t>(1 + thread->rand.Uniform(3), total_keys);
std::vector<std::string> external_files;
std::vector<std::string> data_filenames;
data_filenames.reserve(data_file_count);
for (size_t file_idx = 0; file_idx < data_file_count; ++file_idx) {
data_filenames.push_back(GetDbPath() + "/." +
std::to_string(thread->tid) + "_" +
std::to_string(file_idx) + ".sst");
external_files.push_back(data_filenames.back());
}
std::string standalone_rangedel_filename;
if (test_standalone_range_deletion) {
standalone_rangedel_filename = GetDbPath() + "/." +
std::to_string(thread->tid) +
"_standalone_rangedel.sst";
external_files.push_back(standalone_rangedel_filename);
}
// Temporarily disable error injection for preparation
if (db_fault_injection_fs_) {
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
db_fault_injection_fs_->DisableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
for (const auto& filename : external_files) {
if (raw_env->FileExists(filename).ok()) {
// Maybe we terminated abnormally before, so cleanup to give this file
// ingestion a clean slate
s = raw_env->DeleteFile(filename);
}
if (!s.ok()) {
return;
}
}
if (db_fault_injection_fs_) {
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataRead);
db_fault_injection_fs_->EnableThreadLocalErrorInjection(
FaultInjectionIOType::kMetadataWrite);
}
std::vector<ExternalSstFileInfo> file_infos(data_file_count);
std::deque<SstFileWriter> sst_file_writers;
for (size_t file_idx = 0; s.ok() && file_idx < data_file_count;
++file_idx) {
sst_file_writers.emplace_back(EnvOptions(options_), options_);
s = sst_file_writers.back().Open(data_filenames[file_idx]);
}
SstFileWriter standalone_rangedel_sst_file_writer(EnvOptions(options_),
options_);
if (s.ok() && test_standalone_range_deletion) {
s = standalone_rangedel_sst_file_writer.Open(
standalone_rangedel_filename);
}
if (!s.ok()) {
return;
}
// set pending state on expected values, create and ingest files.
size_t total_keys = keys.size();
for (size_t i = 0; s.ok() && i < total_keys; i++) {
auto& sst_file_writer = sst_file_writers[i % sst_file_writers.size()];
int64_t key = keys.at(i);
char value[100];
auto key_str = Key(key);
@@ -2459,7 +2482,9 @@ class NonBatchedOpsStressTest : public StressTest {
}
}
if (s.ok() && !keys.empty()) {
s = sst_file_writer.Finish();
for (size_t i = 0; s.ok() && i < sst_file_writers.size(); i++) {
s = sst_file_writers[i].Finish(&file_infos[i]);
}
}
if (s.ok() && total_keys != 0 && test_standalone_range_deletion) {
@@ -2477,6 +2502,7 @@ class NonBatchedOpsStressTest : public StressTest {
s = standalone_rangedel_sst_file_writer.Finish();
}
}
bool dropped_without_commit = false;
if (s.ok()) {
IngestExternalFileOptions ingest_options;
ingest_options.move_files = thread->rand.OneInOpt(2);
@@ -2484,24 +2510,95 @@ class NonBatchedOpsStressTest : public StressTest {
ingest_options.verify_checksums_readahead_size =
thread->rand.OneInOpt(2) ? 1024 * 1024 : 0;
ingest_options.fill_cache = thread->rand.OneInOpt(4);
ingest_options.file_opening_threads = 1 + thread->rand.Uniform(4);
ingest_options.prefetch_lmax_index_and_filter_blocks =
!thread->rand.OneInOpt(4);
const bool use_prepare_commit = thread->rand.OneInOpt(
FLAGS_ingest_external_file_prepare_commit_one_in);
const bool use_separate_prepare_calls = use_prepare_commit &&
external_files.size() > 1 &&
thread->rand.OneInOpt(2);
ingest_options_oss << "move_files: " << ingest_options.move_files
<< ", verify_checksums_before_ingest: "
<< ingest_options.verify_checksums_before_ingest
<< ", verify_checksums_readahead_size: "
<< ingest_options.verify_checksums_readahead_size
<< ", fill_cache: " << ingest_options.fill_cache
<< ", file_opening_threads: "
<< ingest_options.file_opening_threads
<< ", prefetch_lmax_index_and_filter_blocks: "
<< ingest_options.prefetch_lmax_index_and_filter_blocks
<< ", ingest_external_file_data_file_count: "
<< data_file_count
<< ", num_external_files: " << external_files.size()
<< ", test_standalone_range_deletion: "
<< test_standalone_range_deletion;
s = db_->IngestExternalFile(column_families_[column_family],
external_files, ingest_options);
<< test_standalone_range_deletion
<< ", use_prepare_commit: " << use_prepare_commit
<< ", use_separate_prepare_calls: "
<< use_separate_prepare_calls
<< ", use_file_info: " << use_file_info;
IngestExternalFileArg arg;
arg.column_family = column_families_[column_family];
arg.external_files = external_files;
arg.options = ingest_options;
if (use_file_info) {
for (const auto& file_info : file_infos) {
arg.file_infos.push_back(file_info.prepared_file_info.get());
}
}
if (use_prepare_commit) {
std::vector<std::unique_ptr<FileIngestionHandle>> handles;
handles.reserve(use_separate_prepare_calls ? external_files.size() : 1);
if (use_separate_prepare_calls) {
for (const auto& external_file : external_files) {
IngestExternalFileArg file_arg;
file_arg.column_family = column_families_[column_family];
file_arg.external_files = {external_file};
file_arg.options = ingest_options;
std::unique_ptr<FileIngestionHandle> handle;
s = db_->PrepareFileIngestion({file_arg}, &handle);
if (!s.ok()) {
break;
}
handles.push_back(std::move(handle));
}
} else {
std::unique_ptr<FileIngestionHandle> handle;
s = db_->PrepareFileIngestion({arg}, &handle);
if (s.ok()) {
handles.push_back(std::move(handle));
}
}
if (s.ok()) {
// Occasionally cancel instead of committing, covering both rollback
// paths.
if (thread->rand.OneInOpt(4)) {
if (thread->rand.OneInOpt(2)) {
for (auto& handle : handles) {
Status abort_status = handle->Abort();
if (!abort_status.ok() && s.ok()) {
s = abort_status;
}
}
} else {
handles.clear(); // RAII rollback via destructors
}
dropped_without_commit = true;
} else {
s = db_->CommitFileIngestionHandles(std::move(handles));
}
}
} else {
s = db_->IngestExternalFiles({arg});
}
}
if (!s.ok()) {
if (!s.ok() || dropped_without_commit) {
for (PendingExpectedValue& pending_expected_value :
pending_expected_values) {
pending_expected_value.Rollback();
}
if (!IsErrorInjectedAndRetryable(s)) {
if (!s.ok() && !IsErrorInjectedAndRetryable(s)) {
fprintf(stderr,
"file ingestion error: %s under specified "
"IngestExternalFileOptions: %s (Empty string or "
@@ -3485,8 +3582,11 @@ class NonBatchedOpsStressTest : public StressTest {
}
};
StressTest* CreateNonBatchedOpsStressTest() {
return new NonBatchedOpsStressTest();
StressTest* CreateNonBatchedOpsStressTest(int db_index,
const std::string& db_path,
const std::string& ev_path,
const std::string& sec_path) {
return new NonBatchedOpsStressTest(db_index, db_path, ev_path, sec_path);
}
} // namespace ROCKSDB_NAMESPACE
@@ -0,0 +1,55 @@
---
title: Resumable Remote Compaction
layout: post
author: hx235
category: blog
---
## Background
RocksDB can offload compaction work to remote workers through the `CompactionService` API. In this model, the **primary RocksDB instance** selects the input files and sends a serialized `CompactionServiceInput` to a worker; the **remote worker** runs `DB::OpenAndCompact()`, writes output SSTs to `output_directory`, and returns a serialized `CompactionServiceResult` that the primary RocksDB instance installs into its LSM tree. See the [Remote Compaction wiki](https://github.com/facebook/rocksdb/wiki/Remote-Compaction) for the full architecture. This lets operators scale compaction throughput with stateless workers while keeping the primary RocksDB instance's CPU and I/O available for serving reads and writes. However, remote compaction jobs can be long-running—sometimes processing hundreds of gigabytes of input. When a worker crashes, gets preempted, or times out, the entire compaction must restart from scratch, wasting all output produced before the interruption and increasing compaction debt on the primary RocksDB instance.
## How Resumable Remote Compaction Works
Resumable remote compaction introduces a **checkpoint-and-resume** mechanism. During a compaction, the worker periodically saves its progress to the `output_directory`. If the compaction is interrupted, a subsequent call to `OpenAndCompact()` with the same output directory can pick up from the last checkpoint rather than starting over.
### Checkpointing
After each output SST file is completed, the worker persists a progress checkpoint to a **compaction progress file** in the output directory `output_directory`. The checkpoint records which internal key to resume from and the metadata of all completed output files. Progress records use **delta encoding**—each record only contains files completed since the last checkpoint—to keep serialization cost linear.
![Checkpointing overview](/static/images/resumable-remote-compaction/checkpointing-overview.svg)
The worker skips checkpointing at boundaries where resuming could be unsafe or requires complicated handling: when range deletions span the file boundary or when adjacent output files share the same user key. These constraints ensure that resuming produces the same results as if the compaction was not interrupted.
### Resuming
When `OpenAndCompact()` is called with `allow_resumption = true`, it scans the output directory for a valid progress file. If one is found, it loads the checkpointed state, seeks the input iterator to the recorded resume key, restores the output file state, and continues compaction from that point. If the progress file is corrupted or missing, the system falls back to a fresh compaction by cleaning the directory.
![Resume flow](/static/images/resumable-remote-compaction/resume-flow.svg)
## How to Enable It
On the primary RocksDB instance, set a `CompactionService` implementation on the DB options. On the remote worker, pass `allow_resumption = true` in `OpenAndCompactOptions` when calling `DB::OpenAndCompact()`. The `output_directory` must be the same across retries for resumption to work—each retry call with the same directory will automatically detect and resume from the previous checkpoint. The `REMOTE_COMPACT_RESUMED_BYTES` statistics ticker tracks the total bytes of output files reused from a previous interrupted run, giving visibility into how much work resumption saved.
```cpp
// Primary RocksDB instance
DBOptions db_options;
db_options.compaction_service = std::make_shared<MyCompactionService>();
// Remote worker
OpenAndCompactOptions options;
options.allow_resumption = true;
std::string result;
Status s = DB::OpenAndCompact(
options,
db_path, // source database path
output_directory, // where output SSTs and progress are stored
compaction_input, // serialized CompactionServiceInput
&result, // serialized CompactionServiceResult
override_options);
```
## Future Work
Today this feature targets remote compaction. The same checkpoint-and-resume mechanism could also support **local compaction** after a crash. The core persistence and resume logic is already in `CompactionJob`; the remaining work is to integrate it with local compaction scheduling and recovery.
+13 -1
View File
@@ -94,11 +94,23 @@ Step 3 - **Synced**: `fsync()` is called when `WriteOptions::sync` is true, or w
Step 4 - **Obsolete**: After all memtables referencing this WAL are flushed, the WAL becomes obsolete and is either recycled or deleted.
## Async WAL Precreation
When `DBOptions::async_wal_precreate` is enabled, RocksDB keeps at most one future WAL file precreated in the background. RocksDB reserves a file number before scheduling the background task; the task opens the file/writer, but deliberately leaves the file as reserved empty storage:
- It does not write compression metadata.
- It does not write predecessor WAL info.
- It does not add the file to `logs_`, `alive_wal_files_`, or MANIFEST WAL tracking.
The file becomes a logical WAL only when foreground `SwitchMemtable()` consumes it. At that point RocksDB writes the normal WAL metadata, flushes the previous WAL writer buffer, and installs the new WAL in the same order as synchronous WAL creation. If foreground rotation reaches the switch while the background task is still running, the writer waits for the reserved file number instead of allocating a higher numbered WAL. This avoids a late lower-numbered WAL appearing after a newer WAL has become live. If background precreation fails, the background task logs the failure and foreground rotation falls back to normal synchronous WAL creation.
Unused precreated WALs are safe across crashes and clean shutdowns because they are zero-record future WAL files. Recovery processes WAL files by log number, marks every observed WAL number as used, and treats an empty future WAL as EOF. On clean close, RocksDB releases the unpublished WAL writer but does not need to delete the empty file. The option is sanitized to false when `recycle_log_file_num > 0`.
## WAL Tracking in MANIFEST
`WalSet` (see `db/wal_edit.h`, tracked in `VersionSet`) manages WAL metadata in the MANIFEST:
- `WalAddition` records are written when a WAL is created or when a closed/inactive WAL's synced size is finalized
- `WalAddition` records are written when a closed/inactive WAL's synced size is finalized
- `WalDeletion` records are written when a WAL becomes obsolete after flush
- Live-WAL syncs (via `DB::SyncWAL()` or `WriteOptions::sync`) are intentionally not tracked in MANIFEST
Loaded 100 of 245 files, more files were not shown because too many files have changed in this diff. Show more