forked from Juniper/contrail-controller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzookeeper_client.cc
377 lines (342 loc) · 11.2 KB
/
zookeeper_client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
//
#include <cerrno>
#include <string.h>
#include <base/logging.h>
#include <zookeeper/zookeeper_client.h>
#include <zookeeper/zookeeper_client_impl.h>
#include <zookeeper/zookeeper_interface.h>
#define ZOO_LOG(_Level, _Msg) \
do { \
if (LoggingDisabled()) break; \
log4cplus::Logger logger = log4cplus::Logger::getRoot(); \
LOG4CPLUS_##_Level(logger, __func__ << ":" << __FILE__ << ":" << \
__LINE__ << ": " << _Msg); \
} while (false)
#define ZOO_LOG_ERR(_Msg) \
do { \
LOG(ERROR, __func__ << ":" << __FILE__ << ":" << __LINE__ << ": " \
<< _Msg); \
} while (false)
namespace zookeeper {
namespace interface {
class ZookeeperCBindings : public ZookeeperInterface {
public:
ZookeeperCBindings() {
}
virtual ~ZookeeperCBindings() {
}
virtual void ZooSetDebugLevel(ZooLogLevel logLevel) {
zoo_set_debug_level(logLevel);
}
virtual zhandle_t* ZookeeperInit(const char *host, watcher_fn fn,
int recv_timeout, const clientid_t *clientid, void *context,
int flags) {
return zookeeper_init(host, fn, recv_timeout, clientid, context,
flags);
}
virtual int ZookeeperClose(zhandle_t *zh) {
return zookeeper_close(zh);
}
virtual int ZooState(zhandle_t *zh) {
return zoo_state(zh);
}
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) {
return zoo_create(zh, path, value, valuelen, acl, flags, path_buffer,
path_buffer_len);
}
virtual int ZooDelete(zhandle_t *zh, const char *path, int version) {
return zoo_delete(zh, path, version);
}
virtual int ZooGet(zhandle_t *zh, const char *path, int watch,
char *buffer, int* buffer_len, struct Stat *stat) {
return zoo_get(zh, path, watch, buffer, buffer_len, stat);
}
};
} // namespace interface
namespace client {
namespace impl {
// ZookeeperClientImpl
ZookeeperClientImpl::ZookeeperClientImpl(const char *hostname,
const char *servers, zookeeper::interface::ZookeeperInterface *zki) :
hostname_(hostname),
servers_(servers),
zk_handle_(NULL),
connected_(false),
zki_(zki) {
// Set loglevel
zki_->ZooSetDebugLevel(ZOO_LOG_LEVEL_DEBUG);
}
ZookeeperClientImpl::~ZookeeperClientImpl() {
}
bool ZookeeperClientImpl::Connect() {
while (true) {
zk_handle_ = zki_->ZookeeperInit(servers_.c_str(),
NULL,
kSessionTimeoutMSec_,
NULL,
NULL,
0);
// Unfortunately, EINVAL is highly overloaded in zookeeper_init
// and can correspond to:
// (1) Empty / invalid 'host' string format.
// (2) Any getaddrinfo error other than EAI_NONAME,
// EAI_NODATA, and EAI_MEMORY are mapped to EINVAL.
// Either way, retrying is not problematic.
if (zk_handle_ == NULL && errno == EINVAL) {
ZOO_LOG(WARN, "zookeeper_init FAILED: (" << errno <<
"): retrying in 1 second");
sleep(1);
continue;
}
if (zk_handle_ == NULL) {
int zerrno(errno);
ZOO_LOG_ERR("zookeeper_init returned NULL zhandle: ("
<< zerrno << ")");
return false;
}
// Block till session is connected
while (!connected_) {
int zstate(zki_->ZooState(zk_handle_));
if (zstate == ZOO_CONNECTED_STATE) {
connected_ = true;
ZOO_LOG(DEBUG, "Session CONNECTED");
break;
} else {
ZOO_LOG(DEBUG, "Session NOT CONNECTED: retrying in 1 second");
sleep(1);
continue;
}
}
break;
}
assert(connected_);
return true;
}
void ZookeeperClientImpl::Shutdown() {
if (zk_handle_) {
int rc(zki_->ZookeeperClose(zk_handle_));
if (rc != ZOK) {
int zerrno(errno);
ZOO_LOG(WARN, "zookeeper_close FAILED (" << rc <<
"): errno: " << zerrno);
}
zk_handle_ = NULL;
}
connected_ = false;
}
bool ZookeeperClientImpl::Reconnect() {
Shutdown();
return Connect();
}
bool ZookeeperClientImpl::IsConnected() const {
return connected_;
}
static inline bool IsZooErrorRecoverable(int zerror) {
return zerror == ZCONNECTIONLOSS ||
zerror == ZOPERATIONTIMEOUT;
}
static inline bool IsZooErrorUnrecoverable(int zerror) {
return zerror == ZINVALIDSTATE;
}
int ZookeeperClientImpl::CreateNodeSync(const char *path, const char *value,
int *err) {
int rc;
retry:
do {
rc = zki_->ZooCreate(zk_handle_, path, value, strlen(value),
&ZOO_OPEN_ACL_UNSAFE, 0, NULL, -1);
} while (IsZooErrorRecoverable(rc));
if (IsZooErrorUnrecoverable(rc)) {
// Reconnect
Reconnect();
goto retry;
}
if (rc != ZOK) {
*err = errno;
}
return rc;
}
int ZookeeperClientImpl::GetNodeDataSync(const char *path, char *buf,
int *buf_len, int *err) {
int rc;
retry:
do {
rc = zki_->ZooGet(zk_handle_, path, 0, buf, buf_len, NULL);
} while (IsZooErrorRecoverable(rc));
if (IsZooErrorUnrecoverable(rc)) {
// Reconnect
Reconnect();
goto retry;
}
if (rc != ZOK) {
*err = errno;
}
return rc;
}
int ZookeeperClientImpl::DeleteNodeSync(const char *path, int *err) {
int rc;
retry:
do {
rc = zki_->ZooDelete(zk_handle_, path, -1);
} while (IsZooErrorUnrecoverable(rc));
if (IsZooErrorUnrecoverable(rc)) {
// Reconnect
Reconnect();
goto retry;
}
if (rc != ZOK) {
*err = errno;
}
return rc;
}
std::string ZookeeperClientImpl::Name() const {
return hostname_;
}
} // namespace impl
// ZookeeperClient
ZookeeperClient::ZookeeperClient(const char *hostname, const char *servers) :
impl_(new impl::ZookeeperClientImpl(hostname, servers,
new zookeeper::interface::ZookeeperCBindings)) {
}
ZookeeperClient::ZookeeperClient(impl::ZookeeperClientImpl *impl) :
impl_(impl) {
}
ZookeeperClient::~ZookeeperClient() {
}
// ZookeeperLockImpl
class ZookeeperLock::ZookeeperLockImpl {
public:
ZookeeperLockImpl(impl::ZookeeperClientImpl *clientImpl,
const char *path) :
clientImpl_(clientImpl),
path_(path),
is_acquired_(false) {
id_ = clientImpl_->Name();
}
std::string Id() const {
return id_;
}
bool Lock() {
ZOO_LOG(INFO, "Trying (" << path_ << "): " << id_);
while (true) {
// Connect if not already done
if (!clientImpl_->IsConnected()) {
bool success(clientImpl_->Connect());
if (!success) {
ZOO_LOG_ERR("Zookeeper Client Connect FAILED");
return success;
}
}
// Try creating the znode
int err;
int rc(clientImpl_->CreateNodeSync(path_.c_str(), id_.c_str(),
&err));
switch (rc) {
case ZOK: {
// We acquired the lock
ZOO_LOG(INFO, "ACQUIRED (" << path_ << "): " << id_);
is_acquired_ = true;
return true;
}
case ZNODEEXISTS: {
// Node exists, get node data and check
char buf[256];
int buf_len(sizeof(buf));
int zerr;
int zrc(clientImpl_->GetNodeDataSync(path_.c_str(), buf,
&buf_len, &zerr));
if (zrc == ZOK) {
// Does it match our ID?
std::string mid(buf, buf_len);
if (id_ == mid) {
// We acquired the lock
ZOO_LOG(INFO, "ACQUIRED EEXIST (" << path_ << "): "
<< id_);
is_acquired_ = true;
return true;
}
ZOO_LOG(INFO, "EEXIST (" << path_ << "): " << mid <<
" , ours: " << id_);
sleep(1);
continue;
} else if (zrc == ZNONODE) {
ZOO_LOG(WARN, "GetNodeDataSync(" << path_ <<
"): Data: " << id_ <<
": No Node EXISTS: retrying in 1 second");
sleep(1);
continue;
} else {
ZOO_LOG_ERR("GetNodeDataSync(" << path_ << "): " <<
id_ << ": FAILED: (" << zrc << ") error: " << zerr);
clientImpl_->Shutdown();
return false;
}
break;
}
default: {
ZOO_LOG_ERR("CreateNodeSync(" << path_ << "): " << id_
<< ": FAILED: (" << rc << ") error: " << err);
clientImpl_->Shutdown();
return false;
}
}
}
}
bool Release() {
bool success;
int err, rc;
if (!is_acquired_) {
ZOO_LOG_ERR("(" << path_ << "): " << id_ <<
": Release WITHOUT Lock");
success = false;
goto cleanup;
}
is_acquired_ = false;
rc = clientImpl_->DeleteNodeSync(path_.c_str(), &err);
if (rc == ZOK) {
ZOO_LOG(INFO, "RELEASED (" << path_ << "): " << id_);
success = true;
goto cleanup;
} else if (rc == ZNONODE) {
ZOO_LOG_ERR("DeleteNodeSync(" << path_ << "): " << id_ <<
": No Node EXISTS(" << err <<
"): Possible concurrent execution");
success = false;
goto cleanup;
} else {
ZOO_LOG_ERR("DeleteNodeSync(" << path_ << "): " << id_ <<
": FAILED (" << rc << "): error " << err);
success = false;
goto cleanup;
}
cleanup:
clientImpl_->Shutdown();
return success;
}
private:
impl::ZookeeperClientImpl *clientImpl_;
std::string path_;
bool is_acquired_;
std::string id_;
};
// ZookeeperLock
ZookeeperLock::ZookeeperLock(ZookeeperClient *client, const char *path) :
impl_(new ZookeeperLockImpl(client->impl_.get(), path)) {
}
ZookeeperLock::~ZookeeperLock() {
}
std::string ZookeeperLock::Id() const {
return impl_->Id();
}
bool ZookeeperLock::Lock() {
return impl_->Lock();
}
bool ZookeeperLock::Release() {
return impl_->Release();
}
} // namespace client
} // namespace zookeeper