| 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
| ||||||||||||||||||||||||||||||||
| 23 | # create base_dir if necessary | |||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | if (! -e $args{base_dir}) { | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 25 | mkpath $args{base_dir} | |||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | or -e $args{base_dir} or die "failed to create directory:$args{base_dir}:$!"; | |||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
mkpath($args{"base_dir"}) or -e $args{"base_dir"}
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"}
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->{"fh"} and $self->{"id_for_fh"} ne $id
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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 59 | 2 | my $fn = $self->_build_filename(); | ||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | open my $fh, '>', "$fn.tmp" | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 61 | or die "failed to open file:$fn.tmp:$!"; | |||||||||||||||||||||||||||||||
| 62 | 2 | autoflush $fh 1; | ||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | flock $fh, LOCK_EX | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 64 | or die "failed to flock file:$fn.tmp:$!"; | |||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | rename "$fn.tmp", $fn | |||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$id ne $self->worker_id->() and flock $fh, 6
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
| ||||||||||||||||||||||||||||||||
| 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
Condition20%1/5
unlink($fn) || ($!{'ENOENT'} || warn("failed to remove an obsolete scoreboard file:${fn}:$!"))
$!{"ENOENT"} or warn "failed to remove an obsolete scoreboard file:${fn}:$!"
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"}
| ||||||||||||||||||||||||||||||||
| 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__ | |||||||||||||||||||||||||||||||