HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.52.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Oct 15 06:39:08 EDT 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/primordial/__pycache__/zookeeper.cpython-39.pyc
a

�,�h�5�@s�dZddlZddlZddlmZddlmZmZddlm	Z	ddl
mZddlm
Z
ddlmZdd	lmZdd
lmZdZefeed�d
d�ZGdd�d�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�d�ZGdd�de�ZGdd�d�ZdS)zZookeeper functions
�N)�Enum)�Any�	Generator)�NamedTemporaryFile)�contextmanager)�KazooClient)�ConnectionLoss)�KazooTimeoutError)�
retry_thisz/etc/sysconfig/zookeeper)�zk_config_file�returncCs�t�dd�}|s�t�|tj�rzt|ddd��>}|��D]$}|���d�r6t|�	d�d�}q6Wd�n1sp0Y|s�t
d��|S)	z�
    Lookup the zookeeper nodes and return them as a comma-separated string
    :returns: The zookeeper nodes as a comma-separated string
    ZZK_HOSTSN�rzUTF-8)�encoding�=�z"Could not retrieve Zookeeper Hosts)�os�getenv�access�R_OK�open�	readlines�strip�
startswith�eval�	partition�ZookeeperException)r�nodesZzkcfg�line�r�E/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/primordial/zookeeper.py�lookup_hostss2r c@s�eZdZdZefed�dd�Zeee	ddfd�dd��Z
eed	�d
d�Zee
d	�dd
�Zdee
e
d�dd�Zeedd�dd�Zdeedd�dd�Zdd�dd�Zed�dd�ZdS)�	Zookeeperz$Represents a connection to Zookeeper�rcCs"||_|��|_t|jd�|_dS)z�
        Initialise a Zookeeper connection
        :param zk_config_file: The path to the zookeeper config file (if not /etc/sysconfig/zookeeper)
        ��hostsN)rr �zk_hostsr�client��selfrrrr�__init__,s
zZookeeper.__init__N�rccs.|j��z|jVW|��n
|��0dS)zz
        Yield a started zookeeper client in a context manager
        :yields: A started instance of KazooClient
        N)r&�start�stop_session�r(rrr�_start_session_cm5s

zZookeeper._start_session_cm)�nodercCs:|���}|�|�duWd�S1s,0YdS)z�
        Check if node exists
        :param node: Name of zookeeper node
        :returns: (bool) Whether the node exists
        N)r.�exists�r(r/�zkrrrr0As
zZookeeper.existscCs:|���}|�|�dWd�S1s,0YdS)zq
        Get the node value
        :param node: Name of zookeeper node
        :returns: The node value
        rN)r.�getr1rrrr3Js
z
Zookeeper.get)r/�defaultrcCsV|���:}|�|�dur4|�|�dWd�SWd�n1sH0Y|S)a5
        Get a node value if it exists. If it does not exist, return the default value specified
        :param node: Name of zookeeper node
        :param default: The default value to return if the node does not exist
        :returns: The node value or the default value if the node does not exist
        Nr)r.r0r3)r(r/r4r2rrr�get_or_defaultSs
:zZookeeper.get_or_default)r/�valuercCsB|���&}|�|�|�||�Wd�S1s40YdS)z~
        Set the node value
        :param node: Name of zookeeper node
        :param value: Value of zookeeper node
        N)r.Zensure_path�set)r(r/r6r2rrrr7_s

z
Zookeeper.setF)r/�	recursivercCsH|���,}|�|�dur&|j||d�Wd�n1s:0YdS)z�
        Delete the node, if it exists
        :param node: Name of zookeeper node
        :param recursive: Whether to delete the node and all child nodes
        N)r8)r.r0�delete)r(r/r8r2rrrr9is
zZookeeper.deletecCs|j��|j��dS)z+End and close the current zookeeper sessionN)r&�stop�closer-rrrr,ss
zZookeeper.stop_sessioncCs
t|j�S)z�
        Lookup the zookeeper nodes and return them as a comma-separated string
        :returns: The zookeeper nodes as a comma-separated string
        )r rr-rrrr xszZookeeper.lookup_hosts)N)F)�__name__�
__module__�__qualname__�__doc__�ZK_CONFIG_FILE�strr)rrrr.�boolr0rr3r5�bytesr7r9r,r rrrrr!)s			

r!cs@eZdZdZed�eed��fdd�Zeed�dd��Z�Z	S)	�ZKFilea�
    Creates a named temporary file with the contents of one or more znodes. Useful for APIs that
    only accept file paths rather than file-like objects.
    Warning: Because the file is a temp file there is a possibility of it being deleted by some
    tempfile cleanup mechanisms (tmpwatch, etc.). In this case it may be a better idea to read
    the znode into memory and create the temp file from that each time you use it.
    r")�znodesrcsDt�j|d�t�|_|D]}|r|j�|�|��q|j��dS)z�
        Load the znode contents into a temporary file
        :param znodes: An expanded list of zookeeper node names
        :param zk_config_file: The zookeeper config file (if not the default of /etc/sysconfig/zookeeper)
        r"N)�superr)r�file�writer3�flush)r(rrEZznode��	__class__rrr)�szZKFile.__init__r*cCs|jjS)zNGet the filename for the temporary file storing the contents of the zk node(s))rG�namer-rrrrL�szZKFile.name)
r<r=r>r?r@rAr)�propertyrL�
__classcell__rrrJrrD�s
rDc@seZdZdS)rN�r<r=r>rrrrr�src@s$eZdZdZdZdZdZdZdZdS)�ZKModezH
    Enum to represent the mode a zk node is currently operating in
    �r���N)	r<r=r>r?�LEADER�FOLLOWER�
STANDALONE�ERROR�UNKNOWNrrrrrP�srPc@s�eZdZdZed�dd�Zdd�Zdd�Zd	d
�Ze	dd��Z
e	d
d��Zee
eejfd�deed�dd��Ze	ed�dd��Ze	ed�dd��Ze	ed�dd��Ze	ed�dd��ZdS)�ZkEnsembleNodez�
    This class represents a single node in a zookeeper cluster and holds to the status/mode that
    node is operating in. Values are leader, follower, standalone, error or unknown.
    ��hostcCs*t|d�|_|�d�\|_|_tj|_dS)z�
        Initialize the class.
        :param host: The host:port connection for the node whose mode/state this class holds.
        r#�:N)r�_ZkEnsembleNode__client�split�_ZkEnsembleNode__host�_ZkEnsembleNode__portrPrY�_ZkEnsembleNode__mode)r(r\rrrr)�szZkEnsembleNode.__init__cCs|j��|jS)z�
        This turns this class/object into a context manager for connections to the zookeeper node.
        This starts up the connection and returns the client as the resource being managed.
        )r^r+r-rrr�	__enter__�s
zZkEnsembleNode.__enter__cCs0z|j��|j��Wnty*Yn0dS)zb
        This method takes care of releasing the client connection to the zookeeper node.
        N)r^r:r;�	Exception)r(Z	_exc_typeZ_exc_valZ_exc_tbrrr�__exit__�s

zZkEnsembleNode.__exit__cCsd�|j|j�S)�<
        Python repr implementation for this class.
        zHost: {}, Mode: {})�formatr\�moder-rrr�__repr__�szZkEnsembleNode.__repr__cCs|jS�N)r`r-rrrr\�szZkEnsembleNode.hostcCs|jSrj)rar-rrr�port�szZkEnsembleNode.port)Z
on_ex_classesF)�forcercCs�|s|jtjur|jS|�p}|jdd�}|dkrt|jdd�}ttdd�|�d���d}|d	d
�}t|��|_ntj|_Wd
�n1s�0Y|jS)aB
        This method returns the mode the zk node is currently operating in.
        If a nodes mode has already been fetched, then this method returns the cached mode/status.
        To initiate a re-fetch of the status, use the force parameter.

        :param force: Force re-fetch of the zk node's status/mode
        sruok)�cmdZimokssrvrcSs
|�d�S)NzMode: )r)�lrrr�<lambda>��z+ZkEnsembleNode.fetch_mode.<locals>.<lambda>�
r�N)	rbrPrY�command�list�filterr_�upperrX)r(rlZ	zk_clientZruokZsrvrZ	mode_line�mrrr�
fetch_mode�s	&zZkEnsembleNode.fetch_moder*cCs|jS)z?
        Property to return the internally cached mode
        )rbr-rrrrh�szZkEnsembleNode.modecCs|jtjkS)z\
        Python property to check if the node is currently operating as a follower.
        )rhrPrVr-rrr�is_follower�szZkEnsembleNode.is_followercCs|jtjkS)zZ
        Python property to check if the node is currently operating as a leader.
        )rhrPrUr-rrr�	is_leaderszZkEnsembleNode.is_leadercCs|jtjkS)za
        Python property to check if the node is currently operating in standalone mode.
        )rhrPrWr-rrr�
is_standaloneszZkEnsembleNode.is_standaloneN)F)r<r=r>r?rAr)rcrerirMr\rkr
rr	�socket�errorrBrPrxrhryrzr{rrrrrZ�s&	


rZc@seZdZdS)�ZkEnsembleStatusNotGatheredNrOrrrrr~sr~c@s�eZdZdZefed�dd�Zedd��Zedd��Z	de
d
�dd�Zee
d
�dd��Zee
d
�dd��Z
ee
d
�dd��Zdd�ZdS)�ZookeeperEnsemblez�
    This class is used to represent a zookeeper ensemble/cluster and test if there is currently
    a quorum in the cluster.
    r"cCsf||_t|�|_|j�d�|_dd�|jD�|_tt|j�d�d|_g|_	g|_
g|_|��dS)z�
        Initialise a Zookeeper connection
        :param zk_config_file: Path to the zookeeper config file (default /etc/sysconfig/zookeeper)
        �,cSsg|]}t|d��qS)r[)rZ)�.0�hrrr�
<listcomp>)rpz.ZookeeperEnsemble.__init__.<locals>.<listcomp>rrQN)
rr Zzk_hosts_conn_strr_r%�ensemble_nodes�int�len�SIZE_FOR_QUORUM�_leaders�
_followers�_standalone�
gather_statusr'rrrr)s
zZookeeperEnsemble.__init__cCs
t|j�S)zS
        Python property that returns the number of nodes in this cluster.
        )r�r�r-rrr�
ensemble_size1szZookeeperEnsemble.ensemble_sizecCs
t|j�S)zU
        Python property that returns the number of followers in the quorum.
        )r�r�r-rrr�followers_size8sz ZookeeperEnsemble.followers_sizeF)rlc	Cst|jD]&}z|�|�Wqty*Yq0qttdd�|j��|_ttdd�|j��|_ttdd�|j��|_dS)a
        Method to gather the status of the nodes in the ensemble.
        Note, currently if the node has a cached status, then that is what's used. We dont force
        a re-fetch.

        :param force: Force re-fetch of the zk node's status/mode
        cSs|jSrj)rz�r/rrrroNrpz1ZookeeperEnsemble.gather_status.<locals>.<lambda>cSs|jSrj)ryr�rrrroOrpcSs|jSrj)r{r�rrrroPrpN)r�rxrdrtrur�r�r�)r(rlr/rrrr�?s
zZookeeperEnsemble.gather_statusr*cCst|j�dkS)zZ
        Python property to check if the cluster is operating in standalone mode.
        r)r�r�r-rrr�is_in_standalone_modeRsz'ZookeeperEnsemble.is_in_standalone_modecCst|j�dkS)zx
        Python property to check if the cluster has a split brain, perhaps as a result of a network partition.
        rQ)r�r�r-rrr�has_split_brainYsz!ZookeeperEnsemble.has_split_braincCs*|jo(|jo(t|j�t|j�|jkS)z�
        Python property to check if the cluster currently has quorum.
        Make sure gather_status has been called before we call this method.
        )r�r�r�r�r�r�r-rrr�
has_quorum`s

��zZookeeperEnsemble.has_quorumcCs�d�dd�|jD��}|jr&d}d}n8|jrVd�|jdj�}d�dd�|jD��}nd}d}d	�d
d�|jD��}d�||j|j|||�S)rfz, cSsg|]
}|j�qSrr[)r��nrrrr�rrpz.ZookeeperEnsemble.__repr__.<locals>.<listcomp>rW�{}rcSsg|]
}|j�qSrr[)r��frrrr�yrpzSPLIT BRAINz	
cSsg|]}d�|��qS)r�)rg)r�r/rrrr�~rpzPNodes: {}
Ensemble mode? {}
Has Quorum? {}
Leader: {}
Followers: {}

Details:
{})�joinr�r�r�rgr�r\r�)r(Zzk_nodesZleaderZ	followers�detailsrrrrins�zZookeeperEnsemble.__repr__N)F)r<r=r>r?r@rAr)rMr�r�rBr�r�r�r�rirrrrrs


r)r?rr|�enumr�typingrr�tempfiler�
contextlibrZkazoo.clientrZkazoo.exceptionsrZkazoo.handlers.threadingr	Zprimordial.utilsr
r@rAr r!rDrdrrPrZr~rrrrr�<module>s&Wi