blib/lib/DBIO/Storage/BlockRunner.pm

filter: Statementstmt 90.9% 101 / 111 Branchbran 66.6% 44 / 66 Conditioncond 58.9% 23 / 39 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 92.5% 25 / 27 Podpod 90.9% 10 / 11 totaltotal 75.8% 210 / 277 CC 49 SCAR 27.5
CC
49
Cov
78%
__ANON__
35.1
new
17.6
run
7.5
CRAP 15.6
« blib/lib/DBIO/PopulateMore.pm ↑ Summary blib/lib/DBIO/Storage/DBI.pm »
1 package DBIO::Storage::BlockRunner;
2 # ABSTRACT: Execute code blocks with transaction wrapping and retry logic
3  
4 345 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 345 our $VERSION = '0.900007';
7  
8 345 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 345 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 345 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 345 ▶use Context::Preserve 'preserve_context';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 345 ▶use DBIO::Util qw(is_exception qsub);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 345 ▶use Scalar::Util qw(weaken blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 345 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 345 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 204 ▶sub new {
Subroutine new: called CC=10 SCAR=28.7
Pod undocumented
20 204 my $class = shift;
21 0 ▶ my %args = @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 204 204
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
20400
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
22  
23 204 for my $required (qw(storage wrap_txn retry_handler)) {
24 DBIO::Exception->throw("Missing required attribute '$required'")
25 612 ▶ unless exists $args{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$required} 0 612 612
26 }
27  
28 # isa check for retry_handler
29 204 ▶ (Scalar::Util::reftype($args{retry_handler})||'') eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($args{"retry_handler"}) || "") eq "CODE" or "DBIO::Exception"->throw("retry_handler must be a CODE reference") 204 0 204
Condition50%1/2
builtin::reftype($args{'retry_handler'}) || ''
l!l
2040
MC/DC0%0/1
builtin::reftype($args{'retry_handler'}) || ''
builtin::reftype($args{'retry_handler'})
Truth table50%1/2
A
A= builtin::reftype($args{'retry_handler'})
Aresult
11
00
30 or DBIO::Exception->throw('retry_handler must be a CODE reference');
31  
32 my $self = bless {
33 storage => $args{storage},
34 wrap_txn => $args{wrap_txn},
35 retry_handler => $args{retry_handler},
36 max_attempts => exists $args{max_attempts} ? $args{max_attempts} : 20,
37 204 ▶ ( exists $args{retry_debug} ? ( retry_debug => $args{retry_debug} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'max_attempts'} ? : 0 204 204
exists $args{'retry_debug'} ? : 0 204 204
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
02040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
38 # failed_attempt_count and exception_stack have init_arg => undef
39 # so they are never accepted from constructor args
40 }, ref $class || $class;
41  
42 204 return $self;
43 }
44  
45 713 ▶sub storage { $_[0]->{storage} }
Subroutine storage: called CC=1 SCAR=0.0
Pod documented
46  
47 216 ▶sub wrap_txn { $_[0]->{wrap_txn} }
Subroutine wrap_txn: called CC=1 SCAR=0.0
Pod documented
48  
49 # true - retry, false - rethrow, or you can throw your own (not catching)
50 72 ▶sub retry_handler { $_[0]->{retry_handler} }
Subroutine retry_handler: called CC=1 SCAR=0.0
Pod documented
51  
52 22 ▶sub retry_debug {
Subroutine retry_debug: called CC=3 SCAR=13.3
Pod documented
53 22 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 22 22
54 0 $_[0]->{retry_debug} = $_[1];
55 0 return $_[1];
56 }
57 # lazy default
58 $_[0]->{retry_debug} = $ENV{DBIO_STORAGE_RETRY_DEBUG}
59 22 ▶ unless exists $_[0]->{retry_debug};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"retry_debug"} 22 0 22
60 22 return $_[0]->{retry_debug};
61 }
62  
63 276 ▶sub max_attempts { $_[0]->{max_attempts} }
Subroutine max_attempts: called CC=1 SCAR=0.0
Pod documented
64  
65 144 ▶sub failed_attempt_count {
Subroutine failed_attempt_count: called CC=2 SCAR=7.2
Pod documented
66 # lazy default
67 $_[0]->{failed_attempt_count} = 0
68 144 ▶ unless exists $_[0]->{failed_attempt_count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"failed_attempt_count"} 0 144 144
69 144 return $_[0]->{failed_attempt_count};
70 }
71  
72 276 ▶sub _set_failed_attempt_count {
Subroutine _set_failed_attempt_count: called CC=3 SCAR=11.1
73 276 $_[0]->{failed_attempt_count} = $_[1];
74 # trigger
75 276 ▶ $_[0]->throw_exception( sprintf (
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->max_attempts <= ($_[1] || 0) 0 276 276
Condition100%2/2
$_[1] || 0
l!l
72204
MC/DC100%1/1
$_[1] || 0
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
76 'Reached max_attempts amount of %d, latest exception: %s',
77 $_[0]->max_attempts, $_[0]->last_exception
78 )) if $_[0]->max_attempts <= ($_[1]||0);
79 276 return $_[1];
80 }
81  
82 72 ▶sub exception_stack {
Subroutine exception_stack: called CC=2 SCAR=7.2
Pod documented
83 # lazy default
84 $_[0]->{exception_stack} = []
85 72 ▶ unless exists $_[0]->{exception_stack};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"exception_stack"} 72 0 72
86 72 return $_[0]->{exception_stack};
87 }
88  
89 204 ▶sub _reset_exception_stack {
Subroutine _reset_exception_stack: called CC=1 SCAR=0.0
90 204 delete $_[0]->{exception_stack};
91 }
92  
93 0 ▶sub last_exception { shift->exception_stack->[-1] }
Subroutine last_exception: not called CC=1 SCAR=6.9
Pod documented
94  
95 0 ▶sub throw_exception { shift->storage->throw_exception (@_) }
Subroutine throw_exception: not called CC=1 SCAR=6.9
Pod documented
96  
97 204 ▶sub run {
Subroutine run: called CC=7 SCAR=20.2
Pod documented
98 204 my $self = shift;
99  
100 204 $self->_reset_exception_stack;
101 204 $self->_set_failed_attempt_count(0);
102  
103 204 my $cref = shift;
104  
105 204 ▶ $self->throw_exception('run() requires a coderef to execute as its first argument')
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($cref) || "") ne "CODE" 0 204 204
Condition50%1/2
builtin::reftype($cref) || ''
l!l
2040
MC/DC0%0/1
builtin::reftype($cref) || ''
builtin::reftype($cref)
Truth table50%1/2
A
A= builtin::reftype($cref)
Aresult
11
00
106 if ( reftype($cref)||'' ) ne 'CODE';
107  
108 204 my $storage = $self->storage;
109  
110 # Don't get into the tangle of code below if we already know the storage is
111 # trying to rollback.
112 204 ▶ $storage->_throw_deferred_rollback if $storage->deferred_rollback;
Branch: 1 branch
BranchTrueFalseTotal
if $storage->deferred_rollback 0 204 204
113  
114 return $cref->( @_ ) if (
115 $storage->{_in_do_block}
116 and
117 204 ▶ ! $self->wrap_txn
Branch: 1 branch
BranchTrueFalseTotal
if $storage->{"_in_do_block"} and not $self->wrap_txn 0 204 204
Condition66%2/3
$storage->{"_in_do_block"} and not $self->wrap_txn
!ll&&!rl&&r
19950
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
118 );
119  
120 204 ▶ local $storage->{_in_do_block} = 1 unless $storage->{_in_do_block};
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->{"_in_do_block"} 199 5 204
121  
122 204 return $self->_run($cref, @_);
123 }
124  
125 # this is the actual recursing worker
126 211 ▶sub _run {
Subroutine _run: called CC=3 SCAR=11.3
127 # internal method - we know that both refs are strong-held by the
128 # calling scope of run(), hence safe to weaken everything
129 211 weaken( my $self = shift );
130 211 weaken( my $cref = shift );
131  
132 211 ▶ my $args = @_ ? \@_ : [];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 211 211
133  
134 # from this point on (defined $txn_init_depth) is an indicator for wrap_txn
135 # save a bit on method calls
136 211 ▶ my $txn_init_depth = $self->wrap_txn ? $self->storage->transaction_depth : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->wrap_txn ? : 113 98 211
137 211 my $txn_begin_ok;
138  
139 211 my $run_err = '';
140  
141 211 ▶ return preserve_context {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 211 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
143 211 ▶ if (defined $txn_init_depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $txn_init_depth) 113 98 211
144 113 $self->storage->txn_begin;
145 109 $txn_begin_ok = 1;
146 }
147 207 $cref->( @$args );
148 70 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
149 70 $run_err = $_;
150 70 (); # important, affects @_ below
151 211 };
152 211 ▶ } replace => sub {
Subroutine __ANON__: called CC=25 SCAR=35.6
153 211 my @res = @_;
154  
155 211 my $storage = $self->storage;
156 211 my $cur_depth = $storage->transaction_depth;
157  
158 211 ▶ 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)) 68 143 211
Condition100%3/3
defined $txn_init_depth and not &is_exception($run_err)
!ll&&!rl&&r
984568
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
159 68 my $delta_txn = (1 + $txn_init_depth) - $cur_depth;
160  
161 68 ▶ if ($delta_txn) {
Branch: 2 branches
BranchTrueFalseTotal
if ($delta_txn) { } 0 68 68
elsif ($storage->deferred_rollback) { } 0 68 68
162 # a rollback in a top-level txn_do is valid-ish (seen in the wild and our own tests)
163 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
164 'Unexpected reduction of transaction depth by %d after execution of '
165 . '%s, skipping txn_commit()',
166 $delta_txn,
167 $cref,
168 ) unless $delta_txn == 1 and $cur_depth == 0;
169 }
170 elsif ($storage->deferred_rollback) {
171 # This means the inner code called 'rollback' in a case where savepoints
172 # weren't enabled, and then caught the exception.
173 0 carp 'A deferred rollback is in effect, but you exited a transaction-wrapped '
174 . 'block cleanly which normally implies "commit". '
175 . "You're getting a rollback instead.";
176 # perform rollback via the error path below
177 0 ▶ $run_err = eval { $storage->txn_rollback; 1 } ? '' : $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $storage->txn_rollback; 1 } } ? : 0 0 0
178 }
179 else {
180 68 ▶ $run_err = eval { $storage->txn_commit; 1 } ? '' : $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $storage->txn_commit; 1 } } ? : 66 2 68
181 }
182 }
183  
184 # something above threw an error (could be the begin, the code or the commit)
185 211 ▶ if ( is_exception $run_err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_exception($run_err)) 72 139 211
186  
187 # roll back the transaction this runner began while it is still open; a
188 # failed commit of the outermost transaction has ended it already, and
189 # its error reaches the caller as it is (karr k372)
190 72 ▶ if ($txn_begin_ok and $storage->transaction_depth > $txn_init_depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txn_begin_ok and $storage->transaction_depth > $txn_init_depth) 41 31 72
Condition100%3/3
$txn_begin_ok and $storage->transaction_depth > $txn_init_depth
!ll&&!rl&&r
29241
MC/DC100%2/2
$txn_begin_ok and $storage->transaction_depth > $txn_init_depth
$txn_begin_ok $storage->transaction_depth > $txn_init_depth
Truth table100%3/3
A and B
A= $txn_begin_okB= $storage->transaction_depth > $txn_init_depth
ABresult
0X0
100
111
191 # some DBDs go crazy if there is nothing to roll back on, perform a soft-check
192 my $rollback_exception = $storage->_seems_connected
193 41 ▶ ? (! eval { $storage->txn_rollback; 1 }) ? $@ : ''
Branch: 2 branches
BranchTrueFalseTotal
$storage->_seems_connected ? : 39 2 41
!eval { do { $storage->txn_rollback; 1 } } ? : 1 38 39
194 : 'lost connection to storage'
195 ;
196  
197 41 ▶ if ( $rollback_exception and (
Branch: 1 branch
BranchTrueFalseTotal
if ($rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIO::Storage::NESTED_ROLLBACK_EXCEPTION")) 3 38 41
Condition50%3/6
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIO::Storage::NESTED_ROLLBACK_EXCEPTION")
!ll&&!rl&&r
3803
defined builtin::blessed($rollback_exception) && $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
!ll&&!rl&&r
300
MC/DC33%1/3
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIO::Storage::NESTED_ROLLBACK_EXCEPTION")
$rollback_exception defined builtin::blessed($rollback_exception) $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
Truth table50%2/4
A and not(B && C)
A= $rollback_exceptionB= defined builtin::blessed($rollback_exception)C= $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
ABCresult
0XX0
1110
10X1
1101
198 ! defined blessed $rollback_exception
199 or
200 ! $rollback_exception->isa('DBIO::Storage::NESTED_ROLLBACK_EXCEPTION')
201 ) ) {
202 3 $run_err = "Transaction aborted: $run_err. Rollback failed: $rollback_exception";
203 }
204 }
205  
206 72 push @{ $self->exception_stack }, $run_err;
207  
208 # this will throw if max_attempts is reached
209 72 $self->_set_failed_attempt_count($self->failed_attempt_count + 1);
210  
211 # init depth of > 0 ( > 1 with AC) implies nesting - no retry attempt queries
212 $storage->throw_exception($run_err) if (
213 (
214 defined $txn_init_depth
215 and
216 # FIXME - we assume that $storage->{_dbh_autocommit} is there if
217 # txn_init_depth is there, but this is a DBI-ism
218 72 ▶ $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) 50 22 72
$storage->{'_dbh_autocommit'} ? : 2 45 47
Condition66%4/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
05022
defined $txn_init_depth and $txn_init_depth > ($storage->{"_dbh_autocommit"} ? 0 : 1)
!ll&&!rl&&r
25470
MC/DC33%1/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 table80%4/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
219 ) or ! $self->retry_handler->($self)
220 );
221  
222 # we got that far - let's retry
223 22 ▶ carp( sprintf 'Retrying %s (attempt %d) after caught exception: %s',
Branch: 1 branch
BranchTrueFalseTotal
if $self->retry_debug 0 22 22
224 $cref,
225 $self->failed_attempt_count + 1,
226 $run_err,
227 ) if $self->retry_debug;
228  
229 22 $storage->_abandon_handle_before_reconnect;
230 22 $storage->ensure_connected;
231 # if txn_depth is > 1 this means something was done to the
232 # original $dbh, otherwise we would not get past the preceding if()
233 7 ▶ $storage->throw_exception(sprintf
Branch: 1 branch
BranchTrueFalseTotal
if defined $txn_init_depth and $storage->transaction_depth 0 7 7
Condition66%2/3
defined $txn_init_depth and $storage->transaction_depth
!ll&&!rl&&r
520
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
234 'Unexpected transaction depth of %d on freshly connected handle',
235 $storage->transaction_depth,
236 ) if (defined $txn_init_depth and $storage->transaction_depth);
237  
238 7 return $self->_run($cref, @$args);
239 }
240  
241 139 ▶ return wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 59 80 139
242 211 };
243 }
244  
245  
246 345 1;
247  
248 __END__
« blib/lib/DBIO/PopulateMore.pm ↑ Summary blib/lib/DBIO/Storage/DBI.pm »