Repository navigation
Fix spurious PreconditionFailed on a retried Iceberg metadata write - #113994
alexey-milovidov merged 3 commits into
Conversation
Iceberg writes its metadata with If-None-Match: *, which makes the PUT non-idempotent. If the object store drops the response after the object has already been written, or MinIO answers the bogus NO_SUCH_KEY that WriteBufferFromS3 already retries, the request is re-sent, the precondition now fails against the object this very request wrote, and the query fails with Code: 499 PreconditionFailed even though the write succeeded. IcebergMetadata::createInitial swallows that only under IF NOT EXISTS, so a plain CREATE TABLE surfaces it. Public CI shows 6 occurrences over 6 unrelated PRs and 4 tests in 120 days, none on master. A conditional create-if-absent PUT now carries a random per-buffer token in S3 custom metadata. On a 412 the buffer issues one HeadObject and reports success only if the stored object carries that exact token, so only a replay of this request is accepted. Token absent, token different, object absent, or a failed HeadObject all throw the original error, keeping a genuinely pre-existing table failing as before. Comparing content instead would not be sound: version-hint.text is written as the literal "1", so two independent writers produce identical bytes and a comparison would accept a foreign object, weakening the compare-and-swap. A single-part ETag is not usable either, because server-side encryption is plumbed into this request, so the ETag is not the body MD5. Custom metadata is not object content, so the JSON other Iceberg engines read is unchanged. Both the token and the recovery are guarded on object_storage_write_if_none_match, whose only non-test producer is the Iceberg write path, so ordinary S3 writes send byte-identical requests and issue no extra request. CompleteMultipartUpload accepts the same header, but Iceberg metadata files are kilobytes against a 32 MiB max_single_part_upload_size, so that path is never taken here. The version-hint update uses If-Match, and its caller already catches and retries without reporting hint-write failures, so a 412 there costs a retry rather than a failed query. Co-Authored-By: Claude Fable 5 <[email protected]>
The write token added in the previous commit only guarded the single-part conditional PUT. CompleteMultipartUpload sets the same If-None-Match header, throws on 412, and is retried by the SDK on a network error, so it carries the identical defect: a completion whose response is lost is replayed, sees the object it just wrote, and fails a query whose write had succeeded. That path was previously dispositioned as unreachable because Iceberg metadata is kilobytes against a 32 MiB single-part threshold. That reasoning was wrong: 32 MiB is the default of s3_max_single_part_upload_size, an ordinary session setting that validateUploadSettings does not constrain, so 0 is accepted and reaches the Iceberg write through S3ObjectStorage::writeObject. With it at 0 a plain CREATE TABLE ... ENGINE = IcebergS3(...) takes the multipart path. A reachability bound that rests on a configurable default is not a reachability proof. The token is now stamped by createMultipartUpload, whose custom metadata lands on the completed object, so the same HeadObject check recognises a replayed completion. Both senders of the conditional header share one helper for the metadata merge rather than duplicating it. isTransientCompleteMultipartUploadError is untouched: a 412 is recognised, never retried. Three further arms pin the guard's remaining false paths, which previously had no test at all, so fail-open weakenings of it survived the whole suite: an object carrying no token, no object present, and a failed HeadObject each still throw the original error. Every conditional injection now also asserts its request really carried If-None-Match. Verified against real MinIO as well as the mock: with the threshold at 0 the stored metadata object's ETag carries S3's -N multipart suffix and the write token, and on a tree without the stamp the same multipart object carries no metadata at all. Nine mutations of the guard each redden exactly their predicted arms.
…eign object DB::S3::Client::CompleteMultipartUpload rewrites a NO_SUCH_UPLOAD failure into a synthetic success whenever a bare HeadObject finds any object at bucket+key. The intent is to recover a completion whose response was lost after the upload had already finished, and for an unconditional write mere existence is adequate proof. For a conditional completion it is not. If-None-Match: * means "create only if absent", so the object found at that key may be exactly the object the condition was meant to reject. The write is then reported as successful while the caller's payload was never stored, which is worse than the spurious PreconditionFailed this branch already handles, because 412 at least surfaced an error. NO_SUCH_UPLOAD is not only the lost-response case either: per the AWS CompleteMultipartUpload documentation it also covers an upload id that is invalid, aborted or already consumed, and WriteBufferFromS3 aborts uploads on its cancel and destructor paths. Gate the existence-only recovery on the request being unconditional, and recover the conditional case in WriteBufferFromS3 against the write token instead, next to the PreconditionFailed guard. An If-Match write mints no token, so it can never recover; that is deliberate and now has its own test. NO_SUCH_UPLOAD is recognised but never made retryable: a consumed upload id cannot succeed on a retry. Unconditional completions keep the previous behaviour byte for byte, which is what copyS3File and the disk write paths rely on; a test asserts it, and a mutation that removes the new recovery reddens only the conditional arm.
Internal second-model review (3 rounds, click to expand)Each round is an independent cold review of the resulting code plus a second-model gate, adjudicated
Two findings of my own were disagreed on measurement rather than acted on: the round-1 mutation Worth recording from round 2: a "NOT in scope" line written in one round becomes a fail-open licence |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260808-225000 |
|
cc @scanhex12 @kssenii, could you review this? A conditional Iceberg metadata write is not idempotent, so a retry after a lost response hits its own object and fails with 412; the fix stamps a per-buffer token in S3 custom metadata and accepts a replay only against that token, and it also stops the NoSuchUpload recovery in S3::Client from reporting success over a foreign object on a conditional completion. |
|
Workflow [PR], commit [6e97697] Summary: ✅
AI ReviewSummaryThis PR makes conditional Iceberg metadata writes on S3 fail closed but recover correctly when a successful write is replayed after a lost response, by tagging Final VerdictNo findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 397/398 (99.75%) · Uncovered code |
CI finish ledger - 6e97697Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
No test-level failure on this head: all 32 executed checks concluded Session id: cron:our-pr-ci-monitor:20260809-230000 |
… went Three pairs of multipart NO_SUCH_UPLOAD arms - If-None-Match, If-Match and unconditional - existed to prove the is_conditional guard behaved for each. The guard is gone: Client::CompleteMultipartUpload no longer reads IfMatchHasBeenSet or IfNoneMatchHasBeenSet, so all three drove identical code and differed only in a setting nothing looks at. Dropped the pair this branch added rather than the one #113994 did. The remaining If-None-Match pair still fails if the guard is ever reintroduced, which is what caught it during the restructure.
…stence Client::CompleteMultipartUpload rewrote NoSuchUpload to success whenever a HeadObject found any object at the key. ClickHouse#56475 added that for a completion whose response was lost, but existence at a key is not evidence of a replay: an upload aborted between create and complete, against a key that already held an object, was acknowledged with none of its rows stored. This layer cannot tell the two apart. It sees only the part list, while everything that identifies the write - metadata, storage class, encryption, conditional headers - is set on CreateMultipartUpload one request earlier. So the inference is removed, and NoSuchUpload is reported as it is. Authorship is proven where it is known instead. Every multipart create now carries the write token introduced by ClickHouse#113994, so the recovery that already existed for conditional writes covers all of them. copyS3File gains the same token and check. isObjectWrittenWithToken is shared by both. An unconditional single-part PUT still carries no token: it cannot receive the 412 that would read one back. The GCS compose request moves out of the completion outcome into Client::composeObjectAfterMultipartUpload, so a recovery that reports success without a successful outcome still composes the object. Closes: ClickHouse#114348
Related: #96121
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a spurious
PreconditionFailed(Code: 499) error when creating or committing to an Iceberg table. If the object store drops the response to the metadata write after the object was written, the retry of that conditional write hits its own object and the query fails even though the write succeeded. Also stops a conditional multipart completion from reporting success over an object it did not write, silently losing the write it was meant to reject.Description
CREATE TABLE ... ENGINE = IcebergS3(...)and Iceberg commits intermittently fail withCode: 499 ... PreconditionFailed ... (S3_ERROR)for a write that landed: 6 occurrences over 6unrelated PRs and 4 tests in 120 days of public CI, 0 on master
(latest report).
Root cause: Iceberg writes its metadata with
If-None-Match: *, so the write is not idempotent.When the response to a successful attempt is lost, or MinIO returns the bogus
NO_SUCH_KEYthatWriteBufferFromS3already retries, the request is re-sent, finds its own object, and gets 412.IcebergMetadata::createInitialswallows that only underIF NOT EXISTS.Fix: a conditional create-if-absent write now carries a random per-buffer token in S3 custom
metadata, and reports success on a replay only when one
HeadObjectfinds that exact token.Token absent, token different, object absent, or a failed
HeadObjectall throw the originalerror, so a genuinely pre-existing table still fails. Three sites are covered: the single-part
PUT; the conditionalCompleteMultipartUpload, reachable from an ordinarySET s3_max_single_part_upload_size = 0; and theNoSuchUploadrecovery inS3::Client, whichrewrote a failure into a success whenever a bare
HeadObjectfound any object at the key, so aconditional create could report success over a foreign object. It is now gated on the request
being unconditional.
Comparing content instead would not be sound:
version-hint.textis the literal"1", so twowriters produce identical bytes. Custom metadata is not object content, so the JSON other engines
read is unchanged; token and recovery are guarded on
object_storage_write_if_none_match, whoseonly non-test producer is Iceberg.
Validated by thirteen deterministic unit tests on the existing mock S3 client (no S3, no network)
and thirteen mutations that each redden exactly the expected tests.
Version info
26.9.1.303(included in26.9and later)