2018-05-21 13:49:54 +00:00
|
|
|
#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;
|
2023-08-03 10:34:43 +00:00
|
|
|
extern const StorageActionBlockType PullReplicationLog = 8;
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
ActionLocksManager::ActionLocksManager(ContextPtr context_) : WithContext(context_->getGlobalContext())
|
2020-05-28 23:01:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:29:52 +00:00
|
|
|
void ActionLocksManager::add(const StorageID & table_id, StorageActionBlockType action_type)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
if (auto table = DatabaseCatalog::instance().tryGetTable(table_id, getContext()))
|
2020-01-24 16:20:36 +00:00
|
|
|
add(table, action_type);
|
|
|
|
}
|
2018-05-21 13:49:54 +00:00
|
|
|
|
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);
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 20:29:52 +00:00
|
|
|
void ActionLocksManager::remove(const StorageID & table_id, StorageActionBlockType action_type)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
if (auto table = DatabaseCatalog::instance().tryGetTable(table_id, getContext()))
|
2020-01-24 16:20:36 +00:00
|
|
|
remove(table, action_type);
|
|
|
|
}
|
2018-05-21 13:49:54 +00:00
|
|
|
|
2020-01-24 16:20:36 +00:00
|
|
|
void ActionLocksManager::remove(const StoragePtr & table, StorageActionBlockType action_type)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(mutex);
|
|
|
|
|
2022-04-18 10:18:43 +00:00
|
|
|
if (storage_locks.contains(table.get()))
|
2020-01-24 16:20:36 +00:00
|
|
|
storage_locks[table.get()].erase(action_type);
|
2018-05-21 13:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ActionLocksManager::cleanExpired()
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock(mutex);
|
2018-05-21 13:49:54 +00:00
|
|
|
|
2018-11-24 01:48:06 +00:00
|
|
|
for (auto it_storage = storage_locks.begin(); it_storage != storage_locks.end();)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
auto & locks = it_storage->second;
|
2018-11-24 01:48:06 +00:00
|
|
|
for (auto it_lock = locks.begin(); it_lock != locks.end();)
|
2018-05-21 13:49:54 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|