blib/lib/DBIO/Replicated/Pool.pm

filter: Statementstmt 77.3% 106 / 137 Branchbran 50.0% 20 / 40 Conditioncond 71.4% 25 / 35 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 79.3% 23 / 29 Podpod 100% 14 / 14 totaltotal 72.3% 196 / 271 CC 38 SCAR 24.8
CC
38
Cov
71%
validate_replicants
56.0
connect_replicants
15.2
new
10.3
CRAP 12.0
« blib/lib/DBIO/Replicated.pm ↑ Summary blib/lib/DBIO/Test/Conformance.pm »
1 package DBIO::Replicated::Pool;
2 # ABSTRACT: Manage a pool of replicant backends
3  
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 our $VERSION = '0.900007';
8  
9 14 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 14 __PACKAGE__->mk_group_accessors(simple => qw/
17 maximum_lag
18 last_validated
19 replicant_type
20 storage_class
21 next_unknown_replicant_id
22 /);
23  
24  
25 32 ▶sub new {
Subroutine new: called CC=9 SCAR=23.3
Pod documented
26 32 my ($class, %args) = @_;
27  
28 my $self = bless {
29 maximum_lag => exists $args{maximum_lag} ? $args{maximum_lag} : 0,
30 last_validated => exists $args{last_validated} ? $args{last_validated} : 0,
31 replicant_type => $args{replicant_type} || 'DBIO::Replicated::Backend::Replicant',
32 storage_class => $args{storage_class} || 'DBIO::Storage::DBI',
33 next_unknown_replicant_id => $args{next_unknown_replicant_id} || 1,
34 replicants => $args{replicants} || {},
35 32 ▶ replicant_order => $args{replicant_order} || [],
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'maximum_lag'} ? : 0 32 32
exists $args{'last_validated'} ? : 0 32 32
Condition70%7/10
$args{'replicant_type'} || 'DBIO::Replicated::Backend::Replicant'
l!l
131
$args{'storage_class'} || 'DBIO::Storage::DBI'
l!l
284
$args{'next_unknown_replicant_id'} || 1
l!l
032
$args{'replicants'} || {}
l!l
032
$args{'replicant_order'} || []
l!l
032
MC/DC100%1/1
$args{'replicant_type'} || 'DBIO::Replicated::Backend::Replicant'
$args{'replicant_type'}
MC/DC100%1/1
$args{'storage_class'} || 'DBIO::Storage::DBI'
$args{'storage_class'}
MC/DC0%0/1
$args{'next_unknown_replicant_id'} || 1
$args{'next_unknown_replicant_id'}
MC/DC0%0/1
$args{'replicants'} || {}
$args{'replicants'}
MC/DC0%0/1
$args{'replicant_order'} || []
$args{'replicant_order'}
Truth table100%2/2
A
A= $args{'replicant_type'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'storage_class'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'next_unknown_replicant_id'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'replicants'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'replicant_order'}
Aresult
11
00
36 }, $class;
37  
38 32 ▶ $self->master($args{master}) if exists $args{master};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"master"} 26 6 32
39  
40 32 return $self;
41 }
42  
43  
44 256 ▶sub master {
Subroutine master: called CC=2 SCAR=6.9
Pod documented
45 256 my $self = shift;
46 256 ▶ $self->{master} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 26 230 256
47 256 return $self->{master};
48 }
49  
50  
51 127 ▶sub replicants {
Subroutine replicants: called CC=2 SCAR=7.1
Pod documented
52 127 my $self = shift;
53 127 ▶ $self->{replicants} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 127 127
54 127 return $self->{replicants};
55 }
56  
57  
58 46 ▶sub set_replicant {
Subroutine set_replicant: called CC=2 SCAR=7.0
Pod documented
59 46 my ($self, $key, $value) = @_;
60 46 push @{ $self->{replicant_order} }, $key
61 46 ▶ unless exists $self->replicants->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->replicants->{$key} 46 0 46
62 46 return $self->replicants->{$key} = $value;
63 }
64  
65 0 ▶sub get_replicant {
Subroutine get_replicant: not called CC=1 SCAR=6.9
Pod documented
66 0 my ($self, $key) = @_;
67 0 return $self->replicants->{$key};
68 }
69  
70  
71 1 ▶sub has_replicants {
Subroutine has_replicants: called CC=1 SCAR=0.0
Pod documented
72 1 my $self = shift;
73 1 return scalar keys %{ $self->replicants };
74 }
75  
76 0 ▶sub num_replicants {
Subroutine num_replicants: not called CC=1 SCAR=6.9
Pod documented
77 0 my $self = shift;
78 0 return scalar keys %{ $self->replicants };
79 }
80  
81 0 ▶sub delete_replicant {
Subroutine delete_replicant: not called CC=2 SCAR=17.9
Pod documented
82 0 my ($self, $key) = @_;
83 $self->{replicant_order} = [
84 0 ▶ grep { $_ ne $key } @{ $self->{replicant_order} || [] }
Condition0%0/2
$self->{'replicant_order'} || []
l!l
00
MC/DC0%0/1
$self->{'replicant_order'} || []
$self->{'replicant_order'}
Truth table0%0/2
A
A= $self->{'replicant_order'}
Aresult
11
00
85 ];
86 0 return delete $self->replicants->{$key};
87 }
88  
89  
90 18 ▶sub all_replicants {
Subroutine all_replicants: called CC=2 SCAR=7.0
Pod documented
91 18 my $self = shift;
92 18 ▶ return map { $self->replicants->{$_} } @{ $self->{replicant_order} || [] };
Condition50%1/2
$self->{'replicant_order'} || []
l!l
180
MC/DC0%0/1
$self->{'replicant_order'} || []
$self->{'replicant_order'}
Truth table50%1/2
A
A= $self->{'replicant_order'}
Aresult
11
00
93 }
94  
95  
96 0 ▶sub connected_replicants {
Subroutine connected_replicants: not called CC=1 SCAR=6.9
Pod documented
97 0 return scalar grep { $_->connected } shift->all_replicants;
98 }
99  
100  
101 17 ▶sub active_replicants {
Subroutine active_replicants: called CC=1 SCAR=0.0
Pod documented
102 17 my $self = shift;
103 17 return grep { $_->active } $self->all_replicants;
104 }
105  
106  
107 25 ▶sub connect_replicants {
Subroutine connect_replicants: called CC=15 SCAR=27.2
Pod documented
108 25 my $self = shift;
109 25 my $schema = shift;
110 25 my @newly_created;
111  
112 25 foreach my $connect_info (@_) {
113 46 ▶ $connect_info = [ $connect_info ] if reftype($connect_info) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($connect_info) ne "ARRAY" 0 46 46
114  
115 # A broker-backed replicant ([$broker]) carries no DSN in hand: the DSN
116 # is resolved from its CredentialSource at connect time. Never reach into
117 # its blessed innards looking for dbh_maker/dsn keys.
118 46 my $is_broker = DBIO::Util::is_access_broker($connect_info->[0]);
119  
120 my $connect_coderef =
121 (reftype($connect_info->[0]) || '') eq 'CODE' ? $connect_info->[0]
122 46 ▶ : !$is_broker && (reftype($connect_info->[0]) || '') eq 'HASH' && $connect_info->[0]->{dbh_maker};
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($connect_info->[0]) || '') eq 'CODE' ? : 0 46 46
Condition100%10/10
builtin::reftype($connect_info->[0]) || ''
l!l
2323
not $is_broker and (builtin::reftype($connect_info->[0]) || "") eq "HASH" and $connect_info->[0]{"dbh_maker"}
!ll&&!rl&&r
3961
not $is_broker and (builtin::reftype($connect_info->[0]) || "") eq "HASH"
!ll&&!rl&&r
16237
builtin::reftype($connect_info->[0]) || ''
l!l
723
MC/DC100%3/3
not $is_broker and (builtin::reftype($connect_info->[0]) || "") eq "HASH" and $connect_info->[0]{"dbh_maker"}
not $is_broker builtin::reftype($connect_info->[0]) $connect_info->[0]{"dbh_maker"}
Truth table100%4/4
not(A) and B and C
A= not $is_brokerB= builtin::reftype($connect_info->[0])C= $connect_info->[0]{"dbh_maker"}
ABCresult
0XX0
10X0
1100
1111
123  
124 46 my $dsn;
125 46 my $replicant = do {
126 14 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 46 my $connect = \&DBI::connect;
128 0 ▶ local *DBI::connect = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 $dsn = $_[1];
130 0 goto $connect;
131 46 };
132 46 $self->connect_replicant($schema, $connect_info);
133 };
134  
135 46 my $key;
136 46 ▶ if (!$dsn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dsn) 46 0 46
137 46 ▶ if (!$connect_coderef && !$is_broker) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $connect_coderef || $is_broker) { } 29 17 46
Condition100%3/3
$connect_coderef || $is_broker
l!l&&r!l&&!r
11629
MC/DC100%2/2
$connect_coderef || $is_broker
$connect_coderef $is_broker
Truth table100%3/3
A || B
A= $connect_coderefB= $is_broker
ABresult
1X1
011
000
138 29 $dsn = $connect_info->[0];
139 29 ▶ $dsn = $dsn->{dsn} if (reftype($dsn) || '') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($dsn) || "") eq "HASH" 6 23 29
Condition100%2/2
builtin::reftype($dsn) || ''
l!l
623
MC/DC100%1/1
builtin::reftype($dsn) || ''
builtin::reftype($dsn)
Truth table100%2/2
A
A= builtin::reftype($dsn)
Aresult
11
00
140 }
141 else {
142 17 $key = 'UNKNOWN_' . $self->next_unknown_replicant_id;
143 17 $self->next_unknown_replicant_id($self->next_unknown_replicant_id + 1);
144 }
145 }
146  
147 46 ▶ if ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn) 29 17 46
148 29 $replicant->dsn($dsn);
149 29 ($key) = ($dsn =~ m/^dbi\:.+\:(.+)$/i);
150  
151 # the name is a pool key (force_pool), shown in traces and messages:
152 # never a password in it
153 29 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 29 0 29
154 29 $key = join ';', grep { !/^\s*(?:password|passwd|pwd)\s*=/i } split /;/, $key;
155 }
156 }
157  
158 46 $replicant->id($key);
159 46 $self->set_replicant($key => $replicant);
160 46 push @newly_created, $replicant;
161 }
162  
163 25 return @newly_created;
164 }
165  
166  
167 48 ▶sub connect_replicant {
Subroutine connect_replicant: called CC=5 SCAR=16.8
Pod documented
168 48 my ($self, $schema, $connect_info) = @_;
169  
170 48 my $storage_class = $self->storage_class;
171 48 $self->ensure_class_loaded($storage_class);
172 48 DBIO::Util::assert_class_isa(
173 storage_class => $storage_class, 'DBIO::Storage::DBI',
174 );
175  
176 47 my $storage = $storage_class->new($schema);
177 47 $storage->connect_info($connect_info);
178 47 ▶ $storage->_determine_driver if $storage->can('_determine_driver');
Branch: 1 branch
BranchTrueFalseTotal
if $storage->can("_determine_driver") 47 0 47
179  
180 47 my $replicant_class = $self->replicant_type;
181 47 $self->ensure_class_loaded($replicant_class);
182 47 DBIO::Util::assert_class_isa(
183 replicant_type => $replicant_class, 'DBIO::Replicated::Backend::Replicant',
184 );
185  
186 46 my $replicant = $replicant_class->new(
187 storage => $storage,
188 master => $self->master,
189 );
190  
191 46 ▶ if ($self->master && $self->master->storage && $self->master->storage->debugobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->master and $self->master->storage and $self->master->storage->debugobj) 46 0 46
Condition33%2/6
$self->master and $self->master->storage and $self->master->storage->debugobj
!ll&&!rl&&r
0046
$self->master and $self->master->storage
!ll&&!rl&&r
0046
MC/DC0%0/3
$self->master and $self->master->storage and $self->master->storage->debugobj
$self->master $self->master->storage $self->master->storage->debugobj
Truth table25%1/4
A and B and C
A= $self->masterB= $self->master->storageC= $self->master->storage->debugobj
ABCresult
0XX0
10X0
1100
1111
192 46 $replicant->install_debug_proxy($self->master->storage->debugobj);
193 }
194  
195 46 return $replicant;
196 }
197  
198 1 ▶sub _safely_ensure_connected {
Subroutine _safely_ensure_connected: called CC=1 SCAR=0.0
199 1 my ($self, $replicant, @args) = @_;
200 1 ▶ return $self->_safely($replicant, '->ensure_connected', sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
201 1 $replicant->ensure_connected(@args);
202 1 });
203 }
204  
205 1 ▶sub _safely {
Subroutine _safely: called CC=1 SCAR=0.0
206 1 my ($self, $replicant, $name, $code) = @_;
207  
208 1 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=1.2
209 1 $code->();
210 0 1;
211 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
212 # names the replicant by its DSN: never the password in it (karr k297)
213 1 my $info = $replicant->storage->_dbi_connect_info;
214 1 $replicant->debugobj->print(DBIO::Util::mask_connect_secrets(
215 sprintf(
216 "Exception trying to $name for replicant %s, error is %s",
217 $info->[0], $_,
218 ),
219 $info,
220 ));
221 1 undef;
222 1 };
223 }
224  
225  
226 0 ▶sub validate_replicants {
Subroutine validate_replicants: not called CC=7 SCAR=40.3
Pod documented
227 0 my $self = shift;
228  
229 0 foreach my $replicant ($self->all_replicants) {
230 0 ▶ if ($self->_safely_ensure_connected($replicant)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_safely_ensure_connected($replicant)) { } 0 0 0
231 0 my $is_replicating = $replicant->is_replicating;
232  
233 0 ▶ unless (defined $is_replicating) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $is_replicating) 0 0 0
234 0 $replicant->debugobj->print(
235 "Storage Driver " . ref($replicant->storage)
236 . " does not support 'is_replicating'. Assuming manual management.\n"
237 );
238 0 next;
239 }
240  
241 0 ▶ if ($is_replicating) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_replicating) { } 0 0 0
242 0 my $lag_behind_master = $replicant->lag_behind_master;
243  
244 0 ▶ unless (defined $lag_behind_master) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lag_behind_master) 0 0 0
245 0 $replicant->debugobj->print(
246 "Storage Driver " . ref($replicant->storage)
247 . " does not support 'lag_behind_master'. Assuming manual management.\n"
248 );
249 0 next;
250 }
251  
252 0 ▶ $replicant->active($lag_behind_master <= $self->maximum_lag ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$lag_behind_master <= $self->maximum_lag ? : 0 0 0
253 }
254 else {
255 0 $replicant->active(0);
256 }
257 }
258 else {
259 0 $replicant->active(0);
260 }
261 }
262  
263 0 $self->last_validated(time);
264 }
265  
266 14 1;
267  
268 __END__
« blib/lib/DBIO/Replicated.pm ↑ Summary blib/lib/DBIO/Test/Conformance.pm »