forked from mark-5/p5-net-zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZooKeeper.xs
2693 lines (2056 loc) · 65.7 KB
/
ZooKeeper.xs
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Net::ZooKeeper - Perl extension for Apache ZooKeeper
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <pthread.h> /* pthread_mutex_lock(), etc. */
#include <string.h> /* memset(), etc. */
#include <limits.h> /* CHAR_BIT */
#include <sys/time.h> /* gettimeofday() */
#include <zookeeper/zookeeper.h>
#include "build/check_zk_version.h"
#define PACKAGE_NAME "Net::ZooKeeper"
#define PACKAGE_SIGNATURE 19631123
#define STAT_PACKAGE_NAME "Net::ZooKeeper::Stat"
#define STAT_PACKAGE_SIGNATURE 19960512
#define WATCH_PACKAGE_NAME "Net::ZooKeeper::Watch"
#define WATCH_PACKAGE_SIGNATURE 20050326
#define MAX_KEY_NAME_LEN 16 /* "children_version" */
#define NUM_ACL_ENTRY_KEYS 3
#define NUM_KEYS 8
#define NUM_STAT_KEYS 11
#define NUM_WATCH_KEYS 3
#define DEFAULT_RECV_TIMEOUT_MSEC 10000
#define DEFAULT_DATA_BUF_LEN 1023
#define DEFAULT_PATH_BUF_LEN 1023
#define DEFAULT_WATCH_TIMEOUT 60000
#define ZOO_LOG_LEVEL_OFF 0
#ifndef strcaseEQ
#define strcaseEQ(a,b) (!strcasecmp((a),(b)))
#endif
typedef struct Stat zk_stat_t;
typedef HV* Net__ZooKeeper__Stat;
typedef struct zk_watch_t zk_watch_t;
struct zk_watch_t {
pthread_mutex_t mutex;
pthread_cond_t cond;
int done;
int watch_event_triggered;
int ret;
int event_type;
int event_state;
unsigned int timeout;
zk_watch_t *prev;
zk_watch_t *next;
int ref_count;
};
typedef HV* Net__ZooKeeper__Watch;
typedef struct {
zhandle_t *handle;
zk_watch_t *first_watch;
int data_buf_len;
int path_buf_len;
unsigned int watch_timeout;
const char *hosts;
int hosts_len;
int last_ret;
int last_errno;
} zk_t;
typedef HV* Net__ZooKeeper;
typedef struct {
I32 signature;
union {
zk_t *zk;
zk_stat_t *stat;
zk_watch_t *watch;
} handle;
} zk_handle_t;
typedef struct {
const char name[MAX_KEY_NAME_LEN + 1];
U32 name_len;
size_t offset;
size_t size;
U32 hash;
} zk_key_t;
static zk_key_t zk_acl_entry_keys[NUM_ACL_ENTRY_KEYS] = {
{"perms", 0, 0, 0, 0},
{"scheme", 0, 0, 0, 0},
{"id", 0, 0, 0, 0}
};
static zk_key_t zk_keys[NUM_KEYS] = {
{"data_read_len", 0, 0, 0, 0},
{"path_read_len", 0, 0, 0, 0},
{"watch_timeout", 0, 0, 0, 0},
{"hosts", 0, 0, 0, 0},
{"session_timeout", 0, 0, 0, 0},
{"session_id", 0, 0, 0, 0},
{"pending_watches", 0, 0, 0, 0},
{"state", 0, 0, 0, 0}
};
static zk_key_t zk_stat_keys[NUM_STAT_KEYS] = {
{"czxid", 0, offsetof(struct Stat, czxid),
sizeof(((struct Stat*) 0)->czxid), 0},
{"mzxid", 0, offsetof(struct Stat, mzxid),
sizeof(((struct Stat*) 0)->mzxid), 0},
{"ctime", 0, offsetof(struct Stat, ctime),
sizeof(((struct Stat*) 0)->ctime), 0},
{"mtime", 0, offsetof(struct Stat, mtime),
sizeof(((struct Stat*) 0)->mtime), 0},
{"version", 0, offsetof(struct Stat, version),
sizeof(((struct Stat*) 0)->version), 0},
{"children_version", 0, offsetof(struct Stat, cversion),
sizeof(((struct Stat*) 0)->cversion), 0},
{"acl_version", 0, offsetof(struct Stat, aversion),
sizeof(((struct Stat*) 0)->aversion), 0},
{"ephemeral_owner", 0, offsetof(struct Stat, ephemeralOwner),
sizeof(((struct Stat*) 0)->ephemeralOwner), 0},
{"data_len", 0, offsetof(struct Stat, dataLength),
sizeof(((struct Stat*) 0)->dataLength), 0},
{"num_children", 0, offsetof(struct Stat, numChildren),
sizeof(((struct Stat*) 0)->numChildren), 0},
{"children_zxid", 0, offsetof(struct Stat, pzxid),
sizeof(((struct Stat*) 0)->pzxid), 0}
};
static zk_key_t zk_watch_keys[NUM_WATCH_KEYS] = {
{"timeout", 0, 0, 0, 0},
{"event", 0, 0, 0, 0},
{"state", 0, 0, 0, 0}
};
static int zk_ignore_session_events = 0;
static void _zk_watcher(zhandle_t *handle, int type, int state,
const char *path, void *context)
{
zk_watch_t *watch_ctx = context;
bool is_session_event = type == ZOO_SESSION_EVENT;
if (is_session_event && zk_ignore_session_events) {
return;
}
if (!is_session_event) {
// mark this watch for garbage collection
watch_ctx->watch_event_triggered = 1;
}
pthread_mutex_lock(&watch_ctx->mutex);
watch_ctx->event_type = type;
watch_ctx->event_state = state;
watch_ctx->done = 1;
pthread_cond_signal(&watch_ctx->cond);
pthread_mutex_unlock(&watch_ctx->mutex);
return;
}
static void _zk_auth_completion(int ret, const void *data)
{
zk_watch_t *watch_ctx = (zk_watch_t*) data;
pthread_mutex_lock(&watch_ctx->mutex);
watch_ctx->ret = ret;
watch_ctx->done = 1;
pthread_cond_signal(&watch_ctx->cond);
pthread_mutex_unlock(&watch_ctx->mutex);
return;
}
static zk_watch_t *_zk_create_watch(pTHX)
{
zk_watch_t *watch;
Newxz(watch, 1, zk_watch_t);
if (pthread_mutex_init(&watch->mutex, NULL)) {
int save_errno = errno;
Safefree(watch);
errno = save_errno;
return NULL;
}
if (pthread_cond_init(&watch->cond, NULL)) {
int save_errno = errno;
pthread_mutex_destroy(&watch->mutex);
Safefree(watch);
errno = save_errno;
return NULL;
}
return watch;
}
static void _zk_destroy_watch(pTHX_ zk_watch_t *watch)
{
pthread_cond_destroy(&watch->cond);
pthread_mutex_destroy(&watch->mutex);
Safefree(watch);
return;
}
static zk_watch_t *_zk_acquire_watch(pTHX)
{
zk_watch_t *watch = _zk_create_watch(aTHX);
if (watch) {
watch->ref_count = 1;
}
return watch;
}
static void _zk_release_watch(pTHX_ zk_watch_t *watch, int list)
{
if (list) {
if (watch->prev) {
watch->prev->next = watch->next;
}
if (watch->next) {
watch->next->prev = watch->prev;
}
watch->prev = NULL;
watch->next = NULL;
}
if (--watch->ref_count == 0) {
_zk_destroy_watch(aTHX_ watch);
}
return;
}
static unsigned int _zk_release_watches(pTHX_ zk_watch_t *first_watch,
int final)
{
zk_watch_t *watch = first_watch->next;
unsigned int pending_watches = 0;
while (watch) {
zk_watch_t *next_watch = watch->next;
int done = final;
if (!final) {
pthread_mutex_lock(&watch->mutex);
// only release watches if the watch event has been triggered
// otherwise zookeeper may trigger it again
done = watch->watch_event_triggered;
pthread_mutex_unlock(&watch->mutex);
}
if (done) {
_zk_release_watch(aTHX_ watch, 1);
}
else {
++pending_watches;
}
watch = next_watch;
}
return pending_watches;
}
static void _zk_replace_watch(pTHX_ zk_handle_t *handle,
zk_watch_t *first_watch,
zk_watch_t *old_watch, zk_watch_t *new_watch)
{
zk_watch_t *next_watch;
new_watch->timeout = old_watch->timeout;
_zk_release_watch(aTHX_ old_watch, 0);
/* cleanup any completed watches not tied to a handle */
_zk_release_watches(aTHX_ first_watch, 0);
next_watch = first_watch->next;
new_watch->prev = first_watch;
new_watch->next = next_watch;
if (next_watch) {
next_watch->prev = new_watch;
}
first_watch->next = new_watch;
++new_watch->ref_count;
handle->handle.watch = new_watch;
return;
}
static void _zk_free_acl(pTHX_ struct ACL_vector *acl)
{
if (acl->data) {
Safefree(acl->data);
}
return;
}
static const char *_zk_fill_acl(pTHX_ AV *acl_arr, struct ACL_vector *acl)
{
I32 num_acl_entries = av_len(acl_arr) + 1;
int i;
Zero(acl, 1, struct ACL_vector);
if (num_acl_entries <= 0) {
return NULL;
}
else if (num_acl_entries > PERL_INT_MAX) {
num_acl_entries = PERL_INT_MAX;
}
Newx(acl->data, num_acl_entries, struct ACL);
for (i = 0; i < num_acl_entries; ++i) {
SV **acl_entry_ptr;
HV *acl_entry_hash;
zk_key_t *key;
SV **val_ptr;
struct ACL acl_entry;
acl_entry_ptr = av_fetch(acl_arr, i, 0);
if (!acl_entry_ptr) {
continue;
}
if (!SvROK(*acl_entry_ptr) ||
SvTYPE(SvRV(*acl_entry_ptr)) != SVt_PVHV) {
_zk_free_acl(aTHX_ acl);
return "invalid ACL entry hash reference";
}
acl_entry_hash = (HV*) SvRV(*acl_entry_ptr);
key = &zk_acl_entry_keys[0];
val_ptr = hv_fetch(acl_entry_hash, key->name, key->name_len, 0);
if (!val_ptr) {
_zk_free_acl(aTHX_ acl);
return "no ACL entry perms element";
}
acl_entry.perms = SvIV(*val_ptr);
if (!acl_entry.perms || (acl_entry.perms & ~ZOO_PERM_ALL)) {
_zk_free_acl(aTHX_ acl);
return "invalid ACL entry perms";
}
key = &zk_acl_entry_keys[1];
val_ptr = hv_fetch(acl_entry_hash, key->name, key->name_len, 0);
if (!val_ptr) {
_zk_free_acl(aTHX_ acl);
return "no ACL entry scheme element";
}
acl_entry.id.scheme = SvPV_nolen(*val_ptr);
key = &zk_acl_entry_keys[2];
val_ptr = hv_fetch(acl_entry_hash, key->name, key->name_len, 0);
if (!val_ptr) {
_zk_free_acl(aTHX_ acl);
return "no ACL entry id element";
}
acl_entry.id.id = SvPV_nolen(*val_ptr);
++acl->count;
acl->data[i] = acl_entry;
}
return NULL;
}
static void _zk_fill_acl_entry_hash(pTHX_ struct ACL *acl_entry,
HV *acl_entry_hash)
{
zk_key_t *key;
SV *val;
key = &zk_acl_entry_keys[0];
val = newSViv(acl_entry->perms);
if (!hv_store(acl_entry_hash, key->name, key->name_len, val, key->hash)) {
SvREFCNT_dec(val);
}
key = &zk_acl_entry_keys[1];
val = newSVpv(acl_entry->id.scheme, 0);
if (!hv_store(acl_entry_hash, key->name, key->name_len, val, key->hash)) {
SvREFCNT_dec(val);
}
key = &zk_acl_entry_keys[2];
val = newSVpv(acl_entry->id.id, 0);
if (!hv_store(acl_entry_hash, key->name, key->name_len, val, key->hash)) {
SvREFCNT_dec(val);
}
return;
}
static zk_handle_t *_zk_check_handle_inner(pTHX_ HV *attr_hash,
I32 package_signature)
{
zk_handle_t *handle = NULL;
if (SvRMAGICAL(attr_hash)) {
MAGIC *magic = mg_find((SV*) attr_hash, PERL_MAGIC_ext);
if (magic) {
handle = (zk_handle_t*) magic->mg_ptr;
if (handle->signature != package_signature) {
handle = NULL;
}
}
}
return handle;
}
static zk_handle_t *_zk_check_handle_outer(pTHX_ HV *hash, HV **attr_hash_ptr,
const char *package_name,
I32 package_signature)
{
zk_handle_t *handle = NULL;
if (attr_hash_ptr) {
*attr_hash_ptr = NULL;
}
if (SvRMAGICAL((SV*) hash)) {
MAGIC *magic = mg_find((SV*) hash, PERL_MAGIC_tied);
if (magic) {
SV *attr = magic->mg_obj;
if (SvROK(attr) && SvTYPE(SvRV(attr)) == SVt_PVHV &&
sv_derived_from(attr, package_name)) {
HV *attr_hash = (HV*) SvRV(attr);
handle = _zk_check_handle_inner(aTHX_ attr_hash,
package_signature);
if (handle && attr_hash_ptr) {
*attr_hash_ptr = attr_hash;
}
}
}
}
return handle;
}
static zk_t *_zk_get_handle_inner(pTHX_ Net__ZooKeeper attr_hash)
{
zk_handle_t *handle;
handle = _zk_check_handle_inner(aTHX_ attr_hash, PACKAGE_SIGNATURE);
return handle ? handle->handle.zk : NULL;
}
static zk_t *_zk_get_handle_outer(pTHX_ Net__ZooKeeper zkh)
{
zk_handle_t *handle;
handle = _zk_check_handle_outer(aTHX_ zkh, NULL, PACKAGE_NAME,
PACKAGE_SIGNATURE);
return handle ? handle->handle.zk : NULL;
}
static zk_stat_t *_zks_get_handle_inner(pTHX_ Net__ZooKeeper__Stat attr_hash)
{
zk_handle_t *handle;
handle = _zk_check_handle_inner(aTHX_ attr_hash, STAT_PACKAGE_SIGNATURE);
return handle ? handle->handle.stat : NULL;
}
static zk_stat_t *_zks_get_handle_outer(pTHX_ Net__ZooKeeper__Stat zksh)
{
zk_handle_t *handle;
handle = _zk_check_handle_outer(aTHX_ zksh, NULL, STAT_PACKAGE_NAME,
STAT_PACKAGE_SIGNATURE);
return handle ? handle->handle.stat : NULL;
}
static zk_watch_t *_zkw_get_handle_inner(pTHX_ Net__ZooKeeper__Watch attr_hash)
{
zk_handle_t *handle;
handle = _zk_check_handle_inner(aTHX_ attr_hash, WATCH_PACKAGE_SIGNATURE);
return handle ? handle->handle.watch : NULL;
}
static zk_watch_t *_zkw_get_handle_outer(pTHX_ Net__ZooKeeper__Watch zkwh,
zk_handle_t **handle_ptr)
{
zk_handle_t *handle;
handle = _zk_check_handle_outer(aTHX_ zkwh, NULL, WATCH_PACKAGE_NAME,
WATCH_PACKAGE_SIGNATURE);
if (handle_ptr) {
*handle_ptr = handle;
}
return handle ? handle->handle.watch : NULL;
}
MODULE = Net::ZooKeeper PACKAGE = Net::ZooKeeper PREFIX = zk_
REQUIRE: 1.9508
PROTOTYPES: ENABLE
BOOT:
{
int i;
for (i = 0; i < NUM_ACL_ENTRY_KEYS; ++i) {
zk_key_t *key = &zk_acl_entry_keys[i];
key->name_len = strlen(key->name);
PERL_HASH(key->hash, key->name, key->name_len);
}
for (i = 0; i < NUM_KEYS; ++i) {
zk_keys[i].name_len = strlen(zk_keys[i].name);
}
for (i = 0; i < NUM_STAT_KEYS; ++i) {
zk_stat_keys[i].name_len = strlen(zk_stat_keys[i].name);
}
for (i = 0; i < NUM_WATCH_KEYS; ++i) {
zk_watch_keys[i].name_len = strlen(zk_watch_keys[i].name);
}
zoo_set_log_stream(NULL);
zoo_set_debug_level(0);
}
I32
zk_constant(alias=Nullch)
char *alias
ALIAS:
ZOK = ZOK
ZSYSTEMERROR = ZSYSTEMERROR
ZRUNTIMEINCONSISTENCY = ZRUNTIMEINCONSISTENCY
ZDATAINCONSISTENCY = ZDATAINCONSISTENCY
ZCONNECTIONLOSS = ZCONNECTIONLOSS
ZMARSHALLINGERROR = ZMARSHALLINGERROR
ZUNIMPLEMENTED = ZUNIMPLEMENTED
ZOPERATIONTIMEOUT = ZOPERATIONTIMEOUT
ZBADARGUMENTS = ZBADARGUMENTS
ZINVALIDSTATE = ZINVALIDSTATE
ZAPIERROR = ZAPIERROR
ZNONODE = ZNONODE
ZNOAUTH = ZNOAUTH
ZBADVERSION = ZBADVERSION
ZNOCHILDRENFOREPHEMERALS = ZNOCHILDRENFOREPHEMERALS
ZNODEEXISTS = ZNODEEXISTS
ZNOTEMPTY = ZNOTEMPTY
ZSESSIONEXPIRED = ZSESSIONEXPIRED
ZINVALIDCALLBACK = ZINVALIDCALLBACK
ZINVALIDACL = ZINVALIDACL
ZAUTHFAILED = ZAUTHFAILED
ZCLOSING = ZCLOSING
ZNOTHING = ZNOTHING
ZSESSIONMOVED = ZSESSIONMOVED
ZOO_EPHEMERAL = ZOO_EPHEMERAL
ZOO_SEQUENCE = ZOO_SEQUENCE
ZOO_PERM_READ = ZOO_PERM_READ
ZOO_PERM_WRITE = ZOO_PERM_WRITE
ZOO_PERM_CREATE = ZOO_PERM_CREATE
ZOO_PERM_DELETE = ZOO_PERM_DELETE
ZOO_PERM_ADMIN = ZOO_PERM_ADMIN
ZOO_PERM_ALL = ZOO_PERM_ALL
ZOO_CREATED_EVENT = ZOO_CREATED_EVENT
ZOO_DELETED_EVENT = ZOO_DELETED_EVENT
ZOO_CHANGED_EVENT = ZOO_CHANGED_EVENT
ZOO_CHILD_EVENT = ZOO_CHILD_EVENT
ZOO_SESSION_EVENT = ZOO_SESSION_EVENT
ZOO_NOTWATCHING_EVENT = ZOO_NOTWATCHING_EVENT
ZOO_EXPIRED_SESSION_STATE = ZOO_EXPIRED_SESSION_STATE
ZOO_AUTH_FAILED_STATE = ZOO_AUTH_FAILED_STATE
ZOO_CONNECTING_STATE = ZOO_CONNECTING_STATE
ZOO_ASSOCIATING_STATE = ZOO_ASSOCIATING_STATE
ZOO_CONNECTED_STATE = ZOO_CONNECTED_STATE
ZOO_LOG_LEVEL_OFF = ZOO_LOG_LEVEL_OFF
ZOO_LOG_LEVEL_ERROR = ZOO_LOG_LEVEL_ERROR
ZOO_LOG_LEVEL_WARN = ZOO_LOG_LEVEL_WARN
ZOO_LOG_LEVEL_INFO = ZOO_LOG_LEVEL_INFO
ZOO_LOG_LEVEL_DEBUG = ZOO_LOG_LEVEL_DEBUG
CODE:
if (!ix) {
if (!alias) {
alias = GvNAME(CvGV(cv));
}
if (strEQ(alias, "ZOK")) {
RETVAL = ZOK;
}
else if (strEQ(alias, "ZOO_LOG_LEVEL_OFF")) {
RETVAL = ZOO_LOG_LEVEL_OFF;
}
else {
Perl_croak(aTHX_ "unknown " PACKAGE_NAME " constant: %s",
alias);
}
}
else {
RETVAL = ix;
}
OUTPUT:
RETVAL
AV *
zk_acl_constant(alias=Nullch)
char *alias
ALIAS:
ZOO_OPEN_ACL_UNSAFE = 1
ZOO_READ_ACL_UNSAFE = 2
ZOO_CREATOR_ALL_ACL = 3
PREINIT:
struct ACL_vector acl;
AV *acl_arr;
int i;
PPCODE:
if (!ix && !alias) {
alias = GvNAME(CvGV(cv));
}
if (ix == 1 || (alias != NULL && strEQ(alias, "ZOO_OPEN_ACL_UNSAFE"))) {
acl = ZOO_OPEN_ACL_UNSAFE;
}
else if (ix == 2 || (alias != NULL && strEQ(alias, "ZOO_READ_ACL_UNSAFE"))) {
acl = ZOO_READ_ACL_UNSAFE;
}
else if (ix == 3 || (alias != NULL && strEQ(alias, "ZOO_CREATOR_ALL_ACL"))) {
acl = ZOO_CREATOR_ALL_ACL;
}
else {
Perl_croak(aTHX_ "unknown " PACKAGE_NAME " constant: %s", alias);
}
acl_arr = newAV();
av_extend(acl_arr, acl.count);
for (i = 0; i < acl.count; ++i) {
HV *acl_entry_hash = newHV();
SV *val;
_zk_fill_acl_entry_hash(aTHX_ &acl.data[i], acl_entry_hash);
val = newRV_noinc((SV*) acl_entry_hash);
if (!av_store(acl_arr, i, val)) {
SvREFCNT_dec(val);
}
}
ST(0) = sv_2mortal(newRV_noinc((SV*) acl_arr));
XSRETURN(1);
void
zk_set_log_level(level)
int level
PPCODE:
if (level < ZOO_LOG_LEVEL_OFF || level > ZOO_LOG_LEVEL_DEBUG) {
Perl_croak(aTHX_ "invalid log level: %d", level);
}
zoo_set_debug_level(level);
XSRETURN_EMPTY;
void
zk_set_deterministic_conn_order(flag)
bool flag
PPCODE:
zoo_deterministic_conn_order(!!flag);
XSRETURN_EMPTY;
void
zk_set_ignore_session_events(flag)
bool flag
PPCODE:
zk_ignore_session_events = flag;
XSRETURN_EMPTY;
void
zk_new(package, hosts, ...)
char *package
char *hosts
PREINIT:
int recv_timeout = DEFAULT_RECV_TIMEOUT_MSEC;
const clientid_t *client_id = NULL;
zk_t *zk;
zk_handle_t *handle;
HV *stash, *zk_hash, *attr_hash;
SV *attr;
int i;
PPCODE:
if (items > 2 && items % 2) {
Perl_croak(aTHX_ "invalid number of arguments");
}
for (i = 2; i < items; i += 2) {
char *key = SvPV_nolen(ST(i));
if (strcaseEQ(key, "session_timeout")) {
recv_timeout = SvIV(ST(i + 1));
/* NOTE: would be nice if requirement in zookeeper_interest()
* that recv_timeout*2 be non-negative was documented
*/
if (recv_timeout < 0 || recv_timeout > (PERL_INT_MAX >> 1)) {
Perl_croak(aTHX_ "invalid session timeout: %d",
recv_timeout);
}
}
else if (strcaseEQ(key, "session_id")) {
STRLEN client_id_len;
client_id = (const clientid_t*) SvPV(ST(i + 1), client_id_len);
if (client_id_len != sizeof(clientid_t)) {
Perl_croak(aTHX_ "invalid session ID");
}
}
}
Newxz(zk, 1, zk_t);
zk->handle = zookeeper_init(hosts, NULL, recv_timeout,
client_id, NULL, 0);
if (!zk->handle) {
Safefree(zk);
XSRETURN_UNDEF;
}
Newxz(zk->first_watch, 1, zk_watch_t);
zk->data_buf_len = DEFAULT_DATA_BUF_LEN;
zk->path_buf_len = DEFAULT_PATH_BUF_LEN;
zk->watch_timeout = DEFAULT_WATCH_TIMEOUT;
zk->hosts_len = strlen(hosts);
zk->hosts = savepvn(hosts, zk->hosts_len);
Newx(handle, 1, zk_handle_t);
handle->signature = PACKAGE_SIGNATURE;
handle->handle.zk = zk;
/* We use several tricks from DBI here. The attr_hash is our
* empty inner hash; we attach extra magic to it in the form of
* our zk_handle_t structure. Then we tie attr_hash to zk_hash,
* our outer hash. This is what is passed around (by reference) by
* callers.
*
* Most methods use _zk_get_handle_outer() which finds our inner
* handle, then returns the zk_t structure from its extra magic
* pointer.
*
* However, the tied hash methods, FETCH(), STORE(), and so forth,
* receive an already-dereferenced inner handle hash. This is
* because we bless both inner and outer handles into this class,
* so when a caller's code references a hash element in our
* outer handle, Perl detects its tied magic, looks up the
* tied object (our inner handle) and invokes the tied hash methods
* in its class on it. Since we blessed it into the same class
* as the outer handle, these methods simply reside in our package.
*/
stash = gv_stashpv(package, GV_ADDWARN);
attr_hash = newHV();
sv_magic((SV*) attr_hash, Nullsv, PERL_MAGIC_ext,
(const char*) handle, 0);
attr = sv_bless(newRV_noinc((SV*) attr_hash), stash);
zk_hash = newHV();
sv_magic((SV*) zk_hash, attr, PERL_MAGIC_tied, Nullch, 0);
SvREFCNT_dec(attr);
ST(0) = sv_bless(sv_2mortal(newRV_noinc((SV*) zk_hash)), stash);
XSRETURN(1);
void
zk_DESTROY(zkh)
Net::ZooKeeper zkh
PREINIT:
zk_handle_t *handle;
HV *attr_hash;
int ret = ZBADARGUMENTS;
PPCODE:
handle = _zk_check_handle_outer(aTHX_ zkh, &attr_hash,
PACKAGE_NAME, PACKAGE_SIGNATURE);
if (!handle) {
handle = _zk_check_handle_inner(aTHX_ zkh, PACKAGE_SIGNATURE);
if (handle) {
attr_hash = zkh;
zkh = NULL;
}
}
if (handle) {
zk_t *zk = handle->handle.zk;
ret = zookeeper_close(zk->handle);
/* detach all now-inactive watches still tied to handles */
_zk_release_watches(aTHX_ zk->first_watch, 1);
Safefree(zk->first_watch);
Safefree(zk->hosts);
Safefree(zk);
Safefree(handle);
sv_unmagic((SV*) attr_hash, PERL_MAGIC_ext);
}
if (zkh && attr_hash) {
sv_unmagic((SV*) zkh, PERL_MAGIC_tied);
}
if (GIMME_V == G_VOID) {
XSRETURN_EMPTY;
}
else if (ret == ZOK) {
XSRETURN_YES;
}
else {
XSRETURN_NO;
}
void
zk_CLONE(package)
char *package
PPCODE:
XSRETURN_EMPTY;
void
zk_CLONE_SKIP(package)
char *package
PPCODE:
XSRETURN_YES;
void
zk_TIEHASH(package, ...)
char *package
PPCODE:
Perl_croak(aTHX_ "tying hashes of class "
PACKAGE_NAME " not supported");
void
zk_UNTIE(attr_hash, ref_count)
Net::ZooKeeper attr_hash
IV ref_count
PPCODE:
Perl_croak(aTHX_ "untying hashes of class "
PACKAGE_NAME " not supported");
void
zk_FIRSTKEY(attr_hash)
Net::ZooKeeper attr_hash
PREINIT:
zk_t *zk;
PPCODE:
zk = _zk_get_handle_inner(aTHX_ attr_hash);
if (!zk) {
Perl_croak(aTHX_ "invalid handle");
}
ST(0) = sv_2mortal(newSVpvn(zk_keys[0].name, zk_keys[0].name_len));
XSRETURN(1);
void
zk_NEXTKEY(attr_hash, attr_key)
Net::ZooKeeper attr_hash
SV *attr_key
PREINIT:
zk_t *zk;
char *key;
int i;
PPCODE:
zk = _zk_get_handle_inner(aTHX_ attr_hash);
if (!zk) {
Perl_croak(aTHX_ "invalid handle");
}
key = SvPV_nolen(attr_key);
for (i = 0; i < NUM_KEYS; ++i) {
if (strcaseEQ(key, zk_keys[i].name)) {