#pragma once #include #include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int NO_ZOOKEEPER; } } 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: ZooKeeperNodeCache(GetZooKeeper get_zookeeper); ZooKeeperNodeCache(const ZooKeeperNodeCache &) = delete; ZooKeeperNodeCache(ZooKeeperNodeCache &&) = default; std::experimental::optional get(const std::string & path); Poco::Event & getChangedEvent() { return context->changed_event; } private: GetZooKeeper get_zookeeper; struct Context { Poco::Event changed_event; std::mutex mutex; zkutil::ZooKeeperPtr zookeeper; std::unordered_set invalidated_paths; }; std::shared_ptr context; std::unordered_set nonexistent_nodes; std::unordered_map node_cache; }; }