| 1 | package DBIO::Forked::Storage; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Fork-based async storage skeleton — make any sync DBIO driver async | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use base 'DBIO::Storage::Async'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # ADR 0030: register the generic 'forked' async mode on the core base storage | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # class so every DBIO driver inherits it. A connection opened with | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # { async => 'forked' } then resolves its embedded async backend to this class. | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # DBIO::Storage::Async extends DBIO::Storage (not ::DBI), so load ::DBI | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # explicitly to reach register_async_mode, and register on it (not via __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # which does not inherit the method). | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use DBIO::Storage::DBI (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 5 | DBIO::Storage::DBI->register_async_mode( forked => __PACKAGE__ ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use Storable (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use DBIO::Forked::Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # --- Constructor --- | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | my ($class, $schema, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | schema => $schema, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | connect_info => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | debug => $ENV{DBIO_TRACE} || 0, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'DBIO_TRACE'} || 0
MC/DC0%0/1
$ENV{'DBIO_TRACE'} || 0
$ENV{'DBIO_TRACE'}
Truth table50%1/2
A
A= $ENV{'DBIO_TRACE'}
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | Scalar::Util::weaken($self->{schema}) if ref $self->{schema}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | sub future_class { 'DBIO::Forked::Future' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
future_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | # --- Connect Info --- | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub connect_info { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
connect_info: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | my ($self, $info) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | $self->{connect_info} = $info if defined $info; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | return $self->{connect_info}; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | # --- Schema / introspection --- | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub schema { $_[0]->{schema} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schema: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | sub debug { $_[0]->{debug} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Model A forks one short-lived child per query, so there is no persistent | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # connection pool to expose (unlike DBIO::Async). Override the inherited | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # abstract pool() with an explicit "not applicable" rather than the misleading | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # "subclass must override" croak. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub pool { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pool: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | croak 'DBIO::Forked uses fork-per-query (Model A); there is no connection pool'; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | # --- Async CRUD --- | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | sub select_async { my $self = shift; $self->_run_forked('select', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub select_single_async { my $self = shift; $self->_run_forked('select_single', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_single_async: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub insert_async { my $self = shift; $self->_run_forked('insert', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub update_async { my $self = shift; $self->_run_forked('update', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub delete_async { my $self = shift; $self->_run_forked('delete', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_async: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | # txn_do_async is just _run_forked('txn_do', $body, @args): the child runs the | ||||||||||||||||||||||||||||||||||||||||||
| 74 | # inherited sync storage's txn_do($body, @args), so BEGIN/body/COMMIT all happen | ||||||||||||||||||||||||||||||||||||||||||
| 75 | # in the child on its freshly-reconnected connection. See the method POD for the | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # return-value and sync-only limits. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | ▶ | sub txn_do_async { my $self = shift; $self->_run_forked('txn_do', @_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn_do_async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # --- Fork-per-query seam (Model A) --- | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # The single point where Model A is realized. One short-lived fork per query: | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # 1. pipe() a read/write pair, then fork(). | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # 2. Child: run the inherited primary sync storage's ORDINARY sync CRUD via | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # L</_forked_child_run> ($schema->storage->$op, shaped to the async row | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # contract -- see that method). The sync storage handles the fork trap | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # itself -- DBIO::Storage::DBI's _verify_pid sets InactiveDestroy on PID | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # change and _get_dbh reconnects fresh before the op -- so we never touch a | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # DBI handle, set no InactiveDestroy, and replay no connect_info. Crucially | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # we call the SYNC method (e.g. select), never select_async: sync does not | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # route to the async backend (core ADR 0030), so there is no re-fork. The | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # result rows (or the error) are Storable-frozen onto the write end; then | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # _exit (NOT exit -- no DESTROY/END in the child, which would tear down | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # resources shared with the parent). | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # 3. Parent: close the write end and return a DBIO::Forked::Future bound to | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # the read fd and the child pid (is_ready peeks to EOF, get blocks + thaws | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # + reaps). | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # EOF-framed: one frozen blob per child; the child closes its write end and the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # parent reads to EOF. No length prefix, no streaming. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 11 | ▶ | sub _run_forked { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_forked: called CC=6 SCAR=27.8
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 11 | my ($self, $op, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 11 | ▶ | pipe(my $rh, my $wh) or croak "DBIO::Forked: pipe failed: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 11 | my $pid = fork(); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 11 | ▶ | croak "DBIO::Forked: fork failed: $!" unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 11 | ▶ | if (!$pid) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | # --- Child --- | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | close $rh; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my @res = eval { $self->_forked_child_run($op, @args) }; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my $payload = $@ ? { error => "$@" } : { rows => \@res }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my $blob = eval { Storable::freeze($payload) }; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (!defined $blob) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | # The result itself would not freeze (e.g. a txn_do body returned a live | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # Row object or a closure). Send a helpful error instead of a corrupt blob. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $blob = Storable::freeze({ error => $self->_serialization_error($op, "$@") }); | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | print {$wh} $blob; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | close $wh; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | # --- Parent --- | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 11 | close $wh; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 11 | return DBIO::Forked::Future->new(read_fh => $rh, pid => $pid); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | # Run $op against the inherited primary sync storage in the child and shape its | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # return value to the async backend contract the core *_async consumers expect | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # (ADR 0031). The sync CRUD return shapes do not all match what the Future must | ||||||||||||||||||||||||||||||||||||||||||
| 133 | # resolve to, so two reads are adapted here, at the seam: | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # * select -- sync select() returns an *unexecuted cursor*; draining it | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # with ->all materializes the flat list of raw row arrayrefs | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # a real cursor yields, which is exactly what select_async | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # must resolve to (consumed by ResultSet::all_async / | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # first_async, which feed the rows back through the ordinary | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # collapse/inflate path). A storage whose select() already | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # returns materialized rows (e.g. a fake) is passed through | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # unchanged -- only a single blessed cursor is drained. | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # * select_single -- sync select_single() returns a flat list of column values; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # wrap it into a single raw-row arrayref (or empty), which is | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # what select_single_async must resolve to (consumed by | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # single_async / count_async, each reading one row). | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # * insert -- sync insert() already returns the returned-columns HASHREF | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # that Row::insert_async folds back into the object; pass it | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # through (ADR 0031 contract). | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # * update/delete/txn_do -- pass the sync return through unchanged. | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub _forked_child_run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_forked_child_run: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my ($self, $op, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $storage = $self->{schema}->storage; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | if ($op eq 'select') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my @r = $storage->select(@args); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return ( @r == 1 && Scalar::Util::blessed($r[0]) && $r[0]->can('all') ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@r == 1 and builtin::blessed($r[0]) and $r[0]->can("all")
@r == 1 and builtin::blessed($r[0])
MC/DC0%0/3
@r == 1 and builtin::blessed($r[0]) and $r[0]->can("all")
@r == 1
builtin::blessed($r[0])
$r[0]->can("all")
Truth table0%0/4
A and B and C
A= @r == 1B= builtin::blessed($r[0])C= $r[0]->can("all")
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | ? $r[0]->all # real cursor -> raw rows | ||||||||||||||||||||||||||||||||||||||||||
| 159 | : @r; # already-materialized rows (fake storage) | ||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | elsif ($op eq 'select_single') { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my @row = $storage->select_single(@args); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | return @row ? [ @row ] : (); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | return $storage->$op(@args); | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | # Build a helpful error for a result that will not Storable-freeze. For txn_do | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # the unserializable value is whatever the user's body returned, so the hint is | ||||||||||||||||||||||||||||||||||||||||||
| 171 | # return-value specific; for CRUD it is the driver's own rows. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | sub _serialization_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_serialization_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my ($self, $op, $err) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | return $op eq 'txn_do' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | ? 'DBIO::Forked: txn_do_async body must return Storable-serializable data ' | ||||||||||||||||||||||||||||||||||||||||||
| 176 | . '(scalars or plain array/hash refs), not live Row/ResultSet objects or ' | ||||||||||||||||||||||||||||||||||||||||||
| 177 | . "code refs: $err" | ||||||||||||||||||||||||||||||||||||||||||
| 178 | : "DBIO::Forked: cannot serialize $op result over the pipe " | ||||||||||||||||||||||||||||||||||||||||||
| 179 | . "(it must be plain serializable data): $err"; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | # --- Sync wrappers --- | ||||||||||||||||||||||||||||||||||||||||||
| 183 | # The sync entry points block on the forked result: run the query in a child | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # and wait for it via ->get. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub select { my $self = shift; $self->select_async(@_)->get } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | sub select_single { my $self = shift; $self->select_single_async(@_)->get } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select_single: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub insert { my $self = shift; $self->insert_async(@_)->get } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | sub update { my $self = shift; $self->update_async(@_)->get } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub delete { my $self = shift; $self->delete_async(@_)->get } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | ▶ | sub txn_do { my $self = shift; $self->txn_do_async(@_)->get } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn_do: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | DBIO::Forked::Storage - Fork-based async storage skeleton — make any sync DBIO driver async | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | version 0.900001 | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | Fork-based async storage backend subclassing core L<DBIO::Storage::Async>. It | ||||||||||||||||||||||||||||||||||||||||||
| 212 | makes B<any> sync DBIO driver async without an async-capable client and without | ||||||||||||||||||||||||||||||||||||||||||
| 213 | an event loop, via C<fork()>-per-query (Model A, see L<DBIO::Forked>): the | ||||||||||||||||||||||||||||||||||||||||||
| 214 | child reconnects the sync driver fresh from the stored DBI-form connect info, | ||||||||||||||||||||||||||||||||||||||||||
| 215 | runs the driver's B<ordinary> sync CRUD (no SQL is re-implemented here), | ||||||||||||||||||||||||||||||||||||||||||
| 216 | serializes the result rows back over a pipe with L<Storable>, and exits; the | ||||||||||||||||||||||||||||||||||||||||||
| 217 | parent returns a L<DBIO::Forked::Future> bound to the pipe read fd. | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | my $storage = DBIO::Forked::Storage->new($schema); | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | Construct the fork-based async backend for C<$schema>. The schema reference is | ||||||||||||||||||||||||||||||||||||||||||
| 226 | weakened (the schema owns the storage, not the other way round). Connect info | ||||||||||||||||||||||||||||||||||||||||||
| 227 | is supplied separately via L</connect_info>. | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =head2 future_class | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | Returns C<'DBIO::Forked::Future'> -- the loop-free, pipe-backed Future this | ||||||||||||||||||||||||||||||||||||||||||
| 232 | backend hands out. | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | =head2 connect_info | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | $storage->connect_info([ $dsn, $user, $pass, \%attrs, \%dbio_opts ]); | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | Store the DBI-form connect info verbatim (the core resolver passes the sync | ||||||||||||||||||||||||||||||||||||||||||
| 239 | storage's C<< _connect_info >> straight through). Returns the stored value. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | B<Informational in Model A>: the forked child runs the inherited sync storage's | ||||||||||||||||||||||||||||||||||||||||||
| 242 | own CRUD, and that storage reconnects itself in the child via its inherited | ||||||||||||||||||||||||||||||||||||||||||
| 243 | fork handling (see C<docs/adr/0002>). This stored connect info is therefore | ||||||||||||||||||||||||||||||||||||||||||
| 244 | B<not consumed> on the query path -- it is kept as latent diagnostics / raw | ||||||||||||||||||||||||||||||||||||||||||
| 245 | material for a possible future "fresh storage" variant. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | =head2 select_async | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | my $future = $storage->select_async($source, $select, $where, $attrs); | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | Run a SELECT in a forked child and return a L<DBIO::Forked::Future> of the | ||||||||||||||||||||||||||||||||||||||||||
| 252 | result rows. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =head2 select_single_async | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | Like L</select_async> but resolves to the first row only. | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | =head2 insert_async | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | my $future = $storage->insert_async($source, \%vals); | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | =head2 update_async | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | my $future = $storage->update_async($source, \%vals, \%where); | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | =head2 delete_async | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | my $future = $storage->delete_async($source, \%where); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | =head2 txn_do_async | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | my $future = $storage->txn_do_async(sub { ... }, @args); | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | Run a whole transaction in a single forked child. Mechanically identical to the | ||||||||||||||||||||||||||||||||||||||||||
| 275 | CRUD path: the child runs the inherited sync storage's ordinary | ||||||||||||||||||||||||||||||||||||||||||
| 276 | C<< txn_do($body, @args) >> (core wraps BEGIN / body / COMMIT|ROLLBACK + retry | ||||||||||||||||||||||||||||||||||||||||||
| 277 | in a C<BlockRunner>), so BEGIN, the body and COMMIT/ROLLBACK all execute on the | ||||||||||||||||||||||||||||||||||||||||||
| 278 | child's own freshly-reconnected sync connection. The C<$body> closure does not | ||||||||||||||||||||||||||||||||||||||||||
| 279 | cross the process boundary as data -- it is inherited through C<fork()> from | ||||||||||||||||||||||||||||||||||||||||||
| 280 | parent memory and runs in the child; it is called as C<< $body->(@args) >> and | ||||||||||||||||||||||||||||||||||||||||||
| 281 | receives no storage argument (it closes over C<$schema>/C<$storage>). | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | B<Two limits the caller must respect:> | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | B<The body's return value must be Storable-serializable> -- scalars or plain | ||||||||||||||||||||||||||||||||||||||||||
| 290 | array/hash refs of scalars. It is frozen back over the pipe, so a live | ||||||||||||||||||||||||||||||||||||||||||
| 291 | C<Row>/C<ResultSet> object (which drags its storage and live DB handle along) | ||||||||||||||||||||||||||||||||||||||||||
| 292 | cannot cross: such a return surfaces as a B<failed Future> with a clear message | ||||||||||||||||||||||||||||||||||||||||||
| 293 | from L</get> (see L</_serialization_error>). Return plain data (ids, hashrefs of | ||||||||||||||||||||||||||||||||||||||||||
| 294 | column values), not live result objects. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | B<The body must use sync operations only.> Calling a C<*_async> method inside | ||||||||||||||||||||||||||||||||||||||||||
| 299 | the body would fork again from within the child. Use the ordinary sync CRUD | ||||||||||||||||||||||||||||||||||||||||||
| 300 | (C<< $schema->resultset(...)->... >>) inside a C<txn_do_async> body. | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||
| 304 | =head1 ACTIVATION | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | Loading this module (or L<DBIO::Forked>) registers a generic C<forked> async | ||||||||||||||||||||||||||||||||||||||||||
| 307 | I<mode> on the core base storage class (ADR 0030): | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | DBIO::Storage::DBI->register_async_mode( forked => 'DBIO::Forked::Storage' ); | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | so every DBIO driver inherits it. A connection then opts in per-connection, at | ||||||||||||||||||||||||||||||||||||||||||
| 312 | C<connect> time: | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | my $schema = MyApp::Schema->connect($dsn, $user, $pass, { async => 'forked' }); | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | Nothing is auto-wired: a connection opened without C<< { async => 'forked' } >> | ||||||||||||||||||||||||||||||||||||||||||
| 317 | stays fully synchronous (its C<*_async> methods croak). When the mode is chosen, | ||||||||||||||||||||||||||||||||||||||||||
| 318 | the core resolver in L<DBIO::Storage::DBI> constructs | ||||||||||||||||||||||||||||||||||||||||||
| 319 | C<< __PACKAGE__->new($schema) >> as the embedded async backend and feeds it the | ||||||||||||||||||||||||||||||||||||||||||
| 320 | sync storage's DBI-form connect info via L</connect_info>; that backend then | ||||||||||||||||||||||||||||||||||||||||||
| 321 | answers the six C<*_async> methods. | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =head1 STATUS | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | All six C<*_async> entry points are implemented (with their sync C<< ->get >> | ||||||||||||||||||||||||||||||||||||||||||
| 326 | wrappers): each forks per query and roundtrips through L</_run_forked> and | ||||||||||||||||||||||||||||||||||||||||||
| 327 | L<DBIO::Forked::Future>. L</txn_do_async> runs the whole transaction in one | ||||||||||||||||||||||||||||||||||||||||||
| 328 | child; see the two caller-facing limits documented on it. | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | DBIO Authors | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 339 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | =cut | ||||||||||||||||||||||||||||||||||||||||||