DBIx/Class/Storage/BlockRunner.pm

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