| 1 |
package Lemonldap::NG::Common::Conf::Backends::Patroni; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Lemonldap::NG::Common::Conf::Backends::_DBI; |
| 5 |
use Lemonldap::NG::Common::Conf::Backends::CDBI; |
| 6 |
our @ISA = ('Lemonldap::NG::Common::Conf::Backends::CDBI'); |
| 7 |
|
| 8 |
*store = \&Lemonldap::NG::Common::Conf::Backends::CDBI::store; |
| 9 |
*load = \&Lemonldap::NG::Common::Conf::Backends::CDBI::load; |
| 10 |
|
| 11 |
sub beforeRetry { |
| 12 |
my ($self) = @_; |
| 13 |
require Lemonldap::NG::Common::UserAgent; |
| 14 |
require JSON; |
| 15 |
my $ua = Lemonldap::NG::Common::UserAgent->new($self); |
| 16 |
$ua->timeout( $self->{patroniTimeout} || 3 ); |
| 17 |
my $res = 0; |
| 18 |
|
| 19 |
# Circuit breaker: avoid hammering Patroni API if it's failing |
| 20 |
my $circuitBreakerDelay = $self->{patroniCircuitBreakerDelay} || 30; |
| 21 |
my $skipApiQuery = |
| 22 |
( $self->{patroniLastFailure} |
| 23 |
&& time() - $self->{patroniLastFailure} < $circuitBreakerDelay ); |
| 24 |
|
| 25 |
# Try to query Patroni API (unless circuit breaker is active) |
| 26 |
# URLs are queried in order - put preferred (local) endpoints first |
| 27 |
if ( !$skipApiQuery ) { |
| 28 |
foreach my $patroniUrl ( split /,\s*/, $self->{patroniUrl} ) { |
| 29 |
my $resp = $ua->get($patroniUrl); |
| 30 |
if ( $resp->is_success ) { |
| 31 |
my $c = eval { JSON::from_json( $resp->decoded_content ) }; |
| 32 |
if ( $@ or !$c->{members} or ref( $c->{members} ) ne 'ARRAY' ) { |
| 33 |
print STDERR "Bad response from $patroniUrl: " |
| 34 |
. $resp->decoded_content . "\n"; |
| 35 |
next; |
| 36 |
} |
| 37 |
my @leaders = |
| 38 |
grep { $_->{role} eq 'leader' } @{ $c->{members} }; |
| 39 |
|
| 40 |
# Check for split-brain scenario |
| 41 |
if ( @leaders > 1 ) { |
| 42 |
my $leadersList = |
| 43 |
join( ', ', map { "$_->{host}:$_->{port}" } @leaders ); |
| 44 |
print STDERR |
| 45 |
"Multiple leaders detected (split-brain) from $patroniUrl - Leaders: $leadersList\n"; |
| 46 |
next; |
| 47 |
} |
| 48 |
|
| 49 |
my ($leader) = @leaders; |
| 50 |
unless ($leader) { |
| 51 |
print STDERR "No leader found from $patroniUrl: " |
| 52 |
. $resp->decoded_content . "\n"; |
| 53 |
next; |
| 54 |
} |
| 55 |
unless ( $leader->{host} && $leader->{port} ) { |
| 56 |
print STDERR |
| 57 |
"Leader missing host or port from $patroniUrl: " |
| 58 |
. $resp->decoded_content . "\n"; |
| 59 |
next; |
| 60 |
} |
| 61 |
|
| 62 |
# Check leader health state |
| 63 |
if ( $leader->{state} && $leader->{state} ne 'running' ) { |
| 64 |
print STDERR |
| 65 |
"Leader not in running state (state=$leader->{state}) from $patroniUrl\n"; |
| 66 |
next; |
| 67 |
} |
| 68 |
|
| 69 |
# Cache the leader info |
| 70 |
$self->{patroniLastLeader} = { |
| 71 |
host => $leader->{host}, |
| 72 |
port => $leader->{port}, |
| 73 |
time => time() |
| 74 |
}; |
| 75 |
|
| 76 |
delete $self->{_dbh}; |
| 77 |
_updateDbiChain( $self, $leader->{host}, $leader->{port} ); |
| 78 |
|
| 79 |
# Reset circuit breaker on success |
| 80 |
delete $self->{patroniLastFailure}; |
| 81 |
|
| 82 |
$res = 1; |
| 83 |
last; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
# Record failure for circuit breaker |
| 88 |
if ( !$res ) { |
| 89 |
$self->{patroniLastFailure} = time(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
# Fallback to cached leader if available and not too old |
| 94 |
if ( !$res && $self->{patroniLastLeader} ) { |
| 95 |
my $cacheTtl = $self->{patroniCacheTTL} || 60; |
| 96 |
my $age = time() - $self->{patroniLastLeader}->{time}; |
| 97 |
if ( $age < $cacheTtl ) { |
| 98 |
print STDERR |
| 99 |
"Patroni API unavailable, using cached leader (${age}s old)\n"; |
| 100 |
delete $self->{_dbh}; |
| 101 |
_updateDbiChain( |
| 102 |
$self, |
| 103 |
$self->{patroniLastLeader}->{host}, |
| 104 |
$self->{patroniLastLeader}->{port} |
| 105 |
); |
| 106 |
$res = 1; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $res; |
| 111 |
} |
| 112 |
|
| 113 |
# Update dbiChain with new host and port |
| 114 |
sub _updateDbiChain { |
| 115 |
my ( $self, $host, $port ) = @_; |
| 116 |
|
| 117 |
# Remove existing host/port parameters more robustly |
| 118 |
my $chain = $self->{dbiChain}; |
| 119 |
|
| 120 |
# Remove host=... and port=... (handles both ;-separated and space-separated) |
| 121 |
$chain =~ s/;\s*host=[^;]+//gi; |
| 122 |
$chain =~ s/;\s*port=[^;]+//gi; |
| 123 |
$chain =~ s/\s+host=[^\s;]+//gi; |
| 124 |
$chain =~ s/\s+port=[^\s;]+//gi; |
| 125 |
|
| 126 |
# Clean up trailing semicolons |
| 127 |
$chain =~ s/;+$//; |
| 128 |
|
| 129 |
# Add new host and port |
| 130 |
my $separator = ( $chain =~ /:$/ ) ? '' : ';'; |
| 131 |
$self->{dbiChain} = "${chain}${separator}host=$host;port=$port"; |
| 132 |
|
| 133 |
return 1; |
| 134 |
} |
| 135 |
|
| 136 |
1; |