Skip to content

Fix S3 native copy silently ignoring the requested byte range - #112392

Merged
murphy-4o merged 2 commits into
ClickHouse:masterfrom
murphy-4o:s3-ranged-native-copy-fix
Jul 30, 2026
Merged

murphy-4o merged 2 commits into
ClickHouse:masterfrom
murphy-4o:s3-ranged-native-copy-fix

Conversation

@murphy-4o

@murphy-4o murphy-4o commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Description

Extracted from #111030 at review request.

copyS3File() takes src_offset/src_size, but picked its route by size alone: a partial copy below max_single_operation_copy_size went through CopyObject, which carries no byte range and copies the entire source. The range parameters silently did not work.

Split into two entry points rather than adding a flag (a prefix range [0, n) is partial despite offset == 0):

  • copyS3File(...) — whole object; the src_offset parameter is removed.
  • copyS3FileRange(offset, size, ...) — always multipart UploadPartCopy, never CopyObject.

Same split on the reader: IBackupReader::copyFileToDisk() is unchanged, and a new copyFileRangeToDisk() copies an explicit range (buffered default in BackupReaderDefault, native override in BackupReaderS3). BackupWriterS3::copyFileFromDisk() compares the copied length against the real source size — the encrypted size for encrypted sources — instead of guessing from the offset.

Not reachable today: the only variable-offset caller is taken solely for immutable-file entries, which never match a base backup partially. copyFileRangeToDisk() is used by #111030.

Version info

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

copyS3File() accepted `src_offset`/`src_size` to copy a part of a source
object, but nothing told it whether the copy was actually partial: the route
was chosen purely by size, and the single-operation path (CopyObject) carries
no byte range. A partial copy that fell under max_single_operation_copy_size
was therefore issued as a whole-object CopyObject, copying the entire source
instead of the requested range.

Rather than add a flag to the existing function, split the two meanings into
two explicit entry points, so a whole-object copy and a ranged copy cannot be
confused:

  * copyS3File(..., src_size, ...) copies the whole source object.
  * copyS3FileRange(..., src_offset, src_size, ...) copies exactly
    [src_offset, src_offset + src_size) of a larger source. It always uses
    multipart UploadPartCopy, which sets a CopySourceRange per part, or the
    buffered ranged read when multipart copy is unavailable; it never reaches
    CopyObject (enforced by a chassert).

Both forward to a shared helper with an internal is-ranged flag, so no caller
can leave that decision at a wrong default.

The backup readers get the same split: IBackupReader::copyFileToDisk() keeps
its whole-file meaning, and the new copyFileRangeToDisk() copies exactly
[offset, size). The buffered implementation of the latter lives once in
BackupReaderDefault, so the Disk, File, Azure and Memory readers inherit a
correct ranged copy without touching their whole-object fast paths (fs::copy,
IDisk::copyFile and the Azure native copy carry no range). Only
BackupReaderS3 overrides it, to use copyS3FileRange().

BackupWriterS3::copyFileFromDisk() now decides between the two by comparing
the requested length against the real size of the source file, instead of
testing `start_pos != 0`: a prefix [0, length) of a bigger file is a range
too. For an encrypted source the entry's size is the encrypted one, so the
comparison uses getEncryptedFileSize(); the native path is only taken when the
disk's `is_encrypted` matches `copy_encrypted`, so that call is never made on
a disk without encryption support. The size comes from the disk's metadata,
which the preceding getBlobPath() call has already consulted, so this costs no
extra request to object storage.

Tests: CopyS3FileRoutingTest covers a whole object -> CopyObject, a mid-object
range -> UploadPartCopy, and a prefix range [0, n) with n < size ->
UploadPartCopy.

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

clickhouse-gh Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5c28acb]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
01666_merge_tree_max_query_limit FAIL cidb IGNORED

AI Review

Summary

This PR splits whole-object and ranged S3 copy paths so partial copies can no longer silently fall through CopyObject, routes small-source ranged copies through buffered reads to respect S3's UploadPartCopy limitation, and threads that contract through the backup reader/writer call sites with focused routing tests. I reviewed the current diff and prior discussion, verified the earlier small-source failure is fixed in the final code, and did not find remaining correctness, compatibility, or evidence gaps.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 29, 2026
Comment thread src/IO/S3/copyS3File.cpp Resolved
AWS accepts x-amz-copy-source-range only when the source object is greater
than 5 MB and answers InvalidRequest otherwise, so a range of a smaller source
cannot be server-side copied at all. Decide before issuing any request: a
ranged copy of a source at or below that size goes through the buffered ranged
read, everything else keeps the multipart UploadPartCopy path.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@murphy-4o
murphy-4o marked this pull request as ready for review July 29, 2026 09:38
@murphy-4o
murphy-4o requested a review from jkartseva July 29, 2026 12:30
@jkartseva jkartseva self-assigned this Jul 29, 2026

@jkartseva jkartseva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.40% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 227/261 (86.97%) · Uncovered code

Full report · Diff report

@murphy-4o
murphy-4o added this pull request to the merge queue Jul 30, 2026
Merged via the queue into ClickHouse:master with commit 24fce03 Jul 30, 2026
351 of 354 checks passed
@murphy-4o
murphy-4o deleted the s3-ranged-native-copy-fix branch July 30, 2026 06:41
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 30, 2026
@filimonov filimonov mentioned this pull request Oct 7, 2026
10 of 30 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog 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