Net::ZooKeeper::Lock - distributed locks via ZooKeeper
version 0.03
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;
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.
Takes a lock and returns object that holds this lock. Throws exception if something goes wrong.
$options is a hashref with following keys:
$options
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.
new
blocking
false
Net::ZooKeeper object.
Net::ZooKeeper
"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.
Net::ZooKeeper::get_children()
Default is just "lock".
Name of your lock (it will be concatenated with lock_prefix for creating template for sequential znodes).
lock_prefix
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.
create_prefix
Default is 1.
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'.
Returns the path of lock znode.
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).
$lock
undef($lock)
It seems that $SIG{PIPE} signal doesn't occurs in Net::ZooKeeper with new versions of ZooKeeper.
$SIG{PIPE}
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.
session_limit
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.
http://zookeeper.apache.org/doc/trunk/recipes.html#sc_recipes_Locks
Oleg Komarov
Yury Zavarin <yury.zavarin@gmail.com>
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.
To install Net::ZooKeeper::Lock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::ZooKeeper::Lock
CPAN shell
perl -MCPAN -e shell install Net::ZooKeeper::Lock
For more information on module installation, please visit the detailed CPAN module installation guide.