Ë
    ÄÓ^jÌ   ã                   óÊ   — d dl Z d dlZd dlZd dlZ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Z ej                  e«      Zd„ Zddedz  fd„Z G d„ d«      Zy# e$ r Y Œ1w xY w)	é    N)ÚTextIOc                  ó‚  — t        j                  ddt         j                  t         j                  ¬«      } | D ]G  }|\  }}}}}	 t        j                   |||«      }|j	                  d«       |j                  d«       |c S  t        d«      ‚# t        $ r(}j                  «        t        d|› �«       Y d}~Œ�d}~ww xY w)aµ  
    Find a free port and binds a temporary socket to it so that the port can be "reserved" until used.

    .. note:: the returned socket must be closed before using the port,
              otherwise an ``address already in use`` error will happen.
              The socket should be held and closed as close to the
              consumer of the port as possible since otherwise, there
              is a greater chance of race-condition where a different
              process may see the port as being free and take it.

    Returns: a socket bound to the reserved free port

    Usage::

    sock = find_free_port()
    port = sock.getsockname()[1]
    sock.close()
    use_port(port)
    Ú	localhostN)ÚhostÚportÚfamilyÚtype)r   r   r   z Socket creation attempt failed: zFailed to create a socket)
ÚsocketÚgetaddrinfoÚ	AF_UNSPECÚSOCK_STREAMÚbindÚlistenÚOSErrorÚcloseÚprintÚRuntimeError)ÚaddrsÚaddrr   r	   ÚprotoÚ_ÚsÚes           ú{/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/distributed/elastic/rendezvous/etcd_server.pyÚfind_free_portr      sº   € ô( ×ÑØ˜t¬F×,<Ñ,<Ä6×CUÑCUô€Eð ò 	:ˆØ$(Ñ!ˆ��e˜Q ð	:Ü—‘˜f d¨EÓ2ˆAØ�F‰FÐ#Ô$Ø�H‰H�QŒKØŠHð	:ô Ð2Ó
3Ð3øô ò 	:Ø�G‰GŒIÜÐ4°Q°CÐ8×9Ñ9ûð	:ús   Á:BÂ	B>ÂB9Â9B>Údata_dirc                 óø   — | rH | j                   «       €7t        j                  d«        | j                  «         | j                  «        |r.t        j                  d|«       t        j                  |d¬«       y y )Nzstopping etcd serverzdeleting etcd data dir: %sT©Úignore_errors)ÚpollÚloggerÚinfoÚ	terminateÚwaitÚshutilÚrmtree)Ú
subprocessr   s     r   Ú	stop_etcdr(   C   s_   € Ù�o�j—o‘oÓ'Ð/Ü�‰Ð*Ô+Øˆ
×ÑÔØˆ
�‰ÔáÜ�‰Ð0°(Ô;Ü�‰�h¨dÖ3ð ó    c            	       óÜ   — e Zd ZdZddedz  fd„Zdej                  fd„Zde	fd„Z
defd„Zdefd	„Z	 	 	 dd
e	de	de	ez  dz  ddfd„Z	 dded
e	de	ez  dz  ddfd„Zd„ Zdd
e	ddfd„Zdd„Zy)Ú
EtcdServera¹  
    .. note:: tested on etcd server v3.4.3.

    Starts and stops a local standalone etcd server on a random free
    port. Useful for single node, multi-worker launches or testing,
    where a sidecar etcd server is more convenient than having to
    separately setup an etcd server.

    This class registers a termination handler to shutdown the etcd
    subprocess on exit. This termination handler is NOT a substitute for
    calling the ``stop()`` method.

    The following fallback mechanism is used to find the etcd binary:

    1. Uses env var TORCHELASTIC_ETCD_BINARY_PATH
    2. Uses ``<this file root>/bin/etcd`` if one exists
    3. Uses ``etcd`` from ``PATH``

    Usage
    ::

     server = EtcdServer("/usr/bin/etcd", 2379, "/tmp/default.etcd")
     server.start()
     client = server.get_client()
     # use client
     server.stop()

    Args:
        etcd_binary_path: path of etcd server binary (see above for fallback path)
    Nr   c                 óª  — d| _         d| _        t        j                  j	                  t
        «      }t        j                  j                  |d«      }t        j                  j                  d|«      | _	        t        j                  j                  | j                  «      sd| _	        |r|nt        j                  d¬«      | _        d | _        d | _        y )Néÿÿÿÿr   zbin/etcdÚTORCHELASTIC_ETCD_BINARY_PATHÚetcdÚtorchelastic_etcd_data)Úprefix)Ú_portÚ_hostÚosÚpathÚdirnameÚ__file__ÚjoinÚenvironÚgetÚ_etcd_binary_pathÚisfileÚtempfileÚmkdtempÚ_base_data_dirÚ	_etcd_cmdÚ
_etcd_proc)Úselfr   ÚrootÚdefault_etcd_bins       r   Ú__init__zEtcdServer.__init__n   sŸ   € ØˆŒ
Ø ˆŒ
ä�w‰w�‰œxÓ(ˆÜŸ7™7Ÿ<™<¨¨jÓ9ÐÜ!#§¡§¡Ø+Ð-=ó"
ˆÔô �w‰w�~‰~˜d×4Ñ4Ô5Ø%+ˆDÔ"ñ !‰H¤h×&6Ñ&6Ð>VÔ&Wð 	Ôð ˆŒØ37ˆ�r)   Úreturnc                 óH   — | j                   st        d«      ‚| j                   S )Nz>No etcd server process started. Call etcd_server.start() first)rA   r   ©rB   s    r   Ú_get_etcd_server_processz#EtcdServer._get_etcd_server_process€   s%   € Ø�ŠÜØPóð ð —?‘?Ð"r)   c                 ó   — | j                   S )z)Return the port the server is running on.)r2   rH   s    r   Úget_portzEtcdServer.get_portˆ   ó   € à�z‰zÐr)   c                 ó   — | j                   S )z)Return the host the server is running on.)r3   rH   s    r   Úget_hostzEtcdServer.get_hostŒ   rL   r)   c                 ó8   — | j                   › d| j                  › �S )z,Return the etcd server endpoint (host:port).ú:)r3   r2   rH   s    r   Úget_endpointzEtcdServer.get_endpoint�   s   € à—*‘*�˜Q˜tŸz™z˜lÐ+Ð+r)   ÚtimeoutÚnum_retriesÚstderrc                 ó   — d}	 	 t         j                  j                  | j                  t	        |«      «      }t        j
                  |d¬«       | j                  |||«      S # t        $ ra}|dz  }t        | j                  «       t        j                  d|«       ||k\  r"t        j                  | j                  d¬«       ‚ Y d}~nd}~ww xY wŒÌ)aî  
        Start the server, and waits for it to be ready. When this function returns the sever is ready to take requests.

        Args:
            timeout: time (in seconds) to wait for the server to be ready
                before giving up.
            num_retries: number of retries to start the server. Each retry
                will wait for max ``timeout`` before considering it as failed.
            stderr: the standard error file handle. Valid values are
                `subprocess.PIPE`, `subprocess.DEVNULL`, an existing file
                descriptor (a positive integer), an existing file object, and
                `None`.

        Raises:
            TimeoutError: if the server is not ready within the specified timeout
        r   T)Úexist_oké   z4Failed to start etcd server, got error: %s, retryingr   N)r4   r5   r8   r?   ÚstrÚmakedirsÚ_startÚ	Exceptionr(   rA   r!   Úwarningr%   r&   ÚatexitÚregister)rB   rR   rS   rT   Úcurr_retriesr   r   s          r   ÚstartzEtcdServer.start”   s³   € ð, ˆØðÜŸ7™7Ÿ<™<¨×(;Ñ(;¼SÀÓ=NÓO�Ü—‘˜H¨tÕ4Ø—{‘{ 8¨W°fÓ=Ð=øÜò Ø Ñ!�Ü˜$Ÿ/™/Ô*Ü—‘ØJÈAôð   ;Ò.Ü—M‘M $×"5Ñ"5ÀTÕJØô /ûðúð s   …AA" Á"	CÁ+ACÃCc                 óB  — t        «       }t        «       }|j                  «       d   | _        |j                  «       d   }t        j                  dj                  | j                  dd|dd| j                  › d| j                  › �dd| j                  › d| j                  › �d	d| j                  › d|› �g
«      «      }t        j                  d
|«       |j                  «        |j                  «        t        j                  |d|¬«      | _        | j                  |«       y )NrW   ú z--enable-v2z
--data-dirz--listen-client-urlszhttp://rP   z--advertise-client-urlsz--listen-peer-urlszStarting etcd server: [%s]T)Ú	close_fdsrT   )r   Úgetsocknamer2   ÚshlexÚsplitr8   r;   r3   r!   r"   r   r'   ÚPopenrA   Ú_wait_for_ready)rB   r   rR   rT   ÚsockÚ	sock_peerÚ	peer_portÚetcd_cmds           r   rZ   zEtcdServer._start»   s  € ô ÓˆÜ"Ó$ˆ	Ø×%Ñ%Ó'¨Ñ*ˆŒ
Ø×)Ñ)Ó+¨AÑ.ˆ	ä—;‘;Ø�H‰Hà×*Ñ*Ø!Ø ØØ*Ø˜dŸj™j˜\¨¨4¯:©:¨,Ð7Ø-Ø˜dŸj™j˜\¨¨4¯:©:¨,Ð7Ø(Ø˜dŸj™j˜\¨¨9¨+Ð6ðóó
ˆô" 	�‰Ð0°(Ô;à�
‰
ŒØ�‰ÔÜ$×*Ñ*¨8¸tÈFÔSˆŒØ×Ñ˜WÕ%r)   c                 ó\   — t        j                  | j                  | j                  dd¬«      S )zNReturn an etcd client object that can be used to make requests to this server.ú/v2é
   ©r   r   Úversion_prefixÚread_timeout)r/   ÚClientr3   r2   rH   s    r   Ú
get_clientzEtcdServer.get_clientÛ   s%   € ä�{‰{Ø—‘ $§*¡*¸UÐQSô
ð 	
r)   c                 ó  — t        j                  | j                  › | j                  dd¬«      }t	        j                  «       |z   }t	        j                  «       |k  rh| j                  «       j                  «       �(| j                  «       j                  }t        d|› �«      ‚	 t        j                  d|j                  «       y t        d«      ‚# t        $ r t	        j                  d«       Y nw xY wt	        j                  «       |k  rŒ¯ŒH)Nrn   é   rp   z*Etcd server process exited with the code: zetcd server ready. version: %srW   z.Timed out waiting for etcd server to be ready!)r/   rs   r3   r2   ÚtimerI   r    Ú
returncoder   r!   r"   Úversionr[   ÚsleepÚTimeoutError)rB   rR   ÚclientÚmax_timeÚexitcodes        r   rh   zEtcdServer._wait_for_readyá   sÝ   € Ü—‘Ø—J‘J�< t§z¡zÀ%ÐVWô
ˆô —9‘9“; Ñ(ˆä�i‰i‹k˜HÒ$Ø×,Ñ,Ó.×3Ñ3Ó5ÐAà×8Ñ8Ó:×EÑE�Ü"Ø@ÀÀ
ÐKóð ðÜ—‘Ð<¸f¿n¹nÔMØô ÐKÓLÐLøô ò Ü—
‘
˜1–ðúô �i‰i‹k˜HÓ$øs   Â$ C ÃC1Ã0C1c                 ón   — t         j                  d«       t        | j                  | j                  «       y)zGStop the server and cleans up auto generated resources (e.g. data dir).zEtcdServer stop method calledN)r!   r"   r(   rA   r?   rH   s    r   ÚstopzEtcdServer.stopõ   s#   € ä�‰Ð3Ô4Ü�$—/‘/ 4×#6Ñ#6Õ7r)   ©N)é<   é   N)r‚   N)r‚   )rF   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rX   rE   r'   rg   rI   ÚintrK   rN   rQ   r   r`   rZ   rt   rh   r€   © r)   r   r+   r+   N   sè   „ ññ>8  t¡ó 8ð$#¨*×*:Ñ*:ó #ð˜#ó ð˜#ó ð,˜có ,ð ØØ&*ñ	%Iàð%Ið ð%Ið �f‘˜tÑ#ð	%Ið
 
ó%IðP OSñ&Øð&Ø&)ð&Ø8;¸f¹ÀtÑ8Kð&à	ó&ò@
ñM sð M°Dó Mô(8r)   r+   r�   )r]   Úloggingr4   re   r%   r
   r'   r=   rw   Útypingr   r/   ÚModuleNotFoundErrorÚ	getLoggerr„   r!   r   rX   r(   r+   r‰   r)   r   ú<module>rŽ      sx   ðó Û Û 	Û Û Û Û Û Û Ý ð	Ûð
 
ˆ×	Ñ	˜8Ó	$€ò"4ñJ4 C¨$¡Jó 4÷j8ò j8øðo ò 	Ùð	ús   ¬A ÁA"Á!A"