File Coverage

blib/lib/Loop/Sustainable/Strategy/MySQL/BalancedReplication.pm
Criterion Covered Total %
statement 18 41 43.9
branch 0 16 0.0
condition n/a
subroutine 6 8 75.0
pod 2 2 100.0
total 26 67 38.8


line stmt bran cond sub pod time code
1             package Loop::Sustainable::Strategy::MySQL::BalancedReplication;
2              
3 1     1   4 use strict;
  1         2  
  1         29  
4 1     1   6 use warnings;
  1         2  
  1         26  
5 1     1   4 use parent qw(Loop::Sustainable::Strategy);
  1         2  
  1         5  
6              
7 1     1   45 use Carp;
  1         1  
  1         82  
8             use Class::Accessor::Lite (
9 1         7 new => 0,
10             rw => [qw/dbh capable_behind_seconds on_error_scale_factor on_error_croak/],
11 1     1   6 );
  1         1  
12 1     1   121 use List::Util qw(max);
  1         1  
  1         345  
13              
14             our $VERSION = '0.01';
15              
16             sub new {
17 0     0 1   my $class = shift;
18 0           my $self = $class->SUPER::new(@_);
19 0 0         unless ( exists $self->{capable_behind_seconds} ) {
20 0           $self->{capable_behind_seconds} = 5;
21             }
22 0 0         unless ( exists $self->{on_error_scale_factor} ) {
23 0           $self->{on_error_scale_factor} = 5;
24             }
25 0 0         unless ( exists $self->{on_error_croak} ) {
26 0           $self->{on_error_croak} = 0;
27             }
28 0           $self;
29             }
30              
31             sub wait_correction {
32 0     0 1   my ( $self, $i, $elapsed, $rv ) = @_;
33              
34 0           my $second_behind_master; ;
35              
36 0           eval {
37 0           my $dbh = $self->{dbh};
38 0 0         my $status = $dbh->selectrow_hashref('SHOW SLAVE STATUS') or croak($dbh->errstr);
39 0 0         if ( defined $status->{Seconds_Behind_Master} ) {
40 0           $second_behind_master = $status->{Seconds_Behind_Master};
41             }
42             };
43 0 0         if (my $e = $@) {
44 0 0         if ( $self->{on_error_croak} ) {
45 0           croak $e;
46             }
47             else {
48 0           carp $e;
49             }
50             };
51              
52 0 0         unless ( defined $second_behind_master ) {
53 0           $second_behind_master = max($self->{capable_behind_seconds}, 5) * $self->{on_error_scale_factor};
54             }
55              
56 0           return max( ( $second_behind_master - $self->{capable_behind_seconds} ) / $self->check_strategy_interval, 0 );
57             }
58              
59             1;
60              
61             __END__