+\hhNdZddlmZddlZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z mZddlmZddlmZddlmZmZdd lmZdd lmZdd lmZmZmZdd lmZdd l m!Z!ddl"m#Z#ddl$m%Z%e rddl&m'Z'm(Z(m)Z)ddl*m+Z+ddl,m-Z-dZ.d"dZ/d#dZ0GddZ1Gdde1Z2Gdde1Z3Gdde1Z4e5Z6d$dZ7d%dZ8d&d Z9d&d!Z:e.rejve:yy)'z9Class to monitor a MongoDB server on a background thread.) annotationsN) TYPE_CHECKINGAnyOptional)commonperiodic_executor) MovingMinimum) _SrvResolver)NetworkTimeout_OperationCancelled)Hello)_async_create_lock) _SDAM_LOGGER _debug_log_SDAMStatusMessage)_shutdown_executors)_is_faas) MovingAverage)ServerDescription)AsyncConnectionPool_CancellationContext)TopologySettings)TopologyFc.d|_d|_d|_y)z'PYTHON-2433 Clear error traceback info.N) __traceback__ __context__ __cause__errors _/root/niggaflix-v3/playground/venv/lib/python3.12/site-packages/pymongo/asynchronous/monitor.py _sanitizer"2sEEEOcDtdtj|z S)zReturn the duration since the given start time. Accounts for buggy platforms where time.monotonic() is not monotonic. See PYTHON-4600. g)maxtime monotonic)starts r!_monotonic_durationr)9s sDNN$u, --r#c<eZdZddZd dZd dZd dZd dZd dZy) MonitorBasecdfd }tj||||}||_ddfd }tj||j tj |||_t|y)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. cVK}|y|jd{y7w)NFT)_run)monitorself_refs r!targetz$MonitorBase.__init__..targetLs-jG,,.  !s )'))interval min_intervalr1nameNc:}|r|jyyN gc_safe_close)dummyr/r0s r!_on_topology_gcz-MonitorBase.__init__.._on_topology_gcYs jG%%'r#)returnboolr6)r9zOptional[Topology]r;None) rAsyncPeriodicExecutor _executorweakrefrefcloseproxy _topology _register) selftopologyr4r2r3r1executorr:r0s @r!__init__zMonitorBase.__init__Cs` %::Ld " (;;tX^^4 xA$r#c8|jjy)z[Start monitoring, or restart after a fork. Multiple calls have no effect. N)r?openrFs r!rKzMonitorBase.openes r#c8|jjy)zGC safe close.N)r?rBrLs r!r8zMonitorBase.gc_safe_closels r#c,K|jyw)zWClose and stop monitoring. open() restarts the monitor after closing. Nr7rLs r!rBzMonitorBase.closeps scTK|jjd{y7w)zWait for the monitor to stop.N)r?joinrLs r!rPzMonitorBase.joinwsnn!!### (&(c8|jjy)z)If the monitor is sleeping, wake it soon.N)r?wakerLs r! request_checkzMonitorBase.request_check{s r#N)rGrr4strr2intr3floatr;r=) __name__ __module__ __qualname__rIrKr8rBrPrTr#r!r+r+Bs! D$r#r+ceZdZ d fd Zd dZd dZd dZd dZd dZd dZ d dZ dd Z dd Z dd Z xZS)Monitorct||d|jtj||_||_||_|jjj|_ |jduxr|jj|_ d|_ d|_t|||j!|j"|_|j&dk(rd|_y|j&dk(rd|_yt+ |_y)aClass 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_taskNstreamTpollF)superrIheartbeat_frequencyrMIN_HEARTBEAT_INTERVAL_server_description_pool _settings _pool_options_event_listeners _listenersenabled_for_server_heartbeat_publish_cancel_context_conn_id _RttMonitor_create_pool_for_monitoraddress _rtt_monitorserver_monitoring_mode_streamr)rFserver_descriptionrGpooltopology_settings __class__s r!rIzMonitor.__init__s   )  1 1  ) )  $6  *..66GGt3d8d8d ?C'+ '    - -.@.H.H I   3 3x ?DL  5 5 ? DL'z>DLr#cB|j}|r|jyy)zCancel any concurrent hello check. Note: this is called from a weakref.proxy callback and MUST NOT take any locks. N)rncancel)rFcontexts r! cancel_checkzMonitor.cancel_checks# &&  NN  r#cK|jj|jjr#|jj d{yy7w)z1Start an _RttMonitor that periodically runs ping.N)rsrKr?_stoppedrBrLs r!_start_rtt_monitorzMonitor._start_rtt_monitorsG  >> " "##))+ + + # +sAAAAc|jj|jj|j yr6)r?rBrsr8r}rLs r!r8zMonitor.gc_safe_closes0  '') r#cKtj|jj|jjdd{y7w)NT)return_exceptions)asynciogatherr?rPrsrLs r!rPz Monitor.joins?nn NN   !4#4#4#9#9#;t   sA AAAcK|j|jjd{|jd{y77wr6)r8rsrB_reset_connectionrLs r!rBz Monitor.closesI %%'''$$&&& ( 's!.AAAA AAcTK|jjd{y7wr6)rgresetrLs r!rzMonitor._reset_connectionsjj   rQc K |j} |jd{|_|jj|j|jjt|jjt d{|j"r^|jj rH|jj$r2|j'd{|jj|jjr&|j r|jj|jjr#|jjd{yy7P#t$r}t|t |jj ||_|j r|jjYd}~|jjr$|jjd{7yyd}~wwxYw77B#t($r|jd{7YwxYw7#|jjr$|jjd{7wwxYww)Nr) reset_poolinterrupt_connections)rf _check_serverr r"rrris_server_type_knownr? skip_sleeprrsrBrD on_changer isinstancer rutopology_versionrReferenceError)rFprev_sdexcs r!r.z Monitor._runs' 0..G 151C1C1E+E(..**((3399&01I1I1O1OQ_&`+   ||((==,,==--///))+''--'2N2N))+ ~~&&''--///'G,F& #+<,,44C,(//NN--/0~~&&''--///'E  0 **,    0~~&&''--///'sJ H$E7E4 E7A!H$HAH$H! AH$:3J-I .J4E77 HAHH$3JHJHH$!H$$I II I I  I J4J JJ  Jc xKd|_tj} |jd{S7#t$rt $rk}t ||j}|j}t|}t|jxr|jxr |j}|jr,|jJ|jj!||||t#j$t&j(rMt+t"t,j.|j0j2|d|d||dz||j |j5d{7t7|t8r|j:j=d{7t?||cYd}~Sd}~wwxYww)z^Call hello or read the next streaming response. Returns a ServerDescription. Nr)message topologyId serverHost serverPortawaited durationMSfailuredriverConnectionIdr) ror&r' _check_oncer Exceptionr"rfrrr)r<rurrrmrkpublish_server_heartbeat_failedr isEnabledForloggingDEBUGrrHEARTBEAT_FAILrD _topology_idrrr rsrr)rFr(r sdrrdurationrs r!rzMonitor._check_serversf    ;))++ ++   ; e ))BjjG*51H4<<,C,C[H[H[\G}}222??SXZab((7 .==#~~::&qz&qz#'$!'+}} ((* * *%!45##))+ + +$WE: :5 ;sVF:979F:9F7 DF2&E)'3F2FF2,F7-F:2F77F:c 2K|jj}|j}t|jjxr(|j xr|j xr |j}|jr*|jJ|jj|||jr.|jjr|jd{|jj4d{}tj t"j$rSt'tt(j*|j,j.|j0|j2|d|d||j4|_ |j0|_|j9|d{\}}|j:s#|j<j?|d{|j<jAd{\}}tC||||}|jr6|jJ|jjE||||j:tj t"j$rbt'tt(jF|j,j.|j0|j2|d|d||dz|jH |cdddd{S777K777#1d{7swYyxYww)zfA single attempt to call hello. Returns a ServerDescription, or raises an exception. Nrr)rrrserverConnectionIdrrr)min_round_trip_timer) rrrrrrrrreply)%rfrrr<rgconnsrurrrmrk publish_server_heartbeat_startedrn cancelledrcheckoutrrrrrrHEARTBEAT_STARTrDridserver_connection_idcancel_contextro_check_with_socket awaitablers add_samplegetr"publish_server_heartbeat_succeededHEARTBEAT_SUCCESSdocument) rFrrrrconnresponseround_trip_timeavg_rttmin_rtts r!rzMonitor._check_once%s **22  % % JJ   a a"2I2I abNaNa  ==??. .. OO < >#~~::'+ww'+'@'@&qz&qz# $(#6#6D  GGDM.2.E.Ed.K(K %Ho%%''22?CCC%)%6%6%:%:%<< GW"7HgSZ[B}}222BB_h8J8J((7 .@@#~~::'+ww'+'@'@&qz&qz#.5"++ Q( ( (  +(  )LC<)( ( ( ( sCLK2"L<K5=LB-L-K8.1LK; !LK>CL L,L-L5L8L;L>LLLL LLcKtj}|jr$t|j d{d}n|j re|j rY|jjrC|j|jj|jjd{}n|jddd{}t|}||fS7707w)zcReturn (Hello, round_trip_time). Can raise ConnectionFailure or OperationFailure. NT)r) r&r' more_to_comer _next_replyruperformed_handshakerfr_hellorhrdr))rFrr(rrs r!rzMonitor._check_with_socketbs     4#3#3#55FH LLT55$:R:R:c:c"[[((9922H "[[t44H&u-!!6  5s49C'C!A6C'2C#3C' C%C'#C'%C')rvrrGrrwrrxrrX)r;r)rrr;ztuple[Hello, float])rYrZr[rIr}rr8rPrBrr.rrr __classcell__rys@r!r^r^sd&*-&*&* &* , &*P ,  '!(0T%;N;z"r#r^c0eZdZdfd ZddZddZxZS) SrvMonitorcRt||dtj|j||_|j j |_t|j jtsJ|j j|_ tj|_y)zClass to poll SRV records on a background thread. Pass a Topology and a TopologySettings. The Topology is weakly referenced. pymongo_srv_polling_threadN)rcrIrMIN_SRV_RESCAN_INTERVALrdrh_seeds _seedlistrfqdnrU_fqdnr&r' _startup_time)rFrGrxrys r!rIzSrvMonitor.__init__{s~   (  * *  1 1  +..$..--s333..-- !^^-r#cjKtj|jtjzkry|j d{}|r6||_ |jj|j d{yy7=7#t$r|jd{7YywxYwwr6) r&r'rrr _get_seedlistrrD on_srv_updaterrB)rFseedlists r!r.zSrvMonitor._runs >> d0063Q3QQ Q ++-- %DN #nn224>>BBB .C! #jjl"" #sTAB3 B B3(B B B B3 B B0'B*(B0-B3/B00B3cK t|j|jjj|jj }|j d{\}}t|dk(rt |jjt|tj|S7Q#t$r|jYywxYww)zXPoll SRV records for a seedlist. Returns a list of ServerDescriptions. Nr)r rrh pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenrr?update_intervalr%rrrT)rFresolverrttls r!rzSrvMonitor._get_seedlists  # ++;;//H #+"@"@"BBMHc8}!" NN * *3sF4R4R+S TOC       s;CAB4 B2!B4=5C2B44C CCC)rGrrxrrX)r;zOptional[list[tuple[str, Any]]])rYrZr[rIr.rrrs@r!rrzs.& #r#rcPeZdZdfd Zd dZd dZd dZd dZd dZd dZ xZ S) rpct||d|jtj||_t |_t|_ t|_ y)z\Maintain round trip times for a server. The Topology is weakly referenced. pymongo_server_rtt_taskN) rcrIrdrrergr_moving_averager _moving_minr_lock)rFrGrxrwrys r!rIz_RttMonitor.__init__sQ   %  1 1  ) )   ,(?') r#ctK|j|jjd{y7wr6)r8rgrrLs r!rBz_RttMonitor.closes* jj   s .868cK|j4d{|jj||jj|dddd{y7K7#1d{7swYyxYww)zAdd a RTT sample.N)rrrr)rFsamples r!rz_RttMonitor.add_samplesd:: 0 0  + +F 3    ' ' / 0 0 0 0 0 0 0sDA8AA87A# A8A!A8!A8#A5)A, *A51A8cK|j4d{|jj|jjfcdddd{S7I7#1d{7swYyxYww)zBGet the calculated average, or None if no samples yet and the min.N)rrrrrLs r!rz_RttMonitor.getsk:: F F''++-t/?/?/C/C/EE F F F F F F FsDA6AA64A! A6AA6A6!A3'A* (A3/A6cK|j4d{|jj|jjdddd{y7I7#1d{7swYyxYww)zReset the average RTT.N)rrrrrLs r!rz_RttMonitor.resets`:: % %  & & (    " " $ % % % % % % %sDA6AA65A! A6AA6A6!A3'A* (A3/A6c"K |jd{}|j|d{y77#t$r|jd{7Yyt$r&|j j d{7YywxYwwr6)_pingrrrBrrgr)rFrtts r!r.z_RttMonitor._runsq % $C//#& & &% & **,   %**""$ $ $ %saB:6:8:B::B AB B%B BB  B B  BcXK|jj4d{}|jjr t dt j }|jd{t|cdddd{S7m7"7 #1d{7swYyxYww)z)Run a "hello" command and return the RTT.Nz_RttMonitor closed) rgrr?rrr&r'hellor))rFrr(s r!rz_RttMonitor._pings::&&( . .D~~&& 455NN$E**,  &u-  . . .   . . . .sWB*BB*A B.B/B= B* B B*BB*B'B B'#B*)rGrrxrrwrrX)rrWr;r=)r;ztuple[Optional[float], float])r;rW) rYrZr[rIrBrrrr.rrrs@r!rprps'*"! 0 F % %.r#rpcbtj|t}tj |yr6)r@rA _unregister _MONITORSadd)r/rAs r!rErEs ++g{ +C MM#r#c.tj|yr6)rremove) monitor_refs r!rrs [!r#cvtytt}|D]}|}|s |jd}yr6)rlistr8)monitorsrAr/s r!_shutdown_monitorsrsDIH$%   ! ! #$ Gr#cBt}|r|t}|r|yyr6)rr)shutdowns r!_shutdown_resourcesrs!!H "H r#)r rr;r=)r(rWr;rW)r/r+r;r=)rz"weakref.ReferenceType[MonitorBase]r;r=rX)<__doc__ __future__rratexitrr&r@typingrrrpymongorr pymongo._csotr !pymongo.asynchronous.srv_resolverr pymongo.errorsr r pymongo.hellor pymongo.lockrpymongo.loggerrrrpymongo.periodic_executorrpymongo.pool_optionsrpymongo.read_preferencesrpymongo.server_descriptionrpymongo.asynchronous.poolrrrpymongo.asynchronous.settingsrpymongo.asynchronous.topologyr_IS_SYNCr"r)r+r^rrpsetrrErrrregisterr\r#r!rs@"  //-':>+GG9)28 ?6 .;;|w"kw"t99x=.+=.F E  "   FOO'( r#