Apache Mesos
Public Member Functions | Protected Attributes | List of all members
ZooKeeper Class Reference

#include <zookeeper.hpp>

Public Member Functions

 ZooKeeper (const std::string &servers, const Duration &sessionTimeout, Watcher *watcher)
 instantiate new ZooKeeper client. More...
 
 ~ZooKeeper ()
 
int getState ()
 get the state of the zookeeper connection. More...
 
int64_t getSessionId ()
 get the current session id. More...
 
Duration getSessionTimeout () const
 get the current session timeout. More...
 
int authenticate (const std::string &scheme, const std::string &credentials)
 authenticate synchronously. More...
 
int create (const std::string &path, const std::string &data, const ACL_vector &acl, int flags, std::string *result, bool recursive=false)
 create a node synchronously. More...
 
int remove (const std::string &path, int version)
 delete a node in zookeeper synchronously. More...
 
int exists (const std::string &path, bool watch, Stat *stat)
 checks the existence of a node in zookeeper synchronously. More...
 
int get (const std::string &path, bool watch, std::string *result, Stat *stat)
 gets the data associated with a node synchronously. More...
 
int getChildren (const std::string &path, bool watch, std::vector< std::string > *results)
 lists the children of a node synchronously. More...
 
int set (const std::string &path, const std::string &data, int version)
 sets the data associated with a node. More...
 
std::string message (int code) const
 return a message describing the return code. More...
 
bool retryable (int code)
 returns whether or not the specified return code implies the operation can be retried "as is" (i.e., without needing to change anything). More...
 

Protected Attributes

ZooKeeperProcess * process
 

Constructor & Destructor Documentation

ZooKeeper::ZooKeeper ( const std::string &  servers,
const Duration sessionTimeout,
Watcher watcher 
)

instantiate new ZooKeeper client.

The constructor initiates a new session, however session establishment is asynchronous, meaning that the session should not be considered established until (and unless) an event of state ZOO_CONNECTED_STATE is received.

Parameters
serverscomma-separated host:port pairs, each corresponding to a ZooKeeper server. e.g. "127.0.0.1:3000,127.0.0.1:3001"
watcherthe instance of Watcher that receives event callbacks. When notifications are triggered the Watcher::process method will be invoked.
ZooKeeper::~ZooKeeper ( )

Member Function Documentation

int ZooKeeper::authenticate ( const std::string &  scheme,
const std::string &  credentials 
)

authenticate synchronously.

int ZooKeeper::create ( const std::string &  path,
const std::string &  data,
const ACL_vector &  acl,
int  flags,
std::string *  result,
bool  recursive = false 
)

create a node synchronously.

This method will create a node in ZooKeeper. A node can only be created if it does not already exists. The Create Flags affect the creation of nodes. If ZOO_EPHEMERAL flag is set, the node will automatically get removed if the client session goes away. If the ZOO_SEQUENCE flag is set, a unique monotonically increasing sequence number is appended to the path name.

Parameters
pathThe name of the node. Expressed as a file name with slashes separating ancestors of the node.
dataThe data to be stored in the node.
aclThe initial ACL of the node. If null, the ACL of the parent will be used.
flagsthis parameter can be set to 0 for normal create or an OR of the Create Flags
resultA string which will be filled with the path of the new node (this might be different than the supplied path because of the ZOO_SEQUENCE flag). The path string will always be null-terminated.
recursiveif true, attempts to create all intermediate znodes as required; note that 'flags' and 'data' will only be applied to the creation of 'basename(path)'.
Returns
one of the following codes are returned: ZOK operation completed successfully ZNONODE the parent node does not exist. ZNODEEXISTS the node already exists ZNOAUTH the client does not have permission. ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. ZBADARGUMENTS - invalid input parameters ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
int ZooKeeper::exists ( const std::string &  path,
bool  watch,
Stat *  stat 
)

checks the existence of a node in zookeeper synchronously.

Parameters
paththe name of the node. Expressed as a file name with slashes separating ancestors of the node.
watchif true, a watch will be set at the server to notify the client if the node changes. The watch will be set even if the node does not exist. This allows clients to watch for nodes to appear.
statthe return stat value of the node.
Returns
return code of the function call. ZOK operation completed successfully ZNONODE the node does not exist. ZNOAUTH the client does not have permission. ZBADARGUMENTS - invalid input parameters ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
int ZooKeeper::get ( const std::string &  path,
bool  watch,
std::string *  result,
Stat *  stat 
)

gets the data associated with a node synchronously.

Parameters
paththe name of the node. Expressed as a file name with slashes separating ancestors of the node.
watchif nonzero, a watch will be set at the server to notify the client if the node changes.
resultthe data returned by the server
statif not nullptr, will hold the value of stat for the path on return.
Returns
return value of the function call. ZOK operation completed successfully ZNONODE the node does not exist. ZNOAUTH the client does not have permission. ZBADARGUMENTS - invalid input parameters ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
int ZooKeeper::getChildren ( const std::string &  path,
bool  watch,
std::vector< std::string > *  results 
)

lists the children of a node synchronously.

Parameters
paththe name of the node. Expressed as a file name with slashes separating ancestors of the node.
watchif true, a watch will be set at the server to notify the client if the node changes.
resultsreturn value of children paths.
Returns
the return code of the function. ZOK operation completed successfully ZNONODE the node does not exist. ZNOAUTH the client does not have permission. ZBADARGUMENTS - invalid input parameters ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
int64_t ZooKeeper::getSessionId ( )

get the current session id.

The current session id or 0 if no session is established.

Duration ZooKeeper::getSessionTimeout ( ) const

get the current session timeout.

The session timeout requested by the client or the negotiated session timeout after the session is established with ZooKeeper. Note that this might differ from the initial sessionTimeout specified when this instance was constructed.

int ZooKeeper::getState ( )

get the state of the zookeeper connection.

The return value will be one of the State Consts.

std::string ZooKeeper::message ( int  code) const

return a message describing the return code.

Returns
string message corresponding to return code.
int ZooKeeper::remove ( const std::string &  path,
int  version 
)

delete a node in zookeeper synchronously.

Parameters
paththe name of the node. Expressed as a file name with slashes separating ancestors of the node.
versionthe expected version of the node. The function will fail if the actual version of the node does not match the expected version. If -1 is used the version check will not take place.
Returns
one of the following values is returned. ZOK operation completed successfully ZNONODE the node does not exist. ZNOAUTH the client does not have permission. ZBADVERSION expected version does not match actual version. ZNOTEMPTY children are present; node cannot be deleted. ZBADARGUMENTS - invalid input parameters ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
bool ZooKeeper::retryable ( int  code)

returns whether or not the specified return code implies the operation can be retried "as is" (i.e., without needing to change anything).

Returns
bool indicating whether operation can be retried.
int ZooKeeper::set ( const std::string &  path,
const std::string &  data,
int  version 
)

sets the data associated with a node.

Parameters
paththe name of the node. Expressed as a file name with slashes separating ancestors of the node.
datathe data to be written to the node.
versionthe expected version of the node. The function will fail if the actual version of the node does not match the expected version. If -1 is used the version check will not take place.
Returns
the return code for the function call. ZOK operation completed successfully ZNONODE the node does not exist. ZNOAUTH the client does not have permission. ZBADVERSION expected version does not match actual version. ZBADARGUMENTS - invalid input parameters ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory

Member Data Documentation

ZooKeeperProcess* ZooKeeper::process
protected

The documentation for this class was generated from the following file: