Aurora/Instance.pm

filter: Statementstmt 31.2% 25 / 80 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 32.0% 8 / 25 Podpod 0.0% 0 / 9 totaltotal 23.2% 33 / 142 CC 14 SCAR 29.1
CC
14
Cov
24%
handler
56.0
__ANON__
42.0
new
6.0
CRAP 18.4
« Aurora/Cluster.pm ↑ Summary Aurora.pm »
1 package DBIx::Aurora::Instance;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use DBIx::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
8 0 my ($class, $connect_info, $opts) = @_;
9 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
10  
11 0 my $self = bless {
12 handler => undef,
13 is_readonly => undef,
14 is_unreachable => undef,
15 maybe_reader => 1,
16 connected_at => 0,
17 }, $class;
18  
19 0 ▶ my $on_connection_event = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
20 0 my ($mode, $dbh, $additional) = @_;
21 0 ▶ if (my $orig = $opts->{$mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig = $opts->{$mode}) 0 0 0
22 0 ▶ if (not ref $orig) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $orig) { } 0 0 0
elsif (ref $orig eq 'CODE') { } 0 0 0
elsif (ref $orig eq 'ARRAY') { } 0 0 0
23 0 $dbh->do($orig);
24 } elsif (ref $orig eq 'CODE') {
25 0 $orig->($dbh);
26 } elsif (ref $orig eq 'ARRAY') {
27 0 $dbh->do($_) for @$orig;
28 } else {
29 0 Carp::croak "Invalid $mode: " . ref $orig;
30 }
31 }
32 0 $additional->($mode, $dbh);
33 0 };
34  
35 $self->{handler} = DBIx::Handler->new(
36 $connect_info->[0],
37 $connect_info->[1],
38 $connect_info->[2],
39 {
40  
41 AutoCommit => 0,
42 mysql_connect_timeout => 1,
43 mysql_write_timeout => 1,
44 mysql_read_timeout => 1,
45 0 %{$connect_info->[3]},
46 RaiseError => 1,
47 },
48 {
49 0 ▶ on_connect_do => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my $dbh = shift;
51 0 ▶ $on_connection_event->(on_connect_do => $dbh, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 my ($mode, $dbh) = @_;
53 0 my $is_readonly = do {
54 0 my $sql = 'SELECT @@global.innodb_read_only AS readonly';
55 0 my $row = $dbh->selectrow_hashref($sql);
56 0 $row->{readonly};
57 };
58 0 $self->{is_readonly} = $is_readonly;
59 0 $self->{maybe_reader} = $is_readonly;
60 0 $self->{connected_at} = Time::HiRes::time;
61 0 });
62 },
63 0 ▶ on_disconnect_do => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 my $dbh = shift;
65 0 ▶ $on_connection_event->(on_disconnect_do => $dbh, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my ($mode, $dbh) = @_;
67 0 $self->{is_readonly} = undef;
68 0 $self->{connected_at} = 0;
69 # DO NOT `undef $self->{maybe_reader}`;
70 0 });
71 },
72 }
73 0 );
74  
75 0 $self;
76 }
77  
78 0 ▶sub handler {
Subroutine handler: not called CC=7 SCAR=40.3
Pod undocumented
79 0 my $self = shift;
80  
81 0 eval { $self->{handler}->dbh };
82 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
83 0 ▶ if ($e =~ /DBI connect\(.+\) failed:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /DBI connect\(.+\) failed:/) { } 0 0 0
84 0 ▶ if ($e =~ /Can't connect to MySQL server on '[^']*' \(111\)/) { # failover
Branch: 3 branches
BranchTrueFalseTotal
if ($e =~ /Can't connect to MySQL server on '[^']*' \(111\)/) { } 0 0 0
elsif ($e =~ /Lost connection to MySQL server at/) { } 0 0 0
elsif ($e =~ /Unknown MySQL server host '[^']*' \(0\)/ or $e =~ /Can't connect to MySQL server on/) { } 0 0 0
85 0 Carp::croak(DBIx::Aurora::Instance::Exception::Connectivity::Failover->new($e));
86 } elsif ($e =~ /Lost connection to MySQL server at/) {
87 0 Carp::croak(DBIx::Aurora::Instance::Exception::Connectivity::LostConnection->new($e));
88 ▶ } elsif ( # unexpected connection errors
Condition0%0/3
$e =~ /Unknown MySQL server host '[^']*' \(0\)/ or $e =~ /Can't connect to MySQL server on/
l!l&&r!l&&!r
000
MC/DC0%0/2
$e =~ /Unknown MySQL server host '[^']*' \(0\)/ or $e =~ /Can't connect to MySQL server on/
$e =~ /Unknown MySQL server host '[^']*' \(0\)/ $e =~ /Can't connect to MySQL server on/
Truth table0%0/3
A or B
A= $e =~ /Unknown MySQL server host '[^']*' \(0\)/B= $e =~ /Can't connect to MySQL server on/
ABresult
1X1
011
000
89 $e =~ /Unknown MySQL server host '[^']*' \(0\)/
90 or $e =~ /Can't connect to MySQL server on/
91 ) {
92 # FIXME $self->{is_unreachable} = 1;
93 0 Carp::croak(DBIx::Aurora::Instance::Exception::Connectivity::Unreachable->new($e));
94 } else {
95 0 Carp::croak(DBIx::Aurora::Instance::Exception::Connectivity->new($e));
96 }
97 } else {
98 0 Carp::croak($e);
99 }
100 }
101  
102 0 return $self->{handler};
103 }
104  
105 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod undocumented
106 0 my $self = shift;
107 0 $self->{is_readonly} = undef;
108 # DO NOT `undef $self->{maybe_reader}`;
109 0 ▶ $self->{handler} && $self->{handler}->disconnect;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"handler"} and $self->{"handler"}->disconnect 0 0 0
110 }
111  
112 0 ▶sub maybe_writer { !shift->{maybe_reader} }
Subroutine maybe_writer: not called CC=1 SCAR=6.9
Pod undocumented
113 0 ▶sub maybe_reader { shift->{maybe_reader} }
Subroutine maybe_reader: not called CC=1 SCAR=6.9
Pod undocumented
114 0 ▶sub is_writer { !shift->{is_readonly} }
Subroutine is_writer: not called CC=1 SCAR=6.9
Pod undocumented
115 0 ▶sub is_reader { shift->{is_readonly} }
Subroutine is_reader: not called CC=1 SCAR=6.9
Pod undocumented
116 0 ▶sub is_unreachable { shift->{is_unreachable} }
Subroutine is_unreachable: not called CC=1 SCAR=6.9
Pod undocumented
117 0 ▶sub connected_at { shift->{connected_at} }
Subroutine connected_at: not called CC=1 SCAR=6.9
Pod undocumented
118  
119 0 ▶sub DESTROY { $_[0]->disconnect }
Subroutine DESTROY: not called CC=1 SCAR=6.9
120  
121 package
122 DBIx::Aurora::Instance::Exception::Connectivity;
123 0 ▶use overload '""' => sub { shift->{msg} };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
124  
125 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
126 0 my ($class, $msg) = @_;
127 0 bless { msg => $msg }, $class;
128 }
129  
130 package
131 DBIx::Aurora::Instance::Exception::Connectivity::Failover;
132 1 ▶use parent -norequire, 'DBIx::Aurora::Instance::Exception::Connectivity';
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 package
135 DBIx::Aurora::Instance::Exception::Connectivity::LostConnection;
136 1 ▶use parent -norequire, 'DBIx::Aurora::Instance::Exception::Connectivity';
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138 package
139 DBIx::Aurora::Instance::Exception::Connectivity::Unreachable;
140 1 ▶use parent -norequire, 'DBIx::Aurora::Instance::Exception::Connectivity';
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 1 1;
143 __END__
« Aurora/Cluster.pm ↑ Summary Aurora.pm »