3 ]K' @sddlZddlZddlZddlZyddlmZmZWn$ek rXddlmZmZYnXddl m Z ej dZ Gddde ZGdd d eZGd d d eZGd d d e ZGdddeZdS)N) PriorityQueueEmpty)ImproperlyConfiguredZ elasticsearchc@s eZdZdZddZddZdS)ConnectionSelectora Simple class used to select a connection from a list of currently live connection instances. In init time it is passed a dictionary containing all the connections' options which it can then use during the selection process. When the `select` method is called it is given a list of *currently* live connections to choose from. The options dictionary is the one that has been passed to :class:`~elasticsearch.Transport` as `hosts` param and the same that is used to construct the Connection object itself. When the Connection was created from information retrieved from the cluster via the sniffing process it will be the dictionary returned by the `host_info_callback`. Example of where this would be useful is a zone-aware selector that would only select connections from it's own zones and only fall back to other connections where there would be none in it's zones. cCs ||_dS)zQ :arg opts: dictionary of connection instances and their options N)connection_opts)selfoptsr F/tmp/pip-build-z81i1n8o/elasticsearch/elasticsearch/connection_pool.py__init__#szConnectionSelector.__init__cCsdS)z} Select a connection from the given list. :arg connections: list of live connections to choose from Nr )r connectionsr r r select)szConnectionSelector.selectN)__name__ __module__ __qualname____doc__r rr r r r rsrc@seZdZdZddZdS)RandomSelectorz' Select a connection at random cCs tj|S)N)randomchoice)rr r r r r7szRandomSelector.selectN)rrrrrr r r r r2srcs(eZdZdZfddZddZZS)RoundRobinSelectorz% Selector using round-robin. cstt|j|tj|_dS)N)superrr threadinglocaldata)rr ) __class__r r r @szRoundRobinSelector.__init__cCs6t|jddd|j_|jjt|;_||jjS)Nrrr)getattrrrlen)rr r r r rDszRoundRobinSelector.select)rrrrr r __classcell__r r )rr r;s rc@sNeZdZdZddedfddZddd Zd d Zdd dZddZ ddZ dS)ConnectionPoola\ Container holding the :class:`~elasticsearch.Connection` instances, managing the selection process (via a :class:`~elasticsearch.ConnectionSelector`) and dead connections. It's only interactions are with the :class:`~elasticsearch.Transport` class that drives all the actions within `ConnectionPool`. Initially connections are stored on the class as a list and, along with the connection options, get passed to the `ConnectionSelector` instance for future reference. Upon each request the `Transport` will ask for a `Connection` via the `get_connection` method. If the connection fails (it's `perform_request` raises a `ConnectionError`) it will be marked as dead (via `mark_dead`) and put on a timeout (if it fails N times in a row the timeout is exponentially longer - the formula is `default_timeout * 2 ** (fail_count - 1)`). When the timeout is over the connection will be resurrected and returned to the live pool. A connection that has been previously marked as dead and succeeds will be marked as live (its fail count will be deleted). <TcKsr|s td||_dd|D|_t|j|_tt|j|_i|_|rTt j |j||_ ||_ |t ||_dS)a :arg connections: list of tuples containing the :class:`~elasticsearch.Connection` instance and it's options :arg dead_timeout: number of seconds a connection should be retired for after a failure, increases on consecutive failures :arg timeout_cutoff: number of consecutive failures after which the timeout doesn't increase :arg selector_class: :class:`~elasticsearch.ConnectionSelector` subclass to use if more than one connection is live :arg randomize_hosts: shuffle the list of connections upon arrival to avoid dog piling effect across processes z>No defined connections, you need to specify at least one host.cSsg|] \}}|qSr r ).0cr r r r {sz+ConnectionPool.__init__..N)rrr tupleorig_connectionsrrdead dead_countrshuffle dead_timeouttimeout_cutoffdictselector)rr r,r-Zselector_classZrandomize_hostskwargsr r r r as  zConnectionPool.__init__Nc Cs|r|ntj}y|jj|Wntk r4dSX|jj|dd}||j|<|jdt|d|j}|j j |||ft j d|||dS)z Mark the connection as dead (failed). Remove it from the live pool and put it on a timeout. :arg connection: the failed instance NrrzMConnection %r has failed for %i times in a row, putting on %i second timeout.) timer remove ValueErrorr*getr,minr-r)putloggerwarning)r connectionnowr*timeoutr r r mark_deads zConnectionPool.mark_deadc Cs&y |j|=Wntk r YnXdS)z Mark connection as healthy after a resurrection. Resets the fail counter for the connection. :arg connection: the connection to redeem N)r*KeyError)rr:r r r mark_lives zConnectionPool.mark_liveFc Cs|jjr|rtj|jSdSy|jjdd\}}Wn$tk rX|rTtj|jSdSX| r|tjkr|jj||fdS|j j |t j d|||S)a Attempt to resurrect a connection from the dead pool. It will try to locate one (not all) eligible (it's timeout is over) connection to return to the live pool. Any resurrected connection is also returned. :arg force: resurrect a connection even if there is none eligible (used when we have no live connections). If force is specified resurrect always returns a connection. NF)blockz&Resurrecting connection %r (force=%s).) r)emptyrrr(r5rr2r7r appendr8info)rforcer<r:r r r resurrects    zConnectionPool.resurrectcCsD|j|jdd}|s$|jdSt|dkr<|jj|S|dS)av Return a connection from the pool using the `ConnectionSelector` instance. It tries to resurrect eligible connections, forces a resurrection when no connections are availible and passes the list of live connections to the selector instance to choose from. Returns a connection instance and it's current fail count. NTrr)rEr rr/r)rr r r r get_connections    zConnectionPool.get_connectioncCsx|jD] }|jqWdS)z/ Explicitly closes connections N)r(close)rconnr r r rGs zConnectionPool.close)N)F) rrrrrr r=r?rErFrGr r r r r!Js &  *r!c@s8eZdZddZddZddZddZeZZZ d S) DummyConnectionPoolcKs6t|dkrtd||_|dd|_|jf|_dS)Nrz9DummyConnectionPool needs exactly one connection defined.r)rrrr:r )rr r0r r r r s  zDummyConnectionPool.__init__cCs|jS)N)r:)rr r r rF sz"DummyConnectionPool.get_connectioncCs|jjdS)z/ Explicitly closes connections N)r:rG)rr r r rG szDummyConnectionPool.closecOsdS)Nr )rargsr0r r r _noopszDummyConnectionPool._noopN) rrrr rFrGrKr=r?rEr r r r rIs  rI)r2rloggingrQueuerr ImportErrorqueue exceptionsr getLoggerr8objectrrrr!rIr r r r s  " 6