
    iǈ                        d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	 ddl
Z
	 ddl
mZ n# e$ r	 ddlmZ Y nw xY wddlmZ ddlmZmZmZ ddlmZ  ej        e          Z G d	 d
e          Ze G d de                      Z G d de          Z G d de          Z G d de          ZdZ dZ! G d de          Z"dS )z)
Connection pooling and host management.
    )total_orderingN)LockRLock	Condition)WeakSet)AuthenticationFailed)ConnectionExceptionEndPointDefaultEndPoint)HostDistancec                       e Zd ZdZdS )NoConnectionsAvailablezb
    All existing connections to a given host are busy, or there are
    no open connections.
    N)__name__
__module____qualname____doc__     `/Users/user/workspace/sujinbaek/cqa-test-app/venv/lib/python3.11/site-packages/cassandra/pool.pyr   r   '   s          	Dr   r   c                      e Zd ZdZdZ	 dZ	 dZ	 dZ	 dZ	 dZ		 dZ
	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZ	 dZdZdZdZdZddZed             Zed             Zed             Zd Zd	 Zd
 Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&dS )Hostz-
    Represents a single Cassandra node.
    NFc                     |t          d          |t          d          t          |t                    r|nt          |          | _         ||           | _        || _        |                     ||           t                      | _	        d S )Nzendpoint may not be Nonez)conviction_policy_factory may not be None)

ValueError
isinstancer
   r   endpointconviction_policyhost_idset_location_infor   lock)selfr   conviction_policy_factory
datacenterrackr   s         r   __init__zHost.__init__   s    7888$,HIII$.x$B$BaX`HaHa!:!:4!@!@z4000GG			r   c                     | j         j        S )zv
        The IP address of the endpoint. This is the RPC address the driver uses when connecting to the node.
        )r   addressr    s    r   r&   zHost.address   s     }$$r   c                     | j         S )z! The datacenter the node is in.  )_datacenterr'   s    r   r"   zHost.datacenter   s     r   c                     | j         S )z The rack the node is in.  )_rackr'   s    r   r#   z	Host.rack   s     zr   c                 "    || _         || _        dS )z
        Sets the datacenter and rack for this node. Intended for internal
        use (by the control connection, which periodically checks the
        ring topology) only.
        N)r)   r+   )r    r"   r#   s      r   r   zHost.set_location_info   s     &


r   c                     | j         s t                              d| j                   | j                                         d| _         d S )NzHost %s is now marked upT)is_uplogdebugr   r   resetr'   s    r   set_upzHost.set_up   sD    z 	AII0$-@@@$$&&&


r   c                     d| _         d S )NF)r.   r'   s    r   set_downzHost.set_down   s    


r   c                 6    | j                             |          S N)r   add_failure)r    connection_excs     r   signal_connection_failurezHost.signal_connection_failure   s    %11.AAAr   c                     | j         d uS r6   )_reconnection_handlerr'   s    r   is_currently_reconnectingzHost.is_currently_reconnecting   s    )55r   c                 d    | j         5  | j        }|| _        |cddd           S # 1 swxY w Y   dS )zv
        Atomically replaces the reconnection handler for this
        host.  Intended for internal use only.
        N)r   r;   )r    new_handlerolds      r    get_and_set_reconnection_handlerz%Host.get_and_set_reconnection_handler   s    
 Y 	 	,C)4D&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   %))c                 l    t          |t                    r| j        |j        k    S | j        j        |k    S r6   )r   r   r   r&   r    others     r   __eq__zHost.__eq__   s3    eT"" 	2=EN22=(E11r   c                 *    t          | j                  S r6   )hashr   r'   s    r   __hash__zHost.__hash__   s    DM"""r   c                 "    | j         |j         k     S r6   )r   rB   s     r   __lt__zHost.__lt__   s    }u~--r   c                 *    t          | j                  S r6   )strr   r'   s    r   __str__zHost.__str__   s    4=!!!r   c                 \    | j         r
d| j         nd}d| j        j        d| j        |dS )N  <z: >)r)   	__class__r   r   )r    dcs     r   __repr__zHost.__repr__   sB    .2.>FFt''))B#~666rrrJJr   )NNN)'r   r   r   r   r   broadcast_addressbroadcast_portbroadcast_rpc_addressbroadcast_rpc_portlisten_addresslisten_portr   r.   release_versionr   dse_versiondse_workloaddse_workloadsr)   r+   r;   r   _currently_handling_node_upr$   propertyr&   r"   r#   r   r2   r4   r9   r<   r@   rD   rG   rI   rL   rT   r   r   r   r   r   /   s         H  N !  N K 
 E O G K
 L M KE D"'
 
 
 
 % % X%     X    X      B B B6 6 6  2 2 2# # #. . ." " "K K K K Kr   r   c                   @    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
S )_ReconnectionHandlerz^
    Abstract class for attempting reconnections with a given
    schedule and scheduler.
    Fc                 L    || _         || _        || _        || _        || _        d S r6   )	schedulerschedulecallbackcallback_argscallback_kwargs)r    rd   re   rf   rg   rh   s         r   r$   z_ReconnectionHandler.__init__  s-    "  *.r   c                     | j         rt                              d           d S t          | j                  }| j                            || j                   d S )Nz2Reconnection handler was cancelled before starting)
_cancelledr/   r0   nextre   rd   run)r    first_delays     r   startz_ReconnectionHandler.start
  sS    ? 	IIJKKKF4=))TX66666r   c                 :   | j         rd S d }	 |                                 }| j         s,|                     |            | j        | j        i | j         n# t          $ r}	 t          | j                  }n# t          $ r d }Y nw xY w| 
                    ||          r=|t                              d           n | j                            || j                   Y d }~nd }~ww xY w|r|                                 d S d S # |r|                                 w w xY w)NzSWill not continue to retry reconnection attempts due to an exhausted retry schedule)rj   try_reconnecton_reconnectionrf   rg   rh   	Exceptionrk   re   StopIterationon_exceptionr/   warningrd   rl   close)r    connexc
next_delays       r   rl   z_ReconnectionHandler.run  sp   ? 	F	%%''D" ? O$$T*** 2N8LNNN%  	B 	B 	B"!$-00

  " " "!


"
   j11 B%KK=> > > > N++JAAA	B(  

 t 

sM   A 4D 
C$ A54C5BCBACD C$$D Dc                     d| _         d S NT)rj   r'   s    r   cancelz_ReconnectionHandler.cancel0  s    r   c                     t                      )z
        Subclasses must implement this method.  It should attempt to
        open a new Connection and return it; if a failure occurs, an
        Exception should be raised.
        )NotImplementedErrorr'   s    r   rp   z"_ReconnectionHandler.try_reconnect3  s     "###r   c                     dS )zk
        Called when a new Connection is successfully opened.  Nothing is
        done by default.
        Nr   r    
connections     r   rq   z$_ReconnectionHandler.on_reconnection;  s	    
 	r   c                 4    t          |t                    rdS dS )a  
        Called when an Exception is raised when trying to connect.
        `exc` is the Exception that was raised and `next_delay` is the
        number of seconds (as a float) that the handler will wait before
        attempting to connect again.

        Subclasses should return :const:`False` if no more attempts to
        connection should be made, :const:`True` otherwise.  The default
        behavior is to always retry unless the error is an
        :exc:`.AuthenticationFailed` instance.
        FT)r   r   r    rx   ry   s      r   rt   z!_ReconnectionHandler.on_exceptionB  s!     c/00 	54r   N)r   r   r   r   rj   r$   rn   rl   r|   rp   rq   rt   r   r   r   rb   rb      s         
 J/ / /7 7 7  <  $ $ $      r   rb   c                   &    e Zd Zd Zd Zd Zd ZdS )_HostReconnectionHandlerc                 v    t          j        | g|R i | || _        || _        || _        || _        || _        d S r6   )rb   r$   is_host_additionon_addon_uphostconnection_factory)r    r   r   r   r   r   argskwargss           r   r$   z!_HostReconnectionHandler.__init__V  sN    %d<T<<<V<<< 0
	"4r   c                 *    |                                  S r6   )r   r'   s    r   rp   z&_HostReconnectionHandler.try_reconnect^  s    &&(((r   c                     t                               d| j                   | j        r|                     | j                   d S |                     | j                   d S )NzBSuccessful reconnection to %s, marking node up if it isn't already)r/   infor   r   r   r   r   s     r   rq   z(_HostReconnectionHandler.on_reconnectiona  sZ    UW[W`aaa  	"KK	"""""JJty!!!!!r   c                     t          |t                    rdS t                              d| j        ||           t                              dd           dS )NFzGError attempting to reconnect to %s, scheduling retry in %s seconds: %szReconnection error detailsT)exc_info)r   r   r/   ru   r   r0   r   s      r   rt   z%_HostReconnectionHandler.on_exceptionh  sX    c/00 	5KKa	:s4 4 4II2TIBBB4r   N)r   r   r   r$   rp   rq   rt   r   r   r   r   r   T  sP        5 5 5) ) )" " "    r   r   c                       e Zd ZdZdZdZdZdZdZdZ	dZ
dZd Zd Zd ZddZd Zd	 Zd
 Zd Zd Zd Zed             ZdS )HostConnectionz
    When using v3 of the native protocol, this is used instead of a connection
    pool per host (HostConnectionPool) due to the increased in-flight capacity
    of individual connections.
    NFc                    || _         || _        t          j        |          | _        t                      | _        t          | j                  | _        d| _	        t                      | _        |t          j        k    r"t                              d| j                    d S |t          j        k    r.|j        j        s"t                              d| j                    d S t                              d| j                    |j                            |j        | j                  | _        |j        | _        | j        r| j                            | j                   t                              d| j                    d S )NFz)Not opening connection to ignored host %sz(Not opening connection to remote host %sz#Initializing connection for host %son_orphaned_stream_releasedz,Finished initializing connection for host %s)r   host_distanceweakrefproxy_sessionr   _lockr   _stream_available_condition_is_replacingset_trashr   IGNOREDr/   r0   REMOTEclusterconnect_to_remote_hostsr   r   r   _connectionkeyspace	_keyspaceset_keyspace_blocking)r    r   r   sessions       r   r$   zHostConnection.__init__  sA   	*g..VV
+4TZ+@+@(" eeL000IIA49MMMFl111'/:a1II@$)LLLF		7CCC"?==dmim  jJ=  K  K )> 	C224>BBB		@$)LLLLLr   c                 ~    | j         rt          d| j        d| j                  | j        }|st	                      |S )N	Pool for  is shutdown)is_shutdownr	   r   r   r   r    rw   s     r   _get_connectionzHostConnection._get_connection  sY     	E%%-1YYY8$)E E E  	+(***r   c                 8   |                                  }|j        rm| j        5  | j        sGd| _        | j                            | j        |           t                              d| j	                   d d d            n# 1 swxY w Y   t          j
                    }|}	 |j        5  |j        r|j        sB|j        |j        k     r2|xj        dz  c_        ||                                fcd d d            S d d d            n# 1 swxY w Y   | |t          j
                    z
  |z   }|dk     rn]| j        5  |j        r|j        r|                                  }n| j                            |           d d d            n# 1 swxY w Y   t%          d          )NTzAConnection to host %s reached orphaned stream limit, replacing...   r   z$All request IDs are currently in use)r   orphaned_threshold_reachedr   r   r   submit_replacer/   r0   r   timer   	is_closed	in_flightmax_request_idget_request_idr   waitr   )r    timeoutrw   rn   	remainings        r   borrow_connectionz HostConnection.borrow_connection  s   ##%%* 	  ) )-D&M((===II[	  	               			E 7 77 7DN 7PTP^aeatPtPtNNa'NN!4!4!6!667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 "#dikk1E9	q==1 E E2 Et~ E//11DD499)DDD	E E E E E E E E E E E E E E E	E %%KLLLs7   AA>>BB&ADD
D7>FFFc                    |sg|j         5  |xj        dz  c_        d d d            n# 1 swxY w Y   | j        5  | j                                         d d d            n# 1 swxY w Y   |j        s|j        r(|j        r	| j        sd S d}|j        sat          	                    dt          |          | j                   | j        j                            | j        |j        d          }d|_        | j        r*|s(d}| j        j                            | j        d           |r|                                  d S d | _        | j        5  | j        r	 d d d            d S d| _        | j                            | j        |           d d d            d S # 1 swxY w Y   d S || j        v r|j         5  |j        t1          |j                  k    r| j        5  || j        v r\| j                            |           t          	                    dt          |          | j                   |                                 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   d S d S )Nr   FWDefunct or closed connection (%s) returned to pool, potentially marking host %s as downr   T%Closing trashed connection (%s) to %s)r   r   r   notify
is_defunctr   signaled_errorshutdown_on_errorr/   r0   idr   r   r   r9   
last_erroron_downshutdownr   r   r   r   r   r   lenorphaned_request_idsremoverv   )r    r   stream_was_orphanedis_downs       r   return_connectionz HostConnection.return_connection  s   " 	: * *$$)$$* * * * * * * * * * * * * * *1 : :077999: : : : : : : : : : : : : : :   "	J$8 "	( 1G G, 1		 457
^^TYP P P-/IIIz4u J N N,0
)% Qg Q%--di%-PPP D#' Z D D) D D D D D D D D *.D&M((
CCC	D D D D D D D D D D D D D D D D D D T[((_ 3 3!+s:3R/S/SSS!Z 3 3)T[88 $ 2 2: > > > #		*QSUV`SaSacgcl m m m * 0 0 2 2 2	3 3 3 3 3 3 3 3 3 3 3 3 3 3 33 3 3 3 3 3 3 3 3 3 3 3 3 3 3  )(sl   '++AA#&A#
	F 'FFF1%IA&I<II	II	II#&I#c                 x    | j         5  | j                                          ddd           dS # 1 swxY w Y   dS zt
        Called when a response for an orphaned stream (timed out on the client
        side) was received.
        N)r   r   r'   s    r   r   z*HostConnection.on_orphaned_stream_released  s    
 - 	6 	6,33555	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6   /33c                    | j         5  | j        r	 d d d            d S 	 d d d            n# 1 swxY w Y   t                              dt	          |          | j                   	 | j        j                            | j        j	        | j
                  }| j        r|                    | j                   || _        |j        5  | j         5  |j        rL|j        t#          |j                  k    r|                                 n| j                            |           d| _        | j                                         d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S # t2          $ rL t                              d| j        j	        d           | j                            | j        |           Y d S w xY w)NReplacing connection (%s) to %sr   FzFailed reconnecting z. Retrying.)r   r   r/   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   addr   r   r   rr   ru   r   r   )r    r   rw   s      r   r   zHostConnection._replace  s   Z 	 	 	 	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 			3R
^^TYOOO	>=(;;DI<Nlp  mM;  N  ND~ ;**4>:::#D
  > >Z > >!< 8%/3z7V3W3WWW&,,.... KOOJ777).D&4;;===> > > > > > > > > > > > > > >> > > > > > > > > > > > > > > > > >	  	< 	< 	<KKKty?Q?Q?QSTTTM  
;;;;;;	<sW   	+//%AE1 E$A4E E$E	E$E	E$$E(+E(1AGGc                    | j         5  | j        r	 d d d            d S d| _        | j                                         d d d            n# 1 swxY w Y   | j        r | j                                         d | _        d }| j         5  | j        r| j        }t                      | _        d d d            n# 1 swxY w Y   || j        D ]}|                                 d S d S r{   )r   r   r   
notify_allr   rv   r   r   )r    trash_connsrw   s      r   r   zHostConnection.shutdown  s   Z 	: 	: (	: 	: 	: 	: 	: 	: 	: 	: $( ,77999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:  	$""$$$#DZ 	$ 	${ $"k!ee	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$
 "  

 #" s(   	A
 A

AA"B33B7:B7c                 z      j         s j        sd S  fd}| _         j                            ||           d S )Nc                 X                         |            |sg n|g} |           d S r6   )r   )rw   errorerrorsrf   r    s      r   $connection_finished_setting_keyspacezXHostConnection._set_keyspace_for_all_conns.<locals>.connection_finished_setting_keyspace-  s?    ""4((($1RR5'FHT6"""""r   )r   r   r   set_keyspace_async)r    r   rf   r   s   ` ` r   _set_keyspace_for_all_connsz*HostConnection._set_keyspace_for_all_conns)  se     	4#3 	F	# 	# 	# 	# 	# 	#
 "++H6Z[[[[[r   c                     | j         }|r|gng S r6   )r   )r    cs     r   get_connectionszHostConnection.get_connections5  s    ssRr   c                     | j         }|r|j        s	|j        sdnd}|r|j        gng }|r|j        gng }| j        |||dS )Nr   r   r   
open_count
in_flightsorphan_requests)r   r   r   r   r   r   )r    r   r   r   r   s        r   	get_statezHostConnection.get_state9  sr    %
$cj.BcjF[cQQbc
/9Aj*++r
?IQ::;;r ,J$J J 	Jr   c                 8    | j         }|r|j        s	|j        sdndS )Nr   r   )r   r   r   r   s     r   r   zHostConnection.open_countA  s*    %
]
(<]
@U]qq\]]r   F)r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r   r   r   r   r   r   r   r`   r   r   r   r   r   r   r  s         DMKHKEIM M M6  M M M>) ) ) )V6 6 6> > >2  ,
\ 
\ 
\     J J J ^ ^ X^ ^ ^r   r   r   
   c                       e Zd ZdZdZdZdZdZdZdZ	dZ
d Zd Zd Zd Zd	 Zd
 Zd Zd Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )HostConnectionPoolzK
    Used to pool connections to a host for v1 and v2 native protocol.
    NFr   c                 p     _         | _        t          j                   _        t                       _        t                       _        t          
                    d j                    j                            |          } fdt          |          D              _        j         _         j        r$ j        D ]}|                     j                   t%                       _        t)          j                     _        | _        t          
                    d j                    d S )Nz,Initializing new connection pool for host %sc                 \    g | ](}j                             j        j                   )S )r   )r   r   r   r   ).0ir   r    r   s     r   
<listcomp>z/HostConnectionPool.__init__.<locals>.<listcomp>b  sN     9 9 9!" %_??ko  lL?  M  M 9 9 9r   z5Finished initializing new connection pool for host %s)r   r   r   r   r   r   r   r   _conn_available_conditionr/   r0   r   get_core_connections_per_hostrange_connectionsr   r   r   r   r   r   _next_trash_allowed_atr   )r    r   r   r   
core_connsrw   s   `` `  r   r$   zHostConnectionPool.__init__X  s#   	*g..WW
)2&		@$)LLL_BB=QQ
9 9 9 9 9 9&+J&7&79 9 9 !)> 	;) ; ;**4>::::ee&*ikk#$		I49UUUUUr   c                 &   | j         rt          d| j        d| j                  | j        }|st                              d| j                   | j        j                            | j	                  }| j
        5  |t          | j                  | j        z   z
  }t          |          D ]1}| xj        dz  c_        | j                            | j                   2	 d d d            n# 1 swxY w Y   |                     |          }|S | j        j                            | j	                  }| j        j                            | j	                  }t'          |d           }	d }
d}|	j        5  |	j        |	j        k     r%|	xj        dz  c_        |	                                }
nd}d d d            n# 1 swxY w Y   |r|                     |          \  }	}
|	j        |k    r,t          | j                  |k     r|                                  |	|
fS )	Nr   r   z-Detected empty pool, opening core conns to %sr   c                     | j         S r6   r   r   s    r   <lambda>z6HostConnectionPool.borrow_connection.<locals>.<lambda>  s    !+ r   keyFT)r   r	   r   r   r/   r0   r   r   r   r   r   r   _scheduled_for_creationr   r   _create_new_connection_wait_for_connget_max_requests_per_connectionget_max_connections_per_hostminr   r   r   r   _maybe_spawn_new_connection)r    r   connsr   	to_creater   rw   max_reqs	max_conns
least_busy
request_idneed_to_waits               r   r   z$HostConnectionPool.borrow_connectiono  s    	E%%-1YYY8$)E E E ! 0	*IIEtyQQQ.LLTM_``J F F '#d.?*@*@4C_*_`	y)) F FA00A500M(()DEEEEFF F F F F F F F F F F F F F F &&w//DK
 },LLTM_``H-JJ4K]^^IU(=(=>>>JJ
 !L ( ('**CCC((A-((!+!:!:!<!<JJ $(L( ( ( ( ( ( ( ( ( ( ( ( ( ( (  F)-)<)<W)E)E&
J
 #x//C8I4J4JY4V4V00222z))s%   :A!C((C,/C,/8F33F7:F7c                    | j         5  | j        t          k    r	 d d d            d S | j        | j        j                            | j                  k    r	 d d d            d S | xj        dz  c_        d d d            n# 1 swxY w Y   t          	                    d| j
                   | j                            | j                   d S )Nr   z4Submitting task for creation of new Connection to %s)r   r   _MAX_SIMULTANEOUS_CREATIONr   r   r   r  r   r/   r0   r   r   r  r'   s    r   r  z.HostConnectionPool._maybe_spawn_new_connection  s8   Z 	. 	.+/III	. 	. 	. 	. 	. 	. 	. 	. $-"7"T"TUYUg"h"hhh		. 	. 	. 	. 	. 	. 	. 	.
 ((A-((	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 			H$)TTTT899999s   A>.A>"A>>BBc                    	 |                                   ni# t          t          j        f$ r+}t                              d| j        |           Y d }~n-d }~wt          $ r t                              d           Y nw xY w| j	        5  | xj
        dz  c_
        d d d            d S # 1 swxY w Y   d S # | j	        5  | xj
        dz  c_
        d d d            w # 1 swxY w Y   w xY w)Nz)Failed to create new connection to %s: %sz,Unexpectedly failed to create new connectionr   )_add_conn_if_under_maxr	   socketr   r/   ru   r   rr   	exceptionr   r   )r    rx   s     r   r  z)HostConnectionPool._create_new_connection  s   	2''))))#V\2 	U 	U 	UKKCTYPSTTTTTTTT 	J 	J 	JMMHIIIII	J  2 2,,1,,2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2,,1,,2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2sm    B2 A=!AB2 'A=:B2 <A==B2 B%%B),B)2C#:CC#CC#CC#c                     | j         j                            | j                  }| j        5  | j        r	 d d d            dS | j        |k    r	 d d d            dS | xj        dz  c_        d d d            n# 1 swxY w Y   t                              d| j	                   	 | j         j        
                    | j	        j        | j                  }| j        r|                    | j         j                   t!          j                    t"          z   | _        | j        5  | j        d d          |gz   }|| _        d d d            n# 1 swxY w Y   t                              dt)          |          | j	                   |                                  dS # t,          t.          j        f$ r}t                              d| j	        |           | j        5  | xj        dz  c_        d d d            n# 1 swxY w Y   | j         j                            | j	        |d          r|                                  Y d }~dS d }~wt8          $ r3 | j        5  | xj        dz  c_        d d d            n# 1 swxY w Y   Y dS w xY w)	NTr   z'Going to open new connection to host %sr   zEAdded new connection (%s) to pool for host %s, signaling availabilityz4Failed to add new connection to pool for host %s: %sFr   )r   r   r  r   r   r   r   r/   r0   r   r   r   r   r   r   r   r   _MIN_TRASH_INTERVALr   r   r   _signal_available_connr	   r  r   ru   r9   r   r   )r    r
  rw   new_connectionsrx   s        r   r  z)HostConnectionPool._add_conn_if_under_max  s   M)FFtGYZZ	Z 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! )++	! 	! 	! 	! 	! 	! 	! 	! OOq OO	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 			;TYGGG	=(;;DI<Nlp  mM;  N  ND~ C**4=+ABBB*.)++8K*KD' 4 4"&"3AAA"6$"?$3!4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 II]hh	+ + +'')))4#V\2 	 	 	KKNPTPY[^___ % %1$% % % % % % % % % % % % % % %}$>>ty#`e>ff  55555# 	 	 	 % %1$% % % % % % % % % % % % % % %55	s   	A7A7A77A;>A;#A=F  E;F EF EAF I=,(H=G1%H=1G5	5H=8G5	9>H==I=I."I=.I2	2I=5I2	6I=<I=c                 z    | j         5  | j                             |           d d d            d S # 1 swxY w Y   d S r6   )r   r   )r    r   s     r   _await_available_connz(HostConnectionPool._await_available_conn  s    + 	9 	9*//888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   044c                 x    | j         5  | j                                          d d d            d S # 1 swxY w Y   d S r6   )r   r   r'   s    r   r  z)HostConnectionPool._signal_available_conn  s    + 	4 	4*11333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4r   c                 x    | j         5  | j                                          d d d            d S # 1 swxY w Y   d S r6   )r   r   r'   s    r   _signal_all_available_connz-HostConnectionPool._signal_all_available_conn  s    + 	8 	8*55777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8r   c                    t          j                     }|}|dk    r|                     |           | j        rt          d          | j        }|rtt          |d           }|j        5  |j        |j        k     r2|xj        dz  c_        ||	                                fcd d d            S 	 d d d            n# 1 swxY w Y   |t          j                     |z
  z
  }|dk    t                      )Nr   zPool is shutdownc                     | j         S r6   r   r   s    r   r   z3HostConnectionPool._wait_for_conn.<locals>.<lambda>  s    ak r   r   r   )r   r  r   r	   r   r  r   r   r   r   r   )r    r   rn   r   r  r  s         r   r  z!HostConnectionPool._wait_for_conn  s~   		!mm &&y111  >)*<===%E G ,A,ABBB
_ G G!+j.GGG",,1,,):+D+D+F+FFG G G G G G G GGG G G G G G G G G G G G G G G
  49;;#67I# !mm& %&&&s   )6B99B= B=c                    |j         5  |s|xj        dz  c_        |j        }d d d            n# 1 swxY w Y   |j        s|j        r|j        st
                              dt          |          | j                   | j	        j
                            | j        |j        d          }d|_        |r|                                  d S |                     |           d S d S || j        v r|j         5  |j        dk    r| j        5  || j        v r| j                            |           d d d            n# 1 swxY w Y   t
                              dt          |          | j                   |                                 d d d            n# 1 swxY w Y   d S | j	        j
                            | j                  }| j	        j
                            | j                  }t-          | j                  |k    r9||k    r3t1          j                    | j        k    r|                     |           d S |                                  d S )Nr   r   Fr   Tr   r   )r   r   r   r   r   r/   r0   r   r   r   r   r9   r   r   r   r   r   r   rv   r   r   get_min_requests_per_connectionr   r   r   r   _maybe_trash_connectionr  )r    r   r   r   r   r   min_reqss          r   r   z$HostConnectionPool.return_connection  s   _ 	- 	-& *$$)$$",I	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-
   	.J$8 	., 	.		 457
^^TYP P P-/IIIz4u J N N,0
) .MMOOOOOMM*-----	. 	. T[((_ + +!+q00!Z ? ?)T[88 $ 2 2: > > >? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 		"I2j>>[_[deee"((***+ + + + + + + + + + + + + + + .LLTM_``J},LLTM_``H 4$%%
22yH7L7LIKK4#>>>,,Z88888++-----sC   .220F$D3'F3D7	7F:D7	;AFFFc                 .    |                                   dS r   )r  r'   s    r   r   z.HostConnectionPool.on_orphaned_stream_released-  s    
 	##%%%%%r   c                 2   | j         j                            | j                  }d}| j        5  || j        vr	 d d d            d S | j        |k    rd}| xj        dz  c_        | j        d d          }|                    |           || _        |j        5  |j	        dk    r[t                              dt          |          | j                   |                                 	 d d d            d d d            d S 	 d d d            n# 1 swxY w Y   | j                            |           d d d            n# 1 swxY w Y   |rPt#          j                    t$          z   | _        t                              dt          |          | j                   d S d S )NFTr   r   z7Skipping trash and closing unused connection (%s) to %szTrashed connection (%s) to %s)r   r   r   r   r   r   r   r   r   r   r/   r0   r   r   rv   r   r   r   r  r   )r    r   r   	did_trashr  s        r   r!  z*HostConnectionPool._maybe_trash_connection4  sa   ]*HHI[\\
	Z 	, 	,!222	, 	, 	, 	, 	, 	, 	, 	, ++ 	1$"&"3AAA"6&&z222$3!_  !+q00		"[]_`j]k]kmqmvwww"((***      	, 	, 	, 	, 	, 	, 	, 	, 1               
+++'	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,*  	R*.)++8K*KD'II5r*~~tyQQQQQ	R 	RsC   D:AD:AD	$D:=D:	D	D:D	D::D>D>c                 "   d}| j         5  || j        v r=| j        d d          }|                    |           || _        | xj        dz  c_        d}d d d            n# 1 swxY w Y   |rct                              dt          |          | j                   |                                 | j	        
                    | j                   d S t                              dt          |          | j                   |                                 d S )NFr   Tr   zClosing connection (%s) to %s)r   r   r   r   r/   r0   r   r   rv   r   r   _retrying_replace)r    r   should_replacer  s       r   r   zHostConnectionPool._replaceP  sE   Z 	& 	&T..."&"3AAA"6&&z222$3!1$!%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&  	II7JSSSM  !788888II5r*~~tyQQQs   AAA!$A!c                    d}	 |                                  }n0# t          $ r# t                              d| j                   Y nw xY w|sAt                              d| j                   | j                            | j                   d S d S )NFz!Failed replacing connection to %sz,Failed replacing connection to %s. Retrying.)	r  rr   r/   r  r   r0   r   r   r'  )r    replaceds     r   r'  z$HostConnectionPool._retrying_replaceb  s    	J2244HH 	J 	J 	JMM=tyIIIII	J 	9IIDdiPPPM  !788888	9 	9s    *AAc                 >   | j         5  | j        r	 d d d            d S d| _        	 d d d            n# 1 swxY w Y   |                                  | j        D ]&}|                                 | xj        dz  c_        '| j        D ]}|                                 d S )NTr   )r   r   r  r   rv   r   r   r   s     r   r   zHostConnectionPool.shutdownl  s   Z 	( 	( (	( 	( 	( 	( 	( 	( 	( 	( $(  		( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	'')))% 	! 	!DJJLLLOOq OOOK 	 	DJJLLLL	 	s   	2266c                 b   | j         rd S | j        j                            | j                  }| j        5  |t          | j                  | j        z   z
  }t          |          D ]1}| xj        dz  c_        | j        
                    | j                   2	 d d d            d S # 1 swxY w Y   d S )Nr   )r   r   r   r   r   r   r   r   r   r   r   r  )r    r   r  r   s       r   ensure_core_connectionsz*HostConnectionPool.ensure_core_connections{  s    	F]*HHI[\\
Z 	B 	B"c$*;&<&<t?[&[\I9%% B B,,1,,$$T%@AAAAB	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	Bs   A!B$$B(+B(c                      t           j                  g s             dS  fd}| _         j        D ]}|                    ||           dS )z
        Asynchronously sets the keyspace for all connections.  When all
        connections have been set, `callback` will be called with two
        arguments: this pool, and a list of any errors that occurred.
        Nc                                          |                                |            |r                    |           s            d S d S r6   )r   r   append)rw   r   rf   r   remaining_callbacksr    s     r   r   z\HostConnectionPool._set_keyspace_for_all_conns.<locals>.connection_finished_setting_keyspace  so    ""4(((&&t,,, %e$$$& 'v&&&&&' 'r   )r   r   r   r   )r    r   rf   r   rw   r   r1  s   ` `  @@r   r   z.HostConnectionPool._set_keyspace_for_all_conns  s     "$"344" 	HT6"""F	' 	' 	' 	' 	' 	' 	' 	' "% 	T 	TD##H.RSSSS	T 	Tr   c                     | j         S r6   )r   r'   s    r   r   z"HostConnectionPool.get_connections  s      r   c                 h    d | j         D             }d | j         D             }| j        | j        ||dS )Nc                     g | ]	}|j         
S r   r   r   r   s     r   r   z0HostConnectionPool.get_state.<locals>.<listcomp>  s    ===aak===r   c                     g | ]	}|j         
S r   )r   r5  s     r   r   z0HostConnectionPool.get_state.<locals>.<listcomp>  s    MMMa11MMMr   r   )r   r   r   )r    r   r   s      r   r   zHostConnectionPool.get_state  sO    ==4+<===
MM4;LMMM ,DO$J J 	Jr   r   )r   r   r   r   r   r   r   r   r   r   r   r$   r   r  r  r  r  r  r  r  r   r   r!  r   r'  r   r-  r   r   r   r   r   r   r   r   J  s{         DMKJIV V V.6* 6* 6*p	: 	: 	:	2 	2 	2" " "H9 9 94 4 48 8 8' ' '2%. %. %. %.N& & &R R R8  $9 9 9  	B 	B 	BT T T4! ! !J J J J Jr   r   )#r   	functoolsr   loggingr  r   	threadingr   r   r   r   r   ImportErrorcassandra.util	cassandrar   cassandra.connectionr	   r
   r   cassandra.policiesr   	getLoggerr   r/   rr   r   objectr   rb   r   r   r  r  r   r   r   r   <module>rA     sh  "  % $ $ $ $ $    , , , , , , , , , , ' ' ' '&&&&&&&&' + * * * * * O O O O O O O O O O + + + + + +g!!	 	 	 	 	Y 	 	 	 HK HK HK HK HK6 HK HK HKVV V V V V6 V V Vr    3   <R^ R^ R^ R^ R^V R^ R^ R^h   ]J ]J ]J ]J ]J ]J ]J ]J ]J ]Js   + 99