Skip to content

Commit b2527e3

Browse files
Use Patroni API for is_restart_pending()
1 parent 4d5e9a5 commit b2527e3

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
@@ -1216,23 +1216,6 @@ def update_user_password(
12161216
if connection is not None:
12171217
connection.close()
12181218

1219-
def is_restart_pending(self) -> bool:
1220-
"""Query pg_settings for pending restart."""
1221-
connection = None
1222-
try:
1223-
with self._connect_to_database() as connection, connection.cursor() as cursor:
1224-
cursor.execute("SELECT COUNT(*) FROM pg_settings WHERE pending_restart=True;")
1225-
return cursor.fetchone()[0] > 0
1226-
except psycopg2.OperationalError:
1227-
logger.warning("Failed to connect to PostgreSQL.")
1228-
return False
1229-
except psycopg2.Error as e:
1230-
logger.error(f"Failed to check if restart is pending: {e}")
1231-
return False
1232-
finally:
1233-
if connection:
1234-
connection.close()
1235-
12361219
@staticmethod
12371220
def build_postgresql_group_map(group_map: Optional[str]) -> List[Tuple]:
12381221
"""Build the PostgreSQL authorization group-map.

src/charm.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,18 +2408,11 @@ def _handle_postgresql_restart_need(self) -> None:
24082408
self._patroni.reload_patroni_configuration()
24092409
except Exception as e:
24102410
logger.error(f"Reload patroni call failed! error: {e!s}")
2411-
# Wait for some more time than the Patroni's loop_wait default value (10 seconds),
2412-
# which tells how much time Patroni will wait before checking the configuration
2413-
# file again to reload it.
2414-
try:
2415-
for attempt in Retrying(stop=stop_after_attempt(5), wait=wait_fixed(3)):
2416-
with attempt:
2417-
restart_postgresql = restart_postgresql or self.postgresql.is_restart_pending()
2418-
if not restart_postgresql:
2419-
raise Exception
2420-
except RetryError:
2421-
# Ignore the error, as it happens only to indicate that the configuration has not changed.
2422-
pass
2411+
2412+
restart_pending = self._patroni.is_restart_pending()
2413+
logger.debug(f"Checking if restart pending: {restart_postgresql} or {restart_pending}")
2414+
restart_postgresql = restart_postgresql or restart_pending
2415+
24232416
self.unit_peer_data.update({"tls": "enabled" if self.is_tls_enabled else ""})
24242417
self.postgresql_client_relation.update_endpoints()
24252418

src/cluster.py

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

440440
return r.json()
441441

442+
def is_restart_pending(self) -> bool:
443+
"""Returns whether the Patroni/PostgreSQL restart pending."""
444+
patroni_status = requests.get(
445+
f"{self._patroni_url}/patroni",
446+
verify=self.verify,
447+
timeout=API_REQUEST_TIMEOUT,
448+
auth=self._patroni_auth,
449+
)
450+
try:
451+
pending_restart = patroni_status.json()["pending_restart"]
452+
except KeyError:
453+
pending_restart = False
454+
pass
455+
logger.debug(f"Patroni API is_restart_pending: {pending_restart}")
456+
457+
return pending_restart
458+
442459
@property
443460
def is_creating_backup(self) -> bool:
444461
"""Returns whether a backup is being created."""

0 commit comments

Comments
 (0)