blib/lib/Lemonldap/NG/Common/Conf/Backends/Patroni.pm

filter: Statementstmt 92.0% 69 / 75 Branchbran 77.2% 17 / 22 Conditioncond 58.3% 14 / 24 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 78.0% 110 / 141 CC 22 SCAR 30.2
CC
22
Cov
83%
beforeRetry
25.0
_updateDbiChain
2.0
BEGIN
1.0
CRAP 20.5
« blib/lib/Lemonldap/NG/Common/Conf/Backends/File.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Notifications.pm »
1 package Lemonldap::NG::Common::Conf::Backends::Patroni;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Lemonldap::NG::Common::Conf::Backends::_DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Lemonldap::NG::Common::Conf::Backends::CDBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our @ISA = ('Lemonldap::NG::Common::Conf::Backends::CDBI');
7  
8 1 *store = \&Lemonldap::NG::Common::Conf::Backends::CDBI::store;
9 1 *load = \&Lemonldap::NG::Common::Conf::Backends::CDBI::load;
10  
11 14 ▶sub beforeRetry {
Subroutine beforeRetry: called CC=21 SCAR=32.2
Pod undocumented
12 14 my ($self) = @_;
13 14 require Lemonldap::NG::Common::UserAgent;
14 14 require JSON;
15 14 my $ua = Lemonldap::NG::Common::UserAgent->new($self);
16 14 ▶ $ua->timeout( $self->{patroniTimeout} || 3 );
Condition50%1/2
$self->{'patroniTimeout'} || 3
l!l
014
MC/DC0%0/1
$self->{'patroniTimeout'} || 3
$self->{'patroniTimeout'}
Truth table50%1/2
A
A= $self->{'patroniTimeout'}
Aresult
11
00
17 14 my $res = 0;
18  
19 # Circuit breaker: avoid hammering Patroni API if it's failing
20 14 ▶ my $circuitBreakerDelay = $self->{patroniCircuitBreakerDelay} || 30;
Condition50%1/2
$self->{'patroniCircuitBreakerDelay'} || 30
l!l
014
MC/DC0%0/1
$self->{'patroniCircuitBreakerDelay'} || 30
$self->{'patroniCircuitBreakerDelay'}
Truth table50%1/2
A
A= $self->{'patroniCircuitBreakerDelay'}
Aresult
11
00
21 my $skipApiQuery =
22 ( $self->{patroniLastFailure}
23 14 ▶ && time() - $self->{patroniLastFailure} < $circuitBreakerDelay );
Condition66%2/3
$self->{'patroniLastFailure'} && time - $self->{'patroniLastFailure'} < $circuitBreakerDelay
!ll&&!rl&&r
1103
MC/DC50%1/2
$self->{'patroniLastFailure'} && time - $self->{'patroniLastFailure'} < $circuitBreakerDelay
$self->{'patroniLastFailure'} time - $self->{'patroniLastFailure'} < $circuitBreakerDelay
Truth table66%2/3
A && B
A= $self->{'patroniLastFailure'}B= time - $self->{'patroniLastFailure'} < $circuitBreakerDelay
ABresult
0X0
100
111
24  
25 # Try to query Patroni API (unless circuit breaker is active)
26 # URLs are queried in order - put preferred (local) endpoints first
27 14 ▶ if ( !$skipApiQuery ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skipApiQuery) 11 3 14
28 11 foreach my $patroniUrl ( split /,\s*/, $self->{patroniUrl} ) {
29 11 my $resp = $ua->get($patroniUrl);
30 11 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) 10 1 11
31 10 my $c = eval { JSON::from_json( $resp->decoded_content ) };
32 10 ▶ 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 10 10
Condition33%2/6
$@ or not $c->{"members"} or ref $c->{"members"} ne "ARRAY"
l!l&&r!l&&!r
0010
$@ or not $c->{"members"}
l!l&&r!l&&!r
0010
MC/DC0%0/3
$@ or not $c->{"members"} or ref $c->{"members"} ne "ARRAY"
$@ not $c->{"members"} ref $c->{"members"} ne "ARRAY"
Truth table25%1/4
A or not(B) or C
A= $@B= not $c->{"members"}C= ref $c->{"members"} ne "ARRAY"
ABCresult
1XX1
01X1
0011
0000
33 0 print STDERR "Bad response from $patroniUrl: "
34 . $resp->decoded_content . "\n";
35 0 next;
36 }
37 my @leaders =
38 10 grep { $_->{role} eq 'leader' } @{ $c->{members} };
39  
40 # Check for split-brain scenario
41 10 ▶ if ( @leaders > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@leaders > 1) 1 9 10
42 my $leadersList =
43 1 join( ', ', map { "$_->{host}:$_->{port}" } @leaders );
44 1 print STDERR
45 "Multiple leaders detected (split-brain) from $patroniUrl - Leaders: $leadersList\n";
46 1 next;
47 }
48  
49 9 my ($leader) = @leaders;
50 9 ▶ unless ($leader) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leader) 0 9 9
51 0 print STDERR "No leader found from $patroniUrl: "
52 . $resp->decoded_content . "\n";
53 0 next;
54 }
55 9 ▶ unless ( $leader->{host} && $leader->{port} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($leader->{"host"} and $leader->{"port"}) 0 9 9
Condition33%1/3
$leader->{"host"} and $leader->{"port"}
!ll&&!rl&&r
009
MC/DC0%0/2
$leader->{"host"} and $leader->{"port"}
$leader->{"host"} $leader->{"port"}
Truth table33%1/3
A and B
A= $leader->{"host"}B= $leader->{"port"}
ABresult
0X0
100
111
56 0 print STDERR
57 "Leader missing host or port from $patroniUrl: "
58 . $resp->decoded_content . "\n";
59 0 next;
60 }
61  
62 # Check leader health state
63 9 ▶ if ( $leader->{state} && $leader->{state} ne 'running' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leader->{"state"} and $leader->{"state"} ne "running") 1 8 9
Condition100%3/3
$leader->{"state"} and $leader->{"state"} ne "running"
!ll&&!rl&&r
621
MC/DC100%2/2
$leader->{"state"} and $leader->{"state"} ne "running"
$leader->{"state"} $leader->{"state"} ne "running"
Truth table100%3/3
A and B
A= $leader->{"state"}B= $leader->{"state"} ne "running"
ABresult
0X0
100
111
64 1 print STDERR
65 "Leader not in running state (state=$leader->{state}) from $patroniUrl\n";
66 1 next;
67 }
68  
69 # Cache the leader info
70 $self->{patroniLastLeader} = {
71 host => $leader->{host},
72 port => $leader->{port},
73 8 time => time()
74 };
75  
76 8 delete $self->{_dbh};
77 8 _updateDbiChain( $self, $leader->{host}, $leader->{port} );
78  
79 # Reset circuit breaker on success
80 8 delete $self->{patroniLastFailure};
81  
82 8 $res = 1;
83 8 last;
84 }
85 }
86  
87 # Record failure for circuit breaker
88 11 ▶ if ( !$res ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res) 3 8 11
89 3 $self->{patroniLastFailure} = time();
90 }
91 }
92  
93 # Fallback to cached leader if available and not too old
94 14 ▶ if ( !$res && $self->{patroniLastLeader} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res and $self->{"patroniLastLeader"}) 2 12 14
Condition100%3/3
not $res and $self->{"patroniLastLeader"}
!ll&&!rl&&r
842
MC/DC100%2/2
not $res and $self->{"patroniLastLeader"}
not $res $self->{"patroniLastLeader"}
Truth table100%3/3
not(A) and B
A= not $resB= $self->{"patroniLastLeader"}
ABresult
0X0
100
111
95 2 ▶ my $cacheTtl = $self->{patroniCacheTTL} || 60;
Condition50%1/2
$self->{'patroniCacheTTL'} || 60
l!l
20
MC/DC0%0/1
$self->{'patroniCacheTTL'} || 60
$self->{'patroniCacheTTL'}
Truth table50%1/2
A
A= $self->{'patroniCacheTTL'}
Aresult
11
00
96 2 my $age = time() - $self->{patroniLastLeader}->{time};
97 2 ▶ if ( $age < $cacheTtl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($age < $cacheTtl) 2 0 2
98 2 print STDERR
99 "Patroni API unavailable, using cached leader (${age}s old)\n";
100 2 delete $self->{_dbh};
101 _updateDbiChain(
102 $self,
103 $self->{patroniLastLeader}->{host},
104 $self->{patroniLastLeader}->{port}
105 2 );
106 2 $res = 1;
107 }
108 }
109  
110 14 return $res;
111 }
112  
113 # Update dbiChain with new host and port
114 10 ▶sub _updateDbiChain {
Subroutine _updateDbiChain: called CC=2 SCAR=6.9
115 10 my ( $self, $host, $port ) = @_;
116  
117 # Remove existing host/port parameters more robustly
118 10 my $chain = $self->{dbiChain};
119  
120 # Remove host=... and port=... (handles both ;-separated and space-separated)
121 10 $chain =~ s/;\s*host=[^;]+//gi;
122 10 $chain =~ s/;\s*port=[^;]+//gi;
123 10 $chain =~ s/\s+host=[^\s;]+//gi;
124 10 $chain =~ s/\s+port=[^\s;]+//gi;
125  
126 # Clean up trailing semicolons
127 10 $chain =~ s/;+$//;
128  
129 # Add new host and port
130 10 ▶ my $separator = ( $chain =~ /:$/ ) ? '' : ';';
Branch: 1 branch
BranchTrueFalseTotal
$chain =~ /:$/ ? : 0 10 10
131 10 $self->{dbiChain} = "${chain}${separator}host=$host;port=$port";
132  
133 10 return 1;
134 }
135  
136 1 1;
« blib/lib/Lemonldap/NG/Common/Conf/Backends/File.pm ↑ Summary blib/lib/Lemonldap/NG/Common/Notifications.pm »