EV/Pool.pm

filter: Statementstmt 98.7% 156 / 158 Branchbran 86.1% 62 / 72 Conditioncond 60.5% 23 / 38 MC/DCmcdc 34.7% 8 / 23 Subroutinesub 100% 29 / 29 Podpod n/a totaltotal 86.8% 278 / 320 CC 55 SCAR 15.9
CC
55
Cov
90%
__ANON__
8.6
_mysql_held_by_callbacks
8.4
_mysql_new_handle
8.0
CRAP 4.9
« EV/Future.pm ↑ Summary EV.pm »
1 package DBIO::MySQL::EV::Pool;
2 # ABSTRACT: EV::MariaDB connection pool for DBIO
3  
4 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 58 our $VERSION = '0.900007';
8  
9 58 ▶use base 'DBIO::Storage::PoolBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 58 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 58 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 58 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 58 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 58 ▶use DBIO::MySQL::EV::QueryExecutor ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18  
19  
20 281 ▶sub _create_connection {
Subroutine _create_connection: called CC=2 SCAR=7.0
21 281 my ($self, $conninfo) = @_;
22 281 $self->_mysql_refuse_unconnectable($conninfo);
23 277 ▶ $self->{_mysql_fork} ||= _mysql_pool_fork_watcher($self);
Condition66%2/3
$self->{"_mysql_fork"} ||= _mysql_pool_fork_watcher($self)
l!l&&r!l&&!r
532240
MC/DC0%0/2
$self->{"_mysql_fork"} ||= _mysql_pool_fork_watcher($self)
$self->{"_mysql_fork"} _mysql_pool_fork_watcher($self)
Truth table66%2/3
A ||= B
A= $self->{"_mysql_fork"}B= _mysql_pool_fork_watcher($self)
ABresult
1X1
011
000
24 277 return $self->_create_ready_connection(%$conninfo);
25 }
26  
27  
28 635 ▶sub _mysql_connectable {
Subroutine _mysql_connectable: called CC=4 SCAR=13.9
29 635 my ($class, $conninfo) = @_;
30 635 ▶ return 0 unless ref $conninfo eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conninfo eq "HASH" 3 632 635
31 632 ▶ return (defined $conninfo->{host} || defined $conninfo->{user}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $conninfo->{'host'} || defined $conninfo->{'user'} ? : 612 20 632
Condition100%3/3
defined $conninfo->{"host"} or defined $conninfo->{"user"}
l!l&&r!l&&!r
608420
MC/DC100%2/2
defined $conninfo->{"host"} or defined $conninfo->{"user"}
defined $conninfo->{"host"} defined $conninfo->{"user"}
Truth table100%3/3
A or B
A= defined $conninfo->{"host"}B= defined $conninfo->{"user"}
ABresult
1X1
011
000
32 }
33  
34  
35 367 ▶sub _mysql_refuse_unconnectable {
Subroutine _mysql_refuse_unconnectable: called CC=6 SCAR=18.1
36 367 my ($class, $conninfo) = @_;
37 367 ▶ return if $class->_mysql_connectable($conninfo);
Branch: 1 branch
BranchTrueFalseTotal
if $class->_mysql_connectable($conninfo) 361 6 367
38 6 ▶ my $shape = !defined $conninfo ? 'nothing (undef)'
Branch: 4 branches
BranchTrueFalseTotal
!defined($conninfo) ? : 0 6 6
ref $conninfo ne 'HASH' ? : 2 4 6
ref $conninfo ? : 0 2 2
!%$conninfo ? : 1 3 4
39 : ref $conninfo ne 'HASH' ? (ref $conninfo ? 'a ' . ref($conninfo) . ' ref' : 'a plain string')
40 : !%$conninfo ? 'an empty hashref'
41 : sprintf 'a hashref with keys (%s)', join ', ', sort keys %$conninfo;
42 6 Carp::croak("MySQL ev cannot connect from the connect info: $shape. "
43 . 'It takes a hashref of EV::MariaDB connection parameters that names a '
44 . 'host or a user -- EV::MariaDB opens no connection without one of them, '
45 . 'and takes no connection string');
46 }
47  
48 # The pool's fork guard: one EV::fork watcher for all its connections, armed in
49 # the process that creates the first one. When it fires, the parent's handles
50 # are in one of two places, and it finishes both, with no bookkeeping per
51 # connection:
52 # - _connections, as long as this process neither armed the watcher nor
53 # owns the pool: everything the pool holds is then inherited -- idle,
54 # checked out and pinned alike, since checked-out and pinned ones stay in
55 # _connections and a retired one has left it. The process that armed the
56 # watcher opened the pool's first connection (a parent that ever opened
57 # one armed it already), so the pool holds nothing it inherited -- k46's
58 # check. Once this process owns the pool (core karr k233), _connections
59 # holds its OWN connections and must be left working. With k233 the first
60 # check never changes an outcome: a process opens a connection only
61 # through acquire, whose _verify_pid made it the owner. With a core before
62 # k233 the owner never changes, and the two checks together are exactly
63 # k46's.
64 # - the weak stash our _verify_pid fills just before core drops the inherited
65 # connections on takeover. Only a handle something else keeps alive is
66 # still there -- a statement in flight, a pinned transaction -- and it
67 # still has its watchers on the parent's socket (karr k54).
68 # The pool's own state is left as it is. Private helpers are _mysql_*: they
69 # share an inheritance chain with core's privates, as in
70 # DBIO::MySQL::EV::Storage.
71 #
72 # keepalive(0): an active EV::fork watcher alone keeps EV::run from ever
73 # returning. A guard must never be the reason the loop goes on running -- a
74 # program that runs the loop until its queries are done would hang on it, in
75 # the parent and in a child alike. It still fires after a fork.
76 224 ▶sub _mysql_pool_fork_watcher {
Subroutine _mysql_pool_fork_watcher: called CC=1 SCAR=0.0
77 224 my ($pool) = @_;
78 224 require EV;
79 224 my $w = EV::fork(_mysql_pool_fork_cb($pool, $$));
80 224 $w->keepalive(0);
81 224 return $w;
82 }
83  
84 225 ▶sub _mysql_pool_fork_cb {
Subroutine _mysql_pool_fork_cb: called CC=1 SCAR=0.2
85 225 my ($pool, $pid) = @_;
86 225 Scalar::Util::weaken(my $weak_pool = $pool);
87 3 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=21.5
88 3 ▶ my $pool = $weak_pool or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pool = $weak_pool or return 3 0 3
89 3 ▶ my @parents = grep { defined } @{ delete $pool->{_mysql_inherited} || [] };
Condition100%2/2
delete $pool->{'_mysql_inherited'} || []
l!l
12
MC/DC100%1/1
delete $pool->{'_mysql_inherited'} || []
delete $pool->{'_mysql_inherited'}
Truth table100%2/2
A
A= delete $pool->{'_mysql_inherited'}
Aresult
11
00
90 0 ▶ push @parents, grep { defined } @{ $pool->{_connections} || [] }
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid and _mysql_pool_inherited($pool) 0 3 3
Condition20%1/5
$$ != $pid and _mysql_pool_inherited($pool)
!ll&&!rl&&r
300
$pool->{'_connections'} || []
l!l
00
MC/DC0%0/2
$$ != $pid and _mysql_pool_inherited($pool)
$$ != $pid _mysql_pool_inherited($pool)
MC/DC0%0/1
$pool->{'_connections'} || []
$pool->{'_connections'}
Truth table33%1/3
A and B
A= $$ != $pidB= _mysql_pool_inherited($pool)
ABresult
0X0
100
111
Truth table0%0/2
A
A= $pool->{'_connections'}
Aresult
11
00
91 if $$ != $pid && _mysql_pool_inherited($pool);
92 # A child resumed the event loop. Its copies of the parent's handles must
93 # not read the parent's sockets: finish them, which in a forked process
94 # drops the watchers and the client state without sending COM_QUIT or
95 # shutting the shared sockets down.
96 3 for my $mdb (@parents) {
97 1 local $@;
98 1 eval { $mdb->finish; 1 };
99 1 DBIO::MySQL::EV::QueryExecutor->forget($mdb); # karr k82
100 }
101 3 return;
102 225 };
103 }
104  
105 # Does this process NOT own the pool -- are the pool's connections still the
106 # ones it inherited across a fork? Exactly the test core's _verify_pid acts on
107 # (karr core k233), so our _verify_pid and the fork watcher agree with core on
108 # which connections are the parent's. Deliberately not the negation of core's
109 # _teardown_belongs_here: that counts a pool with no recorded owner as not
110 # ours, the safe side for closing and the unsafe one here, where finish in the
111 # process that opened a handle is a real close. A pool with no recorded owner
112 # is left alone, as core's _verify_pid leaves it.
113 1594 ▶sub _mysql_pool_inherited {
Subroutine _mysql_pool_inherited: called CC=3 SCAR=11.0
114 1594 my ($pool) = @_;
115 1594 my $owner = $pool->{_owner_pid};
116 1594 ▶ return defined $owner && $owner != $$ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $owner && $owner != $$ ? : 1 1593 1594
Condition66%2/3
defined $owner and $owner != $$
!ll&&!rl&&r
015931
MC/DC50%1/2
defined $owner and $owner != $$
defined $owner $owner != $$
Truth table66%2/3
A and B
A= defined $ownerB= $owner != $$
ABresult
0X0
100
111
117 }
118  
119  
120 1594 ▶sub _verify_pid {
Subroutine _verify_pid: called CC=5 SCAR=16.2
121 1594 my ($self) = @_;
122 1594 ▶ if (_mysql_pool_inherited($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_mysql_pool_inherited($self)) 1 1593 1594
123 1 ▶ my $stash = $self->{_mysql_inherited} ||= [];
Condition50%1/2
$self->{"_mysql_inherited"} ||= []
l!l
01
MC/DC0%0/1
$self->{"_mysql_inherited"} ||= []
$self->{"_mysql_inherited"}
Truth table50%1/2
A
A= $self->{"_mysql_inherited"}
Aresult
11
00
124 1 ▶ push @$stash, grep { defined } @{ $self->{_connections} || [] };
Condition50%1/2
$self->{'_connections'} || []
l!l
10
MC/DC0%0/1
$self->{'_connections'} || []
$self->{'_connections'}
Truth table50%1/2
A
A= $self->{'_connections'}
Aresult
11
00
125 1 Scalar::Util::weaken($_) for @$stash;
126 }
127 1594 return $self->SUPER::_verify_pid(@_);
128 }
129  
130  
131 203 ▶sub _new_connection {
Subroutine _new_connection: called CC=1 SCAR=0.0
132 203 my ($self, %args) = @_;
133  
134 # Weak, like core's own on_connect / on_error: the handle keeps its handlers
135 # for as long as it lives, and the pool holds the handle.
136 203 my $on_error = $args{on_error};
137 203 my $weak_mdb;
138 203 Scalar::Util::weaken(my $weak_pool = $self);
139 6 ▶ $args{on_error} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
140 6 my ($pool, $mdb) = ($weak_pool, $weak_mdb);
141 6 ▶ $pool->_mysql_report_if_gone($mdb) if $pool && $mdb;
Branch: 1 branch
BranchTrueFalseTotal
if $pool and $mdb 4 2 6
Condition66%2/3
$pool and $mdb
!ll&&!rl&&r
024
MC/DC50%1/2
$pool and $mdb
$pool $mdb
Truth table66%2/3
A and B
A= $poolB= $mdb
ABresult
0X0
100
111
142 6 ▶ $on_error->(@_) if $on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_error 6 0 6
143 6 return;
144 203 };
145 203 my $mdb = $self->_mysql_new_handle(%args);
146 203 Scalar::Util::weaken($weak_mdb = $mdb);
147 203 return $mdb;
148 }
149  
150  
151 304 ▶sub _mysql_new_handle {
Subroutine _mysql_new_handle: called CC=8 SCAR=20.8
152 304 my ($class, %args) = @_;
153 304 require EV::MariaDB;
154  
155 # CLIENT_FOUND_ROWS on, as both DBDs have it: an UPDATE reports the rows it
156 # matched, not only those it changed (karr k80). Off only when the connect
157 # info says found_rows => 0 (mysql_client_found_rows=0 in a DSN).
158 304 ▶ $args{found_rows} = 1 unless defined $args{found_rows};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"found_rows"} 302 2 304
159  
160 304 ▶ if (my $on_error = $args{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_error = $args{"on_error"}) 289 15 304
161 10 ▶ $args{on_error} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 10 $on_error->(DBIO::Util::mask_connect_secrets($_[0]), @_[1 .. $#_]);
163 10 return;
164 289 };
165 }
166 # Over TLS libmariadb leaves the socket blocking, and a blocking socket holds
167 # the loop for every statement (karr k85). Once the connect is done -- and
168 # after every reconnect, on_connect fires again -- the socket is made
169 # non-blocking, so the handle suspends on EAGAIN as it does over plain TCP.
170 # For every handle, not only one with ssl_* parameters (karr k95): the connect
171 # info does not say whether TLS was negotiated, and over plain TCP the flag
172 # was measured to change nothing (MySQL 8.0, MariaDB 11.8).
173 304 my $weak_mdb;
174 304 my $on_connect = $args{on_connect};
175 297 ▶ $args{on_connect} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
176 297 ▶ _mysql_socket_nonblocking($weak_mdb) if $weak_mdb;
Branch: 1 branch
BranchTrueFalseTotal
if $weak_mdb 297 0 297
177 297 ▶ return $on_connect ? $on_connect->(@_) : ();
Branch: 1 branch
BranchTrueFalseTotal
$on_connect ? : 293 4 297
178 304 };
179 304 my ($mdb, $died);
180 {
181 304 local $@; # the caller's $@ is left alone when the handle is made
182 304 ▶ eval { $mdb = EV::MariaDB->new(%args); 1 } or $died = $@ || 'EV::MariaDB->new died';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $mdb = "EV::MariaDB"->new(%args); 1 } } or $died = $@ || "EV::MariaDB->new died" 298 6 304
Condition50%1/2
$@ || 'EV::MariaDB->new died'
l!l
60
MC/DC0%0/1
$@ || 'EV::MariaDB->new died'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
183 }
184 304 ▶ Scalar::Util::weaken($weak_mdb = $mdb) if defined $mdb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mdb 298 6 304
185 304 ▶ return $mdb unless defined $died;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $died 298 6 304
186 6 my $text = DBIO::Util::mask_connect_secrets($died, [ undef, $args{user}, $args{password} ]);
187 # EV::MariaDB 0.09 throws from new when the connect cannot even be started
188 # (a socket it cannot open); older ones called on_error. With an on_error
189 # the throw is treated the same way: its masked text goes to the callback,
190 # and the handle returned is an inert one (no host, no user: no connect) the
191 # caller's readiness wiring can hold, already failed.
192 6 ▶ if (my $on_error = $args{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_error = $args{"on_error"}) 4 2 6
193 4 my $inert = EV::MariaDB->new;
194 4 Scalar::Util::weaken($weak_mdb = $inert);
195 4 $on_error->($text);
196 4 return $inert;
197 }
198 2 die $text;
199 }
200  
201  
202 298 ▶sub _mysql_socket_nonblocking {
Subroutine _mysql_socket_nonblocking: called CC=4 SCAR=14.0
203 298 my ($mdb) = @_;
204 298 my $fd = $mdb->socket;
205 298 ▶ return 0 unless defined $fd && $fd =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd =~ /\A[0-9]+\z/ 151 147 298
Condition66%2/3
defined $fd and $fd =~ /\A[0-9]+\z/
!ll&&!rl&&r
0151147
MC/DC50%1/2
defined $fd and $fd =~ /\A[0-9]+\z/
defined $fd $fd =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $fdB= $fd =~ /\A[0-9]+\z/
ABresult
0X0
100
111
206 147 ▶ open(my $dup, '+<&', $fd) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $dup, "+<&", $fd or return 0 147 0 147
207 147 $dup->blocking(0);
208 147 close $dup;
209 147 return 1;
210 }
211  
212  
213 196 ▶sub _mysql_connection_gone {
Subroutine _mysql_connection_gone: called CC=7 SCAR=19.5
214 196 my ($self, $mdb) = @_;
215  
216 # Guarded before vec, which dies on a negative offset.
217 196 my $fd = $mdb->socket;
218 196 ▶ return 1 unless defined $fd && $fd =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd =~ /\A[0-9]+\z/ 27 169 196
Condition66%2/3
defined $fd and $fd =~ /\A[0-9]+\z/
!ll&&!rl&&r
027169
MC/DC50%1/2
defined $fd and $fd =~ /\A[0-9]+\z/
defined $fd $fd =~ /\A[0-9]+\z/
Truth table66%2/3
A and B
A= defined $fdB= $fd =~ /\A[0-9]+\z/
ABresult
0X0
100
111
219  
220 169 ▶ return 0 if $mdb->pending_count;
Branch: 1 branch
BranchTrueFalseTotal
if $mdb->pending_count 5 164 169
221  
222 # select only reports: what the server sent stays on the socket. A zero
223 # timeout cannot wait, but it can be interrupted -- the kernel answers EINTR
224 # for a pending signal when nothing is readable -- which says nothing about
225 # the socket, so ask again.
226 164 my $rin = '';
227 164 vec($rin, $fd, 1) = 1;
228 164 my $found;
229 167 do { $found = select(my $rout = $rin, undef, undef, 0) }
230 164 ▶ while $found < 0 && $!{EINTR};
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$found < 0 and $!{"EINTR"} and do { do { $found = select(my $rout = $rin, undef, undef, 0) } } 3 164 167
Condition66%2/3
$found < 0 and $!{"EINTR"}
!ll&&!rl&&r
16403
MC/DC50%1/2
$found < 0 and $!{"EINTR"}
$found < 0 $!{"EINTR"}
Truth table66%2/3
A and B
A= $found < 0B= $!{"EINTR"}
ABresult
0X0
100
111
231  
232 # Readable (> 0) is gone; so is a select that failed (< 0, the fd is not
233 # open). Only "nothing to read" is a session that is still there.
234 164 ▶ return $found == 0 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$found == 0 ? : 148 16 164
235 }
236  
237  
238 40 ▶sub _mysql_report_if_gone {
Subroutine _mysql_report_if_gone: called CC=2 SCAR=6.9
239 40 my ($self, $mdb) = @_;
240 40 ▶ $self->_mark_connection_dead($mdb) if $self->_mysql_connection_gone($mdb);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_mysql_connection_gone($mdb) 17 23 40
241 40 return;
242 }
243  
244  
245 131 ▶sub _idle_connection_alive {
Subroutine _idle_connection_alive: called CC=2 SCAR=6.9
246 131 my ($self, $mdb) = @_;
247 131 ▶ return $self->_mysql_connection_gone($mdb) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_mysql_connection_gone($mdb) ? : 14 117 131
248 }
249  
250  
251 121 ▶sub _new_waiter {
Subroutine _new_waiter: called CC=2 SCAR=6.9
252 121 my ($self) = @_;
253 121 my $waiter = $self->SUPER::_new_waiter;
254 121 ▶ $waiter->_queued_in($self) if $waiter->can('_queued_in');
Branch: 1 branch
BranchTrueFalseTotal
if $waiter->can("_queued_in") 46 75 121
255 121 return $waiter;
256 }
257  
258  
259 83 ▶sub _mysql_held_by_callbacks {
Subroutine _mysql_held_by_callbacks: called CC=8 SCAR=21.3
260 83 my ($self) = @_;
261 83 ▶ return 0 if $self->available;
Branch: 1 branch
BranchTrueFalseTotal
if $self->available 0 83 83
262 83 ▶ return 0 if keys %{ $self->{_spawning} || {} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->{"_spawning"} or {};} 2 81 83
Condition50%1/2
$self->{'_spawning'} || {}
l!l
830
MC/DC0%0/1
$self->{'_spawning'} || {}
$self->{'_spawning'}
Truth table50%1/2
A
A= $self->{'_spawning'}
Aresult
11
00
263 81 ▶ my @held = grep { defined } @{ $self->{_connections} || [] } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@held) = grep({defined $_;} @{$self->{"_connections"} or [];}) or return 0 81 0 81
Condition50%1/2
$self->{'_connections'} || []
l!l
810
MC/DC0%0/1
$self->{'_connections'} || []
$self->{'_connections'}
Truth table50%1/2
A
A= $self->{'_connections'}
Aresult
11
00
264 81 ▶ DBIO::MySQL::EV::QueryExecutor->_waits_for_callback($_) or return 0 for @held;
Branch: 1 branch
BranchTrueFalseTotal
"DBIO::MySQL::EV::QueryExecutor"->_waits_for_callback($_) or return 0 10 72 82
265 9 return 1;
266 }
267  
268  
269 276 ▶sub _shutdown_connection {
Subroutine _shutdown_connection: called CC=3 SCAR=11.0
270 276 my ($self, $mdb) = @_;
271 276 ▶ return unless defined $mdb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mdb 0 276 276
272 276 local $@;
273 # Before finish: it calls back the statement in flight, which would start
274 # the waiting ones on the finished handle (karr k98).
275 276 DBIO::MySQL::EV::QueryExecutor->_fail_waiting($mdb);
276 276 eval { $mdb->finish; 1 };
277 276 my $err = $@;
278 276 DBIO::MySQL::EV::QueryExecutor->forget($mdb);
279 276 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 273 276
280 273 return;
281 }
282  
283 58 1;
284  
285 __END__
« EV/Future.pm ↑ Summary EV.pm »