Skip to content

Azure: fix ranged copy corruption and harden the copy path - #112876

Closed
arsenmuk wants to merge 1 commit into
arsenmuk/az-retry-callsitesfrom
arsenmuk/az-copy-ranged
Closed

arsenmuk wants to merge 1 commit into
arsenmuk/az-retry-callsitesfrom
arsenmuk/az-copy-ranged

Conversation

@arsenmuk

@arsenmuk arsenmuk commented Aug 1, 2026 •

Copy link
Copy Markdown
Member

Series: #112871 -> #112876 (this), #112872, #112873, #112874, #112875

Problem. A ranged Azure copy corrupts the destination — native CopyFromUri copies the whole source blob (ignoring offset/size) and the single-part read+write fallback reads from byte 0 — and a transient 403/auth after the server-side copy starts spawns a second writer to the same blob. Failure scenario:

copyFile(src, offset=X>0, size=S):
  native CopyFromUri   -> copies the ENTIRE src (ignores X,S)  -> corrupt
  single-part fallback -> reads [0,S) not [X,X+S)             -> corrupt
  transient 403 after StartCopyFromUri -> read+write fallback -> 2 writers to dest

Fix. Gate native copy on offset==0; seek the fallback via LimitSeekableReadBuffer(offset, total_size); only start the copy in the guarded block and poll the same operation on transient errors (no mechanism switch); retry the read+write upload writes through the shared retry helper.

Changes.

  • IO/AzureBlobStorage/copyAzureBlobStorageFile: offset==0 native-copy gate; seek the single-part fallback; poll-same-operation on transient errors; retry upload writes.
  • tests/integration/{test_backup_restore_azure_blob_storage,test_backup_restore_s3}: incremental Log-family / ranged-copy regression tests.

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fixed a ranged Azure Blob Storage copy corrupting the destination during incremental backups: server-side native copy ignored the requested offset and size, and the single-part read-and-write fallback read from the start of the source. Native copy is now used only for a full-object copy, the fallback seeks to the correct offset, and a transient error after a server-side copy has started no longer starts a second writer to the destination.

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1209bcf]

Summary: ✅


AI Review

Summary

This PR fixes the offset > 0 Azure ranged-copy corruption path and hardens retry handling around Azure copy operations, but the current head still misclassifies one class of ranged copy as a whole-object copy. Because the Azure native-copy gate only checks offset == 0, prefix-only ranges can still take CopyFromUri and overwrite the destination with the full source blob, so this is not ready to merge yet.

Findings
❌ Blockers
  • src/IO/AzureBlobStorage/copyAzureBlobStorageFile.cpp:439: copyAzureBlobStorageFile still treats offset == 0 as a whole-object copy, even though its contract and BackupWriterAzureBlobStorage::copyFileFromDisk allow arbitrary [start_pos, start_pos + length) ranges. A prefix copy with start_pos == 0 && length < source_size still enters CopyFromUri and writes the full source blob to the destination. Suggested fix: carry source_size into this decision and require length == source_size before using native copy, or make the same whole-object/range split in BackupWriterAzureBlobStorage::copyFileFromDisk, mirroring BackupWriterS3::copyFileFromDisk.
Final Verdict

Changes requested: the native Azure copy path still violates the range-copy contract for start_pos == 0 && length < source_size, which means partial Azure-to-Azure backup copies can still be corrupted.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.70% 86.70% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 79.00% 79.10% +0.10%

Changed lines: Changed C/C++ lines covered: 88/111 (79.28%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 86/111 (77.48%) · Uncovered code

Full report · Diff report

@arsenmuk
arsenmuk force-pushed the arsenmuk/az-copy-ranged branch from d0bdc3d to 73159d0 Compare August 2, 2026 08:57
@arsenmuk
arsenmuk changed the base branch from master to arsenmuk/az-retry-callsites August 2, 2026 08:58
@arsenmuk arsenmuk removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 3, 2026
@arsenmuk
arsenmuk force-pushed the arsenmuk/az-copy-ranged branch from 1b809ba to 0a1193d Compare August 4, 2026 12:25
@arsenmuk
arsenmuk force-pushed the arsenmuk/az-copy-ranged branch from 0a1193d to a55e304 Compare August 5, 2026 09:49
@SmitaRKulkarni SmitaRKulkarni self-assigned this Aug 10, 2026
@arsenmuk
arsenmuk marked this pull request as ready for review August 11, 2026 14:54
@arsenmuk
arsenmuk force-pushed the arsenmuk/az-copy-ranged branch from a55e304 to 1209bcf Compare August 12, 2026 20:35
if (settings->use_native_copy)
/// Server-side CopyFromUri copies the whole source blob, so native copy is valid only for a
/// full-object copy; a ranged copy (offset > 0) must go via read+write.
if (settings->use_native_copy && offset == 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

copyAzureBlobStorageFile still treats offset == 0 as “whole object”, but this helper's contract and the Azure backup caller both allow arbitrary [start_pos, start_pos + length) ranges. BackupWriterAzureBlobStorage::copyFileFromDisk forwards start_pos and length directly (src/Backups/BackupIO_AzureBlobStorage.cpp:214-226), so a prefix copy with start_pos == 0 && length < source_size still enters CopyFromUri here and copies the entire source blob into the destination.

BackupWriterS3::copyFileFromDisk fixed this exact invariant by checking whole_object = (start_pos == 0) && (length == source_size) before taking the native-copy path (src/Backups/BackupIO_S3.cpp:589-604). Azure needs the same distinction: either thread source_size into copyAzureBlobStorageFile and gate native copy on size == source_size, or make the whole-object/range decision in BackupWriterAzureBlobStorage::copyFileFromDisk before calling this helper. As written, prefix-only Azure-to-Azure backup copies are still corruptible.

@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1209bcfbf with master 405e218ff (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
Binary Master PR Δ
programs/clickhouse-stripped 692.09 MiB 689.12 MiB -2.97 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

14 object files changed (+261.44 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/IO/AzureBlobStorage/copyAzureBlobStorageFile.cpp.o 258.84 KiB 336.75 KiB +77.91 KiB (+30.10%)
src/CMakeFiles/clickhouse_common_io.dir/IO/AzureBlobStorage/copyAzureBlobStorageFile.cpp.o 258.84 KiB 336.75 KiB +77.91 KiB (+30.10%)
src/CMakeFiles/clickhouse_common_io.dir/Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/Azu… 402.88 KiB 425.21 KiB +22.33 KiB (+5.54%)
src/CMakeFiles/dbms.dir/Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureObjectStorage.… 402.88 KiB 425.21 KiB +22.33 KiB (+5.54%)
src/CMakeFiles/dbms.dir/Disks/IO/WriteBufferFromAzureBlobStorage.cpp.o 197.50 KiB 217.48 KiB +19.98 KiB (+10.12%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

"Will attempt to copy using read & write. source container = {} blob = {} and destination container = {} blob = {}",
e.what(), src_container_for_logging, src_blob, dest_container_for_logging, dest_blob);
}
else if (e.StatusCode == Azure::Core::Http::HttpStatusCode::Forbidden)

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.

Can we elaborate a bit on the log message, as to why this error is usually seen. Like for Unauthorized I had added this "Copy operation has thrown unauthorized access error, which indicates that the storage account of the source & destination are not the same."
Maybe we can add something similar to Forbidden & AuthenticationException too, wdyt ?

@clickhouse-gh clickhouse-gh Bot added the comp-azure Azure Blob Storage integration (credentials, multipart, Azure-specific IO). label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Dear @SmitaRKulkarni, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@arsenmuk

Copy link
Copy Markdown
Member Author

Fixed at #114975

@arsenmuk arsenmuk closed this Nov 22, 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-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants