
    Vh                         d dl Z d dlmZmZ d dlmZmZ d dlZ	 d dl	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  G d de      Zdede	j6                  fdZdedeeef   fdZy# e
$ r	 ddlmZ	 Y \w xY w)    N)	b64decode	b64encode)castOptional   )
_etcd_stub)Store)RendezvousConnectionErrorRendezvousParametersRendezvousStateError)RendezvousBackendToken)	EtcdStore)parse_rendezvous_endpointc                      e Zd ZU dZdZej                  ed<   eed<   e	ed<   	 	 ddej                  ded	e
e   d
e
e	   ddf
dZedefd       Zde
eeef      fdZ	 ddede
e   de
eeeef      fdZdej(                  deeef   fdZy)EtcdRendezvousBackenda  Represents an etcd-based rendezvous backend.

    Args:
        client:
            The ``etcd.Client`` instance to use to communicate with etcd.
        run_id:
            The run id of the rendezvous.
        key_prefix:
            The path under which to store the rendezvous state in etcd.
        ttl:
            The TTL of the rendezvous state. If not specified, defaults to two hours.
    i   _client_key_ttlNclientrun_id
key_prefixttlreturnc                     |st        d      || _        |r|dz   |z   | _        n|| _        |r|dkD  r|| _        y | j                  | _        y )Nz&The run id must be a non-empty string./r   )
ValueErrorr   r   r   _DEFAULT_TTL)selfr   r   r   r   s        l/home/dcms/DCMS/lib/python3.12/site-packages/torch/distributed/elastic/rendezvous/etcd_rendezvous_backend.py__init__zEtcdRendezvousBackend.__init__0   sR     EFF"S(61DIDI37DI))DI    c                      y)See base class.zetcd-v2 )r   s    r    namezEtcdRendezvousBackend.nameF   s     r"   c                    	 | j                   j                  | j                        }| j                  |      S # t        j                  $ r Y yt        j
                  t        j                  j                  f$ r}t        d      |d}~ww xY w)r$   NCThe connection to etcd has failed. See inner exception for details.)r   readr   etcdEtcdKeyNotFoundEtcdExceptionurllib3
exceptionsTimeoutErrorr
   _decode_state)r   resultexcs      r    	get_statezEtcdRendezvousBackend.get_stateK   s    	\\&&tyy1F !!&)) ## 	""G$6$6$C$CD 	+U	s   %8 B
,B
9BB
statetokenc                     t        |      j                         }i } fd}|r	 t        |      }|r||d<   nd|d<   	   j                  j
                   j                  | j                  fi |}| |       S g  j!                  |      d}|S # t        $ r
  |       cY S w xY w# t        j                  t        j                  f$ r d}Y ^t        j                  t        j                  j                  f$ r}t        d      |d}~ww xY w)r$   c                  <    j                         } | 	g | d}|S y )NF)r3   )r1   tmpr   s     r    r3   z2EtcdRendezvousBackend.set_state.<locals>.get_state`   s,    ^^%F!$vnun 
r"   	prevIndexF	prevExistNr(   T)r   decodeintr   r   writer   r   r*   EtcdAlreadyExistEtcdCompareFailedr,   r-   r.   r/   r
   r0   )	r   r4   r5   base64_statekwargsr3   r1   r2   r8   s	   `        r    	set_statezEtcdRendezvousBackend.set_stateX   s    !'..0	 #E
 "'F;"'F;	'T\\''		<UfUF >;/t!!&)/4/
+  # {"# %%t'='=> 	F""G$6$6$C$CD 	+U	s.   B 3B( B%$B%(%D,D;DDr1   c                     |j                   j                         }	 t        |      }||j                  fS # t        j                  $ r}t        d      |d }~ww xY w)Nz=The state object is corrupt. See inner exception for details.)valueencoder   binasciiErrorr   modifiedIndex)r   r1   r@   r4   r2   s        r    r0   z#EtcdRendezvousBackend._decode_state   s_    ||**,	l+E f**** ~~ 	&O	s   5 AAA)NN)N)__name__
__module____qualname____doc__r   r*   Client__annotations__strr<   r   r!   propertyr&   tuplebytesr   r3   boolrB   
EtcdResultr0   r%   r"   r    r   r      s     L[[
I
I %)!** * SM	*
 c]* 
*, c  *8E%,$78 * 6:))#+E?)	%ud*+	,)V
+DOO 
+eUl8K 
+r"   r   paramsr   c           	      N   t        | j                  d      \  }}t        t        | j	                  dd            }|dk  rt        d      | j                  dd      j                         j                         }|dk7  r|d	k7  rt        d
      | j                  d      }|r| j                  d      }|r||f}| j                  d      }	 t        j                  ||||||d      S # t        j                  t        j                  j                  f$ r}t        d      |d }~ww xY w)NiK	  )default_portread_timeout<   r   z,The read timeout must be a positive integer.protocolhttphttpsz#The protocol must be HTTP or HTTPS.ssl_certssl_cert_keyca_certT)rX   rZ   certr_   allow_reconnectr(   )r   endpointr   r<   
get_as_intr   getstriplowerr*   rM   r,   r-   r.   r/   r
   )	rU   hostportrX   rZ   r]   r^   r_   r2   s	            r    _create_etcd_clientri      s+   *6??NJD$ V..~rBCLqGHH zz*f-335;;=H6h'1>?? zz*%Hzz.1 !,/H jj#G{{% 
 	
  2 2 ? ?@ 'Q
	s   
C& &-D$DD$c                 h    t        |       }t        || j                  d      }t        |d      }||fS )a  Create a new :py:class:`EtcdRendezvousBackend` from the specified parameters.

    +--------------+-----------------------------------------------------------+
    | Parameter    | Description                                               |
    +==============+===========================================================+
    | read_timeout | The read timeout, in seconds, for etcd operations.        |
    |              | Defaults to 60 seconds.                                   |
    +--------------+-----------------------------------------------------------+
    | protocol     | The protocol to use to communicate with etcd. Valid       |
    |              | values are "http" and "https". Defaults to "http".        |
    +--------------+-----------------------------------------------------------+
    | ssl_cert     | The path to the SSL client certificate to use along with  |
    |              | HTTPS. Defaults to ``None``.                              |
    +--------------+-----------------------------------------------------------+
    | ssl_cert_key | The path to the private key of the SSL client certificate |
    |              | to use along with HTTPS. Defaults to ``None``.            |
    +--------------+-----------------------------------------------------------+
    | ca_cert      | The path to the rool SSL authority certificate. Defaults  |
    |              | to ``None``.                                              |
    +--------------+-----------------------------------------------------------+
    z/torch/elastic/rendezvous)r   z/torch/elastic/store)ri   r   r   r   )rU   r   backendstores       r    create_backendrm      s=    , !(F#*EG f45EE>r"   )rF   base64r   r   typingr   r   urllib3.exceptionsr-   r*   ModuleNotFoundError r   torch.distributedr	   apir
   r   r   dynamic_rendezvousr   r   
etcd_storer   utilsr   r   rM   ri   rQ   rm   r%   r"   r    <module>rx      s     ' ! % $ V V 8 ! ,q+- q+h& 4 & &R/ E:OQV:V4W Q  %$%s   A- -A;:A;