Repository navigation
Reject rename of TimeSeries tables up front (fixes CREATE OR REPLACE orphan + duplicate-UUID abort) - #107110
Reject rename of TimeSeries tables up front (fixes CREATE OR REPLACE orphan + duplicate-UUID abort)#107110groeneai wants to merge 4 commits into
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-5:20260610-225430 |
|
cc @tavplubix @alesapin — could you review this? It stops the server from aborting with |
|
Workflow [PR], commit [8ec1f50] AI ReviewSummaryThis PR changes Missing context / blind spots
Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block Minimum required action: fix table-level cross-database move/exchange classification and add focused regression coverage. |
|
Pushed b322aa8: fix the |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 3/3 (100.00%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 8 line(s) · Uncovered code |
CI summary for
|
|
@groeneai, Please explain how exactly BuzzHouse found this problem - which scenario gives it? |
|
The crash needs two table metadata files in one Atomic database that declare the same explicit UUID. Here is exactly how that state arises, and why it is not reachable by any completed SQL sequence. Why no finished statement can produce it. The engine actively prevents two live tables from sharing an explicit UUID, via two independent guards: the per-UUID data-directory guard in
So the duplicate-UUID-on-disk state is necessarily a leftover artifact, not the result of a completed query. The BuzzHouse path. BuzzHouse emits explicit
Test. A pure-SQL deterministic repro is not possible, for the reason above: the engine forbids the colliding on-disk state from arising except via interruption. So the test either fabricates the leftover artifact or literally interrupts the server. My plan is to rework 04340 to build the second file as a real |
@groeneai, if the table with UUID X already exists, then why |
|
@tavplubix You're right that the live case is already guarded; the duplicate is purely an on-disk crash artifact. Walking through it: Why
In every completed flow this is safe, and I verified that empirically against a master debug build: any serial sequence that would leave two live tables on UUID X is rejected before the file is written, by the shared The two-files-with-UUID-X state appears only when the So the real source defect is in the loader, which matches your instinct
Design fork, I'd like your call before I rewrite
They are complementary. I would suggest (B) as the primary fix with (A) kept only as a non-aborting safety net, but I won't commit before you and @ alexey-milovidov confirm the direction. The non-superficial test alexey asked for then becomes "leave an orphaned Which would you prefer? |
|
@groeneai something is wrong in your analysis. Why don't we check UUID before renaming |
|
@tavplubix you're right, my earlier framing was wrong. I pulled the actual BuzzHouse We already check the UUID before that rename. In Where the on-disk duplicate actually comes from. In the crash log it is an orphaned
So the duplicate-UUID-on-disk state is a cleanup/crash-recovery artifact, and the real defects are upstream of the load: (1) the OR REPLACE leaving an orphaned Which direction do you and @alexey-milovidov prefer:
I lean B as the real fix with A as the safety net, and would rework the regression test to leave a genuine orphaned |
|
@groeneai how is it possible that the file was left on disk after removing the UUID from the map in memory? |
|
@tavplubix I pulled the exact sequence from the crash The query is
The comment at
So the source defect is a partial Candidate source fix: override Does that direction look right to you and @ alexey-milovidov? If so I will move the fix to |
|
@groeneai so looks like the root cause is that StorageTimeSeries breaks the contract, fix checkTableCanBeRenamed |
DatabaseCatalog::addUUIDMapping(uuid, db, table) threw a LOGICAL_ERROR when
the UUID was already mapped to a live table. During ATTACH DATABASE (and
server startup) tables are loaded via attachTable, which calls addUUIDMapping
directly with a real (database, table) pair and bypasses the empty "lock"
reservation. If two table metadata files in the loaded database declare the
same explicit UUID, the second table reaches that throw and the LOGICAL_ERROR
aborts the whole server in debug/sanitizer builds.
Two table .sql files sharing one UUID is user-controlled, external on-disk
state: it can be produced by manual edits, or by a leftover _tmp_replace_*
table from CREATE OR REPLACE TABLE ... UUID '...' that carries the same
explicit UUID as the live target table. That is a user error, not an internal
invariant violation, and the sibling branch already reports the same condition
as a non-fatal TABLE_ALREADY_EXISTS ("UUID collision, most likely because some
not random UUIDs were manually specified"). The runtime ATTACH TABLE path even
hits that friendly error today; only the load path aborts.
Report the collision as TABLE_ALREADY_EXISTS instead of LOGICAL_ERROR. The
conflicting table fails to load with a clear message, the rest of the database
attaches, and the server stays up. This covers both the ATTACH DATABASE async
load path and the server startup path.
Found by BuzzHouse (amd_tsan) on PR ClickHouse#105020 (STID 3178-4166); the family also
hit PRs ClickHouse#100047, ClickHouse#106268, ClickHouse#100703 (STID 3178-4504). New test
04340_attach_database_duplicate_uuid_no_crash reproduces the crash
deterministically and verifies the server survives.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
StorageTimeSeries cannot be renamed: renameInMemory() throws
NOT_IMPLEMENTED unconditionally. But it did not override
checkTableCanBeRenamed(), so DatabaseAtomic::renameTable() passed its
pre-swap guard, swapped the metadata files on disk via renameExchange()
and detached the table in memory, and only then hit the throw in
renameInMemory(). The comment in renameTable() ("after metadata was
successfully moved, the following methods should not throw") documents
exactly the invariant that is violated.
This surfaced through CREATE OR REPLACE TABLE t ENGINE = TimeSeries on
an existing table: the implicit EXCHANGE of the intermediate
_tmp_replace_<hash> table fails halfway, the cleanup DROP then fails
with UNKNOWN_TABLE, and an orphaned _tmp_replace_*.sql carrying the old
explicit UUID is stranded on disk. A later ATTACH DATABASE / restart
loads both that orphan and the live table, hits a duplicate UUID in
DatabaseCatalog::addUUIDMapping and aborts the server with a
LOGICAL_ERROR. This is the BuzzHouse finding STID 3178-4166.
Override checkTableCanBeRenamed() to throw the same NOT_IMPLEMENTED, so
the rename is rejected before any on-disk or in-memory side effect and
no orphan can form. StorageTimeSeries is the only engine that throws in
renameInMemory() while leaving checkTableCanBeRenamed() a no-op.
Replace the previous metadata-editing regression test with
04365_timeseries_create_or_replace_no_orphan, which reproduces the real
path: CREATE OR REPLACE into a TimeSeries table, then DETACH/ATTACH
DATABASE, asserting the rename is rejected, no _tmp_replace_* orphan is
left, and the server stays alive.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
b322aa8 to
149610d
Compare
|
@tavplubix done, implemented the
Reworked the test to the real path: One thing to decide: I left the |
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-1:20260619-135900 |
… test
The Style check requires every query against system.tables to carry a
database = currentDatabase()/$CLICKHOUSE_DATABASE/'literal' predicate
(ci/jobs/scripts/check_style/various_checks.sh). The test operates on a
secondary database ({CLICKHOUSE_DATABASE_1}) it can DETACH/ATTACH, so
currentDatabase() does not apply and the database name is randomized, so
no literal fits. Replace the two system.tables introspection queries with
SHOW TABLES FROM <db> LIKE/NOT LIKE, which is deterministic, parallel-safe,
and asserts the same thing (no orphaned _tmp_replace_* table, the original
table survives) without tripping the style heuristic.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed The open question stands: keep the load-path cron:clickhouse-worker-slot-0:20260619-151500 |
| /// The UUID is already mapped to a live table. This is reachable from user-controlled on-disk state | ||
| /// (two table metadata files sharing an explicit UUID), so it is a user error, not a LOGICAL_ERROR. |
There was a problem hiding this comment.
Done, the check is kept. 8ec1f50 reverts the load-path softening, so addUUIDMapping keeps throwing LOGICAL_ERROR on a duplicate UUID exactly as before and DatabaseCatalog.cpp is now unchanged versus master. With the source fix preventing the orphaned _tmp_replace_*.sql from ever forming, a duplicate explicit UUID during load is a genuine internal-invariant violation again, so LOGICAL_ERROR is the right signal.
Two follow-ups to review on this PR: 1. Close the RENAME DATABASE gap. checkTableCanBeRenamed() rejects a TimeSeries rename before DatabaseAtomic::renameTable() touches the disk, but DatabaseAtomic::renameDatabase() does not call checkTableCanBeRenamed() -- it moves metadata/<db>.sql and updates the catalog name, then calls renameInMemory() directly. A throw from renameInMemory() there would leave a half-applied database rename. Override renameInMemory() with the same guard so the database-only rename (which keeps the table name and UUID, and whose inner tables are resolved by UUID) is allowed, while a genuine table rename is rejected. The shared predicate isDatabaseOnlyRename() distinguishes the two. 2. Restore the original addUUIDMapping() LOGICAL_ERROR check. The source fix (reject the rename up front) prevents the orphaned _tmp_replace_*.sql from ever forming, so a duplicate explicit UUID during load is again a genuine internal-invariant violation rather than reachable user-controlled state. This reverts the load-path softening; DatabaseCatalog.cpp is unchanged versus master. The regression test now also asserts RENAME TABLE is rejected and RENAME DATABASE over a TimeSeries-containing database succeeds. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Heads-up on a conflict between two open PRs that fix the same root cause opposite ways, so only one should land:
Both edit Could you decide which direction is canonical? If renaming should be supported (#107583), this PR's reject is redundant and should be dropped; if renaming should stay unsupported (this PR), #107583's rename implementation should be dropped and its Replicated digest-mismatch fix folded in here. I'm happy to do whichever consolidation you prefer once the direction is settled. |
Pre-PR validation gate(for
Note: there is an unresolved direction question between this PR and #107583 (reject vs. support renaming a Session id: cron:clickhouse-worker-slot-1:20260619-165400 |
| /// Reject genuine renames before DatabaseAtomic touches the disk: renameInMemory() runs after | ||
| /// the metadata files are already swapped/detached, which strands an orphaned _tmp_replace_*.sql | ||
| /// during CREATE OR REPLACE and later aborts the load. RENAME DATABASE is allowed. | ||
| if (!isDatabaseOnlyRename(new_name)) |
There was a problem hiding this comment.
checkTableCanBeRenamed is only called from table-level RENAME / EXCHANGE, not from RENAME DATABASE. Because this reuses isDatabaseOnlyRename, RENAME TABLE db1.t TO db2.t (and EXCHANGE TABLES db1.t AND db2.t when both table names are t) is now allowed: the new StorageID changes only the database name while the table name and UUID stay the same. That moves only the parent TimeSeries metadata; the inner target tables remain owned by the old database (or get cross-wired on EXCHANGE), so dropping that database can delete the targets for the moved table.
Please make checkTableCanBeRenamed reject table-level renames/moves unconditionally and keep the database-only allowance only in renameInMemory, which is the path used by DatabaseAtomic::renameDatabase.
|
I think it's better to support renaming and not just reject it: |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server abort that could happen after
CREATE OR REPLACE TABLE ... ENGINE = TimeSerieson an existing table. The replace failed halfway and left an orphaned_tmp_replace_*metadata file with the old explicit UUID on disk; a laterATTACH DATABASEor restart then loaded two tables with the same UUID and aborted the server withLogical error: Mapping for table with UUID=... already exists.CREATE OR REPLACE/EXCHANGE/RENAMEof aTimeSeriestable now fails up front with a clearNOT_IMPLEMENTEDerror and leaves nothing behind.Description
Found by the BuzzHouse fuzzer (STID 3178-4166).
StorageTimeSeriescannot be renamed:renameInMemory()throwsNOT_IMPLEMENTEDunconditionally. It did not overridecheckTableCanBeRenamed(), soDatabaseAtomic::renameTable()passed its pre-swap guard, swapped the metadata files on disk withrenameExchange()and detached the table in memory, and only then hit the throw inrenameInMemory(). The comment inrenameTable()("After metadata was successfully moved, the following methods should not throw (if they do, it's a logical error)") documents exactly the invariant that was violated.How the crash forms, from the actual fuzzer server log:
CREATE OR REPLACE TABLE t ENGINE = TimeSerieson an existing table goes through an intermediate_tmp_replace_<hash>table and anEXCHANGE.DatabaseAtomic::renameTable()swaps the.sqlfiles and detaches in memory, thenStorageTimeSeries::renameInMemory()throwsNOT_IMPLEMENTED.DROPlooks the temp table up by a name that is already detached and fails withUNKNOWN_TABLE(Cannot DROP temporary table: ... does not exist). The_tmp_replace_*.sqlcarrying the old explicit UUID is stranded on disk.ATTACH DATABASE/ restart loads both that orphan and the live table; both reachDatabaseCatalog::addUUIDMappingwith the same UUID and the server aborts with aLOGICAL_ERROR.Fix (root cause): override
StorageTimeSeries::checkTableCanBeRenamed()to throw the sameNOT_IMPLEMENTED, so the rename is rejected before any on-disk or in-memory side effect and no orphan can form.StorageTimeSeriesis the only engine that throws inrenameInMemory()while leavingcheckTableCanBeRenamed()a no-op, so no sibling engine needs the same change.This PR also keeps a defense-in-depth change in
DatabaseCatalog::addUUIDMapping: a duplicate explicit UUID found during load is reported asTABLE_ALREADY_EXISTSinstead ofLOGICAL_ERROR, so any duplicate-UUID-on-disk state (e.g. a hand-edited metadata file) fails one table's load with a clear message instead of aborting the whole server. Open question for the reviewers below on whether to keep it.Provenance:
BuzzHouse (amd_tsan)on PR Add filesystem cache eviction metrics #105020, STID 3178-4166, commit 5567566Mapping for table with UUID=... already existsfamily also surfaced on PRs Squash source blocks before projection calculate to reduce temp parts #100047, Add du and wc commands to clickhouse-disks #106268, FixGRANT CURRENT GRANTSto account for implicit privileges #100703 (STID 3178-4504), all unrelated to DatabaseCatalog code.Test:
04365_timeseries_create_or_replace_no_orphanreproduces the real path (CREATE OR REPLACEinto aTimeSeriestable, thenDETACH/ATTACH DATABASE) and asserts the rename is rejected, no_tmp_replace_*orphan is left, and the server stays alive. Without the source fix theATTACH DATABASEaborts the server (SIGABRT); with it the test passes.