当前位置: 首页>>代码示例>>Python>>正文


Python DowntimePronePool.mark_dead方法代码示例

本文整理汇总了Python中pyelasticsearch.downtime.DowntimePronePool.mark_dead方法的典型用法代码示例。如果您正苦于以下问题:Python DowntimePronePool.mark_dead方法的具体用法?Python DowntimePronePool.mark_dead怎么用?Python DowntimePronePool.mark_dead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyelasticsearch.downtime.DowntimePronePool的用法示例。


在下文中一共展示了DowntimePronePool.mark_dead方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ElasticSearch

# 需要导入模块: from pyelasticsearch.downtime import DowntimePronePool [as 别名]
# 或者: from pyelasticsearch.downtime.DowntimePronePool import mark_dead [as 别名]

#.........这里部分代码省略.........
        and retrying.

        Retry the request on different servers if the first one is down and
        ``self.max_retries`` > 0.

        :arg method: An HTTP method, like "GET"
        :arg path_components: An iterable of path components, to be joined by
            "/"
        :arg body: The request body
        :arg query_params: A map of querystring param names to values or
            ``None``
        :arg encode_body: Whether to encode the body of the request as JSON
        """
        path = self._join_path(path_components)
        if query_params:
            path = "?".join(
                [path, urlencode(dict((k, self._utf8(self._to_query(v))) for k, v in iteritems(query_params)))]
            )

        request_body = self._encode_json(body) if encode_body else body
        req_method = getattr(self.session, method.lower())

        # We do our own retrying rather than using urllib3's; we want to retry
        # a different node in the cluster if possible, not the same one again
        # (which may be down).
        for attempt in xrange(self.max_retries + 1):
            server_url, was_dead = self.servers.get()
            url = server_url + path
            self.logger.debug("Making a request equivalent to this: curl -X%s '%s' -d '%s'", method, url, request_body)

            try:
                resp = req_method(url, timeout=self.timeout, **({"data": request_body} if body else {}))
            except (ConnectionError, Timeout):
                self.servers.mark_dead(server_url)
                self.logger.info("%s marked as dead for %s seconds.", server_url, self.revival_delay)
                if attempt >= self.max_retries:
                    raise
            else:
                if was_dead:
                    self.servers.mark_live(server_url)
                break

        self.logger.debug("response status: %s", resp.status_code)
        prepped_response = self._decode_response(resp)
        if resp.status_code >= 400:
            self._raise_exception(resp, prepped_response)
        self.logger.debug("got response %s", prepped_response)
        return prepped_response

    def _raise_exception(self, response, decoded_body):
        """Raise an exception based on an error-indicating response from ES."""
        error_message = decoded_body.get("error", decoded_body)

        error_class = ElasticHttpError
        if response.status_code == 404:
            error_class = ElasticHttpNotFoundError
        elif (
            error_message.startswith("IndexAlreadyExistsException")
            or "nested: IndexAlreadyExistsException" in error_message
        ):
            error_class = IndexAlreadyExistsError

        raise error_class(response.status_code, error_message)

    def _encode_json(self, value):
        """
开发者ID:imAlan,项目名称:Scout,代码行数:70,代码来源:client.py

示例2: ElasticSearch

# 需要导入模块: from pyelasticsearch.downtime import DowntimePronePool [as 别名]
# 或者: from pyelasticsearch.downtime.DowntimePronePool import mark_dead [as 别名]

#.........这里部分代码省略.........
        """
        def join_path(path_components):
            """Smush together the path components, ignoring empty ones."""
            path = '/'.join(str(p) for p in path_components if p)
            if not path.startswith('/'):
                path = '/' + path
            return path

        path = join_path(path_components)
        if query_params:
            path = '?'.join(
                [path, urlencode(dict((k, self._to_query(v)) for k, v in
                                      query_params.iteritems()))])

        kwargs = ({'data': self._encode_json(body) if encode_body else body}
                   if body else {})
        req_method = getattr(self.session, method.lower())

        # We do our own retrying rather than using urllib3's; we want to retry
        # a different node in the cluster if possible, not the same one again
        # (which may be down).
        for attempt in xrange(self.max_retries + 1):
            server_url, was_dead = self.servers.get()
            url = server_url + path
            self.logger.debug(
                'making %s request to path: %s %s with body: %s',
                method, url, path, kwargs.get('data', {}))
            try:
                # prefetch=True so the connection can be quickly returned to
                # the pool. This is the default in requests >=0.3.16.
                resp = req_method(
                    url, prefetch=True, timeout=self.timeout, **kwargs)
            except (ConnectionError, Timeout):
                self.servers.mark_dead(server_url)
                self.logger.info('%s marked as dead for %s seconds.',
                                 server_url,
                                 self.revival_delay)
                if attempt >= self.max_retries:
                    raise
            else:
                if was_dead:
                    self.servers.mark_live(server_url)
                break

        self.logger.debug('response status: %s', resp.status_code)
        prepped_response = self._decode_response(resp)
        if resp.status_code >= 400:
            error_class = (ElasticHttpNotFoundError if resp.status_code == 404
                           else ElasticHttpError)
            raise error_class(
                resp.status_code,
                prepped_response.get('error', prepped_response))
        self.logger.debug('got response %s', prepped_response)
        return prepped_response

    def _encode_json(self, body):
        """Return body encoded as JSON."""
        return json.dumps(body, cls=self.json_encoder)

    def _decode_response(self, response):
        """Return a native-Python representation of a response's JSON blob."""
        json_response = response.json
        if json_response is None:
            raise InvalidJsonResponseError(response)
        return json_response
开发者ID:chrisfauerbach,项目名称:pyelasticsearch,代码行数:69,代码来源:client.py


注:本文中的pyelasticsearch.downtime.DowntimePronePool.mark_dead方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。