Repository navigation
Restore a dictionary left unusable by a rejected DETACH ... PERMANENTLY under concurrent DDL - #111742
tiandiwonder wants to merge 3 commits into
Conversation
DETACH DICTIONARY ... PERMANENTLY runs flushAndShutdown() before removeDependencies(). For a dictionary, shutdown deregisters it from ExternalDictionariesLoader, and removeDependencies then throws HAVE_DEPENDENT_OBJECTS when another object still depends on it -- after the dictionary has already been deregistered. The rejected detach left the dictionary attached but unloadable (Dictionary ... not found / BAD_ARGUMENTS) until re-attach or server restart. Make the permanent detach atomic: if removeDependencies throws, re-register the dictionary's loader config repository (new StorageDictionary::restoreDictionaryConfigurationInRepository, mirroring the constructor -- startup() alone does not re-add it and is a no-op under dictionaries_lazy_load) before rethrowing. Co-Authored-By: Claude Opus 4.8 <[email protected]>
PR ClickHouse#105259 (pre-shutdown dependency precheck) merged to master and touched the same DETACH PERMANENTLY branch. Resolve InterpreterDropQuery.cpp keeping both: ClickHouse#105259's precheck (rejects pre-existing dependents before shutdown) and this PR's rollback (restores a dictionary when removeDependencies throws for a dependent registered concurrently, after shutdown). The two are complementary. Add failpoint detach_permanently_pause_before_remove_dependencies and concurrent regression test 04628 that exercises the rollback via the post-precheck race; 04627 now covers the pre-existing-dependent path handled by the precheck. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [02a3809] Summary: ❌
AI ReviewSummaryThis PR makes PR Metadata
Findings❌ Blockers
Final VerdictRequest changes. The rollback needs to distinguish real DDL dictionaries from |
04627 duplicated the pre-existing-dependent case already covered by 04256_105207_detach_dictionary_permanently_dep_check (added by the merged precheck PR); it also passes on master HEAD, so it is not a regression test. Remove it and keep only the concurrent-race test 04628, which exercises the rollback via a failpoint. Tag 04628 no-fasttest because the failpoint (libfiu) is not built in the fast-test image. Co-Authored-By: Claude Opus 4.8 <[email protected]>
| } | ||
| catch (...) | ||
| { | ||
| if (auto * dictionary = dynamic_cast<StorageDictionary *>(table.get())) |
There was a problem hiding this comment.
StorageDictionary also backs CREATE TABLE ... ENGINE = Dictionary(...) (Location::Custom), and that variant never registers an ExternalDictionariesLoader repository or stores a non-null configuration. On that path removeDependencies can still throw for a concurrently added dependent, but this rollback calls restoreDictionaryConfigurationInRepository() anyway. The helper then creates an ExternalLoaderDictionaryStorageConfigRepository whose load() returns nullptr, and ExternalLoader::readFileInfo dereferences it unconditionally, so the rejected DETACH TABLE ... PERMANENTLY now goes through a null-config path during rollback instead of preserving the original HAVE_DEPENDENT_OBJECTS exception. Please gate the restore path to real DDL dictionaries only (for example Location::SameDatabaseAndNameAsDictionary / getConfiguration() != nullptr).
Manerone
left a comment
There was a problem hiding this comment.
Implementation is fine by me, it would be good to fix the comment from the bot and add a test related to that.
| if (query.permanently) | ||
| { | ||
| DatabaseCatalog::instance().removeDependencies(table_id, check_ref_deps, check_loading_deps, is_drop_or_detach_database); | ||
| /// The pre-shutdown check above rejects pre-existing dependents; a dependent registered |
There was a problem hiding this comment.
For my understanding .. what is a "dependent" in the case of dictionaries?
|
|
||
| /// Re-register the loader config repository that shutdown() removed, so an aborted | ||
| /// DETACH ... PERMANENTLY leaves the dictionary loadable again (startup() alone does not re-add it). | ||
| void restoreDictionaryConfigurationInRepository(); |
There was a problem hiding this comment.
Shouldn't that method be called undoShutdown?
| catch (...) | ||
| { | ||
| if (auto * dictionary = dynamic_cast<StorageDictionary *>(table.get())) | ||
| dictionary->restoreDictionaryConfigurationInRepository(); |
There was a problem hiding this comment.
Tbh, the fix looks rather fragile to me and it has a weird effect on the dictionary lifecycle (there is now a new state "undead" from which we can recover to "live" again).
We are now trying to restore the dictionary state if something goes wrong during removeDependencies.
I'm asking myself if it's not the better option to make removeDependencies fail-safe so that it always succeeds (no exceptions)? Then we wouldn't need any of the workaround.
|
Dear @Manerone, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Related: #105259
Problem
#105259 fixed the user-visible case where
DETACH DICTIONARY ... PERMANENTLYwas rejected because a dependent object already existed: it moved the dependency check beforeflushAndShutdown, so a rejected detach no longer shuts the dictionary down.A narrow residual remains under concurrent DDL: dependency registration is not serialized on the referenced object (
InterpreterCreateQuery::createTablecallsaddTableDependenciesafter releasing itsDDLGuard;ALTERlocks only the dependent). So a dependent registered after the pre-shutdown check passes still makes the laterDatabaseCatalog::removeDependenciesthrowHAVE_DEPENDENT_OBJECTS— by which pointflushAndShutdownhas already deregistered the dictionary fromExternalDictionariesLoader, leaving it attached but unloadable (dictGet*→Dictionary ... not found,BAD_ARGUMENTS) until re-attach or restart.This PR is defense-in-depth for that concurrent window; it does not change the already-correct behavior for the pre-existing-dependent case.
Root cause
In the
DETACH ... PERMANENTLYbranch ofInterpreterDropQuery::executeToTableImpl,removeDependenciesruns afterflushAndShutdownand re-checks dependents, throwing when any exist. The pre-shutdown check cannot close the window because a concurrentCREATE/ALTERcan register a dependent between the two checks, so the throw can still happen after the dictionary has been deregistered from the loader.Fix
Make the permanent detach atomic: if
removeDependenciesthrows afterflushAndShutdown, restore the dictionary before rethrowing.StorageDictionary::restoreDictionaryConfigurationInRepository()re-registers the loader config repository exactly as the constructor does (a barestartup()is insufficient — it onlyreloadConfigs, is a no-op underdictionaries_lazy_load, and never re-adds the repository).InterpreterDropQuery.cppwraps theremoveDependenciescall and restores the dictionary on throw; the rejection itself is unchanged (HAVE_DEPENDENT_OBJECTSis still raised). Scoped toDETACH PERMANENTLY(theDROPpath genuinely removes the table).Because the window is only reachable under concurrency, the regression test
04628_detach_permanently_dict_dependency_race.shuses a failpoint (detach_permanently_pause_before_remove_dependencies) to pause the detach between shutdown and dependency removal, registers a dependent in that window, then resumes and asserts the dictionary is still usable. The pre-existing-dependent case is already covered by04256_105207_detach_dictionary_permanently_dep_check(from #105259).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Made a rejected
DETACH DICTIONARY ... PERMANENTLYleave the dictionary usable even when the rejecting dependency is registered concurrently with the detach. Previously such a race could leave the dictionary attached but unloadable (Dictionary ... not found) until it was re-attached or the server restarted.