ConcurrencyLimit/WithLatestStandby.pm

filter: Statementstmt 77.5% 76 / 98 Branchbran 48.3% 29 / 60 Conditioncond 42.8% 12 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 6 totaltotal 56.4% 127 / 225 CC 43 SCAR 35.6
CC
43
Cov
63%
new
61.1
get_lock
36.0
__ANON__
6.0
CRAP 35.0
↑ Summary ConcurrencyLimit/WithStandby.pm »
1 package IPC::ConcurrencyLimit::WithLatestStandby;
2 3 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.17';
7  
8 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Time::HiRes qw(sleep time);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IPC::ConcurrencyLimit;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶sub new {
Subroutine new: called CC=22 SCAR=41.1
Pod undocumented
13 5 my $class = shift;
14 5 my %params = @_;
15 5 my $type = delete $params{type};
16 5 ▶ $type = 'Flock' if not defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 5 0 5
17 5 ▶ croak( __PACKAGE__ . " only supports 'Flock' for now")
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "Flock" 0 5 5
18 if $type ne 'Flock';
19  
20 5 ▶ if (defined $params{max_procs} and $params{max_procs}!=1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"max_procs"} and $params{"max_procs"} != 1) 0 5 5
Condition33%1/3
defined $params{"max_procs"} and $params{"max_procs"} != 1
!ll&&!rl&&r
500
MC/DC0%0/2
defined $params{"max_procs"} and $params{"max_procs"} != 1
defined $params{"max_procs"} $params{"max_procs"} != 1
Truth table33%1/3
A and B
A= defined $params{"max_procs"}B= $params{"max_procs"} != 1
ABresult
0X0
100
111
21 0 croak( __PACKAGE__ . " does not support max_procs!=1, use multiple objects instead.");
22 }
23  
24 5 ▶ my $process_name_change= $params{process_name_change} || 0;
Condition50%1/2
$params{'process_name_change'} || 0
l!l
05
MC/DC0%0/1
$params{'process_name_change'} || 0
$params{'process_name_change'}
Truth table50%1/2
A
A= $params{'process_name_change'}
Aresult
11
00
25 5 ▶ my $path= $params{path} || die __PACKAGE__ . '->new: missing mandatory parameter `path`';
Condition50%1/2
$params{'path'} || die('IPC::ConcurrencyLimit::WithLatestStandby->new: missing mandatory parameter `path`')
l!l
50
MC/DC0%0/1
$params{'path'} || die('IPC::ConcurrencyLimit::WithLatestStandby->new: missing mandatory parameter `path`')
$params{'path'}
Truth table50%1/2
A
A= $params{'path'}
Aresult
11
00
26 5 ▶ my $file_prefix= $params{file_prefix} || "";
Condition50%1/2
$params{'file_prefix'} || ''
l!l
05
MC/DC0%0/1
$params{'file_prefix'} || ''
$params{'file_prefix'}
Truth table50%1/2
A
A= $params{'file_prefix'}
Aresult
11
00
27 5 ▶ my $poll_time= $params{poll_time} || $params{interval} || 1; # seconds to poll (may be fraction)
Condition40%2/5
$params{'poll_time'} || $params{'interval'} || 1
l!l
50
$params{"poll_time"} or $params{"interval"}
l!l&&r!l&&!r
500
MC/DC0%0/2
$params{'poll_time'} || $params{'interval'} || 1
$params{"poll_time"} $params{"interval"}
Truth table33%1/3
A or B
A= $params{"poll_time"}B= $params{"interval"}
ABresult
1X1
011
000
28 5 ▶ my $retries= $params{retries} || undef;
Condition50%1/2
$params{'retries'} || undef
l!l
05
MC/DC0%0/1
$params{'retries'} || undef
$params{'retries'}
Truth table50%1/2
A
A= $params{'retries'}
Aresult
11
00
29 5 ▶ my $timeout= $params{timeout} || undef;
Condition50%1/2
$params{'timeout'} || undef
l!l
05
MC/DC0%0/1
$params{'timeout'} || undef
$params{'timeout'}
Truth table50%1/2
A
A= $params{'timeout'}
Aresult
11
00
30 5 ▶ my $debug= $params{debug} || 0; # show debug?
Condition50%1/2
$params{'debug'} || 0
l!l
50
MC/DC0%0/1
$params{'debug'} || 0
$params{'debug'}
Truth table50%1/2
A
A= $params{'debug'}
Aresult
11
00
31 5 ▶ my $debug_sub= $params{debug_sub} || undef;
Condition50%1/2
$params{'debug_sub'} || undef
l!l
50
MC/DC0%0/1
$params{'debug_sub'} || undef
$params{'debug_sub'}
Truth table50%1/2
A
A= $params{'debug_sub'}
Aresult
11
00
32  
33 my $retry_sub= (ref $retries) ? $retries :
34 (defined $retries &&
35 0 ▶ defined $timeout) ? sub { $_[0] <= $retries && $_[2] <= $timeout } :
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] <= $retries and $_[2] <= $timeout 0 0 0
36 0 ▶ (defined $retries) ? sub { $_[0] <= $retries } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 ▶ (defined $timeout) ? sub { $_[2] <= $timeout } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 5 ▶ sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.9
Branch: 4 branches
BranchTrueFalseTotal
ref $retries ? : 0 5 5
defined $retries && defined $timeout ? : 0 5 5
defined $retries ? : 0 5 5
defined $timeout ? : 0 5 5
Condition33%1/3
defined $retries and defined $timeout
!ll&&!rl&&r
500
MC/DC0%0/2
defined $retries and defined $timeout
defined $retries defined $timeout
Truth table33%1/3
A and B
A= defined $retriesB= defined $timeout
ABresult
0X0
100
111
39  
40 # primary is replace by standby1, is replaced by
41 # standby2, is replaced by standby3. However, standby1
42 # will exit when standby2's lock is held by another process.
43 5 ▶ my @names= map { ($file_prefix ? "$file_prefix.$_" : $_) }
Branch: 1 branch
BranchTrueFalseTotal
$file_prefix ? : 0 20 20
44 qw(primary standby1 standby2 standby3);
45 my @lockers= map {
46 5 ▶ IPC::ConcurrencyLimit->new(
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'Flock' ? : 20 0 20
47 type => $type,
48 max_procs => 1,
49 # future proofing
50 $type eq "Flock" ? (
51 file_prefix => $_,
52 path => $path,
53 ) : (),
54 )
55 } @names;
56  
57 return bless {
58 poll_time => $poll_time,
59 timeout => $timeout, # FYI
60 retries => $retries, # FYI
61 lock_name => \@names,
62 locker => \@lockers,
63 debug => $debug,
64 0 ▶ debug_sub => $debug_sub || sub { warn @_,"\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 5 ▶ retry_sub => $retry_sub,
Condition33%1/3
$debug_sub || sub { warn @_, "\n"; }
l!l&&r!l&&!r
500
MC/DC0%0/2
$debug_sub || sub { warn @_, "\n"; }
$debug_sub sub { warn @_, "\n"; }
Truth table33%1/3
A || B
A= $debug_subB= sub { warn @_, "\n"; }
ABresult
1X1
011
000
66 process_name_change => $process_name_change,
67 }, $class;
68 }
69  
70 24 ▶sub _diag {
Subroutine _diag: called CC=2 SCAR=7.6
71 24 my ($self, $fmt, @args)= @_;
72 24 ▶ if (!@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @args) { } 24 0 24
73 24 $self->{debug_sub}->($fmt);
74 } else {
75 0 $self->{debug_sub}->(sprintf $fmt, @args);
76 }
77 }
78  
79  
80 5 ▶sub get_lock {
Subroutine get_lock: called CC=20 SCAR=35.8
Pod undocumented
81 5 my ($self) = @_;
82  
83 5 my $locker= $self->{locker};
84 5 my $names= $self->{lock_name};
85  
86 5 my $old_oh= $0;
87  
88 $0 = "$old_oh - acquire"
89 5 ▶ if $self->{process_name_change};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"process_name_change"} 0 5 5
90  
91 # try to get the rightmost lock (standby3) if we don't get it
92 # then we exit out. this shouldn't really happen if other things
93 # are sane, for instance when $poll_time is much smaller than
94 # the rate we allocate new workers.
95 5 my $locker_id= $#$locker;
96 5 ▶ if ( $locker->[$locker_id]->get_lock() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locker->[$locker_id]->get_lock) { } 5 0 5
97 $self->_diag( "Got a $names->[$locker_id] lock")
98 5 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 5 0 5
99 } else {
100 $self->_diag( "Failed to get a $names->[$locker_id] lock, entry lock is held by another process" )
101 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
102 $0 = "$old_oh - no-lock-acquired"
103 0 ▶ if $self->{process_name_change};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"process_name_change"} 0 0 0
104 0 return;
105 }
106  
107 # Each worker tries to acquire the lock to its left. If it does
108 # then it abandons its old lock. If that means the worker ends up
109 # on locker_id 0 then they are done, and can do work.
110 # The first standby worker also looks to its right to see if there
111 # is a replacement process for it, if there is it exits, leaving
112 # a gap and letting the replacements shuffle left.
113 5 my $tries= 0;
114 5 my $lock_tries= 0;
115 5 my $standby_start= time();
116 5 my $lock_start= time();
117 5 my $poll_time= $self->{poll_time};
118  
119 5 ▶ while ( $locker_id > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locker_id > 0) 29 4 33
120 $0 = "$old_oh - $names->[$locker_id]"
121 29 ▶ if $self->{process_name_change};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"process_name_change"} 0 29 29
122  
123 # can we shuffle our lock left?
124 29 ▶ if ( $locker->[$locker_id - 1]->get_lock() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$locker[$locker_id - 1]->get_lock) 14 15 29
125 $self->_diag( "Got a $names->[$locker_id -1] lock, dropping old $names->[$locker_id] lock")
126 14 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 14 0 14
127 # yep, we got the lock to the left, so drop our old lock,
128 # and move the pointer left at the same time.
129 14 $locker->[ $locker_id-- ]->release_lock();
130 14 $lock_tries= 0;
131 14 $lock_start= time();
132 14 next;
133 }
134  
135 15 ▶ unless ($self->{retry_sub}->(++$tries, ++$lock_tries, time - $standby_start, time - $lock_start)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"retry_sub"}->(++$tries, ++$lock_tries, &IPC::ConcurrencyLimit::WithLatestStandby::time() - $standby_start, &IPC::ConcurrencyLimit::WithLatestStandby::time() - $lock_start)) 0 15 15
136 $0 = "$old_oh - no-lock-timeout"
137 0 ▶ if $self->{process_name_change};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"process_name_change"} 0 0 0
138 0 return;
139 }
140  
141 # check if we are the first standby worker.
142 15 ▶ if ( $locker_id == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locker_id == 1) 12 3 15
143 # yep - we are the first standby worker,
144 # so check if the lock to our right is being held:
145 12 ▶ if ( $locker->[$locker_id + 1]->get_lock() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$locker[$locker_id + 1]->get_lock) { } 11 1 12
146 # we got the lock, which means nothing else
147 # holds it. so we release the lock and move on.
148 11 $locker->[$locker_id + 1]->release_lock();
149 } else {
150 $self->_diag(
151 "A newer worker is holding the $names->[$locker_id+1] lock, will exit to let it take over"
152 1 ▶ ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 1 0 1
153 # we failed to get the lock, which means there is a newer
154 # process that can replace us so return/exit - this frees up
155 # our lock and lets the newer process to move into our position.
156 $0 = "$old_oh - no-lock-retired"
157 1 ▶ if $self->{process_name_change};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"process_name_change"} 0 1 1
158 1 return;
159 }
160 }
161  
162 # nope - the lock to our left is being held so sleep a while before
163 # we try again. We use the rand and the formula so that items to the
164 # right poll faster than items to the left, and to reduce the chance
165 # that lock holder 1 and lock holder 3 poll lock 2 at the same time
166 # forever. The formula guarantees that items to the left poll faster,
167 # and the rand ensures there is jitter.
168 14 sleep rand(($poll_time / $locker_id)*2);
169 }
170  
171 # assert that $locker_id is 0 at this point.
172 4 ▶ die "panic: We should not reach this point with \$locker_id larger than 0, got $locker_id"
Branch: 1 branch
BranchTrueFalseTotal
if $locker_id 0 4 4
173 if $locker_id;
174  
175 $self->_diag("Got $names->[$locker_id] lock, we are allowed to do work.")
176 4 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 4 0 4
177  
178 # at this point we should be $locker_id == 0 and we can do work.
179 4 ▶ if ($self->{process_name_change}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"process_name_change"}) 0 4 4
180 0 ▶ if ($self->{process_name_change} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'process_name_change'} > 1) { } 0 0 0
181 0 $0 = $old_oh;
182 } else {
183 0 $0 = "$old_oh - $names->[$locker_id]"
184 }
185 }
186 4 return 1;
187 }
188  
189  
190 0 ▶sub is_locked {
Subroutine is_locked: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $self = shift;
192 0 return $self->{locker}[0]->is_locked(@_);
193 }
194  
195 0 ▶sub release_lock {
Subroutine release_lock: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $self = shift;
197 0 return $self->{locker}[0]->release_lock(@_);
198 }
199  
200 0 ▶sub lock_id {
Subroutine lock_id: not called CC=1 SCAR=6.9
Pod undocumented
201 0 my $self = shift;
202 0 return $self->{locker}[0]->lock_id(@_);
203 }
204  
205 0 ▶sub heartbeat {
Subroutine heartbeat: not called CC=1 SCAR=6.9
Pod undocumented
206 0 my $self = shift;
207 0 return $self->{locker}[0]->heartbeat;
208 }
209  
210  
211 3 1;
212  
213 __END__
↑ Summary ConcurrencyLimit/WithStandby.pm »