def test_key_prefix_correct(self):
        kv = KVAdapter()
        kv.kv_put = MagicMock()

        storage = OffsetStorage('my-node' , key_prefix='bq-delivered', kv=kv)
        storage.mark_last_read(150)

        kv.kv_put.assert_called_with('bq-delivered/my-node', '150')
    def test_key_prefix_used_everywhere(self):
        kv = KVAdapter()
        kv.kv_put = MagicMock()
        kv.kv_get = MagicMock(side_effect=[{'Value': '120'}])

        storage = OffsetStorage('server1' , key_prefix='somequeue', kv=kv)
        storage.mark_last_read(120)
        epoch = storage.get_last_read_epoch()

        kv.kv_put.assert_called_with('somequeue/server1', '120')
        kv.kv_get.assert_called_with('somequeue/server1')
        self.assertEqual(120, epoch)
Example #3
0
    def _configure(self) -> None:
        # We can't use broad 0.0.0.0 IP address to make it possible to run
        # multiple hax instances at the same machine (i.e. in failover
        # situation).
        # Instead, every hax will use a private IP only.
        node_address = self._get_my_hostname()

        # Note that bq-delivered mechanism must use a unique node name rather
        # than broad '0.0.0.0' that doesn't identify the node from outside.
        inbox_filter = InboxFilter(
            OffsetStorage(node_address,
                          key_prefix='bq-delivered',
                          kv=self.consul_util.kv))

        conf_obj = ConfObjUtil(self.consul_util)
        planner = self.planner
        herald = self.herald
        consul_util = self.consul_util

        app = self._create_server()
        app.add_routes([
            web.get('/', hello_reply),
            web.post('/', process_ha_states(planner, consul_util)),
            web.post(
                '/watcher/bq',
                process_bq_update(inbox_filter,
                                  BQProcessor(planner, herald, conf_obj))),
            web.post('/api/v1/sns/{operation}',
                     process_sns_operation(planner)),
            web.get('/api/v1/sns/repair-status',
                    get_sns_status(planner, SnsRepairStatus)),
            web.get('/api/v1/sns/rebalance-status',
                    get_sns_status(planner, SnsRebalanceStatus)),
        ])
        self.app = app
Example #4
0
def run_server(
    queue: Queue,
    herald: DeliveryHerald,
    consul_util: ConsulUtil,
    threads_to_wait: List[StoppableThread] = [],
    port=8008,
):
    node_address = consul_util.get_hax_ip_address()

    # We can't use broad 0.0.0.0 IP address to make it possible to run
    # multiple hax instances at the same machine (i.e. in failover situation).
    # Instead, every hax will use a private IP only.
    web_address = node_address

    # Note that bq-delivered mechanism must use a unique node name rather than
    # broad '0.0.0.0' that doesn't identify the node from outside.
    inbox_filter = InboxFilter(
        OffsetStorage(node_address, key_prefix='bq-delivered'))

    conf_obj = ConfObjUtil(consul_util)

    app = web.Application(middlewares=[encode_exception])
    app.add_routes([
        web.get('/', hello_reply),
        web.post('/', process_ha_states(queue, consul_util)),
        web.post(
            '/watcher/bq',
            process_bq_update(inbox_filter,
                              BQProcessor(queue, herald, conf_obj))),
        web.post('/api/v1/sns/{operation}', process_sns_operation(queue)),
        web.get('/api/v1/sns/repair-status',
                get_sns_status(queue, SnsRepairStatus)),
        web.get('/api/v1/sns/rebalance-status',
                get_sns_status(queue, SnsRebalanceStatus)),
    ])
    LOG.info(f'Starting HTTP server at {web_address}:{port} ...')
    try:
        web.run_app(app, host=web_address, port=port)
        LOG.debug('Server stopped normally')
    finally:
        LOG.debug('Stopping the threads')
        for thread in threads_to_wait:
            thread.stop()
        for thread in threads_to_wait:
            thread.join()

        LOG.info('The http server has stopped')