more fair queue for drop table sync (#52276)

This commit is contained in:
Alexander Tokmakov 2023-07-20 14:51:01 +03:00 committed by GitHub
parent 227db5243f
commit f53ff5d4f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -697,6 +697,7 @@ DatabaseCatalog::DatabaseCatalog(ContextMutablePtr global_context_)
, loading_dependencies{"LoadingDeps"}
, view_dependencies{"ViewDeps"}
, log(&Poco::Logger::get("DatabaseCatalog"))
, first_async_drop_in_queue(tables_marked_dropped.end())
{
}
@ -959,9 +960,17 @@ void DatabaseCatalog::enqueueDroppedTableCleanup(StorageID table_id, StoragePtr
std::lock_guard lock(tables_marked_dropped_mutex);
if (ignore_delay)
tables_marked_dropped.push_front({table_id, table, dropped_metadata_path, drop_time});
{
/// Insert it before first_async_drop_in_queue, so sync drop queries will have priority over async ones,
/// but the queue will remain fair for multiple sync drop queries.
tables_marked_dropped.emplace(first_async_drop_in_queue, TableMarkedAsDropped{table_id, table, dropped_metadata_path, drop_time});
}
else
{
tables_marked_dropped.push_back({table_id, table, dropped_metadata_path, drop_time + drop_delay_sec});
if (first_async_drop_in_queue == tables_marked_dropped.end())
--first_async_drop_in_queue;
}
tables_marked_dropped_ids.insert(table_id.uuid);
CurrentMetrics::add(CurrentMetrics::TablesToDropQueueSize, 1);
@ -1012,6 +1021,8 @@ void DatabaseCatalog::dequeueDroppedTableCleanup(StorageID table_id)
/// This maybe throw exception.
renameNoReplace(latest_metadata_dropped_path, table_metadata_path);
if (first_async_drop_in_queue == it_dropped_table)
++first_async_drop_in_queue;
tables_marked_dropped.erase(it_dropped_table);
[[maybe_unused]] auto removed = tables_marked_dropped_ids.erase(dropped_table.table_id.uuid);
assert(removed);
@ -1074,6 +1085,8 @@ void DatabaseCatalog::dropTableDataTask()
table = std::move(*it);
LOG_INFO(log, "Have {} tables in drop queue ({} of them are in use), will try drop {}",
tables_marked_dropped.size(), tables_in_use_count, table.table_id.getNameForLogs());
if (first_async_drop_in_queue == it)
++first_async_drop_in_queue;
tables_marked_dropped.erase(it);
/// Schedule the task as soon as possible, while there are suitable tables to drop.
schedule_after_ms = 0;
@ -1110,6 +1123,8 @@ void DatabaseCatalog::dropTableDataTask()
table.drop_time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()) + drop_error_cooldown_sec;
std::lock_guard lock(tables_marked_dropped_mutex);
tables_marked_dropped.emplace_back(std::move(table));
if (first_async_drop_in_queue == tables_marked_dropped.end())
--first_async_drop_in_queue;
/// If list of dropped tables was empty, schedule a task to retry deletion.
if (tables_marked_dropped.size() == 1)
{

View File

@ -323,6 +323,7 @@ private:
mutable std::mutex ddl_guards_mutex;
TablesMarkedAsDropped tables_marked_dropped TSA_GUARDED_BY(tables_marked_dropped_mutex);
TablesMarkedAsDropped::iterator first_async_drop_in_queue TSA_GUARDED_BY(tables_marked_dropped_mutex);
std::unordered_set<UUID> tables_marked_dropped_ids TSA_GUARDED_BY(tables_marked_dropped_mutex);
mutable std::mutex tables_marked_dropped_mutex;