14 #define ZOO_LOG(_Level, _Msg) \
16 if (LoggingDisabled()) break; \
17 log4cplus::Logger logger = log4cplus::Logger::getRoot(); \
18 LOG4CPLUS_##_Level(logger, __func__ << ":" << __FILE__ << ":" << \
19 __LINE__ << ": " << _Msg); \
22 #define ZOO_LOG_ERR(_Msg) \
24 LOG(ERROR, __func__ << ":" << __FILE__ << ":" << __LINE__ << ": " \
38 zoo_set_debug_level(logLevel);
41 int recv_timeout,
const clientid_t *clientid,
void *context,
43 return zookeeper_init(host, fn, recv_timeout, clientid, context,
47 return zookeeper_close(zh);
52 virtual int ZooCreate(zhandle_t *zh,
const char *path,
const char *value,
53 int valuelen,
const struct ACL_vector *acl,
int flags,
54 char *path_buffer,
int path_buffer_len) {
55 return zoo_create(zh, path, value, valuelen, acl, flags, path_buffer,
59 return zoo_delete(zh, path, version);
61 virtual int ZooGet(zhandle_t *zh,
const char *path,
int watch,
62 char *buffer,
int* buffer_len,
struct Stat *stat) {
63 return zoo_get(zh, path, watch, buffer, buffer_len, stat);
65 virtual int ZooExists(zhandle_t *zh,
const char *path,
int watch,
67 return zoo_exists(zh, path, watch, stat);
70 return zoo_set_context(zh, context);
73 return is_unrecoverable(zh);
83 const char* path,
void* watcherCtx) {
84 if (ZINVALIDSTATE == is_unrecoverable(zh)) {
85 ZOO_LOG(DEBUG,
"Zookeeper callback called with state " << state);
104 zki_->ZooSetDebugLevel(ZOO_LOG_LEVEL_DEBUG);
120 ZOO_LOG_ERR(
"zookeeper_init FAILED: (" << zerrno <<
121 "): servers: " <<
servers_ <<
" retrying in 1 second");
129 if (zstate == ZOO_CONNECTED_STATE) {
131 ZOO_LOG(DEBUG,
"Session CONNECTED");
134 ZOO_LOG(DEBUG,
"Session NOT CONNECTED: retrying in 1 second");
150 ZOO_LOG(WARN,
"zookeeper_close FAILED (" << rc <<
151 "): errno: " << zerrno);
168 return zerror == ZCONNECTIONLOSS ||
169 zerror == ZOPERATIONTIMEOUT;
173 return zerror == ZINVALIDSTATE;
177 int *err,
int flag) {
182 &ZOO_OPEN_ACL_UNSAFE, flag, NULL, -1);
207 if (rc != ZOK && rc != ZNODEEXISTS) {
208 ZOO_LOG_ERR(
"Creation of ZNODE(" << path <<
"): " << value
209 <<
": FAILED: (" << rc <<
") error: " << err);
216 int *buf_len,
int *err) {
278 <<
": FAILED: (" << rc <<
") error: " << err);
293 impl_(new impl::ZookeeperClientImpl(hostname, servers,
294 new zookeeper::interface::ZookeeperCBindings)) {
305 flag |= ZOO_EPHEMERAL;
308 flag |= ZOO_SEQUENCE;
310 return impl_->CreateNode(path, value, flag);
314 return impl_->CheckNodeExist(path);
318 return impl_->DeleteNode(path);
322 return impl_->Shutdown();
328 impl_->SetClient(
this);
346 std::string
Id()
const {
375 int buf_len(
sizeof(buf));
381 std::string mid(buf, buf_len);
393 }
else if (zrc == ZNONODE) {
395 "): Data: " <<
id_ <<
396 ": No Node EXISTS: retrying in 1 second");
401 id_ <<
": FAILED: (" << zrc <<
") error: " << zerr);
409 <<
": FAILED: (" << rc <<
") error: " << err);
422 ": Release WITHOUT Lock");
432 }
else if (rc == ZNONODE) {
434 ": No Node EXISTS(" << err <<
435 "): Possible concurrent execution");
440 ": FAILED (" << rc <<
"): error " << err);
469 return impl_->Lock();
473 return impl_->Release();
ZookeeperLock(ZookeeperClient *client, const char *path)
virtual void ZooSetDebugLevel(ZooLogLevel logLevel)
virtual int ZooState(zhandle_t *zh)
virtual int ZooIsUnrecoverable(zhandle_t *zh)
#define ZOO_LOG_ERR(_Msg)
int CreateNodeSync(const char *path, const char *value, int *err, int flag)
virtual int ZooGet(zhandle_t *zh, const char *path, int watch, char *buffer, int *buffer_len, struct Stat *stat)
bool CreateNode(const char *path, const char *data, int type=Z_NODE_TYPE_PERSISTENT)
bool DeleteNode(const char *path)
bool CheckNodeExist(const char *path)
std::unique_ptr< zookeeper::interface::ZookeeperInterface > zki_
bool CreateNode(const char *path, const char *value, int flag)
bool CheckNodeExist(const char *path)
static bool IsZooErrorRecoverable(int zerror)
impl::ZookeeperClientImpl * clientImpl_
virtual int ZooExists(zhandle_t *zh, const char *path, int watch, struct Stat *stat)
std::unique_ptr< ZookeeperLockImpl > impl_
ZookeeperLockImpl(impl::ZookeeperClientImpl *clientImpl, const char *path)
virtual ~ZookeeperCBindings()
#define ZOO_LOG(_Level, _Msg)
static bool IsZooErrorUnrecoverable(int zerror)
int DeleteNodeSync(const char *path, int *err)
void AddListener(ZooStateCallback cb)
virtual zhandle_t * ZookeeperInit(const char *host, watcher_fn fn, int recv_timeout, const clientid_t *clientid, void *context, int flags)
boost::function< void(void)> ZooStateCallback
virtual int ZooDelete(zhandle_t *zh, const char *path, int version)
virtual int ZooCreate(zhandle_t *zh, const char *path, const char *value, int valuelen, const struct ACL_vector *acl, int flags, char *path_buffer, int path_buffer_len)
virtual ~ZookeeperClient()
ZookeeperClient(const char *hostname, const char *servers)
int GetNodeDataSync(const char *path, char *buf, int *buf_len, int *err)
bool DeleteNode(const char *path)
static const int kSessionTimeoutMSec_
virtual int ZookeeperClose(zhandle_t *zh)
ZookeeperClientImpl(const char *hostname, const char *servers, zookeeper::interface::ZookeeperInterface *zki)
std::unique_ptr< impl::ZookeeperClientImpl > impl_
virtual void ZooSetContext(zhandle_t *zh, void *context)
virtual ~ZookeeperClientImpl()
void ZookeeperWatcher(zhandle_t *zh, int type, int state, const char *path, void *watcherCtx)