Client/Connection.pm

filter: Statementstmt 21.8% 49 / 224 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 39.0% 16 / 41 Podpod 0.0% 0 / 15 totaltotal 17.4% 65 / 372 CC 47 SCAR 39.4
CC
47
Cov
16%
process_packet
240.0
new
30.0
get_in_ready_state
30.0
CRAP 51.5
↑ Summary Client.pm »
1 package POE::Component::Gearman::Client::Connection;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 'state', # one of 3 state constants below
8 'waiting', # hashref of $handle -> [ Task+ ]
9 'need_handle', # arrayref of Gearman::Task objects which
10 # have been submitted but need handles.
11 'parser', # parser object
12 'hostspec', # scalar: "host:ip"
13 'deadtime', # unixtime we're marked dead until.
14 'task2handle', # hashref of stringified Task -> scalar handle
15 'on_ready', # arrayref of on_ready callbacks to run on connect success
16 'on_error', # arrayref of on_error callbacks to run on connect failure
17 'poe_session_id', # POE::Component::Client::TCP session ID
18 2 );
19 2 ▶use Gearman::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Gearman::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use POE qw(Component::Client::TCP Filter::Stream);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use constant S_DISCONNECTED => \"disconnected";
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use constant S_CONNECTING => \"connecting";
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use constant S_READY => \"ready";
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub DEBUGGING () { 0 }
29  
30 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
31 0 my __PACKAGE__ $self = shift;
32 0 my %opts = @_;
33
34 0 ▶ $self = fields::new($self) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
35
36 0 ▶ $self->{hostspec} = delete($opts{hostspec}) or croak("hostspec required");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"hostspec"} = delete $opts{"hostspec"} or croak("hostspec required") 0 0 0
37 0 ▶ croak("hostspec must be in host:port format") if ref $self->{hostspec};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"hostspec"} 0 0 0
38 # TODO: read timeout param
39
40 0 $self->{state} = S_DISCONNECTED;
41 0 $self->{waiting} = {};
42 0 $self->{need_handle} = [];
43 0 $self->{deadtime} = 0;
44 0 $self->{on_ready} = [];
45 0 $self->{on_error} = [];
46 0 $self->{task2handle} = {};
47
48 0 ▶ croak "Unknown parameters: " . join(", ", keys %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
49 0 return $self;
50 }
51  
52 0 ▶sub connect {
Subroutine connect: not called CC=2 SCAR=17.9
Pod undocumented
53 0 my __PACKAGE__ $self = shift;
54
55 0 $self->{state} = S_CONNECTING;
56
57 0 my ($host, $port) = split /:/, $self->{hostspec};
58 0 ▶ $port ||= 7003;
Condition0%0/2
$port ||= 7003
l!l
00
MC/DC0%0/1
$port ||= 7003
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
59 0 warn "Connecting to $self->{hostspec}\n" if DEBUGGING;
60
61 $self->{poe_session_id} = POE::Component::Client::TCP->new(
62 RemoteAddress => $host,
63 RemotePort => $port,
64 Filter => POE::Filter::Stream->new(),
65 0 ▶ Started => sub { $_[HEAP]{connection} = $self },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 Connected => \&_onConnect,
67 ConnectError => \&_onConnectError,
68 Disconnected => \&_onRead,
69 ServerInput => \&_onRead,
70 ServerError => \&_onError
71 );
72
73 0 $self->{parser} = Gearman::ResponseParser::Async->new($self);
74 }
75  
76 0 ▶sub get_session {
Subroutine get_session: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my __PACKAGE__ $self = shift;
78 0 return POE::Kernel->ID_id_to_session($self->{poe_session_id});
79 }
80  
81 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my __PACKAGE__ $self = shift;
83 0 $self->{state} = S_DISCONNECTED;
84 0 $self->_requeue_all;
85 0 POE::Kernel->post( $self->get_session, 'shutdown' );
86 }
87  
88 0 ▶sub add_task {
Subroutine add_task: not called CC=2 SCAR=17.9
Pod undocumented
89 0 my __PACKAGE__ $self = shift;
90 0 my Gearman::Task $task = shift;
91
92 Carp::confess("add_task called when in wrong state")
93 0 ▶ unless $self->{state} == S_READY;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} == \"ready" 0 0 0
94
95 0 warn "writing task $task to $self->{hostspec}\n" if DEBUGGING;
96
97 0 $self->write( $task->pack_submit_packet );
98 0 push @{$self->{need_handle}}, $task;
99 0 Scalar::Util::weaken($self->{need_handle}->[-1]);
100 }
101  
102 # copy-and-paste from Gearman::Client::Async::Connection code
103 0 ▶sub close_when_finished {
Subroutine close_when_finished: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my __PACKAGE__ $self = shift;
105 # FIXME: implement
106 }
107  
108 # copy-and-paste from Gearman::Client::Async::Connection code
109 0 ▶sub hostspec {
Subroutine hostspec: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my __PACKAGE__ $self = shift;
111 0 return $self->{hostspec};
112 }
113  
114 # copy-and-paste from Gearman::Client::Async::Connection code
115 0 ▶sub get_in_ready_state {
Subroutine get_in_ready_state: not called CC=5 SCAR=34.0
Pod undocumented
116 0 my ($self, $on_ready, $on_error) = @_;
117
118 0 ▶ if ($self->{state} == S_READY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == \"ready") 0 0 0
119 0 $on_ready->();
120 0 return;
121 }
122  
123 0 ▶ push @{$self->{on_ready}}, $on_ready if $on_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $on_ready 0 0 0
124 0 ▶ push @{$self->{on_error}}, $on_error if $on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_error 0 0 0
125  
126 0 ▶ $self->connect if $self->{state} == S_DISCONNECTED;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} == \"disconnected" 0 0 0
127 }
128  
129 # copy-and-paste from Gearman::Client::Async::Connection code
130 0 ▶sub mark_dead {
Subroutine mark_dead: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my __PACKAGE__ $self = shift;
132 0 $self->{deadtime} = time + 10;
133 0 warn "$self->{hostspec} marked dead for a bit." if DEBUGGING;
134 }
135  
136 # copy-and-paste from Gearman::Client::Async::Connection code
137 0 ▶sub alive {
Subroutine alive: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my __PACKAGE__ $self = shift;
139 0 return $self->{deadtime} <= time;
140 }
141  
142 # copy-and-paste from Gearman::Client::Async::Connection code
143 0 ▶sub destroy_callbacks {
Subroutine destroy_callbacks: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my __PACKAGE__ $self = shift;
145 0 $self->{on_ready} = [];
146 0 $self->{on_error} = [];
147 }
148  
149 # copy-and-paste from Gearman::Client::Async::Connection code
150 0 ▶sub stuff_outstanding {
Subroutine stuff_outstanding: not called CC=2 SCAR=17.9
Pod undocumented
151 0 my __PACKAGE__ $self = shift;
152 return
153 0 @{$self->{need_handle}} ||
154 0 ▶ %{$self->{waiting}};
Condition0%0/3
@{$self->{'need_handle'};} || %{$self->{'waiting'};}
l!l&&r!l&&!r
000
MC/DC0%0/2
@{$self->{'need_handle'};} || %{$self->{'waiting'};}
@{$self->{'need_handle'};} %{$self->{'waiting'};}
Truth table0%0/3
A || B
A= @{$self->{'need_handle'};}B= %{$self->{'waiting'};}
ABresult
1X1
011
000
155 }
156  
157 # copy-and-paste from Gearman::Client::Async::Connection code
158 0 ▶sub _requeue_all {
Subroutine _requeue_all: not called CC=5 SCAR=34.0
159 0 my __PACKAGE__ $self = shift;
160  
161 0 my $need_handle = $self->{need_handle};
162 0 my $waiting = $self->{waiting};
163  
164 0 $self->{need_handle} = [];
165 0 $self->{waiting} = {};
166  
167 0 ▶ while (@$need_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$need_handle) 0 0 0
168 0 my $task = shift @$need_handle;
169 0 warn "Task $task in need_handle queue during socket error, queueing for redispatch\n" if DEBUGGING;
170 0 ▶ $task->fail if $task;
Branch: 1 branch
BranchTrueFalseTotal
if $task 0 0 0
171 }
172  
173 0 ▶ while (my ($shandle, $tasklist) = each( %$waiting )) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$waiting) 0 0 0
174 0 foreach my $task (@$tasklist) {
175 0 warn "Task $task ($shandle) in waiting queue during socket error, queueing for redispatch\n" if DEBUGGING;
176 0 $task->fail;
177 }
178 }
179 }
180  
181 # copy-and-paste from Gearman::Client::Async::Connection code
182 0 ▶sub process_packet {
Subroutine process_packet: not called CC=15 SCAR=54.8
Pod undocumented
183 0 my __PACKAGE__ $self = shift;
184 0 my $res = shift;
185  
186 0 warn "Got packet '$res->{type}' from $self->{hostspec}\n" if DEBUGGING;
187  
188 0 ▶ if ($res->{type} eq "job_created") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"type"} eq "job_created") 0 0 0
189  
190 0 ▶ die "Um, got an unexpected job_created notification" unless @{ $self->{need_handle} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"need_handle"};} 0 0 0
191 0 ▶ my Gearman::Task $task = shift @{ $self->{need_handle} } or
Branch: 1 branch
BranchTrueFalseTotal
my Gearman::Task $task = shift @{$$self{"need_handle"};} or return 1 0 0 0
192 return 1;
193  
194  
195 0 my $shandle = ${ $res->{'blobref'} };
196 0 ▶ if ($task) {
Branch: 1 branch
BranchTrueFalseTotal
if ($task) 0 0 0
197 0 $self->{task2handle}{"$task"} = $shandle;
198 0 ▶ push @{ $self->{waiting}->{$shandle} ||= [] }, $task;
Condition0%0/2
$self->{"waiting"}{$shandle} ||= []
l!l
00
MC/DC0%0/1
$self->{"waiting"}{$shandle} ||= []
$self->{"waiting"}{$shandle}
Truth table0%0/2
A
A= $self->{"waiting"}{$shandle}
Aresult
11
00
199 }
200 0 return 1;
201 }
202  
203 0 ▶ if ($res->{type} eq "work_fail") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"type"} eq "work_fail") 0 0 0
204 0 my $shandle = ${ $res->{'blobref'} };
205 0 $self->_fail_jshandle($shandle);
206 0 return 1;
207 }
208  
209 0 ▶ if ($res->{type} eq "work_complete") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"type"} eq "work_complete") 0 0 0
210 0 ▶ ${ $res->{'blobref'} } =~ s/^(.+?)\0//
Branch: 1 branch
BranchTrueFalseTotal
${$$res{"blobref"};} =~ s/^(.+?)\0// or die "Bogus work_complete from server" 0 0 0
211 or die "Bogus work_complete from server";
212 0 my $shandle = $1;
213  
214 0 ▶ my $task_list = $self->{waiting}{$shandle} or
Branch: 1 branch
BranchTrueFalseTotal
my $task_list = $self->{"waiting"}{$shandle} or return 0 0 0
215 return;
216  
217 0 ▶ my Gearman::Task $task = shift @$task_list or
Branch: 1 branch
BranchTrueFalseTotal
my Gearman::Task $task = shift @$task_list or return 0 0 0
218 return;
219  
220 0 $task->complete($res->{'blobref'});
221  
222 0 ▶ unless (@$task_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$task_list) 0 0 0
223 0 delete $self->{waiting}{$shandle};
224 0 delete $self->{task2handle}{"$task"};
225 }
226  
227 0 warn "Jobs: " . scalar( keys( %{$self->{waiting}} ) ) . "\n" if DEBUGGING;
228  
229 0 return 1;
230 }
231  
232 0 ▶ if ($res->{type} eq "work_status") {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"type"} eq "work_status") 0 0 0
233 0 my ($shandle, $nu, $de) = split(/\0/, ${ $res->{'blobref'} });
234  
235 0 ▶ my $task_list = $self->{waiting}{$shandle} or
Branch: 1 branch
BranchTrueFalseTotal
my $task_list = $self->{"waiting"}{$shandle} or return 0 0 0
236 return;
237  
238 0 foreach my Gearman::Task $task (@$task_list) {
239 0 $task->status($nu, $de);
240 }
241  
242 0 return 1;
243 }
244  
245 0 die "Unknown/unimplemented packet type: $res->{type}";
246  
247 }
248  
249 # copy-and-paste from Gearman::Client::Async::Connection code
250 0 ▶sub give_up_on {
Subroutine give_up_on: not called CC=4 SCAR=30.0
Pod undocumented
251 0 my __PACKAGE__ $self = shift;
252 0 my $task = shift;
253  
254 0 ▶ my $shandle = $self->{task2handle}{"$task"} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $shandle = $self->{"task2handle"}{"$task"} or return 0 0 0
255 0 ▶ my $task_list = $self->{waiting}{$shandle} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $task_list = $self->{"waiting"}{$shandle} or return 0 0 0
256 0 @$task_list = grep { $_ != $task } @$task_list;
257 0 ▶ unless (@$task_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$task_list) 0 0 0
258 0 delete $self->{waiting}{$shandle};
259 }
260  
261 }
262  
263 # copy-and-paste from Gearman::Client::Async::Connection code
264 # note the failure of a task given by its jobserver-specific handle
265 0 ▶sub _fail_jshandle {
Subroutine _fail_jshandle: not called CC=4 SCAR=30.0
266 0 my __PACKAGE__ $self = shift;
267 0 my $shandle = shift;
268  
269 0 ▶ my $task_list = $self->{waiting}->{$shandle} or
Branch: 1 branch
BranchTrueFalseTotal
my $task_list = $self->{"waiting"}{$shandle} or return 0 0 0
270 return;
271  
272 0 ▶ my Gearman::Task $task = shift @$task_list or
Branch: 1 branch
BranchTrueFalseTotal
my Gearman::Task $task = shift @$task_list or return 0 0 0
273 return;
274  
275 # cleanup
276 0 ▶ unless (@$task_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$task_list) 0 0 0
277 0 delete $self->{task2handle}{"$task"};
278 0 delete $self->{waiting}{$shandle};
279 }
280  
281 0 $task->fail;
282 }
283  
284 0 ▶sub write {
Subroutine write: not called CC=2 SCAR=17.9
Pod undocumented
285 0 my $self = shift;
286 0 my $input = shift;
287 0 my $heap = $self->get_session->get_heap;
288 0 ▶ croak("writing to non-connected socket") unless $heap->{connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $heap->{"connected"} 0 0 0
289 0 $heap->{server}->put($input);
290 }
291  
292 0 ▶sub _onConnect {
Subroutine _onConnect: not called CC=3 SCAR=24.8
293 0 my $self = $_[HEAP]{connection};
294
295 0 ▶ if ($self->{state} == S_CONNECTING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == \"connecting") 0 0 0
296 0 $self->{state} = S_READY;
297 0 warn "$self->{hostspec} connected and ready.\n" if DEBUGGING;
298 0 $_->() foreach @{$self->{on_ready}};
299 0 $self->destroy_callbacks;
300 }
301 }
302  
303 0 ▶sub _onConnectError {
Subroutine _onConnectError: not called CC=2 SCAR=17.9
304 0 my $self = $_[HEAP]{connection};
305 0 warn "Jobserver, $self->{hostspec} ($self) has failed to connect properly\n" if DEBUGGING;
306
307 0 $self->mark_dead;
308 0 $self->close;
309 0 $_->() foreach @{$self->{on_error}};
310 0 $self->destroy_callbacks;
311 }
312  
313 0 ▶sub _onError {
Subroutine _onError: not called CC=4 SCAR=30.0
314 0 my $self = $_[HEAP]{connection};
315 0 my $was_connecting = ($self->{state} == S_CONNECTING);
316  
317 0 ▶ if ($was_connecting && $self->{t_offline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_connecting and $self->{"t_offline"}) 0 0 0
Condition0%0/3
$was_connecting and $self->{"t_offline"}
!ll&&!rl&&r
000
MC/DC0%0/2
$was_connecting and $self->{"t_offline"}
$was_connecting $self->{"t_offline"}
Truth table0%0/3
A and B
A= $was_connectingB= $self->{"t_offline"}
ABresult
0X0
100
111
318 0 return;
319 }
320  
321 0 $self->mark_dead;
322 0 $self->close;
323 0 ▶ $self->on_connect_error if $was_connecting;
Branch: 1 branch
BranchTrueFalseTotal
if $was_connecting 0 0 0
324 }
325  
326 0 ▶sub _onRead {
Subroutine _onRead: not called CC=3 SCAR=24.8
327 0 my $self = $_[HEAP]{connection};
328
329 0 my $input = $_[ARG0]; # should we tell POE::Filter to buffer in chunks of 128 * 1024?
330 0 ▶ unless (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 0 0 0
331 0 ▶ $self->mark_dead if $self->stuff_outstanding;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stuff_outstanding 0 0 0
332 0 $self->close;
333 0 return;
334 }
335  
336 0 $self->{parser}->parse_data(\$input);
337 }
338  
339  
340 # copy-and-paste from Gearman::Client::Async::Connection code
341 package Gearman::ResponseParser::Async;
342  
343 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
344 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
345 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
346  
347 2 ▶use Gearman::ResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
348 2 ▶use base 'Gearman::ResponseParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
349  
350 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
351 0 my $class = shift;
352  
353 0 my $self = $class->SUPER::new;
354  
355 0 $self->{_conn} = shift;
356 0 weaken($self->{_conn});
357  
358 0 return $self;
359 }
360  
361 0 ▶sub on_packet {
Subroutine on_packet: not called CC=2 SCAR=17.9
362 0 my $self = shift;
363 0 my $packet = shift;
364  
365 0 ▶ return unless $self->{_conn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_conn"} 0 0 0
366 0 $self->{_conn}->process_packet( $packet );
367 }
368  
369 0 ▶sub on_error {
Subroutine on_error: not called CC=2 SCAR=17.9
370 0 my $self = shift;
371  
372 0 ▶ return unless $self->{_conn};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_conn"} 0 0 0
373 0 $self->{_conn}->mark_unsafe; # where's this?
374 0 $self->{_conn}->close;
375 }
376  
377 2 1;
↑ Summary Client.pm »