a _Æb.ã@sÒddlZddlZddlZddlZzddlmZmZWn"eyVddlmZmZYn0ddl m Z e  d¡Z Gdd„de ƒZGd d „d eƒZGd d „d eƒZGd d„de ƒZGdd„deƒZGdd„deƒZdS)éN)ÚEmptyÚ PriorityQueue)rré©ÚImproperlyConfiguredZ opensearchc@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:`~opensearchpy.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 úC/tmp/pip-target-4jja1joz/lib/python/opensearchpy/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 ÚselectCszConnectionSelector.selectN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr r r r r*src@seZdZdZdd„ZdS)ÚRandomSelectorz' Select a connection at random cCs t |¡S©N)ÚrandomÚchoicerr r r rQszRandomSelector.selectN)rrrrrr r r r rLsrcs(eZdZdZ‡fdd„Zdd„Z‡ZS)ÚRoundRobinSelectorz% Selector using round-robin. cstt|ƒ |¡t ¡|_dSr)ÚsuperrrÚ threadingÚlocalÚdatar ©Ú __class__r r rZszRoundRobinSelector.__init__cCs6t|jddƒd|j_|jjt|ƒ;_||jjS)NÚrréÿÿÿÿr)Úgetattrrr!Úlenrr r r r^szRoundRobinSelector.select)rrrrrrÚ __classcell__r r rr rUs rc@sVeZdZdZddedfdd„Zddd „Zd d „Zdd d„Zdd„Z dd„Z dd„Z dS)ÚConnectionPoolaY Container holding the :class:`~opensearchpy.Connection` instances, managing the selection process (via a :class:`~opensearchpy.ConnectionSelector`) and dead connections. It's only interactions are with the :class:`~opensearchpy.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:`~opensearchpy.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:`~opensearchpy.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 Ú •óz+ConnectionPool.__init__..N)rrrÚtupleÚorig_connectionsrr$ÚdeadÚ dead_countrÚshuffleÚ dead_timeoutÚtimeout_cutoffÚdictÚselector)r rr2r3Zselector_classZrandomize_hostsÚkwargsr r r r{sÿ  zConnectionPool.__init__NcCs |r|nt ¡}z|j |¡Wn ty@t d|¡YdS0|j |d¡d}||j|<|jdt |d|j ƒ}|j   |||f¡t  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.NrrézMConnection %r has failed for %i times in a row, putting on %i second timeout.)ÚtimerÚremoveÚ ValueErrorÚloggerÚinfor0Úgetr2Úminr3r/ÚputÚwarning)r Ú connectionÚnowr0Útimeoutr r r Ú mark_dead§s& þ üzConnectionPool.mark_deadcCs$z |j|=WntyYn0dS)z© Mark connection as healthy after a resurrection. Resets the fail counter for the connection. :arg connection: the connection to redeem N)r0ÚKeyError)r rAr r r Ú mark_liveÅs  zConnectionPool.mark_liveFcCs |j ¡r|rt |j¡SdSz|jjdd\}}Wn(ty\|rVt |j¡YSYdS0|s‚|t ¡kr‚|j ||f¡dS|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=rr8r?rÚappendr;r<)r ÚforcerCrAr r r Ú resurrectÒs    zConnectionPool.resurrectcCsD| ¡|jdd…}|s$| d¡St|ƒ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 available 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)rKrr$r5rrr r r Úget_connectionüs    zConnectionPool.get_connectioncCs|jD] }| ¡qdS©z/ Explicitly closes connections N)rÚclose)r Úconnr r r rNs zConnectionPool.closecCsdt|ƒj|jfS)Nz<%s: %r>)Útyperr©r r r r Ú__repr__szConnectionPool.__repr__)N)F) rrrrrrrDrFrKrLrNrRr r r r r&dsú ,  *r&c@s8eZdZdd„Zdd„Zdd„Zdd„ZeZZZ d S) ÚDummyConnectionPoolcKs6t|ƒdkrtdƒ‚||_|dd|_|jf|_dS)Nrz9DummyConnectionPool needs exactly one connection defined.r)r$rrrAr)r rr6r r r r!s ÿzDummyConnectionPool.__init__cCs|jSr)rArQr r r rL+sz"DummyConnectionPool.get_connectioncCs|j ¡dSrM)rArNrQr r r rN.szDummyConnectionPool.closecOsdSrr ©r Úargsr6r r r Ú_noop4szDummyConnectionPool._noopN) rrrrrLrNrVrDrFrKr r r r rS s  rSc@s8eZdZdZdd„Zdd„Zdd„ZeZZZ Z dS) ÚEmptyConnectionPoolz4A connection pool that is empty. Errors out if used.cOsg|_g|_dSr)rr)r Ú_Ú__r r r r=szEmptyConnectionPool.__init__cCs tdƒ‚dS)NzNo connections were configuredrrQr r r rLAsz"EmptyConnectionPool.get_connectioncOsdSrr rTr r r rVDszEmptyConnectionPool._noopN) rrrrrrLrVrNrDrFrKr r r r rW:s rW)Úloggingrrr8ÚQueuerrÚ ImportErrorÚqueueÚ exceptionsrÚ getLoggerr;Úobjectrrrr&rSrWr r r r Ús    " =