Skip to content

Commit 11fe78b

Browse files
committed
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. Signed-off-by: Marcelo Henrique Neppel <[email protected]>
1 parent 3346862 commit 11fe78b

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

src/charm.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
ServiceStatus,
9595
)
9696
from requests import ConnectionError as RequestsConnectionError
97-
from tenacity import RetryError, Retrying, stop_after_attempt, stop_after_delay, wait_fixed
97+
from tenacity import RetryError, Retrying, stop_after_delay, wait_fixed
9898

9999
from backups import CANNOT_RESTORE_PITR, S3_BLOCK_MESSAGES, PostgreSQLBackups
100100
from config import CharmConfig
@@ -2237,18 +2237,11 @@ def _handle_postgresql_restart_need(self):
22372237
self._patroni.reload_patroni_configuration()
22382238
except Exception as e:
22392239
logger.error(f"Reload patroni call failed! error: {e!s}")
2240-
# Wait for some more time than the Patroni's loop_wait default value (10 seconds),
2241-
# which tells how much time Patroni will wait before checking the configuration
2242-
# file again to reload it.
2243-
try:
2244-
for attempt in Retrying(stop=stop_after_attempt(5), wait=wait_fixed(3)):
2245-
with attempt:
2246-
restart_postgresql = restart_postgresql or self.postgresql.is_restart_pending()
2247-
if not restart_postgresql:
2248-
raise Exception
2249-
except RetryError:
2250-
# Ignore the error, as it happens only to indicate that the configuration has not changed.
2251-
pass
2240+
2241+
restart_pending = self._patroni.is_restart_pending()
2242+
logger.debug(f"Checking if restart pending: {restart_postgresql} or {restart_pending}")
2243+
restart_postgresql = restart_postgresql or restart_pending
2244+
22522245
self.unit_peer_data.update({"tls": "enabled" if self.is_tls_enabled else ""})
22532246
self.postgresql_client_relation.update_tls_flag("True" if self.is_tls_enabled else "False")
22542247

src/patroni.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,23 @@ def is_replication_healthy(self) -> bool:
364364
logger.debug("replication is healthy")
365365
return True
366366

367+
def is_restart_pending(self) -> bool:
368+
"""Returns whether the Patroni/PostgreSQL restart pending."""
369+
patroni_status = requests.get(
370+
f"{self._patroni_url}/patroni",
371+
verify=self._verify,
372+
timeout=PATRONI_TIMEOUT,
373+
auth=self._patroni_auth,
374+
)
375+
try:
376+
pending_restart = patroni_status.json()["pending_restart"]
377+
except KeyError:
378+
pending_restart = False
379+
pass
380+
logger.debug(f"Patroni API is_restart_pending: {pending_restart}")
381+
382+
return pending_restart
383+
367384
@property
368385
def primary_endpoint_ready(self) -> bool:
369386
"""Is the primary endpoint redirecting connections to the primary pod.

tests/unit/test_charm.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,20 +1590,19 @@ def test_handle_postgresql_restart_need(harness):
15901590

15911591
_is_tls_enabled.return_value = values[0]
15921592
postgresql_mock.is_tls_enabled = PropertyMock(return_value=values[1])
1593-
postgresql_mock.is_restart_pending = PropertyMock(return_value=values[2])
1594-
1595-
harness.charm._handle_postgresql_restart_need()
1596-
_reload_patroni_configuration.assert_called_once()
1597-
if values[0]:
1598-
assert "tls" in harness.get_relation_data(rel_id, harness.charm.unit)
1599-
else:
1600-
assert "tls" not in harness.get_relation_data(rel_id, harness.charm.unit)
1601-
if (values[0] != values[1]) or values[2]:
1602-
_generate_metrics_jobs.assert_called_once_with(values[0])
1603-
_restart.assert_called_once()
1604-
else:
1605-
_generate_metrics_jobs.assert_not_called()
1606-
_restart.assert_not_called()
1593+
with patch("charm.Patroni.is_restart_pending", return_value=values[2]):
1594+
harness.charm._handle_postgresql_restart_need()
1595+
_reload_patroni_configuration.assert_called_once()
1596+
if values[0]:
1597+
assert "tls" in harness.get_relation_data(rel_id, harness.charm.unit)
1598+
else:
1599+
assert "tls" not in harness.get_relation_data(rel_id, harness.charm.unit)
1600+
if (values[0] != values[1]) or values[2]:
1601+
_generate_metrics_jobs.assert_called_once_with(values[0])
1602+
_restart.assert_called_once()
1603+
else:
1604+
_generate_metrics_jobs.assert_not_called()
1605+
_restart.assert_not_called()
16071606

16081607

16091608
def test_set_active_status(harness):

0 commit comments

Comments
 (0)