blib/lib/DBD/Gofer/Transport/Base.pm

filter: Statementstmt 88.5% 124 / 140 Branchbran 65.9% 62 / 94 Conditioncond 42.3% 11 / 26 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 73.6% 14 / 19 Podpod 12.5% 1 / 8 totaltotal 70.6% 214 / 303 CC 59 SCAR 21.4
CC
59
Cov
76%
transmit_request
18.2
__ANON__
10.0
_store_response_in_cache
8.4
CRAP 8.5
« blib/lib/DBD/Gofer/Transport/pipeone.pm ↑ Summary blib/lib/DBI/Util/_accessor.pm »
1 package DBD::Gofer::Transport::Base;
2  
3 # $Id: Base.pm 14120 2010-06-07 19:52:19Z H.Merijn $
4 #
5 # Copyright (c) 2007, Tim Bunce, Ireland
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9  
10 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 63 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 63 ▶use base qw(DBI::Gofer::Transport::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 63 our $VERSION = "0.014121";
16  
17 63 __PACKAGE__->mk_accessors(qw(
18 trace
19 go_dsn
20 go_url
21 go_policy
22 go_timeout
23 go_retry_hook
24 go_retry_limit
25 go_cache
26 cache_hit
27 cache_miss
28 cache_store
29 ));
30 63 __PACKAGE__->mk_accessors_using(make_accessor_autoviv_hashref => qw(
31 meta
32 ));
33  
34  
35 746 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
36 746 my ($class, $args) = @_;
37 746 $args->{$_} = 0 for (qw(cache_hit cache_miss cache_store));
38 746 ▶ $args->{keep_meta_frozen} ||= 1 if $args->{go_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"go_cache"} 6 740 746
Condition50%1/2
$args->{"keep_meta_frozen"} ||= 1
l!l
06
MC/DC0%0/1
$args->{"keep_meta_frozen"} ||= 1
$args->{"keep_meta_frozen"}
Truth table50%1/2
A
A= $args->{"keep_meta_frozen"}
Aresult
11
00
39 #warn "args @{[ %$args ]}\n";
40 746 return $class->SUPER::new($args);
41 }
42  
43  
44 746 ▶sub _init_trace { $ENV{DBD_GOFER_TRACE} || 0 }
Subroutine _init_trace: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DBD_GOFER_TRACE"} or 0 0 746 746
45  
46  
47 0 ▶sub new_response {
Subroutine new_response: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $self = shift;
49 0 return DBI::Gofer::Response->new(@_);
50 }
51  
52  
53 7242 ▶sub transmit_request {
Subroutine transmit_request: called CC=13 SCAR=29.0
Pod undocumented
54 7242 my ($self, $request) = @_;
55 7242 my $trace = $self->trace;
56 7242 my $response;
57  
58 7242 my ($go_cache, $request_cache_key);
59 7242 ▶ if ($go_cache = $self->{go_cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($go_cache = $self->{"go_cache"}) 18 7224 7242
60 $request_cache_key
61 = $request->{meta}{request_cache_key}
62 18 = $self->get_cache_key_for_request($request);
63 18 ▶ if ($request_cache_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request_cache_key) 18 0 18
64 18 my $frozen_response = eval { $go_cache->get($request_cache_key) };
65 18 ▶ if ($frozen_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frozen_response) 4 14 18
66 4 ▶ $self->_dump("cached response found for ".ref($request), $request)
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 4 4
67 if $trace;
68 4 $response = $self->thaw_response($frozen_response);
69 4 ▶ $self->trace_msg("transmit_request is returning a response from cache $go_cache\n")
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 4 4
70 if $trace;
71 4 ++$self->{cache_hit};
72 4 return $response;
73 }
74 14 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
75 14 ++$self->{cache_miss};
76 14 ▶ $self->trace_msg("transmit_request cache miss\n")
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 14 14
77 if $trace;
78 }
79 }
80  
81 7238 my $to = $self->go_timeout;
82 7318 ▶ my $transmit_sub = sub {
Subroutine __ANON__: called CC=7 SCAR=23.0
83 7318 ▶ $self->trace_msg("transmit_request\n") if $trace;
Branch: 1 branch
BranchTrueFalseTotal
if $trace 0 7318 7318
84 0 ▶ local $SIG{ALRM} = sub { die "TIMEOUT\n" } if $to;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $to 760 6558 7318
85  
86 7318 my $response = eval {
87 0 ▶ local $SIG{PIPE} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
88 0 ▶ my $extra = ($! eq "Broken pipe") ? "" : " ($!)";
Branch: 1 branch
BranchTrueFalseTotal
$! eq 'Broken pipe' ? : 0 0 0
89 0 die "Unable to send request: Broken pipe$extra\n";
90 7318 };
91 7318 ▶ alarm($to) if $to;
Branch: 1 branch
BranchTrueFalseTotal
if $to 760 6558 7318
92 7318 $self->transmit_request_by_transport($request);
93 };
94 7318 ▶ alarm(0) if $to;
Branch: 1 branch
BranchTrueFalseTotal
if $to 760 6558 7318
95  
96 7318 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7318 7318
97 0 ▶ return $self->transport_timedout("transmit_request", $to)
Branch: 1 branch
BranchTrueFalseTotal
if $@ eq "TIMEOUT\n" 0 0 0
98 if $@ eq "TIMEOUT\n";
99 0 return $self->new_response({ err => 1, errstr => $@ });
100 }
101  
102 7318 return $response;
103 7238 };
104  
105 7238 $response = $self->_transmit_request_with_retries($request, $transmit_sub);
106  
107 7238 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 7238 7238
108 0 my $frozen_response = delete $response->{meta}{frozen};
109 0 ▶ $self->_store_response_in_cache($frozen_response, $request_cache_key)
Branch: 1 branch
BranchTrueFalseTotal
if $request_cache_key 0 0 0
110 if $request_cache_key;
111 }
112  
113 7238 ▶ $self->trace_msg("transmit_request is returning a response itself\n")
Branch: 1 branch
BranchTrueFalseTotal
if $trace and $response 0 7238 7238
Condition33%1/3
$trace and $response
!ll&&!rl&&r
723800
MC/DC0%0/2
$trace and $response
$trace $response
Truth table33%1/3
A and B
A= $traceB= $response
ABresult
0X0
100
111
114 if $trace && $response;
115  
116 7238 ▶ return $response unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 7238 7238
117 7238 return ($response, $transmit_sub);
118 }
119  
120  
121 7318 ▶sub _transmit_request_with_retries {
Subroutine _transmit_request_with_retries: called CC=3 SCAR=11.8
122 7318 my ($self, $request, $transmit_sub) = @_;
123 7318 my $response;
124 7318 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$response and $self->response_needs_retransmit($request, $response) and do { do { $response = &$transmit_sub() } } 0 7318 7318
Condition33%1/3
$response and $self->response_needs_retransmit($request, $response)
!ll&&!rl&&r
731800
MC/DC0%0/2
$response and $self->response_needs_retransmit($request, $response)
$response $self->response_needs_retransmit($request, $response)
Truth table33%1/3
A and B
A= $responseB= $self->response_needs_retransmit($request, $response)
ABresult
0X0
100
111
125 7318 $response = $transmit_sub->();
126 } while ( $response && $self->response_needs_retransmit($request, $response) );
127 7318 return $response;
128 }
129  
130  
131 7238 ▶sub receive_response {
Subroutine receive_response: called CC=7 SCAR=20.0
Pod undocumented
132 7238 my ($self, $request, $retransmit_sub) = @_;
133 7238 my $to = $self->go_timeout;
134  
135 7321 ▶ my $receive_sub = sub {
Subroutine __ANON__: called CC=6 SCAR=19.2
136 7321 $self->trace_msg("receive_response\n");
137 0 ▶ local $SIG{ALRM} = sub { die "TIMEOUT\n" } if $to;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $to 760 6561 7321
138  
139 7321 my $response = eval {
140 7321 ▶ alarm($to) if $to;
Branch: 1 branch
BranchTrueFalseTotal
if $to 760 6561 7321
141 7321 $self->receive_response_by_transport($request);
142 };
143 7321 ▶ alarm(0) if $to;
Branch: 1 branch
BranchTrueFalseTotal
if $to 760 6561 7321
144  
145 7321 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7321 7321
146 0 ▶ return $self->transport_timedout("receive_response", $to)
Branch: 1 branch
BranchTrueFalseTotal
if $@ eq "TIMEOUT\n" 0 0 0
147 if $@ eq "TIMEOUT\n";
148 0 return $self->new_response({ err => 1, errstr => $@ });
149 }
150 7321 return $response;
151 7238 };
152  
153 7238 my $response;
154 7238 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$self->response_needs_retransmit($request, $response) and do { do { $response = &$receive_sub(); if ($self->response_needs_retransmit($request, $response)) { $response = $self->_transmit_request_with_retries($request, $retransmit_sub); $response ||= &$receive_sub(); } } } 3 7238 7241
155 7241 $response = $receive_sub->();
156 7241 ▶ if ($self->response_needs_retransmit($request, $response)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response_needs_retransmit($request, $response)) 80 7161 7241
157 80 $response = $self->_transmit_request_with_retries($request, $retransmit_sub);
158 80 ▶ $response ||= $receive_sub->();
Condition33%1/3
$response ||= &$receive_sub()
l!l&&r!l&&!r
0800
MC/DC0%0/2
$response ||= &$receive_sub()
$response &$receive_sub()
Truth table33%1/3
A ||= B
A= $responseB= &$receive_sub()
ABresult
1X1
011
000
159 }
160 } while ( $self->response_needs_retransmit($request, $response) );
161  
162 7238 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 7238 0 7238
163 7238 my $frozen_response = delete $response->{meta}{frozen};
164 7238 my $request_cache_key = $request->{meta}{request_cache_key};
165 $self->_store_response_in_cache($frozen_response, $request_cache_key)
166 7238 ▶ if $request_cache_key && $self->{go_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $request_cache_key and $self->{"go_cache"} 14 7224 7238
Condition66%2/3
$request_cache_key and $self->{"go_cache"}
!ll&&!rl&&r
7224014
MC/DC50%1/2
$request_cache_key and $self->{"go_cache"}
$request_cache_key $self->{"go_cache"}
Truth table66%2/3
A and B
A= $request_cache_keyB= $self->{"go_cache"}
ABresult
0X0
100
111
167 }
168  
169 7238 return $response;
170 }
171  
172  
173 3169 ▶sub response_retry_preference {
Subroutine response_retry_preference: called CC=8 SCAR=21.0
Pod documented
174 3169 my ($self, $request, $response) = @_;
175  
176 # give the user a chance to express a preference (or undef for default)
177 3169 ▶ if (my $go_retry_hook = $self->go_retry_hook) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $go_retry_hook = $self->go_retry_hook) 204 2965 3169
178 204 my $retry = $go_retry_hook->($request, $response, $self);
179 204 ▶ $self->trace_msg(sprintf "go_retry_hook returned %s\n",
Condition50%1/2
$retry // "undef"
l!l
2040
MC/DC0%0/1
$retry // "undef"
$retry
Truth table50%1/2
A
A= $retry
Aresult
11
00
180 $retry // 'undef');
181 204 ▶ return $retry if defined $retry;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry 204 0 204
182 }
183  
184 # This is the main decision point. We don't retry requests that got
185 # as far as executing because the error is probably from the database
186 # (not transport) so retrying is unlikely to help. But note that any
187 # severe transport error occurring after execute is likely to return
188 # a new response object that doesn't have the execute flag set. Beware!
189 2965 ▶ return 0 if $response->executed_flag_set;
Branch: 1 branch
BranchTrueFalseTotal
if $response->executed_flag_set 92 2873 2965
190  
191 2873 ▶ return 1 if ($response->errstr || '') =~ m/induced by DBI_GOFER_RANDOM/;
Branch: 1 branch
BranchTrueFalseTotal
if ($response->errstr || "") =~ /induced by DBI_GOFER_RANDOM/ 2661 212 2873
Condition50%1/2
$response->errstr || ''
l!l
28730
MC/DC0%0/1
$response->errstr || ''
$response->errstr
Truth table50%1/2
A
A= $response->errstr
Aresult
11
00
192  
193 212 ▶ return 1 if $request->is_idempotent; # i.e. is SELECT or ReadOnly was set
Branch: 1 branch
BranchTrueFalseTotal
if $request->is_idempotent 100 112 212
194  
195 112 return undef; # we couldn't make up our mind
196 }
197  
198  
199 14482 ▶sub response_needs_retransmit {
Subroutine response_needs_retransmit: called CC=8 SCAR=20.9
Pod undocumented
200 14482 my ($self, $request, $response) = @_;
201  
202 14482 ▶ my $err = $response->err
Branch: 1 branch
BranchTrueFalseTotal
my $err = $response->err or return 0 3169 11313 14482
203 or return 0; # nothing went wrong
204  
205 3169 my $retry = $self->response_retry_preference($request, $response);
206  
207 3169 ▶ if (!$retry) { # false or undef
Branch: 1 branch
BranchTrueFalseTotal
unless ($retry) 318 2851 3169
208 318 $self->trace_msg("response_needs_retransmit: response not suitable for retry\n");
209 318 return 0;
210 }
211  
212 # we'd like to retry but have we retried too much already?
213  
214 2851 my $retry_limit = $self->go_retry_limit;
215 2851 ▶ if (!$retry_limit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($retry_limit) 2736 115 2851
216 2736 $self->trace_msg("response_needs_retransmit: retries disabled (retry_limit not set)\n");
217 2736 return 0;
218 }
219  
220 115 my $request_meta = $request->meta;
221 115 ▶ my $retry_count = $request_meta->{retry_count} || 0;
Condition100%2/2
$request_meta->{'retry_count'} || 0
l!l
3877
MC/DC100%1/1
$request_meta->{'retry_count'} || 0
$request_meta->{'retry_count'}
Truth table100%2/2
A
A= $request_meta->{'retry_count'}
Aresult
11
00
222 115 ▶ if ($retry_count >= $retry_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry_count >= $retry_limit) 32 83 115
223 32 $self->trace_msg("response_needs_retransmit: $retry_count is too many retries\n");
224 # XXX should be possible to disable altering the err
225 32 $response->errstr(sprintf "%s (after %d retries by gofer)", $response->errstr, $retry_count);
226 32 return 0;
227 }
228  
229 # will retry now, do the admin
230 83 ++$retry_count;
231 83 $self->trace_msg("response_needs_retransmit: retry $retry_count\n");
232  
233 # hook so response_retry_preference can defer some code execution
234 # until we've checked retry_count and retry_limit.
235 83 ▶ if (ref $retry eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $retry eq "CODE") 0 83 83
236 0 ▶ $retry->($retry_count, $retry_limit)
Branch: 1 branch
BranchTrueFalseTotal
&$retry($retry_count, $retry_limit) and warn "should return false" 0 0 0
237 and warn "should return false"; # protect future use
238 }
239  
240 83 ++$request_meta->{retry_count}; # update count for this request object
241 83 ++$self->meta->{request_retry_count}; # update cumulative transport stats
242  
243 83 return 1;
244 }
245  
246  
247 0 ▶sub transport_timedout {
Subroutine transport_timedout: not called CC=2 SCAR=17.9
Pod undocumented
248 0 my ($self, $method, $timeout) = @_;
249 0 ▶ $timeout ||= $self->go_timeout;
Condition0%0/3
$timeout ||= $self->go_timeout
l!l&&r!l&&!r
000
MC/DC0%0/2
$timeout ||= $self->go_timeout
$timeout $self->go_timeout
Truth table0%0/3
A ||= B
A= $timeoutB= $self->go_timeout
ABresult
1X1
011
000
250 0 return $self->new_response({ err => 1, errstr => "DBD::Gofer $method timed-out after $timeout seconds" });
251 }
252  
253  
254 # return undef if we don't want to cache this request
255 # subclasses may use more specialized rules
256 18 ▶sub get_cache_key_for_request {
Subroutine get_cache_key_for_request: called CC=2 SCAR=7.0
Pod undocumented
257 18 my ($self, $request) = @_;
258  
259 # we only want to cache idempotent requests
260 # is_idempotent() is true if GOf_REQUEST_IDEMPOTENT or GOf_REQUEST_READONLY set
261 18 ▶ return undef if not $request->is_idempotent;
Branch: 1 branch
BranchTrueFalseTotal
unless $request->is_idempotent 0 18 18
262  
263 # XXX would be nice to avoid the extra freeze here
264 18 my $key = $self->freeze_request($request, undef, 1);
265  
266 #use Digest::MD5; warn "get_cache_key_for_request: ".Digest::MD5::md5_base64($key)."\n";
267  
268 18 return $key;
269 }
270  
271  
272 14 ▶sub _store_response_in_cache {
Subroutine _store_response_in_cache: called CC=7 SCAR=21.3
273 14 my ($self, $frozen_response, $request_cache_key) = @_;
274 my $go_cache = $self->{go_cache}
275 14 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $go_cache = $self->{"go_cache"} or return 14 0 14
276  
277 # new() ensures that enabling go_cache also enables keep_meta_frozen
278 14 ▶ warn "No meta frozen in response" if !$frozen_response;
Branch: 1 branch
BranchTrueFalseTotal
unless $frozen_response 0 14 14
279 14 ▶ warn "No request_cache_key" if !$request_cache_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $request_cache_key 0 14 14
280  
281 14 ▶ if ($frozen_response && $request_cache_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frozen_response and $request_cache_key) 14 0 14
Condition33%1/3
$frozen_response and $request_cache_key
!ll&&!rl&&r
0014
MC/DC0%0/2
$frozen_response and $request_cache_key
$frozen_response $request_cache_key
Truth table33%1/3
A and B
A= $frozen_responseB= $request_cache_key
ABresult
0X0
100
111
282 14 $self->trace_msg("receive_response added response to cache $go_cache\n");
283 14 eval { $go_cache->set($request_cache_key, $frozen_response) };
284 14 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
285 14 ++$self->{cache_store};
286 }
287 }
288  
289 63 1;
290  
291 __END__
« blib/lib/DBD/Gofer/Transport/pipeone.pm ↑ Summary blib/lib/DBI/Util/_accessor.pm »