Skip to content

Add write buffer adaptor to Stream the UNIQUE KEY index SST directly into part storage - #111189

Merged
murphy-4o merged 10 commits into
ClickHouse:masterfrom
JingYanchao:johnjing/sst-writer-write-direct
Jul 31, 2026
Merged

murphy-4o merged 10 commits into
ClickHouse:masterfrom
JingYanchao:johnjing/sst-writer-write-direct

Conversation

@JingYanchao

@JingYanchao JingYanchao commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Write the UNIQUE KEY index SST directly into the part through the storage abstraction, instead of staging it in a local temporary file.

Motivation

The per-part UNIQUE KEY index SST (unique_key_index.sst) is built by RocksDB's SstFileWriter, which needs a real local path. The old code staged it on the context's temporary volume and copied it into the part via a .tmp + replaceFile dance. That hard-depended on a local temporary volume (a remote tmp_policy disk was rejected outright, blocking object-storage deployments) and wrote the bytes twice.

Approach

RocksDB now writes through a custom rocksdb::Env that forwards its bytes straight into the part storage's WriteBuffer, the same path used for column.bin, marks, and the primary index. On the write path this works for local disks and transactional part builds without any local staging or replaceFile. The SST bytes are streamed straight into the part storage WriteBuffer, the same path used for column.bin, marks, and the primary index.

TODO: the read path (ensureValidDenseIndex and the SST probe) still opens unique_key_index.sst via storage.getFullPath() + RocksDB's SstFileReader::Open, which requires a local filesystem path. Lifting the reader onto the IDataPartStorage abstraction (or materializing the SST locally before open) will be addressed in a follow-up PR.

Version info

  • Merged into: 26.8.1.498 (included in 26.8 and later)

@JingYanchao

JingYanchao commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@murphy-4o hi, I hope to use an adapter to write RocksDB SST data directly into ClickHouse's WriteBuffer, thereby simplifying the SST file writing process. Could you please review this and give some suggestion? Thanks!
The core idea is to inject a custom rocksdb::FSWritableFile class to intercept the RocksDB write process, with the actual data being written directly to the WriteBuffer.

@murphy-4o murphy-4o self-assigned this Jul 21, 2026
@murphy-4o murphy-4o added the can be tested Allows running workflows for external contributors label Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f60bb35]

Summary: ✅


AI Review

Summary

This PR replaces the local-temp-file SST staging flow with a custom RocksDB write adapter that streams unique_key_index.sst directly into IDataPartStorage, and the current patch looks mostly sound. The main remaining concern is that the writer still is not fail-closed after an Open/Finish error: if a caller catches that exception and keeps using the same SSTIndexWriter, the object can still finalize or overwrite the failed stream state instead of rejecting further use.

Findings

⚠️ Majors

  • [dismissed by author -- https://github.com/Add write buffer adaptor to Stream the UNIQUE KEY index SST directly into part storage #111189#discussion_r3655278016] [src/Storages/MergeTree/UniqueKey/SSTIndexWriter.cpp:319, 425] SSTIndexWriter still remains reusable after an Open or Finish failure, so the class no longer fails closed. openOutputStreamOnFirstEntry stores out_file, sst_env, and writer before writer->Open("") succeeds, while finalizeToStorage treats any non-null out_file as finalizable even when opened is already false. If a caller catches the original exception, a later finalizeToStorage can commit the half-initialized target, and a retrying addEncoded can overwrite the previous buffer/env without first canceling or resetting them.
    Suggested fix: make the writer permanently failed on any Open/Put/Finish exception by cancel()ing and reset()ting the live out_file/writer/sst_env, recording a failed flag, and making later addEncoded / finalizeToStorage throw LOGICAL_ERROR.
Final Verdict

Changes are close, but I would not approve as-is because SSTIndexWriter still permits post-failure reuse that can commit or clobber a broken SST stream state.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 21, 2026
@JingYanchao
JingYanchao force-pushed the johnjing/sst-writer-write-direct branch from 9ad228b to 2cdbe56 Compare July 22, 2026 04:07
@JingYanchao
JingYanchao force-pushed the johnjing/sst-writer-write-direct branch from 2cdbe56 to e72f957 Compare July 27, 2026 07:16
@JingYanchao
JingYanchao force-pushed the johnjing/sst-writer-write-direct branch from a9dc488 to 0233dbc Compare July 29, 2026 06:40
@murphy-4o
murphy-4o self-requested a review July 30, 2026 02:22
@JingYanchao
JingYanchao force-pushed the johnjing/sst-writer-write-direct branch from 46de89b to b604cf6 Compare July 30, 2026 07:35
optimize comment

optimize code

# Conflicts:
#	src/Common/ProfileEvents.cpp
#	src/Storages/MergeTree/UniqueKey/SSTIndexWriter.cpp
#	src/Storages/MergeTree/UniqueKey/tests/gtest_unique_key_sst_probe.cpp
@JingYanchao
JingYanchao force-pushed the johnjing/sst-writer-write-direct branch from 797d058 to f60bb35 Compare July 30, 2026 11:59
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.60% 78.60% +0.00%

Changed lines: Changed C/C++ lines covered: 129/162 (79.63%) · Uncovered code

Full report · Diff report

@murphy-4o
murphy-4o added this pull request to the merge queue Jul 31, 2026
Merged via the queue into ClickHouse:master with commit 8aad759 Jul 31, 2026
351 of 353 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants