
    &`i6                     P   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 d dlmZ d dlmZmZ d dlmZ  ej        e          ZdZdZee G d	 d
                                  Z G d d          Ze G d de j                              Z G d de          ZdS )    N)	dataclass)AnyCallableDictListOptionalTuple)tabulate)
DeprecatedDeveloperAPI)Templatei,  i  c                   T    e Zd ZU eZeed<   eZeed<   dZ	e
ed<   dZe
ed<   defdZd	S )

SyncConfigsync_periodsync_timeoutFsync_artifactsTsync_artifacts_on_checkpointreturnc                     t          d                              t          ddg| j        | j        gdddd          d	
          S )z2Generate an HTML representation of the SyncConfig.zscrollableTable.html.j2zSync periodzSync timeout)SettingValuehtmlFkeys)tablefmt	showindexheadersnone)table
max_height)r   renderr
   r   r   selfs    n/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/_internal/syncer.py_repr_html_zSyncConfig._repr_html_   sj    12299 -~>".0AB       : 
 
 	
    N)__name__
__module____qualname__DEFAULT_SYNC_PERIODr   int__annotations__DEFAULT_SYNC_TIMEOUTr   r   boolr   strr$    r%   r#   r   r      st          +K***,L#,,, ND   )- $---
S 
 
 
 
 
 
r%   r   c                   j    e Zd ZdefdZed             Zed             Zd Zd
de	e
         defd	ZdS )_BackgroundProcessfnc                 X    || _         d | _        i | _        t          d          | _        d S Nz-inf)_fn_process_resultfloat_start_time)r"   r2   s     r#   __init__z_BackgroundProcess.__init__/   s+     ==r%   c                 B    | j         o| j                                         S N)r6   is_aliver!   s    r#   
is_runningz_BackgroundProcess.is_running5   s    }9!7!7!9!99r%   c                     | j         S r<   )r9   r!   s    r#   
start_timez_BackgroundProcess.start_time9   s    r%   c                       j         rdS i  _         fd}t          j        |           _        d j        _         j                                         t          j                     _        d S )NFc                  ~    	  j         i } n"# t          $ r}|j        d<   Y d }~d S d }~ww xY w| j        d<   d S )N	exceptionresult)r5   	Exceptionr7   )rD   eargskwargsr"   s     r#   
entrypointz,_BackgroundProcess.start.<locals>.entrypointC   sl    !42622   ,-[) &,DL"""s    
0
+0)targetT)	r>   r7   	threadingThreadr6   daemonstarttimer9   )r"   rG   rH   rI   s   ``` r#   rN   z_BackgroundProcess.start=   s    ? 	5	, 	, 	, 	, 	, 	, 	, "(
;;;#9;;r%   Ntimeoutr   c           	         | j         sdS d}|r.t          j                    | j        z
  }t          ||z
  d          }| j                             |           | j                                         rBd| _         t          t          | j        dt          | j                             d| d          d| _         | j
                            d          }|r|| j
                            d          }i | _
        |S )	zWaits for the background process to finish running. Waits until the
        background process has run for at least `timeout` seconds, counting from
        the time when the process was started.Nr   rP   r&   z. did not finish running within the timeout of z	 seconds.rC   rD   )r6   rO   r@   maxjoinr=   TimeoutErrorgetattrr5   r.   r7   get)r"   rP   time_remainingelapsedrC   rD   s         r#   waitz_BackgroundProcess.waitQ   s    } 	4 	7ikkDO3G 7!2A66N>222=!!## 	 DM48ZTX?? D D18D D D  
 L$$[11	 	O!!(++r%   r<   )r&   r'   r(   r   r:   propertyr>   r@   rN   r   r8   r   rZ   r/   r%   r#   r1   r1   .   s        )8 ) ) ) ) : : X:     X ' ' '( HUO s      r%   r1   c            
       l   e Zd ZdZeefdedefdZej	        	 dde
de
dee         d	efd
            Zej	        	 dde
de
dee         d	efd            Zej	        de
d	efd            Zd Zddee         fdZ	 dde
de
dee         d	efdZ	 dde
de
dee         fdZddedefdZd Zd Zd	e
fdZdS )Syncera  Syncer class for synchronizing data between Ray nodes and remote (cloud) storage.

    This class handles data transfer for two cases:

    1. Synchronizing data such as experiment state snapshots from the driver to
       cloud storage.
    2. Synchronizing data such as trial checkpoints from remote trainables to
       cloud storage.

    Synchronizing tasks are usually asynchronous and can be awaited using ``wait()``.
    The base class implements a ``wait_or_retry()`` API that will retry a failed
    sync command.

    The base class also exposes an API to only kick off syncs every ``sync_period``
    seconds.

    Args:
        sync_period: The minimum time in seconds between sync operations, as
            used by ``sync_up/down_if_needed``.
        sync_timeout: The maximum time to wait for a sync process to finish before
            issuing a new sync operation. Ex: should be used by ``wait`` if launching
            asynchronous sync tasks.
    r   r   c                 r    || _         || _        t          d          | _        t          d          | _        d S r4   )r   r   r8   last_sync_up_timelast_sync_down_time)r"   r   r   s      r#   r:   zSyncer.__init__   s6    
 '(!&v#(==   r%   N	local_dir
remote_direxcluder   c                     t           )a(  Synchronize local directory to remote directory.

        This function can spawn an asynchronous process that can be awaited in
        ``wait()``.

        Args:
            local_dir: Local directory to sync from.
            remote_dir: Remote directory to sync up to. This is an URI
                (``protocol://remote/path``).
            exclude: Pattern of files to exclude, e.g.
                ``["*/checkpoint_*]`` to exclude trial checkpoints.

        Returns:
            True if sync process has been spawned, False otherwise.

        NotImplementedError)r"   ra   rb   rc   s       r#   sync_upzSyncer.sync_up   
    ( "!r%   c                     t           )a*  Synchronize remote directory to local directory.

        This function can spawn an asynchronous process that can be awaited in
        ``wait()``.

        Args:
            remote_dir: Remote directory to sync down from. This is an URI
                (``protocol://remote/path``).
            local_dir: Local directory to sync to.
            exclude: Pattern of files to exclude, e.g.
                ``["*/checkpoint_*]`` to exclude trial checkpoints.

        Returns:
            True if sync process has been spawned, False otherwise.

        re   )r"   rb   ra   rc   s       r#   	sync_downzSyncer.sync_down   rh   r%   c                     t           )ag  Delete directory on remote storage.

        This function can spawn an asynchronous process that can be awaited in
        ``wait()``.

        Args:
            remote_dir: Remote directory to delete. This is an URI
                (``protocol://remote/path``).

        Returns:
            True if sync process has been spawned, False otherwise.

        re   )r"   rb   s     r#   deletezSyncer.delete   s
     "!r%   c                     dS )zRetry the last sync up, sync down, or delete command.

        You should implement this method if you spawn asynchronous syncing
        processes.
        Nr/   r!   s    r#   retryzSyncer.retry   s	     	r%   rP   c                     dS )a  Wait for asynchronous sync command to finish.

        You should implement this method if you spawn asynchronous syncing
        processes. This method should timeout after the asynchronous command
        has run for `sync_timeout` seconds and raise a `TimeoutError`.
        Nr/   )r"   rP   s     r#   rZ   zSyncer.wait   s	     	r%   c                     t          j                     }|| j        z
  | j        k    r!|                     |||          }|| _        |S dS )a}  Syncs up if time since last sync up is greater than sync_period.

        Args:
            local_dir: Local directory to sync from.
            remote_dir: Remote directory to sync up to. This is an URI
                (``protocol://remote/path``).
            exclude: Pattern of files to exclude, e.g.
                ``["*/checkpoint_*]`` to exclude trial checkpoints.
        )ra   rb   rc   N)rO   r_   r   rg   )r"   ra   rb   rc   nowrD   s         r#   sync_up_if_neededzSyncer.sync_up_if_needed   s[     ikk''4+;;;\\#
G "  F &)D"M <;r%   c                     t          j                     }|| j        z
  | j        k    r!|                     |||          }|| _        |S dS )a  Syncs down if time since last sync down is greater than sync_period.

        Args:
            remote_dir: Remote directory to sync down from. This is an URI
                (``protocol://remote/path``).
            local_dir: Local directory to sync to.
            exclude: Pattern of files to exclude, e.g.
                ``["*/checkpoint_*]`` to exclude trial checkpoints.
        )rb   ra   rc   N)rO   r`   r   rj   )r"   rb   ra   rc   rq   rD   s         r#   sync_down_if_neededzSyncer.sync_down_if_needed   s[     ikk))T-===^^%G $  F (+D$M >=r%         max_retries	backoff_sc                    |dk    sJ d }t          |dz             D ]}	 |                                  n# t          $ r}||z
  }|dk    rt          j                    }Y d }~ nit
                              dt          |           d| d| d           t          j	        |           | 
                                 Y d }~d }~ww xY w d S t          d| d|           )	Nr      z;The latest sync operation failed with the following error: z

Retrying z! more time(s) after sleeping for z seconds...zFailed sync even after z; retries. The latest sync failed with the following error:
)rangerZ   rE   	traceback
format_excloggererrorreprrO   sleeprn   RuntimeError)r"   rw   rx   last_error_tracebackirF   attempts_remainings          r#   wait_or_retryzSyncer.wait_or_retry  sM   Q#{Q'' 	 	A		   %01_" &**+4+?+A+A(EEEEE2Aww2 2 22 2 %2 2 2   
9%%%

#& FFXk X XAUX X
 
 	
s   5
CC#ACCc                 V    t          d          | _        t          d          | _        d S r4   )r8   r_   r`   r!   s    r#   resetzSyncer.reset+  s$    !&v#(==   r%   c                     d S r<   r/   r!   s    r#   closezSyncer.close/  s    r%   c                     d S r<   r/   r!   s    r#   r$   zSyncer._repr_html_2  s    r%   r<   )ru   rv   )r&   r'   r(   __doc__r)   r,   r8   r:   abcabstractmethodr.   r   r   r-   rg   rj   rl   rn   rZ   rr   rt   r*   r   r   r   r$   r/   r%   r#   r]   r]   r   s6        4 121 11 1 1 1 1 	IM" ""*-"8@"	" " " "* 	IM" ""*-"8@"	" " " "* 	" " " " " "    HUO     JN *-8@	   * JN *-8@   (
 
 
S 
 
 
 
>1 1 1  S      r%   r]   c                   V    e Zd ZdZeefdedef fdZd Zde	e
ef         fdZ	 dd	ed
edee         defdZ	 ddededee         de	e
ef         fdZ	 dd
ed	edee         defdZdedede	e
ef         fdZd
edefdZdede	e
ef         fdZddee         fdZd Zd Z xZS )_BackgroundSyncerzASyncer using a background process for asynchronous file transfer.r   r   c                 x    t          t          |                               ||           d | _        d | _        d S )N)r   r   )superr   r:   _sync_process_current_cmd)r"   r   r   	__class__s      r#   r:   z_BackgroundSyncer.__init__9  sJ    
 	&&//#, 	0 	
 	
 	
 " r%   c                 z    | j         o4| j         j        o(t          j                    | j         j        z
  | j        k     S )zDReturns whether a previous sync is still running within the timeout.)r   r>   rO   r@   r   r!   s    r#   _should_continue_existing_syncz0_BackgroundSyncer._should_continue_existing_syncD  s@      P"-P	d0;;d>OO	
r%   sync_commandc                     | j         rT	 |                                  n># t          $ r1 t                              dt          j                                Y nw xY w|| _        |                                  dS )zoWaits for the previous sync process to finish,
        then launches a new process that runs the given command.z3Last sync command failed with the following error:
N)	r   rZ   rE   r~   warningr|   r}   r   rn   )r"   r   s     r#   _launch_sync_processz&_BackgroundSyncer._launch_sync_processL  s      			   0 +--0 0     )

s    8AANra   rb   rc   r   c                     |                                  r"t                              d| d|            dS |                     |||          }|                     |           dS )Nz1Last sync still in progress, skipping sync up of  to F)
local_pathurirc   T)r   r~   debug_sync_up_commandr   )r"   ra   rb   rc   sync_up_cmds        r#   rg   z_BackgroundSyncer.sync_up[  s     ..00 	LLC'0C C6@C C   5++ j' , 
 
 	!!+...tr%   r   r   c                     t           r<   re   )r"   r   r   rc   s       r#   r   z"_BackgroundSyncer._sync_up_commandl  s
     "!r%   c                     |                                  r"t                              d| d|            dS |                     ||          }|                     |           dS )Nz3Last sync still in progress, skipping sync down of r   F)r   r   T)r   r~   r   _sync_down_commandr   )r"   rb   ra   rc   sync_down_cmds        r#   rj   z_BackgroundSyncer.sync_downq  s     ..00 	NNE)3E E9BE E   5//J9/UU!!-000tr%   c                     t           r<   re   )r"   r   r   s      r#   r   z$_BackgroundSyncer._sync_down_command      !!r%   c                     |                                  rt                              d|            dS |                     |          }|                     |           dS )Nz2Last sync still in progress, skipping deletion of F)r   T)r   r~   r   _delete_commandr   )r"   rb   
delete_cmds      r#   rl   z_BackgroundSyncer.delete  sm    ..00 	NNQZQQ   5))j)99
!!*---tr%   c                     t           r<   re   )r"   r   s     r#   r   z!_BackgroundSyncer._delete_command  r   r%   rP   c                     | j         rN	 | j                             |p| j                   n# t          $ r}|d }~ww xY w	 d | _         d S # d | _         w xY wd S )NrR   )r   rZ   r   rE   )r"   rP   rF   s      r#   rZ   z_BackgroundSyncer.wait  s     	**"''0L4;L'MMMM    N &*"""T"))))	* 	*s    ", A
 
=8=A
 
	Ac                     | j         st          d          | j         \  }}t          |          | _         | j        j        di | d S )Nz"No sync command set, cannot retry.r/   )r   r   r1   r   rN   )r"   cmdrH   s      r#   rn   z_BackgroundSyncer.retry  s[      	ECDDD'V/44  **6*****r%   c                 B    | j                                         }d |d<   |S )Nr   )__dict__copy)r"   states     r#   __getstate__z_BackgroundSyncer.__getstate__  s$    ""$$!%or%   r<   )r&   r'   r(   r   r)   r,   r8   r:   r   r	   r   r   r   r.   r   r   r-   rg   r   rj   r   rl   r   rZ   rn   r   __classcell__)r   s   @r#   r   r   6  s+       KK 12	! 	!	! 	! 	! 	! 	! 	! 	!
 
 
x~1F      JN *-8@	   $ DH" ""$'"2:4."	x~	" " " " JN *-8@	   "c "s "uXt^?T " " " "
 
 
 
 
 
"3 "54+@ " " " "	* 	*HUO 	* 	* 	* 	*+ + +      r%   r   )r   loggingrK   rO   r|   dataclassesr   typingr   r   r   r   r   r	   )ray._private.thirdparty.tabulate.tabulater
   ray.util.annotationsr   r   ray.widgetsr   	getLoggerr&   r~   r)   r,   r   r1   ABCr]   r   r/   r%   r#   <module>r      s   



           ! ! ! ! ! ! = = = = = = = = = = = = = = = = > > > > > > 9 9 9 9 9 9 9 9            		8	$	$     

 
 
 
 
 
 
  
,A A A A A A A AH @ @ @ @ @SW @ @ @Fq q q q q q q q q qr%   