| 1 | package DBIx::QueryByName::SthPool; | ||
| 2 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use DBI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Carp qw(cluck); | ||
| 8 | use DBIx::QueryByName::Logger qw(get_logger debug); | ||
| 9 | use Scalar::Util qw(weaken); | ||
| 10 | |||
| 11 | sub _add_sth { | ||
| 12 | my ($self,$query,$sth) = @_; | ||
| 13 | get_logger->logcroak("undefined query or statement handler") | ||
| 14 | if (!defined $query || !defined $sth); | ||
| 15 | $self->{sths}->{$$}->{$query} = $sth; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub _get_sth { | ||
| 19 | my ($self,$query) = @_; | ||
| 20 | get_logger->logcroak("undefined query") | ||
| 21 | if (!defined $query); | ||
| 22 | return $self->{sths}->{$$}->{$query}; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | return bless( { sths => {} }, $_[0] ); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub parent { | ||
| 30 | my ($self, $parent) = @_; | ||
| 31 | $self->{dbhpool} = $parent->_dbh_pool; | ||
| 32 | weaken $self->{dbhpool}; | ||
| 33 | $self->{querypool} = $parent->_query_pool; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub finish_all_sths { | ||
| 37 | my $self = shift; | ||
| 38 | debug "Closing all sths for pid $$"; | ||
| 39 | foreach my $query ( keys %{$self->{sths}->{$$}} ) { | ||
| 40 | if (defined $self->{sths}->{$$}->{$query}) { | ||
| 41 | $self->{sths}->{$$}->{$query}->finish; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | delete $self->{sths}->{$$}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _prepare { | ||
| 48 | my ($self,$query) = @_; | ||
| 49 | my $log = get_logger(); | ||
| 50 | |||
| 51 | my ($session,$sql) = $self->{querypool}->get_query($query); | ||
| 52 | |||
| 53 | if (defined $self->_get_sth($query)) { | ||
| 54 | debug "Query is already prepared. Using cached value."; | ||
| 55 | return $self->_get_sth($query); | ||
| 56 | } | ||
| 57 | |||
| 58 | my $dbh = $self->{dbhpool}->connect($session); | ||
| 59 | |||
| 60 | debug "Preparing query $query"; | ||
| 61 | my $sth = $dbh->prepare($sql); | ||
| 62 | |||
| 63 | # TODO: add more verbose error description? | ||
| 64 | # TODO: retry in some smart way? | ||
| 65 | if (!defined $sth) { | ||
| 66 | $log->logcroak("failed to prepare query [$query]. Trace: ".cluck); | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->_add_sth($query,$sth); | ||
| 70 | return $sth; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub prepare_and_execute { | ||
| 74 | my ($self,%args) = @_; | ||
| 75 | my $log = get_logger(); | ||
| 76 | |||
| 77 | my $query = $args{query_name} || $log->logcroak("undefined query name"); | ||
| 78 | my $bulk_insertion = $args{bulk_insertion}; | ||
| 79 | $log->logcroak("undefined bulk insertion flag") if (!defined $bulk_insertion); | ||
| 80 | |||
| 81 | $log->logcroak("undefined or wrong query args") | ||
| 82 | if (!exists $args{query_args} || ref $args{query_args} ne 'ARRAY'); | ||
| 83 | my @args = @{$args{query_args}}; | ||
| 84 | |||
| 85 | my ($session,undef) = $self->{querypool}->get_query($query); | ||
| 86 | |||
| 87 | my $retry = $self->{querypool}->get_retry_attribute($query); | ||
| 88 | my $error_connection_lost = qr/could not connect to server|no connection to the server|terminating connection due to administrator command/; | ||
| 89 | my $error_state_unknown = qr/server closed the connection unexpectedly|could not receive data from server|terminating connection due to administrator command/; | ||
| 90 | |||
| 91 | |||
| 92 | my $sth = $self->_prepare($query); | ||
| 93 | |||
| 94 | my $rv; | ||
| 95 | my $error_reported = 0; | ||
| 96 | while (1) { | ||
| 97 | |||
| 98 | # Normally, if traffic between the client and the database | ||
| 99 | # server is interupted (cable cut, whatever), the client will | ||
| 100 | # timeout after 1min (observed on osx). But it has been | ||
| 101 | # observed on some setups (client on linux, server blocked by | ||
| 102 | # drop rule in firewall) that the client hang forever in | ||
| 103 | # execute(). The following code is a workaround: | ||
| 104 | # | ||
| 105 | # my $did_timeout = 0; | ||
| 106 | # eval { | ||
| 107 | # local $SIG{ALRM} = sub { $did_timeout = 1; die 'TIMEOUT' }; | ||
| 108 | # alarm($self->{execute_timeout}); | ||
| 109 | # # call execute | ||
| 110 | # alarm(0); | ||
| 111 | # }; | ||
| 112 | # alarm(0); | ||
| 113 | # | ||
| 114 | # if ($did_timeout) { | ||
| 115 | # } elsif ($@) {} | ||
| 116 | # | ||
| 117 | # But this code works only if perl is running with 'unsafe' | ||
| 118 | # signal handling (env PERL_SIGNALS=unsafe). And we don't want | ||
| 119 | # to compromise DBD::Pg by interupting impromptuously. | ||
| 120 | |||
| 121 | # WARNING: execute* might hang forever | ||
| 122 | if ($bulk_insertion == 1) { | ||
| 123 | unless (scalar @args && ref $args[0] eq 'ARRAY') { | ||
| 124 | $log->logcroak("invalid data structure of args for bulk insertion:" . Dumper(\@args)); | ||
| 125 | } | ||
| 126 | debug "Calling execute_array for query $query"; | ||
| 127 | $rv = $sth->execute_array({},@args); | ||
| 128 | } elsif($bulk_insertion == 0) { | ||
| 129 | debug "Calling execute for query $query with args" . Dumper(\@args); | ||
| 130 | $rv = $sth->execute(@args); | ||
| 131 | } else { | ||
| 132 | $log->logcroak("unexpected value of bulk_insertion: " . Dumper($bulk_insertion)); | ||
| 133 | } | ||
| 134 | |||
| 135 | if (!defined $rv) { | ||
| 136 | my $err = $sth->err || 99999999999999; | ||
| 137 | my $errstr = $sth->errstr || ''; | ||
| 138 | |||
| 139 | debug "An error occured while executing query [$query] [$err] [$errstr]"; | ||
| 140 | |||
| 141 | # if connection error while executing, retry | ||
| 142 | # TODO: support error messages per database type | ||
| 143 | # NOTE: if execute times-out properly, it raises an error with code 7 and text 'could not receive data from server: Operation timed out' | ||
| 144 | if ( | ||
| 145 | $err == 7 && | ||
| 146 | ( | ||
| 147 | ($retry eq 'safe' && $errstr =~ m/$error_connection_lost/) | ||
| 148 | || | ||
| 149 | ($retry eq 'always' && ($errstr =~ m/$error_connection_lost/ || $errstr =~ m/$error_state_unknown/)) | ||
| 150 | ) | ||
| 151 | ) { | ||
| 152 | |||
| 153 | $log->error("Query $query failed, will try again, Error code [$err], Error message [$errstr]. Trace: ".cluck) | ||
| 154 | if ($error_reported == 0); | ||
| 155 | |||
| 156 | # try to reconnect to database | ||
| 157 | my $dbh = $self->{dbhpool}->connect($session); | ||
| 158 | unless ($dbh->ping()) { | ||
| 159 | debug "Can ping database. Trying to disconnect, re-connect and re-prepare."; | ||
| 160 | $self->{dbhpool}->disconnect($session); | ||
| 161 | $self->finish_all_sths(); # TODO: do we really want to finish ALL queries or only those in this session? | ||
| 162 | # TODO: shouldn't we finish first, disconnect then? | ||
| 163 | $sth = $self->_prepare($query); | ||
| 164 | |||
| 165 | } else { | ||
| 166 | debug "Cannot ping database. Waiting 1sec and retrying to execute."; | ||
| 167 | } | ||
| 168 | |||
| 169 | } else { | ||
| 170 | $log->logcroak("Query $query failed, won't try again, Error code [$err], Error message [$errstr]. Trace: ".cluck); | ||
| 171 | # TODO: use sth's pg_cmd_status to warn extra if it was a delete/update/insert query | ||
| 172 | return undef; # Will never reach this line, logcroak will die, but just in case | ||
| 173 | } | ||
| 174 | |||
| 175 | $error_reported = 1; | ||
| 176 | sleep(1); | ||
| 177 | next; | ||
| 178 | } | ||
| 179 | |||
| 180 | if ($error_reported == 1) { | ||
| 181 | $log->info("Successfully retried executing query $query"); | ||
| 182 | } | ||
| 183 | last; | ||
| 184 | } | ||
| 185 | |||
| 186 | return $sth; | ||
| 187 | } | ||
| 188 | |||
| 189 | sub DESTROY { | ||
| 190 | my $self = shift; | ||
| 191 | debug "DESTROY SthPool -> calling finish_all_sths and disconnect_all"; | ||
| 192 | # either this DESTROY is called first, or DbhPool's DESTROY is | ||
| 193 | $self->finish_all_sths(); | ||
| 194 | $self->{dbhpool}->disconnect_all() if (defined $self->{dbhpool}); | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | |||
| 199 | __END__ | ||