#ifndef __STATE_ZOOKEEPER_HPP__ #define __STATE_ZOOKEEPER_HPP__ #include #include #include #include #include #include #include #include #include #include #include #include "messages/state.hpp" #include "state/serializer.hpp" #include "state/state.hpp" #include "zookeeper/authentication.hpp" #include "zookeeper/watcher.hpp" #include "zookeeper/zookeeper.hpp" namespace mesos { namespace internal { namespace state { // Forward declarations. class ZooKeeperStateProcess; template class ZooKeeperState : public State { public: // TODO(benh): Just take a zookeeper::URL. ZooKeeperState( const std::string& servers, const Duration& timeout, const std::string& znode, const Option& auth = Option()); virtual ~ZooKeeperState(); // State implementation. virtual process::Future > names(); protected: // More State implementation. virtual process::Future > fetch(const std::string& name); virtual process::Future swap(const Entry& entry, const UUID& uuid); private: ZooKeeperStateProcess* process; }; class ZooKeeperStateProcess : public process::Process { public: ZooKeeperStateProcess( const std::string& servers, const Duration& timeout, const std::string& znode, const Option& auth); virtual ~ZooKeeperStateProcess(); virtual void initialize(); // State implementation. process::Future > names(); process::Future > fetch(const std::string& name); process::Future swap(const Entry& entry, const UUID& uuid); // ZooKeeper events. void connected(bool reconnect); void reconnecting(); void expired(); void updated(const std::string& path); void created(const std::string& path); void deleted(const std::string& path); private: // Helpers for getting the names, fetching, and swapping. Result > doNames(); Result > doFetch(const std::string& name); Result doSwap(const Entry& entry, const UUID& uuid); const std::string servers; const Duration timeout; const std::string znode; Option auth; // ZooKeeper authentication. const ACL_vector acl; // Default ACL to use. Watcher* watcher; ZooKeeper* zk; enum State { // ZooKeeper connection state. DISCONNECTED, CONNECTING, CONNECTED, } state; struct Names { process::Promise > promise; }; struct Fetch { Fetch(const std::string& _name) : name(_name) {} std::string name; process::Promise > promise; }; struct Swap { Swap(const Entry& _entry, const UUID& _uuid) : entry(_entry), uuid(_uuid) {} Entry entry; UUID uuid; process::Promise promise; }; // TODO(benh): Make pending a single queue of "operations" that can // be "invoked" (C++11 lambdas would help). struct { std::queue names; std::queue fetches; std::queue swaps; } pending; Option error; }; template ZooKeeperState::ZooKeeperState( const std::string& servers, const Duration& timeout, const std::string& znode, const Option& auth) { process = new ZooKeeperStateProcess(servers, timeout, znode, auth); process::spawn(process); } template ZooKeeperState::~ZooKeeperState() { process::terminate(process); process::wait(process); delete process; } template process::Future > ZooKeeperState::names() { return process::dispatch(process, &ZooKeeperStateProcess::names); } template process::Future > ZooKeeperState::fetch( const std::string& name) { return process::dispatch(process, &ZooKeeperStateProcess::fetch, name); } template process::Future ZooKeeperState::swap( const Entry& entry, const UUID& uuid) { return process::dispatch(process, &ZooKeeperStateProcess::swap, entry, uuid); } } // namespace state { } // namespace internal { } // namespace mesos { #endif // __STATE_ZOOKEEPER_HPP__