
    &`iM                     X    d dl Z d dlZd dlmZmZmZmZ d dlmZm	Z	  G d d          Z
dS )    N)DictListOptionalSet)InstanceInstanceUpdateEventc                   (   e Zd ZU dZdZeeded         f                  ed<   e		 d"de
de
dej        d	e
d
ef
d            Ze	d
e
fd            Ze	dej        d
efd            Ze	dej        d
efd            Ze	dej        d
efd            Zdej        d
efdZe		 d"dedej        d	e
d
efd            Ze	dedej        d	e
fd            Ze	deded
efd            Ze	d
eded         f         fd            Ze		 d#deded         d
ed         fd            Ze		 d#deded         d
ed         fd            Ze		 d#deded         d
ee         fd            Zedej        d
ed         fd            Ze	deded
e
fd             Z ed!             Z!dS )$InstanceUtilzt
    A helper class to group updates and operations on an Instance object defined
    in instance_manager.proto
    NzInstance.InstanceStatus_reachable_from instance_idinstance_typestatusdetailsreturnc                     t                      }d|_        | |_        ||_        ||_        t
                              |||           |S )a  
        Returns a new instance with the given status.

        Args:
            instance_id: The instance id.
            instance_type: The instance type.
            status: The status of the new instance.
            details: The details of the status transition.
        r   )r   versionr   r   r   r
   _record_status_transition)r   r   r   r   instances        }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/v2/instance_manager/common.pynew_instancezInstanceUtil.new_instance   sI      ::*!. ..xIII    c                  B    t          t          j                              S )z/
        Returns a random instance id.
        )struuiduuid4 r   r   random_instance_idzInstanceUtil.random_instance_id-   s    
 4:<<   r   instance_statusc                 
   | t           j        k    sJ | t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j	        t           j
        t           j        h
v S )z
        Returns True if the instance is in a status where there could exist
        a cloud instance allocated by the cloud provider.
        )r   UNKNOWN	ALLOCATEDRAY_INSTALLINGRAY_RUNNINGRAY_STOPPINGRAY_STOP_REQUESTEDRAY_STOPPEDTERMINATINGRAY_INSTALL_FAILEDTERMINATION_FAILEDALLOCATION_TIMEOUTr   s    r   is_cloud_instance_allocatedz(InstanceUtil.is_cloud_instance_allocated4   sf     ("22222# !'  '''#
 
 	
r   c                     | t           j        k    sJ | t                              t           j                  v rdS | t                              t           j                  v rdS dS )z
        Returns True if the instance is in a status where the ray process is
        running on the cloud instance.
            i.e. RAY_RUNNING, RAY_STOP_REQUESTED, RAY_STOPPING
        FT)r   r!   r
   get_reachable_statusesr%   r$   r,   s    r   is_ray_runningzInstanceUtil.is_ray_runningH   sj     ("22222lAA!
 
 
 
 5lAA(BVWWWW4ur   c                     | t           j        k    sJ t           j        t                              |           vrdS | t                              t           j                  v rdS dS )z
        Returns True if the instance is in a status where the ray process is
        pending to be started on the cloud instance.

        FT)r   r!   r$   r
   r/   r,   s    r   is_ray_pendingzInstanceUtil.is_ray_pending[   sl     ("22222|'J'J(
 (
 
 
 5 lAA(BVWWWW5tr   c                 N    t           j        t                              |           v S )zt
        Returns True if the instance is in a status where it may transition
        to RAY_RUNNING status.
        )r   r$   r
   r/   r,   s    r   is_ray_running_reachablez%InstanceUtil.is_ray_running_reachableo   s)    
 #|'J'J(
 (
 
 	
r   r   new_instance_statusc                     |t                                           | j                 vrdS || _        t                               | ||           dS )aB  Transitions the instance to the new state.

        Args:
            instance: The instance to update.
            new_instance_status: The new status to transition to.
            details: The details of the transition.

        Returns:
            True if the status transition is successful, False otherwise.
        FT)r
   get_valid_transitionsr   r   )r   r5   r   s      r   
set_statuszInstanceUtil.set_statusx   sU    "  5577HI I 5-..x9LgVVVtr   c                     t          j                    }| j                            t	          j        |||                     dS )zRecords the status transition.

        Args:
            instance: The instance to update.
            status: The new status to transition to.
        )r   timestamp_nsr   N)timetime_nsstatus_historyappendr   StatusHistory)r   r   r   now_nss       r   r   z&InstanceUtil._record_status_transition   sR     &&" &#  	
 	
 	
 	
 	
r   	timeout_sc           	      `   | j         }t                              | |          }t          |          dk    sCJ d| j         dt          |           dt
          j                            |           d            t          |          d         }t          j
                    |z
  |dz  k    rd	S d
S )al  
        Returns True if the instance has been in the current status for more
        than the timeout_seconds.

        Args:
            instance: The instance to check.
            timeout_seconds: The timeout in seconds.

        Returns:
            True if the instance has been in the current status for more than
            the timeout_s seconds.
        )select_instance_status   z	instance z has  z statusg    eAFT)r   r
   get_status_transition_times_nslenr   r   InstanceStatusNamesortedr;   r<   )r   rA   
cur_statusstatus_times_nsstatus_time_nss        r   has_timeoutzInstanceUtil.has_timeout   s     _
&EEZ F 
 
 ?##q(((A, A A33G3G A A&++J77A A A )((  004<>>N*y3??5tr   c                     t           j        t           j        ht           j        t           j        t           j        t           j        ht           j        t           j        t           j        t           j        t           j        t           j	        t           j
        t           j        ht           j        t           j        t           j        t           j	        t           j
        t           j        ht           j        t           j        t           j        t           j	        t           j
        t           j        ht           j        t           j        t           j	        t           j        t           j        ht           j        t           j
        ht           j        t           j	        t           j
        t           j        ht           j	        t           j
        t           j        ht           j
        t           j        t           j        ht           j        t           j
        ht           j        t                      t           j        t                      t           j        t           j
        t           j        ht           j        t                      iS N)r   QUEUED	REQUESTEDr"   ALLOCATION_FAILEDr#   r$   r+   r%   r'   r(   
TERMINATEDr)   r&   r*   setr!   r   r   r   r7   z"InstanceUtil.get_valid_transitions   s    O "  " *
!  ' $ +%$ $#)!2 # $ +
 $ $##&*  + %$ $##  '%$ $#
* '$* ! $ $#	$   $##   #+# '$*  &'$#	* ceeu[
 [	
r   rC   zInstance.StatusHistoryc                 d    g }| j         D ]%}|r|j        |k    r|                    |           &|S )aH  
        Returns the status history of the instance.

        Args:
            instance: The instance.
            select_instance_status: The go-to status to search for, i.e. select
                only status history when the instance transitions into the status.
                If None, returns all status updates.
        )r=   r   r>   )r   rC   historystatus_updates       r   get_status_transitionsz#InstanceUtil.get_status_transitionsb  sO     %4 	* 	*M&!15KKKNN=))))r   c                 ~    t                               | |          }|                    d            |r|d         S dS )z
        Returns the last status transition of the instance.

        Args:
            instance: The instance.
            instance_status: The status to search for. If None, returns the last
                status update.
        c                     | j         S rQ   r:   )xs    r   <lambda>z9InstanceUtil.get_last_status_transition.<locals>.<lambda>  s    1> r   )keyrF   N)r
   rZ   sort)r   rC   rX   s      r   get_last_status_transitionz'InstanceUtil.get_last_status_transitionz  sI     55h@VWW11222 	2;tr   c                 L    d t                               | |          D             S )aL  
        Returns a list of timestamps of the instance status update.

        Args:
            instance: The instance.
            instance_status: The status to search for. If None, returns all
                status updates timestamps.

        Returns:
            The list of timestamps of the instance status updates.
        c                     g | ]	}|j         
S r   r]   ).0es     r   
<listcomp>z?InstanceUtil.get_status_transition_times_ns.<locals>.<listcomp>  s*     
 
 
 N
 
 
r   )r
   rZ   )r   rC   s     r   rG   z+InstanceUtil.get_status_transition_times_ns  s9     
 
!880 
 
 
 	
r   c                 R    | j         |                                  | j         |         S )at  
        Returns the set of instance status that is reachable from the given
        instance status following the status transitions.
        This method is memoized.
        Args:
            instance_status: The instance status to start from.
        Returns:
            The set of instance status that is reachable from the given instance
            status.
        )r   _compute_reachable)clsr   s     r   r/   z#InstanceUtil.get_reachable_statuses  s-     &""$$$"?33r   updatec                    |j         rOdt          j                            |j                   d| j         d| j         d| j         d| j         d|j	         S dt          j                            | j
                   dt          j                            |j                   d| j         d| j         d| j         d| j         d|j	         S )	z3Returns a log string for the given instance update.zNew instance z (id=z, type=z, cloud_instance_id=z	, ray_id=z): zUpdate instance z->)upsertr   rI   rJ   r5   r   r   cloud_instance_idnode_idr   r   )r   rk   s     r   get_log_str_for_updatez#InstanceUtil.get_log_str_for_update  s?    = 	@*//0JKK@ @'@ @080F@ @ &.%?@ @ #*	@ @ 06~	@ @<&++HO<<< <&++F,FGG< < #< < -5,B< < "*!;	< <
 &< <
 ,2>< <	
r   c                     |                                  }fdi | _        t          j                                        D ]%}t                      } |||          | j        |<   &dS )zo
        Computes and memorize the from status sets for each status machine with
        a DFS search.
        c                 j    | |         D ](}||vr"|                     |            | ||           )|S )z^
            Regular DFS algorithm to find all reachable nodes from a given node.
            )add)graphstartvisited	next_nodedfss       r   rx   z,InstanceUtil._compute_reachable.<locals>.dfs  sQ     #5\ 3 3	G++ KK	***Cy'222Nr   N)r7   r   r   rI   valuesrV   )rj   valid_transitionsr   rv   rx   s       @r   ri   zInstanceUtil._compute_reachable  s      5577
	 
	 
	 
	 
	 !-4466 	R 	RFeeG*-#.?*Q*QC''	R 	Rr   )r   rQ   )"__name__
__module____qualname____doc__r   r   r   r   __annotations__staticmethodr   r   rI   r   r   boolr-   r0   r2   r4   r8   r   intrO   r7   r   rZ   rb   rG   classmethodr/   r   rp   ri   r   r   r   r
   r
      s          	 X&,E(FFG    
 	  ' 	
 
   \. ! ! ! ! \! 
X5L 
QU 
 
 
 \
& (? D    \$ (? D    \&
(2I 
d 
 
 
 
   %4  
	   \0 

$,$;
FI
 
 
 \
$ h 3 4    \8 ^
4!3'@#AA$ ^
 ^
 ^
 \^
@  GK  ()B C 
&	'   \.  GK  ()B C 
*	+   \$  GK
 

 ()B C
 
c
 
 
 \
, 4!04 
&	'4 4 4 [4$ 
 
;N 
SV 
 
 
 \
& R R [R R Rr   r
   )r;   r   typingr   r   r   r   'ray.core.generated.instance_manager_pb2r   r   r
   r   r   r   <module>r      s      , , , , , , , , , , , , Q Q Q Q Q Q Q Q\R \R \R \R \R \R \R \R \R \Rr   