Scoreboard.pm

filter: Statementstmt 93.4% 86 / 92 Branchbran 55.5% 20 / 36 Conditioncond 35.7% 5 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 89.4% 17 / 19 Podpod 100% 4 / 4 totaltotal 77.4% 134 / 173 CC 25 SCAR 17.0
CC
25
Cov
78%
update
10.1
_for_all
8.5
new
7.2
CRAP 5.5
↑ Summary Scoreboard/PSGI/App.pm »
1 package Parallel::Scoreboard;
2  
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 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use POSIX qw(:fcntl_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.09';
13  
14 3 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ro => [ qw(base_dir worker_id) ],
16 3 );
17  
18 3 ▶sub new {
Subroutine new: called CC=5 SCAR=19.7
Pod documented
19 3 my $klass = shift;
20 3 my %args = @_;
21 die "mandatory parameter:base_dir is missing"
22 3 ▶ unless $args{base_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"base_dir"} 0 3 3
23 # create base_dir if necessary
24 3 ▶ if (! -e $args{base_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $args{"base_dir"}) 0 3 3
25 mkpath $args{base_dir}
26 0 ▶ or -e $args{base_dir} or die "failed to create directory:$args{base_dir}:$!";
Branch: 1 branch
BranchTrueFalseTotal
mkpath($args{"base_dir"}) or -e $args{"base_dir"} or die qq[failed to create directory:$args{"base_dir"}:$!] 0 0 0
Condition0%0/3
mkpath($args{"base_dir"}) or -e $args{"base_dir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
mkpath($args{"base_dir"}) or -e $args{"base_dir"}
mkpath($args{"base_dir"}) -e $args{"base_dir"}
Truth table0%0/3
A or B
A= mkpath($args{"base_dir"})B= -e $args{"base_dir"}
ABresult
1X1
011
000
27 }
28 # build object
29 my $self = bless {
30 21 ▶ worker_id => sub { $$ },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 3 %args,
32 }, $klass;
33 # remove my status file, just in case
34 3 unlink $self->_build_filename();
35
36 3 return $self;
37 }
38  
39 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
40 3 my $self = shift;
41 # if file is open, close and unlink
42 3 ▶ if ($self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"}) 3 0 3
43 3 close $self->{fh};
44 # during global destruction we may already have lost this
45 3 ▶ unlink $self->_build_filename() if ($self->{base_dir});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base_dir"} 2 1 3
46 }
47 }
48  
49 3 ▶sub update {
Subroutine update: called CC=8 SCAR=23.2
Pod documented
50 3 my ($self, $status) = @_;
51 # open file at the first invocation (tmpfn => lock => rename)
52 3 my $id = $self->worker_id->();
53 3 ▶ if ($self->{fh} && $self->{id_for_fh} ne $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"id_for_fh"} ne $id) 0 3 3
Condition33%1/3
$self->{"fh"} and $self->{"id_for_fh"} ne $id
!ll&&!rl&&r
300
MC/DC0%0/2
$self->{"fh"} and $self->{"id_for_fh"} ne $id
$self->{"fh"} $self->{"id_for_fh"} ne $id
Truth table33%1/3
A and B
A= $self->{"fh"}B= $self->{"id_for_fh"} ne $id
ABresult
0X0
100
111
54 # fork? close but do not unlock
55 0 close $self->{fh};
56 0 undef $self->{fh};
57 }
58 3 ▶ unless ($self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 3 0 3
59 3 my $fn = $self->_build_filename();
60 3 ▶ open my $fh, '>', "$fn.tmp"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$fn.tmp" or die "failed to open file:$fn.tmp:$!" 3 0 3
61 or die "failed to open file:$fn.tmp:$!";
62 3 autoflush $fh 1;
63 3 ▶ flock $fh, LOCK_EX
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "failed to flock file:$fn.tmp:$!" 3 0 3
64 or die "failed to flock file:$fn.tmp:$!";
65 3 ▶ rename "$fn.tmp", $fn
Branch: 1 branch
BranchTrueFalseTotal
rename "$fn.tmp", $fn or die "failed to rename file:$fn.tmp to ${fn}:$!" 3 0 3
66 or die "failed to rename file:$fn.tmp to $fn:$!";
67 3 $self->{fh} = $fh;
68 3 $self->{id_for_fh} = $id;
69 }
70 # write to file with size of the status and its checksum
71 3 ▶ seek $self->{fh}, 0, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, 0, 0 or die "seek failed:$!" 3 0 3
72 or die "seek failed:$!";
73 3 print {$self->{fh}} (
74 md5($status),
75 pack("N", length $status),
76 $status,
77 );
78 }
79  
80 4 ▶sub read_all {
Subroutine read_all: called CC=1 SCAR=0.0
Pod documented
81 4 my $self = shift;
82 4 my %ret;
83 $self->_for_all(
84 9 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
85 9 my ($id, $fh) = @_;
86 # detect collision using md5
87 9 for (1..10) {
88 9 ▶ seek $fh, 0, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 0 or die "seek failed:$!" 9 0 9
89 or die "seek failed:$!";
90 9 my $data = do { local $/; join '', <$fh> };
91 # silently ignore if data is too short
92 9 ▶ return if length($data) < 16 + 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $data < 20 0 9 9
93 # parse input
94 9 my $md5 = substr($data, 0, 16);
95 9 my $size = unpack("N", substr($data, 16, 4));
96 9 my $status = substr($data, 20, $size);
97 # compare md5 to detect collision
98 next
99 9 ▶ if md5($status) ne $md5;
Branch: 1 branch
BranchTrueFalseTotal
if md5($status) ne $md5 0 9 9
100 # have read correct data, save and return
101 9 $ret{$id} = $status;
102 9 return;
103 }
104 # failed to read data in 10 consecutive attempts, bug?
105 0 warn "failed to read status of id:$id, skipping";
106 }
107 4 );
108 4 \%ret;
109 }
110  
111 0 ▶sub cleanup {
Subroutine cleanup: not called CC=1 SCAR=6.9
Pod documented
112 0 my $self = shift;
113 0 ▶ $self->_for_all(sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 }
115  
116 4 ▶sub _for_all {
Subroutine _for_all: called CC=8 SCAR=21.4
117 4 my ($self, $cb) = @_;
118 3 ▶ my @files = glob "$self->{base_dir}/status_*";
Subroutine BEGIN: called CC=1 SCAR=0.0
119 4 for my $fn (@files) {
120 # obtain id from filename (or else ignore);
121 # skip .tmp to avoid racing with update()
122 11 ▶ $fn =~ m|/status_(.*)(?<!\.tmp)$|
Branch: 1 branch
BranchTrueFalseTotal
$fn =~ m[/status_(.*)(?<!\.tmp)$] or next 10 1 11
123 or next;
124 10 my $id = $1;
125 # ignore files removed after glob but before open
126 10 ▶ open my $fh, '+<', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<", $fn or next 10 0 10
127 or next;
128 # check if the file is still opened by the owner process using flock
129 10 ▶ if ($id ne $self->worker_id->() && flock $fh, LOCK_EX | LOCK_NB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id ne $self->worker_id->() and flock $fh, 6) 1 9 10
Condition100%3/3
$id ne $self->worker_id->() and flock $fh, 6
!ll&&!rl&&r
451
MC/DC100%2/2
$id ne $self->worker_id->() and flock $fh, 6
$id ne $self->worker_id->() flock $fh, 6
Truth table100%3/3
A and B
A= $id ne $self->worker_id->()B= flock $fh, 6
ABresult
0X0
100
111
130 # the owner has died, remove status file
131 1 close $fh;
132 unlink $fn
133 or
134 not $!{ENOENT} and
135 1 ▶ warn "failed to remove an obsolete scoreboard file:$fn:$!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $fn or ($!{"ENOENT"} or warn "failed to remove an obsolete scoreboard file:${fn}:$!") 1 0 1
Condition20%1/5
unlink($fn) || ($!{'ENOENT'} || warn("failed to remove an obsolete scoreboard file:${fn}:$!"))
l!l
100
$!{"ENOENT"} or warn "failed to remove an obsolete scoreboard file:${fn}:$!"
l!l
00
MC/DC0%0/2
unlink($fn) || ($!{'ENOENT'} || warn("failed to remove an obsolete scoreboard file:${fn}:$!"))
unlink($fn) $!{"ENOENT"}
Truth table33%1/3
A || B
A= unlink($fn)B= $!{"ENOENT"}
ABresult
1X1
011
000
136 1 next;
137 }
138 # invoke
139 9 $cb->($id, $fh);
140 # close
141 9 close $fh;
142 }
143 }
144  
145 8 ▶sub _build_filename {
Subroutine _build_filename: called CC=1 SCAR=0.0
146 8 my $self = shift;
147 8 return "$self->{base_dir}/status_" . $self->worker_id->();
148 }
149  
150 3 1;
151 __END__
↑ Summary Scoreboard/PSGI/App.pm »