| 1 | package Net::ZooKeeper::Lock; | ||
| 2 | { | ||
| 3 | $Net::ZooKeeper::Lock::VERSION = '0.03'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Net::ZooKeeper qw(:events :node_flags :acls); | ||
| 10 | use Params::Validate qw(:all); | ||
| 11 | |||
| 12 | # ABSTRACT: distributed locks via ZooKeeper | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | my $p = validate(@_, { | ||
| 19 | blocking => { type => BOOLEAN, default => 1 }, | ||
| 20 | zkh => { isa => 'Net::ZooKeeper' }, | ||
| 21 | lock_prefix => { type => SCALAR, regex => qr{^/.+}o, default => '/lock' }, | ||
| 22 | lock_name => { type => SCALAR, regex => qr{^[^/]+$}o }, | ||
| 23 | create_prefix => { type => BOOLEAN, default => 1 }, | ||
| 24 | watch_timeout => { type => SCALAR, regex => qr/^\d+$/o, default => 86400 * 1000 }, | ||
| 25 | data => { type => SCALAR, default => 0 }, | ||
| 26 | }); | ||
| 27 | $p->{lock_prefix} =~ s{/$}{}; | ||
| 28 | |||
| 29 | my $self = $p; | ||
| 30 | bless $self, $class; | ||
| 31 | |||
| 32 | if ($self->_lock) { | ||
| 33 | return $self; | ||
| 34 | } else { | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub lock_path { | ||
| 41 | my $self = shift; | ||
| 42 | return $self->{lock_path}; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub unlock { | ||
| 47 | my $self = shift; | ||
| 48 | $self->{zkh}->delete($self->{lock_path}) if ($self->{lock_path}); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _create_cyclic_path { | ||
| 52 | my ($self, $path) = @_; | ||
| 53 | |||
| 54 | my $current_index = 1; | ||
| 55 | while ($current_index > 0) { | ||
| 56 | $current_index = index($path, "/", $current_index + 1); | ||
| 57 | my $current_path; | ||
| 58 | if ($current_index > 0) { | ||
| 59 | $current_path = substr($path, 0, $current_index); | ||
| 60 | } else { | ||
| 61 | $current_path = $path; | ||
| 62 | } | ||
| 63 | |||
| 64 | if (!$self->{zkh}->exists($current_path)) { | ||
| 65 | $self->{zkh}->create($current_path, '0', | ||
| 66 | acl => ZOO_OPEN_ACL_UNSAFE | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | sub _lock { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | my $zkh = $self->{zkh}; | ||
| 75 | my $lock_prefix = $self->{lock_prefix}; | ||
| 76 | my $lock_name = $self->{lock_name}; | ||
| 77 | |||
| 78 | if ($self->{create_prefix}) { | ||
| 79 | $self->_create_cyclic_path($lock_prefix); | ||
| 80 | } | ||
| 81 | |||
| 82 | my $lock_tmpl = $lock_prefix . "/" . $lock_name . "-"; | ||
| 83 | my $lock_path = $zkh->create($lock_tmpl, $self->{data}, | ||
| 84 | flags => (ZOO_EPHEMERAL | ZOO_SEQUENCE), | ||
| 85 | acl => ZOO_OPEN_ACL_UNSAFE) or | ||
| 86 | die "unable to create sequence znode $lock_tmpl: " . $zkh->get_error . "\n"; | ||
| 87 | |||
| 88 | $self->{lock_path} = $lock_path; | ||
| 89 | |||
| 90 | while (1) { | ||
| 91 | my @child_names = $zkh->get_children($lock_prefix); | ||
| 92 | die "no childs\n" unless (scalar(@child_names)); | ||
| 93 | |||
| 94 | my @less_than_me = sort | ||
| 95 | grep { ($_ =~ m/^${lock_name}-\d+$/) && | ||
| 96 | ($lock_prefix . "/" . $_ lt $lock_path) } | ||
| 97 | @child_names; | ||
| 98 | |||
| 99 | unless (@less_than_me) { | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | |||
| 103 | unless ($self->{blocking}) { | ||
| 104 | $self->unlock; | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | |||
| 108 | $self->_exists($lock_prefix . "/" . $less_than_me[-1]); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _exists { | ||
| 113 | my ($self, $path) = @_; | ||
| 114 | |||
| 115 | my $zkh = $self->{zkh}; | ||
| 116 | |||
| 117 | my $watcher = $zkh->watch(timeout => $self->{watch_timeout}); | ||
| 118 | while (1) { | ||
| 119 | my $exists = $zkh->exists( | ||
| 120 | $path, | ||
| 121 | watch => $watcher, | ||
| 122 | ); | ||
| 123 | |||
| 124 | if (!$exists) { | ||
| 125 | next; | ||
| 126 | } else { | ||
| 127 | my $ret = $watcher->wait; | ||
| 128 | unless ($ret) { | ||
| 129 | next; | ||
| 130 | } | ||
| 131 | |||
| 132 | if ($watcher->{event} == ZOO_DELETED_EVENT) { | ||
| 133 | last; | ||
| 134 | } else { | ||
| 135 | next; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub DESTROY { | ||
| 142 | local $@; | ||
| 143 | |||
| 144 | my $self = shift; | ||
| 145 | |||
| 146 | $self->unlock; | ||
| 147 | }; | ||
| 148 | |||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ |