Skip to content

Bound reads of files inside packed parts to the file's slice - #118483

Merged
alexey-milovidov merged 4 commits into
masterfrom
bound-packed-file-view-reads-pr
Sep 11, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
bound-packed-file-view-reads-pr

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Related: #118475

ReadBufferFromFileView, which reads a file inside data.packed of a part with the Packed storage, seeked the underlying buffer to the file's offset but did not pass the right bound until the reader called setReadUntilPosition. Whole-file reads (metadata files, marks files, small streams) therefore became open-ended range requests on object storage: they transferred the rest of the archive instead of the file, and the HTTP connection could not be returned to the pool.

Measured with a Wide Packed part of a JSON column with 1104 substreams behind an HTTP server with 30 ms latency, SELECT * ... ORDER BY t DESC LIMIT 1: before, 1419 requests of which 712 were open-ended, 5.5 MB transferred, 1215 connections opened; after, 0 open-ended requests, 2.0 MB transferred, and the connection count is the same as for the Full storage. For a Compact Packed part the same query used 12 connections before and 2 after.

The test reads a Wide Packed part on an S3 disk with one thread and synchronous reads and checks that the connection is reused instead of created for every file.

Changelog category (leave one):

  • Performance Improvement

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

Bound object storage requests for files inside packed parts to the file's slice, so that reading a packed part from object storage does not transfer the rest of the archive and reuses HTTP connections.


Workflow [PR]
Sync PR [sync-upstream/pr/118483]

Version info

  • Merged into: 26.9.1.1219 (included in 26.9 and later)

alexey-milovidov and others added 2 commits September 6, 2026 22:38
`ReadBufferFromFileView` (used to read files inside `data.packed` of a part with
the Packed storage) seeked the underlying buffer to the file's offset but did not
pass the right bound until the reader called `setReadUntilPosition`. Reads of
whole files (metadata files, marks files) therefore became open-ended range
requests on object storage: they transferred the rest of the archive instead of
the file, and the HTTP connection could not be returned to the pool. Reading one
row of a `JSON` column with 1104 substreams from a Wide Packed part transferred
5.5 MB in 1419 requests over 1215 connections; the same read from the Full
storage transferred 2.1 MB over 502 connections.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [38e2d96]

Summary: ✅


AI Review

Summary

This PR bounds ReadBufferFromFileView at construction time so whole-file reads of members inside data.packed issue slice-bounded object-storage requests instead of open-ended reads, and adds an S3 stateless test for connection reuse on packed parts. I reviewed the current diff and prior discussion, traced the new eager bound through the direct S3/Web, async, gather, filesystem-cache, page-cache, and nested-view paths, and did not find a remaining correctness, performance, or coverage issue that warrants an inline review comment.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit 38e2d96.

Metric Baseline Current Δ
Lines 89.00% 89.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 6, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Doesn't it make the caching worse?

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 38e2d96f0 with master a61a24c6a (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 5 s compile time in total, 7 of them have a recent master baseline.

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

The change adds a single read-bound to ReadBufferFromFileView so that reads of files inside a Packed-storage archive on object storage stop issuing open-ended range requests and can return their HTTP connection to the pool. That path is specific to Packed parts on S3-style object storage; the ClickBench benchmark runs on local storage and never touches this code. The flagged Q30 improvement (-11.0%) is on a query the history marks as flaky, with CPU essentially unchanged (2386 vs 2383 ms), so it reads as measurement variance rather than a PR effect and was downgraded to not sure. No credible per-query performance impact is expected from this diff.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 30 not_sure 227 202 -11.0% <0.0001 io: This PR only bounds range requests for files inside Packed-storage archives on object storage; ClickBench runs on local storage, so this path is never exercised. The -11.0% on an intrinsically flaky query is run-to-run variance.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 708b1d79-fa61-4cbb-9e99-5fb88850c64b
  • MIRAI run: 7ad018bf-11c7-469f-afe7-82255f3f35d4
  • PR check IDs:
    • clickbench_39865_1789141229
    • clickbench_39878_1789141229
    • clickbench_39884_1789141229
    • tpch_adapted_1_official_39890_1789141229
    • tpch_adapted_1_official_39921_1789141229
    • tpch_adapted_1_official_39935_1789141229

alexey-milovidov and others added 2 commits September 11, 2026 12:24
The flaky check failed 23 times out of 23 on the middle column of
`05112_s3_packed_part_connection_reuse`: `DiskConnectionsCreated <= 2`.
How many connections a query has to create depends on how many are
already stored in the shared pool when it starts, which concurrently
running queries change, so the count is not a property of this change.

`DiskConnectionsReused >= 18` held in every one of those runs, and it is
the assertion that actually pins the fix down: without bounding the read
to the file's slice the response body is never fully consumed, every
connection is reset instead of being returned to the pool, and there is
nothing to reuse.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118483&sha=8a01efa9df92e18f013edc9e18ffdf3b087d2566&name_0=PR
#118483

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 > Doesn't it make the caching worse?

No — the only thing that changes is where the HTTP range of the request ends; the amount of data that is read and cached stays the same.

  • Nothing was ever read past right_bound through the view anyway: nextImpl stops at the right bound and resizeWorkingBuffer truncates the working buffer to it, so the bytes past the end of the file inside data.packed were transferred and then thrown away. And setReadUntilEnd already set the impl bound to right_bound, so the constructor only makes the initial state match the bound the view uses everywhere else.
  • With the filesystem cache, the archive is read through CachedOnDiskReadBufferFromFile, which bounds its remote buffer by the file segment, not by read_until_position: buf->setReadUntilPosition(std::min(range.right + 1, file_size_)), with an explicit comment that this is deliberately not read_until_position because of concurrent queries. So touched cache segments are still downloaded whole, exactly as before.
  • With the page cache (use_page_cache_for_disks_without_file_cache), CachedInMemoryReadBufferFromFile sizes its blocks from block_size and the object's file size and rounds the inner bound up to a block boundary ((read_until_position + block_size - 1) / block_size * block_size), so blocks are cached whole as well.
  • There is also no cross-file read-ahead to lose: PackedFilesReader::readFile and DataPartStorageOnDiskPacked open a fresh disk->readFile per file inside the archive, so an open-ended range never served the next file's read — it only made the connection unusable afterwards.

Also pushed: master merged, and the environment-dependent DiskConnectionsCreated <= 2 column dropped from the new test (it failed 23/23 in the flaky check because the number of connections a query must create depends on what other concurrent queries left in the shared pool; DiskConnectionsReused >= 18, which is what actually pins the fix down, held in all of those runs).

@alexey-milovidov alexey-milovidov self-assigned this Sep 11, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 11, 2026
Merged via the queue into master with commit dc740cb Sep 11, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the bound-packed-file-view-reads-pr branch September 11, 2026 19:47
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 11, 2026
pull Bot pushed a commit to AKJUS/ClickHouse that referenced this pull request Sep 13, 2026
The test asserted that the warm reread of a cold-populated table touches the
source not at all, and started failing on master right after
ClickHouse#118483:

    -warm_served_from_cache	1
    +warm_served_from_cache	0

`min_bytes_for_full_part_storage` is randomized by the test harness, so about
two thirds of the runs store the part with the Packed storage: every file of the
part lives in ONE object, hence ONE filesystem-cache segment. Inside a single
query the marks streams and the data stream each build their own
`ReaderExecutor` over that object (`max_threads = 1` does not serialize them -
the marks load on their own pool threads) and race for the segment's single
downloader role. Only the role winner writes; the losers read through from
source and discard.

Before ClickHouse#118483 this was invisible: a whole-file read inside `data.packed` was an
open-ended range request, so the role winner's fetch ran to the end of the
archive and populated the whole segment. ClickHouse#118483 bounds each read to the file's
slice, so the winner now fills only that slice and the cold read leaves the
segment with holes. The warm read then re-fetches such a segment from its start
(`claimLeadRole`'s `available` prefix is deliberately unused - see the "Coarse
by design" note in `ReaderExecutor::readThroughCaches`), so `warm` is the whole
extent rather than 0.

Pin `min_bytes_for_full_part_storage = 0`: with the Full storage every stream is
its own object and therefore its own cache key, so each segment has exactly one
writer and the cold read populates all of them. That is also what the test means
to cover - the executor's read-through contract - rather than whether concurrent
streams over one shared segment converge.

Verified against a local minio with the failing run's randomized settings, on
26.9.1.1234 (the SHA below): the test as it stands fails 97/200, the patched
test passes 350/350. Cross-checked on 26.9.1.1193 (just before ClickHouse#118483), where
the unpatched test passes 100/100 and the cold read pulls 4064344 bytes from
source for a 1630734-byte archive - the over-read that ClickHouse#118483 removed is what
used to fill the segment.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=1d0999bf07898241e1229af5b6c1717aec5d275c&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20sequential%2C%202%2F2%29

Caused by: ClickHouse#118483
Related: ClickHouse#115664

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
JTCunning pushed a commit to JTCunning/ClickHouse that referenced this pull request Nov 2, 2026
Both tests asserted `DiskConnectionsReused >= 18` for one sequential read
of a 21-column Wide part. The connection pool is keyed on the endpoint and
every test S3 disk is `localhost:11111`, so the counter measured a pool
shared with every concurrent test: in the failed 05110 run all 42
connections were returned (Preserved 42, Reset 0), yet neighbours took
them and the query created 26 and reused 16. The same counters also pass
with each test's own fix reverted, because the test's INSERT leaves idle
connections in the pool (05110: Created 0 / Reused 42; 05112: Reused 21).

Assert each bug's own per-query effect instead:
- 05112: `DiskConnectionsReset <= 2`. Without the view's right bound every
  file read is open-ended, the response is never read to the end and the
  connection is reset instead of returned.
- 05110, now a `.sh` test: count the TEST-level `Read S3 object ... Range:
  a-b` lines of the SELECT. Without the right bound at the object end the
  data file requests have no range end and log no such line.

Negative controls on a debug build (local MinIO): with the gather fix
reverted the new 05110 fails 10/10 back-to-back on a warm pool (0 ranged
requests vs 21, connection counters identical to the fixed build); with
the view bound removed the new 05112 fails 10/10 (Reset 21 vs 0). The
fixed build passes both in 50 sequential, 50 `-j 16` and 30 runs under a
16-way S3 load.

CI failures:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=6101ac5e7d4976004fc00948f42677b22f3f40fc&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%202%2F3%29
https://github.com/ClickHouse/ClickHouse/actions/runs/36682278835/job/109784920371
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123673&sha=b0678011a1bdfaf285150128113a0ffd0e8097b5&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20AsyncInsert%2C%20s3%20storage%2C%20parallel%29

Related: ClickHouse#118480, ClickHouse#118483

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... label Nov 22, 2026
@clickhouse-gh clickhouse-gh Bot removed the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Nov 22, 2026
filimonov added a commit to Altinity/ClickHouse that referenced this pull request Nov 24, 2026
…cked archive member

`ReadBufferFromFileView` never told the buffer under it where a packed archive
member ends, so every member read on an object storage disk was an open-ended
`GET`: a merge of a 2,400-column table reset ~8,100 connections. The view now
passes its right bound down. `05112` asserts `DiskConnectionsReset <= 2`
instead of a reuse count of the shared S3 connection pool.

Upstream: ClickHouse#118483
(merge commit dc740cb)
Upstream: ClickHouse#124776
(merge commit 9e2445a), only its `05112`
part: `05110` tests ClickHouse#118480, which is not on this branch.

Signed-off-by: Mikhail Filimonov <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... pr-performance Pull request with some performance 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.

2 participants