DBIx/Class/Storage/BlockRunner.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 80.0% 32 / 40 Conditioncond 76.9% 20 / 26 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 3 totaltotal 86.0% 166 / 193 CC 30 SCAR 25.5
CC
30
Cov
90%
__ANON__
24.5
run
6.0
_run
3.0
CRAP 12.8
« DBIx/Class/CDBICompat/Retrieve.pm ↑ Summary DBIx/Class/SQLMaker/LimitDialects.pm »
1 package # hide from pause until we figure it all out
2 DBIx::Class::Storage::BlockRunner;
3  
4 251 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 251 ▶use DBIx::Class::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 251 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 251 ▶use Context::Preserve 'preserve_context';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 251 ▶use DBIx::Class::_Util 'is_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 251 ▶use Scalar::Util qw(weaken blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 251 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # DO NOT edit away without talking to riba first, he will just put it back
14 BEGIN {
15 251 ▶ local $ENV{PERL_STRICTURES_EXTRA} = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 251 require Moo; Moo->import;
17 251 require Sub::Quote; Sub::Quote->import('quote_sub');
18 }
19 251 ▶use warnings NONFATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 251 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =head1 NAME
23  
24 DBIx::Class::Storage::BlockRunner - Try running a block of code until success with a configurable retry logic
25  
26 =head1 DESCRIPTION
27  
28 =head1 METHODS
29  
30 =cut
31  
32 251 has storage => (
33 is => 'ro',
34 required => 1,
35 );
36  
37 251 has wrap_txn => (
38 is => 'ro',
39 required => 1,
40 );
41  
42 # true - retry, false - rethrow, or you can throw your own (not catching)
43 251 has retry_handler => (
44 is => 'ro',
45 required => 1,
46 isa => quote_sub( q{
47 (Scalar::Util::reftype($_[0])||'') eq 'CODE'
48 or DBIx::Class::Exception->throw('retry_handler must be a CODE reference')
49 }),
50 );
51  
52 251 has retry_debug => (
53 is => 'rw',
54 # use a sub - to be evaluated on the spot lazily
55 default => quote_sub( '$ENV{DBIC_STORAGE_RETRY_DEBUG}' ),
56 lazy => 1,
57 );
58  
59 251 has max_attempts => (
60 is => 'ro',
61 default => 20,
62 );
63  
64 251 has failed_attempt_count => (
65 is => 'ro',
66 init_arg => undef, # ensures one can't pass the value in
67 writer => '_set_failed_attempt_count',
68 default => 0,
69 lazy => 1,
70 trigger => quote_sub(q{
71 $_[0]->throw_exception( sprintf (
72 'Reached max_attempts amount of %d, latest exception: %s',
73 $_[0]->max_attempts, $_[0]->last_exception
74 )) if $_[0]->max_attempts <= ($_[1]||0);
75 }),
76 );
77  
78 251 has exception_stack => (
79 is => 'ro',
80 init_arg => undef,
81 clearer => '_reset_exception_stack',
82 default => quote_sub(q{ [] }),
83 lazy => 1,
84 );
85  
86 0 ▶sub last_exception { shift->exception_stack->[-1] }
Subroutine last_exception: not called CC=1 SCAR=6.9
Pod undocumented
87  
88 24 ▶sub throw_exception { shift->storage->throw_exception (@_) }
Subroutine throw_exception: called CC=1 SCAR=0.0
Pod undocumented
89  
90 sub run {
91 43435 ▶ my $self = shift;
Subroutine run: called CC=6 SCAR=18.0
Pod undocumented
92  
93 43435 $self->_reset_exception_stack;
94 43435 $self->_set_failed_attempt_count(0);
95  
96 43435 my $cref = shift;
97  
98 43435 ▶ $self->throw_exception('run() requires a coderef to execute as its first argument')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($cref) || "") ne "CODE" 24 43411 43435
Condition100%2/2
builtin::reftype($cref) || ''
l!l
4341124
MC/DC100%1/1
builtin::reftype($cref) || ''
builtin::reftype($cref)
Truth table100%2/2
A
A= builtin::reftype($cref)
Aresult
11
00
99 if ( reftype($cref)||'' ) ne 'CODE';
100  
101 43411 my $storage = $self->storage;
102  
103 return $cref->( @_ ) if (
104 $storage->{_in_do_block}
105 and
106 43411 ▶ ! $self->wrap_txn
Branch: 1 branch
BranchTrueFalseTotal
if $storage->{"_in_do_block"} and not $self->wrap_txn 0 43411 43411
Condition66%2/3
$storage->{"_in_do_block"} and not $self->wrap_txn
!ll&&!rl&&r
43352590
MC/DC0%0/2
$storage->{"_in_do_block"} and not $self->wrap_txn
$storage->{"_in_do_block"} not $self->wrap_txn
Truth table66%2/3
A and not(B)
A= $storage->{"_in_do_block"}B= not $self->wrap_txn
ABresult
0X0
100
111
107 );
108  
109 43411 ▶ local $storage->{_in_do_block} = 1 unless $storage->{_in_do_block};
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->{"_in_do_block"} 43352 59 43411
110  
111 43411 return $self->_run($cref, @_);
112 }
113  
114 # this is the actual recursing worker
115 sub _run {
116 # internal method - we know that both refs are strong-held by the
117 # calling scope of run(), hence safe to weaken everything
118 43422 ▶ weaken( my $self = shift );
Subroutine _run: called CC=3 SCAR=11.1
119 43422 weaken( my $cref = shift );
120  
121 43422 ▶ my $args = @_ ? \@_ : [];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 165 43257 43422
122  
123 # from this point on (defined $txn_init_depth) is an indicator for wrap_txn
124 # save a bit on method calls
125 43422 ▶ my $txn_init_depth = $self->wrap_txn ? $self->storage->transaction_depth : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->wrap_txn ? : 445 42977 43422
126 43422 my $txn_begin_ok;
127  
128 43422 my $run_err = '';
129  
130 return preserve_context {
131 try {
132 43422 ▶ if (defined $txn_init_depth) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (defined $txn_init_depth) 445 42977 43422
133 445 $self->storage->txn_begin;
134 445 $txn_begin_ok = 1;
135 }
136 43422 $cref->( @$args );
137 } catch {
138 148 ▶ $run_err = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
139 148 (); # important, affects @_ below
140 43422 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
141 } replace => sub {
142 43411 ▶ my @res = @_;
Subroutine __ANON__: called CC=22 SCAR=32.0
143  
144 43411 my $storage = $self->storage;
145 43411 my $cur_depth = $storage->transaction_depth;
146  
147 43411 ▶ if (defined $txn_init_depth and $run_err eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $txn_init_depth and $run_err eq "") 308 43103 43411
Condition100%3/3
defined $txn_init_depth and $run_err eq ""
!ll&&!rl&&r
42977126308
MC/DC100%2/2
defined $txn_init_depth and $run_err eq ""
defined $txn_init_depth $run_err eq ""
Truth table100%3/3
A and B
A= defined $txn_init_depthB= $run_err eq ""
ABresult
0X0
100
111
148 308 my $delta_txn = (1 + $txn_init_depth) - $cur_depth;
149  
150 308 ▶ if ($delta_txn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta_txn) { } 0 308 308
151 # a rollback in a top-level txn_do is valid-ish (seen in the wild and our own tests)
152 0 ▶ carp (sprintf
Branch: 1 branch
BranchTrueFalseTotal
unless $delta_txn == 1 and $cur_depth == 0 0 0 0
Condition0%0/3
$delta_txn == 1 and $cur_depth == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$delta_txn == 1 and $cur_depth == 0
$delta_txn == 1 $cur_depth == 0
Truth table0%0/3
A and B
A= $delta_txn == 1B= $cur_depth == 0
ABresult
0X0
100
111
153 'Unexpected reduction of transaction depth by %d after execution of '
154 . '%s, skipping txn_commit()',
155 $delta_txn,
156 $cref,
157 ) unless $delta_txn == 1 and $cur_depth == 0;
158 }
159 else {
160 308 ▶ $run_err = eval { $storage->txn_commit; 1 } ? '' : $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $storage->txn_commit; 1 } } ? : 308 0 308
161 }
162 }
163  
164 # something above threw an error (could be the begin, the code or the commit)
165 43411 ▶ if ( is_exception $run_err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_exception($run_err)) 148 43263 43411
166  
167 # attempt a rollback if we did begin in the first place
168 148 ▶ if ($txn_begin_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn_begin_ok) 126 22 148
169 # some DBDs go crazy if there is nothing to roll back on, perform a soft-check
170 my $rollback_exception = $storage->_seems_connected
171 126 ▶ ? (! eval { $storage->txn_rollback; 1 }) ? $@ : ''
Branch: 2 branches
BranchTrueFalseTotal
$storage->_seems_connected ? : 119 7 126
!eval { do { $storage->txn_rollback; 1 } } ? : 111 8 119
172 : 'lost connection to storage'
173 ;
174  
175 126 ▶ if ( $rollback_exception and (
Branch: 1 branch
BranchTrueFalseTotal
if ($rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")) 8 118 126
Condition83%5/6
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
!ll&&!rl&&r
81108
defined builtin::blessed($rollback_exception) && $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
!ll&&!rl&&r
80110
MC/DC66%2/3
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
$rollback_exception defined builtin::blessed($rollback_exception) $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
Truth table75%3/4
A and not(B && C)
A= $rollback_exceptionB= defined builtin::blessed($rollback_exception)C= $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
ABCresult
0XX0
1110
10X1
1101
176 ! defined blessed $rollback_exception
177 or
178 ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
179 ) ) {
180 8 $run_err = "Transaction aborted: $run_err. Rollback failed: $rollback_exception";
181 }
182 }
183  
184 148 push @{ $self->exception_stack }, $run_err;
185  
186 # this will throw if max_attempts is reached
187 148 $self->_set_failed_attempt_count($self->failed_attempt_count + 1);
188  
189 # init depth of > 0 ( > 1 with AC) implies nesting - no retry attempt queries
190 $storage->throw_exception($run_err) if (
191 (
192 defined $txn_init_depth
193 and
194 # FIXME - we assume that $storage->{_dbh_autocommit} is there if
195 # txn_init_depth is there, but this is a DBI-ism
196 148 ▶ $txn_init_depth > ( $storage->{_dbh_autocommit} ? 0 : 1 )
Branch: 2 branches
BranchTrueFalseTotal
if defined $txn_init_depth and $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1) or not $self->retry_handler->($self) 136 12 148
$storage->{'_dbh_autocommit'} ? : 126 0 126
Condition100%6/6
defined $txn_init_depth and $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1) or not $self->retry_handler->($self)
l!l&&r!l&&!r
1112512
defined $txn_init_depth and $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1)
!ll&&!rl&&r
2215111
MC/DC100%3/3
defined $txn_init_depth and $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1) or not $self->retry_handler->($self)
defined $txn_init_depth $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1) not $self->retry_handler->($self)
Truth table100%5/5
A and B or not(C)
A= defined $txn_init_depthB= $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1)C= not $self->retry_handler->($self)
ABCresult
11X1
0X11
1011
0X00
1000
197 ) or ! $self->retry_handler->($self)
198 );
199  
200 # we got that far - let's retry
201 12 ▶ carp( sprintf 'Retrying %s (attempt %d) after caught exception: %s',
Branch: 1 branch
BranchTrueFalseTotal
if $self->retry_debug 0 12 12
202 $cref,
203 $self->failed_attempt_count + 1,
204 $run_err,
205 ) if $self->retry_debug;
206  
207 12 $storage->ensure_connected;
208 # if txn_depth is > 1 this means something was done to the
209 # original $dbh, otherwise we would not get past the preceding if()
210 11 ▶ $storage->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if defined $txn_init_depth and $storage->transaction_depth 0 11 11
Condition66%2/3
defined $txn_init_depth and $storage->transaction_depth
!ll&&!rl&&r
470
MC/DC0%0/2
defined $txn_init_depth and $storage->transaction_depth
defined $txn_init_depth $storage->transaction_depth
Truth table66%2/3
A and B
A= defined $txn_init_depthB= $storage->transaction_depth
ABresult
0X0
100
111
211 'Unexpected transaction depth of %d on freshly connected handle',
212 $storage->transaction_depth,
213 ) if (defined $txn_init_depth and $storage->transaction_depth);
214  
215 11 return $self->_run($cref, @$args);
216 }
217  
218 43263 ▶ return wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6633 36630 43263
219 43422 };
220 }
221  
222 =head1 AUTHOR AND CONTRIBUTORS
223  
224 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
225  
226 =head1 LICENSE
227  
228 You may distribute this code under the same terms as Perl itself.
229  
230 =cut
231  
232 251 1;
« DBIx/Class/CDBICompat/Retrieve.pm ↑ Summary DBIx/Class/SQLMaker/LimitDialects.pm »