2017-03-17 00:44:00 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <mutex>
|
|
|
|
#include <memory>
|
2017-11-20 04:15:43 +00:00
|
|
|
#include <optional>
|
2017-03-17 00:44:00 +00:00
|
|
|
#include <Poco/Event.h>
|
2018-01-15 19:07:47 +00:00
|
|
|
#include "ZooKeeper.h"
|
|
|
|
#include "Common.h"
|
2017-03-17 00:44:00 +00:00
|
|
|
|
|
|
|
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.
|
2018-10-17 17:23:10 +00:00
|
|
|
/// After a watch fires, the callback or event that was passed by the user is notified.
|
|
|
|
///
|
2017-03-17 00:44:00 +00:00
|
|
|
/// NOTE: methods of this class are not thread-safe.
|
2018-10-17 17:23:10 +00:00
|
|
|
///
|
|
|
|
/// Intended use case: if you need one thread to watch changes in several nodes.
|
|
|
|
/// If instead you use simple a watch event for this, watches will accumulate for nodes that do not change
|
|
|
|
/// or change rarely.
|
2017-03-17 00:44:00 +00:00
|
|
|
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
|
|
|
|
2018-11-28 11:40:59 +00:00
|
|
|
struct ZNode
|
2018-10-17 16:08:00 +00:00
|
|
|
{
|
|
|
|
bool exists = false;
|
|
|
|
std::string contents;
|
|
|
|
Coordination::Stat stat;
|
|
|
|
};
|
|
|
|
|
2018-11-28 11:40:59 +00:00
|
|
|
ZNode get(const std::string & path, EventPtr watch_event);
|
|
|
|
ZNode get(const std::string & path, Coordination::WatchCallback watch_callback);
|
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
|
|
|
|
{
|
|
|
|
std::mutex mutex;
|
|
|
|
std::unordered_set<std::string> invalidated_paths;
|
2018-12-26 17:27:25 +00:00
|
|
|
bool all_paths_invalidated = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
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
|
|
|
|
2018-11-28 11:40:59 +00:00
|
|
|
std::unordered_map<std::string, ZNode> path_to_cached_znode;
|
2017-03-17 00:44:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|