mirror of
https://github.com/outbackdingo/patroni.git
synced 2026-08-25 14:53:37 +00:00
Effectively, this PR consists of a few changes: 1. The easy part: In case of permanent logical slots are defined in the global configuration, Patroni on the primary will not only create them, but also periodically update DCS with the current values of `confirmed_flush_lsn` for all these slots. In order to reduce the number of interactions with DCS the new `/status` key was introduced. It will contain the json object with `optime` and `slots` keys. For backward compatibility the `/optime/leader` will be updated if there are members with old Patroni in the cluster. 2. The tricky part: On replicas that are eligible for a failover, Patroni creates the logical replication slot by copying the slot file from the primary and restarting the replica. In order to copy the slot file Patroni opens a connection to the primary with `rewind` or `superuser` credentials and calls `pg_read_binary_file()` function. When the logical slot already exists on the replica Patroni periodically calls `pg_replication_slot_advance()` function, which allows moving the slot forward. 3. Additional requirements: In order to ensure that primary doesn't cleanup tuples from pg_catalog that are required for logical decoding, Patroni enables `hot_standby_feedback` on replicas with logical slots and on cascading replicas if they are used for streaming by replicas with logical slots. 4. When logical slots are copied from to the replica there is a timeframe when it could be not safe to use them after promotion. Right now there is no protection from promoting such a replica. But, Patroni will show the warning with names of the slots that might be not safe to use. Compatibility. The `pg_replication_slot_advance()` function is only available starting from PostgreSQL 11. For older Postgres versions Patroni will refuse to create the logical slot on the primary. The old "permanent slots" feature, which creates logical slots right after promotion and before allowing connections, was removed. Close: https://github.com/zalando/patroni/issues/1749
168 lines
6.4 KiB
Python
168 lines
6.4 KiB
Python
import os
|
|
import unittest
|
|
import tempfile
|
|
import time
|
|
|
|
from mock import Mock, patch
|
|
from patroni.dcs.raft import DynMemberSyncObj, KVStoreTTL, Raft, SyncObjUtility
|
|
from pysyncobj import SyncObjConf, FAIL_REASON
|
|
|
|
|
|
def remove_files(prefix):
|
|
for f in ('journal', 'dump'):
|
|
f = prefix + f
|
|
if os.path.isfile(f):
|
|
for i in range(0, 15):
|
|
try:
|
|
if os.path.isfile(f):
|
|
os.unlink(f)
|
|
break
|
|
else:
|
|
break
|
|
except Exception:
|
|
time.sleep(1.0)
|
|
|
|
|
|
@patch('pysyncobj.tcp_server.TcpServer.bind', Mock())
|
|
class TestDynMemberSyncObj(unittest.TestCase):
|
|
|
|
@patch('pysyncobj.tcp_server.TcpServer.bind', Mock())
|
|
def setUp(self):
|
|
self.conf = SyncObjConf(appendEntriesUseBatch=False, dynamicMembershipChange=True, autoTick=False)
|
|
self.so = DynMemberSyncObj('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
|
|
|
@patch.object(SyncObjUtility, 'sendMessage')
|
|
def test_add_member(self, mock_send_message):
|
|
mock_send_message.return_value = [{'addr': '127.0.0.1:1235'}, {'addr': '127.0.0.1:1236'}]
|
|
mock_send_message.ver = 0
|
|
DynMemberSyncObj('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
|
self.conf.dynamicMembershipChange = False
|
|
DynMemberSyncObj('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
|
|
|
def test___onUtilityMessage(self):
|
|
self.so._SyncObj__encryptor = Mock()
|
|
mock_conn = Mock()
|
|
mock_conn.sendRandKey = None
|
|
self.so._SyncObj__transport._onIncomingMessageReceived(mock_conn, 'randkey')
|
|
self.so._SyncObj__transport._onIncomingMessageReceived(mock_conn, ['members'])
|
|
self.so._SyncObj__transport._onIncomingMessageReceived(mock_conn, ['status'])
|
|
|
|
def test__SyncObj__doChangeCluster(self):
|
|
self.so._SyncObj__doChangeCluster(['add', '127.0.0.1:1236'])
|
|
|
|
def test_utility(self):
|
|
utility = SyncObjUtility(['127.0.0.1:1235'], self.conf)
|
|
utility.setPartnerNode(list(utility._SyncObj__otherNodes)[0])
|
|
utility.sendMessage(['members'])
|
|
utility._onMessageReceived(0, '')
|
|
|
|
|
|
class TestKVStoreTTL(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.conf = SyncObjConf(appendEntriesUseBatch=False, appendEntriesPeriod=0.001,
|
|
raftMinTimeout=0.004, raftMaxTimeout=0.005, autoTickPeriod=0.001)
|
|
callback = Mock()
|
|
callback.replicated = False
|
|
self.so = KVStoreTTL('127.0.0.1:1234', [], self.conf, on_set=callback, on_delete=callback)
|
|
self.so.set_retry_timeout(10)
|
|
|
|
@staticmethod
|
|
def destroy(so):
|
|
so.destroy()
|
|
so._SyncObj__thread.join()
|
|
|
|
def tearDown(self):
|
|
if self.so:
|
|
self.destroy(self.so)
|
|
|
|
def test_set(self):
|
|
self.assertTrue(self.so.set('foo', 'bar', prevExist=False, ttl=30))
|
|
self.assertFalse(self.so.set('foo', 'bar', prevExist=False, ttl=30))
|
|
self.assertFalse(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1}, prevValue=''))
|
|
self.assertTrue(self.so.retry(self.so._set, 'foo', {'value': 'buz', 'created': 1, 'updated': 1}))
|
|
|
|
def test_delete(self):
|
|
self.conf.autoTickPeriod = 0.1
|
|
self.so.set('foo', 'bar')
|
|
self.so.set('fooo', 'bar')
|
|
self.assertFalse(self.so.delete('foo', prevValue='buz'))
|
|
self.assertTrue(self.so.delete('foo', recursive=True))
|
|
self.assertFalse(self.so.retry(self.so._delete, 'foo', prevValue=''))
|
|
|
|
def test_expire(self):
|
|
self.so.set('foo', 'bar', ttl=0.001)
|
|
time.sleep(1)
|
|
self.assertIsNone(self.so.get('foo'))
|
|
self.assertEqual(self.so.get('foo', recursive=True), {})
|
|
|
|
@patch('time.sleep', Mock())
|
|
def test_retry(self):
|
|
return_values = [FAIL_REASON.QUEUE_FULL] * 2 + [FAIL_REASON.SUCCESS, FAIL_REASON.REQUEST_DENIED]
|
|
|
|
def test(callback):
|
|
callback(True, return_values.pop(0))
|
|
|
|
with patch('time.time', Mock(side_effect=[1, 100])):
|
|
self.assertFalse(self.so.retry(test))
|
|
|
|
self.assertTrue(self.so.retry(test))
|
|
self.assertFalse(self.so.retry(test))
|
|
|
|
def test_on_ready_override(self):
|
|
self.assertTrue(self.so.set('foo', 'bar'))
|
|
self.destroy(self.so)
|
|
self.so = None
|
|
self.conf.onReady = Mock()
|
|
self.conf.autoTick = False
|
|
so = KVStoreTTL('127.0.0.1:1234', ['127.0.0.1:1235'], self.conf)
|
|
so.doTick(0)
|
|
so.destroy()
|
|
|
|
|
|
class TestRaft(unittest.TestCase):
|
|
|
|
_TMP = tempfile.gettempdir()
|
|
|
|
def test_raft(self):
|
|
raft = Raft({'ttl': 30, 'scope': 'test', 'name': 'pg', 'self_addr': '127.0.0.1:1234',
|
|
'retry_timeout': 10, 'data_dir': self._TMP})
|
|
raft.set_retry_timeout(20)
|
|
raft.set_ttl(60)
|
|
self.assertTrue(raft._sync_obj.set(raft.members_path + 'legacy', '{"version":"2.0.0"}'))
|
|
self.assertTrue(raft.touch_member(''))
|
|
self.assertTrue(raft.initialize())
|
|
self.assertTrue(raft.cancel_initialization())
|
|
self.assertTrue(raft.set_config_value('{}'))
|
|
self.assertTrue(raft.write_sync_state('foo', 'bar'))
|
|
self.assertTrue(raft.manual_failover('foo', 'bar'))
|
|
raft.get_cluster()
|
|
self.assertTrue(raft._sync_obj.set(raft.status_path, '{"optime":1234567,"slots":{"ls":12345}}'))
|
|
raft.get_cluster()
|
|
self.assertTrue(raft.update_leader('1'))
|
|
self.assertTrue(raft._sync_obj.set(raft.status_path, '{'))
|
|
raft.get_cluster()
|
|
self.assertTrue(raft.delete_sync_state())
|
|
self.assertTrue(raft.delete_leader())
|
|
self.assertTrue(raft.set_history_value(''))
|
|
self.assertTrue(raft.delete_cluster())
|
|
raft.get_cluster()
|
|
self.assertTrue(raft.take_leader())
|
|
raft.watch(None, 0.001)
|
|
raft._sync_obj.destroy()
|
|
raft._sync_obj._SyncObj__thread.join()
|
|
|
|
def tearDown(self):
|
|
remove_files(os.path.join(self._TMP, '127.0.0.1:1234.'))
|
|
|
|
def setUp(self):
|
|
self.tearDown()
|
|
|
|
@patch('patroni.dcs.raft.KVStoreTTL')
|
|
@patch('threading.Event')
|
|
def test_init(self, mock_event, mock_kvstore):
|
|
mock_kvstore.return_value.applied_local_log = False
|
|
mock_event.return_value.isSet.side_effect = [False, True]
|
|
self.assertIsNotNone(Raft({'ttl': 30, 'scope': 'test', 'name': 'pg', 'patronictl': True,
|
|
'self_addr': '1', 'data_dir': self._TMP}))
|