Skip to content

Commit f1c4356

Browse files
DPE-7726: Use Patroni API for is_restart_pending()
The previous is_restart_pending() waited for long due to the Patroni's loop_wait default value (10 seconds), which tells how much time Patroni will wait before checking the configuration file again to reload it. Instead of checking PostgreSQL pending_restart from pg_settings, let's check Patroni API pending_restart=True flag.
1 parent ee02d5a commit f1c4356

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

lib/charms/postgresql_k8s/v1/postgresql.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,23 +1322,6 @@ def update_user_password(
13221322
if connection is not None:
13231323
connection.close()
13241324

1325-
def is_restart_pending(self) -> bool:
1326-
"""Query pg_settings for pending restart."""
1327-
connection = None
1328-
try:
1329-
with self._connect_to_database() as connection, connection.cursor() as cursor:
1330-
cursor.execute("SELECT COUNT(*) FROM pg_settings WHERE pending_restart=True;")
1331-
return cursor.fetchone()[0] > 0
1332-
except psycopg2.OperationalError:
1333-
logger.warning("Failed to connect to PostgreSQL.")
1334-
return False
1335-
except psycopg2.Error as e:
1336-
logger.error(f"Failed to check if restart is pending: {e}")
1337-
return False
1338-
finally:
1339-
if connection:
1340-
connection.close()
1341-
13421325
def database_exists(self, db: str) -> bool:
13431326
"""Check whether specified database exists."""
13441327
connection = None

src/charm.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,18 +2473,11 @@ def _handle_postgresql_restart_need(self) -> None:
24732473
self._patroni.reload_patroni_configuration()
24742474
except Exception as e:
24752475
logger.error(f"Reload patroni call failed! error: {e!s}")
2476-
# Wait for some more time than the Patroni's loop_wait default value (10 seconds),
2477-
# which tells how much time Patroni will wait before checking the configuration
2478-
# file again to reload it.
2479-
try:
2480-
for attempt in Retrying(stop=stop_after_attempt(5), wait=wait_fixed(3)):
2481-
with attempt:
2482-
restart_postgresql = restart_postgresql or self.postgresql.is_restart_pending()
2483-
if not restart_postgresql:
2484-
raise Exception
2485-
except RetryError:
2486-
# Ignore the error, as it happens only to indicate that the configuration has not changed.
2487-
pass
2476+
2477+
restart_pending = self._patroni.is_restart_pending()
2478+
logger.debug(f"Checking if restart pending: {restart_postgresql} or {restart_pending}")
2479+
restart_postgresql = restart_postgresql or restart_pending
2480+
24882481
self.unit_peer_data.update({"tls": "enabled" if self.is_tls_enabled else ""})
24892482
self.postgresql_client_relation.update_endpoints()
24902483

src/cluster.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,23 @@ def get_patroni_health(self) -> dict[str, str]:
464464

465465
return r.json()
466466

467+
def is_restart_pending(self) -> bool:
468+
"""Returns whether the Patroni/PostgreSQL restart pending."""
469+
patroni_status = requests.get(
470+
f"{self._patroni_url}/patroni",
471+
verify=self.verify,
472+
timeout=API_REQUEST_TIMEOUT,
473+
auth=self._patroni_auth,
474+
)
475+
try:
476+
pending_restart = patroni_status.json()["pending_restart"]
477+
except KeyError:
478+
pending_restart = False
479+
pass
480+
logger.debug(f"Patroni API is_restart_pending: {pending_restart}")
481+
482+
return pending_restart
483+
467484
@property
468485
def is_creating_backup(self) -> bool:
469486
"""Returns whether a backup is being created."""

0 commit comments

Comments
 (0)