
    &gP                       S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKJ	r	J
r
Jr  SSKJrJr  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  SSK J!r!  SSK"J#r#  SSK$J%r%  \	(       a  SSK&J'r'J(r(J)r)  SSK*J+r+  SSK,J-r-  Sr.S"S jr/S#S jr0 " S S5      r1 " S S\15      r2 " S S\15      r3 " S S\15      r4\5" 5       r6S$S jr7S%S jr8S&S  jr9S&S! jr:\.(       a  \Rv                  " \:5        gg)'z9Class to monitor a MongoDB server on a background thread.    )annotationsN)TYPE_CHECKINGAnyOptional)commonperiodic_executor)MovingMinimum)_SrvResolver)NetworkTimeout_OperationCancelled)Hello)_async_create_lock)_SDAM_LOGGER
_debug_log_SDAMStatusMessage)_shutdown_executors)_is_faas)MovingAverage)ServerDescription)AsyncConnectionPool_CancellationContext)TopologySettings)TopologyFc                .    SU l         SU l        SU l        g)z'PYTHON-2433 Clear error traceback info.N)__traceback____context__	__cause__errors    aC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/asynchronous/monitor.py	_sanitizer"   2   s    EEEO    c                F    [        S[        R                  " 5       U -
  5      $ )zReturn the duration since the given start time.

Accounts for buggy platforms where time.monotonic() is not monotonic.
See PYTHON-4600.
g        )maxtime	monotonic)starts    r!   _monotonic_durationr)   9   s     sDNN$u,--r#   c                  P    \ rS rSrS
S jrSS jrSS jrSS jrSS jrSS jr	Sr
g	)MonitorBaseB   c                   ^ SU4S jjn[         R                  " X4XRS9nX`l        SSU4S jjjn[        R                  " XR
                  5      m[        R                  " X5      U l        [        U 5        g)zBase class to do periodic work on a background thread.

The background thread is signaled to stop when the Topology or
this instance is freed.
c                 X   >#    T" 5       n U c  gU R                  5       I S h  vN   g N7f)NFT)_run)monitorself_refs    r!   target$MonitorBase.__init__.<locals>.targetL   s,     jG,,.   !s   *(*)intervalmin_intervalr2   nameNc                D   > T" 5       nU(       a  UR                  5         g g Ngc_safe_close)dummyr0   r1   s     r!   _on_topology_gc-MonitorBase.__init__.<locals>._on_topology_gcY   s      jG%%' r#   )returnboolr8   )r;   zOptional[Topology]r>   None)	r   AsyncPeriodicExecutor	_executorweakrefrefcloseproxy	_topology	_register)	selftopologyr6   r4   r5   r2   executorr<   r1   s	           @r!   __init__MonitorBase.__init__C   s[    	 %::
 "	( 	( ;;t^^4 xA$r#   c                8    U R                   R                  5         g)zKStart monitoring, or restart after a fork.

Multiple calls have no effect.
N)rB   openrI   s    r!   rO   MonitorBase.opene   s    
 	r#   c                8    U R                   R                  5         g)zGC safe close.N)rB   rE   rP   s    r!   r:   MonitorBase.gc_safe_closel   s    r#   c                ,   #    U R                  5         g7f)zGClose and stop monitoring.

open() restarts the monitor after closing.
Nr9   rP   s    r!   rE   MonitorBase.closep   s     
 	s   c                T   #    U R                   R                  5       I Sh  vN   g N7f)zWait for the monitor to stop.N)rB   joinrP   s    r!   rW   MonitorBase.joinw   s     nn!!###   (&(c                8    U R                   R                  5         g)z)If the monitor is sleeping, wake it soon.N)rB   wakerP   s    r!   request_checkMonitorBase.request_check{   s    r#   )rB   rG   N)rJ   r   r6   strr4   intr5   floatr>   r@   )__name__
__module____qualname____firstlineno__rL   rO   r:   rE   rW   r\   __static_attributes__ r#   r!   r+   r+   B   s!     D$r#   r+   c                     ^  \ rS rSr        SU 4S jjrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSrU =r$ )Monitor   c                "  > [         TU ]  USUR                  [        R                  5        Xl        X0l        X@l        U R                  R                  R                  U l
        U R                  SL=(       a    U R                  R                  U l        SU l        SU l        [        UUUR!                  UR"                  5      5      U l        UR&                  S:X  a  SU l        gUR&                  S:X  a  SU l        g[+        5       (       + U l        g)zClass to monitor a MongoDB server on a background thread.

Pass an initial ServerDescription, a Topology, a Pool, and
TopologySettings.

The Topology is weakly referenced. The Pool must be exclusive to this
Monitor.
pymongo_server_monitor_taskNstreamTpollF)superrL   heartbeat_frequencyr   MIN_HEARTBEAT_INTERVAL_server_description_pool	_settings_pool_options_event_listeners
_listenersenabled_for_server_heartbeat_publish_cancel_context_conn_id_RttMonitor_create_pool_for_monitoraddress_rtt_monitorserver_monitoring_mode_streamr   )rI   server_descriptionrJ   pooltopology_settings	__class__s        r!   rL   Monitor.__init__   s     	)11))		
 $6 
*..66GGt3d8d8d?C'+'--.@.H.HI

 33x?DL55? DL'z>DLr#   c                L    U R                   nU(       a  UR                  5         gg)ztCancel any concurrent hello check.

Note: this is called from a weakref.proxy callback and MUST NOT take
any locks.
N)rz   cancel)rI   contexts     r!   cancel_checkMonitor.cancel_check   s#     && NN	 r#   c                   #    U R                   R                  5         U R                  R                  (       a#  U R                   R	                  5       I Sh  vN   gg N7f)z1Start an _RttMonitor that periodically runs ping.N)r   rO   rB   _stoppedrE   rP   s    r!   _start_rtt_monitorMonitor._start_rtt_monitor   sF     
 	 >>""##))+++ #+s   AAAAc                    U R                   R                  5         U R                  R                  5         U R	                  5         g r8   )rB   rE   r   r:   r   rP   s    r!   r:   Monitor.gc_safe_close   s0    '')r#   c                   #    [         R                  " U R                  R                  5       U R                  R                  5       SS9I S h  vN   g  N7f)NT)return_exceptions)asynciogatherrB   rW   r   rP   s    r!   rW   Monitor.join   s>     nnNN!4#4#4#9#9#;t
 	
 	
s   A
AAAc                   #    U R                  5         U R                  R                  5       I S h  vN   U R                  5       I S h  vN   g  N N7fr8   )r:   r   rE   _reset_connectionrP   s    r!   rE   Monitor.close   sG     %%''' $$&&& 	( 	's!   .AAAA	AAc                T   #    U R                   R                  5       I S h  vN   g  N7fr8   )rs   resetrP   s    r!   r   Monitor._reset_connection   s     jj   rY   c                r  #     U R                   n U R                  5       I S h  vN U l         U R                  R                  U R                   U R                   R                  [        U R                   R                  [         5      S9I S h  vN   U R"                  (       ah  U R                   R                  (       aM  U R                   R$                  (       a2  U R'                  5       I S h  vN   U R                  R                  5         U R                   R                  (       a+  UR                  (       a  U R                  R                  5         U R                  R                  (       a#  U R                  R                  5       I S h  vN   g g  GNk! [         a  n[        U5        [	        U R                   R
                  US9U l         UR                  (       a  U R                  R                  5          S nAU R                  R                  (       a$  U R                  R                  5       I S h  vN    g g S nAff = f GN GNY! [(         a    U R                  5       I S h  vN     GNf = f N! U R                  R                  (       a$  U R                  R                  5       I S h  vN    f f = f7f)Nr   )
reset_poolinterrupt_connections)rr   _check_serverr   r"   r   r~   is_server_type_knownrB   
skip_sleepr   r   rE   rG   	on_changer    
isinstancer   r   topology_versionr   ReferenceError)rI   prev_sdexcs      r!   r/   Monitor._run   s    '	0..G151C1C1E+E( ..**((3399&01I1I1O1OQ_&` +    ||((==,,==--///))+''--'2N2N))+
 ~~&&''--/// 'G ,F& 
#+<,,44C,( // NN--/0 ~~&&''--/// 'E
 0  	**,	
 0 ~~&&''--/// 's   J7I F F	F AI IAI +I,A$I 8J7I1	J7F 
H>AH95I 98J71H42J79H>>I I I.$I'%I.*I3 -I..I3 1J739J4,J/-J44J7c                  #    SU l         [        R                  " 5       n U R                  5       I Sh  vN $  N! [         a    e [
         Ga}  n[        U5        U R                  nUR                  n[        U5      n[        U R                  =(       a    UR                  =(       a    UR                  5      nU R                  (       a+  U R                  c   eU R                  R!                  XEX&5        ["        R$                  " [&        R(                  5      (       aJ  [+        ["        [,        R.                  U R0                  R2                  US   US   UUS-  UU R                   S9	  U R5                  5       I Sh  vN    [7        U[8        5      (       a  e U R:                  R=                  5       I Sh  vN    [?        XBS9s SnA$ SnAff = f7f)zNCall hello or read the next streaming response.

Returns a ServerDescription.
Nr        )message
topologyId
serverHost
serverPortawaited
durationMSfailuredriverConnectionIdr   ) r{   r&   r'   _check_oncer   	Exceptionr"   rr   r~   r)   r?   r   r   r   ry   rw   publish_server_heartbeat_failedr   isEnabledForloggingDEBUGr   r   HEARTBEAT_FAILrG   _topology_idr   r   r   r   r   r   )rI   r(   r    sdr~   durationr   s          r!   r   Monitor._check_server   sd    
  	;))++++ 	 	;e))BjjG*51H4<<[B,C,C[H[H[\G}}222??SXb((77 .==#~~::&qz&qz#'$!'+}}
 ((***%!455##))+++$W::5	;sV   G: 8: G: GD+G:E=;8G3F64GGGGGc                  #    U R                   R                  nU R                   n[        U R                  R                  =(       a2    U R
                  =(       a    UR                  =(       a    UR                  5      nU R                  (       a*  U R                  c   eU R                  R                  X5        U R                  (       a3  U R                  R                  (       a  U R                  5       I Sh  vN   U R                  R                  5        ISh  vN n[        R                   " ["        R$                  5      (       aP  ['        [        [(        R*                  U R,                  R.                  UR0                  UR2                  US   US   US9  UR4                  U l        UR0                  U l        U R9                  U5      I Sh  vN u  pVUR:                  (       d#  U R<                  R?                  U5      I Sh  vN   U R<                  RA                  5       I Sh  vN u  px[C        XXxS9nU R                  (       a5  U R                  c   eU R                  RE                  XXUR:                  5        [        R                   " ["        R$                  5      (       a_  ['        [        [(        RF                  U R,                  R.                  UR0                  UR2                  US   US   UUS-  URH                  S9
  UsSSS5      ISh  vN   $  GN! GN GNP GN N N! , ISh  vN  (       d  f       g= f7f)zVA single attempt to call hello.

Returns a ServerDescription, or raises an exception.
Nr   r   )r   r   r   serverConnectionIdr   r   r   )min_round_trip_timer   )	r   r   r   r   r   r   r   r   reply)%rr   r~   r?   rs   connsr   r   r   ry   rw    publish_server_heartbeat_startedrz   	cancelledr   checkoutr   r   r   r   r   r   HEARTBEAT_STARTrG   r   idserver_connection_idcancel_contextr{   _check_with_socket	awaitabler   
add_samplegetr   "publish_server_heartbeat_succeededHEARTBEAT_SUCCESSdocument)	rI   r~   r   r   connresponseround_trip_timeavg_rttmin_rtts	            r!   r   Monitor._check_once%  sf    
 **22%% JJaa"2I2IabNaNa
 ==??...OO<<WND$8$8$B$B((***::&&((D((77 .>>#~~::'+ww'+'@'@&qz&qz#	 $(#6#6D  GGDM.2.E.Ed.K(K%H%%''22?CCC%)%6%6%:%:%<<G"7g[B}}222BBh8J8J ((77 .@@#~~::'+ww'+'@'@&qz&qz#.5"++ Q )(( +(  )LC<) )(((s   C5M7L8"MLMB0L(L5L(L!!L(&L$'CL(ML&MML(!L($L(&M(L?.L1/L?;Mc                  #    [         R                  " 5       nUR                  (       a!  [        UR	                  5       I Sh  vN SS9nOU R
                  (       ao  UR                  (       a^  U R                  R                  (       aC  UR                  U R                  R                  U R                  R                  5      I Sh  vN nOUR                  SS5      I Sh  vN n[        U5      nX44$  N N/ N7f)zSReturn (Hello, round_trip_time).

Can raise ConnectionFailure or OperationFailure.
NT)r   )r&   r'   more_to_comer   _next_replyr   performed_handshakerr   r   _hellort   rp   r)   )rI   r   r(   r   r   s        r!   r   Monitor._check_with_socketb  s     
  4#3#3#55FHLLT55$:R:R:c:c "[[((9922 H "[[t44H&u-!! 6
 5s6   ?C8C2BC8C4C8C6 C84C86C8)	rz   r{   rw   rs   ry   r   rr   rt   r   )r   r   rJ   r   r   r   r   r   ra   )r>   r   )r   r   r>   ztuple[Hello, float])rb   rc   rd   re   rL   r   r   r:   rW   rE   r   r/   r   r   r   rf   __classcell__r   s   @r!   ri   ri      si    &*-&* &* 	&*
 ,&*P,


'!(0T%;N;z" "r#   ri   c                  @   ^  \ rS rSrSU 4S jjrSS jrSS jrSrU =r$ )	
SrvMonitoriz  c                \  > [         TU ]  US[        R                  UR                  5        X l        U R
                  R                  U l        [        U R
                  R                  [        5      (       d   eU R
                  R                  U l        [        R                  " 5       U l        g)zClass to poll SRV records on a background thread.

Pass a Topology and a TopologySettings.

The Topology is weakly referenced.
pymongo_srv_polling_threadN)ro   rL   r   MIN_SRV_RESCAN_INTERVALrp   rt   _seeds	_seedlistr   fqdnr^   _fqdnr&   r'   _startup_time)rI   rJ   r   r   s      r!   rL   SrvMonitor.__init__{  s     	(**11		
 +..$..--s3333..--
!^^-r#   c                x  #    [         R                  " 5       U R                  [        R                  -   :  a  g U R                  5       I S h  vN nU(       a5  Xl         U R                  R                  U R                  5      I S h  vN   g g  NA N! [         a    U R                  5       I S h  vN     g f = f7fr8   )r&   r'   r   r   r   _get_seedlistr   rG   on_srv_updater   rE   )rI   seedlists     r!   r/   SrvMonitor._run  s     >>d0063Q3QQQ++--%N#nn224>>BBB  . C! #jjl""#sT   A	B:BB:(B BB B:B B7.B1/B74B:6B77B:c                  #     [        U R                  U R                  R                  R                  U R                  R
                  5      nUR                  5       I Sh  vN u  p#[        U5      S:X  a  [        e U R                  R                  [        U[        R                  5      5        U$  NQ! [         a    U R                  5          gf = f7f)zHPoll SRV records for a seedlist.

Returns a list of ServerDescriptions.
Nr   )r
   r   rt   pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenr   rB   update_intervalr%   r   r   r\   )rI   resolverr   ttls       r!   r   SrvMonitor._get_seedlist  s     
	#

++;;//H
 #+"@"@"BBMH8}! " NN**3sF4R4R+STO C  	
  	s;   CAB4  B2!B4 =5C2B4 4CCCC)r   r   rt   r   )rJ   r   r   r   ra   )r>   zOptional[list[tuple[str, Any]]])	rb   rc   rd   re   rL   r/   r   rf   r   r   s   @r!   r   r   z  s    .&# r#   r   c                  h   ^  \ rS rSrS
U 4S jjrSS jrSS jrSS jrSS jrSS jr	SS jr
S	rU =r$ )r|   i  c                   > [         TU ]  USUR                  [        R                  5        X0l        [        5       U l        [        5       U l	        [        5       U l        g)zLMaintain round trip times for a server.

The Topology is weakly referenced.
pymongo_server_rtt_taskN)ro   rL   rp   r   rq   rs   r   _moving_averager	   _moving_minr   _lock)rI   rJ   r   r   r   s       r!   rL   _RttMonitor.__init__  sO    
 	%11))		
 
,(?')
r#   c                t   #    U R                  5         U R                  R                  5       I S h  vN   g  N7fr8   )r:   rs   r   rP   s    r!   rE   _RttMonitor.close  s)      jj   s   .868c                   #    U R                    ISh  vN   U R                  R                  U5        U R                  R                  U5        SSS5      ISh  vN   g NK N! , ISh  vN  (       d  f       g= f7f)zAdd a RTT sample.N)r   r   r   r   )rI   samples     r!   r   _RttMonitor.add_sample  sE     :::  ++F3''/ :::::sD   A=AA=7A#A=A!A=!A=#A:)A,*A:6A=c                   #    U R                    ISh  vN   U R                  R                  5       U R                  R                  5       4sSSS5      ISh  vN   $  NI N! , ISh  vN  (       d  f       g= f7f)zBGet the calculated average, or None if no samples yet and the min.N)r   r   r   r   rP   s    r!   r   _RttMonitor.get  sC     :::''++-t/?/?/C/C/EE :::::sD   A;AA;4A!A;AA;A;!A8'A*(A84A;c                   #    U R                    ISh  vN   U R                  R                  5         U R                  R                  5         SSS5      ISh  vN   g NI N! , ISh  vN  (       d  f       g= f7f)zReset the average RTT.N)r   r   r   r   rP   s    r!   r   _RttMonitor.reset  sA     :::  &&(""$ :::::sD   A;AA;5A!A;AA;A;!A8'A*(A84A;c                &  #     U R                  5       I S h  vN nU R                  U5      I S h  vN   g  N N! [         a    U R                  5       I S h  vN     g [         a&    U R
                  R                  5       I S h  vN     g f = f7fr8   )_pingr   r   rE   r   rs   r   )rI   rtts     r!   r/   _RttMonitor._run  so     
	% 

$C//#&&& %& 	**, 	%**""$$$	%sa   B: 6: 8: B: : BABB&BBBBBBc                n  #    U R                   R                  5        ISh  vN nU R                  R                  (       a  [	        S5      e[
        R                  " 5       nUR                  5       I Sh  vN   [        U5      sSSS5      ISh  vN   $  Ns N" N	! , ISh  vN  (       d  f       g= f7f)z)Run a "hello" command and return the RTT.Nz_RttMonitor closed)	rs   r   rB   r   r   r&   r'   hellor)   )rI   r   r(   s      r!   r  _RttMonitor._ping  st     ::&&((D~~&& 455NN$E**,&u- )(( 	 )(((sW   B5BB5AB4B5BB5BB5BB5B2!B$"B2.B5)r   r   r   rs   )rJ   r   r   r   r   r   ra   )r  r`   r>   r@   )r>   ztuple[Optional[float], float])r>   r`   )rb   rc   rd   re   rL   rE   r   r   r   r/   r  rf   r   r   s   @r!   r|   r|     s,    *"!0F
%%. .r#   r|   c                d    [         R                  " U [        5      n[        R	                  U5        g r8   )rC   rD   _unregister	_MONITORSadd)r0   rD   s     r!   rH   rH     s    
++g{
+CMM#r#   c                .    [         R                  U 5        g r8   )r  remove)monitor_refs    r!   r  r    s    [!r#   c                     [         c  g [        [         5      n U  H#  nU" 5       nU(       d  M  UR                  5         M%     S ng r8   )r  listr:   )monitorsrD   r0   s      r!   _shutdown_monitorsr    sA     IH %7!!# 
 Gr#   c                 V    [         n U (       a  U " 5         [        n U (       a  U " 5         g g r8   )r  r   )shutdowns    r!   _shutdown_resourcesr    s!    !H
"H
 r#   )r    r   r>   r@   )r(   r`   r>   r`   )r0   r+   r>   r@   )r  z"weakref.ReferenceType[MonitorBase]r>   r@   ra   )<__doc__
__future__r   r   atexitr   r&   rC   typingr   r   r   pymongor   r   pymongo._csotr	   !pymongo.asynchronous.srv_resolverr
   pymongo.errorsr   r   pymongo.hellor   pymongo.lockr   pymongo.loggerr   r   r   pymongo.periodic_executorr   pymongo.pool_optionsr   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.asynchronous.poolr   r   r   pymongo.asynchronous.settingsr   pymongo.asynchronous.topologyr   _IS_SYNCr"   r)   r+   ri   r   r|   setr  rH   r  r  r  registerrg   r#   r!   <module>r5     s    @ "      / / - ' : >  + G G 9 ) 2 8 
 ?6.; ;|w"k w"t9 9x=.+ =.F E	
"  
OO'( r#   