Repository navigation
Reuse HTTP connections to object storage when a read range ends at the end of the object - #118480
Conversation
…f the object `ReadBufferFromRemoteFSGather::createImplementationBuffer` passed the right bound to the object storage buffer only when it was strictly inside the object. A read up to the end of the file (the last granule of a stream, or a whole stream) became an open-ended `Range: bytes=N-` request. `ReadBufferFromS3` returns the HTTP connection to the pool only after observing EOF, and `ReadBufferFromIStream` observes EOF only when a read returns zero bytes, which never happens when the buffer is sized to exactly the remaining bytes, as the `MergeTree` reader does. So the connection stayed occupied until the stream was destroyed at the end of the query, and every new stream opened a new TLS connection: 2333 connections for 5372 requests when reading one row of a `JSON` column with ~500 substreams per part. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [1861979] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 1861979.
Changed lines: Changed C/C++ lines covered: 128/135 (94.81%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units43 translation units recompiled, 346 s compile time in total, 43 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: This change only touches the remote-filesystem read path: it forwards the exact right bound to object-storage reads so an HTTP connection is returned to the pool when a read ends exactly at the object's end, plus supporting tests. The three flagged ClickBench queries (Q16, Q33, Q34) are all large GROUP BY aggregations that don't exercise this connection-reuse logic, and all three deltas (+7% to +10.5%) sit inside master's current variance band, so they were correctly not treated as directional regressions and are downgraded to not_sure. No query that would actually benefit from connection reuse (many-stream S3 reads) is represented in this suite, so no PR-attributable perf change is visible here. Net: no real regression; the flags are run-to-run variance on unrelated queries. clickbenchFlagged queries (3 of 43)
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
|
|
Just in case, also check |
…unknown `createImplementationBuffer` compared the bound against `start_offset + object.bytes_size` and skipped an object of `StoredObject::UnknownSize` to avoid the overflow of that sum. But the end of an object whose size is unknown does not bound the position at all: such a file consists of a single object - the offsets of the objects after it could not be computed - so a bound past `start_offset` is always inside it. Skipping it dropped a bound that the previous condition did pass, and the read ran past `read_until_position`. It happens when an HTTP server answers without `Content-Length`, on a `web` disk and in S3. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`nextImpl` did not check whether the position had already reached `read_until_position`. After a `seek` to the boundary between two objects of a multi-object file, followed by `setReadUntilPosition` to the same offset, `initialize` picked the object that starts exactly there and `readImpl` returned its data - past the right bound. `moveToNextBuffer` already stops on this condition; check it in `nextImpl` too. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Checked
|
| /// The end of an object bounds the position only if its size is known. An object of `UnknownSize` | ||
| /// is always the only object of its file - the offsets of the objects after it could not be | ||
| /// computed - so a bound past `start_offset` is always inside it. It happens when an HTTP server |
There was a problem hiding this comment.
It does not look like a guaranteed property; on the other hand, setReadUntilPosition should handle too-big offsets and just ignore them. So, looks legit.
There was a problem hiding this comment.
🕵 Right, it is not guaranteed, so the code no longer relies on it (072eee9): the bound is forwarded to an object of UnknownSize only when it is the last one, and the read is left open-ended otherwise. An open-ended read only keeps the HTTP connection occupied, whereas a bound interpreted in the coordinates of a wrong object would cut the data short, so failing to the slower side is the right direction here.
Covered by ReadPipeline.GatherLeavesUnknownSizeObjectUnboundedWhenAnotherFollows in src/IO/tests/gtest_read_pipeline.cpp - it fails (the bound is forwarded) when the is_last_object condition is removed, and passes with it; all 29 ReadPipeline gtests pass.
There was a problem hiding this comment.
🕵 Follow-up: instead of relying on the property or falling back to an open-ended read, the layout is now rejected outright. ReadBufferFromRemoteFSGather could never handle an object of an unknown size followed by other objects - initialize wraps start_offset around UINT64_MAX and places all of them at garbage offsets - so the constructor throws BAD_ARGUMENTS, matching the invariant OffsetMap::build already enforces. The bound is then forwarded to an object of an unknown size unconditionally, as you note setReadUntilPosition is expected to ignore a too-big offset anyway.
…ange-at-object-end
`createImplementationBuffer` forwarded the right bound to an object of `UnknownSize` unconditionally, which is correct only while such an object is the last one of its file - the offsets of the objects after it could not be computed. That is not a guaranteed property, as noted in review, so require it explicitly: forward the bound to an unknown-size object only when nothing follows it, and leave the read open-ended otherwise. An open-ended read merely keeps the HTTP connection occupied, while a bound applied in the coordinates of a wrong object would cut the data short. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Status update:
All 29 |
…e right bound an `std::optional` Address review of #118480. `ReadBufferFromRemoteFSGather` cannot compute the offsets of the objects that follow an object of an unknown size - `initialize` would wrap `start_offset` around - so instead of leaving such a read open-ended (which let the inner buffer hand over bytes past `read_until_position`, because the working buffer is not trimmed), reject that layout in the constructor, the same invariant `OffsetMap::build` already enforces. The bound is then forwarded to an object of an unknown size unconditionally. `read_until_position` becomes an `std::optional<size_t>`: a plain `0` sentinel could not tell an unbounded read apart from an empty range at the start of the file, so `setReadUntilPosition(0)` returned the first object's data instead of EOF. Tests: `ReadPipeline.GatherRejectsUnknownSizeObjectWhenAnotherFollows` and `ReadPipeline.GatherEmptyRangeAtFileStart`. Also make `05110_s3_connection_reuse_many_streams` robust against a contended connection pool: it failed in the flaky check because a concurrent query took the pooled connection, so more than two connections were created while reuse still worked. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ange-at-object-end
|
🕵 Status update:
All |
… file `ReadBufferFromRemoteFSGather` rejected an object of `UnknownSize` only when another object followed it, but the layout `[known..., unknown-size-last]` is broken as well: `UnknownSize` is `UINT64_MAX`, so `initialize` wraps `start_offset` around when it walks past the preceding objects and reports EOF at the start of the last object; `isContentCached` walks the objects the same way. Reject an object of an unknown size whenever a file consists of more than one object, which is the invariant `OffsetMap::build` already enforces. Test: `ReadPipeline.GatherRejectsUnknownSizeObjectAfterAnother`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Status update (2026-09-13):
|
|
🕵 Status update (2026-09-13, second pass):
|
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]>
…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]>
Related: #118475
ReadBufferFromRemoteFSGather::createImplementationBufferpassed the right bound of the read range to the object storage buffer only when it was strictly inside the object. A read up to the end of the file (the last granule of a stream, or a whole stream) became an open-endedRange: bytes=N-request.ReadBufferFromS3returns the HTTP connection to the pool only after observing EOF, andReadBufferFromIStreamobserves EOF only when a read returns zero bytes, which never happens when the buffer is sized to exactly the remaining bytes, as theMergeTreereader does. So the connection stayed occupied until the stream was destroyed at the end of the query, and every new stream opened a new TLS connection: 2333 connections for 5372 requests when reading one row of aJSONcolumn with about 500 substreams per part, with the warningToo many active sessions in group Disk, count 1900in the log.With the bound passed, the request is
Range: bytes=N-M, the connection is released as soon as the range is read, and it is reused by the next stream. The test reads 21 streams of a Wide part on an S3 disk with one thread and synchronous reads and checks that the connection is reused instead of created for every stream.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reuse HTTP connections to object storage when a read range ends at the end of the object. Previously such reads held the connection until the end of the query, and every stream of a
MergeTreepart opened a new connection.Workflow [PR]
Sync PR [sync-upstream/pr/118480]
Version info
26.9.1.1420(included in26.9and later)