2017-03-17 00:44:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <mutex>
|
|
|
|
#include <memory>
|
|
|
|
#include <experimental/optional>
|
|
|
|
#include <Poco/Event.h>
|
|
|
|
#include <zkutil/ZooKeeper.h>
|
|
|
|
#include <zkutil/Common.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NO_ZOOKEEPER;
|
|
|
|
}
|
2017-03-17 00:44:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace zkutil
|
|
|
|
{
|
|
|
|
|
|
|
|
/// This class allows querying the contents of ZooKeeper nodes and caching the results.
|
|
|
|
/// Watches are set for cached nodes and for nodes that were nonexistent at the time of query.
|
|
|
|
/// After a watch fires, a notification is generated for the change event.
|
|
|
|
/// NOTE: methods of this class are not thread-safe.
|
|
|
|
class ZooKeeperNodeCache
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
ZooKeeperNodeCache(GetZooKeeper get_zookeeper);
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ZooKeeperNodeCache(const ZooKeeperNodeCache &) = delete;
|
|
|
|
ZooKeeperNodeCache(ZooKeeperNodeCache &&) = default;
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 08:50:17 +00:00
|
|
|
std::experimental::optional<std::string> get(const std::string & path);
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Event & getChangedEvent() { return context->changed_event; }
|
2017-03-17 00:44:00 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
GetZooKeeper get_zookeeper;
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Context
|
|
|
|
{
|
|
|
|
Poco::Event changed_event;
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::mutex mutex;
|
|
|
|
zkutil::ZooKeeperPtr zookeeper;
|
|
|
|
std::unordered_set<std::string> invalidated_paths;
|
|
|
|
};
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::shared_ptr<Context> context;
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unordered_set<std::string> nonexistent_nodes;
|
|
|
|
std::unordered_map<std::string, std::string> node_cache;
|
2017-03-17 00:44:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|