NAME

Net::ZooKeeper::Lock - distributed locks via ZooKeeper

VERSION

version 0.03

SYNOPSIS

use Net::ZooKeeper::Lock;

# take a lock
my $lock = Net::ZooKeeper::Lock->new({
    zkh => Net::ZooKeeper->new('localhost:2181'),
    lock_name   => 'bar',
});

# release a lock
$lock->unlock;
# or
undef $lock;

DESCRIPTION

This module implements distributed locks via ZooKeeper using Net::ZooKeeper and ZooKeeper recipe described at http://zookeeper.apache.org/doc/trunk/recipes.html#sc_recipes_Locks. It doesn't implements shared locks, it will appear in the next releases.

METHODS

new($options)

Takes a lock and returns object that holds this lock. Throws exception if something goes wrong.

$options is a hashref with following keys:

blocking (optional)

By default module blocks in the new method if lock already taken. If blocking is false, then new doesn't wait if lock already taken and returns undef.

zkh

Net::ZooKeeper object.

lock_prefix (optional)

"Directory" where sequential znodes for lock will be placed. It is good to make different prefixes for different locks if you have many locks (in this case Net::ZooKeeper::get_children() will return data relevant only for one concrete lock. For example, it may looks like "/lock/foo1/" for "foo1" lock and "/lock/foo2" for "foo2" lock.

Default is just "lock".

lock_name

Name of your lock (it will be concatenated with lock_prefix for creating template for sequential znodes).

create_prefix (optional)

If you want to store ephemeral znodes in lock_prefix, then znode with name lock_prefix should be created before creation of ephemeral znodes.

You can create this prefix znode once in your code. Or you can use create_prefix flag, it will check and create lock_prefix znode every time when you try to make new lock with lock_prefix.

Default is 1.

data

Data to be stored in lock znode.

It may be useful to store the hostname and pid of the process, that created the lock.

Default is '0'.

lock_path

Returns the path of lock znode.

unlock

Releases getted lock. This method calls in destructor, so your lock releases when you go out of $lock scope or when you call undef($lock).

MISCELLANEOUS

It seems that $SIG{PIPE} signal doesn't occurs in Net::ZooKeeper with new versions of ZooKeeper.

In this case following situation is possible: some process on some machine have taken the lock, then network disappeared on this machine, then another process on another machine can take the lock after session_limit. And you have 2 processes that holds the same lock.

For such a case you can create some separate check-script that will test connection with ZooKeeper every N seconds < session_limit. If connection lost, then this script can kill all processes on this machine that holds ZooKeeper locks.

SEE ALSO

Net::ZooKeeper

http://zookeeper.apache.org/doc/trunk/recipes.html#sc_recipes_Locks

ACKNOWLEDGEMENTS

Oleg Komarov

AUTHOR

Yury Zavarin <yury.zavarin@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Yury Zavarin.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.