_YÜ&ã @sddlZddlZddlZddlZyddlmZmZWn(ek rtddlmZmZYnXddl m Z ej dƒZ Gdd„de ƒZGdd „d eƒZGd d „d eƒZGd d „d e ƒZGdd„deƒZdS)éN)Ú PriorityQueueÚEmptyé)ÚImproperlyConfiguredZ elasticsearchc@s.eZdZdZdd„Zdd„ZdS)Ú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)ÚselfÚopts©r úF/tmp/pip-build-66pc0izb/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 rs  rc@s"eZdZdZdd„ZdS)ÚRandomSelectorz' Select a connection at random cCs tj|ƒS)N)ÚrandomÚchoice)rr r r r r4szRandomSelector.selectN)rrrrrr r r r r0s rcs4eZdZdZ‡fdd†Zdd„Z‡S)ÚRoundRobinSelectorz% Selector using round-robin. cs)tt|ƒj|ƒtjƒ|_dS)N)Úsuperrr Ú threadingÚlocalÚdata)rr )Ú __class__r r r <szRoundRobinSelector.__init__cCsEt|jddƒd|j_|jjt|ƒ;_||jjS)NÚrrréÿÿÿÿ)ÚgetattrrrÚlen)rr r r r r@szRoundRobinSelector.select)rrrrr rr r )rr r8s rc@speZdZdZddeddd„Zddd „Zd d „Zd d d„Zdd„Z dd„Z 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 peviously marked as dead and succeedes will be marked as live (it's fail count will be deleted). é<éTcKs¥|stdƒ‚||_dd„|Dƒ|_t|jƒ|_tt|jƒƒ|_i|_|rzt 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 )Ú.0Úcr r r r ú ms z+ConnectionPool.__init__..N)rrr ÚtupleÚorig_connectionsrrÚdeadÚ dead_countrÚshuffleÚ dead_timeoutÚtimeout_cutoffÚdictÚselector)rr r+r,Zselector_classZrandomize_hostsÚkwargsr r r r [s     zConnectionPool.__init__Nc CsÀ|r |n tjƒ}y|jj|ƒWntk rDdSYnxX|jj|dƒd}||j|<|jdt|d|jƒ}|j j |||fƒt 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 NrrézMConnection %r has failed for %i times in a row, putting on %i second timeout.) Útimer ÚremoveÚ ValueErrorr)Úgetr+Úminr,r(ÚputÚloggerÚwarning)rÚ connectionÚnowr)Ú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)rr9r r r Ú mark_live—s zConnectionPool.mark_liveFc CsÑ|jjƒr)|r%tj|jƒSdSy|jjddƒ\}}Wn,tk rv|rntj|jƒSdSYnX| rª|tjƒkrª|jj||fƒdS|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. NÚblockFz&Resurrecting connection %r (force=%s).) r(Úemptyrrr'r4rr1r6r Úappendr7Úinfo)rÚforcer;r9r r r Ú resurrect¤s   zConnectionPool.resurrectcCsZ|jƒ|jdd…}|s0|jdƒSt|ƒdkrR|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)rDr rr.r)rr r r r Úget_connectionÎs  zConnectionPool.get_connectioncCs"x|jD]}|jƒq WdS)z/ Explicitly closes connections N)r'Úclose)rÚconnr r r rFçszConnectionPool.close) rrrrrr r<r>rDrErFr r r r r Es # * r c@sNeZdZdd„Zdd„Zdd„Zdd„ZeZZZ d S) ÚDummyConnectionPoolcKsKt|ƒdkrtdƒ‚||_|dd|_|jf|_dS)Nrz9DummyConnectionPool needs exactly one connection defined.r)rrrr9r )rr r/r r r r ïs   zDummyConnectionPool.__init__cCs|jS)N)r9)rr r r rEøsz"DummyConnectionPool.get_connectioncCs|jjƒdS)z/ Explicitly closes connections N)r9rF)rr r r rFûszDummyConnectionPool.closecOsdS)Nr )rÚargsr/r r r Ú_noopszDummyConnectionPool._noopN) rrrr rErFrJr<r>rDr r r r rHîs    rH)r1rÚloggingrÚQueuerrÚ ImportErrorÚqueueÚ exceptionsrÚ getLoggerr7Úobjectrrrr rHr r r r Ús     ! ©