2017-08-31 21:11:25 +00:00
|
|
|
#pragma once
|
2019-05-05 02:01:23 +00:00
|
|
|
|
2020-09-15 09:55:57 +00:00
|
|
|
#include <common/types.h>
|
2019-05-05 02:01:23 +00:00
|
|
|
|
2020-04-06 23:44:45 +00:00
|
|
|
#include <chrono>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <list>
|
2017-09-01 15:05:23 +00:00
|
|
|
#include <vector>
|
2017-08-31 21:11:25 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2017-09-04 12:49:49 +00:00
|
|
|
#include <map>
|
2017-09-04 15:14:05 +00:00
|
|
|
#include <string>
|
2019-09-05 18:09:33 +00:00
|
|
|
#include <unordered_map>
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-11-27 16:45:45 +00:00
|
|
|
class RWLockImpl;
|
|
|
|
using RWLock = std::shared_ptr<RWLockImpl>;
|
2017-08-31 21:11:25 +00:00
|
|
|
|
|
|
|
|
2020-04-16 18:56:21 +00:00
|
|
|
/// Implements Readers-Writers locking algorithm that serves requests in "Phase Fair" order.
|
2020-04-06 23:44:45 +00:00
|
|
|
/// (Phase Fair RWLock as suggested in https://www.cs.unc.edu/~anderson/papers/rtsj10-for-web.pdf)
|
2020-04-16 18:56:21 +00:00
|
|
|
/// It is used for synchronizing access to various objects on query level (i.e. Storages).
|
|
|
|
///
|
|
|
|
/// In general, ClickHouse processes queries by multiple threads of execution in parallel.
|
|
|
|
/// As opposed to the standard OS synchronization primitives (mutexes), this implementation allows
|
|
|
|
/// unlock() to be called by a thread other than the one, that called lock().
|
|
|
|
/// It is also possible to acquire RWLock in Read mode without waiting (FastPath) by multiple threads,
|
|
|
|
/// that execute the same query (share the same query_id).
|
2019-02-27 18:39:55 +00:00
|
|
|
///
|
|
|
|
/// NOTE: it is important to allow acquiring the same lock in Read mode without waiting if it is already
|
|
|
|
/// acquired by another thread of the same query. Otherwise the following deadlock is possible:
|
|
|
|
/// - SELECT thread 1 locks in the Read mode
|
|
|
|
/// - ALTER tries to lock in the Write mode (waits for SELECT thread 1)
|
|
|
|
/// - SELECT thread 2 tries to lock in the Read mode (waits for ALTER)
|
2018-11-27 16:45:45 +00:00
|
|
|
class RWLockImpl : public std::enable_shared_from_this<RWLockImpl>
|
2017-08-31 21:11:25 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
Read,
|
2018-11-27 16:45:45 +00:00
|
|
|
Write,
|
2017-08-31 21:11:25 +00:00
|
|
|
};
|
|
|
|
|
2018-11-27 16:45:45 +00:00
|
|
|
static RWLock create() { return RWLock(new RWLockImpl); }
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2019-02-27 18:34:10 +00:00
|
|
|
/// Just use LockHolder::reset() to release the lock
|
|
|
|
class LockHolderImpl;
|
|
|
|
friend class LockHolderImpl;
|
|
|
|
using LockHolder = std::shared_ptr<LockHolderImpl>;
|
2017-09-04 12:49:49 +00:00
|
|
|
|
2020-04-06 23:44:45 +00:00
|
|
|
/// Empty query_id means the lock is acquired from outside of query context (e.g. in a background thread).
|
|
|
|
LockHolder getLock(Type type, const String & query_id,
|
|
|
|
const std::chrono::milliseconds & lock_timeout_ms = std::chrono::milliseconds(0));
|
2019-02-28 09:37:05 +00:00
|
|
|
|
|
|
|
/// Use as query_id to acquire a lock outside the query context.
|
|
|
|
inline static const String NO_QUERY = String();
|
2020-04-07 11:34:35 +00:00
|
|
|
inline static const auto default_locking_timeout_ms = std::chrono::milliseconds(120000);
|
2017-09-01 15:05:23 +00:00
|
|
|
|
2017-08-31 21:11:25 +00:00
|
|
|
private:
|
2020-04-06 23:44:45 +00:00
|
|
|
/// Group of locking requests that should be granted simultaneously
|
|
|
|
/// i.e. one or several readers or a single writer
|
2017-08-31 21:11:25 +00:00
|
|
|
struct Group
|
|
|
|
{
|
|
|
|
const Type type;
|
2020-04-06 23:44:45 +00:00
|
|
|
size_t requests;
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2019-09-05 15:14:17 +00:00
|
|
|
std::condition_variable cv; /// all locking requests of the group wait on this condvar
|
2017-08-31 21:11:25 +00:00
|
|
|
|
2020-04-06 23:44:45 +00:00
|
|
|
explicit Group(Type type_) : type{type_}, requests{0} {}
|
2017-08-31 21:11:25 +00:00
|
|
|
};
|
|
|
|
|
2020-04-06 23:44:45 +00:00
|
|
|
using GroupsContainer = std::list<Group>;
|
|
|
|
using OwnerQueryIds = std::unordered_map<String, size_t>;
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable std::mutex internal_state_mtx;
|
|
|
|
|
|
|
|
GroupsContainer readers_queue;
|
|
|
|
GroupsContainer writers_queue;
|
|
|
|
GroupsContainer::iterator rdlock_owner{readers_queue.end()}; /// equals to readers_queue.begin() in read phase
|
|
|
|
/// or readers_queue.end() otherwise
|
|
|
|
GroupsContainer::iterator wrlock_owner{writers_queue.end()}; /// equals to writers_queue.begin() in write phase
|
|
|
|
/// or writers_queue.end() otherwise
|
2019-09-05 18:09:33 +00:00
|
|
|
OwnerQueryIds owner_queries;
|
|
|
|
|
2020-04-06 23:44:45 +00:00
|
|
|
private:
|
|
|
|
RWLockImpl() = default;
|
|
|
|
void unlock(GroupsContainer::iterator group_it, const String & query_id) noexcept;
|
2020-04-16 18:56:21 +00:00
|
|
|
void dropOwnerGroupAndPassOwnership(GroupsContainer::iterator group_it) noexcept;
|
2017-08-31 21:11:25 +00:00
|
|
|
};
|
|
|
|
}
|