blib/lib/DBIO/Storage/Async.pm

filter: Statementstmt 90.3% 1238 / 1370 Branchbran 78.9% 540 / 684 Conditioncond 62.6% 284 / 453 MC/DCmcdc 41.0% 113 / 275 Subroutinesub 85.0% 233 / 274 Podpod 100% 37 / 37 totaltotal 79.0% 2445 / 3093 CC 513 SCAR 20.1
CC
513
Cov
82%
__ANON__
72.0
__ANON__
30.0
_txn_retire_on_cancel
30.0
CRAP 7.5
« blib/lib/DBIO/DeploymentHandler/VersionHandler/Monotonic.pm ↑ Summary blib/lib/DBIO.pm »
1 package DBIO::Storage::Async;
2 # ABSTRACT: Base class for async storage implementations
3  
4 109 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 109 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 109 our $VERSION = '0.900007';
8  
9 109 ▶use base 'DBIO::Storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 109 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 109 ▶use Scalar::Util qw(blessed weaken refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 109 ▶use DBIO::Storage::Async::TransactionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 109 ▶use DBIO::Storage::Async::PipelineContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 109 ▶use DBIO::Storage::PoolBase ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 109 ▶use DBIO::SQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 109 ▶use DBIO::Util qw(bare_binds mask_connect_secrets mask_statement_fingerprint);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 109 ▶use DBIO::Exception::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # Reused for its DDL grouping (deploy_async, ADR 0026 addendum / k132 async
20 # parity): the async deploy asks the SAME per-statement transactional hook and
21 # gets the SAME order/grouping as the sync path, so the grouping is a single
22 # source of truth rather than a re-implementation. DBIO::Deploy::Base itself is
23 # light -- only DBIO::SQL::Util + Scalar::Util -- and does not pull the deploy
24 # machinery. Loaded without importing (namespace clean).
25 109 ▶use DBIO::Deploy::Base ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 # A backend used as a PRIMARY storage with no embedding sync owner provisions
27 # its own private sync sibling to prepare SELECT args and anchor context
28 # ownership (ADR 0051, karr k204). DBIO::Storage::DBI does not use this class at
29 # compile time, so this is not a circular use.
30 109 ▶use DBIO::Storage::DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 109 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 1046 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
35 1046 my ($class, $schema, $args) = @_;
36 my $self = bless {
37 schema => $schema,
38 pool => undef,
39 connect_info => undef,
40 sql_maker => undef,
41 _conninfo_provider => undef,
42 1046 debug => $class->_trace_level_of($ENV{DBIO_TRACE}),
43 debugobj => undef,
44 }, $class;
45 1046 ▶ weaken($self->{schema}) if ref $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"schema"} 530 516 1046
46 1046 $self->_rotate_context_owner_token;
47 1046 return $self;
48 }
49  
50 # Each backend/pool generation is identified only by this fresh reference. It
51 # is intentionally opaque: class, DSN and schema equality prove no ownership.
52 2050 ▶sub _new_context_owner_token {
Subroutine _new_context_owner_token: called CC=1 SCAR=0.0
53 2050 my $opaque;
54 2050 return \$opaque;
55 }
56  
57 1333 ▶sub _context_owner_token {
Subroutine _context_owner_token: called CC=1 SCAR=0.0
58 1333 my ($self) = @_;
59 1333 return $self->{_context_owner_token};
60 }
61  
62 # Rotate locally first, then mirror onto the concrete sync owner only if that
63 # owner still caches this backend. DBIO::Storage::DBI performs the stale-backend
64 # identity check in _adopt_async_context_owner_token.
65 2050 ▶sub _rotate_context_owner_token {
Subroutine _rotate_context_owner_token: called CC=4 SCAR=13.9
66 2050 my ($self) = @_;
67 2050 my $token = $self->_new_context_owner_token;
68 2050 $self->{_context_owner_token} = $token;
69 2050 ▶ if (my $owner = $self->_owner_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $owner = $self->_owner_storage) 546 1504 2050
70 546 ▶ $owner->_adopt_async_context_owner_token($self, $token)
Branch: 1 branch
BranchTrueFalseTotal
if $owner->can("_adopt_async_context_owner_token") 454 92 546
71 if $owner->can('_adopt_async_context_owner_token');
72 }
73 # A self-provisioned owner (karr k204) is private to this backend and caches
74 # no backend of its own, so the stale-backend guard in
75 # _adopt_async_context_owner_token would refuse it -- seed it directly so it
76 # stays in lockstep across reconfiguration/disconnect generations.
77 2050 ▶ if (my $self_owner = $self->{_self_owner_storage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $self_owner = $self->{"_self_owner_storage"}) 2 2048 2050
78 2 $self_owner->_async_context_owner_token($token);
79 }
80 2050 return $token;
81 }
82  
83  
84 2 ▶sub future_class {
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
85 2 croak 'Subclass must override future_class';
86 }
87  
88  
89 0 ▶sub pool {
Subroutine pool: not called CC=1 SCAR=6.9
Pod documented
90 0 croak 'Subclass must override pool';
91 }
92  
93  
94 542 ▶sub _is_access_broker_connect_info {
Subroutine _is_access_broker_connect_info: called CC=4 SCAR=13.9
95 542 my ($self, $info) = @_;
96  
97 542 ▶ return 0 unless ref $info eq 'ARRAY' && @$info == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info eq "ARRAY" and @$info == 1 208 334 542
Condition100%3/3
ref $info eq "ARRAY" and @$info == 1
!ll&&!rl&&r
7201334
MC/DC100%2/2
ref $info eq "ARRAY" and @$info == 1
ref $info eq "ARRAY" @$info == 1
Truth table100%3/3
A and B
A= ref $info eq "ARRAY"B= @$info == 1
ABresult
0X0
100
111
98 334 ▶ return 0 unless blessed($info->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($info->[0]) 55 279 334
99  
100 279 return $info->[0]->isa('DBIO::AccessBroker');
101 }
102  
103  
104 200 ▶sub set_access_broker {
Subroutine set_access_broker: called CC=3 SCAR=11.0
Pod documented
105 200 my ($self, $broker, $mode) = @_;
106  
107 200 $self->next::method($broker, $mode);
108  
109 186 delete @{$self}{qw( _conninfo _pool_size _opts _access_broker_call_attributes )};
110  
111 186 weaken(my $storage = $self);
112 161 ▶ $self->{_conninfo_provider} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
113 161 ▶ croak 'This AccessBroker connect info provider cannot answer: the async '
Branch: 1 branch
BranchTrueFalseTotal
unless $storage 1 160 161
114 . 'storage it was made for is gone'
115 unless $storage;
116 # The provider outlives a detached broker: on the storage, and in a pool
117 # built with it. There is nobody to ask then, and nothing stored to
118 # connect from instead (karr k273). The words are the transports' own.
119 160 ▶ croak sprintf '%s has no AccessBroker attached to ask for connect info',
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->access_broker 17 143 160
120 ref $storage
121 unless $storage->access_broker;
122 143 return $storage->_async_broker_conninfo($storage->access_broker_mode);
123 186 };
124  
125 # A pool built before this attach holds the provider or the static conninfo
126 # it was built with. It is handed this provider, so its next connection is
127 # opened from the broker attached now. What is open stays open and a
128 # transaction stays on its connection, with the broker it began with (karr
129 # k331). Read from the slot, not through pool(): that would build a pool, and
130 # not every transport has one. A pool without the setter is left as it is.
131 186 my $built = $self->{pool};
132 $built->set_conninfo_provider($self->{_conninfo_provider})
133 186 ▶ if blessed($built) && $built->can('set_conninfo_provider');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($built) and $built->can("set_conninfo_provider") 8 178 186
Condition100%3/3
builtin::blessed($built) and $built->can("set_conninfo_provider")
!ll&&!rl&&r
17718
MC/DC100%2/2
builtin::blessed($built) and $built->can("set_conninfo_provider")
builtin::blessed($built) $built->can("set_conninfo_provider")
Truth table100%3/3
A and B
A= builtin::blessed($built)B= $built->can("set_conninfo_provider")
ABresult
0X0
100
111
134  
135 186 return $self;
136 }
137  
138  
139 316 ▶sub clear_access_broker {
Subroutine clear_access_broker: called CC=2 SCAR=6.9
Pod documented
140 316 my $self = shift;
141  
142 316 ▶ delete @{$self}{qw( _pool_size _opts _access_broker_call_attributes )}
Branch: 1 branch
BranchTrueFalseTotal
if $self->access_broker 40 276 316
143 if $self->access_broker;
144  
145 316 return $self->next::method(@_);
146 }
147  
148  
149 166 ▶sub _setup_access_broker {
Subroutine _setup_access_broker: called CC=1 SCAR=0.0
150 166 my ($self, $broker) = @_;
151 166 return $self->set_access_broker($broker, 'write');
152 }
153  
154  
155 300 ▶sub _clear_access_broker {
Subroutine _clear_access_broker: called CC=1 SCAR=0.0
156 300 my $self = shift;
157  
158 300 $self->clear_access_broker;
159 300 delete @{$self}{qw(
160 _conninfo_provider _conninfo _pool_size _opts _access_broker_call_attributes
161 )};
162  
163 300 return $self;
164 }
165  
166  
167 301 ▶sub _conninfo_provider { $_[0]->{_conninfo_provider} }
Subroutine _conninfo_provider: called CC=1 SCAR=0.0
168  
169  
170 1 ▶sub _async_broker_conninfo {
Subroutine _async_broker_conninfo: called CC=1 SCAR=0.0
171 1 croak 'Subclass must override _async_broker_conninfo to consume an AccessBroker';
172 }
173  
174  
175 607 ▶sub connect_info {
Subroutine connect_info: called CC=8 SCAR=20.8
Pod documented
176 607 my ($self, $info) = @_;
177 607 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 289 318 607
178 # A connect_info setter always opens a new ownership generation. disconnect
179 # performs the rotation when a pool exists; without a materialised pool we
180 # still rotate here so contexts cannot survive reconfiguration. Listen state
181 # goes through disconnect too: its connection was opened with the old info.
182 289 ▶ if ($self->{pool} || $self->{_listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pool'} or $self->{'_listen'}) { } 8 281 289
Condition66%2/3
$self->{"pool"} or $self->{"_listen"}
l!l&&r!l&&!r
80281
MC/DC50%1/2
$self->{"pool"} or $self->{"_listen"}
$self->{"pool"} $self->{"_listen"}
Truth table66%2/3
A or B
A= $self->{"pool"}B= $self->{"_listen"}
ABresult
1X1
011
000
183 8 $self->disconnect;
184 }
185 else {
186 281 $self->_rotate_context_owner_token;
187 }
188  
189 # ADR 0050: a connect_info is a full reconfiguration, also one that throws
190 # (karr k283). Everything the call before left is forgotten here, before
191 # anything of the new connect info is read -- reading can throw: a
192 # driver's reshape seam, the refusal of options beside a broker, the
193 # broker itself, an answer of nothing or of no usable shape, two pool
194 # sizes that disagree. What is set below is this call's.
195 289 $self->_clear_access_broker;
196 289 $self->{connect_info} = undef;
197  
198 # Strip DBIO-private attrs before the DB-specific reshape seam,
199 # so _normalize_conninfo (and DBI->connect) only see real DBD attributes.
200 289 $info = $self->_strip_private_connect_attrs($info);
201 283 $info = $self->_normalize_conninfo($info);
202 281 my $beside = $self->_access_broker_call_options($info);
203 278 $self->{connect_info} = $info;
204  
205 278 ▶ if ($beside || $self->_is_access_broker_connect_info($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beside or $self->_is_access_broker_connect_info($info)) { } 163 115 278
Condition100%3/3
$beside or $self->_is_access_broker_connect_info($info)
l!l&&r!l&&!r
25138115
MC/DC100%2/2
$beside or $self->_is_access_broker_connect_info($info)
$beside $self->_is_access_broker_connect_info($info)
Truth table100%3/3
A or B
A= $besideB= $self->_is_access_broker_connect_info($info)
ABresult
1X1
011
000
206 # No conninfo is kept with a broker attached (karr k252): every
207 # connection asks the broker anew (_conninfo_provider), so a stored copy
208 # could only ever be an earlier answer -- password included -- which
209 # nothing may use.
210 163 $self->_setup_access_broker($info->[0]);
211 # What stands beside the broker is kept before the answer is read: with
212 # the broker attached, a connection opened after a refused answer must
213 # not be opened without it.
214 163 ▶ $self->{_access_broker_call_attributes} = $beside->{attributes} if $beside;
Branch: 1 branch
BranchTrueFalseTotal
if $beside 25 138 163
215 163 my $answer = $self->_current_async_connect_info($self->access_broker_mode);
216 141 my (undef, $pool_size, $opts) = $self->_normalize_async_connect_info($answer);
217 127 my $given_pool_size = $self->_access_broker_pool_size($answer, $beside);
218 118 ▶ $pool_size = $given_pool_size if defined $given_pool_size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $given_pool_size 31 87 118
219 118 $self->{_pool_size} = $pool_size;
220 118 $self->{_opts} = $opts;
221 }
222 else {
223 115 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info($info);
224 108 $self->{_conninfo} = $conninfo;
225 108 $self->{_pool_size} = $pool_size;
226 108 $self->{_opts} = $opts;
227 }
228 }
229 544 return $self->{connect_info};
230 }
231  
232  
233 281 ▶sub _access_broker_call_options {
Subroutine _access_broker_call_options: called CC=10 SCAR=23.2
234 281 my ($self, $info) = @_;
235  
236 281 ▶ return undef unless ref $info eq 'ARRAY' && @$info == 2
Branch: 1 branch
BranchTrueFalseTotal
unless ref $info eq "ARRAY" and @$info == 2 and ref $info->[1] eq "HASH" and builtin::blessed($info->[0]) and $info->[0]->isa("DBIO::AccessBroker") 253 28 281
Condition83%10/12
ref $info eq "ARRAY" and @$info == 2 and ref $info->[1] eq "HASH" and builtin::blessed($info->[0]) and $info->[0]->isa("DBIO::AccessBroker")
!ll&&!rl&&r
253028
ref $info eq "ARRAY" and @$info == 2 and ref $info->[1] eq "HASH" and builtin::blessed($info->[0])
!ll&&!rl&&r
1866728
ref $info eq "ARRAY" and @$info == 2 and ref $info->[1] eq "HASH"
!ll&&!rl&&r
186095
ref $info eq "ARRAY" and @$info == 2
!ll&&!rl&&r
717995
MC/DC60%3/5
ref $info eq "ARRAY" and @$info == 2 and ref $info->[1] eq "HASH" and builtin::blessed($info->[0]) and $info->[0]->isa("DBIO::AccessBroker")
ref $info eq "ARRAY" @$info == 2 ref $info->[1] eq "HASH" builtin::blessed($info->[0]) $info->[0]->isa("DBIO::AccessBroker")
Truth table66%4/6
A and B and C and D and E
A= ref $info eq "ARRAY"B= @$info == 2C= ref $info->[1] eq "HASH"D= builtin::blessed($info->[0])E= $info->[0]->isa("DBIO::AccessBroker")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
237 && ref $info->[1] eq 'HASH'
238 && blessed($info->[0]) && $info->[0]->isa('DBIO::AccessBroker');
239  
240 28 my %attributes = %{ $info->[1] };
241 28 my $pool_size = delete $attributes{pool_size};
242  
243 $self->throw_exception(
244 'Transport ' . (ref($self) || $self) . ' does not take connection settings '
245 . 'beside an AccessBroker (it does not declare '
246 . "'broker_connect_options' among its transport_capabilities): "
247 28 ▶ . join(', ', sort keys %{ $info->[1] })
Branch: 1 branch
BranchTrueFalseTotal
if %attributes || defined $pool_size and not $self->_takes_broker_connect_options 3 25 28
Condition77%7/9
%attributes || defined $pool_size and not $self->_takes_broker_connect_options
!ll&&!rl&&r
2233
%attributes or defined $pool_size
l!l&&r!l&&!r
14122
ref $self || $self
l!l&&r!l&&!r
300
MC/DC100%3/3
%attributes || defined $pool_size and not $self->_takes_broker_connect_options
%attributes defined $pool_size not $self->_takes_broker_connect_options
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table100%5/5
A or B and not(C)
A= %attributesB= defined $pool_sizeC= not $self->_takes_broker_connect_options
ABCresult
00X0
1X00
0100
1X11
0111
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
248 ) if (%attributes || defined $pool_size)
249 && !$self->_takes_broker_connect_options;
250  
251 25 return { pool_size => $pool_size, attributes => \%attributes };
252 }
253  
254  
255 68 ▶sub _access_broker_call_attributes {
Subroutine _access_broker_call_attributes: called CC=2 SCAR=6.9
256 68 ▶ return $_[0]->{_access_broker_call_attributes} || {};
Condition100%2/2
$_[0]{'_access_broker_call_attributes'} || {}
l!l
1454
MC/DC100%1/1
$_[0]{'_access_broker_call_attributes'} || {}
$_[0]{'_access_broker_call_attributes'}
Truth table100%2/2
A
A= $_[0]{'_access_broker_call_attributes'}
Aresult
11
00
257 }
258  
259  
260 128 ▶sub _access_broker_pool_size {
Subroutine _access_broker_pool_size: called CC=8 SCAR=20.8
261 128 my ($self, $answer, $beside) = @_;
262  
263 # The answer's attribute hashes, a later one winning -- the slots
264 # _strip_private_connect_attrs finds, merged the way
265 # DBIO::Storage::DBI::_normalize_connect_info merges them.
266 128 my @attr_hashes;
267 128 ▶ if (ref $answer eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $answer eq "ARRAY") 128 0 128
268 @attr_hashes = ref $answer->[0] eq 'HASH' ? $answer->[0] # [ \%conninfo, \%opts ]
269 : ref $answer->[0] eq 'CODE' ? $answer->[1] # [ $coderef, \%extra ]
270 128 ▶ : @{$answer}[3, 4]; # [ $dsn, $user, $pass, \%attrs ]
Branch: 2 branches
BranchTrueFalseTotal
ref $answer->[0] eq 'HASH' ? : 109 19 128
ref $answer->[0] eq 'CODE' ? : 0 19 19
271 }
272  
273 128 my %answered;
274 128 for my $attrs (grep { ref $_ eq 'HASH' } @attr_hashes) {
275 129 ▶ $answered{pool_size} = $attrs->{pool_size} if defined $attrs->{pool_size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"pool_size"} 28 101 129
276 }
277  
278 128 my %given_beside;
279 $given_beside{pool_size} = $beside->{pool_size}
280 128 ▶ if $beside && defined $beside->{pool_size};
Branch: 1 branch
BranchTrueFalseTotal
if $beside and defined $beside->{"pool_size"} 23 105 128
Condition100%3/3
$beside and defined $beside->{"pool_size"}
!ll&&!rl&&r
103223
MC/DC100%2/2
$beside and defined $beside->{"pool_size"}
$beside defined $beside->{"pool_size"}
Truth table100%3/3
A and B
A= $besideB= defined $beside->{"pool_size"}
ABresult
0X0
100
111
281  
282 128 return $self->_merge_access_broker_settings(\%answered, \%given_beside)->{pool_size};
283 }
284  
285  
286 11 ▶sub _core_read_capabilities { qw(broker_connect_options) }
Subroutine _core_read_capabilities: called CC=1 SCAR=0.0
287  
288 34 ▶sub _takes_broker_connect_options {
Subroutine _takes_broker_connect_options: called CC=1 SCAR=0.0
289 34 my $self = shift;
290 34 return scalar grep { $_ eq 'broker_connect_options' } $self->transport_capabilities;
291 }
292  
293  
294 375 ▶sub _current_async_connect_info {
Subroutine _current_async_connect_info: called CC=4 SCAR=13.9
295 375 my ($self, $mode) = @_;
296  
297 375 ▶ return [ $self->{_conninfo}, $self->{_opts} || {} ]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_broker 72 303 375
Condition100%2/2
$self->{'_opts'} || {}
l!l
369
MC/DC100%1/1
$self->{'_opts'} || {}
$self->{'_opts'}
Truth table100%2/2
A
A= $self->{'_opts'}
Aresult
11
00
298 unless $self->access_broker;
299  
300 303 my $answer = $self->current_access_broker_connect_info($mode);
301 251 ▶ return ref $answer eq 'HASH' ? [ $answer, {} ] : $answer;
Branch: 1 branch
BranchTrueFalseTotal
ref $answer eq 'HASH' ? : 190 61 251
302 }
303  
304 # Split a [ \%conninfo, \%opts ] pair into ($conninfo, $pool_size, $opts),
305 # working on copies so the caller's structures are never mutated. pool_size
306 # is stripped out of the conninfo (default 5); opts default to an empty hash.
307 #
308 # Connect info that is something, but no array, throws -- naming its shape and,
309 # with a broker attached, the broker class it is taken to be the answer of;
310 # never a value (karr k264). $info->[0] on a plain string dies quoting the
311 # string, and that may be a DSN carrying a password. The refusal sits here
312 # rather than in _current_async_connect_info so that a transport reading the
313 # answer there can still name an unusable one in its own words.
314 370 ▶sub _normalize_async_connect_info {
Subroutine _normalize_async_connect_info: called CC=11 SCAR=24.0
315 370 my ($self, $info) = @_;
316  
317 370 ▶ if (defined $info && ref $info ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info and ref $info ne "ARRAY") 35 335 370
Condition100%3/3
defined $info and ref $info ne "ARRAY"
!ll&&!rl&&r
133435
MC/DC100%2/2
defined $info and ref $info ne "ARRAY"
defined $info ref $info ne "ARRAY"
Truth table100%3/3
A and B
A= defined $infoB= ref $info ne "ARRAY"
ABresult
0X0
100
111
318 35 ▶ my $shape = !ref $info ? 'a plain scalar'
Branch: 2 branches
BranchTrueFalseTotal
!ref($info) ? : 19 16 35
builtin::blessed($info) ? : 5 11 16
319 : blessed($info) ? 'an object of class ' . ref $info
320 : 'a ' . ref($info) . ' reference';
321 35 ▶ my $broker = ref $self ? $self->access_broker : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 35 0 35
322  
323 35 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
$broker ? : 28 7 35
324 $broker
325 ? 'AccessBroker ' . ref($broker) . ' answered connect info an async '
326 . "storage cannot connect from ($shape). It takes a hash of connection "
327 . 'parameters, or an array such as the DBI form '
328 . '[ $dsn, $user, $password, \%attrs ].'
329 : "An async storage cannot connect from this connect info ($shape). It "
330 . 'takes an array, such as [ \%conninfo, \%options ] or the DBI form '
331 . '[ $dsn, $user, $password, \%attrs ].'
332 );
333 }
334  
335 335 my $conninfo = $info->[0];
336 335 ▶ $conninfo = ref($conninfo) eq 'HASH' ? { %$conninfo } : $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
ref $conninfo eq 'HASH' ? : 284 51 335
337  
338 335 my $opts = $info->[1];
339 335 ▶ $opts = ref($opts) eq 'HASH' ? { %$opts } : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq 'HASH' ? : 270 65 335
340  
341 335 my $pool_size = 5;
342 335 ▶ if (ref($conninfo) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conninfo eq "HASH") 284 51 335
343 284 ▶ $pool_size = delete $conninfo->{pool_size} // 5;
Condition100%2/2
delete $conninfo->{"pool_size"} // 5
l!l
62222
MC/DC100%1/1
delete $conninfo->{"pool_size"} // 5
delete $conninfo->{"pool_size"}
Truth table100%2/2
A
A= delete $conninfo->{"pool_size"}
Aresult
11
00
344 }
345  
346 335 return ($conninfo, $pool_size, $opts);
347 }
348  
349  
350 272 ▶sub _normalize_conninfo { return $_[1] }
Subroutine _normalize_conninfo: called CC=1 SCAR=0.0
351  
352  
353 322 ▶sub async_option_names { () }
Subroutine async_option_names: called CC=1 SCAR=0.0
Pod documented
354  
355  
356 146 ▶sub declared_async_option_names {
Subroutine declared_async_option_names: called CC=1 SCAR=0.0
Pod documented
357 146 my ($class) = @_;
358 146 my %seen;
359 146 return grep { !$seen{$_}++ } $class->_core_async_option_names, $class->async_option_names;
360 }
361  
362  
363 791 ▶sub async_option {
Subroutine async_option: called CC=2 SCAR=6.9
Pod documented
364 791 my ($self, $name) = @_;
365 791 ▶ return $self->{_async_options} ? $self->{_async_options}{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_async_options'} ? : 64 727 791
366 }
367  
368 # The core checks the values of the options it owns; a backend checks its own
369 # (async_option_names).
370 18 ▶sub _assert_async_option {
Subroutine _assert_async_option: called CC=2 SCAR=6.9
371 18 my ($self, $name, $value) = @_;
372 DBIO::Storage::PoolBase->assert_limit_option($name => $value)
373 18 ▶ if grep { $_ eq $name } qw( acquire_timeout max_waiters txn_timeout reset_timeout );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $name;} "acquire_timeout", "max_waiters", "txn_timeout", "reset_timeout" 13 5 18
374 12 return;
375 }
376  
377  
378 0 ▶sub _delay {
Subroutine _delay: not called CC=1 SCAR=6.9
379 0 my ($self, $seconds) = @_;
380 0 return $self->_listen_delay($seconds);
381 }
382  
383 # Pull the DBIO-private connect attributes out of the incoming RAW
384 # connect info BEFORE the DB-specific _normalize_conninfo seam reshapes it (e.g.
385 # folds attrs into a libpq hash), so that seam -- and ultimately DBI->connect --
386 # only ever sees real DBD attributes. The sync path does the equivalent split in
387 # DBIO::Storage::DBI::_normalize_connect_info; both consume the same private-attr
388 # name lists on DBIO::Storage (_dbio_storage_option_names /
389 # _dbio_sql_maker_option_names), so the two paths cannot drift.
390 #
391 # The incoming info is the raw DBI array form _async_storage hands us (the same
392 # shapes _normalize_connect_info parses): dsn/user/pass + \%attrs[+\%extra],
393 # a single \%hash (Catalyst-style), or a coderef + \%extra. We locate the attr
394 # hash(es) of whichever form, strip the private attrs out of a COPY, and pass
395 # everything else through untouched -- never mutating the caller's structure.
396 # Broker-form info ([$broker]) carries no caller attr hash and is left as-is;
397 # the connect options beside a broker ([$broker, \%options], karr k243) are
398 # stripped like any other attr hash.
399 290 ▶sub _strip_private_connect_attrs {
Subroutine _strip_private_connect_attrs: called CC=15 SCAR=27.1
400 290 my ($self, $info) = @_;
401  
402 # Every call is a new reading of the connect info: what the one before left
403 # as async options is gone, whatever form this one comes in (karr core k397).
404 290 $self->{_async_options} = {};
405 290 my %async_options;
406  
407 290 ▶ return $info
Branch: 1 branch
BranchTrueFalseTotal
if ref $info ne "ARRAY" or $self->_is_access_broker_connect_info($info) 145 145 290
Condition100%3/3
ref $info ne "ARRAY" or $self->_is_access_broker_connect_info($info)
l!l&&r!l&&!r
7138145
MC/DC100%2/2
ref $info ne "ARRAY" or $self->_is_access_broker_connect_info($info)
ref $info ne "ARRAY" $self->_is_access_broker_connect_info($info)
Truth table100%3/3
A or B
A= ref $info ne "ARRAY"B= $self->_is_access_broker_connect_info($info)
ABresult
1X1
011
000
408 if ref $info ne 'ARRAY'
409 or $self->_is_access_broker_connect_info($info);
410  
411 145 my @args = @$info; # shallow copy; the attr hashes below are copied before mutation
412  
413 145 my @attr_slots;
414 145 ▶ if (ref $args[0] eq 'CODE') { # coderef + optional \%extra_attributes
Branch: 3 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'CODE') { } 7 138 145
elsif (builtin::blessed($args[0]) and $args[0]->isa('DBIO::AccessBroker')) { } 28 110 138
elsif (ref $args[0] eq 'HASH') { } 86 24 110
415 7 ▶ push @attr_slots, \$args[1] if ref $args[1] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[1] eq "HASH" 7 0 7
416 }
417 ▶ elsif (blessed($args[0]) && $args[0]->isa('DBIO::AccessBroker')) {
Condition66%2/3
builtin::blessed($args[0]) and $args[0]->isa("DBIO::AccessBroker")
!ll&&!rl&&r
110028
MC/DC50%1/2
builtin::blessed($args[0]) and $args[0]->isa("DBIO::AccessBroker")
builtin::blessed($args[0]) $args[0]->isa("DBIO::AccessBroker")
Truth table66%2/3
A and B
A= builtin::blessed($args[0])B= $args[0]->isa("DBIO::AccessBroker")
ABresult
0X0
100
111
418 28 ▶ push @attr_slots, \$args[1] if ref $args[1] eq 'HASH'; # broker + \%options
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[1] eq "HASH" 28 0 28
419 }
420 elsif (ref $args[0] eq 'HASH') { # single \%hash (Catalyst-style config)
421 86 push @attr_slots, \$args[0];
422 }
423 else { # dsn/user/pass + \%attrs [+ \%extra_attrs]
424 24 push @attr_slots, \$args[$_] for grep ref $args[$_] eq 'HASH', 3, 4;
425 }
426  
427 145 my @storage_opt_names = ($self->_dbio_storage_option_names, 'cursor_class');
428 145 my %storage_options;
429  
430 145 for my $slot (@attr_slots) {
431 144 my %attrs = %{ $$slot }; # copy before mutating
432  
433 # storage_options + cursor_class -> keep for the storage, out of
434 # DBI->connect. Same sink the sync path uses (DBIO::Storage::DBI stashes
435 # these as storage_options). Held for the pool on_connect seam
436 # (on_connect_do / on_connect_call) to consume; no further processing here.
437 $storage_options{$_} = delete $attrs{$_}
438 144 for grep exists $attrs{$_}, @storage_opt_names;
439  
440 # quote_char/name_sep/quote_names -> sql_maker config, not DBD attrs. The
441 # async sql_maker path (_sql_maker_args) does not source quoting from
442 # connect_info yet (driver adapters set quote_char statically), so dropping
443 # them is correct for now. Their proper future sink is the backend's
444 # sql_maker -- see the TODO block in t/storage/async_connect_info_leak.t so
445 # a later async-quoting feature does not pull them back into raw DBD attrs.
446 144 delete @attrs{ $self->_dbio_sql_maker_option_names };
447  
448 # async -> mode selector; the backend already knows its own mode, so it must
449 # never reach DBI->connect. ignore_version -> consumed by the
450 # DeploymentHandler version-storage component, never by the DBD.
451 144 delete @attrs{qw( async ignore_version )};
452  
453 # The async options other than pool_size -- the core's and those this
454 # backend declares (async_option_names) -- are the backend's own: kept for
455 # async_option, out of what reaches the transport's reshape seam and
456 # DBI->connect. pool_size stays where it always was, in the conninfo, for
457 # _normalize_async_connect_info. A later slot wins, as the attribute hashes
458 # of the DBI form merge (karr core k397).
459 144 for my $name (grep { $_ ne 'pool_size' } $self->declared_async_option_names) {
460 574 ▶ next unless exists $attrs{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs{$name} 556 18 574
461 18 my $value = delete $attrs{$name};
462 18 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 18 18
463 18 $self->_assert_async_option($name => $value);
464 12 $async_options{$name} = $value;
465 }
466  
467 138 $$slot = \%attrs;
468 }
469  
470 # Slot the storage options aside; nothing consumes them yet.
471 139 $self->{_storage_options} = \%storage_options;
472 139 $self->{_async_options} = \%async_options;
473  
474 139 return \@args;
475 }
476  
477  
478 3670 ▶sub _owner_storage {
Subroutine _owner_storage: called CC=4 SCAR=13.9
479 3670 my $self = shift;
480 3670 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 730 2940 3670
481 730 $self->{_owner_storage} = $_[0];
482 730 ▶ weaken($self->{_owner_storage}) if ref $self->{_owner_storage};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"_owner_storage"} 729 1 730
483 # An external owner supersedes any self-provisioned sibling: the embedding
484 # sync storage is the authoritative owner (it carries the real capabilities,
485 # on_connect actions and DSN). Drop the fallback so there is exactly one.
486 730 ▶ delete $self->{_self_owner_storage} if ref $self->{_owner_storage};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"_owner_storage"} 729 1 730
487 }
488 3670 return $self->{_owner_storage};
489 }
490  
491  
492 495 ▶sub _effective_owner_storage {
Subroutine _effective_owner_storage: called CC=3 SCAR=11.0
493 495 my ($self) = @_;
494 495 ▶ if (my $owner = $self->_owner_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $owner = $self->_owner_storage) 489 6 495
495 489 return $owner;
496 }
497 6 ▶ return $self->{_self_owner_storage} ||= $self->_build_self_owner_storage;
Condition66%2/3
$self->{"_self_owner_storage"} ||= $self->_build_self_owner_storage
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->{"_self_owner_storage"} ||= $self->_build_self_owner_storage
$self->{"_self_owner_storage"} $self->_build_self_owner_storage
Truth table66%2/3
A ||= B
A= $self->{"_self_owner_storage"}B= $self->_build_self_owner_storage
ABresult
1X1
011
000
498 }
499  
500 # Build the private sync owner for a primary (unowned) backend. It is a plain
501 # DBIO::Storage::DBI -- _select_args is core-only, no driver overrides it -- that
502 # SHARES this backend's already-configured sql_maker, so the args it prepares
503 # are byte-identical to the ones the async path would build with its own maker
504 # (the mock-cursor / real-cursor parity invariant, karr core k55 / ADR 0046).
505 # It carries this backend's current context-owner token so ownership assertions
506 # on it succeed, and it is held STRONGLY (unlike the weak _owner_storage
507 # back-reference) because nothing else keeps it alive. No connection is opened:
508 # _select_args and the token machinery never touch a dbh.
509 4 ▶sub _build_self_owner_storage {
Subroutine _build_self_owner_storage: called CC=1 SCAR=0.0
510 4 my ($self) = @_;
511 4 my $owner = DBIO::Storage::DBI->new($self->schema);
512 4 $owner->_sql_maker($self->sql_maker);
513 4 $owner->_async_context_owner_token($self->_context_owner_token);
514 4 return $owner;
515 }
516  
517 # Resolve the effective owner of $storage with backward compatibility for a
518 # storage that fulfils only the older _owner_storage contract and never gained
519 # _effective_owner_storage -- a bare driver MOCK that plays the backend role for
520 # a context without subclassing this class (karr k204 addendum). A real async
521 # storage runs its self-provisioning resolver; such a mock falls back to its
522 # wired _owner_storage, which IS its effective owner (a wired owner always wins
523 # over a self-provisioned sibling), and an ownerless mock still yields undef so
524 # the caller throws the same "requires its owning sync storage" /
525 # "ownership cannot be proven" message as before. Single source of truth so the
526 # call-sites (here, TransactionContext, PipelineContext) never drift. A plain
527 # function, not a method: the invocant may lack the method entirely.
528 502 ▶sub _effective_owner_of {
Subroutine _effective_owner_of: called CC=2 SCAR=6.9
529 502 my ($storage) = @_;
530 502 ▶ return $storage->can('_effective_owner_storage')
Branch: 1 branch
BranchTrueFalseTotal
$storage->can('_effective_owner_storage') ? : 494 8 502
531 ? $storage->_effective_owner_storage
532 : $storage->_owner_storage;
533 }
534  
535  
536 51 ▶sub _setup_pool_connection {
Subroutine _setup_pool_connection: called CC=3 SCAR=11.0
537 51 my ($self, $conn) = @_;
538  
539 51 ▶ my $owner = $self->_owner_storage or return;
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->_owner_storage or return 50 1 51
540 50 ▶ return unless $owner->can('_run_pool_connect_actions');
Branch: 1 branch
BranchTrueFalseTotal
unless $owner->can("_run_pool_connect_actions") 13 37 50
541  
542 37 $owner->_run_pool_connect_actions($self->_pool_connect_runner($conn));
543  
544 31 return;
545 }
546  
547  
548 40 ▶sub _teardown_pool_connection {
Subroutine _teardown_pool_connection: called CC=3 SCAR=11.0
549 40 my ($self, $conn) = @_;
550  
551 40 ▶ my $owner = $self->_owner_storage or return;
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->_owner_storage or return 32 8 40
552 32 ▶ return unless $owner->can('_run_pool_disconnect_actions');
Branch: 1 branch
BranchTrueFalseTotal
unless $owner->can("_run_pool_disconnect_actions") 13 19 32
553  
554 19 $owner->_run_pool_disconnect_actions($self->_pool_connect_runner($conn));
555  
556 19 return;
557 }
558  
559 # The runner the owning sync storage gets for the statements of the connect and
560 # disconnect actions: it executes each on $conn through
561 # _run_pool_connect_statement and traces it like the sync path does
562 # (_do_query): _debug_query with the statement in its fingerprint form, no
563 # binds shown, query_end after the statement succeeded. The row queries of a
564 # connect_call_* (dbio_rows) are not traced and return their rows.
565 56 ▶sub _pool_connect_runner {
Subroutine _pool_connect_runner: called CC=1 SCAR=0.0
566 56 my ($self, $conn) = @_;
567  
568 31 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
569 31 my ($sql, $attrs, @bind) = @_;
570  
571 31 ▶ my $rows_query = ref $attrs eq 'HASH' && $attrs->{dbio_rows};
Condition66%2/3
ref $attrs eq 'HASH' && $attrs->{'dbio_rows'}
!ll&&!rl&&r
2902
MC/DC50%1/2
ref $attrs eq 'HASH' && $attrs->{'dbio_rows'}
ref $attrs eq 'HASH' $attrs->{'dbio_rows'}
Truth table66%2/3
A && B
A= ref $attrs eq 'HASH'B= $attrs->{'dbio_rows'}
ABresult
0X0
100
111
572 31 ▶ my $end = $rows_query
Branch: 1 branch
BranchTrueFalseTotal
$rows_query ? : 2 29 31
573 ? undef
574 : $self->_debug_query(mask_statement_fingerprint($sql), \@bind);
575  
576 31 my @result = $self->_run_pool_connect_statement($conn, $sql, $attrs, @bind);
577 25 ▶ $end->() if $end;
Branch: 1 branch
BranchTrueFalseTotal
if $end 6 19 25
578  
579 25 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 25 25
580 56 };
581 }
582  
583  
584 0 ▶sub _reset_pool_connection {
Subroutine _reset_pool_connection: not called CC=1 SCAR=6.9
585 0 my ($self, $conn) = @_;
586 0 return $self->future_class->fail(
587 ref($self) . ' has no _reset_pool_connection: the connection is retired'
588 );
589 }
590  
591  
592 27 ▶sub _run_pool_connect_statement {
Subroutine _run_pool_connect_statement: called CC=3 SCAR=11.0
593 27 my ($self, $conn, $sql, @args) = @_;
594 27 my $dbh = $self->_pool_connect_dbh($conn);
595  
596 26 ▶ if (ref $args[0] eq 'HASH' and $args[0]{dbio_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH" and $args[0]{"dbio_rows"}) 3 23 26
Condition100%3/3
ref $args[0] eq "HASH" and $args[0]{"dbio_rows"}
!ll&&!rl&&r
2213
MC/DC100%2/2
ref $args[0] eq "HASH" and $args[0]{"dbio_rows"}
ref $args[0] eq "HASH" $args[0]{"dbio_rows"}
Truth table100%3/3
A and B
A= ref $args[0] eq "HASH"B= $args[0]{"dbio_rows"}
ABresult
0X0
100
111
597 3 my (undef, @bind) = @args;
598 3 return $dbh->selectall_arrayref($sql, undef, @bind);
599 }
600  
601 23 $dbh->do($sql, @args);
602 17 return;
603 }
604  
605 # Extract a blocking-do-capable DBI handle from a pool connection value. The two
606 # shapes the DBD-based Model-B backends use are a bare $dbh and the future_io
607 # { dbh => $dbh } wrapper (DBIO::PostgreSQL::Storage::Async::_create_pool_connection
608 # and friends). Anything else is a native backend that must override
609 # _run_pool_connect_statement.
610 27 ▶sub _pool_connect_dbh {
Subroutine _pool_connect_dbh: called CC=6 SCAR=18.2
611 27 my ($self, $conn) = @_;
612  
613 27 ▶ return $conn
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($conn) and $conn->can("do") 3 24 27
Condition66%2/3
builtin::blessed($conn) and $conn->can("do")
!ll&&!rl&&r
2403
MC/DC50%1/2
builtin::blessed($conn) and $conn->can("do")
builtin::blessed($conn) $conn->can("do")
Truth table66%2/3
A and B
A= builtin::blessed($conn)B= $conn->can("do")
ABresult
0X0
100
111
614 if blessed($conn) && $conn->can('do');
615  
616 return $conn->{dbh}
617 24 ▶ if ref $conn eq 'HASH' && blessed($conn->{dbh}) && $conn->{dbh}->can('do');
Branch: 1 branch
BranchTrueFalseTotal
if ref $conn eq "HASH" and builtin::blessed($conn->{"dbh"}) and $conn->{"dbh"}->can("do") 23 1 24
Condition66%4/6
ref $conn eq "HASH" and builtin::blessed($conn->{"dbh"}) and $conn->{"dbh"}->can("do")
!ll&&!rl&&r
1023
ref $conn eq "HASH" and builtin::blessed($conn->{"dbh"})
!ll&&!rl&&r
1023
MC/DC33%1/3
ref $conn eq "HASH" and builtin::blessed($conn->{"dbh"}) and $conn->{"dbh"}->can("do")
ref $conn eq "HASH" builtin::blessed($conn->{"dbh"}) $conn->{"dbh"}->can("do")
Truth table50%2/4
A and B and C
A= ref $conn eq "HASH"B= builtin::blessed($conn->{"dbh"})C= $conn->{"dbh"}->can("do")
ABCresult
0XX0
10X0
1100
1111
618  
619 1 croak 'Cannot run pool connect action: connection is neither a do-capable '
620 . 'DBI handle nor a { dbh => $dbh } wrapper -- override '
621 . '_run_pool_connect_statement for this backend';
622 }
623  
624  
625 465 ▶sub sql_maker {
Subroutine sql_maker: called CC=2 SCAR=7.0
Pod documented
626 465 my $self = shift;
627 465 ▶ $self->{sql_maker} ||= do {
Condition66%2/3
$self->{"sql_maker"} ||= do { my $class = $self->sql_maker_class; $class->new($self->_sql_maker_args) }
l!l&&r!l&&!r
2152500
MC/DC0%0/2
$self->{"sql_maker"} ||= do { my $class = $self->sql_maker_class; $class->new($self->_sql_maker_args) }
$self->{"sql_maker"} do { my $class = $self->sql_maker_class; $class->new($self->_sql_maker_args) }
Truth table66%2/3
A ||= B
A= $self->{"sql_maker"}B= do { my $class = $self->sql_maker_class; $class->new($self->_sql_maker_args) }
ABresult
1X1
011
000
628 250 my $class = $self->sql_maker_class;
629 250 $class->new($self->_sql_maker_args);
630 };
631 }
632  
633  
634 0 ▶sub sql_maker_class {
Subroutine sql_maker_class: not called CC=1 SCAR=6.9
Pod documented
635 0 croak 'Subclass must override sql_maker_class';
636 }
637  
638  
639 3 ▶sub _sql_maker_args { () }
Subroutine _sql_maker_args: called CC=1 SCAR=0.0
640  
641  
642 33 ▶sub transport_capabilities { () }
Subroutine transport_capabilities: called CC=1 SCAR=0.0
Pod documented
643  
644  
645 79 ▶sub select_async {
Subroutine select_async: called CC=1 SCAR=0.0
Pod documented
646 79 my $self = shift;
647 79 return $self->_run_crud_pooled('select', @_);
648 }
649  
650  
651 62 ▶sub select_single_async {
Subroutine select_single_async: called CC=1 SCAR=0.0
Pod documented
652 62 my $self = shift;
653 62 return $self->_run_crud_pooled('select_single', @_);
654 }
655  
656  
657 40 ▶sub insert_async {
Subroutine insert_async: called CC=1 SCAR=0.0
Pod documented
658 40 my $self = shift;
659 40 return $self->_run_crud_pooled('insert', @_);
660 }
661  
662  
663 30 ▶sub update_async {
Subroutine update_async: called CC=1 SCAR=0.0
Pod documented
664 30 my $self = shift;
665 30 return $self->_run_crud_pooled('update', @_);
666 }
667  
668  
669 24 ▶sub delete_async {
Subroutine delete_async: called CC=1 SCAR=0.0
Pod documented
670 24 my $self = shift;
671 24 return $self->_run_crud_pooled('delete', @_);
672 }
673  
674 # Prepare a SELECT through the owning sync storage, preserving the exact
675 # validation, join pruning and complex-prefetch rewrite its cursor path uses.
676 # _select_args returns the operation name first; the async SQLMaker consumes
677 # only the prepared select arguments that follow it.
678 227 ▶sub _prepare_select_args_async {
Subroutine _prepare_select_args_async: called CC=4 SCAR=14.2
679 227 my ($self, $ident, $select, $where, $attrs) = @_;
680 227 ▶ $attrs = {} unless defined $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 23 204 227
681  
682 227 my $owner = _effective_owner_of($self);
683 227 ▶ $self->throw_exception('async SELECT requires its owning sync storage')
Branch: 1 branch
BranchTrueFalseTotal
unless $owner and $owner->can("_select_args") 0 227 227
Condition33%1/3
$owner and $owner->can("_select_args")
!ll&&!rl&&r
00227
MC/DC0%0/2
$owner and $owner->can("_select_args")
$owner $owner->can("_select_args")
Truth table33%1/3
A and B
A= $ownerB= $owner->can("_select_args")
ABresult
0X0
100
111
684 unless $owner && $owner->can('_select_args');
685  
686 227 my @prepared = $owner->_select_args($ident, $select, $where, $attrs);
687 224 shift @prepared;
688 224 return @prepared;
689 }
690  
691  
692 # The DB-specific INSERT-column retrieval (_post_insert_sql) and first-row
693 # post-processing for select_single live here exactly once, so the pooled path
694 # (_pool_runner) and the pinned txn path (_pinned_runner, used by the
695 # TransactionContext) share identical behaviour.
696 438 ▶sub _run_crud {
Subroutine _run_crud: called CC=27 SCAR=33.2
697 438 my ($self, $op, $runner, @args) = @_;
698  
699 438 my $sm = $self->sql_maker;
700 438 ▶ if ($op eq 'select' || $op eq 'select_single') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq 'select' or $op eq 'select_single') { } 226 212 438
elsif ($op eq 'insert') { } 121 91 212
elsif ($op eq 'update') { } 51 40 91
elsif ($op eq 'delete') { } 40 0 40
Condition100%3/3
$op eq "select" or $op eq "select_single"
l!l&&r!l&&!r
14482212
MC/DC100%2/2
$op eq "select" or $op eq "select_single"
$op eq "select" $op eq "select_single"
Truth table100%3/3
A or B
A= $op eq "select"B= $op eq "select_single"
ABresult
1X1
011
000
701 226 my $ident = $args[0];
702 226 @args = $self->_prepare_select_args_async(@args);
703 223 ▶ my $attrs = $args[3] || {};
Condition50%1/2
$args[3] || {}
l!l
2230
MC/DC0%0/1
$args[3] || {}
$args[3]
Truth table50%1/2
A
A= $args[3]
Aresult
11
00
704 223 my ($sql, @bind) = $sm->select(@args);
705 # as in _gen_sql_bind: the values of the bind attribute go first
706 223 ▶ @bind = bare_binds(@{ $attrs->{bind} || [] }, @bind);
Condition100%2/2
$attrs->{'bind'} || []
l!l
14776
MC/DC100%1/1
$attrs->{'bind'} || []
$attrs->{'bind'}
Truth table100%2/2
A
A= $attrs->{'bind'}
Aresult
11
00
707 223 my $bind_attrs = $self->_crud_bind_attrs(select => $ident, \@bind, @args);
708 223 my $future = $runner->($sql, \@bind, $bind_attrs);
709  
710 223 ▶ if ($op eq 'select' && $attrs->{software_limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "select" and $attrs->{"software_limit"}) 6 217 223
Condition100%3/3
$op eq "select" and $attrs->{"software_limit"}
!ll&&!rl&&r
821356
MC/DC100%2/2
$op eq "select" and $attrs->{"software_limit"}
$op eq "select" $attrs->{"software_limit"}
Truth table100%3/3
A and B
A= $op eq "select"B= $attrs->{"software_limit"}
ABresult
0X0
100
111
711 6 my $rows = $attrs->{rows};
712 6 ▶ my $offset = $attrs->{offset} || 0;
Condition100%2/2
$attrs->{'offset'} || 0
l!l
15
MC/DC100%1/1
$attrs->{'offset'} || 0
$attrs->{'offset'}
Truth table100%2/2
A
A= $attrs->{'offset'}
Aresult
11
00
713 6 my $fc = $self->future_class;
714 4 ▶ $future = $future->then(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
715 4 my @raw_rows = @_;
716 4 ▶ splice @raw_rows, 0, $offset if $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset 1 3 4
717 4 ▶ splice @raw_rows, $rows if $rows && @raw_rows > $rows;
Branch: 1 branch
BranchTrueFalseTotal
if $rows and @raw_rows > $rows 1 3 4
Condition66%2/3
$rows and @raw_rows > $rows
!ll&&!rl&&r
031
MC/DC50%1/2
$rows and @raw_rows > $rows
$rows @raw_rows > $rows
Truth table66%2/3
A and B
A= $rowsB= @raw_rows > $rows
ABresult
0X0
100
111
718 4 return $fc->done(@raw_rows);
719 6 });
720 }
721  
722 223 ▶ return $self->_hold_derived($future) unless $op eq 'select_single';
Branch: 1 branch
BranchTrueFalseTotal
unless $op eq "select_single" 141 82 223
723 78 ▶ return $self->_hold_derived($future->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
724 78 my @rows = @_;
725 78 ▶ return @rows ? $rows[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 61 17 78
726 82 }));
727 }
728 elsif ($op eq 'insert') {
729 # The CRUD contract is insert_async($source, \%rowdata) (ADR 0031 §3),
730 # where $source is a result source. SQLMaker wants a plain table name,
731 # so unwrap a blessed source to its ->name; a bare string passes through.
732 121 my ($source, @rest) = @args;
733 121 my $to_insert = $rest[0];
734 121 ▶ my $table = (blessed($source) && $source->can('name'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($source) && $source->can('name') ? : 73 48 121
Condition66%2/3
builtin::blessed($source) and $source->can("name")
!ll&&!rl&&r
48073
MC/DC50%1/2
builtin::blessed($source) and $source->can("name")
builtin::blessed($source) $source->can("name")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("name")
ABresult
0X0
100
111
735 ? $source->name
736 : $source;
737  
738 # The sync insert fills an auto_nextval column that has no value through
739 # _prefetch_autovalues; this path has no such seam yet, so a missing value
740 # is refused here instead of reaching the server as NULL or a NOT NULL
741 # error.
742 121 ▶ if (blessed($source) && $source->can('columns_info')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($source) and $source->can("columns_info")) 73 48 121
Condition66%2/3
builtin::blessed($source) and $source->can("columns_info")
!ll&&!rl&&r
48073
MC/DC50%1/2
builtin::blessed($source) and $source->can("columns_info")
builtin::blessed($source) $source->can("columns_info")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("columns_info")
ABresult
0X0
100
111
743 73 my $colinfo = $source->columns_info;
744 73 ▶ if (my @missing = grep {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = grep({$colinfo->{$_}{"auto_nextval"} and not ref $to_insert eq "HASH" && exists $to_insert->{$_};} sort(keys %$colinfo))) 1 72 73
745 $colinfo->{$_}{auto_nextval}
746 280 ▶ and ! (ref $to_insert eq 'HASH' && exists $to_insert->{$_})
Condition83%5/6
$colinfo->{$_}{'auto_nextval'} && !(ref $to_insert eq 'HASH' && exists $to_insert->{$_})
!ll&&!rl&&r
27721
ref $to_insert eq 'HASH' && exists $to_insert->{$_}
!ll&&!rl&&r
012
MC/DC66%2/3
$colinfo->{$_}{'auto_nextval'} && !(ref $to_insert eq 'HASH' && exists $to_insert->{$_})
$colinfo->{$_}{'auto_nextval'} ref $to_insert eq 'HASH' exists $to_insert->{$_}
Truth table75%3/4
A && not(B && C)
A= $colinfo->{$_}{'auto_nextval'}B= ref $to_insert eq 'HASH'C= exists $to_insert->{$_}
ABCresult
0XX0
1110
10X1
1101
747 } sort keys %$colinfo) {
748 $self->throw_exception( sprintf(
749 'insert_async cannot fill the auto_nextval column%s %s of %s: the '
750 . 'asynchronous insert does not fetch the sequence value the '
751 . 'synchronous insert fetches. Give %s a value on insert (literal '
752 . 'SQL such as \\"nextval(...)" counts), or use the synchronous '
753 . 'insert',
754 ( @missing > 1 ? 's' : '' ),
755 1 ▶ join(', ', map { "'$_'" } @missing),
Branch: 2 branches
BranchTrueFalseTotal
@missing > 1 ? : 0 1 1
@missing > 1 ? : 0 1 1
756 $source->source_name,
757 ( @missing > 1 ? 'them' : 'it' ),
758 ));
759 }
760 }
761  
762 120 my ($sql, @bind) = $sm->insert($table, _own_options(insert => @rest));
763 120 @bind = bare_binds(@bind);
764 120 my $post = $self->_post_insert_sql;
765 120 ▶ $sql .= $post if length $post && $sql !~ /RETURNING/i;
Branch: 1 branch
BranchTrueFalseTotal
if length $post and not $sql =~ /RETURNING/i 107 13 120
Condition66%2/3
length $post and not $sql =~ /RETURNING/i
!ll&&!rl&&r
130107
MC/DC50%1/2
length $post and not $sql =~ /RETURNING/i
length $post not $sql =~ /RETURNING/i
Truth table66%2/3
A and not(B)
A= length $postB= not $sql =~ /RETURNING/i
ABresult
0X0
100
111
766 120 my $bind_attrs = $self->_crud_bind_attrs(insert => $source, \@bind, $table, @rest);
767  
768 # ADR 0031 §3: insert_async must resolve the returned-columns HASHREF --
769 # the supplied insert data overlaid with the DB-populated columns (autoinc
770 # PK + retrieve-on-insert), exactly what sync $storage->insert returns --
771 # so create_async / Row::insert_async can fold it back via
772 # _store_inserted_columns. The runner yields the raw RETURNING row(s); map
773 # the first onto the insert data. (select_async / select_single_async keep
774 # resolving raw arrayrefs, matching the sync cursor shape.)
775 93 ▶ return $self->_hold_derived($runner->($sql, \@bind, $bind_attrs)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
776 93 my @rows = @_;
777 93 return $self->_insert_returned_columns($source, $to_insert, $rows[0]);
778 120 }));
779 }
780 elsif ($op eq 'update') {
781 # Same CRUD contract shape as insert: update_async($source, \%vals,
782 # \%where), $source a result source. SQLMaker wants a plain table name --
783 # unwrap a blessed source to its ->name here too, exactly like the insert
784 # branch above; a bare string passes through unchanged.
785 51 my ($source, @rest) = @args;
786 51 ▶ my $table = (blessed($source) && $source->can('name'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($source) && $source->can('name') ? : 37 14 51
Condition66%2/3
builtin::blessed($source) and $source->can("name")
!ll&&!rl&&r
14037
MC/DC50%1/2
builtin::blessed($source) and $source->can("name")
builtin::blessed($source) $source->can("name")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("name")
ABresult
0X0
100
111
787 ? $source->name
788 : $source;
789  
790 51 my ($sql, @bind) = $sm->update($table, _own_options(update => @rest));
791 51 @bind = bare_binds(@bind);
792 51 my $bind_attrs = $self->_crud_bind_attrs(update => $source, \@bind, $table, @rest);
793  
794 # update_async must resolve the affected-row COUNT (a plain scalar),
795 # matching what sync $storage->update returns -- Row::update_async relies
796 # on this to detect "row not found" / "updated more than one row" exactly
797 # like sync _update_body does. Extract exactly the one resolved value the
798 # transport is contracted to provide for this op (see _query_async POD),
799 # the same way the insert branch above extracts $rows[0] rather than
800 # handing the caller a raw, unshaped list.
801 48 ▶ return $self->_hold_derived($runner->($sql, \@bind, $bind_attrs)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
802 48 my ($rows) = @_;
803 48 return $rows;
804 51 }));
805 }
806 elsif ($op eq 'delete') {
807 40 my ($source, @rest) = @args;
808 40 ▶ my $table = (blessed($source) && $source->can('name'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($source) && $source->can('name') ? : 13 27 40
Condition66%2/3
builtin::blessed($source) and $source->can("name")
!ll&&!rl&&r
27013
MC/DC50%1/2
builtin::blessed($source) and $source->can("name")
builtin::blessed($source) $source->can("name")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("name")
ABresult
0X0
100
111
809 ? $source->name
810 : $source;
811  
812 40 my ($sql, @bind) = $sm->delete($table, _own_options(delete => @rest));
813 40 @bind = bare_binds(@bind);
814 40 my $bind_attrs = $self->_crud_bind_attrs(delete => $source, \@bind, $table, @rest);
815  
816 # Same affected-row-count contract as update_async, above.
817 32 ▶ return $self->_hold_derived($runner->($sql, \@bind, $bind_attrs)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
818 32 my ($rows) = @_;
819 32 return $rows;
820 40 }));
821 }
822 0 croak "Unknown CRUD operation: $op";
823 }
824  
825 # The Future a seam hands its caller, derived with ->then from the pool's
826 # acquire, is held the same way (karr k407): pipeline, the pooled branch of
827 # deploy_async and a pooled DDL run.
828 #
829 # The Future _run_crud hands its caller is derived with ->then from the one
830 # the transport's query seam returned (karr k331). With Future.pm that is a
831 # sequence the transport's Future holds only weakly, so a caller that does not
832 # keep it -- a pooled insert_async in void context, a statement dropped inside
833 # a pipeline batch -- freed it, and Future.pm warned "lost a sequence Future"
834 # when the answer came. Same remedy as the transaction context's statements
835 # (karr k292): hand the Future to itself with retain, which keeps it until it
836 # is ready -- done, failed or cancelled -- and lets go then. A holder sees no
837 # difference. Asked through can(): DBIO::Test::Future holds a chain from its
838 # head and has no retain.
839 801 ▶sub _hold_derived {
Subroutine _hold_derived: called CC=2 SCAR=7.1
840 801 my ($self, $future) = @_;
841 801 ▶ $future->retain if $future->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $future->can("retain") 0 801 801
842 801 return $future;
843 }
844  
845 # Shape the row a RETURNING insert yields into the returned-columns hashref
846 # sync insert() produces (ADR 0031 §3): the supplied insert data overlaid with
847 # any DB-populated columns. Handles both row shapes a driver's transport may
848 # yield -- a column=>value hashref (used as-is), or a positional arrayref
849 # (RETURNING *), zipped against the source's declared column order via
850 # _returning_columns. A driver whose insert cannot RETURNING (last_insert_id
851 # only) overrides this to assemble the hashref its own way.
852 93 ▶sub _insert_returned_columns {
Subroutine _insert_returned_columns: called CC=7 SCAR=19.5
853 93 my ($self, $source, $to_insert, $row) = @_;
854  
855 93 ▶ my %returned = %{ $to_insert || {} };
Condition50%1/2
$to_insert || {}
l!l
930
MC/DC0%0/1
$to_insert || {}
$to_insert
Truth table50%1/2
A
A= $to_insert
Aresult
11
00
856 93 ▶ return \%returned unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 2 91 93
857  
858 91 ▶ if (ref $row eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $row eq 'HASH') { } 73 18 91
elsif (ref $row eq 'ARRAY') { } 11 7 18
859 73 @returned{ keys %$row } = values %$row;
860 }
861 elsif (ref $row eq 'ARRAY') {
862 11 my @cols = $self->_returning_columns($source);
863 11 ▶ @returned{@cols} = @$row if @cols && @cols == @$row;
Branch: 1 branch
BranchTrueFalseTotal
if @cols and @cols == @$row 4 7 11
Condition100%3/3
@cols and @cols == @$row
!ll&&!rl&&r
344
MC/DC100%2/2
@cols and @cols == @$row
@cols @cols == @$row
Truth table100%3/3
A and B
A= @colsB= @cols == @$row
ABresult
0X0
100
111
864 }
865  
866 91 return \%returned;
867 }
868  
869 # Column names for a positional RETURNING * row, in result order. Default uses
870 # the result source's declared column order (matches RETURNING * on engines
871 # that emit columns in declaration order). A driver with a different order, or
872 # one that emits an explicit RETURNING list, overrides _insert_returned_columns.
873 13 ▶sub _returning_columns {
Subroutine _returning_columns: called CC=3 SCAR=11.0
874 13 my ($self, $source) = @_;
875 13 ▶ return () unless blessed($source) && $source->can('columns');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("columns") 4 9 13
Condition66%2/3
builtin::blessed($source) and $source->can("columns")
!ll&&!rl&&r
409
MC/DC50%1/2
builtin::blessed($source) and $source->can("columns")
builtin::blessed($source) $source->can("columns")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("columns")
ABresult
0X0
100
111
876 9 return $source->columns;
877 }
878  
879 # _run_crud on the pooled runner: the call the public *_async methods of the
880 # storage make. The Future it returns waits for a pool connection of its own;
881 # when that wait is a place in the pool's queue, the pool is told which Future
882 # waits there (PoolBase::_note_queued_for), so a transaction or pipeline of this
883 # pool that is handed such a Future by its coderef can tell it cannot settle
884 # before the transaction ends (_txn_inner_future, karr core k397). Only a pool
885 # that already exists is asked: a transaction or pipeline holds one by then,
886 # and so does every storage that has run a statement.
887 235 ▶sub _run_crud_pooled {
Subroutine _run_crud_pooled: called CC=5 SCAR=16.1
888 235 my ($self, $op, @args) = @_;
889  
890 # An unbound call made while a transaction's coderef runs is not in that
891 # transaction. Under debug (DBIO_TRACE) it says so; the text names the
892 # operation, never a value or a bind (karr core k400).
893 warn "DBIO: ${op}_async on the storage ran while a txn_do_async coderef was"
894 . " running; it is not part of the transaction (call it on the context)\n"
895 235 ▶ if $self->{_txn_coderef_running} && $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_txn_coderef_running"} and $self->debug 1 234 235
Condition100%3/3
$self->{"_txn_coderef_running"} and $self->debug
!ll&&!rl&&r
224101
MC/DC100%2/2
$self->{"_txn_coderef_running"} and $self->debug
$self->{"_txn_coderef_running"} $self->debug
Truth table100%3/3
A and B
A= $self->{"_txn_coderef_running"}B= $self->debug
ABresult
0X0
100
111
896  
897 235 my $pool = $self->{pool};
898 235 ▶ return $self->_run_crud($op, $self->_pool_runner, @args)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pool) and $pool->can("_collect_queued") 221 14 235
Condition100%3/3
builtin::blessed($pool) and $pool->can("_collect_queued")
!ll&&!rl&&r
1279414
MC/DC100%2/2
builtin::blessed($pool) and $pool->can("_collect_queued")
builtin::blessed($pool) $pool->can("_collect_queued")
Truth table100%3/3
A and B
A= builtin::blessed($pool)B= $pool->can("_collect_queued")
ABresult
0X0
100
111
899 unless blessed($pool) && $pool->can('_collect_queued');
900  
901 return $self->_note_pooled_derivation(
902 14 ▶ sub { $self->_run_crud($op, $self->_pool_runner, @args) }
Subroutine __ANON__: called CC=1 SCAR=0.0
903 14 );
904 }
905  
906 # Runs $code and reports the Future it returns to the pool as waiting wherever
907 # the calls on the storage inside it queued: a Future that an unbound
908 # $rs->all_async (or a Row or ResultSetColumn one) derives from the Futures of
909 # the storage is known to the pool like the storage's own (karr core k397).
910 349 ▶sub _note_pooled_derivation {
Subroutine _note_pooled_derivation: called CC=4 SCAR=13.9
911 349 my ($self, $code) = @_;
912  
913 349 my $pool = $self->{pool};
914 349 ▶ return $code->()
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pool) and $pool->can("_collect_queued") 333 16 349
Condition100%3/3
builtin::blessed($pool) and $pool->can("_collect_queued")
!ll&&!rl&&r
20512816
MC/DC100%2/2
builtin::blessed($pool) and $pool->can("_collect_queued")
builtin::blessed($pool) $pool->can("_collect_queued")
Truth table100%3/3
A and B
A= builtin::blessed($pool)B= $pool->can("_collect_queued")
ABresult
0X0
100
111
915 unless blessed($pool) && $pool->can('_collect_queued');
916  
917 16 my ($future, @waiters) = $pool->_collect_queued($code);
918 16 ▶ $pool->_note_queued_for($future, @waiters) if @waiters;
Branch: 1 branch
BranchTrueFalseTotal
if @waiters 9 7 16
919 16 return $future;
920 }
921  
922 # Runner that executes on a freshly-acquired pooled connection and releases
923 # it when done. This is the normal (non-transactional) path.
924 257 ▶sub _pool_runner {
Subroutine _pool_runner: called CC=1 SCAR=0.0
925 257 my $self = shift;
926 253 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
927 253 my ($sql, $bind, $bind_attrs) = @_;
928 253 return $self->_query_async($sql, $bind, $bind_attrs);
929 257 };
930 }
931  
932 # Runner that executes on a pinned connection without releasing it. Used by
933 # TransactionContext so CRUD inside a txn hits the same connection that
934 # BEGIN/COMMIT ran on.
935 181 ▶sub _pinned_runner {
Subroutine _pinned_runner: called CC=1 SCAR=0.0
936 181 my ($self, $conn) = @_;
937 181 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
938 181 my ($sql, $bind, $bind_attrs) = @_;
939 181 return $self->_query_async_pinned($conn, $sql, $bind, $bind_attrs);
940 181 };
941 }
942  
943  
944 55 ▶sub _debug_query {
Subroutine _debug_query: called CC=13 SCAR=25.7
945 55 my ($self, $sql, $bind, $bind_attrs) = @_;
946  
947 55 ▶ my $level = $self->_trace_level_of($self->{debug}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $level = $self->_trace_level_of($self->{"debug"}) or return 31 24 55
948  
949 31 my $owner = $self->_owner_storage;
950 31 ▶ my $debugobj = $owner && $owner->can('debugobj')
Branch: 1 branch
BranchTrueFalseTotal
$owner && $owner->can('debugobj') ? : 30 1 31
Condition66%2/3
$owner and $owner->can("debugobj")
!ll&&!rl&&r
1030
MC/DC50%1/2
$owner and $owner->can("debugobj")
$owner $owner->can("debugobj")
Truth table66%2/3
A and B
A= $ownerB= $owner->can("debugobj")
ABresult
0X0
100
111
951 ? $owner->debugobj
952 : $self->debugobj;
953  
954 31 my @trace;
955 31 ▶ if ($level >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level >= 2) 27 4 31
956 27 ▶ my $redact = $owner && $owner->can('redact_bind_value')
Branch: 1 branch
BranchTrueFalseTotal
$owner && $owner->can('redact_bind_value') ? : 26 1 27
Condition66%2/3
$owner and $owner->can("redact_bind_value")
!ll&&!rl&&r
1026
MC/DC50%1/2
$owner and $owner->can("redact_bind_value")
$owner $owner->can("redact_bind_value")
Truth table66%2/3
A and B
A= $ownerB= $owner->can("redact_bind_value")
ABresult
0X0
100
111
957 ? $owner->redact_bind_value
958 : DBIO::Storage::DBI->redact_bind_value;
959 27 my $i = 0;
960 @trace = map {
961 41 ▶ my $attrs = $bind_attrs && $bind_attrs->[$i++];
Condition66%2/3
$bind_attrs && $$bind_attrs[$i++]
!ll&&!rl&&r
5036
MC/DC50%1/2
$bind_attrs && $$bind_attrs[$i++]
$bind_attrs $$bind_attrs[$i++]
Truth table66%2/3
A && B
A= $bind_attrsB= $$bind_attrs[$i++]
ABresult
0X0
100
111
962 41 ▶ if (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 40 1 41
963 40 ▶ my $name = ref $attrs eq 'HASH' ? $attrs->{dbic_colname} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs eq 'HASH' ? : 36 4 40
964 40 my $shown = $redact->($name, $_, $attrs);
965 40 ▶ $shown = substr($shown, 0, 32) . '...' if length($shown) > 32;
Branch: 1 branch
BranchTrueFalseTotal
if length $shown > 32 1 39 40
966 40 qq{'$shown'};
967 }
968 else {
969 1 q{NULL};
970 }
971 27 ▶ } @{ $bind || [] };
Condition50%1/2
$bind || []
l!l
270
MC/DC0%0/1
$bind || []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
972 }
973  
974 # One element at least, or the statistics object prints nothing for a
975 # statement without binds (as the sync _query_start does).
976 31 ▶ $debugobj->query_start($sql, @trace ? @trace : '');
Branch: 1 branch
BranchTrueFalseTotal
@trace ? : 24 7 31
977 31 ▶ return sub { $debugobj->query_end($sql, @trace) };
Subroutine __ANON__: called CC=1 SCAR=0.0
978 }
979  
980 # The arguments of an insert, update or delete after the table name, with a
981 # flat copy of the options hash: insert ($data, $opts), update ($set, $where,
982 # $opts), delete ($where, $opts). A driver SQL maker consumes keys of the
983 # options it receives (on_conflict, insert_or), so each rendering of a
984 # statement gets a copy of its own and the options of the caller stay as given.
985 109 my %OPTIONS_AT = (insert => 1, update => 2, delete => 1);
986 225 ▶sub _own_options {
Subroutine _own_options: called CC=2 SCAR=6.9
987 225 my ($op, @args) = @_;
988 225 my $at = $OPTIONS_AT{$op};
989 225 ▶ $args[$at] = { %{ $args[$at] } } if ref $args[$at] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[$at] eq "HASH" 12 213 225
990 225 return @args;
991 }
992  
993 # The bind attributes of a statement core built, by the rule of the sync path
994 # (_gen_sql_bind + _resolve_bindattrs: dbic_colname, sqlt_datatype, redact for
995 # encode_column). The wire binds stay bare values; the attributes are a second
996 # list parallel to them and carry no value. Only a trace at level 2 or more
997 # reads them, so only then is the statement generated a second time, with the
998 # column names kept in the binds. Anything that does not line up gives undef:
999 # a trace must never change what a statement does.
1000 434 ▶sub _crud_bind_attrs {
Subroutine _crud_bind_attrs: called CC=10 SCAR=23.3
1001 434 my ($self, $op, $ident, $wire, @sm_args) = @_;
1002  
1003 434 ▶ return undef unless $self->_trace_level_of($self->{debug}) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_trace_level_of($self->{"debug"}) >= 2 413 21 434
1004  
1005 21 my $owner = _effective_owner_of($self);
1006 21 ▶ return undef unless $owner && $owner->can('_resolve_bindattrs');
Branch: 1 branch
BranchTrueFalseTotal
unless $owner and $owner->can("_resolve_bindattrs") 0 21 21
Condition33%1/3
$owner and $owner->can("_resolve_bindattrs")
!ll&&!rl&&r
0021
MC/DC0%0/2
$owner and $owner->can("_resolve_bindattrs")
$owner $owner->can("_resolve_bindattrs")
Truth table33%1/3
A and B
A= $ownerB= $owner->can("_resolve_bindattrs")
ABresult
0X0
100
111
1007  
1008 # A result source gives its columns; the from structure of a select is
1009 # resolved by _resolve_bindattrs itself, as in _gen_sql_bind.
1010 21 ▶ my $colinfos = blessed($ident) && $ident->can('columns_info')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($ident) && $ident->can('columns_info') ? : 14 7 21
Condition66%2/3
builtin::blessed($ident) and $ident->can("columns_info")
!ll&&!rl&&r
7014
MC/DC50%1/2
builtin::blessed($ident) and $ident->can("columns_info")
builtin::blessed($ident) $ident->can("columns_info")
Truth table66%2/3
A and B
A= builtin::blessed($ident)B= $ident->can("columns_info")
ABresult
0X0
100
111
1011 ? $ident->columns_info
1012 : undef;
1013  
1014 21 my $sm = $self->sql_maker;
1015 21 my $attrs = eval {
1016 21 my @pairs = do {
1017 21 local $sm->{bindtype} = 'columns';
1018 21 ▶ my ($sql, @p) = $op eq 'select'
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'select' ? : 7 14 21
1019 ? $sm->select(@sm_args)
1020 : $sm->$op($sm_args[0], _own_options($op, @sm_args[1 .. $#sm_args]));
1021 21 ▶ unshift @p, @{ $sm_args[3]{bind} || [] } if $op eq q(select);
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "select" 7 14 21
Condition100%2/2
$sm_args[3]{'bind'} || []
l!l
34
MC/DC100%1/1
$sm_args[3]{'bind'} || []
$sm_args[3]{'bind'}
Truth table100%2/2
A
A= $sm_args[3]{'bind'}
Aresult
11
00
1022 21 @p;
1023 };
1024 @pairs == @$wire
1025 21 ▶ ? [ map { $_->[0] } @{ $owner->_resolve_bindattrs($ident, \@pairs, $colinfos) } ]
Branch: 1 branch
BranchTrueFalseTotal
@pairs == @$wire ? : 21 0 21
1026 : undef;
1027 };
1028 21 return $attrs;
1029 }
1030  
1031 # Build SQL and run a CRUD op on a pinned connection. Entry point used by
1032 # TransactionContext; shares _run_crud's builders / post-processing.
1033 181 ▶sub _run_crud_pinned {
Subroutine _run_crud_pinned: called CC=1 SCAR=0.0
1034 181 my ($self, $op, $conn, @args) = @_;
1035 181 return $self->_run_crud($op, $self->_pinned_runner($conn), @args);
1036 }
1037  
1038  
1039 0 ▶sub _query_async {
Subroutine _query_async: not called CC=1 SCAR=6.9
1040 0 croak 'Subclass must override _query_async($sql, $bind, $bind_attrs)';
1041 }
1042  
1043  
1044 482 ▶sub _seam_future {
Subroutine _seam_future: called CC=8 SCAR=21.1
1045 482 my ($self, $method, @args) = @_;
1046  
1047 482 my ($f, $err);
1048 {
1049 482 local $@;
1050 482 ▶ eval { $f = $self->$method(@args); 1 } or $err = $@ || "$method died";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $f = $self->$method(@args); 1 } } or $err = $@ || "$method died" 471 11 482
Condition50%1/2
$@ || "$method died"
l!l
110
MC/DC0%0/1
$@ || "$method died"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
1051 }
1052 482 ▶ return $self->future_class->fail($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 11 471 482
1053 471 ▶ return $f if blessed($f) && $f->can('then');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($f) and $f->can("then") 465 6 471
Condition66%2/3
builtin::blessed($f) and $f->can("then")
!ll&&!rl&&r
60465
MC/DC50%1/2
builtin::blessed($f) and $f->can("then")
builtin::blessed($f) $f->can("then")
Truth table66%2/3
A and B
A= builtin::blessed($f)B= $f->can("then")
ABresult
0X0
100
111
1054 6 ▶ return $self->future_class->fail(
Branch: 1 branch
BranchTrueFalseTotal
defined $f ? : 1 5 6
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1055 (ref($self) || $self) . "->$method returned "
1056 . (defined $f ? 'no Future' : 'undef, not a Future')
1057 );
1058 }
1059  
1060  
1061 0 ▶sub _query_async_pinned {
Subroutine _query_async_pinned: not called CC=1 SCAR=6.9
1062 0 croak 'Subclass must override _query_async_pinned($conn, $sql, $bind, $bind_attrs)';
1063 }
1064  
1065  
1066 0 ▶sub _await_query_result {
Subroutine _await_query_result: not called CC=1 SCAR=6.9
1067 0 croak 'Subclass must override _await_query_result($conn, $sql, $bind)';
1068 }
1069  
1070  
1071 0 ▶sub _await_conn_ready {
Subroutine _await_conn_ready: not called CC=1 SCAR=6.9
1072 0 croak 'Subclass must override _await_conn_ready($conn)';
1073 }
1074  
1075  
1076 0 ▶sub _transform_sql {
Subroutine _transform_sql: not called CC=1 SCAR=6.9
1077 0 croak 'Subclass must override _transform_sql($sql)';
1078 }
1079  
1080  
1081 0 ▶sub _post_insert_sql {
Subroutine _post_insert_sql: not called CC=1 SCAR=6.9
1082 0 croak 'Subclass must override _post_insert_sql';
1083 }
1084  
1085  
1086 663 ▶sub txn_do_async {
Subroutine txn_do_async: called CC=9 SCAR=23.1
Pod documented
1087 663 my ($self, $coderef, @args) = @_;
1088  
1089 663 my $fc = $self->future_class;
1090  
1091 # No transaction through a broker that is not transaction-safe: the rule the
1092 # sync storage keeps in txn_begin (karr k270). Asked before a connection is
1093 # taken, and reported the way every other failure of a transaction is.
1094 663 my $refusal;
1095 {
1096 663 local $@;
1097 663 ▶ eval { $self->_assert_transaction_safe_access_broker; 1 } or $refusal = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_assert_transaction_safe_access_broker; 1 } } or $refusal = $@ 649 14 663
1098 }
1099 663 ▶ return $fc->fail($refusal) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 14 649 663
1100  
1101 # txn_timeout (karr core k397): the timer is made before the transaction
1102 # starts, so a storage that cannot time it does not start one it could not
1103 # end. The timeout ends the transaction through the cancel path, so it needs a
1104 # Future class that can be cancelled.
1105 649 my $timeout = $self->async_option('txn_timeout');
1106 649 my $timer;
1107 649 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) 0 649 649
1108 0 my $unable;
1109 0 ▶ if (!$fc->can('cancel')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fc->can('cancel')) { } 0 0 0
1110 0 $unable = $self->_exception_object(
1111 "txn_timeout needs a Future class that can be cancelled, and $fc cannot be"
1112 );
1113 }
1114 else {
1115 0 $timer = $self->_seam_future(_delay => $timeout);
1116 0 ▶ $unable = $timer->failure if $timer->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $timer->is_failed 0 0 0
1117 }
1118 0 ▶ return $fc->fail($unable) if defined $unable;
Branch: 1 branch
BranchTrueFalseTotal
if defined $unable 0 0 0
1119 }
1120  
1121 # The pool this transaction begins on is the one it ends on: the finalizer
1122 # hands the connection back to $pool, never to $self->pool asked again
1123 # (karr dbio-async k46).
1124 649 my $pool = $self->pool;
1125 648 ▶ my $txn_future = $pool->acquire_txn->then(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1126 648 my $conn = shift;
1127  
1128 # The one record of what became of $conn: the finalizer, the cancel and the
1129 # paths that cut a transaction short all go through it (karr k274).
1130 648 my $guard = {
1131 pool => $pool, conn => $conn, done => 0,
1132 lease => _pool_lease($pool, $conn),
1133 };
1134  
1135 648 my ($txn_ctx, $terminal, $thrown);
1136 {
1137 648 local $@;
1138 648 my $ok = eval {
1139 648 my $txn_ctx_class = $self->_txn_context_class;
1140 648 my $accessor = $self->_txn_conn_accessor;
1141 648 $txn_ctx = $txn_ctx_class->new(
1142 storage => $self,
1143 $accessor => $conn,
1144 );
1145 646 ▶ $txn_ctx->_set_txn_guard($guard) if $txn_ctx->can('_set_txn_guard');
Branch: 1 branch
BranchTrueFalseTotal
if $txn_ctx->can("_set_txn_guard") 636 10 646
1146 646 $terminal = $self->_txn_terminal_chain(
1147 $txn_ctx,
1148 $conn,
1149 $coderef,
1150 \@args,
1151 );
1152 645 1;
1153 };
1154 648 ▶ $thrown = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 645 648
1155 }
1156 648 ▶ $terminal = $fc->fail($thrown) if defined $thrown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $thrown 3 645 648
1157  
1158 648 return $self->_finalize_txn_async($txn_ctx, $conn, $terminal, $pool, $guard);
1159 649 });
1160  
1161 # Retain the caller-visible sequence too: it is the downstream Future that
1162 # encompasses context invalidation and release. Retaining only the terminal
1163 # upstream chain can still let this finalizer sequence be collected early.
1164 649 ▶ $txn_future->retain if $txn_future->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $txn_future->can("retain") 1 648 649
1165 649 ▶ return defined $timer ? $self->_txn_timed($txn_future, $timer, $timeout) : $txn_future;
Branch: 1 branch
BranchTrueFalseTotal
defined $timer ? : 0 649 649
1166 }
1167  
1168 # The exception object of a message, for a failed Future.
1169 0 ▶sub _exception_object {
Subroutine _exception_object: not called CC=1 SCAR=6.9
1170 0 my ($self, $message) = @_;
1171 0 local $@;
1172 0 eval { $self->throw_exception($message); 1 };
1173 0 return $@;
1174 }
1175  
1176 # The transaction Future of a storage with txn_timeout (karr core k397): the
1177 # Future the caller gets, wrapped around $txn_future. When $timer fires before
1178 # the transaction is over, $txn_future is cancelled -- the cancel path of karr
1179 # k274 runs: ROLLBACK goes out best-effort and is not waited for, the context
1180 # ends, the connection is retired -- and the caller's Future fails with
1181 # "transaction timed out after N seconds". A transaction that ends first
1182 # stops the timer. Cancelling the caller's Future cancels the transaction, as
1183 # without a timeout.
1184 0 ▶sub _txn_timed {
Subroutine _txn_timed: not called CC=3 SCAR=24.8
1185 0 my ($self, $txn_future, $timer, $timeout) = @_;
1186  
1187 0 my $outer = $txn_future->new;
1188 0 my $timed_out;
1189  
1190 0 ▶ $txn_future->on_ready(sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
1191 0 my $f = shift;
1192 0 ▶ $timer->cancel if !$timer->is_ready && $timer->can('cancel');
Branch: 1 branch
BranchTrueFalseTotal
if not $timer->is_ready and $timer->can("cancel") 0 0 0
Condition0%0/3
not $timer->is_ready and $timer->can("cancel")
!ll&&!rl&&r
000
MC/DC0%0/2
not $timer->is_ready and $timer->can("cancel")
not $timer->is_ready $timer->can("cancel")
Truth table0%0/3
not(A) and B
A= not $timer->is_readyB= $timer->can("cancel")
ABresult
0X0
100
111
1193 0 ▶ return if $outer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $outer->is_ready 0 0 0
1194 0 ▶ return $outer->cancel if $f->is_cancelled && !$timed_out;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled and not $timed_out 0 0 0
Condition0%0/3
$f->is_cancelled and not $timed_out
!ll&&!rl&&r
000
MC/DC0%0/2
$f->is_cancelled and not $timed_out
$f->is_cancelled not $timed_out
Truth table0%0/3
A and not(B)
A= $f->is_cancelledB= not $timed_out
ABresult
0X0
100
111
1195 0 ▶ return if $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled 0 0 0
1196 0 ▶ return $outer->fail($f->failure) if $f->is_failed;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_failed 0 0 0
1197 0 return $outer->done($f->get);
1198 0 });
1199 0 ▶ $outer->on_cancel(sub { $txn_future->cancel unless $txn_future->is_ready });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $txn_future->is_ready 0 0 0
1200  
1201 0 ▶ my $chain = $timer->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1202 0 ▶ return if $txn_future->is_ready || $outer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $txn_future->is_ready or $outer->is_ready 0 0 0
Condition0%0/3
$txn_future->is_ready or $outer->is_ready
l!l&&r!l&&!r
000
MC/DC0%0/2
$txn_future->is_ready or $outer->is_ready
$txn_future->is_ready $outer->is_ready
Truth table0%0/3
A or B
A= $txn_future->is_readyB= $outer->is_ready
ABresult
1X1
011
000
1203 0 $timed_out = 1;
1204 0 $txn_future->cancel;
1205 0 $outer->fail($self->_exception_object("transaction timed out after $timeout seconds"));
1206 0 return;
1207 0 });
1208 0 ▶ $chain->retain if $chain->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $chain->can("retain") 0 0 0
1209 0 ▶ $outer->retain if $outer->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $outer->can("retain") 0 0 0
1210 0 return $outer;
1211 }
1212  
1213 # Build the BEGIN/callback/COMMIT-or-ROLLBACK sequence without releasing the
1214 # pinned connection. The outer finalizer owns that one terminal action.
1215 646 ▶sub _txn_terminal_chain {
Subroutine _txn_terminal_chain: called CC=2 SCAR=6.9
1216 646 my ($self, $txn_ctx, $conn, $coderef, $args) = @_;
1217  
1218 646 my $fc = $self->future_class;
1219 my $chain = $self->_query_async_pinned($conn, 'BEGIN', [])->then(
1220 639 ▶ sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
1221 639 my ($inner, $callback_error);
1222 {
1223 639 local $@;
1224 639 my $ok = eval {
1225 639 ▶ local $self->{_txn_coderef_running} = ( $self->{_txn_coderef_running} || 0 ) + 1;
Condition100%2/2
$self->{'_txn_coderef_running'} || 0
l!l
1638
MC/DC100%1/1
$self->{'_txn_coderef_running'} || 0
$self->{'_txn_coderef_running'}
Truth table100%2/2
A
A= $self->{'_txn_coderef_running'}
Aresult
11
00
1226 639 $inner = $coderef->($txn_ctx, @$args);
1227 594 1;
1228 };
1229 639 ▶ $callback_error = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 45 594 639
1230 }
1231  
1232 639 ▶ if (defined $callback_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $callback_error) 45 594 639
1233 # A connection left unusable gets its ROLLBACK best-effort from
1234 # _finalize_txn_async, not waited for here (karr k232).
1235 45 ▶ return $fc->fail($callback_error)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_unusable($txn_ctx) 2 43 45
1236 if $self->_txn_conn_unusable($txn_ctx);
1237 43 return $self->_txn_rollback_and_fail($txn_ctx, $conn, $callback_error);
1238 }
1239  
1240 # blessed, not ref: an unblessed return (the hashref $ctx->insert
1241 # resolves, say) dies in ->can, and a die here sent neither COMMIT nor
1242 # ROLLBACK -- the connection went back to the pool mid-transaction
1243 # (karr k228).
1244 594 ▶ my $inner_future = blessed($inner) && $inner->can('then')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($inner) && $inner->can('then') ? : 547 47 594
Condition66%2/3
builtin::blessed($inner) and $inner->can("then")
!ll&&!rl&&r
470547
MC/DC50%1/2
builtin::blessed($inner) and $inner->can("then")
builtin::blessed($inner) $inner->can("then")
Truth table66%2/3
A and B
A= builtin::blessed($inner)B= $inner->can("then")
ABresult
0X0
100
111
1245 ? $inner
1246 : $fc->done($inner);
1247  
1248 # The driver's chance to refuse the inner Future before it is chained
1249 # on (_txn_inner_future). A throw is a coderef die; a return that is
1250 # no Future leaves the coderef's own (karr k334).
1251 594 my ($seam_future, $seam_error);
1252 {
1253 594 local $@;
1254 594 my $ok = eval {
1255 594 $seam_future = $self->_txn_inner_future($txn_ctx, $conn, $inner_future);
1256 589 1;
1257 };
1258 594 ▶ $seam_error = $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 5 589 594
1259 }
1260 594 ▶ if (defined $seam_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seam_error) 5 589 594
1261 5 ▶ return $fc->fail($seam_error)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_unusable($txn_ctx) 0 5 5
1262 if $self->_txn_conn_unusable($txn_ctx);
1263 5 return $self->_txn_rollback_and_fail($txn_ctx, $conn, $seam_error);
1264 }
1265 589 ▶ $inner_future = $seam_future
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($seam_future) and $seam_future->can("then") 589 0 589
Condition33%1/3
builtin::blessed($seam_future) and $seam_future->can("then")
!ll&&!rl&&r
00589
MC/DC0%0/2
builtin::blessed($seam_future) and $seam_future->can("then")
builtin::blessed($seam_future) $seam_future->can("then")
Truth table33%1/3
A and B
A= builtin::blessed($seam_future)B= $seam_future->can("then")
ABresult
0X0
100
111
1266 if blessed($seam_future) && $seam_future->can('then');
1267  
1268 my $chained = $inner_future->then(
1269 529 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1270 529 my @result = @_;
1271 529 ▶ return $self->_txn_commit_refusal($txn_ctx)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_unusable($txn_ctx) 6 523 529
1272 if $self->_txn_conn_unusable($txn_ctx);
1273 # COMMIT only once every statement the context handed out has an
1274 # answer, and only if none of them failed (karr k236).
1275 523 ▶ return $self->_txn_statements_settled($txn_ctx)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1276 523 return $self->_txn_commit_if_clean($txn_ctx, $conn, $_[0], @result);
1277 523 });
1278 },
1279 60 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1280 60 my $error = shift;
1281 60 ▶ return $fc->fail($error) if $self->_txn_conn_unusable($txn_ctx);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_unusable($txn_ctx) 8 52 60
1282 52 return $self->_txn_rollback_and_fail($txn_ctx, $conn, $error);
1283 },
1284 589 );
1285 589 return $self->_txn_chain_cancelled_with($inner_future, $chained, $txn_ctx, $conn);
1286 },
1287 6 ▶ sub { return $fc->fail(_txn_prefixed_error(BEGIN => $_[0])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1288 646 );
1289  
1290 # Real Future holds a downstream sequence Future only weakly. Keep the
1291 # terminal chain alive until it settles; immediate Future shims need no such
1292 # support and may not implement retain.
1293 645 ▶ $chain->retain if $chain->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $chain->can("retain") 1 644 645
1294 645 return $chain;
1295 }
1296  
1297 # The Future of the transaction's coderef, cancelled by itself (karr
1298 # dbio-postgresql-ev k70): Future.pm runs no ->then callback of a cancelled
1299 # Future, so $chained would stay pending for good, with no COMMIT, no ROLLBACK
1300 # and no hand-back. It ends as a cancel of the whole transaction does
1301 # (_txn_retire_on_cancel): ROLLBACK best-effort, connection retired. The
1302 # transaction fails; whether the work took effect is unknown. Returned is the
1303 # Future the chain continues with -- $chained itself when $inner_future cannot
1304 # be cancelled.
1305 589 ▶sub _txn_chain_cancelled_with {
Subroutine _txn_chain_cancelled_with: called CC=3 SCAR=22.8
1306 589 my ($self, $inner_future, $chained, $txn_ctx, $conn) = @_;
1307  
1308 589 ▶ return $chained
Branch: 1 branch
BranchTrueFalseTotal
unless $inner_future->can("on_cancel") and not $inner_future->is_ready 589 0 589
Condition33%1/3
$inner_future->can("on_cancel") and not $inner_future->is_ready
!ll&&!rl&&r
58900
MC/DC0%0/2
$inner_future->can("on_cancel") and not $inner_future->is_ready
$inner_future->can("on_cancel") not $inner_future->is_ready
Truth table33%1/3
A and not(B)
A= $inner_future->can("on_cancel")B= not $inner_future->is_ready
ABresult
0X0
100
111
1309 unless $inner_future->can('on_cancel') && !$inner_future->is_ready;
1310  
1311 0 my $fc = $self->future_class;
1312 0 my $guard = $self->_txn_guard($txn_ctx);
1313 0 my $gate = $fc->new;
1314 # A closure, not the Future itself: Future::XS calls the on_ready of a
1315 # Future cancelled through ->on_cancel($future) with no Future as argument.
1316 # And it marks a Future cancelled only after its on_cancel callbacks ran, so
1317 # $gate is not ready yet while its own cancel cancels $chained; the flag
1318 # keeps that from cancelling $gate again.
1319 0 my $gate_cancelling;
1320 0 ▶ $gate->on_cancel(sub { $gate_cancelling = 1; $chained->cancel });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1321 0 ▶ $chained->on_ready(sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
1322 0 my $c = shift;
1323 0 ▶ return if $gate->is_ready || $gate_cancelling;
Branch: 1 branch
BranchTrueFalseTotal
if $gate->is_ready or $gate_cancelling 0 0 0
Condition0%0/3
$gate->is_ready or $gate_cancelling
l!l&&r!l&&!r
000
MC/DC0%0/2
$gate->is_ready or $gate_cancelling
$gate->is_ready $gate_cancelling
Truth table0%0/3
A or B
A= $gate->is_readyB= $gate_cancelling
ABresult
1X1
011
000
1324 0 ▶ return $gate->cancel if $c->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $c->is_cancelled 0 0 0
1325 0 ▶ return $c->is_failed ? $gate->fail($c->failure) : $gate->done($c->get);
Branch: 1 branch
BranchTrueFalseTotal
$c->is_failed ? : 0 0 0
1326 0 });
1327 0 ▶ $inner_future->on_cancel(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1328 0 ▶ return if $gate->is_ready || $gate_cancelling;
Branch: 1 branch
BranchTrueFalseTotal
if $gate->is_ready or $gate_cancelling 0 0 0
Condition0%0/3
$gate->is_ready or $gate_cancelling
l!l&&r!l&&!r
000
MC/DC0%0/2
$gate->is_ready or $gate_cancelling
$gate->is_ready $gate_cancelling
Truth table0%0/3
A or B
A= $gate->is_readyB= $gate_cancelling
ABresult
1X1
011
000
1329 0 ▶ $self->_txn_retire_on_cancel($txn_ctx, $conn, $guard) if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 0 0 0
1330 0 $gate->fail($self->_txn_cancelled_error(
1331 q{the Future of the transaction's coderef was cancelled}
1332 ));
1333 # Cancelling $chained cancels $inner_future, the Future whose cancel is
1334 # running here; Future::XS does not take that cancel for a no-op, so it
1335 # waits until $inner_future is cancelled.
1336 0 ▶ $inner_future->on_ready(sub { $chained->cancel unless $chained->is_ready });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $chained->is_ready 0 0 0
1337 0 });
1338 0 return $gate;
1339 }
1340  
1341 # The error a transaction fails with when something cancelled it from inside.
1342 0 ▶sub _txn_cancelled_error {
Subroutine _txn_cancelled_error: not called CC=2 SCAR=17.9
1343 0 my ($self, $what) = @_;
1344 0 my $err;
1345 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->throw_exception("Transaction cancelled: $what. The connection is retired; whether the " . 'transaction took effect is unknown.'); 1 } } or $err = $@ 0 0 0
1346 0 $self->throw_exception(
1347 "Transaction cancelled: $what. The connection is retired; whether the "
1348 . 'transaction took effect is unknown.'
1349 );
1350 0 1;
1351 } or $err = $@;
1352 0 return $err;
1353 }
1354  
1355 # The failure of a BEGIN or COMMIT with the step's name in front: the text
1356 # "$step failed: $error". A DBIO::Exception::Server stays an object -- a derived
1357 # copy, same class and fields, context set, stringifying with the prefix -- so
1358 # the SQLSTATE of a serialization failure at COMMIT is not lost to the text
1359 # (ADR 0062, decision 2026-10-12). Anything that is no such object stays the
1360 # string it was.
1361 18 ▶sub _txn_prefixed_error {
Subroutine _txn_prefixed_error: called CC=3 SCAR=11.0
1362 18 my ($step, $error) = @_;
1363 18 my $text = "$step failed: $error";
1364 18 ▶ return $text
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($error) and $error->isa("DBIO::Exception::Server") 9 9 18
Condition66%2/3
builtin::blessed($error) and $error->isa("DBIO::Exception::Server")
!ll&&!rl&&r
909
MC/DC50%1/2
builtin::blessed($error) and $error->isa("DBIO::Exception::Server")
builtin::blessed($error) $error->isa("DBIO::Exception::Server")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->isa("DBIO::Exception::Server")
ABresult
0X0
100
111
1365 unless blessed($error) && $error->isa('DBIO::Exception::Server');
1366 9 return $error->with_context($step, $text);
1367 }
1368  
1369 # Resolves once every statement Future $txn_ctx handed out has settled --
1370 # including any handed out while this waited -- with [ $error ] for the first
1371 # of them, in the order they were handed out, that failed; with nothing when
1372 # none did. One needs_all per round over Futures that cannot fail, so a long
1373 # run of statements that already settled costs no recursion (karr k236). A
1374 # context class of a backend's own is asked through can().
1375 646 ▶sub _txn_statements_settled {
Subroutine _txn_statements_settled: called CC=6 SCAR=18.0
1376 646 my ($self, $txn_ctx, $from, $first_failure) = @_;
1377 646 ▶ $from //= 0;
Condition100%2/2
$from //= 0
l!l
123523
MC/DC100%1/1
$from //= 0
$from
Truth table100%2/2
A
A= $from
Aresult
11
00
1378  
1379 646 my $fc = $self->future_class;
1380 646 ▶ my @statements = ($txn_ctx && $txn_ctx->can('_txn_statements'))
Branch: 1 branch
BranchTrueFalseTotal
$txn_ctx && $txn_ctx->can('_txn_statements') ? : 638 8 646
Condition66%2/3
$txn_ctx and $txn_ctx->can("_txn_statements")
!ll&&!rl&&r
08638
MC/DC50%1/2
$txn_ctx and $txn_ctx->can("_txn_statements")
$txn_ctx $txn_ctx->can("_txn_statements")
Truth table66%2/3
A and B
A= $txn_ctxB= $txn_ctx->can("_txn_statements")
ABresult
0X0
100
111
1381 ? $txn_ctx->_txn_statements
1382 : ();
1383 646 ▶ if ($from >= @statements) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from >= @statements) 523 123 646
1384 523 ▶ return $first_failure ? $fc->done($first_failure) : $fc->done;
Branch: 1 branch
BranchTrueFalseTotal
$first_failure ? : 7 516 523
1385 }
1386  
1387 123 my @failures;
1388 my @settled = map {
1389 123 _txn_statement_settled($fc, $statements[$_], \@failures, $_)
1390 } $from .. $#statements;
1391  
1392 123 my $seen = scalar @statements;
1393 123 ▶ return $fc->needs_all(@settled)->then(sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
1394 123 ▶ if (!$first_failure and my ($first) = grep { defined } @failures) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $first_failure and my($first) = grep({defined $_;} @failures)) 7 116 123
Condition66%2/3
not $first_failure and my($first) = grep({defined $_;} @failures)
!ll&&!rl&&r
01167
MC/DC50%1/2
not $first_failure and my($first) = grep({defined $_;} @failures)
not $first_failure my($first) = grep({defined $_;} @failures)
Truth table66%2/3
not(A) and B
A= not $first_failureB= my($first) = grep({defined $_;} @failures)
ABresult
0X0
100
111
1395 7 ▶ $first_failure = ref $first eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $first eq 'HASH' ? : 0 7 7
1396 ? [ $self->_txn_cancelled_statement_error ]
1397 : $first;
1398 }
1399 123 return $self->_txn_statements_settled($txn_ctx, $seen, $first_failure);
1400 123 });
1401 }
1402  
1403 # A Future that is done once the tracked statement $f has settled, whichever
1404 # way, and never fails. A failure is noted at $index as [ $error ]. A Future
1405 # that is cancelled -- Future.pm's wait_any cancels the statement that lost a
1406 # race against a timeout -- runs no catch callback and would be waited on
1407 # forever; it is noted as { cancelled => 1 }. Nobody will see its result, so
1408 # the transaction cannot vouch for it. Cancelling is probed through can(): the
1409 # DBIO::Future contract has no cancel, and a class without one cannot be
1410 # cancelled.
1411 151 ▶sub _txn_statement_settled {
Subroutine _txn_statement_settled: called CC=5 SCAR=20.3
1412 151 my ($fc, $f, $failures, $index) = @_;
1413 151 ▶ if ($f->can('is_cancelled') && $f->is_cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->can("is_cancelled") and $f->is_cancelled) 0 151 151
Condition33%1/3
$f->can("is_cancelled") and $f->is_cancelled
!ll&&!rl&&r
15100
MC/DC0%0/2
$f->can("is_cancelled") and $f->is_cancelled
$f->can("is_cancelled") $f->is_cancelled
Truth table33%1/3
A and B
A= $f->can("is_cancelled")B= $f->is_cancelled
ABresult
0X0
100
111
1414 0 $failures->[$index] = { cancelled => 1 };
1415 0 return $fc->done;
1416 }
1417 151 my $settled = $f->catch(_txn_failure_recorder($fc, $failures, $index));
1418 151 ▶ $f->on_cancel(_txn_cancel_recorder($settled, $failures, $index))
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("on_cancel") and not $f->is_ready 0 151 151
Condition33%1/3
$f->can("on_cancel") and not $f->is_ready
!ll&&!rl&&r
15100
MC/DC0%0/2
$f->can("on_cancel") and not $f->is_ready
$f->can("on_cancel") not $f->is_ready
Truth table33%1/3
A and not(B)
A= $f->can("on_cancel")B= not $f->is_ready
ABresult
0X0
100
111
1419 if $f->can('on_cancel') && !$f->is_ready;
1420 151 return $settled;
1421 }
1422  
1423 # The catch callback for one tracked statement: note its error at its
1424 # position. Named subs, so each callback closes over what it names only.
1425 151 ▶sub _txn_failure_recorder {
Subroutine _txn_failure_recorder: called CC=1 SCAR=0.0
1426 151 my ($fc, $failures, $index) = @_;
1427 9 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1428 9 $failures->[$index] = [ $_[0] ];
1429 9 return $fc->done;
1430 151 };
1431 }
1432  
1433 # The on_cancel callback for one tracked statement. $settled is held weakly:
1434 # it holds $f in turn (a derived Future keeps its source for cancellation), and
1435 # a strong reference back would make a cycle of every statement.
1436 0 ▶sub _txn_cancel_recorder {
Subroutine _txn_cancel_recorder: not called CC=1 SCAR=6.9
1437 0 my ($settled, $failures, $index) = @_;
1438 0 weaken($settled);
1439 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1440 0 ▶ $failures->[$index] //= { cancelled => 1 };
Condition0%0/2
$failures->[$index] //= {"cancelled", 1}
l!l
00
MC/DC0%0/1
$failures->[$index] //= {"cancelled", 1}
$failures->[$index]
Truth table0%0/2
A
A= $failures->[$index]
Aresult
11
00
1441 0 ▶ $settled->done if $settled && !$settled->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $settled and not $settled->is_ready 0 0 0
Condition0%0/3
$settled and not $settled->is_ready
!ll&&!rl&&r
000
MC/DC0%0/2
$settled and not $settled->is_ready
$settled not $settled->is_ready
Truth table0%0/3
A and not(B)
A= $settledB= not $settled->is_ready
ABresult
0X0
100
111
1442 0 };
1443 }
1444  
1445 # The error a transaction fails with when one of its statements was cancelled
1446 # before it was answered.
1447 0 ▶sub _txn_cancelled_statement_error {
Subroutine _txn_cancelled_statement_error: not called CC=2 SCAR=17.9
1448 0 my ($self) = @_;
1449 0 my $err;
1450 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->throw_exception("COMMIT not sent: a statement of the transaction was cancelled before it was answered, so whether it took effect is unknown. The transaction is rolled back."); 1 } } or $err = $@ 0 0 0
1451 0 $self->throw_exception(
1452 'COMMIT not sent: a statement of the transaction was cancelled before it '
1453 . 'was answered, so whether it took effect is unknown. The transaction is '
1454 . 'rolled back.'
1455 );
1456 0 1;
1457 } or $err = $@;
1458 0 return $err;
1459 }
1460  
1461 # The end of a transaction whose coderef succeeded, once its statements have
1462 # settled. COMMIT -- unless a pipeline left the connection unusable meanwhile,
1463 # then nothing is sent on it and the refusal is the same as when that was known
1464 # before the wait (karr k232), or one of the statements failed, then ROLLBACK
1465 # and that statement's error (karr k236). A COMMIT the transport reports as
1466 # rolled back fails the transaction as well; nothing follows it, the server
1467 # has ended the transaction. The context ends as COMMIT goes out: a statement
1468 # issued on it after that would run after the transaction (karr k237).
1469 523 ▶sub _txn_commit_if_clean {
Subroutine _txn_commit_if_clean: called CC=3 SCAR=11.0
1470 523 my ($self, $txn_ctx, $conn, $failed, @result) = @_;
1471  
1472 523 my $fc = $self->future_class;
1473 523 ▶ return $self->_txn_commit_refusal($txn_ctx)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_unusable($txn_ctx) 0 523 523
1474 if $self->_txn_conn_unusable($txn_ctx);
1475 523 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 7 516 523
1476 7 return $self->_txn_rollback_and_fail($txn_ctx, $conn, $failed->[0]);
1477 }
1478 516 _txn_context_ends($txn_ctx);
1479 return $self->_query_async_pinned($conn, 'COMMIT', [])->then(
1480 503 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1481 503 ▶ return $self->_txn_rolled_back_by_server
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_commit_rolled_back($conn, @_) 1 502 503
1482 if $self->_txn_commit_rolled_back($conn, @_);
1483 502 return $fc->done(@result);
1484 },
1485 12 ▶ sub { return $self->_txn_abandon_after_failed_commit($txn_ctx, $conn, $_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1486 516 );
1487 }
1488  
1489 # A COMMIT the server refused ends the transaction whatever the server says (a
1490 # deferred constraint is checked at commit time, and a server may keep the
1491 # transaction open): ROLLBACK goes out on the pinned connection, waited for.
1492 # If it works the connection goes back to the pool as usual; if it fails or its
1493 # dispatch throws, the connection is retired, never handed back (the same
1494 # pool retirement a cancelled transaction uses). The transaction fails with the
1495 # error of the COMMIT -- the DBIO::Exception::Server object itself, never that
1496 # of the ROLLBACK (karr k372).
1497 12 ▶sub _txn_abandon_after_failed_commit {
Subroutine _txn_abandon_after_failed_commit: called CC=4 SCAR=13.9
1498 12 my ($self, $txn_ctx, $conn, $commit_error) = @_;
1499  
1500 12 my $fc = $self->future_class;
1501 12 my $error = _txn_prefixed_error(COMMIT => $commit_error);
1502  
1503 # Only a COMMIT the server refused is a failed COMMIT (ADR 0062). One the
1504 # transport refused or that was cancelled never got an answer from the
1505 # server: it stays what it was, the transaction fails with its error and the
1506 # connection's fate is the finalizer's.
1507 12 ▶ return $fc->fail($error)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($commit_error) and $commit_error->isa("DBIO::Exception::Server") 4 8 12
Condition66%2/3
builtin::blessed($commit_error) and $commit_error->isa("DBIO::Exception::Server")
!ll&&!rl&&r
408
MC/DC50%1/2
builtin::blessed($commit_error) and $commit_error->isa("DBIO::Exception::Server")
builtin::blessed($commit_error) $commit_error->isa("DBIO::Exception::Server")
Truth table66%2/3
A and B
A= builtin::blessed($commit_error)B= $commit_error->isa("DBIO::Exception::Server")
ABresult
0X0
100
111
1508 unless blessed($commit_error) && $commit_error->isa('DBIO::Exception::Server');
1509  
1510 # Gone already (reported dead, handed back by a cancel): nothing to roll
1511 # back, nothing left to retire.
1512 8 ▶ return $fc->fail($error) if $self->_txn_conn_gone($txn_ctx, $conn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_gone($txn_ctx, $conn) 0 8 8
1513  
1514 2 ▶ my $retire = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1515 2 $self->_txn_retire_conn($txn_ctx, $conn);
1516 2 return $fc->fail($error);
1517 8 };
1518  
1519 8 my $sent = $self->_seam_future(_query_async_pinned => $conn, 'ROLLBACK', []);
1520 return $sent->then(
1521 6 ▶ sub { return $fc->fail($error) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1522 8 $retire,
1523 );
1524 }
1525  
1526 # Takes the transaction's connection out of circulation: the pool retires it
1527 # when it goes back (guard), else the context marks it unusable for the
1528 # finalizer. Used where a ROLLBACK was not confirmed.
1529 20 ▶sub _txn_retire_conn {
Subroutine _txn_retire_conn: called CC=5 SCAR=20.3
1530 20 my ($self, $txn_ctx, $conn) = @_;
1531 20 ▶ if (my $guard = $self->_txn_guard($txn_ctx)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $guard = $self->_txn_guard($txn_ctx)) { } 20 0 20
elsif ($txn_ctx and $txn_ctx->can('_mark_txn_conn_unusable')) { } 0 0 0
1532 20 ▶ unless ($guard->{done}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($guard->{"done"}++) 20 0 20
1533 20 local $@;
1534 20 eval { $guard->{pool}->_retire_in_use_connection($conn) };
1535 }
1536 }
1537 ▶ elsif ($txn_ctx && $txn_ctx->can('_mark_txn_conn_unusable')) {
Condition0%0/3
$txn_ctx and $txn_ctx->can("_mark_txn_conn_unusable")
!ll&&!rl&&r
000
MC/DC0%0/2
$txn_ctx and $txn_ctx->can("_mark_txn_conn_unusable")
$txn_ctx $txn_ctx->can("_mark_txn_conn_unusable")
Truth table0%0/3
A and B
A= $txn_ctxB= $txn_ctx->can("_mark_txn_conn_unusable")
ABresult
0X0
100
111
1538 0 $txn_ctx->_mark_txn_conn_unusable;
1539 }
1540 20 return;
1541 }
1542  
1543 # The end of a transaction that failed with $error -- its coderef died or its
1544 # Future failed, or one of its statements failed: ROLLBACK, then fail with
1545 # $error. The context ends as ROLLBACK goes out (karr k237). A ROLLBACK that
1546 # fails, or whose dispatch throws, does not replace $error: the transaction
1547 # fails as the synchronous txn_do does when its rollback fails
1548 # (DBIO::Storage::BlockRunner), with both errors, the original first.
1549 107 ▶sub _txn_rollback_and_fail {
Subroutine _txn_rollback_and_fail: called CC=4 SCAR=13.9
1550 107 my ($self, $txn_ctx, $conn, $error) = @_;
1551  
1552 107 my $fc = $self->future_class;
1553 107 _txn_context_ends($txn_ctx);
1554  
1555 107 ▶ my $rollback = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1556 # No ROLLBACK onto a connection that is gone: it would only be answered
1557 # with "not sent", and that is no error of the transaction (karr
1558 # dbio-async k47). The finalizer's release retires it, the pool having
1559 # been told it is dead. Nor one the cancel has retired already (karr
1560 # k274), nor one a pipeline could not take out of pipeline mode (karr
1561 # k232): the finalizer sends that one a ROLLBACK best-effort and retires it.
1562 107 ▶ return $fc->fail($error) if $self->_txn_conn_gone($txn_ctx, $conn)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_txn_conn_gone($txn_ctx, $conn) or $self->_txn_conn_unusable($txn_ctx) 1 106 107
Condition66%2/3
$self->_txn_conn_gone($txn_ctx, $conn) or $self->_txn_conn_unusable($txn_ctx)
l!l&&r!l&&!r
01106
MC/DC50%1/2
$self->_txn_conn_gone($txn_ctx, $conn) or $self->_txn_conn_unusable($txn_ctx)
$self->_txn_conn_gone($txn_ctx, $conn) $self->_txn_conn_unusable($txn_ctx)
Truth table66%2/3
A or B
A= $self->_txn_conn_gone($txn_ctx, $conn)B= $self->_txn_conn_unusable($txn_ctx)
ABresult
1X1
011
000
1563 || $self->_txn_conn_unusable($txn_ctx);
1564  
1565 106 my $sent = $self->_seam_future(_query_async_pinned => $conn, 'ROLLBACK', []);
1566 return $sent->then(
1567 88 ▶ sub { return $fc->fail($error) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1568 18 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1569 # A ROLLBACK nobody confirmed leaves the transaction's state unknown:
1570 # the connection is retired, never handed back.
1571 18 $self->_txn_retire_conn($txn_ctx, $conn);
1572 18 return $fc->fail($self->_txn_rollback_failed_error($error, $_[0]));
1573 },
1574 106 );
1575 107 };
1576  
1577 # A pipeline run on the transaction whose sync is not answered yet has the
1578 # connection in pipeline mode: a ROLLBACK sent now goes out there, and the
1579 # connection would be handed back to the pool in that mode as soon as the
1580 # ROLLBACK is answered (karr k331). The ROLLBACK waits until each such
1581 # pipeline is over -- sync answered and pipeline mode left, or the pipeline
1582 # failed or was cancelled, which leaves the connection to the checks above.
1583 107 ▶ my @pipelines = $txn_ctx && $txn_ctx->can('_txn_pending_pipelines')
Branch: 1 branch
BranchTrueFalseTotal
$txn_ctx && $txn_ctx->can('_txn_pending_pipelines') ? : 105 2 107
Condition66%2/3
$txn_ctx and $txn_ctx->can("_txn_pending_pipelines")
!ll&&!rl&&r
02105
MC/DC50%1/2
$txn_ctx and $txn_ctx->can("_txn_pending_pipelines")
$txn_ctx $txn_ctx->can("_txn_pending_pipelines")
Truth table66%2/3
A and B
A= $txn_ctxB= $txn_ctx->can("_txn_pending_pipelines")
ABresult
0X0
100
111
1584 ? $txn_ctx->_txn_pending_pipelines
1585 : ();
1586 107 ▶ return $rollback->() unless @pipelines;
Branch: 1 branch
BranchTrueFalseTotal
unless @pipelines 103 4 107
1587  
1588 4 my @failures;
1589 return $fc->needs_all(
1590 4 map { _txn_statement_settled($fc, $pipelines[$_], \@failures, $_) }
1591 0 .. $#pipelines
1592 )->then($rollback);
1593 }
1594  
1595 # The error a transaction fails with when its ROLLBACK failed: the text the
1596 # synchronous txn_do uses (DBIO::Storage::BlockRunner), so the same match --
1597 # /Rollback failed/ -- works in every mode, immediate included (ADR 0055
1598 # addendum 2026-09-27, karr k237).
1599 18 ▶sub _txn_rollback_failed_error {
Subroutine _txn_rollback_failed_error: called CC=4 SCAR=14.0
1600 18 my ($self, $error, $rollback_error) = @_;
1601 18 my $err;
1602 18 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->throw_exception("Transaction aborted: $error. Rollback failed: $rollback_error"); 1 } } or $err = $@ 0 18 18
1603 18 $self->throw_exception(
1604 "Transaction aborted: $error. Rollback failed: $rollback_error"
1605 );
1606 0 1;
1607 } or $err = $@;
1608 # A server's refusal of the ROLLBACK stays the object (ADR 0062, decision
1609 # 2026-10-12): a derived copy with the text just thrown and context ROLLBACK.
1610 18 ▶ return $rollback_error->with_context(ROLLBACK => "$err")
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($rollback_error) and $rollback_error->isa("DBIO::Exception::Server") 1 17 18
Condition100%3/3
builtin::blessed($rollback_error) and $rollback_error->isa("DBIO::Exception::Server")
!ll&&!rl&&r
1521
MC/DC100%2/2
builtin::blessed($rollback_error) and $rollback_error->isa("DBIO::Exception::Server")
builtin::blessed($rollback_error) $rollback_error->isa("DBIO::Exception::Server")
Truth table100%3/3
A and B
A= builtin::blessed($rollback_error)B= $rollback_error->isa("DBIO::Exception::Server")
ABresult
0X0
100
111
1611 if blessed($rollback_error) && $rollback_error->isa('DBIO::Exception::Server');
1612 17 return $err;
1613 }
1614  
1615 # COMMIT or ROLLBACK is going out on the transaction's connection: from here
1616 # on its context refuses every statement (_assert_context_active), which
1617 # would otherwise run after the transaction, outside it (karr k237). The
1618 # finalizer invalidates again before the release; that is a no-op then. A
1619 # context class of a backend's own is asked through can().
1620 623 ▶sub _txn_context_ends {
Subroutine _txn_context_ends: called CC=3 SCAR=11.0
1621 623 my ($txn_ctx) = @_;
1622 623 ▶ $txn_ctx->_invalidate if $txn_ctx && $txn_ctx->can('_invalidate');
Branch: 1 branch
BranchTrueFalseTotal
if $txn_ctx and $txn_ctx->can("_invalidate") 613 10 623
Condition66%2/3
$txn_ctx and $txn_ctx->can("_invalidate")
!ll&&!rl&&r
010613
MC/DC50%1/2
$txn_ctx and $txn_ctx->can("_invalidate")
$txn_ctx $txn_ctx->can("_invalidate")
Truth table66%2/3
A and B
A= $txn_ctxB= $txn_ctx->can("_invalidate")
ABresult
0X0
100
111
1623 623 return;
1624 }
1625  
1626 # Every path after a successful acquire converges here. Invalidate any context
1627 # that was constructed before returning the connection to the pool, then
1628 # preserve the terminal result/error. A release throw replaces success, but
1629 # never an existing error. $pool is the pool $conn was acquired from. $guard
1630 # is the transaction's record of its connection's hand-back (karr k274).
1631 #
1632 # The Future returned is the one a caller cancels: Future.pm runs none of the
1633 # callbacks below for it, so the cancel is answered here
1634 # (_txn_retire_on_cancel).
1635 648 ▶sub _finalize_txn_async {
Subroutine _finalize_txn_async: called CC=4 SCAR=14.0
1636 648 my ($self, $txn_ctx, $conn, $terminal, $pool, $guard) = @_;
1637 648 ▶ $guard ||= {
Condition50%1/2
$guard ||= {"pool", $pool, "conn", $conn, "done", 0, "lease", _pool_lease($pool, $conn)}
l!l
6480
MC/DC0%0/1
$guard ||= {"pool", $pool, "conn", $conn, "done", 0, "lease", _pool_lease($pool, $conn)}
$guard
Truth table50%1/2
A
A= $guard
Aresult
11
00
1638 pool => $pool, conn => $conn, done => 0,
1639 lease => _pool_lease($pool, $conn),
1640 };
1641  
1642 648 my $fc = $self->future_class;
1643 my $final = $terminal->then(
1644 502 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1645 502 my @result = @_;
1646 502 ▶ $txn_ctx->_invalidate
Branch: 1 branch
BranchTrueFalseTotal
if $txn_ctx and $txn_ctx->can("_invalidate") 494 8 502
Condition66%2/3
$txn_ctx and $txn_ctx->can("_invalidate")
!ll&&!rl&&r
08494
MC/DC50%1/2
$txn_ctx and $txn_ctx->can("_invalidate")
$txn_ctx $txn_ctx->can("_invalidate")
Truth table66%2/3
A and B
A= $txn_ctxB= $txn_ctx->can("_invalidate")
ABresult
0X0
100
111
1647 if $txn_ctx && $txn_ctx->can('_invalidate');
1648  
1649 502 my ($released, $release_error);
1650 {
1651 502 local $@;
1652 502 $released = eval {
1653 502 $self->_txn_hand_back($txn_ctx, $conn, $pool, $guard);
1654 501 1;
1655 };
1656 502 ▶ $release_error = $@ unless $released;
Branch: 1 branch
BranchTrueFalseTotal
unless $released 1 501 502
1657 }
1658  
1659 502 ▶ return $released
Branch: 1 branch
BranchTrueFalseTotal
$released ? : 501 1 502
1660 ? $fc->done(@result)
1661 : $fc->fail($release_error);
1662 },
1663 146 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1664 146 my $error = shift;
1665 146 ▶ $txn_ctx->_invalidate
Branch: 1 branch
BranchTrueFalseTotal
if $txn_ctx and $txn_ctx->can("_invalidate") 142 4 146
Condition100%3/3
$txn_ctx and $txn_ctx->can("_invalidate")
!ll&&!rl&&r
22142
MC/DC100%2/2
$txn_ctx and $txn_ctx->can("_invalidate")
$txn_ctx $txn_ctx->can("_invalidate")
Truth table100%3/3
A and B
A= $txn_ctxB= $txn_ctx->can("_invalidate")
ABresult
0X0
100
111
1666 if $txn_ctx && $txn_ctx->can('_invalidate');
1667 {
1668 146 local $@;
1669 146 eval { $self->_txn_hand_back($txn_ctx, $conn, $pool, $guard) };
1670 }
1671 146 return $fc->fail($error);
1672 },
1673 648 );
1674 0 ▶ $final->on_cancel(sub { $self->_txn_retire_on_cancel($txn_ctx, $conn, $guard) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
1675 648 ▶ if $final->can('on_cancel') && !$final->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $final->can("on_cancel") and not $final->is_ready 0 648 648
Condition33%1/3
$final->can("on_cancel") and not $final->is_ready
!ll&&!rl&&r
64800
MC/DC0%0/2
$final->can("on_cancel") and not $final->is_ready
$final->can("on_cancel") not $final->is_ready
Truth table33%1/3
A and not(B)
A= $final->can("on_cancel")B= not $final->is_ready
ABresult
0X0
100
111
1676 648 return $final;
1677 }
1678  
1679 # The transaction's one hand-back of its connection. Normally a release. A
1680 # connection the transaction context reports unusable -- a pipeline on it could
1681 # not leave pipeline mode (karr k232) -- would break the next borrower, so it is
1682 # retired instead, through the pool's in-use retirement so that a caller queued
1683 # for its slot gets a fresh connection. The ROLLBACK the terminal chain held
1684 # back goes out first, best-effort: it is not waited for, since a connection in
1685 # that state may never answer it, and its outcome changes nothing -- closing the
1686 # session ends the transaction on the server either way. It matters where the
1687 # transport's _shutdown_connection closes nothing.
1688 #
1689 # Both go to $pool, the pool the transaction acquired $conn from -- not to
1690 # $self->pool, which builds a pool where the storage has none: a transaction
1691 # that ends after its storage was disconnected would put a new pool on it
1692 # (karr dbio-async k46). The pool it began on is shut down by then, and takes
1693 # a release or a retirement as a no-op.
1694 #
1695 # Once: a connection the cancel has retired already (_txn_retire_on_cancel) is
1696 # not handed back again (karr k274).
1697 648 ▶sub _txn_hand_back {
Subroutine _txn_hand_back: called CC=5 SCAR=16.2
1698 648 my ($self, $txn_ctx, $conn, $pool, $guard) = @_;
1699 648 ▶ return if $guard && $guard->{done}++;
Branch: 1 branch
BranchTrueFalseTotal
if $guard and $guard->{"done"}++ 20 628 648
Condition66%2/3
$guard and $guard->{"done"}++
!ll&&!rl&&r
062820
MC/DC50%1/2
$guard and $guard->{"done"}++
$guard $guard->{"done"}++
Truth table66%2/3
A and B
A= $guardB= $guard->{"done"}++
ABresult
0X0
100
111
1700  
1701 628 ▶ unless ($self->_txn_conn_unusable($txn_ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_txn_conn_unusable($txn_ctx)) 611 17 628
1702 611 ▶ $pool->release($conn, $guard ? $guard->{lease} : ());
Branch: 1 branch
BranchTrueFalseTotal
$guard ? : 611 0 611
1703 609 return;
1704 }
1705  
1706 17 $self->_txn_send_rollback_unwaited($conn, $pool);
1707 17 $pool->_retire_in_use_connection($conn);
1708 16 return;
1709 }
1710  
1711 # The end of a transaction whose Future was cancelled (karr k274). Future.pm
1712 # runs no ->then callback of a cancelled Future, so nothing else would end it:
1713 # the connection stayed checked out, the transaction open on the server, and a
1714 # pool of one waited for good. What is left of the transaction's connection
1715 # cannot be vouched for -- a statement in flight on it may or may not take
1716 # effect, a COMMIT in flight may or may not have committed -- so it is not
1717 # returned to the pool. ROLLBACK goes out best-effort, not waited for (nothing
1718 # may ever answer it), the context is invalidated, and the connection is
1719 # retired, its slot going to a fresh connection. Once per transaction, however
1720 # many paths notice the cancel and whether or not the finalizer ran.
1721 0 ▶sub _txn_retire_on_cancel {
Subroutine _txn_retire_on_cancel: not called CC=5 SCAR=34.0
1722 0 my ($self, $txn_ctx, $conn, $guard) = @_;
1723 0 ▶ return if $guard->{done}++;
Branch: 1 branch
BranchTrueFalseTotal
if $guard->{"done"}++ 0 0 0
1724  
1725 # A ROLLBACK or COMMIT already out has ended the context; there is nothing
1726 # left to end.
1727 0 ▶ my $open = !$txn_ctx || !$txn_ctx->can('in_txn') || $txn_ctx->in_txn;
Condition0%0/6
!($txn_ctx && $txn_ctx->can('in_txn')) || $txn_ctx->in_txn
l!l&&r!l&&!r
000
$txn_ctx && $txn_ctx->can('in_txn')
!ll&&!rl&&r
000
MC/DC0%0/3
!($txn_ctx && $txn_ctx->can('in_txn')) || $txn_ctx->in_txn
$txn_ctx $txn_ctx->can('in_txn') $txn_ctx->in_txn
Truth table0%0/4
not(A && B) || C
A= $txn_ctxB= $txn_ctx->can('in_txn')C= $txn_ctx->in_txn
ABCresult
0XX1
10X1
1111
1100
1728 0 _txn_context_ends($txn_ctx);
1729 0 ▶ $self->_txn_send_rollback_unwaited($conn, $guard->{pool}) if $open;
Branch: 1 branch
BranchTrueFalseTotal
if $open 0 0 0
1730  
1731 0 local $@;
1732 0 eval { $guard->{pool}->_retire_in_use_connection($conn) };
1733 0 return;
1734 }
1735  
1736 # ROLLBACK, best-effort and not waited for. Not onto a connection the pool has
1737 # been told is dead (karr dbio-async k47).
1738 17 ▶sub _txn_send_rollback_unwaited {
Subroutine _txn_send_rollback_unwaited: called CC=6 SCAR=19.4
1739 17 my ($self, $conn, $pool) = @_;
1740 17 ▶ return if $pool && $pool->can('_connection_is_dead') && $pool->_connection_is_dead($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $pool and $pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn) 0 17 17
Condition66%4/6
$pool and $pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn)
!ll&&!rl&&r
1160
$pool and $pool->can("_connection_is_dead")
!ll&&!rl&&r
0116
MC/DC0%0/3
$pool and $pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn)
$pool $pool->can("_connection_is_dead") $pool->_connection_is_dead($conn)
Truth table50%2/4
A and B and C
A= $poolB= $pool->can("_connection_is_dead")C= $pool->_connection_is_dead($conn)
ABCresult
0XX0
10X0
1100
1111
1741 17 local $@;
1742 17 eval {
1743 17 my $f = $self->_query_async_pinned($conn, 'ROLLBACK', []);
1744 17 ▶ $f->on_fail(sub {}) if blessed($f) && $f->can('on_fail');
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($f) and $f->can("on_fail") 0 17 17
Condition33%1/3
builtin::blessed($f) and $f->can("on_fail")
!ll&&!rl&&r
0170
MC/DC0%0/2
builtin::blessed($f) and $f->can("on_fail")
builtin::blessed($f) $f->can("on_fail")
Truth table33%1/3
A and B
A= builtin::blessed($f)B= $f->can("on_fail")
ABresult
0X0
100
111
1745 };
1746 17 return;
1747 }
1748  
1749 # Is the transaction's connection past a ROLLBACK: reported dead to its pool,
1750 # or already handed back by the cancel?
1751 115 ▶sub _txn_conn_gone {
Subroutine _txn_conn_gone: called CC=6 SCAR=18.7
1752 115 my ($self, $txn_ctx, $conn) = @_;
1753 115 ▶ my $guard = $self->_txn_guard($txn_ctx) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $guard = $self->_txn_guard($txn_ctx) or return 0 113 2 115
1754 113 ▶ return 1 if $guard->{done};
Branch: 1 branch
BranchTrueFalseTotal
if $guard->{"done"} 0 113 113
1755 113 my $pool = $guard->{pool};
1756 113 ▶ return ($pool && $pool->can('_connection_is_dead') && $pool->_connection_is_dead($conn)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pool && $pool->can('_connection_is_dead') && $pool->_connection_is_dead($conn) ? : 0 113 113
Condition66%4/6
$pool and $pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn)
!ll&&!rl&&r
48650
$pool and $pool->can("_connection_is_dead")
!ll&&!rl&&r
04865
MC/DC0%0/3
$pool and $pool->can("_connection_is_dead") and $pool->_connection_is_dead($conn)
$pool $pool->can("_connection_is_dead") $pool->_connection_is_dead($conn)
Truth table50%2/4
A and B and C
A= $poolB= $pool->can("_connection_is_dead")C= $pool->_connection_is_dead($conn)
ABCresult
0XX0
10X0
1100
1111
1757 }
1758  
1759 135 ▶sub _txn_guard {
Subroutine _txn_guard: called CC=3 SCAR=11.0
1760 135 my ($self, $txn_ctx) = @_;
1761 135 ▶ return unless $txn_ctx && $txn_ctx->can('_txn_guard');
Branch: 1 branch
BranchTrueFalseTotal
unless $txn_ctx and $txn_ctx->can("_txn_guard") 2 133 135
Condition66%2/3
$txn_ctx and $txn_ctx->can("_txn_guard")
!ll&&!rl&&r
02133
MC/DC50%1/2
$txn_ctx and $txn_ctx->can("_txn_guard")
$txn_ctx $txn_ctx->can("_txn_guard")
Truth table66%2/3
A and B
A= $txn_ctxB= $txn_ctx->can("_txn_guard")
ABresult
0X0
100
111
1762 133 return $txn_ctx->_txn_guard;
1763 }
1764  
1765 # Did something run on the transaction's connection leave it unusable? Asked
1766 # through can(): a backend may bring a context class of its own.
1767 1897 ▶sub _txn_conn_unusable {
Subroutine _txn_conn_unusable: called CC=4 SCAR=13.9
1768 1897 my ($self, $txn_ctx) = @_;
1769 1897 ▶ return 0 unless $txn_ctx && $txn_ctx->can('_txn_conn_unusable');
Branch: 1 branch
BranchTrueFalseTotal
unless $txn_ctx and $txn_ctx->can("_txn_conn_unusable") 32 1865 1897
Condition100%3/3
$txn_ctx and $txn_ctx->can("_txn_conn_unusable")
!ll&&!rl&&r
2301865
MC/DC100%2/2
$txn_ctx and $txn_ctx->can("_txn_conn_unusable")
$txn_ctx $txn_ctx->can("_txn_conn_unusable")
Truth table100%3/3
A and B
A= $txn_ctxB= $txn_ctx->can("_txn_conn_unusable")
ABresult
0X0
100
111
1770 1865 ▶ return $txn_ctx->_txn_conn_unusable ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$txn_ctx->_txn_conn_unusable ? : 34 1831 1865
1771 }
1772  
1773 # The failed Future a transaction ends with when its coderef succeeded but its
1774 # connection was left unusable: COMMIT cannot be sent on it.
1775 6 ▶sub _txn_commit_refusal {
Subroutine _txn_commit_refusal: called CC=5 SCAR=17.1
1776 6 my ($self, $txn_ctx) = @_;
1777 # A pipeline that failed passed the error that ended its batch (karr k396):
1778 # the transaction fails with that one.
1779 6 ▶ if ($txn_ctx && $txn_ctx->can('_txn_conn_unusable_error')
Branch: 1 branch
BranchTrueFalseTotal
if ($txn_ctx and $txn_ctx->can("_txn_conn_unusable_error") and defined(my $batch_error = $txn_ctx->_txn_conn_unusable_error)) 2 4 6
Condition50%3/6
$txn_ctx and $txn_ctx->can("_txn_conn_unusable_error") and defined(my $batch_error = $txn_ctx->_txn_conn_unusable_error)
!ll&&!rl&&r
042
$txn_ctx and $txn_ctx->can("_txn_conn_unusable_error")
!ll&&!rl&&r
006
MC/DC33%1/3
$txn_ctx and $txn_ctx->can("_txn_conn_unusable_error") and defined(my $batch_error = $txn_ctx->_txn_conn_unusable_error)
$txn_ctx $txn_ctx->can("_txn_conn_unusable_error") defined(my $batch_error = $txn_ctx->_txn_conn_unusable_error)
Truth table50%2/4
A and B and C
A= $txn_ctxB= $txn_ctx->can("_txn_conn_unusable_error")C= defined(my $batch_error = $txn_ctx->_txn_conn_unusable_error)
ABCresult
0XX0
10X0
1100
1111
1780 && defined(my $batch_error = $txn_ctx->_txn_conn_unusable_error)) {
1781 2 return $self->future_class->fail($batch_error);
1782 }
1783 4 my $err;
1784 4 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->throw_exception("COMMIT not sent: a pipeline on the transaction connection could not leave pipeline mode (its _pipeline_exit died), so no statement on it can be relied on. The transaction is rolled back and the connection retired."); 1 } } or $err = $@ 0 4 4
1785 4 $self->throw_exception(
1786 'COMMIT not sent: a pipeline on the transaction connection could not '
1787 . 'leave pipeline mode (its _pipeline_exit died), so no statement on it '
1788 . 'can be relied on. The transaction is rolled back and the connection '
1789 . 'retired.'
1790 );
1791 0 1;
1792 } or $err = $@;
1793 4 return $self->future_class->fail($err);
1794 }
1795  
1796  
1797 502 ▶sub _txn_commit_rolled_back { 0 }
Subroutine _txn_commit_rolled_back: called CC=1 SCAR=0.0
1798  
1799 # The failed Future a transaction ends with when its COMMIT was answered as a
1800 # rollback (_txn_commit_rolled_back).
1801 1 ▶sub _txn_rolled_back_by_server {
Subroutine _txn_rolled_back_by_server: called CC=2 SCAR=7.2
1802 1 my ($self) = @_;
1803 1 my $err;
1804 1 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->throw_exception("COMMIT was answered with ROLLBACK: the server had already aborted the transaction and committed nothing. A statement in it failed without the transaction seeing the failure -- one sent on its connection past the transaction context, or a pipeline statement whose batch dropped its error."); 1 } } or $err = $@ 0 1 1
1805 1 $self->throw_exception(
1806 'COMMIT was answered with ROLLBACK: the server had already aborted the '
1807 . 'transaction and committed nothing. A statement in it failed without the '
1808 . 'transaction seeing the failure -- one sent on its connection past the '
1809 . 'transaction context, or a pipeline statement whose batch dropped its '
1810 . 'error.'
1811 );
1812 0 1;
1813 } or $err = $@;
1814 1 return $self->future_class->fail($err);
1815 }
1816  
1817  
1818 592 ▶sub _txn_inner_future {
Subroutine _txn_inner_future: called CC=1 SCAR=0.0
1819 592 my ($self, $txn_ctx, $conn, $inner) = @_;
1820 592 $self->_refuse_own_pool_waiters($inner, 'transaction');
1821 588 return $inner;
1822 }
1823  
1824 # The inner Future of a transaction or pipeline waits in the queue of the pool
1825 # the transaction or pipeline holds a connection of: it is withdrawn from the
1826 # queue, failed, and the bracket fails with the throw (karr core k397).
1827 616 ▶sub _refuse_own_pool_waiters {
Subroutine _refuse_own_pool_waiters: called CC=4 SCAR=13.9
1828 616 my ($self, $inner, $what) = @_;
1829  
1830 616 my $pool = $self->{pool};
1831 616 ▶ return unless blessed($pool) && $pool->can('_refuse_queued_for');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pool) and $pool->can("_refuse_queued_for") 213 403 616
Condition66%2/3
builtin::blessed($pool) and $pool->can("_refuse_queued_for")
!ll&&!rl&&r
0213403
MC/DC50%1/2
builtin::blessed($pool) and $pool->can("_refuse_queued_for")
builtin::blessed($pool) $pool->can("_refuse_queued_for")
Truth table66%2/3
A and B
A= builtin::blessed($pool)B= $pool->can("_refuse_queued_for")
ABresult
0X0
100
111
1832  
1833 403 my $message = "the coderef returned a Future queued for this $what\'s own pool; "
1834 . 'call it on the context';
1835 403 ▶ $self->throw_exception($message)
Branch: 1 branch
BranchTrueFalseTotal
if $pool->_refuse_queued_for($inner, $message) 7 396 403
1836 if $pool->_refuse_queued_for($inner, $message);
1837 396 return;
1838 }
1839  
1840  
1841 624 ▶sub _txn_context_class { 'DBIO::Storage::Async::TransactionContext' }
Subroutine _txn_context_class: called CC=1 SCAR=0.0
1842  
1843  
1844 936 ▶sub _txn_conn_accessor { 'txn_conn' }
Subroutine _txn_conn_accessor: called CC=1 SCAR=0.0
1845  
1846  
1847 57 ▶sub pipeline {
Subroutine pipeline: called CC=4 SCAR=13.9
Pod documented
1848 57 my ($self, $coderef) = @_;
1849  
1850 57 ▶ if (my $refusal = $self->_pipeline_refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refusal = $self->_pipeline_refusal) 2 54 56
1851 2 return $refusal;
1852 }
1853  
1854 # No batch through a broker that is not transaction-safe, as txn_do_async
1855 # (karr k273): asked before a connection is taken from the pool.
1856 54 my $broker_refusal;
1857 {
1858 54 local $@;
1859 54 ▶ eval { $self->_assert_transaction_safe_access_broker; 1 } or $broker_refusal = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_assert_transaction_safe_access_broker; 1 } } or $broker_refusal = $@ 49 5 54
1860 }
1861 54 ▶ return $self->future_class->fail($broker_refusal) if defined $broker_refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $broker_refusal 5 49 54
1862  
1863 # Released to, or retired from, the pool it was acquired from -- not
1864 # $self->pool asked again when the batch ends (karr dbio-async k46).
1865 49 my $pool = $self->pool;
1866 49 ▶ return $self->_hold_derived($pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1867 49 my $conn = shift;
1868 49 my $lease = _pool_lease($pool, $conn);
1869 return $self->_pipeline_on_conn(
1870 $conn, $coderef,
1871 31 ▶ sub { $pool->release($conn, $lease) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1872 18 ▶ sub { $pool->_retire_in_use_connection($conn) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1873 49 );
1874 49 }));
1875 }
1876  
1877  
1878 91 ▶sub _pipeline_refusal {
Subroutine _pipeline_refusal: called CC=5 SCAR=16.5
1879 91 my ($self) = @_;
1880  
1881 91 my $base_enter = DBIO::Storage::Async->can('_pipeline_enter');
1882 91 my $own_enter = $self->can('_pipeline_enter');
1883 91 ▶ return undef if $own_enter && $own_enter != $base_enter;
Branch: 1 branch
BranchTrueFalseTotal
if $own_enter and $own_enter != $base_enter 79 12 91
Condition66%2/3
$own_enter and $own_enter != $base_enter
!ll&&!rl&&r
01279
MC/DC50%1/2
$own_enter and $own_enter != $base_enter
$own_enter $own_enter != $base_enter
Truth table66%2/3
A and B
A= $own_enterB= $own_enter != $base_enter
ABresult
0X0
100
111
1884  
1885 12 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1110
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1886 12 my $err;
1887 12 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->throw_exception("$class does not support pipeline() (no _pipeline_enter): this async " . 'transport cannot batch statements. Issue the statements individually, ' . "or use an async mode whose transport supports pipelining. (Transport " . 'authors: override _pipeline_enter/_pipeline_sync/_pipeline_exit to ' . "support it.)"); 1 } } or $err = $@ 0 12 12
1888 12 $self->throw_exception(
1889 "$class does not support pipeline() (no _pipeline_enter): this async "
1890 . "transport cannot batch statements. Issue the statements individually, "
1891 . "or use an async mode whose transport supports pipelining. (Transport "
1892 . "authors: override _pipeline_enter/_pipeline_sync/_pipeline_exit to "
1893 . "support it.)"
1894 );
1895 0 1;
1896 } or $err = $@;
1897 12 return $self->future_class->fail($err);
1898 }
1899  
1900  
1901 74 ▶sub _pipeline_on_conn {
Subroutine _pipeline_on_conn: called CC=15 SCAR=27.9
1902 74 my ($self, $conn, $coderef, $release, $retire) = @_;
1903  
1904 74 my $fc = $self->future_class;
1905 74 ▶ $release ||= sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$release ||= sub { 1; }
l!l&&r!l&&!r
49250
MC/DC0%0/2
$release ||= sub { 1; }
$release sub { 1; }
Truth table66%2/3
A ||= B
A= $releaseB= sub { 1; }
ABresult
1X1
011
000
1906 74 ▶ $retire ||= $release;
Condition33%1/3
$retire ||= $release
l!l&&r!l&&!r
7400
MC/DC0%0/2
$retire ||= $release
$retire $release
Truth table33%1/3
A ||= B
A= $retireB= $release
ABresult
1X1
011
000
1907  
1908 # Build the context BEFORE entering pipeline mode: a throw from the
1909 # constructor then cannot strand a connection that is already bracketed.
1910 # It must still hand the connection back (karr k231).
1911 74 my $batch;
1912 74 my $built = eval {
1913 74 $batch = $self->_pipeline_context_class->new(
1914 storage => $self,
1915 pipeline_conn => $conn,
1916 );
1917 73 1;
1918 };
1919 74 ▶ if (!$built) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($built) 1 73 74
1920 1 my $ctx_err = $@;
1921 1 $release->();
1922 1 return $fc->fail($ctx_err);
1923 }
1924  
1925 73 my $entered = eval { $self->_pipeline_enter($conn); 1 };
1926 73 ▶ if (!$entered) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($entered) 1 72 73
1927 1 my $enter_err = $@;
1928 # Pipeline mode was never entered (the base seam croaks when a backend
1929 # declines pipelining) -- release the connection but do NOT call
1930 # _pipeline_exit, there is nothing to exit.
1931 1 $release->();
1932 1 return $fc->fail($enter_err);
1933 }
1934  
1935 72 my $result = eval { $coderef->($batch) };
1936 72 my $err = $@;
1937  
1938 # The driver's chance to refuse the Future the coderef returned
1939 # (_pipeline_inner_future); a throw is a coderef die (karr core k397).
1940 72 ▶ if (!$err && blessed($result) && $result->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $err and builtin::blessed($result) and $result->can("then")) 24 48 72
Condition83%5/6
not $err and builtin::blessed($result) and $result->can("then")
!ll&&!rl&&r
48024
not $err and builtin::blessed($result)
!ll&&!rl&&r
64224
MC/DC66%2/3
not $err and builtin::blessed($result) and $result->can("then")
not $err builtin::blessed($result) $result->can("then")
Truth table75%3/4
not(A) and B and C
A= not $errB= builtin::blessed($result)C= $result->can("then")
ABCresult
0XX0
10X0
1100
1111
1941 24 my $seam;
1942 24 local $@;
1943 24 ▶ if (eval { $seam = $self->_pipeline_inner_future($batch, $conn, $result); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $seam = $self->_pipeline_inner_future($batch, $conn, $result); 1 } }) { } 21 3 24
1944 21 ▶ $result = $seam if blessed($seam) && $seam->can('then');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($seam) and $seam->can("then") 21 0 21
Condition33%1/3
builtin::blessed($seam) and $seam->can("then")
!ll&&!rl&&r
0021
MC/DC0%0/2
builtin::blessed($seam) and $seam->can("then")
builtin::blessed($seam) $seam->can("then")
Truth table33%1/3
A and B
A= builtin::blessed($seam)B= $seam->can("then")
ABresult
0X0
100
111
1945 }
1946 else {
1947 3 $err = $@;
1948 }
1949 }
1950  
1951 # The pipeline context is valid ONLY for the synchronous run of the batch
1952 # coderef (dbio-async ADR 0003): statements are batched onto the bracketed
1953 # connection during that run and synced immediately below. Invalidate the
1954 # context now so a context-bound ResultSet/Row held past this point fails
1955 # loudly rather than silently re-issuing on a pooled connection outside the
1956 # bracket.
1957 72 ▶ $batch->_invalidate if $batch->can('_invalidate');
Branch: 1 branch
BranchTrueFalseTotal
if $batch->can("_invalidate") 72 0 72
1958  
1959 # The connection goes back once every statement of the batch and the Future
1960 # the coderef returned are decided (karr k396): sync, statement results,
1961 # returned Future, then the hand-back. A late error therefore never reaches a
1962 # connection another caller has meanwhile been lent.
1963 #
1964 # The bracket is decided exactly once. A failure -- the coderef died, the
1965 # sync or the returned Future failed, the exit died, the batch Future was
1966 # cancelled -- retires the connection, never releases it: unsynced or
1967 # half-read statements leave it in a state nobody may inherit. Inside a
1968 # transaction $retire only marks the connection unusable and the boundary
1969 # decides. Every statement Future still open fails with that error; no sync
1970 # is sent on the failure path, and a transport callback arriving later finds
1971 # the Future decided and does nothing. Pipeline mode is left best-effort.
1972 72 my $handed_back = 0;
1973 72 my $exited = 0;
1974 91 ▶ my $leave = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1975 91 ▶ return undef if $exited++;
Branch: 1 branch
BranchTrueFalseTotal
if $exited++ 19 72 91
1976 72 my $left = eval { $self->_pipeline_exit($conn); 1 };
1977 72 ▶ return $left ? undef : $@;
Branch: 1 branch
BranchTrueFalseTotal
$left ? : 55 17 72
1978 72 };
1979 35 ▶ my $fail = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
1980 35 my ($error, $exit_died) = @_;
1981 35 ▶ unless ($handed_back++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handed_back++) 35 0 35
1982 35 $leave->();
1983 # The error goes to $retire for a transaction to fail with; a connection
1984 # that could not leave pipeline mode is the transaction's generic case.
1985 35 ▶ { local $@; eval { $retire->($exit_died ? () : $error) }; }
Branch: 1 branch
BranchTrueFalseTotal
$exit_died ? : 14 21 35
1986 35 ▶ $batch->_fail_open_statements($error) if $batch->can('_fail_open_statements');
Branch: 1 branch
BranchTrueFalseTotal
if $batch->can("_fail_open_statements") 35 0 35
1987 }
1988 35 return $fc->fail($error);
1989 72 };
1990  
1991 72 ▶ return $fail->($err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 9 63 72
1992  
1993 # A sync that dies before it has a Future -- or returns none -- becomes a
1994 # failed one and takes the failure branch below, like k229's DDL dispatch
1995 # (_seam_future, karr k252). The hand-back stays behind the sync Future's
1996 # completion -- the obligation documented at _pipeline_sync.
1997 63 my $synced = $self->_seam_future(_pipeline_sync => $conn);
1998  
1999 56 ▶ my $batch_future = $synced->then(sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
2000 # The sync is answered: statement results have been read off the
2001 # connection. Leave pipeline mode, then wait for the rest.
2002 56 ▶ if (defined(my $exit_err = $leave->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $exit_err = &$leave())) 14 42 56
2003 14 return $fail->($exit_err, 1);
2004 }
2005  
2006 my @settled = map {
2007 5 ▶ $_->then(sub { return $fc->done }, sub { return $fc->done })
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2008 42 ▶ } grep { !$_->is_ready } ($batch->can('_statements') ? $batch->_statements : ());
Branch: 1 branch
BranchTrueFalseTotal
$batch->can('_statements') ? : 42 0 42
2009 42 ▶ my $thenable = blessed($result) && $result->can('then');
Condition66%2/3
builtin::blessed($result) && $result->can('then')
!ll&&!rl&&r
27015
MC/DC50%1/2
builtin::blessed($result) && $result->can('then')
builtin::blessed($result) $result->can('then')
Truth table66%2/3
A && B
A= builtin::blessed($result)B= $result->can('then')
ABresult
0X0
100
111
2010 10 ▶ push @settled, $result->then(sub { return $fc->done }, sub { return $fc->done })
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
2011 42 ▶ if $thenable;
Branch: 1 branch
BranchTrueFalseTotal
if $thenable 15 27 42
2012  
2013 42 ▶ return $fc->needs_all(@settled)->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2014 42 ▶ if ($thenable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thenable) 15 27 42
2015 10 ▶ return $result->then(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
2016 10 my @value = @_;
2017 10 ▶ unless ($handed_back++) { $release->() }
Branch: 1 branch
BranchTrueFalseTotal
unless ($handed_back++) 10 0 10
2018 10 return $fc->done(@value);
2019 5 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2020 5 return $fail->($_[0]);
2021 15 });
2022 }
2023 27 ▶ unless ($handed_back++) { $release->() }
Branch: 1 branch
BranchTrueFalseTotal
unless ($handed_back++) 27 0 27
2024 27 return $fc->done($result);
2025 42 });
2026 7 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2027 7 return $fail->($_[0]);
2028 63 });
2029  
2030 # Cancelled while the bracket is open (karr k274): Future.pm runs neither
2031 # branch above, so the connection would stay in pipeline mode and checked
2032 # out. Whether the batch took effect is unknown and the sync may still be
2033 # answered on it, so it is retired, never released -- once, and not at all
2034 # when the bracket finished before the cancel. The statements still open fail
2035 # with a message of their own: there is no error that ended the batch.
2036 0 ▶ $batch_future->on_cancel(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2037 0 ▶ return if $handed_back;
Branch: 1 branch
BranchTrueFalseTotal
if $handed_back 0 0 0
2038 0 $handed_back = 1;
2039 0 local $@;
2040 0 eval { $retire->() };
2041 0 eval { $batch->_fail_open_statements('pipeline cancelled') };
2042 63 ▶ }) if $batch_future->can('on_cancel') && !$batch_future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $batch_future->can("on_cancel") and not $batch_future->is_ready 0 63 63
Condition33%1/3
$batch_future->can("on_cancel") and not $batch_future->is_ready
!ll&&!rl&&r
6300
MC/DC0%0/2
$batch_future->can("on_cancel") and not $batch_future->is_ready
$batch_future->can("on_cancel") not $batch_future->is_ready
Truth table33%1/3
A and not(B)
A= $batch_future->can("on_cancel")B= not $batch_future->is_ready
ABresult
0X0
100
111
2043 63 return $batch_future;
2044 }
2045  
2046  
2047 74 ▶sub _pipeline_context_class { 'DBIO::Storage::Async::PipelineContext' }
Subroutine _pipeline_context_class: called CC=1 SCAR=0.0
2048  
2049  
2050 24 ▶sub _pipeline_inner_future {
Subroutine _pipeline_inner_future: called CC=1 SCAR=0.0
2051 24 my ($self, $batch, $conn, $inner) = @_;
2052 24 $self->_refuse_own_pool_waiters($inner, 'pipeline');
2053 21 return $inner;
2054 }
2055  
2056  
2057 0 ▶sub _pipeline_enter {
Subroutine _pipeline_enter: not called CC=1 SCAR=6.9
2058 0 croak 'Subclass must override _pipeline_enter($conn)';
2059 }
2060  
2061  
2062 0 ▶sub _pipeline_sync {
Subroutine _pipeline_sync: not called CC=1 SCAR=6.9
2063 0 croak 'Subclass must override _pipeline_sync($conn)';
2064 }
2065  
2066  
2067 0 ▶sub _pipeline_exit {
Subroutine _pipeline_exit: not called CC=1 SCAR=6.9
2068 0 croak 'Subclass must override _pipeline_exit($conn)';
2069 }
2070  
2071  
2072 109 __PACKAGE__->mk_group_accessors(simple => qw(on_listen_error on_listen_reconnect));
2073 109 __PACKAGE__->mk_group_accessors(inherited => qw(listen_backoff_initial listen_backoff_max));
2074  
2075 109 __PACKAGE__->listen_backoff_initial(1);
2076 109 __PACKAGE__->listen_backoff_max(30);
2077  
2078  
2079 329 ▶sub listen {
Subroutine listen: called CC=9 SCAR=22.0
Pod documented
2080 329 my ($self, $channel, $cb) = @_;
2081 329 my $refusal = $self->_listen_refusal('listen', $channel);
2082 329 ▶ $self->throw_exception($refusal) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 17 312 329
2083 312 ▶ $self->throw_exception('listen() requires a callback coderef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 4 308 312
2084 unless ref $cb eq 'CODE';
2085  
2086 308 my $L = $self->_listen_state(1);
2087 308 my $fc = $self->future_class;
2088 308 my $known = exists $L->{channels}{$channel};
2089 308 $L->{channels}{$channel} = $cb;
2090  
2091 return $fc->done
2092 308 ▶ if $known && $L->{subscribed}{$channel} && $L->{phase} eq 'up';
Branch: 1 branch
BranchTrueFalseTotal
if $known and $L->{"subscribed"}{$channel} and $L->{"phase"} eq "up" 4 304 308
Condition83%5/6
$known and $L->{"subscribed"}{$channel} and $L->{"phase"} eq "up"
!ll&&!rl&&r
30404
$known and $L->{"subscribed"}{$channel}
!ll&&!rl&&r
30044
MC/DC66%2/3
$known and $L->{"subscribed"}{$channel} and $L->{"phase"} eq "up"
$known $L->{"subscribed"}{$channel} $L->{"phase"} eq "up"
Truth table75%3/4
A and B and C
A= $knownB= $L->{"subscribed"}{$channel}C= $L->{"phase"} eq "up"
ABCresult
0XX0
10X0
1100
1111
2093  
2094 304 my $f = $fc->new;
2095 304 push @{ $L->{waiting}{$channel} }, $f;
2096  
2097 304 ▶ if ($L->{phase} eq 'idle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($L->{'phase'} eq 'idle') { } 176 128 304
elsif (defined $L->{'conn'} and not $known) { } 44 168 212
2098 176 $self->_listen_connect($L, 0);
2099 }
2100 ▶ elsif (defined $L->{conn} && !$known) {
Condition66%2/3
defined $L->{"conn"} and not $known
!ll&&!rl&&r
84044
MC/DC50%1/2
defined $L->{"conn"} and not $known
defined $L->{"conn"} not $known
Truth table66%2/3
A and not(B)
A= defined $L->{"conn"}B= not $known
ABresult
0X0
100
111
2101 44 $self->_listen_enqueue($L, $L->{gen}, '_listen_op_subscribe', $L->{conn}, $channel);
2102 }
2103 # Otherwise a connection is being opened or awaited: the next one subscribes
2104 # every registered channel, this one included. A known channel that is not
2105 # subscribed yet already has its subscribe queued.
2106  
2107 304 return $self->_listen_handout($L, $f);
2108 }
2109  
2110  
2111 29 ▶sub unlisten {
Subroutine unlisten: called CC=6 SCAR=17.9
Pod documented
2112 29 my ($self, $channel) = @_;
2113 29 my $refusal = $self->_listen_refusal('listen', $channel);
2114 29 ▶ $self->throw_exception($refusal) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 4 25 29
2115  
2116 25 my $fc = $self->future_class;
2117 25 ▶ my $L = $self->_listen_state or return $fc->done;
Branch: 1 branch
BranchTrueFalseTotal
my $L = $self->_listen_state or return $fc->done 24 1 25
2118 24 ▶ return $fc->done unless exists $L->{channels}{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $L->{"channels"}{$channel} 4 20 24
2119  
2120 20 delete $L->{channels}{$channel};
2121 20 delete $L->{subscribed}{$channel};
2122 20 $self->_listen_settle($L, $channel, 0,
2123 "unlisten('$channel') was called before its subscription took effect");
2124  
2125 20 ▶ unless (%{ $L->{channels} }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$L->{"channels"};}) 12 8 20
2126 12 $self->_listen_teardown($L, 'the last channel was unlistened');
2127 12 return $fc->done;
2128 }
2129  
2130 8 ▶ return $fc->done unless defined $L->{conn};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $L->{"conn"} 0 8 8
2131 return $self->_listen_handout($L,
2132 8 $self->_listen_enqueue($L, $L->{gen}, '_listen_op_unsubscribe', $L->{conn}, $channel));
2133 }
2134  
2135  
2136 24 ▶sub notify {
Subroutine notify: called CC=2 SCAR=6.9
Pod documented
2137 24 my ($self, $channel, $payload) = @_;
2138 24 my $refusal = $self->_listen_refusal('notify', $channel);
2139 24 ▶ $self->throw_exception($refusal) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 12 12 24
2140 12 return $self->_listen_seam_future('_notify_send', $channel, $payload);
2141 }
2142  
2143  
2144 98 ▶sub listen_alive {
Subroutine listen_alive: called CC=6 SCAR=17.9
Pod documented
2145 98 my ($self) = @_;
2146 98 my $refusal = $self->_listen_refusal('listen');
2147 98 ▶ $self->throw_exception($refusal) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 4 94 98
2148  
2149 94 ▶ my $L = $self->_listen_state or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $L = $self->_listen_state or return 0 77 17 94
2150 77 ▶ return 0 unless $L->{phase} eq 'up' && defined $L->{conn};
Branch: 1 branch
BranchTrueFalseTotal
unless $L->{"phase"} eq "up" and defined $L->{"conn"} 12 65 77
Condition66%2/3
$L->{"phase"} eq "up" and defined $L->{"conn"}
!ll&&!rl&&r
12065
MC/DC50%1/2
$L->{"phase"} eq "up" and defined $L->{"conn"}
$L->{"phase"} eq "up" defined $L->{"conn"}
Truth table66%2/3
A and B
A= $L->{"phase"} eq "up"B= defined $L->{"conn"}
ABresult
0X0
100
111
2151  
2152 65 my $dead = $self->_listen_probe_error($L->{conn});
2153 65 ▶ return 1 unless defined $dead;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dead 61 4 65
2154  
2155 4 $self->_listen_failed($L, $L->{gen}, $dead);
2156 4 return 0;
2157 }
2158  
2159  
2160 60 ▶sub listen_reconnect {
Subroutine listen_reconnect: called CC=4 SCAR=13.9
Pod documented
2161 60 my ($self) = @_;
2162 60 my $refusal = $self->_listen_refusal('listen');
2163 60 ▶ $self->throw_exception($refusal) if defined $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if defined $refusal 4 56 60
2164  
2165 56 my $fc = $self->future_class;
2166 56 ▶ my $L = $self->_listen_state or return $fc->done;
Branch: 1 branch
BranchTrueFalseTotal
my $L = $self->_listen_state or return $fc->done 52 4 56
2167 52 ▶ return $fc->done unless %{ $L->{channels} };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$L->{"channels"};} 0 52 52
2168  
2169 52 my $f = $fc->new;
2170 52 push @{ $L->{ready} }, $f;
2171  
2172 52 $self->_listen_cancel_timer($L);
2173 52 $self->_listen_drop_conn($L);
2174 52 $L->{attempt} = 0;
2175 52 $self->_listen_connect($L, 1);
2176  
2177 52 return $self->_listen_handout($L, $f);
2178 }
2179  
2180  
2181 0 ▶sub _listen_open {
Subroutine _listen_open: not called CC=1 SCAR=6.9
2182 0 croak 'Subclass must override _listen_open';
2183 }
2184  
2185  
2186 0 ▶sub _listen_close {
Subroutine _listen_close: not called CC=1 SCAR=6.9
2187 0 croak 'Subclass must override _listen_close($conn)';
2188 }
2189  
2190  
2191 0 ▶sub _listen_subscribe {
Subroutine _listen_subscribe: not called CC=1 SCAR=6.9
2192 0 croak 'Subclass must override _listen_subscribe($conn, $channel)';
2193 }
2194  
2195  
2196 0 ▶sub _listen_unsubscribe {
Subroutine _listen_unsubscribe: not called CC=1 SCAR=6.9
2197 0 croak 'Subclass must override _listen_unsubscribe($conn, $channel)';
2198 }
2199  
2200  
2201 0 ▶sub _listen_watch {
Subroutine _listen_watch: not called CC=1 SCAR=6.9
2202 0 croak 'Subclass must override _listen_watch($conn)';
2203 }
2204  
2205  
2206 0 ▶sub _listen_unwatch {
Subroutine _listen_unwatch: not called CC=1 SCAR=6.9
2207 0 croak 'Subclass must override _listen_unwatch($conn)';
2208 }
2209  
2210  
2211 0 ▶sub _listen_probe {
Subroutine _listen_probe: not called CC=1 SCAR=6.9
2212 0 croak 'Subclass must override _listen_probe($conn)';
2213 }
2214  
2215  
2216 0 ▶sub _listen_delay {
Subroutine _listen_delay: not called CC=1 SCAR=6.9
2217 0 croak 'Subclass must override _listen_delay($seconds)';
2218 }
2219  
2220  
2221 0 ▶sub _notify_send {
Subroutine _notify_send: not called CC=1 SCAR=6.9
2222 0 croak 'Subclass must override _notify_send($channel, $payload)';
2223 }
2224  
2225  
2226 49 ▶sub _listen_dispatch {
Subroutine _listen_dispatch: called CC=4 SCAR=13.9
2227 49 my ($self, $channel, $payload, $sender_pid) = @_;
2228 49 ▶ my $L = $self->_listen_state or return;
Branch: 1 branch
BranchTrueFalseTotal
my $L = $self->_listen_state or return 48 1 49
2229 48 ▶ my $cb = $L->{channels}{$channel} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $L->{"channels"}{$channel} or return 40 8 48
2230  
2231 40 local $@;
2232 40 ▶ eval { $cb->($channel, $payload, $sender_pid); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$cb($channel, $payload, $sender_pid); 1 } } or $self->_listen_report("listen callback for channel '${channel}' died: $@") 36 4 40
2233 or $self->_listen_report("listen callback for channel '$channel' died: $@");
2234 40 return;
2235 }
2236  
2237  
2238 37 ▶sub _listen_lost {
Subroutine _listen_lost: called CC=5 SCAR=16.2
2239 37 my ($self, $conn, $error) = @_;
2240 37 ▶ my $L = $self->_listen_state or return;
Branch: 1 branch
BranchTrueFalseTotal
my $L = $self->_listen_state or return 36 1 37
2241 36 ▶ return unless defined $L->{conn} && _listen_same_conn($L->{conn}, $conn);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $L->{"conn"} and _listen_same_conn($L->{"conn"}, $conn) 4 32 36
Condition66%2/3
defined $L->{"conn"} and _listen_same_conn($L->{"conn"}, $conn)
!ll&&!rl&&r
0432
MC/DC50%1/2
defined $L->{"conn"} and _listen_same_conn($L->{"conn"}, $conn)
defined $L->{"conn"} _listen_same_conn($L->{"conn"}, $conn)
Truth table66%2/3
A and B
A= defined $L->{"conn"}B= _listen_same_conn($L->{"conn"}, $conn)
ABresult
0X0
100
111
2242 32 ▶ $self->_listen_failed($L, $L->{gen}, $error // 'listen connection lost');
Condition50%1/2
$error // "listen connection lost"
l!l
320
MC/DC0%0/1
$error // "listen connection lost"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
2243 32 return;
2244 }
2245  
2246  
2247 25 ▶sub _listen_error {
Subroutine _listen_error: called CC=2 SCAR=6.9
2248 25 my ($self, $error) = @_;
2249 25 ▶ $self->_listen_state or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_listen_state or return 16 9 25
2250 16 $self->_listen_report($error);
2251 16 return;
2252 }
2253  
2254 # --- LISTEN machinery (private) -----------------------------------------------
2255 #
2256 # State lives in $self->{_listen}, created by the first listen():
2257 #
2258 # pid the process that created it; every other process ignores it
2259 # gen bumped on every connection change; an async callback carrying an
2260 # older gen belongs to a superseded attempt and does nothing
2261 # phase idle | opening | subscribing | up | waiting (backoff pending)
2262 # conn the dedicated listen connection, when one is open
2263 # watching whether _listen_watch has been started on conn
2264 # channels channel => callback (the registry)
2265 # subscribed channel => 1, for the current conn
2266 # waiting channel => [ Futures handed out by listen() ]
2267 # ready [ Futures handed out by listen_reconnect() ]
2268 # queue tail of the serialised channel-operation chain on conn
2269 # delay the pending backoff timer
2270 # attempt consecutive failed attempts, for the backoff
2271 # proto the last pending Future a listen seam returned, held weakly: the
2272 # prototype of the Futures handed to callers (_listen_handout)
2273 #
2274 # Callbacks hold the storage weakly and the state strongly: a storage dropped
2275 # while a reconnect is pending is not kept alive by its own timer, and the
2276 # state it points at is torn down in DESTROY.
2277  
2278 # Why a listen-family call must be refused, or undef: the capability gate
2279 # first, then the channel name when the call takes one. Returned, not thrown,
2280 # so the exception names the public method rather than this helper.
2281 540 ▶sub _listen_refusal {
Subroutine _listen_refusal: called CC=6 SCAR=17.9
2282 540 my ($self, $capability, @channel) = @_;
2283 return 'LISTEN/NOTIFY not supported by this storage driver'
2284 540 ▶ unless grep { $_ eq $capability } $self->transport_capabilities;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $capability;} $self->transport_capabilities 33 507 540
2285 507 ▶ return undef unless @channel;
Branch: 1 branch
BranchTrueFalseTotal
unless @channel 150 357 507
2286 357 my ($channel) = @channel;
2287 357 ▶ return 'LISTEN/NOTIFY requires a channel name'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel and not ref $channel and length $channel 8 349 357
Condition83%5/6
defined $channel and not ref $channel and length $channel
!ll&&!rl&&r
44349
defined $channel and not ref $channel
!ll&&!rl&&r
40353
MC/DC66%2/3
defined $channel and not ref $channel and length $channel
defined $channel not ref $channel length $channel
Truth table75%3/4
A and not(B) and C
A= defined $channelB= not ref $channelC= length $channel
ABCresult
0XX0
10X0
1100
1111
2288 unless defined $channel && !ref $channel && length $channel;
2289 349 return undef;
2290 }
2291  
2292 # The listen state of THIS process, or undef. State inherited across fork()
2293 # belongs to the parent: it is ignored, never touched, and replaced by a fresh
2294 # one when the child itself listens ($create).
2295 1257 ▶sub _listen_state {
Subroutine _listen_state: called CC=5 SCAR=16.1
2296 1257 my ($self, $create) = @_;
2297 1257 my $L = $self->{_listen};
2298 1257 ▶ undef $L if $L && $L->{pid} != $$;
Branch: 1 branch
BranchTrueFalseTotal
if $L and $L->{"pid"} != $$ 6 1251 1257
Condition100%3/3
$L and $L->{"pid"} != $$
!ll&&!rl&&r
21510366
MC/DC100%2/2
$L and $L->{"pid"} != $$
$L $L->{"pid"} != $$
Truth table100%3/3
A and B
A= $LB= $L->{"pid"} != $$
ABresult
0X0
100
111
2299 1257 ▶ return $L if $L || !$create;
Branch: 1 branch
BranchTrueFalseTotal
if $L or not $create 1081 176 1257
Condition100%3/3
$L or not $create
l!l&&r!l&&!r
103645176
MC/DC100%2/2
$L or not $create
$L not $create
Truth table100%3/3
A or not(B)
A= $LB= not $create
ABresult
1X1
011
000
2300 return $self->{_listen} = {
2301 176 pid => $$,
2302 gen => 0,
2303 phase => 'idle',
2304 attempt => 0,
2305 channels => {},
2306 subscribed => {},
2307 waiting => {},
2308 ready => [],
2309 };
2310 }
2311  
2312 1549 ▶sub _listen_is_current {
Subroutine _listen_is_current: called CC=4 SCAR=14.0
2313 1549 my ($self, $L, $gen) = @_;
2314 1549 my $cur = $self->{_listen};
2315 1549 ▶ return $cur && $cur == $L && $L->{pid} == $$ && $L->{gen} == $gen;
Condition77%7/9
$cur && $cur == $L && $L->{'pid'} == $$ && $L->{'gen'} == $gen
!ll&&!rl&&r
9121528
$cur and $cur == $L and $L->{"pid"} == $$
!ll&&!rl&&r
901540
$cur and $cur == $L
!ll&&!rl&&r
901540
MC/DC50%2/4
$cur && $cur == $L && $L->{'pid'} == $$ && $L->{'gen'} == $gen
$cur $cur == $L $L->{"pid"} == $$ $L->{'gen'} == $gen
Truth table60%3/5
A and B and C && D
A= $curB= $cur == $LC= $L->{"pid"} == $$D= $L->{'gen'} == $gen
ABCDresult
0XXX0
10XX0
110X0
11100
11111
2316 }
2317  
2318 # A plain function: the connection is opaque, a ref or a plain value.
2319 36 ▶sub _listen_same_conn {
Subroutine _listen_same_conn: called CC=6 SCAR=23.5
2320 36 my ($x, $y) = @_;
2321 36 ▶ return 0 unless defined $x && defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x and defined $y 0 36 36
Condition33%1/3
defined $x and defined $y
!ll&&!rl&&r
0036
MC/DC0%0/2
defined $x and defined $y
defined $x defined $y
Truth table33%1/3
A and B
A= defined $xB= defined $y
ABresult
0X0
100
111
2322 36 ▶ return ref $x
Branch: 1 branch
BranchTrueFalseTotal
ref $x ? : 36 0 36
Condition33%2/6
ref $y and builtin::refaddr($x) == builtin::refaddr($y)
!ll&&!rl&&r
0432
not ref $y and $x eq $y
!ll&&!rl&&r
000
MC/DC50%1/2
ref $y and builtin::refaddr($x) == builtin::refaddr($y)
ref $y builtin::refaddr($x) == builtin::refaddr($y)
MC/DC0%0/2
not ref $y and $x eq $y
not ref $y $x eq $y
Truth table66%2/3
A and B
A= ref $yB= builtin::refaddr($x) == builtin::refaddr($y)
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not ref $yB= $x eq $y
ABresult
0X0
100
111
2323 ? (ref $y && refaddr($x) == refaddr($y))
2324 : (!ref $y && $x eq $y);
2325 }
2326  
2327 # Call a Future-returning seam, turning a throw into a failed Future and a
2328 # plain return into a done one. A pending Future the seam returns is noted as
2329 # the prototype for the Futures handed to callers.
2330 663 ▶sub _listen_seam_future {
Subroutine _listen_seam_future: called CC=8 SCAR=21.8
2331 663 my ($self, $method, @args) = @_;
2332 663 my $fc = $self->future_class;
2333 663 my (@r, $err);
2334 {
2335 663 local $@;
2336 663 ▶ eval { @r = $self->$method(@args); 1 } or $err = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @r = $self->$method(@args); 1 } } or $err = $@ 663 0 663
2337 }
2338 663 ▶ return $fc->fail($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 663 663
2339 663 ▶ return $fc->done(@r) unless @r == 1 && blessed($r[0]) && $r[0]->can('then');
Branch: 1 branch
BranchTrueFalseTotal
unless @r == 1 and builtin::blessed($r[0]) and $r[0]->can("then") 0 663 663
Condition33%2/6
@r == 1 and builtin::blessed($r[0]) and $r[0]->can("then")
!ll&&!rl&&r
00663
@r == 1 and builtin::blessed($r[0])
!ll&&!rl&&r
00663
MC/DC0%0/3
@r == 1 and builtin::blessed($r[0]) and $r[0]->can("then")
@r == 1 builtin::blessed($r[0]) $r[0]->can("then")
Truth table25%1/4
A and B and C
A= @r == 1B= builtin::blessed($r[0])C= $r[0]->can("then")
ABCresult
0XX0
10X0
1100
1111
2340  
2341 663 my $L = $self->_listen_state;
2342 663 ▶ weaken($L->{proto} = $r[0]) if $L && !$r[0]->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $L and not $r[0]->is_ready 230 433 663
Condition100%3/3
$L and not $r[0]->is_ready
!ll&&!rl&&r
12421230
MC/DC100%2/2
$L and not $r[0]->is_ready
$L not $r[0]->is_ready
Truth table100%3/3
A and not(B)
A= $LB= not $r[0]->is_ready
ABresult
0X0
100
111
2343 663 return $r[0];
2344 }
2345  
2346 # The Future handed to the caller of listen / unlisten / listen_reconnect (karr
2347 # k222, ADR 0056 addendum). Core settles the Futures it hands out, so it must
2348 # construct them, and one constructed from future_class knows no event loop:
2349 # pending, its ->get cannot wait (Future.pm: "is not yet complete and does not
2350 # provide ->await"). So a pending one goes out as $proto->new -- the Future.pm
2351 # idiom for "a new Future of the same kind", which carries the transport's
2352 # class and, where it has one, its loop -- and core's own settles it. Its
2353 # ->get then awaits by driving the loop the transport's seams resolve on.
2354 # Where the transport has shown no pending Future, $f is handed out as it is.
2355 364 ▶sub _listen_handout {
Subroutine _listen_handout: called CC=3 SCAR=11.0
2356 364 my ($self, $L, $f) = @_;
2357 364 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 171 193 364
2358 193 ▶ my $proto = $L->{proto} or return $f;
Branch: 1 branch
BranchTrueFalseTotal
my $proto = $L->{"proto"} or return $f 192 1 193
2359  
2360 192 my $out = $proto->new;
2361 192 $self->_listen_retain($f->then(
2362 $self->_listen_forward($out, 1),
2363 $self->_listen_forward($out, 0),
2364 ));
2365 192 return $out;
2366 }
2367  
2368 # The callback that settles a handed-out Future like core's own. A named sub,
2369 # so it closes over $out and nothing else; the storage is held weakly, as in
2370 # _listen_callback. It never asks whether the attempt is still current: a
2371 # Future handed out is settled whatever happened to the attempt since.
2372 384 ▶sub _listen_forward {
Subroutine _listen_forward: called CC=1 SCAR=0.0
2373 384 my ($self, $out, $ok) = @_;
2374 384 my $fc = $self->future_class;
2375 384 weaken(my $weak = $self);
2376 52 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
2377 52 ▶ my $self = $weak or return $fc->done;
Branch: 1 branch
BranchTrueFalseTotal
my $self = $weak or return $fc->done 52 0 52
2378 52 $self->_listen_resolve($out, $ok, $_[0]);
2379 52 return $fc->done;
2380 384 };
2381 }
2382  
2383 # Build a Future callback that runs $method($L, $gen, @args, @future_values).
2384 # Built here, in a named sub, so it closes over nothing but these arguments;
2385 # the storage is held weakly.
2386 1714 ▶sub _listen_callback {
Subroutine _listen_callback: called CC=1 SCAR=0.0
2387 1714 my ($self, $L, $gen, $method, @args) = @_;
2388 1714 my $fc = $self->future_class;
2389 1714 weaken(my $weak = $self);
2390 965 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
2391 965 ▶ my $self = $weak or return $fc->done;
Branch: 1 branch
BranchTrueFalseTotal
my $self = $weak or return $fc->done 965 0 965
2392 965 my $r = $self->$method($L, $gen, @args, @_);
2393 965 ▶ return (blessed($r) && $r->can('then')) ? $r : $fc->done;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($r) && $r->can('then') ? : 259 706 965
Condition66%2/3
builtin::blessed($r) and $r->can("then")
!ll&&!rl&&r
7060259
MC/DC50%1/2
builtin::blessed($r) and $r->can("then")
builtin::blessed($r) $r->can("then")
Truth table66%2/3
A and B
A= builtin::blessed($r)B= $r->can("then")
ABresult
0X0
100
111
2394 1714 };
2395 }
2396  
2397 # Keep a derived Future alive until it settles. A real Future holds its
2398 # sequence Futures only weakly, so an unheld one would never run its callback.
2399 1017 ▶sub _listen_retain {
Subroutine _listen_retain: called CC=2 SCAR=7.1
2400 1017 my ($self, $f) = @_;
2401 1017 ▶ $f->retain if $f->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("retain") 0 1017 1017
2402 1017 return $f;
2403 }
2404  
2405 300 ▶sub _listen_connect {
Subroutine _listen_connect: called CC=1 SCAR=0.0
2406 300 my ($self, $L, $reconnect) = @_;
2407 300 my $gen = ++$L->{gen};
2408 300 $L->{phase} = 'opening';
2409  
2410 300 $self->_listen_retain(
2411 $self->_listen_seam_future('_listen_open')->then(
2412 $self->_listen_callback($L, $gen, '_listen_opened', $reconnect),
2413 $self->_listen_callback($L, $gen, '_listen_open_failed'),
2414 )
2415 );
2416 300 return;
2417 }
2418  
2419 182 ▶sub _listen_opened {
Subroutine _listen_opened: called CC=7 SCAR=19.6
2420 182 my ($self, $L, $gen, $reconnect, $conn) = @_;
2421  
2422 182 ▶ unless ($self->_listen_is_current($L, $gen)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_listen_is_current($L, $gen)) 4 178 182
2423 # A superseded attempt still hands over the connection it opened. It is
2424 # this process's connection and nobody else will close it.
2425 4 ▶ if ($L->{pid} == $$ && defined $conn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($L->{"pid"} == $$ and defined $conn) 4 0 4
Condition33%1/3
$L->{"pid"} == $$ and defined $conn
!ll&&!rl&&r
004
MC/DC0%0/2
$L->{"pid"} == $$ and defined $conn
$L->{"pid"} == $$ defined $conn
Truth table33%1/3
A and B
A= $L->{"pid"} == $$B= defined $conn
ABresult
0X0
100
111
2426 4 local $@;
2427 4 ▶ eval { $self->_listen_close($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_listen_close($conn); 1 } } or $self->_listen_report("closing a superseded listen connection failed: $@") 4 0 4
2428 or $self->_listen_report("closing a superseded listen connection failed: $@");
2429 }
2430 4 return;
2431 }
2432  
2433 178 ▶ return $self->_listen_failed($L, $gen, '_listen_open resolved without a connection')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn 4 174 178
2434 unless defined $conn;
2435  
2436 174 $L->{conn} = $conn;
2437 174 $L->{phase} = 'subscribing';
2438 174 $L->{subscribed} = {};
2439 174 $L->{queue} = undef;
2440 174 $L->{watching} = 0;
2441  
2442 $self->_listen_enqueue($L, $gen, '_listen_op_subscribe', $conn, $_)
2443 174 for sort keys %{ $L->{channels} };
2444 174 $self->_listen_enqueue($L, $gen, '_listen_op_arm', $conn, $reconnect);
2445 174 return;
2446 }
2447  
2448 # The text of a listen-connection error never quotes the connect info's
2449 # credentials, as the pool's connect error does not (ADR 0025 addendum): the
2450 # text is masked as such, and by what the storage knows of its connect info --
2451 # a string conninfo as the DSN slot, a hash with a password as the password
2452 # slot, the DBI form as it is.
2453 70 ▶sub _listen_masked {
Subroutine _listen_masked: called CC=9 SCAR=23.8
2454 70 my ($self, $text) = @_;
2455  
2456 70 my $info = eval {
2457 70 my $answer = $self->_current_async_connect_info($self->access_broker_mode);
2458 70 ▶ ref $answer eq 'ARRAY' ? $answer->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $answer eq 'ARRAY' ? : 70 0 70
2459 };
2460 70 ▶ $info = $self->{_conninfo} unless defined $info;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info 68 2 70
2461  
2462 my @known = ref $info eq 'ARRAY' ? ( $info )
2463 : ref $info eq 'HASH'
2464 70 ▶ ? ( [ undef, undef, $info->{password} // $info->{Password} ] )
Branch: 3 branches
BranchTrueFalseTotal
ref $info eq 'ARRAY' ? : 0 70 70
ref $info eq 'HASH' ? : 2 68 70
defined $info && !ref($info) ? : 0 68 68
Condition33%2/6
$info->{"password"} // $info->{"Password"}
l!l&&r!l&&!r
200
defined $info and not ref $info
!ll&&!rl&&r
6800
MC/DC0%0/2
$info->{"password"} // $info->{"Password"}
$info->{"password"} $info->{"Password"}
MC/DC0%0/2
defined $info and not ref $info
defined $info not ref $info
Truth table33%1/3
A // B
A= $info->{"password"}B= $info->{"Password"}
ABresult
1X1
011
000
Truth table33%1/3
A and not(B)
A= defined $infoB= not ref $info
ABresult
0X0
100
111
2465 : defined $info && !ref $info ? ( [ $info ] )
2466 : ();
2467  
2468 70 $text = mask_connect_secrets($text);
2469 70 $text = mask_connect_secrets($text, $_) for @known;
2470 70 return $text;
2471 }
2472  
2473 57 ▶sub _listen_open_failed {
Subroutine _listen_open_failed: called CC=2 SCAR=7.1
2474 57 my ($self, $L, $gen, $err) = @_;
2475 57 ▶ $self->_listen_failed($L, $gen, $self->_listen_masked(
Condition50%1/2
$err // "unknown error"
l!l
570
MC/DC0%0/1
$err // "unknown error"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
2476 'opening the listen connection failed: ' . ($err // 'unknown error')));
2477 57 return;
2478 }
2479  
2480 # Serialise one channel operation behind the previous one on this connection.
2481 # It runs whether the previous one succeeded or failed.
2482 420 ▶sub _listen_enqueue {
Subroutine _listen_enqueue: called CC=2 SCAR=6.9
2483 420 my ($self, $L, $gen, $method, @args) = @_;
2484 420 ▶ my $prev = $L->{queue} || $self->future_class->done;
Condition66%2/3
$L->{'queue'} || $self->future_class->done
l!l&&r!l&&!r
2461740
MC/DC0%0/2
$L->{'queue'} || $self->future_class->done
$L->{'queue'} $self->future_class->done
Truth table66%2/3
A || B
A= $L->{'queue'}B= $self->future_class->done
ABresult
1X1
011
000
2485 420 my $cb = $self->_listen_callback($L, $gen, $method, @args);
2486 420 ▶ my $run = sub { $cb->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
2487 420 my $next = $self->_listen_retain($prev->then($run, $run));
2488 420 $L->{queue} = $next;
2489 420 return $next;
2490 }
2491  
2492 238 ▶sub _listen_op_subscribe {
Subroutine _listen_op_subscribe: called CC=4 SCAR=14.6
2493 238 my ($self, $L, $gen, $conn, $channel) = @_;
2494 238 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 238 238
2495 238 ▶ return unless exists $L->{channels}{$channel}; # unlistened meanwhile
Branch: 1 branch
BranchTrueFalseTotal
unless exists $L->{"channels"}{$channel} 0 238 238
2496 238 ▶ return if $L->{subscribed}{$channel};
Branch: 1 branch
BranchTrueFalseTotal
if $L->{"subscribed"}{$channel} 0 238 238
2497  
2498 238 return $self->_listen_seam_future('_listen_subscribe', $conn, $channel)->then(
2499 $self->_listen_callback($L, $gen, '_listen_op_subscribed', $channel),
2500 $self->_listen_callback($L, $gen, '_listen_op_failed', $conn, $channel, 'LISTEN'),
2501 );
2502 }
2503  
2504 217 ▶sub _listen_op_subscribed {
Subroutine _listen_op_subscribed: called CC=4 SCAR=14.0
2505 217 my ($self, $L, $gen, $channel) = @_;
2506 217 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 217 217
2507 217 ▶ return unless exists $L->{channels}{$channel};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $L->{"channels"}{$channel} 0 217 217
2508 217 $L->{subscribed}{$channel} = 1;
2509 # While the connection is still being set up, _listen_op_arm settles it.
2510 217 ▶ $self->_listen_settle($L, $channel, 1) if $L->{phase} eq 'up';
Branch: 1 branch
BranchTrueFalseTotal
if $L->{"phase"} eq "up" 28 189 217
2511 217 return;
2512 }
2513  
2514 8 ▶sub _listen_op_unsubscribe {
Subroutine _listen_op_unsubscribe: called CC=3 SCAR=11.2
2515 8 my ($self, $L, $gen, $conn, $channel) = @_;
2516 8 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 8 8
2517 8 ▶ return if exists $L->{channels}{$channel}; # listened again meanwhile
Branch: 1 branch
BranchTrueFalseTotal
if exists $L->{"channels"}{$channel} 0 8 8
2518  
2519 8 my $fc = $self->future_class;
2520 return $self->_listen_seam_future('_listen_unsubscribe', $conn, $channel)->then(
2521 8 ▶ sub { return $fc->done },
Subroutine __ANON__: called CC=1 SCAR=0.0
2522 8 $self->_listen_callback($L, $gen, '_listen_op_failed', $conn, $channel, 'UNLISTEN'),
2523 );
2524 }
2525  
2526 # A channel operation failed. The probe decides what failed: a dead connection
2527 # goes to self-healing; on a live one the statement itself was refused.
2528 13 ▶sub _listen_op_failed {
Subroutine _listen_op_failed: called CC=7 SCAR=20.4
2529 13 my ($self, $L, $gen, $conn, $channel, $verb, $err) = @_;
2530 13 my $fc = $self->future_class;
2531 13 ▶ return $fc->done unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 13 13
2532  
2533 13 ▶ $err = $self->_listen_masked(
Condition50%1/2
$err // "unknown error"
l!l
130
MC/DC0%0/1
$err // "unknown error"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
2534 "$verb on channel '$channel' failed: " . ($err // 'unknown error'));
2535  
2536 13 ▶ if (defined $self->_listen_probe_error($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_listen_probe_error($conn)) 4 9 13
2537 4 $self->_listen_failed($L, $gen, $err);
2538 4 return $fc->fail($err);
2539 }
2540  
2541 9 ▶ if ($verb eq 'LISTEN' && exists $L->{channels}{$channel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq "LISTEN" and exists $L->{"channels"}{$channel}) 9 0 9
Condition33%1/3
$verb eq "LISTEN" and exists $L->{"channels"}{$channel}
!ll&&!rl&&r
009
MC/DC0%0/2
$verb eq "LISTEN" and exists $L->{"channels"}{$channel}
$verb eq "LISTEN" exists $L->{"channels"}{$channel}
Truth table33%1/3
A and B
A= $verb eq "LISTEN"B= exists $L->{"channels"}{$channel}
ABresult
0X0
100
111
2542 9 delete $L->{channels}{$channel};
2543 9 $self->_listen_report($err);
2544 9 $self->_listen_settle($L, $channel, 0, $err);
2545 9 ▶ $self->_listen_teardown($L, $err) unless %{ $L->{channels} };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$L->{"channels"};} 5 4 9
2546 9 return $fc->fail($err);
2547 }
2548  
2549 0 $self->_listen_report($err);
2550 0 return $fc->fail($err);
2551 }
2552  
2553 # Last step on a fresh connection, queued behind every subscribe: re-arm.
2554 174 ▶sub _listen_op_arm {
Subroutine _listen_op_arm: called CC=10 SCAR=23.0
2555 174 my ($self, $L, $gen, $conn, $reconnect) = @_;
2556 174 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 5 169 174
2557  
2558 # Watched from the call on, not from its return (karr k222): the transport
2559 # may dispatch or report the connection lost from inside _listen_watch, and
2560 # whatever closes the connection then must unwatch it too.
2561 169 $L->{watching} = 1;
2562 169 my $err;
2563 {
2564 169 local $@;
2565 169 ▶ eval { $self->_listen_watch($conn); 1 } or $err = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_listen_watch($conn); 1 } } or $err = $@ 165 4 169
2566 }
2567 169 ▶ $err = "starting to watch the listen connection failed: $err" if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 4 165 169
2568  
2569 # A callback or a death report during the watch ended this attempt: whatever
2570 # did so owns the state now.
2571 169 ▶ unless ($self->_listen_is_current($L, $gen)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_listen_is_current($L, $gen)) 8 161 169
2572 8 ▶ $self->_listen_report($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 8 8
2573 8 return;
2574 }
2575 161 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 4 157 161
2576 4 $L->{watching} = 0; # a watch that threw started nothing to stop
2577 4 $self->_listen_failed($L, $gen, $err);
2578 4 return;
2579 }
2580  
2581 157 $L->{phase} = 'up';
2582 157 $L->{attempt} = 0;
2583  
2584 157 ▶ $self->_listen_fire_reconnect if $reconnect;
Branch: 1 branch
BranchTrueFalseTotal
if $reconnect 48 109 157
2585 157 ▶ return unless $self->_listen_is_current($L, $gen); # the hook may act
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 157 157
2586  
2587 $self->_listen_settle($L, $_, 1)
2588 157 for grep { $L->{subscribed}{$_} } keys %{ $L->{waiting} };
2589 157 $self->_listen_settle_ready($L, 1);
2590 157 return;
2591 }
2592  
2593 # The connection of attempt $gen is dead or could not be set up: close it,
2594 # report, fail what waited on this attempt, schedule the next one.
2595 #
2596 # The state is settled before anything user-visible runs: the hook and the
2597 # failed Futures' callbacks may call back into the API (listen_reconnect from
2598 # on_listen_error is the obvious case), so they must find the dead connection
2599 # already gone, and whatever they start supersedes this path.
2600 105 ▶sub _listen_failed {
Subroutine _listen_failed: called CC=7 SCAR=20.5
2601 105 my ($self, $L, $gen, $err) = @_;
2602 105 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 105 105
2603 105 ▶ $err //= 'listen connection lost';
Condition50%1/2
$err //= "listen connection lost"
l!l
1050
MC/DC0%0/1
$err //= "listen connection lost"
$err
Truth table50%1/2
A
A= $err
Aresult
11
00
2604  
2605 105 my $next = ++$L->{gen};
2606 105 my ($conn, $watching) = $self->_listen_detach_conn($L);
2607 105 ▶ $L->{phase} = %{ $L->{channels} } ? 'waiting' : 'idle';
Branch: 1 branch
BranchTrueFalseTotal
%{$L->{'channels'};} ? : 105 0 105
2608  
2609 105 $self->_listen_report($err);
2610 105 $self->_listen_close_conn($L, $conn, $watching);
2611 105 ▶ return unless $self->_listen_is_current($L, $next);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $next) 0 105 105
2612  
2613 105 $self->_listen_settle_all($L, 0, $err);
2614 105 ▶ return unless $self->_listen_is_current($L, $next) && %{ $L->{channels} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $next) and %{$L->{"channels"};} 0 105 105
Condition33%1/3
$self->_listen_is_current($L, $next) and %{$L->{"channels"};}
!ll&&!rl&&r
00105
MC/DC0%0/2
$self->_listen_is_current($L, $next) and %{$L->{"channels"};}
$self->_listen_is_current($L, $next) %{$L->{"channels"};}
Truth table33%1/3
A and B
A= $self->_listen_is_current($L, $next)B= %{$L->{"channels"};}
ABresult
0X0
100
111
2615  
2616 105 $self->_listen_schedule($L, $next);
2617 105 return;
2618 }
2619  
2620 105 ▶sub _listen_schedule {
Subroutine _listen_schedule: called CC=1 SCAR=0.0
2621 105 my ($self, $L, $gen) = @_;
2622 105 my $seconds = $self->_listen_backoff(++$L->{attempt});
2623 105 $L->{phase} = 'waiting';
2624  
2625 105 my $timer = $self->_listen_seam_future('_listen_delay', $seconds);
2626 105 $L->{delay} = $timer;
2627 105 $self->_listen_retain($timer->then(
2628 $self->_listen_callback($L, $gen, '_listen_delay_elapsed'),
2629 $self->_listen_callback($L, $gen, '_listen_delay_failed'),
2630 ));
2631 105 return;
2632 }
2633  
2634 105 ▶sub _listen_backoff {
Subroutine _listen_backoff: called CC=2 SCAR=6.9
2635 105 my ($self, $attempt) = @_;
2636 105 my $max = $self->listen_backoff_max;
2637 105 my $delay = $self->listen_backoff_initial * 2 ** ($attempt - 1);
2638 105 ▶ return $delay > $max ? $max : $delay;
Branch: 1 branch
BranchTrueFalseTotal
$delay > $max ? : 12 93 105
2639 }
2640  
2641 76 ▶sub _listen_delay_elapsed {
Subroutine _listen_delay_elapsed: called CC=2 SCAR=6.9
2642 76 my ($self, $L, $gen) = @_;
2643 76 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 4 72 76
2644 72 delete $L->{delay};
2645 72 $self->_listen_connect($L, 1);
2646 72 return;
2647 }
2648  
2649 0 ▶sub _listen_delay_failed {
Subroutine _listen_delay_failed: not called CC=3 SCAR=24.8
2650 0 my ($self, $L, $gen, $err) = @_;
2651 0 ▶ return unless $self->_listen_is_current($L, $gen);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_listen_is_current($L, $gen) 0 0 0
2652 0 delete $L->{delay};
2653 0 $L->{phase} = 'idle';
2654 0 ▶ $self->_listen_report(
Condition0%0/2
$err // "unknown error"
l!l
00
MC/DC0%0/1
$err // "unknown error"
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
2655 'the reconnect backoff timer failed, self-healing has stopped '
2656 . '(listen_reconnect or a new listen resumes it): ' . ($err // 'unknown error')
2657 );
2658 0 return;
2659 }
2660  
2661 169 ▶sub _listen_cancel_timer {
Subroutine _listen_cancel_timer: called CC=5 SCAR=17.2
2662 169 my ($self, $L) = @_;
2663 169 ▶ my $timer = delete $L->{delay} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $timer = delete $L->{"delay"} or return 33 136 169
2664 33 ▶ return unless $L->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $L->{"pid"} == $$ 0 33 33
2665 33 ▶ $timer->cancel if $timer->can('cancel') && !$timer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $timer->can("cancel") and not $timer->is_ready 0 33 33
Condition33%1/3
$timer->can("cancel") and not $timer->is_ready
!ll&&!rl&&r
3300
MC/DC0%0/2
$timer->can("cancel") and not $timer->is_ready
$timer->can("cancel") not $timer->is_ready
Truth table33%1/3
A and not(B)
A= $timer->can("cancel")B= not $timer->is_ready
ABresult
0X0
100
111
2666 33 return;
2667 }
2668  
2669 # undef when the connection answers the probe, otherwise why it does not.
2670 78 ▶sub _listen_probe_error {
Subroutine _listen_probe_error: called CC=4 SCAR=14.0
2671 78 my ($self, $conn) = @_;
2672 78 my ($alive, $err);
2673 {
2674 78 local $@;
2675 78 ▶ eval { $alive = $self->_listen_probe($conn); 1 } or $err = $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $alive = $self->_listen_probe($conn); 1 } } or $err = $@ 78 0 78
2676 }
2677 78 ▶ return "listen connection probe failed: $err" if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 78 78
2678 78 ▶ return $alive ? undef : 'listen connection probe failed';
Branch: 1 branch
BranchTrueFalseTotal
$alive ? : 70 8 78
2679 }
2680  
2681 # Stop watching and close the current connection, if this process owns it.
2682 169 ▶sub _listen_drop_conn {
Subroutine _listen_drop_conn: called CC=1 SCAR=0.0
2683 169 my ($self, $L) = @_;
2684 169 $self->_listen_close_conn($L, $self->_listen_detach_conn($L));
2685 169 return;
2686 }
2687  
2688 # Take the connection out of the state; returns ($conn, $watching).
2689 274 ▶sub _listen_detach_conn {
Subroutine _listen_detach_conn: called CC=1 SCAR=0.0
2690 274 my ($self, $L) = @_;
2691 274 my $conn = delete $L->{conn};
2692 274 my $watching = delete $L->{watching};
2693 274 $L->{subscribed} = {};
2694 274 $L->{queue} = undef;
2695 274 return ($conn, $watching);
2696 }
2697  
2698 # The physical half: unwatch and close, only in the process that opened it.
2699 274 ▶sub _listen_close_conn {
Subroutine _listen_close_conn: called CC=6 SCAR=18.1
2700 274 my ($self, $L, $conn, $watching) = @_;
2701 274 ▶ return unless defined $conn && $L->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conn and $L->{"pid"} == $$ 119 155 274
Condition66%2/3
defined $conn and $L->{"pid"} == $$
!ll&&!rl&&r
1190155
MC/DC50%1/2
defined $conn and $L->{"pid"} == $$
defined $conn $L->{"pid"} == $$
Truth table66%2/3
A and B
A= defined $connB= $L->{"pid"} == $$
ABresult
0X0
100
111
2702  
2703 155 local $@;
2704 155 ▶ if ($watching) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watching) 146 9 155
2705 146 ▶ eval { $self->_listen_unwatch($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_listen_unwatch($conn); 1 } } or $self->_listen_report("stopping the listen watch failed: $@") 146 0 146
2706 or $self->_listen_report("stopping the listen watch failed: $@");
2707 }
2708 155 ▶ eval { $self->_listen_close($conn); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_listen_close($conn); 1 } } or $self->_listen_report("closing the listen connection failed: $@") 155 0 155
2709 or $self->_listen_report("closing the listen connection failed: $@");
2710 155 return;
2711 }
2712  
2713 # End listening altogether: the last channel went, or the storage disconnects.
2714 # State inherited from a parent process is only forgotten, never acted on.
2715 119 ▶sub _listen_teardown {
Subroutine _listen_teardown: called CC=7 SCAR=20.4
2716 119 my ($self, $L, $reason) = @_;
2717 119 ▶ $L //= $self->{_listen} or return;
Branch: 1 branch
BranchTrueFalseTotal
$L //= $self->{"_listen"} or return 119 0 119
Condition66%2/3
$L //= $self->{"_listen"}
l!l&&r!l&&!r
171020
MC/DC0%0/2
$L //= $self->{"_listen"}
$L $self->{"_listen"}
Truth table66%2/3
A //= B
A= $LB= $self->{"_listen"}
ABresult
1X1
011
000
2718 119 ▶ delete $self->{_listen} if $self->{_listen} && $self->{_listen} == $L;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_listen"} and $self->{"_listen"} == $L 119 0 119
Condition33%1/3
$self->{"_listen"} and $self->{"_listen"} == $L
!ll&&!rl&&r
00119
MC/DC0%0/2
$self->{"_listen"} and $self->{"_listen"} == $L
$self->{"_listen"} $self->{"_listen"} == $L
Truth table33%1/3
A and B
A= $self->{"_listen"}B= $self->{"_listen"} == $L
ABresult
0X0
100
111
2719 119 ▶ return unless $L->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $L->{"pid"} == $$ 2 117 119
2720  
2721 117 $L->{gen}++;
2722 117 $self->_listen_cancel_timer($L);
2723 117 $self->_listen_drop_conn($L);
2724 117 $L->{phase} = 'idle';
2725 117 $L->{attempt} = 0;
2726 117 ▶ $self->_listen_settle_all($L, 0, $reason // 'listening has ended');
Condition50%1/2
$reason // "listening has ended"
l!l
1170
MC/DC0%0/1
$reason // "listening has ended"
$reason
Truth table50%1/2
A
A= $reason
Aresult
11
00
2727 117 return;
2728 }
2729  
2730 200 ▶sub _listen_settle {
Subroutine _listen_settle: called CC=3 SCAR=11.0
2731 200 my ($self, $L, $channel, $ok, $err) = @_;
2732 200 ▶ my $futures = delete $L->{waiting}{$channel} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $futures = delete $L->{"waiting"}{$channel} or return 184 16 200
2733 184 $self->_listen_resolve($_, $ok, $err) for @$futures;
2734 184 return;
2735 }
2736  
2737 379 ▶sub _listen_settle_ready {
Subroutine _listen_settle_ready: called CC=3 SCAR=11.0
2738 379 my ($self, $L, $ok, $err) = @_;
2739 379 ▶ my @futures = @{ $L->{ready} || [] };
Condition50%1/2
$L->{'ready'} || []
l!l
3790
MC/DC0%0/1
$L->{'ready'} || []
$L->{'ready'}
Truth table50%1/2
A
A= $L->{'ready'}
Aresult
11
00
2740 379 $L->{ready} = [];
2741 379 $self->_listen_resolve($_, $ok, $err) for @futures;
2742 379 return;
2743 }
2744  
2745 222 ▶sub _listen_settle_all {
Subroutine _listen_settle_all: called CC=2 SCAR=6.9
2746 222 my ($self, $L, $ok, $err) = @_;
2747 222 $self->_listen_settle($L, $_, $ok, $err) for keys %{ $L->{waiting} };
2748 222 $self->_listen_settle_ready($L, $ok, $err);
2749 222 return;
2750 }
2751  
2752 # $f may be gone: in global destruction Perl frees objects in no particular
2753 # order, and a Future still waiting in the state can be freed before the
2754 # storage whose DESTROY settles it (karr k237). Nobody can observe it then.
2755 268 ▶sub _listen_resolve {
Subroutine _listen_resolve: called CC=5 SCAR=16.2
2756 268 my ($self, $f, $ok, $err) = @_;
2757 268 ▶ return if !defined $f || $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $f or $f->is_ready 1 267 268
Condition66%2/3
not defined $f or $f->is_ready
l!l&&r!l&&!r
10267
MC/DC50%1/2
not defined $f or $f->is_ready
not defined $f $f->is_ready
Truth table66%2/3
not(A) or B
A= not defined $fB= $f->is_ready
ABresult
1X1
011
000
2758 267 local $@;
2759 267 ▶ eval { $ok ? $f->done : $f->fail($err); 1 }
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $ok ? $f->done : $f->fail($err); 1 } } or $self->_listen_report("a callback on a listen Future died: $@") 267 0 267
$ok ? : 205 62 267
2760 or $self->_listen_report("a callback on a listen Future died: $@");
2761 267 return;
2762 }
2763  
2764 134 ▶sub _listen_report {
Subroutine _listen_report: called CC=4 SCAR=13.9
2765 134 my ($self, $err) = @_;
2766 134 ▶ chomp(my $msg = defined $err ? "$err" : 'unknown listen error');
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 134 0 134
2767 134 my $hook = $self->on_listen_error;
2768 134 ▶ unless ($hook) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hook) 8 126 134
2769 8 warn "DBIO LISTEN error: $msg\n";
2770 8 return;
2771 }
2772 126 local $@;
2773 126 ▶ eval { $hook->($self, $msg); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$hook($self, $msg); 1 } } or warn "DBIO on_listen_error hook died: $@" 122 4 126
2774 or warn "DBIO on_listen_error hook died: $@";
2775 126 return;
2776 }
2777  
2778 48 ▶sub _listen_fire_reconnect {
Subroutine _listen_fire_reconnect: called CC=3 SCAR=11.2
2779 48 my ($self) = @_;
2780 48 ▶ my $hook = $self->on_listen_reconnect or return;
Branch: 1 branch
BranchTrueFalseTotal
my $hook = $self->on_listen_reconnect or return 48 0 48
2781 48 local $@;
2782 48 ▶ eval { $hook->($self); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$hook($self); 1 } } or $self->_listen_report("on_listen_reconnect hook died: $@") 48 0 48
2783 or $self->_listen_report("on_listen_reconnect hook died: $@");
2784 48 return;
2785 }
2786  
2787  
2788 93 ▶sub deploy_async {
Subroutine deploy_async: called CC=7 SCAR=19.5
Pod documented
2789 93 my ($self, $schema, $opts) = @_;
2790 93 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
192
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
2791  
2792 # Generate the install DDL from the schema classes (synchronous, in-memory --
2793 # no DB roundtrips). Uses the same _ddl_class->install_ddl($schema) convention
2794 # the sync DBIO::Deploy::Base path uses, NOT a driver-specific schema method.
2795 93 my $ddl = $self->_install_ddl($schema);
2796  
2797 # Optional DROP TABLE pre-pass for idempotent re-runs.
2798 93 ▶ if ($opts->{add_drop_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"add_drop_table"}) 1 92 93
2799 1 $ddl = join("\n\n", $self->_drop_statements_for($schema), $ddl);
2800 }
2801  
2802 93 my $fc = $self->future_class;
2803  
2804 # ADR 0026: never blanket-wrap DDL in a transaction. On a non-transactional
2805 # engine an async transaction is a false atomicity promise (MySQL/Oracle/...
2806 # implicitly COMMIT each DDL statement), so run the whole batch
2807 # statement-at-a-time on one pooled connection with a one-shot warning -- the
2808 # Future-side mirror of DBIO::Deploy::Base::_execute_ddl.
2809 93 ▶ unless ($self->_use_transactional_ddl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_use_transactional_ddl) 30 63 93
2810 30 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
3000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
2811 30 $self->_warn_non_txn_ddl_once(
2812 "$class -- async DDL loop is not atomic; recovery depends on the driver's version-row gate"
2813 );
2814  
2815 # Released to the pool it was acquired from, not to $self->pool asked
2816 # again when the batch ends (karr dbio-async k46).
2817 30 my $pool = $self->pool;
2818 30 ▶ return $self->_hold_derived($pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2819 30 my $conn = shift;
2820 # A dispatch that dies before it has a Future must still release $conn
2821 # (karr k229): as a failed Future it takes the same release below.
2822 30 my $ddl_future = $self->_seam_future(_execute_ddl_async => $conn, $ddl);
2823 30 my ($release, $retire) = _hand_back_once($pool, $conn);
2824 21 ▶ return _retire_on_cancel($ddl_future->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2825 21 $release->();
2826 21 return $fc->done(@_);
2827 9 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2828 9 my $err = shift;
2829 9 $release->();
2830 9 return $fc->fail($err);
2831 30 }), $retire);
2832 30 }));
2833 }
2834  
2835 # Transactional engine. ADR 0026 addendum (k132), async parity (k199): the
2836 # capability answers for the connection, but atomicity is per statement. Ask
2837 # the SAME per-statement hook the sync path asks -- on the owning sync storage,
2838 # the one _use_transactional_ddl delegates to and the one that carries the
2839 # _ddl_statement_is_transactional capability -- and reuse the sync grouping so
2840 # the runs come out in the SAME order and grouping. A transaction-safe run is
2841 # bracketed in one txn_do_async on a pinned connection; a run the hook declares
2842 # non-transactional executes on its own pooled connection, outside any
2843 # transaction. All statements safe (or no hook) => one run, identical to
2844 # bracketing the whole loop.
2845 63 my @stmts = DBIO::Deploy::Base::_split_ddl($self->_owner_storage, $ddl);
2846 63 my @runs = DBIO::Deploy::Base::_group_ddl_statements($self->_owner_storage, \@stmts);
2847  
2848 63 ▶ if (@runs > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@runs > 1) 17 46 63
2849 17 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
2850 17 my $loose = scalar grep { !$_->[0] } @runs;
2851 17 $self->_warn_split_ddl_once(
2852 "$class -- $loose statement(s) cannot run inside a transaction, so the "
2853 . "async DDL batch was split into " . scalar(@runs) . " runs and is not "
2854 . "atomic as a whole; recovery depends on the driver's version-row gate"
2855 );
2856 }
2857  
2858 # The pool the deploy begins on is the pool of every run of it (karr k331).
2859 63 return $self->_run_ddl_runs_async(\@runs, 0, $self->pool);
2860 }
2861  
2862 # Execute the runs of a transactional engine's batch sequentially, in order,
2863 # from run $from on. A failure in any run short-circuits the rest: the ->then
2864 # chain propagates the failure and the next run is never started. Each run
2865 # releases its own connection exactly once -- a txn run through txn_do_async's
2866 # finalizer, an outlier run in both the success and failure continuations
2867 # below.
2868 #
2869 # The next run is this method again, one further on -- not an anonymous sub
2870 # that calls itself through its own variable. Such a sub holds itself, and with
2871 # it the backend and the runs, for good: every deploy left its backend behind,
2872 # and the backend its pool with the connections in it. What chains the runs
2873 # here is held by the Future of the run before only, and goes with it.
2874 #
2875 # $pool is the pool the deploy began on, handed from run to run (karr k331).
2876 # A run that finds it shut down -- the storage was disconnected between two
2877 # runs -- fails the deploy: a later run on a NEW pool would carry on on another
2878 # connection set with the batch's bracket broken, and leave the disconnected
2879 # storage holding a pool nobody asked for. There is no resuming.
2880 74 ▶sub _run_ddl_runs_async {
Subroutine _run_ddl_runs_async: called CC=8 SCAR=21.0
2881 74 my ($self, $runs, $from, $pool) = @_;
2882 74 ▶ $from //= 0;
Condition50%1/2
$from //= 0
l!l
740
MC/DC0%0/1
$from //= 0
$from
Truth table50%1/2
A
A= $from
Aresult
11
00
2883 74 ▶ $pool //= $self->pool;
Condition33%1/3
$pool //= $self->pool
l!l&&r!l&&!r
7400
MC/DC0%0/2
$pool //= $self->pool
$pool $self->pool
Truth table33%1/3
A //= B
A= $poolB= $self->pool
ABresult
1X1
011
000
2884  
2885 74 my $fc = $self->future_class;
2886  
2887 74 ▶ while (my $run = $runs->[$from]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $run = $runs->[$from]) 579 37 616
2888 579 ▶ return $fc->fail('storage disconnected during deploy')
Branch: 1 branch
BranchTrueFalseTotal
if _pool_is_shut_down($pool) 1 578 579
2889 if _pool_is_shut_down($pool);
2890  
2891 578 my $run_future = $self->_start_ddl_run_async($run, $pool);
2892  
2893 # Answered at once and without error: the next run is the next turn of this
2894 # loop, not a further call on top of this one (a ->then on a Future that is
2895 # ready runs its callback on the spot, so a long batch would otherwise
2896 # recurse once per run).
2897 578 ▶ if ($run_future->is_ready && !$run_future->is_failed && !_is_cancelled($run_future)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run_future->is_ready and not $run_future->is_failed and not _is_cancelled($run_future)) 542 36 578
Condition83%5/6
$run_future->is_ready and not $run_future->is_failed and not _is_cancelled($run_future)
!ll&&!rl&&r
360542
$run_future->is_ready and not $run_future->is_failed
!ll&&!rl&&r
1917542
MC/DC66%2/3
$run_future->is_ready and not $run_future->is_failed and not _is_cancelled($run_future)
$run_future->is_ready not $run_future->is_failed not _is_cancelled($run_future)
Truth table75%3/4
A and not(B) and not(C)
A= $run_future->is_readyB= not $run_future->is_failedC= not _is_cancelled($run_future)
ABCresult
0XX0
10X0
1100
1111
2898 542 $from++;
2899 542 next;
2900 }
2901  
2902 # Pending, failed or cancelled: the chain goes on from the Future of this
2903 # run (a failure passes through without the callback).
2904 36 my $next = $from + 1;
2905 11 ▶ return $run_future->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2906 11 return $self->_run_ddl_runs_async($runs, $next, $pool);
2907 36 });
2908 }
2909  
2910 37 return $fc->done;
2911 }
2912  
2913 1203 ▶sub _pool_is_shut_down {
Subroutine _pool_is_shut_down: called CC=4 SCAR=13.9
2914 1203 my ($pool) = @_;
2915 1203 ▶ return 0 unless Scalar::Util::blessed($pool) && $pool->can('is_shutdown');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pool) and $pool->can("is_shutdown") 31 1172 1203
Condition66%2/3
builtin::blessed($pool) and $pool->can("is_shutdown")
!ll&&!rl&&r
0311172
MC/DC50%1/2
builtin::blessed($pool) and $pool->can("is_shutdown")
builtin::blessed($pool) $pool->can("is_shutdown")
Truth table66%2/3
A and B
A= builtin::blessed($pool)B= $pool->can("is_shutdown")
ABresult
0X0
100
111
2916 1172 ▶ return $pool->is_shutdown ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pool->is_shutdown ? : 2 1170 1172
2917 }
2918  
2919 # Start one run of a batch -- $run is [ $transactional, @statements ] -- and
2920 # answer its Future.
2921 578 ▶sub _start_ddl_run_async {
Subroutine _start_ddl_run_async: called CC=3 SCAR=11.0
2922 578 my ($self, $run, $pool) = @_;
2923 578 my $fc = $self->future_class;
2924 578 my ($transactional, @group) = @$run;
2925  
2926 578 ▶ if ($transactional) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transactional) 307 271 578
2927 # The connection BEGIN ran on, asked of the context under the name
2928 # txn_do_async built it with -- never under txn_conn as such (karr k284).
2929 307 my $accessor = $self->_txn_conn_accessor;
2930 306 ▶ return $self->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2931 306 my ($ctx) = @_;
2932 306 return $self->_run_ddl_stmts_async($ctx->$accessor, \@group, undef, $pool);
2933 307 });
2934 }
2935  
2936 # Same as the non-transactional engine branch of deploy_async (karr k229,
2937 # and karr dbio-async k46 for the pool the release goes to).
2938 271 ▶ $pool //= $self->pool;
Condition33%1/3
$pool //= $self->pool
l!l&&r!l&&!r
27100
MC/DC0%0/2
$pool //= $self->pool
$pool $self->pool
Truth table33%1/3
A //= B
A= $poolB= $self->pool
ABresult
1X1
011
000
2939 271 ▶ return $self->_hold_derived($pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2940 271 my $conn = shift;
2941 271 my $ddl_future = $self->_seam_future(_run_ddl_stmts_async => $conn, \@group);
2942 271 my ($release, $retire) = _hand_back_once($pool, $conn);
2943 262 ▶ return _retire_on_cancel($ddl_future->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2944 262 $release->();
2945 262 return $fc->done;
2946 9 ▶ }, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2947 9 my $err = shift;
2948 9 $release->();
2949 9 return $fc->fail($err);
2950 271 }), $retire);
2951 271 }));
2952 }
2953  
2954 2738 ▶sub _is_cancelled {
Subroutine _is_cancelled: called CC=3 SCAR=13.1
2955 2738 my ($f) = @_;
2956 2738 ▶ return $f->can('is_cancelled') && $f->is_cancelled ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$f->can('is_cancelled') && $f->is_cancelled ? : 0 2738 2738
Condition33%1/3
$f->can("is_cancelled") and $f->is_cancelled
!ll&&!rl&&r
273800
MC/DC0%0/2
$f->can("is_cancelled") and $f->is_cancelled
$f->can("is_cancelled") $f->is_cancelled
Truth table33%1/3
A and B
A= $f->can("is_cancelled")B= $f->is_cancelled
ABresult
0X0
100
111
2957 }
2958  
2959 # The two ways a DDL run on a pooled connection hands it back, sharing one
2960 # record so that it happens once (karr k274): release, when the statements were
2961 # answered; retire, when the Future the caller holds was cancelled with a
2962 # statement in flight -- its outcome is unknown and the connection may still be
2963 # busy with it, so it does not go back into the pool.
2964 # The lease of the borrow $conn is in (karr core k275), taken right after the
2965 # acquire resolved and handed back with the release. A pool that is not a
2966 # DBIO::Storage::PoolBase has none: its release is called as before.
2967 998 ▶sub _pool_lease {
Subroutine _pool_lease: called CC=2 SCAR=6.9
2968 998 my ($pool, $conn) = @_;
2969 998 ▶ return $pool->can('_lease') ? $pool->_lease($conn) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pool->can('_lease') ? : 739 259 998
2970 }
2971  
2972 301 ▶sub _hand_back_once {
Subroutine _hand_back_once: called CC=1 SCAR=1.2
2973 301 my ($pool, $conn) = @_;
2974 301 my $gone;
2975 301 my $lease = _pool_lease($pool, $conn);
2976 return (
2977 301 ▶ sub { return if $gone++; $pool->release($conn, $lease); return },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if $gone++ 0 301 301
2978 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2979 0 ▶ return if $gone++;
Branch: 1 branch
BranchTrueFalseTotal
if $gone++ 0 0 0
2980 0 local $@;
2981 0 eval { $pool->_retire_in_use_connection($conn) };
2982 0 return;
2983 },
2984 301 );
2985 }
2986  
2987 # Run $retire when the Future $f is cancelled while pending; returns $f.
2988 301 ▶sub _retire_on_cancel {
Subroutine _retire_on_cancel: called CC=3 SCAR=12.5
2989 301 my ($f, $retire) = @_;
2990 301 ▶ $f->on_cancel($retire) if $f->can('on_cancel') && !$f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("on_cancel") and not $f->is_ready 0 301 301
Condition33%1/3
$f->can("on_cancel") and not $f->is_ready
!ll&&!rl&&r
30100
MC/DC0%0/2
$f->can("on_cancel") and not $f->is_ready
$f->can("on_cancel") not $f->is_ready
Truth table33%1/3
A and not(B)
A= $f->can("on_cancel")B= not $f->is_ready
ABresult
0X0
100
111
2991 301 return $f;
2992 }
2993  
2994  
2995 12 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=1 SCAR=0.0
2996 12 my ($self, $schema) = @_;
2997 12 return $self->_ddl_class->install_ddl($schema);
2998 }
2999  
3000  
3001 1 ▶sub _ddl_class {
Subroutine _ddl_class: called CC=1 SCAR=0.0
3002 1 croak 'Subclass must override _ddl_class to support deploy_async';
3003 }
3004  
3005  
3006 96 ▶sub _use_transactional_ddl {
Subroutine _use_transactional_ddl: called CC=4 SCAR=13.9
3007 96 my ($self) = @_;
3008 96 ▶ my $owner = $self->_owner_storage or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->_owner_storage or return 0 94 2 96
3009 94 ▶ return 0 unless $owner->can('_use_transactional_ddl');
Branch: 1 branch
BranchTrueFalseTotal
unless $owner->can("_use_transactional_ddl") 0 94 94
3010 94 ▶ return $owner->_use_transactional_ddl ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$owner->_use_transactional_ddl ? : 64 30 94
3011 }
3012  
3013  
3014 43 ▶sub _execute_ddl_async {
Subroutine _execute_ddl_async: called CC=1 SCAR=0.0
3015 43 my ($self, $conn, $ddl) = @_;
3016 43 my @stmts = DBIO::Deploy::Base::_split_ddl($self->_owner_storage, $ddl);
3017 43 return $self->_run_ddl_stmts_async($conn, \@stmts);
3018 }
3019  
3020 # Chain a pre-split list of DDL statements on one connection, sequentially --
3021 # each waits for the previous, so the transport never has two DDL statements in
3022 # flight on one connection. Shared by _execute_ddl_async (the string entry
3023 # point, which splits and skips comment-only statements first) and deploy_async
3024 # (which passes an already-grouped, already-cleaned run of statements).
3025 #
3026 # The next statement is this method again, one further on ($from, which a
3027 # caller does not pass), as _txn_statements_settled goes on -- not an anonymous
3028 # sub calling itself through its own variable, which holds itself and with it
3029 # the backend, the connection and the statements not sent yet, for good (see
3030 # _run_ddl_runs_async). The list is copied once, on the way in: the caller's is
3031 # neither changed nor followed.
3032 #
3033 # $pool, when given, is the pool of the deploy this run belongs to: found shut
3034 # down between two statements, the run fails with 'storage disconnected during
3035 # deploy' and sends nothing more -- the transaction around it rolls back.
3036 650 ▶sub _run_ddl_stmts_async {
Subroutine _run_ddl_stmts_async: called CC=10 SCAR=23.0
3037 650 my ($self, $conn, $stmts, $from, $pool) = @_;
3038 650 ▶ $stmts = [ @$stmts ] unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 621 29 650
3039 650 ▶ $from //= 0;
Condition100%2/2
$from //= 0
l!l
29621
MC/DC100%1/1
$from //= 0
$from
Truth table100%2/2
A
A= $from
Aresult
11
00
3040  
3041 650 ▶ while (defined(my $stmt = $stmts->[$from])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $stmt = $stmts->[$from])) 2259 587 2846
3042 2259 ▶ return $self->future_class->fail('storage disconnected during deploy')
Branch: 1 branch
BranchTrueFalseTotal
if $from > 0 and $pool and _pool_is_shut_down($pool) 1 2258 2259
Condition100%6/6
$from > 0 and $pool and _pool_is_shut_down($pool)
!ll&&!rl&&r
16356231
$from > 0 and $pool
!ll&&!rl&&r
6151020624
MC/DC100%3/3
$from > 0 and $pool and _pool_is_shut_down($pool)
$from > 0 $pool _pool_is_shut_down($pool)
Truth table100%4/4
A and B and C
A= $from > 0B= $poolC= _pool_is_shut_down($pool)
ABCresult
0XX0
10X0
1100
1111
3043 if $from > 0 && $pool && _pool_is_shut_down($pool);
3044  
3045 2258 my $f = $self->_query_async_pinned($conn, $stmt, []);
3046  
3047 # Answered at once and without error: the next statement is the next turn
3048 # of this loop, not a further call on top of this one -- a ->then on a
3049 # Future that is ready runs its callback on the spot, so a long run would
3050 # otherwise recurse once per statement. Never a second statement before
3051 # this one is answered: the loop only goes on past a ready Future.
3052 2255 ▶ if ($f->is_ready && !$f->is_failed && !_is_cancelled($f)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_ready and not $f->is_failed and not _is_cancelled($f)) 2196 59 2255
Condition83%5/6
$f->is_ready and not $f->is_failed and not _is_cancelled($f)
!ll&&!rl&&r
5902196
$f->is_ready and not $f->is_failed
!ll&&!rl&&r
40192196
MC/DC66%2/3
$f->is_ready and not $f->is_failed and not _is_cancelled($f)
$f->is_ready not $f->is_failed not _is_cancelled($f)
Truth table75%3/4
A and not(B) and not(C)
A= $f->is_readyB= not $f->is_failedC= not _is_cancelled($f)
ABCresult
0XX0
10X0
1100
1111
3053 2196 $from++;
3054 2196 next;
3055 }
3056  
3057 # Pending, failed or cancelled: go on from this Future (a failure passes
3058 # through without the callback, and nothing after it is sent).
3059 59 my $next = $from + 1;
3060 29 ▶ return $f->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
3061 29 return $self->_run_ddl_stmts_async($conn, $stmts, $next, $pool);
3062 59 });
3063 }
3064  
3065 587 return $self->future_class->done;
3066 }
3067  
3068  
3069 52 ▶sub _ddl_quote_ident {
Subroutine _ddl_quote_ident: called CC=1 SCAR=0.0
3070 52 my ($self, $name) = @_;
3071 52 return DBIO::SQL::Util::_quote_ident($name);
3072 }
3073  
3074  
3075 5 ▶sub _drop_statements_for {
Subroutine _drop_statements_for: called CC=1 SCAR=0.0
3076 5 my ($self, $schema) = @_;
3077 return DBIO::SQL::Util::_drop_statements(
3078 5 ▶ $schema, sub { $self->_ddl_quote_ident($_[0]) }, { cascade => 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
3079 );
3080 }
3081  
3082 # ADR 0026: one-shot informational warning when async DDL runs on a
3083 # non-transactional engine, mirroring the sync DBIO::Deploy::Base helper. Kept
3084 # as a local one-time closure (not DBIO::Carp) so nothing leaks into the
3085 # package's method namespace (t/55namespaces_cleaned.t). Keyed on the full
3086 # message so a different class gets its own one-shot.
3087 109 my %_WARNED_NON_TXN_DDL;
3088 30 ▶sub _warn_non_txn_ddl_once {
Subroutine _warn_non_txn_ddl_once: called CC=2 SCAR=6.9
3089 30 my ($self, $msg) = @_;
3090 30 ▶ return if $_WARNED_NON_TXN_DDL{$msg}++;
Branch: 1 branch
BranchTrueFalseTotal
if $_WARNED_NON_TXN_DDL{$msg}++ 21 9 30
3091 9 warn "non-transactional DDL on $msg\n";
3092 }
3093  
3094 # ADR 0026 addendum (k132), async parity (k199): one-shot notice that a
3095 # transactional engine's DDL batch was split into multiple runs because the
3096 # per-statement hook declared some statement non-transactional, so the batch is
3097 # no longer atomic as a whole. Keyed on the CLASS, not the message -- the text
3098 # carries per-batch statement/run counts that vary, so keying on the message
3099 # would re-arm the one-shot for every new upgrade shape (the same reasoning as
3100 # the sync DBIO::Deploy::Base::_warn_split_ddl_once). Local closure, not
3101 # DBIO::Carp, to keep the package method namespace clean (t/55namespaces_cleaned.t).
3102 my %_WARNED_SPLIT_DDL;
3103 17 ▶sub _warn_split_ddl_once {
Subroutine _warn_split_ddl_once: called CC=3 SCAR=11.3
3104 17 my ($self, $msg) = @_;
3105 17 ▶ my $key = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
3106 17 ▶ return if $_WARNED_SPLIT_DDL{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $_WARNED_SPLIT_DDL{$key}++ 12 5 17
3107 5 warn "split DDL batch on $msg\n";
3108 }
3109  
3110 # --- Sync fallbacks ---
3111 # Let sync methods work by blocking on the async result via ->get.
3112 # Useful for scripts/migrations.
3113  
3114  
3115 4 ▶sub select { my $self = shift; return $self->select_async(@_)->get }
Subroutine select: called CC=1 SCAR=0.0
Pod documented
3116 3 ▶sub select_single { my $self = shift; return $self->select_single_async(@_)->get }
Subroutine select_single: called CC=1 SCAR=0.0
Pod documented
3117 3 ▶sub insert { my $self = shift; return $self->insert_async(@_)->get }
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
3118 3 ▶sub update { my $self = shift; return $self->update_async(@_)->get }
Subroutine update: called CC=1 SCAR=0.0
Pod documented
3119 3 ▶sub delete { my $self = shift; return $self->delete_async(@_)->get }
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
3120 3 ▶sub txn_do { my $self = shift; return $self->txn_do_async(@_)->get }
Subroutine txn_do: called CC=1 SCAR=0.0
Pod documented
3121 1 ▶sub deploy { my $self = shift; return $self->deploy_async(@_)->get }
Subroutine deploy: called CC=1 SCAR=0.0
Pod documented
3122  
3123 # --- Schema integration ---
3124  
3125 235 ▶sub schema { $_[0]->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
3126  
3127  
3128 14 ▶sub debug { $_[0]->{debug} }
Subroutine debug: called CC=1 SCAR=0.0
Pod documented
3129  
3130  
3131 1 ▶sub in_txn { 0 }
Subroutine in_txn: called CC=1 SCAR=0.0
Pod documented
3132  
3133  
3134 2 ▶sub connected { defined $_[0]->{pool} && $_[0]->pool->available > 0 }
Subroutine connected: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{"pool"} and $_[0]->pool->available > 0 1 1 2
3135  
3136  
3137 723 ▶sub disconnect {
Subroutine disconnect: called CC=3 SCAR=11.0
Pod documented
3138 723 my $self = shift;
3139 # Rotate before shutdown so no context from the outgoing pool generation can
3140 # dispatch while or after that pool is being torn down. This also rotates when
3141 # the pool was never materialised.
3142 723 $self->_rotate_context_owner_token;
3143 $self->_listen_teardown(undef, 'the storage was disconnected')
3144 723 ▶ if $self->{_listen};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_listen"} 102 621 723
3145 723 ▶ if ($self->{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pool"}) 507 216 723
3146 507 $self->{pool}->shutdown;
3147 507 $self->{pool} = undef;
3148 }
3149 }
3150  
3151 941 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
3152 941 my $self = shift;
3153 941 ▶ $self->disconnect if $self->{pool} || $self->{_listen};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pool"} or $self->{"_listen"} 417 524 941
Condition100%3/3
$self->{"pool"} or $self->{"_listen"}
l!l&&r!l&&!r
32691524
MC/DC100%2/2
$self->{"pool"} or $self->{"_listen"}
$self->{"pool"} $self->{"_listen"}
Truth table100%3/3
A or B
A= $self->{"pool"}B= $self->{"_listen"}
ABresult
1X1
011
000
3154 }
3155  
3156 109 1;
3157  
3158 __END__
« blib/lib/DBIO/DeploymentHandler/VersionHandler/Monotonic.pm ↑ Summary blib/lib/DBIO.pm »