
    Oǻi %                     v   d dl mZmZ d dlmZ d dlmZmZmZm	Z	 d dl
Z
d dlmZmZmZ d dlmZmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZmZ d dlmZmZ d dl m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z'm(Z(m)Z)m*Z* d dl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d dl3m4Z4 dZ5 G d de      Z6defdZ7e G d d             Z8e G d d             Z9y)    )	dataclassfield)Enum)ListOptionalTypeUnionN)ConnectionPoolRedisRedisCluster)ExponentialWithJitterBackoff	NoBackoff)WeightedList)EventDispatcherEventDispatcherInterface)MaintNotificationsConfig)DEFAULT_GRACE_PERIODCircuitBreakerPBCircuitBreakerAdapter)Database	Databases)DEFAULT_FAILOVER_ATTEMPTSDEFAULT_FAILOVER_DELAYFailoverStrategyWeightBasedFailoverStrategy)DEFAULT_FAILURE_RATE_THRESHOLD!DEFAULT_FAILURES_DETECTION_WINDOWDEFAULT_MIN_NUM_FAILURESCommandFailureDetectorFailureDetector)DEFAULT_HEALTH_CHECK_DELAYDEFAULT_HEALTH_CHECK_INTERVALDEFAULT_HEALTH_CHECK_POLICYDEFAULT_HEALTH_CHECK_PROBESHealthCheckHealthCheckPoliciesPingHealthCheck)Retryx   c                       e Zd ZdZdZdZy)InitialHealthCheckall_availablemajority_availableone_availableN)__name__
__module____qualname__ALL_AVAILABLEMAJORITY_AVAILABLEONE_AVAILABLE     R/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/redis/multidb/config.pyr+   r+   -   s    #M-#Mr6   r+   returnc                      t               S N)r   r5   r6   r7   default_event_dispatcherr;   3   s    r6   c                       e Zd ZU dZdZeed<    ee      Z	eed<   dZ
ee   ed<   dZee   ed<   dZee   ed	<   eZeed
<   dZee   ed<   defdZy)DatabaseConfiga  
    Dataclass representing the configuration for a database connection.

    This class is used to store configuration settings for a database connection,
    including client options, connection sourcing details, circuit breaker settings,
    and cluster-specific properties. It provides a structure for defining these
    attributes and allows for the creation of customized configurations for various
    database setups.

    Attributes:
        weight (float): Weight of the database to define the active one.
        client_kwargs (dict): Additional parameters for the database client connection.
        from_url (Optional[str]): Redis URL way of connecting to the database.
        from_pool (Optional[ConnectionPool]): A pre-configured connection pool to use.
        circuit (Optional[CircuitBreaker]): Custom circuit breaker implementation.
        grace_period (float): Grace period after which we need to check if the circuit could be closed again.
        health_check_url (Optional[str]): URL for health checks. Cluster FQDN is typically used
            on public Redis Enterprise endpoints.

    Methods:
        default_circuit_breaker:
            Generates and returns a default CircuitBreaker instance adapted for use.
    g      ?weightdefault_factoryclient_kwargsNfrom_url	from_poolcircuitgrace_periodhealth_check_urlr8   c                 X    t        j                  | j                        }t        |      S )N)reset_timeout)	pybreakerr   rE   r   )selfcircuit_breakers     r7   default_circuit_breakerz&DatabaseConfig.default_circuit_breakerY   s#    #22ARARS&77r6   )r/   r0   r1   __doc__r>   float__annotations__r   dictrA   rB   r   strrC   r
   rD   r   r   rE   rF   rL   r5   r6   r7   r=   r=   7   ss    0 FE5M45"Hhsm"*.Ix'.(,GXn%,.L%.&*hsm*8 8r6   r=   c                      e Zd ZU dZee   ed<   eZe	e
eef      ed<    e edd      d      Zeed	<   d
Zeee      ed<   eZeed<   eZeed<   eZeed<   d
Zeee      ed<   eZeed<   eZeed<   e Z!eed<   e"Z#e$ed<   d
Z%ee&   ed<   e'Z(eed<   e)Z*eed<   e+Z,eed<    e-e.      Z/e0ed<   e1jd                  Z3e1ed<   de4fdZ5dee   fdZ6dee   fdZ7de&fdZ8y
) MultiDbConfiga	  
    Configuration class for managing multiple database connections in a resilient and fail-safe manner.

    Attributes:
        databases_config: A list of database configurations.
        client_class: The client class used to manage database connections.
        command_retry: Retry strategy for executing database commands.
        failure_detectors: Optional list of additional failure detectors for monitoring database failures.
        min_num_failures: Minimal count of failures required for failover
        failure_rate_threshold: Percentage of failures required for failover
        failures_detection_window: Time interval for tracking database failures.
        health_checks: Optional list of additional health checks performed on databases.
        health_check_interval: Time interval for executing health checks.
        health_check_probes: Number of attempts to evaluate the health of a database.
        health_check_probes_delay: Delay between health check attempts.
        health_check_policy: Policy for determining database health based on health checks.
        failover_strategy: Optional strategy for handling database failover scenarios.
        failover_attempts: Number of retries allowed for failover operations.
        failover_delay: Delay between failover attempts.
        auto_fallback_interval: Time interval to trigger automatic fallback.
        event_dispatcher: Interface for dispatching events related to database operations.
        initial_health_check_policy: Defines the policy used to determine whether the databases setup is
                                     healthy during the initial health check.

    Methods:
        databases:
            Retrieves a collection of database clients managed by weighted configurations.
            Initializes database clients based on the provided configuration and removes
            redundant retry objects for lower-level clients to rely on global retry logic.

        default_failure_detectors:
            Returns the default list of failure detectors used to monitor database failures.

        default_health_checks:
            Returns the default list of health checks used to monitor database health
            with specific retry and backoff strategies.

        default_failover_strategy:
            Provides the default failover strategy used for handling failover scenarios
            with defined retry and backoff configurations.
    databases_configclient_class   
   )basecap   )backoffretriescommand_retryNfailure_detectorsmin_num_failuresfailure_rate_thresholdfailures_detection_windowhealth_checkshealth_check_intervalhealth_check_probeshealth_check_probes_delayhealth_check_policyfailover_strategyfailover_attemptsfailover_delayauto_fallback_intervalr?   event_dispatcherinitial_health_check_policyr8   c           	         t               }| j                  D ]l  }t        dt                     |j                  d<   d|j                  vrt        d      |j                  d<   |j                  r2 | j                  j                  |j                  fi |j                  }n}|j                  rU|j                  j                  t        dt                            | j                  j                  |j                        }n | j                  d	i |j                  }|j                  |j                         n|j                  }|j                  t        |||j                  |j                        |j                         o |S )
Nr   )r\   r[   retrymaint_notifications_configF)enabled)connection_pool)clientrD   r>   rF   r5   )r   rT   r(   r   rA   r   rB   rU   rC   	set_retryrD   rL   addr   r>   rF   )rJ   	databasesdatabase_configrr   rD   s        r7   ru   zMultiDbConfig.databases   sz    N	#44 )	O 6;9;6O))'2 ,?3P3PP,U;  --.JK ''3**33#,,0?0M0M !**))33!Y[9 **44$3$=$= 5  +**K_-J-JK #**2  779$,, 
 MM!#*11%4%E%E	  &&C)	V r6   c                 \    t        | j                  | j                  | j                        gS )N)r_   r`   failure_detection_window)r   r_   r`   ra   rJ   s    r7   default_failure_detectorsz'MultiDbConfig.default_failure_detectors   s/    "!%!6!6'+'B'B)-)G)G
 	
r6   c                     t               gS r:   )r'   ry   s    r7   default_health_checksz#MultiDbConfig.default_health_checks   s    
 	
r6   c                     t               S r:   )r   ry   s    r7   default_failover_strategyz'MultiDbConfig.default_failover_strategy   s    *,,r6   )9r/   r0   r1   rM   r   r=   rO   r   rU   r   r	   r   r(   r   r]   r^   r   r    r   r_   intr   r`   rN   r   ra   rb   r%   r"   rc   r$   rd   r!   re   r#   rf   r&   rg   r   r   rh   r   ri   DEFAULT_AUTO_FALLBACK_INTERVALrj   r   r;   rk   r   r+   r2   rl   r   ru   rz   r|   r~   r5   r6   r7   rS   rS   ^   s\   (T >**5:L$uUL012: ,!<aM5  :>x_ 56=4c4$BEB'HuH15M8D-.5#@5@::'AuA/J,J48x 0186s62NE2$BEB1602.  7I6V6V!3V.9 .`
4+@ 

tK'8 

-+; -r6   rS   ):dataclassesr   r   enumr   typingr   r   r   r	   rI   redisr
   r   r   redis.backoffr   r   redis.data_structurer   redis.eventr   r   redis.maint_notificationsr   redis.multidb.circuitr   r   r   redis.multidb.databaser   r   redis.multidb.failoverr   r   r   r   redis.multidb.failure_detectorr   r   r   r   r    redis.multidb.healthcheckr!   r"   r#   r$   r%   r&   r'   redis.retryr(   r   r+   r;   r=   rS   r5   r6   r7   <module>r      s    (  . .  5 5 A - A > 
 7     !$ $ $":  #8 #8 #8L A- A- A-r6   