-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZk.cpp
More file actions
333 lines (315 loc) · 8.63 KB
/
Copy pathZk.cpp
File metadata and controls
333 lines (315 loc) · 8.63 KB
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
#include <cstring>
#include <map>
#include <cstdio>
#include <signal.h>
#include <sys/types.h>
#include <string>
#include <iostream>
#include <errno.h>
#include "ConstDef.h"
#include "Config.h"
#include "Util.h"
#include "Process.h"
#include "Log.h"
#include "Zk.h"
using namespace std;
char _zkLockBuf[512] = {0};
Zk* Zk::zk = NULL;
Zk* Zk::getInstance() {
if (!zk) {
zk = new Zk();
}
return zk;
}
void Zk::processDeleteEvent(zhandle_t* zhandle, const string& path) {
Zk* zk = Zk::getInstance();
Config* conf = Config::getInstance();
if (path == conf->getNodeList()) {
LOG(LOG_INFO, "node %s is removed", path.c_str());
zk->createZnode(path);
}
if (path == conf->getMonitorList()) {
LOG(LOG_INFO, "monitor dir %s is removed. Restart main loop", path.c_str());
Process::setStop();
}
}
/*
The watcher in Zk will deal with events as follow:
1. zk disconnect with zookeeper server -> restart main loop
2. md5List is removed -> create it
3. monitorList is removed -> restart main loop
*/
void Zk::watcher(zhandle_t* zhandle, int type, int state, const char* node, void* context){
dp();
switch (type) {
case SESSION_EVENT_DEF:
if (state == ZOO_EXPIRED_SESSION_STATE) {
LOG(LOG_DEBUG, "[session state: ZOO_EXPIRED_STATA: %d]", state);
LOG(LOG_INFO, "restart the main loop!");
kill(getpid(), SIGUSR2);
}
else {
LOG(LOG_DEBUG, "[ session state: %d ]", state);
}
break;
case CHILD_EVENT_DEF:
LOG(LOG_DEBUG, "[ child event ] ...");
break;
case CREATED_EVENT_DEF:
LOG(LOG_DEBUG, "[ created event ]...");
break;
case DELETED_EVENT_DEF:
LOG(LOG_DEBUG, "[ deleted event ] ...");
processDeleteEvent(zhandle, string(node));
break;
case CHANGED_EVENT_DEF:
LOG(LOG_DEBUG, "[ changed event ] ...");
break;
default:
break;
}
}
Zk::Zk():_zh(NULL), _recvTimeout(3000), _zkLogPath(""), _zkHost(""), _zkLogFile(NULL) {
conf = Config::getInstance();
}
/*
* Initialize Zk api environment.
*
* zk_host [in] zookeeper server host
* log_path [in] zookeerp api log path
* recv_timeout [in] receive timeout
*
*/
int Zk::initEnv(const string zkHost, const string zkLogPath, const int recvTimeout) {
if (zkLogPath.size() <= 0) {
return M_ERR;
}
_zkLogFile = fopen(zkLogPath.c_str(), "a");
if (!_zkLogFile) {
LOG(LOG_ERROR, "log file open failed. path:%s. error:%s", zkLogPath.c_str(), strerror(errno));
return M_ERR;
}
_zkLogPath = zkLogPath;
//set the log file stream of zookeeper
zoo_set_log_stream(_zkLogFile);
LOG(LOG_INFO, "zoo_set_log_stream path:%s", zkLogPath.c_str());
if (zkHost.size() <= 0) {
LOG(LOG_ERROR, "zkHost %s wrong!", zkHost.c_str());
fclose(_zkLogFile);
return M_ERR;
}
_zkHost = zkHost;
_recvTimeout = recvTimeout > 1000 ? recvTimeout : _recvTimeout;
//init zookeeper handler
_zh = zookeeper_init(zkHost.c_str(), watcher, _recvTimeout, NULL, NULL, 0);
if (!_zh) {
LOG(LOG_ERROR, "zookeeper_init failed. Check whether zk_host(%s) is correct or not", zkHost.c_str());
fclose(_zkLogFile);
return M_ERR;
}
return M_OK;
}
void Zk::destroyEnv() {
if (_zh) {
LOG(LOG_DEBUG, "zookeeper close ...");
zookeeper_close(_zh);
_zh = NULL;
}
if (_zkLogFile) {
// set the zookeeper log stream to be default stderr
zoo_set_log_stream(NULL);
LOG(LOG_DEBUG, "zkLog close ...");
fclose(_zkLogFile);
_zkLogFile = NULL;
}
zk = NULL;
}
Zk::~Zk(){
destroyEnv();
};
void Zk::zErrorHandler(const int& ret) {
if (ret == ZSESSIONEXPIRED || /*!< The session has been expired by the server */
ret == ZSESSIONMOVED || /*!< session moved to another server, so operation is ignored */
ret == ZOPERATIONTIMEOUT ||/*!< Operation timeout */
ret == ZINVALIDSTATE) /*!< Invliad zhandle state */
{
LOG(LOG_ERROR, "API return: %s. Reinitialize zookeeper handle.", zerror(ret));
}
else if (ret == ZCLOSING || /*!< ZooKeeper is closing */
ret == ZCONNECTIONLOSS) /*!< Connection to the server has been lost */
{
LOG(LOG_FATAL_ERROR, "connect to zookeeper Failed!. API return : %s. Try to initialize zookeeper handle", zerror(ret));
}
}
bool Zk::znodeExist(const string& path) {
if (!_zh) {
LOG(LOG_ERROR, "_zh is NULL!");
return false;
}
struct Stat stat;
int ret = zoo_exists(_zh, path.c_str(), 1, &stat);
if (ret == ZOK) {
LOG(LOG_INFO, "node exist. node: %s", path.c_str());
return true;
}
else if (ret == ZNONODE) {
LOG(LOG_INFO, "node not exist. node: %s", path.c_str());
return false;
}
else {
LOG(LOG_ERROR, "zoo_exist failed. error: %s. node: %s", zerror(ret), path.c_str());
zErrorHandler(ret);
return false;
}
}
//after create znode, set the watcher with zoo_exists
int Zk::createZnode(string path) {
vector<string> nodeList = Util::split(path, '/');
string node("");
//root should sure be exist
for (auto it = nodeList.begin(); it != nodeList.end(); ++it) {
node += "/";
node += (*it);
int ret = zoo_create(_zh, node.c_str(), NULL, 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
if (ret == ZOK) {
LOG(LOG_INFO, "Create node succeeded. node: %s", node.c_str());
}
else if (ret == ZNODEEXISTS) {
LOG(LOG_INFO, "Create node .Node exists. node: %s", node.c_str());
}
else {
LOG(LOG_ERROR, "create node failed. error: %s. node: %s ", zerror(ret), node.c_str());
zErrorHandler(ret);
return M_ERR;
}
}
//add the watcher
struct Stat stat;
zoo_exists(_zh, node.c_str(), 1, &stat);
return M_OK;
}
int Zk::createZnode2(string path) {
if (path.size() <= 0) {
LOG(LOG_ERROR, "parameter error, path is empty");
return M_ERR;
}
if (path[0] != '/') {
path = '/' + path;
}
if (path.back() == '/') {
path.pop_back();
}
string node = path;
while (1) {
int ret = zoo_create(_zh, node.c_str(), NULL, 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0);
if (ret == ZOK) {
LOG(LOG_INFO, "Create node succeeded. node: %s", path.c_str());
if (node == path) {
break;
}
else {
node = path;
}
}
else if (ret == ZNODEEXISTS) {
LOG(LOG_INFO, "Create node .Node exists. node: %s", path.c_str());
if (node == path) {
break;
}
else {
node = path;
}
}
else if (ret == ZNONODE) {
size_t pos = node.rfind('/');
node = node.substr(0, pos);
}
else {
LOG(LOG_ERROR, "create node failed. error: %s. node: %s ", zerror(ret), node.c_str());
zErrorHandler(ret);
return M_ERR;
}
}
//add the watcher
struct Stat stat;
zoo_exists(_zh, node.c_str(), 1, &stat);
return M_OK;
}
int Zk::checkAndCreateZnode(string path) {
if (path.size() <= 0) {
return M_ERR;
}
if (path[0] != '/') {
path = '/' + path;
}
if (path.back() == '/') {
path.pop_back();
}
// check weather the node exist
if (znodeExist(path)) {
return M_OK;
}
else {
if (createZnode(path) == M_OK) {
LOG(LOG_INFO, "create znode succeeded, path:%s", path.c_str());
return M_OK;
}
else {
LOG(LOG_ERROR, "create znode failed, path:%s", path.c_str());
return M_ERR;
}
}
}
int Zk::registerMonitor(string path) {
int ret = ZOK;
while (_zh) {
memset(_zkLockBuf, 0, sizeof(_zkLockBuf));
string hostName = conf->getMonitorHostname();
//register the monitor.
if (hostName.empty()) {
ret = zoo_create(_zh, path.c_str(), NULL, 0, &ZOO_OPEN_ACL_UNSAFE,
ZOO_EPHEMERAL | ZOO_SEQUENCE, _zkLockBuf, sizeof(_zkLockBuf));
}
//if set the value of monitor node. It may be used in rebalance
else {
ret = zoo_create(_zh, path.c_str(), hostName.c_str(), hostName.length(), &ZOO_OPEN_ACL_UNSAFE,
ZOO_EPHEMERAL | ZOO_SEQUENCE, _zkLockBuf, sizeof(_zkLockBuf));
}
if (ret == ZOK) {
LOG(LOG_INFO, "Create zookeeper node succeeded. node: %s", _zkLockBuf);
}
else if (ret == ZNODEEXISTS) {
LOG(LOG_INFO, "Create zookeeper node .Node exists. node: %s", _zkLockBuf);
}
else {
LOG(LOG_ERROR, "create zookeeper node failed. API return : %d. node: %s ", ret, path.c_str());
zErrorHandler(ret);
// wait a second
sleep(1);
continue;
}
break;
}
if (!_zh) {
LOG(LOG_TRACE, "zkLock...out...error return...");
return M_ERR;
}
return M_OK;
}
int Zk::setZnode(string node, string data) {
int ver = -1; //will not check the version of node
if (!_zh) {
LOG(LOG_FATAL_ERROR, "_zh is NULL. restart the main loop!");
Process::setStop();
return -1;
}
int status = zoo_set(_zh, node.c_str(), data.c_str(), data.length(), ver);
if (status == ZOK) {
return 0;
}
else {
LOG(LOG_ERROR, "%s failed when zoo_set node(%s), data(%d), error:%s", \
__FUNCTION__, node.c_str(), data.c_str(), zerror(status));
return -1;
}
}