Scoreboard.pm

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