Skip to content

Cut the buffered bytes past a lowered right bound of an Azure read - #120957

Merged
alexey-milovidov merged 8 commits into
masterfrom
azure-read-bound-change-discards-buffer
Oct 10, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
azure-read-bound-change-discards-buffer

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

Related: #115706
Related: #120419

ReadBufferFromAzureBlobStorage::setReadUntilPosition only recorded the new bound and marked the download as not initialized, leaving the working buffer as it was. When the bound was lowered below the end of the buffered bytes, those bytes were still handed out past the bound, and the next download was then opened where the previous response ended rather than at the bound, so the following nextImpl failed with the logical error Attempt to read beyond right offset (100 > 39). Formats that adjust the bound mid-read (Parquet, Arrow, ORC through AsynchronousBoundedReadBuffer) are the callers.

The buffered bytes that lie past a lowered bound are now cut off the working buffer, and the next download, if any, starts at the bound. Buffered bytes within the new bound are kept, so raising the bound goes on reopening the download after them, as before, without downloading them again. Setting the bound that is already in effect no longer drops the download. The download itself is always reopened under a changed bound, since the current one was requested under the previous bound. (ReadBufferFromS3 drops the whole buffer and reopens at the position on any bound change; this keeps what is still usable instead.)

src/Disks/tests/gtest_azure_read_buffer_right_bound_change.cpp lowers the bound below already buffered bytes, raises it after some bytes were consumed, and sets the same bound twice, checking the bytes delivered, the final position, and the offsets the fake endpoint was asked for. Without the change the first fails with Attempt to read beyond right offset (100 > 39).

This is one of the independent parts of #115706, which was reverted in #120419.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix the logical error Attempt to read beyond right offset when the right bound of a read from Azure Blob Storage is lowered below bytes the reader has already buffered.


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

Version info

  • Merged into: 26.10.1.2077-master (included in 26.10 and later)

`ReadBufferFromAzureBlobStorage::setReadUntilPosition` only recorded the new bound and
marked the download as not initialized, but left the working buffer as it was. The bytes
that the current response had already delivered were still handed out - past a lowered
bound too - and the next download was then opened where the previous response ended
rather than where the reader was, so the following `nextImpl` failed with the logical
error `Attempt to read beyond right offset`, whether the bound was lowered or raised.
Setting the bound that was already in effect did the same, because it also dropped the
download.

A change of the right bound is now a new logical read, the same way a seek is: the working
buffer is dropped, the position is kept, and the download is reopened at the position under
the new bound. Setting the bound that is already in effect does nothing, so a caller that
sets it repeatedly does not reopen the download. `setReadUntilEnd` uses the same path.

`src/Disks/tests/gtest_azure_read_buffer_right_bound_change.cpp` lowers the bound below
already buffered bytes, raises it after some were consumed, and sets the same bound twice,
checking the bytes delivered, the final position, and the offsets the endpoint was asked for.

This is one of the independent parts of
#115706, which was reverted in
#120419.
@alexey-milovidov
alexey-milovidov marked this pull request as draft September 19, 2026 02:50
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3d09934]

Summary: ✅


AI Review

Summary

This PR fixes ReadBufferFromAzureBlobStorage so a mid-read right-bound change reconciles the buffered bytes with the new bound instead of handing out stale bytes past it or reopening the next Azure download at the wrong offset. In the current code, the previously reported failure modes are addressed: lowering the bound trims or drops buffered data as needed, raising or lifting the bound keeps still-valid buffered bytes while forcing a reopen under the new bound, repeated calls with the same bound are a no-op, and the focused regression tests cover those transitions. I did not find a remaining correctness, compatibility, or safety issue in the current diff.

Final Verdict
  • Status: ✅ Approve
  • No further action required.

LLVM Coverage Report

Measured on commit 3d09934.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 92.10% +0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 17/18 (94.44%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 19, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). label Sep 19, 2026
`ReadBufferFromAzureBlobStorage::setReadUntilPosition` only recorded the new bound and
marked the download as not initialized, leaving the working buffer as it was. When the
bound was lowered below the end of the buffered bytes, those bytes were still handed out
past the bound, and the next download was then opened where the previous response ended
rather than at the bound, so the following `nextImpl` failed with the logical error
`Attempt to read beyond right offset (100 > 39)`. Formats that adjust the bound mid-read
(Parquet, Arrow, ORC through `AsynchronousBoundedReadBuffer`) are the callers.

The buffered bytes that lie past a lowered bound are now cut off the working buffer, and
the next download, if any, starts at the bound. Buffered bytes within the new bound are
kept, so raising the bound goes on reopening the download after them, as before, without
downloading them again. Setting the bound that is already in effect no longer drops the
download. The download itself is always reopened under a changed bound, since the current
one was requested under the previous bound.

`src/Disks/tests/gtest_azure_read_buffer_right_bound_change.cpp` lowers the bound below
already buffered bytes (fails with the logical error without the change), raises it after
some bytes were consumed, and sets the same bound twice, checking the bytes delivered, the
final position, and the offsets the fake endpoint was asked for.

This is one of the independent parts of
#115706, which was reverted in
#120419.
@alexey-milovidov alexey-milovidov changed the title Drop the buffered download when the right bound of an Azure read changes Cut the buffered bytes past a lowered right bound of an Azure read Sep 19, 2026
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review September 19, 2026 03:26
@clickhouse-gh

clickhouse-gh Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 3d099342df1d6fcb5175bbe3096bc19c0907682e was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

alexey-milovidov and others added 2 commits September 25, 2026 07:01
…ure read

`setReadUntilPosition` returned early when the download was already closed
by an earlier bound change, so a second change before the next read (raise
the bound keeping buffered bytes, then lower it below them) left the bytes
past the new bound in the working buffer; they were handed out and the next
`nextImpl` failed with `Attempt to read beyond right offset`. The trimming
is now applied on every bound change.

The abandoned response is also released right away (`data_stream.reset`)
instead of staying pinned until the next `initialize` or the destruction of
the buffer.

Added the test `BoundRaisedThenLoweredBeforeNextRead`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
alexey-milovidov and others added 3 commits October 2, 2026 20:21
…ReadUntilPosition`

Lifting the right bound is a bound change like any other: it now closes the
download opened under the previous bound, releasing the response, and keeps
the buffered bytes, which all lie within the lifted bound, instead of dropping
them and downloading them again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ange-discards-buffer

Master changed `read_until_position` of `ReadBufferFromAzureBlobStorage` to
`std::optional<size_t>` and added a simpler bound-change handling that drops the
whole working buffer. The conflict is resolved in favour of this PR's
reconciliation (keep the buffered bytes within the new bound, cut those past it,
close the download), adapted to the optional bound: `setReadUntilEnd` resets the
optional instead of passing 0, and a bound of 0 is now a real bound.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120957&sha=af0ee540af5c7dc5fd2210c37330c316fe8c28ed&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20parallel%29 (05219_streaming_queries_part_offset: TIMEOUT_EXCEEDED on a tiny INSERT; it also fails in PRs 106734, 94017, 94148 in the last 30 days) and provide a fix in a separate PR. If the fix is already in progress, link it here.

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating the 05219_streaming_queries_part_offset TIMEOUT_EXCEEDED; I'll follow up here with a fix PR (or my findings) shortly.

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

I opened the fix: #123897. The 8-row INSERT waited about 21 s on the filesystem cache priority lock, held by a concurrent query's debug/sanitizer-only full cache check (check_cache_probability) on the write-through Azure cache. The test's own max_execution_time = 20 turned that slow but correct run into Code: 159, so the PR removes it, as #113737 did for 04545. The other failures in #106734, #94148 and #94017 come from those PRs' own randomization changes (prefer_optimize_projection, forced reverse primary keys), not from this test.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged the latest master (clean merge, the PR diff is unchanged). The remaining red, Upgrade check (amd_release) with UNKNOWN_IDENTIFIER from MutatePlainMergeTreeTask, is unrelated and tracked in #120975 (fix in progress: #118499). @groeneai, thanks for #123897 for the 05219_streaming_queries_part_offset timeout.

@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 self-assigned this Oct 10, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 10, 2026
Merged via the queue into master with commit ed166bd Oct 10, 2026
150 checks passed
@alexey-milovidov
alexey-milovidov deleted the azure-read-bound-change-discards-buffer branch October 10, 2026 22:41
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). pr-bugfix Pull request with bugfix, not backported by default 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