Permalink
Browse files

remove all the fork hook stuff, keep the global mutex for now

  • Loading branch information...
1 parent e69ef12 commit 9643ee5bddb545d56928ffc32918b7c7246ba658 @slyphon slyphon committed May 10, 2012
Showing with 9 additions and 134 deletions.
  1. +7 −8 ext/c_zookeeper.rb
  2. +2 −13 ext/zookeeper_base.rb
  3. +0 −113 lib/zookeeper/forked.rb
View
@@ -53,6 +53,13 @@ def initialize(host, event_queue, opts={})
@_session_timeout_msec = DEFAULT_SESSION_TIMEOUT_MSEC
+ if cid = opts[:client_id]
+ raise ArgumentError, "opts[:client_id] must be a CZookeeper::ClientId" unless cid.kind_of?(ClientId)
+ raise ArgumentError, "session_id must not be nil" if cid.session_id.nil?
+ end
+
+ @_client_id = opts[:client_id]
+
@start_stop_mutex = Monitor.new
# used to signal that we're running
@@ -63,12 +70,6 @@ def initialize(host, event_queue, opts={})
@event_thread = nil
- @subscriptions = [
- Zookeeper.prepare_for_fork { @start_stop_mutex.mon_enter },
- Zookeeper.after_fork_in_parent { @start_stop_mutex.mon_exit },
- Zookeeper.after_fork_in_child { @start_stop_mutex.mon_exit },
- ]
-
setup_event_thread!
zkrb_init(@host)
@@ -118,8 +119,6 @@ def close
@start_stop_mutex.synchronize(&fn_close)
end
- @subscriptions.each(&:unregister)
-
nil
end
View
@@ -89,15 +89,7 @@ def reopen_after_fork!
end
private :reopen_after_fork!
- def after_fork_hook
- logger.debug { "after_fork_hook called, @after_fork_sub: #{@after_fork_sub}" }
- afs, @after_fork_sub = @after_fork_sub, nil
- afs.unregister if afs
-
- @czk.close if @czk
- end
-
def reopen(timeout = 10, watcher=nil)
if watcher and (watcher != @default_watcher)
raise "You cannot set the watcher to a different value this way anymore!"
@@ -106,10 +98,7 @@ def reopen(timeout = 10, watcher=nil)
reopen_after_fork! if forked?
@mutex.synchronize do
- after_fork_hook
-
- @after_fork_sub = Zookeeper.after_fork_in_child(&method(:after_fork_hook))
-
+ @czk.close if @czk
@czk = CZookeeper.new(@host, @event_queue)
# flushes all outstanding watcher reqs.
@@ -138,7 +127,7 @@ def initialize(host, timeout = 10, watcher=nil)
# argument ends with '/'
raise ArgumentError, "Host argument #{host.inspect} may not end with /" if host.end_with?('/')
- @host = host
+ @host = host.dup
@default_watcher = (watcher or get_default_global_watcher)
@@ -1,67 +1,5 @@
module Zookeeper
module Forked
- @hooks = {
- :prepare => [],
- :after_child => [],
- :after_parent => []
- } unless @hooks
-
- class Subscription
- def initialize(hook_type, block)
- @hook_type = hook_type
- @block = block
- end
-
- def unregister
- Forked.unregister(@hook_type, @block)
- end
- alias unsubscribe unregister
-
- def call
- @block.call
- end
- end
-
- class << self
- attr_reader :hooks
-
- def fire_prepare_hooks!
- @hooks[:prepare].each(&:call)
- end
-
- def fire_after_child_hooks!
- @hooks[:after_child].each(&:call)
- end
-
- def fire_after_parent_hooks!
- @hooks[:after_parent].each(&:call)
- end
-
- # called when a block should be removed from the hooks
- def unregister(hook_type, block)
- @hooks.fetch(hook_type).delete(block)
- end
-
- # used by tests
- def clear!
- @hooks.values(&:clear)
- end
-
- def register(hook_type, block)
- unless hooks.has_key?(hook_type)
- raise "Invalid hook type specified: #{hook.inspect}"
- end
-
- unless block.respond_to?(:call)
- raise ArgumentError, "You must provide either a callable an argument or a block"
- end
-
- Subscription.new(hook_type, block).tap do |sub|
- @hooks[hook_type] << sub
- end
- end
- end
-
# the includer provides an 'original_pid' method, which is set
# when the original 'owning' process creates the object.
#
@@ -77,56 +15,5 @@ def update_pid!
self.original_pid = Process.pid
end
end # Forked
-
- class << self
- def prepare_for_fork(callable=nil, &blk)
- Forked.register(:prepare, callable||blk)
- end
-
- def after_fork_in_parent(callable=nil, &blk)
- Forked.register(:after_parent, callable||blk)
- end
-
- # called in the *child* process after a fork
- # returns a Subscription that may be used to unregister a hook
- def after_fork_in_child(callable=nil, &blk)
- Forked.register(:after_child, callable||blk)
- end
- end
end # Zookeeper
-module ::Kernel
- def fork_with_zookeeper_hooks(&block)
- parent_pid = Process.pid
-
- if block
- new_block = proc do
- ::Zookeeper::Forked.fire_after_child_hooks!
- block.call
- end
-
- ::Zookeeper::Forked.fire_prepare_hooks!
- fork_without_zookeeper_hooks(&new_block).tap do
- ::Zookeeper::Forked.fire_after_parent_hooks!
- end
- else
- ::Zookeeper::Forked.fire_prepare_hooks!
- if pid = fork_without_zookeeper_hooks
- ::Zookeeper::Forked.fire_after_parent_hooks!
- # we're in the parent
- return pid
- else
- # we're in the child
- ::Zookeeper::Forked.fire_after_child_hooks!
- return nil
- end
- end
- end
-
- # prevent infinite recursion if we get reloaded
- unless method_defined?(:fork_without_zookeeper_hooks)
- alias_method :fork_without_zookeeper_hooks, :fork
- alias_method :fork, :fork_with_zookeeper_hooks
- end
-end
-

0 comments on commit 9643ee5

Please sign in to comment.