2016-03-01 17:47:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <zkutil/ZooKeeper.h>
|
2016-03-25 12:21:10 +00:00
|
|
|
#include <zkutil/Common.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2016-03-01 17:47:53 +00:00
|
|
|
#include <Poco/Event.h>
|
|
|
|
#include <string>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace zkutil
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Distributed read/write lock for ZooKeeper.
|
|
|
|
* Such a RWLock object may not be shared among threads.
|
|
|
|
*/
|
|
|
|
class RWLock final
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
Read = 0,
|
|
|
|
Write
|
|
|
|
};
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
enum Mode
|
|
|
|
{
|
|
|
|
Blocking = 0,
|
|
|
|
NonBlocking
|
|
|
|
};
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using CancellationHook = std::function<void()>;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
RWLock() = default;
|
2016-03-03 14:37:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Create under the specified ZooKeeper path a queue for lock requests
|
|
|
|
/// if it doesn't exist yet.
|
|
|
|
RWLock(GetZooKeeper get_zookeeper_, const std::string & path_);
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
RWLock(const RWLock &) = delete;
|
|
|
|
RWLock & operator=(const RWLock &) = delete;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
RWLock(RWLock &&) = default;
|
|
|
|
RWLock & operator=(RWLock &&) = default;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
operator bool() const;
|
2016-03-03 14:37:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Register a function that cancels lock operations if requested.
|
|
|
|
void setCancellationHook(CancellationHook cancellation_hook_);
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Get a read lock.
|
|
|
|
void acquireRead(RWLock::Mode mode = RWLock::Blocking);
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Get a write lock.
|
|
|
|
void acquireWrite(RWLock::Mode mode = RWLock::Blocking);
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Check whether we have acquired the lock.
|
|
|
|
bool ownsLock() const;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Release lock.
|
|
|
|
void release();
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <Type, Mode = Blocking> class Guard;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Cancel any ongoing operation if requested.
|
|
|
|
template <RWLock::Type lock_type> void acquireImpl(RWLock::Mode mode);
|
|
|
|
void abortIfRequested();
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Helper that acquires an alive ZooKeeper session.
|
|
|
|
GetZooKeeper get_zookeeper;
|
|
|
|
EventPtr event = std::make_shared<Poco::Event>();
|
|
|
|
/// Function that cancels barrier operations if requested.
|
|
|
|
CancellationHook cancellation_hook;
|
|
|
|
/// Path to the lock request queue.
|
|
|
|
std::string path;
|
|
|
|
/// Identifier of our request for a lock.
|
|
|
|
std::string key;
|
|
|
|
bool owns_lock = false;
|
2016-03-01 17:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Scoped version of RWLock.
|
|
|
|
*/
|
|
|
|
template <RWLock::Type lock_type, RWLock::Mode lock_mode>
|
|
|
|
class RWLock::Guard final
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static_assert((lock_type == RWLock::Read) || (lock_type == RWLock::Write), "Invalid RWLock type");
|
|
|
|
static_assert((lock_mode == RWLock::Blocking) || (lock_mode == RWLock::NonBlocking), "Invalid RWLock mode");
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Acquire lock.
|
|
|
|
Guard(RWLock & rw_lock_)
|
|
|
|
: rw_lock(rw_lock_)
|
|
|
|
{
|
|
|
|
if (lock_type == RWLock::Read)
|
|
|
|
rw_lock.acquireRead(lock_mode);
|
|
|
|
else if (lock_type == RWLock::Write)
|
|
|
|
rw_lock.acquireWrite(lock_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Release lock.
|
|
|
|
~Guard()
|
|
|
|
{
|
|
|
|
if (rw_lock.ownsLock())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
rw_lock.release();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Guard(const Guard &) = delete;
|
|
|
|
Guard & operator=(const Guard &) = delete;
|
2016-03-01 17:47:53 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
RWLock & rw_lock;
|
2016-03-01 17:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|