
    +hRj             
       :   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZm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 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*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ deAdeeAeBf   deAfdZCd ZDd ZEd ZFdedede
eeBeBf   e
eAef   f   fdZGd ZHd ZIdZJdZKdZLdZMd ZNd! ZO G d" d#e!      ZP G d$ d%      ZQ G d& d'eQe      ZR G d( d)      ZS G d* d+      ZT G d, d-      ZU G d. d/e      ZV G d0 d1eR      ZWd2eAde	d3ef   fd4ZXd5ZYeYD ]2  ZZeZj                  d6d7      j                         ZZ e]eWeZ eXeZ             4  G d8 d9      Z^ G d: d;      Z_y)<    N)OrderedDict)AnyCallableDictListOptionalTupleUnion)CommandsParserEncoder)
parse_scan)default_backoff)CaseInsensitiveDictPubSubRedis)READ_COMMANDSRedisClusterCommands)list_or_args)ConnectionPoolDefaultParser	parse_url)REDIS_CLUSTER_HASH_SLOTSkey_slot)AskErrorAuthenticationErrorClusterCrossSlotErrorClusterDownErrorClusterErrorConnectionError	DataErrorMasterDownError
MovedErrorRedisClusterException
RedisErrorResponseErrorSlotNotCoveredErrorTimeoutErrorTryAgainErrorLock)Retry)HIREDIS_AVAILABLE
dict_mergelist_keys_to_dictmerge_resultsafe_strstr_if_byteshostportreturnc                     |  d| S )N: r2   r3   s     @/var/www/html/venv/lib/python3.12/site-packages/redis/cluster.pyget_node_namer:   .   s    V1TF    c                 ^    | j                   xs   | j                  j                  |d   fi |S )Nr   )
connectionconnection_poolget_connection)
redis_nodeargsoptionss      r9   r?   r?   2   s:       $MJ$>$>$M$MQ%% r;   c                 t    i }g }|j                         D ]  \  }}t        |fi |\  }}|||<   ||z  }  ||fS N)itemsr   )	commandresrB   cursorsret	node_nameresponsecursorrs	            r9   parse_scan_resultrN   8   sX    G
C"yy{ 	8x373	#	q
 C<r;   c                     t               }|j                         D ]  }|D ]  \  }}	 ||xx   |z  cc<     |j                         D cg c]	  \  }}||f }}}|S # t        $ r |||<   Y Nw xY wc c}}w rD   )r   valuesKeyErrorrE   )	rF   rG   rB   numsub_dnumsub_tupschannel	numsubbednumsub
ret_numsubs	            r9   parse_pubsub_numsubrX   C   s    }Hzz| ."- 	.GY.!Y.!	.. <D>>;KL7F#LJL	  .$-!. Ms   AA2A/.A/resprB   c                    
 |j                  dd      
dt        dt        t        t        f   f
fd}i }| D ].  }|d d \  }}}|dd  } || |D 	cg c]  }	 ||	 	 c}	d|||f<   0 |S c c}	w )Ncurrent_host rA   r4   c                  2    t        | d         xs | d   fS Nr      )r1   )rA   r[   s    r9   
fix_serverz'parse_cluster_slots.<locals>.fix_serverU   s     DG$4d1g==r;      )primaryreplicas)getr   r	   str)rY   rB   r`   slotsslotstartendrb   rc   replicar[   s             @r9   parse_cluster_slotsrk   P   s     ;;~r2L># >%S/ > E 
"2AhsG8!7+=EF'W-F
eSj
 L Gs   A*c                    t        | d   t              r| S g }| D ]  }g g d}t        dt        |d         d      D ]'  }|d   j	                  |d   |   |d   |dz      f       ) |d   }|D ]A  }i }t        dt        |      d      D ]  }||dz      |||   <    |d   j	                  |       C |j	                  |        |S )z(
    Parse CLUSTER SHARDS response.
    r   )rf   nodesr_      rf   ra   rm   )
isinstancedictrangelenappend)	rY   rB   shardsxshardirm   node	dict_nodes	            r9   parse_cluster_shardsrz   d   s     $q'4 F 
r*q#ad)Q' 	<A'N!!1Q47QqT!a%[":;	<! 	-DI1c$i+ 1%)!a%[	$q'"1'N!!),		-
 	e
 Mr;   c                 $    | j                  d      S )z+
    Parse CLUSTER MYSHARDID response.
    utf-8)decode)rY   rB   s     r9   parse_cluster_myshardidr~   z   s     ;;wr;   rb   rj   zslot-id)$charsetconnection_classr>   connection_pool_classclient_namecredential_providerdbdecode_responsesencodingencoding_errorserrorsr2   lib_namelib_versionmax_connections
nodes_flagredis_connect_funcpasswordr3   queue_classretryretry_on_timeoutprotocolsocket_connect_timeoutsocket_keepalivesocket_keepalive_optionssocket_timeoutsslssl_ca_certsssl_ca_datassl_certfilessl_cert_reqsssl_keyfilessl_passwordunix_socket_pathusernamer8   c                  x    | j                         D ci c]  \  }}|t        v r|t        vr|| }}}|S c c}}w )z9
    Remove unsupported or disabled keys from kwargs
    )rE   REDIS_ALLOWED_KEYSKWARGS_DISABLED_KEYS)kwargskvconnection_kwargss       r9   cleanup_kwargsr      sN     LLNAq""q0D'D 	
1  s   6c            
       @    e Zd Z eej
                  eeee	e
ed      Zy)ClusterParser)ASKTRYAGAINMOVEDCLUSTERDOWN	CROSSSLOT
MASTERDOWNN)__name__
__module____qualname__r-   r   EXCEPTION_CLASSESr   r(   r"   r   r   r!   r7   r;   r9   r   r      s-    "''%+.)	

r;   r   c                      e Zd ZdZdZdZdZdZdZeeeeehZ	 e
 eg de       eg de       ed	ge       eg d
e            Zg dfZeeedZ e
 eddge       edgd        eg de       eg dd        eddgd        edgd        edge       edgd        edgd        edgd       
      ZeeefZd"d#d!Zy )$AbstractRedisCluster   	primariesrc   allrandomzdefault-node)WzACL CATzACL DELUSERz
ACL DRYRUNzACL GENPASSzACL GETUSERzACL HELPzACL LISTzACL LOGzACL LOADzACL SAVEzACL SETUSERz	ACL USERSz
ACL WHOAMIAUTHCLIENT LISTzCLIENT SETINFOCLIENT SETNAMECLIENT GETNAME
CONFIG SETCONFIG REWRITECONFIG RESETSTATTIMEPUBSUB CHANNELSPUBSUB NUMPATPUBSUB NUMSUBPUBSUB SHARDCHANNELSPUBSUB SHARDNUMSUBPINGINFOSHUTDOWNKEYSDBSIZEBGSAVESLOWLOG GETSLOWLOG LENSLOWLOG RESETWAITWAITAOFSAVEMEMORY PURGEzMEMORY MALLOC-STATSzMEMORY STATSLASTSAVEzCLIENT TRACKINGINFOCLIENT PAUSECLIENT UNPAUSECLIENT UNBLOCKz	CLIENT IDzCLIENT REPLYzCLIENT GETREDIRzCLIENT INFOCLIENT KILLREADONLYzCLUSTER INFOzCLUSTER MEETCLUSTER MYSHARDIDzCLUSTER NODESzCLUSTER REPLICASzCLUSTER RESETzCLUSTER SET-CONFIG-EPOCHCLUSTER SLOTSCLUSTER SHARDSzCLUSTER COUNT-FAILURE-REPORTSzCLUSTER KEYSLOTCOMMANDzCOMMAND COUNTzCOMMAND LISTzCOMMAND GETKEYS
CONFIG GETDEBUG	RANDOMKEYr   	READWRITEr   zTFUNCTION LOADzTFUNCTION DELETEzTFUNCTION LISTTFCALLTFCALLASYNCzGRAPH.CONFIGzLATENCY HISTORYzLATENCY LATESTzLATENCY RESETzMODULE LISTzMODULE LOADzMODULE UNLOADzMODULE LOADEX)FLUSHALLFLUSHDBzFUNCTION DELETEzFUNCTION FLUSHzFUNCTION LISTzFUNCTION LOADzFUNCTION RESTOREzREDISGEARS_2.REFRESHCLUSTERSCANSCRIPT EXISTSSCRIPT FLUSHSCRIPT LOADzFUNCTION DUMP)zCLUSTER COUNTKEYSINSLOTzCLUSTER DELSLOTSzCLUSTER DELSLOTSRANGEzCLUSTER GETKEYSINSLOTzCLUSTER SETSLOT)z	FT.CREATEz	FT.SEARCHzFT.AGGREGATEz
FT.EXPLAINzFT.EXPLAINCLIz
FT,PROFILEzFT.ALTERzFT.DROPINDEXzFT.ALIASADDzFT.ALIASUPDATEzFT.ALIASDELz
FT.TAGVALSz	FT.SUGADDz	FT.SUGGETz	FT.SUGDELz	FT.SUGLENzFT.SYNUPDATEz
FT.SYNDUMPzFT.SPELLCHECKz
FT.DICTADDz
FT.DICTDELzFT.DICTDUMPzFT.INFOzFT._LISTz	FT.CONFIGzFT.ADDzFT.DELzFT.DROPzFT.GETzFT.MGETz	FT.SYNADD)r   r   r   r   r   r   c                 F    t        t        |j                                     S rD   )sumlistrP   rF   rG   s     r9   <lambda>zAbstractRedisCluster.<lambda>|  s    CSZZ\8J4K r;   )r   r   r   )r   r   r   r   r   r   r   r   r   r   r   c                 X    t        |t              rt        |j                               S |S rD   )ro   rp   r   rP   r   s     r9   r   zAbstractRedisCluster.<lambda>      jd6KSZZ\!2 QT r;   r   r   c                 X    t        |t              rt        |j                               S |S rD   )ro   rp   r   rP   r   s     r9   r   zAbstractRedisCluster.<lambda>  r   r;   r   c                 B    t        |j                               dkD  rdS dS r^   )r   rP   r   s     r9   r   zAbstractRedisCluster.<lambda>  s    #cjjl:Ka:OQ UV r;   r   r   c                 P    t        |j                               j                         S rD   )r   rP   popr   s     r9   r   zAbstractRedisCluster.<lambda>  s    $szz|2D2H2H2J r;   r   c                 f    t        |j                          D cg c]  }t        |       c}S c c}w rD   )ziprP   r   )rF   rG   r   s      r9   r   zAbstractRedisCluster.<lambda>  s#    S#**,EW4XSV4X 4Xs   .r   c                 4    t        |j                               S rD   )r   rP   r   s     r9   r   zAbstractRedisCluster.<lambda>  s    SZZ\AR r;   Nc                    |r|| j                   _        y| j                         }| j                         D cg c]
  }||k7  s	| }}|r%t	        j
                  |      | j                   _        y| j                         D cg c]
  }||k7  s	| }}|r%t	        j
                  |      | j                   _        yyc c}w c c}w )a  Replace the default cluster node.
        A random cluster node will be chosen if target_node isn't passed, and primaries
        will be prioritized. The default node will not be changed if there are no other
        nodes in the cluster.

        Args:
            target_node (ClusterNode, optional): Target node to replace the default
            node. Defaults to None.
        N)nodes_managerdefault_nodeget_default_nodeget_primariesr   choiceget_replicas)selftarget_node	curr_noderx   r   rc   s         r9   replace_default_nodez)AbstractRedisCluster.replace_default_node  s     .9D+--/I*.*<*<*>T$$)BSTIT28--	2J""/ .2->->-@VTDIDUDVV6<mmH6MD&&3  U Ws   
B:B:
B?B?rD   )r   ClusterNoder4   N)r   r   r   RedisClusterRequestTTL	PRIMARIESREPLICAS	ALL_NODESRANDOMDEFAULT_NODE
NODE_FLAGSr-   r.   SLOT_IDCOMMAND_FLAGSSEARCH_COMMANDSrk   rz   r~   #CLUSTER_COMMANDS_RESPONSE_CALLBACKSrX   r/   rN   RESULT_CALLBACKSr   r'   r   ERRORS_ALLOW_RETRYr  r7   r;   r9   r   r      s   IHIF!LXy&,GJXr u[	
x 	 	
" 	?+V4 		
_yMx 	
"OJ -.4+' "?,@ACVWK	
 	?	
 	 U	
  	vT	
 	 V	
 	6($56OJ	
 	X	
 	>*,RSM'R *<9IJNr;   r   c                      e Zd Zed        Z	 	 	 	 	 	 	 	 	 	 	 d5dee   dedeed      deded	   d
e	dede	de	dee   dee
eegeeef   f      fdZd Zd Zd Zd Zd Zd Zd6dZd Zd Zd Zd Zd7dZd Zd Zded	   fd Zd8d!Zd9d"Zd6d#Zd:d$Z	 	 	 	 	 	 d;d%Z d& Z!ded   fd'Z"d( Z#d) Z$d* Z%d+ Z&d, Z'd- Z(d. Z)d/ Z*d0 Z+d1 Z,d2 Z-d3 Z.d4 Z/y)<RedisClusterc                      | dd|i|S )a  
        Return a Redis client object configured from the given URL

        For example::

            redis://[[username]:[password]]@localhost:6379/0
            rediss://[[username]:[password]]@localhost:6379/0
            unix://[username@]/path/to/socket.sock?db=0[&password=password]

        Three URL schemes are supported:

        - `redis://` creates a TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/redis>
        - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
          <https://www.iana.org/assignments/uri-schemes/prov/rediss>
        - ``unix://``: creates a Unix Domain Socket connection.

        The username, password, hostname, path and all querystring values
        are passed through urllib.parse.unquote in order to replace any
        percent-encoded values with their corresponding characters.

        There are several ways to specify a database number. The first value
        found will be used:

            1. A ``db`` querystring option, e.g. redis://localhost?db=0
            2. If using the redis:// or rediss:// schemes, the path argument
               of the url, e.g. redis://localhost/0
            3. A ``db`` keyword argument to this function.

        If none of these options are specified, the default db=0 is used.

        All querystring options are cast to their appropriate Python types.
        Boolean arguments can be specified with string values "True"/"False"
        or "Yes"/"No". Values that cannot be properly cast cause a
        ``ValueError`` to be raised. Once parsed, the querystring arguments
        and keyword arguments are passed to the ``ConnectionPool``'s
        class initializer. In the case of conflicting arguments, querystring
        arguments always win.

        urlr7   r7   )clsr  r   s      r9   from_urlzRedisCluster.from_url  s    T %s%f%%r;   Nr2   r3   startup_nodesr  cluster_error_retry_attemptsr   r+   require_full_coveragereinitialize_stepsread_from_replicasdynamic_startup_nodesr  address_remapc           	      .   |g }d|v rt        d      d}|
d}t        |
      }d|v rt        d      d|v r|d   dk7  rt        d	      |j                  |       |j                  d
      }|j                  d|      }|j	                  t        ||             n9|||j	                  t        ||             nt        |      dk(  rt        d      |j                  dd      | _        |j                  d| j                  i       t        di |}|r%|| _        |j                  d| j                  i       n%|j                  dt        t               d      i       t        |j                  dd      |j                  dd      |j                  dd            | _        || _        | j"                  j$                  j'                         | _        | j"                  j*                  j'                         | _        || _        d| _        || _        t5        d||||	|d|| _        t9        | j"                  j:                        | _        t9        | j"                  j>                        | _         tC        |       | _"        tG        jH                         | _%        y)a  
         Initialize a new RedisCluster client.

         :param startup_nodes:
             List of nodes from which initial bootstrapping can be done
         :param host:
             Can be used to point to a startup node
         :param port:
             Can be used to point to a startup node
         :param require_full_coverage:
            When set to False (default value): the client will not require a
            full coverage of the slots. However, if not all slots are covered,
            and at least one node has 'cluster-require-full-coverage' set to
            'yes,' the server will throw a ClusterDownError for some key-based
            commands. See -
            https://redis.io/topics/cluster-tutorial#redis-cluster-configuration-parameters
            When set to True: all slots must be covered to construct the
            cluster client. If not all slots are covered, RedisClusterException
            will be thrown.
        :param read_from_replicas:
             Enable read from replicas in READONLY mode. You can read possibly
             stale data.
             When set to true, read commands will be assigned between the
             primary and its replications in a Round-Robin manner.
         :param dynamic_startup_nodes:
             Set the RedisCluster's startup nodes to all of the discovered nodes.
             If true (default value), the cluster's discovered nodes will be used to
             determine the cluster nodes-slots mapping in the next topology refresh.
             It will remove the initial passed startup nodes if their endpoints aren't
             listed in the CLUSTER SLOTS output.
             If you use dynamic DNS endpoints for startup nodes but CLUSTER SLOTS lists
             specific IP addresses, it is best to set it to false.
        :param cluster_error_retry_attempts:
             Number of times to retry before raising an error when
             :class:`~.TimeoutError` or :class:`~.ConnectionError` or
             :class:`~.ClusterDownError` are encountered
        :param reinitialize_steps:
            Specifies the number of MOVED errors that need to occur before
            reinitializing the whole cluster topology. If a MOVED error occurs
            and the cluster does not need to be reinitialized on this current
            error handling, only the MOVED slot will be patched with the
            redirected node.
            To reinitialize the cluster on every MOVED error, set
            reinitialize_steps to 1.
            To avoid reinitializing the cluster on moved errors, set
            reinitialize_steps to 0.
        :param address_remap:
            An optional callable which, when provided with an internal network
            address of a node, e.g. a `(host, port)` tuple, will return the address
            where the node is reachable.  This can be used to map the addresses at
            which the nodes _think_ they are, to addresses at which a client may
            reach them, such as when they sit behind a proxy.

         :**kwargs:
             Extra arguments that will be sent into Redis instance when created
             (See Official redis-py doc for supported kwargs
         [https://github.com/andymccurdy/redis-py/blob/master/redis/client.py])
             Some kwargs are not supported and will raise a
             RedisClusterException:
                 - db (Redis do not support database SELECT in cluster mode)
        Nr   z4Argument 'db' is not possible to use in cluster modeFTpathzFRedisCluster does not currently support Unix Domain Socket connectionsr   z9A ``db`` querystring option can only be 0 in cluster moder2   r3   a5  RedisCluster requires at least one node to discover the cluster. Please provide one of the followings:
1. host and port, for example:
 RedisCluster(host='localhost', port=6379)
2. list of startup nodes, for example:
 RedisCluster(startup_nodes=[ClusterNode('localhost', 6379), ClusterNode('localhost', 6378)])r   r   r   r|   r   strictr   )r  r  r  r  r  r7   )&r#   r   updaterd   rs   r  rr   r   user_on_connect_func
on_connectr   r   r+   r   r   encoderr  	__class__r  copycommand_flagsr
  
node_flagsr  reinitialize_counterr  NodesManagerr   r   r  cluster_response_callbacksr  result_callbacksr   commands_parser	threadingr*   _lock)r   r2   r3   r  r  r   r  r  r  r  r  r  r   r  url_optionss                  r9   __init__zRedisCluster.__init__  s   X  M6>'F 
 ?H#C.K$+)  {"{4'8A'=+O  MM+&::f%D::fd+D  T4!89$"2  T4!891$'4  %+JJ/CT$J!+T__=>)&)DJMM7DJJ/0MM7E/*;Q$?@AJJz7+JJ((3JJ)51

 -I)!^^99>>@..3388:"4$%!"4) 
'"7"7'
 
 +>NN>>+
' !4DNN4S4S T-d3^^%
r;   c                     | S rD   r7   r   s    r9   	__enter__zRedisCluster.__enter__  s    r;   c                 $    | j                          y rD   closer   exc_type	exc_value	tracebacks       r9   __exit__zRedisCluster.__exit__      

r;   c                 $    | j                          y rD   r6  r3  s    r9   __del__zRedisCluster.__del__  r=  r;   c                     | j                         D ]4  }|j                  s	 |j                  j                  j                          6 y # t        $ r Y Cw xY wrD   )	get_nodesredis_connectionr>   
disconnectOSErrorr   rx   s     r9   disconnect_connection_poolsz(RedisCluster.disconnect_connection_pools  sR    NN$ 	D$$))99DDF	  s   $A			AAc                    |j                  t               |j                          | j                  r8|j	                  d       t        |j                               dk7  rt        d      | j                  | j                  |       yy)z
        Initialize the connection, authenticate and select a database and send
         READONLY if it is set during object initialization.
        r   OKzREADONLY command failedN)	
set_parserr   r#  r  send_commandr1   read_responser   r"  )r   r=   s     r9   r#  zRedisCluster.on_connect  sx    
 	m,"" ##J/J44674?%&?@@$$0%%j1 1r;   c                     |j                   sI| j                  5  |j                   s| j                  j                  |g       d d d        |j                   S |j                   S # 1 sw Y   |j                   S xY wrD   )rB  r/  r   create_redis_connectionsrE  s     r9   get_redis_connectionz!RedisCluster.get_redis_connection  sk    $$ H,,&&??GH $$$t$$$H $$$s   )A""A6c                 <    | j                   j                  |||      S rD   )r   get_noder   r2   r3   rJ   s       r9   rP  zRedisCluster.get_node  s    !!**4yAAr;   c                 @    | j                   j                  t              S rD   )r   get_nodes_by_server_typePRIMARYr3  s    r9   r   zRedisCluster.get_primaries      !!::7CCr;   c                 @    | j                   j                  t              S rD   )r   rS  REPLICAr3  s    r9   r   zRedisCluster.get_replicas  rU  r;   c                     t        j                  t        | j                  j                  j                                     S rD   )r   r   r   r   nodes_cacherP   r3  s    r9   get_random_nodezRedisCluster.get_random_node  s+    }}T$"4"4"@"@"G"G"IJKKr;   c                 \    t        | j                  j                  j                               S rD   )r   r   rY  rP   r3  s    r9   rA  zRedisCluster.get_nodes  s"    D&&2299;<<r;   c                    | j                  |      }| j                  j                  j                  |      }|t	        |      dk(  rt        d| d      |r&t	        | j                  j                  |         dk  ry|rd}||   S d}||   S )z
        Get the node that holds the key's slot.
        If replica set to True but the slot doesn't have any replicas, None is
        returned.
        Nr   Slot "z " is not covered by the cluster.rn   r_   )keyslotr   slots_cacherd   rr   r&   )r   keyrj   rg   
slot_cachenode_idxs         r9   get_node_from_keyzRedisCluster.get_node_from_key  s     ||C ''3377=
ZA!5%tf4T&UVVs4--99$?@1DH
 (## H(##r;   c                 .    | j                   j                  S )z0
        Get the cluster's default node
        )r   r   r3  s    r9   r   zRedisCluster.get_default_node  s     !!...r;   c                 d    || j                  |j                        y|| j                  _        y)z
        Set the default node of the cluster.
        :param node: 'ClusterNode'
        :return True if the default node was set, else False
        rJ   FT)rP  namer   r   rE  s     r9   set_default_nodezRedisCluster.set_default_node  s1     <4==499==E*.'r;   r4   c                     | j                   S rD   )r   r3  s    r9   	get_retryzRedisCluster.get_retry  s    zzr;   c                 r    || _         | j                         D ]  }|j                  j                  |        y rD   )r   rA  rB  	set_retry)r   r   rx   s      r9   rl  zRedisCluster.set_retry  s3    
NN$ 	3D!!++E2	3r;   c                     || j                         }|j                  t        d|j                   d      |j                  j	                         S )ai  
        Returns a Monitor object for the specified target node.
        The default cluster node will be selected if no target node was
        specified.
        Monitor is useful for handling the MONITOR command to the redis server.
        next_command() method returns one command from monitor
        listen() method yields commands from monitor.
        zCluster Node z has no redis_connection)r   rB  r#   rg  monitor)r   r   s     r9   rn  zRedisCluster.monitor  s\     //1K''/' 0 011IJ  ++3355r;   c                 "    t        | f|||d|S )z~
        Allows passing a ClusterNode, or host&port, to get a pubsub instance
        connected to the specified node
        )rx   r2   r3   )ClusterPubSub)r   rx   r2   r3   r   s        r9   pubsubzRedisCluster.pubsub  s    
 TM4dMfMMr;   c                 &   |rt        d      |rt        d      t        | j                  | j                  | j                  j                  | j
                  | j                  | j                  | j                  | j                  | j                  	      S )ac  
        Cluster impl:
            Pipelines do not work in cluster mode the same way they
            do in normal mode. Create a clone of this object so
            that simulating pipelines will work correctly. Each
            command will be called directly when used and
            when calling execute() will only return the result stack.
        z(shard_hint is deprecated in cluster modez)transaction is deprecated in cluster mode)	r   r-  r  r,  r+  r  r  r  lock)r#   ClusterPipeliner   r-  r  r,  r+  r  r  r  r/  )r   transaction
shard_hints      r9   pipelinezRedisCluster.pipeline
  s     '(RSS'(STT,, 00,,::!22'+'F'F)-)J)J#66#66

 
	
r;   c           	      0    |t         } || ||||||      S )aL  
        Return a new Lock object using key ``name`` that mimics
        the behavior of threading.Lock.

        If specified, ``timeout`` indicates a maximum life for the lock.
        By default, it will remain locked until release() is called.

        ``sleep`` indicates the amount of time to sleep per loop iteration
        when the lock is in blocking mode and another client is currently
        holding the lock.

        ``blocking`` indicates whether calling ``acquire`` should block until
        the lock has been acquired or to fail immediately, causing ``acquire``
        to return False and the lock not being acquired. Defaults to True.
        Note this value can be overridden by passing a ``blocking``
        argument to ``acquire``.

        ``blocking_timeout`` indicates the maximum amount of time in seconds to
        spend trying to acquire the lock. A value of ``None`` indicates
        continue trying forever. ``blocking_timeout`` can be specified as a
        float or integer, both representing the number of seconds to wait.

        ``lock_class`` forces the specified lock implementation. Note that as
        of redis-py 3.0, the only lock class we implement is ``Lock`` (which is
        a Lua-based lock). So, it's unlikely you'll need this parameter, unless
        you have created your own custom lock class.

        ``thread_local`` indicates whether the lock token is placed in
        thread-local storage. By default, the token is placed in thread local
        storage so that a thread only sees its token, not a token set by
        another thread. Consider the following timeline:

            time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds.
                     thread-1 sets the token to "abc"
            time: 1, thread-2 blocks trying to acquire `my-lock` using the
                     Lock instance.
            time: 5, thread-1 has not yet completed. redis expires the lock
                     key.
            time: 5, thread-2 acquired `my-lock` now that it's available.
                     thread-2 sets the token to "xyz"
            time: 6, thread-1 finishes its work and calls release(). if the
                     token is *not* stored in thread local storage, then
                     thread-1 would see the token value as "xyz" and would be
                     able to successfully release the thread-2's lock.

        In some use cases it's necessary to disable thread local storage. For
        example, if you have code where one thread acquires a lock and passes
        that lock instance to a worker thread to release later. If thread
        local storage isn't disabled in this case, the worker thread won't see
        the token set by the thread that acquired the lock. Our assumption
        is that these cases aren't common and as such default to using
        thread local storage.)timeoutsleepblockingblocking_timeoutthread_localr)   )r   rg  ry  rz  r{  r|  
lock_classr}  s           r9   rs  zRedisCluster.lock%  s2    | J-%
 	
r;   c                 "    || j                   |<   y)zSet a custom Response CallbackN)r+  )r   rF   callbacks      r9   set_response_callbackz"RedisCluster.set_response_callbacko  s    3;''0r;   c                    |d   j                         }t        |      dk\  rB|d    d|d    j                         | j                  v r|d    d|d    j                         }|j                  dd       }||}n| j                  j	                  |      }|| j
                  j                  k(  r| j                         gS || j
                  j                  k(  r| j                         S || j
                  j                  k(  r| j                         S || j
                  j                  k(  r| j                         S || j
                  j                  k(  r| j                  j                   gS || j
                  j"                  d   v r| j                  j                   gS  | j$                  | }| j                  j'                  || j(                  xr |t*        v       }|gS )Nr   rn    r_   r   )upperrr   r'  r   rd   r%  r  rZ  r  r   r  r   r  rA  r	  r   r   r  determine_slotget_node_from_slotr  r   )r   rA   r   rF   r   command_flagrg   rx   s           r9   _determine_nodeszRedisCluster._determine_nodess  s    q'--/t9>a	47)4::<@R@RRa	47),224GZZd3
!%L  --11':L4>>000((*++T^^555%%''T^^444$$&&T^^555>>##T^^888&&334466q99&&3344 '4&&-D%%88d--J']2JD 6Mr;   c                 Z    | j                   dk(  ry| j                  | j                   z  dk(  S )Nr   F)r  r)  r3  s    r9   _should_reinitializedz"RedisCluster._should_reinitialized  s1    
 ""a',,t/F/FF!KKr;   c                 N    | j                   j                  |      }t        |      S )z
        Calculate keyslot for a given key.
        See Keys distribution model in https://redis.io/topics/cluster-spec
        )r$  encoder   )r   r`  r   s      r9   r^  zRedisCluster.keyslot  s"    
 LL${r;   c                 p    | j                         j                  } | j                  j                  |g| S )a  
        Get the keys in the command. If the command has no keys in in, None is
        returned.

        NOTE: Due to a bug in redis<7.0, this function does not work properly
        for EVAL or EVALSHA when the `numkeys` arg is 0.
         - issue: https://github.com/redis/redis/issues/9493
         - fix: https://github.com/redis/redis/pull/9733

        So, don't use this function with EVAL or EVALSHA.
        )r   rB  r-  get_keys)r   rA   
redis_conns      r9   _get_command_keyszRedisCluster._get_command_keys  s6     **,==
,t##,,Z?$??r;   c                    |d   }| j                   j                  |      t        k(  r|d   S |j                         dv r]t	        |      dk  rt        d|       t        |d         }|dd|z    }t	        |      dk(  rt        j                  dt              S |}nY | j                  | }|t	        |      dk(  r:|j                         dv rt        j                  dt              S t        d|       t	        |      dk(  r| j                  |d         S |D ch c]  }| j                  |       }}t	        |      dk7  rt        | d	      |j                         S c c}w )
a  
        Figure out what slot to use based on args.

        Raises a RedisClusterException if there's a missing key and we can't
            determine what slots to map the command to; or, if the keys don't
            all map to the same key slot.
        r   r_   )EVALEVALSHArn   zInvalid args in command: ra   )FCALLFCALL_ROzNo way to dispatch this command to Redis Cluster. Missing key.
You can execute the command by specifying target nodes.
Command: z) - all keys must map to the same key slot)r'  rd   r  r  rr   r#   intr   	randranger   r  r^  r   )r   rA   rF   num_actual_keys	eval_keyskeysr`  rf   s           r9   r  zRedisCluster.determine_slot  sy    q'!!'*g57N ==?114yA~+.Gv,NOO!$q'lOQ_!45I 9~"''+CDDD)4))40D|s4yA~ ==?&;;!++A/GHH+//3f6  t9><<Q(( /33sc"33u:?')DE  yy{ 4s   E!c                     | j                   S )z.
        Get the connections' encoder
        )r$  r3  s    r9   get_encoderzRedisCluster.get_encoder  s     ||r;   c                 .    | j                   j                  S )z9
        Get the connections' key-word arguments
        )r   r   r3  s    r9   get_connection_kwargsz"RedisCluster.get_connection_kwargs  s     !!333r;   c                 B    t        |t              xr || j                  v S rD   )ro   re   r(  )r   target_nodess     r9   _is_nodes_flagzRedisCluster._is_nodes_flag   s    ,,P1PPr;   c                     t        |t              r|}|S t        |t              r|g}|S t        |t              r|j	                         }|S t        dt        |             )Nztarget_nodes type can be one of the following: node_flag (PRIMARIES, REPLICAS, RANDOM, ALL_NODES),ClusterNode, list<ClusterNode>, or dict<any, ClusterNode>. The passed type is )ro   r   r  rp   rP   	TypeErrortype)r   r  rm   s      r9   _parse_target_nodesz RedisCluster._parse_target_nodes  s{    lD) E   k2!NE  d+ !'')E  & '+<&8%9; r;   c                    d}d}d}|j                  dd      }|$| j                  |      s| j                  |      }d}|rdn| j                  }d|z   }t	        |      D ]  }		 i }
|sM | j
                  |i |d|i}|st        d| d	      t        |      dk(  r|d   | j                         k(  rd}|D ]%  } | j                  |g|i ||
|j                  <   '  | j                  |d   |
fi |c S  y# t        $ rI}|dkD  r=t        |      | j                  j                  v r|r| j!                          |dz  }Y d}~|d}~ww xY w)
a"  
        Wrapper for ERRORS_ALLOW_RETRY error handling.

        It will try the number of times specified by the config option
        "self.cluster_error_retry_attempts" which defaults to 3 unless manually
        configured.

        If it reaches the number of times, the command will raise the exception

        Key argument :target_nodes: can be passed with the following types:
            nodes_flag: PRIMARIES, REPLICAS, ALL_NODES, RANDOM
            ClusterNode
            list<ClusterNode>
            dict<Any, ClusterNode>
        FNr  Tr   r_   r   !No targets were found to execute  command on)r   r  r  r  rq   r  r#   rr   r   _execute_commandrg  _process_result	Exceptionr  r%  r  r  )r   rA   r   target_nodes_specifiedis_default_noder  passed_targetsretry_attemptsexecute_attempts_rG   rx   es                r9   execute_commandzRedisCluster.execute_command  s     "'ND9%d.A.A..Q33NCL%)" (AT-N-N 	 ~-'(  	A-#84#8#8$!'$4B$L (3?v[Q  L)Q.(Ot/D/D/FF*.( RD%:T%:%:4%Q$%Q&%QC		NR ,t++DGSCFCC) 	*  !A%$q'T^^5V5V*V&113 #a'N Gs   #BC99	E=EEEc                    |d   }d}d}d}d}d}	t        | j                        }
|
dkD  r|
dz  }
	 |r| j                  |      }nC|	rA | j                  | }| j                  j                  || j                  xr |t        v       }d}	| j                  |      }t        |g|i |}|r'|j                  d        |j                  |dfi | d} |j                  |   |j                  ||fi |}|| j                  v r | j                  |   |fi |}|||j                  j                  |       S S tM        d      # t        $ r  t         t"        f$ rj}||j%                          | j                  j&                  j)                  |j*                  d       d|_        | j                  j/                          |d}~wt0        $ rn}| xj2                  dz  c_        | j5                         r"| j                  j/                          d| _        n| j                  j7                  |       d}	Y d}~nd}~wt8        $ r* |
| j                  dz  k  rt;        j<                  d	       Y nt>        $ r-}tA        |jB                  |jD                  
      }d}Y d}~nnd}~wtF        $ r6}t;        j<                  d       | j                  j/                          |d}~wtH        $ r  tJ        $ r}|r|j%                          |d}~ww xY w|?|j                  j                  |       n## ||j                  j                  |       w w xY w|
dkD  r&)z9
        Send a command to a node in the cluster
        r   NFr_   rf  ASKINGTrn   g?r8   g      ?zTTL exhausted.)'r  r  rP  r  r   r  r  r   rN  r?   rJ  parse_responser+  r>   releaser   r   r'   rC  r  r   rg  rB  
initializer"   r)  r  update_moved_exceptionr(   timerz  r   r:   r2   r3   r   r%   r  r   )r   r   rA   r   rF   r@   r=   redirect_addraskingmovedttlrg   rK   r  s                 r9   r  zRedisCluster._execute_command]  sG    q'

$--.Ag1HCRC"&----"HK /4..5D"&"4"4"G"Gd55R']:R#K "E!66{C
+JHHH
++H5-J--j(MfM"F'
''.4:44ZSFSd===Gt>>wG  $* H  p )..66zB * +,,u ' #\2  )))+ ""0044[5E5EtL/3,""--/  ))Q.)--/&&11301D-&&==a@  %44q88JJt$  -166 G#  

4 ""--/   ))+
 )..66zB )..66zB *g Agsn   C)E LA%GLA$H61L' 63L)L' +L3#JL' L'1KL-LLL' ' Mc                     	 | j                   5  | j                  r| j                  j                          d d d        y # 1 sw Y   y xY w# t        $ r Y y w xY wrD   )r/  r   r7  AttributeErrorr3  s    r9   r7  zRedisCluster.close  sQ    	 /%%&&,,./ / /  		s+   A
 '>A
 AA
 A
 
	AAc                     || j                   v r | j                   |   ||fi |S t        |      dk(  rt        |j                               d   S |S )a  
        Process the result of the executed command.
        The function would return a dict or a single value.

        :type command: str
        :type res: dict

        `res` should be in the following format:
            Dict<node_name, command_result>
        r_   r   )r,  rr   r   rP   )r   rF   rG   r   s       r9   r  zRedisCluster._process_result  sY     d+++14((1'3I&IIX] 

%a((Jr;   c                     t        | ||       y)a  
        This function can be used to add externally defined redis modules,
        and their namespaces to the redis client.

        ``funcname`` - A string containing the name of the function to create
        ``func`` - The function, being added to this class.
        N)setattr)r   funcnamefuncs      r9   load_external_modulez!RedisCluster.load_external_module  s     	h%r;   )Ni  Nra   NF   FTNNNNN)F)r   r+   r4   NrD   NN)Ng?TNNT)0r   r   r   classmethodr  r   re   r  r   boolr   r	   r1  r4  r<  r?  rF  r#  rN  rP  r   r   rZ  rA  rc  r   rh  rj  rl  rn  rq  rw  rs  r  r  r  r^  r  r  r  r  r  r  r  r  r7  r  r  r7   r;   r9   r  r    s   )& )&Z #7;,-#'&+"##(&*!IM\&sm\& \&  ] 34	\&
 '*\&  \&  $\&  \& !\&  $\& c]\&  #sU38_)D EF\&|2*%BDDL=$(/	8G, 3
6"N
< H
T<%43F %NL@7r4Q(DLb-H(&r;   r  c                   &    e Zd ZddZd Zd Zd Zy)r  Nc                     |dk(  rt        j                  |      }|| _        || _        t	        ||      | _        || _        || _        y )N	localhost)socketgethostbynamer2   r3   r:   rg  server_typerB  )r   r2   r3   r  rB  s        r9   r1  zClusterNode.__init__  sF    ;''-D		!$-	& 0r;   c                     d| j                    d| j                   d| j                   d| j                   d| j                   dS )Nz[host=z,port=z,name=z,server_type=z,redis_connection=])r2   r3   rg  r  rB  r3  s    r9   __repr__zClusterNode.__repr__  sW    TYYK  II; II; ++, -  $ 5 56a	9	
r;   c                 X    t        |t              xr |j                  | j                  k(  S rD   )ro   r  rg  )r   objs     r9   __eq__zClusterNode.__eq__  s!    #{+EDII0EEr;   c                 R    | j                   | j                   j                          y y rD   )rB  r7  r3  s    r9   r?  zClusterNode.__del__   s%      ,!!'') -r;   r  )r   r   r   r1  r  r  r?  r7   r;   r9   r  r    s    1
F*r;   r  c                   >    e Zd ZdZd
deddfdZdededefdZdd	Zy)LoadBalancerz$
    Round-Robin Load Balancing
    start_indexr4   Nc                      i | _         || _        y rD   )primary_to_idxr  )r   r  s     r9   r1  zLoadBalancer.__init__
  s     &r;   rb   	list_sizec                 |    | j                   j                  || j                        }|dz   |z  | j                   |<   |S )Nr_   )r  
setdefaultr  )r   rb   r  server_indexs       r9   get_server_indexzLoadBalancer.get_server_index  s@    **55gt?O?OP(4q(8I'EG$r;   c                 8    | j                   j                          y rD   )r  clearr3  s    r9   resetzLoadBalancer.reset  s    !!#r;   )r   )r4   N)	r   r   r   __doc__r  r1  re   r  r  r7   r;   r9   r  r    s9    'C ' '   $r;   r  c                       e Zd Zddddedfdeeeegeeef   f      fdZ	ddZ
d Zd Zdd	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zdededeeef   fdZy)r*  FNTr  c                 8   i | _         i | _        i | _        d | _        | j	                  |       || _        || _        || _        || _        || _	        d | _
        || _        t               | _        |t        j                         }|| _        | j#                          y rD   )rY  r_  r  r   populate_startup_nodesr  _require_full_coverage_dynamic_startup_nodesr   r  _moved_exceptionr   r  read_load_balancerr.  r*   r/  r  )	r   r  r  r  rs  r  r   r  r   s	            r9   r1  zNodesManager.__init__  s      ##M2 &;#&;#%:"* $!'"..<>>#D
r;   c                     |rB|r@|dk(  rt        j                  |      }| j                  j                  t	        ||            S |r| j                  j                  |      S y)z
        Get the requested node from the cluster's nodes.
        nodes.
        :return: ClusterNode if the node exists, else None
        r  r8   N)r  r  rY  rd   r:   rQ  s       r9   rP  zNodesManager.get_node6  s\     D{"++D1##''4d(KLL##''	22r;   c                     || _         y rD   )r  )r   	exceptions     r9   r  z#NodesManager.update_moved_exceptionF  s
     )r;   c                     | j                   }| j                  |j                  |j                        }||j                  t
        urJt
        |_        n>t        |j                  |j                  t
              }|| j                  |j                  <   || j                  |j                     v r| j                  |j                     d   }t        |_        | j                  |j                     j                  |       | j                  |j                     j                  |       || j                  |j                     d<   | j                  |k(  r)|| _        d| _         y|g| j                  |j                  <   d| _         y)z@
        Update the slot's node with the redirected one
        r8   Nr   )r  rP  r2   r3   r  rT  r  rY  rg  r_  slot_idrW  rs   remover   )r   r  redirected_nodeold_primarys       r9   _update_moved_slotsz NodesManager._update_moved_slotsI  sE    !!--QVV!&&-A&**'9.5+ *!&&!&&'BO5DD_112d..qyy99 **1995a8K '.K#QYY'..{; QYY'..?-<DQYY'*  K/$3! !% ,;*;DQYY' $r;   c                    | j                   r1| j                  5  | j                   r| j                          ddd       | j                  j	                  |      t        | j                  |         dk(  rt        d| d| j                   d      |du rO| j                  |   d   j                  }| j                  j                  |t        | j                  |               }nX|$|t        k(  st        | j                  |         dk(  rd}n/t        j                  dt        | j                  |         dz
        }| j                  |   |   S # 1 sw Y   xY w)z9
        Gets a node that servers this hash slot
        Nr   r]  z5" not covered by the cluster. "require_full_coverage="Tr_   )r  r/  r  r_  rd   rr   r&   r  rg  r  r  rT  r   randint)r   rg   r  r  primary_namerb  s         r9   r  zNodesManager.get_node_from_slotp  sX       /((,,./ %-T5E5Ed5K1LPQ1Q% **.*E*E)FaI 
 %++D1!499L..??c$"2"24"89H g%4##D)*a/ H ~~aT-=-=d-C)Dq)HIH%h//9/ /s   EEc                 z    | j                   j                         D cg c]  }|j                  |k(  r| c}S c c}w )z
        Get all nodes with the specified server type
        :param server_type: 'primary' or 'replica'
        :return: list of ClusterNode
        )rY  rP   r  )r   r  rx   s      r9   rS  z%NodesManager.get_nodes_by_server_type  sA     ((//1
;. 
 	
 
s   8c                 D    |D ]  }|| j                   |j                  <    y)zK
        Populate all startup nodes and filters out any duplicates
        N)r  rg  )r   rm   ns      r9   r  z#NodesManager.populate_startup_nodes  s'      	+A)*Dqvv&	+r;   c                 :    t        dt              D ]  }||vs y y)Nr   FT)rq   r   )r   r_  rw   s      r9   check_slots_coveragez!NodesManager.check_slots_coverage  s+     q23 	A#	 r;   c                     |D ]G  }|j                    | j                  d|j                  |j                  d| j                  |_         I y)zV
        This function will create a redis connection to all nodes in :nodes:
        Nr8   r7   )rB  create_redis_noder2   r3   r   )r   rm   rx   s      r9   rM  z%NodesManager.create_redis_connections  sT      	D$$,(>(>(> ))6:6L6L)%	r;   c                     | j                   rD|j                  d|i       |j                  d|i       t         | j                  di |      }|S t        d||d|}|S )Nr2   r3   )r>   r8   r7   )r  r!  r   r   )r   r2   r3   r   rM   s        r9   r  zNodesManager.create_redis_node  sf    ==MM64.)MM64.)&@d&@&@&J6&JKA  54d5f5Ar;   c                     t        ||      }|j                  |      }|L| j                  j                  |      }||j                  t	        |||      }|j
                  |k7  r||_        |S rD   )r:   rd   rY  rB  r  r  )r   r2   r3   roletmp_nodes_cacherJ   r   s          r9   _get_or_create_cluster_nodez(NodesManager._get_or_create_cluster_node  sw    !$-	%)))4 **..y9K"k&B&B&J)$d;&&$.*.'r;   c           
         | j                          i }i }g }d}d}| j                  }d}| j                  j                         D ]  }	 |j                  r|j                  }	nF | j
                  |j                  |j                  fi |}	|	| j                  |j                     _        t        |	j                         j                  d            du rt        d      t        |	j                  d            }
d}t!        |
      dk(  rDt!        |
d   d	   d         dk(  r-t!        | j                        dk(  r|j                  |
d   d	   d<   |
D ]  }|d	   }t        |d         }|d
k(  r|j                  }t#        |d         }| j%                  ||      \  }}| j'                  ||t(        |      }|||j                  <   t+        t#        |d         t#        |d         dz         D ]*  }||vrg ||<   ||   j-                  |       t+        dt!        |            D cg c]  }||   	 }}|D ]e  }t        |d         }|d   }| j%                  ||      \  }}| j'                  ||t.        |      }||   j-                  |       |||j                  <   g ||   d   }|j                  |j                  k7  s|j-                  |j                   d|j                   d|        t!        |      dkD  st        ddj1                  |               | j3                  |      }|s n |st        dt5        |             || j7                  t9        |j                                      |s+| j:                  rt        dt!        |       dt<         d      || _        || _         | jC                  t(              d   | _"        | jF                  r|| _        d| _$        y# t        $ r}|}Y d}~d}~ww xY wc c}w )z
        Initializes the nodes cache, slots cache and redis connections.
        :startup_nodes:
            Responsible for discovering other nodes in the cluster
        FNcluster_enabledz(Cluster mode is not enabled on this noder   Tr_   r   rn   r\   ra   z vs z
 on slot: r  z6startup_nodes could not agree on a valid slots cache: z, zORedis Cluster cannot be connected. Please provide at least one reachable node: z9All slots are not covered after query all startup_nodes. z of z covered...)%r  r   r  rP   rB  r  r2   r3   rg  r  inford   r#   r1   r  r  rr   r  remap_host_portr  rT  rq   rs   rW  joinr  re   rM  r   r  r   rY  r_  rS  r   r  r  )r   r  	tmp_slotsdisagreementsstartup_nodes_reachablefully_coveredr   r  startup_noderM   cluster_slotsr  rg   primary_noder2   r3   r   rw   jreplica_nodesreplica_nodetarget_replica_nodetmp_slots                          r9   r  zNodesManager.initialize  se    	

	"'''	 ..557 Y	L00$55A /..$))<+<+<@FA NOD&&|'8'89J%678EA/B  !-Q->->-O P*.' M"a'a(+A./14**+q0)5):):a #A&% ."#Aw#LO42:',,D<?+!11$=
d">>$ 5@ 0 01s47|Sa\A-=>  "A	)')	!!!++K8:?3t9:M(NQa(N(N,9 4L#/Q#@D#/?D)-)=)=dD)IJD$262R2R $dG_3/ &aL//0CD !4 , 3 8 84  $-Q<?#==K,<,<<)00#+==/k6F6F5GzRSQT U  #=1A5&;'448IIm4L3M%O'" !"; "."` !55i@M sY	v ''''*9~&68  	%%d?+A+A+C&DE !<!< (Ky>"$'?&@ A  +$ 99'B1E&&!0D $M   		L )Os   B0O#O<
#	O9,O44O9c                     d | _         | j                  j                         D ])  }|j                  s|j                  j	                          + y rD   )r   rY  rP   rB  r7  rE  s     r9   r7  zNodesManager.closeZ  sC     $$++- 	.D$$%%++-	.r;   c                 X    	 | j                   j                          y # t        $ r Y y w xY wrD   )r  r  r  r3  s    r9   r  zNodesManager.reset`  s+    	##))+ 		s    	))r2   r3   r4   c                 H    | j                   r| j                  ||f      S ||fS )z
        Remap the host and port returned from the cluster to a different
        internal value.  Useful if the client is not connecting directly
        to the cluster.
        )r  )r   r2   r3   s      r9   r
  zNodesManager.remap_host_portg  s,     %%tTl33Tzr;   r  )FN)r   r   r   r   r   r   re   r  r	   r1  rP  r  r  r  rS  r  r  rM  r  r  r  r7  r  r
  r7   r;   r9   r*  r*    s     #",IM  #sU38_)D EF: *%%N!0F

+"E%N.C s uS#X r;   r*  c                   z     e Zd ZdZ	 	 	 	 d fd	ZddZd Zd Zd Zd Z	d Z
d	 Z	 dd
Zd Zd Zd Zd Z xZS )rp  z
    Wrapper for PubSub class.

    IMPORTANT: before using ClusterPubSub, read about the known limitations
    with pubsub in Cluster mode and learn how to workaround them:
    https://redis-py-cluster.readthedocs.io/en/stable/pubsub.html
    c                 "   d| _         | j                  ||||       | j                   dn$|j                  | j                         j                  }|| _        i | _        | j                         | _        t        |    d||j                  |d| y)a*  
        When a pubsub instance is created without specifying a node, a single
        node will be transparently chosen for the pubsub connection on the
        first command execution. The node will be determined by:
         1. Hashing the channel name in the request to find its keyslot
         2. Selecting a node that handles the keyslot: If read_from_replicas is
            set to true, a replica can be selected.

        :type redis_cluster: RedisCluster
        :type node: ClusterNode
        :type host: str
        :type port: int
        N)r>   r$  push_handler_funcr7   )
rx   set_pubsub_noderN  r>   clusternode_pubsub_mapping_pubsubs_generatorsuperr1  r$  )	r   redis_clusterrx   r2   r3   r  r   r>   r%  s	           r9   r1  zClusterPubSub.__init__{  s    , 	]D$= yy  33DII>NN 	
 %#% "&"9"9"; 	
+!))/	
 		
r;   c                    |2| j                  |||j                  |j                         |}|| _        y|3|1|j                  ||      }| j                  ||||       |}|| _        yt	        ||g      du rt        d      d}|| _        y)aN  
        The pubsub node will be set according to the passed node, host and port
        When none of the node, host, or port are specified - the node is set
        to None and will be determined by the keyslot of the channel in the
        first command to be executed.
        RedisClusterException will be thrown if the passed node does not exist
        in the cluster.
        If host is passed without port, or vice versa, a DataError will be
        thrown.
        :type cluster: RedisCluster
        :type node: ClusterNode
        :type host: str
        :type port: int
        Nr8   Tz6Passing a host requires passing a port, and vice versa)_raise_on_invalid_noder2   r3   rP  anyr    rx   )r   r  rx   r2   r3   pubsub_nodes         r9   r  zClusterPubSub.set_pubsub_node  s     ''tyy$))LK  	 $"2##D#9D''tTBK  	 $$&TUU K	r;   c                     | j                   S )zJ
        Get the node that is being used as the pubsub connection
        )rx   r3  s    r9   get_pubsub_nodezClusterPubSub.get_pubsub_node  s     yyr;   c                 d    ||j                  |j                        t        d| d| d      y)zl
        Raise a RedisClusterException if the node is None or doesn't exist in
        the cluster.
        Nrf  zNode r6   z doesn't exist in the cluster)rP  rg  r#   )r   r#  rx   r2   r3   s        r9   r%  z$ClusterPubSub._raise_on_invalid_node  sF    
 <=11DII1FN'vQtf$AB  Or;   c                    | j                   S| j                  t        |      dkD  r[|d   }| j                  j	                  |      }| j                  j
                  j                  || j                  j                        }n| j                  j                         }|| _	        | j                  j                  |      }|j                  | _        | j                  j                  d| j                        | _         | j                   j                  | j                         | j                  5t         s/| j                   j"                  j%                  | j                         | j                   } | j&                  ||j(                  g|  y)z
        Execute a subscribe/unsubscribe command.

        Taken code from redis-py and tweak to make it work within a cluster.
        Nr_   rq  )r=   r>   rr   r  r^  r   r  r  rZ  rx   rN  r?   rv  _register_connect_callbackr#  r  r,   _parserset_push_handler_executerJ  )r   rA   rT   rg   rx   rB  r=   s          r9   r  zClusterPubSub.execute_command  s5    ??"##+t9q= #1gG<<//8D<<55HHdll==D
  <<779D 	#'<<#D#DT#J '7'G'G$"22AA$//DO
 OO66tG%%1:K''889O9OP__
j*"9"9ADAr;   c                     	 | j                   |j                     S # t        $ rD |j                  j	                  | j
                        }|| j                   |j                  <   |cY S w xY w)N)r  )r   rg  rQ   rB  rq  r  )r   rx   rq  s      r9   _get_node_pubsubzClusterPubSub._get_node_pubsub  sj    	++DII66 	**11"&"8"8 2 F 39D$$TYY/M	s    A
A('A(c                     t        t        | j                              D ]-  }t        | j                        }|j                         }|+|c S  y rD   )rq   rr   r   nextr!  get_message)r   r  rq  messages       r9   _sharded_message_generatorz(ClusterPubSub._sharded_message_generator  sO    s43345 	A$112F((*G"		
 r;   c              #   T   K   	 | j                   j                         D ]  }|  $wrD   )r   rP   r   rq  s     r9   r!  z ClusterPubSub._pubsubs_generator
  s1     2299;  s   &(c                    |r+| j                   |j                     j                  ||      }n| j                         }|y t	        |d         dk(  r|d   | j
                  v r| j
                  j                  |d          | j                  j                  |d   d        | j                  j                  |d         }| j                   |j                     j                  du r%| j                   j                  |j                         | j                  s2| j                  s&| j                  s| j                  j                          | j                   s|ry |S )N)ignore_subscribe_messagesry  r  sunsubscriberT   F)r   rg  r4  r6  r1   "pending_unsubscribe_shard_channelsr  shard_channelsr   r  rc  
subscribedchannelspatternssubscribed_eventr  r:  )r   r:  ry  r   r5  rx   s         r9   get_sharded_messagez!ClusterPubSub.get_sharded_message  s.    ..{/?/?@LL*CW M G 557G?'&/*n<y!T%L%LL77>>wy?QR##''	(:DA||55gi6HI++DII6AAUJ,,00;}}T]]4;N;N !!'')))-Fr;   c                 h   |rt        |d   |dd        }t        j                  |      }|j                  |       |j	                         D ]  \  }}| j
                  j                  |      }| j                  |      }|r |j                  di ||i n|j                  |       | j                  j                  |j                         | j                  j                  | j                  |d i             |j                  s| j                  r| j                  j                          d| _         y )Nr   r_   r7   )r   rp   fromkeysr!  rE   r  rc  r1  
ssubscriber=  r<  difference_update_normalize_keysr>  rA  sethealth_check_response_counter)r   rA   r   
s_channels	s_channelhandlerrx   rq  s           r9   rE  zClusterPubSub.ssubscribe)  s   Qab2D]]4(
&!","2"2"4 	7Iw<<11)<D**40F!!!9Y$89!!),&&v'<'<=33EE$$i%67   %%))+562	7r;   c                    |rt        |d   |dd        }n| j                  }|D ]d  }| j                  j                  |      }| j	                  |      }|j                  |       | j                  j                  |j                         f y r^   )r   r=  r  rc  r1  r;  r<  r!  )r   rA   rK  rx   ps        r9   r;  zClusterPubSub.sunsubscribe=  s    Qab2D&&D 	I<<11)<D%%d+ANN9%33::44		r;   c                 H    | j                   | j                   j                  S y)zH
        Get the Redis connection of the pubsub connected node.
        N)rx   rB  r3  s    r9   rN  z"ClusterPubSub.get_redis_connectionK  s#     99 99--- !r;   c                     | j                   r| j                   j                          | j                  j                         D ]  }|j                   j                           y)z3
        Disconnect the pubsub connection.
        N)r=   rC  r   rP   r8  s     r9   rC  zClusterPubSub.disconnectR  sK     ??OO&&(..557 	+F((*	+r;   )NNNNr  )Fg        N)r   r   r   r  r1  r  r)  r%  r  r1  r6  r!  rB  rE  r;  rN  rC  __classcell__r%  s   @r9   rp  rp  r  sb     %
N B#BJ IM47(.+r;   rp  c                   4    e Zd ZdZeeeeefZ		 	 	 	 	 	 	 d(ddddde
eeef      de
eeef      de
ed	      d
ededefdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd)dZd Z	 d*dZ	 d* fd	Zd Z d Z!d Z"d Z#d  Z$d! Z%d" Z&d# Z'd$ Z(d% Z)d& Z*d' Z+ xZ,S )+rt  z8
    Support for Redis pipeline
    in cluster mode
    r   r*  r-  r   r,  r+  r  r  r  r  r  c
                    g | _         || _        || _        d| _        |xs$ | j                  j
                  j                         | _        |r|ng | _        || _	        | j                  j                  j                         | _        || _        || _        d| _        || _        t!        |
j#                  dd      |
j#                  dd      |
j#                  dd            | _        |	t'        j(                         }	|	| _        y)	r  Fr   r   r|   r   r   r   N)command_stackr   r-  refresh_table_asapr%  r  r&  r,  r  r  r  r'  r+  r  r)  r  r   rd   r$  r.  r*   r/  )r   r   r-  r,  r+  r  r  r  r  rs  r   s              r9   r1  zClusterPipeline.__init__j  s      *."'F ? ? D D F 	 /<]"4!^^99>>@*D',H)$%!"4JJz7+JJ((3JJ)51

 <>>#D
r;   c                 .    t        |       j                   S r  )r  r   r3  s    r9   r  zClusterPipeline.__repr__  s    t*%%&'r;   c                     | S rX  r7   r3  s    r9   r4  zClusterPipeline.__enter__  s    r;   c                 $    | j                          yr  N)r  r8  s       r9   r<  zClusterPipeline.__exit__  s    

r;   c                 D    	 | j                          y # t        $ r Y y w xY wrD   )r  r  r3  s    r9   r?  zClusterPipeline.__del__  s"    	JJL 		s    	c                 ,    t        | j                        S rX  )rr   rU  r3  s    r9   __len__zClusterPipeline.__len__  s    4%%&&r;   c                      y)z?Pipeline instances should  always evaluate to True on Python 3+Tr7   r3  s    r9   __bool__zClusterPipeline.__bool__  s    r;   c                 &     | j                   |i |S )z?
        Wrapper function for pipeline_execute_command
        )pipeline_execute_commandr   rA   r   s      r9   r  zClusterPipeline.execute_command  s     -t,,d=f==r;   c           	      x    | j                   j                  t        ||t        | j                                      | S )zN
        Appends the executed command to the pipeline's command stack
        )rU  rs   PipelineCommandrr   r   rA   rB   s      r9   rb  z(ClusterPipeline.pipeline_execute_command  s6     	!!D'3t/A/A+BC	
 r;   c                     |D ]J  }|j                   }t        |t              s | j                  ||j                  dz   |j
                         | y)z8
        Raise the first exception on the stack
        r_   N)resultro   r  annotate_exceptionpositionrA   )r   stackcrM   s       r9   raise_first_errorz!ClusterPipeline.raise_first_error  sI      	AA!Y'''1::>166B		r;   c                     dj                  t        t        |            }d| d| d|j                  d    }|f|j                  dd z   |_        y)zS
        Provides extra context to the exception prior to it being handled
        r  z
Command # z (z) of pipeline caused error: r   r_   N)r  mapr0   rA   )r   r  numberrF   cmdmsgs         r9   ri  z"ClusterPipeline.annotate_exception  s_     hhs8W-.3% (&^^A./1 	 ).."44	r;   c                     | j                   }	 | j                  ||      | j                          S # | j                          w xY w)zB
        Execute all the commands in the current pipeline
        )rU  send_cluster_commandsr  )r   raise_on_errorrk  s      r9   executezClusterPipeline.execute  s7     ""	--e^DJJLDJJLs	   0 Ac                 L    g | _         t               | _        d| _        d| _        y)z/
        Reset back to empty pipeline.
        FN)rU  rH  scriptswatchingexplicit_transactionr3  s    r9   r  zClusterPipeline.reset  s&      u  $)!r;   c                     |sg S | j                   }	 	 | j                  |||      S # t        t        f$ r}|dkD  r|dz  }n|Y d}~nd}~ww xY w?)a  
        Wrapper for CLUSTERDOWN error handling.

        If the cluster reports it is down it is assumed that:
         - connection_pool was disconnected
         - connection_pool was reseted
         - refereh_table_asap set to True

        It will try the number of times specified by
        the config option "self.cluster_error_retry_attempts"
        which defaults to 3 unless manually configured.

        If it reaches the number of times, the command will
        raises ClusterDownException.
        )ru  allow_redirectionsr   r_   N)r  _send_cluster_commandsr   r   )r   rk  ru  r|  r  r  s         r9   rt  z%ClusterPipeline.send_cluster_commands  s}    $ I::22#1'9 3  
 %o6 !A% #a'NG  s   ' AAAc                    t        |d       }d}i }|D ],  }	 |j                  j                  dd      }|r#| j                  |      s| j	                  |      }	n7 | j
                  |j                  d|i}	|	st        d|j                   d	      t        |	      d
kD  rt        d|j                         |	d   }
|
| j                         k(  rd}|
j                  }||vrL| j                  |
      }	 t        ||j                        }t#        |j$                  |j&                  |      ||<   ||   j)                  |       / |j+                         }|D ]  }|j-                           |D ]  }|j/                           |j+                         D ]'  }|j&                  j1                  |j2                         ) t        d |D        d       }|r|r| xj4                  d
z  c_        | j7                         r,| j                  j                          |r| j!                          |D ]+  }	 t9        | t  |j                  i |j                  |_        - g }t        |d       D ]t  }|j                  d   | j@                  v r< | j@                  |j                  d      |j<                  fi |j                  |_        |j)                  |j<                         v |r| jC                  |       |S # t        $ r. | j                  j                          |r| j!                           w xY w# t>        $ r}||_        Y d}~d}~ww xY w)z
        Send a bunch of cluster commands to the redis cluster.

        `allow_redirections` If the pipeline should follow
        `ASK` & `MOVED` responses automatically. If set
        to false it will raise RedisClusterException.
        c                     | j                   S rD   rj  ru   s    r9   r   z8ClusterPipeline._send_cluster_commands.<locals>.<lambda>*  s
    ajj r;   )r`  FTr  N	node_flagr  r  r_   zToo many targets for command r   c              3   h   K   | ]*  }t        |j                  t        j                        r| , y wrD   )ro   rh  rt  r  ).0rl  s     r9   	<genexpr>z9ClusterPipeline._send_cluster_commands.<locals>.<genexpr>  s,      ahh(J(JK s   02c                     | j                   S rD   r  r  s    r9   r   z8ClusterPipeline._send_cluster_commands.<locals>.<lambda>  s
    !** r;   c                     | j                   S rD   r  r  s    r9   r   z8ClusterPipeline._send_cluster_commands.<locals>.<lambda>  s
    QZZ r;   )"sortedrB   r   r  r  r  rA   r#   rr   r   rg  rN  r?   r   r   r  r  NodeCommandsr  r>   rs   rP   writereadr  r=   r)  r  r"  r  rh  r$   r+  rm  )r   rk  ru  r|  attemptr  rm   rl  r  r  rx   rJ   r@   r=   node_commandsr  r  rK   r%  s                     r9   r}  z&ClusterPipeline._send_cluster_commands  sY    $89
  /	A
 "#~t!D!$*=*=n*M#'#;#;N#KL#84#8#8$+9$L (3?x{S  |$q(/7x@  $A40022&*O
 !II	E)!%!:!:4!@J%3J%G
 (4"11"22"(E)$
 i ''*_/	r  	AGGI	  	AFFH	*  	4A%%all3	4  
 %
 )& %%*%))+""--/"--/ !!  %w6L!))LAH	! #78 	&AvvayD;;;E4::166!9EHH !		 OOAHH%	& ""5)] +  **557* 557| " ! AHH!s$   5K4.(L.47L+.	M	7MM	c                     |st        d      y)r  z4ASK & MOVED redirection not allowed in this pipelineNr#   )r   r|  s     r9   _fail_on_redirectz!ClusterPipeline._fail_on_redirect  s    !'F  "r;   c                 (     | j                   dg| S )NEXISTS)r  )r   r  s     r9   existszClusterPipeline.exists  s    #t##H4t44r;   c                     t        d      )r  z method eval() is not implementedr  r3  s    r9   evalzClusterPipeline.eval  s    #$FGGr;   c                     t        d      )r  z!method multi() is not implementedr  r3  s    r9   multizClusterPipeline.multi      #$GHHr;   c                     t        d      )r  z5method immediate_execute_command() is not implementedr  rf  s      r9   immediate_execute_commandz)ClusterPipeline.immediate_execute_command  s    #C
 	
r;   c                     t        d      )r  z0method _execute_transaction() is not implementedr  rc  s      r9   _execute_transactionz$ClusterPipeline._execute_transaction  s    #$VWWr;   c                     t        d      )r  z(method load_scripts() is not implementedr  r3  s    r9   load_scriptszClusterPipeline.load_scripts  s    #$NOOr;   c                     t        d      )r  z!method watch() is not implementedr  r   namess     r9   watchzClusterPipeline.watch  r  r;   c                     t        d      )r  z#method unwatch() is not implementedr  r3  s    r9   unwatchzClusterPipeline.unwatch  s    #$IJJr;   c                     t        d      )r  z4method script_load_for_pipeline() is not implementedr  rc  s      r9   script_load_for_pipelinez(ClusterPipeline.script_load_for_pipeline  s    #B
 	
r;   c                 ^    t        |      dk7  rt        d      | j                  d|d         S )z7
        "Delete a key specified by ``names``"
        r_   z=deleting multiple keys is not implemented in pipeline commandDELr   rr   r#   r  r  s     r9   deletezClusterPipeline.delete  s8     u:?'O  ##E5844r;   c                 ^    t        |      dk7  rt        d      | j                  d|d         S )z7
        "Unlink a key specified by ``names``"
        r_   z>unlinking multiple keys is not implemented in pipeline commandUNLINKr   r  r  s     r9   unlinkzClusterPipeline.unlink  s8     u:?'P  ##HeAh77r;   )NNNFra   r  N)T)TT)-r   r   r   r  r   r'   r"   r   r(   r  r   r   re   r   r   r  r  r1  r  r4  r<  r?  r^  r`  r  rb  rm  ri  rv  r  rt  r}  r  r  r  r  r  r  r  r  r  r  r  r  rQ  rR  s   @r9   rt  rt  \  sJ    	 ;?DH7;#(,-"##%# *# #4X#67	#
 %-T#x--@$A#  ] 34# !# '*#  #J('>	5*B >B#L >BfP5HI
XPIK
	5	8r;   rt  rg  .c                       fd}|S )zi
    Prints error because some pipelined commands should
    be blocked when running in cluster-mode
    c                  "    t        d d      )Nz"ERROR: Calling pipelined function z1 is blocked when running redis in cluster mode...r  )rA   r   rg  s     r9   innerz%block_pipeline_command.<locals>.inner	  s!    #0 74 5
 	
r;   r7   )rg  r  s   ` r9   block_pipeline_commandr  		  s    
 Lr;   )EBGREWRITEAOFr   BITOP
BRPOPLPUSHr   r   r   r   CLIENTr   r   r   r   CONFIGr   ECHOr  r   r   r   r   r   MGETzMGET NONATOMICMOVEMSETzMSET NONATOMICMSETNXPFCOUNTPFMERGEr   PUBLISHr   r   r   RENAMERENAMENX	RPOPLPUSHr   r   r   r   zSCRIPT KILLr   SCRIPTSDIFF
SDIFFSTOREz SENTINEL GET MASTER ADDR BY NAMEzSENTINEL MASTERzSENTINEL MASTERSzSENTINEL MONITORzSENTINEL REMOVEzSENTINEL SENTINELSzSENTINEL SETzSENTINEL SLAVESSENTINELr   SINTERSINTERSTORESLAVEOFr   r   r   SLOWLOGSMOVESORTSUNIONSUNIONSTOREr   r  r  c                       e Zd ZdZddZy)re  r  Nc                 `    || _         |i }|| _        || _        d | _        d | _        d| _        y )NF)rA   rB   rj  rh  rx   r  )r   rA   rB   rj  s       r9   r1  zPipelineCommand.__init__i	  s6    	?G 	r;   r  )r   r   r   r  r1  r7   r;   r9   re  re  f	  s
    r;   re  c                   (    e Zd ZdZd Zd Zd Zd Zy)r  r  c                 <    || _         || _        || _        g | _        yr[  )r  r>   r=   commands)r   r  r>   r=   s       r9   r1  zNodeCommands.__init__w	  s     ,.$r;   c                 :    | j                   j                  |       yr[  )r  rs   )r   rl  s     r9   rs   zNodeCommands.append~	  s    Qr;   c                     | j                   }| j                  }|D ]	  }d|_         	 |j                  |j	                  |D cg c]  }|j
                   c}             yc c}w # t        t        f$ r}|D ]	  }||_         Y d}~yd}~ww xY w)z=
        Code borrowed from Redis so it can be fixed
        N)r=   r  rh  send_packed_commandpack_commandsrA   r   r'   )r   r=   r  rl  r  s        r9   r  zNodeCommands.write	  s     __
==  	AAH	
	**((()CQ!&&)CD)C. 	 	s)   A& A!A& !A& &B5BBc                 p   | j                   }| j                  D ]@  }|j                  	  | j                  ||j                  d   fi |j
                  |_        B y# t        t        f$ r#}| j                  D ]	  }||_         Y d}~ yd}~wt        $ r t        j                         d   |_        Y w xY w)r  Nr   r_   )r=   r  rh  r  rA   rB   r   r'   r$   sysexc_info)r   r=   rl  r  s       r9   r  zNodeCommands.read	  s    __
 	1A& xx12t22:qvvayVAIIVAH+	1, (6 !]] %#$%! 1"||~a0AH1s   0AB5,B

(B54B5N)r   r   r   r  r1  rs   r  r  r7   r;   r9   r  r  t	  s     ,1r;   r  )`r   r  r  r.  r  collectionsr   typingr   r   r   r   r   r	   r
   redis._parsersr   r   redis._parsers.helpersr   redis.backoffr   redis.clientr   r   r   redis.commandsr   r   redis.commands.helpersr   redis.connectionr   r   r   	redis.crcr   r   redis.exceptionsr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   
redis.lockr*   redis.retryr+   redis.utilsr,   r-   r.   r/   r0   r1   re   r  r:   r?   rN   rX   rk   rz   r~   rT  rW  r  r   r   r   r   r   r  r  r  r*  rp  rt  r  PIPELINE_BLOCKED_COMMANDSrF   replacelowerr  re  r  r7   r;   r9   <module>r     s     
   # D D D 2 - ) ; ; > / E E 8    "    5c? s 

	%S/4S>
)*(,  

% L ( 
M qN qNhi&')= i&X* *8$ $&W Wt
g+F g+Tj8l j8Z #s();  F N ) GGooc3'--/GOW&<W&EFG B1 B1r;   