Util.pm

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