Merge pull request #51908 from ClickHouse/fix_deadlock_on_catalog_shutdown

Fix deadlock on DatabaseCatalog shutdown
This commit is contained in:
Alexander Tokmakov 2023-07-07 19:06:34 +03:00 committed by GitHub
commit 152a4b9d4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -56,6 +56,7 @@ namespace ErrorCodes
extern const int DATABASE_ACCESS_DENIED; extern const int DATABASE_ACCESS_DENIED;
extern const int LOGICAL_ERROR; extern const int LOGICAL_ERROR;
extern const int HAVE_DEPENDENT_OBJECTS; extern const int HAVE_DEPENDENT_OBJECTS;
extern const int UNFINISHED;
} }
TemporaryTableHolder::TemporaryTableHolder(ContextPtr context_, const TemporaryTableHolder::Creator & creator, const ASTPtr & query) TemporaryTableHolder::TemporaryTableHolder(ContextPtr context_, const TemporaryTableHolder::Creator & creator, const ASTPtr & query)
@ -196,6 +197,9 @@ void DatabaseCatalog::startupBackgroundCleanup()
void DatabaseCatalog::shutdownImpl() void DatabaseCatalog::shutdownImpl()
{ {
is_shutting_down = true;
wait_table_finally_dropped.notify_all();
if (cleanup_task) if (cleanup_task)
(*cleanup_task)->deactivate(); (*cleanup_task)->deactivate();
@ -1161,8 +1165,13 @@ void DatabaseCatalog::waitTableFinallyDropped(const UUID & uuid)
std::unique_lock lock{tables_marked_dropped_mutex}; std::unique_lock lock{tables_marked_dropped_mutex};
wait_table_finally_dropped.wait(lock, [&]() TSA_REQUIRES(tables_marked_dropped_mutex) -> bool wait_table_finally_dropped.wait(lock, [&]() TSA_REQUIRES(tables_marked_dropped_mutex) -> bool
{ {
return !tables_marked_dropped_ids.contains(uuid); return !tables_marked_dropped_ids.contains(uuid) || is_shutting_down;
}); });
/// TSA doesn't support unique_lock
if (TSA_SUPPRESS_WARNING_FOR_READ(tables_marked_dropped_ids).contains(uuid))
throw Exception(ErrorCodes::UNFINISHED, "Did not finish dropping the table with UUID {} because the server is shutting down, "
"will finish after restart", uuid);
} }
void DatabaseCatalog::addDependencies( void DatabaseCatalog::addDependencies(

View File

@ -308,6 +308,8 @@ private:
Poco::Logger * log; Poco::Logger * log;
std::atomic_bool is_shutting_down = false;
/// Do not allow simultaneous execution of DDL requests on the same table. /// Do not allow simultaneous execution of DDL requests on the same table.
/// database name -> database guard -> (table name mutex, counter), /// database name -> database guard -> (table name mutex, counter),
/// counter: how many threads are running a query on the table at the same time /// counter: how many threads are running a query on the table at the same time