aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/ZooKeeper/ZooKeeperLock.cpp
blob: 6ee1c380efbbc9e0ae5f24d62c1efddc877c6080 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <Common/ZooKeeper/ZooKeeperLock.h>
#include <Common/logger_useful.h>
#include <Common/ErrorCodes.h>
#include <filesystem>

namespace DB
{

namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}

}

namespace fs = std::filesystem;

namespace zkutil
{

ZooKeeperLock::ZooKeeperLock(
    const ZooKeeperPtr & zookeeper_,
    const std::string & lock_prefix_,
    const std::string & lock_name_,
    const std::string & lock_message_)
    : zookeeper(zookeeper_)
    , lock_path(fs::path(lock_prefix_) / lock_name_)
    , lock_message(lock_message_)
    , log(&Poco::Logger::get("zkutil::Lock"))
{
    zookeeper->createIfNotExists(lock_prefix_, "");
}

ZooKeeperLock::~ZooKeeperLock()
{
    try
    {
        unlock();
    }
    catch (...)
    {
        DB::tryLogCurrentException(__PRETTY_FUNCTION__);
    }
}

bool ZooKeeperLock::isLocked() const
{
    return locked;
}

const std::string & ZooKeeperLock::getLockPath() const
{
    return lock_path;
}

void ZooKeeperLock::unlock()
{
    if (!locked)
        return;

    locked = false;

    if (zookeeper->expired())
    {
        LOG_WARNING(log, "Lock is lost, because session was expired. Path: {}, message: {}", lock_path, lock_message);
        return;
    }

    Coordination::Stat stat;
    /// NOTE It will throw if session expired after we checked it above
    bool result = zookeeper->exists(lock_path, &stat);

    if (result && stat.ephemeralOwner == zookeeper->getClientID())
        zookeeper->remove(lock_path, -1);
    else if (result)
        throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Lock is lost, it has another owner. Path: {}, message: {}, owner: {}, our id: {}",
                        lock_path, lock_message, stat.ephemeralOwner, zookeeper->getClientID());
    else
        throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Lock is lost, node does not exist. Path: {}, message: {}", lock_path, lock_message);
}

bool ZooKeeperLock::tryLock()
{
    Coordination::Error code = zookeeper->tryCreate(lock_path, lock_message, zkutil::CreateMode::Ephemeral);

    if (code == Coordination::Error::ZOK)
    {
        locked = true;
    }
    else if (code != Coordination::Error::ZNODEEXISTS)
    {
        throw Coordination::Exception(code);
    }

    return locked;
}

std::unique_ptr<ZooKeeperLock> createSimpleZooKeeperLock(
    const ZooKeeperPtr & zookeeper, const String & lock_prefix, const String & lock_name, const String & lock_message)
{
    return std::make_unique<ZooKeeperLock>(zookeeper, lock_prefix, lock_name, lock_message);
}


}