-
Notifications
You must be signed in to change notification settings - Fork 16
/
zoo_craq.Tc
336 lines (301 loc) · 9.94 KB
/
zoo_craq.Tc
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
#include "logging.h"
#include "zoo_craq.h"
ptr<callback<void, bool> > init_cb;
string my_zoo_id;
int zoo_node_count = 0;
bool zoo_connected = false;
map<string, Node> zoo_nodes;
zhandle_t * zh;
map<int, timecb_t *> read_timeouts;
map<int, timecb_t *> write_timeouts;
map<int, ptr<callback<void, int> > > czoo_create_cbs;
map<int, ptr<callback<void, vector<string> *> > > czoo_get_children_cbs;
map<string, czoo_get_children_watcher> czoo_get_children_watches;
// Watcher functions with context info
map<string, czoo_get_children_watcher_ctx> czoo_get_children_watches_ctx;
map<string, void*> czoo_get_children_contexts;
map<int, ptr<callback<void, string *> > > czoo_get_cbs;
void czoo_watcher(zhandle_t *zzh, int type, int state, const char *path, void* context) {
if (type == ZOO_SESSION_EVENT) {
if (state == ZOO_CONNECTED_STATE) {
//LOG_WARN << "Connected to ZooKeeper.\n";
TRIGGER(init_cb, true);
} else if (state == ZOO_AUTH_FAILED_STATE) {
LOG_FATAL << "ZooKeeper Authentication failed.\n";
} else if (state == ZOO_EXPIRED_SESSION_STATE) {
LOG_FATAL << "ZooKeeper session expired.\n";
} else if (state == ZOO_CONNECTING_STATE ) {
LOG_WARN << "Connecting to ZooKeeper...\n";
} else if (state == ZOO_ASSOCIATING_STATE) {
LOG_WARN << "Associating ZooKeeper connection...\n";
}
} else if( type == ZOO_CREATED_EVENT ) {
} else if( type == ZOO_DELETED_EVENT ) {
} else if( type == ZOO_CHANGED_EVENT ) {
} else if( type == ZOO_CHILD_EVENT ) {
string the_path = path;
map<string, czoo_get_children_watcher>::iterator it = czoo_get_children_watches.find(the_path);
if(it != czoo_get_children_watches.end()) {
(it->second)(the_path, NULL);
}
map<string, czoo_get_children_watcher_ctx>::iterator it2 = czoo_get_children_watches_ctx.find(the_path);
if(it2 != czoo_get_children_watches_ctx.end()) {
(it2->second)(the_path, czoo_get_children_contexts[the_path], NULL);
}
} else if( type == ZOO_NOTWATCHING_EVENT ) {
}
}
void czoo_fdcb_read_timeout(int fd) {
//LOG_WARN << "read timeout " << fd << "\n";
map<int, timecb_t *>::iterator it = read_timeouts.find(fd);
if( it != read_timeouts.end() ) {
read_timeouts.erase(it);
}
//fdcb(fd, selread, 0);
int events = 0;
//events |= ZOOKEEPER_READ;
zookeeper_process(zh, events);
czoo_interest();
}
void czoo_fdcb_read(int fd) {
//LOG_WARN << "got read for fd: " << fd << "\n";
map<int, timecb_t *>::iterator it = read_timeouts.find(fd);
if( it != read_timeouts.end() ) {
timecb_remove(it->second);
read_timeouts.erase(it);
}
int events = 0;
events |= ZOOKEEPER_READ;
zookeeper_process(zh, events);
czoo_interest();
}
void czoo_fdcb_write_timeout(int fd) {
//LOG_WARN << "write timeout " << fd << "\n";
map<int, timecb_t *>::iterator it = write_timeouts.find(fd);
if( it != write_timeouts.end() ) {
write_timeouts.erase(it);
}
//fdcb(fd, selwrite, 0);
int events = 0;
//events |= ZOOKEEPER_WRITE;
zookeeper_process(zh, events);
czoo_interest();
}
void czoo_fdcb_write(int fd) {
//LOG_WARN << "got write for fd: " << fd << "\n";
map<int, timecb_t *>::iterator it = write_timeouts.find(fd);
if( it != write_timeouts.end() ) {
timecb_remove(it->second);
write_timeouts.erase(it);
}
int events = 0;
events |= ZOOKEEPER_WRITE;
zookeeper_process(zh, events);
czoo_interest();
}
void czoo_interest() {
int fd;
int interest;
timeval tv;
int ret = zookeeper_interest(zh, &fd, &interest, &tv);
if(ret != ZOK) {
if (ret == ZCONNECTIONLOSS)
LOG_FATAL << "Lost connection to zookeeper";
if (ret == ZOPERATIONTIMEOUT)
LOG_FATAL << "Timed out in connection to zookeeper";
LOG_FATAL << "Bad return from interest!\n";
}
if (fd != -1) {
if (interest & ZOOKEEPER_READ) {
//LOG_WARN << "interest read fd: " << fd << "\n";
timecb_t * timeout = delaycb(tv.tv_sec, tv.tv_usec * 1000, wrap(czoo_fdcb_read_timeout, fd));
map<int, timecb_t *>::iterator it = read_timeouts.find(fd);
if( it != read_timeouts.end() ) {
timecb_remove(it->second);
read_timeouts.erase(it);
}
read_timeouts[fd] = timeout;
fdcb(fd, selread, wrap(czoo_fdcb_read, fd));
} else {
//LOG_WARN << "disabling read fd: " << fd << "\n";
map<int, timecb_t *>::iterator it = read_timeouts.find(fd);
if( it != read_timeouts.end() ) {
timecb_remove(it->second);
read_timeouts.erase(it);
}
fdcb(fd, selread, 0);
}
if (interest & ZOOKEEPER_WRITE) {
//LOG_WARN << "interest write fd: " << fd << "\n";
timecb_t * timeout = delaycb(tv.tv_sec, tv.tv_usec * 1000, wrap(czoo_fdcb_write_timeout, fd));
map<int, timecb_t *>::iterator it = write_timeouts.find(fd);
if( it != write_timeouts.end() ) {
timecb_remove(it->second);
write_timeouts.erase(it);
}
write_timeouts[fd] = timeout;
fdcb(fd, selwrite, wrap(czoo_fdcb_write, fd));
} else {
//LOG_WARN << "disabling write fd: " << fd << "\n";
map<int, timecb_t *>::iterator it = write_timeouts.find(fd);
if( it != write_timeouts.end() ) {
timecb_remove(it->second);
write_timeouts.erase(it);
}
fdcb(fd, selwrite, 0);
}
} else {
LOG_FATAL << "got 0 fd\n";
fd = 0;
}
}
void czoo_interest_timer() {
czoo_interest();
delaycb(0, 1000 * 1000, wrap(czoo_interest_timer));
}
tamed void
czoo_init(const char * host, ptr<callback<void, bool> > cb, ZooLogLevel log_level) {
init_cb = cb;
zoo_set_debug_level(log_level);
zh = zookeeper_init(host, &czoo_watcher, 5000, 0, 0, 0);
if(!zh) {
TRIGGER( cb, false );
return;
}
zoo_connected = true;
czoo_interest_timer();
}
void czoo_created(int rc, const char *name, const void *data) {
int * where = (int *)data;
/*bool status = true;
if ( !(rc==ZOK || rc==ZNODEEXISTS) ) {
status = false;
}*/
//LOG_WARN << "rc " << rc << "\n";
//LOG_WARN << "created " << (name==NULL ? "NULL" : name) << "\n";
map<int, ptr<callback<void, int> > >::iterator it = czoo_create_cbs.find(*where);
if(it != czoo_create_cbs.end()) {
TRIGGER( it->second, rc);
czoo_create_cbs.erase(it);
}
delete where;
}
tamed void
czoo_create( string path, string value, const struct ACL_vector *acl, int flags, ptr<callback<void, int> > cb ) {
int * where = new int;
if(czoo_create_cbs.size()==0) {
*where = 0;
} else {
map<int, ptr<callback<void, int> > >::iterator it = czoo_create_cbs.end();
it--;
*where = it->first + 1;
}
czoo_create_cbs[*where] = cb;
//LOG_WARN << "path " << path.c_str() << "\n";
//LOG_WARN << "value " << value.c_str() << "\n";
//LOG_WARN << "len " << value.length() << "\n";
//LOG_WARN << "acl " << acl << "\n";
//LOG_WARN << "flags " << flags << "\n";
int rc = zoo_acreate(zh, path.c_str(), value.c_str(), value.length(),
acl, flags, &czoo_created, where);
if(rc) {
czoo_create_cbs.erase(*where);
TRIGGER( cb, rc );
delete where;
}
}
void czoo_got_children(int rc, const struct String_vector *strings, const void *data) {
int * where = (int *)data;
//LOG_WARN << "Got children.";
map<int, ptr<callback<void, vector<string> *> > >::iterator it = czoo_get_children_cbs.find(*where);
if(it != czoo_get_children_cbs.end()) {
if (rc!=ZOK || !strings) {
TRIGGER( it->second, NULL);
} else {
vector<string> * to_ret = new vector<string>(strings->count);
for (int i=0; i < strings->count; i++) {
(*to_ret)[i] = strings->data[i];
}
TRIGGER( it->second, to_ret);
}
czoo_get_children_cbs.erase(it);
}
delete where;
}
tamed void czoo_get_children(string path, czoo_get_children_watcher func,
ptr<callback<void, vector<string> *> > cb) {
int watch = 0;
if(func != NULL) {
watch = 1;
czoo_get_children_watches[path] = func;
}
int * where = new int;
if(czoo_get_children_cbs.size()==0) {
*where = 0;
} else {
map<int, ptr<callback<void, vector<string> *> > >::iterator it = czoo_get_children_cbs.end();
it--;
*where = it->first + 1;
}
czoo_get_children_cbs[*where] = cb;
int rc = zoo_aget_children(zh, path.c_str(), watch, &czoo_got_children, where);
if(rc) {
czoo_get_children_cbs.erase(*where);
TRIGGER( cb, NULL );
delete where;
}
}
tamed void czoo_get_children_2(string path, czoo_get_children_watcher_ctx func, void* context, ptr<callback<void, vector<string> *> > cb) {
int watch = 0;
if(func != NULL) {
watch = 1;
czoo_get_children_watches_ctx[path] = func;
czoo_get_children_contexts[path] = context;
}
int * where = new int;
if(czoo_get_children_cbs.size()==0) {
*where = 0;
} else {
map<int, ptr<callback<void, vector<string> *> > >::iterator it = czoo_get_children_cbs.end();
it--;
*where = it->first + 1;
}
czoo_get_children_cbs[*where] = cb;
int rc = zoo_aget_children(zh, path.c_str(), watch, &czoo_got_children, where);
if(rc) {
czoo_get_children_cbs.erase(*where);
TRIGGER( cb, NULL );
delete where;
}
}
void czoo_got(int rc, const char *value, int value_len, const struct Stat *stat, const void *data) {
int * where = (int *)data;
map<int, ptr<callback<void, string * > > >::iterator it = czoo_get_cbs.find(*where);
if(it != czoo_get_cbs.end()) {
if (rc!=ZOK) {
TRIGGER( it->second, NULL);
} else {
string * to_ret = new string(value, value_len);
TRIGGER( it->second, to_ret);
}
czoo_get_cbs.erase(it);
}
delete where;
}
tamed void czoo_get(string path, ptr<callback<void, string *> > cb) {
int * where = new int;
if(czoo_get_cbs.size()==0) {
*where = 0;
} else {
map<int, ptr<callback<void, string *> > >::iterator it = czoo_get_cbs.end();
it--;
*where = it->first + 1;
}
czoo_get_cbs[*where] = cb;
int rc = zoo_aget(zh, path.c_str(), 0, &czoo_got, where);
if (rc) {
czoo_get_cbs.erase(*where);
TRIGGER( cb, NULL );
delete where;
}
}