2017-08-31 21:11:25 +00:00
|
|
|
#include "RWLockFIFO.h"
|
2017-09-26 17:19:16 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2017-09-01 15:05:23 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Poco/Ext/ThreadNumber.h>
|
2017-09-26 17:19:16 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event RWLockAcquiredReadLocks;
|
|
|
|
extern const Event RWLockAcquiredWriteLocks;
|
|
|
|
extern const Event RWLockReadersWaitMilliseconds;
|
|
|
|
extern const Event RWLockWritersWaitMilliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric RWLockWaitingReaders;
|
|
|
|
extern const Metric RWLockWaitingWriters;
|
|
|
|
extern const Metric RWLockActiveReaders;
|
|
|
|
extern const Metric RWLockActiveWriters;
|
|
|
|
}
|
2017-09-01 15:05:23 +00:00
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-26 17:19:16 +00:00
|
|
|
class RWLockFIFO::LockHandlerImpl
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
2017-09-26 17:19:16 +00:00
|
|
|
RWLockFIFOPtr parent;
|
2017-08-31 21:11:25 +00:00
|
|
|
GroupsContainer::iterator it_group;
|
|
|
|
ClientsContainer::iterator it_client;
|
2017-09-26 17:19:16 +00:00
|
|
|
ThreadToHandler::iterator it_handler;
|
|
|
|
CurrentMetrics::Increment active_client_increment;
|
|
|
|
|
|
|
|
LockHandlerImpl(RWLockFIFOPtr && parent, GroupsContainer::iterator it_group, ClientsContainer::iterator it_client);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
LockHandlerImpl(const LockHandlerImpl & other) = delete;
|
|
|
|
|
|
|
|
~LockHandlerImpl();
|
|
|
|
|
|
|
|
friend class RWLockFIFO;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
RWLockFIFO::LockHandler RWLockFIFO::getLock(RWLockFIFO::Type type, RWLockFIFO::Client client)
|
|
|
|
{
|
|
|
|
Stopwatch watch(CLOCK_MONOTONIC_COARSE);
|
|
|
|
CurrentMetrics::Increment waiting_client_increment((type == Read) ? CurrentMetrics::RWLockWaitingReaders
|
|
|
|
: CurrentMetrics::RWLockWaitingWriters);
|
|
|
|
auto finalize_metrics = [type, &watch] ()
|
|
|
|
{
|
|
|
|
ProfileEvents::increment((type == Read) ? ProfileEvents::RWLockAcquiredReadLocks
|
|
|
|
: ProfileEvents::RWLockAcquiredWriteLocks);
|
|
|
|
ProfileEvents::increment((type == Read) ? ProfileEvents::RWLockReadersWaitMilliseconds
|
|
|
|
: ProfileEvents::RWLockWritersWaitMilliseconds, watch.elapsedMilliseconds());
|
|
|
|
};
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
auto this_thread_id = std::this_thread::get_id();
|
2017-09-26 17:19:16 +00:00
|
|
|
GroupsContainer::iterator it_group;
|
|
|
|
ClientsContainer::iterator it_client;
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
/// Check if the same thread is acquiring previously acquired lock
|
|
|
|
auto it_handler = thread_to_handler.find(this_thread_id);
|
|
|
|
if (it_handler != thread_to_handler.end())
|
|
|
|
{
|
|
|
|
auto handler_ptr = it_handler->second.lock();
|
|
|
|
|
|
|
|
if (!handler_ptr)
|
|
|
|
throw Exception("Lock handler cannot be nullptr. This is a bug", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (type != Read || handler_ptr->it_group->type != Read)
|
|
|
|
throw Exception("Attempt to acquire exclusive lock recursively", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
handler_ptr->it_client->info += "; " + client.info;
|
|
|
|
|
2018-06-20 15:21:42 +00:00
|
|
|
return handler_ptr;
|
2017-09-04 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
if (type == Type::Write || queue.empty() || queue.back().type == Type::Write)
|
|
|
|
{
|
|
|
|
/// Create new group of clients
|
|
|
|
it_group = queue.emplace(queue.end(), type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Will append myself to last group
|
|
|
|
it_group = std::prev(queue.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Append myself to the end of chosen group
|
|
|
|
auto & clients = it_group->clients;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
it_client = clients.emplace(clients.end(), std::move(client));
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// Remove group if it was the first client in the group and an error occurred
|
|
|
|
if (clients.empty())
|
|
|
|
queue.erase(it_group);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:05:23 +00:00
|
|
|
it_client->thread_number = Poco::ThreadNumber::get();
|
|
|
|
it_client->enqueue_time = time(nullptr);
|
|
|
|
it_client->type = type;
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
LockHandler res(new LockHandlerImpl(shared_from_this(), it_group, it_client));
|
|
|
|
|
|
|
|
/// Insert myself (weak_ptr to the handler) to threads set to implement recursive lock
|
|
|
|
it_handler = thread_to_handler.emplace(this_thread_id, res).first;
|
|
|
|
res->it_handler = it_handler;
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
/// We are first, we should not wait anything
|
|
|
|
/// If we are not the first client in the group, a notification could be already sent
|
|
|
|
if (it_group == queue.begin())
|
2017-09-01 15:05:23 +00:00
|
|
|
{
|
|
|
|
it_client->start_time = it_client->enqueue_time;
|
2017-09-26 17:19:16 +00:00
|
|
|
finalize_metrics();
|
2017-08-31 21:11:25 +00:00
|
|
|
return res;
|
2017-09-01 15:05:23 +00:00
|
|
|
}
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
/// Wait a notification
|
2018-11-24 01:48:06 +00:00
|
|
|
it_group->cv.wait(lock, [&] () { return it_group == queue.begin(); });
|
2017-09-01 15:05:23 +00:00
|
|
|
|
|
|
|
it_client->start_time = time(nullptr);
|
2017-09-26 17:19:16 +00:00
|
|
|
finalize_metrics();
|
2017-09-01 15:05:23 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RWLockFIFO::Clients RWLockFIFO::getClientsInTheQueue() const
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
Clients res;
|
|
|
|
for (const auto & group : queue)
|
|
|
|
{
|
|
|
|
for (const auto & client : group.clients)
|
|
|
|
{
|
|
|
|
res.emplace_back(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
RWLockFIFO::LockHandlerImpl::~LockHandlerImpl()
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(parent->mutex);
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
/// Remove weak_ptr to the handler, since there are no owners of the current lock
|
|
|
|
parent->thread_to_handler.erase(it_handler);
|
|
|
|
|
|
|
|
/// Removes myself from client list of our group
|
|
|
|
it_group->clients.erase(it_client);
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
/// Remove the group if we were the last client and notify the next group
|
|
|
|
if (it_group->clients.empty())
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
2018-08-26 02:08:35 +00:00
|
|
|
auto & parent_queue = parent->queue;
|
|
|
|
parent_queue.erase(it_group);
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2018-08-26 02:08:35 +00:00
|
|
|
if (!parent_queue.empty())
|
|
|
|
parent_queue.front().cv.notify_all();
|
2017-08-31 21:11:25 +00:00
|
|
|
}
|
2017-09-01 15:05:23 +00:00
|
|
|
|
|
|
|
parent.reset();
|
2017-08-31 21:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-04 12:49:49 +00:00
|
|
|
RWLockFIFO::LockHandlerImpl::LockHandlerImpl(RWLockFIFOPtr && parent, RWLockFIFO::GroupsContainer::iterator it_group,
|
|
|
|
RWLockFIFO::ClientsContainer::iterator it_client)
|
2017-09-26 17:19:16 +00:00
|
|
|
: parent{std::move(parent)}, it_group{it_group}, it_client{it_client},
|
|
|
|
active_client_increment{(it_client->type == RWLockFIFO::Read) ? CurrentMetrics::RWLockActiveReaders
|
|
|
|
: CurrentMetrics::RWLockActiveWriters}
|
|
|
|
{}
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
}
|