
    i!3                     d   d dl Z d dlmZ d dlmZ d dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlZd dlmZ 	 d dlmc mZ n# e$ r  ed          w xY wd dlmZmZmZmZmZ  ej        e          Zd Z G d	 d
e          Zda  e j!         eet@                                G d de          Z"dS )    N)deque)partial)LockThread)DependencyExceptionaN  The C extension needed to use libev was not found.  This probably means that you didn't have the required build dependencies when installing the driver.  See https://docs.datastax.com/en/developer/python-driver/latest/installation/index.html#c-extensions for instructions on installing build dependencies and building the C extension.)
ConnectionConnectionShutdownNONBLOCKINGTimerTimerManagerc                 6    | r|                                   d S d S N)_cleanup)loops    k/Users/user/workspace/sujinbaek/cqa-test-app/venv/lib/python3.11/site-packages/cassandra/io/libevreactor.pyr   r   -   s%          c                   P    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S )	LibevLoopc                 .   t          j                    | _        t          j                    | _        t          j        | j                  | _        | j                                         | j        	                                 d| _
        d| _        t                      | _        t                      | _        d | _        t!                      | _        t!                      | _        t!                      | _        t                      | _        t          j        | j        | j                  | _        | j        	                                 | j                                         t1                      | _        t          j        | j        | j                  | _        d S )NF)osgetpid_pidlibevLoop_loopAsync	_notifierstartunref_started	_shutdownr   _lock_lock_thread_threadset_live_conns
_new_conns_closed_conns_conn_set_lockPrepare_loop_will_run	_preparerr   _timersr   _on_loop_timer_loop_timerselfs    r   __init__zLibevLoop.__init__4   s    IKK	Z\\
TZ00 	
VV
 FF 55%% UU"fftz43FGG
#~~ ;tz43FGGr   c                    d}| j         5  | j        s#t                              d           d| _        d}d d d            n# 1 swxY w Y   |rf| j        5  | j        s@t          | j        d          | _        d| j        _	        | j        
                                 d d d            n# 1 swxY w Y   | j                                         d S )NFzStarting libev event loopT
event_loop)targetname)r"   r    logdebugr#   r!   r   	_run_loopr$   daemonr   r   send)r1   should_starts     r   maybe_startzLibevLoop.maybe_startU   sO   Z 	$ 	$= $		5666 $#		$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$  	)" ) )~ )#)l#S#S#SDL*.DL'L&&(((	) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 	s$   +AAAAB))B-0B-c                 &   	 | j                                          | j        5  | j        s.| j        r't
                              d           	 d d d            Wt
                              d           d| _        	 d d d            d S # 1 swxY w Y   )NTzRestarting event loopz2All Connections currently closed, event loop endedF)r   r   r"   r!   r&   r7   r8   r    r0   s    r   r9   zLibevLoop._run_loopf   s    	J  ~ $*: II5666       IIRSSS$)DM               	s   *B"BBBc                    d| _         | j        sd S | j        | j        z  | j        z  D ]=}|                                 |j        |j        fD ]}|r|                                 >| 	                                 | j
        5  | j                            d           d d d            n# 1 swxY w Y   | j                                        rt                              d           t                              d           d S )NTg      ?)timeoutzrEvent loop thread could not be joined, so shutdown may not be clean. Please call Cluster.shutdown() to avoid this.zEvent loop thread was joined)r!   r$   r&   r'   r(   close_write_watcher_read_watcherstopnotifyr#   joinis_aliver7   warningr8   )r1   connwatchers      r   r   zLibevLoop._cleanupt   sV   | 	F$t69KK 	# 	#DJJLLL /1CD # # #LLNNN# 	  	+ 	+Lc***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ <  "" 	AKK@A A A 			011111s   B))B-0B-c                 l    | j                             |           | j                                         d S r   )r-   	add_timerr   r;   )r1   timers     r   rL   zLibevLoop.add_timer   s2    u%%%r   c                     | j         sM| j                                        }|r0| j                            |t          j                    z
             d S d S | j                                         d S r   )r!   r-   service_timeoutsr/   r   timerD   )r1   next_ends     r   _update_timerzLibevLoop._update_timer   sv    ~ 	$|4466H ? &&x$)++'=>>>>>? ? !!#####r   c                 8    | j                                          d S r   )r-   rO   r0   s    r   r.   zLibevLoop._on_loop_timer   s    %%'''''r   c                 8    | j                                          d S r   )r   r;   r0   s    r   rE   zLibevLoop.notify   s    r   c                    | j         5  | j                                        }|                    |           || _        | j                                        }|                    |           || _        d d d            d S # 1 swxY w Y   d S r   )r)   r&   copyaddr'   )r1   rI   new_live_connsnew_new_connss       r   connection_createdzLibevLoop.connection_created   s      	, 	,!-2244Nt$$$-D O0022Md###+DO	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   A+B  BBc                 J   | j         5  | j                                        }|                    |           || _        | j                                        }|                    |           || _        d d d            n# 1 swxY w Y   | j                                         d S r   )r)   r&   rV   discardr(   rW   r   r;   )r1   rI   rX   new_closed_connss       r   connection_destroyedzLibevLoop.connection_destroyed   s      	2 	2!-2244N""4(((-D#16688  &&&!1D	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	s   A+A??BBc                 &   d}| j         D ]j}|j        s1|j        r*|j        r|j                                         d|_        d}:|j        r)|j        s"|j                                         d|_        d}k| j        rY| j        5  | j        }t                      | _        d d d            n# 1 swxY w Y   |D ]}|j	                                         d}| j
        r| j        5  | j
        }t                      | _
        d d d            n# 1 swxY w Y   |D ]F}|j        r|j                                         |`|j	        r|j	                                         |`	Gd}|                                  |r| j                                         d S d S )NFT)r&   r   _write_watcher_is_activerB   rD   r   r'   r)   r%   rC   r(   rR   r   r;   )r1   preparechangedrI   to_startto_stops         r   r+   zLibevLoop._loop_will_run   s[   $ 		 		D: $"? & /',,...05- D$A #))+++04-? 	$ ( (?"%%%( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ! + +"((****G 	$ + +,%(UU"+ + + + + + + + + + + + + + +   + +& ,',,...+% +&++---*G 	 	"N!!!!!	" 	"s$   B**B.1B.#D

DDN)__name__
__module____qualname__r2   r=   r9   r   rL   rR   r.   rE   rZ   r^   r+    r   r   r   r   2   s        H H HB  "  2 2 20  $ $ $( ( (  , , ,
 
 
," ," ," ," ,"r   r   c                       e Zd ZdZdZdZdZdZed             Z	ed             Z
ed             Zd Zd Zdd	Zdd
Zd ZdS )LibevConnectionzW
    An implementation of :class:`.Connection` that uses libev for its event loop.
    FNc                     t           st                      a d S t           j        t          j                    k    r>t
                              d           |                                  t                      a d S d S )Nz8Detected fork, clearing and reinitializing reactor state)_global_loopr   r   r   r   r7   r8   handle_forkclss    r   initialize_reactorz"LibevConnection.initialize_reactor   se      	+$;;LLL BIKK//		TUUU!!!({{ 0/r   c                 N    t           rt                                            d a d S d S r   )rl   r   rn   s    r   rm   zLibevConnection.handle_fork   s1      	 !!###LLL	  	 r   c                 Z    t          ||          }t                              |           |S r   )r   rl   rL   )ro   r@   callbackrM   s       r   create_timerzLibevConnection.create_timer  s*    gx((u%%%r   c                    t          j        | g|R i | t                      | _        t                      | _        |                                  | j                            d           t          j	        5  t          j        | j                                        t          j        t          j        | j                  | _        t          j        | j                                        t          j        t          j        | j                  | _        d d d            n# 1 swxY w Y   |                                  t                              |            t                                           d S Nr   )r   r2   r   r   _deque_lock_connect_socket_socketsetblockingrl   r"   r   IOfilenoEV_READr   handle_readrC   EV_WRITEhandle_writerB   _send_options_messagerZ   r=   )r1   argskwargss      r   r2   zLibevConnection.__init__  ss   D242226222WW
66  ### 	y 	y!&$,*=*=*?*?P\Pbdhdt!u!uD"'(4<+>+>+@+@%.R^Rdfjfw"x"xD	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	y 	""$$$''--- 	  """""s   6BDD"Dc                    | j         5  | j        r	 d d d            d S d| _        d d d            n# 1 swxY w Y   t                              dt	          |           | j                   t                              |            | j        	                                 t                              d| j                   | j
        s,|                     t          d| j        z                       d S d S )NTzClosing connection (%s) to %szClosed socket to %szConnection to %s was closed)lock	is_closedr7   r8   idendpointrl   r^   ry   rA   
is_defuncterror_all_requestsr	   r0   s    r   rA   zLibevConnection.close  sP   Y 	" 	"~ 	" 	" 	" 	" 	" 	" 	" 	" "DN	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"
 			12d88T]KKK))$///		'777  	S##"#@4=#PQQS S S S S	S 	Ss   	1155c                    |t           j        z  rK|r#t          |t          j        |                    }nt          d          }|                     |           d S 	 	 | j        5  | j        	                                }d d d            n# 1 swxY w Y   n# t          $ r | j        sd| _        Y d S w xY w	 | j                            |          }|t          |          k     rP| j        5  | j                            ||d                     d d d            n# 1 swxY w Y   |dk    r	d| _        d S n# t           j        $ r}|j        d         t&          v s%|j        d         t(          j        t(          j        fv rU|j        d         t&          v rd| _        | j        5  | j                            |           d d d            n# 1 swxY w Y   n|                     |           Y d }~d S d }~ww xY w)Nlibev reported an errorTr   F)r   EV_ERRORIOErrorr   strerror	Exceptiondefunctrw   r   popleft
IndexError_socket_writablery   r;   len
appendleftsocketerrorr   r
   sslSSL_ERROR_WANT_READSSL_ERROR_WANT_WRITE)r1   rJ   reventserrnoexcnext_msgsenterrs           r   r   zLibevConnection.handle_write,  s   U^# 	 ;eR[%7%788 9::LLF	% 4 4#z1133H4 4 4 4 4 4 4 4 4 4 4 4 4 4 4   , 1,0D)
|((22 #h--'') ? ?
--htuuo>>>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? qyy05-% < 	 	 	HQK;..(?AY'ZZZx{k1105-) 8 8
--h7778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 LL%%%		s   B $B
>B 
BB BB B21B26D4 *#DD D4G1AG,G9G,G			G,G		G,,G1c                    |t           j        z  rK|r#t          |t          j        |                    }nt          d          }|                     |           d S 	 	 | j                            | j	                  }| j
                            |           t          |          | j	        k     rnSn# t          j        $ r}t          |t           j                  r`|j        d         t           j        t           j        fv r | j
                                        sY d }~d S nj|                     |           Y d }~d S |j        d         t,          v r | j
                                        sY d }~d S n|                     |           Y d }~d S Y d }~nd }~ww xY w| j
                                        r|                                  d S t0                              d|            |                                  d S )Nr   Tr   zConnection %s closed by server)r   r   r   r   r   r   r   ry   recvin_buffer_size_iobufwriter   r   r   
isinstancer   SSLErrorr   r   r   tellr
   process_io_bufferr7   r8   rA   )r1   rJ   r   r   r   bufr   s          r   r~   zLibevConnection.handle_readU  s   U^# 	 ;eR[%7%788 9::LLF	l''(;<<!!#&&&s88d111	 | 	 	 	#s|,, 8A;3#:C<T"UUU;++--  LL%%%FFFFF!++{'')) FFFFF S!!!	 ; 	""$$$$$II6===JJLLLLLs,   AB1 1F AFF:-F-FFc                 n   | j         }t          |          |k    rDg }t          dt          |          |          D ]"}|                    ||||z                       #n|g}| j        5  | j                            |           t                                           d d d            d S # 1 swxY w Y   d S rv   )	out_buffer_sizer   rangeappendrw   r   extendrl   rE   )r1   datasabschunksis        r   pushzLibevConnection.pushy  s   #t99tF1c$ii.. 0 0d1QX:.////0 VF 	" 	"Jf%%%!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   )4B**B.1B.r   )re   rf   rg   __doc__r`   rC   rB   ry   classmethodrp   rm   rt   r2   rA   r   r~   r   rh   r   r   rj   rj      s           %MNG+ + [+     [    [
# # #&S S S"' ' ' 'R" " " "H" " " " "r   rj   )#atexitcollectionsr   	functoolsr   loggingr   r   r   	threadingr   r   rP   	cassandrar   cassandra.io.libevwrapperiolibevwrapperr   ImportErrorcassandra.connectionr   r	   r
   r   r   	getLoggerre   r7   r   objectr   rl   registerrj   rh   r   r   <module>r      s                  				  



 " " " " " " " "  ) ) ) ) ) )	----------   

	  D D D D D D D D D D D D D D g!!  
n" n" n" n" n" n" n" n"b  ,// 0 0 0]" ]" ]" ]" ]"j ]" ]" ]" ]" ]"s	   	> A