ClickHouse/src/Storages/MergeTree/EphemeralLockInZooKeeper.h

118 lines
3.0 KiB
C++
Raw Normal View History

2014-04-02 07:59:43 +00:00
#pragma once
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/Exception.h>
#include <IO/ReadHelpers.h>
2014-04-02 07:59:43 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
/// A class that is used for locking a block number in a partition.
/// It creates a secondary ephemeral node in `temp_path` and a main ephemeral node with `path_prefix`
/// that references the secondary node. The reasons for this two-level scheme are historical (of course
/// it would be simpler to allocate block numbers for all partitions in one ZK directory).
class EphemeralLockInZooKeeper : public boost::noncopyable
2014-04-02 07:59:43 +00:00
{
public:
EphemeralLockInZooKeeper(
const String & path_prefix_, const String & temp_path, zkutil::ZooKeeper & zookeeper_, Coordination::Requests * precheck_ops = nullptr);
EphemeralLockInZooKeeper() = default;
EphemeralLockInZooKeeper(EphemeralLockInZooKeeper && rhs) noexcept
{
*this = std::move(rhs);
}
EphemeralLockInZooKeeper & operator=(EphemeralLockInZooKeeper && rhs) noexcept
{
zookeeper = rhs.zookeeper;
rhs.zookeeper = nullptr;
path_prefix = std::move(rhs.path_prefix);
path = std::move(rhs.path);
holder_path = std::move(rhs.holder_path);
return *this;
}
bool isCreated() const
{
return zookeeper && !holder_path.empty() && !path.empty();
}
String getPath() const
{
checkCreated();
return path;
}
2017-04-16 15:00:33 +00:00
/// Parse the number at the end of the path.
UInt64 getNumber() const
{
checkCreated();
return parse<UInt64>(path.c_str() + path_prefix.size(), path.size() - path_prefix.size());
}
void unlock();
2017-04-16 15:00:33 +00:00
/// Adds actions equivalent to `unlock()` to the list.
void getUnlockOps(Coordination::Requests & ops);
2018-03-15 19:05:58 +00:00
/// Do not delete nodes in destructor. You may call this method after 'getUnlockOps' and successful execution of these ops,
/// because the nodes will be already deleted.
void assumeUnlocked()
{
2018-03-15 19:06:53 +00:00
holder_path.clear();
}
void checkCreated() const
{
if (!isCreated())
throw Exception("EphemeralLock is not created", ErrorCodes::LOGICAL_ERROR);
}
~EphemeralLockInZooKeeper();
private:
zkutil::ZooKeeper * zookeeper = nullptr;
String path_prefix;
String path;
String holder_path;
};
/// Acquires block number locks in all partitions.
2020-02-05 11:18:11 +00:00
class EphemeralLocksInAllPartitions : private boost::noncopyable
{
public:
EphemeralLocksInAllPartitions(
const String & block_numbers_path, const String & path_prefix, const String & temp_path,
zkutil::ZooKeeper & zookeeper_);
struct LockInfo
{
String path;
String holder_path;
String partition_id;
UInt64 number = 0;
};
const std::vector<LockInfo> & getLocks() const { return locks; }
void unlock();
~EphemeralLocksInAllPartitions();
2014-07-25 16:32:02 +00:00
2014-04-02 07:59:43 +00:00
private:
zkutil::ZooKeeper & zookeeper;
std::vector<LockInfo> locks;
2014-04-02 07:59:43 +00:00
};
}