slyphon-zookeeper 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/ext/zookeeper_c.c +8 -2
- data/ext/zookeeper_lib.c +13 -15
- data/lib/zookeeper/em_client.rb +61 -17
- data/slyphon-zookeeper.gemspec +1 -1
- data/spec/em_spec.rb +6 -32
- data/spec/spec_helper.rb +8 -0
- metadata +4 -4
data/ext/zookeeper_c.c
CHANGED
@@ -19,6 +19,7 @@
|
|
19
19
|
#include <unistd.h>
|
20
20
|
#include <sys/fcntl.h>
|
21
21
|
#include <pthread.h>
|
22
|
+
#include <inttypes.h>
|
22
23
|
|
23
24
|
#include "zookeeper_lib.h"
|
24
25
|
|
@@ -49,7 +50,9 @@ static int destroy_zkrb_instance(struct zkrb_instance_data* ptr) {
|
|
49
50
|
if (ptr->zh) {
|
50
51
|
const void *ctx = zoo_get_context(ptr->zh);
|
51
52
|
/* Note that after zookeeper_close() returns, ZK handle is invalid */
|
53
|
+
if (ZKRBDebugging) fprintf(stderr, "calling zookeeper_close\n");
|
52
54
|
rv = zookeeper_close(ptr->zh);
|
55
|
+
if (ZKRBDebugging) fprintf(stderr, "zookeeper_close returned %d\n", rv);
|
53
56
|
free((void *) ctx);
|
54
57
|
}
|
55
58
|
|
@@ -69,7 +72,7 @@ static void print_zkrb_instance_data(struct zkrb_instance_data* ptr) {
|
|
69
72
|
fprintf(stderr, "zkrb_instance_data (%p) {\n", ptr);
|
70
73
|
fprintf(stderr, " zh = %p\n", ptr->zh);
|
71
74
|
fprintf(stderr, " { state = %d }\n", zoo_state(ptr->zh));
|
72
|
-
fprintf(stderr, " id = %
|
75
|
+
fprintf(stderr, " id = %"PRId64"\n", ptr->myid.client_id); // PRId64 defined in inttypes.h
|
73
76
|
fprintf(stderr, " q = %p\n", ptr->queue);
|
74
77
|
fprintf(stderr, "}\n");
|
75
78
|
}
|
@@ -521,7 +524,7 @@ static VALUE method_get_next_event(VALUE self, VALUE blocking) {
|
|
521
524
|
rb_raise(rb_eRuntimeError, "read failed: %d", errno);
|
522
525
|
}
|
523
526
|
else if (ZKRBDebugging) {
|
524
|
-
fprintf(stderr, "read %
|
527
|
+
fprintf(stderr, "read %zd bytes from the queue (%p)'s pipe\n", bytes_read, zk->queue);
|
525
528
|
}
|
526
529
|
|
527
530
|
continue;
|
@@ -579,10 +582,13 @@ static VALUE method_close_handle(VALUE self) {
|
|
579
582
|
// has been called
|
580
583
|
rb_iv_set(self, "@_closed", Qtrue);
|
581
584
|
|
585
|
+
if (ZKRBDebugging) fprintf(stderr, "calling destroy_zkrb_instance\n");
|
582
586
|
|
583
587
|
/* Note that after zookeeper_close() returns, ZK handle is invalid */
|
584
588
|
int rc = destroy_zkrb_instance(zk);
|
585
589
|
|
590
|
+
if (ZKRBDebugging) fprintf(stderr, "destroy_zkrb_instance returned: %d\n", rc);
|
591
|
+
|
586
592
|
return INT2FIX(rc);
|
587
593
|
}
|
588
594
|
|
data/ext/zookeeper_lib.c
CHANGED
@@ -16,6 +16,7 @@ wickman@twitter.com
|
|
16
16
|
#include <stdlib.h>
|
17
17
|
#include <pthread.h>
|
18
18
|
#include <unistd.h>
|
19
|
+
#include <inttypes.h>
|
19
20
|
|
20
21
|
#define GET_SYM(str) ID2SYM(rb_intern(str))
|
21
22
|
|
@@ -56,9 +57,6 @@ void zkrb_enqueue(zkrb_queue_t *q, zkrb_event_t *elt) {
|
|
56
57
|
if ((ret == -1)) {
|
57
58
|
fprintf(stderr, "WARNING: write to queue (%p) pipe failed!\n", q);
|
58
59
|
}
|
59
|
-
/* else {*/
|
60
|
-
/* fprintf(stderr, "successfully enqueued event on queue (%p)\n", q);*/
|
61
|
-
/* }*/
|
62
60
|
}
|
63
61
|
}
|
64
62
|
|
@@ -283,17 +281,17 @@ VALUE zkrb_event_to_ruby(zkrb_event_t *event) {
|
|
283
281
|
void zkrb_print_stat(const struct Stat *s) {
|
284
282
|
fprintf(stderr, "stat {\n");
|
285
283
|
if (s != NULL) {
|
286
|
-
fprintf(stderr, "\t czxid: %
|
287
|
-
fprintf(stderr, "\t mzxid: %
|
288
|
-
fprintf(stderr, "\t ctime: %
|
289
|
-
fprintf(stderr, "\t mtime: %
|
290
|
-
fprintf(stderr, "\t version: %d\n"
|
291
|
-
fprintf(stderr, "\t cversion: %d\n"
|
292
|
-
fprintf(stderr, "\t aversion: %d\n"
|
293
|
-
fprintf(stderr, "\t ephemeralOwner: %
|
294
|
-
fprintf(stderr, "\t dataLength: %d\n"
|
295
|
-
fprintf(stderr, "\t numChildren: %d\n"
|
296
|
-
fprintf(stderr, "\t pzxid: %
|
284
|
+
fprintf(stderr, "\t czxid: %"PRId64"\n", s->czxid); // PRId64 defined in inttypes.h
|
285
|
+
fprintf(stderr, "\t mzxid: %"PRId64"\n", s->mzxid);
|
286
|
+
fprintf(stderr, "\t ctime: %"PRId64"\n", s->ctime);
|
287
|
+
fprintf(stderr, "\t mtime: %"PRId64"\n", s->mtime);
|
288
|
+
fprintf(stderr, "\t version: %d\n", s->version);
|
289
|
+
fprintf(stderr, "\t cversion: %d\n", s->cversion);
|
290
|
+
fprintf(stderr, "\t aversion: %d\n", s->aversion);
|
291
|
+
fprintf(stderr, "\t ephemeralOwner: %"PRId64"\n", s->ephemeralOwner);
|
292
|
+
fprintf(stderr, "\t dataLength: %d\n", s->dataLength);
|
293
|
+
fprintf(stderr, "\t numChildren: %d\n", s->numChildren);
|
294
|
+
fprintf(stderr, "\t pzxid: %"PRId64"\n", s->pzxid);
|
297
295
|
} else {
|
298
296
|
fprintf(stderr, "\tNULL\n");
|
299
297
|
}
|
@@ -309,7 +307,7 @@ zkrb_calling_context *zkrb_calling_context_alloc(int64_t req_id, zkrb_queue_t *q
|
|
309
307
|
|
310
308
|
void zkrb_print_calling_context(zkrb_calling_context *ctx) {
|
311
309
|
fprintf(stderr, "calling context (%p){\n", ctx);
|
312
|
-
fprintf(stderr, "\treq_id = %
|
310
|
+
fprintf(stderr, "\treq_id = %"PRId64"\n", ctx->req_id);
|
313
311
|
fprintf(stderr, "\tqueue = %p\n", ctx->queue);
|
314
312
|
fprintf(stderr, "}\n");
|
315
313
|
}
|
data/lib/zookeeper/em_client.rb
CHANGED
@@ -37,29 +37,28 @@ module ZookeeperEM
|
|
37
37
|
def close(&block)
|
38
38
|
on_close(&block)
|
39
39
|
|
40
|
-
|
40
|
+
logger.debug { "#{self.class.name}: close called, closed? #{closed?} running? #{running?}" }
|
41
41
|
|
42
42
|
if @_running
|
43
43
|
@start_stop_mutex.synchronize do
|
44
44
|
@_running = false
|
45
45
|
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
close_handle
|
47
|
+
if @em_connection
|
48
|
+
EM.next_tick do
|
49
|
+
@em_connection.detach do
|
50
|
+
logger.debug { "#{self.class.name}: connection unbound, continuing with shutdown" }
|
51
|
+
finish_closing
|
52
|
+
end
|
54
53
|
end
|
55
|
-
|
56
|
-
|
54
|
+
else
|
55
|
+
logger.debug { "#{self.class.name}: em_connection was never set up, finish closing" }
|
56
|
+
finish_closing
|
57
57
|
end
|
58
58
|
else
|
59
|
-
logger.debug { "we are not running, so returning on_close deferred" }
|
59
|
+
logger.debug { "#{self.class.name}: we are not running, so returning on_close deferred" }
|
60
60
|
end
|
61
61
|
|
62
|
-
on_close.succeed
|
63
62
|
on_close
|
64
63
|
end
|
65
64
|
|
@@ -73,14 +72,30 @@ module ZookeeperEM
|
|
73
72
|
EM.schedule do
|
74
73
|
if running? and not closed?
|
75
74
|
begin
|
75
|
+
logger.debug { "adding EM.watch(#{selectable_io.inspect})" }
|
76
76
|
@em_connection = EM.watch(selectable_io, ZKConnection, self) { |cnx| cnx.notify_readable = true }
|
77
77
|
rescue Exception => e
|
78
78
|
$stderr.puts "caught exception from EM.watch(): #{e.inspect}"
|
79
79
|
end
|
80
|
-
on_attached.succeed
|
81
80
|
end
|
82
81
|
end
|
83
82
|
end
|
83
|
+
|
84
|
+
def finish_closing
|
85
|
+
unless @_closed
|
86
|
+
@start_stop_mutex.synchronize do
|
87
|
+
logger.debug { "closing handle" }
|
88
|
+
close_handle
|
89
|
+
end
|
90
|
+
|
91
|
+
unless selectable_io.closed?
|
92
|
+
logger.debug { "calling close on selectable_io: #{selectable_io.inspect}" }
|
93
|
+
selectable_io.close
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
on_close.succeed
|
98
|
+
end
|
84
99
|
end
|
85
100
|
|
86
101
|
# this class is handed to EventMachine.watch to handle event dispatching
|
@@ -94,24 +109,53 @@ module ZookeeperEM
|
|
94
109
|
|
95
110
|
def initialize(zk_client)
|
96
111
|
@zk_client = zk_client
|
112
|
+
end
|
113
|
+
|
114
|
+
def post_init
|
115
|
+
logger.debug { "post_init called" }
|
97
116
|
@attached = true
|
117
|
+
|
118
|
+
@on_unbind = EM::DefaultDeferrable.new.tap do |d|
|
119
|
+
d.callback do
|
120
|
+
logger.debug { "on_unbind deferred fired" }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# probably because of the way post_init works, unless we fire this
|
125
|
+
# callback in next_tick @em_connection in the client may not be set
|
126
|
+
# (which on_attached callbacks may be relying on)
|
127
|
+
EM.next_tick do
|
128
|
+
logger.debug { "firing on_attached callback" }
|
129
|
+
@zk_client.on_attached.succeed
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# EM::DefaultDeferrable that will be called back when our em_connection has been detached
|
134
|
+
# and we've completed the close operation
|
135
|
+
def on_unbind(&block)
|
136
|
+
@on_unbind.callback(&block) if block
|
137
|
+
@on_unbind
|
98
138
|
end
|
99
139
|
|
100
140
|
def attached?
|
101
141
|
@attached
|
102
142
|
end
|
103
143
|
|
104
|
-
def
|
144
|
+
def unbind
|
145
|
+
on_unbind.succeed
|
146
|
+
end
|
147
|
+
|
148
|
+
def detach(&blk)
|
149
|
+
on_unbind(&blk)
|
105
150
|
return unless @attached
|
106
151
|
@attached = false
|
107
|
-
super
|
108
|
-
logger.debug { "#{self.class.name}: detached" }
|
152
|
+
rval = super()
|
153
|
+
logger.debug { "#{self.class.name}: detached, rval: #{rval.inspect}" }
|
109
154
|
end
|
110
155
|
|
111
156
|
# we have an event waiting
|
112
157
|
def notify_readable
|
113
158
|
if @zk_client.running?
|
114
|
-
# logger.debug { "#{self.class.name}: dispatching events while #{@zk_client.running?}" }
|
115
159
|
|
116
160
|
read_io_nb if @zk_client.dispatch_next_callback(false)
|
117
161
|
|
data/slyphon-zookeeper.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "slyphon-zookeeper"
|
6
|
-
s.version = '0.2.
|
6
|
+
s.version = '0.2.4'
|
7
7
|
|
8
8
|
s.authors = ["Phillip Pearson", "Eric Maland", "Evan Weaver", "Brian Wickman", "Neil Conway", "Jonathan D. Simms"]
|
9
9
|
s.email = ["slyphon@gmail.com"]
|
data/spec/em_spec.rb
CHANGED
@@ -20,7 +20,12 @@ describe 'ZookeeperEM' do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def teardown_and_done
|
23
|
-
@zk.close
|
23
|
+
@zk.close do
|
24
|
+
logger.debug { "TEST: about to call done" }
|
25
|
+
EM.next_tick do
|
26
|
+
done
|
27
|
+
end
|
28
|
+
end
|
24
29
|
end
|
25
30
|
|
26
31
|
describe 'selectable_io' do
|
@@ -55,37 +60,6 @@ describe 'ZookeeperEM' do
|
|
55
60
|
teardown_and_done
|
56
61
|
end
|
57
62
|
end
|
58
|
-
|
59
|
-
it %[should not be read-ready if there's no event] do
|
60
|
-
pending "get this to work in jruby" if defined?(::JRUBY_VERSION)
|
61
|
-
# there's always an initial event after connect
|
62
|
-
|
63
|
-
# except in jruby
|
64
|
-
# if defined?(::JRUBY_VERSION)
|
65
|
-
# @zk.get(:path => '/', :callback => @data_cb)
|
66
|
-
# end
|
67
|
-
|
68
|
-
setup_zk do
|
69
|
-
events = 0
|
70
|
-
|
71
|
-
while true
|
72
|
-
r, *_ = IO.select([@zk.selectable_io], [], [], 0.2)
|
73
|
-
|
74
|
-
break unless r
|
75
|
-
|
76
|
-
h = @zk.get_next_event(false)
|
77
|
-
@zk.selectable_io.read(1)
|
78
|
-
|
79
|
-
events += 1
|
80
|
-
|
81
|
-
h.should be_kind_of(Hash)
|
82
|
-
end
|
83
|
-
|
84
|
-
events.should == 1
|
85
|
-
|
86
|
-
teardown_and_done
|
87
|
-
end
|
88
|
-
end
|
89
63
|
end
|
90
64
|
|
91
65
|
describe 'em_connection' do
|
data/spec/spec_helper.rb
CHANGED
@@ -13,6 +13,14 @@ Zookeeper.logger = Logger.new(File.expand_path('../../test.log', __FILE__)).tap
|
|
13
13
|
log.level = Logger::DEBUG
|
14
14
|
end
|
15
15
|
|
16
|
+
|
17
|
+
# NOTE: this is a useful debugging setup. have our logs and the low-level C
|
18
|
+
# logging statements both go to stderr. to use, comment the above and uncomment
|
19
|
+
# below
|
20
|
+
|
21
|
+
# Zookeeper.logger = Logger.new($stderr).tap { |l| l.level = Logger::DEBUG }
|
22
|
+
# Zookeeper.set_debug_level(4)
|
23
|
+
|
16
24
|
def logger
|
17
25
|
Zookeeper.logger
|
18
26
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: slyphon-zookeeper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 4
|
10
|
+
version: 0.2.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Phillip Pearson
|
@@ -20,7 +20,7 @@ autorequire:
|
|
20
20
|
bindir: bin
|
21
21
|
cert_chain: []
|
22
22
|
|
23
|
-
date: 2011-06-
|
23
|
+
date: 2011-06-29 00:00:00 +00:00
|
24
24
|
default_executable:
|
25
25
|
dependencies:
|
26
26
|
- !ruby/object:Gem::Dependency
|