| 1 | package DBIO::PostgreSQL::Storage::Async; | ||
| 2 | # ABSTRACT: future_io async PostgreSQL transport over DBD::Pg's pg_async binding | ||
| 3 | |||
| 4 | 31 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 31 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | our $VERSION = '0.900007'; | ||
| 8 | |||
| 9 | 31 | ▶ | use base 'DBIO::Async::Storage'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 31 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 31 | ▶ | use Scalar::Util (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 31 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use DBD::Pg qw(:async); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Future::IO; | ||
| 16 | use DBIO::Util (); | ||
| 17 | use DBIO::Exception (); | ||
| 18 | use DBIO::PostgreSQL::SQLMaker; | ||
| 19 | use DBIO::PostgreSQL::DDL (); | ||
| 20 | use DBIO::PostgreSQL::Util (); | ||
| 21 | use namespace::clean; | ||
| 22 | |||
| 23 | # The private attribute of a database handle that holds the values bound to the | ||
| 24 | # statement in flight (_submit_query ... _collect_result), for _pg_raise. | ||
| 25 | use constant BINDS_SLOT => 'private_dbio_binds'; | ||
| 26 | |||
| 27 | |||
| 28 | # --- Transport capabilities ------------------------------------------------- | ||
| 29 | |||
| 30 | |||
| 31 | sub transport_capabilities { | ||
| 32 | my $self = shift; | ||
| 33 | return ($self->next::method(@_), qw(listen notify broker_connect_options)); | ||
| 34 | } | ||
| 35 | |||
| 36 | # --- SQL shaping seams ------------------------------------------------------ | ||
| 37 | |||
| 38 | |||
| 39 | sub sql_maker_class { 'DBIO::PostgreSQL::SQLMaker' } | ||
| 40 | |||
| 41 | |||
| 42 | sub _sql_maker_args { | ||
| 43 | my ($self) = @_; | ||
| 44 | my $owner = ref $self ? $self->_owner_storage : undef; | ||
| 45 | |||
| 46 | my @quoting = (quote_char => '"', name_sep => '.'); | ||
| 47 | if ($owner) { | ||
| 48 | my $sync = $owner->sql_maker; | ||
| 49 | @quoting = ( | ||
| 50 | (defined $sync->{quote_char} ? (quote_char => $sync->{quote_char}) : ()), | ||
| 51 | name_sep => $sync->{name_sep}, | ||
| 52 | ); | ||
| 53 | } | ||
| 54 | |||
| 55 | return ( | ||
| 56 | @quoting, | ||
| 57 | limit_dialect => 'LimitOffset', | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | # The shared DBIO::PostgreSQL::SQLMaker also feeds the sync DBI driver, which | ||
| 62 | # needs '?', so the maker keeps emitting '?'. The numbering therefore happens | ||
| 63 | # here, in the transport, once, on what reaches the query seams (core k70 / | ||
| 64 | # ADR 0032). DBD::Pg would take '?' as well as '$N'; the statement is numbered | ||
| 65 | # all the same, so that _submit_query can set pg_placeholder_dollaronly and | ||
| 66 | # DBD::Pg leaves every '?' that is no placeholder -- the jsonpath operator, | ||
| 67 | # which the maker writes '@\?' and the numbering hands on as '@?', first of | ||
| 68 | # all -- as it is. | ||
| 69 | # | ||
| 70 | # Which '?' is a placeholder is not decided here: that is one rule about | ||
| 71 | # PostgreSQL's scanner, kept in DBIO::PostgreSQL::Util::number_placeholders and | ||
| 72 | # shared with the native ev transport. | ||
| 73 | |||
| 74 | |||
| 75 | sub _transform_sql { | ||
| 76 | my ($self, $sql) = @_; | ||
| 77 | return DBIO::PostgreSQL::Storage::Async::_Refusal::of_util( | ||
| 78 | sub { DBIO::PostgreSQL::Util::number_placeholders($sql) }, | ||
| 79 | ); | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub _post_insert_sql { ' RETURNING *' } | ||
| 84 | |||
| 85 | # --- Connect-info seam ------------------------------------------------------ | ||
| 86 | |||
| 87 | |||
| 88 | sub _normalize_conninfo { | ||
| 89 | my ($self, $info) = @_; | ||
| 90 | |||
| 91 | # DBI-form: [ 'dbi:Pg:...', $user, $pass, \%attrs, \%extra ] -> our conninfo | ||
| 92 | # shape. Both attribute hashes, the later winning, as | ||
| 93 | # DBIO::Storage::DBI::_normalize_connect_info merges them (karr k91). | ||
| 94 | if (ref $info eq 'ARRAY' | ||
| 95 | && defined $info->[0] && !ref $info->[0] && $info->[0] =~ /^dbi:/i) { | ||
| 96 | my ($dsn, $user, $pass) = @$info; | ||
| 97 | my %conninfo = ( | ||
| 98 | dsn => $dsn, | ||
| 99 | user => $user, | ||
| 100 | password => $pass, | ||
| 101 | attrs => { map { %$_ } grep { ref $_ eq 'HASH' } @{$info}[3, 4] }, | ||
| 102 | ); | ||
| 103 | $conninfo{pool_size} = delete $conninfo{attrs}{pool_size} | ||
| 104 | if defined $conninfo{attrs}{pool_size}; | ||
| 105 | return [ \%conninfo, {} ]; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Broker arrayref [ $broker ], or an already-normalised [ \%conninfo, \%opts ] | ||
| 109 | # pair -- leave for the inherited broker detection / normalisation. | ||
| 110 | return $info; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub _async_broker_conninfo { | ||
| 115 | my ($self, $mode) = @_; | ||
| 116 | |||
| 117 | # Without a broker _current_async_connect_info answers what is stored, and | ||
| 118 | # that is no broker's answer (karr k97; as dbio-async refuses, its k43). | ||
| 119 | _pg_refuse(sprintf '%s has no AccessBroker attached to ask for connect info', | ||
| 120 | ref($self) || $self) | ||
| 121 | unless $self->access_broker; | ||
| 122 | |||
| 123 | my ($conninfo) = $self->_normalize_async_connect_info( | ||
| 124 | $self->_pg_broker_connect_info($self->_current_async_connect_info($mode)) | ||
| 125 | ); | ||
| 126 | $conninfo->{attrs} = $self->_merge_access_broker_settings( | ||
| 127 | $conninfo->{attrs}, $self->_access_broker_call_attributes, | ||
| 128 | ); | ||
| 129 | return $conninfo; | ||
| 130 | } | ||
| 131 | |||
| 132 | # What the broker answered, in any shape _async_broker_conninfo lists, as the | ||
| 133 | # [ \%conninfo, \%opts ] pair core's _normalize_async_connect_info splits. | ||
| 134 | sub _pg_broker_connect_info { | ||
| 135 | my ($self, $info) = @_; | ||
| 136 | |||
| 137 | $info = [ $info, {} ] if ref $info eq 'HASH'; | ||
| 138 | |||
| 139 | if (ref $info eq 'ARRAY') { | ||
| 140 | my $first = $info->[0]; | ||
| 141 | if (defined $first && !ref $first && $first =~ /^dbi:/i) { | ||
| 142 | # Core's strip also records the storage options it pulls out; that | ||
| 143 | # record belongs to connect_info, not to one broker answer. | ||
| 144 | local $self->{_storage_options}; | ||
| 145 | return $self->_normalize_conninfo($self->_strip_private_connect_attrs($info)); | ||
| 146 | } | ||
| 147 | return [ $self->_pg_broker_conninfo_hash($first), $info->[1] ] | ||
| 148 | if ref $first eq 'HASH'; | ||
| 149 | } | ||
| 150 | |||
| 151 | _pg_refuse(sprintf( | ||
| 152 | 'PostgreSQL future_io cannot connect from what the AccessBroker returned: %s. ' | ||
| 153 | . "It takes a DBI-form arrayref [ 'dbi:Pg:...', \$user, \$password, \\%%attrs ], " | ||
| 154 | . 'a hashref of libpq connection parameters (dbname, host, port, user, password, ...), ' | ||
| 155 | . 'a hashref with a dsn (dsn, user, password, attrs), ' | ||
| 156 | . 'or either hashref paired with an options hashref', | ||
| 157 | $self->_pg_describe_shape($info)), | ||
| 158 | _pg_answer_connect_info($info)); | ||
| 159 | } | ||
| 160 | |||
| 161 | # One conninfo hash from a broker: taken as it is when it has a dsn, otherwise | ||
| 162 | # its libpq connection parameters become the dsn. | ||
| 163 | sub _pg_broker_conninfo_hash { | ||
| 164 | my ($self, $hash) = @_; | ||
| 165 | my %conninfo = %$hash; | ||
| 166 | return \%conninfo if defined $conninfo{dsn}; | ||
| 167 | |||
| 168 | my %transport_key = map { $_ => 1 } qw( user password attrs pool_size ); | ||
| 169 | my $known = _pg_answer_connect_info($hash); | ||
| 170 | # An empty dbname names no database: refused, as the sync storage refuses | ||
| 171 | # it (core karr k340), not dropped into a DSN without one. | ||
| 172 | _pg_refuse('PostgreSQL future_io cannot connect from what the AccessBroker ' | ||
| 173 | . 'returned: its dbname is an empty dbname, which names no database. Give ' | ||
| 174 | . 'the database name, or leave dbname out to connect with host and port alone', | ||
| 175 | $known) | ||
| 176 | if defined $conninfo{dbname} && !ref $conninfo{dbname} && !length $conninfo{dbname}; | ||
| 177 | |||
| 178 | my %param; | ||
| 179 | for my $key (grep { !$transport_key{$_} } keys %conninfo) { | ||
| 180 | my $value = delete $conninfo{$key}; | ||
| 181 | $param{$key} = $value if defined $value && length $value; | ||
| 182 | } | ||
| 183 | |||
| 184 | # A dbname that is a whole DSN is the DSN, as core and dbio-async read it | ||
| 185 | # (core ADR 0057) -- not a database of that name. Nothing set beside it can | ||
| 186 | # be put into it: where it would go is the DSN's business. Core says so for | ||
| 187 | # host and port; here every other parameter would become a part of the DSN | ||
| 188 | # as well, so each one is refused in the same words rather than dropped. | ||
| 189 | if (defined $param{dbname} && !ref $param{dbname} && $param{dbname} =~ /^dbi:/i) { | ||
| 190 | $conninfo{dsn} = delete $param{dbname}; | ||
| 191 | _pg_refuse('Connection parameter(s) ' . join(', ', map { "'$_'" } sort keys %param) | ||
| 192 | . ' given beside a dbname that is a whole DSN: they cannot be put into ' | ||
| 193 | . 'it. Give them inside the DSN, or give dbname as a database name.', | ||
| 194 | $known) | ||
| 195 | if %param; | ||
| 196 | return \%conninfo; | ||
| 197 | } | ||
| 198 | |||
| 199 | # "dbi:Pg:" alone leaves the target to libpq's environment (PGHOST, | ||
| 200 | # PGDATABASE, a service file); the sync storage refuses it too. | ||
| 201 | _pg_refuse('PostgreSQL future_io cannot connect from what the AccessBroker ' | ||
| 202 | . 'returned: it names no dbname, host or port, so libpq would take the ' | ||
| 203 | . 'target from its environment (PGHOST, PGDATABASE, ...). Give at least ' | ||
| 204 | . 'one of dbname, host or port (or service, hostaddr)', | ||
| 205 | $known) | ||
| 206 | unless grep { exists $param{$_} } qw(dbname host port hostaddr service); | ||
| 207 | |||
| 208 | my @params; | ||
| 209 | for my $key (sort keys %param) { | ||
| 210 | # A key is a libpq parameter name: it goes into the DSN as it is, so only | ||
| 211 | # the plain form is a key. | ||
| 212 | _pg_refuse("PostgreSQL future_io cannot connect from what the AccessBroker returned: " | ||
| 213 | . "its connection parameter '$key' is not a libpq parameter name " | ||
| 214 | . "(lower case letters, digits and underscores, beginning with a letter " | ||
| 215 | . "or an underscore)", $known) | ||
| 216 | unless $key =~ /\A[a-z_][a-z0-9_]*\z/; | ||
| 217 | _pg_refuse("PostgreSQL future_io cannot connect from what the AccessBroker returned: " | ||
| 218 | . "its libpq connection parameter '$key' is a reference, not a value", $known) | ||
| 219 | if ref $param{$key}; | ||
| 220 | # The DSN is a C string from DBD::Pg on: it would end at the NUL, and | ||
| 221 | # every parameter sorted behind this one would be gone with it. | ||
| 222 | _pg_refuse("PostgreSQL future_io cannot connect from what the AccessBroker returned: " | ||
| 223 | . "its libpq connection parameter '$key' contains a NUL character, which " | ||
| 224 | . 'no connection string can carry', $known) | ||
| 225 | if $param{$key} =~ /\0/; | ||
| 226 | push @params, "$key=" . $self->_pg_dsn_value($param{$key}); | ||
| 227 | } | ||
| 228 | $conninfo{dsn} = 'dbi:Pg:' . join ';', @params; | ||
| 229 | return \%conninfo; | ||
| 230 | } | ||
| 231 | |||
| 232 | # A libpq parameter value as it goes into a DBD::Pg DSN, so that libpq reads | ||
| 233 | # back the value it was given (karr k80, k96). The rule -- what libpq and | ||
| 234 | # DBD::Pg each do to the text, and what is written against it -- is kept in | ||
| 235 | # DBIO::PostgreSQL::Util::quote_dsn_value, which the sync storage writes | ||
| 236 | # through as well (DBIO::PostgreSQL::Storage::_connect_params_dsn, karr k102): | ||
| 237 | # the two connections of one schema get the same DSN for the same parameters. | ||
| 238 | sub _pg_dsn_value { | ||
| 239 | my ($self, $value) = @_; | ||
| 240 | return DBIO::PostgreSQL::Util::quote_dsn_value($value); | ||
| 241 | } | ||
| 242 | |||
| 243 | # The shape of a broker's answer, for an error message: types and hash keys, | ||
| 244 | # never values -- they carry the password. | ||
| 245 | sub _pg_describe_shape { | ||
| 246 | my ($self, $value) = @_; | ||
| 247 | return 'nothing (undef)' unless defined $value; | ||
| 248 | return 'a plain string' unless ref $value; | ||
| 249 | return sprintf 'a hashref with keys (%s)', join ', ', sort keys %$value | ||
| 250 | if ref $value eq 'HASH'; | ||
| 251 | return sprintf 'an arrayref of (%s)', join ', ', | ||
| 252 | map { !defined $_ ? 'undef' : ref $_ ? ref($_) . ' ref' : 'string' } @$value | ||
| 253 | if ref $value eq 'ARRAY'; | ||
| 254 | return 'a ' . ref($value) . ' ref'; | ||
| 255 | } | ||
| 256 | |||
| 257 | # --- Pool connection lifecycle ---------------------------------------------- | ||
| 258 | |||
| 259 | |||
| 260 | sub _create_pool_connection { | ||
| 261 | my ($self, $conninfo) = @_; | ||
| 262 | return $self->_pg_connect($conninfo); | ||
| 263 | } | ||
| 264 | |||
| 265 | # The one DBI->connect of this transport: a pool connection, and the dedicated | ||
| 266 | # listen connection (_listen_open). The forced attributes win over the | ||
| 267 | # caller's; AutoInactiveDestroy among them, so no connection of this transport | ||
| 268 | # can be ended by a forked child's copy of its handle (karr k79). HandleError | ||
| 269 | # is forced too, around the caller's (_pg_handle_error): this is where every | ||
| 270 | # error DBI reports for the handle, and for the connect itself, is raised. | ||
| 271 | sub _pg_connect { | ||
| 272 | my ($self, $conninfo) = @_; | ||
| 273 | |||
| 274 | _pg_refuse('PostgreSQL async conninfo must be a hashref with a dsn', | ||
| 275 | _pg_answer_connect_info($conninfo)) | ||
| 276 | unless ref $conninfo eq 'HASH' && defined $conninfo->{dsn}; | ||
| 277 | |||
| 278 | # A whole DSN that names no target leaves it to libpq's environment, as the | ||
| 279 | # connection parameters a broker answers do when they name none (karr k121). | ||
| 280 | _pg_refuse('PostgreSQL future_io cannot connect with a DSN that names no ' | ||
| 281 | . 'dbname, host or port, so libpq would take the target from its ' | ||
| 282 | . 'environment (PGHOST, PGDATABASE, ...). Give at least one of dbname, ' | ||
| 283 | . 'host or port (or service, hostaddr) in the DSN', | ||
| 284 | _pg_answer_connect_info($conninfo)) | ||
| 285 | if !ref $conninfo->{dsn} && $conninfo->{dsn} =~ /\Adbi:Pg\b/i | ||
| 286 | && !DBIO::PostgreSQL::Util::dsn_names_target($conninfo->{dsn}); | ||
| 287 | |||
| 288 | my %attrs = %{ $conninfo->{attrs} || {} }; | ||
| 289 | my @info = ( | ||
| 290 | $conninfo->{dsn}, | ||
| 291 | $conninfo->{user}, | ||
| 292 | $conninfo->{password}, | ||
| 293 | { | ||
| 294 | %attrs, | ||
| 295 | AutoCommit => 1, | ||
| 296 | RaiseError => 1, | ||
| 297 | PrintError => 0, | ||
| 298 | AutoInactiveDestroy => 1, | ||
| 299 | HandleError => _pg_handle_error($attrs{HandleError}), | ||
| 300 | }, | ||
| 301 | ); | ||
| 302 | |||
| 303 | # Never the password in what a failed connect says (karr k111, core k297). | ||
| 304 | # DBI's message quotes the DSN it was given; libpq's reason quotes what it | ||
| 305 | # could not read -- a URI whole, and the user and password DBD::Pg puts | ||
| 306 | # behind it. Both ways a connect fails are raised in here -- DBI's message | ||
| 307 | # by the HandleError above, and the connect that answered false because the | ||
| 308 | # caller's HandleError took the error -- so one mask covers them, and the | ||
| 309 | # stack trace Carp's verbose mode adds to either. | ||
| 310 | my ($dbh, $ok, $err); | ||
| 311 | { | ||
| 312 | local $@; | ||
| 313 | $ok = eval { | ||
| 314 | $dbh = DBI->connect(@info) | ||
| 315 | or _pg_raise("DBD::Pg connect failed: $DBI::errstr"); | ||
| 316 | 1; | ||
| 317 | }; | ||
| 318 | $err = $@; | ||
| 319 | } | ||
| 320 | die DBIO::Util::mask_connect_secrets($err, \@info) unless $ok; | ||
| 321 | |||
| 322 | return { dbh => $dbh }; | ||
| 323 | } | ||
| 324 | |||
| 325 | # The HandleError of every handle this transport opens: _pg_dbi_error. When | ||
| 326 | # the connect attributes carry one of the caller's, that one is asked first | ||
| 327 | # and keeps what DBI gives a HandleError -- answering true it has taken the | ||
| 328 | # error, dying it has replaced it. Anything that is no code is left for DBI to | ||
| 329 | # refuse. | ||
| 330 | sub _pg_handle_error { | ||
| 331 | my ($theirs) = @_; | ||
| 332 | return \&_pg_dbi_error unless defined $theirs; | ||
| 333 | return $theirs unless (Scalar::Util::reftype($theirs) // '') eq 'CODE'; | ||
| 334 | return sub { $theirs->(@_) ? 1 : _pg_dbi_error(@_) }; | ||
| 335 | } | ||
| 336 | |||
| 337 | # DBI calls its HandleError with the message it would have died with -- the | ||
| 338 | # driver and method, "failed:" and the driver's text -- the handle, and what | ||
| 339 | # the method returned. | ||
| 340 | sub _pg_dbi_error { | ||
| 341 | my ($message, $h) = @_; | ||
| 342 | my $dbh = eval { ref $h && $h->{Type} && $h->{Type} eq 'st' ? $h->{Database} : $h }; | ||
| 343 | return _pg_raise($message, $dbh); | ||
| 344 | } | ||
| 345 | |||
| 346 | |||
| 347 | sub _pg_raise { | ||
| 348 | my ($message, $dbh) = @_; | ||
| 349 | $message =~ s/\n\z//; | ||
| 350 | $message = _pg_mask_bound($message, $dbh); | ||
| 351 | $message = DBIO::PostgreSQL::Util::mask_error_details( | ||
| 352 | $message, DBIO::PostgreSQL::Util::error_sqlstate($dbh)); | ||
| 353 | die $Carp::Verbose ? DBIO::Util::masked_longmess($message) : "$message\n"; | ||
| 354 | } | ||
| 355 | |||
| 356 | # The values of the statement in flight stand at its handle (BINDS_SLOT), so | ||
| 357 | # that the HandleError, which knows the handle and nothing else, can take them | ||
| 358 | # out of the text. | ||
| 359 | sub _pg_mask_bound { | ||
| 360 | my ($message, $dbh) = @_; | ||
| 361 | my $binds = eval { ref $dbh ? $dbh->{+BINDS_SLOT} : undef }; | ||
| 362 | return $message unless ref $binds eq 'ARRAY' && @$binds; | ||
| 363 | my $masked = eval { | ||
| 364 | DBIO::Util::mask_bound_values($message, $binds, sub { $dbh->quote($_[0]) }); | ||
| 365 | }; | ||
| 366 | return defined $masked ? $masked : $message; | ||
| 367 | } | ||
| 368 | |||
| 369 | # The statement is over: the values leave the handle, and the statement handle | ||
| 370 | # that holds them as its parameters is let go. A statement without values | ||
| 371 | # leaves its handle where it is (_txn_commit_rolled_back reads a COMMIT's). | ||
| 372 | sub _pg_forget_binds { | ||
| 373 | my ($conn) = @_; | ||
| 374 | return unless ref $conn eq 'HASH'; | ||
| 375 | my $dbh = $conn->{dbh}; | ||
| 376 | my $held = eval { $dbh ? $dbh->{+BINDS_SLOT} : undef }; | ||
| 377 | return unless $held; | ||
| 378 | eval { $dbh->{+BINDS_SLOT} = undef }; | ||
| 379 | delete $conn->{sth}; | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | sub _pg_refuse { goto &DBIO::PostgreSQL::Storage::Async::_Refusal::raise } | ||
| 385 | |||
| 386 | # The connect info a refusal is about, in the DBI form the trace masks | ||
| 387 | # (DBIO::Util::masked_longmess): from a conninfo hash, an answer in the DBI | ||
| 388 | # form, or the hash of a [ \%conninfo, \%opts ] pair. Nothing for anything | ||
| 389 | # else. | ||
| 390 | sub _pg_answer_connect_info { | ||
| 391 | my ($info) = @_; | ||
| 392 | $info = $info->[0] if ref $info eq 'ARRAY' && ref $info->[0] eq 'HASH'; | ||
| 393 | return [ @{$info}{qw( dsn user password attrs )} ] if ref $info eq 'HASH'; | ||
| 394 | return [ @{$info}[0 .. 3] ] if ref $info eq 'ARRAY'; | ||
| 395 | return undef; | ||
| 396 | } | ||
| 397 | |||
| 398 | # Where a refusal is thrown from. DBIO::Exception->throw names the first frame | ||
| 399 | # that is not this package's, nor of a package the patterns of | ||
| 400 | # _skip_namespace_frames name (DBIO::Carp): DBIO's own, and here Future's and | ||
| 401 | # IO::Async's as well -- a refusal reached in a callback of the loop would | ||
| 402 | # otherwise name Future/PP.pm. %Carp::Internal keeps these frames out of the | ||
| 403 | # origin the exception names (the transport's method that refused) and out of | ||
| 404 | # the start of a stack trace. | ||
| 405 | { | ||
| 406 | package # not a class of its own: the place a refusal is thrown from | ||
| 407 | DBIO::PostgreSQL::Storage::Async::_Refusal; | ||
| 408 | |||
| 409 | $Carp::Internal{ (__PACKAGE__) }++; | ||
| 410 | |||
| 411 | sub _skip_namespace_frames { '^Future\b|^IO::Async\b' } | ||
| 412 | |||
| 413 | sub raise { | ||
| 414 | my ($message, $connect_info) = @_; | ||
| 415 | DBIO::Exception->throw($message, $Carp::Verbose, $connect_info); | ||
| 416 | } | ||
| 417 | |||
| 418 | # A refusal of a DBIO::PostgreSQL::Util rule, raised again as this | ||
| 419 | # transport's. The rule croaks, naming its caller -- the code ref, in this | ||
| 420 | # file; Carp's short form is asked for, so that is what comes back, and the | ||
| 421 | # refusal is raised again in the rule's words without that place. | ||
| 422 | sub of_util { | ||
| 423 | my ($code) = @_; | ||
| 424 | my ($answer, $ok, $err); | ||
| 425 | { | ||
| 426 | local $@; | ||
| 427 | $ok = eval { local $Carp::Verbose = 0; $answer = $code->(); 1 }; | ||
| 428 | $err = $@; | ||
| 429 | } | ||
| 430 | return $answer if $ok; | ||
| 431 | die $err unless !ref $err && $err =~ s/ at \Q${\ __FILE__}\E line \d+[^\n]*\n\z//; | ||
| 432 | raise($err); | ||
| 433 | } | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | sub _reset_pool_connection { | ||
| 438 | my ($self, $conn) = @_; | ||
| 439 | return $self->_query_async_pinned($conn, 'DISCARD ALL', [])->then(sub { | ||
| 440 | $self->_setup_pool_connection($conn); | ||
| 441 | return $self->future_class->done; | ||
| 442 | }); | ||
| 443 | } | ||
| 444 | |||
| 445 | |||
| 446 | sub _shutdown_pool_connection { | ||
| 447 | my ($self, $conn) = @_; | ||
| 448 | my $dbh = $conn->{dbh} or return; | ||
| 449 | $dbh->disconnect if $dbh->{Active}; | ||
| 450 | } | ||
| 451 | |||
| 452 | # --- Readiness / socket seams ----------------------------------------------- | ||
| 453 | |||
| 454 | |||
| 455 | sub _conn_ready { | ||
| 456 | my ($self, $conn) = @_; | ||
| 457 | return $conn->{dbh} && $conn->{dbh}{Active} ? 1 : 0; | ||
| 458 | } | ||
| 459 | |||
| 460 | |||
| 461 | sub _conn_fileno { | ||
| 462 | my ($self, $conn) = @_; | ||
| 463 | return $conn->{dbh}{pg_socket}; | ||
| 464 | } | ||
| 465 | |||
| 466 | # --- Query submit / collect ------------------------------------------------- | ||
| 467 | |||
| 468 | |||
| 469 | sub _submit_query { | ||
| 470 | my ($self, $conn, $sql, $bind) = @_; | ||
| 471 | $bind //= []; | ||
| 472 | |||
| 473 | my $dbh = $conn->{dbh}; | ||
| 474 | |||
| 475 | # Release the previous (already-collected) async statement handle BEFORE | ||
| 476 | # starting a new async query. DBD::Pg tracks async state on the DATABASE | ||
| 477 | # handle; destroying a spent async sth *after* the new execute clobbers the | ||
| 478 | # new query's async status back to 0, so the next _collect_result sees | ||
| 479 | # "pg_ready: No asynchronous query is running" on the second query over a | ||
| 480 | # reused pooled connection. Clearing it here means the new execute owns the | ||
| 481 | # dbh async slot uncontested. | ||
| 482 | delete $conn->{sth}; | ||
| 483 | $dbh->{+BINDS_SLOT} = @$bind ? [ @$bind ] : undef; | ||
| 484 | |||
| 485 | my $ok = eval { | ||
| 486 | my $sth = $dbh->prepare($sql, { | ||
| 487 | pg_async => PG_ASYNC, | ||
| 488 | pg_placeholder_dollaronly => 1, | ||
| 489 | }); | ||
| 490 | $sth->execute(@$bind); | ||
| 491 | $conn->{sth} = $sth; | ||
| 492 | 1; | ||
| 493 | }; | ||
| 494 | my $error = $@; | ||
| 495 | unless ($ok) { | ||
| 496 | _pg_forget_binds($conn); | ||
| 497 | die $error; | ||
| 498 | } | ||
| 499 | |||
| 500 | return; | ||
| 501 | } | ||
| 502 | |||
| 503 | |||
| 504 | sub _collect_result { | ||
| 505 | my ($self, $conn) = @_; | ||
| 506 | |||
| 507 | my $future; | ||
| 508 | my $ok = eval { $future = $self->_pg_collect($conn); 1 }; | ||
| 509 | my $error = $@; | ||
| 510 | |||
| 511 | # Waiting for more of the result (the re-poll recurses here) keeps the values | ||
| 512 | # for the HandleError; every other end of the statement takes them off. | ||
| 513 | _pg_forget_binds($conn) unless $ok && !$future->is_ready; | ||
| 514 | die $error unless $ok; | ||
| 515 | return $future; | ||
| 516 | } | ||
| 517 | |||
| 518 | sub _pg_collect { | ||
| 519 | my ($self, $conn) = @_; | ||
| 520 | |||
| 521 | my $dbh = $conn->{dbh}; | ||
| 522 | |||
| 523 | # The result may not have fully arrived on the first readable event. pg_ready | ||
| 524 | # consumes what is on the wire and reports whether the result is complete; | ||
| 525 | # if not, wait for more and re-check rather than blocking in pg_result. | ||
| 526 | unless ($dbh->pg_ready) { | ||
| 527 | return $self->_await_readable($conn)->then(sub { | ||
| 528 | return $self->_collect_result($conn); | ||
| 529 | }); | ||
| 530 | } | ||
| 531 | |||
| 532 | # Finalise; dies (RaiseError) on a query error and on a lost connection. | ||
| 533 | my $sth = $conn->{sth}; | ||
| 534 | my $rv = $dbh->pg_result; | ||
| 535 | |||
| 536 | # A cancelled statement is the one failure pg_result does not die on: it | ||
| 537 | # returns 0E0 and leaves SQLSTATE 57014 as the only trace (see POD). | ||
| 538 | _pg_raise("DBD::Pg::db pg_result failed: PostgreSQL cancelled the statement " | ||
| 539 | . "(SQLSTATE 57014 query_canceled: statement_timeout or a cancel request; " | ||
| 540 | . "DBD::Pg does not pass on the server's message)", $dbh) | ||
| 541 | if ($dbh->state // '') eq '57014'; | ||
| 542 | |||
| 543 | if (($sth->{NUM_OF_FIELDS} || 0) > 0) { | ||
| 544 | my $rows = $sth->fetchall_arrayref; | ||
| 545 | return $self->future_class->done(@$rows); | ||
| 546 | } | ||
| 547 | |||
| 548 | return $self->future_class->done($rv); | ||
| 549 | } | ||
| 550 | |||
| 551 | |||
| 552 | sub _txn_commit_rolled_back { | ||
| 553 | my ($self, $conn) = @_; | ||
| 554 | my $sth = ref $conn eq 'HASH' ? $conn->{sth} : undef; | ||
| 555 | return 0 unless $sth; | ||
| 556 | return (($sth->{pg_cmd_status} // '') eq 'ROLLBACK') ? 1 : 0; | ||
| 557 | } | ||
| 558 | |||
| 559 | |||
| 560 | sub _conn_error_is_fatal { | ||
| 561 | my ($self, $conn) = @_; | ||
| 562 | return $self->_pg_socket_gone($conn); | ||
| 563 | } | ||
| 564 | |||
| 565 | # The one verdict on whether a connection is dead, shared by | ||
| 566 | # _conn_error_is_fatal and _idle_connection_alive: libpq has no server | ||
| 567 | # connection open (pg_socket -1, or unknown), or there is no handle at all. | ||
| 568 | sub _pg_socket_gone { | ||
| 569 | my ($self, $conn) = @_; | ||
| 570 | my $dbh = ref $conn eq 'HASH' ? $conn->{dbh} : undef; | ||
| 571 | return $dbh && ($dbh->{pg_socket} // -1) >= 0 ? 0 : 1; | ||
| 572 | } | ||
| 573 | |||
| 574 | |||
| 575 | sub _idle_connection_alive { | ||
| 576 | my ($self, $conn) = @_; | ||
| 577 | return 0 if $self->_pg_socket_gone($conn); | ||
| 578 | |||
| 579 | my $dbh = $conn->{dbh}; | ||
| 580 | for (1 .. 4) { | ||
| 581 | return 1 unless $self->_pg_readable_now($conn); | ||
| 582 | |||
| 583 | my ($ok, $err); | ||
| 584 | { | ||
| 585 | local $@; | ||
| 586 | $ok = eval { $self->_pg_nonblocking($conn, sub { $dbh->pg_notifies }); 1 }; | ||
| 587 | $err = $@; | ||
| 588 | } | ||
| 589 | return 0 if $self->_pg_socket_gone($conn); | ||
| 590 | die $err unless $ok; | ||
| 591 | } | ||
| 592 | return 1; | ||
| 593 | } | ||
| 594 | |||
| 595 | # Is the connection's socket readable right now? A select with a zero timeout, | ||
| 596 | # on the cached dup of the socket (_conn_poll_fh). A select error counts as | ||
| 597 | # readable: the read that follows is what finds out. | ||
| 598 | sub _pg_readable_now { | ||
| 599 | my ($self, $conn) = @_; | ||
| 600 | my $rin = ''; | ||
| 601 | vec($rin, fileno($self->_conn_poll_fh($conn)), 1) = 1; | ||
| 602 | return select($rin, undef, undef, 0) != 0; | ||
| 603 | } | ||
| 604 | |||
| 605 | # --- LISTEN / NOTIFY seams -------------------------------------------------- | ||
| 606 | # | ||
| 607 | # Core DBIO::Storage::Async owns the contract and the machinery (ADR 0056); | ||
| 608 | # DBIO::Async::Storage owns the Future::IO watch loop, the backoff timer and the | ||
| 609 | # pooled _notify_send (dbio-async ADR 0005). What is left is PostgreSQL's: | ||
| 610 | # the dedicated DBD::Pg handle, quoted LISTEN / UNLISTEN, the pg_notifies drain, | ||
| 611 | # the probe and the pg_notify statement. Private helpers are named _pg_*, not | ||
| 612 | # _listen_*: they share an inheritance chain with core's private _listen_* | ||
| 613 | # helpers, and a same-named method appearing in core later would be shadowed. | ||
| 614 | |||
| 615 | |||
| 616 | sub _listen_open { | ||
| 617 | my ($self) = @_; | ||
| 618 | my $provider = $self->_conninfo_provider; | ||
| 619 | my $conninfo = $provider ? $provider->() : $self->{_conninfo}; | ||
| 620 | return $self->future_class->done($self->_pg_connect($conninfo)); | ||
| 621 | } | ||
| 622 | |||
| 623 | |||
| 624 | sub _listen_close { | ||
| 625 | my ($self, $conn) = @_; | ||
| 626 | my $dbh = delete $conn->{dbh} or return; | ||
| 627 | $dbh->disconnect if $dbh->{Active}; | ||
| 628 | return; | ||
| 629 | } | ||
| 630 | |||
| 631 | |||
| 632 | sub _listen_subscribe { | ||
| 633 | my ($self, $conn, $channel) = @_; | ||
| 634 | return $self->_pg_listen_command($conn, LISTEN => $channel); | ||
| 635 | } | ||
| 636 | |||
| 637 | |||
| 638 | sub _listen_unsubscribe { | ||
| 639 | my ($self, $conn, $channel) = @_; | ||
| 640 | return $self->_pg_listen_command($conn, UNLISTEN => $channel); | ||
| 641 | } | ||
| 642 | |||
| 643 | sub _pg_listen_command { | ||
| 644 | my ($self, $conn, $verb, $channel) = @_; | ||
| 645 | my $sql = "$verb " . $self->_pg_quote_channel($channel); | ||
| 646 | my $dbh = $conn->{dbh} or _pg_refuse("$verb: the listen connection is closed"); | ||
| 647 | $self->_pg_nonblocking($conn, sub { $dbh->do($sql) }); | ||
| 648 | $self->_pg_dispatch_buffered($conn); | ||
| 649 | return $self->future_class->done; | ||
| 650 | } | ||
| 651 | |||
| 652 | |||
| 653 | sub _pg_quote_channel { | ||
| 654 | my ($self, $channel) = @_; | ||
| 655 | return DBIO::PostgreSQL::Storage::Async::_Refusal::of_util( | ||
| 656 | sub { DBIO::PostgreSQL::Util::quote_channel($channel) }, | ||
| 657 | ); | ||
| 658 | } | ||
| 659 | |||
| 660 | |||
| 661 | sub _listen_drain { | ||
| 662 | my ($self, $conn) = @_; | ||
| 663 | my $dbh = $conn->{dbh} or _pg_refuse('the listen connection is closed'); | ||
| 664 | |||
| 665 | my @drained; | ||
| 666 | $self->_pg_nonblocking($conn, sub { | ||
| 667 | while (my $n = $dbh->pg_notifies) { | ||
| 668 | my ($channel, $sender_pid, $payload) = @$n; | ||
| 669 | push @drained, [ $channel, $payload, $sender_pid ]; | ||
| 670 | } | ||
| 671 | _pg_raise('draining the listen connection failed: ' . ($dbh->errstr // 'unknown error')) | ||
| 672 | if $dbh->err; | ||
| 673 | }); | ||
| 674 | return \@drained; | ||
| 675 | } | ||
| 676 | |||
| 677 | |||
| 678 | sub _listen_probe { | ||
| 679 | my ($self, $conn) = @_; | ||
| 680 | my $dbh = $conn->{dbh} or return 0; | ||
| 681 | return 0 unless $self->_pg_nonblocking($conn, sub { $dbh->ping }); | ||
| 682 | $self->_pg_dispatch_buffered($conn); | ||
| 683 | return 1; | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | sub _pg_dispatch_buffered { | ||
| 688 | my ($self, $conn) = @_; | ||
| 689 | for my $n (@{ $self->_listen_drain($conn) }) { | ||
| 690 | last unless $conn->{dbh}; | ||
| 691 | $self->_listen_dispatch(@$n); | ||
| 692 | } | ||
| 693 | return; | ||
| 694 | } | ||
| 695 | |||
| 696 | |||
| 697 | sub _pg_nonblocking { | ||
| 698 | my ($self, $conn, $code) = @_; | ||
| 699 | my $fh = do { local $@; eval { $self->_conn_poll_fh($conn) } }; | ||
| 700 | my $was_blocking = $fh ? $fh->blocking(0) : 0; | ||
| 701 | |||
| 702 | my ($r, $ok, $err); | ||
| 703 | { | ||
| 704 | local $@; | ||
| 705 | $ok = eval { $r = $code->(); 1 }; | ||
| 706 | $err = $@; | ||
| 707 | } | ||
| 708 | $fh->blocking(1) if $was_blocking; | ||
| 709 | die $err unless $ok; | ||
| 710 | return $r; | ||
| 711 | } | ||
| 712 | |||
| 713 | |||
| 714 | sub _notify_sql { | ||
| 715 | my ($self, $channel, $payload) = @_; | ||
| 716 | return ('SELECT pg_notify(?, ?)', [ $channel, $payload ]); | ||
| 717 | } | ||
| 718 | |||
| 719 | # --- Deploy seam ------------------------------------------------------------ | ||
| 720 | |||
| 721 | |||
| 722 | sub _ddl_class { 'DBIO::PostgreSQL::DDL' } | ||
| 723 | |||
| 724 | |||
| 725 | sub deploy_async { | ||
| 726 | my ($self, $schema, @rest) = @_; | ||
| 727 | |||
| 728 | my @declared = DBIO::PostgreSQL::DDL::_schema_decl($schema, 'pg_extensions'); | ||
| 729 | return $self->SUPER::deploy_async($schema, @rest) unless @declared; | ||
| 730 | |||
| 731 | return $self->_seam_future(_query_async => | ||
| 732 | DBIO::PostgreSQL::DDL::control_schemas_sql(@declared), [], | ||
| 733 | )->then(sub { | ||
| 734 | my %fixed = map { $_->[0] => $_->[1] } @_; | ||
| 735 | DBIO::PostgreSQL::DDL::check_control_schemas($schema, \%fixed); | ||
| 736 | return $self->_deploy_async_with_control_schemas($schema, \%fixed, @rest); | ||
| 737 | }); | ||
| 738 | } | ||
| 739 | |||
| 740 | # The schemas a control file fixes are handed to _install_ddl for the length | ||
| 741 | # of the synchronous call core's deploy_async makes to it, and no longer: a | ||
| 742 | # second deploy on the same storage reads its own. | ||
| 743 | sub _deploy_async_with_control_schemas { | ||
| 744 | my ($self, $schema, $fixed, @rest) = @_; | ||
| 745 | local $self->{_extension_control_schemas} = $fixed; | ||
| 746 | return $self->SUPER::deploy_async($schema, @rest); | ||
| 747 | } | ||
| 748 | |||
| 749 | |||
| 750 | sub _install_ddl { | ||
| 751 | my ($self, $schema) = @_; | ||
| 752 | return $self->_ddl_class->install_ddl($schema, | ||
| 753 | extension_control_schemas => $self->{_extension_control_schemas} // {}); | ||
| 754 | } | ||
| 755 | |||
| 756 | 1; | ||
| 757 | |||
| 758 | __END__ | ||