Пример #1
0
    def test_get_routes_calls_returns_none(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_false):
            config_cache = ConfigCache()
            routes = config_cache.get_routes()

        self.assertIs(routes, None)
Пример #2
0
    def test_get_routes_calls_returns_config(self):
        with patch.object(
                NativeProxy, 'cache_exists', self.cache_true
        ), patch.object(NativeProxy, 'cache_get',  self.cache_get_routes):
            config_cache = ConfigCache()
            routes = config_cache.get_routes()

        self.cache_get_routes.assert_called_once_with(
            'routes', CACHE_CONFIG)
        self.assertEqual(routes, self.routes)
Пример #3
0
class Router(object):
    def __init__(self):
        self._config_cache = ConfigCache()
        self._blacklist_cache = BlacklistCache()
        self._personality = self._config_cache.get_config().personality
        self._active_worker_socket = dict()
        self._dispatch = Dispatch()

    def _get_next_service_domain(self):
        if self._personality == personalities.CORRELATION:
            return personalities.STORAGE
        if self._personality == personalities.SYSLOG:
            return personalities.STORAGE
        if self._personality == personalities.NORMALIZATION:
            return personalities.STORAGE
        return None

    def _get_route_targets(self, service_domain):
        routes = self._config_cache.get_routes()
        for domain in routes:
            if domain['service_domain'] == service_domain:
                return domain['targets']
        return None

    def _blacklist_worker(self, service_domain, worker_id):
        self._active_worker_socket[service_domain] = None
        self._blacklist_cache.add_blacklist_worker(worker_id)

        config = self._config_cache.get_config()
        if config:
            token_header = {
                "WORKER-ID": config.worker_id,
                "WORKER-TOKEN": config.worker_token
            }

            request_uri = "{0}/worker/{1}".format(
                config.coordinator_uri, worker_id)

            try:
                http_request(request_uri, token_header, http_verb='PUT')

            except requests.RequestException:
                #Todo log failure to contact coordinator
                pass

    def _get_worker_socket(self, service_domain):
        worker_socket = self._active_worker_socket.get(service_domain)
        if worker_socket:
            return worker_socket

        for worker in self._get_route_targets(service_domain):
            if not self._blacklist_cache.is_worker_blacklisted(
                    worker['worker_id']):

                if worker['ip_address_v6']:
                    protocol = socket.AF_INET6
                    address = (worker['ip_address_v6'], 9001, 0, 0)
                else:
                    protocol = socket.AF_INET
                    address = (worker['ip_address_v4'], 9001)

                sock = socket.socket(protocol, socket.SOCK_STREAM)

                try:
                    sock.connect(address)
                    worker_socket = (worker, sock)
                    self._active_worker_socket[service_domain] = worker_socket
                    return worker_socket
                except socket.error as ex:
                    self._blacklist_worker(
                        service_domain, worker['worker_id'])
        return None

    def route_message(self, message):
        next_service_domain = self._get_next_service_domain()
        worker_socket = self._get_worker_socket(next_service_domain)
        while worker_socket:
            worker, sock = worker_socket
            try:
                self._dispatch.dispatch_message(message, sock)
                return
            except DispatchException:
                #TODO(dmend) log this and report to coordinator
                self._blacklist_worker(next_service_domain,
                                       worker['worker_id'])
            worker_socket = self._get_worker_socket(next_service_domain)
        raise RoutingException()