Module: ZK::Locker

Defined in:
lib/zk/locker.rb

Overview

Note:

These lock instances are not safe for use across threads. If you want to use the same Locker instance between threads, it is your responsibility to synchronize operations.

Note:

Lockers are instances that hold the lock. A single connection may have many instances trying to lock the same path and only one (in the case of an ExclusiveLocker) will hold the lock.

The most convenient way to create instances of locks is by using the methods

This module contains implementations of the locking primitives described here, that allow a user to implement cluster-wide global locks (with both blocking and non-blocking semantics).

There are both shared and exclusive lock implementations.

The implementation is fairly true to the description in the recipes, and the key is generated using a combination of the name provided, and a root_lock_node path whose default value is /_zklocking. If you look below at the 'Key path creation' example, you'll see that we do a very simple escaping of the name given. There was a distinct tradeoff to be made between making the locks easy to debug in zookeeper and making them more collision tolerant. If the key naming causees issues, please file a bug and we'll try to work out a solution (hearing about use cases is incredibly halpful in guiding development).

If you're interested in how the algorithm works, have a look at ExclusiveLocker's documentation.

Examples:

Key path creation


"#{root_lock_node}/#{name.gsub('/', '__')}/#{shared_or_exclusive_prefix}"

Defined Under Namespace

Classes: ExclusiveLocker, LockerBase, SharedLocker

Constant Summary

SHARED_LOCK_PREFIX =
'sh'.freeze
EXCLUSIVE_LOCK_PREFIX =
'ex'.freeze

Class Method Summary (collapse)

Class Method Details

+ (ExclusiveLocker) exclusive_locker(client, name, *args)

Create an ExclusiveLocker instance

Parameters:

  • client (Client::Threaded)

    a client instance

  • name (String)

    Unique name that will be used to generate a key. All instances created with the same root_lock_node and name will be holding the same lock.

Returns:



56
57
58
# File 'lib/zk/locker.rb', line 56

def self.exclusive_locker(client, name, *args)
  ExclusiveLocker.new(client, name, *args)
end

+ (SharedLocker) shared_locker(client, name, *args)

Create a SharedLocker instance

Parameters:

  • client (Client::Threaded)

    a client instance

  • name (String)

    Unique name that will be used to generate a key. All instances created with the same root_lock_node and name will be holding the same lock.

Returns:



47
48
49
# File 'lib/zk/locker.rb', line 47

def self.shared_locker(client, name, *args)
  SharedLocker.new(client, name, *args)
end