| 1 | package DBIx::Class::Storage::DBI::mysql::Retryable; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use DBI '1.630'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use base qw< DBIx::Class::Storage::DBI::mysql >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Algorithm::Backoff::RetryTimeouts; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Context::Preserve; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use DBIx::ParseError::MySQL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use List::Util qw< min max >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Storable qw< dclone >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Time::HiRes qw< time sleep >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # ABSTRACT: MySQL-specific DBIC storage engine with retry support | ||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | our $VERSION = 'v1.0.3'; # VERSION | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||
| 24 | #pod package MySchema; | ||||||||||||||||||||||||||||
| 25 | #pod | ||||||||||||||||||||||||||||
| 26 | #pod # Recommended | ||||||||||||||||||||||||||||
| 27 | #pod DBIx::Class::Storage::DBI::mysql::Retryable->_use_join_optimizer(0); | ||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||
| 29 | #pod __PACKAGE__->storage_type('::DBI::mysql::Retryable'); | ||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||
| 31 | #pod # Optional settings (defaults shown) | ||||||||||||||||||||||||||||
| 32 | #pod my $storage_class = 'DBIx::Class::Storage::DBI::mysql::Retryable'; | ||||||||||||||||||||||||||||
| 33 | #pod $storage_class->parse_error_class('DBIx::ParseError::MySQL'); | ||||||||||||||||||||||||||||
| 34 | #pod $storage_class->timer_class('Algorithm::Backoff::RetryTimeouts'); | ||||||||||||||||||||||||||||
| 35 | #pod $storage_class->timer_options({}); # same defaults as the timer class | ||||||||||||||||||||||||||||
| 36 | #pod $storage_class->aggressive_timeouts(0); | ||||||||||||||||||||||||||||
| 37 | #pod $storage_class->retries_before_error_prefix(1); | ||||||||||||||||||||||||||||
| 38 | #pod $storage_class->warn_on_retryable_error(0); | ||||||||||||||||||||||||||||
| 39 | #pod $storage_class->enable_retryable(1); | ||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||
| 41 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | #pod | ||||||||||||||||||||||||||||
| 43 | #pod This storage engine for L<DBIx::Class> is a MySQL-specific engine that will explicitly | ||||||||||||||||||||||||||||
| 44 | #pod retry on MySQL-specific transient error messages, as identified by L<DBIx::ParseError::MySQL>, | ||||||||||||||||||||||||||||
| 45 | #pod using L<Algorithm::Backoff::RetryTimeouts> as its retry algorithm. This engine should be | ||||||||||||||||||||||||||||
| 46 | #pod much better at handling deadlocks, connection errors, and Galera node flips to ensure the | ||||||||||||||||||||||||||||
| 47 | #pod transaction always goes through. | ||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||
| 49 | #pod =head2 How Retryable Works | ||||||||||||||||||||||||||||
| 50 | #pod | ||||||||||||||||||||||||||||
| 51 | #pod A DBIC command triggers some sort of connection to the MySQL server to send SQL. First, | ||||||||||||||||||||||||||||
| 52 | #pod Retryable makes sure the connection C<mysql_*_timeout> values (except C<mysql_read_timeout> | ||||||||||||||||||||||||||||
| 53 | #pod unless L</aggressive_timeouts> is set) are set properly. (The default settings for | ||||||||||||||||||||||||||||
| 54 | #pod L<RetryTimeouts|Algorithm::Backoff::RetryTimeouts/Typical scenario> will use half of the | ||||||||||||||||||||||||||||
| 55 | #pod maximum duration, with some jitter.) If the connection was successful, a few C<SET SESSION> | ||||||||||||||||||||||||||||
| 56 | #pod commands for timeouts are sent first: | ||||||||||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||||||||||
| 58 | #pod wait_timeout # only with aggressive_timeouts=1 | ||||||||||||||||||||||||||||
| 59 | #pod lock_wait_timeout | ||||||||||||||||||||||||||||
| 60 | #pod innodb_lock_wait_timeout | ||||||||||||||||||||||||||||
| 61 | #pod net_read_timeout | ||||||||||||||||||||||||||||
| 62 | #pod net_write_timeout | ||||||||||||||||||||||||||||
| 63 | #pod | ||||||||||||||||||||||||||||
| 64 | #pod If the DBIC command fails at any point in the process, and the error is a recoverable | ||||||||||||||||||||||||||||
| 65 | #pod failure (according to the L<error parsing class|DBIx::ParseError::MySQL>), the retry | ||||||||||||||||||||||||||||
| 66 | #pod process starts. | ||||||||||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||||||||||
| 68 | #pod The timeouts are only checked during the retry handler. Since DB operations are XS | ||||||||||||||||||||||||||||
| 69 | #pod calls, Perl-style "safe" ALRM signals won't do any good, and the engine won't attempt to | ||||||||||||||||||||||||||||
| 70 | #pod use unsafe ones. Thus, the engine relies on the server to honor the timeouts set during | ||||||||||||||||||||||||||||
| 71 | #pod each attempt, and will give up if it runs out of time or attempts. | ||||||||||||||||||||||||||||
| 72 | #pod | ||||||||||||||||||||||||||||
| 73 | #pod If the DBIC command succeeds during the process, program flow resumes as normal. If any | ||||||||||||||||||||||||||||
| 74 | #pod re-attempts happened during the DBIC command, the timeouts are reset back to the original | ||||||||||||||||||||||||||||
| 75 | #pod post-connection values. | ||||||||||||||||||||||||||||
| 76 | #pod | ||||||||||||||||||||||||||||
| 77 | #pod =head1 STORAGE OPTIONS | ||||||||||||||||||||||||||||
| 78 | #pod | ||||||||||||||||||||||||||||
| 79 | #pod =cut | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 3 | __PACKAGE__->mk_group_accessors('inherited' => qw< | |||||||||||||||||||||||||||
| 82 | parse_error_class timer_class | ||||||||||||||||||||||||||||
| 83 | timer_options aggressive_timeouts | ||||||||||||||||||||||||||||
| 84 | retries_before_error_prefix | ||||||||||||||||||||||||||||
| 85 | warn_on_retryable_error enable_retryable | ||||||||||||||||||||||||||||
| 86 | >); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | __PACKAGE__->mk_group_accessors('simple' => qw< | |||||||||||||||||||||||||||
| 89 | _retryable_timer _retryable_current_timeout | ||||||||||||||||||||||||||||
| 90 | _retryable_call_type _retryable_exception_prefix | ||||||||||||||||||||||||||||
| 91 | _retryable_original_die_handler | ||||||||||||||||||||||||||||
| 92 | >); | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Set defaults | ||||||||||||||||||||||||||||
| 95 | 3 | __PACKAGE__->parse_error_class('DBIx::ParseError::MySQL'); | |||||||||||||||||||||||||||
| 96 | 3 | __PACKAGE__->timer_class('Algorithm::Backoff::RetryTimeouts'); | |||||||||||||||||||||||||||
| 97 | 3 | __PACKAGE__->timer_options({}); | |||||||||||||||||||||||||||
| 98 | 3 | __PACKAGE__->aggressive_timeouts(0); | |||||||||||||||||||||||||||
| 99 | 3 | __PACKAGE__->retries_before_error_prefix(1); | |||||||||||||||||||||||||||
| 100 | 3 | __PACKAGE__->warn_on_retryable_error(0); | |||||||||||||||||||||||||||
| 101 | 3 | __PACKAGE__->enable_retryable(1); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #pod =head2 parse_error_class | ||||||||||||||||||||||||||||
| 104 | #pod | ||||||||||||||||||||||||||||
| 105 | #pod Class used to parse MySQL error messages. | ||||||||||||||||||||||||||||
| 106 | #pod | ||||||||||||||||||||||||||||
| 107 | #pod Default is L<DBIx::ParseError::MySQL>. If a different class is used, it must support a | ||||||||||||||||||||||||||||
| 108 | #pod similar interface, especially the L<C<is_transient>|DBIx::ParseError::MySQL/is_transient> | ||||||||||||||||||||||||||||
| 109 | #pod method. | ||||||||||||||||||||||||||||
| 110 | #pod | ||||||||||||||||||||||||||||
| 111 | #pod =head2 timer_class | ||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||
| 113 | #pod Algorithm class used to determine timeout and sleep values during the retry process. | ||||||||||||||||||||||||||||
| 114 | #pod | ||||||||||||||||||||||||||||
| 115 | #pod Default is L<Algorithm::Backoff::RetryTimeouts>. If a different class is used, it must | ||||||||||||||||||||||||||||
| 116 | #pod support a similar interface, including the dual return of the L<C<failure>|Algorithm::Backoff::RetryTimeouts/failure> | ||||||||||||||||||||||||||||
| 117 | #pod method. | ||||||||||||||||||||||||||||
| 118 | #pod | ||||||||||||||||||||||||||||
| 119 | #pod =head2 timer_options | ||||||||||||||||||||||||||||
| 120 | #pod | ||||||||||||||||||||||||||||
| 121 | #pod Options to pass to the timer algorithm constructor, as a hashref. | ||||||||||||||||||||||||||||
| 122 | #pod | ||||||||||||||||||||||||||||
| 123 | #pod Default is an empty hashref, which would retain all of the defaults of the algorithm | ||||||||||||||||||||||||||||
| 124 | #pod module. | ||||||||||||||||||||||||||||
| 125 | #pod | ||||||||||||||||||||||||||||
| 126 | #pod =head2 aggressive_timeouts | ||||||||||||||||||||||||||||
| 127 | #pod | ||||||||||||||||||||||||||||
| 128 | #pod Boolean that controls whether to use some of the more aggressive, query-unfriendly | ||||||||||||||||||||||||||||
| 129 | #pod timeouts: | ||||||||||||||||||||||||||||
| 130 | #pod | ||||||||||||||||||||||||||||
| 131 | #pod =over | ||||||||||||||||||||||||||||
| 132 | #pod | ||||||||||||||||||||||||||||
| 133 | #pod =item mysql_read_timeout | ||||||||||||||||||||||||||||
| 134 | #pod | ||||||||||||||||||||||||||||
| 135 | #pod Controls the timeout for all read operations. Since SQL queries in the middle of | ||||||||||||||||||||||||||||
| 136 | #pod sending its first set of row data are still considered to be in a read operation, those | ||||||||||||||||||||||||||||
| 137 | #pod queries could time out during those circumstances. | ||||||||||||||||||||||||||||
| 138 | #pod | ||||||||||||||||||||||||||||
| 139 | #pod If you're confident that you don't have any SQL statements that would take longer than | ||||||||||||||||||||||||||||
| 140 | #pod C<R/2> (or at least returning results before that time), you can turn this option on. | ||||||||||||||||||||||||||||
| 141 | #pod Otherwise, you may experience longer-running statements going into a retry death spiral | ||||||||||||||||||||||||||||
| 142 | #pod until they finally hit the Retryable timeout for good and die. | ||||||||||||||||||||||||||||
| 143 | #pod | ||||||||||||||||||||||||||||
| 144 | #pod =item wait_timeout | ||||||||||||||||||||||||||||
| 145 | #pod | ||||||||||||||||||||||||||||
| 146 | #pod Controls how long the MySQL server waits for activity from the connection before timing | ||||||||||||||||||||||||||||
| 147 | #pod out. While most applications are going to be using the database connection pretty | ||||||||||||||||||||||||||||
| 148 | #pod frequently, the MySQL default (8 hours) is much much longer than the mere seconds this | ||||||||||||||||||||||||||||
| 149 | #pod engine would set it to. | ||||||||||||||||||||||||||||
| 150 | #pod | ||||||||||||||||||||||||||||
| 151 | #pod =back | ||||||||||||||||||||||||||||
| 152 | #pod | ||||||||||||||||||||||||||||
| 153 | #pod Default is off. Obviously, this setting only makes sense with L</retryable_timeout> | ||||||||||||||||||||||||||||
| 154 | #pod turned on. | ||||||||||||||||||||||||||||
| 155 | #pod | ||||||||||||||||||||||||||||
| 156 | #pod =head2 retries_before_error_prefix | ||||||||||||||||||||||||||||
| 157 | #pod | ||||||||||||||||||||||||||||
| 158 | #pod Controls the number of retries (not tries) needed before the exception message starts | ||||||||||||||||||||||||||||
| 159 | #pod using the statistics prefix, which looks something like this: | ||||||||||||||||||||||||||||
| 160 | #pod | ||||||||||||||||||||||||||||
| 161 | #pod Failed dbh_do coderef: Out of retries, attempts: 5 / 4, timer: 34.5 / 50.0 sec | ||||||||||||||||||||||||||||
| 162 | #pod | ||||||||||||||||||||||||||||
| 163 | #pod The default is 1, which means a failed first attempt (like a non-transient failure) will | ||||||||||||||||||||||||||||
| 164 | #pod show a normal exception, and the second attempt will use the prefix. You can set this to | ||||||||||||||||||||||||||||
| 165 | #pod 0 to always show the prefix, or a large number like 99 to keep the exception clean. | ||||||||||||||||||||||||||||
| 166 | #pod | ||||||||||||||||||||||||||||
| 167 | #pod =head2 warn_on_retryable_error | ||||||||||||||||||||||||||||
| 168 | #pod | ||||||||||||||||||||||||||||
| 169 | #pod Boolean that controls whether to warn on retryable failures, as the engine encounters | ||||||||||||||||||||||||||||
| 170 | #pod them. Many applications don't want spam on their screen for recoverable conditions, but | ||||||||||||||||||||||||||||
| 171 | #pod this may be useful for debugging or CLI tools. | ||||||||||||||||||||||||||||
| 172 | #pod | ||||||||||||||||||||||||||||
| 173 | #pod Unretryable failures always generate an exception as normal, regardless of the setting. | ||||||||||||||||||||||||||||
| 174 | #pod | ||||||||||||||||||||||||||||
| 175 | #pod This is functionally equivalent to L<DBI/PrintError>, but since L<"RaiseError"|DBI/RaiseError> | ||||||||||||||||||||||||||||
| 176 | #pod is already the DBIC-required default, the former option can't be used within DBI. | ||||||||||||||||||||||||||||
| 177 | #pod | ||||||||||||||||||||||||||||
| 178 | #pod Default is off. | ||||||||||||||||||||||||||||
| 179 | #pod | ||||||||||||||||||||||||||||
| 180 | #pod =head2 enable_retryable | ||||||||||||||||||||||||||||
| 181 | #pod | ||||||||||||||||||||||||||||
| 182 | #pod Boolean that enables the Retryable logic. This can be turned off to temporarily disable | ||||||||||||||||||||||||||||
| 183 | #pod it, and revert to DBIC's basic "retry once if disconnected" default. This may be useful | ||||||||||||||||||||||||||||
| 184 | #pod if a process is already using some other retry logic (like L<DBIx::OnlineDDL>). | ||||||||||||||||||||||||||||
| 185 | #pod | ||||||||||||||||||||||||||||
| 186 | #pod Messing with this setting in the middle of a database action would not be wise. | ||||||||||||||||||||||||||||
| 187 | #pod | ||||||||||||||||||||||||||||
| 188 | #pod Default is on. | ||||||||||||||||||||||||||||
| 189 | #pod | ||||||||||||||||||||||||||||
| 190 | #pod =for Pod::Coverage max_attempts retryable_timeout disable_retryable | ||||||||||||||||||||||||||||
| 191 | #pod | ||||||||||||||||||||||||||||
| 192 | #pod =cut | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | ### Backward-compatibility for legacy attributes | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 5 | ▶ | sub max_attempts { | ||||||||||||||||||||||||||
|
Subroutine
max_attempts: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 198 | 5 | my $opts = $self->timer_options; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 5 | ▶ | return $opts->{max_attempts} = $_[0] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 3 | ▶ | return $opts->{max_attempts} // 8; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"max_attempts"} // 8
MC/DC100%1/1
$opts->{"max_attempts"} // 8
$opts->{"max_attempts"}
Truth table100%2/2
A
A= $opts->{"max_attempts"}
| |||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 22 | ▶ | sub retryable_timeout { | ||||||||||||||||||||||||||
|
Subroutine
retryable_timeout: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 206 | 22 | my $opts = $self->timer_options; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 22 | ▶ | return $opts->{max_actual_duration} = $_[0] if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | 20 | ▶ | return $opts->{max_actual_duration} // 50; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"max_actual_duration"} // 50
MC/DC100%1/1
$opts->{"max_actual_duration"} // 50
$opts->{"max_actual_duration"}
Truth table100%2/2
A
A= $opts->{"max_actual_duration"}
| |||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 4 | ▶ | sub disable_retryable { | ||||||||||||||||||||||||||
|
Subroutine
disable_retryable: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 213 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 214 | 4 | ▶ | $self->enable_retryable( $_[0] ? 0 : 1 ) if @_; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 215 | 4 | ▶ | return $self->enable_retryable ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | #pod =head1 METHODS | ||||||||||||||||||||||||||||
| 219 | #pod | ||||||||||||||||||||||||||||
| 220 | #pod =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 53 | ▶ | sub _build_retryable_timer { | ||||||||||||||||||||||||||
|
Subroutine
_build_retryable_timer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 223 | 53 | my $self = shift; | |||||||||||||||||||||||||||
| 224 | return $self->timer_class->new( | ||||||||||||||||||||||||||||
| 225 | 53 | %{ dclone $self->timer_options } | |||||||||||||||||||||||||||
| 226 | ); | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 28 | ▶ | sub _reset_retryable_timeout { | ||||||||||||||||||||||||||
|
Subroutine
_reset_retryable_timeout: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 230 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Use a temporary timer to get the first timeout value | ||||||||||||||||||||||||||||
| 233 | 28 | my $timeout = $self->_build_retryable_timer->timeout; | |||||||||||||||||||||||||||
| 234 | 28 | ▶ | $timeout = 0 if $timeout == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 28 | $self->_retryable_current_timeout($timeout); | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 31 | ▶ | sub _failed_attempt_count { shift->_retryable_timer->{_attempts} // 0 } | ||||||||||||||||||||||||||
|
Subroutine
_failed_attempt_count: called CC=2 SCAR=7.6
Condition50%1/2
+(shift())->_retryable_timer->{"_attempts"} // 0
MC/DC0%0/1
+(shift())->_retryable_timer->{"_attempts"} // 0
+(shift())->_retryable_timer->{"_attempts"}
Truth table50%1/2
A
A= +(shift())->_retryable_timer->{"_attempts"}
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | # Constructor | ||||||||||||||||||||||||||||
| 241 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 242 | 2 | my $self = shift->next::method(@_); | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 2 | $self->_reset_retryable_timeout; | |||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 2 | $self; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Return the list of timeout strings to check | ||||||||||||||||||||||||||||
| 250 | 8 | ▶ | sub _timeout_set_list { | ||||||||||||||||||||||||||
|
Subroutine
_timeout_set_list: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 251 | 8 | my ($self, $type) = @_; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 8 | my @timeout_set; | |||||||||||||||||||||||||||
| 254 | 8 | ▶ | if ($type eq 'dbi') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 255 | 3 | @timeout_set = (qw< connect write >); | |||||||||||||||||||||||||||
| 256 | 3 | ▶ | push @timeout_set, 'read' if $self->aggressive_timeouts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 3 | @timeout_set = map { "mysql_${_}_timeout" } @timeout_set; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | elsif ($type eq 'session') { | ||||||||||||||||||||||||||||
| 261 | 5 | @timeout_set = (qw< lock_wait innodb_lock_wait net_read net_write >); | |||||||||||||||||||||||||||
| 262 | 5 | ▶ | push @timeout_set, 'wait' if $self->aggressive_timeouts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 5 | @timeout_set = map { "${_}_timeout" } @timeout_set; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | else { | ||||||||||||||||||||||||||||
| 267 | 0 | die "Unknown mysql timeout set: $type"; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 8 | return @timeout_set; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # Set the timeouts for reconnections by inserting them into the default DBI connection | ||||||||||||||||||||||||||||
| 274 | # attributes. | ||||||||||||||||||||||||||||
| 275 | 14 | ▶ | sub _default_dbi_connect_attributes () { | ||||||||||||||||||||||||||
|
Subroutine
_default_dbi_connect_attributes: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 276 | 14 | my $self = shift; | |||||||||||||||||||||||||||
| 277 | 14 | ▶ | return $self->next::method unless $self->_retryable_current_timeout && $self->enable_retryable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_retryable_current_timeout and $self->enable_retryable
MC/DC100%2/2
$self->_retryable_current_timeout and $self->enable_retryable
$self->_retryable_current_timeout
$self->enable_retryable
Truth table100%3/3
A and B
A= $self->_retryable_current_timeoutB= $self->enable_retryable
| |||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 3 | my $timeout = int( $self->_retryable_current_timeout + 0.5 ); | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | return +{ | ||||||||||||||||||||||||||||
| 282 | 6 | (map {; $_ => $timeout } $self->_timeout_set_list('dbi')), # set timeouts | |||||||||||||||||||||||||||
| 283 | mysql_auto_reconnect => 0, # do not use MySQL's own reconnector | ||||||||||||||||||||||||||||
| 284 | 3 | %{ $self->next::method }, # inherit the other default attributes | |||||||||||||||||||||||||||
| 285 | }; | ||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # Re-apply the timeout settings above on _dbi_connect_info. Used after the initial | ||||||||||||||||||||||||||||
| 289 | # connection by the retry handling. | ||||||||||||||||||||||||||||
| 290 | 0 | ▶ | sub _set_dbi_connect_info { | ||||||||||||||||||||||||||
|
Subroutine
_set_dbi_connect_info: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 291 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 292 | 0 | ▶ | return unless $self->_retryable_current_timeout && $self->enable_retryable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->_retryable_current_timeout and $self->enable_retryable
MC/DC0%0/2
$self->_retryable_current_timeout and $self->enable_retryable
$self->_retryable_current_timeout
$self->enable_retryable
Truth table0%0/3
A and B
A= $self->_retryable_current_timeoutB= $self->enable_retryable
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | my $timeout = int( $self->_retryable_current_timeout + 0.5 ); | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 0 | my $info = $self->_dbi_connect_info; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # Not even going to attempt this one... | ||||||||||||||||||||||||||||
| 299 | 0 | ▶ | if (ref $info eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | ▶ | warn <<"EOW" unless $ENV{DBIC_RETRYABLE_DONT_SET_CONNECT_SESSION_VARS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | *************************************************************************** | ||||||||||||||||||||||||||||
| 303 | Your connect_info is a coderef, which means connection-based MySQL timeouts | ||||||||||||||||||||||||||||
| 304 | cannot be dynamically changed. Under certain conditions, the connection (or | ||||||||||||||||||||||||||||
| 305 | combination of connection attempts) may take longer to timeout than your | ||||||||||||||||||||||||||||
| 306 | current timer settings. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | You'll want to revert to a 4-element style DBI argument set, to fully | ||||||||||||||||||||||||||||
| 309 | support the timeout functionality. | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | To disable this warning, set a true value to the environment variable | ||||||||||||||||||||||||||||
| 312 | DBIC_RETRYABLE_DONT_SET_CONNECT_SESSION_VARS | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | *************************************************************************** | ||||||||||||||||||||||||||||
| 315 | EOW | ||||||||||||||||||||||||||||
| 316 | 0 | return; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | my $dbi_attr = $info->[3]; | |||||||||||||||||||||||||||
| 320 | 0 | ▶ | return unless $dbi_attr && ref $dbi_attr eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$dbi_attr and ref $dbi_attr eq "HASH"
MC/DC0%0/2
$dbi_attr and ref $dbi_attr eq "HASH"
$dbi_attr
ref $dbi_attr eq "HASH"
Truth table0%0/3
A and B
A= $dbi_attrB= ref $dbi_attr eq "HASH"
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 0 | $dbi_attr->{$_} = $timeout for $self->_timeout_set_list('dbi'); | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # Set session timeouts for post-connection variables | ||||||||||||||||||||||||||||
| 326 | 23 | ▶ | sub _run_connection_actions { | ||||||||||||||||||||||||||
|
Subroutine
_run_connection_actions: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 327 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 328 | 23 | $self->_set_retryable_session_timeouts; | |||||||||||||||||||||||||||
| 329 | 23 | $self->next::method(@_); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 23 | ▶ | sub _set_retryable_session_timeouts { | ||||||||||||||||||||||||||
|
Subroutine
_set_retryable_session_timeouts: called CC=9 SCAR=27.5
| |||||||||||||||||||||||||||||
| 333 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 334 | 23 | ▶ | return unless $self->_retryable_current_timeout && $self->enable_retryable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_retryable_current_timeout and $self->enable_retryable
MC/DC100%2/2
$self->_retryable_current_timeout and $self->enable_retryable
$self->_retryable_current_timeout
$self->enable_retryable
Truth table100%3/3
A and B
A= $self->_retryable_current_timeoutB= $self->enable_retryable
| |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 5 | my $timeout = int( $self->_retryable_current_timeout + 0.5 ); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # Ironically, we aren't running our own SET SESSION commands with their own | ||||||||||||||||||||||||||||
| 339 | # BlockRunner protection, since that may lead to infinite stack recursion. Instead, | ||||||||||||||||||||||||||||
| 340 | # put it in a basic eval, and do a quick is_transient check. If it passes, let the | ||||||||||||||||||||||||||||
| 341 | # next *_do/_do_query call handle it. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 5 | local $@; | |||||||||||||||||||||||||||
| 344 | 5 | eval { | |||||||||||||||||||||||||||
| 345 | # Again, don't want to let outside handlers ruin our error checking. This | ||||||||||||||||||||||||||||
| 346 | # expires before our 'die' statements below. | ||||||||||||||||||||||||||||
| 347 | 5 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 5 | my $dbh = $self->_dbh; | |||||||||||||||||||||||||||
| 350 | 5 | ▶ | if ($dbh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 5 | $dbh->do("SET SESSION $_=$timeout") for $self->_timeout_set_list('session'); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | }; | ||||||||||||||||||||||||||||
| 354 | # Protect $@ again, just in case the parser class does something inappropriate | ||||||||||||||||||||||||||||
| 355 | # with a blessed $error | ||||||||||||||||||||||||||||
| 356 | 5 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 0 | ▶ | die unless do { # bare die for $@ propagation | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | 0 | local $@; | |||||||||||||||||||||||||||
| 359 | 0 | $self->parse_error_class->new($error)->is_transient; | |||||||||||||||||||||||||||
| 360 | }; | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | # The error may have been transient, but we might have ran out of retries, anyway | ||||||||||||||||||||||||||||
| 363 | 0 | ▶ | die if $error =~ m<Failed \w+ coderef: .+, attempts: \d+ / \d+, timer: [\d\.]+ / [\d\.]+ sec, last exception: >; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | warn "Encountered a recoverable error during SET SESSION timeout commands: $error" if $self->warn_on_retryable_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | # Make sure the initial connection call is protected from retryable failures | ||||||||||||||||||||||||||||
| 370 | 21 | ▶ | sub _connect { | ||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 371 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 372 | 21 | ▶ | return $self->next::method() unless $self->enable_retryable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | # next::can here to do mro calculations prior to sending to _blockrunner_do | ||||||||||||||||||||||||||||
| 374 | 19 | return $self->_blockrunner_do( connect => $self->next::can() ); | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | #pod =head2 dbh_do | ||||||||||||||||||||||||||||
| 378 | #pod | ||||||||||||||||||||||||||||
| 379 | #pod my $val = $schema->storage->dbh_do( | ||||||||||||||||||||||||||||
| 380 | #pod sub { | ||||||||||||||||||||||||||||
| 381 | #pod my ($storage, $dbh, @binds) = @_; | ||||||||||||||||||||||||||||
| 382 | #pod $dbh->selectrow_array($sql, undef, @binds); | ||||||||||||||||||||||||||||
| 383 | #pod }, | ||||||||||||||||||||||||||||
| 384 | #pod @passed_binds, | ||||||||||||||||||||||||||||
| 385 | #pod ); | ||||||||||||||||||||||||||||
| 386 | #pod | ||||||||||||||||||||||||||||
| 387 | #pod This is very much like L<DBIx::Class::Storage::DBI/dbh_do>, except it doesn't require a | ||||||||||||||||||||||||||||
| 388 | #pod connection failure to retry the sub block. Instead, it will also retry on locks, query | ||||||||||||||||||||||||||||
| 389 | #pod interruptions, and failovers. | ||||||||||||||||||||||||||||
| 390 | #pod | ||||||||||||||||||||||||||||
| 391 | #pod Normal users of DBIC typically won't use this method directly. Instead, any ResultSet | ||||||||||||||||||||||||||||
| 392 | #pod or Result method that contacts the DB will send its SQL through here, and protect it from | ||||||||||||||||||||||||||||
| 393 | #pod retryable failures. | ||||||||||||||||||||||||||||
| 394 | #pod | ||||||||||||||||||||||||||||
| 395 | #pod However, this method is recommended over using C<< $schema->storage->dbh >> directly to | ||||||||||||||||||||||||||||
| 396 | #pod run raw SQL statements. | ||||||||||||||||||||||||||||
| 397 | #pod | ||||||||||||||||||||||||||||
| 398 | #pod =cut | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | # Main "doer" method for both dbh_do and txn_do | ||||||||||||||||||||||||||||
| 401 | 41 | ▶ | sub _blockrunner_do { | ||||||||||||||||||||||||||
|
Subroutine
_blockrunner_do: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 402 | 41 | my $self = shift; | |||||||||||||||||||||||||||
| 403 | 41 | my $call_type = shift; | |||||||||||||||||||||||||||
| 404 | 41 | my $run_target = shift; | |||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | # See https://metacpan.org/release/DBIx-Class/source/lib/DBIx/Class/Storage/DBI.pm#L842 | ||||||||||||||||||||||||||||
| 407 | 41 | ▶ | my $args = @_ ? \@_ : []; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 59 | ▶ | my $target_runner = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 410 | # dbh_do and txn_do have different sub arguments, and _connect shouldn't | ||||||||||||||||||||||||||||
| 411 | # have a _get_dbh call. | ||||||||||||||||||||||||||||
| 412 | 59 | ▶ | if ($call_type eq 'txn_do') { $run_target->( @$args ); } | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 413 | 28 | elsif ($call_type eq 'dbh_do') { $self->$run_target( $self->_get_dbh, @$args ); } | |||||||||||||||||||||||||||
| 414 | 20 | elsif ($call_type eq 'connect') { $self->$run_target( @$args ); } | |||||||||||||||||||||||||||
| 415 | 0 | else { die "Unknown call type: $call_type" } | |||||||||||||||||||||||||||
| 416 | 41 | }; | |||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | # Transaction depth short circuit (same as DBIx::Class::Storage::DBI) | ||||||||||||||||||||||||||||
| 419 | 41 | ▶ | return $target_runner->() if $self->{_in_do_block} || $self->transaction_depth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"_in_do_block"} or $self->transaction_depth
MC/DC50%1/2
$self->{"_in_do_block"} or $self->transaction_depth
$self->{"_in_do_block"}
$self->transaction_depth
Truth table66%2/3
A or B
A= $self->{"_in_do_block"}B= $self->transaction_depth
| |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # Given our transaction depth short circuits, we should be at the outermost loop, | ||||||||||||||||||||||||||||
| 422 | # so it's safe to reset our variables. | ||||||||||||||||||||||||||||
| 423 | 25 | $self->_retryable_timer( $self->_build_retryable_timer ); | |||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | 25 | my $timeout = $self->_retryable_timer->timeout; | |||||||||||||||||||||||||||
| 426 | 25 | ▶ | $timeout = 0 if $timeout == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 427 | 25 | $self->_retryable_current_timeout($timeout); | |||||||||||||||||||||||||||
| 428 | 25 | $self->_retryable_call_type($call_type); | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | # We have some post-processing to do, so save the BlockRunner object, and then save | ||||||||||||||||||||||||||||
| 431 | # the result in a context-sensitive manner. | ||||||||||||||||||||||||||||
| 432 | 25 | my $br = DBIx::Class::Storage::BlockRunner->new( | |||||||||||||||||||||||||||
| 433 | storage => $self, | ||||||||||||||||||||||||||||
| 434 | wrap_txn => $call_type eq 'txn_do', | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # This neuters the max_attempts trigger in failed_attempt_count, so that the main check | ||||||||||||||||||||||||||||
| 437 | # in our retry_handler works as expected. | ||||||||||||||||||||||||||||
| 438 | max_attempts => 99999, | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | retry_handler => \&_blockrunner_retry_handler, | ||||||||||||||||||||||||||||
| 441 | ); | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | ### XXX: Outside exception handlers shouldn't interrupt the retry process, as it might never | ||||||||||||||||||||||||||||
| 444 | ### never return back from the eval. This should really be a part of BlockRunner, but it's | ||||||||||||||||||||||||||||
| 445 | ### not our module, so we hit the "local $SIG{__DIE__}" bit here. What that means is that | ||||||||||||||||||||||||||||
| 446 | ### we're removing the die handler a bit too high up in the process, and we have exception | ||||||||||||||||||||||||||||
| 447 | ### throwing that should use the outside handler. | ||||||||||||||||||||||||||||
| 448 | ### | ||||||||||||||||||||||||||||
| 449 | ### Also, BlockRunner doesn't always protect its $@ result from DBI evals, so we also need to | ||||||||||||||||||||||||||||
| 450 | ### protect that here. | ||||||||||||||||||||||||||||
| 451 | ### | ||||||||||||||||||||||||||||
| 452 | ### So, we save it here, and throw it out when we're done. | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 25 | $self->_retryable_original_die_handler( $SIG{__DIE__} ); | |||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | 25 | ▶ | return preserve_context { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 457 | 25 | local $@; | |||||||||||||||||||||||||||
| 458 | 25 | local $SIG{__DIE__}; | |||||||||||||||||||||||||||
| 459 | 25 | $br->run($target_runner); | |||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||
| 461 | 25 | ▶ | after => sub { $self->_reset_timers_and_timeouts }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | 0 | $self->_retryable_original_die_handler(undef); | |||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | # Our own BlockRunner retry handler | ||||||||||||||||||||||||||||
| 467 | 21 | ▶ | sub _blockrunner_retry_handler { | ||||||||||||||||||||||||||
|
Subroutine
_blockrunner_retry_handler: called CC=8 SCAR=21.4
| |||||||||||||||||||||||||||||
| 468 | 21 | my $br = shift; | |||||||||||||||||||||||||||
| 469 | 21 | my $self = $br->storage; # "self" for this module | |||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 21 | my $last_error = $br->last_exception; | |||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | # Record the failure in the timer algorithm (prior to any checks) | ||||||||||||||||||||||||||||
| 474 | 21 | my ($sleep_time, $new_timeout) = $self->_retryable_timer->failure; | |||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | # If it's not a retryable error, stop here | ||||||||||||||||||||||||||||
| 477 | 21 | my $parsed_error = $self->parse_error_class->new($last_error); | |||||||||||||||||||||||||||
| 478 | 21 | ▶ | return $self->_reset_and_fail('Exception not transient') unless $parsed_error->is_transient; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 19 | $last_error =~ s/\n.+//s; | |||||||||||||||||||||||||||
| 481 | 19 | ▶ | $self->_warn_retryable_error($last_error) if $self->warn_on_retryable_error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | # Either stop here (because of timeout or max attempts), sleep, or don't | ||||||||||||||||||||||||||||
| 484 | 19 | ▶ | if ($sleep_time == -1) { return $self->_reset_and_fail('Out of retries') } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 485 | 10 | elsif ($sleep_time) { sleep $sleep_time; } | |||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | 18 | ▶ | if ($new_timeout > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | # Reset the connection timeouts before we connect again | ||||||||||||||||||||||||||||
| 489 | 0 | $self->_retryable_current_timeout($new_timeout); | |||||||||||||||||||||||||||
| 490 | 0 | $self->_set_dbi_connect_info; | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | # Force a disconnect, but only if the connection seems to be in a broken state | ||||||||||||||||||||||||||||
| 494 | 18 | local $@; | |||||||||||||||||||||||||||
| 495 | 18 | ▶ | unless ($parsed_error->error_type eq 'lock') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 496 | 5 | eval { local $SIG{__DIE__}; $self->disconnect }; | |||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | # Because BlockRunner calls this unprotected, and because our own _connect is going | ||||||||||||||||||||||||||||
| 500 | # to hit the _in_do_block short-circuit, we should call this ourselves, in a | ||||||||||||||||||||||||||||
| 501 | # protected eval, and re-direct any errors as if it was another failed attempt. | ||||||||||||||||||||||||||||
| 502 | 18 | eval { $self->ensure_connected }; | |||||||||||||||||||||||||||
| 503 | 18 | ▶ | if (my $connect_error = $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | 0 | push @{ $br->exception_stack }, $connect_error; | |||||||||||||||||||||||||||
| 505 | 0 | return _blockrunner_retry_handler($br); | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | 18 | return 1; | |||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 19 | ▶ | sub dbh_do { | ||||||||||||||||||||||||||
|
Subroutine
dbh_do: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 512 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 513 | 19 | ▶ | return $self->next::method(@_) unless $self->enable_retryable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | 17 | return $self->_blockrunner_do( dbh_do => @_ ); | |||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | #pod =head2 txn_do | ||||||||||||||||||||||||||||
| 518 | #pod | ||||||||||||||||||||||||||||
| 519 | #pod my $val = $schema->txn_do( | ||||||||||||||||||||||||||||
| 520 | #pod sub { | ||||||||||||||||||||||||||||
| 521 | #pod # ...DBIC calls within transaction... | ||||||||||||||||||||||||||||
| 522 | #pod }, | ||||||||||||||||||||||||||||
| 523 | #pod @misc_args_passed_to_coderef, | ||||||||||||||||||||||||||||
| 524 | #pod ); | ||||||||||||||||||||||||||||
| 525 | #pod | ||||||||||||||||||||||||||||
| 526 | #pod Works just like L<DBIx::Class::Storage/txn_do>, except it's now protected against | ||||||||||||||||||||||||||||
| 527 | #pod retryable failures. | ||||||||||||||||||||||||||||
| 528 | #pod | ||||||||||||||||||||||||||||
| 529 | #pod Calling this method through the C<$schema> object is typically more convenient. | ||||||||||||||||||||||||||||
| 530 | #pod | ||||||||||||||||||||||||||||
| 531 | #pod =cut | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 5 | ▶ | sub txn_do { | ||||||||||||||||||||||||||
|
Subroutine
txn_do: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 534 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 535 | 5 | ▶ | return $self->next::method(@_) unless $self->enable_retryable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | # Connects or reconnects on pid change to grab correct txn_depth (same as | ||||||||||||||||||||||||||||
| 538 | # DBIx::Class::Storage::DBI) | ||||||||||||||||||||||||||||
| 539 | 5 | $self->_get_dbh; | |||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | 5 | $self->_blockrunner_do( txn_do => @_ ); | |||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | #pod =for Pod::Coverage is_dbi_error_retryable | ||||||||||||||||||||||||||||
| 545 | #pod | ||||||||||||||||||||||||||||
| 546 | #pod =cut | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | ### XXX: This is a now deprecated method that only existed in the non-public version, but | ||||||||||||||||||||||||||||
| 549 | ### it's a public method that should still exist for anybody previously using it. | ||||||||||||||||||||||||||||
| 550 | 2 | ▶ | sub is_dbi_error_retryable { | ||||||||||||||||||||||||||
|
Subroutine
is_dbi_error_retryable: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 551 | 2 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 552 | 2 | return $self->parse_error_class->new($error)->is_transient; | |||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | #pod =head2 throw_exception | ||||||||||||||||||||||||||||
| 556 | #pod | ||||||||||||||||||||||||||||
| 557 | #pod $storage->throw_exception('It failed'); | ||||||||||||||||||||||||||||
| 558 | #pod | ||||||||||||||||||||||||||||
| 559 | #pod Works just like L<DBIx::Class::Storage/throw_exception>, but also reports attempt and | ||||||||||||||||||||||||||||
| 560 | #pod timer statistics, in case the transaction was tried multiple times. | ||||||||||||||||||||||||||||
| 561 | #pod | ||||||||||||||||||||||||||||
| 562 | #pod =cut | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | 25 | ▶ | sub _reset_timers_and_timeouts { | ||||||||||||||||||||||||||
|
Subroutine
_reset_timers_and_timeouts: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 565 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # Only reset timeouts if we have to, but check before we clear | ||||||||||||||||||||||||||||
| 568 | 25 | ▶ | my $needs_resetting = $self->_failed_attempt_count && $self->_retryable_current_timeout; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_failed_attempt_count && $self->_retryable_current_timeout
MC/DC0%0/2
$self->_failed_attempt_count && $self->_retryable_current_timeout
$self->_failed_attempt_count
$self->_retryable_current_timeout
Truth table66%2/3
A && B
A= $self->_failed_attempt_countB= $self->_retryable_current_timeout
| |||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 25 | $self->_retryable_timer(undef); | |||||||||||||||||||||||||||
| 571 | 25 | $self->_reset_retryable_timeout; | |||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | 25 | ▶ | if ($needs_resetting) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | 0 | $self->_set_dbi_connect_info; | |||||||||||||||||||||||||||
| 575 | 0 | $self->_set_retryable_session_timeouts; | |||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | # Useful for chaining to the return call in _blockrunner_retry_handler | ||||||||||||||||||||||||||||
| 579 | 25 | return undef; | |||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 0 | ▶ | sub _warn_retryable_error { | ||||||||||||||||||||||||||
|
Subroutine
_warn_retryable_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 583 | 0 | my ($self, $error) = @_; | |||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 0 | my $timer = $self->_retryable_timer; | |||||||||||||||||||||||||||
| 586 | 0 | my $current_attempt_count = $self->_failed_attempt_count + 1; | |||||||||||||||||||||||||||
| 587 | my $debug_msg = sprintf( | ||||||||||||||||||||||||||||
| 588 | 'Retrying %s coderef, attempt %u of %u, timer: %.1f / %.1f sec, last exception: %s', | ||||||||||||||||||||||||||||
| 589 | $self->_retryable_call_type, | ||||||||||||||||||||||||||||
| 590 | $current_attempt_count, $self->max_attempts, | ||||||||||||||||||||||||||||
| 591 | $timer->{_last_timestamp} - $timer->{_start_timestamp}, $timer->{max_actual_duration}, | ||||||||||||||||||||||||||||
| 592 | 0 | $error | |||||||||||||||||||||||||||
| 593 | ); | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | 0 | warn $debug_msg; | |||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | 3 | ▶ | sub _reset_and_fail { | ||||||||||||||||||||||||||
|
Subroutine
_reset_and_fail: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||
| 599 | 3 | my ($self, $fail_reason) = @_; | |||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | # About to throw the main exception, so set the original handler | ||||||||||||||||||||||||||||
| 602 | 3 | $SIG{__DIE__} = $self->_retryable_original_die_handler; | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | # First error (by default): just pass the exception unaltered | ||||||||||||||||||||||||||||
| 605 | 3 | ▶ | if ($self->_failed_attempt_count <= $self->retries_before_error_prefix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 606 | 0 | $self->_retryable_exception_prefix(undef); | |||||||||||||||||||||||||||
| 607 | 0 | return $self->_reset_timers_and_timeouts; | |||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | 3 | my $timer = $self->_retryable_timer; | |||||||||||||||||||||||||||
| 611 | $self->_retryable_exception_prefix( sprintf( | ||||||||||||||||||||||||||||
| 612 | 'Failed %s coderef: %s, attempts: %u / %u, timer: %.1f / %.1f sec', | ||||||||||||||||||||||||||||
| 613 | $self->_retryable_call_type, $fail_reason, | ||||||||||||||||||||||||||||
| 614 | $self->_failed_attempt_count, $self->max_attempts, | ||||||||||||||||||||||||||||
| 615 | $timer->{_last_timestamp} - $timer->{_start_timestamp}, $timer->{max_actual_duration}, | ||||||||||||||||||||||||||||
| 616 | 3 | ) ); | |||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 3 | return $self->_reset_timers_and_timeouts; | |||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | 26 | ▶ | sub throw_exception { | ||||||||||||||||||||||||||
|
Subroutine
throw_exception: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||
| 622 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | # Clear the prefix as we use it | ||||||||||||||||||||||||||||
| 625 | 26 | my $exception_prefix = $self->_retryable_exception_prefix; | |||||||||||||||||||||||||||
| 626 | 26 | ▶ | $self->_retryable_exception_prefix(undef) if $exception_prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | 26 | ▶ | return $self->next::method(@_) unless $exception_prefix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | 3 | my $error = shift; | |||||||||||||||||||||||||||
| 631 | 3 | $exception_prefix .= ', last exception: '; | |||||||||||||||||||||||||||
| 632 | 3 | ▶ | if (blessed $error && $error->isa('DBIx::Class::Exception')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($error) and $error->isa("DBIx::Class::Exception")
MC/DC0%0/2
builtin::blessed($error) and $error->isa("DBIx::Class::Exception")
builtin::blessed($error)
$error->isa("DBIx::Class::Exception")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->isa("DBIx::Class::Exception")
| |||||||||||||||||||||||||||||
| 633 | 3 | $error->{msg} = $exception_prefix.$error->{msg}; | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | else { | ||||||||||||||||||||||||||||
| 636 | 0 | $error = $exception_prefix.$error; | |||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||
| 638 | 3 | return $self->next::method($error, @_); | |||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | #pod =head1 CAVEATS | ||||||||||||||||||||||||||||
| 642 | #pod | ||||||||||||||||||||||||||||
| 643 | #pod =head2 Transactions without txn_do | ||||||||||||||||||||||||||||
| 644 | #pod | ||||||||||||||||||||||||||||
| 645 | #pod Retryable is transaction-safe. Only the outermost transaction depth gets the retry | ||||||||||||||||||||||||||||
| 646 | #pod protection, since that's the only layer that is idempotent and atomic. | ||||||||||||||||||||||||||||
| 647 | #pod | ||||||||||||||||||||||||||||
| 648 | #pod However, transaction commands like C<txn_begin> and C<txn_scope_guard> are NOT granted | ||||||||||||||||||||||||||||
| 649 | #pod retry protection, because DBIC/Retryable does not have a defined transaction-safe code | ||||||||||||||||||||||||||||
| 650 | #pod closure to use upon reconnection. Only C<txn_do> will have the protections available. | ||||||||||||||||||||||||||||
| 651 | #pod | ||||||||||||||||||||||||||||
| 652 | #pod For example: | ||||||||||||||||||||||||||||
| 653 | #pod | ||||||||||||||||||||||||||||
| 654 | #pod # Has retry protetion | ||||||||||||||||||||||||||||
| 655 | #pod my $rs = $schema->resultset('Foo'); | ||||||||||||||||||||||||||||
| 656 | #pod $rs->delete; | ||||||||||||||||||||||||||||
| 657 | #pod | ||||||||||||||||||||||||||||
| 658 | #pod # This effectively turns off retry protection | ||||||||||||||||||||||||||||
| 659 | #pod $schema->txn_begin; | ||||||||||||||||||||||||||||
| 660 | #pod | ||||||||||||||||||||||||||||
| 661 | #pod # NOT protected from retryable errors! | ||||||||||||||||||||||||||||
| 662 | #pod my $result = $rs->create({bar => 12}); | ||||||||||||||||||||||||||||
| 663 | #pod $result->update({baz => 42}); | ||||||||||||||||||||||||||||
| 664 | #pod | ||||||||||||||||||||||||||||
| 665 | #pod $schema->txn_commit; | ||||||||||||||||||||||||||||
| 666 | #pod # Retry protection is back on | ||||||||||||||||||||||||||||
| 667 | #pod | ||||||||||||||||||||||||||||
| 668 | #pod # Do this instead! | ||||||||||||||||||||||||||||
| 669 | #pod $schema->txn_do(sub { | ||||||||||||||||||||||||||||
| 670 | #pod my $result = $rs->create({bar => 12}); | ||||||||||||||||||||||||||||
| 671 | #pod $result->update({baz => 42}); | ||||||||||||||||||||||||||||
| 672 | #pod }); | ||||||||||||||||||||||||||||
| 673 | #pod | ||||||||||||||||||||||||||||
| 674 | #pod # Still has retry protection | ||||||||||||||||||||||||||||
| 675 | #pod $rs->delete; | ||||||||||||||||||||||||||||
| 676 | #pod | ||||||||||||||||||||||||||||
| 677 | #pod All of this behavior mimics how DBIC's original storage engines work. | ||||||||||||||||||||||||||||
| 678 | #pod | ||||||||||||||||||||||||||||
| 679 | #pod =head2 (Ab)using $dbh directly | ||||||||||||||||||||||||||||
| 680 | #pod | ||||||||||||||||||||||||||||
| 681 | #pod Similar to C<txn_begin>, directly accessing and using a DBI database or statement handle | ||||||||||||||||||||||||||||
| 682 | #pod does NOT grant retry protection, even if they are acquired from the storage engine via | ||||||||||||||||||||||||||||
| 683 | #pod C<< $storage->dbh >>. | ||||||||||||||||||||||||||||
| 684 | #pod | ||||||||||||||||||||||||||||
| 685 | #pod Instead, use L</dbh_do>. This method is also used by DBIC for most of its active DB | ||||||||||||||||||||||||||||
| 686 | #pod calls, after it has composed a proper SQL statement to run. | ||||||||||||||||||||||||||||
| 687 | #pod | ||||||||||||||||||||||||||||
| 688 | #pod =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 689 | #pod | ||||||||||||||||||||||||||||
| 690 | #pod L<DBIx::Connector::Retry::MySQL> - A similar engine for DBI connections, using L<DBIx::Connector::Retry> as a base. | ||||||||||||||||||||||||||||
| 691 | #pod | ||||||||||||||||||||||||||||
| 692 | #pod L<DBIx::Class::Storage::BlockRunner> - Base module in DBIC that controls how transactional coderefs are ran and retried | ||||||||||||||||||||||||||||
| 693 | #pod | ||||||||||||||||||||||||||||
| 694 | #pod =cut | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | 3 | 1; | |||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | __END__ | ||||||||||||||||||||||||||||