Class: ZK::Locker::LockerBase
- Inherits:
-
Object
- Object
- ZK::Locker::LockerBase
- Includes:
- ZK::Logging
- Defined in:
- lib/z_k/locker.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#lock_path ⇒ Object
readonly
our absolute lock node path.
-
#root_lock_path ⇒ Object
readonly
:nodoc:.
-
#zk ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.digit_from_lock_path(path) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #cleanup_lock_path! ⇒ Object protected
-
#create_lock_path!(prefix = 'lock') ⇒ Object
protected
prefix is the string that will appear in front of the sequence num, defaults to ‘lock’.
- #create_root_path! ⇒ Object protected
- #digit_from(path) ⇒ Object protected
- #in_waiting_status ⇒ Object protected
-
#initialize(zookeeper_client, name, root_lock_node = "/_zklocking") ⇒ LockerBase
constructor
A new instance of LockerBase.
-
#lock_basename ⇒ Object
the basename of our lock path.
- #lock_children(watch = false) ⇒ Object protected
- #locked? ⇒ Boolean
- #ordered_lock_children(watch = false) ⇒ Object protected
- #unlock! ⇒ Object
-
#waiting? ⇒ Boolean
returns true if this locker is waiting to acquire lock.
-
#with_lock ⇒ Object
block caller until lock is aquired, then yield.
Constructor Details
#initialize(zookeeper_client, name, root_lock_node = "/_zklocking") ⇒ LockerBase
Returns a new instance of LockerBase.
45 46 47 48 49 50 51 52 |
# File 'lib/z_k/locker.rb', line 45 def initialize(zookeeper_client, name, root_lock_node = "/_zklocking") @zk = zookeeper_client @root_lock_node = root_lock_node @path = name @locked = false @waiting = false @root_lock_path = "#{@root_lock_node}/#{@path.gsub("/", "__")}" end |
Instance Attribute Details
#lock_path ⇒ Object (readonly)
our absolute lock node path
ex. ‘/_zklocking/foobar/__blah/lock000000007’
37 38 39 |
# File 'lib/z_k/locker.rb', line 37 def lock_path @lock_path end |
#root_lock_path ⇒ Object (readonly)
:nodoc:
39 40 41 |
# File 'lib/z_k/locker.rb', line 39 def root_lock_path @root_lock_path end |
#zk ⇒ Object
:nodoc:
32 33 34 |
# File 'lib/z_k/locker.rb', line 32 def zk @zk end |
Class Method Details
.digit_from_lock_path(path) ⇒ Object
:nodoc:
41 42 43 |
# File 'lib/z_k/locker.rb', line 41 def self.digit_from_lock_path(path) #:nodoc: path[/0*(\d+)$/, 1].to_i end |
Instance Method Details
#cleanup_lock_path! ⇒ Object (protected)
126 127 128 129 130 |
# File 'lib/z_k/locker.rb', line 126 def cleanup_lock_path! logger.debug { "removing lock path #{@lock_path}" } @zk.delete(@lock_path) @zk.delete(root_lock_path) rescue Exceptions::NotEmpty end |
#create_lock_path!(prefix = 'lock') ⇒ Object (protected)
prefix is the string that will appear in front of the sequence num, defaults to ‘lock’
117 118 119 120 121 122 123 124 |
# File 'lib/z_k/locker.rb', line 117 def create_lock_path!(prefix='lock') @lock_path = @zk.create("#{root_lock_path}/#{prefix}", "", :mode => :ephemeral_sequential) logger.debug { "got lock path #{@lock_path}" } @lock_path rescue Exceptions::NoNode create_root_path! retry end |
#create_root_path! ⇒ Object (protected)
111 112 113 |
# File 'lib/z_k/locker.rb', line 111 def create_root_path! @zk.mkdir_p(@root_lock_path) end |
#digit_from(path) ⇒ Object (protected)
97 98 99 |
# File 'lib/z_k/locker.rb', line 97 def digit_from(path) self.class.digit_from_lock_path(path) end |
#in_waiting_status ⇒ Object (protected)
90 91 92 93 94 95 |
# File 'lib/z_k/locker.rb', line 90 def in_waiting_status w, @waiting = @waiting, true yield ensure @waiting = w end |
#lock_basename ⇒ Object
the basename of our lock path
for the lock_path ‘/_zklocking/foobar/__blah/lock000000007’ lock_basename is ‘lock000000007’
returns nil if lock_path is not set
68 69 70 |
# File 'lib/z_k/locker.rb', line 68 def lock_basename lock_path and File.basename(lock_path) end |
#lock_children(watch = false) ⇒ Object (protected)
101 102 103 |
# File 'lib/z_k/locker.rb', line 101 def lock_children(watch=false) @zk.children(root_lock_path, :watch => watch) end |
#locked? ⇒ Boolean
72 73 74 |
# File 'lib/z_k/locker.rb', line 72 def locked? false|@locked end |
#ordered_lock_children(watch = false) ⇒ Object (protected)
105 106 107 108 109 |
# File 'lib/z_k/locker.rb', line 105 def ordered_lock_children(watch=false) lock_children(watch).tap do |ary| ary.sort! { |a,b| digit_from(a) <=> digit_from(b) } end end |
#unlock! ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/z_k/locker.rb', line 76 def unlock! if @locked cleanup_lock_path! @locked = false true end end |
#waiting? ⇒ Boolean
returns true if this locker is waiting to acquire lock
85 86 87 |
# File 'lib/z_k/locker.rb', line 85 def waiting? #:nodoc: false|@waiting end |
#with_lock ⇒ Object
block caller until lock is aquired, then yield
55 56 57 58 59 60 |
# File 'lib/z_k/locker.rb', line 55 def with_lock lock!(true) yield ensure unlock! end |