
    VhW                         d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 	 d dl
Z
d Z G d d	e	      Zy# e$ r	 ddlmZ
 Y w xY w)
    N)	b64decode	b64encode)Optional)Store   )
_etcd_stubc                  V    t        j                  t        j                  dd             y )Nr   g?)timesleeprandomuniform     _/home/dcms/DCMS/lib/python3.12/site-packages/torch/distributed/elastic/rendezvous/etcd_store.py	cas_delayr      s    JJv~~a%&r   c                        e Zd ZdZ	 ddeej                     f fdZd Zde	fdZ
dedefdZdd	eej                     fd
ZdefdZdefdZde	fdZddZ xZS )	EtcdStorez
    Implement a c10 Store interface by piggybacking on the rendezvous etcd instance.

    This is the store object returned by ``EtcdRendezvous``.
    timeoutc                     t         |           || _        || _        || j	                  |       | j                  j                  d      s| xj                  dz  c_        y y )N/)super__init__clientprefixset_timeoutendswith)selfetcd_clientetcd_store_prefixr   	__class__s       r   r   zEtcdStore.__init__&   sW     	!'W%{{##C(KK3K )r   c                     | j                   j                  | j                  | j                  |      z   | j                  |             y)z
        Write a key/value pair into ``EtcdStore``.

        Both key and value may be either Python ``str`` or ``bytes``.
        )keyvalueN)r   setr   _encode)r   r"   r#   s      r   r$   zEtcdStore.set8   s4     	DKK$,,s*;;4<<PUCVWr   returnc                     | j                   | j                  |      z   }| j                  |g      }|t        d| d      | j	                  ||         S )aV  
        Get a value by key, possibly doing a blocking wait.

        If key is not immediately present, will do a blocking wait
        for at most ``timeout`` duration or until the key is published.


        Returns:
            value ``(bytes)``

        Raises:
            LookupError - If key still not published after timeout
        zKey z not found in EtcdStore)r   r%   _try_wait_getLookupError_decode)r   r"   b64_keykvss       r   getzEtcdStore.get@   sZ     ++S 11  '+;SE)@ABB||CL))r   numc           	         | j                  |      }	 | j                  j                  | j                  |z   | j                  t	        |            d      }t        | j                  |j                              S # t        j                  $ r Y nw xY w	 | j                  j                  | j                  |z         }| j                  t	        t        | j                  |j                              |z               }	 | j                  j                  |j                  ||j                        }t        | j                  |j                              S # t        j                  $ r t                Y nw xY w)a	  
        Atomically increment a value by an integer amount.

        The integer is represented as a string using base 10. If key is not present,
        a default value of ``0`` will be assumed.

        Returns:
             the new (incremented) value


        F)r"   r#   	prevExistr"   )r"   r#   
prev_value)r%   r   writer   strintr*   r#   etcdEtcdAlreadyExistr-   test_and_setr"   EtcdCompareFailedr   )r   r"   r.   r+   node	new_values         r   addzEtcdStore.addV   s5    ,,s#		;;$$KK')ll3s8, % D
 t||DJJ/00$$ 		  ;;??t{{W'<?=DST\\$**-E)F)L%MNI{{//	djj 0  4<<

344))  s%   A&A: :BB=AE E32E3override_timeoutc                     |D cg c]   }| j                   | j                  |      z   " }}| j                  ||      }|t        d      yc c}w )z
        Wait until all of the keys are published, or until timeout.

        Raises:
            LookupError - if timeout occurs
        Nz+Timeout while waiting for keys in EtcdStore)r   r%   r(   r)   )r   keysr=   r"   b64_keysr,   s         r   waitzEtcdStore.wait|   sZ     @DDDKK$,,s"33DD  +;<;KLL  Es   %Ac                     |D cg c]   }| j                   | j                  |      z   " }}| j                  |t        j                  d            }|duS c c}w )zCCheck if all of the keys are immediately present (without waiting).r   )microseconds)r=   N)r   r%   r(   datetime	timedelta)r   r?   r"   r@   r,   s        r   checkzEtcdStore.check   s_    ?CDDKK$,,s"33DD  %//Q? ! 
 $ Es   %Ac                     t        |      t        k(  rt        |      j                         S t        |      t        k(  r't        |j                               j                         S t        d      Nz"Value must be of type str or bytes)typebytesr   decoder4   encode
ValueErrorr   r#   s     r   r%   zEtcdStore._encode   sT    ;%U#**,,%[CU\\^,3355=>>r   c                     t        |      t        k(  rt        |      S t        |      t        k(  rt        |j	                               S t        d      rH   )rI   rJ   r   r4   rL   rM   rN   s     r   r*   zEtcdStore._decode   sB    ;%U##%[CU\\^,,=>>r   c                    || j                   n|}t        j                         |j                         z   }	 d }	 | j                  j	                  | j
                        }|j                  D ci c]'  }|j                  |v r|j                  |j                  ) }}t        |      t        |      k(  r|S 	 |t        j                         z
  }|dk  ry 	 |r|j                  dz   nd}	| j                  j                  | j
                  d||	       c c}w # t        j                  $ r Y uw xY w# t        j                  $ r t        j                         |k\  rY y Y (t        j                  $ r Y =w xY w)NTr1   r   r   )r"   	recursiver   index)r   r
   total_secondsr   r-   r   childrenr"   r#   lenr6   EtcdKeyNotFound
etcd_indexwatchEtcdWatchTimedOutEtcdEventIndexCleared)
r   r@   r=   r   deadline	all_nodesr:   	req_nodeswatch_timeoutrR   s
             r   r(   zEtcdStore._try_wait_get   sh   "2":$,,@P99;!6!6!88I KKOOO<	 !* 2 2xx8+ HHdjj(	  y>S]2$$ 3 %tyy{2M!4=	,,q01!!")	 " / 
 ''  )) 99;(*-- s;   4D .,DD <D/ D D,+D,/*E4E43E4)N)__name__
__module____qualname____doc__r   rD   rE   r   r$   rJ   r-   r5   r<   rA   boolrF   r4   r%   r*   r(   __classcell__)r    s   @r   r   r      s     15
 (,,-$X*% *,$C $C $L
M8H4F4F+G 
MT ? ?? ?'r   r   )rD   r   r
   base64r   r   typingr   torch.distributedr   r6   ModuleNotFoundError r   r   r   r   r   r   <module>rj      sJ       '  $%'
y y  %$%s   5 AA