Repository navigation
Fix assertHasValidVersionMetadata exception when part directory is externally removed - #99399
Conversation
…externally removed When `system.columns` captures `shared_ptr<IStorage>` to a MergeTree table in an Ordinary database with parts having non-prehistoric `creation_tid`, and the table is DETACH'ed while its part directories are externally removed, the storage destructor can fire with parts referencing deleted `txn_version.txt` files, causing `chassert(assertHasValidVersionMetadata())` to fail. The fix: when the version metadata file is missing, check whether the part directory itself still exists. If the directory is gone, there is nothing to validate -- return true instead of false. Add a regression test that reproduces the race condition. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99272&sha=8a1f5e87eee68b39005270be969516abd1fcbb48&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%2C%202%2F2%29 Closes ClickHouse#99375 Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Workflow [PR], commit [427981d] Summary: ❌
AI ReviewSummaryThis PR hardens ClickHouse Rules
Final Verdict
|
|
|
||
| $CLICKHOUSE_CLIENT --allow_deprecated_database_ordinary=1 -q "DROP DATABASE IF EXISTS ${db_name}" | ||
|
|
||
| echo "OK" |
There was a problem hiding this comment.
This is printed if the client succeeded or not. Is it intended?
There was a problem hiding this comment.
It doesn't matter. It was initially part of the script to reproduce it, but I kept it when migrating to a stateless test. I'll remove it
Reduce concurrent `system.columns` queries from 20 to 5 to avoid OOM under ASan, and wait per-PID to suppress bash "Killed" messages that the test framework flags as stderr errors. Co-Authored-By: Claude Opus 4.6 <[email protected]>
When an Ordinary database reuses the same data directory path after DROP + CREATE, the `txn_version.txt` file may belong to a different incarnation of the part. In this case, the `creation_tid` on disk won't match the in-memory state. This is not corruption — skip validation instead of asserting. Co-Authored-By: Claude Opus 4.6 <[email protected]>
LLVM Coverage Report
PR changed lines: PR changed-lines coverage: 0.00% (0/21, 0 noise lines excluded) |
3915a2c
On object-storage disks (e.g. Azure), `readFileIfExists` can still throw because `IMetadataStorage::getStorageObjectsIfExist` has a TOCTOU race: `existsFile()` succeeds, then `getStorageObjects()` tries to read metadata that was already deleted by a concurrent `rm -rf` of the part directory. The existing fix from #99399 only handled the case where `readFileIfExists` returns nullptr (local disk). This adds the same directory-existence check in the catch block to handle the throwing case on object-storage disks. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99545&sha=a9ef26aeae01e50aab174ae4fb26d37b8e36cba6&name_0=PR&name_1=Stateless%20tests%20%28arm_asan%2C%20azure%2C%20parallel%29 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
| if [ -n "$data_dir" ]; then | ||
| for d in "${data_dir}"all_*; do | ||
| [ -d "$d" ] || continue | ||
| rm -rf "$d" |
There was a problem hiding this comment.
Unfortunately, the test breaks the invariant of ReplicatedMergeTree, leading to inconsistency, detected by Stress test: assertHasValidVersionMetadata.
Doing rm manually is an illegal operation in tests. I will remove. The fix is good on its own.
There was a problem hiding this comment.
Doing
rmmanually is an illegal operation in tests. I will remove. The fix is good on its own.
In this case then, is there some "legal" way to test changes like this? Should we consider to implement one in the future?
There was a problem hiding this comment.
| /// belongs to a different incarnation of the part (e.g., an Ordinary database | ||
| /// reusing the same data directory path after DROP + CREATE). There is nothing |
There was a problem hiding this comment.
I think this situation is not okay, and we should not handle it. Btw, how exactly does this happen? Looks like it should throw an exception if the data dir is not empty on table creation:
ClickHouse/src/Storages/StorageMergeTree.cpp
Lines 202 to 207 in 121eec5
| /// The part directory may have been removed externally (e.g., between | ||
| /// DETACH and ATTACH in an Ordinary database). If the directory is gone, |
There was a problem hiding this comment.
How did we get this IMergeTreeDataPart instance at all if the part dir was removed between DETACH and ATTACH? We would not load this data part on ATTACH if it was removed
There was a problem hiding this comment.
The part instance isn't from the ATTACH after rm -rf. It's from a previous ATTACH, held alive by system.columns's StoragePtrs. The sequence is:
- ATTACH (loads part)
- system.columns captures references in ColumnsSource.
- DETACH
- rm -rf
- system.columns releases the reference
- The destructor fires on the part whose directory is gone.
There was a problem hiding this comment.
Yes, I understand the problem, but I don't understand the comment:
The part directory may have been removed externally (e.g., between DETACH and ATTACH in an Ordinary database)
But in this case we would not load the removed data part. Also, it's not possible to detach and re-attach a table while some query is still using it
There was a problem hiding this comment.
Yes, the comment is wrong, because it was based in what the test was doing, but not what was triggering the assert (the destructor is delayed and being called at the end of the system.columns query).
I can adjust the comment, or since we've removed the tests deleting data (thinking it was ok doing so after detach returned) we can revert the change altogether (the test was already removed). WDYT?
There was a problem hiding this comment.
I think it's better to keep the check stricter given that the root cause is addressed by removing the inconsistent test. We also have some other asserts/logical errors that can be triggered by manual intervention into keeper or data on disk
There was a problem hiding this comment.
Perfect. I'll remove it then. Thanks for having a look!
Summary
In CI, test
03915_intersecting_parts_rollbackruns in parallel with tests that querysystem.columns(e.g.,00941_system_columns_race_condition). The race condition:03915creates a MergeTree table in an Ordinary database and doesBEGIN TRANSACTION; INSERT, creating a part with non-prehistoriccreation_tidsystem.columnsquery capturesshared_ptr<IStorage>to all tables, including this one03915doesDETACH TABLE, thenrm -rfon part directories as part of its normal intersecting-parts-rollback test proceduresystem.columnsquery finishes and releases itsshared_ptr— now the last reference to the storageremoveIfNeededcallsassertHasValidVersionMetadata, which tries to readtxn_version.txtfrom the already-deleted part directory, returnsfalse, andchassertfiresThe fix: in
assertHasValidVersionMetadata, when the version metadata file is missing, check whether the part directory itself still exists. If the directory is gone, there is nothing to validate — returntrueinstead offalse.Also adds a regression test that reproduces this race.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99272&sha=8a1f5e87eee68b39005270be969516abd1fcbb48&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%2C%202%2F2%29
#99272
Closes #99375
Closes #98949
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Note
Low Risk
Low risk: changes only a debug-only validation path to tolerate externally deleted part directories, plus adds a race-focused stateless regression test.
Overview
Prevents
chassert(assertHasValidVersionMetadata())from aborting whentxn_version.txtis missing because the entire part directory was removed externally;assertHasValidVersionMetadata()now treats a missing version file as valid if the part storage no longer exists.Adds a stateless regression test (
04039_assert_valid_version_metadata_race.sh) that reproduces thesystem.columns/DETACHrace by deleting a part directory during concurrent metadata queries.Written by Cursor Bugbot for commit e0140dd. This will update automatically on new commits. Configure here.
Version info
26.3.1.684