U
    gE9                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZmZmZ d dlmZ G dd	 d	eZG d
d deZG dd de	ZG dd deeZG dd dZG dd de
ZG dd deZdS )    N)Optional)Redis)SentinelCommands)
ConnectionConnectionPoolSSLConnection)ConnectionErrorReadOnlyErrorResponseErrorTimeoutError)str_if_bytesc                   @   s   e Zd ZdS )MasterNotFoundErrorN__name__
__module____qualname__ r   r   2/tmp/pip-unpacked-wheel-f3sx1i9r/redis/sentinel.pyr      s   r   c                   @   s   e Zd ZdS )SlaveNotFoundErrorNr   r   r   r   r   r      s   r   c                       sf   e Zd Z fddZdd Z fddZdd Zd	d
 Zddddee	 ee	 d fddZ
  ZS )SentinelManagedConnectionc                    s   | d| _t jf | d S )Nconnection_pool)popr   super__init__)selfkwargs	__class__r   r   r      s    z"SentinelManagedConnection.__init__c                 C   sR   | j }dt| j dt| j d|j d}| jrNd| j d| j }|| }|S )N<.	(service=z%s)>z,host=z,port=)r   typer   r   service_namehostport)r   poolsZ	host_infor   r   r   __repr__   s    $z"SentinelManagedConnection.__repr__c                    sD   |\| _ | _t   | jjr@| d t|  dkr@t	dd S )NZPINGZPONGzPING failed)
r#   r$   r   connectr   check_connectionZsend_commandr   read_responser   )r   addressr   r   r   
connect_to$   s    

z$SentinelManagedConnection.connect_toc              	   C   sf   | j r
d S | jjr$| | j  n>| j D ].}z| |W   S  tk
rZ   Y q.Y q.X q.td S N)_sockr   	is_masterr,   get_master_addressrotate_slavesr   r   )r   slaver   r   r   _connect_retry,   s    z(SentinelManagedConnection._connect_retryc                 C   s   | j | jdd S )Nc                 S   s   d S r-   r   )errorr   r   r   <lambda>:       z3SentinelManagedConnection.connect.<locals>.<lambda>)retryZcall_with_retryr3   r   r   r   r   r(   9   s    z!SentinelManagedConnection.connectF)disconnect_on_errorpush_requestc                   sH   zt  j|||dW S  tk
rB   | jjr<|   td Y nX d S )N)disable_decodingr9   r:   z"The previous master is now a slave)r   r*   r	   r   r/   
disconnectr   )r   r;   r9   r:   r   r   r   r*   <   s    z'SentinelManagedConnection.read_response)F)r   r   r   r   r'   r,   r3   r(   r   boolr*   __classcell__r   r   r   r   r      s    r   c                   @   s   e Zd ZdS )SentinelManagedSSLConnectionNr   r   r   r   r   r?   U   s   r?   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
SentinelConnectionPoolProxyc                 C   s0   t || _|| _|| _|| _|| _|   d S r-   )weakrefrefconnection_pool_refr/   r)   r"   sentinel_managerreset)r   r   r/   r)   r"   rD   r   r   r   r   Z   s    z$SentinelConnectionPoolProxy.__init__c                 C   s   d | _ d | _d S r-   )master_addressslave_rr_counterr8   r   r   r   rE   i   s    z!SentinelConnectionPoolProxy.resetc                 C   sD   | j | j}| jr@| j|kr@|| _|  }|d k	r@|jdd |S )NF)Zinuse_connections)rD   discover_masterr"   r/   rF   rC   r<   )r   rF   r   r   r   r   r0   m   s    z.SentinelConnectionPoolProxy.get_master_addressc                 c   s   | j | j}|rh| jd kr2tdt|d | _tt|D ](}| jd t| | _|| j }|V  q>z|  V  W n t	k
r   Y nX t
d| jd S )Nr      zNo slave found for )rD   discover_slavesr"   rG   randomrandintlenranger0   r   r   )r   slaves_r2   r   r   r   r1   x   s    

z)SentinelConnectionPoolProxy.rotate_slavesN)r   r   r   r   rE   r0   r1   r   r   r   r   r@   Y   s   r@   c                       s\   e Zd ZdZ fddZdd Z fddZedd	 Z fd
dZ	dd Z
dd Z  ZS )SentinelConnectionPoolz
    Sentinel backed connection pool.

    If ``check_connection`` flag is set to True, SentinelManagedConnection
    sends a PING command right after establishing the connection.
    c                    s~   | d|ddrtnt|d< |dd| _|dd| _t| | j| j||d| _t j	f | | j| j
d< || _|| _d S )	NZconnection_classsslFr/   Tr)   )r   r/   r)   r"   rD   r   )getr   r?   r   r/   r)   r@   proxyr   r   connection_kwargsr"   rD   )r   r"   rD   r   r   r   r   r      s&    
zSentinelConnectionPool.__init__c              	   C   s:   | j r
dnd}dt| j dt| j d| j d| d	S )NZmasterr2   r   r   r    (z))>)r/   r!   r   r   r"   )r   roler   r   r   r'      s    *zSentinelConnectionPool.__repr__c                    s   t    | j  d S r-   )r   rE   rT   r8   r   r   r   rE      s    
zSentinelConnectionPool.resetc                 C   s   | j jS r-   )rT   rF   r8   r   r   r   rF      s    z%SentinelConnectionPool.master_addressc                    s4   | j  p| j o| j|j|jfk}t }|o2||S r-   )r/   rF   r#   r$   r   owns_connection)r   
connectioncheckparentr   r   r   rX      s
    z&SentinelConnectionPool.owns_connectionc                 C   s
   | j  S r-   )rT   r0   r8   r   r   r   r0      s    z)SentinelConnectionPool.get_master_addressc                 C   s
   | j  S )zRound-robin slave balancer)rT   r1   r8   r   r   r   r1      s    z$SentinelConnectionPool.rotate_slaves)r   r   r   __doc__r   r'   rE   propertyrF   rX   r0   r1   r>   r   r   r   r   rQ      s   
rQ   c                   @   sf   e Zd ZdZdddZdd Zdd	 Zd
d Zdd Zdd Z	dd Z
eefddZeefddZdS )Sentinelar  
    Redis Sentinel cluster client

    >>> from redis.sentinel import Sentinel
    >>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
    >>> master = sentinel.master_for('mymaster', socket_timeout=0.1)
    >>> master.set('foo', 'bar')
    >>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
    >>> slave.get('foo')
    b'bar'

    ``sentinels`` is a list of sentinel nodes. Each node is represented by
    a pair (hostname, port).

    ``min_other_sentinels`` defined a minimum number of peers for a sentinel.
    When querying a sentinel, if it doesn't meet this threshold, responses
    from that sentinel won't be considered valid.

    ``sentinel_kwargs`` is a dictionary of connection arguments used when
    connecting to sentinel instances. Any argument that can be passed to
    a normal Redis connection can be specified here. If ``sentinel_kwargs`` is
    not specified, any socket_timeout and socket_keepalive options specified
    in ``connection_kwargs`` will be used.

    ``connection_kwargs`` are keyword arguments that will be used when
    establishing a connection to a Redis server.
    r   Nc                    sJ   |d krdd |  D }| _ fdd|D  _| _| _| _d S )Nc                 S   s    i | ]\}}| d r||qS )Zsocket_)
startswith).0kvr   r   r   
<dictcomp>   s    
  z%Sentinel.__init__.<locals>.<dictcomp>c                    s    g | ]\}}t ||f jqS r   )r   sentinel_kwargs)r`   hostnamer$   r8   r   r   
<listcomp>   s   z%Sentinel.__init__.<locals>.<listcomp>)itemsrd   	sentinelsmin_other_sentinelsrU   _force_master_ip)r   rh   ri   rd   Zforce_master_iprU   r   r8   r   r      s    

zSentinel.__init__c                 O   s\   t |dd}d| kr&|d |r@t| jj|| n| jD ]}|j|| qFdS )z
        Execute Sentinel command in sentinel nodes.
        once - If set to True, then execute the resulting command on a single
        node at random, rather than across the entire sentinel cluster.
        onceFT)r=   rS   keysr   rK   choicerh   execute_command)r   argsr   rk   sentinelr   r   r   rn      s    

zSentinel.execute_commandc                 C   sN   g }| j D ]}|d|jj q
dt| j dt| j dd| dS )Nz{host}:{port}r   r   z(sentinels=[,z])>)	rh   append
format_mapr   rU   r!   r   r   join)r   Zsentinel_addressesrp   r   r   r   r'     s    
(zSentinel.__repr__c                 C   s2   |d r|d s|d rdS |d | j k r.dS dS )Nr/   is_sdownis_odownFznum-other-sentinelsT)ri   )r   stater"   r   r   r   check_master_state  s
    zSentinel.check_master_statec           
      C   s   t  }t| jD ]\}}z| }W nB ttfk
rf } z || d| W Y qW 5 d}~X Y nX ||}|r| ||r|| jd  | jd< | j|< | j	dk	r| j	n|d }||d f  S qd}	t
|dkrdd| }	td	||	 dS )
z
        Asks sentinel servers for the Redis master's address corresponding
        to the service labeled ``service_name``.

        Returns a pair (address, port) or raises MasterNotFoundError if no
        master is found.
        z - Nr   ipr$    z : z, zNo master found for )list	enumeraterh   Zsentinel_mastersr   r   rr   rS   rx   rj   rM   rt   r   )
r   r"   Zcollected_errorsZsentinel_norp   Zmasterserw   ry   
error_infor   r   r   rH     s*    
zSentinel.discover_masterc                 C   s:   g }|D ],}|d s|d rq| |d |d f q|S )z1Remove slaves that are in an ODOWN or SDOWN staterv   ru   ry   r$   )rr   )r   rO   Zslaves_aliver2   r   r   r   filter_slavesB  s    zSentinel.filter_slavesc                 C   sT   | j D ]H}z||}W n tttfk
r6   Y qY nX | |}|r|  S qg S )z;Returns a list of alive slaves for service ``service_name``)rh   Zsentinel_slavesr   r
   r   r   )r   r"   rp   rO   r   r   r   rJ   K  s    



zSentinel.discover_slavesc                 K   s0   d|d< t | j}|| |||| f|S )a  
        Returns a redis client instance for the ``service_name`` master.

        A :py:class:`~redis.sentinel.SentinelConnectionPool` class is
        used to retrieve the master's address before establishing a new
        connection.

        NOTE: If the master's address has changed, any cached connections to
        the old master are closed.

        By default clients will be a :py:class:`~redis.Redis` instance.
        Specify a different class to the ``redis_class`` argument if you
        desire something different.

        The ``connection_pool_class`` specifies the connection pool to
        use.  The :py:class:`~redis.sentinel.SentinelConnectionPool`
        will be used by default.

        All other keyword arguments are merged with any connection_kwargs
        passed to this class and passed to the connection pool as keyword
        arguments to be used to initialize Redis connections.
        Tr/   dictrU   updateZ	from_poolr   r"   Zredis_classZconnection_pool_classr   rU   r   r   r   
master_forW  s    

zSentinel.master_forc                 K   s0   d|d< t | j}|| |||| f|S )a  
        Returns redis client instance for the ``service_name`` slave(s).

        A SentinelConnectionPool class is used to retrieve the slave's
        address before establishing a new connection.

        By default clients will be a :py:class:`~redis.Redis` instance.
        Specify a different class to the ``redis_class`` argument if you
        desire something different.

        The ``connection_pool_class`` specifies the connection pool to use.
        The SentinelConnectionPool will be used by default.

        All other keyword arguments are merged with any connection_kwargs
        passed to this class and passed to the connection pool as keyword
        arguments to be used to initialize Redis connections.
        Fr/   r   r   r   r   r   	slave_for{  s    

zSentinel.slave_for)r   NN)r   r   r   r\   r   rn   r'   rx   rH   r   rJ   r   rQ   r   r   r   r   r   r   r^      s"      
#	
'r^   )rK   rA   typingr   Zredis.clientr   Zredis.commandsr   Zredis.connectionr   r   r   Zredis.exceptionsr   r	   r
   r   Zredis.utilsr   r   r   r   r?   r@   rQ   r^   r   r   r   r   <module>   s   A0=