
    &`i(                     .   d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	m
Z
mZ d dlmZmZmZ d dlmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZ  e j        e          Z e G d	 d
                      Z!e G d d                      Z" G d de          Z#dS )    N)defaultdict)	dataclassfield)AnyDictListOptionalSet)DISABLE_LAUNCH_CONFIG_CHECK_KEYDISABLE_NODE_UPDATERS_KEYFOREGROUND_NODE_LAUNCH_KEY)NodeIDNodeIPNodeKind
NodeStatusNodeType)NodeProvider)NODE_KIND_HEADTAG_RAY_NODE_KINDTAG_RAY_NODE_STATUSTAG_RAY_REPLICA_INDEXTAG_RAY_USER_NODE_TYPEc                   t    e Zd ZU dZ ee          Zeee	f         e
d<    ee          Zee         e
d<   dS )ScaleRequestzStores desired scale computed by the autoscaler.

    Attributes:
        desired_num_workers: Map of worker NodeType to desired number of workers of
            that type.
        workers_to_delete: List of ids of nodes that should be removed.
    )default_factorydesired_num_workersworkers_to_deleteN)__name__
__module____qualname____doc__r   dictr   r   r   int__annotations__setr   r
   r        y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/batching_node_provider.pyr   r      sg           05uT/J/J/Jhm,JJJ%*U3%?%?%?s6{?????r'   r   c                   b    e Zd ZU dZeed<   eed<   ee         ed<   e	ed<   dZ
ee         ed<   dS )NodeDataa  Stores all data about a Ray node needed by the autoscaler.

    Attributes:
        kind: Whether the node is the head or a worker.
        type: The user-defined type of the node.
        replica_index: An identifier for nodes in a replica of a TPU worker group.
            This value is set as a Pod label by a GKE webhook when TPUs are requested
        ip: Cluster-internal ip of the node. ip can be None if the ip
            has not yet been assigned.
        status: The status of the node. You must adhere to the following semantics
            for status:
            * The status must be "up-to-date" if and only if the node is running.
            * The status must be "update-failed" if and only if the node is in an
                unknown or failed state.
            * If the node is in a pending (starting-up) state, the status should be
                a brief user-facing description of why the node is pending.
    kindtypeipstatusNreplica_index)r   r   r    r!   r   r$   r   r	   r   r   r/   strr&   r'   r(   r*   r*   &   sa          $ NNN
NNN#'M8C='''''r'   r*   c                   h   e Zd ZdZdeeef         deddfdZdeee	f         fdZ
deddfd	Zdd
Zdeeef         dee         fdZd Zdeeef         fdZdedeeef         fdZdedefdZdeeef         deeef         dedeeeef                  fdZdedeeeef                  fdZdS )BatchingNodeProvidera  Abstract subclass of NodeProvider meant for use with external cluster managers.

    Batches reads of cluster state into a single method, get_node_data, called at the
    start of an autoscaling update.

    Batches modifications to cluster state into a single method, submit_scale_request,
    called at the end of an autoscaling update.

    Implementing a concrete subclass of BatchingNodeProvider only requires overriding
    get_node_data() and submit_scale_request().

    See the method docstrings for more information.

    Note that an autoscaling update may be conditionally
    cancelled using the optional method safe_to_scale()
    of the root NodeProvider.
    provider_configcluster_namereturnNc                    t          j        | ||           i | _        |                    t          d          du sJ dt           d            |                    t
          d          du sJ dt
           d            |                    t          d          du sJ dt           d            d| _        t                      | _	        t          t          t                             | _        d S )NFTz'To use BatchingNodeProvider, must set `z:True`.)r   __init__node_data_dictgetr   r   r   scale_change_neededr   scale_requestr   listr0   replica_index_to_nodes)selfr3   r4   s      r(   r7   zBatchingNodeProvider.__init__T   s   
 	dO\BBB68
  95AATIIIW5NWWW JII""#BEJJdRRR9/9 9 9 SRR
  :EBBdJJJX5OXXX KJJ $) )^^ '2$s)&<&<###r'   c                     t           )a8  Queries cluster manager for node info. Returns a mapping from node id to
        NodeData.

        Each NodeData value must adhere to the semantics of the NodeData docstring.
        (Note in particular the requirements for NodeData.status.)

        Consistency requirement:
        If a node id was present in ScaleRequest.workers_to_delete of a previously
        submitted scale request, it should no longer be present as a key in
        get_node_data.
        (Node termination must be registered immediately when submit_scale_request
        returns.)
        NotImplementedErrorr>   s    r(   get_node_dataz"BatchingNodeProvider.get_node_datat   s
     "!r'   r;   c                     t           )a  Tells the cluster manager which nodes to delete and how many nodes of
        each node type to maintain.

        Consistency requirement:
        If a node id was present in ScaleRequest.workers_to_delete of a previously
        submitted scale request, it should no longer be present as key in get_node_data.
        (Node termination must be registered immediately when submit_scale_request
        returns.)
        r@   )r>   r;   s     r(   submit_scale_requestz)BatchingNodeProvider.submit_scale_request   s
     "!r'   c                 V    | j         r|                     | j                   d| _         dS )z3Submit a scale request if it is necessary to do so.FN)r:   rE   r;   rB   s    r(   post_processz!BatchingNodeProvider.post_process   s2    # 	:%%d&8999#(   r'   tag_filtersc                     d _                                           _        t                                           t                                 _        t           j                                                  } j	        
                                 |D ]6} j        |         j        }|  j	        |                             |           7 fd|D             }|S )NF)r   r   c                     g | ]A}                                                     |                                           k    ?|BS r&   )items	node_tags).0noder>   rH   s     r(   
<listcomp>z=BatchingNodeProvider.non_terminated_nodes.<locals>.<listcomp>   sR     
 
 
  ""dnnT&:&:&@&@&B&BBB BBBr'   )r:   rC   r8   r   cur_num_workersr%   r;   r<   keysr=   clearr/   append)r>   rH   	all_nodesnode_idr/   filtered_nodess   ``    r(   non_terminated_nodesz)BatchingNodeProvider.non_terminated_nodes   s    #( "0022 * $ 4 4 6 6!ee
 
 
 ,113344	#))+++  	K 	KG /8FM(+M:AA'JJJ

 
 
 
 
!
 
 

 r'   c                 6    |                      | j                  S )zCReturns dict mapping node type to the number of nodes of that type.)_cur_num_workersr8   rB   s    r(   rP   z$BatchingNodeProvider.cur_num_workers   s     $$T%8999r'   r8   c                     t          t                    }|                                D ](}|j        t          k    r||j        xx         dz  cc<   )|S )N   )r   r#   valuesr+   r   r,   )r>   r8   num_workers_dict	node_datas       r(   rY   z%BatchingNodeProvider._cur_num_workers   sb    &s++'..00 	2 	2I~//Y^,,,1,,,,r'   rU   c                     | j         |         }t          |j        t          |j        t
          |j        i}|j        |j        |t          <   |S N)	r8   r   r+   r   r.   r   r,   r/   r   )r>   rU   r^   tagss       r(   rL   zBatchingNodeProvider.node_tags   sJ    '0	y~!1"IN

 ".*3*AD&'r'   c                 &    | j         |         j        S r`   )r8   r-   )r>   rU   s     r(   internal_ipz BatchingNodeProvider.internal_ip   s    "7+..r'   node_configra   countc                 b    |t                    }| j        j        |xx         |z  cc<   d| _        d S )NT)r   r;   r   r:   )r>   rd   ra   re   	node_types        r(   create_nodez BatchingNodeProvider.create_node   s?     /0	.y999UB999#'   r'   c                    || j         j        v r t                              d| d           d S || j        vr t                              d| d           d S | j        |         j        }| j         j        |         dk    rt          d| d          | j         j        |xx         dz  cc<   | j         j                            |           | 	                    |          }t          |v rk|t                   }| j        |         D ]P}|| j         j        vr@| j         j                            |           t                              d| d	| d
           Qd| _        d S )Nz#Autoscaler tried to terminate node z8 twice in the same update. Skipping termination request.z*Autoscaler tried to terminate unkown node z. Skipping termination request.r   z>NodeProvider attempted to request less than 0 workers of type r[   zAutoscaler terminating node z in multi-host replica .T)r;   r   loggerwarningr8   r,   r   AssertionErroraddrL   r   r=   infor:   )r>   rU   rg   ra   node_replica_index	worker_ids         r(   terminate_nodez#BatchingNodeProvider.terminate_node   s   d(:::NN2g 2 2 2   F $---NN2W 2 2 2   F'05	 1)<AA >> > >   	.y999Q>999,00999 ~~g&& D((!%&;!<!89KL  	D$6$HHH&8<<YGGGKKGy G G1CG G G   $(   r'   )r5   N)r   r   r    r!   r   r0   r   r7   r   r*   rC   r   rE   rG   r   rW   rP   rY   rL   rc   r#   r	   rh   rr   r&   r'   r(   r2   r2   A   s        $=c3h= = 
	= = = =@"tFH$45 " " " " 
", 
"4 
" 
" 
" 
") ) ) )S#X 49    6: : :
 tCH~        	 	c3h 	 	 	 	/3 /3 / / / /(S>(15c3h(HK(	$sCx.	!( ( ( (+(c +(htCH~.F +( +( +( +( +( +(r'   r2   )$loggingcollectionsr   dataclassesr   r   typingr   r   r   r	   r
   !ray.autoscaler._private.constantsr   r   r   ray.autoscaler._private.utilr   r   r   r   r   ray.autoscaler.node_providerr   ray.autoscaler.tagsr   r   r   r   r   	getLoggerr   rk   r   r*   r2   r&   r'   r(   <module>r|      s    # # # # # # ( ( ( ( ( ( ( ( 1 1 1 1 1 1 1 1 1 1 1 1 1 1         
 X W W W W W W W W W W W W W 5 5 5 5 5 5              
	8	$	$ 
@ 
@ 
@ 
@ 
@ 
@ 
@ 
@ ( ( ( ( ( ( ( (4~( ~( ~( ~( ~(< ~( ~( ~( ~( ~(r'   