Class: ZK::Locker::LockerBase

Inherits:
Object
  • Object
show all
Includes:
ZK::Logging
Defined in:
lib/z_k/locker.rb

Direct Known Subclasses

ExclusiveLocker, SharedLocker

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_pathObject (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_pathObject (readonly)

:nodoc:



39
40
41
# File 'lib/z_k/locker.rb', line 39

def root_lock_path
  @root_lock_path
end

#zkObject

: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_statusObject (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_basenameObject

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

Returns:

  • (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

Returns:

  • (Boolean)


85
86
87
# File 'lib/z_k/locker.rb', line 85

def waiting? #:nodoc:
  false|@waiting
end

#with_lockObject

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