
    Ii                    Z   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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  SSKJrJrJrJrJrJrJrJ r   SSK!J"r"  SS	K#J$r$J%r%  SS
K&J'r'J(r(J)r)J*r*J+r+  SSK,J-r-  SSK.J/r/  SSK0J1r1J2r2J3r3J4r4J5r5  SSK6J7r7  SSK8J9r9J:r:  SSK;J<r<  SSK=J>r>  SSK?J@r@  SSKAJBrBJCrCJDrDJErEJFrF  \(       a  SSKGJHrH  SSKIJJrJ  SSKKJLrLJMrM  SrN\O" \" \P5      R                  5      rRS S jrS " S S5      rT " S S5      rU      S!S jrVS"S jrW S#     S$S jjrXg)%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)ConnectionFailureInvalidOperationNetworkTimeoutNotPrimaryErrorOperationFailurePyMongoErrorServerSelectionTimeoutError
WriteError)Hello)_create_lock_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)periodic_executor)_ServerSession_ServerSessionPool)
SrvMonitor)Pool)Server)SRV_POLLING_TOPOLOGIESTOPOLOGY_TYPETopologyDescription)_updated_topology_description_srv_pollingupdated_topology_description)ObjectId)TopologySettings)ClusterTime_AddressTc                    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        B/venv/lib/python3.13/site-packages/pymongo/synchronous/topology.pyprocess_events_queuer>   R   sT    A
	LLNE HBI  {{ 	
 	s   - AAc                     \ rS rSrSrS0S jrS1S jrS2S jr   S3           S4S jjr            S5S jr	    S6             S7S	 jjr
    S6             S7S
 jjr  S8         S9S 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S@S jrSAS jrSAS jrSBS jrSCS jrSCS jrSDSES jjrSFS jrS1S jrS1S jr\SGS j5       rSHS jr SIS jr!SJS  jr"SKS! jr#S1S" jr$SLS# jr%SMS$ jr&SMS% jr'S1S& jr(S1S' jr)SNS( jr*SNS) jr+SOS* jr,SPS+ jr-SQS, jr.SRS- jr/SSS. jr0S/r1g)TTopologyc   z*Monitor a topology of one or more servers.c           	     
  ^ 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'  [!        [        U R                   ["        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  [!        [        U R                   UU R4                  ["        R<                  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  [!        [        U R                   US   US   ["        RB                  S9  M     [E        URG                  5       5      U l$        SU l%        SU l&        [O        5       n[Q        U5      U l)        U R*                  RU                  U5      U l+        0 U l,        S U l-        S U l.        [_        5       U l0        U R
                  (       d  U R                  (       a  U R                  c   eSU4S	 jjn[b        Rd                  " [f        Rh                  [f        Rj                  US
S9n[l        Rn                  " U R                  URp                  5      mXpl	        URs                  5         S U l:        U R*                  Rv                  b7  U R*                  Rx                  (       d  [{        X R*                  5      U l:        g g g )Nd   )maxsize
topologyIdmessagerF   previousDescriptionnewDescriptionrG   r      )rF   
serverHost
serverPortrG   Fc                    > [        T 5      $ N)r>   )weaks   r=   target!Topology.__init__.<locals>.target   s    +D11    pymongo_events_thread)intervalmin_intervalrQ   name)returnbool)>_topology_id_pool_options_event_listeners
_listenersenabled_for_server_publish_serverenabled_for_topology_publish_tp_events_Topology__events_executorr6   Queuer   isEnabledForloggingDEBUGr   r   START_TOPOLOGYputpublish_topology_opened	_settingsr-   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   r   _lockcondition_class
_condition_servers_pid_max_cluster_timer'   _session_poolr%   PeriodicExecutorr   EVENTS_QUEUE_FREQUENCYMIN_HEARTBEAT_INTERVALweakrefrefcloseopen_srv_monitorfqdnload_balancedr(   )	selftopology_settingstopology_description
initial_tdseedr{   rQ   executorrP   s	           @r=   __init__Topology.__init__f   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5\
..88?02#'	8</14#3#3<<+++2 )9966#::,	H ;;t||X^^<D%-"MMO >>*4>>3O3O *4 @D 4P*rS   c                F   [         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                     U R                  R                  5        H  nUR                  5         M     U R                  R                  5         SSS5        U R                     U R                  5         SSS5        g! , (       d  f       N4= f! , (       d  f       g= 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, MongoClient must be created after any
  forking.

N   )      skip_file_prefixes
stacklevel   )zMongoClient opened before fork. May not be entirely fork-safe, proceed with caution. See PyMongo's documentation for details: https://www.mongodb.com/docs/languages/python/pymongo-driver/current/faq/#is-pymongo-fork-safe-)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)  "mm224FLLN 5 ""((*  ZZ! Z  Zs   AD'D
D
D c                b    [         R                  " 5       nUc  U R                  R                  $ U$ rO   )r   	remainingrk   server_selection_timeout)r   timeouts     r=   get_server_selection_timeout%Topology.get_server_selection_timeout   s(    //#?>>:::rS   Nc           
         Uc  U R                  5       nOUnU R                     U R                  XX%U5      nU Vs/ s H,  n[        [        U R                  UR                  5      5      PM.     snsSSS5        $ s  snf ! , (       d  f       g= 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{   _select_servers_loopr
   r*   get_server_by_addressaddress)	r   selector	operationr   r   operation_idserver_timeoutrw   sds	            r=   select_serversTopology.select_servers   s    . $+!>>@N5NZZ"&";";)7#
 PcObVT77

CDOb Z
 Zs   A?3A:.A?:A??
Bc                x   [         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(       dk  [        [        [        R&                  UUUU R                  U R                  R                  R                  [)        U[         R                  " 5       -
  5      S9  S	nU R+                  5         U R-                  5         U R.                  R1                  [2        R4                  5        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	$ )
z7select_servers() guts. Hold the lock when calling this.F)rG   r   r   operationIdtopologyDescriptionclientId)custom_selectorr   )rG   r   r   r   r   r   failurez, Timeout: zs, Topology Description: )rG   r   r   r   r   r   remainingTimeMST)time	monotonicr   re   rf   rg   r   r   STARTEDdescription_topology_settingsrZ   ro   apply_selectorrk   server_selectorFAILED_error_messager   WAITINGintr   _request_check_allr}   waitr   r   check_compatible)
r   r   r   r   r   r   nowend_timelogged_waitingrw   s
             r=   r   Topology._select_servers_loop#  s:    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>>3C(C$D	 "&!##% OO  !>!>?..0.."C"&"3"3"B"B4>>3Q3Q #C #S &%Z 	**,""rS   c                    U R                  XX4U5      n[        Xu5      n[        U5      S:X  a  US   $ [        R                  " US5      u  pUR
                  R                  U	R
                  R                  ::  a  U$ U	$ )NrK   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_servero  sw     %%!9L
 "'Aw<11:!==!4<<''7<<+G+GGNNrS   c                   U R                  UUUUUUS9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$ )zALike select_servers, but choose a random server if several match.r   r   rK   )rG   r   r   r   r   r   rL   rM   )r   r   get_timeoutset_rttr   min_round_trip_timer   re   rf   rg   r   r   	SUCCEEDEDr   rZ   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
 rS   c                0    U R                  [        UUUUS9$ )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   )r   r!   )r   r   r   r   r   s        r=   select_server_by_address!Topology.select_server_by_address  s+    2 !!$% " 
 	
rS   c                ,   U R                   nUR                  UR                     n[        XQ5      (       a  g[	        U R                   U5      nUR
                  (       d/  UR                  (       ad  UR                  [        R                  :X  aF  U R                  R                  UR                  5      nU(       a  UR                  R                  5         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         U R+                  UR,                  5        U R.                  (       a]  U(       dV  U R                  c   eU R                  R!                  U R"                  R0                  X@R                   U R&                  445        [2        R4                  " [6        R8                  5      (       a:  U(       d3  [;        [2        U R&                  UU R                   [<        R>                  S9  U R@                  (       aV  UR                  [        RB                  :X  a8  U R                   R                  [D        ;  a  U R@                  RG                  5         U(       aE  U R                  R                  UR                  5      nU(       a  UR                  RI                  US9  U RJ                  RM                  5         g)zYProcess a new ServerDescription on an opened topology.

Hold the lock when calling this.
NrH   )interrupt_connections)'ro   _server_descriptionsr   _is_stale_server_descriptionr/   is_readableis_server_type_knowntopology_typer,   Singler~   getr   readyr_   rb   ri   r]   "publish_server_description_changedrZ   _update_servers_receive_cluster_time_no_lockcluster_timera   rq   r   re   rf   rg   r   r   rr   r   rp   r+   r   r   r}   
notify_all)	r   server_description
reset_poolr   td_oldsd_oldnew_tdr   suppress_events	            r=   _process_changeTopology._process_change  sH    "",,-?-G-GH'CC-d.?.?AST))338L8LP]PdPd8d]]&&'9'A'ABF!!#5<<+++LLOOFF1C1K1KTM^M^_ #**+=+J+JKN<<+++LLOOHH..0A0AB $$W]]33N,,$*#00*::   M$9$99!!//7MM##% ]]&&'9'A'ABF!!8M!N 	""$rS   c                    U R                      U R                  (       a<  U R                  R                  UR                  5      (       a  U R                  XU5        SSS5        g! , (       d  f       g= f)z>Process a new ServerDescription after an hello call completes.N)r{   ry   ro   
has_serverr   r   )r   r   r   r   s       r=   	on_changeTopology.on_change  sM     ZZ || 1 1 < <=O=W=W X X$$%7EZ[ ZZs   AA$$
A2c                $   U R                   nUR                  [        ;  a  g[        U R                   U5      U l         U R	                  5         U R
                  (       aV  U R                  c   eU R                  R                  U R                  R                  X R                   U R                  445        [        R                  " [        R                  5      (       a4  [        [        U R                  UU R                   [         R"                  S9  gg)zOProcess a new seedlist on an opened topology.
Hold the lock when calling this.
NrH   )ro   r   r+   r.   r   ra   rb   ri   r]   rq   rZ   r   re   rf   rg   r   r   rr   )r   seedlistr   s      r=   _process_srv_updateTopology._process_srv_update%  s     ""'==EdFWFWYab<<+++LLOOHH..0A0AB $$W]]33,,$*#00*:: 4rS   c                    U R                      U R                  (       a  U R                  U5        SSS5        g! , (       d  f       g= f)z?Process a new list of nodes obtained from scanning SRV records.N)r{   ry   r  )r   r  s     r=   on_srv_updateTopology.on_srv_updateA  s*     ZZ||((2 ZZs	   #9
A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_addressH  s     }}  ))rS   c                    XR                   ;   $ rO   )r~   r  s     r=   r  Topology.has_serverR  s    --''rS   c                   U R                      U R                  R                  nU[        R                  :w  a
   SSS5        g[        U R                  5       5      S   R                  sSSS5        $ ! , (       d  f       g= f)z!Return primary's address or None.Nr   )r{   ro   r   r,   ReplicaSetWithPrimaryr$   _new_selectionr   )r   r   s     r=   get_primaryTopology.get_primaryU  s\     ZZ --;;M C CC Z
 ,D,?,?,AB1EMM ZZs   ,A1%A11
A?c                f   U R                      U R                  R                  nU[        R                  [        R
                  4;  a  [        5       sSSS5        $ [        U" U R                  5       5      5       Vs1 s H  o3R                  iM     snsSSS5        $ s  snf ! , (       d  f       g= f)z+Return set of replica set member addresses.N)
r{   ro   r   r,   r  ReplicaSetNoPrimarysetiterr  r   )r   r   r   r   s       r=   _get_replica_set_members!Topology._get_replica_set_members_  s     ZZ --;;M3311%  u Z *.ht7J7J7L.M)NO)N2JJ)NO Z P Zs$   AB"!B"<BB"B""
B0c                ,    U R                  [        5      $ )z"Return set of secondary addresses.)r  r#   r   s    r=   get_secondariesTopology.get_secondariesl  s    ,,-FGGrS   c                ,    U R                  [        5      $ )z Return set of arbiter addresses.)r  r"   r  s    r=   get_arbitersTopology.get_arbitersp  s    ,,-DEErS   c                    U R                   $ )z1Return a document, the highest seen $clusterTime.r   r  s    r=   max_cluster_timeTopology.max_cluster_timet  s    %%%rS   c                p    U(       a/  U R                   (       a  US   U R                   S   :  a  Xl         g g g )NclusterTimer%  r   r   s     r=   r   &Topology._receive_cluster_time_no_lockx  s;      **.1G1G1VV)5& W	 rS   c                r    U R                      U R                  U5        S S S 5        g ! , (       d  f       g = frO   )r{   r   r*  s     r=   receive_cluster_timeTopology.receive_cluster_time  s!    ZZ..|< ZZ   (
6c                    U R                      U R                  5         U R                  R                  U5        SSS5        g! , (       d  f       g= 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  s1    ZZ##%OO  + ZZs   ,A
A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.
)ro   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1rS   c           	        / nU R                      U R                  5        HP  nU R                  UR                     nUR	                  X3R
                  R                  R                  5       45        MR     S S S 5        U H!  u  p4 UR
                  R                  U5        M#     g ! , (       d  f       N6= f! [         a;  n[        USUSS 5      nU R                  UR                  R                  U5        e S nAff = f)Nr   F)r{   r7  r~   r   appendr   genget_overallremove_stale_socketsr   _ErrorContexthandle_errorr   )r   r   r   r   
generationexcctxs          r=   update_poolTopology.update_pool  s    ZZ//1rzz2(C(C(EFG 2  #*F00< #* Z   #CJtD!!&"4"4"<"<cBs$   A%B$B5$
B25
C:?6C55C:c                ^   U R                      U R                  nU R                  R                  5        H  nUR	                  5         M     U R                  R                  5       U l        U R                  R                  5       R                  5        H)  u  p4X0R                  ;   d  M  X@R                  U   l        M+     U R                  (       a  U R                  R	                  5         SU l
        SU l        SSS5        U R                  (       Ga  U R                  c   e[        [        R                   0 U R                  R"                  U R                  R$                  U R                  R&                  U R                  R(                  5      U l        U R                  R+                  U R,                  R.                  WU R                  U R0                  445        U R                  R+                  U R,                  R2                  U R0                  445        [4        R6                  " [8        R:                  5      (       aZ  [=        [4        U R0                  WU R                  [>        R@                  S9  [=        [4        U R0                  [>        RB                  S9  U RD                  (       d  U R                  (       a_  U RF                  R	                  5         U RF                  RI                  S5        [K        [L        RN                  " U R                  5      5        gg! , (       d  f       GN,= f)zClear pools and terminate monitors. Topology does not reopen on
demand. Any further operations will raise
:exc:`~.errors.InvalidOperation`.
FTNrH   rE   rK   )(r{   ro   r~   r   r   r   rw   itemsr   r   ry   rz   ra   rb   r-   r,   rp   rn   max_set_versionmax_election_idr   ri   r]   rq   rZ   publish_topology_closedr   re   rf   rg   r   r   rr   STOP_TOPOLOGYr_   rc   joinr>   r   r   )r   old_tdr   r   r   s        r=   r   Topology.close  sT   
 ZZ&&F--..0 1 !% 1 1 7 7 9D#00DDFLLNmm+9;MM'*6  O
   !!'') 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*:: ):):DVDdDd 4#3#3""((*""''* T\\!:;	 $4i Zs   BL(AL
L,c                    U R                   $ rO   )ro   r  s    r=   r   Topology.description  s       rS   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    !!))++rS   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=   rU  Topology.get_server_session  s    !!445LMMrS   c                :    U R                   R                  U5        g rO   )r   return_server_session)r   server_sessions     r=   rY  Topology.return_server_session  s    00@rS   c                B    [         R                  " U R                  5      $ )z]A Selection object, initially including all known servers.

Hold the lock when calling this.
)r    from_topology_descriptionro   r  s    r=   r  Topology._new_selection  s    
 2243D3DEErS   c                   U R                   (       a  [        S5      eU R                  (       d  SU l        U R                  5         U R                  (       d  U R
                  (       a  U R                  R                  5         U R                  (       a8  U R                  R                  [        ;   a  U R                  R                  5         U R                  R                  (       a?  U R                  [        U R                   S   [#        SU R$                  SS.5      5      5        U R&                  R)                  5        H  nUR                  5         M     g)zKStart monitors, or restart after a fork.

Hold the lock when calling this.
z"Cannot use MongoClient after closeTr   rK      )ok	serviceIdmaxWireVersionN)rz   r   ry   r   ra   r_   rc   r   r   r   r   r+   rk   r   r   r   rx   r   rZ   r~   r   r   r   s     r=   r   Topology._ensure_opened   s    
 <<"#GHH||DL  " 4#7#7&&++-   d&6&6&D&DH^&^!!&&(~~++$$%,,Q/QT5F5FZ\]^ mm**,FKKM -rS   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
isinstancerg  dict _is_stale_error_topology_version)r   r   err_ctxr   cur_tvrn  error_tvs          r=   _is_stale_errorTopology._is_stale_error!  s    ""7+><<(()@)@'BTBTUU ##44WUI..%--.. ==,,->?/AArS   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        U(       d  UR*                  S::  a  UR-                  U5        UR/                  5         g UR                  (       dE  U R                  R
                  (       d  U R'                  [)        XS95        UR-                  U5        g g [        U[0        5      (       a_  U R                  R
                  (       d  U R'                  [)        XS95        UR-                  U5        UR2                  R5                  5         g g )Ncodei{'  rn     )rv  r~   rn  rl  rk   r   completed_handshakerp  r   r   r   r   ro  ry  rg  r   r   _NOT_PRIMARY_CODES_SHUTDOWN_CODESr   r   max_wire_versionr   request_checkr   _monitorcancel_check)	r   r   rs  r   rn  rl  err_codedefaultis_shutting_downs	            r=   _handle_errorTopology._handle_error5  s   11w'''
 >>''
7C^C^e^,,1L1L
 z**1ABCC uf%% :: $.e_#E#E%4 ==,,VW=><<<#+~/M/M#M ~~33(():7)PQ#(@(@A(ELL,$$&00~~33(():7)PQZ( 1 011 >>//$$%6w%LMLL$ OO((* 2rS   c                r    U R                      U R                  X5        SSS5        g! , (       d  f       g= 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   rs  s      r=   r?  Topology.handle_errorw  s#     ZZw0 ZZr/  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  rd  s     r=   r   Topology._request_check_all  s%    mm**,F  " -rS   c           
        U R                   R                  5       R                  5        GHU  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         M  U R                  U   R                   R"                  nX R                  U   l        XbR"                  :w  d  GM#  U R                  U   R$                  R'                  UR"                  5        GMX     [)        U R                  R                  5       5       HR  u  pU R                   R+                  U5      (       a  M'  UR-                  5         U R                  R/                  U5        MT     g)zbSync our Servers from TopologyDescription.server_descriptions.

Hold the lock while calling this.
)r   topologyr   r   N)r   r   monitortopology_id	listenersevents)ro   rw   rF  r~   rk   monitor_class_create_pool_for_monitorr_   rb   r   r   r*   _create_pool_for_serverrZ   r]   r   r   is_writabler   update_is_writablerv   r  r   pop)r   r   r   r  rP   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&  $}}W5AAMM57g&2>>1MM'*//BB2>>R= K@  $DMM$7$7$9:OG$$//88!!'*  ;rS   c                r    U R                   R                  XR                   R                  U R                  S9$ )N)	client_id)rk   
pool_classpool_optionsrZ   r  s     r=   r   Topology._create_pool_for_server  s3    ~~((^^00D<M<M ) 
 	
rS   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  )rk   r  r   r  _ssl_contextr  r\   r  r  r  r  rZ   )r   r   optionsmonitor_pool_optionss       r=   r  !Topology._create_pool_for_monitor  s    ..--
  +#33"22,,(/(K(K#44OO>>))
 
 ~~((UdFWFW ) 
 	
rS   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rO   rz  ).0r   rn  s     r=   	<genexpr>*Topology._error_message.<locals>.<genexpr>  s     G;||u,;s   rK   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rO   )rn  str)r  r   s     r=   r  r    s#     Xf<< 1FLL 1 1s   66)ro   r   r,   r  r  Shardedr5  r$   rv   rw   r   formatrk   rn   rn  allr  intersectionrx   r  rK  )r   r   is_replica_setserver_plural	addressesr   samern  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XrS   c                    SnU R                   (       d  SnSU R                  R                   SU U R                  < S3$ )N zCLOSED < >)ry   	__class____name__ro   )r   msgs     r=   __repr__Topology.__repr__  s>    ||C4>>**+1SE$2C2C1FaHHrS   c                    U R                   n[        [        UR                  5      5      UR                  UR
                  UR                  4$ )z?The properties to use for MongoClient/Topology equality checks.)rk   tuplesortedrs   rn   r   srv_service_name)r   tss     r=   eq_propsTopology.eq_props
  s8    ^^fRXX&')<)<bggrGZGZ[[rS   c                    [        XR                  5      (       a!  U R                  5       UR                  5       :H  $ [        $ rO   )rp  r  r  NotImplemented)r   others     r=   __eq__Topology.__eq__  s/    e^^,,==?enn&666rS   c                4    [        U R                  5       5      $ rO   )hashr  r  s    r=   __hash__Topology.__hash__  s    DMMO$$rS   )__events_executorrz   r}   ro   rb   r]   r{   r   ry   r   r_   ra   rx   r~   r   rk   r   rZ   )r   r1   )rX   None)rX   float)NNN)r    Callable[[Selection], Selection]r   r  r   Optional[float]r   Optional[_Address]r   Optional[int]rX   list[Server])r   r  r   r  r   r  r   r  r   r  rX   list[ServerDescription])NNNN)r   r  r   r  r   r  r   r  r   Optional[list[Server]]r   r  rX   r*   )NN)
r   r3   r   r  r   r  r   r  rX   r*   )FF)r   r   r   rY   r   rY   rX   r  )r  zlist[tuple[str, Any]]rX   r  )r   r3   rX   zOptional[Server])r   r3   rX   rY   )rX   r  )r   r  rX   set[_Address])rX   r  )rX   zOptional[ClusterTime])r   Optional[Mapping[str, Any]]rX   r  )   )r1  r   rX   r  )rX   r  )rX   r-   )rX   zlist[_ServerSession])rV  r  rX   r&   )rZ  r&   rX   r  )rX   r    )r   r3   rs  r>  rX   rY   )r   r3   rs  r>  rX   r  )r   r3   rX   r)   )r   r  rX   r  )rX   r  )rX   z>tuple[tuple[_Address, ...], Optional[str], Optional[str], str])r  objectrX   rY   )rX   r   )2r  
__module____qualname____firstlineno____doc__r   r   r   r   r   r   r   r   r   r  r  r  r   r  r  r  r  r"  r&  r   r-  r2  r7  rC  r   propertyr   rR  rU  rY  r  r   rv  r  r?  r   r   r  r  r   r  r  r  r  __static_attributes__ rS   r=   r@   r@   c   s   4fAP'"R 59&*&*#2# # #2	#
 $# $# 
#JJ#2J# J# 	J#
 $J# $J# 
!J#` 59&*8<&*2  #2	
 $  6 $ 
2 59&*8<&* 2    #2	 
 $   6  $  
 L 37&*

 
 #0	

 $
 

H !&+	H%-H% H%  $	H%
 
H%Z !&+	\-\ \  $	\
 
\(83*(NPHF&6=,2"=<~ ! !,NAFBB(@+D1#
(+T


,8YtI\

%rS   r@   c                  6    \ rS rSrSr          SS jrSrg)r>  i  z.An error with context for SDAM error handling.c                @    Xl         X l        X0l        X@l        XPl        g rO   )rn  r  rk  r|  rl  )r   rn  r  rk  r|  rl  s         r=   r   _ErrorContext.__init__  s      
 0.#6 $rS   )r|  rn  r  rl  rk  N)
rn  BaseExceptionr  r   rk  r   r|  rY   rl  zOptional[ObjectId])r  r  r  r  r  r   r  r  rS   r=   r>  r>    s:    8%% % 	%
 "% '%rS   r>  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_tvru  s     r=   rr  rr  *  s<     X-+(;"77i HY$777rS   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  )rm  )
current_sdnew_sdr  new_tvs       r=   r   r   5  sL    #44f6M6MV^+&"55i 6)#444rS   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   	..)r8   z"weakref.ReferenceType[queue.Queue]rX   rY   )r  r  ru  r  rX   rY   )r  r   r  r   rX   rY   rO   )r  r  r   r  rX   r  )Yr  
__future__r   rf   r   r6   r   r   r   r   r   pathlibr   typingr   r   r   r   r	   r
   pymongor   r   r   pymongo.errorsr   r   r   r   r   r   r   r   pymongo.hellor   pymongo.lockr   r   pymongo.loggerr   r   r   r   r   pymongo.pool_optionsr   pymongo.server_descriptionr   pymongo.server_selectorsr    r!   r"   r#   r$   pymongo.synchronousr%   "pymongo.synchronous.client_sessionr&   r'   pymongo.synchronous.monitorr(   pymongo.synchronous.poolr)   pymongo.synchronous.serverr*   pymongo.topology_descriptionr+   r,   r-   r.   r/   bsonr0   pymongo.synchronous.settingsr1   pymongo.typingsr2   r3   _IS_SYNCr  __file__parentr   r>   r@   r>  rr  r   r   r  rS   r=   <module>r     s   C "  	   
     H H 1 1	 	 	   ,  - 8  2 Q 2 ) -  =54>(()"r% r%j% %$8+87R8	85 OS
"
"5K
"
"rS   