RDFS
The Rice Comp413 2017 class' continuation on the work of the 2016 RDFS.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
zkwrapper.h
1 // Copyright 2017 Rice University, COMP 413 2017
2 
3 #ifndef ZKWRAPPER_INCLUDE_ZKWRAPPER_H_
4 #define ZKWRAPPER_INCLUDE_ZKWRAPPER_H_
5 
6 #include <zookeeper.h>
7 #include <easylogging++.h>
8 #include <string.h>
9 #include <string>
10 #include <vector>
11 #include <iostream>
12 #include <memory>
13 #include <cstring>
14 #include <map>
15 #include "LRUCache.h"
16 
17 
18 /*
19  * These extend the set of error codes defined by ZooKeeper to provide more
20  * error codes that can be returned by zkwrapper.
21  */
22 enum ZK_ERRORS {
23  ZKWRAPPERINSUFFICIENTBUFFER = -997,
24  ZKWRAPPERUNINITIALIZED = -998,
25  ZKWRAPPERDEFAULTERROR = -999
26 };
27 
28 class ZkNnClient;
29 
34 class ZooOp {
35  public:
36  ZooOp(const std::string &path_in,
37  const std::vector<std::uint8_t> &data_in) {
38  this->path = new char[path_in.size() + 1];
39  snprintf(this->path, path_in.size() + 1, "%s", path_in.c_str());
40  if (data_in.size() != 0) { // Only save non-empty data
41  this->num_bytes = data_in.size();
42  this->data = new char[this->num_bytes];
43  memcpy(this->data, data_in.data(), data_in.size());
44  }
45  op = new zoo_op_t();
46  }
47 
48  ~ZooOp() {
49  delete path;
50  if (data) {
51  delete data;
52  }
53  delete op;
54  }
55 
56  zoo_op_t *op = nullptr;
57  char *path = nullptr;
58  char *data = nullptr;
59  int num_bytes = 0;
60 };
61 
62 class ZKWrapper {
63  public:
76  ZKWrapper(const std::string &host, int &error_code, const std::string &root);
77 
78  ~ZKWrapper();
79 
85  std::string prepend_zk_root(const std::string &path) const;
86 
87  std::string removeZKRoot(const std::string &path) const;
88 
89  std::string removeZKRootAndDir(const std::string &prefix,
90  const std::string &path) const;
91 
98  static std::string translate_error(int error_code);
99 
100  static std::string translate_watch_event_type(int type);
101 
102  static std::string translate_watch_state(int state);
103 
104  static void watcher_znode_data(zhandle_t *zzh,
105  int type,
106  int state,
107  const char *path,
108  void *watcherCtx);
109 
119  bool create(const std::string &path,
120  const std::vector<std::uint8_t> &data,
121  int &error_code,
122  bool ephemeral,
123  bool sync = true) const;
124 
125  bool create_ephemeral(const std::string &path,
126  const std::vector<std::uint8_t> &data,
127  int &error_code,
128  bool sync = true) const;
129 
144  bool create_sequential(const std::string &path,
145  const std::vector<std::uint8_t> &data,
146  std::string &new_path,
147  bool ephemeral,
148  int &error_code,
149  bool sync = true) const;
150 
161  bool recursive_create(const std::string &path,
162  const std::vector<std::uint8_t> &data,
163  int &error_code,
164  bool sync = true) const;
165 
176  bool exists(const std::string &path, bool &exist, int &error_code) const;
177 
192  bool wexists(const std::string &path,
193  bool &exist,
194  watcher_fn watch,
195  void *watcherCtx,
196  int &error_code) const;
197 
206  bool delete_node(const std::string &path,
207  int &error_code,
208  bool sync = true) const;
209 
219  bool recursive_delete(const std::string &path, int &error_code) const;
220 
232  bool get_children(const std::string &path,
233  std::vector<std::string> &children,
234  int &error_code) const;
235 
251  bool wget_children(const std::string &path,
252  std::vector<std::string> &children,
253  watcher_fn watch,
254  void *watcherCtx,
255  int &error_code) const;
256 
276  bool get(const std::string &path,
277  std::vector<std::uint8_t> &data,
278  int &error_code,
279  bool resize) const;
280 
290  bool get_info(const std::string &path,
291  struct Stat &stat,
292  int &error_code) const;
293 
313  bool wget(const std::string &path,
314  std::vector<std::uint8_t> &data,
315  watcher_fn watch,
316  void *watcherCtx,
317  int &error_code,
318  bool resize) const;
319 
330  bool set(const std::string &path,
331  const std::vector<std::uint8_t> &data,
332  int &error_code,
333  bool sync = true,
334  int version = -1) const;
335 
344  // TODO(2016): Crexate a path buffer for returning sequential path names
345  std::shared_ptr<ZooOp> build_create_op(const std::string &path,
346  const std::vector<std::uint8_t> &data,
347  const int flags = 0) const;
348 
355  std::shared_ptr<ZooOp> build_delete_op(const std::string &path,
356  int version = -1) const;
357 
364  std::shared_ptr<ZooOp> build_set_op(const std::string &path,
365  const std::vector<std::uint8_t> &data,
366  int version = -1) const;
367 
380  bool execute_multi(const std::vector<std::shared_ptr<ZooOp>> operations,
381  std::vector<zoo_op_result> &results,
382  int &error_code,
383  bool sync = true) const;
384 
392  bool flush(const std::string &full_path, bool synchronous = false) const;
393 
394  void close();
395 
396  static std::vector<uint8_t> get_byte_vector(const std::string &string);
397 
398  static void print_error(int error) {
399  LOG(ERROR) << "[zkwrapper] Got error: " << translate_error(error);
400  }
401 
402  static const std::vector<std::uint8_t> EMPTY_VECTOR;
403 
404  private:
405  friend void watcher(zhandle_t *zzh, int type, int state, const char *path,
406  void *watcherCtx);
407 
409  zhandle_t *zh;
410  std::string root = "";
411  /*
412  * Was a connection to ZooKeeper successfully established? Note that a
413  * successful connection requires a successful call to zookeeper_init
414  * and an invocation of the global watcher (passed into zookeeper_init)
415  * with the desired state (ZOO_CONNECTED_STATE).
416  */
417  bool connected = false;
418  /*
419  * Use the "initializing" flag to explicitly allow certain methods to execute
420  * in the constructor. Otherwise, the "initialized" flag prevents any useful
421  * method from being invoked on an unsuccessfully initialized instance.
422  */
423  bool initializing = true;
424  bool initialized = false;
425 
426  static const int MAX_PAYLOAD;
427  static const int MAX_PATH_LEN;
428  static const int NUM_SEQUENTIAL_DIGITS;
429  static const int DEFAULT_ZK_RECV_TIMEOUT;
430  static const int INITIAL_CONNECTION_TIMEOUT_MILLIS;
431  static const int INITIAL_CONNECTION_RETRY_INTERVAL_MILLIS;
432  static const int ROOT_CREATION_RETRY_LIMIT;
433  static const int ROOT_CREATION_RETRY_INTERVAL_MILLIS;
434 
435  /*
436  * TODO(2017): This field (as well fields with the same name in several other
437  * classes) is not being used anywhere. We have no idea what the 2016 folks
438  * intended to do with it.
439  */
440  static const std::string CLASS_NAME;
441 };
442 
443 #endif // ZKWRAPPER_INCLUDE_ZKWRAPPER_H_
Definition: LRUCache.h:209
std::shared_ptr< ZooOp > build_delete_op(const std::string &path, int version=-1) const
Definition: zkwrapper.cc:723
bool set(const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool sync=true, int version=-1) const
Definition: zkwrapper.cc:478
bool recursive_create(const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool sync=true) const
Definition: zkwrapper.cc:354
bool flush(const std::string &full_path, bool synchronous=false) const
Definition: zkwrapper.cc:780
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
Definition: zkwrapper.cc:306
static std::string translate_error(int error_code)
Definition: zkwrapper.cc:71
friend void watcher(zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx)
Definition: zkwrapper.cc:32
bool wget(const std::string &path, std::vector< std::uint8_t > &data, watcher_fn watch, void *watcherCtx, int &error_code, bool resize) const
Definition: zkwrapper.cc:384
std::shared_ptr< ZooOp > build_set_op(const std::string &path, const std::vector< std::uint8_t > &data, int version=-1) const
Definition: zkwrapper.cc:731
bool wget_children(const std::string &path, std::vector< std::string > &children, watcher_fn watch, void *watcherCtx, int &error_code) const
Definition: zkwrapper.cc:677
Definition: zkwrapper.h:62
std::string prepend_zk_root(const std::string &path) const
Definition: zkwrapper.cc:240
bool create(const std::string &path, const std::vector< std::uint8_t > &data, int &error_code, bool ephemeral, bool sync=true) const
Definition: zkwrapper.cc:271
bool execute_multi(const std::vector< std::shared_ptr< ZooOp >> operations, std::vector< zoo_op_result > &results, int &error_code, bool sync=true) const
Definition: zkwrapper.cc:745
std::shared_ptr< ZooOp > build_create_op(const std::string &path, const std::vector< std::uint8_t > &data, const int flags=0) const
Definition: zkwrapper.cc:707
bool get_children(const std::string &path, std::vector< std::string > &children, int &error_code) const
Definition: zkwrapper.cc:654
Definition: zkwrapper.h:34
bool exists(const std::string &path, bool &exist, int &error_code) const
Definition: zkwrapper.cc:508
bool get_info(const std::string &path, struct Stat &stat, int &error_code) const
Definition: zkwrapper.cc:584
ZKWrapper(const std::string &host, int &error_code, const std::string &root)
Definition: zkwrapper.cc:141
bool delete_node(const std::string &path, int &error_code, bool sync=true) const
Definition: zkwrapper.cc:562
bool wexists(const std::string &path, bool &exist, watcher_fn watch, void *watcherCtx, int &error_code) const
Definition: zkwrapper.cc:532
bool recursive_delete(const std::string &path, int &error_code) const
Definition: zkwrapper.cc:612