Class: ZK::Locker::ExclusiveLocker

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

Overview

An exclusive lock implementation

If the name ‘dingus’ is given, then in the case of an exclusive lock, the algorithm works like:

  • lock_path = zk.create("/_zklocking/dingus/ex", :sequential => true, :ephemeral => true)
  • extract the digit from the lock path
  • of all the children under ‘/_zklocking/dingus’, do we have the lowest digit?
    • yes: then we hold the lock, if we’re non-blocking, return true
    • no: is the lock blocking?
      • yes: then set a watch on the next-to-lowest node and sleep the current thread until that node has been deleted
      • no: return false, you lose

Instance Attribute Summary

Attributes inherited from LockerBase

#lock_path

Instance Method Summary collapse

Methods inherited from LockerBase

#assert, #initialize, #interrupt!, #lock!, #lock_basename, #locked?, #unlock, #unlock!, #with_lock

Methods included from ZK::Logger

#logger, wrapped_logger, wrapped_logger=

Constructor Details

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

Instance Method Details

#acquirable?Boolean

Note:

It should be obvious, but there is no way to guarantee that between the time this method checks the server and taking any action to acquire the lock, another client may grab the lock before us (or converseley, another client may release the lock). This is simply meant as an advisory, and may be useful in some cases.

  • If this instance holds the lock we return true (as we have already succeeded in acquiring the lock)
  • If this instance doesn’t hold the lock, we’ll do a check on the server to see if there are any participants who hold the lock and would prevent us from acquiring the lock. * If this instance could acquire the lock we will return true. * If another client would prevent us from acquiring the lock, we return false.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/zk/locker/exclusive_locker.rb', line 38

def acquirable?
  return true if locked? 
  stat = zk.stat(root_lock_path)
  !stat.exists? or stat.num_children == 0
rescue Exceptions::NoNode   # XXX: is this ever hit? stat shouldn't raise
  true
end

#assert!Object

This is for users who wish to check that the assumption is correct that they actually still hold the lock. (check for session interruption, perhaps a lock is obtained in one method and handed to another)

This, unlike will actually go and check the conditions that constitute “holding the lock” with the server.

checks that we:

  • we have obtained the lock (i.e. is true)
  • have a lock path
  • our lock path still exists
  • there are no locks, exclusive or shared, with lower numbers than ours

Examples:


def process_jobs
  @lock.with_lock do
    @jobs.each do |j| 
      @lock.assert!
      perform_job(j)
    end
  end
end

def perform_job(j)
  puts "hah! he thinks we're workin!"
  sleep(60)
end

Raises:



33
34
35
# File 'lib/zk/locker/exclusive_locker.rb', line 33

def assert!
  super
end

#lock(blocking = false) ⇒ true, ... #lock(opts = {}) ⇒ true, ...

obtain an exclusive lock.

Overloads:

  • #lock(blocking = false) ⇒ true, ...
    Deprecated.

    in favor of the options hash style

    Parameters:

    • blocking (true, false) (defaults to: false)

      if true we block the caller until we can obtain a lock on the resource

  • #lock(opts = {}) ⇒ true, ...

    Options Hash (opts):

    • :wait (true, false, Numeric) — default: false

      If true we block the caller until we obtain a lock on the resource. If false, we do not block. If a Numeric, the number of seconds we should wait for the lock to be acquired. Will raise LockWaitTimeoutError if we exceed the timeout.

    Since:

    • 1.7

Returns:

  • (true)

    if we’re already obtained a shared lock, or if we were able to obtain the lock in non-blocking mode.

  • (false)

    if we did not obtain the lock in non-blocking mode

  • (void)

    if we obtained the lock in blocking mode.

Raises:

  • (InterruptedSession)

    raised when blocked waiting for a lock and the underlying client’s session is interrupted.

  • (LockWaitTimeoutError)

    if the given timeout is exceeded waiting for the lock to be acquired

See Also:



20
21
22
# File 'lib/zk/locker/exclusive_locker.rb', line 20

def lock(opts={})
  super
end