Util.pm

filter: Statementstmt 48.0% 74 / 154 Branchbran 20.7% 17 / 82 Conditioncond 32.1% 9 / 28 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 89.4% 17 / 19 Podpod 100% 7 / 7 totaltotal 41.6% 128 / 307 CC 49 SCAR 46.6
CC
49
Cov
38%
read_res_packet
324.9
_read_sock
42.0
__ANON__
20.0
CRAP 105.6
« Worker.pm ↑ Summary Taskset.pm »
1 package Gearman::Util;
2 20 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 $Gearman::Util::VERSION = version->declare("2.004.015");
4  
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # for sake of _read_sock
9 20 ▶no warnings "recursion";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # man errno
12 # Resource temporarily unavailable
13 # (may be the same value as EWOULDBLOCK) (POSIX.1)
14 20 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use Scalar::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Gearman::Util - Utility functions for gearman distributed job system
22  
23 =head1 METHODS
24  
25 =cut
26  
27 sub DEBUG () {0}
28  
29 # I: to jobserver
30 # O: out of job server
31 # W: worker
32 # C: client of job server
33 # J: jobserver
34 20 our %cmd = (
35 1 => ['I', "can_do"], # from W: [FUNC]
36 2 => ['I', "cant_do"], # from W: [FUNC]
37 3 => ['I', "reset_abilities"], # from W: ---
38 4 => ['I', "pre_sleep"], # from W: ---
39 6 => ['O', "noop"], # J->W ---
40 7 => ['I', "submit_job"], # C->J FUNC[0]UNIQ[0]ARGS
41 8 => ['O', "job_created"], # J->C HANDLE
42 9 => ['I', "grab_job"], # W->J --
43 10 => ['O', "no_job"], # J->W --
44 11 => ['O', "job_assign"], # J->W HANDLE[0]FUNC[0]ARG
45 12 => ['IO', "work_status"], # W->J/C: HANDLE[0]NUMERATOR[0]DENOMINATOR
46 13 => ['IO', "work_complete"], # W->J/C: HANDLE[0]RES
47 14 => ['IO', "work_fail"], # W->J/C: HANDLE
48 15 => ['I', "get_status"], # C->J: HANDLE
49 16 => ['I', "echo_req"], # ?->J TEXT
50 17 => ['O', "echo_res"], # J->? TEXT
51 18 => ['I', "submit_job_bg"], # C->J " " " " "
52 19 => ['O', "error"], # J->? ERRCODE[0]ERR_TEXT
53 20 => ['O', "status_res"], # C->J: HANDLE[0]KNOWN[0]RUNNING[0]NUM[0]DENOM
54 21 => ['I', "submit_job_high"], # C->J FUNC[0]UNIQ[0]ARGS
55 22 => ['I', "set_client_id"], # W->J: [RANDOM_STRING_NO_WHITESPACE]
56 23 => ['I', "can_do_timeout"], # from W: FUNC[0]TIMEOUT
57  
58 # for worker to declare to the jobserver that this worker is only connected
59 # to one jobserver, so no polls/grabs will take place, and server is free
60 # to push "job_assign" packets back down.
61 24 => ['I', "all_yours"], # W->J ---
62 25 => ['IO', "work_exception"], # W->J/C: HANDLE[0]EXCEPTION
63 26 => ['I', "option_req"], # C->J: [OPT]
64 27 => ['O', "option_res"], # J->C: [OPT]
65 28 => ['IO', "work_data"], # W->J/C: HANDLE[0]RES
66 29 => ['IO', "work_warning"], # W->J/C: HANDLE[0]RES
67 32 => ['I', "submit_job_high_bg"], # C->J FUNC[0]UNIQ[0]ARGS
68 33 => ['I', "submit_job_low"], # C->J FUNC[0]UNIQ[0]ARGS
69 34 => ['I', "submit_job_low_bg"], # C->J FUNC[0]UNIQ[0]ARGS
70 );
71  
72 20 our %num; # name -> num
73 20 ▶while (my ($num, $ary) = each %cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Gearman::Util::cmd) 620 20 640
74 620 ▶ die if $num{ $ary->[1] };
Branch: 1 branch
BranchTrueFalseTotal
if $Gearman::Util::num{$ary->[1]} 0 620 620
75 620 $num{ $ary->[1] } = $num;
76 }
77  
78 =head2 cmd_name($num)
79  
80 B<return> cmd
81  
82 =cut
83  
84 39 ▶sub cmd_name {
Subroutine cmd_name: called CC=2 SCAR=7.1
Pod documented
85 39 my $num = shift;
86 39 my $c = $cmd{$num};
87 39 ▶ return $c ? $c->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 39 0 39
88 }
89  
90 =head2 pack_req_command($key, $arg)
91  
92 B<return> request string
93  
94 =cut
95  
96 75 ▶sub pack_req_command {
Subroutine pack_req_command: called CC=1 SCAR=0.0
Pod documented
97 75 return _pack_command("REQ", @_);
98 }
99  
100 =head2 pack_res_command($cmd, $arg)
101  
102 B<return> response string
103  
104 =cut
105  
106 64 ▶sub pack_res_command {
Subroutine pack_res_command: called CC=1 SCAR=0.0
Pod documented
107 64 return _pack_command("RES", @_);
108 }
109  
110 =head2 read_res_packet($sock, $err_ref, $timeout)
111  
112 B<return> undef on closed socket or malformed packet
113  
114 =cut
115  
116 1 ▶sub read_res_packet {
Subroutine read_res_packet: called CC=20 SCAR=57.8
Pod documented
117 1 warn " Entering read_res_packet" if DEBUG;
118 1 my $sock = shift;
119 1 my $err_ref = shift;
120 1 my $timeout = shift;
121 1 my $time_start = Time::HiRes::time();
122 1 ▶ unless (Scalar::Util::blessed($sock)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($sock)) 1 0 1
123 # for the sake of Gearman::Client::Async
124 # see https://github.com/p-alik/perl-Gearman/issues/37
125 1 ▶ (ref($sock) eq "GLOB") || die "provided value is not a blessed object";
Branch: 1 branch
BranchTrueFalseTotal
ref $sock eq "GLOB" or die "provided value is not a blessed object" 0 1 1
126 0 ▶ ($$sock && $$sock eq '*Gearman::Worker::$sock')
Branch: 1 branch
BranchTrueFalseTotal
$$sock and $$sock eq "*Gearman::Worker::\$sock" or die "provided value is not a GLOB of type Gearman::Worker::\$sock" 0 0 0
Condition0%0/3
$$sock and $$sock eq "*Gearman::Worker::\$sock"
!ll&&!rl&&r
000
MC/DC0%0/2
$$sock and $$sock eq "*Gearman::Worker::\$sock"
$$sock $$sock eq "*Gearman::Worker::\$sock"
Truth table0%0/3
A and B
A= $$sockB= $$sock eq "*Gearman::Worker::\$sock"
ABresult
0X0
100
111
127 || die
128 "provided value is not a GLOB of type Gearman::Worker::\$sock";
129 } ## end unless (Scalar::Util::blessed...)
130  
131 0 ▶ my $err = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
132 0 my $code = shift;
133 0 ▶ Scalar::Util::blessed($sock) && $sock->close() if $sock->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $sock->connected 0 0 0
Condition0%0/6
$sock->connected && (builtin::blessed($sock) && $sock->close)
!ll&&!rl&&r
000
builtin::blessed($sock) and $sock->close
!ll&&!rl&&r
000
MC/DC0%0/3
$sock->connected && (builtin::blessed($sock) && $sock->close)
$sock->connected builtin::blessed($sock) $sock->close
Truth table0%0/4
A && B and C
A= $sock->connectedB= builtin::blessed($sock)C= $sock->close
ABCresult
0XX0
10X0
1100
1111
134 0 ▶ $$err_ref = $code if ref $err_ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err_ref 0 0 0
135 0 return undef;
136 0 };
137  
138 0 $sock->blocking(0);
139  
140 0 my $is = IO::Select->new($sock);
141  
142 0 my $readlen = 12;
143 0 my $offset = 0;
144 0 my $buf = '';
145 0 my $using_ssl = $sock->isa("IO::Socket::SSL");
146  
147 0 my ($magic, $type, $len);
148  
149 0 warn " Starting up event loop\n" if DEBUG;
150 0 while (1) {
151 0 ▶ if ($using_ssl && $sock->pending()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($using_ssl and $sock->pending) { } 0 0 0
Condition0%0/3
$using_ssl and $sock->pending
!ll&&!rl&&r
000
MC/DC0%0/2
$using_ssl and $sock->pending
$using_ssl $sock->pending
Truth table0%0/3
A and B
A= $using_sslB= $sock->pending
ABresult
0X0
100
111
152 0 warn " We have @{[ $sock->pending() ]} bytes...\n" if DEBUG;
153 }
154 else {
155 0 my $time_remaining = undef;
156 0 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 0 0 0
157 0 warn " We have a timeout of $timeout\n" if DEBUG;
158 0 $time_remaining = $time_start + $timeout - Time::HiRes::time();
159 0 ▶ return $err->("timeout") if $time_remaining < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $time_remaining < 0 0 0 0
160 }
161  
162 0 ▶ $is->can_read($time_remaining) || next;
Branch: 1 branch
BranchTrueFalseTotal
$is->can_read($time_remaining) or next 0 0 0
163 } ## end else [ if ($using_ssl && $sock...)]
164 0 warn " Entering read loop\n" if DEBUG;
165  
166 0 my ($ok, $err_code) = _read_sock($sock, \$buf, \$readlen, \$offset);
167 0 ▶ if (!defined($ok)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ok) { } 0 0 0
elsif ($ok == 0) { } 0 0 0
168 0 next;
169 }
170 elsif ($ok == 0) {
171 0 return $err->($err_code);
172 }
173  
174 0 ▶ if (!defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
175 0 ▶ next unless length($buf) >= 12;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buf >= 12 0 0 0
176 0 my $header = substr($buf, 0, 12, '');
177 0 ($magic, $type, $len) = unpack("a4NN", $header);
178 0 ▶ return $err->("malformed_magic: '$magic'") unless $magic eq "\0RES";
Branch: 1 branch
BranchTrueFalseTotal
unless $magic eq "\0RES" 0 0 0
179 0 my $starting = length($buf);
180 0 $readlen = $len - $starting;
181 0 $offset = $starting;
182  
183 0 ▶ if ($readlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readlen) 0 0 0
184 0 my ($ok, $err_code)
185 = _read_sock($sock, \$buf, \$readlen, \$offset);
186 0 ▶ if (!defined($ok)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ok) { } 0 0 0
elsif ($ok == 0) { } 0 0 0
187 0 next;
188 }
189 elsif ($ok == 0) {
190 0 return $err->($err_code);
191 }
192 } ## end if ($readlen)
193 } ## end if (!defined $type)
194  
195 0 $type = $cmd{$type};
196 0 ▶ return $err->("bogus_command") unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
197 0 ▶ return $err->("bogus_command_type") unless index($type->[0], "O") != -1;
Branch: 1 branch
BranchTrueFalseTotal
unless index($type->[0], "O") != -1 0 0 0
198  
199 0 warn " Fully formed res packet, returning; type=$type->[1] len=$len\n"
200 if DEBUG;
201  
202 0 $sock->blocking(1);
203  
204 return {
205 0 type => $type->[1],
206 len => $len,
207 blobref => \$buf,
208 };
209 } ## end while (1)
210 } ## end sub read_res_packet
211  
212 0 ▶sub _read_sock {
Subroutine _read_sock: not called CC=6 SCAR=37.4
213 0 my ($sock, $buf_ref, $readlen_ref, $offset_ref) = @_;
214 0 local $!;
215 0 my $rv = sysread($sock, $$buf_ref, $$readlen_ref, $$offset_ref);
216 0 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 0 0
217 0 warn " Read error: $!\n" if DEBUG;
218 0 ▶ $! == EAGAIN && return;
Branch: 1 branch
BranchTrueFalseTotal
$! == 11 and return 0 0 0
219 }
220  
221 0 ▶ return (0, "read_error") unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
222 0 ▶ return (0, "eof") unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
223  
224 0 ▶ unless ($rv >= $$readlen_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv >= $$readlen_ref) 0 0 0
225 0 warn
226 " Partial read of $rv bytes, at offset $$offset_ref, readlen was $$readlen_ref\n"
227 if DEBUG;
228 0 $$offset_ref += $rv;
229 0 $$readlen_ref -= $rv;
230  
231 0 $sock->blocking(1);
232 0 my $ret = _read_sock($sock, $buf_ref, $readlen_ref, $offset_ref);
233 0 $sock->blocking(0);
234 0 return $ret;
235 } ## end unless ($rv >= $$readlen_ref)
236  
237 0 warn " Finished reading\n" if DEBUG;
238 0 return (1);
239 } ## end sub _read_sock
240  
241 =head2 read_text_status($sock, $err_ref)
242  
243 =cut
244  
245 1 ▶sub read_text_status {
Subroutine read_text_status: called CC=6 SCAR=28.1
Pod documented
246 1 my $sock = shift;
247 1 my $err_ref = shift;
248  
249 1 ▶ my $err = sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
250 1 my $code = shift;
251 1 ▶ $sock->close() if $sock->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $sock->connected 0 1 1
252 1 ▶ $$err_ref = $code if ref $err_ref;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err_ref 1 0 1
253 1 return undef;
254 1 };
255  
256 1 ▶ $sock->connected || return $err->("can't read from unconnected socket");
Branch: 1 branch
BranchTrueFalseTotal
$sock->connected or return &$err("can't read from unconnected socket") 0 1 1
257 0 my @lines;
258 0 my $complete = 0;
259 0 ▶ while (my $line = <$sock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 0 0 0
260 0 chomp $line;
261 0 ▶ return $err->($1) if $line =~ /^ERR (\w+) /;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^ERR (\w+) / 0 0 0
262  
263 0 ▶ if ($line eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq ".") 0 0 0
264 0 $complete++;
265 0 last;
266 }
267  
268 0 push @lines, $line;
269 } ## end while (my $line = <$sock>)
270 0 ▶ return $err->("eof") unless $complete;
Branch: 1 branch
BranchTrueFalseTotal
unless $complete 0 0 0
271  
272 0 return @lines;
273 } ## end sub read_text_status
274  
275 =head2 send_req($sock, $reqref)
276  
277 =cut
278  
279 5 ▶sub send_req {
Subroutine send_req: called CC=7 SCAR=24.0
Pod documented
280 5 my ($sock, $reqref) = @_;
281 5 ▶ return 0 unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 3 2 5
282  
283 2 my $data = ${$reqref};
284 2 (my $total_len) = (my $len) = length($data);
285 2 my ($num_zero_writes, $offset) = (0, 0);
286 2 local $SIG{PIPE} = "IGNORE";
287  
288 2 ▶ while ($len && ($num_zero_writes < 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len and $num_zero_writes < 5) 2 0 2
Condition33%1/3
$len and $num_zero_writes < 5
!ll&&!rl&&r
002
MC/DC0%0/2
$len and $num_zero_writes < 5
$len $num_zero_writes < 5
Truth table33%1/3
A and B
A= $lenB= $num_zero_writes < 5
ABresult
0X0
100
111
289 2 my $written = $sock->syswrite($data, $len, $offset);
290 1 ▶ if (!defined $written) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $written) { } 1 0 1
elsif ($written > 0) { } 0 0 0
291 1 warn "send_req: syswrite error: $!" if DEBUG;
292 1 return 0;
293 }
294 elsif ($written > 0) {
295 0 $len -= $written;
296 0 $offset += $written;
297 }
298 else {
299 0 $num_zero_writes++;
300 }
301 } ## end while ($len && ($num_zero_writes...))
302  
303 0 ▶ return ($total_len > 0 && $offset == $total_len);
Condition0%0/3
$total_len > 0 && $offset == $total_len
!ll&&!rl&&r
000
MC/DC0%0/2
$total_len > 0 && $offset == $total_len
$total_len > 0 $offset == $total_len
Truth table0%0/3
A && B
A= $total_len > 0B= $offset == $total_len
ABresult
0X0
100
111
304 } ## end sub send_req
305  
306 =head2 wait_for_readability($fileno, $timeout)
307  
308 given a file descriptor number and a timeout,
309  
310 wait for that descriptor to become readable
311  
312 B<return> 0 or 1 on if it did or not
313  
314 =cut
315  
316 1 ▶sub wait_for_readability {
Subroutine wait_for_readability: called CC=4 SCAR=15.0
Pod documented
317 1 my ($fileno, $timeout) = @_;
318 1 ▶ return 0 unless $fileno && $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $fileno and $timeout 0 1 1
Condition33%1/3
$fileno and $timeout
!ll&&!rl&&r
001
MC/DC0%0/2
$fileno and $timeout
$fileno $timeout
Truth table33%1/3
A and B
A= $filenoB= $timeout
ABresult
0X0
100
111
319  
320 1 my $rin = '';
321 1 vec($rin, $fileno, 1) = 1;
322 1 my $nfound = select($rin, undef, undef, $timeout);
323  
324 # nfound can be undef or 0, both failures, or 1, a success
325 1 ▶ return $nfound ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$nfound ? : 0 1 1
326 } ## end sub wait_for_readability
327  
328 #
329 # _pack_command($prefix, $key, $arg)
330 #
331 139 ▶sub _pack_command {
Subroutine _pack_command: called CC=5 SCAR=16.1
332 139 my ($prefix, $key, $arg) = @_;
333 139 ▶ ($key && $num{$key}) || die sprintf("Bogus type arg of '%s'", $key || '');
Branch: 1 branch
BranchTrueFalseTotal
$key and $Gearman::Util::num{$key} or die sprintf("Bogus type arg of '%s'", $key || "") 135 4 139
Condition100%5/5
$key and $Gearman::Util::num{$key}
!ll&&!rl&&r
22135
$key || ''
l!l
22
MC/DC100%2/2
$key and $Gearman::Util::num{$key}
$key $Gearman::Util::num{$key}
MC/DC100%1/1
$key || ''
$key
Truth table100%3/3
A and B
A= $keyB= $Gearman::Util::num{$key}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $key
Aresult
11
00
334  
335 135 ▶ $arg ||= '';
Condition100%2/2
$arg ||= ""
l!l
7164
MC/DC100%1/1
$arg ||= ""
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
336 135 my $len = length($arg);
337 135 return "\0$prefix" . pack("NN", $num{$key}, $len) . $arg;
338 } ## end sub _pack_command
339  
340 20 1;
« Worker.pm ↑ Summary Taskset.pm »