Session/Store/Patroni.pm

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