_Y;@sddlZddlmZddlmZddlmZmZddlm Z m Z m Z ddl m Z mZmZmZmZdd ZGd d d eZdS) N)chain)Urllib3HttpConnection)ConnectionPoolDummyConnectionPool)JSONSerializer DeserializerDEFAULT_SERIALIZERS)ConnectionErrorTransportErrorSerializationErrorConnectionTimeoutImproperlyConfiguredcCs#|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-66pc0izb/elasticsearch/elasticsearch/transport.py get_host_info src@seZdZdZeeeddddedddddd d d Zd dZ ddZ ddZ dddZ ddZ dddZddZddddZddZdS) 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 overriden 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$r1Zsniff_on_startr,r0r-r#Z serializersZdefault_mimetyperr!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()r3rrrradd_connection|szTransport.add_connectioncsvfdd}t||}tt||}t|dkrZt|_nj|j_dS)z Instantiate all the connections and crate new connection pool to hold them. Tries to identify unchanged hosts and re-use existing :class:`~elasticsearch.Connection` instances. :arg hosts: same as `__init__` cstdr<x*jjD]\}}||kr|SqWjj}|j|d|kr|djjkrtd|djj jjfj|S)Nr)schemez`Scheme specified in connection (%s) is not the same as the connection class (%s) specifies (%s).) hasattrr)Zconnection_optsr&rrr%Ztransport_schemar__name__)r connectionZold_hostr&)r3rr_create_connections  "#z5Transport.set_connections.._create_connectionrN)maplistziplenrr)r$r&)r3r'r;r*r)r3rr(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.r/r2r)get_connection)r3rrrr@s  zTransport.get_connectioncCs|j}ytj|_xt|jj|jD]t}yS|jddd|s[|jnd\}}}|jj ||j d}PWq4t t fk rYq4Xq4Wt 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/httptimeoutNz content-typezN/AzUnable to sniff hosts.Znodes)r/r.rr)r*r+perform_requestr0rloadsrr r r r=values)r3initialZprevious_sniffc_headersrrrr_get_sniff_datas"  !  zTransport._get_sniff_datacCs|i}|jdijd}| s4d|kr8dS|jdd\|d<|dsz(Transport.sniff_hosts..zN/Az.Unable to sniff hosts - no viable hosts found.)rIr=filterr r()r3rErr'r)r3rr2s (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-r2)r3r:rrrrUs zTransport.mark_deadcCs|dk r|jj|}|dkr|jdkr|jdkrQd}n1|jdkr|dkrri}||ds (