pg.ddx.io patroni/patroni GitHub issues and pull requests
help / color / mirror / Atom feed[patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
8+ messages / 3 participants
[nested] [flat]
* [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
@ 2026-11-17 18:31 "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
0 siblings, 0 replies; 8+ messages in thread
From: CyberDem0n (@CyberDem0n) @ 2026-11-17 18:31 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
The leader applies a new watchdog timeout with the keepalive that follows the leader key update with the new TTL. That left a few gaps:
- After a reload the HA loop already sleeps for the new `loop_wait`, while the watchdog is still armed with the timeout of the previous configuration. Not every DCS starts the next cycle right away when the TTL changes, e.g. Raft doesn't. Raising `ttl` 30->60 and `loop_wait` 10->25 with `safety_margin` 10 on Raft reset the host 20 seconds later. Now the next cycle starts right away if the armed timeout is shorter than the new `loop_wait`.
- The check of activation that the timeout isn't shorter than `loop_wait` wasn't repeated when a reload changed the timeout or `loop_wait`. With `ttl` 20, `safety_margin` 12 and `loop_wait` 10 the next keepalive set the timeout to 8 seconds, and the device fired during the next HA loop sleep. A device with a fixed 15 second timeout fired after `loop_wait` 10->18. Such a device is now disabled, like on activation. In `required` mode the primary is demoted instead of the host being reset, and is promoted again once the timing is fixed.
- After a TTL change every keepalive failed with `Error while sending keepalive` if the device doesn't support setting the timeout, or if the watchdog was replaced by `NullWatchdog`, e.g. after it was disabled because of an unsafe timeout.
Stacked on #3794: without it a `required` mode primary that is demoted because of the unsafe timeout would stay a replica with `watchdog_failed` until restart.
Extracted from #3786.
### Reproduction
Same setup as in #3794: single-node cluster on Raft with `ttl: 30`, `loop_wait: 10`, `watchdog.driver: testing`, and a FIFO reader on the device that emulates the softdog semantics.
| Scenario | master | this PR |
|---|---|---|
| `automatic`, `safety_margin: 10`: `edit-config -s ttl=60 -s loop_wait=25` | still armed with 20 seconds while the HA loop sleeps 25: host reset | `updated with 50 second timeout` right after the reload |
| `automatic`, `safety_margin: 12`: `edit-config -s ttl=20` | timeout set to 8 seconds with `loop_wait` 10: host reset | `loop_wait of 10 seconds is too long for watchdog 8 second timeout`, the device is disabled |
| device with a fixed 15 second timeout (no `WDIOF_SETTIMEOUT`): `edit-config -s ttl=40` | `Error while sending keepalive` (`EOPNOTSUPP`) on every loop | `updated with 15 second timeout` |
| the same device, then `edit-config -s loop_wait=18` | host reset | `loop_wait of 18 seconds is too long for watchdog 15 second timeout` right after the reload, the device is disabled |
| `required`, `safety_margin: 12`: `edit-config -s ttl=20`, then `ttl=30` | host reset | demoted, replica with `watchdog_failed`, promoted again 10 seconds after `ttl=30` |
New tests: `test_timing_reload`, `test_timing_reload_with_fixed_timeout_device` and `test_reload_config_wakes_up_ha_for_watchdog`.
inline patch 1: patroni/__main__.py, patroni/watchdog/base.py, tests/test_patroni.py and 1 more (+78/-4 lines) download inline-1.patch
diff --git a/patroni/__main__.py b/patroni/__main__.py
index 1ceaa2913..62df557ad 100644
--- a/patroni/__main__.py
+++ b/patroni/__main__.py
@@ -193,6 +193,10 @@ def reload_config(self, sighup: bool = False, local: Optional[bool] = False) ->
if local or received_new_cert:
self.api.reload_config(self.config['restapi'])
self.watchdog.reload_config(self.config)
+ if self.watchdog.needs_immediate_keepalive:
+ # Not every DCS starts the next cycle right away when the TTL changes, so the HA loop would sleep for
+ # the new loop_wait while the watchdog is still armed with the timeout of the previous configuration.
+ self.ha.wakeup()
self._last_effective_role = ROLE_CONFIG_SUFFIX_MAP.get(self.postgresql.role)
self._last_effective_pg_config = \
self.config.build_effective_postgresql_configuration(self.postgresql.role)
diff --git a/patroni/watchdog/base.py b/patroni/watchdog/base.py
index 5599226db..ed93fde3f 100644
--- a/patroni/watchdog/base.py
+++ b/patroni/watchdog/base.py
@@ -235,11 +235,14 @@ def keepalive(self) -> None:
self._disable()
self.impl = self.config.get_impl()
self._activate()
- if self.config.timeout != self.active_config.timeout:
- self.impl.set_timeout(self.config.timeout)
- if self.is_running:
+ timeout_changed = self.config.timeout != self.active_config.timeout
+ if self.impl.is_running and (timeout_changed or self.config.loop_wait != self.active_config.loop_wait):
+ # Same checks as on activation, a device might not support setting the timeout or might not accept
+ # one that covers the new loop_wait.
+ actual_timeout = self._set_timeout()
+ if timeout_changed and self.is_running:
logger.info("%s updated with %s second timeout, timing slack %s seconds",
- self.impl.describe(), self.impl.get_timeout(), self.config.timing_slack)
+ self.impl.describe(), actual_timeout, self.config.timing_slack)
self.active_config = self.config
except WatchdogError as e:
logger.error("Error while sending keepalive: %s", e)
@@ -249,6 +252,20 @@ def keepalive(self) -> None:
def is_running(self) -> bool:
return self.impl.is_running
+ @property
+ @synchronized
+ def needs_immediate_keepalive(self) -> bool:
+ """The timeout armed for the previous configuration would expire before the HA loop wakes up after the new
+ ``loop_wait``. Only the keepalive following the leader key update with the new TTL may apply the new timeout,
+ so it has to be sent right away."""
+ if not self.active or not self.impl.is_running:
+ return False
+ try:
+ # The actual timeout, because a device might not have accepted the configured one.
+ return self.impl.get_timeout() < self.config.loop_wait
+ except WatchdogError:
+ return True
+
@property
@synchronized
def is_healthy(self) -> bool:
diff --git a/tests/test_patroni.py b/tests/test_patroni.py
index 00f16b2cd..7f481d895 100644
--- a/tests/test_patroni.py
+++ b/tests/test_patroni.py
@@ -285,6 +285,12 @@ def test_reload_config(self):
self.p._get_tags = Mock(side_effect=Exception)
self.p.reload_config(local=True)
+ def test_reload_config_wakes_up_ha_for_watchdog(self):
+ with patch.object(self.p, 'watchdog', Mock(needs_immediate_keepalive=True)), \
+ patch.object(self.p.ha, 'wakeup') as mock_wakeup:
+ self.p.reload_config()
+ mock_wakeup.assert_called_once_with()
+
def test_reload_config_checks_certificate_when_local_config_changed(self):
"""A renewed certificate must be detected even when the local config changed too."""
self.p.api.reload_local_certificate = Mock(return_value=True)
diff --git a/tests/test_watchdog.py b/tests/test_watchdog.py
index 0eb2c717a..bfb3fbe68 100644
--- a/tests/test_watchdog.py
+++ b/tests/test_watchdog.py
@@ -238,6 +238,53 @@ def test_reacquire_after_mode_change(self):
self.assertTrue(watchdog.is_running)
watchdog.disable()
+ @patch('platform.system', Mock(return_value='Linux'))
+ @patch.object(LinuxWatchdogDevice, 'can_be_disabled', PropertyMock(return_value=True))
+ def test_timing_reload(self):
+ watchdog = Watchdog({'ttl': 30, 'loop_wait': 10, 'watchdog': {'safety_margin': 10}})
+ self.assertTrue(watchdog.activate())
+ device = mock_devices[-1]
+ self.assertFalse(watchdog.needs_immediate_keepalive)
+ # the new configuration is safe, but the next HA loop sleep is longer than the armed timeout
+ watchdog.reload_config({'ttl': 60, 'loop_wait': 25, 'watchdog': {'safety_margin': 10}})
+ self.assertTrue(watchdog.needs_immediate_keepalive)
+ watchdog.keepalive()
+ self.assertEqual(device.timeout, 49)
+ self.assertFalse(watchdog.needs_immediate_keepalive)
+ with patch.object(LinuxWatchdogDevice, 'get_timeout', Mock(side_effect=WatchdogError(''))):
+ self.assertTrue(watchdog.needs_immediate_keepalive)
+
+ # a timeout shorter than loop_wait must not stay armed
+ watchdog.reload_config({'ttl': 30, 'loop_wait': 25, 'watchdog': {'safety_margin': 10}})
+ watchdog.keepalive()
+ self.assertFalse(device.open)
+ self.assertEqual(device.writes[-1], b'V')
+ self.assertFalse(watchdog.is_running)
+ with patch('patroni.watchdog.base.logger.error') as mock_error:
+ watchdog.reload_config({'ttl': 60, 'loop_wait': 25, 'watchdog': {'safety_margin': 10}})
+ watchdog.keepalive()
+ mock_error.assert_not_called()
+
+ @patch('platform.system', Mock(return_value='Linux'))
+ @patch.object(LinuxWatchdogDevice, 'can_be_disabled', PropertyMock(return_value=True))
+ @patch.object(LinuxWatchdogDevice, 'has_set_timeout', Mock(return_value=False))
+ def test_timing_reload_with_fixed_timeout_device(self):
+ watchdog = Watchdog({'ttl': 30, 'loop_wait': 10, 'watchdog': {}})
+ with patch.object(LinuxWatchdogDevice, 'get_timeout', Mock(return_value=15)):
+ self.assertTrue(watchdog.activate())
+ device = mock_devices[-1]
+ with patch('patroni.watchdog.base.logger.error') as mock_error:
+ watchdog.reload_config({'ttl': 40, 'loop_wait': 10, 'watchdog': {}})
+ watchdog.keepalive()
+ mock_error.assert_not_called()
+ self.assertEqual(watchdog.active_config.ttl, 40)
+ self.assertTrue(device.open)
+ watchdog.reload_config({'ttl': 40, 'loop_wait': 18, 'watchdog': {}})
+ self.assertTrue(watchdog.needs_immediate_keepalive)
+ watchdog.keepalive()
+ self.assertFalse(device.open)
+ self.assertEqual(device.writes[-1], b'V')
+
class TestNullWatchdog(unittest.TestCase):
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
@ 2026-11-17 18:31 "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: CyberDem0n (@CyberDem0n) @ 2026-11-17 18:31 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@CyberDem0n requested a review.
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
@ 2026-11-17 18:59 "codecov[bot] (bot) (@codecov[bot])" <noreply+codecov[bot]@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: codecov[bot] (bot) (@codecov[bot]) @ 2026-11-17 18:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
## [Codecov](https://app.codecov.io/gh/patroni/patroni/pull/3795?dropdown=coverage&src=pr&el=h1&utm_m...) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:loudspeaker: Thoughts on this report? [Let us know!](https://github.com/codecov/feedback/issues/255)
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
@ 2026-11-19 10:17 "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-19 10:17 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@hughcapet applied label "bug".
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795 (dismissed): Apply watchdog timing changes safely on reload
@ 2026-11-19 17:59 "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-19 17:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795 (approved): Apply watchdog timing changes safely on reload
@ 2026-11-19 19:58 "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-19 19:58 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
@ 2026-11-19 19:59 "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-19 19:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@hughcapet merged this (commit b4bf694fca04).
^ permalink raw [nested|flat] 8+ messages in thread
* Re: [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload
@ 2026-11-19 19:59 "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
6 siblings, 0 replies; 8+ messages in thread
From: hughcapet (@hughcapet) @ 2026-11-19 19:59 UTC (permalink / raw)
To: patroni/patroni <noreply+patroni-patroni@github.com>
@hughcapet closed this.
^ permalink raw [nested|flat] 8+ messages in thread
end of thread, other threads:[~2026-11-19 19:59 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 18:31 [patroni/patroni] PR #3795: Apply watchdog timing changes safely on reload "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
2026-11-17 18:31 ` "CyberDem0n (@CyberDem0n)" <noreply+CyberDem0n@github.com>
2026-11-17 18:59 ` "codecov[bot] (bot) (@codecov[bot])" <noreply+codecov[bot]@github.com>
2026-11-19 10:17 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
2026-11-19 17:59 ` Re: [patroni/patroni] PR #3795 (dismissed): Apply watchdog timing changes safely on reload "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
2026-11-19 19:58 ` Re: [patroni/patroni] PR #3795 (approved): Apply watchdog timing changes safely on reload "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
2026-11-19 19:59 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
2026-11-19 19:59 ` "hughcapet (@hughcapet)" <noreply+hughcapet@github.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox