blib/lib/DBIO/Async/Storage.pm

filter: Statementstmt 98.1% 537 / 547 Branchbran 78.7% 181 / 230 Conditioncond 66.3% 130 / 196 MC/DCmcdc 47.6% 51 / 107 Subroutinesub 100% 117 / 117 Podpod 100% 4 / 4 totaltotal 84.9% 1020 / 1201 CC 194 SCAR 17.7
CC
194
Cov
87%
_broker_answer_connect_info
15.1
_ready_end
13.9
_statement_timer_arm
12.7
CRAP 5.9
↑ Summary blib/lib/DBIO/Async/Future.pm »
1 package DBIO::Async::Storage;
2 # ABSTRACT: Future::IO transport backend for DBIO async storage
3  
4 44 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 44 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 44 our $VERSION = '0.900007';
8  
9 44 ▶use base 'DBIO::Storage::Async';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 44 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 44 ▶use Scalar::Util qw( blessed looks_like_number refaddr reftype weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 44 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 44 ▶use Future::IO qw( POLLIN );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 44 ▶use DBIO::Storage::DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 44 ▶use DBIO::Async::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 44 ▶use DBIO::Async::TransactionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 44 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21  
22 1537 ▶sub future_class { 'Future' }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
23  
24  
25 15 ▶sub transport_capabilities { qw(on_connect_replay) }
Subroutine transport_capabilities: called CC=1 SCAR=0.0
Pod documented
26  
27  
28 403 ▶sub pool {
Subroutine pool: called CC=3 SCAR=11.0
Pod documented
29 403 my $self = shift;
30 403 ▶ $self->{pool} ||= do {
Condition66%2/3
$self->{"pool"} ||= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
l!l&&r!l&&!r
1622410
MC/DC0%0/2
$self->{"pool"} ||= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
$self->{"pool"} do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
Truth table66%2/3
A ||= B
A= $self->{"pool"}B= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
ABresult
1X1
011
000
31 my %args = (
32 storage => $self,
33 size => $self->{_pool_size},
34 6 ▶ on_error => sub { warn "DBIO::Async pool error: $_[0]\n" },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 241 );
36  
37 241 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 52 189 241
38 52 $args{conninfo_provider} = $provider;
39 }
40 else {
41 189 $args{conninfo} = $self->{_conninfo};
42 }
43  
44 241 DBIO::Async::Pool->new(%args);
45 };
46 }
47  
48  
49 57 ▶sub _async_broker_conninfo {
Subroutine _async_broker_conninfo: called CC=3 SCAR=11.2
50 57 my ($self, $mode) = @_;
51  
52 57 ▶ croak sprintf '%s has no AccessBroker attached to ask for connect info',
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_broker 2 55 57
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
53 ref($self) || $self
54 unless $self->access_broker;
55  
56 55 my $info = $self->_broker_answer_connect_info(
57 $self->current_access_broker_connect_info($mode)
58 );
59 41 my ($conninfo) = $self->_normalize_async_connect_info(
60 $self->_normalize_conninfo($info)
61 );
62 41 return $conninfo;
63 }
64  
65 # A broker's answer as connect info for the driver's _normalize_conninfo: the
66 # DBI form, or the transport's own shape as it is (see _async_broker_conninfo).
67 51 ▶sub _broker_answer_connect_info {
Subroutine _broker_answer_connect_info: called CC=15 SCAR=27.1
68 51 my ($self, $answer) = @_;
69  
70 51 ▶ return $self->_broker_answer_dbi_form($answer)
Branch: 1 branch
BranchTrueFalseTotal
if ref $answer eq "ARRAY" and defined $answer->[0] and not ref $answer->[0] and $answer->[0] =~ /^dbi:/i 20 31 51
Condition88%8/9
ref $answer eq "ARRAY" and defined $answer->[0] and not ref $answer->[0] and $answer->[0] =~ /^dbi:/i
!ll&&!rl&&r
30120
ref $answer eq "ARRAY" and defined $answer->[0] and not ref $answer->[0]
!ll&&!rl&&r
28221
ref $answer eq "ARRAY" and defined $answer->[0]
!ll&&!rl&&r
28023
MC/DC75%3/4
ref $answer eq "ARRAY" and defined $answer->[0] and not ref $answer->[0] and $answer->[0] =~ /^dbi:/i
ref $answer eq "ARRAY" defined $answer->[0] not ref $answer->[0] $answer->[0] =~ /^dbi:/i
Truth table80%4/5
A and B and not(C) and D
A= ref $answer eq "ARRAY"B= defined $answer->[0]C= not ref $answer->[0]D= $answer->[0] =~ /^dbi:/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
71 if ref $answer eq 'ARRAY'
72 && defined $answer->[0] && !ref $answer->[0] && $answer->[0] =~ /^dbi:/i;
73  
74 31 ▶ if (ref $answer eq 'HASH' && !exists $answer->{dsn} && exists $answer->{dbname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $answer eq "HASH" and not exists $answer->{"dsn"} and exists $answer->{"dbname"}) 22 9 31
Condition100%6/6
ref $answer eq "HASH" and not exists $answer->{"dsn"} and exists $answer->{"dbname"}
!ll&&!rl&&r
8122
ref $answer eq "HASH" and not exists $answer->{"dsn"}
!ll&&!rl&&r
3523
MC/DC100%3/3
ref $answer eq "HASH" and not exists $answer->{"dsn"} and exists $answer->{"dbname"}
ref $answer eq "HASH" not exists $answer->{"dsn"} exists $answer->{"dbname"}
Truth table100%4/4
A and not(B) and C
A= ref $answer eq "HASH"B= not exists $answer->{"dsn"}C= exists $answer->{"dbname"}
ABCresult
0XX0
10X0
1100
1111
75 22 return $self->_broker_answer_dbi_form($self->_broker_params_dbi_form($answer));
76 }
77  
78 9 ▶ my $own = ref $answer eq 'HASH' && exists $answer->{dsn} ? [ $answer, {} ]
Branch: 2 branches
BranchTrueFalseTotal
ref $answer eq 'HASH' && exists $answer->{'dsn'} ? : 5 4 9
ref $answer eq 'ARRAY' && ref $answer->[0] eq 'HASH' ? : 2 2 4
Condition100%6/6
ref $answer eq "HASH" and exists $answer->{"dsn"}
!ll&&!rl&&r
315
ref $answer eq "ARRAY" and ref $answer->[0] eq "HASH"
!ll&&!rl&&r
112
MC/DC100%2/2
ref $answer eq "HASH" and exists $answer->{"dsn"}
ref $answer eq "HASH" exists $answer->{"dsn"}
MC/DC100%2/2
ref $answer eq "ARRAY" and ref $answer->[0] eq "HASH"
ref $answer eq "ARRAY" ref $answer->[0] eq "HASH"
Truth table100%3/3
A and B
A= ref $answer eq "HASH"B= exists $answer->{"dsn"}
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= ref $answer eq "ARRAY"B= ref $answer->[0] eq "HASH"
ABresult
0X0
100
111
79 : ref $answer eq 'ARRAY' && ref $answer->[0] eq 'HASH' ? $answer
80 : undef;
81 9 ▶ if ($own) {
Branch: 1 branch
BranchTrueFalseTotal
if ($own) 7 2 9
82 7 my @beside = sort keys %{ $self->_access_broker_call_attributes };
83 7 ▶ croak sprintf
Branch: 1 branch
BranchTrueFalseTotal
if @beside 1 6 7
84 'The DBD attributes given beside the AccessBroker (%s) cannot be merged '
85 . "into what it answered, %s: that is this transport's own connect info, "
86 . "whose attributes are the driver's shape. Have the broker answer a "
87 . 'DBI-form arrayref or connection parameters, or give the attributes in '
88 . "the broker's answer",
89 join(', ', @beside), _broker_answer_shape($answer)
90 if @beside;
91 6 return $own;
92 }
93  
94 2 ▶ croak sprintf
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
95 '%s cannot connect from what the AccessBroker answered: %s. It takes a '
96 . "DBI-form arrayref [ 'dbi:...', \$user, \$password, \\%%attrs ], a hashref "
97 . 'of connection parameters with a dbname (dbname, host, port, user, '
98 . 'password, ...), or its own connect info: a hashref with a dsn, or a '
99 . '[ \\%%conninfo, \\%%opts ] pair',
100 ref($self) || $self, _broker_answer_shape($answer);
101 }
102  
103 # A hash of connection parameters (Static) as the DBI form, read the way
104 # DBIO::Storage::DBI::_normalize_connect_info reads it (core karr k239). Only
105 # the owning sync storage knows the DBD, so it builds the DSN.
106 22 ▶sub _broker_params_dbi_form {
Subroutine _broker_params_dbi_form: called CC=8 SCAR=20.9
107 22 my ($self, $params) = @_;
108 22 my %attrs = %$params;
109  
110 22 my $dsn;
111 22 ▶ if (defined $attrs{dbname} && $attrs{dbname} =~ /^dbi:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs{'dbname'} and $attrs{'dbname'} =~ /^dbi:/i) { } 13 9 22
Condition66%2/3
defined $attrs{"dbname"} and $attrs{"dbname"} =~ /^dbi:/i
!ll&&!rl&&r
0913
MC/DC50%1/2
defined $attrs{"dbname"} and $attrs{"dbname"} =~ /^dbi:/i
defined $attrs{"dbname"} $attrs{"dbname"} =~ /^dbi:/i
Truth table66%2/3
A and B
A= defined $attrs{"dbname"}B= $attrs{"dbname"} =~ /^dbi:/i
ABresult
0X0
100
111
112 # No owner is needed for this one, so it is read here -- as core's
113 # _dsn_from_connect_params reads it (ADR 0057): host and port are parts of
114 # the DSN and never DBD attributes, empty ones are dropped, and a set one
115 # cannot be put into a whole DSN -- as an attribute it would be lost.
116 13 $dsn = delete $attrs{dbname};
117 13 my %part;
118 13 @part{qw( host port )} = delete @attrs{qw( host port )};
119 13 ▶ my @beside = grep { defined $part{$_} && length $part{$_} } qw( host port );
Condition100%3/3
defined $part{$_} && length $part{$_}
!ll&&!rl&&r
1727
MC/DC100%2/2
defined $part{$_} && length $part{$_}
defined $part{$_} length $part{$_}
Truth table100%3/3
A && B
A= defined $part{$_}B= length $part{$_}
ABresult
0X0
100
111
120 13 ▶ croak 'Connection parameter(s) ' . join(', ', map { "'$_'" } @beside)
Branch: 1 branch
BranchTrueFalseTotal
if @beside 5 8 13
121 . ' given beside a dbname that is a whole DSN: they cannot be put into '
122 . 'it. Give them inside the DSN, or give dbname as a database name.'
123 if @beside;
124 }
125 else {
126 9 my $owner = $self->_owner_storage;
127 9 ▶ croak sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $owner and $owner->can("_dsn_from_connect_params") 1 8 9
Condition50%3/6
$owner and $owner->can("_dsn_from_connect_params")
!ll&&!rl&&r
108
ref $self || $self
l!l&&r!l&&!r
100
MC/DC50%1/2
$owner and $owner->can("_dsn_from_connect_params")
$owner $owner->can("_dsn_from_connect_params")
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A and B
A= $ownerB= $owner->can("_dsn_from_connect_params")
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
128 'Cannot build a DSN from the connection parameters the AccessBroker '
129 . 'answered, %s: which DBD driver they are for is known to the owning sync '
130 . 'storage, and this %s backend has none. Connect through the schema -- '
131 . "MyApp::Schema->connect(\$broker, { async => 'future_io' }) -- or use a "
132 . 'broker that answers a DSN',
133 _broker_answer_shape($params), ref($self) || $self
134 unless $owner && $owner->can('_dsn_from_connect_params');
135 8 $dsn = $owner->_dsn_from_connect_params(\%attrs);
136 }
137  
138 16 my ($user, $password) = delete @attrs{qw( user password )};
139 16 return [ $dsn, $user, $password, \%attrs ];
140 }
141  
142 # A DBI-form answer ready for the driver: DBIO-private attributes stripped,
143 # the attribute hashes merged, pool_size out, the settings beside the broker in.
144 36 ▶sub _broker_answer_dbi_form {
Subroutine _broker_answer_dbi_form: called CC=1 SCAR=0.0
145 36 my ($self, $info) = @_;
146  
147 36 my @info = do {
148 # Core's strip also records the storage options it pulls out. That record
149 # belongs to connect_info, not to one answer asked for per connection.
150 36 local $self->{_storage_options};
151 36 @{ $self->_strip_private_connect_attrs($info) };
152 };
153  
154 36 my %attrs = map { %$_ } grep { ref $_ eq 'HASH' } @info[3, 4];
155 36 delete $attrs{pool_size};
156  
157 return [
158 36 @info[0 .. 2],
159 $self->_merge_access_broker_settings(\%attrs, $self->_access_broker_call_attributes),
160 ];
161 }
162  
163 # A plain function: the shape of a broker's answer for an error message --
164 # types and hash keys, never values, which carry the password.
165 4 ▶sub _broker_answer_shape {
Subroutine _broker_answer_shape: called CC=7 SCAR=21.5
166 4 my ($value) = @_;
167 4 ▶ return 'nothing (undef)' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
168 4 ▶ return 'a plain string' unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 4 4
169 4 ▶ return sprintf 'a hashref with keys (%s)', join ', ', sort keys %$value
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 3 1 4
170 if ref $value eq 'HASH';
171 return sprintf 'an arrayref of (%s)', join ', ',
172 1 ▶ map { !defined $_ ? 'undef' : ref $_ ? ref($_) . ' ref' : 'string' } @$value
Branch: 3 branches
BranchTrueFalseTotal
if ref $value eq "ARRAY" 1 0 1
!defined($_) ? : 0 1 1
ref $_ ? : 0 1 1
173 if ref $value eq 'ARRAY';
174 0 return 'a ' . ref($value) . ' ref';
175 }
176  
177 # --- Future::IO transport ---
178 # Concrete overrides of the DBIO::Storage::Async transport seams. The inherited
179 # Model-B orchestration (_run_crud & friends) calls these; they route through
180 # the Future::IO watcher seam below.
181  
182  
183 191 ▶sub _query_async {
Subroutine _query_async: called CC=4 SCAR=13.9
184 191 my ($self, $sql, $bind, $bind_attrs) = @_;
185 191 ▶ $bind //= [];
Condition100%2/2
$bind //= []
l!l
1883
MC/DC100%1/1
$bind //= []
$bind
Truth table100%2/2
A
A= $bind
Aresult
11
00
186 191 $sql = $self->_transform_sql($sql);
187  
188 191 my $end = $self->_debug_query($sql, $bind, $bind_attrs);
189  
190 191 my $pool = $self->pool;
191 166 ▶ my $held = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
192 166 my $conn = shift;
193 # The lease of this borrow, taken before anything can go on with the
194 # connection; the release hands it back (karr k50, core k275).
195 166 my $lease = $pool->_lease($conn);
196 166 my $f = $self->_seam_future(_await_query_result => $conn, $sql, $bind);
197 166 my $caller = $self->_flight_register($f, undef, $conn, $pool);
198 166 $f->on_cancel($self->_abandon_on_cancel($conn, $pool));
199 166 $f->on_fail($self->_mark_dead_on_fail($conn, $pool));
200 166 $f->on_ready($self->_release_on_ready($conn, $pool, $caller, $lease));
201 166 return $caller;
202 186 });
203  
204 # The wait for the pool is held as the flight is: a caller that lets go of
205 # its Future while still queued must not take the chain with it -- the
206 # release would hand the connection to a waiter with nothing behind it
207 # (karr k46). As core's txn_do_async holds its own sequence.
208 186 ▶ $held->retain if $held->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $held->can("retain") 186 0 186
209 # query_end of the trace, as the statement settles: done, failed or
210 # cancelled. The coderef of core holds no bind (ADR 0044).
211 186 ▶ $held->on_ready($end) if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 13 173 186
212 186 return $held;
213 }
214  
215  
216 166 ▶sub _abandon_on_cancel {
Subroutine _abandon_on_cancel: called CC=1 SCAR=0.0
217 166 my ($self, $conn, $pool) = @_;
218 166 ▶ return sub { $self->_report_abandoned($conn, $pool) };
Subroutine __ANON__: called CC=1 SCAR=0.0
219 }
220  
221 # Report a connection whose statement was cancelled in flight dead to $pool,
222 # the pool the statement was dispatched with; its hand-back then retires it.
223 # Without a pool there is nobody to tell. Never dies (see _abandon_on_cancel).
224 34 ▶sub _report_abandoned {
Subroutine _report_abandoned: called CC=3 SCAR=11.0
225 34 my ($self, $conn, $pool) = @_;
226 34 ▶ return unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 1 33 34
227 33 local $@;
228 33 ▶ eval { $pool->_mark_connection_dead($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pool->_mark_connection_dead($conn); 1 } } or warn "DBIO::Async: reporting an abandoned connection to the pool failed: $@" 33 0 33
229 or warn "DBIO::Async: reporting an abandoned connection to the pool failed: $@";
230 33 return;
231 }
232  
233  
234 166 ▶sub _release_on_ready {
Subroutine _release_on_ready: called CC=1 SCAR=0.0
235 166 my ($self, $conn, $pool, $caller, $lease) = @_;
236 166 ▶ return sub { $self->_release_and_settle($conn, $pool, $caller, $_[0], $lease) };
Subroutine __ANON__: called CC=1 SCAR=0.0
237 }
238  
239 # The body of the callback _release_on_ready builds: the connection goes back
240 # to the pool it was acquired from, then the caller's Future takes the
241 # statement's outcome. A release that dies still dies into whoever settled the
242 # statement, as it did when the caller held that Future itself -- but only
243 # once the caller has its outcome.
244 156 ▶sub _release_and_settle {
Subroutine _release_and_settle: called CC=5 SCAR=17.1
245 156 my ($self, $conn, $pool, $caller, $flight, $lease) = @_;
246 156 my $died;
247 {
248 156 local $@;
249 # With the borrow's lease when there is one: a release that comes after
250 # the connection went on is then no release of this statement's holder
251 # and the pool does not take it (karr k50, core k275). No lease -- a
252 # caller of _release_on_ready that took none -- releases as ever.
253 156 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { defined $lease ? $pool->release($conn, $lease) : $pool->release($conn); 1 } } or $died = $@ || "release died" 156 0 156
254 156 ▶ defined $lease ? $pool->release($conn, $lease) : $pool->release($conn);
Branch: 1 branch
BranchTrueFalseTotal
defined $lease ? : 156 0 156
255 156 ▶ 1;
Condition0%0/2
$@ || 'release died'
l!l
00
MC/DC0%0/1
$@ || 'release died'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
256 } or $died = $@ || 'release died';
257 }
258 156 _settle_caller($caller, $flight);
259 156 ▶ die $died if defined $died;
Branch: 1 branch
BranchTrueFalseTotal
if defined $died 0 156 156
260 156 return;
261 }
262  
263  
264 358 ▶sub _mark_dead_on_fail {
Subroutine _mark_dead_on_fail: called CC=1 SCAR=0.0
265 358 my ($self, $conn, $pool) = @_;
266 358 ▶ return sub { $self->_mark_if_fatal($conn, $pool, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
267 }
268  
269 # The body of the callback _mark_dead_on_fail builds: ask the seam with the
270 # query's failure, report the connection to $pool -- the pool the statement
271 # was dispatched with -- when it is dead. Without a pool there is nobody to
272 # tell, which is no failed report. Nothing leaves here (see
273 # _mark_dead_on_fail).
274 33 ▶sub _mark_if_fatal {
Subroutine _mark_if_fatal: called CC=8 SCAR=20.8
275 33 my ($self, $conn, $pool, @failure) = @_;
276 33 local $@;
277  
278 33 my $fatal;
279 33 my $asked = eval { $fatal = $self->_conn_error_is_fatal($conn, @failure); 1 };
280 33 ▶ my $broken = $asked ? undef : ($@ || 'it died without a message');
Branch: 1 branch
BranchTrueFalseTotal
$asked ? : 31 2 33
Condition50%1/2
$@ or "it died without a message"
l!l
20
MC/DC0%0/1
$@ or "it died without a message"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
281 33 ▶ return unless $fatal || defined $broken;
Branch: 1 branch
BranchTrueFalseTotal
unless $fatal or defined $broken 20 13 33
Condition100%3/3
$fatal or defined $broken
l!l&&r!l&&!r
11220
MC/DC100%2/2
$fatal or defined $broken
$fatal defined $broken
Truth table100%3/3
A or B
A= $fatalB= defined $broken
ABresult
1X1
011
000
282  
283 13 ▶ if (defined $broken) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $broken) 2 11 13
284 2 chomp(my $why = "$broken");
285 2 $self->_pool_report($pool, '_conn_error_is_fatal died, so the connection '
286 . "is treated as dead: $why");
287 }
288 13 ▶ return unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 3 10 13
289  
290 10 ▶ eval { $pool->_mark_connection_dead($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pool->_mark_connection_dead($conn); 1 } } or warn "DBIO::Async: reporting a dead connection to the pool failed: $@" 10 0 10
291 or warn "DBIO::Async: reporting a dead connection to the pool failed: $@";
292 10 return;
293 }
294  
295 # Something went wrong where nothing may be thrown: tell $pool's on_error.
296 # Warned instead when there is no pool, or when its on_error dies in turn.
297 6 ▶sub _pool_report {
Subroutine _pool_report: called CC=3 SCAR=11.0
298 6 my ($self, $pool, $report) = @_;
299 6 local $@;
300 6 ▶ return if $pool && eval { $pool->{on_error}->($report); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $pool and eval { do { $pool->{"on_error"}->($report); 1 } } 5 1 6
Condition66%2/3
$pool and eval { do { $pool->{"on_error"}->($report); 1 } }
!ll&&!rl&&r
015
MC/DC50%1/2
$pool and eval { do { $pool->{"on_error"}->($report); 1 } }
$pool eval { do { $pool->{"on_error"}->($report); 1 } }
Truth table66%2/3
A and B
A= $poolB= eval { do { $pool->{"on_error"}->($report); 1 } }
ABresult
0X0
100
111
301 1 warn "DBIO::Async pool error: $report\n";
302 1 return;
303 }
304  
305  
306 295 ▶sub _query_async_pinned {
Subroutine _query_async_pinned: called CC=3 SCAR=11.0
307 295 my ($self, $conn, $sql, $bind, $bind_attrs) = @_;
308 295 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
2950
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
309 295 $sql = $self->_transform_sql($sql);
310  
311 295 my $end = $self->_debug_query($sql, $bind, $bind_attrs);
312  
313 # Whichever way the statement goes, the Future the caller gets carries
314 # query_end (the pinned path has three returns).
315 295 my $f = $self->_pinned_send($conn, $sql, $bind);
316 295 ▶ $f->on_ready($end) if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 13 282 295
317 295 return $f;
318 }
319  
320 # The dispatch of _query_async_pinned: unqueued, queued behind the statement
321 # in flight, or started on an idle connection.
322 295 ▶sub _pinned_send {
Subroutine _pinned_send: called CC=8 SCAR=20.8
323 295 my ($self, $conn, $sql, $bind) = @_;
324  
325 295 ▶ return $self->_start_unqueued($conn, $sql, $bind)
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($conn) // "") eq "HASH" 2 293 295
Condition50%1/2
builtin::reftype($conn) // ""
l!l
2950
MC/DC0%0/1
builtin::reftype($conn) // ""
builtin::reftype($conn)
Truth table50%1/2
A
A= builtin::reftype($conn)
Aresult
11
00
326 unless (reftype($conn) // '') eq 'HASH';
327  
328 293 ▶ my $queue = $conn->{_dbio_async_queue} //= { waiting => [] };
Condition100%2/2
$conn->{"_dbio_async_queue"} //= {"waiting", []}
l!l
126167
MC/DC100%1/1
$conn->{"_dbio_async_queue"} //= {"waiting", []}
$conn->{"_dbio_async_queue"}
Truth table100%2/2
A
A= $conn->{"_dbio_async_queue"}
Aresult
11
00
329 293 ▶ if ($queue->{busy} || @{ $queue->{waiting} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue->{"busy"} or @{$queue->{"waiting"};}) 107 186 293
Condition66%2/3
$queue->{"busy"} or @{$queue->{"waiting"};}
l!l&&r!l&&!r
1070186
MC/DC50%1/2
$queue->{"busy"} or @{$queue->{"waiting"};}
$queue->{"busy"} @{$queue->{"waiting"};}
Truth table66%2/3
A or B
A= $queue->{"busy"}B= @{$queue->{"waiting"};}
ABresult
1X1
011
000
330 107 my $limit = $self->async_option('max_pending_pinned');
331 107 ▶ return $self->future_class->fail('not sent: ' . $limit . ' statements '
Branch: 1 branch
BranchTrueFalseTotal
if $limit and _queue_pending($queue) >= $limit 2 105 107
Condition100%3/3
$limit and _queue_pending($queue) >= $limit
!ll&&!rl&&r
9962
MC/DC100%2/2
$limit and _queue_pending($queue) >= $limit
$limit _queue_pending($queue) >= $limit
Truth table100%3/3
A and B
A= $limitB= _queue_pending($queue) >= $limit
ABresult
0X0
100
111
332 . "already wait on this connection (max_pending_pinned)\n")
333 if $limit && _queue_pending($queue) >= $limit;
334 105 return $self->_queue_wait($queue, $sql, $bind);
335 }
336 186 return $self->_queue_start($conn, $queue, undef, $sql, $bind);
337 }
338  
339  
340 # The pinned dispatch itself, unqueued: the query on the wire, a dispatch that
341 # dies or returns no Future as a failed Future (core's _seam_future), and the
342 # k26 classification on a failure, reported to $pool.
343 192 ▶sub _dispatch_pinned {
Subroutine _dispatch_pinned: called CC=1 SCAR=0.0
344 192 my ($self, $conn, $pool, $sql, $bind) = @_;
345 192 my $f = $self->_seam_future(_await_query_result => $conn, $sql, $bind);
346 192 $f->on_fail($self->_mark_dead_on_fail($conn, $pool));
347 192 return $f;
348 }
349  
350 # A pinned statement on a connection that is no hashref: no queue, so it goes
351 # out at once -- held, and handed to its caller, like every other flight.
352 2 ▶sub _start_unqueued {
Subroutine _start_unqueued: called CC=1 SCAR=0.0
353 2 my ($self, $conn, $sql, $bind) = @_;
354 2 my $pool = $self->_built_pool;
355 2 my $flight = $self->_dispatch_pinned($conn, $pool, $sql, $bind);
356 2 my $caller = $self->_flight_register($flight, undef, $conn, $pool);
357 2 $flight->on_ready(_settle_caller_cb($caller));
358 2 return $caller;
359 }
360  
361 2 ▶sub _settle_caller_cb {
Subroutine _settle_caller_cb: called CC=1 SCAR=0.0
362 2 my ($caller) = @_;
363 2 ▶ return sub { _settle_caller($caller, $_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
364 }
365  
366 # A statement for a connection that is busy: a pending Future for the caller,
367 # of the class of the statement in flight so that ->get runs its loop, and an
368 # entry at the back of the queue.
369 105 ▶sub _queue_wait {
Subroutine _queue_wait: called CC=3 SCAR=11.1
370 105 my ($self, $queue, $sql, $bind) = @_;
371 105 ▶ my $proto = ref $queue->{busy} ? $queue->{busy} : $queue->{last};
Branch: 1 branch
BranchTrueFalseTotal
ref $queue->{'busy'} ? : 105 0 105
372 105 ▶ my $proxy = $proto ? $proto->new : $self->future_class->new;
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 105 0 105
373 105 my $entry = [ $proxy, $sql, $bind ];
374 105 push @{ $queue->{waiting} }, $entry;
375 105 $proxy->on_cancel(_queue_drop_cb($entry));
376 105 return $proxy;
377 }
378  
379 # Cancelled while waiting: empty the entry, so its SQL and binds go now and the
380 # queue skips it. Holds the entry weakly -- the Future must not keep it.
381 105 ▶sub _queue_drop_cb {
Subroutine _queue_drop_cb: called CC=1 SCAR=0.0
382 105 my ($entry) = @_;
383 105 weaken($entry);
384 105 ▶ return sub { @$entry = () if $entry };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $entry 56 0 56
385 }
386  
387 # Put one statement on the wire, or refuse it on a connection a cancelled
388 # statement left behind, and settle $proxy -- the caller's Future, made here
389 # when the statement did not wait -- from it (_queue_settled).
390 233 ▶sub _queue_start {
Subroutine _queue_start: called CC=4 SCAR=13.9
391 233 my ($self, $conn, $queue, $proxy, $sql, $bind) = @_;
392 233 $queue->{busy} = 1; # a statement issued during the dispatch waits
393 233 my $pool = $self->_built_pool;
394 my $refusal = $queue->{abandoned}
395 233 ▶ // (_queue_conn_reported_dead($conn, $pool)
Branch: 1 branch
BranchTrueFalseTotal
_queue_conn_reported_dead($conn, $pool) ? : 1 190 191
Condition100%2/2
$queue->{"abandoned"} // (_queue_conn_reported_dead($conn, $pool) ? "not sent: this connection was reported dead to the pool (_conn_error_is_fatal), so nothing more is sent on it\n" : undef)
l!l
42191
MC/DC100%1/1
$queue->{"abandoned"} // (_queue_conn_reported_dead($conn, $pool) ? "not sent: this connection was reported dead to the pool (_conn_error_is_fatal), so nothing more is sent on it\n" : undef)
$queue->{"abandoned"}
Truth table100%2/2
A
A= $queue->{"abandoned"}
Aresult
11
00
396 ? 'not sent: this connection was reported dead to the pool '
397 . "(_conn_error_is_fatal), so nothing more is sent on it\n"
398 : undef);
399 233 ▶ my $flight = defined $refusal
Branch: 1 branch
BranchTrueFalseTotal
defined $refusal ? : 43 190 233
400 ? $self->future_class->fail($refusal)
401 : $self->_dispatch_pinned($conn, $pool, $sql, $bind);
402 233 $queue->{busy} = $queue->{last} = $flight;
403 233 $proxy = $self->_flight_register($flight, $proxy, $conn, $pool);
404 233 $flight->on_ready($self->_queue_settled_cb($conn, $queue, $proxy, $pool));
405 233 return $proxy;
406 }
407  
408 233 ▶sub _queue_settled_cb {
Subroutine _queue_settled_cb: called CC=1 SCAR=0.0
409 233 my ($self, $conn, $queue, $proxy, $pool) = @_;
410 233 ▶ return sub { $self->_queue_settled($conn, $queue, $proxy, $pool, $_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
411 }
412  
413 # The statement in flight settled, so the connection is free. Nothing waiting:
414 # the queue is gone BEFORE the caller's callbacks run, and a statement they
415 # issue goes out at once -- a synchronous one too, such as every statement of
416 # a txn_do block, which core runs from the callback of BEGIN's Future (karr
417 # k34). Statements waiting: they were issued first and go first; the queue
418 # stays busy through the callbacks, so what those issue queues behind them,
419 # and advances once they have run. The backlog -- what was waiting when this
420 # outcome came in, issued before anyone knew it -- is counted before them.
421 222 ▶sub _queue_settled {
Subroutine _queue_settled: called CC=4 SCAR=13.9
422 222 my ($self, $conn, $queue, $proxy, $pool, $flight) = @_;
423 222 ▶ return $self->_queue_advance($conn, $queue, $pool, $flight)
Branch: 1 branch
BranchTrueFalseTotal
if $flight->is_cancelled 34 188 222
424 if $flight->is_cancelled;
425  
426 188 $queue->{backlog} = scalar @{ $queue->{waiting} };
427 188 ▶ $self->_queue_idle($conn, $queue) unless $queue->{backlog};
Branch: 1 branch
BranchTrueFalseTotal
unless $queue->{"backlog"} 154 34 188
428 188 _settle_caller($proxy, $flight);
429 188 ▶ $self->_queue_advance($conn, $queue, $pool, $flight) if $queue->{backlog};
Branch: 1 branch
BranchTrueFalseTotal
if $queue->{"backlog"} 34 154 188
430 188 return;
431 }
432  
433 # The caller's Future takes the outcome of the statement's. A cancelled
434 # statement was cancelled through that Future, or abandoned with its
435 # connection (_abandon_flights_on): either way the caller's is settled already.
436 346 ▶sub _settle_caller {
Subroutine _settle_caller: called CC=5 SCAR=16.1
437 346 my ($caller, $flight) = @_;
438 346 ▶ return if $caller->is_ready || $caller->is_cancelled || $flight->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $caller->is_ready or $caller->is_cancelled or $flight->is_cancelled 26 320 346
Condition83%5/6
$caller->is_ready or $caller->is_cancelled or $flight->is_cancelled
l!l&&r!l&&!r
260320
$caller->is_ready or $caller->is_cancelled
l!l&&r!l&&!r
1610320
MC/DC66%2/3
$caller->is_ready or $caller->is_cancelled or $flight->is_cancelled
$caller->is_ready $caller->is_cancelled $flight->is_cancelled
Truth table75%3/4
A or B or C
A= $caller->is_readyB= $caller->is_cancelledC= $flight->is_cancelled
ABCresult
1XX1
01X1
0011
0000
439 320 ▶ return $caller->fail($flight->failure) if $flight->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $flight->is_failed 76 244 320
440 244 return $caller->done($flight->get);
441 }
442  
443 # Nothing in flight, nothing waiting: the connection is idle, and carries no
444 # queue -- unless a cancelled statement left it behind, or the pool closed it:
445 # then the queue stays, to refuse what comes.
446 175 ▶sub _queue_idle {
Subroutine _queue_idle: called CC=2 SCAR=6.9
447 175 my ($self, $conn, $queue) = @_;
448 175 ▶ delete $conn->{_dbio_async_queue} unless defined $queue->{abandoned};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $queue->{"abandoned"} 122 53 175
449 175 delete @$queue{qw( busy last backlog )};
450 175 return;
451 }
452  
453 # After the settled statement's callbacks: dispatch the next one waiting, or
454 # leave the connection idle. See THE PINNED STATEMENT QUEUE for what a failure
455 # and a cancel change.
456 68 ▶sub _queue_advance {
Subroutine _queue_advance: called CC=11 SCAR=24.2
457 68 my ($self, $conn, $queue, $pool, $flight) = @_;
458 68 $queue->{busy} = 1; # what the refusals below make callers issue waits
459  
460 68 ▶ if ($flight->is_cancelled) {
Branch: 2 branches
BranchTrueFalseTotal
if ($flight->is_cancelled) { } 34 34 68
elsif ($flight->is_failed and _queue_conn_reported_dead($conn, $pool)) { } 2 50 52
461 34 $self->_queue_abandon($conn, $queue, $pool);
462 }
463 ▶ elsif ($flight->is_failed && _queue_conn_reported_dead($conn, $pool)) {
Condition100%3/3
$flight->is_failed and _queue_conn_reported_dead($conn, $pool)
!ll&&!rl&&r
18142
MC/DC100%2/2
$flight->is_failed and _queue_conn_reported_dead($conn, $pool)
$flight->is_failed _queue_conn_reported_dead($conn, $pool)
Truth table100%3/3
A and B
A= $flight->is_failedB= _queue_conn_reported_dead($conn, $pool)
ABresult
0X0
100
111
464 2 my $why = 'not sent: the statement before it on this connection failed, '
465 . 'and the connection was reported dead (_conn_error_is_fatal), '
466 . "so what was queued behind it is not put on it\n";
467 2 ▶ for my $entry (splice @{ $queue->{waiting} }, 0, $queue->{backlog} // 0) {
Condition50%1/2
$queue->{"backlog"} // 0
l!l
20
MC/DC0%0/1
$queue->{"backlog"} // 0
$queue->{"backlog"}
Truth table50%1/2
A
A= $queue->{"backlog"}
Aresult
11
00
468 2 my ($proxy) = splice @$entry;
469 2 ▶ $proxy->fail($why) if $proxy && !$proxy->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $proxy and not $proxy->is_ready 2 0 2
Condition33%1/3
$proxy and not $proxy->is_ready
!ll&&!rl&&r
002
MC/DC0%0/2
$proxy and not $proxy->is_ready
$proxy not $proxy->is_ready
Truth table33%1/3
A and not(B)
A= $proxyB= not $proxy->is_ready
ABresult
0X0
100
111
470 }
471 }
472 68 delete $queue->{backlog};
473  
474 68 ▶ while (my $entry = shift @{ $queue->{waiting} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @{$$queue{"waiting"};}) 51 21 72
475 51 my ($proxy, $sql, $bind) = splice @$entry;
476 51 ▶ next unless $proxy && !$proxy->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $proxy and not $proxy->is_ready 4 47 51
Condition66%2/3
$proxy and not $proxy->is_ready
!ll&&!rl&&r
4047
MC/DC50%1/2
$proxy and not $proxy->is_ready
$proxy not $proxy->is_ready
Truth table66%2/3
A and not(B)
A= $proxyB= not $proxy->is_ready
ABresult
0X0
100
111
477 47 $self->_queue_start($conn, $queue, $proxy, $sql, $bind);
478 47 return;
479 }
480  
481 21 $self->_queue_idle($conn, $queue);
482 21 return;
483 }
484  
485 # The statement in flight was cancelled; its result is still on its way.
486 # Refuse everything from here on, and have the pool retire the connection. A
487 # connection closed under its statement (_abandon_flights_on) refuses already,
488 # and the pool that closed it needs no telling.
489 34 ▶sub _queue_abandon {
Subroutine _queue_abandon: called CC=2 SCAR=6.9
490 34 my ($self, $conn, $queue, $pool) = @_;
491 34 ▶ return if defined $queue->{abandoned};
Branch: 1 branch
BranchTrueFalseTotal
if defined $queue->{"abandoned"} 28 6 34
492 6 $queue->{abandoned} = 'not sent: a statement on this connection was '
493 . 'cancelled while in flight, and its result is still unread on the '
494 . "connection, so nothing more is sent on it\n";
495 6 $self->_report_abandoned($conn, $pool);
496 6 return;
497 }
498  
499 # Has $pool -- the pool the failed statement was dispatched with -- been told
500 # $conn is dead? Asked once _mark_dead_on_fail has run. A plain function: no
501 # pool, nobody told.
502 207 ▶sub _queue_conn_reported_dead {
Subroutine _queue_conn_reported_dead: called CC=4 SCAR=13.9
503 207 my ($conn, $pool) = @_;
504 207 ▶ return 0 unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 16 191 207
505 191 ▶ return $pool->can('_connection_is_dead') && $pool->_connection_is_dead($conn)
Branch: 1 branch
BranchTrueFalseTotal
$pool->can('_connection_is_dead') && $pool->_connection_is_dead($conn) ? : 3 188 191
Condition66%2/3
$pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn)
!ll&&!rl&&r
01883
MC/DC50%1/2
$pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn)
$pool->can("_connection_is_dead") $pool->_connection_is_dead($conn)
Truth table66%2/3
A and B
A= $pool->can("_connection_is_dead")B= $pool->_connection_is_dead($conn)
ABresult
0X0
100
111
506 ? 1 : 0;
507 }
508  
509  
510 # The storage's pool as it stands -- never built here (see STATEMENTS IN
511 # FLIGHT). The slot is the one core's disconnect and connected read.
512 235 ▶sub _built_pool { $_[0]->{pool} }
Subroutine _built_pool: called CC=1 SCAR=0.0
513  
514 # Enter a dispatched statement into the register and return the Future its
515 # caller gets -- $caller if it has one already (a statement that waited in the
516 # pinned queue), else a new one of the flight's class, so that ->get on it
517 # runs the flight's loop. The first thing a settled flight does is leave the
518 # register; the caller's cancel reaches the flight through _let_go. A
519 # statement that never took off -- its dispatch died, or it was refused -- is
520 # settled already and is not entered: there is nothing to hold.
521 401 ▶sub _flight_register {
Subroutine _flight_register: called CC=3 SCAR=11.0
522 401 my ($self, $flight, $caller, $conn, $pool) = @_;
523 401 ▶ $caller //= $flight->new;
Condition66%2/3
$caller //= $flight->new
l!l&&r!l&&!r
473540
MC/DC0%0/2
$caller //= $flight->new
$caller $flight->new
Truth table66%2/3
A //= B
A= $callerB= $flight->new
ABresult
1X1
011
000
524 401 ▶ return $caller if $flight->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $flight->is_ready 62 339 401
525  
526 339 my $key = refaddr $flight;
527 my $entry = $self->{_flights}{$key} = {
528 flight => $flight,
529 caller => $caller,
530 conn => $conn,
531 pool => $pool,
532 seq => ++$self->{_flight_seq},
533 339 pid => $$,
534 };
535 339 $self->_statement_timer_arm($key, $entry);
536 339 $flight->on_ready($self->_flight_landed_cb($key));
537 339 $caller->on_cancel($self->_flight_cancel_cb($flight));
538 339 return $caller;
539 }
540  
541 # Named subs, as _release_on_ready is one: each callback closes over what it
542 # is handed, never over a statement's SQL or bind values (ADR 0044).
543 339 ▶sub _flight_landed_cb {
Subroutine _flight_landed_cb: called CC=1 SCAR=0.0
544 339 my ($self, $key) = @_;
545 339 ▶ return sub { $self->_flight_forget($key); return };
Subroutine __ANON__: called CC=1 SCAR=0.0
546 }
547  
548 # Take a flight out of the register and stop its statement_timeout timer, if
549 # it has one. A timer that fired is gone from the entry already.
550 361 ▶sub _flight_forget {
Subroutine _flight_forget: called CC=4 SCAR=13.9
551 361 my ($self, $key) = @_;
552 361 ▶ my $entry = delete $self->{_flights}{$key} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = delete $self->{"_flights"}{$key} or return undef 318 43 361
553 318 ▶ if (my $timer = delete $entry->{timer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = delete $entry->{"timer"}) 5 313 318
554 5 local $@;
555 5 eval { $self->_let_go($timer); 1 }
556 5 ▶ or $self->_pool_report($entry->{pool}, 'Stopping the statement_timeout '
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_let_go($timer); 1 } } or $self->_pool_report($entry->{"pool"}, 'Stopping the statement_timeout ' . "timer of a settled statement died: $@") 5 0 5
557 . "timer of a settled statement died: $@");
558 }
559 318 return $entry;
560 }
561  
562 # The caller's Future holds the flight weakly: the register is what holds a
563 # flight, and the flight holds its caller's Future. A strong reference back
564 # would make the two keep each other, and the register an ornament.
565 339 ▶sub _flight_cancel_cb {
Subroutine _flight_cancel_cb: called CC=1 SCAR=0.0
566 339 my ($self, $flight) = @_;
567 339 weaken($flight);
568 339 ▶ return sub { $self->_let_go($flight) if $flight };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $flight 15 0 15
569 }
570  
571  
572 268 ▶sub _abandon_flights_on {
Subroutine _abandon_flights_on: called CC=10 SCAR=23.3
573 268 my ($self, $conn) = @_;
574 268 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 268 268
575 268 ▶ return unless ref $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conn 0 268 268
576  
577 # Finished, whether or not anything is on it: from here on the queue exists
578 # to refuse (_queue_start), and stays (_queue_idle). Inside an eval -- a
579 # hash that will not take the key must not cost the close.
580 268 ▶ if ((reftype($conn) // '') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($conn) // "") eq "HASH") 266 2 268
Condition50%1/2
builtin::reftype($conn) // ""
l!l
2680
MC/DC0%0/1
builtin::reftype($conn) // ""
builtin::reftype($conn)
Truth table50%1/2
A
A= builtin::reftype($conn)
Aresult
11
00
581 266 local $@;
582 266 eval {
583 266 ▶ my $queue = $conn->{_dbio_async_queue} //= { waiting => [] };
Condition100%2/2
$conn->{"_dbio_async_queue"} //= {"waiting", []}
l!l
31235
MC/DC100%1/1
$conn->{"_dbio_async_queue"} //= {"waiting", []}
$conn->{"_dbio_async_queue"}
Truth table100%2/2
A
A= $conn->{"_dbio_async_queue"}
Aresult
11
00
584 266 ▶ $queue->{abandoned} //= 'not sent: this connection was closed -- the '
Condition100%2/2
$queue->{"abandoned"} //= "not sent: this connection was closed -- the storage was disconnected, its pool shut down, or the connection retired -- so nothing more is sent on it\n"
l!l
3263
MC/DC100%1/1
$queue->{"abandoned"} //= "not sent: this connection was closed -- the storage was disconnected, its pool shut down, or the connection retired -- so nothing more is sent on it\n"
$queue->{"abandoned"}
Truth table100%2/2
A
A= $queue->{"abandoned"}
Aresult
11
00
585 . 'storage was disconnected, its pool shut down, or the connection '
586 . "retired -- so nothing more is sent on it\n";
587 266 1;
588 };
589 266 $self->_ready_let_go($conn);
590 }
591  
592 268 my $addr = refaddr $conn;
593 0 my @flights = sort { $a->{seq} <=> $b->{seq} }
594 46 ▶ grep { ref $_->{conn} && refaddr($_->{conn}) == $addr }
Condition66%2/3
ref $_->{'conn'} && builtin::refaddr($_->{'conn'}) == $addr
!ll&&!rl&&r
0343
MC/DC50%1/2
ref $_->{'conn'} && builtin::refaddr($_->{'conn'}) == $addr
ref $_->{'conn'} builtin::refaddr($_->{'conn'}) == $addr
Truth table66%2/3
A && B
A= ref $_->{'conn'}B= builtin::refaddr($_->{'conn'}) == $addr
ABresult
0X0
100
111
595 268 ▶ values %{ $self->{_flights} || {} };
Condition100%2/2
$self->{'_flights'} || {}
l!l
17593
MC/DC100%1/1
$self->{'_flights'} || {}
$self->{'_flights'}
Truth table100%2/2
A
A= $self->{'_flights'}
Aresult
11
00
596 268 $self->_flight_abandon($_) for @flights;
597 268 return;
598 }
599  
600 # End one flight whose connection is being closed: off the register, the
601 # caller told, the poll let go of. The caller is failed first, so it hears
602 # what happened rather than seeing a cancel; letting go of the flight then
603 # runs the transport's own callbacks on it, which find the caller settled and
604 # a pool that no longer takes the connection. A callback that dies on either
605 # way is reported, not thrown (see _abandon_flights_on).
606 43 ▶sub _flight_abandon {
Subroutine _flight_abandon: called CC=7 SCAR=19.9
607 43 my ($self, $entry) = @_;
608 43 my ($flight, $caller) = @$entry{qw( flight caller )};
609 43 $self->_flight_forget(refaddr $flight);
610  
611 43 my @died;
612 {
613 43 local $@;
614 43 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $caller->fail("Statement abandoned in flight: its connection was closed under it -- the storage was disconnected, its pool shut down, or the connection retired -- so its answer is not read, and whether it took effect is unknown\n") unless $caller->is_ready; 1 } } or push @died, $@ || "unknown error" 39 4 43
615 43 ▶ $caller->fail('Statement abandoned in flight: its connection was '
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->is_ready 43 0 43
616 . 'closed under it -- the storage was disconnected, its pool shut '
617 . 'down, or the connection retired -- so its answer is not read, and '
618 . "whether it took effect is unknown\n") unless $caller->is_ready;
619 39 ▶ 1;
Condition50%1/2
$@ || 'unknown error'
l!l
40
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
620 } or push @died, $@ || 'unknown error';
621 43 ▶ eval { $self->_let_go($flight); 1 } or push @died, $@ || 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_let_go($flight); 1 } } or push @died, $@ || "unknown error" 43 0 43
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
622 }
623 43 for my $death (@died) {
624 4 chomp(my $why = "$death");
625 4 $self->_pool_report($entry->{pool}, 'A callback died as a statement in '
626 . "flight was failed with the close of its connection: $why");
627 }
628 43 return;
629 }
630  
631  
632 403 ▶sub async_option_names { qw( statement_timeout max_pending_pinned ) }
Subroutine async_option_names: called CC=1 SCAR=0.0
Pod documented
633  
634 # The values of this transport's own options (core checks its own). The
635 # messages name the option and the form, never the value.
636 29 ▶sub _assert_async_option {
Subroutine _assert_async_option: called CC=12 SCAR=24.9
637 29 my ($self, $name, $value) = @_;
638  
639 29 ▶ if ($name eq 'statement_timeout') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "statement_timeout") 13 16 29
640 13 ▶ my $number = defined $value && !ref $value && looks_like_number($value)
Condition91%11/12
defined $value && !ref($value) && &looks_like_number($value) && $value == $value && abs $value != 9**9**9
!ll&&!rl&&r
418
defined $value and not ref $value and &looks_like_number($value) and $value == $value
!ll&&!rl&&r
319
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
2110
defined $value and not ref $value
!ll&&!rl&&r
0211
MC/DC80%4/5
defined $value && !ref($value) && &looks_like_number($value) && $value == $value && abs $value != 9**9**9
defined $value not ref $value &looks_like_number($value) $value == $value abs $value != 9**9**9
Truth table83%5/6
A and not(B) and C and D && E
A= defined $valueB= not ref $valueC= &looks_like_number($value)D= $value == $valueE= abs $value != 9**9**9
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
641 && $value == $value && abs($value) != 9**9**9;
642 13 ▶ DBIO::Exception->throw("Connect option 'statement_timeout' takes a number "
Branch: 1 branch
BranchTrueFalseTotal
unless $number and $value >= 0 6 7 13
Condition100%3/3
$number and $value >= 0
!ll&&!rl&&r
517
MC/DC100%2/2
$number and $value >= 0
$number $value >= 0
Truth table100%3/3
A and B
A= $numberB= $value >= 0
ABresult
0X0
100
111
643 . 'of seconds, 0 or more (0 turns the timeout off)')
644 unless $number && $value >= 0;
645 7 return;
646 }
647 16 ▶ if ($name eq 'max_pending_pinned') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "max_pending_pinned") 9 7 16
648 9 ▶ DBIO::Exception->throw("Connect option 'max_pending_pinned' takes a "
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/ 6 3 9
Condition83%5/6
defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/
!ll&&!rl&&r
243
defined $value and not ref $value
!ll&&!rl&&r
027
MC/DC66%2/3
defined $value and not ref $value and $value =~ /\A[1-9][0-9]*\z/
defined $value not ref $value $value =~ /\A[1-9][0-9]*\z/
Truth table75%3/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A[1-9][0-9]*\z/
ABCresult
0XX0
10X0
1100
1111
649 . 'positive integer')
650 unless defined $value && !ref $value && $value =~ /\A[1-9][0-9]*\z/;
651 3 return;
652 }
653 7 return $self->SUPER::_assert_async_option($name, $value);
654 }
655  
656 # Arm the statement_timeout timer of a statement just put on the wire, and keep
657 # it on the register entry; _flight_forget stops it when the statement settles.
658 # A named sub chain, as everywhere: the timer's callback closes over the key
659 # and a weak storage, never the SQL or the binds (ADR 0044).
660 339 ▶sub _statement_timer_arm {
Subroutine _statement_timer_arm: called CC=8 SCAR=25.4
661 339 my ($self, $key, $entry) = @_;
662 339 ▶ my $seconds = $self->async_option('statement_timeout') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $seconds = $self->async_option("statement_timeout") or return 7 332 339
663  
664 7 my $timer;
665 7 local $@;
666 7 my $armed = eval { $timer = $self->_delay($seconds); 1 };
667 7 ▶ unless ($armed && blessed($timer) && $timer->can('on_done') && $timer->can('cancel')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($armed and builtin::blessed($timer) and $timer->can("on_done") and $timer->can("cancel")) 0 7 7
Condition33%3/9
$armed and builtin::blessed($timer) and $timer->can("on_done") and $timer->can("cancel")
!ll&&!rl&&r
007
$armed and builtin::blessed($timer) and $timer->can("on_done")
!ll&&!rl&&r
007
$armed and builtin::blessed($timer)
!ll&&!rl&&r
007
MC/DC0%0/4
$armed and builtin::blessed($timer) and $timer->can("on_done") and $timer->can("cancel")
$armed builtin::blessed($timer) $timer->can("on_done") $timer->can("cancel")
Truth table20%1/5
A and B and C and D
A= $armedB= builtin::blessed($timer)C= $timer->can("on_done")D= $timer->can("cancel")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
668 0 ▶ $self->_pool_report($entry->{pool}, 'statement_timeout could not be armed, '
Branch: 1 branch
BranchTrueFalseTotal
$armed ? : 0 0 0
Condition0%0/2
$@ or "_delay died"
l!l
00
MC/DC0%0/1
$@ or "_delay died"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
669 . 'so the statement runs without a time limit: '
670 . ($armed ? '_delay returned no Future' : ($@ || '_delay died')));
671 0 return;
672 }
673 7 $entry->{timeout} = $seconds;
674 7 $entry->{timer} = $timer;
675 7 $timer->on_done(_statement_timeout_cb($self, $key));
676 7 return;
677 }
678  
679 7 ▶sub _statement_timeout_cb {
Subroutine _statement_timeout_cb: called CC=1 SCAR=0.0
680 7 my ($self, $key) = @_;
681 7 weaken($self);
682 7 ▶ return sub { $self->_statement_timed_out($key) if $self; return };
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if $self 2 0 2
683 }
684  
685 # The time of a statement on the wire is up: its connection is reported dead
686 # first (so what its caller does next finds it so), the caller hears why, and
687 # the flight is let go of -- which cancels it, runs the transport's callbacks
688 # (pooled: the retiring release; pinned: the queue's refusal of what waits) and
689 # finds the caller settled. A flight inherited across a fork does nothing in
690 # the child. Nothing leaves here: a callback that dies goes to the pool's
691 # on_error.
692 2 ▶sub _statement_timed_out {
Subroutine _statement_timed_out: called CC=9 SCAR=24.8
693 2 my ($self, $key) = @_;
694 2 ▶ my $entry = $self->{_flights}{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = $self->{"_flights"}{$key} or return 2 0 2
695 2 ▶ return unless $entry->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{"pid"} == $$ 0 2 2
696 2 delete $entry->{timer}; # it fired
697  
698 2 my ($flight, $caller) = @$entry{qw( flight caller )};
699 2 $self->_report_abandoned($entry->{conn}, $entry->{pool});
700  
701 2 my @died;
702 {
703 2 local $@;
704 2 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $caller->fail(qq[statement timed out after $entry->{"timeout"} seconds ] . '(statement_timeout): the statement was cancelled and its ' . "connection retired, and whether it took effect is unknown\n") unless $caller->is_ready; 1 } } or push @died, $@ || "unknown error" 2 0 2
705 2 ▶ $caller->fail("statement timed out after $entry->{timeout} seconds "
Branch: 1 branch
BranchTrueFalseTotal
unless $caller->is_ready 2 0 2
706 . '(statement_timeout): the statement was cancelled and its '
707 . "connection retired, and whether it took effect is unknown\n")
708 unless $caller->is_ready;
709 2 ▶ 1;
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
710 } or push @died, $@ || 'unknown error';
711 2 ▶ eval { $self->_let_go($flight); 1 } or push @died, $@ || 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_let_go($flight); 1 } } or push @died, $@ || "unknown error" 2 0 2
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
712 }
713 2 for my $death (@died) {
714 0 chomp(my $why = "$death");
715 0 $self->_pool_report($entry->{pool}, 'A callback died as a statement was '
716 . "failed with its statement_timeout: $why");
717 }
718 2 return;
719 }
720  
721 # How many statements wait in the queue of a pinned connection. A waiting
722 # statement whose caller cancelled it is an emptied entry and does not count.
723 8 ▶sub _queue_pending {
Subroutine _queue_pending: called CC=1 SCAR=0.0
724 8 my ($queue) = @_;
725 8 return scalar grep { @$_ } @{ $queue->{waiting} };
726 }
727  
728  
729 # --- Future::IO Watcher Seam ---
730 #
731 # These methods isolate the Future::IO dependency. A driver that uses its
732 # own event-loop integration overrides _query_async / _query_async_pinned
733 # entirely and never calls these.
734  
735  
736  
737 362 ▶sub _await_readable {
Subroutine _await_readable: called CC=1 SCAR=0.0
738 362 my ($self, $conn) = @_;
739 362 return Future::IO->poll($self->_conn_poll_fh($conn), POLLIN)
740 ->then(_in_this_process_cb($$));
741 }
742  
743 # What every poll continues with: its result, in the process that made the
744 # poll -- and in any other a Future that never settles, so that nothing chained
745 # onto the poll ever runs there. A plain function, and a named one: the
746 # callback closes over a pid and nothing else (ADR 0044).
747 362 ▶sub _in_this_process_cb {
Subroutine _in_this_process_cb: called CC=1 SCAR=0.1
748 362 my ($pid) = @_;
749 362 ▶ return sub { return $$ == $pid ? Future->done(@_) : Future->new };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$$ == $pid ? : 277 0 277
750 }
751  
752  
753 94 ▶sub _let_go {
Subroutine _let_go: called CC=3 SCAR=11.0
754 94 my ($self, $f) = @_;
755 94 ▶ $f->cancel if $f->can('cancel') && !$f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("cancel") and not $f->is_ready 93 1 94
Condition66%2/3
$f->can("cancel") and not $f->is_ready
!ll&&!rl&&r
0193
MC/DC50%1/2
$f->can("cancel") and not $f->is_ready
$f->can("cancel") not $f->is_ready
Truth table66%2/3
A and not(B)
A= $f->can("cancel")B= not $f->is_ready
ABresult
0X0
100
111
756 94 return;
757 }
758  
759  
760 396 ▶sub _conn_poll_fh {
Subroutine _conn_poll_fh: called CC=4 SCAR=15.2
761 396 my ($self, $conn) = @_;
762 396 ▶ return $conn->{_dbio_async_poll_fh} ||= do {
Condition66%2/3
$conn->{"_dbio_async_poll_fh"} ||= do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
l!l&&r!l&&!r
1592370
MC/DC0%0/2
$conn->{"_dbio_async_poll_fh"} ||= do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
$conn->{"_dbio_async_poll_fh"} do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
Truth table66%2/3
A ||= B
A= $conn->{"_dbio_async_poll_fh"}B= do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
ABresult
1X1
011
000
763 237 my $fd = $self->_conn_fileno($conn);
764 237 ▶ open my $fh, '+<&', $fd
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!") 237 0 237
defined $fd ? : 0 0 0
765 or croak 'Cannot dup async connection fd '
766 . (defined $fd ? $fd : 'undef') . " for Future::IO poll: $!";
767 237 $fh;
768 };
769 }
770  
771  
772 401 ▶sub _await_conn_ready {
Subroutine _await_conn_ready: called CC=5 SCAR=16.1
773 401 my ($self, $conn, $watch) = @_;
774 401 ▶ return $self->future_class->done($conn) if $self->_conn_ready($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_conn_ready($conn) 372 27 399
775  
776 27 my $first = !$watch;
777 27 ▶ $watch = $self->_ready_watch($conn) if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 24 3 27
778  
779 27 ▶ my $ready = $self->future_class->call(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
780 27 my $poll = $self->_await_readable($conn);
781 26 ▶ if ($watch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watch) 26 0 26
782 26 $watch->{poll} = $poll;
783 26 weaken($watch->{poll});
784 }
785 26 return $poll->then($self->_ready_next_cb($conn, $watch));
786 27 });
787  
788 27 ▶ if ($first and $watch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first and $watch) 24 3 27
Condition66%2/3
$first and $watch
!ll&&!rl&&r
3024
MC/DC50%1/2
$first and $watch
$first $watch
Truth table66%2/3
A and B
A= $firstB= $watch
ABresult
0X0
100
111
789 24 $watch->{ready} = $ready;
790 24 weaken($watch->{ready});
791 24 $ready->on_ready(_ready_settled_cb($conn, $watch));
792 }
793 27 return $ready;
794 }
795  
796 # One connection's wait for readiness, kept on the connection hashref; none
797 # for a connection that is no hashref (or will not take the key).
798 24 ▶sub _ready_watch {
Subroutine _ready_watch: called CC=5 SCAR=17.3
799 24 my ($self, $conn) = @_;
800 24 ▶ return unless ref $conn and (reftype($conn) // '') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conn and (builtin::reftype($conn) // "") eq "HASH" 0 24 24
Condition40%2/5
ref $conn and (builtin::reftype($conn) // "") eq "HASH"
!ll&&!rl&&r
0024
builtin::reftype($conn) // ""
l!l
240
MC/DC0%0/2
ref $conn and (builtin::reftype($conn) // "") eq "HASH"
ref $conn builtin::reftype($conn)
Truth table33%1/3
A and B
A= ref $connB= builtin::reftype($conn)
ABresult
0X0
100
111
801 24 my $watch = {};
802 24 local $@;
803 24 ▶ eval { $conn->{_dbio_async_ready} = $watch; 1 } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $conn->{"_dbio_async_ready"} = $watch; 1 } } or return 24 0 24
804 24 return $watch;
805 }
806  
807 # Named subs, as _release_on_ready is one: they close over the connection and
808 # the bookkeeping, never over a caller's data (ADR 0044).
809 26 ▶sub _ready_next_cb {
Subroutine _ready_next_cb: called CC=1 SCAR=0.0
810 26 my ($self, $conn, $watch) = @_;
811 26 ▶ return sub { return $self->_await_conn_ready($conn, $watch) };
Subroutine __ANON__: called CC=1 SCAR=0.0
812 }
813  
814 24 ▶sub _ready_settled_cb {
Subroutine _ready_settled_cb: called CC=1 SCAR=0.0
815 24 my ($conn, $watch) = @_;
816 24 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
817 24 my $mine = $conn->{_dbio_async_ready};
818 24 ▶ delete $conn->{_dbio_async_ready} if $mine and $mine == $watch;
Branch: 1 branch
BranchTrueFalseTotal
if $mine and $mine == $watch 20 4 24
Condition66%2/3
$mine and $mine == $watch
!ll&&!rl&&r
4020
MC/DC50%1/2
$mine and $mine == $watch
$mine $mine == $watch
Truth table66%2/3
A and B
A= $mineB= $mine == $watch
ABresult
0X0
100
111
819 24 return;
820 24 };
821 }
822  
823 # The pool is closing $conn (_abandon_flights_on): the poll its readiness wait
824 # is on is let go of, the one place a poll is given up. Letting go of the poll
825 # alone settles nothing -- the Future above it stays pending -- so the wait is
826 # ended in a second step, _ready_end, which the pool takes once its own
827 # bookkeeping of the close is done. Never dies.
828 266 ▶sub _ready_let_go {
Subroutine _ready_let_go: called CC=5 SCAR=17.8
829 266 my ($self, $conn) = @_;
830 266 ▶ my $watch = $conn->{_dbio_async_ready} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watch = $conn->{"_dbio_async_ready"} or return 10 256 266
831 10 ▶ my $poll = $watch->{poll} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $poll = $watch->{"poll"} or return 10 0 10
832  
833 10 local $@;
834 10 ▶ eval { $self->_let_go($poll); 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_let_go($poll); 1 } } or do { chomp(my $why = $@ || "unknown error"); $self->_pool_report($self->_built_pool, 'A callback died as the readiness ' . "poll of a connection was let go of with its close: $why") } 10 0 10
835 0 ▶ chomp(my $why = $@ || 'unknown error');
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
836 0 $self->_pool_report($self->_built_pool, 'A callback died as the readiness '
837 . "poll of a connection was let go of with its close: $why");
838 };
839 10 return;
840 }
841  
842 # The second step (DBIO::Async::Pool::_retire_connection): the Future the pool
843 # waits on for a connection that was retired while it was coming up FAILS. The
844 # pool's chain on it is what offers the slot to the next caller (core k260 /
845 # k261), which a cancel would not. It comes after the pool parked that chain
846 # (core k261), so the caller hears the pool's own reason, not this one. Never
847 # dies.
848 63 ▶sub _ready_end {
Subroutine _ready_end: called CC=9 SCAR=26.3
849 63 my ($self, $conn) = @_;
850 63 ▶ return unless ref $conn and (reftype($conn) // '') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conn and (builtin::reftype($conn) // "") eq "HASH" 0 63 63
Condition40%2/5
ref $conn and (builtin::reftype($conn) // "") eq "HASH"
!ll&&!rl&&r
0063
builtin::reftype($conn) // ""
l!l
630
MC/DC0%0/2
ref $conn and (builtin::reftype($conn) // "") eq "HASH"
ref $conn builtin::reftype($conn)
Truth table33%1/3
A and B
A= ref $connB= builtin::reftype($conn)
ABresult
0X0
100
111
851 63 ▶ my $watch = delete $conn->{_dbio_async_ready} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watch = delete $conn->{"_dbio_async_ready"} or return 4 59 63
852 4 ▶ my $ready = $watch->{ready} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ready = $watch->{"ready"} or return 4 0 4
853  
854 4 local $@;
855 eval {
856 4 ▶ $ready->fail('Connection closed while it was coming up -- the connection '
Branch: 1 branch
BranchTrueFalseTotal
unless $ready->is_ready 4 0 4
857 . "was retired\n") unless $ready->is_ready;
858 4 1;
859 4 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ready->fail("Connection closed while it was coming up -- the connection was retired\n") unless $ready->is_ready; 1 } } or do { chomp(my $why = $@ || "unknown error"); $self->_pool_report($self->_built_pool, 'A callback died as the readiness ' . "wait of a connection was failed with its retirement: $why") } 4 0 4
860 0 ▶ chomp(my $why = $@ || 'unknown error');
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
861 0 $self->_pool_report($self->_built_pool, 'A callback died as the readiness '
862 . "wait of a connection was failed with its retirement: $why");
863 };
864 4 return;
865 }
866  
867  
868 352 ▶sub _await_query_result {
Subroutine _await_query_result: called CC=1 SCAR=0.0
869 352 my ($self, $conn, $sql, $bind) = @_;
870  
871 352 ▶ return $self->future_class->call(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
872 352 $self->_submit_query($conn, $sql, $bind);
873 343 return $self->_await_readable($conn)->then($self->_collect_on_readable($conn));
874 352 });
875 }
876  
877  
878 343 ▶sub _collect_on_readable {
Subroutine _collect_on_readable: called CC=1 SCAR=0.0
879 343 my ($self, $conn) = @_;
880 343 ▶ return sub { $self->_collect_result($conn) };
Subroutine __ANON__: called CC=1 SCAR=0.0
881 }
882  
883 # --- LISTEN / NOTIFY over Future::IO (core ADR 0056, ADR 0005, karr k22) ---
884 #
885 # The Future::IO-generic listen seams, once for every future_io driver: the
886 # timer, the watch loop, the pooled notify. The machinery that calls them is
887 # core's; the DB-specific halves (_listen_drain, _notify_sql and core's
888 # croaking _listen_open/_close/_subscribe/_unsubscribe/_probe) are the driver's.
889  
890  
891  
892 96 ▶sub _listen_delay {
Subroutine _listen_delay: called CC=1 SCAR=0.0
893 96 my ($self, $seconds) = @_;
894 96 return Future::IO->sleep($seconds);
895 }
896  
897  
898 22 ▶sub _listen_poll_interval { return undef }
Subroutine _listen_poll_interval: called CC=1 SCAR=0.0
899  
900  
901 27 ▶sub _listen_watch {
Subroutine _listen_watch: called CC=6 SCAR=17.9
902 27 my ($self, $conn) = @_;
903 27 my $interval = $self->_listen_poll_interval;
904 27 ▶ croak '_listen_poll_interval must be undef (socket-driven) or a positive '
Branch: 1 branch
BranchTrueFalseTotal
if defined $interval and not &looks_like_number($interval) && $interval > 0 1 26 27
Condition83%5/6
defined $interval and not &looks_like_number($interval) && $interval > 0
!ll&&!rl&&r
2151
&looks_like_number($interval) && $interval > 0
!ll&&!rl&&r
015
MC/DC66%2/3
defined $interval and not &looks_like_number($interval) && $interval > 0
defined $interval &looks_like_number($interval) $interval > 0
Truth table75%3/4
A and not(B && C)
A= defined $intervalB= &looks_like_number($interval)C= $interval > 0
ABCresult
0XX0
1110
10X1
1101
905 . "number of seconds (timer-driven), got '$interval'"
906 if defined $interval && !(looks_like_number($interval) && $interval > 0);
907  
908 26 ▶ $self->_conn_poll_fh($conn) unless defined $interval;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $interval 21 5 26
909  
910 26 $self->_watch_stop($conn);
911 26 ▶ my $first = $self->_listen_delay($interval // 0);
Condition100%2/2
$interval // 0
l!l
521
MC/DC100%1/1
$interval // 0
$interval
Truth table100%2/2
A
A= $interval
Aresult
11
00
912 my $watch = $conn->{_dbio_async_listen_watch} = {
913 26 pid => $$,
914 interval => $interval,
915 };
916 26 $self->_watch_await($conn, $watch, $first, '_watch_elapsed');
917 26 return;
918 }
919  
920  
921 26 ▶sub _listen_unwatch {
Subroutine _listen_unwatch: called CC=1 SCAR=0.0
922 26 my ($self, $conn) = @_;
923 26 $self->_watch_stop($conn);
924 26 return;
925 }
926  
927  
928 22 ▶sub _notify_send {
Subroutine _notify_send: called CC=1 SCAR=0.0
929 22 my ($self, $channel, $payload) = @_;
930 22 my ($sql, $bind) = $self->_notify_sql($channel, $payload);
931 22 return $self->_query_async($sql, $bind);
932 }
933  
934 # --- the watch loop's private steps ---
935 #
936 # Named subs, not nested closures, for the reason _release_on_ready gives: a
937 # callback built here closes over exactly what it is handed.
938  
939 57 ▶sub _watch_stop {
Subroutine _watch_stop: called CC=4 SCAR=13.9
940 57 my ($self, $conn) = @_;
941 57 ▶ return unless ref $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conn 0 57 57
942 # Off the connection FIRST: cancelling runs the pending Future's on_ready
943 # callback synchronously, and that callback must find the watch gone.
944 57 ▶ my $watch = delete $conn->{_dbio_async_listen_watch} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $watch = delete $conn->{"_dbio_async_listen_watch"} or return 26 31 57
945 26 ▶ my $pending = delete $watch->{pending} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = delete $watch->{"pending"} or return 19 7 26
946 19 $self->_let_go($pending);
947 19 return;
948 }
949  
950 123 ▶sub _watch_live {
Subroutine _watch_live: called CC=3 SCAR=11.0
951 123 my ($self, $conn, $watch) = @_;
952 123 my $current = $conn->{_dbio_async_listen_watch};
953 return $current
954 && refaddr($current) == refaddr($watch)
955 123 ▶ && $watch->{pid} == $$;
Condition83%5/6
$current && builtin::refaddr($current) == builtin::refaddr($watch) && $watch->{'pid'} == $$
!ll&&!rl&&r
201102
$current and builtin::refaddr($current) == builtin::refaddr($watch)
!ll&&!rl&&r
200103
MC/DC66%2/3
$current && builtin::refaddr($current) == builtin::refaddr($watch) && $watch->{'pid'} == $$
$current builtin::refaddr($current) == builtin::refaddr($watch) $watch->{'pid'} == $$
Truth table75%3/4
A and B && C
A= $currentB= builtin::refaddr($current) == builtin::refaddr($watch)C= $watch->{'pid'} == $$
ABCresult
0XX0
10X0
1100
1111
956 }
957  
958 62 ▶sub _watch_await {
Subroutine _watch_await: called CC=1 SCAR=0.0
959 62 my ($self, $conn, $watch, $f, $step) = @_;
960 62 $watch->{pending} = $f;
961 62 $f->on_ready($self->_watch_callback($conn, $watch, $step));
962 62 return;
963 }
964  
965 62 ▶sub _watch_callback {
Subroutine _watch_callback: called CC=1 SCAR=0.1
966 62 my ($self, $conn, $watch, $step) = @_;
967 62 weaken(my $weak_self = $self);
968 62 weaken(my $weak_conn = $conn);
969 62 weaken(my $weak_watch = $watch);
970 62 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=17.3
971 62 my ($f) = @_;
972 62 my ($self, $conn, $watch) = ($weak_self, $weak_conn, $weak_watch);
973 62 ▶ return unless $self && $conn && $watch && $self->_watch_live($conn, $watch);
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $conn and $watch and $self->_watch_live($conn, $watch) 19 43 62
Condition44%4/9
$self and $conn and $watch and $self->_watch_live($conn, $watch)
!ll&&!rl&&r
01943
$self and $conn and $watch
!ll&&!rl&&r
0062
$self and $conn
!ll&&!rl&&r
0062
MC/DC25%1/4
$self and $conn and $watch and $self->_watch_live($conn, $watch)
$self $conn $watch $self->_watch_live($conn, $watch)
Truth table40%2/5
A and B and C and D
A= $selfB= $connC= $watchD= $self->_watch_live($conn, $watch)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
974 43 delete $watch->{pending};
975 43 $self->_watch_run($conn, $watch, $step, $f);
976 43 return;
977 62 };
978 }
979  
980 # Every step runs from a Future callback, inside the event loop: an error
981 # escaping here would surface in whatever unrelated ->get drove the loop.
982 43 ▶sub _watch_run {
Subroutine _watch_run: called CC=4 SCAR=13.9
983 43 my ($self, $conn, $watch, $step, @args) = @_;
984 43 my $err;
985 {
986 43 local $@;
987 43 ▶ eval { $self->$step($conn, $watch, @args); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->$step($conn, $watch, @args); 1 } } or $err = $@ || "the listen watch died without a message" 38 5 43
Condition50%1/2
$@ || 'the listen watch died without a message'
l!l
50
MC/DC0%0/1
$@ || 'the listen watch died without a message'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
988 or $err = $@ || 'the listen watch died without a message';
989 }
990 43 ▶ $self->_watch_lost($conn, $watch, $err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 5 38 43
991 43 return;
992 }
993  
994 5 ▶sub _watch_lost {
Subroutine _watch_lost: called CC=3 SCAR=11.1
995 5 my ($self, $conn, $watch, $err) = @_;
996 5 ▶ return unless $self->_watch_live($conn, $watch);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_watch_live($conn, $watch) 0 5 5
997 5 $self->_watch_stop($conn);
998 5 local $@;
999 5 ▶ eval { $self->_listen_lost($conn, $err); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_listen_lost($conn, $err); 1 } } or warn "DBIO LISTEN error: reporting a lost listen connection failed: $@" 5 0 5
1000 or warn "DBIO LISTEN error: reporting a lost listen connection failed: $@";
1001 5 return;
1002 }
1003  
1004 # A timer elapsed: the first tick after the watch started, or an interval.
1005 26 ▶sub _watch_elapsed {
Subroutine _watch_elapsed: called CC=1 SCAR=0.0
1006 26 my ($self, $conn, $watch, $f) = @_;
1007 26 _watch_check($f, 'the listen watch timer');
1008 26 return $self->_watch_drain($conn, $watch);
1009 }
1010  
1011 15 ▶sub _watch_polled {
Subroutine _watch_polled: called CC=2 SCAR=6.9
1012 15 my ($self, $conn, $watch, $f) = @_;
1013 15 _watch_check($f, 'polling the listen connection');
1014 14 ▶ return $self->_watch_arm($conn, $watch)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_watch_readable_now($conn) 1 13 14
1015 unless $self->_watch_readable_now($conn);
1016 13 return $self->_watch_drain($conn, $watch);
1017 }
1018  
1019 39 ▶sub _watch_drain {
Subroutine _watch_drain: called CC=3 SCAR=11.0
1020 39 my ($self, $conn, $watch) = @_;
1021 39 my $drained = $self->_listen_drain($conn);
1022 37 ▶ return $self->_watch_await($conn, $watch, $drained, '_watch_drained')
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($drained) and $drained->can("then") 2 35 37
Condition66%2/3
builtin::blessed($drained) and $drained->can("then")
!ll&&!rl&&r
3502
MC/DC50%1/2
builtin::blessed($drained) and $drained->can("then")
builtin::blessed($drained) $drained->can("then")
Truth table66%2/3
A and B
A= builtin::blessed($drained)B= $drained->can("then")
ABresult
0X0
100
111
1023 if blessed($drained) && $drained->can('then');
1024 35 return $self->_watch_deliver($conn, $watch, $drained);
1025 }
1026  
1027 2 ▶sub _watch_drained {
Subroutine _watch_drained: called CC=1 SCAR=0.0
1028 2 my ($self, $conn, $watch, $f) = @_;
1029 2 _watch_check($f, '_listen_drain');
1030 1 my ($notifications) = $f->get;
1031 1 return $self->_watch_deliver($conn, $watch, $notifications);
1032 }
1033  
1034 36 ▶sub _watch_deliver {
Subroutine _watch_deliver: called CC=6 SCAR=18.0
1035 36 my ($self, $conn, $watch, $notifications) = @_;
1036 36 ▶ die '_listen_drain must return an arrayref of [$channel, $payload, '
Branch: 1 branch
BranchTrueFalseTotal
unless ref $notifications eq "ARRAY" 1 35 36
1037 . "\$sender_pid] tuples, or a Future of one\n"
1038 unless ref $notifications eq 'ARRAY';
1039  
1040 35 for my $n (@$notifications) {
1041 23 ▶ return unless $self->_watch_live($conn, $watch);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_watch_live($conn, $watch) 2 21 23
1042 21 ▶ die '_listen_drain returned a notification that is not an arrayref '
Branch: 1 branch
BranchTrueFalseTotal
unless ref $n eq "ARRAY" 0 21 21
1043 . "[\$channel, \$payload, \$sender_pid]\n"
1044 unless ref $n eq 'ARRAY';
1045 21 $self->_listen_dispatch(@{$n}[0 .. 2]);
1046 }
1047  
1048 33 ▶ return unless $self->_watch_live($conn, $watch);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_watch_live($conn, $watch) 0 33 33
1049 33 return $self->_watch_arm($conn, $watch);
1050 }
1051  
1052 34 ▶sub _watch_arm {
Subroutine _watch_arm: called CC=2 SCAR=6.9
1053 34 my ($self, $conn, $watch) = @_;
1054 return $self->_watch_await($conn, $watch,
1055 $self->_listen_delay($watch->{interval}), '_watch_elapsed')
1056 34 ▶ if defined $watch->{interval};
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch->{"interval"} 5 29 34
1057 29 return $self->_watch_await($conn, $watch,
1058 $self->_await_readable($conn), '_watch_polled');
1059 }
1060  
1061 # Is the socket readable right now? Asked after every poll, because the
1062 # built-in Future::IO implementation may report a poll ready without having
1063 # selected (see THE LISTEN WATCH LOOP). A select error counts as readable: the
1064 # drain is what finds out whether the connection is dead.
1065 14 ▶sub _watch_readable_now {
Subroutine _watch_readable_now: called CC=1 SCAR=0.0
1066 14 my ($self, $conn) = @_;
1067 14 my $rin = '';
1068 14 vec($rin, fileno($self->_conn_poll_fh($conn)), 1) = 1;
1069 14 return select($rin, undef, undef, 0) != 0;
1070 }
1071  
1072 # A plain function: die when a Future the loop awaited did not complete.
1073 43 ▶sub _watch_check {
Subroutine _watch_check: called CC=3 SCAR=11.0
1074 43 my ($f, $what) = @_;
1075 43 ▶ die "$what was cancelled\n" if $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled 0 43 43
1076 43 my $err = $f->failure;
1077 43 ▶ return unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 41 2 43
1078 2 chomp($err = "$err");
1079 2 die "$what failed: $err\n";
1080 }
1081  
1082 # --- Transaction context ---
1083 #
1084 # NOT seam hooks: this transport ships its own TransactionContext, so it can
1085 # answer both of these itself instead of re-croaking over core's concrete
1086 # defaults (karr k13). An adapter overrides them only for a different context
1087 # class or constructor key.
1088  
1089  
1090 42 ▶sub _txn_context_class { 'DBIO::Async::TransactionContext' }
Subroutine _txn_context_class: called CC=1 SCAR=0.0
1091  
1092  
1093 42 ▶sub _txn_conn_accessor { 'txn_conn' }
Subroutine _txn_conn_accessor: called CC=1 SCAR=0.0
1094  
1095 # --- DB-specific seam hooks ---
1096 #
1097 # Filled in by a concrete DBD subclass; each croaks until provided. The
1098 # SQL-shaping and pipeline seams are inherited as croaking seams from
1099 # DBIO::Storage::Async. These are the ones specific to the Future::IO transport,
1100 # plus _normalize_conninfo, whose inherited identity default does not fit an
1101 # abstract transport -- a concrete backend must name its own conninfo shape.
1102 # _conn_error_is_fatal and _idle_connection_alive are the ones with a default:
1103 # false and true, so a driver that fills neither keeps the behaviour from
1104 # before them (karr k26, k28).
1105  
1106  
1107 1 ▶sub _submit_query {
Subroutine _submit_query: called CC=1 SCAR=0.0
1108 1 croak 'Subclass must override _submit_query($conn, $sql, $bind)';
1109 }
1110  
1111  
1112 1 ▶sub _collect_result {
Subroutine _collect_result: called CC=1 SCAR=0.0
1113 1 croak 'Subclass must override _collect_result($conn)';
1114 }
1115  
1116  
1117 1 ▶sub _normalize_conninfo {
Subroutine _normalize_conninfo: called CC=1 SCAR=0.0
1118 1 croak 'Subclass must override _normalize_conninfo($info)';
1119 }
1120  
1121  
1122 1 ▶sub _create_pool_connection {
Subroutine _create_pool_connection: called CC=1 SCAR=0.0
1123 1 croak 'Subclass must override _create_pool_connection($conninfo)';
1124 }
1125  
1126  
1127 1 ▶sub _shutdown_pool_connection {
Subroutine _shutdown_pool_connection: called CC=1 SCAR=0.0
1128 1 croak 'Subclass must override _shutdown_pool_connection($conn)';
1129 }
1130  
1131  
1132 1 ▶sub _conn_ready {
Subroutine _conn_ready: called CC=1 SCAR=0.0
1133 1 croak 'Subclass must override _conn_ready($conn)';
1134 }
1135  
1136  
1137 1 ▶sub _conn_fileno {
Subroutine _conn_fileno: called CC=1 SCAR=0.0
1138 1 croak 'Subclass must override _conn_fileno($conn)';
1139 }
1140  
1141  
1142 16 ▶sub _conn_error_is_fatal { 0 }
Subroutine _conn_error_is_fatal: called CC=1 SCAR=0.0
1143  
1144  
1145 50 ▶sub _idle_connection_alive { 1 }
Subroutine _idle_connection_alive: called CC=1 SCAR=0.0
1146  
1147  
1148 1 ▶sub _listen_drain {
Subroutine _listen_drain: called CC=1 SCAR=0.0
1149 1 croak 'Subclass must override _listen_drain($conn)';
1150 }
1151  
1152  
1153 1 ▶sub _notify_sql {
Subroutine _notify_sql: called CC=1 SCAR=0.0
1154 1 croak 'Subclass must override _notify_sql($channel, $payload)';
1155 }
1156  
1157 44 1;
1158  
1159 __END__
↑ Summary blib/lib/DBIO/Async/Future.pm »