-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_zookeeper.c
658 lines (486 loc) · 16.6 KB
/
mod_zookeeper.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
* mod_zookeeper.c
*
* ZooKeeper connection module for Apache httpd 2.4.x.
*
* Copyright 2009, 2018 Chris Darroch
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mod_zookeeper.h"
#include "http_log.h"
#include "apr_hash.h"
#include "apr_lib.h"
#include "apr_pools.h"
#include "apr_strings.h"
#include "apr_thread_rwlock.h"
#define DEFAULT_ZOOKEEPER_RECV_TIMEOUT 4000 /* milliseconds */
#define DEFAULT_ZOOKEEPER_CONN_TRIES 5
#if !APR_HAS_THREADS
#error Threads required for ZooKeeper connections
#endif
struct ap_zk_priv_t
{
apr_pool_t *pool;
server_rec *server;
apr_thread_rwlock_t *rwlock;
int invalid;
};
struct ap_zk_watch_priv_t
{
apr_pool_t *pool;
server_rec *server;
ap_zk_t *zk;
};
static apr_hash_t *zk_handles;
module AP_MODULE_DECLARE_DATA zookeeper_module;
static const char *zk_hosts_cmd(cmd_parms *cmd, void *dconf,
const char *name, const char *hosts,
const char *arg)
{
ap_zk_t *zk;
int recv_timeout = DEFAULT_ZOOKEEPER_RECV_TIMEOUT;
const char *err;
err = ap_check_cmd_context(cmd, NOT_IN_VIRTUALHOST | NOT_IN_LIMIT);
if (err) {
return err;
}
if (apr_hash_get(zk_handles, name, APR_HASH_KEY_STRING)) {
return apr_psprintf(cmd->pool, "non-unique connection name: %s", name);
}
if (arg) {
const char *p;
for (p = arg; *p; ++p) {
if (!apr_isdigit(*p)) {
return apr_psprintf(cmd->pool,
"non-numeric ping timeout value: %s", arg);
}
}
recv_timeout = atoi(arg);
}
zk = apr_pcalloc(cmd->pool, sizeof(ap_zk_t));
zk->name = name;
zk->hosts = hosts;
zk->recv_timeout = recv_timeout;
zk->priv = apr_pcalloc(cmd->pool, sizeof(ap_zk_priv_t));
apr_hash_set(zk_handles, name, APR_HASH_KEY_STRING, zk);
return NULL;
}
static apr_status_t ap_zk_error(server_rec *s, ap_zk_t *zk, int ret,
const char *msg)
{
apr_status_t status;
if (!s) {
return APR_EINVAL;
}
if (!zk || !zk->priv) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EINVAL, s,
"invalid ZooKeeper connection");
return APR_EINVAL;
}
if (ret == ZSYSTEMERROR) {
status = errno;
}
else {
status = APR_ECONNABORTED;
if (ret == ZINVALIDSTATE || ret == ZCONNECTIONLOSS) {
zk->priv->invalid = 1;
}
}
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"%s [%s]: %s", msg, zk->name, zerror(ret));
return status;
}
static void zk_session_watcher(zhandle_t *handle, int type, int state,
const char *path, void *context)
{
if (type == ZOO_SESSION_EVENT
&& (state == ZOO_AUTH_FAILED_STATE ||
state == ZOO_EXPIRED_SESSION_STATE)) {
ap_zk_t *zk = (ap_zk_t *)zoo_get_context(handle);
ap_zk_error(zk->priv->server, zk, ZINVALIDSTATE,
"ZooKeeper session expired or authorization failed");
}
}
static apr_status_t zk_cleanup(void *data)
{
ap_zk_t *zk = data;
if (zk->handle) {
int ret;
ret = zookeeper_close(zk->handle);
zk->handle = NULL;
if (ret != ZOK) {
return ap_zk_error(zk->priv->server, zk, ret,
"unable to close ZooKeeper connection");
}
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, zk->priv->server,
"closed connection to ZooKeeper [%s]", zk->name);
}
return APR_SUCCESS;
}
static void zk_connect(ap_zk_t *zk)
{
int i = 0;
while (i++ < DEFAULT_ZOOKEEPER_CONN_TRIES) {
zk->handle = zookeeper_init(zk->hosts, zk_session_watcher,
zk->recv_timeout, NULL, zk, 0);
if (zk->handle) {
apr_pool_cleanup_register(zk->priv->pool, zk, zk_cleanup,
apr_pool_cleanup_null);
zk->priv->invalid = 0;
ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS,
zk->priv->server,
"opened connection to ZooKeeper [%s]", zk->name);
return;
}
}
ap_log_error(APLOG_MARK, APLOG_ERR, APR_TIMEUP, zk->priv->server,
"unable to open ZooKeeper connection [%s]", zk->name);
}
static int zk_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
{
zk_handles = apr_hash_make(p);
return OK;
}
static void zk_child_init(apr_pool_t *p, server_rec *s)
{
apr_hash_index_t *index;
zoo_set_debug_level(0);
for (index = apr_hash_first(p, zk_handles); index;
index = apr_hash_next(index)) {
ap_zk_t *zk;
apr_status_t status;
apr_hash_this(index, NULL, NULL, (void *)&zk);
status = apr_pool_create(&zk->priv->pool, p);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to create memory pool for "
"ZooKeeper connection [%s]", zk->name);
continue;
}
zk->priv->server = s;
status = apr_thread_rwlock_create(&zk->priv->rwlock, zk->priv->pool);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to create lock for ZooKeeper connection [%s]",
zk->name);
continue;
}
zk->priv->invalid = 1;
zk_connect(zk);
}
}
static int ap_zk_lookup(const char *name)
{
ap_zk_t *zk;
zk = apr_hash_get(zk_handles, name, APR_HASH_KEY_STRING);
return zk ? 1 : 0;
}
static apr_status_t zk_release(server_rec *s, ap_zk_t *zk, const char *type)
{
apr_status_t status;
status = apr_thread_rwlock_unlock(zk->priv->rwlock);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to release %s lock for "
"ZooKeeper connection [%s]", type, zk->name);
}
return status;
}
static apr_status_t zk_acquire(server_rec *s, const char *name,
ap_zk_t **zk_ptr, int try)
{
ap_zk_t *zk;
apr_status_t status;
if (!s) {
return APR_EINVAL;
}
if (!zk_ptr) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EINVAL, s,
"invalid ZooKeeper connection pointer");
return APR_EINVAL;
}
*zk_ptr = NULL;
zk = apr_hash_get(zk_handles, name, APR_HASH_KEY_STRING);
if (!zk) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_NOTFOUND, s,
"ZooKeeper connection not registered [%s]", name);
return APR_NOTFOUND;
}
if (!zk->priv || !zk->priv->rwlock) {
ap_log_error(APLOG_MARK, APLOG_CRIT, APR_ENOLOCK, s,
"no lock available for ZooKeeper connection [%s]", name);
return APR_ENOLOCK;
}
if (zk->priv->invalid) {
status = apr_thread_rwlock_wrlock(zk->priv->rwlock);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to acquire write lock for "
"ZooKeeper connection [%s]", name);
return status;
}
if (zk->priv->invalid) {
status = apr_pool_cleanup_run(zk->priv->pool, zk, zk_cleanup);
if (status == APR_SUCCESS) {
zk_connect(zk);
}
}
status = zk_release(s, zk, "write");
if (status != APR_SUCCESS) {
return status;
}
}
status = try ? apr_thread_rwlock_tryrdlock(zk->priv->rwlock)
: apr_thread_rwlock_rdlock(zk->priv->rwlock);
if (status == APR_SUCCESS) {
if (!zk->priv->invalid && zk->handle != NULL) {
*zk_ptr = zk;
}
else {
zk_release(s, zk, "read");
status = APR_ECONNABORTED;
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to initialize handle for "
"ZooKeeper connection [%s]", name);
}
}
else if (!try || !APR_STATUS_IS_EBUSY(status)) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to acquire read lock for "
"ZooKeeper connection [%s]", name);
}
return status;
}
static apr_status_t ap_zk_acquire(server_rec *s, const char *name,
ap_zk_t **zk_ptr)
{
return zk_acquire(s, name, zk_ptr, 0);
}
static apr_status_t ap_zk_tryacquire(server_rec *s, const char *name,
ap_zk_t **zk_ptr)
{
return zk_acquire(s, name, zk_ptr, 1);
}
static apr_status_t ap_zk_release(server_rec *s, ap_zk_t *zk)
{
if (!s) {
return APR_EINVAL;
}
if (!zk || !zk->priv) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EINVAL, s,
"invalid ZooKeeper connection");
return APR_EINVAL;
}
if (!zk->priv->rwlock) {
ap_log_error(APLOG_MARK, APLOG_CRIT, APR_ENOLOCK, s,
"no lock available for ZooKeeper connection [%s]",
zk->name);
return APR_ENOLOCK;
}
return zk_release(s, zk, "read");
}
static void ap_zk_watcher(zhandle_t *handle, int type, int state,
const char *path, void *context)
{
ap_zk_watch_t *watch = (ap_zk_watch_t *)context;
server_rec *s = watch->priv->server;
const char *name = watch->priv->zk->name;
int count;
apr_status_t status;
/* watchers may receive multiple session events before node event */
if (type == ZOO_SESSION_EVENT) {
if (watch->watcher) {
watch->watcher(handle, type, state, path, watch->context);
}
return;
}
status = apr_thread_mutex_lock(watch->mutex);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to acquire watch mutex for "
"ZooKeeper connection [%s]", name);
return;
}
--watch->count;
status = apr_thread_cond_broadcast(watch->cond);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to signal watch condition variable for "
"ZooKeeper connection [%s]", name);
}
status = apr_thread_mutex_unlock(watch->mutex);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to release watch mutex for "
"ZooKeeper connection [%s]", name);
return;
}
if (watch->watcher) {
watch->watcher(handle, type, state, path, watch->context);
}
status = apr_thread_mutex_lock(watch->mutex);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to acquire watch mutex for "
"ZooKeeper connection [%s]", name);
return;
}
count = --watch->count;
status = apr_thread_mutex_unlock(watch->mutex);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to release watch mutex for "
"ZooKeeper connection [%s]", name);
}
/* Only safe to access watch structure now if we made final decrement. */
if (count >= 0) {
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_EGENERAL, s,
"unexpected watch count %d for "
"ZooKeeper connection [%s]", count, name);
}
if (count < -1) {
apr_pool_destroy(watch->priv->pool);
}
return;
}
static apr_status_t ap_zk_create_watch(server_rec *s, ap_zk_t *zk,
ap_zk_watcher_t *watcher,
void *context,
ap_zk_watch_t **watch_ptr)
{
apr_pool_t *pool;
ap_zk_watch_t *new_watch;
apr_status_t status;
if (!s) {
return APR_EINVAL;
}
if (!zk || !zk->priv) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EINVAL, s,
"invalid ZooKeeper connection");
return APR_EINVAL;
}
if (!watch_ptr) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EINVAL, s,
"invalid ZooKeeper watch pointer");
return APR_EINVAL;
}
*watch_ptr = NULL;
status = apr_pool_create(&pool, zk->priv->pool);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to create watch memory pool for "
"ZooKeeper connection [%s]", zk->name);
return status;
}
new_watch = apr_pcalloc(pool, sizeof(ap_zk_watch_t));
status = apr_thread_mutex_create(&new_watch->mutex,
APR_THREAD_MUTEX_DEFAULT, pool);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to create watch mutex for "
"ZooKeeper connection [%s]", zk->name);
apr_pool_destroy(pool);
return status;
}
status = apr_thread_cond_create(&new_watch->cond, pool);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, status, s,
"unable to create watch condition variable for "
"ZooKeeper connection [%s]", zk->name);
apr_pool_destroy(pool);
return status;
}
new_watch->count = 1;
if (watcher) {
new_watch->watcher = watcher;
if (context) {
new_watch->context = context;
}
}
new_watch->priv = apr_pcalloc(pool, sizeof(ap_zk_watch_priv_t));
new_watch->priv->pool = pool;
new_watch->priv->server = s;
new_watch->priv->zk = zk;
*watch_ptr = new_watch;
return APR_SUCCESS;
}
static apr_status_t ap_zk_destroy_watch(server_rec *s, ap_zk_watch_t *watch)
{
const char *name;
int count;
apr_status_t status;
if (!s) {
return APR_EINVAL;
}
if (!watch || !watch->priv || !watch->priv->zk) {
ap_log_error(APLOG_MARK, APLOG_ERR, APR_EINVAL, s,
"invalid watch monitor");
return APR_EINVAL;
}
name = watch->priv->zk->name;
status = apr_thread_mutex_lock(watch->mutex);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to acquire watch mutex for "
"ZooKeeper connection [%s]", watch->priv->zk->name);
return status;
}
count = --watch->count;
status = apr_thread_mutex_unlock(watch->mutex);
if (status != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, status, s,
"unable to release watch mutex for "
"ZooKeeper connection [%s]", watch->priv->zk->name);
return status;
}
/* Only safe to access watch structure now if we made final decrement. */
if (count >= 0) {
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_EGENERAL, s,
"unexpected watch count %d for "
"ZooKeeper connection [%s]", count, name);
}
if (count < -1) {
apr_pool_destroy(watch->priv->pool);
}
return APR_SUCCESS;
}
static const command_rec zk_cmds[] = {
AP_INIT_TAKE23("ZKCluster", zk_hosts_cmd, NULL, RSRC_CONF,
"ZooKeeper connection name, hosts, and timeout"),
{NULL}
};
static void zk_hooks(apr_pool_t *p)
{
ap_hook_pre_config(zk_pre_config, NULL, NULL,
APR_HOOK_MIDDLE);
ap_hook_child_init(zk_child_init, NULL, NULL,
APR_HOOK_MIDDLE);
APR_REGISTER_OPTIONAL_FN(ap_zk_error);
APR_REGISTER_OPTIONAL_FN(ap_zk_lookup);
APR_REGISTER_OPTIONAL_FN(ap_zk_acquire);
APR_REGISTER_OPTIONAL_FN(ap_zk_tryacquire);
APR_REGISTER_OPTIONAL_FN(ap_zk_release);
APR_REGISTER_OPTIONAL_FN(ap_zk_watcher);
APR_REGISTER_OPTIONAL_FN(ap_zk_create_watch);
APR_REGISTER_OPTIONAL_FN(ap_zk_destroy_watch);
}
AP_DECLARE_MODULE(zookeeper) = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
zk_cmds,
zk_hooks
};