Skip to content

Fix WriteFileOperation::undo() incorrectly deleting pre-existing metadata files - #99129

Merged
tiandiwonder merged 1 commit into
ClickHouse:masterfrom
tiandiwonder:invalid_version_metadata
Mar 11, 2026
Merged

tiandiwonder merged 1 commit into
ClickHouse:masterfrom
tiandiwonder:invalid_version_metadata

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor
`WriteFileOperation::undo()` used `!prev_data.has_value()` as the sole
condition for deleting the file, but `prev_data` is also unset when
`readStringUntilEOF` throws after `readFileIfExists` had already opened
an existing file (e.g. via ThreadFuzzer / fault injection on an async
`ThreadPoolReader::submit()`). In that case the file existed and its
content was unchanged, but `undo()` would delete it.

Fix: add a `file_existed` flag that is set to `true` immediately after
`readFileIfExists` confirms the file is present, before any read that
can throw. `undo()` now only removes the file when both `!prev_data`
and `!file_existed` — meaning `execute()` never observed the file on
disk and therefore either created it (delete warranted) or never wrote
it at all (no-op).

The observable symptom was an `assertHasValidVersionMetadata()` exception
on `DROP TABLE` against a TTL table on Azure blob storage with
transactions enabled: fault injection caused the `txn_version.txt`
metadata file to be deleted during `appendRemovalTIDToVersionMetadata`,
then silently recreated without the required `version: 1` header.

Add a `write_file_operation_fail_on_read` failpoint that fires inside
`WriteFileOperation::execute()` after the file is opened but before
`prev_data` is set, and a regression test
`AppendFileTxReadFailDoesNotDeleteFile` that verifies the file survives
a failed append transaction.

Closes #94142

CI reports:

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Touches transactional metadata write/undo logic for object-storage disks; a bug here could cause metadata loss or incorrect rollback behavior, but the change is narrowly scoped and covered by a new regression test.

Overview
Fixes a rollback bug in object-storage metadata transactions where WriteFileOperation::undo() could delete a pre-existing file if execute() failed after opening it but before capturing prev_data.

The operation now tracks whether the file was observed to exist (file_existed) and only deletes on undo when the file was newly created, plus a new write_file_operation_fail_on_read failpoint and a regression test (AppendFileTxReadFailDoesNotDeleteFile) to ensure a failed append transaction does not remove the original file.

Written by Cursor Bugbot for commit 0be09f0. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.569

@clickhouse-gh

clickhouse-gh Bot commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0be09f0]

Summary: ✅

@clickhouse-gh

clickhouse-gh Bot commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.30% 76.30% +0.00%

PR changed lines: PR changed-lines coverage: 100.00% (59/59)
Diff coverage report
Uncovered code

@Michicosun Michicosun self-assigned this Mar 10, 2026
@tiandiwonder
tiandiwonder added this pull request to the merge queue Mar 11, 2026
Merged via the queue into ClickHouse:master with commit 332ce2b Mar 11, 2026
159 checks passed
@tiandiwonder
tiandiwonder deleted the invalid_version_metadata branch March 11, 2026 00:31
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

Logical error: 'assertHasValidVersionMetadata()' (STID: 2508-48ff)

4 participants