
    &`iR                     0   d dl Z d dlZd dl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 d dl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 d dl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* d dl+m,Z,  ej-        e.          Z/ G d de	          Z0 G d de          Z1 ed           G d d                      Z2e G d d                      Z3 G d d          Z4 G d de1          Z5 G d d e1          Z6 G d! d"e1          Z7dS )#    N)ABCabstractmethod)	dataclassfield)Enum)Path)AnyDictListOptional)binary_to_hex)env_integer)	GcsClient)"AUTOSCALER_MAX_CONCURRENT_LAUNCHESDEFAULT_UPSCALING_SPEEDDISABLE_LAUNCH_CONFIG_CHECK_KEYDISABLE_NODE_UPDATERS_KEY)AutoscalingConfigProducer)BASE_READONLY_CONFIG)format_readonly_node_typehash_launch_confhash_runtime_confprepare_configvalidate_config)NodeType)get_cluster_resource_state)is_head_nodec                   .    e Zd ZdZdZdZdZdZdZdZ	dZ
d	S )
Providerr                        N)__name__
__module____qualname__UNKNOWNALIYUNAWSAZUREGCPKUBERAYLOCAL	READ_ONLY     }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/v2/instance_manager/config.pyr   r   $   s6        GF
CE
CGEIIIr3   r   c                   @    e Zd ZdZedd            Zed             ZdS )IConfigReadera"  An interface for reading Autoscaling config.

    A utility class that reads autoscaling configs from various sources:
        - File
        - In-memory dict
        - Remote config service (e.g. KubeRay's config)

    Example:
        reader = FileConfigReader("path/to/config.yaml")
        # Get the recently cached config.
        config = reader.get_cached_autoscaling_config()

        ...
        # Refresh the cached config.
        reader.refresh_cached_autoscaling_config()
        config = reader.get_cached_autoscaling_config()

    returnAutoscalingConfigc                     dS )zReturns the recently read autoscaling config.

        Returns:
            AutoscalingConfig: The recently read autoscaling config.
        Nr2   selfs    r4   get_cached_autoscaling_configz+IConfigReader.get_cached_autoscaling_configC   s	     	r3   c                     dS )z Read the config from the source.Nr2   r:   s    r4   !refresh_cached_autoscaling_configz/IConfigReader.refresh_cached_autoscaling_configL   s	     	r3   N)r7   r8   )r'   r(   r)   __doc__r   r<   r>   r2   r3   r4   r6   r6   /   sY         &    ^   ^  r3   r6   T)frozenc                       e Zd ZU  edd          Zeed<    edd          Zeed<    edd          Zeed	<    ed
d          Z	eed<    edd          Z
eed<    edd          Zeed<    edd          Zeed<   dS )InstanceReconcileConfig1RAY_AUTOSCALER_RECONCILE_REQUEST_STATUS_TIMEOUT_SiX  request_status_timeout_s2RAY_AUTOSCALER_RECONCILE_ALLOCATE_STATUS_TIMEOUT_Si,  allocate_status_timeout_s5RAY_AUTOSCALER_RECONCILE_RAY_INSTALL_STATUS_TIMEOUT_Si  ray_install_status_timeout_s5RAY_AUTOSCALER_RECONCILE_TERMINATING_STATUS_TIMEOUT_Sterminating_status_timeout_s<RAY_AUTOSCALER_RECONCILE_RAY_STOP_REQUESTED_STATUS_TIMEOUT_S#ray_stop_requested_status_timeout_s9RAY_AUTOSCALER_RECONCILE_TRANSIENT_STATUS_WARN_INTERVAL_SZ    transient_status_warn_interval_s:RAY_AUTOSCALER_RECONCILE_MAX_NUM_RETRY_REQUEST_TO_ALLOCATEr"   !max_num_retry_request_to_allocateN)r'   r(   r)   r   rD   int__annotations__rF   rH   rJ   rL   rO   rQ   r2   r3   r4   rB   rB   R   s<         %0K;W% %c    &1[<c& &s    )4?) ) #    )4?) ) #   
 0;{F0 0'   
 -8KCR- -$c    .9[Da. .%s     r3   rB   c                       e Zd ZU dZeed<   eed<   eed<   dZee	         ed<    e
e          Zeee	f         ed<    e
e          Zeeef         ed	<   d
Zeed<   d ZdS )NodeTypeConfigz
    NodeTypeConfig is the helper class to provide node type specific configs.
    This maps to subset of the `available_node_types` field in the
    autoscaling config.
    namemin_worker_nodesmax_worker_nodesNidle_timeout_s)default_factory	resourceslabels launch_config_hashc                 D    | j         | j        k    sJ | j         dk    sJ d S )Nr   )rW   rX   r:   s    r4   __post_init__zNodeTypeConfig.__post_init__   s3    $(=====$))))))r3   )r'   r(   r)   r?   r   rS   rR   rY   r   floatr   dictr[   r
   strr\   r^   r`   r2   r3   r4   rU   rU   t   s           NNN&*NHUO***"'%"="="=ItCJ==="U4888FDcN888 !   * * * * *r3   rU   c                      e Zd ZdZ	 d(deeef         deddfdZdeeef         deddfdZ	d)d	Z
d
edeeef         fdZd
edeeef         fdZdee         fdZdee         fdZdee         fdZd
edee         fdZd
edee         fdZd
ededee         fdZd
edeeef         fdZd
edeeef         fdZd*defdZd
edefdZdeeef         fdZdefdZdee         fdZdee         fdZ deeef         fdZ!defdZ"defdZ#defdZ$dee         fd Z%defd!Z&de'fd"Z(deeef         fd#Z)defd$Z*e+de,fd%            Z-e+defd&            Z.e+defd'            Z/dS )+r8   aK  
    AutoscalingConfig is the helper class to provide autoscaling
    related configs.

    # TODO(rickyx):
        1. Move the config validation logic here.
        2. Deprecate the ray-schema.json for validation because it's
        static thus not possible to validate the config with interdependency
        of each other.
    Fconfigsskip_content_hashr7   Nc                 @    d| _         |                     ||           dS )z
        Args:
            configs : The raw configs dict.
            skip_content_hash :
                Whether to skip file mounts/ray command hash calculation.
        FN)_sync_continuouslyupdate_configsr;   re   rf   s      r4   __init__zAutoscalingConfig.__init__   s)     #(G%677777r3   c                     t          |          | _        t          | j                   |rd S |                                  | j                            dd          | _        d S )N"generate_file_mounts_contents_hashT)r   _configsr   _calculate_hashesgetrh   rj   s      r4   ri   z AutoscalingConfig.update_configs   se    &w//&&& 	F   "&-"3"30$#
 #
r3   c           	      x   t                               d           t          | j                            di           | j                            dg           | j                            dg           | j                            dg           g| j                            dd                    \  | _        | _        d S )	Nz4Calculating hashes for file mounts and ray commands.file_mountscluster_synced_filesworker_setup_commandsworker_start_ray_commandsrm   T)rm   )loggerinfor   rn   rp   _runtime_hash_file_mounts_contents_hashr:   s    r4   ro   z#AutoscalingConfig._calculate_hashes   s    JKKK>OMmR00M4b99!!"92>>!!"=rBB 04}/@/@4d0 0
?
 
?
 
?
;D;;;r3   ray_node_typec                 V    t          j        |                     |d          pi           S )Nnode_configcopydeepcopyget_node_type_specific_configr;   rz   s     r4   get_cloud_node_configz'AutoscalingConfig.get_cloud_node_config   s/    }..}mLLRPR
 
 	
r3   c                    t          j        | j                            di                     }| j        d         |                             di           }|                    |           | j                            d          |k    rJd|v rEt
                              d                    |d         |d                              |d         |d<   nId|v rEt
                              d                    |d         |d                              |d         |d<   |                    dd	           |                    dd	           |S )
a7  
        Return the docker config for the specified node type.
            If it's a head node, the image will be chosen in the following order:
                1. Node specific docker image.
                2. The 'docker' config's 'head_image' field.
                3. The 'docker' config's 'image' field.
            If it's a worker node, the image will be chosen in the following order:
                1. Node specific docker image.
                2. The 'docker' config's 'worker_image' field.
                3. The 'docker' config's 'image' field.
        dockeravailable_node_typeshead_node_type
head_imagez<Overwriting image={} by head_image({}) for head node docker.imageworker_imagez@Overwriting image={} by worker_image({}) for worker node docker.N)	r~   r   rn   rp   updaterv   rw   formatpop)r;   rz   docker_confignode_specific_docker_configs       r4   get_docker_configz#AutoscalingConfig.get_docker_config   s\    dm&7&7"&E&EFF&*m4J&K'

#h

 	$ 	8999=-..-??},,RYY%g.l0K   
 *7|)Dg&..V]]%g.n0M   
 *7~)Fg& 	,---.$///r3   c                 8    | j                             dg           S )Nru   rn   rp   r:   s    r4   get_worker_start_ray_commandsz/AutoscalingConfig.get_worker_start_ray_commands   s    }  !<bAAAr3   c                 8    | j                             dg           S )Nhead_setup_commandsr   r:   s    r4   get_head_setup_commandsz)AutoscalingConfig.get_head_setup_commands   s    }  !6;;;r3   c                 8    | j                             dg           S )Nhead_start_ray_commandsr   r:   s    r4   get_head_start_ray_commandsz-AutoscalingConfig.get_head_start_ray_commands   s    }  !:B???r3   c                     |                      |d          }|Ht                              d                    |                     | j                            dg           S |S )z
        Return the worker setup commands for the specified node type.

        If the node type specific worker setup commands are not specified,
        return the global worker setup commands.
        rt   Nz)Using global worker setup commands for {}r   rv   rw   r   rn   rp   )r;   rz   worker_setup_commands      r4   get_worker_setup_commandsz+AutoscalingConfig.get_worker_setup_commands   sr      $AA2 
  
  ' KK;BB=QQ   =$$%<bAAA##r3   c                     |                      |d          }|Ht                              d                    |                     | j                            dg           S |S )z
        Return the initialization commands for the specified node type.

        If the node type specific initialization commands are not specified,
        return the global initialization commands.
        initialization_commandsNz+Using global initialization commands for {}r   )r;   rz   initialization_commands      r4   get_initialization_commandsz-AutoscalingConfig.get_initialization_commands  sp     "&!C!C4"
 "
 ")KK=DD]SS   =$$%>CCC%%r3   config_namec                 p    | j         d                             |i           }|                    |d           S )Nr   r   )r;   rz   r   node_specific_configs       r4   r   z/AutoscalingConfig.get_node_type_specific_config!  s?      $}-CDHH2 
  
 $''T:::r3   c                 V    t          j        |                     |d          pi           S )Nr[   r}   r   s     r4   get_node_resourcesz$AutoscalingConfig.get_node_resources)  s.    }..}kJJPb
 
 	
r3   c                 V    t          j        |                     |d          pi           S )Nr\   r}   r   s     r4   get_node_labelsz!AutoscalingConfig.get_node_labels.  s.    }..}hGGM2
 
 	
r3   c                 8    | j                             ||          S Nr   )r;   r   defaults      r4   
get_configzAutoscalingConfig.get_config3  s    }  g666r3   c                    | j         }|                     |d          }|t          j        t          j        fv r|                    dd          S |t          j        k    r*|                    di                               dd          S |t          j        k    r|                    dd          S |t          j        t          j	        t          j
        fv rdS t          d|           )Nr|   InstanceTyper]   azure_arm_parametersvmSizemachineTypezUnknown provider )providerr   r   r,   r+   rp   r-   r.   r/   r0   r*   
ValueError)r;   rz   r   r|   s       r4   get_provider_instance_typez,AutoscalingConfig.get_provider_instance_type6  s    =88VVho666??>2666''??#92>>BB8RPPP%%??="555(*HNH<LMMM2;;;<<<r3   c                 >   | j                             di           }|sdS i }| j                             di           }|                                 }|sJ |                                D ]\  }}t	          |                    di           |          }|                    dd          }||k    r|dz  }t          ||                    dd          ||                    d	d          |                    d
i           |                    di           |          ||<   |S )z
        Returns the node type configs from the `available_node_types` field.

        Returns:
            Dict[NodeType, NodeTypeConfig]: The node type configs.
        r   Nauthr|   max_workersr   r    min_workersrY   r[   r\   )rV   rW   rX   rY   r[   r\   r^   )rn   rp   get_head_node_typeitemsr   rU   )	r;   r   node_type_configsauth_configr   	node_typer|   r^   max_workers_nodess	            r4   get_node_type_configsz'AutoscalingConfig.get_node_type_configsD  sE     $}001GLL# 	4m''330022~&:&@&@&B&B 	 	"I{!1r22K" " !,q A A**!Q&!+9!,!B!B!2*/?FF%//+r::"x44#5, , ,i(( ! r3   c                     | j                             di           }t          |          dk    r't          |                                          d         S | j                             d          S )z
        Returns the head node type.

        If there is only one node type, return the only node type as the head
        node type.
        If there are multiple node types, return the head node type specified
        in the config.
        r   r    r   r   )rn   rp   lenlistkeys)r;   r   s     r4   r   z$AutoscalingConfig.get_head_node_typee  sh      $}001GLL#$$)),113344Q77}  !1222r3   c                 .    |                      dd           S )Nr   r   r:   s    r4   get_max_num_worker_nodesz*AutoscalingConfig.get_max_num_worker_nodess  s    }d333r3   c                 <    |                                  }||dz   S d S )Nr    )r   )r;   max_num_workerss     r4   get_max_num_nodesz#AutoscalingConfig.get_max_num_nodesv  s)    7799&"Q&&tr3   c                     | j         S r   )rn   r:   s    r4   get_raw_config_mutablez(AutoscalingConfig.get_raw_config_mutable|  s
    }r3   c                 8    |                      dt                    S )Nupscaling_speed)r   r   r:   s    r4   get_upscaling_speedz%AutoscalingConfig.get_upscaling_speed  s    02IJJJr3   c                     t           S r   )r   r:   s    r4   get_max_concurrent_launchesz-AutoscalingConfig.get_max_concurrent_launches  s    11r3   c                 n    | j                             di           }|                    t          d          S )Nr   F)rn   rp   r   r;   provider_configs     r4   disable_node_updatersz'AutoscalingConfig.disable_node_updaters  s0    -++J;;""#<eDDDr3   c                 @    |                      dd          }||dz  ndS )z[
        Returns the idle timeout in seconds if present in config, otherwise None.
        idle_timeout_minutesN<   r   )r;   rY   s     r4   get_idle_timeout_sz$AutoscalingConfig.get_idle_timeout_s  s-     )?FF&4&@~""dJr3   c                 `    |                                  }|                    t          d          S )NT)get_provider_configrp   r   r   s     r4   disable_launch_config_checkz-AutoscalingConfig.disable_launch_config_check  s*    2244""#BDIIIr3   c                     t                      S r   )rB   r:   s    r4   get_instance_reconcile_configz/AutoscalingConfig.get_instance_reconcile_config  s     '(((r3   c                 8    | j                             di           S )Nr   r   r:   s    r4   r   z%AutoscalingConfig.get_provider_config  s    }  R000r3   c                 4    t          j        | j                  S r   )yaml	safe_dumprn   r:   s    r4   dumpzAutoscalingConfig.dump  s    ~dm,,,r3   c                 t   | j                             di                               dd          }|dk    rt          j        S |dk    rt          j        S |dk    rt          j        S |dk    rt          j        S |dk    rt          j        S |d	k    rt          j        S |d
k    rt          j	        S t          j
        S )Nr   typer]   localawsazuregcpaliyunkuberayreadonly)rn   rp   r   r0   r,   r-   r.   r+   r/   r1   r*   )r;   provider_strs     r4   r   zAutoscalingConfig.provider  s    }((R88<<VRHH7"">!U""<W$$>!U""<X%%?"Y&&##Z''%%##r3   c                 X    t          | d          s|                                  | j        S )Nrx   )hasattrro   rx   r:   s    r4   runtime_hashzAutoscalingConfig.runtime_hash  s/    t_-- 	%""$$$!!r3   c                 X    t          | d          s|                                  | j        S )Nry   )r   ro   ry   r:   s    r4   file_mounts_contents_hashz+AutoscalingConfig.file_mounts_contents_hash  s0    t9:: 	%""$$$..r3   )F)r7   Nr   )0r'   r(   r)   r?   r
   rc   r	   boolrk   ri   ro   r   r   r   r   r   r   r   r   r   r   r   ra   r   r   r   r   rU   r   r   rR   r   r   r   r   r   r   r   r   rB   r   r   r   propertyr   r   r   r   r2   r3   r4   r8   r8      s       	 	 #(8 8c3h8  8 
	8 8 8 8
d38n 
 
RV 
 
 
 

 
 
 

8 
S#X 
 
 
 

)x )DcN ) ) ) )VBtCy B B B B<c < < < <@T#Y @ @ @ @$x $DI $ $ $ $&& &d3i & & & &";%;47;	#; ; ; ;
 
T#u*=M 
 
 
 


X 
$sCx. 
 
 
 

7 7s 7 7 7 7= =S = = = =!tHn,D'E ! ! ! !B3H 3 3 3 34(3- 4 4 4 48C=    S#X    KU K K K K2S 2 2 2 2Et E E E EKHUO K K K KJT J J J J)/F ) ) ) )1T#s(^ 1 1 1 1-c - - - - $( $ $ $ X$& "c " " " X"
 /3 / / / X/ / /r3   r8   c                   F    e Zd ZdZddededdfdZdefdZdefd	Z	d
 Z
dS )FileConfigReaderz3A class that reads cluster config from a yaml file.Tconfig_filerf   r7   Nc                     t          |                                          | _        || _        |                                 | _        dS )z
        Args:
            config_file: The path to the config file.
            skip_content_hash:  Whether to skip file mounts/ray command
                hash calculation. Default to True.
        N)r   resolve_config_file_path_skip_content_hash_read_cached_config)r;   r   rf   s      r4   rk   zFileConfigReader.__init__  s>     "&k!2!2!:!:!<!<"3"jjllr3   c                     t          | j                  5 }t          j        |                                          }t          || j                  cd d d            S # 1 swxY w Y   d S )Nrf   )openr   r   	safe_loadreadr8   r   )r;   fconfigs      r4   r   zFileConfigReader._read  s    $()) 	XQ^AFFHH--F$Vt?VWWW	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	Xs   <AA"%A"c                     | j         S zQ
        Returns:
            AutoscalingConfig: The autoscaling config.
        r   r:   s    r4   r<   z.FileConfigReader.get_cached_autoscaling_config  s     ""r3   c                 8    |                                  | _        d S r   )r   r   r:   s    r4   r>   z2FileConfigReader.refresh_cached_autoscaling_config  s    "jjllr3   )T)r'   r(   r)   r?   rc   r   rk   r8   r   r<   r>   r2   r3   r4   r   r     s        ==	+ 	+C 	+D 	+D 	+ 	+ 	+ 	+X( X X X X
#/@ # # # #+ + + + +r3   r   c                   <    e Zd ZdZdefdZdefdZdefdZd Z	dS )	KubeRayConfigReaderz;A class that reads cluster config from a K8s RayCluster CR.config_producerc                 F    || _         |                                 | _        d S r   )_config_producer_generate_configs_from_k8sr   )r;   r	  s     r4   rk   zKubeRayConfigReader.__init__  s$     /"==??r3   r7   c                 D    t          |                                           S r   )r8   r  r:   s    r4   r  z.KubeRayConfigReader._generate_configs_from_k8s  s     !6!6!8!8999r3   c                     | j         S r  r  r:   s    r4   r<   z1KubeRayConfigReader.get_cached_autoscaling_config  s    
 ""r3   c                 8    |                                  | _        dS )z
        Reads the configs from the K8s RayCluster CR.

        This reads from the K8s API server every time to pick up changes.
        N)r  r   r:   s    r4   r>   z5KubeRayConfigReader.refresh_cached_autoscaling_config  s     #==??r3   N)
r'   r(   r)   r?   r   rk   r8   r  r<   r>   r2   r3   r4   r  r    s        EE@(A @ @ @ @:,= : : : :#/@ # # # #@ @ @ @ @r3   r  c                   6    e Zd ZdZdefdZdefdZdefdZdS )ReadOnlyProviderConfigReaderzA class that reads cluster config for a read-only provider.

    This is used for laptop mode / manual cluster setup modes, in order to
    provide status reporting in the same way for users.gcs_addressc                 H    t           | _        t          |          | _        d S )N)address)r   rn   r   _gcs_client)r;   r  s     r4   rk   z%ReadOnlyProviderConfigReader.__init__   s"    ,$[999r3   r7   c                 B   t          | j                  }i }d }|j        D ]}|j        }|s!t	          t          |j                            }t          |          r|}||vr.t          |j	                  dt          |          rdndi d||<   ot          |          s||         dxx         dz  cc<   |rM| j
        d                             |           t          |          | j
        d<   |s
J d            || j
        d<   | j
                            dd            d S )	Nr   r    )r[   r   r   r|   r   r   zHead node type should be found.r   r   )r   r  node_statesray_node_type_namer   r   node_idr   rb   total_resourcesrn   r   r   r   )r;   ray_cluster_resource_stater   r   
node_stater   s         r4   r>   z>ReadOnlyProviderConfigReader.refresh_cached_autoscaling_config  sl   %?@P%Q%Q"  "4@ 	D 	DJ"5I Y5mJDV6W6WXX	J'' +!* 444!%j&@!A!A#$(4Z(@(@#G11a#%	3 3$Y// "*-- D$Y/>>>!C>>> 	=M01889MNNN+./C+D+DDM-(!DD#DDD>.<DM*+ 	0$77777r3   c                 .    t          | j        d          S )NTr   )r8   rn   r:   s    r4   r<   z:ReadOnlyProviderConfigReader.get_cached_autoscaling_config'  s     $GGGGr3   N)	r'   r(   r)   r?   rc   rk   r8   r>   r<   r2   r3   r4   r  r    sy        ; ;
:C : : : :!83D !8 !8 !8 !8FH/@ H H H H H Hr3   r  )8r~   loggingabcr   r   dataclassesr   r   enumr   pathlibr   typingr	   r
   r   r   r   ray._common.utilsr   ray._private.ray_constantsr   ray._rayletr   !ray.autoscaler._private.constantsr   r   r   r   2ray.autoscaler._private.kuberay.autoscaling_configr   ray.autoscaler._private.monitorr   ray.autoscaler._private.utilr   r   r   r   r   ray.autoscaler.v2.schemar   ray.autoscaler.v2.sdkr   ray.autoscaler.v2.utilsr   	getLoggerr'   rv   r   r6   rB   rU   r8   r   r  r  r2   r3   r4   <module>r/     sX     # # # # # # # # ( ( ( ( ( ( ( (             , , , , , , , , , , , ,  + + + + + + 2 2 2 2 2 2 ! ! ! ! ! !            Y X X X X X @ @ @ @ @ @              . - - - - - < < < < < < 0 0 0 0 0 0		8	$	$    t            C      F $       B * * * * * * * *:l/ l/ l/ l/ l/ l/ l/ l/^	+ + + + +} + + +>@ @ @ @ @- @ @ @4.H .H .H .H .H= .H .H .H .H .Hr3   