Async/Storage.pm

filter: Statementstmt 78.5% 66 / 84 Branchbran 28.5% 4 / 14 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 77.7% 28 / 36 Podpod 100% 3 / 3 totaltotal 68.6% 105 / 153 CC 12 SCAR 12.4
CC
12
Cov
69%
_conn_poll_fh
20.0
_debug_query
6.0
pool
3.3
CRAP 3.4
↑ Summary Async/Pool.pm »
1 package DBIO::Async::Storage;
2 # ABSTRACT: Future::IO transport backend for DBIO async storage
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use base 'DBIO::Storage::Async';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Future::IO qw( POLLIN );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use DBIO::Async::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use DBIO::Async::TransactionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17 1 ▶sub future_class { 'Future' }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
18  
19  
20 0 ▶sub transport_capabilities { qw(on_connect_replay) }
Subroutine transport_capabilities: not called CC=1 SCAR=6.9
Pod documented
21  
22  
23 18 ▶sub pool {
Subroutine pool: called CC=3 SCAR=11.8
Pod documented
24 18 my $self = shift;
25 18 ▶ $self->{pool} ||= do {
Condition66%2/3
$self->{"pool"} ||= do {
l!l&&r!l&&!r
1080
MC/DC0%0/2
$self->{"pool"} ||= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
$self->{"pool"} do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
Truth table66%2/3
A ||= B
A= $self->{"pool"}B= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::Async::Pool"->new(%args) }
ABresult
1X1
011
000
26 my %args = (
27 storage => $self,
28 size => $self->{_pool_size},
29 0 ▶ on_error => sub { warn "DBIO::Async pool error: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 8 );
31  
32 8 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 0 8 8
33 0 $args{conninfo_provider} = $provider;
34 }
35 else {
36 8 $args{conninfo} = $self->{_conninfo};
37 }
38  
39 8 DBIO::Async::Pool->new(%args);
40 };
41 }
42  
43  
44 1 ▶sub _async_broker_conninfo {
Subroutine _async_broker_conninfo: called CC=1 SCAR=0.0
45 1 my ($self, $mode) = @_;
46 1 my ($conninfo) = $self->_normalize_async_connect_info(
47 $self->_current_async_connect_info($mode)
48 );
49 1 return $conninfo;
50 }
51  
52 # --- Future::IO transport ---
53 # Concrete overrides of the DBIO::Storage::Async transport seams. The inherited
54 # Model-B orchestration (_run_crud & friends) calls these; they route through
55 # the Future::IO watcher seam below.
56  
57  
58 2 ▶sub _query_async {
Subroutine _query_async: called CC=3 SCAR=11.1
59 2 my ($self, $sql, $bind) = @_;
60 2 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
20
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
61 2 $sql = $self->_transform_sql($sql);
62  
63 2 ▶ $self->_debug_query($sql, $bind) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
64  
65 2 ▶ return $self->pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 2 my $conn = shift;
67 return $self->_await_query_result($conn, $sql, $bind)
68 2 ▶ ->on_ready(sub { $self->pool->release($conn) });
Subroutine __ANON__: called CC=1 SCAR=0.0
69 2 });
70 }
71  
72  
73 1 ▶sub _query_async_pinned {
Subroutine _query_async_pinned: called CC=3 SCAR=11.3
74 1 my ($self, $conn, $sql, $bind) = @_;
75 1 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
10
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
76 1 $sql = $self->_transform_sql($sql);
77  
78 1 ▶ $self->_debug_query($sql, $bind) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
79  
80 1 return $self->_await_query_result($conn, $sql, $bind);
81 }
82  
83 0 ▶sub _debug_query {
Subroutine _debug_query: not called CC=2 SCAR=17.9
84 0 my ($self, $sql, $bind) = @_;
85 0 ▶ my $bind_str = join(', ', map { defined $_ ? "'$_'" : 'NULL' } @$bind);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
86 0 warn "$sql: $bind_str\n";
87 }
88  
89 # --- Future::IO Watcher Seam ---
90 #
91 # These methods isolate the Future::IO dependency. A driver that uses its
92 # own event-loop integration overrides _query_async / _query_async_pinned
93 # entirely and never calls these.
94  
95  
96 0 ▶sub _await_readable {
Subroutine _await_readable: not called CC=1 SCAR=6.9
97 0 my ($self, $conn) = @_;
98 0 return Future::IO->poll($self->_conn_poll_fh($conn), POLLIN);
99 }
100  
101  
102 0 ▶sub _conn_poll_fh {
Subroutine _conn_poll_fh: not called CC=4 SCAR=30.0
103 0 my ($self, $conn) = @_;
104 0 ▶ return $conn->{_dbio_async_poll_fh} ||= do {
Condition0%0/3
$conn->{"_dbio_async_poll_fh"} ||= do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$conn->{"_dbio_async_poll_fh"} ||= do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
$conn->{"_dbio_async_poll_fh"} do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
Truth table0%0/3
A ||= B
A= $conn->{"_dbio_async_poll_fh"}B= do { my $fd = $self->_conn_fileno($conn); open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!"); $fh }
ABresult
1X1
011
000
105 0 my $fd = $self->_conn_fileno($conn);
106 0 ▶ open my $fh, '+<&', $fd
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "+<&", $fd or croak('Cannot dup async connection fd ' . (defined $fd ? $fd : "undef") . " for Future::IO poll: $!") 0 0 0
defined $fd ? : 0 0 0
107 or croak 'Cannot dup async connection fd '
108 . (defined $fd ? $fd : 'undef') . " for Future::IO poll: $!";
109 0 $fh;
110 };
111 }
112  
113  
114 10 ▶sub _await_conn_ready {
Subroutine _await_conn_ready: called CC=2 SCAR=10.1
115 10 my ($self, $conn) = @_;
116 10 ▶ return Future->done($conn) if $self->_conn_ready($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_conn_ready($conn) 10 0 10
117  
118 0 ▶ return Future->call(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 ▶ return $self->_await_readable($conn)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 return $self->_await_conn_ready($conn);
121 0 });
122 0 });
123 }
124  
125  
126 3 ▶sub _await_query_result {
Subroutine _await_query_result: called CC=1 SCAR=0.0
127 3 my ($self, $conn, $sql, $bind) = @_;
128  
129 3 ▶ return Future->call(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 3 $self->_submit_query($conn, $sql, $bind);
131 3 ▶ return $self->_await_readable($conn)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 3 return $self->_collect_result($conn, $sql, $bind);
133 3 });
134 3 });
135 }
136  
137 # --- DB-specific seam hooks ---
138 #
139 # Filled in by a concrete DBD subclass; each croaks until provided. The
140 # SQL-shaping and pipeline seams are inherited as croaking seams from
141 # DBIO::Storage::Async. These are the ones specific to the Future::IO transport,
142 # plus the two whose inherited base default (identity conninfo / a named txn
143 # context) does not fit an abstract transport -- a concrete backend must name
144 # its own conninfo shape and transaction context.
145  
146 1 ▶sub _submit_query {
Subroutine _submit_query: called CC=1 SCAR=0.0
147 1 croak 'Subclass must override _submit_query($conn, $sql, $bind)';
148 }
149  
150 1 ▶sub _collect_result {
Subroutine _collect_result: called CC=1 SCAR=0.0
151 1 croak 'Subclass must override _collect_result($conn, $sql, $bind)';
152 }
153  
154 1 ▶sub _normalize_conninfo {
Subroutine _normalize_conninfo: called CC=1 SCAR=0.0
155 1 croak 'Subclass must override _normalize_conninfo($info)';
156 }
157  
158 1 ▶sub _create_pool_connection {
Subroutine _create_pool_connection: called CC=1 SCAR=0.0
159 1 croak 'Subclass must override _create_pool_connection($conninfo)';
160 }
161  
162 1 ▶sub _shutdown_pool_connection {
Subroutine _shutdown_pool_connection: called CC=1 SCAR=0.0
163 1 croak 'Subclass must override _shutdown_pool_connection($conn)';
164 }
165  
166 1 ▶sub _conn_ready {
Subroutine _conn_ready: called CC=1 SCAR=0.0
167 1 croak 'Subclass must override _conn_ready($conn)';
168 }
169  
170 0 ▶sub _conn_fileno {
Subroutine _conn_fileno: not called CC=1 SCAR=6.9
171 0 croak 'Subclass must override _conn_fileno($conn) when _conn_ready can return false';
172 }
173  
174 1 ▶sub _txn_context_class {
Subroutine _txn_context_class: called CC=1 SCAR=0.0
175 1 croak 'Subclass must override _txn_context_class';
176 }
177  
178 1 ▶sub _txn_conn_accessor {
Subroutine _txn_conn_accessor: called CC=1 SCAR=0.0
179 1 croak 'Subclass must override _txn_conn_accessor';
180 }
181  
182 8 1;
183  
184 __END__
185  
186 =pod
187  
188 =encoding UTF-8
189  
190 =head1 NAME
191  
192 DBIO::Async::Storage - Future::IO transport backend for DBIO async storage
193  
194 =head1 VERSION
195  
196 version 0.900001
197  
198 =head1 DESCRIPTION
199  
200 The C<future_io> async backend (ADR 0030): one loop-agnostic transport that
201 drives a driver's own non-blocking DBD binding through L<Future::IO>. The
202 event loop is chosen by installing a C<Future::IO::Impl::*> adapter, not by
203 picking a distribution.
204  
205 The B<Model-B orchestration> -- connect-info normalisation, the CRUD runner
206 (L<DBIO::Storage::Async/_run_crud> with its pooled and pinned runners), INSERT
207 returned-columns mapping, L<DBIO::Storage::Async/txn_do_async> bracketing
208 through the generic L<DBIO::Storage::Async::TransactionContext>, and the
209 L<DBIO::Storage::Async/pipeline> scaffold -- is inherited concretely from
210 L<DBIO::Storage::Async> (ADR 0030 §4). This class supplies only the
211 B<transport>: the L<Future::IO> query execution (L</_query_async> /
212 L</_query_async_pinned> over L</_await_query_result>), the L<Future>
213 implementation (L</future_class>) and the connection pool (L</pool>), plus the
214 DB-specific seam hooks a concrete DBD subclass fills in.
215  
216 =head1 METHODS
217  
218 =head2 future_class
219  
220 Returns C<'Future'> -- this backend uses L<Future> from CPAN.
221  
222 =head2 transport_capabilities
223  
224 my @caps = DBIO::Async::Storage->transport_capabilities; # ('on_connect_replay')
225  
226 Class method (see L<DBIO::Storage::Async/transport_capabilities>). Declares the
227 named transport capabilities this C<future_io> transport provides, so
228 L<DBIO::Storage::DBI/_async_storage> lets an async extension layer that declares
229 a matching C<required_transport_capabilities> compose onto it (and croaks on a
230 shortfall rather than silently dropping the feature).
231  
232 This transport declares C<on_connect_replay>: its pool
233 (L<DBIO::Async::Pool>, a L<DBIO::Storage::PoolBase>) drives core's
234 L<DBIO::Storage::Async/_setup_pool_connection> on every freshly-spawned
235 connection, and the C<< { dbh => $dbh } >> connection shape is handled by the
236 base C<_run_pool_connect_statement> -- so each pooled async connection replays
237 the owning sync storage's C<on_connect_do>/C<on_connect_call> (and the
238 C<on_disconnect_*> actions at shutdown) end to end (karr #68 seam). A concrete
239 DBD subclass inherits this obligation for free.
240  
241 =head2 pool
242  
243 Returns the L<DBIO::Async::Pool> connection pool, created lazily on first
244 access. Fed the per-spawn C<conninfo_provider> when an AccessBroker is attached
245 (see L<DBIO::Storage::Async/_conninfo_provider>), otherwise the static
246 conninfo.
247  
248 =head2 _async_broker_conninfo
249  
250 my $conninfo = $storage->_async_broker_conninfo($mode);
251  
252 AccessBroker seam (see L<DBIO::Storage::Async/ACCESSBROKER CONSUMPTION>):
253 return one fresh, storage-native conninfo for a single new pool connection,
254 built from the current broker credentials via the inherited normalisation.
255  
256 =head2 _query_async
257  
258 Transport override: execute a query on a freshly-acquired pooled connection,
259 releasing it once the Future is ready.
260  
261 Receives SQL in the C<sql_maker> C<?>-placeholder dialect (the core #70 seam
262 contract) and shapes it into the driver's own dialect B<internally>, up front,
263 through L</_transform_sql> before the query reaches the wire -- callers no longer
264 pre-shape. The C<?>-E<gt>C<$N>-style rewrite is idempotent on already-shaped SQL
265 (no bare C<?> left to touch), which is what keeps the transition window safe
266 while core still shapes at its own call site.
267  
268 =head2 _query_async_pinned
269  
270 Transport override: like L</_query_async> but runs on the supplied pinned
271 connection and does B<not> release it -- used for queries inside a pinned
272 transaction. Shapes the incoming C<?>-dialect SQL internally through
273 L</_transform_sql> exactly as L</_query_async> does.
274  
275 =head2 _await_readable
276  
277 my $future = $storage->_await_readable($conn);
278  
279 Returns a Future that becomes ready once the connection's socket is readable.
280 The single seam through which this transport touches L<Future::IO>: a concrete
281 driver's L</_collect_result> that must wait for more of a result also calls this
282 rather than re-implementing the fd wrapping. The filehandle C<poll> needs is
283 supplied by L</_conn_poll_fh>.
284  
285 =head2 _conn_poll_fh
286  
287 my $fh = $storage->_conn_poll_fh($conn);
288  
289 The stable poll filehandle for C<$conn>, created lazily on first use and cached
290 on the connection for its whole life. L<Future::IO-E<gt>poll> requires a real
291 filehandle, but a driver's L</_conn_fileno> hands back the raw integer socket fd
292 (e.g. DBD::Pg's C<pg_socket>). The C<< '+<&' >> open mode dups that fd (via
293 C<dup(2)>) into an independent filehandle, so closing this handle never touches
294 the driver's own socket.
295  
296 The dup is cached B<per connection> for a correctness reason, not merely for
297 speed. L<Future::IO> watches the filehandle and, once the poll resolves,
298 unwatches it B<by its fileno> -- but its impls mark the Future done I<before>
299 they unwatch. A fresh-dup-per-poll handle closed the moment its poll resolved
300 would already be gone when the loop unwatches: C<< $fh->fileno >> returns
301 C<undef>, the loop's watch table is corrupted, and the freed fd -- reused by the
302 next dup -- collides with the stale watch (observed as
303 C<pg_ready: No asynchronous query is running> on the following query). Reusing
304 one stable handle for every poll on a connection keeps the fileno valid across
305 the whole watch/unwatch cycle. The dup fd dies with the connection it mirrors,
306 when C<$conn> is torn down.
307  
308 Cached on the connection hashref under the reserved key C<_dbio_async_poll_fh>;
309 a driver whose connection is not a hashref must supply its own caching.
310  
311 =head2 _await_conn_ready
312  
313 my $future = $storage->_await_conn_ready($conn);
314  
315 Returns a Future that resolves to C<$conn> once the connection is ready for
316 queries. Uses L</_conn_ready> to check readiness and L</_await_readable> to
317 wait on the fd from L</_conn_fileno> when not yet ready.
318  
319 =head2 _await_query_result
320  
321 my $future = $storage->_await_query_result($conn, $sql, $bind);
322  
323 Submits the query via L</_submit_query>, waits for the connection socket to
324 become readable via L</_await_readable>, and collects the result via
325 L</_collect_result>. Returns a Future resolving to the result rows.
326  
327 =head1 SEAM HOOKS
328  
329 Overridden by a concrete DBD subclass; each croaks until provided.
330  
331 =head2 Query execution / connection lifecycle
332  
333 =over 4
334  
335 =item * L</_submit_query> -- send query bytes to the wire (non-blocking)
336  
337 =item * L</_collect_result> -- read the result from the wire
338  
339 =item * L</_conn_ready> -- is the connection ready for queries?
340  
341 =item * L</_conn_fileno> -- socket fd for the L<Future::IO> watcher seam
342  
343 =item * L</_create_pool_connection> / L</_shutdown_pool_connection> -- pool
344 connection lifecycle
345  
346 =back
347  
348 =head2 SQL / connect-info / transaction shaping
349  
350 The SQL-shaping seams (C<sql_maker_class>, C<_transform_sql>,
351 C<_post_insert_sql>) and the pipeline seams
352 (C<_pipeline_enter>/C<_pipeline_sync>/C<_pipeline_exit>) are inherited as
353 croaking seams from L<DBIO::Storage::Async>. This transport additionally
354 requires:
355  
356 =over 4
357  
358 =item * L</_normalize_conninfo> -- DB-specific connect-info conversion
359  
360 =item * L</_txn_context_class> / L</_txn_conn_accessor> -- the
361 transaction-context class and the pinned-connection accessor key
362  
363 =back
364  
365 =head1 AUTHOR
366  
367 DBIO Authors
368  
369 =head1 COPYRIGHT AND LICENSE
370  
371 Copyright (C) 2026 DBIO Authors
372  
373 This is free software; you can redistribute it and/or modify it under
374 the same terms as the Perl 5 programming language system itself.
375  
376 =cut
↑ Summary Async/Pool.pm »