Sha256: b5fbca33c9e18d834e0e803dcc27934ecdcac0d151d9e36c5803c8baaa9357d2

Contents?: true

Size: 1.59 KB

Versions: 38

Compression:

Stored size: 1.59 KB

Contents

#!/usr/bin/env ruby

require 'zk'
require 'logger'

$stderr.sync = true

ZK.logger = log = Logger.new('informal.log').tap { |l| l.level = Logger::DEBUG }
Zookeeper.logger = log
Zookeeper.set_debug_level(4)

class ::Exception
  def to_std_format
    "#{self.class}: #{message}\n" + backtrace {|n| "\t#{n}"}.join("\n")
  end
end

def safe_join(th, timeout=nil)
  begin
    th.join(timeout)
  rescue Exception => e
    $stderr.puts "#{th[:name]} raised #{e.to_std_format}"
  end
end

def print_error
  yield
rescue Exception => e
  $stderr.puts "caught exception in #{Thread.current[:name]}: #{e.to_std_format}"
end

lock_name = 'the_big_sleep'

q = Queue.new


th1 = Thread.new do
  print_error do
    ZK.open do |zk|
      $stderr.puts "first connection session_id: 0x%x" % zk.session_id
      sub = zk.on_expired_session do |state|
        $stderr.puts "OH NOES! thread 1 got an expired session! #{state.inspect}"
      end

      zk.with_lock(lock_name) do
        q.push(:ok_sleeping)
        sleep # we now sleep with the fishes
      end
    end
  end
end

th1[:name] = 'thread 1'

q.pop

Thread.pass until th1.status == 'sleep'

$stderr.puts "ok, now try to acquire lock"

th2 = Thread.new do
  print_error do
    ZK.open do |zk|
      $stderr.puts "second connection session_id: 0x%x" % zk.session_id

      sub = zk.on_expired_session do |state|
        $stderr.puts "OH NOES! thread 2 got an expired session! #{state.inspect}"
      end

      zk.with_lock(lock_name) do
        $stderr.puts "acquired the lock in second thread"
      end
    end
  end
end

th2[:name] = 'thread 2'

[th1, th2].each(&method(:safe_join))

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
zk-1.10.0 spec/informal/lock_with_dead_session.rb
zk-1.9.6 spec/informal/lock_with_dead_session.rb
zk-1.9.5 spec/informal/lock_with_dead_session.rb
zk-1.9.4 spec/informal/lock_with_dead_session.rb
zk-1.9.3 spec/informal/lock_with_dead_session.rb
zk-1.9.2 spec/informal/lock_with_dead_session.rb
zk-1.9.1 spec/informal/lock_with_dead_session.rb
zk-1.9.0 spec/informal/lock_with_dead_session.rb
zk-1.8.0 spec/informal/lock_with_dead_session.rb
zk-1.7.5 spec/informal/lock_with_dead_session.rb
zk-1.7.4 spec/informal/lock_with_dead_session.rb
zk-1.7.3 spec/informal/lock_with_dead_session.rb
zk-1.7.2 spec/informal/lock_with_dead_session.rb
zk-1.7.1 spec/informal/lock_with_dead_session.rb
zk-1.7.0 spec/informal/lock_with_dead_session.rb
zk-1.6.5 spec/informal/lock_with_dead_session.rb
zk-1.6.4 spec/informal/lock_with_dead_session.rb
zk-1.6.3 spec/informal/lock_with_dead_session.rb
zk-1.6.2 spec/informal/lock_with_dead_session.rb
zk-1.6.1 spec/informal/lock_with_dead_session.rb