Ejemplo n.º 1
0
 def test_on_ready_override(self):
     self.assertTrue(self.so.set('foo', 'bar'))
     self.so.destroy()
     self.so = None
     so = KVStoreTTL(Mock(), None, None, self_addr='127.0.0.1:1234',
                     partner_addrs=['127.0.0.1:1235'], patronictl=True)
     so.doTick(0)
     so.destroy()
Ejemplo n.º 2
0
 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()
Ejemplo n.º 3
0
class TestKVStoreTTL(unittest.TestCase):

    @patch.object(SyncObjConf, 'fullDumpFile', PropertyMock(return_value=None), create=True)
    @patch.object(SyncObjConf, 'journalFile', PropertyMock(return_value=None), create=True)
    def setUp(self):
        callback = Mock()
        callback.replicated = False
        self.so = KVStoreTTL(None, callback, callback, self_addr='127.0.0.1:1234')
        self.so.startAutoTick()
        self.so.set_retry_timeout(10)

    def tearDown(self):
        if self.so:
            self.so.destroy()

    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.so.autoTickPeriod = 0.2
        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.so.destroy()
        self.so = None
        so = KVStoreTTL(Mock(), None, None, self_addr='127.0.0.1:1234',
                        partner_addrs=['127.0.0.1:1235'], patronictl=True)
        so.doTick(0)
        so.destroy()
Ejemplo n.º 4
0
class RaftController(AbstractPatroniDaemon):
    def __init__(self, config):
        super(RaftController, self).__init__(config)

        raft_config = self.config.get('raft')
        self_addr = raft_config['self_addr']
        template = os.path.join(raft_config.get('data_dir', ''), self_addr)
        self._syncobj_config = SyncObjConf(autoTick=False,
                                           appendEntriesUseBatch=False,
                                           dynamicMembershipChange=True,
                                           journalFile=template + '.journal',
                                           fullDumpFile=template + '.dump')
        self._raft = KVStoreTTL(self_addr,
                                raft_config.get('partner_addrs',
                                                []), self._syncobj_config)

    def _run_cycle(self):
        try:
            self._raft.doTick(self._syncobj_config.autoTickPeriod)
        except Exception:
            logger.exception('doTick')

    def _shutdown(self):
        self._raft.destroy()