blib/lib/DBIx/TxnPool.pm

filter: Statementstmt 19.5% 28 / 143 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 20.0% 8 / 40 Podpod 61.5% 8 / 13 totaltotal 16.3% 44 / 270 CC 34 SCAR 26.7
CC
34
Cov
14%
finish
45.0
new
42.0
_check_deadlock
30.0
CRAP 14.4
↑ 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.11;
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 ▶ 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
34 }
35  
36 sub DESTROY {
37 0 ▶ local $@;
Subroutine DESTROY: not called CC=2 SCAR=17.9
38  
39 $_[0]->finish
40 0 ▶ unless ( $_[0]->{repeated_deadlocks} >= $_[0]->{max_repeated_deadlocks} );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"repeated_deadlocks"} >= $_[0]{"max_repeated_deadlocks"} 0 0 0
41 }
42  
43 sub txn_item (&@) {
44 0 ▶ __PACKAGE__->new( %{ __make_chain( 'item_callback', @_ ) } );
Subroutine txn_item: not called CC=1 SCAR=6.9
Pod documented
45 }
46  
47 sub txn_post_item (&@) {
48 0 ▶ __make_chain( 'post_item_callback', @_ );
Subroutine txn_post_item: not called CC=1 SCAR=6.9
Pod documented
49 }
50  
51 sub txn_commit (&@) {
52 0 ▶ __make_chain( 'commit_callback', @_ );
Subroutine txn_commit: not called CC=1 SCAR=6.9
Pod documented
53 }
54  
55 sub txn_sort (&@) {
56 0 ▶ my $ret = __make_chain( 'sort_callback', @_ );
Subroutine txn_sort: not called CC=1 SCAR=6.9
Pod documented
57 0 $ret->{sort_callback_package} = caller;
58 0 $ret;
59 }
60  
61 sub __make_chain {
62 0 ▶ my $cb_name = shift;
Subroutine __make_chain: not called CC=2 SCAR=17.9
63 0 my $cb_func = shift;
64 0 my $ret;
65  
66 0 ▶ ( $ret = ref $_[0] eq 'HASH' ? $_[0] : { @_ } )->{ $cb_name } = $cb_func;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
67 0 $ret;
68 }
69  
70 0 ▶sub dbh { $_[0]->{dbh} }
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
71  
72 sub add {
73 0 ▶ my ( $self, $data ) = @_;
Subroutine add: not called CC=4 SCAR=30.0
Pod documented
74  
75 0 $self->{repeated_deadlocks} = 0;
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=38.1
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 try {
167 0 ▶ $self->{repeated_deadlocks} = 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 $self->commit_txn;
169 }
170 catch {
171 0 ▶ $self->_check_deadlock( $_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 };
173  
174 0 ▶ if ( exists $self->{post_item_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"post_item_callback"}) 0 0 0
175 0 foreach my $data ( @{ $self->{pool} } ) {
176 0 local $_ = $data;
177 0 $self->{post_item_callback}->( $self, $data );
178 }
179 }
180  
181 0 $self->{pool} = [];
182  
183 confess "assert: _amnt_nested_signals is not zero!"
184 0 ▶ if $self->{_amnt_nested_signals};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_amnt_nested_signals"} 0 0 0
185 }
186  
187 sub start_txn {
188 0 ▶ my $self = shift;
Subroutine start_txn: not called CC=2 SCAR=17.9
Pod undocumented
189  
190 0 ▶ if ( ! $self->{in_txn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"in_txn"}) 0 0 0
191 $self->_safe_signals( sub {
192 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
193 0 $self->{in_txn} = 1;
194 0 } );
195 }
196 }
197  
198 sub rollback_txn {
199 0 ▶ my $self = shift;
Subroutine rollback_txn: not called CC=2 SCAR=17.9
Pod undocumented
200  
201 0 ▶ if ( $self->{in_txn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_txn"}) 0 0 0
202 $self->_safe_signals( sub {
203 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
204 0 $self->{in_txn} = undef;
205 0 } );
206 }
207 }
208  
209 sub commit_txn {
210 0 ▶ my $self = shift;
Subroutine commit_txn: not called CC=3 SCAR=24.8
Pod undocumented
211  
212 0 ▶ if ( $self->{in_txn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_txn"}) 0 0 0
213 $self->_safe_signals( sub {
214 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
215 0 $self->{in_txn} = undef;
216 0 } );
217 $self->{commit_callback}->( $self )
218 0 ▶ if exists $self->{commit_callback};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"commit_callback"} 0 0 0
219 }
220 }
221  
222 0 ▶sub amount_deadlocks { $_[0]->{amount_deadlocks} }
Subroutine amount_deadlocks: not called CC=1 SCAR=6.9
Pod documented
223  
224 sub _safe_signals {
225 0 ▶ my ( $self, $code ) = @_;
Subroutine _safe_signals: not called CC=3 SCAR=24.8
226  
227 0 ▶ if ( ! $self->{_amnt_nested_signals}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_amnt_nested_signals"}++) 0 0 0
228 0 for ( @{ $self->{block_signals} } ) {
229 0 $self->{_saved_signal_masks}{ $_ } = $Signal::Mask{ $_ };
230 0 $Signal::Mask{ $_ } = 1;
231 }
232 }
233 try {
234 0 ▶ $code->();
Subroutine __ANON__: not called CC=1 SCAR=6.9
235 }
236 catch {
237 0 ▶ die $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 }
239 finally {
240 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
241 0 for ( @{ $self->{block_signals} } ) {
242 0 $Signal::Mask{ $_ } = delete $self->{_saved_signal_masks}{ $_ };
243 }
244 }
245 0 };
246 }
247  
248 1 1;
249  
250 __END__
↑ Summary