
    &`iH                     <   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
mZ d dlmZmZmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)  e j*        e+          Z,e-Z.e	 G d d                      Z/ G d de0          Z1 G d de1          Z2 G d de1          Z3 G d de          Z4 e	d           G d d                      Z5 e	d           G d d                      Z6 G d de4          Z7dS )     N)ABCabstractmethod)ThreadPoolExecutor)	dataclass)Queue)AnyDictListOptional)"AUTOSCALER_MAX_CONCURRENT_LAUNCHESAUTOSCALER_MAX_LAUNCH_BATCH)hash_launch_conf)NodeProvider)
NODE_KIND_HEADNODE_KIND_UNMANAGEDNODE_KIND_WORKERSTATUS_UNINITIALIZEDTAG_RAY_LAUNCH_CONFIGTAG_RAY_LAUNCH_REQUESTTAG_RAY_NODE_KINDTAG_RAY_NODE_NAMETAG_RAY_NODE_STATUSTAG_RAY_USER_NODE_TYPE)IConfigReader)NodeType)NodeKindc                   V    e Zd ZU dZeed<   eed<   eed<   eed<   dZ	e
e         ed<   dS )CloudInstancezu
    A class that represents a cloud instance in the cluster, with necessary metadata
    of the cloud instance.
    cloud_instance_id	node_type	node_kind
is_runningN
request_id)__name__
__module____qualname____doc__CloudInstanceId__annotations__r   r   boolr#   r   str     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/v2/instance_manager/node_provider.pyr   r   ,   sd           '&&& !%J$$$$$r-   r   c                   0     e Zd ZU dZeed<   d fdZ xZS )CloudInstanceProviderErrorzh
    An base error class that represents an error that happened in the cloud instance
    provider.
    timestamp_nsreturnNc                 X    t                                          |           || _        d S N)super__init__r1   )selfmsgr1   	__class__s      r.   r6   z#CloudInstanceProviderError.__init__N   s)    (r-   r2   N)r$   r%   r&   r'   intr)   r6   __classcell__r9   s   @r.   r0   r0   E   sW           ) ) ) ) ) ) ) ) ) )r-   r0   c                   z     e Zd ZU eed<   eed<   eed<   	 	 ddedededededee         d	df fd
Z	d	efdZ
 xZS )LaunchNodeErrorr    countr#    Nr1   detailscauser2   c                     d| d| d| d| }t                                          ||           || _        || _        || _        |r	|| _        d S d S )NzFailed to launch z nodes of type  with request id : r1   )r5   r6   r    r@   r#   	__cause__)	r7   r    r@   r#   r1   rB   rC   r8   r9   s	           r.   r6   zLaunchNodeError.__init__[   s    2 2 2i 2 2$2 2(/2 2 	 	<888"
$ 	#"DNNN	# 	#r-   c                 F    d| j          d| j         d| j         d| j         S )NzLaunchNodeError(node_type=z, count=, request_id=): )r    r@   r#   rH   r7   s    r.   __repr__zLaunchNodeError.__repr__o   sF    ? ? ? ? ?/? ?.2n? ?	
r-   rA   N)r$   r%   r&   r   r)   r;   r+   r   	Exceptionr6   rM   r<   r=   s   @r.   r?   r?   S   s         JJJOOO %)# ## # 	#
 # # 	"# 
# # # # # #(
# 
 
 
 
 
 
 
 
r-   r?   c                   l     e Zd ZU eed<   eed<   	 	 ddededededee         ddf fd	Z	defd
Z
 xZS )TerminateNodeErrorr   r#   rA   Nr1   rB   rC   r2   c                     d| d| d| }t                                          ||           || _        || _        |r	|| _        d S d S )NzFailed to terminate node rE   rF   rG   )r5   r6   r   r#   rH   )r7   r   r#   r1   rB   rC   r8   r9   s          r.   r6   zTerminateNodeError.__init__|   s    2(9 2 2$2 2(/2 2 	 	<888!2$ 	#"DNNN	# 	#r-   c                 6    d| j          d| j         d| j         S )Nz%TerminateNodeError(cloud_instance_id=rJ   rK   )r   r#   rH   rL   s    r.   rM   zTerminateNodeError.__repr__   s9    ?D4J ? ?/? ?.2n? ?	
r-   rN   )r$   r%   r&   r(   r)   r+   r;   r   rO   r6   rM   r<   r=   s   @r.   rQ   rQ   v   s         &&&&OOO %)# #*# # 	#
 # 	"# 
# # # # # #$
# 
 
 
 
 
 
 
 
r-   rQ   c                       e Zd ZdZedeeef         fd            Zede	e         de
ddfd            Zedeeef         de
ddfd	            Zede	e         fd
            ZdS )ICloudInstanceProvidera  
    The interface for a cloud instance provider.

    This interface is a minimal interface that should be implemented by the
    various cloud instance providers (e.g. AWS, and etc).

    The cloud instance provider is responsible for managing the cloud instances in the
    cluster. It provides the following main functionalities:
        - Launch new cloud instances.
        - Terminate existing running instances.
        - Get the non-terminated cloud instances in the cluster.
        - Poll the errors that happened for the updates to the cloud instance provider.

    Below properties of the cloud instance provider are assumed with this interface:

    1. Eventually consistent
    The cloud instance provider is expected to be eventually consistent with the
    cluster state. For example, when a cloud instance is request to be terminated
    or launched, the provider may not immediately reflect the change in its state.
    However, the provider is expected to eventually reflect the change in its state.

    2. Asynchronous
    The provider could also be asynchronous, where the termination/launch
    request may not immediately return the result of the request.

    3. Unique cloud instance ids
    Cloud instance ids are expected to be unique across the cluster.

    4. Idempotent updates
    For the update APIs (e.g. ensure_min_nodes, terminate), the provider may use the
    request ids to provide idempotency.

    Usage:
        ```
            provider: ICloudInstanceProvider = ...

            # Update the cluster with a desired shape.
            provider.launch(
                shape={
                    "worker_nodes": 10,
                    "ray_head": 1,
                },
                request_id="1",
            )

            # Get the non-terminated nodes of the cloud instance provider.
            running = provider.get_non_terminated()

            # Poll the errors
            errors = provider.poll_errors()

            # Terminate nodes.
            provider.terminate(
                ids=["cloud_instance_id_1", "cloud_instance_id_2"],
                request_id="2",
            )

            # Process the state of the provider.
            ...
        ```
    r2   c                     dS )zGet the non-terminated cloud instances in the cluster.

        Returns:
            A dictionary of the non-terminated cloud instances in the cluster.
            The key is the cloud instance id, and the value is the cloud instance.
        Nr,   rL   s    r.   get_non_terminatedz)ICloudInstanceProvider.get_non_terminated   s	     	r-   idsr#   Nc                     dS )a  
        Terminate the cloud instances asynchronously.

        This method is expected to be idempotent, i.e. if the same request id is used
        to terminate the same cloud instances, this should be a no-op if
        the cloud instances are already terminated or being terminated.

        Args:
            ids: the cloud instance ids to terminate.
            request_id: a unique id that identifies the request.
        Nr,   r7   rX   r#   s      r.   	terminatez ICloudInstanceProvider.terminate   s	     	r-   shapec                     dS )zLaunch the cloud instances asynchronously.

        Args:
            shape: A map from node type to number of nodes to launch.
            request_id: a unique id that identifies the update request.
        Nr,   r7   r\   r#   s      r.   launchzICloudInstanceProvider.launch   s	     	r-   c                     dS )z
        Poll the errors that happened since the last poll.

        This method would also clear the errors that happened since the last poll.

        Returns:
            The errors that happened since the last poll.
        Nr,   rL   s    r.   poll_errorsz"ICloudInstanceProvider.poll_errors   s	     	r-   )r$   r%   r&   r'   r   r	   r(   r   rW   r
   r+   r[   r   r;   r_   r0   ra   r,   r-   r.   rU   rU      s        < <| D-)G$H    ^ T/2      ^ HcM"  
	   ^ 	T"<= 	 	 	 ^	 	 	r-   rU   T)frozenc                   2    e Zd ZU dZeed<   eed<   eed<   dS )CloudInstanceLaunchRequestz)
    The arguments to launch a node.
    r    r@   r#   N)r$   r%   r&   r'   r   r)   r;   r+   r,   r-   r.   rd   rd     s:          
 JJJOOOOOr-   rd   c                   (    e Zd ZU dZeed<   eed<   dS )CloudInstanceTerminateRequestz,
    The arguments to terminate a node.
    r   r#   N)r$   r%   r&   r'   r(   r)   r+   r,   r-   r.   rf   rf     s1          
 '&&&OOOOOr-   rf   c                       e Zd ZdZeefdededededdf
 fdZ	e
defd	            Zdeeef         fd
Zdee         fdZdeeef         deddfdZdee         deddfdZdeeef         deddfdZdee         deddfdZdedededdfdZ	 dee         deeeef                  fdZdeeef         dee         fdZdedefdZddZ dedeeef         fdZ!defdZ" xZ#S )NodeProviderAdaptera@  
    Warps a NodeProviderV1 to a ICloudInstanceProvider.

    TODO(rickyx):
    The current adapter right now consists of two sets of APIs:
    - v1: the old APIs that are used by the autoscaler, where
    we forward the calls to the NodeProviderV1.
    - v2: the new APIs that are used by the autoscaler v2, this is
    defined in the ICloudInstanceProvider interface.

    We should eventually remove the v1 APIs and only use the v2 APIs.
    It's currently left as a TODO since changing the v1 APIs would
    requires a lot of changes in the cluster launcher codebase.
    v1_providerconfig_readermax_launch_batch_per_typemax_concurrent_launchesr2   Nc                 8   t                                                       || _        || _        t	          dd          | _        || _        t          j        |t          |          z            }t	          |d          | _
        t                      | _        dS )ai  
        Args:
            v1_provider: The v1 node provider to wrap.
            config_reader: The config reader to read the autoscaling config.
            max_launch_batch_per_type: The maximum number of nodes to launch per
                node type in a single batch.
            max_concurrent_launches: The maximum number of concurrent launches.
           zray::NodeProviderAdapter)max_workersthread_name_prefixzray::NodeLauncherPoolN)r5   r6   _v1_provider_config_readerr   _main_executor_max_launch_batch_per_typemathceilfloat_node_launcher_executorsr   _errors_queue)r7   ri   rj   rk   rl   max_batchesr9   s         r.   r6   zNodeProviderAdapter.__init__2  s      	'+0.H
 
 

 +D'i#e,E&F&FF
 
 );#6)
 )
 )
% #WWr-   c                     | j         S r4   )rq   rL   s    r.   ri   zNodeProviderAdapter.v1_providerX  s      r-   c           
         i }|                      i           }|D ]}|                     |          }|                    t          t                    }|t          k    rC|t
          k    rt          j        }n*|t          k    rt          j	        }nt          d|           t          ||                    t          d          |                     |          |                    t          d          |          ||<   |S )NzInvalid node kind: rA   )r   r    r"   r#   r!   )_v1_non_terminated_nodes_v1_node_tagsgetr   r   r   r   WORKERr   HEAD
ValueErrorr   r   _v1_is_runningr   )r7   nodescloud_instance_idsr   	node_tagsnode_kind_tagr!   s          r.   rW   z&NodeProviderAdapter.get_non_terminated\  s   !::2>> "4 	 	**+<==I%MM*;=PQQM 333"222$O		.00$M		 !F}!F!FGGG'4"3#--(>CC../@AA$==)?DD#( ( (E#$$ r-   c                     g }| j                                         sE|                    | j                                                    | j                                         E|S r4   )ry   emptyappend
get_nowait)r7   errorss     r.   ra   zNodeProviderAdapter.poll_errorsz  s]    $**,, 	;MM$,7799::: $**,, 	;r-   r\   r#   c                 H    | j                             | j        ||           d S r4   )rs   submit
_do_launchr^   s      r.   r_   zNodeProviderAdapter.launch  s'    
 	""4?E:FFFFFr-   rX   c                 H    | j                             | j        ||           d S r4   )rs   r   _do_terminaterZ   s      r.   r[   zNodeProviderAdapter.terminate  s&    ""4#5sJGGGGGr-   c                     |                                 D ]M\  }}|dk    rBt          || j                  }| j                            | j        |||           ||z  }|dk    BNdS )z
        Launch the cloud instances by calling into the v1 base node provider.

        Args:
            shape: The requested to launch node type and number of nodes.
            request_id: The request id that identifies the request.
        r   N)itemsminrt   rx   r   _launch_nodes_by_type)r7   r\   r#   r    r@   	to_launchs         r.   r   zNodeProviderAdapter._do_launch  s     !& 
	# 
	#Iu!))t'FGG	-44.	   " !))
	# 
	#r-   c           
         	 |                      |           dS # t          $ r`}|D ]R}t          ||t          t	          j                                        }||_        | j                            |           SY d}~dS d}~ww xY w)aK  
        Terminate the cloud instances by calling into the v1 base node provider.

        If errors happen during the termination, the errors will be put into the
        errors queue.

        Args:
            ids: The cloud instance ids to terminate.
            request_id: The request id that identifies the request.
        N)	_v1_terminate_nodesrO   rQ   r;   timetime_nsrH   ry   put)r7   rX   r#   eiderrors         r.   r   z!NodeProviderAdapter._do_terminate  s    	.$$S))))) 	. 	. 	. . .*2z3t|~~;N;NOO"#"&&u----. . . . . .	.s    
BAA>>Br    r@   c                    	 | j                                         }|                    |          }|                    |          }|                    |          }t          ||                    di                     }t          d                    |                    dd                    t          t          t          t          t          |t          |t          |i}	t                               d                    ||                     | j                            ||	|||           t                               d                    ||                     dS # t(          $ r}
t                               d                    |||
                     t+          |||t-          t/          j                                        }|
|_        | j                            |           Y d}
~
dS d}
~
ww xY w)	a  
        Launch nodes of the given node type.

        Args:
            node_type: The node type to launch.
            count: Number of nodes to launch.
            request_id: A unique id that identifies the request.

        Raises:
            ValueError: If the node type is invalid.
            LaunchNodeError: If the launch failed and raised by the underlying provider.
        authzray-{}-workercluster_namerA   zLaunching {} nodes of type {}.zLaunched {} nodes of type {}.z(Failed to launch {} nodes of type {}: {}N)rr   get_cached_autoscaling_configget_cloud_node_configget_node_resourcesget_node_labelsr   
get_configr   formatr   r   r   r   r   r   r   loggerinforq   %create_node_with_resources_and_labelsrO   r?   r;   r   r   rH   ry   r   )r7   r    r@   r#   configlaunch_config	resourceslabelslaunch_hashr   r   r   s               r.   r   z)NodeProviderAdapter._launch_nodes_by_type  s   &"	*(FFHHF"88CCM11)<<I++I66F +=&:K:KFTV:W:WXXK!?#9#9%%nb99$ $ "#3#%9%{&
&		I KK8??yQQRRRCCy%F   KK7>>uiPPQQQQQ 	* 	* 	*KK:AA%TUVV   $Iuj#dlnnBUBUVVEEO""5)))))))))	*s   EE 
G,!B G''G,c                 6    | j                             |          S r4   )rq   terminate_nodes)r7   rX   s     r.   r   z'NodeProviderAdapter._v1_terminate_nodes  s      00555r-   tag_filtersc                 6    | j                             |          S r4   )rq   non_terminated_nodes)r7   r   s     r.   r}   z,NodeProviderAdapter._v1_non_terminated_nodes  s      55kBBBr-   node_idc                 6    | j                             |          S r4   )rq   r"   r7   r   s     r.   r   z"NodeProviderAdapter._v1_is_running  s     ++G444r-   c                 8    | j                                          d S r4   )rq   post_processrL   s    r.   _v1_post_processz$NodeProviderAdapter._v1_post_process
  s    &&(((((r-   c                 6    | j                             |          S r4   )rq   r   r   s     r.   r~   z!NodeProviderAdapter._v1_node_tags  s     **7333r-   c                 4    | j                                         S r4   )rq   safe_to_scalerL   s    r.   _v1_safe_to_scalez%NodeProviderAdapter._v1_safe_to_scale  s     ..000r-   r:   )$r$   r%   r&   r'   r   r   NodeProviderV1r   r;   r6   propertyri   r	   r(   r   rW   r
   r0   ra   r   r+   r_   r[   r   r   r   r   r   r   r}   r*   r   r   r~   r   r<   r=   s   @r.   rh   rh   "  s        & *E'I$% $%#$% %$% $'	$%
 "%$% 
$% $% $% $% $% $%L !^ ! ! ! X!D-)G$H    <T"<=    GHcM"G G 
	G G G GHT/2 H H H H H H#HcM"# # 
	# # # #0.o!6 .C .D . . . .(5*5* 5* 	5*
 
5* 5* 5* 5*t6(6	$sCx.	!6 6 6 6
CS>C	o	C C C C
5o 5$ 5 5 5 5) ) ) )4_ 4c3h 4 4 4 414 1 1 1 1 1 1 1 1r-   rh   )8loggingru   r   abcr   r   concurrent.futuresr   dataclassesr   queuer   typingr   r	   r
   r   !ray.autoscaler._private.constantsr   r   ray.autoscaler._private.utilr   ray.autoscaler.node_providerr   r   ray.autoscaler.tagsr   r   r   r   r   r   r   r   r   r   )ray.autoscaler.v2.instance_manager.configr   ray.autoscaler.v2.schemar   'ray.core.generated.instance_manager_pb2r   	getLoggerr$   r   r+   r(   r   rO   r0   r?   rQ   rU   rd   rf   rh   r,   r-   r.   <module>r      sU      # # # # # # # # 1 1 1 1 1 1 ! ! ! ! ! !       , , , , , , , , , , , ,        : 9 9 9 9 9 G G G G G G                        D C C C C C - - - - - - < < < < < <		8	$	$  % % % % % % % %0) ) ) ) ) ) ) ) 
  
  
  
  
0  
  
  
F
 
 
 
 
3 
 
 
>p p p p pS p p pf $
 
 
 
 
 
 
 
 $       o1 o1 o1 o1 o10 o1 o1 o1 o1 o1r-   