slyphon-zookeeper 0.2.5 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/ext/dbg.h ADDED
@@ -0,0 +1,37 @@
1
+ #ifndef __dbg_h__
2
+ #define __dbg_h__
3
+
4
+ // ALL GLORY TO THE Zed A. Shaw
5
+ // http://c.learncodethehardway.org/book/learn-c-the-hard-waych21.html#x26-10500021
6
+
7
+ #include <stdio.h>
8
+ #include <errno.h>
9
+ #include <string.h>
10
+
11
+ #ifdef NDEBUG
12
+ #define debug(M, ...)
13
+ #else
14
+ #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
15
+ #endif
16
+
17
+ #define clean_errno() (errno == 0 ? "None" : strerror(errno))
18
+
19
+ #define log_err(M, ...) fprintf(stderr, "[ERROR] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
20
+
21
+ #define log_warn(M, ...) fprintf(stderr, "[WARN] (%s:%d: errno: %s) " M "\n", __FILE__, __LINE__, clean_errno(), ##__VA_ARGS__)
22
+
23
+ #define log_info(M, ...) fprintf(stderr, "[INFO] (%s:%d) " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
24
+
25
+ #define check(A, M, ...) if(!(A)) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
26
+
27
+ #define sentinel(M, ...) { log_err(M, ##__VA_ARGS__); errno=0; goto error; }
28
+
29
+ #define check_mem(A) check((A), "Out of memory.")
30
+
31
+ #define check_debug(A, M, ...) if(!(A)) { debug(M, ##__VA_ARGS__); errno=0; goto error; }
32
+
33
+ #define zkrb_debug(M, ...) if (ZKRBDebugging) fprintf(stderr, "DEBUG %s:%d: " M "\n", __FILE__, __LINE__, ##__VA_ARGS__)
34
+ #define zkrb_debug_inst(O, M, ...) zkrb_debug("obj_id: %lx, " M, FIX2LONG(rb_obj_id(O)), ##__VA_ARGS__)
35
+
36
+ #endif
37
+
data/ext/depend ADDED
@@ -0,0 +1,4 @@
1
+ zookeeper_c.c: zookeeper_lib.c dbg.h
2
+
3
+ zookeeper_lib.c: zookeeper_lib.h
4
+
data/ext/extconf.rb CHANGED
@@ -11,12 +11,14 @@ $LDFLAGS = "#{RbConfig::CONFIG['LDFLAGS']} #{$LDFLAGS}".gsub("$(ldflags)", "").g
11
11
  $CXXFLAGS = " -std=gnu++98 #{$CFLAGS}"
12
12
  $CPPFLAGS = $ARCH_FLAG = $DLDFLAGS = ""
13
13
 
14
+ ZK_DEBUG = (ENV['DEBUG'] or ARGV.any? { |arg| arg == '--debug' })
15
+ DEBUG_CFLAGS = " -O0 -ggdb3 -DHAVE_DEBUG"
14
16
 
15
- if ENV['DEBUG']
17
+ if ZK_DEBUG
16
18
  $stderr.puts "*** Setting debug flags. ***"
17
- $CFLAGS << " -O0 -ggdb3 -DHAVE_DEBUG"
18
19
  $EXTRA_CONF = " --enable-debug"
19
20
  $CFLAGS.gsub!(/ -O[^0] /, ' ')
21
+ $CFLAGS << DEBUG_CFLAGS
20
22
  end
21
23
 
22
24
  $includes = " -I#{HERE}/include"
@@ -46,7 +48,11 @@ Dir.chdir(HERE) do
46
48
  end
47
49
 
48
50
  Dir.chdir(BUNDLE_PATH) do
49
- safe_sh("./configure --prefix=#{HERE} --with-pic --without-cppunit --disable-dependency-tracking #{$EXTRA_CONF} 2>&1")
51
+ configure = "./configure --prefix=#{HERE} --with-pic --without-cppunit --disable-dependency-tracking #{$EXTRA_CONF} 2>&1"
52
+
53
+ configure = "env CFLAGS='#{DEBUG_CFLAGS}' #{configure}" if ZK_DEBUG
54
+
55
+ safe_sh(configure)
50
56
  safe_sh("make 2>&1")
51
57
  safe_sh("make install 2>&1")
52
58
  end
@@ -62,4 +68,6 @@ Dir.chdir("#{HERE}/lib") do
62
68
  end
63
69
  $LIBS << " -lzookeeper_mt_gem"
64
70
 
71
+
65
72
  create_makefile 'zookeeper_c'
73
+
@@ -28,6 +28,7 @@ class ZookeeperBase < CZookeeper
28
28
  end
29
29
 
30
30
  @start_stop_mutex.synchronize do
31
+ # $stderr.puts "%s: calling init, self.obj_id: %x" % [self.class, object_id]
31
32
  init(@host)
32
33
 
33
34
  if timeout > 0
data/ext/zookeeper_c.c CHANGED
@@ -22,7 +22,7 @@
22
22
  #include <inttypes.h>
23
23
 
24
24
  #include "zookeeper_lib.h"
25
-
25
+ #include "dbg.h"
26
26
 
27
27
  static VALUE Zookeeper = Qnil;
28
28
 
@@ -32,6 +32,7 @@ struct zkrb_instance_data {
32
32
  zhandle_t *zh;
33
33
  clientid_t myid;
34
34
  zkrb_queue_t *queue;
35
+ long object_id; // the ruby object this instance data is associated with
35
36
  };
36
37
 
37
38
  typedef enum {
@@ -44,23 +45,28 @@ typedef enum {
44
45
  #define IS_SYNC(zkrbcall) ((zkrbcall)==SYNC || (zkrbcall)==SYNC_WATCH)
45
46
  #define IS_ASYNC(zkrbcall) ((zkrbcall)==ASYNC || (zkrbcall)==ASYNC_WATCH)
46
47
 
48
+
47
49
  static int destroy_zkrb_instance(struct zkrb_instance_data* ptr) {
48
50
  int rv = ZOK;
49
51
 
50
52
  if (ptr->zh) {
51
53
  const void *ctx = zoo_get_context(ptr->zh);
52
54
  /* Note that after zookeeper_close() returns, ZK handle is invalid */
53
- if (ZKRBDebugging) fprintf(stderr, "calling zookeeper_close\n");
55
+ zkrb_debug("obj_id: %lx, calling zookeeper_close", ptr->object_id);
54
56
  rv = zookeeper_close(ptr->zh);
55
- if (ZKRBDebugging) fprintf(stderr, "zookeeper_close returned %d\n", rv);
57
+ zkrb_debug("obj_id: %lx, zookeeper_close returned %d", ptr->object_id, rv);
56
58
  free((void *) ctx);
57
59
  }
58
60
 
59
61
  #warning [wickman] TODO: fire off warning if queue is not empty
60
- if (ptr->queue) zkrb_queue_free(ptr->queue);
62
+ if (ptr->queue) {
63
+ zkrb_debug("obj_id: %lx, freeing queue pointer: %p", ptr->object_id, ptr->queue);
64
+ zkrb_queue_free(ptr->queue);
65
+ }
61
66
 
62
67
  ptr->zh = NULL;
63
68
  ptr->queue = NULL;
69
+
64
70
  return rv;
65
71
  }
66
72
 
@@ -70,10 +76,11 @@ static void free_zkrb_instance_data(struct zkrb_instance_data* ptr) {
70
76
 
71
77
  static void print_zkrb_instance_data(struct zkrb_instance_data* ptr) {
72
78
  fprintf(stderr, "zkrb_instance_data (%p) {\n", ptr);
73
- fprintf(stderr, " zh = %p\n", ptr->zh);
74
- fprintf(stderr, " { state = %d }\n", zoo_state(ptr->zh));
75
- fprintf(stderr, " id = %"PRId64"\n", ptr->myid.client_id); // PRId64 defined in inttypes.h
76
- fprintf(stderr, " q = %p\n", ptr->queue);
79
+ fprintf(stderr, " zh = %p\n", ptr->zh);
80
+ fprintf(stderr, " { state = %d }\n", zoo_state(ptr->zh));
81
+ fprintf(stderr, " id = %"PRId64"\n", ptr->myid.client_id); // PRId64 defined in inttypes.h
82
+ fprintf(stderr, " q = %p\n", ptr->queue);
83
+ fprintf(stderr, " obj_id = %lx\n", ptr->object_id);
77
84
  fprintf(stderr, "}\n");
78
85
  }
79
86
 
@@ -154,6 +161,8 @@ static VALUE method_init(int argc, VALUE* argv, VALUE self) {
154
161
  zkrb_calling_context *ctx =
155
162
  zkrb_calling_context_alloc(ZKRB_GLOBAL_REQ, zk_local_ctx->queue);
156
163
 
164
+ zk_local_ctx->object_id = FIX2LONG(rb_obj_id(self));
165
+
157
166
  zk_local_ctx->zh =
158
167
  zookeeper_init(
159
168
  RSTRING_PTR(hostPort),
@@ -168,7 +177,7 @@ static VALUE method_init(int argc, VALUE* argv, VALUE self) {
168
177
  rb_raise(rb_eRuntimeError, "error connecting to zookeeper: %d", errno);
169
178
  }
170
179
 
171
- rb_iv_set(self, "@data", data);
180
+ rb_iv_set(self, "@_data", data);
172
181
  rb_iv_set(self, "@selectable_io", create_selectable_io(zk_local_ctx->queue));
173
182
  rb_iv_set(self, "@_running", Qtrue);
174
183
 
@@ -178,7 +187,7 @@ static VALUE method_init(int argc, VALUE* argv, VALUE self) {
178
187
 
179
188
  #define FETCH_DATA_PTR(x, y) \
180
189
  struct zkrb_instance_data * y; \
181
- Data_Get_Struct(rb_iv_get(x, "@data"), struct zkrb_instance_data, y); \
190
+ Data_Get_Struct(rb_iv_get(x, "@_data"), struct zkrb_instance_data, y); \
182
191
  if ((y)->zh == NULL) \
183
192
  rb_raise(rb_eRuntimeError, "zookeeper handle is closed")
184
193
 
@@ -189,7 +198,7 @@ static VALUE method_init(int argc, VALUE* argv, VALUE self) {
189
198
  } \
190
199
  Check_Type(path, T_STRING); \
191
200
  struct zkrb_instance_data * zk; \
192
- Data_Get_Struct(rb_iv_get(self, "@data"), struct zkrb_instance_data, zk); \
201
+ Data_Get_Struct(rb_iv_get(self, "@_data"), struct zkrb_instance_data, zk); \
193
202
  if (!zk->zh) \
194
203
  rb_raise(rb_eRuntimeError, "zookeeper handle is closed"); \
195
204
  zkrb_calling_context* cb_ctx = \
@@ -489,7 +498,7 @@ static VALUE method_get_next_event(VALUE self, VALUE blocking) {
489
498
  // ruby-land semaphore that we can also use in the java extension
490
499
  //
491
500
  if (is_closed(self) || !is_running(self)) {
492
- /* fprintf(stderr, "method_get_next_event: running is false, returning nil\n");*/
501
+ zkrb_debug_inst(self, "is_closed(self): %d, is_running(self): %d, method_get_next_event is exiting loop", is_closed(self), is_running(self));
493
502
  return Qnil; // this case for shutdown
494
503
  }
495
504
 
@@ -512,9 +521,8 @@ static VALUE method_get_next_event(VALUE self, VALUE blocking) {
512
521
  if (bytes_read == -1) {
513
522
  rb_raise(rb_eRuntimeError, "read failed: %d", errno);
514
523
  }
515
- else if (ZKRBDebugging) {
516
- fprintf(stderr, "read %zd bytes from the queue (%p)'s pipe\n", bytes_read, zk->queue);
517
- }
524
+
525
+ zkrb_debug_inst(self, "read %zd bytes from the queue (%p)'s pipe", bytes_read, zk->queue);
518
526
 
519
527
  continue;
520
528
  }
@@ -545,6 +553,7 @@ static VALUE method_client_id(VALUE self) {
545
553
  static VALUE method_wake_event_loop_bang(VALUE self) {
546
554
  FETCH_DATA_PTR(self, zk);
547
555
 
556
+ zkrb_debug_inst(self, "Waking event loop: %p", zk->queue);
548
557
  zkrb_signal(zk->queue);
549
558
 
550
559
  return Qnil;
@@ -563,7 +572,8 @@ static VALUE method_close_handle(VALUE self) {
563
572
  FETCH_DATA_PTR(self, zk);
564
573
 
565
574
  if (ZKRBDebugging) {
566
- fprintf(stderr, "CLOSING ZK INSTANCE:");
575
+ /* fprintf(stderr, "CLOSING ZK INSTANCE: obj_id %lx", FIX2LONG(rb_obj_id(self)));*/
576
+ zkrb_debug_inst(self, "CLOSING_ZK_INSTANCE");
567
577
  print_zkrb_instance_data(zk);
568
578
  }
569
579
 
@@ -571,14 +581,15 @@ static VALUE method_close_handle(VALUE self) {
571
581
  // has been called
572
582
  rb_iv_set(self, "@_closed", Qtrue);
573
583
 
574
- if (ZKRBDebugging) fprintf(stderr, "calling destroy_zkrb_instance\n");
584
+ zkrb_debug_inst(self, "calling destroy_zkrb_instance");
575
585
 
576
586
  /* Note that after zookeeper_close() returns, ZK handle is invalid */
577
587
  int rc = destroy_zkrb_instance(zk);
578
588
 
579
- if (ZKRBDebugging) fprintf(stderr, "destroy_zkrb_instance returned: %d\n", rc);
589
+ zkrb_debug("destroy_zkrb_instance returned: %d", rc);
580
590
 
581
- return INT2FIX(rc);
591
+ return Qnil;
592
+ /* return INT2FIX(rc);*/
582
593
  }
583
594
 
584
595
  static VALUE method_deterministic_conn_order(VALUE self, VALUE yn) {
@@ -657,3 +668,5 @@ void Init_zookeeper_c() {
657
668
  Zookeeper = rb_define_class("CZookeeper", rb_cObject);
658
669
  zkrb_define_methods();
659
670
  }
671
+
672
+ // vim:ts=2:sw=2:sts=2:et
data/ext/zookeeper_lib.c CHANGED
@@ -17,6 +17,7 @@ wickman@twitter.com
17
17
  #include <pthread.h>
18
18
  #include <unistd.h>
19
19
  #include <inttypes.h>
20
+ #include "dbg.h"
20
21
 
21
22
  #define GET_SYM(str) ID2SYM(rb_intern(str))
22
23
 
@@ -29,7 +30,7 @@ pthread_mutex_t zkrb_q_mutex = PTHREAD_MUTEX_INITIALIZER;
29
30
  *
30
31
  * NOTE: be *very careful* in these functions, calling *ANY* ruby interpreter
31
32
  * function when you're not in an interpreter thread can hork ruby, trigger a
32
- * [BUG], corrupt the stack, kill you dog, knock up your daughter, etc. etc.
33
+ * [BUG], corrupt the stack, kill your dog, knock up your daughter, etc. etc.
33
34
  *
34
35
  *********************************************************************************
35
36
  */
@@ -53,10 +54,8 @@ void zkrb_enqueue(zkrb_queue_t *q, zkrb_event_t *elt) {
53
54
  // this from a ruby thread. Calling into the interpreter from a non-ruby
54
55
  // thread is bad, mm'kay?
55
56
 
56
- if (ZKRBDebugging) {
57
- if ((ret == -1)) {
58
- fprintf(stderr, "WARNING: write to queue (%p) pipe failed!\n", q);
59
- }
57
+ if ((ret == -1)) {
58
+ fprintf(stderr, "WARNING: write to queue (%p) pipe failed!\n", q);
60
59
  }
61
60
  }
62
61
 
@@ -93,7 +92,7 @@ void zkrb_signal(zkrb_queue_t *q) {
93
92
  ssize_t ret = write(q->pipe_write, "0", 1); /* Wake up Ruby listener */
94
93
  pthread_mutex_unlock(&zkrb_q_mutex);
95
94
  if (ret == -1)
96
- rb_raise(rb_eRuntimeError, "write to pipe failed: %d", errno);
95
+ log_err("write to pipe failed");
97
96
  }
98
97
 
99
98
  zkrb_queue_t *zkrb_queue_alloc(void) {
@@ -11,6 +11,7 @@ module ZookeeperEM
11
11
  @on_close = EM::DefaultDeferrable.new
12
12
  @on_attached = EM::DefaultDeferrable.new
13
13
  @em_connection = nil
14
+ logger.debug { "ZookeeperEM::Client obj_id %x: init" % [object_id] }
14
15
  super(*a, &b)
15
16
  end
16
17
 
@@ -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.5'
6
+ s.version = '0.2.7'
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"]
metadata CHANGED
@@ -1,10 +1,15 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: slyphon-zookeeper
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.5
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 7
10
+ version: 0.2.7
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Phillip Pearson
9
14
  - Eric Maland
10
15
  - Evan Weaver
@@ -14,60 +19,85 @@ authors:
14
19
  autorequire:
15
20
  bindir: bin
16
21
  cert_chain: []
17
- date: 2011-11-14 00:00:00.000000000 Z
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
22
+
23
+ date: 2011-11-17 00:00:00 Z
24
+ dependencies:
25
+ - !ruby/object:Gem::Dependency
20
26
  name: rspec
21
- requirement: &70134739892840 !ruby/object:Gem::Requirement
27
+ prerelease: false
28
+ requirement: &id001 !ruby/object:Gem::Requirement
22
29
  none: false
23
- requirements:
24
- - - ! '>='
25
- - !ruby/object:Gem::Version
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ hash: 15
34
+ segments:
35
+ - 2
36
+ - 0
37
+ - 0
26
38
  version: 2.0.0
27
39
  type: :development
28
- prerelease: false
29
- version_requirements: *70134739892840
30
- - !ruby/object:Gem::Dependency
40
+ version_requirements: *id001
41
+ - !ruby/object:Gem::Dependency
31
42
  name: flexmock
32
- requirement: &70134739892020 !ruby/object:Gem::Requirement
43
+ prerelease: false
44
+ requirement: &id002 !ruby/object:Gem::Requirement
33
45
  none: false
34
- requirements:
46
+ requirements:
35
47
  - - ~>
36
- - !ruby/object:Gem::Version
48
+ - !ruby/object:Gem::Version
49
+ hash: 41
50
+ segments:
51
+ - 0
52
+ - 8
53
+ - 11
37
54
  version: 0.8.11
38
55
  type: :development
39
- prerelease: false
40
- version_requirements: *70134739892020
41
- - !ruby/object:Gem::Dependency
56
+ version_requirements: *id002
57
+ - !ruby/object:Gem::Dependency
42
58
  name: eventmachine
43
- requirement: &70134739891420 !ruby/object:Gem::Requirement
59
+ prerelease: false
60
+ requirement: &id003 !ruby/object:Gem::Requirement
44
61
  none: false
45
- requirements:
46
- - - =
47
- - !ruby/object:Gem::Version
62
+ requirements:
63
+ - - "="
64
+ - !ruby/object:Gem::Version
65
+ hash: 62196363
66
+ segments:
67
+ - 1
68
+ - 0
69
+ - 0
70
+ - beta
71
+ - 4
48
72
  version: 1.0.0.beta.4
49
73
  type: :development
50
- prerelease: false
51
- version_requirements: *70134739891420
52
- - !ruby/object:Gem::Dependency
74
+ version_requirements: *id003
75
+ - !ruby/object:Gem::Dependency
53
76
  name: evented-spec
54
- requirement: &70134739890760 !ruby/object:Gem::Requirement
77
+ prerelease: false
78
+ requirement: &id004 !ruby/object:Gem::Requirement
55
79
  none: false
56
- requirements:
80
+ requirements:
57
81
  - - ~>
58
- - !ruby/object:Gem::Version
82
+ - !ruby/object:Gem::Version
83
+ hash: 59
84
+ segments:
85
+ - 0
86
+ - 9
87
+ - 0
59
88
  version: 0.9.0
60
89
  type: :development
61
- prerelease: false
62
- version_requirements: *70134739890760
90
+ version_requirements: *id004
63
91
  description: twitter's zookeeper client
64
- email:
92
+ email:
65
93
  - slyphon@gmail.com
66
94
  executables: []
67
- extensions:
95
+
96
+ extensions:
68
97
  - ext/extconf.rb
69
98
  extra_rdoc_files: []
70
- files:
99
+
100
+ files:
71
101
  - .gitignore
72
102
  - CHANGELOG
73
103
  - Gemfile
@@ -78,6 +108,8 @@ files:
78
108
  - examples/cloud_config.rb
79
109
  - ext/.gitignore
80
110
  - ext/Rakefile
111
+ - ext/dbg.h
112
+ - ext/depend
81
113
  - ext/extconf.rb
82
114
  - ext/zkc-3.3.3.tar.gz
83
115
  - ext/zookeeper_base.rb
@@ -108,30 +140,39 @@ files:
108
140
  - test/test_watcher2.rb
109
141
  homepage: https://github.com/slyphon/zookeeper
110
142
  licenses: []
143
+
111
144
  post_install_message:
112
145
  rdoc_options: []
113
- require_paths:
146
+
147
+ require_paths:
114
148
  - lib
115
149
  - ext
116
- required_ruby_version: !ruby/object:Gem::Requirement
150
+ required_ruby_version: !ruby/object:Gem::Requirement
117
151
  none: false
118
- requirements:
119
- - - ! '>='
120
- - !ruby/object:Gem::Version
121
- version: '0'
122
- required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ hash: 3
156
+ segments:
157
+ - 0
158
+ version: "0"
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
160
  none: false
124
- requirements:
125
- - - ! '>='
126
- - !ruby/object:Gem::Version
127
- version: '0'
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ hash: 3
165
+ segments:
166
+ - 0
167
+ version: "0"
128
168
  requirements: []
169
+
129
170
  rubyforge_project:
130
- rubygems_version: 1.8.10
171
+ rubygems_version: 1.8.6
131
172
  signing_key:
132
173
  specification_version: 3
133
174
  summary: twitter's zookeeper client
134
- test_files:
175
+ test_files:
135
176
  - spec/default_watcher_spec.rb
136
177
  - spec/em_spec.rb
137
178
  - spec/log4j.properties