
    ng\jf0                     N   S SK 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  S SKJr  S SKJrJr  S SK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  S S
K J!r!  S SK"J#r#  S SK$J%r%  S SK&J'r'J(r(  S SK)J*r+  S SK,J-r-J.r.  S SK/J0r0  S SK1J2r2  S SK3J4r4   " S S\.5      r5 " S S\-\55      r6g)    )abstractmethod)iscoroutinefunction)datetime)Any	AwaitableCallableListOptionalUnion)RedisCluster)PipelinePubSub)AsyncDatabaseDatabase	Databases)AsyncActiveDatabaseChanged&CloseConnectionOnActiveDatabaseChangedRegisterCommandFailure"ResubscribeOnActiveDatabaseChanged)DEFAULT_FAILOVER_ATTEMPTSDEFAULT_FAILOVER_DELAYAsyncFailoverStrategyDefaultFailoverStrategyExecutorFailoverStrategyExecutor)AsyncFailureDetector)record_geo_failover)Retry)AsyncOnCommandsFailEventEventDispatcherInterface)State)BaseCommandExecutorCommandExecutor)DEFAULT_AUTO_FALLBACK_INTERVAL)GeoFailoverReason)KeyTc                      \ rS rSr\\S\4S j5       5       r\\S\\	   4S j5       5       r
\S\	SS4S j5       r\\S\\   4S j5       5       r\S	\S
\SS4S j5       r\\S\\   4S j5       5       r\R&                  \S\SS4S j5       5       r\\S\4S j5       5       r\\S\4S j5       5       r\S 5       r\S 5       r\S\4S j5       r\S\\/S4   4S j5       r\S\4S j5       r \S\!S\"4S j5       r#Sr$g)AsyncCommandExecutor!   returnc                     g)zReturns a list of databases.N selfs    a/home/edenadmin/noVNC/venv/lib/python3.13/site-packages/redis/asyncio/multidb/command_executor.py	databasesAsyncCommandExecutor.databases"        	    c                     g)z$Returns a list of failure detectors.Nr+   r,   s    r.   failure_detectors&AsyncCommandExecutor.failure_detectors(   r1   r2   failure_detectorNc                     g)z=Adds a new failure detector to the list of failure detectors.Nr+   r-   r6   s     r.   add_failure_detector)AsyncCommandExecutor.add_failure_detector.   s     	r2   c                     g)z"Returns currently active database.Nr+   r,   s    r.   active_database$AsyncCommandExecutor.active_database3   r1   r2   databasereasonc                    #    g7f)z{Sets the currently active database.

Args:
    database: The new active database.
    reason: The reason for the failover.
Nr+   )r-   r>   r?   s      r.   set_active_database(AsyncCommandExecutor.set_active_database9   s
      	   c                     g)z Returns currently active pubsub.Nr+   r,   s    r.   active_pubsub"AsyncCommandExecutor.active_pubsubE   r1   r2   pubsubc                     g)zSets currently active pubsub.Nr+   r-   rG   s     r.   rE   rF   K   r1   r2   c                     g)z#Returns failover strategy executor.Nr+   r,   s    r.   failover_strategy_executor/AsyncCommandExecutor.failover_strategy_executorQ   r1   r2   c                     g)zReturns command retry object.Nr+   r,   s    r.   command_retry"AsyncCommandExecutor.command_retryW   r1   r2   c                    #    g7f)z:Initializes a PubSub object on a currently active databaseNr+   r-   kwargss     r.   rG   AsyncCommandExecutor.pubsub]   
      	rC   c                    #    g7f)z*Executes a command and returns the result.Nr+   )r-   argsoptionss      r.   execute_command$AsyncCommandExecutor.execute_commandb   rT   rC   command_stackc                    #    g7f)z)Executes a stack of commands in pipeline.Nr+   )r-   rZ   s     r.   execute_pipeline%AsyncCommandExecutor.execute_pipelineg   rT   rC   transactionc                    #    g7f)z1Executes a transaction block wrapped in callback.Nr+   )r-   r^   watchesrW   s       r.   execute_transaction(AsyncCommandExecutor.execute_transactionl   s
     
 	rC   method_namec                    #    g7f)z*Executes a given method on active pub/sub.Nr+   )r-   rc   rV   rR   s       r.   execute_pubsub_method*AsyncCommandExecutor.execute_pubsub_methods   rT   rC   
sleep_timec                    #    g7f)z!Executes pub/sub run in a thread.Nr+   )r-   rg   rR   s      r.   execute_pubsub_run'AsyncCommandExecutor.execute_pubsub_runx   rT   rC   r+   )%__name__
__module____qualname____firstlineno__propertyr   r   r/   r	   r   r4   r9   r
   r   r<   r$   rA   r   rE   setterr   rK   r   rN   rG   rX   tupler\   r   r   ra   strre   floatr   ri   __static_attributes__r+   r2   r.   r'   r'   !   s
   9    4(<#=    5I d   -!8    	%	/@			 	 x/    F t    ,D    u        E   #XJ$45  s   5 s  r2   r'   c                      ^  \ rS rSr\\\4S\\   S\	S\
S\S\S\S\S	\4U 4S
 jjjr\S\	4S j5       r\S\\   4S j5       rS\SS4S jr\S\\   4S j5       rS\S\SS4S jr\S\\   4S j5       r\R6                  S\SS4S j5       r\S\4S j5       r\S\
4S j5       rS rS r S\!4S jr"SSSS.S \#S!/\$\%\&\%   4   4   S"\'S#\\(   S$\)S%\\   4
S& jjr*S'\(4S( jr+ S4S)\S\%4S* jjr, S5S+\#S,\!4S- jjr-S. r.S/ r/S0\!4S1 jr0S2 r1S3r2U =r3$ )6DefaultCommandExecutor~   r4   r/   rN   failover_strategyevent_dispatcherfailover_attemptsfailover_delayauto_fallback_intervalc	                   > [         T
U ]  U5        U H  n	U	R                  U S9  M     X l        Xl        X0l        [        XFU5      U l        XPl        SU l	        SU l
        0 U l        U R                  5         U R                  5         g)aX  
Initialize the DefaultCommandExecutor instance.

Args:
    failure_detectors: List of failure detector instances to monitor database health
    databases: Collection of available databases to execute commands on
    command_retry: Retry policy for failed command execution
    failover_strategy: Strategy for handling database failover
    event_dispatcher: Interface for dispatching events
    failover_attempts: Number of failover attempts
    failover_delay: Delay between failover attempts
    auto_fallback_interval: Time interval in seconds between attempts to fall back to a primary database
)command_executorN)super__init__set_command_executor
_databases_failure_detectors_command_retryr   _failover_strategy_executor_event_dispatcher_active_database_active_pubsub_active_pubsub_kwargs_setup_event_dispatcher_schedule_next_fallback)r-   r4   r/   rN   rx   ry   rz   r{   r|   fd	__class__s             r.   r   DefaultCommandExecutor.__init__   s    0 	/0#B##T#: $ $"3++J.,
( "24804%'"$$&$$&r2   r)   c                     U R                   $ N)r   r,   s    r.   r/    DefaultCommandExecutor.databases   s    r2   c                     U R                   $ r   )r   r,   s    r.   r4   (DefaultCommandExecutor.failure_detectors   s    &&&r2   r6   Nc                 :    U R                   R                  U5        g r   )r   appendr8   s     r.   r9   +DefaultCommandExecutor.add_failure_detector   s    &&'78r2   c                     U R                   $ r   )r   r,   s    r.   r<   &DefaultCommandExecutor.active_database   s    $$$r2   r>   r?   c                    #    U R                   nXl         Ub\  X1LaW  [        UUUS9I S h  vN   U R                  R                  [	        UU R                   U 40 U R
                  D65      I S h  vN   g g g  NJ N	7f)N)	fail_fromfail_tor?   )r   r   r   dispatch_asyncr   r   )r-   r>   r?   
old_actives       r.   rA   *DefaultCommandExecutor.set_active_database   s      **
 (!j&@%$   
 ((77*)) 00	   'A!
s"   (A9A5AA9-A7.A97A9c                     U R                   $ r   r   r,   s    r.   rE   $DefaultCommandExecutor.active_pubsub       """r2   rG   c                     Xl         g r   r   rI   s     r.   rE   r      s    $r2   c                     U R                   $ r   )r   r,   s    r.   rK   1DefaultCommandExecutor.failover_strategy_executor   s    ///r2   c                     U R                   $ r   )r   r,   s    r.   rN   $DefaultCommandExecutor.command_retry   r   r2   c                     U R                   cf  [        U R                  R                  [        5      (       a  [        S5      eU R                  R                  R                  " S0 UD6U l         Xl        g g )Nz(PubSub is not supported for RedisClusterr+   )r   
isinstancer   clientr   
ValueErrorrG   r   rQ   s     r.   rG   DefaultCommandExecutor.pubsub   s`    &$//66EE !KLL"&"7"7">">"E"E"O"OD)/& 'r2   c                 X   ^ ^^#    UUU 4S jnT R                  UT5      I S h  vN $  N7f)Nc                     >#    TR                   R                  R                  " T0 TD6I S h  vN n TR                  T5      I S h  vN   U $  N N7fr   )r   r   rX   _register_command_execution)responserV   rW   r-   s    r.   callback8DefaultCommandExecutor.execute_command.<locals>.callback   sW     !2299II  H 224888O	 9s!   *AAAAAA_execute_with_failure_detection)r-   rV   rW   r   s   ``` r.   rX   &DefaultCommandExecutor.execute_command   s&     	 99(DIIIIs   *(*rZ   c                 T   ^ ^#    UU 4S jnT R                  UT5      I S h  vN $  N7f)Nc                  l  >#    TR                   R                  R                  5        IS h  vN n T H  u  pU R                  " U0 UD6  M     U R	                  5       I S h  vN nTR                  T5      I S h  vN   UsS S S 5      IS h  vN   $  Ne N2 N N! , IS h  vN  (       d  f       g = f7fr   )r   r   pipelinerX   executer   )pipecommandrW   r   rZ   r-   s       r.   r   9DefaultCommandExecutor.execute_pipeline.<locals>.callback   s     ,,33<<>>$(5$G(('=W= )6 "&/66}EEE ?>> 0E ?>>>sh   )B4BB41B!B"B:B;B B4BB4BBB4B1 B#!B1-B4r   )r-   rZ   r   s   `` r.   r\   'DefaultCommandExecutor.execute_pipeline   s&     	  99(MRRRRs   (&(F
shard_hintvalue_from_callablewatch_delayfuncr   r`   r   r   r   c                b   ^ ^^^^^#    UU UUUU4S jnT R                  U5      I S h  vN $  N7f)Nc                     >#    TR                   R                  R                  " T/TQ7TTTS.6I S h  vN n TR                  S5      I S h  vN   U $  N N7f)Nr   r+   )r   r   r^   r   )r   r   r-   r   r   r   r`   s    r.   r   <DefaultCommandExecutor.execute_transaction.<locals>.callback  sg     !2299EE &$7' H 222666O 7s!   0AAAAAAr   )r-   r   r   r   r   r`   r   s   `````` r.   ra   *DefaultCommandExecutor.execute_transaction   s)     		 		 99(CCCCs    /-/rc   c                 ^   ^ ^^^#    UUUU 4S jnT R                   " U/TQ76 I S h  vN $  N7f)Nc                     >#    [        TR                  T5      n [        U 5      (       a  U " T0 TD6I S h  vN nOU " T0 TD6nTR                  T5      I S h  vN   U$  N( N7fr   )getattrrE   r   r   )methodr   rV   rR   rc   r-   s     r.   r   >DefaultCommandExecutor.execute_pubsub_method.<locals>.callback  sg     T//=F"6**!'!8!88!4262224888O 9 9s!   2A"A!A"A A" A"r   )r-   rc   rV   rR   r   s   ```` r.   re   ,DefaultCommandExecutor.execute_pubsub_method  s.     	 	 99(JTJJJJs    -+-rg   c                 Z   ^ ^^^#    UUU U4S jnT R                  U5      I S h  vN $  N7f)Nc                  V   >#    TR                   R                  TT TS9I S h  vN $  N7f)N)poll_timeoutexception_handlerrG   )r   run)r   rG   r-   rg   s   r.   r   ;DefaultCommandExecutor.execute_pubsub_run.<locals>.callback   s7     ,,00'"3 1    s   )')r   )r-   rg   r   rG   r   s   ```` r.   ri   )DefaultCommandExecutor.execute_pubsub_run  s)     	 	 99(CCCCs   +)+r   cmdsc                 ~   ^ ^^^#    UU 4S jmT R                   R                  U4S jUU 4S j5      I Sh  vN $  N7f)z?
Execute a commands execution callback with failure detection.
c                  b   >#    TR                  5       I S h  vN   T " 5       I S h  vN $  N N7fr   )_check_active_database)r   r-   s   r.   wrapperGDefaultCommandExecutor._execute_with_failure_detection.<locals>.wrapper0  s+     --///!## 0#s   /+/-//c                     > T " 5       $ r   r+   )r   s   r.   <lambda>HDefaultCommandExecutor._execute_with_failure_detection.<locals>.<lambda>6  s    GIr2   c                 *   > TR                   " U /TQ76 $ r   )_on_command_fail)errorr   r-   s    r.   r   r   7  s    $//==r2   N)r   call_with_retry)r-   r   r   r   s   ```@r.   r   6DefaultCommandExecutor._execute_with_failure_detection)  s6     	$
 ((88=
 
 	
 
s   0=;=c                   #    U R                   be  U R                   R                  R                  [        R                  :w  d3  U R
                  S:  a}  U R                  [        R                  " 5       ::  aY  U R                  U R                  R                  5       I Sh  vN [        R                  5      I Sh  vN   U R                  5         ggg N2 N7f)z2
Checks if active a database needs to be updated.
Nr   )r   circuitstateCBStateCLOSED_auto_fallback_interval_next_fallback_attemptr   nowrA   r   r   r$   	AUTOMATICr   r,   s    r.   r   -DefaultCommandExecutor._check_active_database:  s     
 !!)$$,,22gnnD,,q0//8<<>A **66>>@@!++   ((* B 1
 As$   BCCC8C9CCc                 h   #    U R                   R                  [        X!5      5      I S h  vN   g  N7fr   )r   r   r   )r-   r   rV   s      r.   r   'DefaultCommandExecutor._on_command_failL  s+     $$33$T1
 	
 	
s   (202cmdc                 h   #    U R                    H  nUR                  U5      I S h  vN   M     g  N	7fr   )r   register_command_execution)r-   r   detectors      r.   r   2DefaultCommandExecutor._register_command_executionQ  s,     //H55c::: 0:s   $20
2c                     [        U R                  5      n[        5       n[        5       nU R                  R                  [        U/[        UU/05        g)z 
Registers necessary listeners.
N)r   r   r   r   r   register_listenersr   r   )r-   failure_listenerresubscribe_listenerclose_connection_listeners       r.   r   .DefaultCommandExecutor._setup_event_dispatcherU  sW     2$2I2IJAC$J$L!11(+;*<*-(-	
r2   )r   r   r   r   r   r   r   r   )NN)r+   )4rk   rl   rm   rn   r   r   r#   r	   r   r   r   r   r   intrs   r   ro   r/   r4   r9   r
   r   r<   r$   rA   r   rE   rp   r   rK   rN   rG   rX   rq   r\   r   r   r   r   r%   rr   boolra   re   ri   r   r   r   r   r   rt   __classcell__)r   s   @r.   rv   rv   ~   sa    "; 6(F(' 45(' (' 	('
 1(' 3(' (' (' !&(' ('T 9   '4(<#= ' '95I 9d 9 %-!8 % %%/@	* #x/ # # %F %t % % 0,D 0 0 #u # #0J
SE 
S  %)$)'+D
|U3	#+>%??@D D SM	D
 "D e_D*Ks K AE
D
D	
D 13
 
(-
"+$

;U ;
 
r2   rv   N)7abcr   asyncior   r   typingr   r   r   r	   r
   r   redis.asyncior   redis.asyncio.clientr   r   redis.asyncio.multidb.databaser   r   r   redis.asyncio.multidb.eventr   r   r   r   redis.asyncio.multidb.failoverr   r   r   r   r   &redis.asyncio.multidb.failure_detectorr   $redis.asyncio.observability.recorderr   redis.asyncio.retryr   redis.eventr   r   redis.multidb.circuitr    r   redis.multidb.command_executorr!   r"   redis.multidb.configr#   redis.observability.attributesr$   redis.typingr%   r'   rv   r+   r2   r.   <module>r     ss     '  B B & 1 M M   H D % J 2 O ? < Z? Zzf
02F f
r2   