U Dc_,@sddlZddlZddlZddlZzddlmZmZWn$ek rXddlmZmZYnXddl m Z e dZ Gddde ZGdd d eZGd d d eZGd d d e ZGdddeZGdddeZdS)N) PriorityQueueEmptyImproperlyConfiguredZ 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_optsselfoptsr D/tmp/pip-target-m1bursmm/lib/python/elasticsearch/connection_pool.py__init__4szConnectionSelector.__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__rrr r r r r!src@seZdZdZddZdS)RandomSelectorz' Select a connection at random cCs t|SN)randomchoicerr r r rHszRandomSelector.selectN)rrrrrr r r r rCsrcs(eZdZdZfddZddZZS)RoundRobinSelectorz% Selector using round-robin. cstt||t|_dSr)superrr threadinglocaldatar  __class__r r rQszRoundRobinSelector.__init__cCs6t|jddd|j_|jjt|;_||jjS)Nrrr)getattrrr!lenrr r r rUszRoundRobinSelector.select)rrrrrr __classcell__r r rr rLs rc@sVeZdZdZddedfddZddd Zd d Zdd dZddZ ddZ 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||_ ||_ |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)rrrtupleorig_connectionsrr$dead dead_countrshuffle dead_timeouttimeout_cutoffdictselector)r rr1r2Zselector_classZrandomize_hostskwargsr r r rrs  zConnectionPool.__init__NcCs|r|nt}z|j|Wn"tk rBtd|YdSX|j|dd}||j|<|jdt |d|j }|j |||ft 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 zEAttempted to remove %r, but it does not exist in the connection pool.NrrzMConnection %r has failed for %i times in a row, putting on %i second timeout.)timerremove ValueErrorloggerinfor/getr1minr2r.putwarning)r connectionnowr/timeoutr r r mark_deads& zConnectionPool.mark_deadcCs&z |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)r r@r r r mark_lives zConnectionPool.mark_liveFcCs|jr|rt|jSdSz|jjdd\}}Wn*tk r^|rXt|jYSYdSX|s|tkr|j||fdS|j |t 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-r<rr7r>rappendr:r;)r forcerBr@r r r resurrects   zConnectionPool.resurrectcCsD||jdd}|s$|dSt|dkr<|j|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)rJrr$r4rrr r r get_connections    zConnectionPool.get_connectioncCs|jD] }|qdSz/ Explicitly closes connections N)rclose)r connr r r rM s zConnectionPool.closecCsdt|j|jfS)Nz<%s: %r>)typerrr r r r __repr__szConnectionPool.__repr__)N)F) rrrrrrrCrErJrKrMrQr r r r r&[s ,  *r&c@s8eZdZddZddZddZddZeZZZ d S) DummyConnectionPoolcKs6t|dkrtd||_|dd|_|jf|_dS)Nrz9DummyConnectionPool needs exactly one connection defined.r)r$rrr@r)r rr5r r r rs zDummyConnectionPool.__init__cCs|jSr)r@rPr r r rK"sz"DummyConnectionPool.get_connectioncCs|jdSrL)r@rMrPr r r rM%szDummyConnectionPool.closecOsdSrr r argsr5r r r _noop+szDummyConnectionPool._noopN) rrrrrKrMrUrCrErJr r r r rRs  rRc@s8eZdZdZddZddZddZeZZZ Z dS) EmptyConnectionPoolz4A connection pool that is empty. Errors out if used.cOsg|_g|_dSr)rr)r ___r r r r4szEmptyConnectionPool.__init__cCs tddS)NzNo connections were configuredrrPr r r rK8sz"EmptyConnectionPool.get_connectioncOsdSrr rSr r r rU;szEmptyConnectionPool._noopN) rrrrrrKrUrMrCrErJr r r r rV1s rV)r7rloggingrQueuerr ImportErrorqueue exceptionsr getLoggerr:objectrrrr&rRrVr r r r s   " =