From b84e22c4ea0d9e45e416f286d1fc15b916f92191 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Mon, 4 Jul 2016 10:56:37 +0200 Subject: [PATCH] Implement more checks in the follow method Although such situation should not happen in reality (follow method is not supposed to be called when when the node is holding leader lock and postgres is running), but to be on the safe side it is better to implement as much checks as possible, because this method could potentially remove data directory. --- patroni/postgresql.py | 8 ++++++-- tests/test_postgresql.py | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/patroni/postgresql.py b/patroni/postgresql.py index 1da93eec..8b2ab861 100644 --- a/patroni/postgresql.py +++ b/patroni/postgresql.py @@ -730,8 +730,12 @@ class Postgresql(object): change_role = self.role == 'master' - self._need_rewind = (not leader or leader.name != self.name) \ - and (self._need_rewind or change_role and self.can_rewind) + if leader and leader.name == self.name: + self._need_rewind = False + if self.is_running(): + return + else: + self._need_rewind = self._need_rewind or change_role and self.can_rewind if self._need_rewind: logger.info("set the rewind flag after demote") diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 81f31105..f744e401 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -257,6 +257,8 @@ class TestPostgresql(unittest.TestCase): with patch.object(Postgresql, 'check_recovery_conf', Mock(return_value=True)): self.assertTrue(self.p.follow(None, None)) # nothing to do, recovery.conf has good primary_conninfo + self.p.follow(self.me, self.me) # follow is called when the node is holding leader lock + with patch.object(Postgresql, 'restart', Mock(return_value=False)): self.p.set_role('replica') self.p.follow(None, None) # restart without rewind