Skip to content

Cherry pick #120910 to 26.7: Bound a sequential Azure read by the locally set right bound, not by the response length - #123802

Merged
robot-ch-test-poll3 merged 30 commits into
backport/26.7/120910from
cherrypick/26.7/120910
Oct 4, 2026
Merged

robot-ch-test-poll3 merged 30 commits into
backport/26.7/120910from
cherrypick/26.7/120910

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #120910

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 28 commits September 18, 2026 19:38
…the response length

`ReadBufferFromAzureBlobStorage::initialize` derived `total_size` from
`BodyStream::Length`, which is the `Content-Length` chosen by the remote endpoint.
With `read_until_position = 100`, `offset = 0` and a 64-byte reading buffer, an
endpoint that answers the 100-byte ranged request with `Content-Length = 128` made
`nextImpl` hand out 64 bytes twice and only trip the right-bound check on the
following call, so 28 bytes from outside the requested range had already escaped into
the caller. With a reading buffer larger than the range, all 28 escape in a single call.

Derive the bound of the current download from `read_until_position`, which is set
locally by the caller, and let the length of the response only shorten it. The size
of the blob from the same response is not used for this: it comes from the same
untrusted place, and an endpoint that answers a ranged request with `200 OK` would
make it truncate a legitimate read.

`src/Disks/tests/gtest_azure_read_buffer.cpp` gains regressions that drive `nextImpl`
through `setReadUntilPosition` against a fake Azure HTTP transport whose response is
longer than the requested range, with a reading buffer both smaller and larger than
the range. Without the change they fail with
`Attempt to read beyond right offset (128 > 99)`.

This is one of the independent parts of
#115706, which was reverted in
#120419. The positioned read path,
`readBigAt`, was fixed separately in
#119054.
`ReadBufferFromAzureBlobStorage::setReadUntilPosition` only set
`read_until_position` and dropped `initialized`, leaving `working_buffer` and
`offset` alone. The bytes that the previous, wider bound allowed to buffer stayed
in the working buffer, so a caller that tightened the bound after a partial read
still got them: fill 64 bytes for a 100-byte bound, consume 10, call
`setReadUntilPosition(20)`, and `ReadBuffer::read` keeps returning bytes `10..63`
before the `read_until_position < offset` guard fires on the later refill. That
breaks the `supportsRightBoundedReads` contract, which promises that the new
bound applies before any further bytes are exposed.

Give up the buffered bytes and rewind `offset` to the position the caller has read
up to, the same way `setReadUntilEnd` and `ReadBufferFromS3::setReadUntilPosition`
do, so the next `nextImpl` downloads the range under the new bound. Setting the
same bound again is now a no-op instead of a forced re-download.

`src/Disks/tests/gtest_azure_read_buffer.cpp` gains a range-aware fake transport
and regressions that tighten the bound after a partial read (with a well-behaved
and with an overlong endpoint) and check that `eof` is reported exactly at the new
bound, plus a widening case as a control. Without the change the tightening cases
read 54 bytes past the new bound and then throw
`Attempt to read beyond right offset`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… range is a bound

`ReadBufferFromAzureBlobStorage` used `0` as the "no bound" sentinel for
`read_until_position`, so `setReadUntilPosition(0)` - the empty range `[0, 0)` -
was taken for an unbounded read: `nextImpl` and `initialize` both tested the
bound for being nonzero, and the reader delivered the contents of the blob
instead of reporting EOF, contradicting `supportsRightBoundedReads`.

The bound is now a `std::optional<size_t>`, which is empty only when the caller
has not set one or has called `setReadUntilEnd`. The single call site that
passed a literal `0` for "no bound", `AzureObjectStorage::readObject`, passes
`std::nullopt`.

Three regressions in `gtest_azure_read_buffer.cpp`: `EmptyRange` (a bound of
zero reports EOF without issuing a download at all), `EmptyRangeThenReadUntilEnd`
(`setReadUntilEnd` still lifts it) and `TightenedToEmptyRangeAfterPartialRead`
(a bound below the current position followed by a `seek`, which
`ReadBuffer::setReadUntilPosition` recommends supporting). All three fail on a
control binary that keeps the old zero sentinel.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…d offset

The endpoint chooses the range it answers a ranged request with. It is
allowed to ignore the requested range and answer `200 OK` with the whole
blob, and a misbehaving one can answer with a different range altogether.
In either case the body was handed to the caller as if it were the bytes
at the requested offset: after `seek(100)` and `setReadUntilPosition(200)`
a response starting at byte `0` delivered bytes `0..99` under offsets
`100..199`, a wrong slice of the blob.

`checkResponseStartsAtRequestedOffset` compares the range of the response
with the offset that was requested and rejects a mismatch, in both
`initialize` (the sequential path) and `readBigAt`. There is no fallback
that skips the unwanted prefix: only the correct slice is accepted.

New gtests `AzureReadUntilPosition.RejectsResponseStartingAtWrongOffset`
and `AzureReadBigAt.RejectsResponseStartingAtWrongOffset` read from a
nonzero offset through `RangeResponseTransport`, which answers every
request with the blob from its beginning, and
`AzureReadUntilPosition.SeekToNonZeroOffsetWithHonestEndpoint` pins that
an endpoint honouring the range still works. A control binary with the
check removed fails both rejection tests; 15/15 `AzureRead*` pass with it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Conflict in `src/Disks/IO/ReadBufferFromAzureBlobStorage.cpp`: master has
landed `checkReturnedRange`, which does exactly what the
`checkResponseStartsAtRequestedOffset` helper added here did - it compares
`ContentRange.Offset` of the response with the requested offset and throws,
in both `initialize` and `readBigAt`. Resolved by keeping master's helper and
dropping the duplicate one, along with the two gtests that only pinned that
behaviour, which master's new `gtest_azure_read_buffer_range_check.cpp`
covers. The rest of this PR - bounding a sequential read by the locally set
`read_until_position` instead of the response length - is unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…metadata

`AzureObjectStorage::readObject` passed `std::nullopt` as `read_until_position`,
so a plain whole-object read had no trusted right bound at all and its size came
from the length of the response, which is exactly what this PR set out to stop
trusting. An endpoint answering a request for a 100-byte object with 128 bytes
handed the caller 28 bytes past the end of the object on the default path used
by `ObjectStorageSourceReader::open` and `readSmallObjectAndGetObjectMetadata`.

`StoredObject::bytes_size` is the locally known bound for that case, so it is now
threaded through as the initial `read_until_position`, with the `UnknownSize`
sentinel (and `0`) mapped to `std::nullopt` exactly as in
`S3ObjectStorage::readObject`.

Two gtests on the `readObject` path: `AzureReadObject` checks that a 100-byte
object served by a 128-byte response yields 100 bytes, and that an object of
unknown size still reads to the end. A control build keeping `std::nullopt`
fails the first with `Which is: 128` against `Which is: 100`; 19/19 `AzureRead*`
pass with the fix.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`AzureObjectStorage::readObject` mapped both `StoredObject::UnknownSize` and
`0` to "no right bound". `UnknownSize` is the default-constructed value of
`StoredObject::bytes_size`, so it is the only sentinel a caller that does not
know the size leaves behind; `bytes_size == 0` describes a genuinely empty
object and is a bound like any other.

With `0` taken for "unknown", an empty object was read unbounded, so an
endpoint answering the `GET` with a non-empty body handed the caller bytes of
an object whose trusted metadata says it has none - exactly the leak this
change is about, on the one input where the bound is tightest.

`S3ObjectStorage::readObject` keeps mapping `0` to "no bound" because
`ReadBufferFromS3::read_until_position` is a plain `size_t` whose `0` already
means "unbounded", so it cannot express the empty range at all.
`ReadBufferFromAzureBlobStorage` can, since the bound became a
`std::optional<size_t>` in an earlier round of this review.

New regression `AzureReadObject.EmptyObjectReadsAsEmpty`: a `StoredObject`
of size `0` against an endpoint serving 128 bytes must read as empty. A
control build keeping the old condition fails it; 20/20 `AzureRead*` pass
with the fix.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`AzureObjectStorage::readObject` bounds the read by `StoredObject::bytes_size`, so an object
whose size is known to be zero is read as empty without issuing a single `Download` request.
`readSmallObjectAndGetObjectMetadata` then called `getObjectMetadataFromTheLastRequest`, which
threw `NOT_INITIALIZED` because there was no request to take the metadata from.

Request the metadata explicitly on that path. New gtest `AzureReadObject.EmptyObjectStillReportsMetadata`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…tadata`

`ObjectMetadata::etag` keeps the quotes of the header as the Azure SDK returns them.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`StoredObject::bytes_size` and `StoredObject::etag` describe one generation
of the object. `AzureObjectStorage::readObject` threaded the size through as
a hard right bound but dropped the `ETag`, so a blob that was replaced with a
longer one after the listing was silently truncated to the stale size
instead of being rejected.

`ReadBufferFromAzureBlobStorage` now takes the expected `ETag`, sends every
`Download` with it as the `If-Match` access condition, checks the `ETag` of
the response against it for endpoints that ignore the condition, and maps
the `412 Precondition Failed` to the new `AZURE_OBJECT_CHANGED_DURING_READ`
error, which is not retried. `readObject` passes `object.etag`, and the
zero-byte path of `readSmallObjectAndGetObjectMetadata`, which issues no
download, checks the `ETag` of the properties instead.

Six gtests cover the honest endpoint, an endpoint that ignores `If-Match`,
the unpinned read, the zero-byte metadata path and `readBigAt`; a control
build with the pinning disabled fails five of them.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ate the Azure `If-Match` pin by `azure_validate_etag_on_read`

Two review findings on the object-storage read pipeline, now that
`AzureObjectStorage::readObject` bounds the read by `StoredObject::bytes_size`
and pins it to `StoredObject::etag`:

1. The cluster functions' `skip_object_metadata` placeholder is a
   default-constructed `ObjectMetadata` with `size_bytes == 0`,
   `is_size_known == true` and `is_fetched == false`. With
   `cluster_table_function_split_granularity = 'bucket'` the initiator
   opens every file itself in `ObjectIteratorSplitByBuckets::next` through
   `createReadBuffer` with exactly that placeholder, so on Azure the file was
   bounded to zero bytes and read as empty. `createReadBuffer` now passes
   `StoredObject::UnknownSize` unless the metadata was really fetched.
   A new integration test reads a Parquet blob through
   `azureBlobStorageCluster` with bucket granularity and compares it with the
   plain `azureBlobStorage` read.

2. `StoredObject::etag` was only set under `s3_validate_etag_on_read`, whose
   documented scope is S3 and GCS, so Azure table reads with that setting off
   applied the listed size as a hard bound without arming `If-Match`. Azure
   gets its own setting, `azure_validate_etag_on_read` (default on, recorded
   in the 26.10 settings history), which controls both the `ETag` pin and the
   refresh of the unfetched placeholder for `azureBlobStorageCluster`, the
   same way `s3_validate_etag_on_read` does for `s3Cluster`. The
   `AZURE_OBJECT_CHANGED_DURING_READ` messages now name the setting.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…readObject` path

Every Azure integration test on the previous head failed with
`AZURE_OBJECT_CHANGED_DURING_READ` (`ETag changed from 0x2092E2E4D16C760 to
"0x2092E2E4D16C760"`): a blob listing spells the `ETag` unquoted in its XML
body, while the `ETag` header of a download is quoted, so the generation check
rejected every honest read. `ReadBufferFromAzureBlobStorage::quotedETag` now
canonicalizes both sides to the quoted spelling, which is also what `If-Match`
carries.

Also address review: the plain `readObject` path of an object listed as empty
issued no download, so nothing pinned it to the listed generation and a blob
replaced with a non-empty one was returned as a clean empty file. The
generation is now checked on the properties in `readObject` as well
(`AzureObjectStorage::getObjectMetadataOfListedGeneration`, shared with
`readSmallObjectAndGetObjectMetadata`).

New gtests: `AzureReadObject.EmptyObjectRejectsReplacedBlob`,
`AzureReadObject.AcceptsTheUnquotedETagOfAListing`,
`AzureReadObject.EmptyObjectAcceptsTheUnquotedETagOfAListing`,
`AzureQuotedETag.Spellings`.

CI: https://s3.amazonaws.com/clickhouse-test-reports/PRs/120910/1be102e0a04ee2752d16a4a978e21f0bb55e7651/pr/integration_tests_amd_asan_ubsan_db_disk_7_8/job.log
PR: #120910

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…cally

Conflict in `AzureObjectStorage::readObject`: master (#121200 area) turned
`connection_params` into a `MultiVersion` and dropped the `auth_method_`
constructor parameter; this branch passes `object.etag` as the last
`ReadBufferFromAzureBlobStorage` argument. Kept both, and removed the
`AuthMethod` argument from the two `AzureObjectStorage` constructions in
`gtest_azure_read_buffer.cpp`.
…e` of an Azure read

`AzureObjectStorage::readObject` bounds the read by `StoredObject::bytes_size`, but
`ReadBufferFromAzureBlobStorage::tryGetFileSize` still learned the size from a live
`GetProperties` request, which describes whatever generation the blob has by now.
`CachedInMemoryReadBufferFromFile` sizes itself by `getFileSize` in its constructor and
throws `UNEXPECTED_END_OF_FILE` when the inner buffer ends earlier, so with
`azure_validate_etag_on_read = 0` a blob grown since it was listed turned the bounded read
into an exception on the page-cache path instead of the stale-size prefix.

`ReadBufferFromAzureBlobStorage` takes an optional `file_size_` (the same shape as
`ReadBufferFromS3`), and `readObject` passes the known size both as the right bound and as
the file size, so every wrapper sees one size and no request is needed to learn it.

Tests: `AzureReadObject.FileSizeIsTheListedSize` (fails on a control build that keeps
`std::nullopt` for the file size: `getFileSize` returns the `0` of the endpoint's `HEAD`
instead of `100`) and `AzureReadObject.UnknownObjectSizeIsAskedFromTheEndpoint`.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…cally

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

Also resolved conflicts in:
- `src/Common/ErrorCodes.cpp`

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…eration-pinned for lazy materialization

`AzureObjectStorage::readObject` now pins every `GET` to `StoredObject::etag` with `If-Match`,
so the lazy Parquet reread on Azure is as generation-safe as on S3 with `s3_validate_etag_on_read`.
Wire the setting into `ReadFromObjectStorageStep::canUseLazyMaterialization` and
`LazyReadFromObjectStorageSource::updatePipeline`, so wide Parquet reads on Azure are no longer
kept on the single-pass plan.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…position`

#120963, merged into `master`, added code to
`ReadBufferFromAzureBlobStorage::nextImpl` that treats `read_until_position` as an `off_t`
with zero meaning no bound. In this PR it is a `std::optional<size_t>`, so the merged code did
not compile in the merge queue. Dereference the bound where it is known to be set.

Merge queue failure: https://s3.amazonaws.com/clickhouse-test-reports/REFs/gh-readonly-queue/master/pr-120910-38c3aeff5b23272021e0a5cc0686e63b1607d401/c20dae0928d9442f3de1361797cf66d0267cf361/mergequeueci/build_amd_binary/job.log
PR: #120910

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ds short

With #120963 from `master`, a response that ends
before the right bound is reopened, and a read that still cannot reach the bound fails with
`UNEXPECTED_END_OF_FILE` instead of silently ending at the end of the short response. The test
`AzureReadUntilPosition.ShortRangeResponse` of this PR pinned the old behaviour, so it now
expects the exception. It still guarantees that no bytes past the response reach the caller.

Merge queue failure: https://s3.amazonaws.com/clickhouse-test-reports/REFs/gh-readonly-queue/master/pr-120910-38c3aeff5b23272021e0a5cc0686e63b1607d401/c20dae0928d9442f3de1361797cf66d0267cf361/mergequeueci/build_amd_binary/job.log
PR: #120910

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bound a sequential Azure read by the locally set right bound, not by the response length
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Oct 4, 2026
alexey-milovidov and others added 2 commits October 4, 2026 04:24
Resolve the conflicts of the cherry-pick of #120910 to 26.7. The release
branch lacks several prerequisites of the original change, so only its own
part is kept:
- `checkReturnedRange`, the premature-end-of-response handling and the lazy
  `getBlobClient` are not on 26.7; the `ETag` pinning and the local right
  bound are applied to the existing `blob_client` code instead.
- Lazy materialization from object storage is not on 26.7, so the changes to
  `LazyReadFromObjectStorageSource`, `canUseLazyMaterialization` and the test
  `05259_lazy_materialization_azure_etag_pinned` are dropped.
- `gtest_azure_read_buffer.cpp` is not on 26.7 (it came with #119054, which is
  not backported), so it is not added here.
- The new setting `azure_validate_etag_on_read` is recorded in the "26.8"
  block of `SettingsChangesHistory.cpp`, as other backported settings are.
On 26.7 the last error code is 1009, so `END` must be raised to cover the
new code 1021, which keeps the same number as on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Resolved the conflicts with backport/26.7/120910 (merge commit 71e690c, plus 99c884a). The 26.7 branch lacks several prerequisites of #120910, so only its own part is ported:

  • checkReturnedRange, the premature-end-of-response handling and the lazy getBlobClient (from earlier master PRs that were not backported) are not on 26.7. The local right bound (std::optional read_until_position, getTotalSizeOfCurrentDownload), the setReadUntilPosition fix, and the ETag pinning (If-Match, checkReturnedGeneration, rethrowIfGenerationChanged) are applied to the existing blob_client code instead.
  • Lazy materialization from object storage is not on 26.7, so the changes to LazyReadFromObjectStorageSource and canUseLazyMaterialization and the test 05259_lazy_materialization_azure_etag_pinned are dropped.
  • gtest_azure_read_buffer.cpp came with Fix unbound read in Azure::readBigAt #119054, which is not backported, so it is not added here.
  • AZURE_OBJECT_CHANGED_DURING_READ keeps code 1021 as on master, and ErrorCodes::END is raised to cover it.
  • azure_validate_etag_on_read is recorded in the "26.8" block of SettingsChangesHistory.cpp, the same way other settings backported to 26.7 are.

The touched translation units compile on the 26.7 tree. A human should still review the port before it is merged.

@robot-ch-test-poll3
robot-ch-test-poll3 merged commit 252d774 into backport/26.7/120910 Oct 4, 2026
1 check passed
@robot-ch-test-poll3
robot-ch-test-poll3 deleted the cherrypick/26.7/120910 branch October 4, 2026 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants