blib/lib/DBIO/Replicated/Balancer.pm

filter: Statementstmt 86.6% 84 / 97 Branchbran 70.0% 28 / 40 Conditioncond 54.1% 13 / 24 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 90.9% 20 / 22 Podpod 100% 9 / 9 totaltotal 76.4% 159 / 208 CC 29 SCAR 14.2
CC
29
Cov
78%
next_storage_with_fallback
10.5
select
5.9
_select
5.9
CRAP 4.1
« blib/lib/DBIO/DeploymentHandler.pm ↑ Summary blib/lib/DBIO/Relationship/HasMany.pm »
1 package DBIO::Replicated::Balancer;
2 # ABSTRACT: Base class for replicated read balancing
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.900007';
8  
9 7 ▶use base 'DBIO::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 __PACKAGE__->mk_group_accessors(simple => qw/
15 auto_validate_every
16 current_replicant
17 _on_master
18 /);
19  
20  
21 7 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 7 my ($class, %args) = @_;
23 my $self = bless {
24 auto_validate_every => $args{auto_validate_every},
25 current_replicant => $args{current_replicant},
26 _on_master => 0,
27 master => $args{master},
28 pool => $args{pool},
29 7 }, $class;
30  
31 7 ▶ $self->current_replicant($self->next_storage) if $self->pool;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pool 6 1 7
32  
33 7 return $self;
34 }
35  
36  
37 28 ▶sub master {
Subroutine master: called CC=2 SCAR=7.1
Pod documented
38 28 my $self = shift;
39 28 ▶ $self->{master} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 28 28
40 28 return $self->{master};
41 }
42  
43  
44 28 ▶sub pool {
Subroutine pool: called CC=2 SCAR=6.9
Pod documented
45 28 my $self = shift;
46 28 ▶ $self->{pool} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 27 28
47 28 return $self->{pool};
48 }
49  
50  
51 0 ▶sub next_storage {
Subroutine next_storage: not called CC=1 SCAR=6.9
Pod documented
52 0 die ref($_[0]) . ' must implement next_storage()';
53 }
54  
55  
56 11 ▶sub next_storage_with_fallback {
Subroutine next_storage_with_fallback: called CC=6 SCAR=23.5
Pod documented
57 11 my ($self, @args) = @_;
58 11 my $now = time;
59  
60 11 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->auto_validate_every and $self->auto_validate_every + $self->pool->last_validated <= $now) 0 11 11
Condition33%1/3
defined $self->auto_validate_every and $self->auto_validate_every + $self->pool->last_validated <= $now
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $self->auto_validate_every and $self->auto_validate_every + $self->pool->last_validated <= $now
defined $self->auto_validate_every $self->auto_validate_every + $self->pool->last_validated <= $now
Truth table33%1/3
A and B
A= defined $self->auto_validate_everyB= $self->auto_validate_every + $self->pool->last_validated <= $now
ABresult
0X0
100
111
61 defined $self->auto_validate_every
62 && ($self->auto_validate_every + $self->pool->last_validated) <= $now
63 ) {
64 0 $self->pool->validate_replicants;
65 }
66  
67 11 ▶ if (my $next = $self->next_storage(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $self->next_storage(@args)) 11 0 11
68 11 ▶ $self->master->debugobj->print("Moved back to slave\n") if $self->_on_master;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_on_master 0 11 11
69 11 $self->_on_master(0);
70 11 return $next;
71 }
72  
73 0 ▶ $self->master->debugobj->print("No Replicants validate, falling back to master reads.\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_on_master 0 0 0
74 unless $self->_on_master;
75  
76 0 $self->_on_master(1);
77 0 return $self->master;
78 }
79  
80  
81 11 ▶sub increment_storage {
Subroutine increment_storage: called CC=1 SCAR=0.0
Pod documented
82 11 my $self = shift;
83 11 $self->current_replicant($self->next_storage_with_fallback);
84 }
85  
86  
87 # A select with a lock attribute (for => 'update', 'shared', any defined value)
88 # takes its lock where the write goes: the master.
89 10 ▶sub _has_lock_attribute {
Subroutine _has_lock_attribute: called CC=2 SCAR=7.1
90 10 my ($args) = @_;
91 10 ▶ return ref($args->[-1]) eq 'HASH' && defined $args->[-1]{for};
Condition66%2/3
ref $args->[-1] eq 'HASH' && defined $args->[-1]{'for'}
!ll&&!rl&&r
073
MC/DC50%1/2
ref $args->[-1] eq 'HASH' && defined $args->[-1]{'for'}
ref $args->[-1] eq 'HASH' defined $args->[-1]{'for'}
Truth table66%2/3
A && B
A= ref $args->[-1] eq 'HASH'B= defined $args->[-1]{'for'}
ABresult
0X0
100
111
92 }
93  
94 5 ▶sub select {
Subroutine select: called CC=5 SCAR=17.8
Pod documented
95 5 my $self = shift;
96 5 my @args = @_;
97  
98 5 ▶ if (ref($args[-1]) eq 'HASH' && (my $forced_pool = $args[-1]->{force_pool})) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[-1] eq 'HASH' and my $forced_pool = $args[-1]{'force_pool'}) { } 0 5 5
elsif ($self->master->transaction_depth or _has_lock_attribute(\@args)) { } 2 3 5
Condition33%1/3
ref $args[-1] eq "HASH" and my $forced_pool = $args[-1]{"force_pool"}
!ll&&!rl&&r
050
MC/DC0%0/2
ref $args[-1] eq "HASH" and my $forced_pool = $args[-1]{"force_pool"}
ref $args[-1] eq "HASH" my $forced_pool = $args[-1]{"force_pool"}
Truth table33%1/3
A and B
A= ref $args[-1] eq "HASH"B= my $forced_pool = $args[-1]{"force_pool"}
ABresult
0X0
100
111
99 0 delete $args[-1]->{force_pool};
100 0 return $self->_get_forced_pool($forced_pool)->select(@args);
101 }
102 ▶ elsif ($self->master->transaction_depth || _has_lock_attribute(\@args)) {
Condition66%2/3
$self->master->transaction_depth or _has_lock_attribute(\@args)
l!l&&r!l&&!r
023
MC/DC50%1/2
$self->master->transaction_depth or _has_lock_attribute(\@args)
$self->master->transaction_depth _has_lock_attribute(\@args)
Truth table66%2/3
A or B
A= $self->master->transaction_depthB= _has_lock_attribute(\@args)
ABresult
1X1
011
000
103 2 return $self->master->select(@args);
104 }
105  
106 3 $self->increment_storage;
107 3 return $self->current_replicant->select(@args);
108 }
109  
110 9 ▶sub select_single {
Subroutine select_single: called CC=5 SCAR=16.2
Pod documented
111 9 my $self = shift;
112 9 my @args = @_;
113  
114 9 ▶ if (ref($args[-1]) eq 'HASH' && (my $forced_pool = $args[-1]->{force_pool})) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[-1] eq 'HASH' and my $forced_pool = $args[-1]{'force_pool'}) { } 5 4 9
elsif ($self->master->transaction_depth or _has_lock_attribute(\@args)) { } 1 3 4
Condition66%2/3
ref $args[-1] eq "HASH" and my $forced_pool = $args[-1]{"force_pool"}
!ll&&!rl&&r
045
MC/DC50%1/2
ref $args[-1] eq "HASH" and my $forced_pool = $args[-1]{"force_pool"}
ref $args[-1] eq "HASH" my $forced_pool = $args[-1]{"force_pool"}
Truth table66%2/3
A and B
A= ref $args[-1] eq "HASH"B= my $forced_pool = $args[-1]{"force_pool"}
ABresult
0X0
100
111
115 5 delete $args[-1]->{force_pool};
116 5 return $self->_get_forced_pool($forced_pool)->select_single(@args);
117 }
118 ▶ elsif ($self->master->transaction_depth || _has_lock_attribute(\@args)) {
Condition66%2/3
$self->master->transaction_depth or _has_lock_attribute(\@args)
l!l&&r!l&&!r
013
MC/DC50%1/2
$self->master->transaction_depth or _has_lock_attribute(\@args)
$self->master->transaction_depth _has_lock_attribute(\@args)
Truth table66%2/3
A or B
A= $self->master->transaction_depthB= _has_lock_attribute(\@args)
ABresult
1X1
011
000
119 1 return $self->master->select_single(@args);
120 }
121  
122 3 $self->increment_storage;
123 3 return $self->current_replicant->select_single(@args);
124 }
125  
126  
127 0 ▶sub columns_info_for {
Subroutine columns_info_for: not called CC=1 SCAR=6.9
Pod documented
128 0 my $self = shift;
129 0 $self->increment_storage;
130 0 return $self->current_replicant->columns_info_for(@_);
131 }
132  
133 2 ▶sub _select {
Subroutine _select: called CC=5 SCAR=17.8
134 2 my $self = shift;
135 2 my @args = @_;
136  
137 2 ▶ if (ref($args[-1]) eq 'HASH' && (my $forced_pool = $args[-1]->{force_pool})) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args[-1] eq 'HASH' and my $forced_pool = $args[-1]{'force_pool'}) { } 0 2 2
elsif ($self->master->transaction_depth or _has_lock_attribute(\@args)) { } 1 1 2
Condition33%1/3
ref $args[-1] eq "HASH" and my $forced_pool = $args[-1]{"force_pool"}
!ll&&!rl&&r
020
MC/DC0%0/2
ref $args[-1] eq "HASH" and my $forced_pool = $args[-1]{"force_pool"}
ref $args[-1] eq "HASH" my $forced_pool = $args[-1]{"force_pool"}
Truth table33%1/3
A and B
A= ref $args[-1] eq "HASH"B= my $forced_pool = $args[-1]{"force_pool"}
ABresult
0X0
100
111
138 0 delete $args[-1]->{force_pool};
139 0 return $self->_get_forced_pool($forced_pool)->_select(@args);
140 }
141 ▶ elsif ($self->master->transaction_depth || _has_lock_attribute(\@args)) {
Condition66%2/3
$self->master->transaction_depth or _has_lock_attribute(\@args)
l!l&&r!l&&!r
101
MC/DC50%1/2
$self->master->transaction_depth or _has_lock_attribute(\@args)
$self->master->transaction_depth _has_lock_attribute(\@args)
Truth table66%2/3
A or B
A= $self->master->transaction_depthB= _has_lock_attribute(\@args)
ABresult
1X1
011
000
142 1 return $self->master->_select(@args);
143 }
144  
145 1 $self->increment_storage;
146 1 return $self->current_replicant->_select(@args);
147 }
148  
149 # The pre-rendered statement path (DBIO::Storage::DBI/_execute_rendered), which
150 # ResultSet::results_exist runs its EXISTS statement on. A read, so it routes
151 # like _select: master inside a transaction, a replicant otherwise. The
152 # statement carries no attrs hashref, so there is no force_pool key to honour.
153 1 ▶sub _execute_rendered {
Subroutine _execute_rendered: called CC=2 SCAR=7.0
154 1 my $self = shift;
155  
156 1 ▶ return $self->master->_execute_rendered(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $self->master->transaction_depth 0 1 1
157 if $self->master->transaction_depth;
158  
159 1 $self->increment_storage;
160 1 return $self->current_replicant->_execute_rendered(@_);
161 }
162  
163 # ADR 0040 section 6: the same statement path stopped one verb
164 # short (DBIO::Storage::DBI/_prepare_rendered), for an engine whose answer is
165 # an attribute of the PREPARED statement. It routes like the method it is half
166 # of. Not optional bookkeeping, for the reason spelled out under
167 # _explain_query below: a Balancer is not a storage and has no AUTOLOAD.
168 1 ▶sub _prepare_rendered {
Subroutine _prepare_rendered: called CC=2 SCAR=7.0
169 1 my $self = shift;
170  
171 1 ▶ return $self->master->_prepare_rendered(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $self->master->transaction_depth 0 1 1
172 if $self->master->transaction_depth;
173  
174 1 $self->increment_storage;
175 1 return $self->current_replicant->_prepare_rendered(@_);
176 }
177  
178 # ADR 0040: DBIO::ResultSet::explain's whole operation, which the
179 # driver storage owns (DBIO::Storage::DBI/_explain_query). A read -- it plans a
180 # read query, and every statement it issues goes down _execute_rendered anyway
181 # -- so it routes like _select: master inside a transaction, a replicant
182 # otherwise. This forwarder is not optional bookkeeping: a Balancer is not a
183 # storage and has no AUTOLOAD, so a method listed in
184 # DBIO::Replicated::Storage's @reader_methods and missing here dies on the
185 # read handler.
186 1 ▶sub _explain_query {
Subroutine _explain_query: called CC=2 SCAR=7.0
187 1 my $self = shift;
188  
189 1 ▶ return $self->master->_explain_query(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $self->master->transaction_depth 0 1 1
190 if $self->master->transaction_depth;
191  
192 1 $self->increment_storage;
193 1 return $self->current_replicant->_explain_query(@_);
194 }
195  
196 2 ▶sub _dbh_columns_info_for {
Subroutine _dbh_columns_info_for: called CC=2 SCAR=6.9
197 2 my ($self, $dbh, $table) = @_;
198  
199 # Like select/select_single, route to master when inside a transaction so
200 # we read the data the current transaction can actually see.
201 # _dbh_columns_info_for is provided by base Storage::DBI with a fixed
202 # ($self, $dbh, $table) signature, so it cannot honour force_pool like
203 # select/select_single do; callers who need force_pool routing must drop
204 # down to columns_info_for instead.
205 2 ▶ if ($self->master->transaction_depth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->master->transaction_depth) 1 1 2
206 1 return $self->master->_dbh_columns_info_for($dbh, $table);
207 }
208  
209 1 $self->increment_storage;
210 1 return $self->current_replicant->_dbh_columns_info_for($dbh, $table);
211 }
212  
213 8 ▶sub _get_forced_pool {
Subroutine _get_forced_pool: called CC=4 SCAR=14.0
214 8 my ($self, $forced_pool) = @_;
215  
216 8 ▶ if (blessed $forced_pool) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($forced_pool)) { } 0 8 8
elsif ($forced_pool eq 'master') { } 5 3 8
elsif (my $replicant = $self->pool->replicants->{$forced_pool}) { } 1 2 3
217 0 return $forced_pool;
218 }
219 elsif ($forced_pool eq 'master') {
220 5 return $self->master;
221 }
222 elsif (my $replicant = $self->pool->replicants->{$forced_pool}) {
223 1 return $replicant;
224 }
225  
226 # a replicant is named after its DSN: never a password in it (karr k297)
227 $self->master->throw_exception(
228 2 "'" . DBIO::Util::mask_connect_secrets($forced_pool) . "' is not a named replicant."
229 );
230 }
231  
232 7 1;
233  
234 __END__
« blib/lib/DBIO/DeploymentHandler.pm ↑ Summary blib/lib/DBIO/Relationship/HasMany.pm »