Skip to content

Fix transient Azure errors misreported as broken data parts - #105891

Draft
arsenmuk wants to merge 28 commits into
masterfrom
arsenmuk/azure-error-handling-and-403
Draft

arsenmuk wants to merge 28 commits into
masterfrom
arsenmuk/azure-error-handling-and-403

Conversation

@arsenmuk

@arsenmuk arsenmuk commented May 26, 2026 •

Copy link
Copy Markdown
Member

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):

Fixed transient Azure Blob Storage errors during the RBAC permission-propagation window — a 403 Forbidden, a credential AuthenticationException, or a connect/request timeout surfaced as 408 — being misreported as POTENTIALLY_BROKEN_DATA_PART and raising false critical alerts. Such transient authorization/transport failures are now treated as retryable and, if they persist, surface as a plain Azure error instead of a phantom broken part.

Also fixes some following legacy issues:

  • 403 handling: treat Azure 403 as retryable in all callers, not only the startup access check — the background-merge read path (no longer reclassified as POTENTIALLY_BROKEN_DATA_PART), the write paths, the size/metadata lookups (tryGetFileSize/getRemoteFileMetadata/getObjectMetadata), and the server-side native copy (falls back to read+write on 403 instead of throwing)
  • AuthenticationException: credential/RBAC token-acquisition failures (Azure::Core::Credentials::AuthenticationException, a separate type from RequestFailedException) were not treated as retryable and could fail on the first attempt or hit the broken-part path — now retried within the same budget across the merge, write (incl. ADLS Gen2), size/metadata, delete, and native-copy paths
  • Request timeout (Closes #110724): a connect/request timeout was caught in the Poco transport and fabricated into a synthetic 408 response, which isRetryableAzureException then dropped as non-retryable — so a transient timeout could surface a healthy part as POTENTIALLY_BROKEN_DATA_PART. The timeout handler now throws Azure::Core::Http::TransportException (already retried by the SDK retry policy and by isRetryableAzureException), and a genuine 408 is treated as retryable
  • Native copy on a partial range (Azure): copyAzureBlobStorageFile used server-side CopyFromUri, which copies the whole source blob even for a ranged copy — now native copy is used only for a full-object copy (offset==0), and the read+write path that ranged copies fall back to seeks the source to offset. The same bug on S3 was fixed independently on master (copyS3FileRange), so this PR keeps only an incremental Log-family S3 native-copy regression test that exercises that fix end-to-end
  • Native-copy poll race: once StartCopyFromUri had started a server-side copy, a transient error from PollUntilDone fell back to read+write and started a second writer against the same destination while the original copy could still be in flight — now the read+write fallback applies only when starting the copy fails; a started copy is polled to completion on the same operation
  • exists() / delete retries: AzureObjectStorage::exists() and the object-delete paths (single removeObjectImpl, batch SubmitBatch, and per-object deferred responses) now retry a transient 403/AuthenticationException like the read/upload paths, via a shared retryAzureOperation helper (the per-object system.blob_storage_log accounting is preserved)
  • Post-upload verification: the check_objects_after_upload GetProperties() is now retried, so a transient failure of that check no longer turns an already-successful upload into a final failure
  • listObjects pagination bypass: it used the SDK MoveToNextPage, which skipped endpoint-prefix stripping on every page after the first — now paginates with the continuation token so every page is stripped
  • Batch-delete logging: a batch-delete request failure skipped the per-object loop and dropped system.blob_storage_log Delete events — now each object is logged before rethrowing
  • downloadRange null BodyStream: added a null check + RECEIVED_EMPTY_DATA in readBigAt
  • switched several Azure call sites to catch Azure::Core::RequestFailedException instead of only Azure::Storage::StorageException, so non-storage Azure failures don't bypass their status-code handling

Tests: a failpoint-based integration suite (test_azure_403_handling: 403 retried / not a broken part, credential auth-failure not a broken part, a 408 request timeout retried on read, batch-delete failure still logged), plus Azure and S3 incremental Log-family native-copy regression tests.

Closes #106298
Closes #110724

@clickhouse-gh

clickhouse-gh Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0657b45]

Summary: ✅


AI Review

Summary

This PR broadens Azure retry handling so transient 403, credential AuthenticationException, and timeout paths stop being misreported as POTENTIALLY_BROKEN_DATA_PART, and it adds targeted Azure/S3 regression tests. The main MergeTree/read/copy fixes look directionally right, but two write/delete paths still break the promised retry/accounting contract.

Findings

❌ Blockers

  • [src/Disks/IO/WriteBufferFromAzureBlobStorage.cpp:130] execWithRetry now treats normal write-path 403/408 as retryable, but the single-part Upload and multipart StageBlock callers reuse one MemoryBodyStream across attempts. After the first failed attempt the stream may already be consumed, so the retry can resend 0 or only a suffix of the payload and corrupt the object.
  • [src/Disks/DiskObjectStorage/ObjectStorages/AzureBlobStorage/AzureObjectStorage.cpp:384] removeObjectImpl now retries single deletes through retryAzureOperation, but it only logs final failures for RequestFailedException. An exhausted AuthenticationException bypasses this catch and drops the system.blob_storage_log Delete row, violating the deletion-logging contract and the PR’s stated per-object delete accounting.
Final Verdict

Request changes.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 26, 2026
@arsenmuk arsenmuk changed the title Arsenmuk/azure error handling and 403 Fix handling of 403 from Azure + error-handling improvements May 26, 2026
@arsenmuk
arsenmuk force-pushed the arsenmuk/azure-error-handling-and-403 branch from 23db35d to d906f01 Compare May 27, 2026 08:09
@arsenmuk
arsenmuk force-pushed the arsenmuk/azure-error-handling-and-403 branch from 673c72f to 62433f9 Compare June 1, 2026 15:16
@arsenmuk
arsenmuk force-pushed the arsenmuk/azure-error-handling-and-403 branch from c1feb11 to 180ef2d Compare June 3, 2026 12:57
@CheSema CheSema self-assigned this Jun 4, 2026
@arsenmuk
arsenmuk force-pushed the arsenmuk/azure-error-handling-and-403 branch from 7403d37 to 775580d Compare June 5, 2026 06:03
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Jun 5, 2026
Comment thread src/Core/Settings.cpp Resolved
arsenmuk and others added 9 commits July 30, 2026 12:27
copyAzureBlobStorageFile's native server-side copy caught only Unauthorized
(401) and BadRequest (400) and rethrew everything else, so a transient
RBAC-propagation 403 surfaced as a hard error and a credential
AuthenticationException escaped the catch entirely -- both contradicting the
change's "treat Azure 403 as retryable in all callers" goal. Route 403 and
AuthenticationException to the existing read+write fallback, whose retry
loops already treat them as retryable.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
getObjectMetadata issued GetProperties() with no retry, so a transient 403
or a credential AuthenticationException escaped raw. It is on the copyObject
path and under tryGetObjectMetadata (which maps only NotFound), so the
transient failure was not shielded by any retry loop. Wrap it in the same
isRetryableAzureException + AuthenticationException retry policy the read
path uses.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Remove the single-use azureCopyShouldTryNativeCopy predicate (its only consumer,
the unit test, was dropped) and inline the offset==0 check into
copyAzureBlobStorageFile.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ReadBufferFromAzureBlobStorage and AzureObjectStorage each had an identical
anonymous-namespace getBlobPropertiesWithRetry (retry GetProperties on a
retryable 403 / credential AuthenticationException with bounded backoff). Move
it to a single IO/AzureBlobStorage/getBlobPropertiesWithRetry.{h,cpp} and call
it from both.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…broken part

A connect/request timeout was caught in PocoAzureHTTPClient and turned into a synthetic HTTP 408 RawResponse. isRetryableAzureException classified 408 as non-retryable, so a transient timeout on the read path failed on the first attempt (bypassing the max_single_read_retries budget) and could be reported as POTENTIALLY_BROKEN_DATA_PART (issue #110724) -- the same false-critical symptom this PR already fixes for 403 and credential failures.

Throw Azure::Core::Http::TransportException from the timeout handler instead of fabricating a 408, so the SDK RetryPolicy retries it and isRetryableAzureException treats it as retryable; also treat a genuine 408 as retryable as a safety net. Add a ONCE azure_inject_timeout failpoint and a read-retry regression test (one injected 408 is retried away and the read still returns its data).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Generalize the getBlobPropertiesWithRetry retry policy (retry a transient 403 / other retryable RequestFailedException and a credential AuthenticationException with bounded backoff) into a reusable retryAzureOperation() so the delete and native-copy paths can share the exact same policy. getBlobPropertiesWithRetry now delegates to it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
check_objects_after_upload issued a raw GetProperties() after a successful upload; a transient 403/auth on that verification turned a good write into a final failure. Route it through getBlobPropertiesWithRetry.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
exists() ran GetProperties() under only a NotFound catch, and the delete paths (single, batch SubmitBatch, and per-object results) were single-shot, so a transient RBAC-propagation 403 or AuthenticationException failed them on the first attempt. Route them through the shared bounded retry helper, preserving the NotFound->absent mapping and the per-object blob_storage_log accounting (logged once, on the final outcome).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
A transient 403/auth from PollUntilDone does not mean the copy failed -- only that polling failed. Falling back to read+write there starts a second writer to the destination while the server-side copy may still be in flight. Only start the copy inside the fallback-eligible try; once StartCopyFromUri has returned, poll to completion on the same operation under the bounded retry policy and surface the error instead of switching mechanisms.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@arsenmuk
arsenmuk force-pushed the arsenmuk/azure-error-handling-and-403 branch from 7932a53 to 7f2398b Compare July 30, 2026 12:40
@arsenmuk arsenmuk changed the title Fix handling of 403 from Azure + error-handling improvements Fix transient Azure errors (403 / credential auth / request timeout) misreported as broken data parts Jul 30, 2026
The per-object delete retry used a bare `catch (...)` to fall through and record the original error; ClickHouse's check_style `catch_all` rule flags a catch-all that neither rethrows, logs, nor saves the exception. Catch the specific Azure exception types (`RequestFailedException` / `AuthenticationException`) and record the retry's own error instead.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@arsenmuk arsenmuk changed the title Fix transient Azure errors (403 / credential auth / request timeout) misreported as broken data parts Fix transient Azure errors misreported as broken data parts Jul 30, 2026
arsenmuk added 4 commits July 31, 2026 11:21
The read+write fallback in copyAzureBlobStorageFile (a ranged copy, or when a native server-side copy is skipped/unavailable) issued its Upload / StageBlock / CommitBlockList calls with no retry, so a transient destination 403 / auth / timeout failed the copy on the first attempt while the read side already retried. Wrap each write in the shared retryAzureOperation helper (budget max_single_download_retries) and rebuild the body stream per attempt so a retry re-sends from the start.

Add a write-scoped ONCE failpoint azure_inject_forbidden_on_upload and an integration test that forces the read+write path (allow_azure_native_copy=0) and asserts the backup still succeeds.
…urning empty checksums

checkDataPart caught a retryable exception (transient read, memory limit, or a retryable Azure 403/timeout) and returned empty checksums. Four of its six callers treat that empty result as a successful check: CHECK TABLE reports the part OK, loadChecksums persists an empty checksums.txt, the replicated part-check thread logs "looks good", and the CHECK TABLE recount branch instead throws a false mismatch. Rethrow the retryable exception instead -- every caller already has an isRetryableException guard that expects it, so the failure is surfaced/retried rather than masquerading as verified.

Remove the now-redundant empty-result guard on the fetch path in DataPartsExchange. Broken-part suppression (POTENTIALLY_BROKEN_DATA_PART) is unchanged: it is gated at the read/reportBroken site, not by checkDataPart's return value.
Both catch arms of runWithRetries repeated the same budget-check / log_event / throw / warn / sleep / backoff sequence. Factor it into one retry_or_throw lambda. The non-retryable HTTP branch keeps its own immediate throw so the numeric status code stays in the message. Behavior-preserving.
The one-line wrapper had its own translation unit; move it inline into the header (all call sites already include it) and drop the .cpp.
@arsenmuk

Copy link
Copy Markdown
Member Author

Addressed the "checkDataPart masquerades transient errors as verified" blocker in d5e36e4. checkDataPart now rethrows a retryable exception instead of returning empty checksums. That empty return was silently treated as a successful check by four of its six callers: CHECK TABLE reporting the part OK (StorageMergeTree.cpp:3436), loadChecksums persisting an empty checksums.txt (IMergeTreeDataPart.cpp:2034), the replicated part-check thread logging "looks good" (ReplicatedMergeTreePartCheckThread.cpp:401), and the CHECK TABLE recount branch (StorageMergeTree.cpp:3415) instead raising a false mismatch. Each caller already has an isRetryableException(...) guard that expects a rethrow, so a transient failure is now surfaced / retried rather than masquerading as verified. The now-redundant empty-result guard on the fetch path (DataPartsExchange.cpp) is removed. Broken-part suppression (POTENTIALLY_BROKEN_DATA_PART) is unchanged — it is gated at the read/reportBroken site, not by checkDataPart's return — confirmed by the unchanged Azure-403 merge/read tests. Covered by test_azure_403_check_table_surfaces_transient_not_verified.

catch (const Azure::Core::RequestFailedException & e)
{
if (i == num_tries - 1 || !isRetryableAzureException(e, /* may_be_provisioning_access */ write_settings.is_initial_access_check))
if (i == num_tries - 1 || !isRetryableAzureException(e))

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.

Retrying 403/408 here widens the normal blob-write path, but the two body-carrying callers still keep a single MemoryBodyStream outside the retry loop: the single-part Upload in preFinalize and the multipart StageBlock in writePart. After the first attempt the SDK may already have consumed that stream, so the next Upload/StageBlock can resend 0 or only a suffix of the payload instead of the original bytes.

copyAzureBlobStorageFile had to rebuild its MemoryBodyStream inside retryAzureOperation for exactly this reason. Please rebuild or rewind the request body per attempt here too before making a normal write-path 403/timeout retryable.

}
}
catch (const Azure::Storage::StorageException & e)
catch (const Azure::Core::RequestFailedException & e)

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.

removeObjectImpl now routes single-object deletes through retryAzureOperation, but this catch only records system.blob_storage_log failures for RequestFailedException. If token acquisition keeps failing until the retry budget is exhausted, retryAzureOperation rethrows Azure::Core::Credentials::AuthenticationException, which bypasses this block and drops the Delete log row for that deletion attempt.

That violates the deletion-logging contract, and it also contradicts the PR body's "removeObjectImpl preserves per-object delete accounting" claim. Please catch Azure::Core::Credentials::AuthenticationException here as well, log it once, and then rethrow.

@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 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.60% 78.60% +0.00%

Changed lines: Changed C/C++ lines covered: 198/294 (67.35%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Dear @arsenmuk, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

This branch has not been deployed

No deployments
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

Projects

None yet

4 participants