
    Oǻi)'                        U d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ dZdZdZdZ e j8                  e      Z G d de      Z G d de      Z  G d de       Z! G d de!      Z" G d de!      Z# G d de!      Z$ G d de      Z%e%jL                  Z'e%e(d<    G d de      Z) G d  d!e      Z*y)"    N)ABCabstractmethod)Enum)sleep)ListOptionalTupleUnion)Redis)	NoBackoff)DEFAULT_TIMEOUT
HttpClient)UnhealthyDatabaseException)Retry      g      ?i  c                   "    e Zd Zedefd       Zy)HealthCheckreturnc                      y)z(Function to determine the health status.N )selfdatabases     W/opt/lhia/marcimex/agent/venv/lib/python3.12/site-packages/redis/multidb/healthcheck.pycheck_healthzHealthCheck.check_health        	    N)__name__
__module____qualname__r   boolr   r   r   r   r   r      s      r   r   c                   p    e Zd ZdZeedefd              Zeedefd              Z	ede
e   defd       Zy)HealthCheckPolicyz)
    Health checks execution policy.
    r   c                      y)z*Number of probes to execute health checks.Nr   r   s    r   health_check_probesz%HealthCheckPolicy.health_check_probes!        	r   c                      y)z"Delay between health check probes.Nr   r%   s    r   health_check_delayz$HealthCheckPolicy.health_check_delay'   r'   r   health_checksc                      y)z8Execute health checks and return database health status.Nr   r   r*   r   s      r   executezHealthCheckPolicy.execute-   r   r   N)r   r   r    __doc__propertyr   intr&   floatr)   r   r   r!   r-   r   r   r   r#   r#      st     S    E    T+%6 T  r   r#   c                   h    e Zd ZdedefdZedefd       Zedefd       Ze	de
e   defd       Zy	)
AbstractHealthCheckPolicyr&   r)   c                 @    |dk  rt        d      || _        || _        y )N   z*health_check_probes must be greater than 0)
ValueError_health_check_probes_health_check_delay)r   r&   r)   s      r   __init__z"AbstractHealthCheckPolicy.__init__4   s&    "IJJ$7!#5 r   r   c                     | j                   S N)r7   r%   s    r   r&   z-AbstractHealthCheckPolicy.health_check_probes:   s    (((r   c                     | j                   S r;   )r8   r%   s    r   r)   z,AbstractHealthCheckPolicy.health_check_delay>   s    '''r   r*   c                      y r;   r   r,   s      r   r-   z!AbstractHealthCheckPolicy.executeB   s    r   N)r   r   r    r0   r1   r9   r/   r&   r)   r   r   r   r!   r-   r   r   r   r3   r3   3   sr    6C 6U 6 )S ) ) (E ( ( T+%6 T  r   r3   c                   B     e Zd ZdZdedef fdZdee   de	fdZ
 xZS )HealthyAllPolicyzM
    Policy that returns True if all health check probes are successful.
    r&   r)   c                 &    t         |   ||       y r;   superr9   r   r&   r)   	__class__s      r   r9   zHealthyAllPolicy.__init__L       ,.@Ar   r*   r   c                     |D ]Z  }t        | j                        D ]@  }	 |j                  |      s  y	 || j                  dz
  k  s,t        | j                         B \ y# t        $ r}t	        d||      d }~ww xY w)NFUnhealthy databaser5   Tranger&   r   	Exceptionr   r   r8   )r   r*   r   health_checkattemptes         r   r-   zHealthyAllPolicy.executeO   s    ) 		4L !9!9: 4X'44X>$ ?
 T5599$2234		4  ! X45I8UVWWXs   A!!	A<*A77A<r   r   r    r.   r0   r1   r9   r   r   r!   r-   __classcell__rD   s   @r   r?   r?   G   s6    BC BU BT+%6 T r   r?   c                   B     e Zd ZdZdedef fdZdee   de	fdZ
 xZS )HealthyMajorityPolicyzW
    Policy that returns True if a majority of health check probes are successful.
    r&   r)   c                 &    t         |   ||       y r;   rA   rC   s      r   r9   zHealthyMajorityPolicy.__init__b   rE   r   r*   r   c                    |D ]  }| j                   dz  dk(  r| j                   dz  }n| j                   dz   dz  }t        | j                         D ]I  }	 |j                  |      s|dz  }|dk  r  y|| j                   dz
  k  s5t        | j                         K  y# t        $ r!}|dz  }|dk  rt	        d||      Y d }~Rd }~ww xY w)N   r   r5   FrG   T)r&   rI   r   rJ   r   r   r8   )r   r*   r   rK   allowed_unsuccessful_probesrL   rM   s          r   r-   zHealthyMajorityPolicy.executee   s    ) 	4L''!+q0.2.F.F.J+/3/G/G!/Kq.P+ !9!9: 4
'44X>3q836!;#( T5599$2234	4*  ! /14/2a780(A  8s   B	C'CCrN   rP   s   @r   rR   rR   ]   s6    BC BU BT+%6 T r   rR   c                   B     e Zd ZdZdedef fdZdee   de	fdZ
 xZS )HealthyAnyPolicyzT
    Policy that returns True if at least one health check probe is successful.
    r&   r)   c                 &    t         |   ||       y r;   rA   rC   s      r   r9   zHealthyAnyPolicy.__init__   rE   r   r*   r   c                 6   d}|D ]n  }d }t        | j                        D ]C  }	 |j                  |      rd} n-d}	 || j                  dz
  k  s/t        | j                         E |s|s|c S |rk|sn| |S # t        $ r}t	        d||      }Y d }~Xd }~ww xY w)NFTrG   r5   rH   )r   r*   r   
is_healthyrK   	exceptionrL   rM   s           r   r-   zHealthyAnyPolicy.execute   s    
) 	 LI !9!9: 4	#00:%)
%*
 T5599$2234 i!!I+	 .  !  :,h!Is   A8A88	BBBrN   rP   s   @r   rX   rX   ~   s6    BC BU BT+%6 T r   rX   c                       e Zd ZeZeZeZy)HealthCheckPoliciesN)	r   r   r    r?   HEALTHY_ALLrR   HEALTHY_MAJORITYrX   HEALTHY_ANYr   r   r   r^   r^      s    "K,"Kr   r^   DEFAULT_HEALTH_CHECK_POLICYc                       e Zd ZdZdefdZy)PingHealthCheckz-
    Health check based on PING command.
    r   c                     t        |j                  t              r|j                  j                  d      S |j                  j	                         }|D ]  }|j
                  j                  d      r y y)NPINGFT)
isinstanceclientr   execute_command	get_nodesredis_connection)r   r   	all_nodesnodes       r   r   zPingHealthCheck.check_health   sd    hoou-??226:: !113I! !,,<<VD ! r   N)r   r   r    r.   r!   r   r   r   r   rd   rd      s    
 
r   rd   c                       e Zd ZdZdeeddddddddfdedededee	e
e
f      d	ed
ee
   dee
   deee
ef      dee
   dee
   dee
   fdZdefdZy)LagAwareHealthCheckz
    Health check available for Redis Enterprise deployments.
    Verify via REST API that the database is healthy based on different lags.
    i$  NTrest_api_portlag_aware_tolerancetimeout
auth_basic
verify_tlsca_fileca_pathca_dataclient_cert_fileclient_key_fileclient_key_passwordc                 z    t        ||t        t               d      |||||	|
|
      | _        || _        || _        y)a  
        Initialize LagAwareHealthCheck with the specified parameters.

        Args:
            rest_api_port: Port number for Redis Enterprise REST API (default: 9443)
            lag_aware_tolerance: Tolerance in lag between databases in MS (default: 100)
            timeout: Request timeout in seconds (default: DEFAULT_TIMEOUT)
            auth_basic: Tuple of (username, password) for basic authentication
            verify_tls: Whether to verify TLS certificates (default: True)
            ca_file: Path to CA certificate file for TLS verification
            ca_path: Path to CA certificates directory for TLS verification
            ca_data: CA certificate data as string or bytes
            client_cert_file: Path to client certificate file for mutual TLS
            client_key_file: Path to client private key file for mutual TLS
            client_key_password: Password for encrypted client private key
        r   )retries)
rr   rs   retryrt   ru   rv   rw   rx   ry   rz   N)r   r   r   _http_client_rest_api_port_lag_aware_tolerance)r   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   s               r   r9   zLagAwareHealthCheck.__init__   sK    @ '!	Q/!-+ 3
 ,$7!r   r   c                 ~   |j                   t        d      t        |j                  t              r|j                  j                         d   }n#|j                  j                  d   j                  }|j                    d| j                   }|| j                  _
        d }| j                  j                  d      D ]+  }|d   D ]!  }|d   |k(  r|} |d   D ]  }||k(  s	|} ! # - | t        j                  d	       t        d
      d|d    d| j                   }| j                  j                  |d       y)Nz[Database health check url is not set. Please check DatabaseConfig for the current database.hostr   :z/v1/bdbs	endpointsdns_nameaddrz8LagAwareHealthCheck failed: Couldn't find a matching bdbzCould not find a matching bdbz	/v1/bdbs/uidz=/availability?extend_check=lag&availability_lag_tolerance_ms=F)expect_jsonT)health_check_urlr6   rg   rh   r   get_connection_kwargsstartup_nodesr   r   r~   base_urlgetloggerwarningr   )	r   r   db_hostr   matching_bdbbdbendpointr   urls	            r   r   z LagAwareHealthCheck.check_health   sl   $$,m  hoou-oo;;=fEGoo33A6;;G//0$2E2E1FG%-" $$((4 
	C, 	J'72#&L %V, Dw'*	
	 NNUV<== U+, -??C?X?X>Y[ 	 	cu5 r   )r   r   r    r.   DEFAULT_LAG_AWARE_TOLERANCEr   r0   r1   r   r	   strr!   r
   bytesr9   r   r   r   r   ro   ro      s     "#>(04!%!%/3*.)--1-8-8 !-8 	-8
 U38_--8 -8 #-8 #-8 %U
+,-8 #3--8 "#-8 &c]-8^' 'r   ro   )+loggingabcr   r   enumr   timer   typingr   r   r	   r
   redisr   redis.backoffr   redis.http.http_clientr   r   redis.multidb.exceptionr   redis.retryr   DEFAULT_HEALTH_CHECK_PROBESDEFAULT_HEALTH_CHECK_INTERVALDEFAULT_HEALTH_CHECK_DELAYr   	getLoggerr   r   r   r#   r3   r?   rR   rX   r^   r_   rb   __annotations__rd   ro   r   r   r   <module>r      s     #   / /  # > >   !   " 			8	$#  . 1 (0 ,5 B"0 "J#$ # 4G3R3R 0 Rk $\+ \r   