Skip to content

options: expose direct-IO setters for the SST writer and reader - #856

Merged
ti-chi-bot[bot] merged 2 commits into
tikv:masterfrom
sanketkedia:sst-direct-io-setters
Sep 22, 2026
Merged

ti-chi-bot[bot] merged 2 commits into
tikv:masterfrom
sanketkedia:sst-direct-io-setters

Conversation

@sanketkedia

@sanketkedia sanketkedia commented Jul 30, 2026 •

Copy link
Copy Markdown

What is changed and how it works?

See tikv/tikv#19917 for full context. Exposes two direct-IO setters that TiKV needs in order to stop its SST importer from polluting the OS page cache:

  • EnvOptions::set_use_direct_writes — lets SstFileWriter write an SST without
    populating the page cache. Adds the missing crocksdb_envoptions_set_use_direct_writes
    shim; RocksDB already supports the underlying EnvOptions::use_direct_writes, using
    O_DIRECT on Linux and fcntl(F_NOCACHE) on macOS.
  • ColumnFamilyOptions::set_use_direct_reads — lets a standalone SstFileReader read an
    SST the same way. Rust-only wrapper; it reuses the existing
    crocksdb_options_set_use_direct_reads FFI, so there is no new C code on this path.

Neither setter changes any default — both are false unless explicitly set, so existing
users are unaffected.

Check List

Tests

  • Unit test — test_sst_direct_io in tests/cases/test_ingest_external_file.rs: writes
    an SST with direct writes, reads it back with direct reads and verifies checksum +
    contents, then reads it once more buffered to confirm the alignment padding
    O_DIRECT requires does not leak into the file format.

Side effects

  • Performance regression
  • Breaking backward compatibility

No behaviour change unless a caller opts in.

Related changes

  • Consumed by a follow-up PR to tikv/tikv implementing the importer fix; that PR is blocked
    on this one landing, since TiKV pins rocksdb by git.

Summary by CodeRabbit

  • New Features

    • Added an option to enable direct writes for environment settings.
    • Direct I/O supports creating, verifying, ingesting, and reading SST files while preserving compatibility with buffered I/O.
  • Documentation

    • Updated direct I/O terminology to be platform-neutral.
  • Tests

    • Expanded coverage for direct and buffered read/write combinations, including checksum validation and expected SST contents.

TiKV's SST importer writes ingested SSTs buffered and then reads each one
back in full, also buffered, to verify checksums. During a large ADD INDEX
ingest that streams the entire index through the OS page cache twice and
evicts the foreground read working set: reads that were page-cache hits
(~50us) become device reads (~400us), degrading foreground p99 by ~2x.
See tikv/tikv#19917 for the full analysis.

Fixing that in TiKV needs two knobs this crate does not currently expose:

- EnvOptions::set_use_direct_writes, so SstFileWriter can write without
  populating the page cache. RocksDB uses O_DIRECT on Linux and
  fcntl(F_NOCACHE) on macOS.
- ColumnFamilyOptions::set_use_direct_reads, so a standalone SstFileReader
  can read the same way. use_direct_reads is a DB-level RocksDB option and
  DBOptions already exposes it here, but SstFileReader::new only accepts
  ColumnFamilyOptions; in crocksdb both wrap the same underlying Options
  object, so the setter is added there as well.

Neither setter changes any default: both are false unless explicitly set.

The test writes an SST with direct writes, reads it back with direct reads,
and then reads it again buffered to confirm the alignment padding O_DIRECT
requires does not leak into the file format.

Ref: tikv/tikv#19917

Signed-off-by: Sanket Kedia <kediasanket11121993@gmail.com>
@ti-chi-bot ti-chi-bot Bot added dco-signoff: yes Indicates the PR's author has signed the dco. contribution This PR is from a community contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Jul 30, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 30, 2026

Copy link
Copy Markdown

Hi @sanketkedia. Thanks for your PR.

I'm waiting for a tikv member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot Bot added the first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. label Jul 30, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 30, 2026

Copy link
Copy Markdown

Welcome @sanketkedia!

It looks like this is your first PR to tikv/rust-rocksdb 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to tikv/rust-rocksdb. 😃

@ti-chi-bot ti-chi-bot Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 30, 2026
@coderabbitai

coderabbitai Bot commented Jul 30, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 4d07eafc-7c1c-44c8-99a6-a7868e9ab6d5

📥 Commits

Reviewing files that changed from the base of the PR and between f5efbde and 05ea1fa.

📒 Files selected for processing (2)
  • src/rocksdb_options.rs
  • tests/cases/test_ingest_external_file.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/rocksdb_options.rs

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

Adds a C and Rust FFI setter for direct writes. Updates direct-I/O documentation. Expands SST tests to cover direct and buffered reads and writes, checksum validation, iteration, and database ingestion.

Changes

Direct I/O option support

Layer / File(s) Summary
Direct I/O options API
librocksdb_sys/crocksdb/c.cc, librocksdb_sys/crocksdb/crocksdb/c.h, librocksdb_sys/src/lib.rs, src/rocksdb_options.rs
Adds the C export and Rust FFI binding for use_direct_writes. Updates direct-I/O documentation for read and write options.
Direct-I/O SST validation
tests/cases/test_ingest_external_file.rs
Tests direct and buffered SST write/read combinations. Verifies checksums, key/value iteration, and ingestion into a database with buffered reads.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant RustAPI
  participant RocksDBOptions
  participant SSTFile
  participant Database
  RustAPI->>RocksDBOptions: configure direct writes
  RocksDBOptions->>SSTFile: write direct or buffered SST
  RustAPI->>SSTFile: read with direct or buffered options
  SSTFile->>RustAPI: return checksum and key/value results
  RustAPI->>Database: ingest direct-written SST
  Database->>RustAPI: return buffered reads and iteration results
Loading

Suggested reviewers: andremouche

Merge Risk: ⚪ Minimal · up to 05ea1

The direct-I/O bindings and expanded SST compatibility coverage introduce no actionable merge-blocking risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: exposing direct-I/O setters for SST writing and reading. It is concise, specific, and consistent with the implemented APIs and tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI

Warning

Some tools did not complete. Review the errors below.

🔧 Clippy (1.98.0)

Clippy execution failed


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/rocksdb_options.rs`:
- Around line 1492-1501: Update the public documentation for
set_use_direct_reads and the corresponding DBOptions setter to replace
platform-specific “O_DIRECT” wording with “direct I/O,” while preserving the
existing explanation of bypassing the OS page cache.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9b84651c-38cf-40e3-8bac-60966ef818dc

📥 Commits

Reviewing files that changed from the base of the PR and between cdb577c and f5efbde.

📒 Files selected for processing (5)
  • librocksdb_sys/crocksdb/c.cc
  • librocksdb_sys/crocksdb/crocksdb/c.h
  • librocksdb_sys/src/lib.rs
  • src/rocksdb_options.rs
  • tests/cases/test_ingest_external_file.rs

Comment thread src/rocksdb_options.rs Resolved

@AndreMouche AndreMouche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM @Connor1996 Please take a look

@ti-chi-bot ti-chi-bot Bot added the lgtm label Aug 19, 2026
@ti-chi-bot

ti-chi-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-08-19 17:54:12.211858005 +0000 UTC m=+92287.382952118: ☑️ agreed by AndreMouche.

@ti-chi-bot

ti-chi-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: AndreMouche

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added the approved label Aug 19, 2026
@LykxSassinator

Copy link
Copy Markdown

Although the internal RocksDB read/write APIs are designed to guarantee forward and backward compatibility when writes use Direct I/O and reads use buffered I/O, I still have some concerns. To be safe, I'd suggest adding more test cases to explicitly validate compatibility for the following scenario:

1. Buffered write -> Direct I/O read

The current test covers "direct-write" -> "buffered-read", which is the important “disable Direct I/O after writing” compatibility case.
Could we also add the reverse case: create an SST with default buffered I/O, then reopen it with set_use_direct_reads(true) and verify its checksum and entries?

Direct I/O is an I/O mode rather than an SST format attribute, so this should work without any format metadata or migration. Adding this case would make the compatibility matrix explicit and protect both directions when users toggle the option.

2. Direct I/O write -> ingest -> normal DB read

Could we also extend the test to ingest a directly-written SST into a DB and read the ingested keys through the normal buffered DB read path?

Reopening the external SST with a buffered SstFileReader already validates its table format, but an ingest test would cover the complete TiKV lifecycle: create with Direct I/O, ingest into RocksDB, disable/not use Direct I/O, and serve normal reads. This would provide stronger regression coverage for the compatibility concern.

Address review feedback on the direct-IO setters:

- Test buffered write -> direct read in addition to direct write ->
  direct/buffered read, making the compatibility matrix explicit.
- Ingest a directly-written SST into a DB and verify it serves normal
  buffered point and iterator reads, covering TiKV's full lifecycle.
- Say "direct I/O" instead of "O_DIRECT" in the setter docs, since
  RocksDB uses fcntl(F_NOCACHE) on macOS.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Sanket Kedia <kediasanket11121993@gmail.com>
@sanketkedia

Copy link
Copy Markdown
Author

I've addressed your comments. Please take a look again. thank you!

@ti-chi-bot
ti-chi-bot Bot merged commit 25e516a into tikv:master Sep 22, 2026
7 checks passed
sanketkedia added a commit to sanketkedia/tikv that referenced this pull request Sep 25, 2026
…ngest

An ImportSST.Write writes each SST buffered and then reads it back in full,
also buffered, to verify checksums. For a large ADD INDEX with DXF + global
sort, that streams the whole index through the OS page cache twice and evicts
the working set that concurrent foreground reads depend on: reads that were
page-cache hits (~50us) become device reads (~400us), and foreground p99
degrades ~2x while throughput falls to 0.72x of baseline.

Add import.use-direct-io-for-ingest. When set, DDL-sourced ingest writes its
SSTs with direct I/O and verifies them with direct I/O too, so ingested data
never enters the page cache. Off by default: direct I/O needs a data directory
on a filesystem that supports it, so enabling it is an explicit operator
decision.

Both halves are required. Applying direct I/O only to the writer is a measured
no-op, because the mandatory verification read immediately repopulates the
cache with the data the write kept out of it.

Scoped to DDL-sourced writes because that is the only workload measured. The
discriminator already arrives on the RPC: request_source is
{external|internal}_{tidb_req_source}_{source_task_name}, TiDB's DDL ingest
backend tags its writes internal_lightning_ddl, and the trailing component is
the same task name resource control already matches on. No protocol or
TiDB-side change is needed.

The new trait methods SstReader::open_for_one_shot_read and
SstWriterBuilder::set_use_direct_writes both default to ignoring the flag, so
no other engine implementation has to change.

Cargo.toml temporarily patches rocksdb to the branch behind
tikv/rust-rocksdb#856, which adds the two setters this needs. That must be
removed, and replaced with `cargo update -p rocksdb`, before this can merge.

ref tikv#19917

Signed-off-by: Sanket Kedia <kediasanket11121993@gmail.com>
sanketkedia added a commit to sanketkedia/tikv that referenced this pull request Sep 25, 2026
…ngest

An ImportSST.Write writes each SST buffered and then reads it back in full,
also buffered, to verify checksums. For a large ADD INDEX with DXF + global
sort, that streams the whole index through the OS page cache twice and evicts
the working set that concurrent foreground reads depend on: reads that were
page-cache hits (~50us) become device reads (~400us), and foreground p99
degrades ~2x while throughput falls to 0.72x of baseline.

Add import.use-direct-io-for-ingest. When set, DDL-sourced ingest writes its
SSTs with direct I/O and verifies them with direct I/O too, so ingested data
never enters the page cache. Off by default: direct I/O needs a data directory
on a filesystem that supports it, so enabling it is an explicit operator
decision.

Both halves are required. Applying direct I/O only to the writer is a measured
no-op, because the mandatory verification read immediately repopulates the
cache with the data the write kept out of it.

Scoped to DDL-sourced writes because that is the only workload measured. The
discriminator already arrives on the RPC: request_source is
{external|internal}_{tidb_req_source}_{source_task_name}, TiDB's DDL ingest
backend tags its writes internal_lightning_ddl, and the trailing component is
the same task name resource control already matches on. No protocol or
TiDB-side change is needed.

The new trait methods SstReader::open_for_one_shot_read and
SstWriterBuilder::set_use_direct_writes both default to ignoring the flag, so
no other engine implementation has to change.

Cargo.toml temporarily patches rocksdb to the branch behind
tikv/rust-rocksdb#856, which adds the two setters this needs. That must be
removed, and replaced with `cargo update -p rocksdb`, before this can merge.

ref tikv#19917

Signed-off-by: Sanket Kedia <kediasanket11121993@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved contribution This PR is from a community contributor. dco-signoff: yes Indicates the PR's author has signed the dco. first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. lgtm needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants