aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/ZooKeeper/ZooKeeperCachingGetter.h
blob: 936ee660d8d693e30d18836bbcc15746c4fc8f93 (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
#pragma once
#include <Common/ZooKeeper/Common.h>


namespace zkutil
{

class ZooKeeperCachingGetter : boost::noncopyable
{
public:
    enum class SessionStatus
    {
        New,
        Cached
    };

    explicit ZooKeeperCachingGetter(zkutil::GetZooKeeper get_zookeeper_);

    ZooKeeperCachingGetter(const ZooKeeperCachingGetter &) = delete;
    ZooKeeperCachingGetter & operator=(const ZooKeeperCachingGetter &) = delete;

    /// Returns the ZooKeeper session and the status whether it was taken from the cache or opened new,
    /// because the session has expired or the cache was empty
    std::pair<zkutil::ZooKeeperPtr, SessionStatus> getZooKeeper();
    void resetCache();

private:
    std::pair<zkutil::ZooKeeperPtr, SessionStatus> getZooKeeperNoLock() TSA_REQUIRES(cached_zookeeper_ptr_mutex);

    std::mutex cached_zookeeper_ptr_mutex;
    zkutil::ZooKeeperPtr cached_zookeeper_ptr TSA_GUARDED_BY(cached_zookeeper_ptr_mutex);

    zkutil::GetZooKeeper get_zookeeper;
};

}