mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Convert roles to enums (#3303)
This commit is contained in:
+32
-32
@@ -18,7 +18,7 @@ from patroni.postgresql.bootstrap import Bootstrap
|
||||
from patroni.postgresql.callback_executor import CallbackAction
|
||||
from patroni.postgresql.cancellable import CancellableSubprocess
|
||||
from patroni.postgresql.config import ConfigHandler
|
||||
from patroni.postgresql.misc import PostgresqlState
|
||||
from patroni.postgresql.misc import PostgresqlRole, PostgresqlState
|
||||
from patroni.postgresql.postmaster import PostmasterProcess
|
||||
from patroni.postgresql.rewind import Rewind
|
||||
from patroni.postgresql.slots import SlotsHandler
|
||||
@@ -59,7 +59,7 @@ def get_cluster_bootstrapping_without_leader(cluster_config=None):
|
||||
def get_cluster_initialized_without_leader(leader=False, failover=None, sync=None, cluster_config=None, failsafe=False):
|
||||
m1 = Member(0, 'leader', 28, {'conn_url': 'postgres://replicator:[email protected]:5435/postgres',
|
||||
'api_url': 'http://127.0.0.1:8008/patroni', 'xlog_location': 4,
|
||||
'role': 'primary', 'state': 'running'})
|
||||
'role': PostgresqlRole.PRIMARY, 'state': 'running'})
|
||||
leader = Leader(0, 0, m1 if leader else Member(0, '', 28, {}))
|
||||
m2 = Member(0, 'other', 28, {'conn_url': 'postgres://replicator:[email protected]:5436/postgres',
|
||||
'api_url': 'http://127.0.0.1:8011/patroni',
|
||||
@@ -214,7 +214,7 @@ class TestHa(PostgresInit):
|
||||
def setUp(self):
|
||||
super(TestHa, self).setUp()
|
||||
self.p.set_state(PostgresqlState.RUNNING)
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.p.postmaster_start_time = MagicMock(return_value=str(postmaster_start_time))
|
||||
self.p.can_create_replica_without_replication_connection = MagicMock(return_value=False)
|
||||
self.e = get_dcs({'etcd': {'ttl': 30, 'host': 'ok:2379', 'scope': 'test',
|
||||
@@ -242,9 +242,9 @@ class TestHa(PostgresInit):
|
||||
with patch.object(Postgresql, '_cluster_info_state_get', Mock(return_value='streaming')):
|
||||
self.ha.touch_member()
|
||||
self.p.timeline_wal_position = Mock(return_value=(0, 1, 1))
|
||||
self.p.set_role('standby_leader')
|
||||
self.p.set_role(PostgresqlRole.STANDBY_LEADER)
|
||||
self.ha.touch_member()
|
||||
self.p.set_role('primary')
|
||||
self.p.set_role(PostgresqlRole.PRIMARY)
|
||||
self.ha.dcs.touch_member = true
|
||||
self.ha.touch_member()
|
||||
|
||||
@@ -299,7 +299,7 @@ class TestHa(PostgresInit):
|
||||
self.p.follow = false
|
||||
self.p.is_running = false
|
||||
self.p.name = 'leader'
|
||||
self.p.set_role('demoted')
|
||||
self.p.set_role(PostgresqlRole.DEMOTED)
|
||||
self.p.controldata = lambda: {'Database cluster state': 'shut down', 'Database system identifier': SYSID}
|
||||
self.ha.cluster = get_cluster_initialized_with_leader()
|
||||
self.assertEqual(self.ha.run_cycle(), 'starting as readonly because i had the session lock')
|
||||
@@ -308,7 +308,7 @@ class TestHa(PostgresInit):
|
||||
self.p.start = false
|
||||
self.p.is_running = false
|
||||
self.p.name = 'leader'
|
||||
self.p.set_role('primary')
|
||||
self.p.set_role(PostgresqlRole.PRIMARY)
|
||||
self.p.controldata = lambda: {'Database cluster state': 'in production', 'Database system identifier': SYSID}
|
||||
self.ha.cluster = get_cluster_initialized_with_leader()
|
||||
self.assertEqual(self.ha.run_cycle(), 'starting primary after failure')
|
||||
@@ -346,7 +346,7 @@ class TestHa(PostgresInit):
|
||||
def test_recover_with_rewind(self):
|
||||
self.p.is_running = false
|
||||
self.ha.cluster = get_cluster_initialized_with_leader()
|
||||
self.ha.cluster.leader.member.data.update(version='2.0.2', role='primary')
|
||||
self.ha.cluster.leader.member.data.update(version='2.0.2', role=PostgresqlRole.PRIMARY)
|
||||
self.ha._rewind.pg_rewind = true
|
||||
self.ha._rewind.check_leader_is_not_in_recovery = true
|
||||
with patch.object(Rewind, 'rewind_or_reinitialize_needed_and_possible', Mock(return_value=True)):
|
||||
@@ -414,7 +414,7 @@ class TestHa(PostgresInit):
|
||||
def test_long_promote(self):
|
||||
self.ha.has_lock = true
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('primary')
|
||||
self.p.set_role(PostgresqlRole.PRIMARY)
|
||||
self.assertEqual(self.ha.run_cycle(), 'no action. I am (postgresql0), the leader with the lock')
|
||||
|
||||
def test_demote_after_failing_to_obtain_lock(self):
|
||||
@@ -579,7 +579,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
def test_update_failsafe(self):
|
||||
self.assertRaises(Exception, self.ha.update_failsafe, {})
|
||||
self.p.set_role('primary')
|
||||
self.p.set_role(PostgresqlRole.PRIMARY)
|
||||
self.assertEqual(self.ha.update_failsafe({}), 'Running as a leader')
|
||||
|
||||
def test_call_failsafe_member(self):
|
||||
@@ -715,7 +715,7 @@ class TestHa(PostgresInit):
|
||||
self.assertEqual(self.ha.run_cycle(), 'updated leader lock during restart')
|
||||
|
||||
self.ha.update_lock = false
|
||||
self.p.set_role('primary')
|
||||
self.p.set_role(PostgresqlRole.PRIMARY)
|
||||
with patch('patroni.async_executor.CriticalTask.cancel', Mock(return_value=False)), \
|
||||
patch('patroni.async_executor.CriticalTask.result',
|
||||
PropertyMock(return_value=PostmasterProcess(os.getpid())), create=True), \
|
||||
@@ -907,7 +907,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
def test_manual_failover_process_no_leader(self):
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
|
||||
# failover to another member, fetch_node_status for candidate fails
|
||||
with patch('patroni.ha.logger.warning') as mock_warning:
|
||||
@@ -917,7 +917,7 @@ class TestHa(PostgresInit):
|
||||
('%s: member %s is %s', 'manual failover', 'leader', 'not reachable'))
|
||||
|
||||
# failover to another member, candidate is accessible, in_recovery
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.ha.fetch_node_status = get_node_status()
|
||||
self.assertEqual(self.ha.run_cycle(), 'following a different leader because i am not the healthiest node')
|
||||
|
||||
@@ -928,7 +928,7 @@ class TestHa(PostgresInit):
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock')
|
||||
|
||||
# failover to me but I am set to nofailover. In no case I should be elected as a leader
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.ha.cluster = get_cluster_initialized_without_leader(failover=Failover(0, '', 'postgresql0', None))
|
||||
self.ha.patroni.nofailover = True
|
||||
self.assertEqual(self.ha.run_cycle(), 'following a different leader because I am not allowed to promote')
|
||||
@@ -952,7 +952,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
def test_manual_switchover_process_no_leader(self):
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
|
||||
# I was the leader, other members are healthy
|
||||
self.ha.fetch_node_status = get_node_status()
|
||||
@@ -989,7 +989,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
# manual failover to our node (postgresql0),
|
||||
# which name is not in sync nodes list (some sync nodes are available)
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.ha.cluster = get_cluster_initialized_without_leader(failover=Failover(0, '', 'postgresql0', None),
|
||||
sync=('leader1', 'other'))
|
||||
self.p.sync_handler.current_state = Mock(return_value=(CaseInsensitiveSet(['leader1']),
|
||||
@@ -1045,7 +1045,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
# switchover to me, I am not leader
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.ha.cluster = get_cluster_initialized_without_leader(failover=Failover(0, 'leader', self.p.name, None))
|
||||
self.assertEqual(self.ha.run_cycle(), 'PAUSE: promoted self to leader by acquiring session lock')
|
||||
|
||||
@@ -1117,9 +1117,9 @@ class TestHa(PostgresInit):
|
||||
def test_post_recover(self, mock_call_nowait):
|
||||
self.p.is_running = false
|
||||
self.ha.has_lock = true
|
||||
self.p.set_role('primary')
|
||||
self.p.set_role(PostgresqlRole.PRIMARY)
|
||||
self.assertEqual(self.ha.post_recover(), 'removed leader key after trying and failing to start postgres')
|
||||
self.assertEqual(self.p.role, 'demoted')
|
||||
self.assertEqual(self.p.role, PostgresqlRole.DEMOTED)
|
||||
mock_call_nowait.assert_called_once_with(CallbackAction.ON_ROLE_CHANGE)
|
||||
self.ha.has_lock = false
|
||||
self.assertEqual(self.ha.post_recover(), 'failed to start postgres')
|
||||
@@ -1167,7 +1167,7 @@ class TestHa(PostgresInit):
|
||||
self.assertEqual(self.ha.run_cycle(), "restart scheduled")
|
||||
|
||||
def test_restart_matches(self):
|
||||
self.p._role = 'replica'
|
||||
self.p._role = PostgresqlRole.REPLICA
|
||||
self.p._connection.server_version = 90500
|
||||
self.p._pending_restart = True
|
||||
self.assertFalse(self.ha.restart_matches("primary", "9.5.0", True))
|
||||
@@ -1195,7 +1195,7 @@ class TestHa(PostgresInit):
|
||||
self.ha._leader_timeline = 1
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to a standby leader because i had the session lock')
|
||||
self.assertEqual(self.ha.run_cycle(), 'no action. I am (leader), the standby leader with the lock')
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.p.config.check_recovery_conf = Mock(return_value=(True, False))
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to a standby leader because i had the session lock')
|
||||
|
||||
@@ -1221,7 +1221,7 @@ class TestHa(PostgresInit):
|
||||
@patch.object(Rewind, 'can_rewind', PropertyMock(return_value=True))
|
||||
def test_process_unhealthy_standby_cluster_as_cascade_replica(self):
|
||||
self.p.is_primary = false
|
||||
self.p.name = 'replica'
|
||||
self.p.name = PostgresqlRole.REPLICA
|
||||
self.ha.cluster = get_standby_cluster_initialized_with_only_leader()
|
||||
self.assertTrue(self.ha.run_cycle().startswith('running pg_rewind from remote_member:'))
|
||||
|
||||
@@ -1458,7 +1458,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
mock_set_sync = self.p.sync_handler.set_synchronous_standby_names = Mock()
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.ha.has_lock = true
|
||||
mock_write_sync = self.ha.dcs.write_sync_state = Mock(return_value=SyncState.empty())
|
||||
self.p.name = 'leader'
|
||||
@@ -1472,7 +1472,7 @@ class TestHa(PostgresInit):
|
||||
mock_set_sync.reset_mock()
|
||||
|
||||
# When we just became primary nobody is sync
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
mock_write_sync.return_value = False
|
||||
self.assertTrue(self.ha.enforce_primary_role('msg', 'promote msg') != 'promote msg')
|
||||
mock_set_sync.assert_not_called()
|
||||
@@ -1481,7 +1481,7 @@ class TestHa(PostgresInit):
|
||||
self.ha.is_synchronous_mode = true
|
||||
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.p.name = 'other'
|
||||
self.ha.cluster = get_cluster_initialized_without_leader(sync=('leader', 'other2'))
|
||||
mock_write_sync = self.ha.dcs.write_sync_state = Mock(return_value=SyncState.empty())
|
||||
@@ -1512,7 +1512,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
self.p.name = 'other'
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
mock_restart = self.p.restart = Mock(return_value=True)
|
||||
self.ha.cluster = get_cluster_initialized_with_leader(sync=('leader', 'other'))
|
||||
self.ha.touch_member = Mock(return_value=True)
|
||||
@@ -1627,7 +1627,7 @@ class TestHa(PostgresInit):
|
||||
self.p.data_directory_empty = Mock(side_effect=OSError(5, "Input/output error: '{}'".format(self.p.data_dir)))
|
||||
self.assertEqual(self.ha.run_cycle(),
|
||||
'released leader key voluntarily as data dir not accessible and currently leader')
|
||||
self.assertEqual(self.p.role, 'uninitialized')
|
||||
self.assertEqual(self.p.role, PostgresqlRole.UNINITIALIZED)
|
||||
|
||||
# as has_lock is mocked out, we need to fake the leader key release
|
||||
self.ha.has_lock = false
|
||||
@@ -1651,7 +1651,7 @@ class TestHa(PostgresInit):
|
||||
self.p.is_primary = false
|
||||
self.ha.run_cycle()
|
||||
exit_mock.assert_called_once_with(1)
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.ha.dcs.initialize = Mock()
|
||||
with patch.object(Postgresql, 'cb_called', PropertyMock(return_value=True)):
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock')
|
||||
@@ -1687,7 +1687,7 @@ class TestHa(PostgresInit):
|
||||
self.ha.is_paused = true
|
||||
self.p.data_directory_empty = true
|
||||
self.assertEqual(self.ha.run_cycle(), 'PAUSE: running with empty data directory')
|
||||
self.assertEqual(self.p.role, 'uninitialized')
|
||||
self.assertEqual(self.p.role, PostgresqlRole.UNINITIALIZED)
|
||||
|
||||
@patch('patroni.ha.Ha.sysid_valid', MagicMock(return_value=True))
|
||||
def test_sysid_no_match_in_pause(self):
|
||||
@@ -1742,7 +1742,7 @@ class TestHa(PostgresInit):
|
||||
self.p._major_version = 90500
|
||||
self.ha.cluster = get_cluster_initialized_without_leader(sync=('other', self.p.name + ',foo'))
|
||||
self.p.is_primary = false
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
mock_write_sync = self.ha.dcs.write_sync_state = Mock(return_value=None)
|
||||
# Postgres 9.5, write_sync_state to DCS failed
|
||||
self.assertEqual(self.ha.run_cycle(),
|
||||
@@ -1764,7 +1764,7 @@ class TestHa(PostgresInit):
|
||||
self.p._major_version = 90600
|
||||
mock_set_sync.reset_mock()
|
||||
mock_write_sync.reset_mock()
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
# Postgres 9.6, with quorum commit we avoid updating /sync key and put some nodes to ssn
|
||||
self.assertEqual(self.ha.run_cycle(), 'promoted self to leader by acquiring session lock')
|
||||
self.assertEqual(mock_write_sync.call_count, 0)
|
||||
@@ -1773,7 +1773,7 @@ class TestHa(PostgresInit):
|
||||
|
||||
self.p._major_version = 150000
|
||||
mock_set_sync.reset_mock()
|
||||
self.p.set_role('replica')
|
||||
self.p.set_role(PostgresqlRole.REPLICA)
|
||||
self.p.name = 'nonsync'
|
||||
self.ha.fetch_node_status = get_node_status()
|
||||
# Postgres 15, with quorum commit. Non-sync node promoted we avoid updating /sync key and put some nodes to ssn
|
||||
|
||||
Reference in New Issue
Block a user