diff --git a/src/Databases/DatabaseAtomic.cpp b/src/Databases/DatabaseAtomic.cpp index ab22d78c576..6d564bc29a3 100644 --- a/src/Databases/DatabaseAtomic.cpp +++ b/src/Databases/DatabaseAtomic.cpp @@ -571,17 +571,8 @@ void DatabaseAtomic::renameDictionaryInMemoryUnlocked(const StorageID & old_name const auto & dict = dynamic_cast(*result.object); dict.updateDictionaryName(new_name); } -void DatabaseAtomic::waitDetachedTableNotInUseOrThrow(const UUID & uuid, bool throw_if_in_use) +void DatabaseAtomic::waitDetachedTableNotInUse(const UUID & uuid) { - if (throw_if_in_use) - { - DetachedTables not_in_use; - std::lock_guard lock{mutex}; - not_in_use = cleanupDetachedTables(); - assertDetachedTableNotInUse(uuid); - return; - } - /// Table is in use while its shared_ptr counter is greater than 1. /// We cannot trigger condvar on shared_ptr destruction, so it's busy wait. while (true) @@ -597,5 +588,13 @@ void DatabaseAtomic::waitDetachedTableNotInUseOrThrow(const UUID & uuid, bool th } } +void DatabaseAtomic::checkDetachedTableNotInUse(const UUID & uuid) +{ + DetachedTables not_in_use; + std::lock_guard lock{mutex}; + not_in_use = cleanupDetachedTables(); + assertDetachedTableNotInUse(uuid); +} + } diff --git a/src/Databases/DatabaseAtomic.h b/src/Databases/DatabaseAtomic.h index 4ff7600da11..695d22360ca 100644 --- a/src/Databases/DatabaseAtomic.h +++ b/src/Databases/DatabaseAtomic.h @@ -57,7 +57,8 @@ public: void tryCreateSymlink(const String & table_name, const String & actual_data_path, bool if_data_path_exist = false); void tryRemoveSymlink(const String & table_name); - void waitDetachedTableNotInUseOrThrow(const UUID & uuid, bool throw_if_in_use) override; + void waitDetachedTableNotInUse(const UUID & uuid) override; + void checkDetachedTableNotInUse(const UUID & uuid) override; void setDetachedTableNotInUseForce(const UUID & uuid); protected: diff --git a/src/Databases/IDatabase.h b/src/Databases/IDatabase.h index 3dc9c2b37c0..8c356b88460 100644 --- a/src/Databases/IDatabase.h +++ b/src/Databases/IDatabase.h @@ -345,7 +345,8 @@ public: virtual void assertCanBeDetached(bool /*cleanup*/) {} - virtual void waitDetachedTableNotInUseOrThrow(const UUID & /*uuid*/, bool /*throw_if_in_use*/) { } + virtual void waitDetachedTableNotInUse(const UUID & /*uuid*/) { } + virtual void checkDetachedTableNotInUse(const UUID & /*uuid*/) { } /// Ask all tables to complete the background threads they are using and delete all table objects. virtual void shutdown() = 0; diff --git a/src/Interpreters/InterpreterCreateQuery.cpp b/src/Interpreters/InterpreterCreateQuery.cpp index 2e9c92228ab..28a06c913c9 100644 --- a/src/Interpreters/InterpreterCreateQuery.cpp +++ b/src/Interpreters/InterpreterCreateQuery.cpp @@ -1003,7 +1003,10 @@ bool InterpreterCreateQuery::doCreateTable(ASTCreateQuery & create, /// so we allow waiting here. If database_atomic_wait_for_drop_and_detach_synchronously is disabled /// and old storage instance still exists it will throw exception. bool throw_if_table_in_use = getContext()->getSettingsRef().database_atomic_wait_for_drop_and_detach_synchronously; - database->waitDetachedTableNotInUseOrThrow(create.uuid, throw_if_table_in_use); + if (throw_if_table_in_use) + database->checkDetachedTableNotInUse(create.uuid); + else + database->waitDetachedTableNotInUse(create.uuid); } StoragePtr res; diff --git a/src/Interpreters/InterpreterDropQuery.cpp b/src/Interpreters/InterpreterDropQuery.cpp index 5c12383af4a..8ac6a0ae3a8 100644 --- a/src/Interpreters/InterpreterDropQuery.cpp +++ b/src/Interpreters/InterpreterDropQuery.cpp @@ -81,7 +81,7 @@ void InterpreterDropQuery::waitForTableToBeActuallyDroppedOrDetached(const ASTDr if (query.kind == ASTDropQuery::Kind::Drop) DatabaseCatalog::instance().waitTableFinallyDropped(uuid_to_wait); else if (query.kind == ASTDropQuery::Kind::Detach) - db->waitDetachedTableNotInUseOrThrow(uuid_to_wait, false); + db->waitDetachedTableNotInUse(uuid_to_wait); } BlockIO InterpreterDropQuery::executeToTable(ASTDropQuery & query)