3 ]<@sddlZddlmZddlmZddlmZddlmZm Z ddl m Z m Z m Z ddlmZdd lmZmZmZmZd d ZGd d d eZdS)N)chain)python_version)Urllib3HttpConnection)ConnectionPoolDummyConnectionPool)JSONSerializer DeserializerDEFAULT_SERIALIZERS)__versionstr__)ConnectionErrorTransportErrorSerializationErrorConnectionTimeoutcCs|jdgdgkrdS|S)an Simple callback that takes the node info from `/_cluster/nodes` and a parsed connection information and return the connection information. If `None` is returned this node will be skipped. Useful for filtering nodes (by proximity for example) or if additional information needs to be provided for the :class:`~elasticsearch.Connection` class. By default master only nodes are filtered out since they shouldn't typically be used for API operations. :arg node_info: node information from `/_cluster/nodes` :arg host: connection information (host, port) extracted from the node info ZrolesZmasterN)get) node_infohostr@/tmp/pip-build-z81i1n8o/elasticsearch/elasticsearch/transport.py get_host_infosrc@seZdZdZeeeddddedddddd fd d Zd dZ ddZ ddZ d ddZ ddZ d!ddZddZd"ddZddZdS)# Transportz Encapsulation of transport-related to logic. Handles instantiation of the individual connections as well as creating a connection pool to hold them. Main interface is the `perform_request` method. FNg?zapplication/jsonGETcKstj}| || j<| r |j| t|| |_| |_||_| |_||_ | |_ ||_ ||_ ||_ ||_|j||jjdd|_||_||_tj|_||_||_|r|jddS)a :arg hosts: list of dictionaries, each containing keyword arguments to create a `connection_class` instance :arg connection_class: subclass of :class:`~elasticsearch.Connection` to use :arg connection_pool_class: subclass of :class:`~elasticsearch.ConnectionPool` to use :arg host_info_callback: callback responsible for taking the node information from `/_cluser/nodes`, along with already extracted information, and producing a list of arguments (same as `hosts` parameter) :arg sniff_on_start: flag indicating whether to obtain a list of nodes from the cluser at startup time :arg sniffer_timeout: number of seconds between automatic sniffs :arg sniff_on_connection_fail: flag controlling if connection failure triggers a sniff :arg sniff_timeout: timeout used for the sniff request - it should be a fast api call and we are talking potentially to more nodes so we want to fail quickly. Not used during initial sniffing (if ``sniff_on_start`` is on) when the connection still isn't initialized. :arg serializer: serializer instance :arg serializers: optional dict of serializer instances that will be used for deserializing data coming from the server. (key is the mimetype) :arg default_mimetype: when no mimetype is specified by the server response assume this mimetype, defaults to `'application/json'` :arg max_retries: maximum number of retries before an exception is propagated :arg retry_on_status: set of HTTP status codes on which we should retry on a different node. defaults to ``(502, 503, 504)`` :arg retry_on_timeout: should timeout trigger a retry on different node? (default `False`) :arg send_get_body_as: for GET requests with body this option allows you to specify an alternate way of execution for environments that don't support passing bodies with GET requests. If you set this to 'POST' a POST method will be used instead, if to 'source' then the body will be serialized and passed as a query parameter `source`. Any extra keyword arguments will be passed to the `connection_class` when creating and instance unless overridden by that connection's options provided as part of the hosts parameter. NT)r copymimetypeupdater deserializer max_retriesretry_on_timeoutretry_on_statussend_get_body_as serializerconnection_pool_classconnection_classkwargshostsset_connectionsconnection_pool connectionsseed_connectionssniffer_timeoutsniff_on_connection_failtime last_sniff sniff_timeouthost_info_callback sniff_hosts)selfr(r&r%r2Zsniff_on_startr-r1r.r$Z serializersZdefault_mimetyper r"r!r#r'Z _serializersrrr__init__-s.:     zTransport.__init__cCs|jj||j|jdS)z Create a new :class:`~elasticsearch.Connection` instance and add it to the pool. :arg host: kwargs that will be used to create the instance N)r(appendr))r4rrrradd_connections zTransport.add_connectioncsRfdd}t||}tt||}t|dkr._create_connectionrN)maplistziplenrr*r%r')r4r(r:r+r)r4rr)s    zTransport.set_connectionscCs,|jr"tj|j|jkr"|j|jjS)z Retreive a :class:`~elasticsearch.Connection` instance from the :class:`~elasticsearch.ConnectionPool` instance. )r-r/r0r3r*get_connection)r4rrrr?szTransport.get_connectioncCs|j}ytj|_xvt|jj|jD]X}y:|jdd|s<|jndd\}}}|jj ||j d}PWq$t t fk rzYq$Xq$Wt ddWn||_YnXt|djS) a Perform the request to get sniffins information. Returns a list of dictionaries (one per node) containing all the information from the cluster. It also sets the last_sniff attribute in case of a successful attempt. In rare cases it might be possible to override this method in your custom Transport class to serve data from alternative source like configuration management. rz/_nodes/_all/httpN)timeoutz content-typezN/AzUnable to sniff hosts.Znodes)r0r/rr*r+r,perform_requestr1rloadsrr rr r<values)r4initialZprevious_sniffc_headersrrrr_get_sniff_datas&   zTransport._get_sniff_datacCs\i}|jdijd}| s$d|kr(dS|jdd\|d<|d<t|d|d<|j||S)NhttpZpublish_address:rrport)rrsplitintr2)r4Z host_inforaddressrrr_get_host_infoszTransport._get_host_infocsBj|}ttdfdd|D}|s4tddj|dS)af Obtain a list of nodes from the cluster and create a new connection pool using the information retrieved. To extract the node connection parameters use the ``nodes_to_host_callback``. :arg initial: flag indicating if this is during startup (``sniff_on_start``), ignore the ``sniff_timeout`` if ``True`` Nc3s|]}j|VqdS)N)rO).0n)r4rr sz(Transport.sniff_hosts..zN/Az.Unable to sniff hosts - no viable hosts found.)rHr<filterr r))r4rDrr(r)r4rr3s zTransport.sniff_hostscCs|jj||jr|jdS)z Mark a connection as dead (failed) in the connection pool. If sniffing on failure is enabled this will initiate the sniffing process. :arg connection: instance of :class:`~elasticsearch.Connection` that failed N)r* mark_deadr.r3)r4r9rrrrTs zTransport.mark_deadcCs|dk rX|jj|}|dkrX|jdkrX|jdkr6d}n"|jdkrX|dkrLi}||d<d}|dk ry|jdd}Wnttfk rYnXf}d}|r|jdd}|jd f}t|tr|f}x.t |j d D]}|j } y"| j |||||||d \} } } Wnt k r} z~|dkr.| jd kr.d Sd }t| trF|j}n$t| trXd}n| j|jkrjd}|r|j| ||j krnWYdd} ~ XqX|jj| |dkrd| kodkSS| r|jj| | jd} | SqWdS)a Perform the actual request. Retrieve a connection from the connection pool, pass all the information to it's perform_request method and return the data. If an exception was raised, mark the connection as failed and retry (up to `max_retries` times). If the operation was succesful and the connection used was previously marked as dead, mark it as live, resetting it's failure count. :arg method: HTTP method to use :arg url: absolute url (without host) to target :arg headers: dictionary of headers, will be handed over to the underlying :class:`~elasticsearch.Connection` class :arg params: dictionary of query parameters, will be handed over to the underlying :class:`~elasticsearch.Connection` class for serialization :arg body: body of the request, will be serializes using serializer and passed to the connection NHEADrPOSTsourcezutf-8 surrogatepassrequest_timeoutignorer)rGrZr@iFTi,z content-type)rUr)r$dumpsr#encodeUnicodeDecodeErrorAttributeErrorpop isinstancerMranger r?rAr status_coderr!r r"rTr*Z mark_liverrBr)r4methodurlrGparamsbodyrZr@attemptr9statusZheaders_responsedataeretryrrrrA sn            zTransport.perform_requestcCs|jjdS)z/ Explicitly closes connections N)r*close)r4rrrrmszTransport.close)rrr)F)F)NNN)__name__ __module__ __qualname____doc__rrrrr5r7r)r?rHrOr3rTrArmrrrrr%s0 T " *  cr)r/ itertoolsrplatformrr9rr*rrr$rr r r exceptionsr r rrrobjectrrrrrs