3 ²˜‚]ž<ã@s�ddlZddlmZddlmZddlmZddlmZm Z ddl m Z m Z m Z ddlmZdd lmZmZmZmZd d „ZGd d „d eƒZdS)éN)Úchain)Úpython_versioné)ÚUrllib3HttpConnection)ÚConnectionPoolÚDummyConnectionPool)ÚJSONSerializerÚ DeserializerÚDEFAULT_SERIALIZERS)Ú__versionstr__)ÚConnectionErrorÚTransportErrorÚSerializationErrorÚConnectionTimeoutcCs|jdgƒdgkrdS|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_infoÚhost©rú@/tmp/pip-build-z81i1n8o/elasticsearch/elasticsearch/transport.pyÚ get_host_infosrc@s†eZdZdZeeeddddeƒdddddd fd d „Zd d„Z dd„Z dd„Z d dd„Z dd„Z d!dd„Zdd„Zd"dd„Zdd„ZdS)#Ú 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/jsonééöé÷éøÚGETcKs²tjƒ}| || j<| r |j| ƒt|| ƒ|_| |_||_| |_||_ | |_ ||_ ||_ ||_ ||_|j|ƒ|jjdd…|_||_||_tjƒ|_||_||_|r®|jdƒdS)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 ÚcopyÚmimetypeÚupdater Ú deserializerÚ max_retriesÚretry_on_timeoutÚretry_on_statusÚsend_get_body_asÚ serializerÚconnection_pool_classÚconnection_classÚkwargsÚhostsÚset_connectionsÚconnection_poolÚ connectionsÚseed_connectionsÚsniffer_timeoutÚsniff_on_connection_failÚtimeÚ last_sniffÚ sniff_timeoutÚhost_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|jƒdS)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))r4rrrrÚadd_connection‘s zTransport.add_connectioncsR‡fdd„}t||ƒ}tt||ƒƒ}t|ƒdkr._create_connectionrN)ÚmapÚlistÚzipÚlenrr*r%r')r4r(r:r+r)r4rr)šs    zTransport.set_connectionscCs,|jr"tjƒ|j|jkr"|jƒ|jjƒS)zŠ Retreive a :class:`~elasticsearch.Connection` instance from the :class:`~elasticsearch.ConnectionPool` instance. )r-r/r0r3r*Úget_connection)r4rrrr?¼szTransport.get_connectioncCs²|j}y†tjƒ|_xvt|jj|jƒD]X}y:|jdd|s<|jndd�\}}}|jj ||j dƒƒ}PWq$t t fk rzYq$Xq$Wt ddƒ‚Wn||_‚YnXt|djƒƒS) 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_requestr1rÚloadsrr rr r<Úvalues)r4ÚinitialZprevious_sniffÚcÚ_ÚheadersrrrrÚ_get_sniff_dataÆs&   zTransport._get_sniff_datacCs\i}|jdiƒjdƒ}| s$d|kr(dS|jddƒ\|d<|d<t|dƒ|d<|j||ƒS)NÚhttpZpublish_addressú:rrÚport)rÚrsplitÚintr2)r4Z host_inforÚaddressrrrÚ_get_host_infoðszTransport._get_host_infocsBˆj|ƒ}ttd‡fdd„|Dƒƒƒ}|s4tddƒ‚ˆj|ƒ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)Ú.0Ún)r4rrú sz(Transport.sniff_hosts..zN/Az.Unable to sniff hosts - no viable hosts found.)rHr<Úfilterr r))r4rDrr(r)r4rr3ýs zTransport.sniff_hostscCs|jj|ƒ|jr|jƒdS)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 rŠy|jddƒ}Wnttfk rˆYnXf}d}|r¾|jddƒ}|jd fƒ}t|tƒr¾|f}�x.t |j d ƒD�]}|j ƒ} y"| j |||||||d �\} } } Wnœt k �rœ} z~|dk�r.| jd k�r.d Sd }t| tƒ�rF|j}n$t| tƒ�rXd}n| j|jk�rjd}|�rŠ|j| ƒ||j k�rŒ‚n‚WYdd} ~ XqÐX|jj| ƒ|dk�rÎd| k�oÈdkSS| �rè|jj| | jdƒƒ} | SqÐWdS)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 NÚHEADrÚPOSTÚsourcezutf-8Ú surrogatepassÚrequest_timeoutÚignorer)rGrZr@i”FTéÈi,z content-type)rUr)r$Údumpsr#ÚencodeÚUnicodeDecodeErrorÚAttributeErrorÚpopÚ isinstancerMÚranger r?rAr Ú status_coderr!r r"rTr*Z mark_liverrBr)r4ÚmethodÚurlrGÚparamsÚbodyrZr@Úattemptr9ÚstatusZheaders_responseÚdataÚeÚretryrrrrA sn            zTransport.perform_requestcCs|jjƒdS)z/ Explicitly closes connections N)r*Úclose)r4rrrrmƒszTransport.close)rrr)F)F)NNN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrr5r7r)r?rHrOr3rTrArmrrrrr%s0 T " *  cr)r/Ú itertoolsrÚplatformrr9rr*rrr$rr r Úr Ú exceptionsr r rrrÚobjectrrrrrÚs