Repository navigation
Refuse RENAME/EXCHANGE of tables while DROP DATABASE is running - #133016
Marouane-elfadli wants to merge 18 commits into
Conversation
Add test 05829 for DROP DATABASE vs RENAME/EXCHANGE Add reference for test 05829
|
@PedroTadim Hi , I’ve opened this PR. I spent time since yesterday understanding the issue more deeply and reworking the solution with my own approach, using LLM assistance along the way. You were right about having too many failpoints, so I reduced them to one focused failpoint and addressed the previous feedback. Could you take another look and let me know what you think of the new approach? Thanks for your guidance! |
|
@groeneai take a look on this and give me your opinion about it ! |
|
Workflow [PR], commit [2504de8] Summary: ❌
|
Add a newline at the end of the file for consistency.
Added a colon for clarity in the failpoint types comment.
|
I checked the current head ( What holds:
What blocks:
Smaller: I'd rather not give a score. Once these are fixed I see nothing else blocking, and the review and merge decision belong to the maintainers. CI runs again on the next push. |
Refactor database locking mechanism during rename operations to use optional locks.
Added a comment regarding database creation warnings and clarified behavior of RENAME and DROP operations.
|
@groeneai I think its good right know ! ? |
| databases_locked_by_rename.insert(database_name); | ||
| locks.emplace_back(database_name, std::move(*lock)); |
There was a problem hiding this comment.
[Blocker] Keep thread-local lock ownership consistent when allocation fails
databases_locked_by_rename.insert succeeds before locks.emplace_back, which can throw while allocating the vector or copying the database name. The constructor's catch calls release, but that only erases names already present in locks. The current name therefore stays in the thread-local set after its actual shared lock has been released.
Every later rename on this server thread then takes the contains shortcut for that database without acquiring its mutex, even while DROP DATABASE holds the exclusive lock. This restores the original race: a prepared table can escape the drop, or an exchange can cause the other database's table to be deleted. An isolated allocation-fault reproduction using the helper verbatim confirmed that the stale entry survives and the next construction makes no catalog lock attempt.
Record the lock in locks before publishing its name in the thread-local set, so the existing catch can clean up either insertion failure. Add coverage that injects an allocation failure and verifies that a subsequent rename still acquires the real database lock.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes6 object files changed (+26.60 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units10 translation units recompiled, 26 s compile time in total, 10 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
I checked the new head ( Fixed:
Still open:
Minor: in Moving the lock into Once these two points are fixed, I see nothing else blocking. The review and the merge decision belong to the maintainers. |
| /// and the nested rename would fail halfway through the outer one. | ||
| thread_local std::set<String> databases_locked_by_rename; | ||
|
|
||
| class RenameDatabaseLocks |
There was a problem hiding this comment.
@Marouane-elfadli why did you need to create this class that you call only once?
Removed a fail point for time series renaming and added a new pauseable fail point for silk fiber socket send timeout.
Refactor database locking mechanism in InterpreterRenameQuery.
|
@PedroTadim i stop this PR give up i try my best i think GROENAI HE CAN fix this bug |
|
Thanks, I'll take #132379 from here. I'm investigating it and will post the PR link (or my findings) here. |
|
I opened #133361 for #132379. It builds on the approach of this PR: every |
…s a refresh Since `Refuse RENAME and EXCHANGE of the tables of a database being dropped` (ClickHouse#133016) `DROP DATABASE` holds the exclusive side of `restart_replica_mutex` from listing its tables until it has dropped them, and every `RENAME` and `EXCHANGE` try-locks the shared side, failing with `UNFINISHED`. The window in which `test_drop_database_concurrent_exchange` exchanged a table (stopping a refreshable view that waits for its refresh) is inside that lock, so the exchange is now refused and the other database's table keeps its own row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`DROP DATABASE` now refuses a concurrent `RENAME` or `EXCHANGE` of its tables (ClickHouse#133016), so the window this case exchanged a table in is closed, and its remaining assertion (the exchange fails with `UNFINISHED`) holds with either lookup in the pre-flight. `05839_drop_database_refuses_concurrent_rename` covers that refusal. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #132379
Related: #132599
Related: #123694
Related: #51851
Related: #71333
Supersedes #132599, which I am closing: its branch was based on an old master, so its diff contained unrelated changes. This PR has the same fix on a clean branch, with the review comments of #132599 addressed.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
RENAME TABLEorEXCHANGE TABLESof the tables of a database no longer runs concurrently withDROP DATABASEof that database. Before, a table moved to another database during the drop was still shut down by it and stayed read-only (TABLE_IS_READ_ONLYforReplicatedMergeTree) and unable to merge (MergeTree) until a restart, and anEXCHANGEcould make the drop remove the wrong table. Such a rename is now refused withUNFINISHEDwhile the database is being dropped. Closes #132379.Description
DROP DATABASEcollects the storages of the database, callsflushAndPrepareForShutdownon them, then drops them by name. A concurrentRENAME/EXCHANGEcould change the tables of the database in between:RENAME TABLE a.t TO b.tmoved a prepared table to another database, where it stayed read-only / without merges after the drop succeeded.EXCHANGE TABLES a.t AND b.umade the drop remove the originalb.u, while the prepareda.tsurvived asb.u.RENAME TABLE a.t TO a.t2made the drop fail withDATABASE_NOT_EMPTYand left the prepared table unusable.DROP DATABASEalready holdsgetLockForDropDatabaseexclusively for the whole collect/prepare/drop phase. The fix makes everyRENAME/EXCHANGEtake a shared lock on all databases it touches (tryGetLockForRestartReplica) before taking its table guards:UNFINISHED;Changes:
InterpreterRenameQuery.cpp: the fix.InterpreterDropQuery.cpp,FailPoint.cpp: one new failpoint,drop_database_holding_drop_lock, a pause point right afterprepare_tables, under the lock, used by the test. No behaviour change otherwise.05829_drop_database_blocks_rename: pauses the drop after its tables are prepared, checks that cross-databaseRENAME/EXCHANGEand an in-databaseRENAMEof its tables are refused withUNFINISHEDwhile a rename inside the other database still works, then checks that the surviving table is writable and mergeable.01150_ddl_guard_rwr: tolerates the new error code 341 (UNFINISHED), because the test races renames withDETACH DATABASE.Workflow [PR]
Sync PR [sync-upstream/pr/133016]