| 1 | # -*-cperl-*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # DBD::Patroni - DBI driver for PostgreSQL with Patroni cluster support | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Copyright (c) 2025 Xavier Guimard | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # License or the Artistic License, as specified in the Perl README file. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | package DBD::Patroni; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | require DBD::Pg; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | our $VERSION = '0.03'; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | our $drh = undef; # Driver handle | ||||||||||||||||||||||||||||||||||||||||||
| 20 | our $err = 0; # DBI error code | ||||||||||||||||||||||||||||||||||||||||||
| 21 | our $errstr = ''; # DBI error string | ||||||||||||||||||||||||||||||||||||||||||
| 22 | our $state = ''; # DBI state | ||||||||||||||||||||||||||||||||||||||||||
| 23 | our $rr_idx = 0; # Round-robin index for replica selection | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Load submodules | ||||||||||||||||||||||||||||||||||||||||||
| 26 | require DBD::Patroni::dr; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | require DBD::Patroni::db; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | require DBD::Patroni::st; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # DBI driver method - required for DBI->connect("dbi:Patroni:...") | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub driver { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
driver: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return $drh if $drh; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my ( $class, $attr ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | $class .= "::dr"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $drh = DBI::_new_drh( | |||||||||||||||||||||||||||||||||||||||||
| 38 | $class, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | Name => 'Patroni', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | Version => $VERSION, | ||||||||||||||||||||||||||||||||||||||||||
| 42 | Attribution => 'DBD::Patroni by Xavier Guimard', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | return $drh; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # Discover Patroni cluster via REST API | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _discover_cluster { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_discover_cluster: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my ( $urls, $timeout ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $timeout //= 3; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$timeout //= 3
MC/DC0%0/1
$timeout //= 3
$timeout
Truth table0%0/2
A
A= $timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | require JSON; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||
| 57 | timeout => $timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | env_proxy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | for my $url ( split /[,\s]+/, $urls ) { | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | next unless $url; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $resp = $ua->get($url); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | next unless $resp->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | my $data = eval { JSON::decode_json( $resp->decoded_content ) }; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | next if $@ or !$data->{members} or ref( $data->{members} ) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$@ or not $data->{"members"} or ref $data->{"members"} ne "ARRAY"
$@ or not $data->{"members"}
MC/DC0%0/3
$@ or not $data->{"members"} or ref $data->{"members"} ne "ARRAY"
$@
not $data->{"members"}
ref $data->{"members"} ne "ARRAY"
Truth table0%0/4
A or not(B) or C
A= $@B= not $data->{"members"}C= ref $data->{"members"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my ($leader) = grep { $_->{role} eq 'leader' } @{ $data->{members} }; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my @replicas = grep { $_->{role} ne 'leader' } @{ $data->{members} }; | |||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return ( $leader, @replicas ) if $leader; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # Select a replica based on load balancing mode | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 8 | ▶ | sub _select_replica { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_select_replica: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | my ( $replicas, $mode ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 8 | ▶ | return undef unless $replicas && @$replicas; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$replicas and @$replicas
MC/DC100%2/2
$replicas and @$replicas
$replicas
@$replicas
Truth table100%3/3
A and B
A= $replicasB= @$replicas
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 6 | ▶ | $mode //= 'round_robin'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mode //= "round_robin"
MC/DC0%0/1
$mode //= "round_robin"
$mode
Truth table50%1/2
A
A= $mode
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 6 | ▶ | if ( $mode eq 'random' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | return $replicas->[ int( rand(@$replicas) ) ]; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | elsif ( $mode eq 'leader_only' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | else { # round_robin | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | return $replicas->[ $rr_idx++ % @$replicas ]; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | # Parse and extract Patroni parameters from DSN | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | sub _parse_dsn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_dsn: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 5 | my ($dsn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 5 | my %params; | |||||||||||||||||||||||||||||||||||||||||
| 99 | my @remaining; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 5 | for my $part ( split /;/, $dsn ) { | |||||||||||||||||||||||||||||||||||||||||
| 102 | 13 | ▶ | if ( $part =~ /^(patroni_url|patroni_lb|patroni_timeout)=(.*)$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | $params{ lc($1) } = $2; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | push @remaining, $part; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | return ( join( ';', @remaining ), \%params ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | # Build DSN with host/port, cleaning up any existing host/port params | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | ▶ | sub _build_dsn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_dsn: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | my ( $dsn, $host, $port ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # Remove existing host/port parameters | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | $dsn =~ s/(?:host|port)=[^;]*;?//gi; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | # Clean up multiple semicolons and leading/trailing semicolons | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 5 | $dsn =~ s/;+/;/g; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 5 | $dsn =~ s/^;|;$//g; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | # Append new host/port | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 5 | return "$dsn;host=$host;port=$port"; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | # Detect read-only queries | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 10 | ▶ | sub _is_readonly { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_readonly: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 10 | my $sql = shift; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 10 | ▶ | return 0 unless defined $sql; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # SELECT or WITH ... SELECT (CTE) | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | return $sql =~ /^\s*(SELECT|WITH\s+\w+.*?\bSELECT)\b/si ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | # Detect connection errors | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 24 | ▶ | sub _is_connection_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_connection_error: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 24 | my $error = shift; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 24 | ▶ | return 0 unless $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 22 | ▶ | return 1 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | if $error =~ | ||||||||||||||||||||||||||||||||||||||||||
| 144 | /(?:c(?:o(?:nnection (?:re(?:fused|set)|timed out)|uld not connect)|annot execute .* in a read-only transaction)|t(?:he database system is (?:s(?:hutting down|tarting up)|in recovery mode)|erminating connection)|no(?: connection to the server|t accepting connections)|re(?:covery is in progress|ad-only transaction)|(?:server closed the|lost) connection|hot standby mode is disabled)/i; | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | # Execute with automatic retry on failure (shared helper) | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub _with_retry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_with_retry: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my ( $dbh, $target, $code ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my $wantarray = wantarray; | |||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | foreach my $attempt ( 0 .. 1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | my @results; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | @results = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | $result = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my $error = $@; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | # Only retry on connection errors, not SQL errors | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if ( _is_connection_error($error) && $attempt == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
_is_connection_error($error) and $attempt == 0
MC/DC0%0/2
_is_connection_error($error) and $attempt == 0
_is_connection_error($error)
$attempt == 0
Truth table0%0/3
A and B
A= _is_connection_error($error)B= $attempt == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | warn | |||||||||||||||||||||||||||||||||||||||||
| 172 | "DBD::Patroni: Connection error on $target, rediscovering cluster...\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ( DBD::Patroni::db::_rediscover_cluster($dbh) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | die $error; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | return $wantarray ? @results : $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||||||||||||||||