Class: ZK::Locker::ExclusiveLocker

Inherits:
LockerBase show all
Defined in:
lib/zk/locker.rb

Overview

An exclusive lock implementation

Instance Method Summary collapse

Methods inherited from LockerBase

#cleanup_lock_path!, #create_lock_path!, #create_root_path!, #digit_from, #in_waiting_status, #initialize, #lock_basename, #lock_children, #locked?, #ordered_lock_children, #unlock!, #waiting?, #with_lock

Constructor Details

This class inherits a constructor from ZK::Locker::LockerBase

Instance Method Details

#block_until_write_lock!Object (protected)



245
246
247
248
249
250
251
252
253
254
# File 'lib/zk/locker.rb', line 245

def block_until_write_lock!
  begin
    path = [root_lock_path, next_lowest_node].join('/')
    logger.debug { "SharedLocker#block_until_write_lock! path=#{path.inspect}" }
    @zk.block_until_node_deleted(path)
  rescue WeAreTheLowestLockNumberException
  end

  @locked = true
end

#got_write_lock?Boolean (protected)

Returns:

  • (Boolean)


241
242
243
# File 'lib/zk/locker.rb', line 241

def got_write_lock?
  ordered_lock_children.first == lock_basename
end

#lock!(blocking = false) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/zk/locker.rb', line 213

def lock!(blocking=false)
  return true if @locked
  create_lock_path!(EXCLUSIVE_LOCK_PREFIX)

  if got_write_lock?
    @locked = true
  elsif blocking
    in_waiting_status do
      block_until_write_lock!
    end
  else
    cleanup_lock_path!
    false
  end
end

#next_lowest_nodeObject (protected)

the node that is next-lowest in sequence number to ours, the one we watch for updates to



232
233
234
235
236
237
238
239
# File 'lib/zk/locker.rb', line 232

def next_lowest_node
  ary = ordered_lock_children()
  my_idx = ary.index(lock_basename)

  raise WeAreTheLowestLockNumberException if my_idx == 0

  ary[(my_idx - 1)] 
end