blib/lib/DBIx/TxnPool.pm

filter: Statementstmt 19.1% 28 / 146 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 20.0% 8 / 40 Podpod 61.5% 8 / 13 totaltotal 16.0% 44 / 275 SCAR 67.3
CC
35
Cov
13%
finish
44.1
new
42.0
_check_deadlock
30.0
CRAP 834.2
↑ Summary
1 package DBIx::TxnPool;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter 5.57 qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Signal::Mask;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = 0.12;
12 1 our $BlockSignals = [ qw( TERM INT ) ];
13 1 our @EXPORT = qw( txn_item txn_post_item txn_commit txn_sort );
14  
15 # It's better to look for the "try restarting transaction" string
16 # because sometime may be happens other error: Lock wait timeout exceeded
17 1 ▶use constant DEADLOCK_REGEXP => qr/try restarting transaction/o;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub new {
20 0 ▶ my ( $class, %args ) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
21  
22 confess( "The dbh should be defined" )
23 0 ▶ unless $args{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dbh"} 0 0 0
24  
25 0 ▶ $args{size} ||= 100;
Condition0%0/2
$args{"size"} ||= 100
l!l
00
MC/DC0%0/1
$args{"size"} ||= 100
$args{"size"}
Truth table0%0/2
A
A= $args{"size"}
Aresult
11
00
26 0 ▶ $args{block_signals} ||= $BlockSignals;
Condition0%0/3
$args{"block_signals"} ||= $DBIx::TxnPool::BlockSignals
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"block_signals"} ||= $DBIx::TxnPool::BlockSignals
$args{"block_signals"} $DBIx::TxnPool::BlockSignals
Truth table0%0/3
A ||= B
A= $args{"block_signals"}B= $DBIx::TxnPool::BlockSignals
ABresult
1X1
011
000
27 0 ▶ $args{max_repeated_deadlocks} ||= 5;
Condition0%0/2
$args{"max_repeated_deadlocks"} ||= 5
l!l
00
MC/DC0%0/1
$args{"max_repeated_deadlocks"} ||= 5
$args{"max_repeated_deadlocks"}
Truth table0%0/2
A
A= $args{"max_repeated_deadlocks"}
Aresult
11
00
28 0 $args{_amnt_nested_signals} = 0;
29 0 $args{_saved_signal_masks} = {};
30 0 $args{pool} = [];
31 0 $args{amount_deadlocks} = 0;
32  
33 0 $args{repeated_deadlocks} = 0;
34  
35 0 ▶ bless \%args, ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
36 }
37  
38 sub DESTROY {
39 0 ▶ local $@;
Subroutine DESTROY: not called CC=2 SCAR=17.9
40  
41 $_[0]->finish
42 0 ▶ unless ( $_[0]->{repeated_deadlocks} >= $_[0]->{max_repeated_deadlocks} );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"repeated_deadlocks"} >= $_[0]{"max_repeated_deadlocks"} 0 0 0
43 }
44  
45 sub txn_item (&@) {
46 0 ▶ __PACKAGE__->new( %{ __make_chain( 'item_callback', @_ ) } );
Subroutine txn_item: not called CC=1 SCAR=6.9
Pod documented
47 }
48  
49 sub txn_post_item (&@) {
50 0 ▶ __make_chain( 'post_item_callback', @_ );
Subroutine txn_post_item: not called CC=1 SCAR=6.9
Pod documented
51 }
52  
53 sub txn_commit (&@) {
54 0 ▶ __make_chain( 'commit_callback', @_ );
Subroutine txn_commit: not called CC=1 SCAR=6.9
Pod documented
55 }
56  
57 sub txn_sort (&@) {
58 0 ▶ my $ret = __make_chain( 'sort_callback', @_ );
Subroutine txn_sort: not called CC=1 SCAR=6.9
Pod documented
59 0 $ret->{sort_callback_package} = caller;
60 0 $ret;
61 }
62  
63 sub __make_chain {
64 0 ▶ my $cb_name = shift;
Subroutine __make_chain: not called CC=2 SCAR=17.9
65 0 my $cb_func = shift;
66 0 my $ret;
67  
68 0 ▶ ( $ret = ref $_[0] eq 'HASH' ? $_[0] : { @_ } )->{ $cb_name } = $cb_func;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
69 0 $ret;
70 }
71  
72 0 ▶sub dbh { $_[0]->{dbh} }
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
73  
74 sub add {
75 0 ▶ my ( $self, $data ) = @_;
Subroutine add: not called CC=4 SCAR=30.0
Pod documented
76  
77 confess "assert: _amnt_nested_signals is not zero!"
78 0 ▶ if $self->{_amnt_nested_signals};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_amnt_nested_signals"} 0 0 0
79  
80 try {
81 0 ▶ push @{ $self->{pool} }, $data;
Subroutine __ANON__: not called CC=2 SCAR=17.9
82  
83 0 ▶ if ( ! $self->{sort_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"sort_callback"}) 0 0 0
84 0 $self->start_txn;
85 $self->_safe_signals( sub {
86 0 ▶ local $_ = $data;
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $self->{item_callback}->( $self, $data );
88 0 } );
89 }
90 }
91 catch {
92 0 ▶ $self->_check_deadlock( $_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 };
94  
95 $self->finish
96 0 ▶ if ( @{ $self->{pool} } >= $self->{size} );
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"pool"};} >= $self->{"size"} 0 0 0
97  
98 confess "assert: _amnt_nested_signals is not zero!"
99 0 ▶ if $self->{_amnt_nested_signals};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_amnt_nested_signals"} 0 0 0
100 }
101  
102 sub _check_deadlock {
103 0 ▶ my ( $self, $error ) = @_;
Subroutine _check_deadlock: not called CC=5 SCAR=34.0
104  
105 0 my $dbi_error = $DBI::err;
106  
107 0 $self->rollback_txn;
108  
109 # For example codes: https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
110 # MySQL codes 1213 & 1205 are reasons to redo transaction again
111 # For other SQL engines i don't know codes - patches are walcome! ;-) [https://github.com/Perlover/DBIx-TxnPool]
112 0 ▶ if ( defined $dbi_error && ( $dbi_error == 1213 || $dbi_error == 1205 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbi_error and $dbi_error == 1213 || $dbi_error == 1205) { } 0 0 0
Condition0%0/6
defined $dbi_error and $dbi_error == 1213 || $dbi_error == 1205
!ll&&!rl&&r
000
$dbi_error == 1213 or $dbi_error == 1205
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $dbi_error and $dbi_error == 1213 || $dbi_error == 1205
defined $dbi_error $dbi_error == 1213 $dbi_error == 1205
Truth table0%0/4
A and B or C
A= defined $dbi_errorB= $dbi_error == 1213C= $dbi_error == 1205
ABCresult
0XX0
1000
11X1
1011
113 0 $self->{amount_deadlocks}++;
114 0 ▶ if ( $self->{repeated_deadlocks} >= $self->{max_repeated_deadlocks} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'repeated_deadlocks'} >= $self->{'max_repeated_deadlocks'}) { } 0 0 0
115 0 $self->{pool} = []; # If DESTROY calls finish() there will not problems
116 0 confess( "limit ($self->{repeated_deadlocks}) of deadlock resolvings" )
117 }
118 else {
119 0 $self->play_pool;
120 }
121 } else {
122 # Fatal error - may be bad SQL statement - finish
123 0 $self->{pool} = []; # If DESTROY calls finish() there will not problems
124 0 confess( "error in item callback ($error)" );
125 }
126 }
127  
128 sub play_pool {
129 0 ▶ my $self = shift;
Subroutine play_pool: not called CC=1 SCAR=6.9
Pod undocumented
130  
131 0 $self->start_txn;
132  
133 $self->_safe_signals( sub {
134 0 ▶ select( undef, undef, undef, 0.5 * ++$self->{repeated_deadlocks} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 } );
136  
137 try {
138 0 ▶ foreach my $data ( @{ $self->{pool} } ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
139 $self->_safe_signals( sub {
140 0 ▶ local $_ = $data;
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 $self->{item_callback}->( $self, $data );
142 0 } );
143 }
144 }
145 catch {
146 0 ▶ $self->_check_deadlock( $_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 };
148 }
149  
150 sub finish {
151 0 ▶ my $self = shift;
Subroutine finish: not called CC=7 SCAR=37.9
Pod documented
152  
153 confess "assert: _amnt_nested_signals is not zero!"
154 0 ▶ if $self->{_amnt_nested_signals};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_amnt_nested_signals"} 0 0 0
155  
156 0 ▶ if ( $self->{sort_callback} && @{ $self->{pool} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sort_callback"} and @{$self->{"pool"};}) 0 0 0
Condition0%0/3
$self->{"sort_callback"} and @{$self->{"pool"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sort_callback"} and @{$self->{"pool"};}
$self->{"sort_callback"} @{$self->{"pool"};}
Truth table0%0/3
A and B
A= $self->{"sort_callback"}B= @{$self->{"pool"};}
ABresult
0X0
100
111
157 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
158 0 local *a = *{"$self->{sort_callback_package}\::a"};
159 0 local *b = *{"$self->{sort_callback_package}\::b"};
160  
161 0 $self->{pool} = [ sort { $self->{sort_callback}->() } ( @{ $self->{pool} } ) ];
162  
163 0 $self->play_pool;
164 }
165  
166 0 $self->commit_txn;
167  
168 0 ▶ if ( exists $self->{post_item_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"post_item_callback"}) 0 0 0
169 0 foreach my $data ( @{ $self->{pool} } ) {
170 0 local $_ = $data;
171 0 $self->{post_item_callback}->( $self, $data );
172 }
173 }
174  
175 0 $self->{pool} = [];
176  
177 confess "assert: _amnt_nested_signals is not zero!"
178 0 ▶ if $self->{_amnt_nested_signals};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_amnt_nested_signals"} 0 0 0
179 }
180  
181 sub start_txn {
182 0 ▶ my $self = shift;
Subroutine start_txn: not called CC=2 SCAR=17.9
Pod undocumented
183  
184 0 ▶ if ( ! $self->{in_txn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"in_txn"}) 0 0 0
185 $self->_safe_signals( sub {
186 0 ▶ $self->{dbh}->begin_work or confess 'DBI error: ' . $self->{dbh}->errstr;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->begin_work or confess('DBI error: ' . $self->{"dbh"}->errstr) 0 0 0
187 0 $self->{in_txn} = 1;
188 0 } );
189 }
190 }
191  
192 sub rollback_txn {
193 0 ▶ my $self = shift;
Subroutine rollback_txn: not called CC=2 SCAR=17.9
Pod undocumented
194  
195 0 ▶ if ( $self->{in_txn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_txn"}) 0 0 0
196 $self->_safe_signals( sub {
197 0 ▶ $self->{dbh}->rollback or confess 'DBI error: ' . $self->{dbh}->errstr;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->rollback or confess('DBI error: ' . $self->{"dbh"}->errstr) 0 0 0
198 0 $self->{in_txn} = undef;
199 0 } );
200 }
201 }
202  
203 sub commit_txn {
204 0 ▶ my $self = shift;
Subroutine commit_txn: not called CC=4 SCAR=30.0
Pod undocumented
205  
206 0 ▶ if ( $self->{in_txn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_txn"}) 0 0 0
207 try {
208 $self->_safe_signals( sub {
209 0 ▶ $self->{dbh}->commit or confess 'DBI error: ' . $self->{dbh}->errstr;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{"dbh"}->commit or confess('DBI error: ' . $self->{"dbh"}->errstr) 0 0 0
210 0 $self->{in_txn} = undef;
211 0 ▶ } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 1;
213 }
214 catch {
215 0 ▶ $self->_check_deadlock( $_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 0 $self->commit_txn;
217 0 0;
218 0 ▶ } or return;
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { $self->_safe_signals(sub { $self->{"dbh"}->commit or confess('DBI error: ' . $self->{"dbh"}->errstr); $self->{"in_txn"} = undef; } ); 1; } , &catch(sub { $self->_check_deadlock($_); $self->commit_txn; 0; } )) or return 0 0 0
219  
220 0 $self->{repeated_deadlocks} = 0;
221 $self->{commit_callback}->( $self )
222 0 ▶ if exists $self->{commit_callback};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"commit_callback"} 0 0 0
223 }
224 }
225  
226 0 ▶sub amount_deadlocks { $_[0]->{amount_deadlocks} }
Subroutine amount_deadlocks: not called CC=1 SCAR=6.9
Pod documented
227  
228 sub _safe_signals {
229 0 ▶ my ( $self, $code ) = @_;
Subroutine _safe_signals: not called CC=3 SCAR=24.8
230  
231 0 ▶ if ( ! $self->{_amnt_nested_signals}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_amnt_nested_signals"}++) 0 0 0
232 0 for ( @{ $self->{block_signals} } ) {
233 0 $self->{_saved_signal_masks}{ $_ } = $Signal::Mask{ $_ };
234 0 $Signal::Mask{ $_ } = 1;
235 }
236 }
237 try {
238 0 ▶ $code->();
Subroutine __ANON__: not called CC=1 SCAR=6.9
239 }
240 catch {
241 0 ▶ die $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 }
243 finally {
244 0 ▶ if ( ! --$self->{_amnt_nested_signals} ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (not --$self->{"_amnt_nested_signals"}) 0 0 0
245 0 for ( @{ $self->{block_signals} } ) {
246 0 $Signal::Mask{ $_ } = delete $self->{_saved_signal_masks}{ $_ };
247 }
248 }
249 0 };
250 }
251  
252 1 1;
253  
254 __END__
↑ Summary