Session/Store/Patroni.pm

filter: Statementstmt 49.4% 91 / 184 Branchbran 34.6% 27 / 78 Conditioncond 21.5% 20 / 93 MC/DCmcdc 1.9% 1 / 51 Subroutinesub 76.4% 13 / 17 Podpod 0.0% 0 / 6 totaltotal 35.4% 152 / 429 CC 78 SCAR 51.7
CC
78
Cov
39%
checkMaster
600.0
_endTransaction
53.6
connection
43.1
CRAP 175.8
↑ Summary Session/Store/LDAP.pm »
1 package Web::Session::Store::Patroni;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Apache::Session::Store::Postgres;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Web::Session::Store::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Web::Session::Store::Postgres;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 our @ISA = qw(Apache::Session::Store::Postgres);
11 10 our $VERSION = '1.0.0';
12  
13 # Cache structure per DataSource:
14 # {
15 # leader => { host => '...', port => '...', time => ... },
16 # lastFailure => timestamp,
17 # }
18 10 our %patroniCache;
19  
20 11 ▶sub connection {
Subroutine connection: called CC=21 SCAR=37.6
Pod undocumented
21 11 my $self = shift;
22 11 my $session = shift;
23  
24 11 Web::Session::Store::DBI::_forgetInheritedHandle($self);
25 11 ▶ return if ( defined $self->{dbh} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 0 11 11
26  
27 $self->{'table_name'} =
28 11 ▶ $session->{args}->{TableName} || $Apache::Session::Store::DBI::TableName;
Condition33%1/3
$session->{'args'}{'TableName'} || $Apache::Session::Store::DBI::TableName
l!l&&r!l&&!r
0110
MC/DC0%0/2
$session->{'args'}{'TableName'} || $Apache::Session::Store::DBI::TableName
$session->{'args'}{'TableName'} $Apache::Session::Store::DBI::TableName
Truth table33%1/3
A || B
A= $session->{'args'}{'TableName'}B= $Apache::Session::Store::DBI::TableName
ABresult
1X1
011
000
29  
30 11 ▶ if ( exists $session->{args}->{Handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $session->{"args"}{"Handle"}) 0 11 11
31 0 $self->{dbh} = $session->{args}->{Handle};
32 0 $self->{commit} = $session->{args}->{Commit};
33 0 return;
34 }
35  
36 # Store original DataSource as cache key. Kept when _try() reconnects:
37 # $session->{args}->{DataSource} then holds the DSN of the failed
38 # primary, and the new leader must be cached under the original key, the
39 # one that the next sessions read
40 my $originalDataSource = $self->{_originalDataSource} //=
41 11 ▶ $session->{args}->{DataSource};
Condition66%2/3
$self->{"_originalDataSource"} //= $session->{"args"}{"DataSource"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
$self->{"_originalDataSource"} //= $session->{"args"}{"DataSource"}
$self->{"_originalDataSource"} $session->{"args"}{"DataSource"}
Truth table66%2/3
A //= B
A= $self->{"_originalDataSource"}B= $session->{"args"}{"DataSource"}
ABresult
1X1
011
000
42  
43 # Use cached leader if available and not expired
44 11 ▶ my $cache = $patroniCache{$originalDataSource} || {};
Condition50%1/2
$Web::Session::Store::Patroni::patroniCache{$originalDataSource} || {}
l!l
011
MC/DC0%0/1
$Web::Session::Store::Patroni::patroniCache{$originalDataSource} || {}
$Web::Session::Store::Patroni::patroniCache{$originalDataSource}
Truth table50%1/2
A
A= $Web::Session::Store::Patroni::patroniCache{$originalDataSource}
Aresult
11
00
45 11 ▶ my $cacheTTL = $session->{args}->{PatroniCacheTTL} || 60;
Condition50%1/2
$session->{'args'}{'PatroniCacheTTL'} || 60
l!l
011
MC/DC0%0/1
$session->{'args'}{'PatroniCacheTTL'} || 60
$session->{'args'}{'PatroniCacheTTL'}
Truth table50%1/2
A
A= $session->{'args'}{'PatroniCacheTTL'}
Aresult
11
00
46  
47 11 ▶ if ( $cache->{leader}
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->{"leader"} and $cache->{"leader"}{"time"} and time - $cache->{"leader"}{"time"} < $cacheTTL) 0 11 11
Condition33%2/6
$cache->{"leader"} and $cache->{"leader"}{"time"} and time - $cache->{"leader"}{"time"} < $cacheTTL
!ll&&!rl&&r
1100
$cache->{"leader"} and $cache->{"leader"}{"time"}
!ll&&!rl&&r
1100
MC/DC0%0/3
$cache->{"leader"} and $cache->{"leader"}{"time"} and time - $cache->{"leader"}{"time"} < $cacheTTL
$cache->{"leader"} $cache->{"leader"}{"time"} time - $cache->{"leader"}{"time"} < $cacheTTL
Truth table25%1/4
A and B and C
A= $cache->{"leader"}B= $cache->{"leader"}{"time"}C= time - $cache->{"leader"}{"time"} < $cacheTTL
ABCresult
0XX0
10X0
1100
1111
48 and $cache->{leader}->{time}
49 and ( time() - $cache->{leader}->{time} ) < $cacheTTL )
50 {
51 $session->{args}->{DataSource} =
52 0 _buildDataSource( $originalDataSource, $cache->{leader} );
53 }
54  
55 #the programmer has to tell us what commit policy to use
56 11 ▶ my $commit = $session->{args}->{Commit} // 1;
Condition100%2/2
$session->{"args"}{"Commit"} // 1
l!l
83
MC/DC100%1/1
$session->{"args"}{"Commit"} // 1
$session->{"args"}{"Commit"}
Truth table100%2/2
A
A= $session->{"args"}{"Commit"}
Aresult
11
00
57  
58 # Connection pool of the Postgres store (see Store/Postgres.pm), keyed by
59 # the DSN of the primary in use
60 return $self->_pooledPrimary( $session->{args}, $commit )
61 if ( Web::Session::Store::Postgres::_poolApplies( $session->{args},
62 11 ▶ $commit ) );
Branch: 1 branch
BranchTrueFalseTotal
if Web::Session::Store::Postgres::_poolApplies($session->{"args"}, $commit) 3 8 11
63  
64 # AutoCommit off, as Postgres: see "Why AutoCommit differs" in Store/DBI.pm
65 8 my $reused;
66 8 foreach ( 0 .. 1 ) {
67 (
68 $self->checkMaster( $session->{args} )
69 or warn "Patroni check failed"
70 8 ▶ ) if $self->{failure};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"failure"} 0 8 8
Condition20%1/5
$self->{'failure'} && ($self->checkMaster($session->{'args'}) || warn('Patroni check failed'))
!ll&&!rl&&r
800
$self->checkMaster($session->{"args"}) or warn "Patroni check failed"
l!l
00
MC/DC0%0/2
$self->{'failure'} && ($self->checkMaster($session->{'args'}) || warn('Patroni check failed'))
$self->{'failure'} $self->checkMaster($session->{"args"})
Truth table33%1/3
A && B
A= $self->{'failure'}B= $self->checkMaster($session->{"args"})
ABresult
0X0
100
111
71 8 eval {
72 $reused = Web::Session::Store::DBI::_reusedHandle(
73 $self, $session->{args},
74 8 @{ $session->{args} }{qw(DataSource UserName Password)},
75 { RaiseError => 1, AutoCommit => 0 }
76 );
77 $self->{dbh} = $reused || DBI->connect(
78 $session->{args}->{DataSource},
79 $session->{args}->{UserName},
80 $session->{args}->{Password},
81 8 ▶ { RaiseError => 1, AutoCommit => 0 }
Condition60%3/5
$reused || 'DBI'->connect($session->{'args'}{'DataSource'}, $session->{'args'}{'UserName'}, $session->{'args'}{'Password'}, {'RaiseError', 1, 'AutoCommit', 0}) || die($DBI::errstr)
l!l
80
$reused or "DBI"->connect($session->{"args"}{"DataSource"}, $session->{"args"}{"UserName"}, $session->{"args"}{"Password"}, {"RaiseError", 1, "AutoCommit", 0})
l!l&&r!l&&!r
710
MC/DC0%0/2
$reused || 'DBI'->connect($session->{'args'}{'DataSource'}, $session->{'args'}{'UserName'}, $session->{'args'}{'Password'}, {'RaiseError', 1, 'AutoCommit', 0}) || die($DBI::errstr)
$reused "DBI"->connect($session->{"args"}{"DataSource"}, $session->{"args"}{"UserName"}, $session->{"args"}{"Password"}, {"RaiseError", 1, "AutoCommit", 0})
Truth table66%2/3
A or B
A= $reusedB= "DBI"->connect($session->{"args"}{"DataSource"}, $session->{"args"}{"UserName"}, $session->{"args"}{"Password"}, {"RaiseError", 1, "AutoCommit", 0})
ABresult
1X1
011
000
82 ) || die $DBI::errstr;
83 };
84 8 ▶ if ( $@ and !$_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $_) { } 0 8 8
Condition33%1/3
$@ and not $_
!ll&&!rl&&r
800
MC/DC0%0/2
$@ and not $_
$@ not $_
Truth table33%1/3
A and not(B)
A= $@B= not $_
ABresult
0X0
100
111
85 0 $self->{failure} = 1;
86 }
87 else {
88 8 last;
89 }
90 }
91 8 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
92  
93 #If we open the connection, we close the connection
94 8 ▶ $self->{disconnect} = $reused ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$reused ? : 7 1 8
95  
96 8 $self->{commit} = $commit;
97 }
98  
99 # connection() with the pool: a connection to the primary is borrowed from
100 # the pool of its DSN (the one in $args->{DataSource}, resolved from the
101 # leader cache or by checkMaster()), with the retry of connection().
102 #
103 # Keyed by the DSN of the primary, the pool never lends a connection to a
104 # former primary once the leader changed: such a connection may still answer
105 # ping() (a node demoted behind a proxy) or hang until a TCP timeout (an
106 # isolated node), and every write would fail. The failover is detected as
107 # without the pool: a statement that fails (a write on a demoted node, or a
108 # connection error) makes _try() give the connection back (flagged by
109 # HandleError, it is closed) and retry with a new primary found by
110 # checkMaster(). When the DSN changes, the idle connections to the former one
111 # are closed at once instead of waiting for IdleTimeout, and those lent at
112 # this time are closed when given back (see _dropIdle()).
113 #
114 # Pooled connections run with AutoCommit on, but this store keeps one
115 # transaction per tie, as without the pool: its materialize() is the one of
116 # Apache::Session::Store::Postgres (SELECT ... FOR UPDATE), and the row lock
117 # must last until untie so that two requests updating the same session wait
118 # for each other instead of overwriting each other's changes. The
119 # transaction starts here, before the first statement on the lent
120 # connection, and ends in DESTROY (see _endTransaction()).
121 3 ▶sub _pooledPrimary {
Subroutine _pooledPrimary: called CC=8 SCAR=27.7
122 3 my ( $self, $args, $commit ) = @_;
123 3 foreach ( 0 .. 1 ) {
124 3 ▶ if ( $self->{failure} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"failure"}) 0 3 3
125 my $failed = Web::Session::Store::Postgres::_poolKey(
126 0 @{$args}{qw(DataSource UserName)} );
127 0 ▶ $self->checkMaster($args) or warn "Patroni check failed";
Branch: 1 branch
BranchTrueFalseTotal
$self->checkMaster($args) or warn "Patroni check failed" 0 0 0
128 Web::Session::Store::Postgres::_dropIdle($failed)
129 if ( $failed ne Web::Session::Store::Postgres::_poolKey(
130 0 ▶ @{$args}{qw(DataSource UserName)} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $failed ne Web::Session::Store::Postgres::_poolKey(@{$args;}{"DataSource", "UserName"}) 0 0 0
131 }
132 3 eval {
133 $self->Web::Session::Store::Postgres::_pooledConnection(
134 3 @{$args}{qw(DataSource UserName Password)}, $args );
135 };
136 3 ▶ if ( $@ and !$_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $_) { } 0 3 3
Condition33%1/3
$@ and not $_
!ll&&!rl&&r
300
MC/DC0%0/2
$@ and not $_
$@ not $_
Truth table33%1/3
A and not(B)
A= $@B= not $_
ABresult
0X0
100
111
137 0 $self->{failure} = 1;
138 }
139 else {
140 3 last;
141 }
142 }
143 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
144 3 $self->{dbh}->begin_work;
145 3 $self->{commit} = $commit;
146 }
147  
148 # Ends the transaction of a tie on a pooled connection: committed if the
149 # store commits and no statement failed, rolled back otherwise. A connection
150 # whose transaction can't be ended is flagged, so that _giveBack() closes it:
151 # a connection never goes back to the pool inside a transaction
152 3 ▶sub _endTransaction {
Subroutine _endTransaction: called CC=14 SCAR=39.8
153 3 my ($self) = @_;
154 3 ▶ my $dbh = $self->{dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->{"dbh"} or return 3 0 3
155  
156 # Lent when the process forked: the transaction is the parent's
157 3 ▶ return if ( ( $dbh->{private_ws_pid} // 0 ) != $$ );
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"private_ws_pid"} // 0) != $$ 0 3 3
Condition50%1/2
$dbh->{"private_ws_pid"} // 0
l!l
30
MC/DC0%0/1
$dbh->{"private_ws_pid"} // 0
$dbh->{"private_ws_pid"}
Truth table50%1/2
A
A= $dbh->{"private_ws_pid"}
Aresult
11
00
158 3 local $@;
159  
160 # End of the program: the session is committed as without the pool, the
161 # connection is not given back (see _giveBack())
162 3 ▶ if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${^GLOBAL_PHASE} eq "DESTRUCT") 0 3 3
163 0 eval {
164 $dbh->commit
165 0 ▶ if ( $self->{commit} and $dbh->{Active} and !$dbh->{AutoCommit} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"commit"} and $dbh->{"Active"} and not $dbh->{"AutoCommit"} 0 0 0
Condition0%0/6
$self->{"commit"} and $dbh->{"Active"} and not $dbh->{"AutoCommit"}
!ll&&!rl&&r
000
$self->{"commit"} and $dbh->{"Active"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"commit"} and $dbh->{"Active"} and not $dbh->{"AutoCommit"}
$self->{"commit"} $dbh->{"Active"} not $dbh->{"AutoCommit"}
Truth table0%0/4
A and B and not(C)
A= $self->{"commit"}B= $dbh->{"Active"}C= not $dbh->{"AutoCommit"}
ABCresult
0XX0
10X0
1100
1111
166 };
167 0 return;
168 }
169 3 ▶ return if ( $dbh->{AutoCommit} );
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"AutoCommit"} 0 3 3
170 3 my $ok = eval {
171 ( $self->{commit} and !$dbh->{private_ws_error} )
172 3 ▶ ? $dbh->commit
Branch: 1 branch
BranchTrueFalseTotal
$self->{'commit'} && !$dbh->{'private_ws_error'} ? : 3 0 3
Condition33%1/3
$self->{"commit"} and not $dbh->{"private_ws_error"}
!ll&&!rl&&r
003
MC/DC0%0/2
$self->{"commit"} and not $dbh->{"private_ws_error"}
$self->{"commit"} not $dbh->{"private_ws_error"}
Truth table33%1/3
A and not(B)
A= $self->{"commit"}B= not $dbh->{"private_ws_error"}
ABresult
0X0
100
111
173 : $dbh->rollback;
174 3 1;
175 };
176 3 ▶ unless ( $ok and $dbh->{AutoCommit} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and $dbh->{"AutoCommit"}) 0 3 3
Condition33%1/3
$ok and $dbh->{"AutoCommit"}
!ll&&!rl&&r
003
MC/DC0%0/2
$ok and $dbh->{"AutoCommit"}
$ok $dbh->{"AutoCommit"}
Truth table33%1/3
A and B
A= $okB= $dbh->{"AutoCommit"}
ABresult
0X0
100
111
177 0 ▶ eval { $dbh->rollback } unless ( $dbh->{AutoCommit} );
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->{"AutoCommit"} 0 0 0
178 0 $dbh->{private_ws_error} = 1;
179 }
180 }
181  
182 # Apache::Session::Store::Postgres::DESTROY() commits: not in a child
183 # created by fork() on the parent's handle (see _forgetInheritedHandle() in
184 # Store/DBI.pm). A pooled connection ends its transaction and is given back
185 14 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
186 14 my $self = shift;
187 14 ▶ return if ( Web::Session::Store::DBI::_forgetInheritedHandle($self) );
Branch: 1 branch
BranchTrueFalseTotal
if Web::Session::Store::DBI::_forgetInheritedHandle($self) 1 13 14
188 13 ▶ if ( defined $self->{pool_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pool_key"}) 3 10 13
189 3 $self->_endTransaction;
190 3 return $self->Web::Session::Store::Postgres::_giveBack;
191 }
192 10 $self->SUPER::DESTROY(@_);
193 }
194  
195 3 ▶sub _try {
Subroutine _try: called CC=6 SCAR=21.5
196 3 my $self = shift;
197 3 my $sub = shift;
198 3 my $res;
199  
200 3 foreach ( 0 .. 1 ) {
201 $res =
202 3 eval { Apache::Session::Store::Postgres->can($sub)->( $self, @_ ) };
203 3 ▶ if ( $@ and $@ !~ /Object does not exist/ and !$_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /Object does not exist/ and not $_) { } 0 3 3
Condition33%2/6
$@ and not $@ =~ /Object does not exist/ and not $_
!ll&&!rl&&r
300
$@ and not $@ =~ /Object does not exist/
!ll&&!rl&&r
300
MC/DC0%0/3
$@ and not $@ =~ /Object does not exist/ and not $_
$@ not $@ =~ /Object does not exist/ not $_
Truth table25%1/4
A and not(B) and not(C)
A= $@B= not $@ =~ /Object does not exist/C= not $_
ABCresult
0XX0
10X0
1100
1111
204 0 $self->{failure} = 1;
205 0 $self->DESTROY;
206 0 delete $self->{"${sub}_sth"};
207 0 delete $self->{dbh};
208 }
209 else {
210 3 last;
211 }
212 }
213 3 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
214 3 return $res;
215 }
216  
217 4 ▶sub insert {
Subroutine insert: called CC=1 SCAR=0.0
Pod undocumented
218 4 my $self = shift;
219 4 return $self->_try( 'insert', @_ );
220 }
221  
222 # update() and materialize() keep the serialized data that the database
223 # holds, and update() doesn't rewrite an unchanged session, as the stores
224 # inheriting from Web::Session::Store::DBI (see "Unchanged sessions are not
225 # rewritten" there)
226 4 ▶sub update {
Subroutine update: called CC=2 SCAR=6.9
Pod undocumented
227 4 my $self = shift;
228 4 ▶ return if ( Web::Session::Store::DBI::_unchanged( $self, $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if Web::Session::Store::DBI::_unchanged($self, $_[0]) 2 2 4
229 2 my $res = $self->_try( 'update', @_ );
230 2 Web::Session::Store::DBI::_remember( $self, $_[0] );
231 2 return $res;
232 }
233  
234 1 ▶sub materialize {
Subroutine materialize: called CC=1 SCAR=0.0
Pod undocumented
235 1 my $self = shift;
236 1 my $res = $self->_try( 'materialize', @_ );
237 1 Web::Session::Store::DBI::_remember( $self, $_[0] );
238 1 return $res;
239 }
240  
241 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my $self = shift;
243 0 return $self->_try( 'remove', @_ );
244 }
245  
246 0 ▶sub checkMaster {
Subroutine checkMaster: not called CC=24 SCAR=64.0
Pod undocumented
247 0 my ( $self, $args ) = @_;
248 0 delete $self->{failure};
249  
250 my $originalDataSource =
251 0 ▶ $self->{_originalDataSource} || $args->{DataSource};
Condition0%0/3
$self->{'_originalDataSource'} || $args->{'DataSource'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_originalDataSource'} || $args->{'DataSource'}
$self->{'_originalDataSource'} $args->{'DataSource'}
Truth table0%0/3
A || B
A= $self->{'_originalDataSource'}B= $args->{'DataSource'}
ABresult
1X1
011
000
252 0 ▶ my $cache = $patroniCache{$originalDataSource} ||= {};
Condition0%0/2
$Web::Session::Store::Patroni::patroniCache{$originalDataSource} ||= {}
l!l
00
MC/DC0%0/1
$Web::Session::Store::Patroni::patroniCache{$originalDataSource} ||= {}
$Web::Session::Store::Patroni::patroniCache{$originalDataSource}
Truth table0%0/2
A
A= $Web::Session::Store::Patroni::patroniCache{$originalDataSource}
Aresult
11
00
253  
254 # Circuit breaker: avoid hammering Patroni API if it's failing
255 0 ▶ my $circuitBreakerDelay = $args->{PatroniCircuitBreakerDelay} || 30;
Condition0%0/2
$args->{'PatroniCircuitBreakerDelay'} || 30
l!l
00
MC/DC0%0/1
$args->{'PatroniCircuitBreakerDelay'} || 30
$args->{'PatroniCircuitBreakerDelay'}
Truth table0%0/2
A
A= $args->{'PatroniCircuitBreakerDelay'}
Aresult
11
00
256 0 ▶ if ( $cache->{lastFailure}
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->{"lastFailure"} and time - $cache->{"lastFailure"} < $circuitBreakerDelay) 0 0 0
Condition0%0/3
$cache->{"lastFailure"} and time - $cache->{"lastFailure"} < $circuitBreakerDelay
!ll&&!rl&&r
000
MC/DC0%0/2
$cache->{"lastFailure"} and time - $cache->{"lastFailure"} < $circuitBreakerDelay
$cache->{"lastFailure"} time - $cache->{"lastFailure"} < $circuitBreakerDelay
Truth table0%0/3
A and B
A= $cache->{"lastFailure"}B= time - $cache->{"lastFailure"} < $circuitBreakerDelay
ABresult
0X0
100
111
257 and ( time() - $cache->{lastFailure} ) < $circuitBreakerDelay )
258 {
259 # Circuit breaker active, try cached leader as fallback
260 0 return $self->_useCachedLeader( $args, $originalDataSource,
261 "Circuit breaker active" );
262 }
263  
264 0 require JSON;
265 0 require LWP::UserAgent;
266 0 require IO::Socket::SSL;
267  
268 # SSL verification: secure by default, can be disabled with PatroniVerifySSL => 0
269 0 ▶ my $verify_ssl = $args->{PatroniVerifySSL} // 1;
Condition0%0/2
$args->{"PatroniVerifySSL"} // 1
l!l
00
MC/DC0%0/1
$args->{"PatroniVerifySSL"} // 1
$args->{"PatroniVerifySSL"}
Truth table0%0/2
A
A= $args->{"PatroniVerifySSL"}
Aresult
11
00
270 0 my %ssl_opts;
271 0 ▶ if ($verify_ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verify_ssl) { } 0 0 0
272 %ssl_opts = (
273 verify_hostname => 1,
274 SSL_verify_mode => &IO::Socket::SSL::SSL_VERIFY_PEER,
275 ( $args->{PatroniSSLCAFile} ? ( SSL_ca_file => $args->{PatroniSSLCAFile} ) : () ),
276 0 ▶ ( $args->{PatroniSSLCAPath} ? ( SSL_ca_path => $args->{PatroniSSLCAPath} ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$args->{'PatroniSSLCAFile'} ? : 0 0 0
$args->{'PatroniSSLCAPath'} ? : 0 0 0
277 );
278 }
279 else {
280 0 %ssl_opts = (
281 verify_hostname => 0,
282 SSL_verify_mode => &IO::Socket::SSL::SSL_VERIFY_NONE,
283 );
284 }
285  
286 my $ua = LWP::UserAgent->new(
287 env_proxy => 1,
288 ssl_opts => \%ssl_opts,
289 0 ▶ timeout => $args->{PatroniTimeout} || 3,
Condition0%0/2
$args->{'PatroniTimeout'} || 3
l!l
00
MC/DC0%0/1
$args->{'PatroniTimeout'} || 3
$args->{'PatroniTimeout'}
Truth table0%0/2
A
A= $args->{'PatroniTimeout'}
Aresult
11
00
290 );
291 0 my $res;
292  
293 0 ▶ foreach my $patroniUrl ( split /[,\s]\s*/,
Condition0%0/3
$args->{'PatroniUrl'} || $args->{'patroniUrl'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{'PatroniUrl'} || $args->{'patroniUrl'}
$args->{'PatroniUrl'} $args->{'patroniUrl'}
Truth table0%0/3
A || B
A= $args->{'PatroniUrl'}B= $args->{'patroniUrl'}
ABresult
1X1
011
000
294 ( $args->{PatroniUrl} || $args->{patroniUrl} ) )
295 {
296 0 my $resp = $ua->get($patroniUrl);
297 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) 0 0 0
298 0 my $c = eval { JSON::from_json( $resp->decoded_content ) };
299 0 ▶ if ( $@ or !$c->{members} or ref( $c->{members} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $c->{"members"} or ref $c->{"members"} ne "ARRAY") 0 0 0
Condition0%0/6
$@ or not $c->{"members"} or ref $c->{"members"} ne "ARRAY"
l!l&&r!l&&!r
000
$@ or not $c->{"members"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or not $c->{"members"} or ref $c->{"members"} ne "ARRAY"
$@ not $c->{"members"} ref $c->{"members"} ne "ARRAY"
Truth table0%0/4
A or not(B) or C
A= $@B= not $c->{"members"}C= ref $c->{"members"} ne "ARRAY"
ABCresult
1XX1
01X1
0011
0000
300 0 print STDERR "Bad response from $patroniUrl: "
301 . $resp->decoded_content . "\n";
302 0 next;
303 }
304  
305 0 my @leaders = grep { $_->{role} eq 'leader' } @{ $c->{members} };
306  
307 # Check for split-brain scenario
308 0 ▶ if ( @leaders > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@leaders > 1) 0 0 0
309 my $leadersList =
310 0 join( ', ', map { "$_->{host}:$_->{port}" } @leaders );
311 0 print STDERR
312 "Multiple leaders detected (split-brain) from $patroniUrl"
313 . " - Leaders: $leadersList\n";
314 0 next;
315 }
316  
317 0 my ($leader) = @leaders;
318 0 ▶ unless ($leader) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leader) 0 0 0
319 0 print STDERR "No leader found from $patroniUrl: "
320 . $resp->decoded_content . "\n";
321 0 next;
322 }
323  
324 # Validate leader has required fields
325 0 ▶ unless ( defined $leader->{host} && defined $leader->{port} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $leader->{"host"} and defined $leader->{"port"}) 0 0 0
Condition0%0/3
defined $leader->{"host"} and defined $leader->{"port"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $leader->{"host"} and defined $leader->{"port"}
defined $leader->{"host"} defined $leader->{"port"}
Truth table0%0/3
A and B
A= defined $leader->{"host"}B= defined $leader->{"port"}
ABresult
0X0
100
111
326 0 print STDERR "Leader missing host or port from $patroniUrl: "
327 . $resp->decoded_content . "\n";
328 0 next;
329 }
330  
331 # Check leader health state
332 0 ▶ if ( $leader->{state} && $leader->{state} ne 'running' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leader->{"state"} and $leader->{"state"} ne "running") 0 0 0
Condition0%0/3
$leader->{"state"} and $leader->{"state"} ne "running"
!ll&&!rl&&r
000
MC/DC0%0/2
$leader->{"state"} and $leader->{"state"} ne "running"
$leader->{"state"} $leader->{"state"} ne "running"
Truth table0%0/3
A and B
A= $leader->{"state"}B= $leader->{"state"} ne "running"
ABresult
0X0
100
111
333 0 print STDERR
334 "Leader not in running state (state=$leader->{state})"
335 . " from $patroniUrl\n";
336 0 next;
337 }
338  
339 # Cache the leader info
340 $cache->{leader} = {
341 host => $leader->{host},
342 port => $leader->{port},
343 0 time => time()
344 };
345  
346 # Reset circuit breaker on success
347 0 delete $cache->{lastFailure};
348  
349 $args->{DataSource} =
350 0 _buildDataSource( $originalDataSource, $leader );
351 0 $res = 1;
352 0 last;
353 }
354 }
355  
356 # If API failed, record for circuit breaker and try cached leader
357 0 ▶ unless ($res) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 0 0 0
358 0 $cache->{lastFailure} = time();
359 0 $res = $self->_useCachedLeader( $args, $originalDataSource,
360 "Patroni API unavailable" );
361 }
362  
363 0 return $res;
364 }
365  
366 # Use cached leader as fallback
367 0 ▶sub _useCachedLeader {
Subroutine _useCachedLeader: not called CC=6 SCAR=37.4
368 0 my ( $self, $args, $originalDataSource, $reason ) = @_;
369  
370 0 ▶ my $cache = $patroniCache{$originalDataSource} || {};
Condition0%0/2
$Web::Session::Store::Patroni::patroniCache{$originalDataSource} || {}
l!l
00
MC/DC0%0/1
$Web::Session::Store::Patroni::patroniCache{$originalDataSource} || {}
$Web::Session::Store::Patroni::patroniCache{$originalDataSource}
Truth table0%0/2
A
A= $Web::Session::Store::Patroni::patroniCache{$originalDataSource}
Aresult
11
00
371 0 ▶ my $cacheTTL = $args->{PatroniCacheTTL} || 60;
Condition0%0/2
$args->{'PatroniCacheTTL'} || 60
l!l
00
MC/DC0%0/1
$args->{'PatroniCacheTTL'} || 60
$args->{'PatroniCacheTTL'}
Truth table0%0/2
A
A= $args->{'PatroniCacheTTL'}
Aresult
11
00
372  
373 0 ▶ if ( $cache->{leader}
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->{"leader"} and $cache->{"leader"}{"time"} and time - $cache->{"leader"}{"time"} < $cacheTTL) 0 0 0
Condition0%0/6
$cache->{"leader"} and $cache->{"leader"}{"time"} and time - $cache->{"leader"}{"time"} < $cacheTTL
!ll&&!rl&&r
000
$cache->{"leader"} and $cache->{"leader"}{"time"}
!ll&&!rl&&r
000
MC/DC0%0/3
$cache->{"leader"} and $cache->{"leader"}{"time"} and time - $cache->{"leader"}{"time"} < $cacheTTL
$cache->{"leader"} $cache->{"leader"}{"time"} time - $cache->{"leader"}{"time"} < $cacheTTL
Truth table0%0/4
A and B and C
A= $cache->{"leader"}B= $cache->{"leader"}{"time"}C= time - $cache->{"leader"}{"time"} < $cacheTTL
ABCresult
0XX0
10X0
1100
1111
374 and $cache->{leader}->{time}
375 and ( time() - $cache->{leader}->{time} ) < $cacheTTL )
376 {
377 0 my $age = time() - $cache->{leader}->{time};
378 0 print STDERR "$reason, using cached leader (${age}s old)\n";
379 $args->{DataSource} =
380 0 _buildDataSource( $originalDataSource, $cache->{leader} );
381 0 return 1;
382 }
383 0 return 0;
384 }
385  
386 # Build DataSource string with new host/port
387 0 ▶sub _buildDataSource {
Subroutine _buildDataSource: not called CC=2 SCAR=17.9
388 0 my ( $originalDataSource, $leader ) = @_;
389  
390 0 my $chain = $originalDataSource;
391  
392 # Remove existing host/port parameters
393 0 $chain =~ s/;\s*host=[^;]+//gi;
394 0 $chain =~ s/;\s*port=[^;]+//gi;
395 0 $chain =~ s/\s+host=[^\s;]+//gi;
396 0 $chain =~ s/\s+port=[^\s;]+//gi;
397  
398 # Clean up trailing semicolons
399 0 $chain =~ s/;+$//;
400  
401 # Add new host and port
402 0 ▶ my $separator = ( $chain =~ /:$/ ) ? '' : ';';
Branch: 1 branch
BranchTrueFalseTotal
$chain =~ /:$/ ? : 0 0 0
403 0 return "${chain}${separator}host=$leader->{host};port=$leader->{port}";
404 }
405  
406 10 1;
↑ Summary Session/Store/LDAP.pm »