mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Implement sync_priority tag (#3223)
This commit is contained in:
+39
-32
@@ -160,40 +160,47 @@ class TestConfig(unittest.TestCase):
|
||||
|
||||
@patch.object(Config, 'get')
|
||||
@patch('patroni.config.logger')
|
||||
def test__validate_failover_tags(self, mock_logger, mock_get):
|
||||
"""Ensures that only one of `nofailover` or `failover_priority` can be provided"""
|
||||
# Providing one of `nofailover` or `failover_priority` is fine
|
||||
for single_param in ({"nofailover": True}, {"failover_priority": 1}, {"failover_priority": 0}):
|
||||
mock_get.side_effect = [single_param] * 2
|
||||
self.assertIsNone(self.config._validate_failover_tags())
|
||||
mock_logger.warning.assert_not_called()
|
||||
def test__validate_tags(self, mock_logger, mock_get):
|
||||
"""Ensures that only one of `nofailover`/`nosync' or `failover_priority`/`sync_priority` can be provided"""
|
||||
tag_setup = (('nofailover', 'failover_priority'),
|
||||
('nosync', 'sync_priority',))
|
||||
|
||||
# Providing both `nofailover` and `failover_priority` is fine if consistent
|
||||
for consistent_state in (
|
||||
{"nofailover": False, "failover_priority": 1},
|
||||
{"nofailover": True, "failover_priority": 0},
|
||||
{"nofailover": "False", "failover_priority": 0}
|
||||
):
|
||||
mock_get.side_effect = [consistent_state] * 2
|
||||
self.assertIsNone(self.config._validate_failover_tags())
|
||||
mock_logger.warning.assert_not_called()
|
||||
for tag, priority_tag in tag_setup:
|
||||
# Providing one tag is fine
|
||||
for single_param in ({tag: True}, {priority_tag: 1}, {priority_tag: 0}):
|
||||
mock_get.side_effect = [single_param] * 2
|
||||
self.assertIsNone(self.config._validate_contradictory_tags())
|
||||
mock_logger.warning.assert_not_called()
|
||||
|
||||
# Providing both inconsistently should log a warning
|
||||
for inconsistent_state in (
|
||||
{"nofailover": False, "failover_priority": 0},
|
||||
{"nofailover": True, "failover_priority": 1},
|
||||
{"nofailover": "False", "failover_priority": 1},
|
||||
{"nofailover": "", "failover_priority": 0}
|
||||
):
|
||||
mock_get.side_effect = [inconsistent_state] * 2
|
||||
self.assertIsNone(self.config._validate_failover_tags())
|
||||
mock_logger.warning.assert_called_once_with(
|
||||
'Conflicting configuration between nofailover: %s and failover_priority: %s.'
|
||||
+ ' Defaulting to nofailover: %s',
|
||||
inconsistent_state['nofailover'],
|
||||
inconsistent_state['failover_priority'],
|
||||
inconsistent_state['nofailover'])
|
||||
mock_logger.warning.reset_mock()
|
||||
# Providing both tags is fine if consistent
|
||||
for consistent_state in (
|
||||
{tag: False, priority_tag: 1},
|
||||
{tag: True, priority_tag: 0},
|
||||
{tag: "False", priority_tag: 0}
|
||||
):
|
||||
mock_get.side_effect = [consistent_state] * 2
|
||||
self.assertIsNone(self.config._validate_contradictory_tags())
|
||||
mock_logger.warning.assert_not_called()
|
||||
|
||||
# Providing both inconsistently should log a warning
|
||||
for inconsistent_state in (
|
||||
{tag: False, priority_tag: 0},
|
||||
{tag: True, priority_tag: 1},
|
||||
{tag: "False", priority_tag: 1},
|
||||
{tag: "", priority_tag: 0}
|
||||
):
|
||||
mock_get.side_effect = [inconsistent_state] * 2
|
||||
self.assertIsNone(self.config._validate_contradictory_tags())
|
||||
mock_logger.warning.assert_called_once_with(
|
||||
'Conflicting configuration between %s: %s and %s: %s.'
|
||||
+ ' Defaulting to %s: %s',
|
||||
tag,
|
||||
inconsistent_state[tag],
|
||||
priority_tag,
|
||||
inconsistent_state[priority_tag],
|
||||
tag,
|
||||
inconsistent_state[tag])
|
||||
mock_logger.warning.reset_mock()
|
||||
|
||||
def test__process_postgresql_parameters(self):
|
||||
expected_params = {
|
||||
|
||||
@@ -140,6 +140,7 @@ class TestGenerateConfig(unittest.TestCase):
|
||||
},
|
||||
'tags': {
|
||||
'failover_priority': 1,
|
||||
'sync_priority': 1,
|
||||
'noloadbalance': False,
|
||||
'clonefrom': True,
|
||||
'nosync': False,
|
||||
|
||||
+4
-2
@@ -99,12 +99,13 @@ def get_cluster_initialized_with_leader_and_failsafe():
|
||||
|
||||
def get_node_status(reachable=True, in_recovery=True, dcs_last_seen=0,
|
||||
timeline=2, wal_position=10, nofailover=False,
|
||||
watchdog_failed=False, failover_priority=1):
|
||||
watchdog_failed=False, failover_priority=1, sync_priority=1):
|
||||
def fetch_node_status(e):
|
||||
tags = {}
|
||||
if nofailover:
|
||||
tags['nofailover'] = True
|
||||
tags['failover_priority'] = failover_priority
|
||||
tags['sync_priority'] = sync_priority
|
||||
return _MemberStatus(e, reachable, in_recovery, wal_position,
|
||||
{'tags': tags, 'watchdog_failed': watchdog_failed,
|
||||
'dcs_last_seen': dcs_last_seen, 'timeline': timeline})
|
||||
@@ -156,6 +157,7 @@ zookeeper:
|
||||
self.watchdog = Watchdog(self.config)
|
||||
self.request = lambda *args, **kwargs: requests_get(args[0].api_url, *args[1:], **kwargs)
|
||||
self.failover_priority = 1
|
||||
self.sync_priority = 1
|
||||
|
||||
|
||||
def run_async(self, func, args=()):
|
||||
@@ -1572,7 +1574,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
def test_effective_tags(self):
|
||||
self.ha._disable_sync = True
|
||||
self.assertEqual(self.ha.get_effective_tags(), {'foo': 'bar', 'nosync': True})
|
||||
self.assertEqual(self.ha.get_effective_tags(), {'foo': 'bar', 'nosync': True, 'sync_priority': 0})
|
||||
self.ha._disable_sync = False
|
||||
self.assertEqual(self.ha.get_effective_tags(), {'foo': 'bar'})
|
||||
|
||||
|
||||
@@ -202,6 +202,12 @@ class TestPatroni(unittest.TestCase):
|
||||
tags = {'nofailover': True, 'failover_priority': 1}
|
||||
self.assertEqual(self.p._filter_tags(tags), tags)
|
||||
|
||||
tags = {'nosync': False, 'sync_priority': 0}
|
||||
self.assertEqual(self.p._filter_tags(tags), tags)
|
||||
|
||||
tags = {'nosync': True, 'sync_priority': 1}
|
||||
self.assertEqual(self.p._filter_tags(tags), tags)
|
||||
|
||||
def test_noloadbalance(self):
|
||||
self.p.tags['noloadbalance'] = True
|
||||
self.assertTrue(self.p.noloadbalance)
|
||||
|
||||
Reference in New Issue
Block a user