RDFS
The Rice Comp413 2017 class' continuation on the work of the 2016 RDFS.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
ZKWrapper Class Reference

Public Member Functions

 ZKWrapper (const std::string &host, int &error_code, const std::string &root)
 
std::string prepend_zk_root (const std::string &path) const
 
std::string removeZKRoot (const std::string &path) const
 
std::string removeZKRootAndDir (const std::string &prefix, const std::string &path) const
 
bool create (const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool ephemeral, bool sync=true) const
 
bool create_ephemeral (const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool sync=true) const
 
bool create_sequential (const std::string &path, const std::vector< std::uint8_t > &data, std::string &new_path, bool ephemeral, int &error_code, bool sync=true) const
 
bool recursive_create (const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool sync=true) const
 
bool exists (const std::string &path, bool &exist, int &error_code) const
 
bool wexists (const std::string &path, bool &exist, watcher_fn watch, void *watcherCtx, int &error_code) const
 
bool delete_node (const std::string &path, int &error_code, bool sync=true) const
 
bool recursive_delete (const std::string &path, int &error_code) const
 
bool get_children (const std::string &path, std::vector< std::string > &children, int &error_code) const
 
bool wget_children (const std::string &path, std::vector< std::string > &children, watcher_fn watch, void *watcherCtx, int &error_code) const
 
bool get (const std::string &path, std::vector< std::uint8_t > &data, int &error_code, bool resize) const
 
bool get_info (const std::string &path, struct Stat &stat, int &error_code) const
 
bool wget (const std::string &path, std::vector< std::uint8_t > &data, watcher_fn watch, void *watcherCtx, int &error_code, bool resize) const
 
bool set (const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool sync=true, int version=-1) const
 
std::shared_ptr< ZooOpbuild_create_op (const std::string &path, const std::vector< std::uint8_t > &data, const int flags=0) const
 
std::shared_ptr< ZooOpbuild_delete_op (const std::string &path, int version=-1) const
 
std::shared_ptr< ZooOpbuild_set_op (const std::string &path, const std::vector< std::uint8_t > &data, int version=-1) const
 
bool execute_multi (const std::vector< std::shared_ptr< ZooOp >> operations, std::vector< zoo_op_result > &results, int &error_code, bool sync=true) const
 
bool flush (const std::string &full_path, bool synchronous=false) const
 
void close ()
 

Static Public Member Functions

static std::string translate_error (int error_code)
 
static std::string translate_watch_event_type (int type)
 
static std::string translate_watch_state (int state)
 
static void watcher_znode_data (zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx)
 
static std::vector< uint8_t > get_byte_vector (const std::string &string)
 
static void print_error (int error)
 

Static Public Attributes

static const std::vector
< std::uint8_t > 
EMPTY_VECTOR = std::vector<std::uint8_t>(0)
 

Friends

void watcher (zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx)
 

Constructor & Destructor Documentation

ZKWrapper::ZKWrapper ( const std::string &  host,
int &  error_code,
const std::string &  root 
)

Initializes zookeeper. If error_code is ZOK after return, a connection to ZooKeeper would have been established, and the root would have been created.

Parameters
hostThe location of where Zookeeper is running. For local development this will usually be 'localhost:2181'
error_codeInteger reference, set to a value in ZK_ERRORS Otherwise, an error code is returned. The meaning of an error code can be retrieved from translate_error()
root

Member Function Documentation

std::shared_ptr< ZooOp > ZKWrapper::build_create_op ( const std::string &  path,
const std::vector< std::uint8_t > &  data,
const int  flags = 0 
) const
Parameters
pathpath of znode
datadata to initialize the node with. Set to the empty string to create an empty znode
flagsnode flags: ZOO_EPHEMERAL, ZOO_SEQUENCE, ZOO_EPHEMERAL || ZOO_SEQUENCE
Returns
a ZooOp to be used in execute_multi
std::shared_ptr< ZooOp > ZKWrapper::build_delete_op ( const std::string &  path,
int  version = -1 
) const
Parameters
pathof znode
versionChecks the version of the znode before deleting. Defaults to -1, which does not perform the check.
Returns
a ZooOp to be used in execute_multi
std::shared_ptr< ZooOp > ZKWrapper::build_set_op ( const std::string &  path,
const std::vector< std::uint8_t > &  data,
int  version = -1 
) const
Parameters
path
data
version
Returns
bool ZKWrapper::create ( const std::string &  path,
const std::vector< std::uint8_t > &  data,
int &  error_code,
bool  ephemeral,
bool  sync = true 
) const

Create a znode in zookeeper

Parameters
pathThe location of the new znode within the zookeeper structure
dataThe data contained in this znode
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::create_sequential ( const std::string &  path,
const std::vector< std::uint8_t > &  data,
std::string &  new_path,
bool  ephemeral,
int &  error_code,
bool  sync = true 
) const

Creates a sequential znode

Parameters
pathThe path to the new sequential znode. The last component must end in "-" like: '/foo/bar-'. When the node is created, a 10 digit sequential ID unique to the parent node will be appended to the name.
dataThe data contained in this znode
new_pathWill contain the value of the newly created path
ephemeralIf true, the created node will ephemeral
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::delete_node ( const std::string &  path,
int &  error_code,
bool  sync = true 
) const

Deletes a znode from zookeeper

Parameters
pathThe path to the znode that should be deleted
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::execute_multi ( const std::vector< std::shared_ptr< ZooOp >>  operations,
std::vector< zoo_op_result > &  results,
int &  error_code,
bool  sync = true 
) const

Runs all of the zookeeper operations within the operations vector atomically (without ordering). Atomic execution mean that either all of the operations will succeed, else they will all be rolled back.

Parameters
operationsa vector of operations to be executed
resultsa vector that maps to the results of each of the executed operations
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation worked successfully; false otherwise.
bool ZKWrapper::exists ( const std::string &  path,
bool &  exist,
int &  error_code 
) const

Checks if a znode exists or not.

Parameters
pathThe path to the node
existSet to true if a znode exists at the given path, false otherwise
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::flush ( const std::string &  full_path,
bool  synchronous = false 
) const

Flush changes inside of ZooKeeper

Parameters
full_paththe full path of the znode directory to be flushed. Must be qualified with the ZooKeeper root
synchronousWhether this operation is blocking
Returns
true on success
bool ZKWrapper::get ( const std::string &  path,
std::vector< std::uint8_t > &  data,
int &  error_code,
bool  resize 
) const

Gets the data associated with a node.

Parameters
pathThe path to the node
dataReference to a vector which will be filled with the znode data. The 2016 folks claimed that this field "Should be of size MAX_PAYLOAD when passed in, will be resized in this method." We (2017) are not sure why there is a MAX_PAYLOAD limit in the first place, and the 2016 folks never honored their own requirement.
error_codeInt reference, set to a value in ZK_ERRORS
resizeShould automatic resizing of the data vector be performed? If this argument is false, then the data buffer is expected to be large enough to hold everything in the node, otherwise the operation will fail.
Returns
True if the operation completed successfully and all of the node data fits within the data buffer; False otherwise (caller should check 'error_code' value)
bool ZKWrapper::get_children ( const std::string &  path,
std::vector< std::string > &  children,
int &  error_code 
) const

This function gets a list of children of the znode specified by the path

Parameters
pathThe path of parent node
childrenReference to a vector which will be populated with the names of the children znodes of the given path TODO: How large should this vector be when passed in?
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::get_info ( const std::string &  path,
struct Stat &  stat,
int &  error_code 
) const

Gets the info associated with a znode

Parameters
pathThe path to the node
statReference to a stat struct to be filled with znode info
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
std::string ZKWrapper::prepend_zk_root ( const std::string &  path) const

Prepends the ZooKeeper root to all paths passed in

Parameters
paththe original path
Returns
the updated path
bool ZKWrapper::recursive_create ( const std::string &  path,
const std::vector< std::uint8_t > &  data,
int &  error_code,
bool  sync = true 
) const

Recursively creates a new znode, non-existent znodes in the specified path will be created

Parameters
pathThe path to create
dataThe data to store in the new znode
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::recursive_delete ( const std::string &  path,
int &  error_code 
) const

Recursively deletes the znode specified in the path and any children of that path

Parameters
pathThe path the znode (and its children) which will be deleted
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::set ( const std::string &  path,
const std::vector< std::uint8_t > &  data,
int &  error_code,
bool  sync = true,
int  version = -1 
) const

Sets the data in a given znode

Parameters
pathThe path to the znode
dataThe data that this znode should contain
versionA version number indicating changes to the data at this node
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
std::string ZKWrapper::translate_error ( int  error_code)
static

Translate numerical error code to zookeeper error string

Parameters
error_codeInt reference, set to a value in ZK_ERRORS
Returns
A string translation of the error code
bool ZKWrapper::wexists ( const std::string &  path,
bool &  exist,
watcher_fn  watch,
void *  watcherCtx,
int &  error_code 
) const

This function is similar to 'exists' except it allows the caller to specify a watcher object rather than a boolean watch flag.

Parameters
pathThe path to the znode that needs to be checked
existSet to true if a znode exists at the given path, false otherwise
watchA watcher function
watcherCtxUser specific data, will be passed to the watcher callback.
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::wget ( const std::string &  path,
std::vector< std::uint8_t > &  data,
watcher_fn  watch,
void *  watcherCtx,
int &  error_code,
bool  resize 
) const

This function is similar to 'get' except it allows one to specify a watcher object.

Parameters
pathThe path to the node
dataReference to a vector which will be filled with the znode data Should be of size MAX_PAYLOAD when passed in, will be resized in this method
watchA watcher function
watcherCtxUser specific data, will be passed to the watcher callback.
error_codeInt reference, set to a value in ZK_ERRORS
resizeShould automatic resizing of the data vector be performed? If this argument is false, then the data buffer is expected to be large enough to hold everything in the node, otherwise the operation will fail.
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)
bool ZKWrapper::wget_children ( const std::string &  path,
std::vector< std::string > &  children,
watcher_fn  watch,
void *  watcherCtx,
int &  error_code 
) const

Similar to 'get_children', except it allows one to specify a watcher object rather than a boolean watch flag.

Parameters
pathThe path to get children of and the node to place the watch on
childrenReference to a vector which will be populated with the names of the children znodes of the given path TODO: How large should this vector be when passed in?
watchA watcher function
watcherCtxUser specific data, will be passed to the watcher callback.
error_codeInt reference, set to a value in ZK_ERRORS
Returns
True if the operation completed successfully, False otherwise (caller should check 'error_code' value)

Friends And Related Function Documentation

void watcher ( zhandle_t *  zzh,
int  type,
int  state,
const char *  path,
void *  watcherCtx 
)
friend

Currently watches the zookeeper handle and closes zookeeper on failed auths or expired sessions.

Parameters
zzhzookeeper handle
typetype of event
statestate of the event
pathpath to the watcher node
watcherCtxthe state of the watcher

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