
    @hj                    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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(J)r)  SS
K*J+r+J,r,J-r-J.r.J/r/  SSK0J1r1  SSK2J3r3  SSK4J5r5J6r6J7r7J8r8J9r9  SSK:J;r;J<r<  SSK=J>r>J?r?  SSK@JArA  SSKBJCrC  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)	ConnectionFailureInvalidOperationNetworkTimeoutNotPrimaryErrorOperationFailurePyMongoErrorServerSelectionTimeoutErrorWaitQueueTimeoutError
WriteError)Hello)
_cond_wait_create_condition_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)_ServerSession_ServerSessionPool)MonitorBase
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        aC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\pymongo/synchronous/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           	     \
  ^ 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      (       aE  [!        [        ["        R<                  U R                   [?        U5      [?        U R4                  5      S9  UR@                   H  nU R
                  (       aK  U R                  c   eU R                  R'                  U R                  RB                  X@R                   445        [        R                  " [        R                  5      (       d  M  [!        [        ["        RD                  U R                   US   US   S9  M     [G        URI                  5       5      U l%        SU l&        SU l'        [Q        5       U l)        [U        U RR                  [V        (       a  U R*                  RX                  OS 5      U l-        0 U l.        S U l/        S U l0        [c        5       U l2        U R
                  (       d  U R                  (       a  U R                  c   eSU4S	 jjn[f        Rh                  " [j        Rl                  [j        Rn                  US
S9n[p        Rr                  " U R                  URt                  5      mX`l	        URw                  5         S U l<        U R*                  Rz                  b5  U R*                  R|                  (       d  [        X R*                  5      U l<        / U l@        g )Nd   )maxsizemessage
topologyIdrI   rJ   previousDescriptionnewDescriptionr      )rI   rJ   
serverHost
serverPortFc                    > [        T 5      $ N)rA   )weaks   r@   target!Topology.__init__.<locals>.target   s    +D11    pymongo_events_thread)intervalmin_intervalrT   name)returnbool)A_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repr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   PeriodicExecutorr   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*::,,$($4#D$5$56 &++D##||///  $//"G"G$PaPaIb!cd((77 .;;#00#Aw#Aw ,  $$8$L$L$NO!^
+JJ((66
 13#'	8</14#3#3<<+++2 )9966#::,	H ;;t||X^^<D%-"MMO >>*4>>3O3O *4 @D 24rV   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://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 	 "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$ rR   )r   	remainingrn   server_selection_timeout)r   timeouts     r@   get_server_selection_timeout%Topology.get_server_selection_timeout   s(    //#?>>:::rV   Nc           
     x   Uc  U R                  5       nOUn[        (       d!  U R                  (       a  U R                  5         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   r   cleanup_monitorsr   _select_servers_loopr
   r-   get_server_by_addressaddress)	r   selector	operationr   r   operation_idserver_timeoutr{   sds	            r@   select_serversTopology.select_servers  s    . $+!>>@N5N xD//!!#ZZ"&";";)7#
 PcObVT77

CDOb Z
 Zs   B+'3B&B+&B++
B9c                t   [         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         U R-                  5         [/        U R0                  [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)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   remainingTimeMST)time	monotonicr   rh   ri   rj   r   r    STARTEDdescription_topology_settingsr]   rr   apply_selectorrn   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_waitingr{   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>>;K0K(L$M	 "&!##% t(E(EF..0.."C"&"3"3"B"B4>>3Q3Q #C #S &%Z 	**,""rV   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	$ )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  sw     %%!9L
 "'Aw<11:!==!4<<''7<<+G+GGNNrV   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   rN   )rI   r   r   r   r   r   rO   rP   )r   r   get_timeoutset_rttr   min_round_trip_timer   rh   ri   rj   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
 rV   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 !!$% " 
 	
rV   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*                  (       a]  U(       dV  U R                  c   eU R                  R!                  U R"                  R,                  X@R                   U R&                  445        [.        R0                  " [2        R4                  5      (       aL  U(       dE  [7        [.        [8        R:                  U R&                  [=        U5      [=        U R                   5      S9  U R>                  (       a  UR                  [        R@                  :X  ah  U R                   R                  [B        ;  aJ  U R>                  RE                  5         [F        (       d%  U RH                  RK                  U R>                  5        U RL                  RO                  5         g)zYProcess a new ServerDescription on an opened topology.

Hold the lock when calling this.
NrK   )(rr   _server_descriptionsr   _is_stale_server_descriptionr2   is_readableis_server_type_knowntopology_typer/   Singler   getr   readyrb   re   rl   r`   "publish_server_description_changedr]   _update_serversrd   rt   r   rh   ri   rj   r   r   ru   rv   r   rs   r.   r   r   r   appendr   
notify_all)	r   server_description
reset_poolinterrupt_connections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!!#5<<+++LLOOFF1C1K1KTM^M^_ #N<<+++LLOOHH..0A0AB $$W]]33N*::,,$(L#D$5$56   M$9$99!!//7MM##%8##**4+<+<= 	""$rV   c                   U R                      U R                  (       a<  U R                  R                  UR                  5      (       a  U R                  XU5        SSS5        U(       aG  U R                  R                  UR                  5      nU(       a  UR                  R                  US9  ggg! , (       d  f       N]= f)z>Process a new ServerDescription after an hello call completes.N)r   )
r   r}   rr   
has_serverr   r  r   r   r   r   )r   r   r   r   r   s        r@   	on_changeTopology.on_change  s     ZZ || 1 1 < <=O=W=W X X$$%7EZ[  ]]&&'9'A'ABF!!8M!N   Zs   AB22
C c           	     H   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      (       aF  [        [        [         R"                  U R                  [%        U5      [%        U R                   5      S9  gg)zOProcess a new seedlist on an opened topology.
Hold the lock when calling this.
NrK   )rr   r   r.   r1   r   rd   re   rl   r`   rt   r]   r   rh   ri   rj   r   r   ru   rv   )r   seedlistr  s      r@   _process_srv_updateTopology._process_srv_update0  s     ""'==EdFWFWYab<<+++LLOOHH..0A0AB $$W]]33*::,,$(L#D$5$56 4rV   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   r}   r  )r   r  s     r@   on_srv_updateTopology.on_srv_updateL  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_addressS  s     }}  ))rV   c                    XR                   ;   $ rR   )r   r  s     r@   r  Topology.has_server]  s    --''rV   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   rr   r   r/   ReplicaSetWithPrimaryr'   _new_selectionr   )r   r   s     r@   get_primaryTopology.get_primary`  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   rr   r   r/   r  ReplicaSetNoPrimarysetiterr  r   )r   r   r   r   s       r@   _get_replica_set_members!Topology._get_replica_set_membersj  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_secondariesw  s    ,,-FGGrV   c                ,    U R                  [        5      $ )z Return set of arbiter addresses.)r   r%   r#  s    r@   get_arbitersTopology.get_arbiters{  s    ,,-DEErV   c                    U R                   $ )z1Return a document, the highest seen $clusterTime.r   r#  s    r@   max_cluster_timeTopology.max_cluster_time  s    %%%rV   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	 rV   c                r    U R                      U R                  U5        S S S 5        g ! , (       d  f       g = frR   )r   r1  r/  s     r@   receive_cluster_timeTopology.receive_cluster_time  s!    ZZ..|< ZZ   (
6c                    U R                      U R                  5         [        U 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  s-    ZZ##%t	2 ZZs	   '=
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.
)rr   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1rV   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   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    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           	     F   U R                      U R                  nU R                  R                  5        HE  nUR	                  5         [
        (       a  M   U R                  R                  UR                  5        MG     U R                  R                  5       U l        U R                  R                  5       R                  5        H)  u  p4X0R                  ;   d  M  X@R                  U   l        M+     U R                  (       aJ  U R                  R	                  5         [
        (       d%  U R                  R                  U 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                  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      (       al  [E        [<        [F        RH                  U R8                  [K        W5      [K        U R                  5      S9  [E        [<        [F        RL                  U R8                  S9  U RN                  (       d  U R                   (       a_  U RP                  R	                  5         U RP                  RS                  S5        [U        [V        RX                  " 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`.
FTNrK   rH   rN   )-r   rr   r   r   r   r   r   r   _monitorr   r{   itemsr   r   r}   r~   rd   re   r0   r/   rs   rq   max_set_versionmax_election_idr   rl   r`   rt   r]   publish_topology_closedr   rh   ri   rj   r   r   ru   rv   STOP_TOPOLOGYrb   rf   joinrA   r   r   )r   old_tdr   r   r   s        r@   r   Topology.close  s   
 ZZ&&F--..0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*::,,$(L#D$5$56 &8&F&FSWSdSd 4#3#3""((*""''* T\\!:;	 $4q Zs   ANBNB N
N c                    U R                   $ rR   )rr   r#  s    r@   r   Topology.description  s       rV   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    !!))++rV   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MrV   c                :    U R                   R                  U5        g rR   )r   return_server_session)r   server_sessions     r@   r`  Topology.return_server_session  s    00@rV   c                B    [         R                  " U R                  5      $ )z]A Selection object, initially including all known servers.

Hold the lock when calling this.
)r#   from_topology_descriptionrr   r#  s    r@   r  Topology._new_selection  s    
 2243D3DEErV   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   rN      )ok	serviceIdmaxWireVersionN)r~   r   r}   r   rd   rb   rf   r   r   r   r   r.   rn   r   r  r"   r|   r   r]   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 -rV   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
isinstancern  dict _is_stale_error_topology_version)r   r   err_ctxr   cur_tvru  error_tvs          r@   _is_stale_errorTopology._is_stale_error0  s    ""7+><<(()@)@'BTBTUU ##44WUI..%--.. ==,,->?/AArV   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      (       au  [        U[2        5      (       a  g U R                  R
                  (       d  U R'                  [)        XS95        UR-                  U5        UR4                  R7                  5         g g )Ncodei{'  ru     )r}  r   ru  rs  rn   r   completed_handshakerw  r   r   r   r   rv  r  rn  r   r   _NOT_PRIMARY_CODES_SHUTDOWN_CODESr  r"   max_wire_versionr   request_checkr   r   rL  cancel_check)	r   r   rz  r   ru  rs  err_codedefaultis_shutting_downs	            r@   _handle_errorTopology._handle_errorD  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%!677 >>//$$%6w%LMLL$ OO((* 2rV   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   rz  s      r@   rE  Topology.handle_error  s#     ZZw0 ZZr6  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  rk  s     r@   r   Topology._request_check_all  s%    mm**,F  " -rV   c           
     Z   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       H  u  pU R                   R+                  U5      (       a  M'  UR-                  5         [.        (       d%  U R0                  R3                  UR4                  5        U R                  R7                  U5        M     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)rr   r{   rM  r   rn   monitor_class_create_pool_for_monitorrb   re   r   r   r-   _create_pool_for_serverr]   r`   r   r   is_writabler   update_is_writablerz   r  r   r   r   r   rL  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&  $}}W5AAMM57g&2>>1MM'*//BB2>>R= K@  $DMM$7$7$9:OG$$//88x''..v?!!'*  ;rV   c                r    U R                   R                  XR                   R                  U R                  S9$ )N)	client_id)rn   
pool_classpool_optionsr]   r  s     r@   r   Topology._create_pool_for_server  s3    ~~((^^00D<M<M ) 
 	
rV   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_apiT)is_sdamr  )rn   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>>))
 
 ~~((44CTCT ) 
 	
rV   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   ru  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   )ru  str)r  r   s     r@   r  r    s#     Xf<< 1FLL 1 1s   66)rr   r   r/   r  r  Shardedr<  r'   rz   r{   r   formatrn   rq   ru  allr  intersectionr|   r  rR  )r   r   is_replica_setserver_plural	addressesr   sameru  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XrV   c                6   / 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  g ! [         a     NAf = fs  snf )Nreturn_exceptionsT)r   r   r  
IndexErrorasynciogatherrR  )r   tasksts      r@   r   Topology.cleanup_monitors  sz    	%%T004467 %%% 	515a51JTJ  		1s   A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__rr   )r   msgs     r@   __repr__Topology.__repr__   s>    ||C4>>**+1SE$2C2C1FaHHrV   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.)rn   tuplesortedrw   rq   r   srv_service_name)r   tss     r@   eq_propsTopology.eq_props&  s8    ^^fRXX&')<)<bggrGZGZ[[rV   c                    [        XR                  5      (       a!  U R                  5       UR                  5       :H  $ [        $ rR   )rw  r  r  NotImplemented)r   others     r@   __eq__Topology.__eq__+  s/    e^^,,==?enn&666rV   c                4    [        U R                  5       5      $ rR   )hashr  r#  s    r@   __hash__Topology.__hash__0  s    DMMO$$rV   )__events_executorr~   r   rr   re   r`   r   r   r   r}   r   rb   rd   r|   r   r   rn   r   r]   )r   r4   )r[   None)r[   float)NNN)r    Callable[[Selection], Selection]r   r  r   Optional[float]r   Optional[_Address]r   Optional[int]r[   list[Server])r   r  r   r  r   r  r   r  r   r  r[   list[ServerDescription])NNNN)r   r  r   r  r   r  r   r  r   Optional[list[Server]]r   r  r[   r-   )NN)
r   r6   r   r  r   r  r   r  r[   r-   )FF)r   r"   r   r\   r   r\   r[   r  )r  zlist[tuple[str, Any]]r[   r  )r   r6   r[   zOptional[Server])r   r6   r[   r\   )r[   r  )r   r  r[   set[_Address])r[   r  )r[   zOptional[ClusterTime])r0  Optional[Mapping[str, Any]]r[   r  )   )r8  r   r[   r  )r[   r  )r[   r0   )r[   zlist[_ServerSession])r]  r  r[   r(   )ra  r(   r[   r  )r[   r#   )r   r6   rz  rD  r[   r\   )r   r6   rz  rD  r[   r  )r   r6   r[   r,   )r   r  r[   r  )r[   r  )r[   z>tuple[tuple[_Address, ...], Optional[str], Optional[str], str])r  objectr[   r\   )r[   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  r9  r>  rI  r   propertyr   rY  r\  r`  r  r   r}  r  rE  r   r   r  r  r   r   r  r  r  r  __static_attributes__ rV   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 !&+	C%-C% C%  $	C%
 
C%P !&+	O-O O  $	O
 
O283*(NPHF&6=32"A<F ! !,NAFBB(B+H1#
*+X


,8YtKI\

%rV   rC   c                  6    \ rS rSrSr          SS jrSrg)rD  i4  z.An error with context for SDAM error handling.c                @    Xl         X l        X0l        X@l        XPl        g rR   )ru  r  rr  r  rs  )r   ru  r  rr  r  rs  s         r@   r   _ErrorContext.__init__7  s      
 0.#6 $rV   )r  ru  r  rs  rr  N)
ru  BaseExceptionr  r   rr  r   r  r\   rs  zOptional[ObjectId])r  r  r  r  r  r   r  r  rV   r@   rD  rD  4  s:    8%% % 	%
 "% '%rV   rD  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@   ry  ry  F  s<     X-+(;"77i HY$777rV   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  )rt  )
current_sdnew_sdr  new_tvs       r@   r   r   Q  sL    #44f6M6MV^+&"55i 6)#444rV   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]r[   r\   )r  r  r|  r  r[   r\   )r  r"   r  r"   r[   r\   rR   )r  r  r   r  r[   r  )\r  
__future__r   r  ri   r   r9   r   r   r   r   r   pathlibr   typingr   r   r   r   r	   r
   pymongor   r   r   r   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.synchronous.client_sessionr(   r)   pymongo.synchronous.monitorr*   r+   pymongo.synchronous.poolr,   pymongo.synchronous.serverr-   pymongo.topology_descriptionr.   r/   r0   r1   r2   bsonr3   pymongo.synchronous.settingsr4   pymongo.typingsr5   r6   r   r  __file__parentr   rA   rC   rD  ry  r   r   r  rV   r@   <module>r"     s   C "   	   
     H H D D
 
 
   
  - 8  R ? ) -  =54>(()"I% I%X% %$8+87R8	85 OS
"
"5K
"
"rV   