
    &g!                    f   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rSSK	r	SSK
r
SSKrSSKJr  SSKJrJrJrJrJrJr  SSKJrJrJrJr  SSK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&J'r'J(r(J)r)J*r*J+r+J,r,J-r-  SSK.J/r/  SSK0J1r1J2r2J3r3  SSK4J5r5J6r6J7r7J8r8J9r9  SSK:J;r;  SSK<J=r=  SSK>J?r?J@r@JArAJBrBJCrC  SSKDJErEJFrFJGrGJHrHJIrI  \(       a  SSKJJKrK  SSKLJMrM  SSKNJOrOJPrP  SrQ\R" \" \S5      R                  5      rUSS jrV " S S5      rW " S S5      rX      S S jrYS!S jrZ S"     S#S jjr[g)$z<Internal class to monitor a topology of one or more servers.    )annotationsN)Path)TYPE_CHECKINGAnyCallableMappingOptionalcast)_csotcommonhelpers_sharedperiodic_executor)_ServerSession_ServerSessionPool)MonitorBase
SrvMonitor)Pool)Server)	ConnectionFailureInvalidOperationNetworkTimeoutNotPrimaryErrorOperationFailurePyMongoErrorServerSelectionTimeoutErrorWaitQueueTimeoutError
WriteError)Hello)_async_cond_wait_async_create_condition_async_create_lock)_SDAM_LOGGER_SERVER_SELECTION_LOGGER
_debug_log_SDAMStatusMessage_ServerSelectionStatusMessage)PoolOptions)ServerDescription)	Selectionany_server_selectorarbiter_server_selectorsecondary_server_selectorwritable_server_selector)SRV_POLLING_TOPOLOGIESTOPOLOGY_TYPETopologyDescription)_updated_topology_description_srv_pollingupdated_topology_description)ObjectId)TopologySettings)ClusterTime_AddressFc                    U " 5       nU(       d  g  UR                  5       nUu  p4U" U6   M  ! [        R                   a     gf = f)NFT)
get_nowaitqueueEmpty)	queue_refqeventfnargss        bC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/asynchronous/topology.pyprocess_events_queuerA   W   sT    A
	LLNE HBI  {{ 	
 	s   - AAc                     \ rS rSrSrS1S jrS2S jrS3S jr   S4           S5S jjr            S6S jr	    S7             S8S	 jjr
    S7             S8S
 jjr  S9         S:S jjr  S;       S<S jjr  S;       S<S jjrS=S jrS=S jrS>S jrS?S jrS@S jr    SAS jrSBS jrSBS jrSCS jrSDS jrSDS jrSESFS jjrSGS jrS2S jrS2S jr\SHS j5       rSIS jr SJS jr!SKS  jr"SLS! jr#S2S" jr$SMS# jr%SNS$ jr&SNS% jr'S2S& jr(S2S' jr)SOS( jr*SOS) jr+SPS* jr,S2S+ jr-SQS, jr.SRS- jr/SSS. jr0STS/ jr1S0r2g)UTopologyh   z*Monitor a topology of one or more servers.c           	     8
  ^ UR                   U l         UR                  R                  U l        U R                  S L=(       a    U R                  R                  U l        U R                  S L=(       a    U R                  R                  U l        S U l        S U l	        U R
                  (       d  U R                  (       a  [        R                  " SS9U l        [        R                  " [        R                  5      (       a'  [!        [        ["        R$                  U R                   S9  U R                  (       aK  U R                  c   eU R                  R'                  U R                  R(                  U R                   445        Xl        [-        UR/                  5       UR1                  5       UR2                  S S U5      nX l        [-        [6        R8                  0 S S S U R*                  5      nU R                  (       aV  U R                  c   eU R                  R'                  U R                  R:                  X0R4                  U R                   445        [        R                  " [        R                  5      (       a3  [!        [        ["        R<                  U R                   UU R4                  S9  UR>                   H  nU R
                  (       aK  U R                  c   eU R                  R'                  U R                  R@                  X@R                   445        [        R                  " [        R                  5      (       d  M  [!        [        ["        RB                  U R                   US   US   S9  M     [E        URG                  5       5      U l$        SU l%        SU l&        [O        5       U l(        [S        U RP                  [T        (       a  U R*                  RV                  OS 5      U l,        0 U l-        S U l.        S U l/        [a        5       U l1        U R
                  (       d  U R                  (       a  U R                  c   eSU4S	 jjn[d        Rf                  " [h        Rj                  [h        Rl                  US
S9n[n        Rp                  " U R                  URr                  5      mX`l	        URu                  5         S U l;        U R*                  Rx                  b5  U R*                  Rz                  (       d  [}        X R*                  5      U l;        / U l?        g )Nd   )maxsizemessage
topologyIdrI   rJ   previousDescriptionnewDescriptionr      )rI   rJ   
serverHost
serverPortFc                 "   >#    [        T 5      $ 7fN)rA   )weaks   r@   target!Topology.__init__.<locals>.target   s     +D11s   pymongo_events_thread)intervalmin_intervalrT   name)returnbool)@_topology_id_pool_options_event_listeners
_listenersenabled_for_server_publish_serverenabled_for_topology_publish_tp_events_Topology__events_executorr9   Queuer"   isEnabledForloggingDEBUGr$   r%   START_TOPOLOGYputpublish_topology_opened	_settingsr0   get_topology_typeget_server_descriptionsreplica_set_name_descriptionr/   Unknown$publish_topology_description_changedTOPOLOGY_CHANGEseedspublish_server_openedSTART_SERVERlistserver_descriptions_seed_addresses_opened_closedr!   _lockr    _IS_SYNCcondition_class
_condition_servers_pid_max_cluster_timer   _session_poolr   AsyncPeriodicExecutorr   EVENTS_QUEUE_FREQUENCYMIN_HEARTBEAT_INTERVALweakrefrefcloseopen_srv_monitorfqdnload_balancedr   _monitor_tasks)selftopology_settingstopology_description
initial_tdseedrT   executorrS   s          @r@   __init__Topology.__init__k   s   -::+99JJ#d:at?a?a??$6_4??;_;_ &*4#3#3 ;;s3DL$$W]]33*99,, <<+++LLdooEEHYHYG[\]*2//1557.. 
 1(!!2tT4

 <<+++LLOOHH!2!2D4E4EF $$W]]33*::,,$.#00 &++D##||///  $//"G"G$PaPaIb!cd((77 .;;#00#Aw#Aw ,  $$8$L$L$NO')
1JJ((66
 13#'	8</14#3#3<<+++2 )>>66#::,	H ;;t||X^^<D%-"MMO >>*4>>3O3O *4 @D 24    c                  #    [         R                  " 5       nU R                  c  Xl        OXR                  :w  a  Xl        [        R                  SS S:  a
  S[
        40nOSS0n[        R                  "  S0 UD6  U R                   ISh  vN   U R                  R                  5        H  nUR                  5       I Sh  vN   M     U R                  R                  5         SSS5      ISh  vN   U R                   ISh  vN   U R                  5       I Sh  vN   SSS5      ISh  vN   g N Nr NF! , ISh  vN  (       d  f       N[= f NM N7 N)! , ISh  vN  (       d  f       g= f7f)a  Start monitoring, or restart after a fork.

No effect if called multiple times.

.. warning:: Topology is shared among multiple threads and is protected
  by mutual exclusion. Using Topology from a process other than the one
  that initialized it will emit a warning and may result in deadlock. To
  prevent this from happening, AsyncMongoClient must be created after any
  forking.

N   )      skip_file_prefixes
stacklevel   )zAsyncMongoClient opened before fork. May not be entirely fork-safe, proceed with caution. See PyMongo's documentation for details: https://dochub.mongodb.org/core/pymongo-fork-deadlock)osgetpidr   sysversion_info_pymongo_dirwarningswarnr}   r   valuesr   r   reset_ensure_opened)r   pidkwargsservers       r@   r   Topology.open   s     iik99IIII#w..@&*MMH 	 zzz"mm224F ,,.(( 5 ""((* "z :::%%''' :: " ) "zzz ' :::s   BE3D3E31D9D5"D9%E30D71E3EE3	EEE"E3-E.E35D97E39E?E EE3EE3E0E" E0,E3c                b    [         R                  " 5       nUc  U R                  R                  $ U$ rR   )r   	remainingrm   server_selection_timeout)r   timeouts     r@   get_server_selection_timeout%Topology.get_server_selection_timeout   s(    //#?>>:::r   Nc           
       #    Uc  U R                  5       nOUn[        (       d)  U R                  (       a  U R                  5       I Sh  vN   U R                   ISh  vN   U R                  XX%U5      I Sh  vN nU Vs/ s H,  n[        [        U R                  UR                  5      5      PM.     snsSSS5      ISh  vN   $  N Nl NSs  snf  N! , ISh  vN  (       d  f       g= f7f)a2  Return a list of Servers matching selector, or time out.

:param selector: function that takes a list of Servers and returns
    a subset of them.
:param operation: The name of the operation that the server is being selected for.
:param server_selection_timeout: maximum seconds to wait.
    If not provided, the default value common.SERVER_SELECTION_TIMEOUT
    is used.
:param address: optional server address to select.

Calls self.open() if needed.

Raises exc:`ServerSelectionTimeoutError` after
`server_selection_timeout` if no matching servers are found.
N)
r   r~   r   cleanup_monitorsr}   _select_servers_loopr
   r   get_server_by_addressaddress)	r   selector	operationr   r   operation_idserver_timeoutry   sds	            r@   select_serversTopology.select_servers  s     . $+!>>@N5N xD//''))):::(,(A(A)7) #
 PcObVT77

CDOb :: *# :::s~   AC/C	C/C
C/!C8C9C3C4C6C/CC/
C/CCC/C,CC,(C/c                  #    [         R                  " 5       nXb-   nSn[        R                  " [        R
                  5      (       aI  [        [        [        R                  UUUU R                  U R                  R                  R                  S9  U R                  R                  XU R                  R                  S9n	U	(       Gd  US:X  d  Xg:  a  [        R                  " [        R
                  5      (       aY  [        [        [        R                   UUUU R                  U R                  R                  R                  U R#                  U5      S9  [%        U R#                  U5       SU SU R                  < 35      eU(       dn  [        [        [        R&                  UUUU R                  U R                  R                  R                  [)        SU[         R                  " 5       -
  -  5      S	9  S
nU R+                  5       I Sh  vN   U R-                  5         [/        U R0                  [2        R4                  5      I Sh  vN   U R                  R7                  5         [         R                  " 5       nU R                  R                  XU R                  R                  S9n	U	(       d  GM  U R                  R7                  5         U	$  N N7f)z7select_servers() guts. Hold the lock when calling this.F)rI   r   r   operationIdtopologyDescriptionclientId)custom_selectorr   )rI   r   r   r   r   r   failurez, Timeout: zs, Topology Description: i  )rI   r   r   r   r   r   remainingTimeMSTN)time	monotonicr#   rg   rh   ri   r$   r&   STARTEDdescription_topology_settingsr\   rq   apply_selectorrm   server_selectorFAILED_error_messager   WAITINGintr   _request_check_allr   r   r   r   check_compatible)
r   r   r   r   r   r   nowend_timelogged_waitingry   s
             r@   r   Topology._select_servers_loop.  sU     nn=#00??(5==!#($($4$4))<<II #//>>t~~/M/M ? 
 &!|s~+88GG0 = D D!)"+$0,0,<,<!%!1!1!D!D!Q!Q $ 3 3H =	 2**845[	Ibcgcscsbvw  ",9AA%' ,(,(8(8!--@@MM$'4>>;K0K(L$M	 "&%%'''##% #4??F4Q4QRRR..0.."C"&"3"3"B"B4>>3Q3Q #C #S &%Z 	**,"" ( Ss+   HKK	;KKA(K0KKc                  #    U R                  XX4U5      I S h  vN n[        Xu5      n[        U5      S:X  a  US   $ [        R                  " US5      u  pUR
                  R                  U	R
                  R                  ::  a  U$ U	$  Nn7f)NrN   r   r   )r   _filter_serverslenrandomsamplepooloperation_count)
r   r   r   r   r   deprioritized_serversr   serversserver1server2s
             r@   _select_serverTopology._select_serverz  s      ++!9L
 
 "'Aw<11:!==!4<<''7<<+G+GGNN
s   B
BA/B
c                  #    U R                  UUUUUUS9I Sh  vN n[        R                  " 5       (       a*  [        R                  " UR                  R
                  5        [        R                  " [        R                  5      (       ay  [        [        [        R                  UUUU R                  U R                  R                  R                  UR                  R                  S   UR                  R                  S   S9	  U$  N7f)zALike select_servers, but choose a random server if several match.r   Nr   rN   )rI   r   r   r   r   r   rO   rP   )r   r   get_timeoutset_rttr   min_round_trip_timer#   rg   rh   ri   r$   r&   	SUCCEEDEDr   r\   r   )r   r   r   r   r   r   r   r   s           r@   select_serverTopology.select_server  s      **$!% + 
 
 MM&,,@@A#00??(5??!#($($4$4))<<II!--55a8!--55a8
 -
s   D	DC-D	c                L   #    U R                  [        UUUUS9I Sh  vN $  N7f)a  Return a Server for "address", reconnecting if necessary.

If the server's type is not known, request an immediate check of all
servers. Time out after "server_selection_timeout" if the server
cannot be reached.

:param address: A (host, port) pair.
:param operation: The name of the operation that the server is being selected for.
:param server_selection_timeout: maximum seconds to wait.
    If not provided, the default value
    common.SERVER_SELECTION_TIMEOUT is used.
:param operation_id: The unique id of the current operation being performed. Defaults to None if not provided.

Calls self.open() if needed.

Raises exc:`ServerSelectionTimeoutError` after
`server_selection_timeout` if no matching servers are found.
r   N)r   r*   )r   r   r   r   r   s        r@   select_server_by_address!Topology.select_server_by_address  s8     2 ''$% ( 
 
 	
 
s   $"$c                  #    U R                   nUR                  UR                     n[        XQ5      (       a  g[	        U R                   U5      nUR
                  (       d/  UR                  (       al  UR                  [        R                  :X  aN  U R                  R                  UR                  5      nU(       a"  UR                  R                  5       I Sh  vN   XQ:H  nU R                  (       a^  U(       dW  U R                  c   eU R                  R!                  U R"                  R$                  XQUR                  U R&                  445        X`l         U R)                  5       I Sh  vN   U R*                  (       a]  U(       dV  U R                  c   eU R                  R!                  U R"                  R,                  X@R                   U R&                  445        [.        R0                  " [2        R4                  5      (       a:  U(       d3  [7        [.        [8        R:                  U R&                  UU R                   S9  U R<                  (       a  UR                  [        R>                  :X  ap  U R                   R                  [@        ;  aR  U R<                  RC                  5       I Sh  vN   [D        (       d%  U RF                  RI                  U R<                  5        U(       aM  U R                  R                  UR                  5      nU(       a!  UR                  RK                  US9I Sh  vN   U RL                  RO                  5         g GNu GN N N'7f)zYProcess a new ServerDescription on an opened topology.

Hold the lock when calling this.
NrK   )interrupt_connections)(rq   _server_descriptionsr   _is_stale_server_descriptionr2   is_readableis_server_type_knowntopology_typer/   Singler   getr   readyra   rd   rk   r_   "publish_server_description_changedr\   _update_serversrc   rs   r"   rg   rh   ri   r$   r%   rt   r   rr   r.   r   r~   r   appendr   r   
notify_all)	r   server_description
reset_poolr   td_oldsd_oldnew_tdr   suppress_events	            r@   _process_changeTopology._process_change  s     "",,-?-G-GH'CC-d.?.?AST))338L8LP]PdPd8d]]&&'9'A'ABFkk'')))5<<+++LLOOFF1C1K1KTM^M^_ #""$$$N<<+++LLOOHH..0A0AB $$W]]33N*::,,$*#00   M$9$99!!//7MM##))+++8##**4+<+<= ]]&&'9'A'ABFkk''>S'TTT 	""$e * 	%4 , UsK   CMMBM)M*D?M)M*BM-M. MMMMc                @  #    U R                    ISh  vN   U R                  (       aD  U R                  R                  UR                  5      (       a  U R                  XU5      I Sh  vN   SSS5      ISh  vN   g Nj N N	! , ISh  vN  (       d  f       g= f7f)z>Process a new ServerDescription after an hello call completes.N)r}   r{   rq   
has_serverr   r  )r   r   r   r   s       r@   	on_changeTopology.on_change  sh      ::: || 1 1 < <=O=W=W X X**+=K`aaa :: b :::sW   BA>BAB(B )B-B8B9B BBB
BBBc                @  #    U R                   nUR                  [        ;  a  g[        U R                   U5      U l         U R	                  5       I Sh  vN   U R
                  (       aV  U R                  c   eU R                  R                  U R                  R                  X R                   U R                  445        [        R                  " [        R                  5      (       a4  [        [        [         R"                  U R                  UU R                   S9  gg N7f)zOProcess a new seedlist on an opened topology.
Hold the lock when calling this.
NrK   )rq   r   r.   r1   r   rc   rd   rk   r_   rs   r\   r"   rg   rh   ri   r$   r%   rt   )r   seedlistr   s      r@   _process_srv_updateTopology._process_srv_update1  s      ""'==EdFWFWYab""$$$<<+++LLOOHH..0A0AB $$W]]33*::,,$*#00 4 	%s   ADDC
Dc                   #    U R                    ISh  vN   U R                  (       a  U R                  U5      I Sh  vN   SSS5      ISh  vN   g N? N N	! , ISh  vN  (       d  f       g= f7f)z?Process a new list of nodes obtained from scanning SRV records.N)r}   r{   r  )r   r  s     r@   on_srv_updateTopology.on_srv_updateM  sB      :::||..x888 ::8 :::sT   A3AA3&AAAA3AA3AA3A0A" A0,A3c                8    U R                   R                  U5      $ )a"  Get a Server or None.

Returns the current version of the server immediately, even if it's
Unknown or absent from the topology. Only use this in unittests.
In driver code, use select_server_by_address, since then you're
assured a recent view of the server's type and wire protocol version.
)r   r   r   r   s     r@   r   Topology.get_server_by_addressT  s     }}  ))r   c                    XR                   ;   $ rR   )r   r  s     r@   r  Topology.has_server^  s    --''r   c                Z  #    U R                    ISh  vN   U R                  R                  nU[        R                  :w  a   SSS5      ISh  vN   g[        U R                  5       5      S   R                  sSSS5      ISh  vN   $  Nw N> N	! , ISh  vN  (       d  f       g= f7f)z!Return primary's address or None.Nr   )r}   rq   r   r/   ReplicaSetWithPrimaryr-   _new_selectionr   )r   r   s     r@   get_primaryTopology.get_primarya  sh      ::: --;;M C CC ::
 ,D,?,?,AB1EMM :::::sb   B+BB+,BB+BB+%B9B+BB+B+B+B(BB($B+c                  #    U R                    ISh  vN   U R                  R                  nU[        R                  [        R
                  4;  a  [        5       sSSS5      ISh  vN   $ [        U" U R                  5       5      5       Vs1 s H  o3R                  iM     snsSSS5      ISh  vN   $  N NQs  snf  N! , ISh  vN  (       d  f       g= f7f)z+Return set of replica set member addresses.N)
r}   rq   r   r/   r  ReplicaSetNoPrimarysetiterr  r   )r   r   r   r   s       r@   _get_replica_set_members!Topology._get_replica_set_membersk  s     
 ::: --;;M3311%  u :: *.ht7J7J7L.M)NO)N2JJ)NO :: P :::su   CB7CACC'B9(C-!CB;#C%C1C 2C9C;C CCC	CCc                H   #    U R                  [        5      I Sh  vN $  N7f)z"Return set of secondary addresses.N)r  r,   r   s    r@   get_secondariesTopology.get_secondariesz  s     223LMMMM   " "c                H   #    U R                  [        5      I Sh  vN $  N7f)z Return set of arbiter addresses.N)r  r+   r"  s    r@   get_arbitersTopology.get_arbiters~  s     223JKKKKr%  c                    U R                   $ )z1Return a document, the highest seen $clusterTime.r   r"  s    r@   max_cluster_timeTopology.max_cluster_time  s    %%%r   c                p    U(       a/  U R                   (       a  US   U R                   S   :  a  Xl         g g g )NclusterTimer*  r   cluster_times     r@   _receive_cluster_time_no_lock&Topology._receive_cluster_time_no_lock  s;      **.1G1G1VV)5& W	 r   c                   #    U R                    IS h  vN   U R                  U5        S S S 5      IS h  vN   g  N& N! , IS h  vN  (       d  f       g = f7frR   )r}   r1  r/  s     r@   receive_cluster_timeTopology.receive_cluster_time  s+     :::..|< :::::s<   A:A>A<AAAAAAc                   #    U R                    ISh  vN   U R                  5         [        U R                  U5      I Sh  vN   SSS5      ISh  vN   g NC N N	! , ISh  vN  (       d  f       g= f7f)z=Wake all monitors, wait for at least one to check its server.N)r}   r   r   r   )r   	wait_times     r@   request_check_allTopology.request_check_all  sE     :::##%"4??I>>> ::> :::sV   A7AA7*AAAA7AA7AA7A4#A&$A40A7c                    U R                   R                  [        R                  :X  a  U R                   R                  $ U R                   R
                  $ )znReturn a list of all data-bearing servers.

This includes any server that might be selected for an operation.
)rq   r   r/   r   known_serversreadable_serversr"  s    r@   data_bearing_serversTopology.data_bearing_servers  sB    
 **m.B.BB$$222  111r   c           	     b  #    / nU R                    IS h  vN   U R                  5        HP  nU R                  UR                     nUR	                  X3R
                  R                  R                  5       45        MR     S S S 5      IS h  vN   U H)  u  p4 UR
                  R                  U5      I S h  vN   M+     g  N N6! , IS h  vN  (       d  f       NK= f N'! [         aD  n[        USUSS 5      nU R                  UR                  R                  U5      I S h  vN    e S nAff = f7f)Nr   F)r}   r=  r   r   r   r   genget_overallremove_stale_socketsr   _ErrorContexthandle_errorr   )r   r   r   r   
generationexcctxs          r@   update_poolTopology.update_pool  s     ::://1rzz2(C(C(EFG 2 : #*Fkk66zBBB #* ::: C #CJtD''(:(:(B(BCHHHs   D/B>D/A%C>D/	C 
D/C4C5C9D/ D/CC	CD/C
D,(8D' D#!D''D,,D/c                  #    U R                    ISh  vN   U R                  nU R                  R                  5        HM  nUR	                  5       I Sh  vN   [
        (       a  M(  U R                  R                  UR                  5        MO     U R                  R                  5       U l        U R                  R                  5       R                  5        H)  u  p4X0R                  ;   d  M  X@R                  U   l        M+     U R                  (       aR  U R                  R	                  5       I Sh  vN   [
        (       d%  U R                  R                  U R                  5        SU l        SU l        SSS5      ISh  vN   U R                   (       Ga  U R"                  c   e[%        [&        R(                  0 U R                  R*                  U R                  R,                  U R                  R.                  U R                  R0                  5      U l        U R"                  R3                  U R4                  R6                  WU R                  U R8                  445        U R"                  R3                  U R4                  R:                  U R8                  445        [<        R>                  " [@        RB                  5      (       aZ  [E        [<        [F        RH                  U R8                  WU R                  S9  [E        [<        [F        RJ                  U R8                  S9  U RL                  (       d  U R                   (       ag  U RN                  R	                  5         U RN                  RQ                  S5      I Sh  vN   [S        [T        RV                  " U R"                  5      5        gg GN GNX GN~ GN3! , ISh  vN  (       d  f       GNI= f NV7f)zClear pools and terminate monitors. Topology does not reopen on
demand. Any further operations will raise
:exc:`~.errors.InvalidOperation`.
NFTrK   rH   rN   ),r}   rq   r   r   r   r~   r   r   _monitorr   ry   itemsr   r   r{   r|   rc   rd   r0   r/   rr   rp   max_set_versionmax_election_idr   rk   r_   rs   r\   publish_topology_closedr"   rg   rh   ri   r$   r%   rt   STOP_TOPOLOGYra   re   joinrA   r   r   )r   old_tdr   r   r   s        r@   r   Topology.close  s    
 :::&&F--..0lln$$x''..v? 1 !% 1 1 7 7 9D#00DDFLLNmm+9;MM'*6  O
   ''--///x''..t/@/@A DLDL) :. <<+++ 3%%!!22!!11!!11!!44!D LLOOHH))))	 LLdooEEHYHYG[\]$$W]]33*::,,$*#00 &8&F&FSWSdSd 4#3#3""((*((--a000 T\\!:;	 $4q  % 0 :::v 1s   ON)O=N5N,N5&BN5,AN51N/2AN54O?N2 G9O9O:0O,N5/N52O5O;N><O	Oc                    U R                   $ rR   )rq   r"  s    r@   r   Topology.description  s       r   c                6    U R                   R                  5       $ )z"Pop all session ids from the pool.)r   pop_allr"  s    r@   pop_all_sessionsTopology.pop_all_sessions   s    !!))++r   c                8    U R                   R                  U5      $ )z>Start or resume a server session, or raise ConfigurationError.)r   get_server_session)r   session_timeout_minutess     r@   r[  Topology.get_server_session  s    !!445LMMr   c                :    U R                   R                  U5        g rR   )r   return_server_session)r   server_sessions     r@   r_  Topology.return_server_session  s    00@r   c                B    [         R                  " U R                  5      $ )z]A Selection object, initially including all known servers.

Hold the lock when calling this.
)r)   from_topology_descriptionrq   r"  s    r@   r  Topology._new_selection  s    
 2243D3DEEr   c                  #    U R                   (       a  [        S5      eU R                  (       Gd  SU l        U R                  5       I Sh  vN   U R                  (       d  U R
                  (       a  U R                  R                  5         U R                  (       a8  U R                  R                  [        ;   a  U R                  R                  5         U R                  R                  (       aG  U R                  [        U R                   S   [#        SU R$                  SS.5      5      5      I Sh  vN   U R&                  R)                  5        H  nUR                  5       I Sh  vN   M     g GN& NA N7f)zKStart monitors, or restart after a fork.

Hold the lock when calling this.
z'Cannot use AsyncMongoClient after closeTNr   rN      )ok	serviceIdmaxWireVersion)r|   r   r{   r   rc   ra   re   r   r   r   r   r.   rm   r   r  r(   rz   r   r\   r   r   r   r   s     r@   r   Topology._ensure_opened  s    
 <<"#LMM|||DL&&((( 4#7#7&&++-   d&6&6&D&DH^&^!!&&(~~++**%,,Q/QT5F5FZ\]^   mm**,F++- -) )  s7   A	E8E1C&E82E434E8'E6(
E84E86E8c                   U R                   R                  U5      nUc  gUR                  R                  UR                  UR
                  5      (       a  gUR                  R                  nUR                  nS nU(       aK  [        US5      (       a:  [        UR                  [        5      (       a  UR                  R                  S5      n[        XF5      $ )NTdetailstopologyVersion)r   r   _poolstale_generationsock_generation
service_idr   topology_versionerrorhasattr
isinstancerm  dict _is_stale_error_topology_version)r   r   err_ctxr   cur_tvrt  error_tvs          r@   _is_stale_errorTopology._is_stale_error3  s    ""7+><<(()@)@'BTBTUU ##44WUI..%--.. ==,,->?/AAr   c                  #    U R                  X5      (       a  g U R                  U   nUR                  nUR                  nU R                  R
                  (       a  U(       d  UR                  (       d  g [        U[        5      (       a  UR                  (       a  g [        U[        5      (       a  g [        U[        [        45      (       Ga]  [        US5      (       a  UR                  nO5[        U[        5      (       a  SOS nUR                  R                  SU5      nU[         R"                  ;   a  U[         R$                  ;   nU R                  R
                  (       d   U R'                  [)        XS95      I S h  vN   U(       d  UR*                  S::  a  UR-                  U5      I S h  vN   UR/                  5         g UR                  (       dU  U R                  R
                  (       d   U R'                  [)        XS95      I S h  vN   UR-                  U5      I S h  vN   g g [        U[0        5      (       a  [        U[2        5      (       a  g U R                  R
                  (       d   U R'                  [)        XS95      I S h  vN   UR-                  U5      I S h  vN   UR4                  R7                  5         g g  GNH GN N N NC N,7f)Ncodei{'  rt     )r|  r   rt  rr  rm   r   completed_handshakerv  r   r   r   r   ru  r  rm  r   r   _NOT_PRIMARY_CODES_SHUTDOWN_CODESr  r(   max_wire_versionr   request_checkr   r   rK  cancel_check)	r   r   ry  r   rt  rr  err_codedefaultis_shutting_downs	            r@   _handle_errorTopology._handle_errorG  s"    11w'''
 >>''
7C^C^e^,,1L1L
 z**1ABCC uf%% :: $.e_#E#E%4 ==,,VW=><<<#+~/M/M#M ~~33../@/VWWW#(@(@A(E ,,z222$$&00~~33../@/VWWWll:... 1 011%!677 >>//**+<W+RSSS,,z*** OO((* 2 X 3
 X. T*sn   E<K>K?/K.K	/AKKK$K%A'KKK%K&!K	KKKKKc                   #    U R                    ISh  vN   U R                  X5      I Sh  vN   SSS5      ISh  vN   g N. N N	! , ISh  vN  (       d  f       g= f7f)zHandle an application error.

May reset the server to Unknown, clear the pool, and request an
immediate check depending on the error and the context.
N)r}   r  )r   r   ry  s      r@   rD  Topology.handle_error  s;      :::$$W666 ::6 :::sQ   A"AA"AAAA"AA"AA"AAAA"c                f    U R                   R                  5        H  nUR                  5         M     g)z3Wake all monitors. Hold the lock when calling this.N)r   r   r  rj  s     r@   r   Topology._request_check_all  s%    mm**,F  " -r   c           
       #    U R                   R                  5       R                  5        GHe  u  pXR                  ;  a  U R                  R                  UU U R                  U5      U R                  S9nSnU R                  (       a-  U R                  b   [        R                  " U R                  5      n[        UU R                  U5      UU R                  U R                  US9nXPR                  U'   UR                  5       I Sh  vN   M  U R                  U   R                   R"                  nX R                  U   l        XbR"                  :w  d  GM+  U R                  U   R$                  R'                  UR"                  5      I Sh  vN   GMh     [)        U R                  R                  5       5       H  u  pU R                   R+                  U5      (       a  M'  UR-                  5       I Sh  vN   [.        (       d%  U R0                  R3                  UR4                  5        U R                  R7                  U5        M     g GN@ N NY7f)zbSync our Servers from TopologyDescription.server_descriptions.

Hold the lock while calling this.
)r   topologyr   r   N)r   r   monitortopology_id	listenersevents)rq   ry   rL  r   rm   monitor_class_create_pool_for_monitorra   rd   r   r   r   _create_pool_for_serverr\   r_   r   r   is_writabler   update_is_writablerx   r  r   r~   r   r   rK  pop)r   r   r   r  rS   r   was_writables          r@   r   Topology._update_servers  s    
  ,,@@BHHJKGmm+..66')!66w?&*nn	 7  ''DLL,D";;t||4D')55g># $ 1 1"oo *0g&kkm##  $}}W5AAMM57g&2>>1--055HHXXX= K@  $DMM$7$7$9:OG$$//88lln$$x''..v?!!'*  ; $ Y %sE   DIIA	I5II
AII2I3AI
IIc                r    U R                   R                  XR                   R                  U R                  S9$ )N)	client_id)rm   
pool_classpool_optionsr\   r  s     r@   r   Topology._create_pool_for_server  s3    ~~((^^00D<M<M ) 
 	
r   c                :   U R                   R                  n[        UR                  UR                  UR                  UR
                  UR                  UR                  UR                  SUR                  S9	nU R                   R                  XSU R                  S9$ )NF)	connect_timeoutsocket_timeoutssl_contexttls_allow_invalid_hostnamesevent_listenersappnamedriverpause_enabled
server_api)	handshaker  )rm   r  r'   r  _ssl_contextr  r^   r  r  r  r  r\   )r   r   optionsmonitor_pool_optionss       r@   r  !Topology._create_pool_for_monitor  s    ..--
  +#33"22,,(/(K(K#44OO>>))
 
 ~~((UdFWFW ) 
 	
r   c                  ^ U R                   R                  [        R                  [        R                  4;   nU(       a  SnO-U R                   R                  [        R
                  :X  a  SnOSnU R                   R                  (       a  U[        L a  U(       a  gSU-  $ SU SU S3$ [        U R                   R                  5       5      n[        U R                   R                  5       R                  5       5      nU(       d2  U(       a&  S	R                  UU R                  R                  5      $ S
U-  $ US   R                  m[        U4S jUSS  5       5      nU(       aH  Tc  SU-  $ U(       a.  [!        U5      R#                  U R$                  5      (       d  SU-  $ ['        T5      $ SR)                  S U 5       5      $ )zUFormat an error message if server selection fails.

Hold the lock when calling this.
zreplica set membersmongosesr   zNo primary available for writeszNo %s available for writeszNo z match selector ""z)No {} available for replica set name "{}"zNo %s availabler   c              3  @   >#    U  H  oR                   T:H  v   M     g 7frR   r  ).0r   rt  s     r@   	<genexpr>*Topology._error_message.<locals>.<genexpr>	  s     G;||u,;s   rN   NzNo %s found yetz\Could not reach any servers in %s. Replica set is configured with internal hostnames or IPs?,c              3  p   #    U  H,  oR                   (       d  M  [        UR                   5      v   M.     g 7frR   )rt  str)r  r   s     r@   r  r    s#     Xf<< 1FLL 1 1s   66)rq   r   r/   r  r  Shardedr;  r-   rx   ry   r   formatrm   rp   rt  allr  intersectionrz   r  rQ  )r   r   is_replica_setserver_plural	addressesr   samert  s          @r@   r   Topology._error_message  s   
 **88//--=
 

 1M,,0E0EE&M%M**33!<7-GG]O+<XJaHHT..BBDEI4,,@@BIIKLG!FMM%77 
 -}<< AJ$$EG712;GGD=,}<<!#i.*E*EdFZFZ*[*[FHQR
 5z!xxXXXXr   c                R  #    / n U R                   (       a<  UR                  U R                   R                  5       5        U R                   (       a  M<  [        R
                  " U Vs/ s H  o"R                  5       PM     snSS06I S h  vN   g ! [         a     NIf = fs  snf  N7f)Nreturn_exceptionsT)r   r   r  
IndexErrorasynciogatherrQ  )r   tasksts      r@   r   Topology.cleanup_monitors  s     	%%T004467 %%% nn7Avvx7P4PPP  		7PsA   B'AB B''B  
B'
B%B'
BB'B	B'c                    SnU R                   (       d  SnSU R                  R                   SU U R                  < S3$ )N zCLOSED < >)r{   	__class____name__rq   )r   msgs     r@   __repr__Topology.__repr__#  s>    ||C4>>**+1SE$2C2C1FaHHr   c                    U R                   n[        [        UR                  5      5      UR                  UR
                  UR                  4$ )zDThe properties to use for AsyncMongoClient/Topology equality checks.)rm   tuplesortedru   rp   r   srv_service_name)r   tss     r@   eq_propsTopology.eq_props)  s8    ^^fRXX&')<)<bggrGZGZ[[r   c                    [        XR                  5      (       a!  U R                  5       UR                  5       :H  $ [        $ rR   )rv  r  r  NotImplemented)r   others     r@   __eq__Topology.__eq__.  s/    e^^,,==?enn&666r   c                4    [        U R                  5       5      $ rR   )hashr  r"  s    r@   __hash__Topology.__hash__3  s    DMMO$$r   )__events_executorr|   r   rq   rd   r_   r}   r   r   r{   r   ra   rc   rz   r   r   rm   r   r\   )r   r4   )rZ   None)rZ   float)NNN)r    Callable[[Selection], Selection]r   r  r   Optional[float]r   Optional[_Address]r   Optional[int]rZ   list[Server])r   r  r   r  r   r  r   r  r   r  rZ   list[ServerDescription])NNNN)r   r  r   r  r   r  r   r  r   Optional[list[Server]]r   r  rZ   r   )NN)
r   r6   r   r  r   r  r   r  rZ   r   )FF)r   r(   r   r[   r   r[   rZ   r  )r  zlist[tuple[str, Any]]rZ   r  )r   r6   rZ   zOptional[Server])r   r6   rZ   r[   )rZ   r  )r   r  rZ   set[_Address])rZ   r  )rZ   zOptional[ClusterTime])r0  Optional[Mapping[str, Any]]rZ   r  )   )r7  r   rZ   r  )rZ   r  )rZ   r0   )rZ   zlist[_ServerSession])r\  r  rZ   r   )r`  r   rZ   r  )rZ   r)   )r   r6   ry  rC  rZ   r[   )r   r6   ry  rC  rZ   r  )r   r6   rZ   r   )r   r  rZ   r  )rZ   r  )rZ   z>tuple[tuple[_Address, ...], Optional[str], Optional[str], str])r  objectrZ   r[   )rZ   r   )3r  
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r  r  r  r  r   r  r  r  r#  r'  r+  r1  r4  r8  r=  rH  r   propertyr   rX  r[  r_  r  r   r|  r  rD  r   r   r  r  r   r   r  r  r  r  __static_attributes__ r   r@   rC   rC   h   s   4j4X%(N 59&*&*'2' ' #2	'
 $' $' 
'RJ#2J# J# 	J#
 $J# $J# 
!J#` 59&*8<&*2  #2	
 $  6 $ 
2 59&*8<&* 2    #2	 
 $   6  $  
 L 37&*

 
 #0	

 $
 

H !&+	I%-I% I%  $	I%
 
I%\ !&+	b-b b  $	b
 
b(89*(NP8P	PNL&6=?2"A<F ! !,NAF BB(B+H7#
*+X


,8YtQI\

%r   rC   c                  6    \ rS rSrSr          SS jrSrg)rC  i7  z.An error with context for SDAM error handling.c                @    Xl         X l        X0l        X@l        XPl        g rR   )rt  r  rq  r  rr  )r   rt  r  rq  r  rr  s         r@   r   _ErrorContext.__init__:  s      
 0.#6 $r   )r  rt  r  rr  rq  N)
rt  BaseExceptionr  r   rq  r   r  r[   rr  zOptional[ObjectId])r  r  r  r  r  r   r  r  r   r@   rC  rC  7  s:    8%% % 	%
 "% '%r   rC  c                @    U b  Uc  gU S   US   :w  a  gU S   US   :  $ )z9Return True if the error's topologyVersion is <= current.F	processIdcounterr  )
current_tvr{  s     r@   rx  rx  I  s<     X-+(;"77i HY$777r   c                n    U R                   UR                   p2Ub  Uc  gUS   US   :w  a  gUS   US   :  $ )z4Return True if the new topologyVersion is < current.Fr  r  )rs  )
current_sdnew_sdr  new_tvs       r@   r   r   T  sL    #44f6M6MV^+&"55i 6)#444r   c                f    U(       d  U $ U  Vs/ s H  o"U;  d  M
  UPM     nnU=(       d    U $ s  snf )zBFilter out deprioritized servers from a list of server candidates.r  )
candidatesr   r   filtereds       r@   r   r   ^  s:     !%/WZ6AV3VZHW !z! Xs   	..)r;   z"weakref.ReferenceType[queue.Queue]rZ   r[   )r  r  r{  r  rZ   r[   )r  r(   r  r(   rZ   r[   rR   )r
  r  r   r  rZ   r  )\r  
__future__r   r  rh   r   r9   r   r   r   r   r   pathlibr   typingr   r   r   r   r	   r
   pymongor   r   r   r   #pymongo.asynchronous.client_sessionr   r   pymongo.asynchronous.monitorr   r   pymongo.asynchronous.poolr   pymongo.asynchronous.serverr   pymongo.errorsr   r   r   r   r   r   r   r   r   pymongo.hellor   pymongo.lockr   r    r!   pymongo.loggerr"   r#   r$   r%   r&   pymongo.pool_optionsr'   pymongo.server_descriptionr(   pymongo.server_selectorsr)   r*   r+   r,   r-   pymongo.topology_descriptionr.   r/   r0   r1   r2   bsonr3   pymongo.asynchronous.settingsr4   pymongo.typingsr5   r6   r~   r  __file__parentr   rA   rC   rC  rx  r   r   r  r   r@   <module>r!     s   C "   	   
     H H D D R @ * .
 
 
   
  - 8   >54>(()"L% L%^% %$8+87R8	85 OS
"
"5K
"
"r   