ClickHouse/src/Interpreters/ActionLocksManager.cpp

102 lines
2.9 KiB
C++
Raw Normal View History

#include "ActionLocksManager.h"
#include <Interpreters/Context.h>
#include <Databases/IDatabase.h>
#include <Storages/IStorage.h>
namespace DB
{
namespace ActionLocks
{
extern const StorageActionBlockType PartsMerge = 1;
extern const StorageActionBlockType PartsFetch = 2;
extern const StorageActionBlockType PartsSend = 3;
extern const StorageActionBlockType ReplicationQueue = 4;
2019-04-22 15:11:16 +00:00
extern const StorageActionBlockType DistributedSend = 5;
2019-08-01 15:36:12 +00:00
extern const StorageActionBlockType PartsTTLMerge = 6;
2019-09-03 14:50:49 +00:00
extern const StorageActionBlockType PartsMove = 7;
}
ActionLocksManager::ActionLocksManager(ContextPtr context_) : WithContext(context_->getGlobalContext())
2020-05-28 23:01:18 +00:00
{
}
template <typename F>
inline void forEachTable(F && f, ContextPtr context)
{
for (auto & elem : DatabaseCatalog::instance().getDatabases())
2020-05-28 20:10:45 +00:00
for (auto iterator = elem.second->getTablesIterator(context); iterator->isValid(); iterator->next())
2020-06-02 02:06:16 +00:00
if (auto table = iterator->table())
f(table);
}
void ActionLocksManager::add(StorageActionBlockType action_type, ContextPtr context_)
{
forEachTable([&](const StoragePtr & table) { add(table, action_type); }, context_);
}
2020-03-04 20:29:52 +00:00
void ActionLocksManager::add(const StorageID & table_id, StorageActionBlockType action_type)
{
if (auto table = DatabaseCatalog::instance().tryGetTable(table_id, getContext()))
2020-01-24 16:20:36 +00:00
add(table, action_type);
}
2020-01-24 16:20:36 +00:00
void ActionLocksManager::add(const StoragePtr & table, StorageActionBlockType action_type)
{
ActionLock action_lock = table->getActionLock(action_type);
if (!action_lock.expired())
{
std::lock_guard lock(mutex);
storage_locks[table.get()][action_type] = std::move(action_lock);
}
}
void ActionLocksManager::remove(StorageActionBlockType action_type)
{
2019-01-02 06:44:36 +00:00
std::lock_guard lock(mutex);
for (auto & storage_elem : storage_locks)
storage_elem.second.erase(action_type);
}
2020-03-04 20:29:52 +00:00
void ActionLocksManager::remove(const StorageID & table_id, StorageActionBlockType action_type)
{
if (auto table = DatabaseCatalog::instance().tryGetTable(table_id, getContext()))
2020-01-24 16:20:36 +00:00
remove(table, action_type);
}
2020-01-24 16:20:36 +00:00
void ActionLocksManager::remove(const StoragePtr & table, StorageActionBlockType action_type)
{
std::lock_guard lock(mutex);
if (storage_locks.count(table.get()))
storage_locks[table.get()].erase(action_type);
}
void ActionLocksManager::cleanExpired()
{
2019-01-02 06:44:36 +00:00
std::lock_guard lock(mutex);
2018-11-24 01:48:06 +00:00
for (auto it_storage = storage_locks.begin(); it_storage != storage_locks.end();)
{
auto & locks = it_storage->second;
2018-11-24 01:48:06 +00:00
for (auto it_lock = locks.begin(); it_lock != locks.end();)
{
if (it_lock->second.expired())
it_lock = locks.erase(it_lock);
else
++it_lock;
}
if (locks.empty())
it_storage = storage_locks.erase(it_storage);
else
++it_storage;
}
}
}