blib/lib/Lock/Server.pm

filter: Statementstmt 92.6% 278 / 300 Branchbran 76.8% 63 / 82 Conditioncond 57.8% 37 / 64 MC/DCmcdc 21.6% 8 / 37 Subroutinesub 92.5% 37 / 40 Podpod 100% 6 / 6 totaltotal 81.1% 429 / 529 CC 70 SCAR 19.1
CC
70
Cov
85%
new
16.4
_run_loop
13.1
new
9.5
CRAP 6.7
↑ Summary
1 package Lock::Server;
2  
3 =head1 NAME
4  
5 Lock::Server - Light-weight RESTful socket based resource locking manager.
6  
7 =head1 DESCRIPTION
8  
9 This creates a socket server that handles lock and
10 unlock requests. The lock requests only return once a lock is
11 obtained or a timeout has occurred. A lock may only be locked
12 for a specific amount of time before the lock is timed out.
13  
14 The protocol used is RESTFUL HTTP though the helper class wraps
15 that. It uses the GET verb with the following paths :
16  
17 * CHECK/key - returns 1 if the key in question is currently locked
18 returns 0 if not
19  
20 * LOCK/key/requester - returns lock expire time or 0
21 if there was an error
22  
23 * UNLOCK/key/requester - returns 1 if the unlock went as expected,
24 0 otherwise
25  
26 * VERIFY/key/requester - returns 1 if the key is locked to the
27 requester and did not time out and 0
28 otherwise.
29 * PING - returns 1 if the server is active
30  
31 * SHUTDOWN - stops this LockServer
32  
33 This does not do deadlock detection, relying on the timeouts to
34 prevent the system from getting in a hopelessly tangled state.
35 Care should be taken, as with any resource locking system, with
36 the use of Lock::Server. Adjust the timeouts for what makes sense
37 with the system you are designing. The lock requests return with the
38 time that the lock will expire.
39  
40 =head1 SYNPOSIS
41  
42 use Lock::Server;
43 use Lock::Server::Client;
44  
45 my $lockServer = new Lock::Server( {
46 lock_timeout => 10, #microsecondsseconds. default is 3000
47 lock_attempt_timeout => 12, #microseconds. default is 4000
48 port => 888, #default is 8004
49 host => 'localhost', #default 127.0.0.1
50 } );
51  
52 if( my $childPid = $lockServer->start ) {
53 print "Lock server started in child thread $childPid\n";
54 }
55  
56 my $optional_args = { reconnect_attempts => 3, time_between_attempts => 1 };
57 my $lockClient_A = $lockServer->client( "CLIENT_A", $optional_args );
58 my $lockClient_B =
59 new Lock::Server::Client( "CLIENT_B", 'localhost', 888, $optional_args );
60  
61 if( $lockClient_A->lock( "KEYA" ) ) {
62 print "Lock Successfull for locker A and KEYA\n";
63 } else {
64 print "Could not obtain lock in 12 seconds.\n";
65 }
66  
67 # KEYA for LockerI times out after 10 seconds.
68 # Lock Client B waits until it can obtain the lock
69 if( $lockClient_B->lock( "KEYA" ) ) {
70 print "Lock Successfull for Client B lock 'KEYA'\n";
71 } else {
72 print "Could not obtain lock in 12 seconds.\n";
73 }
74  
75 # KEYA for LockerII is now freed. The next locker
76 # attempting to lock KEYA will then obtain the lock.
77 if( $lockClientB->unlock( "KEYA" ) ) {
78 print "Unlock Successfull\n";
79 }
80  
81 if( $lockServer->stop ) {
82 print "Lock server shut down.\n";
83 }
84  
85 =head1 METHODS
86  
87 =cut
88  
89 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 9 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 9 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 9 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 9 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 9 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
100 9 ▶use Time::HiRes qw(ualarm usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
101  
102 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 9 $VERSION = '1.75';
105  
106  
107 9 $Lock::Server::DEBUG = 0;
108  
109 =head2 Lock::Server::new( $args )
110  
111 Creates a new lock server for the given optional arguments.
112
113 Arguments are :
114 * port - port to serve on. Defaults to 8004
115 * lock_timeout - low long should a lock last in seconds
116 * lock_attempt_timeout - how long should a requester
117 wait for a lock in seconds
118 * allow_shutdown - allows a client to shut the server down
119 * reconnect_attempts - if port is busy when starting the server
120 how many retries to connect before giving up and failing startup
121 * time_between_attempts - interval between reconnection attempts
122  
123 =cut
124 11 ▶sub new {
Subroutine new: called CC=9 SCAR=28.0
Pod documented
125 11 my( $pkg, $args ) = @_;
126 11 ▶ my $class = ref( $pkg ) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
127 bless {
128 lock_timeout => $args->{lock_timeout} || 3,
129 lock_attempt_timeout => $args->{lock_attempt_timeout} || 4,
130 host => $args->{host} || '127.0.0.1',
131 port => $args->{port} || 8004,
132 allow_shutdown => $args->{allow_shutdown},
133 max_connections => $args->{max_connections} || 10,
134 _pids => {},
135 _id2pid => {},
136 _locks => {},
137 _locker_counts => {},
138 attempts => $args->{reconnect_attemps} || 10,
139 11 ▶ time_between_attempts => $args->{time_between_attempts} || 5, #seconds
Condition50%7/14
$args->{'lock_timeout'} || 3
l!l
110
$args->{'lock_attempt_timeout'} || 4
l!l
110
$args->{'host'} || '127.0.0.1'
l!l
011
$args->{'port'} || 8004
l!l
011
$args->{'max_connections'} || 10
l!l
011
$args->{'reconnect_attemps'} || 10
l!l
011
$args->{'time_between_attempts'} || 5
l!l
011
MC/DC0%0/1
$args->{'lock_timeout'} || 3
$args->{'lock_timeout'}
MC/DC0%0/1
$args->{'lock_attempt_timeout'} || 4
$args->{'lock_attempt_timeout'}
MC/DC0%0/1
$args->{'host'} || '127.0.0.1'
$args->{'host'}
MC/DC0%0/1
$args->{'port'} || 8004
$args->{'port'}
MC/DC0%0/1
$args->{'max_connections'} || 10
$args->{'max_connections'}
MC/DC0%0/1
$args->{'reconnect_attemps'} || 10
$args->{'reconnect_attemps'}
MC/DC0%0/1
$args->{'time_between_attempts'} || 5
$args->{'time_between_attempts'}
Truth table50%1/2
A
A= $args->{'lock_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'lock_attempt_timeout'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'host'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'port'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'max_connections'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'reconnect_attemps'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'time_between_attempts'}
Aresult
11
00
140  
141 }, $class;
142 } #new
143  
144  
145 =head2 client( lockername )
146  
147 Returns a client with the given name that can send lock and unlock requests for keys.
148  
149 =cut
150 21 ▶sub client {
Subroutine client: called CC=1 SCAR=0.0
Pod documented
151 21 my( $self, $name, $args ) = @_;
152 21 Lock::Server::Client->new( $name, $self->{host}, $self->{port}, $args );
153 }
154  
155 =head2 ping
156  
157 Returns '1' if this lock server is up and running
158  
159 =cut
160 8 ▶sub ping {
Subroutine ping: called CC=1 SCAR=0.0
Pod documented
161 8 return shift->client("PING")->ping;
162 }
163  
164 =head2 stop
165  
166 Kills the lock server, breaking off any connections that are waiting for a lock.
167  
168 =cut
169 3 ▶sub stop {
Subroutine stop: called CC=3 SCAR=11.0
Pod documented
170 3 my $self = shift;
171  
172 3 _log( " with '$self->{listener_socket}' socket" );
173 3 ▶ if( $self->{listener_socket} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"listener_socket"}) 3 0 3
174 3 $self->{listener_socket}->close;
175 }
176  
177 3 ▶ if( my $pid = $self->{server_pid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $self->{'server_pid'}) { } 2 1 3
178 2 $self->{error} = "Sending INT signal to lock server of pid '$pid'";
179 2 _log( " Killing lock server proc $pid" );
180 2 kill 'INT', $pid;
181  
182 2 my $res = waitpid( $pid, WNOHANG );
183  
184 2 _log( " STOP DONE" );
185 } else {
186 1 $self->{error} = "No lock server running";
187 1 return 0;
188 }
189  
190 2 return 1;
191 }
192  
193 =head2 start
194  
195 Starts the lock server in a child process, opening up a
196 tcpip socket and returning the child pid or 0 if there
197 was an error.
198  
199 =cut
200 11 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
201 11 my $self = shift;
202 11 my $sock = $self->_create_listener_socket;
203 11 $self->{listener_socket} = $sock;
204 11 ▶ die "Unable to open lockserver socket $@,$! " unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 11 11
205  
206 11 ▶ if( my $pid = fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) 7 4 11
207 # parent
208 7 $self->{server_pid} = $pid;
209 7 return $pid;
210 }
211  
212 # child process
213 4 $0 = "LockServer";
214 4 $self->_run_loop( $sock );
215 1 exit;
216 } #start
217  
218 =head2 run
219  
220 Runs the lock server.
221  
222 =cut
223 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod documented
224 0 my $self = shift;
225 0 my $sock = $self->_create_listener_socket;
226 0 $self->{listener_socket} = $sock;
227 0 ▶ die "Unable to open lockserver socket $@,$! " unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
228 0 $self->_run_loop( $sock );
229 0 exit;
230 } #run
231  
232 11 ▶sub _create_listener_socket {
Subroutine _create_listener_socket: called CC=7 SCAR=20.7
233 11 my $self = shift;
234  
235 11 my( $listener_socket, $count );
236  
237 11 my $st = Time::HiRes::time;
238
239 11 ▶ until( $listener_socket || $count++ > $self->{attempts} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listener_socket or $count++ > $self->{"attempts"}) 17 0 17
Condition33%1/3
$listener_socket or $count++ > $self->{"attempts"}
l!l&&r!l&&!r
0017
MC/DC0%0/2
$listener_socket or $count++ > $self->{"attempts"}
$listener_socket $count++ > $self->{"attempts"}
Truth table33%1/3
A or B
A= $listener_socketB= $count++ > $self->{"attempts"}
ABresult
1X1
011
000
240 $listener_socket = new IO::Socket::INET(
241 Listen => 1,
242 LocalPort => $self->{port},
243 # LocalAddr => "$self->{host}:$self->{port}",
244 # Proto => 'tcp',
245 # ReuseAddr => 1,
246 # ReusePort => 1,
247 17 );
248 17 ▶ last if $listener_socket;
Branch: 1 branch
BranchTrueFalseTotal
if $listener_socket 11 6 17
249 6 print STDERR "Unable to open the lock server socket $@, $!. Retry $count of 10\n";
250 6 ▶ sleep $count*$self->{time_between_attempts} unless $listener_socket || $count > $self->{attempts};
Branch: 1 branch
BranchTrueFalseTotal
unless $listener_socket or $count > $self->{"attempts"} 6 0 6
Condition33%1/3
$listener_socket or $count > $self->{"attempts"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$listener_socket or $count > $self->{"attempts"}
$listener_socket $count > $self->{"attempts"}
Truth table33%1/3
A or B
A= $listener_socketB= $count > $self->{"attempts"}
ABresult
1X1
011
000
251 }
252 11 ▶ unless( $listener_socket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listener_socket) 0 11 11
253  
254 0 $self->{error} = "Unable to open socket on port '$self->{port}' : $! $@\n";
255 0 _log( "unable to start lock server : $@ $!." );
256 0 return 0;
257 }
258  
259 # if this is cancelled, make sure all child procs are killed too
260 1 ▶ $SIG{TERM} = $SIG{INT} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
261 1 _log( "lock server : got INT signal. Shutting down." );
262 1 ▶ $listener_socket && $listener_socket->close;
Branch: 1 branch
BranchTrueFalseTotal
$listener_socket and $listener_socket->close 1 0 1
263  
264 1 kill 'INT', keys %{ $self->{_pids} };
265  
266 1 ▶ while( (my $kidpid = waitpid( -1, WNOHANG ) ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $kidpid = waitpid(-1, 1)) > 0) 2 1 3
267 2 _log( " Killed $kidpid" );
268 }
269 1 $self->{_pids} = {};
270 1 _log( "lock server : got INT signal. EXITING." );
271 1 exit;
272 11 };
273 11 return $listener_socket;
274 } #_create_listener_socket
275  
276 4 ▶sub _run_loop {
Subroutine _run_loop: called CC=13 SCAR=25.7
277 4 my( $self, $listener_socket ) = @_;
278  
279 4 my $sel = IO::Select->new( $listener_socket );
280 4 my @ready;
281 4 ▶ while(@ready = $sel->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ready = $sel->can_read) 208 1 209
282 208 for my $connection (@ready) {
283 210 ▶ if( $connection == $listener_socket ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection == $listener_socket) { } 105 105 210
284 105 $sel->add($listener_socket->accept );
285 } else {
286 105 my $req = <$connection>;
287 105 $req =~ s/\s+$//s;
288 105 _log( "lock server : incoming request : '$req'" );
289 # could have headers, but ignore those. Find \n\n
290 105 ▶ while( my $data = <$connection> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = readline $connection)) 105 0 105
291 105 chomp $data;
292 105 ▶ last unless $data =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $data =~ /\S/ 105 0 105
293 }
294  
295 105 my( $cmd, $key, $locker_id ) = split( '/', substr( $req, 5 ) );
296 105 ▶ if( $cmd eq 'CHECK' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($cmd eq 'CHECK') { } 20 85 105
elsif ($cmd eq 'LOCK') { } 22 63 85
elsif ($cmd eq 'UNLOCK') { } 17 46 63
elsif ($cmd eq 'VERIFY') { } 35 11 46
elsif ($cmd eq 'PING') { } 9 2 11
elsif ($cmd eq 'SHUTDOWN') { } 2 0 2
297 20 $self->_check( $connection, $key );
298 } elsif( $cmd eq 'LOCK' ) {
299 22 $self->_lock( $connection, $key, $locker_id );
300 } elsif( $cmd eq 'UNLOCK' ) {
301 17 $self->_unlock( $connection, $key, $locker_id );
302 } elsif( $cmd eq 'VERIFY' ) {
303 35 $self->_verify( $connection, $key, $locker_id );
304 } elsif( $cmd eq 'PING' ) {
305 9 print $connection "1\n";
306 } elsif( $cmd eq 'SHUTDOWN') {
307 2 ▶ if( $self->{allow_shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'allow_shutdown'}) { } 1 1 2
308 1 print $connection "1\n";
309 1 $connection->close;
310 1 $self->stop;
311 } else {
312 1 _log( "lock server : got shutdown request but not configured to allow it" );
313 }
314 } else {
315 0 _log( "lock server : did not understand command '$cmd'" );
316 }
317 103 $sel->remove($connection);
318 103 $connection->close;
319 }
320 } #ready loop
321 } #can_read loop
322 } #_run_loop
323  
324 20 ▶sub _check {
Subroutine _check: called CC=6 SCAR=17.9
325 20 my( $self, $connection, $key_to_check ) = @_;
326 20 _log( "locker server check for key '$key_to_check'" );
327  
328 20 ▶ $self->{_locks}{$key_to_check} ||= [];
Condition100%2/2
$self->{"_locks"}{$key_to_check} ||= []
l!l
173
MC/DC100%1/1
$self->{"_locks"}{$key_to_check} ||= []
$self->{"_locks"}{$key_to_check}
Truth table100%2/2
A
A= $self->{"_locks"}{$key_to_check}
Aresult
11
00
329 20 my $lockers = $self->{_locks}{$key_to_check};
330  
331
332 #check for timed out lockers
333 20 my $t = Time::HiRes::time;
334 20 ▶ while( @$lockers && $t > $self->{_locker_counts}{$lockers->[0]}{$key_to_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}) 2 20 22
Condition100%3/3
@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
!ll&&!rl&&r
8122
MC/DC100%2/2
@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
@$lockers $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
Truth table100%3/3
A and B
A= @$lockersB= $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
ABresult
0X0
100
111
335 2 _log( "lock server _check : '$key_to_check' timed out for locker '$lockers->[0]'" );
336 2 ▶ if( 1 == keys %{ $self->{_locker_counts}{$lockers->[0]} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %{$self->{'_locker_counts'}{$lockers->[0]};}) { } 2 0 2
337 2 delete $self->{_locker_counts}{$lockers->[0]};
338 } else {
339 0 delete $self->{_locker_counts}{$lockers->[0]}{$key_to_check};
340 }
341 2 shift @$lockers;
342 }
343  
344  
345 20 ▶ if( @$lockers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lockers) { } 12 8 20
346 12 print $connection "1\n";
347 } else {
348 8 print $connection "0\n";
349 }
350 }
351  
352 339 ▶sub _log {
Subroutine _log: called CC=2 SCAR=7.1
353 339 my $msg = shift;
354 339 $msg = "($$) $msg";
355 339 ▶ print STDERR "Lock::Server : $msg\n" if $Lock::Server::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Lock::Server::DEBUG 0 339 339
356 }
357  
358 22 ▶sub _lock {
Subroutine _lock: called CC=8 SCAR=21.6
359 22 my( $self, $connection, $key_to_lock, $locker_id ) = @_;
360 22 _log( "lock request : for '$locker_id' and key '$key_to_lock'" );
361  
362 22 ▶ $self->{_locks}{$key_to_lock} ||= [];
Condition100%2/2
$self->{"_locks"}{$key_to_lock} ||= []
l!l
157
MC/DC100%1/1
$self->{"_locks"}{$key_to_lock} ||= []
$self->{"_locks"}{$key_to_lock}
Truth table100%2/2
A
A= $self->{"_locks"}{$key_to_lock}
Aresult
11
00
363 22 my $lockers = $self->{_locks}{$key_to_lock};
364 #check for timed out lockers
365 22 my $t = Time::HiRes::time;
366  
367 22 ▶ while( @$lockers && $t > $self->{_locker_counts}{$lockers->[0]}{$key_to_lock} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_lock}) 0 22 22
Condition66%2/3
@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_lock}
!ll&&!rl&&r
1480
MC/DC0%0/2
@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_lock}
@$lockers $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_lock}
Truth table66%2/3
A and B
A= @$lockersB= $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_lock}
ABresult
0X0
100
111
368 0 _log( "lock '$key_to_lock' timed out for locker '$lockers->[0]'" );
369 0 ▶ if( 1 == keys %{ $self->{_locker_counts}{$lockers->[0]} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %{$self->{'_locker_counts'}{$lockers->[0]};}) { } 0 0 0
370 0 delete $self->{_locker_counts}{$lockers->[0]};
371 } else {
372 0 delete $self->{_locker_counts}{$lockers->[0]}{$key_to_lock};
373 }
374 0 shift @$lockers;
375 }
376  
377 22 ▶ if( 0 < (grep { $_ eq $locker_id } @$lockers) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < grep({$_ eq $locker_id;} @$lockers)) 3 19 22
378 3 _log( "lock request error. '$locker_id' already in the lock queue" );
379 3 print $connection "0\n";
380 3 return;
381 }
382  
383 # store when this times out
384 19 my $timeout_time = Time::HiRes::time + $self->{lock_timeout};
385 19 $self->{_locker_counts}{$locker_id}{$key_to_lock} = $timeout_time;
386 19 push @$lockers, $locker_id;
387  
388 19 _log( "lock request : there are now ".scalar(@$lockers)." lockers" );
389 19 ▶ if( @$lockers > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lockers > 1) { } 5 14 19
390 5 ▶ if( (my $pid=fork)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 3 2 5
391 3 $self->{_id2pid}{$locker_id} = $pid;
392 3 $self->{_pids}{$pid} = 1;
393 3 _log( "lock request : parent process associating '$locker_id' with pid '$pid' ".scalar(@$lockers)." lockers" );
394 # parent
395 } else {
396 # use Devel::SimpleProfiler;Devel::SimpleProfiler::start;
397 2 $0 = "LockServer processing request";
398 0 ▶ $SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
399 0 _log( "lock request : child got INT, exiting." );
400 0 $connection->close;
401 0 exit;
402 2 };
403 1 ▶ $SIG{HUP} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
404 1 _log( "lock request : child got HUP, so is now locked." );
405 1 $connection->print( "$timeout_time\n" );
406 1 $connection->close;
407 1 exit;
408 2 };
409 2 _log( "lock request : child ready to wait" );
410 2 usleep 1_000_000 * $self->{lock_attempt_timeout};
411 1 _log( "lock request failed : child timed out" );
412 1 print $connection "0\n";
413 1 $connection->close;
414 1 exit;
415 }
416 } else {
417 14 _log( "lock request : no need to invoke more processes. locking" );
418 14 print $connection "$timeout_time\n";
419 }
420 } #_lock
421  
422 17 ▶sub _unlock {
Subroutine _unlock: called CC=5 SCAR=16.1
423 17 my( $self, $connection, $key_to_unlock, $locker_id ) = @_;
424 17 _log( "unlock request : key '$key_to_unlock' for locker '$locker_id'" );
425  
426 17 ▶ $self->{_locks}{$key_to_unlock} ||= [];
Condition50%1/2
$self->{"_locks"}{$key_to_unlock} ||= []
l!l
170
MC/DC0%0/1
$self->{"_locks"}{$key_to_unlock} ||= []
$self->{"_locks"}{$key_to_unlock}
Truth table50%1/2
A
A= $self->{"_locks"}{$key_to_unlock}
Aresult
11
00
427 17 my $lockers = $self->{_locks}{$key_to_unlock};
428  
429 17 ▶ if( $lockers->[0] eq $locker_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lockers->[0] eq $locker_id) { } 11 6 17
430 11 shift @$lockers;
431 11 delete $self->{_locker_counts}{$locker_id}{$key_to_unlock};
432 11 ▶ if( 0 == scalar(keys %{$self->{_locker_counts}{$locker_id}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == scalar keys %{$$self{"_locker_counts"}{$locker_id};}) 8 3 11
433 8 _log( "unlock : remove information about '$locker_id'" );
434 8 delete $self->{_id2pid}{$locker_id};
435 8 delete $self->{_locker_counts}{$locker_id};
436 }
437 11 _log( "unlocking '$locker_id'" );
438 11 ▶ if( @$lockers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lockers) { } 2 9 11
439 2 my $next_locker_id = $lockers->[0];
440 2 my $pid = $self->{_id2pid}{$next_locker_id};
441 2 _log( "unlock : next locker in queue is '$next_locker_id'. Sending kill signal to its pid '$pid'" );
442 2 kill 'HUP', $pid;
443 } else {
444 9 _log( "unlock : now no one waiting on a lock for key '$key_to_unlock'" );
445 }
446 11 _log( "unlock : done, informing connection" );
447 11 print $connection "1\n";
448 } else {
449 6 _log( "unlock error : Wrong locker_id to unlock for unlock for locker '$locker_id' and key '$key_to_unlock'. The locker_id must be the one at the front of the queue" );
450 # "Wrong locker_id to unlock. The locker_id must be the one at the front of the queue";
451 6 print $connection "0\n";
452 }
453 } #_unlock
454  
455 35 ▶sub _verify {
Subroutine _verify: called CC=6 SCAR=18.0
456 35 my( $self, $connection, $key_to_check, $locker_id ) = @_;
457  
458 35 _log( "verify : locker server check for key '$key_to_check' for locker '$locker_id'" );
459  
460 35 ▶ $self->{_locks}{$key_to_check} ||= [];
Condition50%1/2
$self->{"_locks"}{$key_to_check} ||= []
l!l
350
MC/DC0%0/1
$self->{"_locks"}{$key_to_check} ||= []
$self->{"_locks"}{$key_to_check}
Truth table50%1/2
A
A= $self->{"_locks"}{$key_to_check}
Aresult
11
00
461 35 my $lockers = $self->{_locks}{$key_to_check};
462  
463 #check for timed out lockers
464 35 my $t = Time::HiRes::time;
465 35 _log( "verify: compare '$t' > '$self->{_locker_counts}{$lockers->[0]}{$key_to_check}' if the first is greater, there is a time out" );
466 35 ▶ while( @$lockers && $t > $self->{_locker_counts}{$lockers->[0]}{$key_to_check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}) 1 35 36
Condition100%3/3
@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
!ll&&!rl&&r
16191
MC/DC100%2/2
@$lockers and $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
@$lockers $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
Truth table100%3/3
A and B
A= @$lockersB= $t > $self->{"_locker_counts"}{$lockers->[0]}{$key_to_check}
ABresult
0X0
100
111
467 1 _log( "verify: '$key_to_check' timed out for locker '$lockers->[0]'" );
468 1 ▶ if( 1 == keys %{ $self->{_locker_counts}{$lockers->[0]} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %{$self->{'_locker_counts'}{$lockers->[0]};}) { } 0 1 1
469 0 delete $self->{_locker_counts}{$lockers->[0]};
470 } else {
471 1 delete $self->{_locker_counts}{$lockers->[0]}{$key_to_check};
472 }
473 1 shift @$lockers;
474 }
475  
476 35 ▶ if( $lockers->[0] eq $locker_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lockers->[0] eq $locker_id) { } 17 18 35
477 17 print $connection "1\n";
478 } else {
479 18 print $connection "0\n";
480 }
481 }
482  
483  
484  
485 =head1 Helper package
486  
487 =head2 NAME
488  
489 Lock::Server::Client - client for locking server.
490  
491 =head2 DESCRIPTION
492  
493 Sends request to a Lock::Server to lock, unlock and check locks.
494  
495 =head2 METHODS
496  
497 =cut
498 package Lock::Server::Client;
499  
500 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
501 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
502 9 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
503  
504 9 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
505  
506 =head3 new( lockername, host, port )
507  
508 Creates a client object with the given name for the host and port.
509  
510 =cut
511 22 ▶sub new {
Subroutine new: called CC=7 SCAR=22.5
512 22 my( $pkg, $lockerName, $host, $port, $args ) = @_;
513 22 ▶ die "Must supply locker name" unless $lockerName;
Branch: 1 branch
BranchTrueFalseTotal
unless $lockerName 0 22 22
514  
515 22 ▶ $host ||= '127.0.0.1';
Condition50%1/2
$host ||= "127.0.0.1"
l!l
220
MC/DC0%0/1
$host ||= "127.0.0.1"
$host
Truth table50%1/2
A
A= $host
Aresult
11
00
516 22 ▶ $port ||= '8004';
Condition50%1/2
$port ||= 8004
l!l
220
MC/DC0%0/1
$port ||= 8004
$port
Truth table50%1/2
A
A= $port
Aresult
11
00
517  
518 22 ▶ my $class = ref( $pkg ) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
0220
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
519 bless {
520 host => $host,
521 port => $port,
522 name => $lockerName,
523 attempts => $args->{reconnect_attemps} || 3,
524 22 ▶ time_between_attempts => $args->{time_between_attempts} || 3,
Condition50%2/4
$args->{'reconnect_attemps'} || 3
l!l
022
$args->{'time_between_attempts'} || 3
l!l
022
MC/DC0%0/1
$args->{'reconnect_attemps'} || 3
$args->{'reconnect_attemps'}
MC/DC0%0/1
$args->{'time_between_attempts'} || 3
$args->{'time_between_attempts'}
Truth table50%1/2
A
A= $args->{'reconnect_attemps'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'time_between_attempts'}
Aresult
11
00
525 }, $class;
526 } #new
527  
528 162 ▶sub _get_sock {
Subroutine _get_sock: called CC=7 SCAR=19.8
529 162 my $self = shift;
530 162 ▶ my $attempts = shift || $self->{attempts};
Condition66%2/3
shift() || $self->{'attempts'}
l!l&&r!l&&!r
251370
MC/DC0%0/2
shift() || $self->{'attempts'}
shift() $self->{'attempts'}
Truth table66%2/3
A || B
A= shift()B= $self->{'attempts'}
ABresult
1X1
011
000
531  
532 # try a few times, then give up
533 162 my( $sock, $count );
534 162 ▶ until( $sock || $count++ > $attempts ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock or $count++ > $attempts) 162 158 320
Condition66%2/3
$sock or $count++ > $attempts
l!l&&r!l&&!r
1580162
MC/DC50%1/2
$sock or $count++ > $attempts
$sock $count++ > $attempts
Truth table66%2/3
A or B
A= $sockB= $count++ > $attempts
ABresult
1X1
011
000
535 162 $sock = new IO::Socket::INET( "$self->{host}:$self->{port}" );
536 162 ▶ sleep $self->{time_between_attempts}*($count) unless $sock || $count > $attempts;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock or $count > $attempts 4 158 162
Condition66%2/3
$sock or $count > $attempts
l!l&&r!l&&!r
15804
MC/DC50%1/2
$sock or $count > $attempts
$sock $count > $attempts
Truth table66%2/3
A or B
A= $sockB= $count > $attempts
ABresult
1X1
011
000
537 }
538 158 ▶ die "Could not connect : $@" unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 158 158
539 158 binmode $sock, ':utf8';
540 158 $sock;
541 }
542  
543 =head3 isLocked( key )
544  
545 Returns true if the key is locked by anyone.
546  
547 =cut
548 28 ▶sub isLocked {
Subroutine isLocked: called CC=1 SCAR=0.0
549 28 my( $self, $key ) = @_;
550 28 my $sock = $self->_get_sock;
551  
552 28 $sock->print( "GET /CHECK/$key\n\n" );
553 28 my $resp = <$sock>;
554 28 $sock->close;
555 28 chomp $resp;
556 28 $resp;
557 }
558  
559 =head3 lockedByMe( key )
560  
561 Returns true if the key is locked by this client or
562 anyone with the name of this client. The name was given in the constructor.
563  
564 =cut
565 58 ▶sub lockedByMe {
Subroutine lockedByMe: called CC=1 SCAR=0.0
566 58 my( $self, $key ) = @_;
567 58 my $sock = $self->_get_sock;
568  
569 58 $sock->print( "GET /VERIFY/$key/$self->{name}\n\n" );
570 58 my $resp = <$sock>;
571 58 $sock->close;
572 58 chomp $resp;
573 58 $resp;
574 }
575  
576 =head3 lock( key )
577  
578 Attempt to get the lock for the given key. Returns true if the lock
579 was obtained.
580  
581 =cut
582 24 ▶sub lock {
Subroutine lock: called CC=1 SCAR=0.0
583 24 my( $self, $key ) = @_;
584 24 my $sock = $self->_get_sock;
585  
586 24 $sock->print( "GET /LOCK/$key/$self->{name}\n\n" );
587 24 my $resp = <$sock>;
588 24 $sock->close;
589 24 chomp $resp;
590 24 $resp;
591 }
592  
593 =head3 unlock( key )
594  
595 Attempt to get unlock the given key. Returns true if the
596 key was locked to this client ( or someting with the same name ).
597  
598 =cut
599 27 ▶sub unlock {
Subroutine unlock: called CC=1 SCAR=0.0
600 27 my( $self, $key ) = @_;
601 27 my $sock = $self->_get_sock;
602 27 $sock->print( "GET /UNLOCK/$key/$self->{name}\n\n" );
603 27 my $resp = <$sock>;
604 27 $sock->close;
605 27 chomp $resp;
606 27 $resp;
607 }
608  
609 22 ▶sub ping {
Subroutine ping: called CC=2 SCAR=6.9
610 22 my( $self, $timeout ) = @_;
611  
612 22 ▶ $timeout //= 3;
Condition50%1/2
$timeout //= 3
l!l
022
MC/DC0%0/1
$timeout //= 3
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
613  
614
615 22 ▶ local $SIG{ALRM} = sub { die "ALARM\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
616 22 alarm $timeout;
617 22 my $resp = '0';
618 22 eval {
619 22 my $sock = $self->_get_sock( 1 );
620 18 $sock->print( "GET /PING\n\n" );
621 18 $resp = <$sock>;
622 16 alarm 0;
623 16 $sock->close;
624 };
625 22 chomp $resp;
626 22 $resp;
627 } #ping
628  
629 3 ▶sub shutdown {
Subroutine shutdown: called CC=2 SCAR=7.0
630 3 my( $self, $timeout ) = @_;
631  
632 3 ▶ $timeout //= 3;
Condition50%1/2
$timeout //= 3
l!l
03
MC/DC0%0/1
$timeout //= 3
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
633  
634 0 ▶ local $SIG{ALRM} = sub { die "ALARM\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
635 3 alarm $timeout;
636 3 eval {
637 3 my $sock = $self->_get_sock( 1 );
638 3 $sock->print( "GET /SHUTDOWN\n\n" );
639 3 alarm 0;
640 3 $sock->close;
641 };
642 3 $@;
643 } #shutdown
644  
645  
646 9 1;
647  
648  
649 __END__
↑ Summary