
    &`i"                     p   d dl Z d dlm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 d dlmZ d d	lmZ d d
lmZmZmZ d dlmZmZ d dlm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% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3  e j4        e5          Z6 G d d          Z7dS )    N)Queue)ListOptional)urlsplit)	GcsClient)_get_node_provider)AutoscalerEventLogger)KubeRayProvider)ReadOnlyProvider)AutoscalingConfigIConfigReaderProvider)InstanceManagerInstanceUpdatedSubscriber)InstanceStorage)ICloudInstanceProviderNodeProviderAdapter)RayInstaller)
Reconciler)InMemoryStorage)CloudInstanceUpdater)CloudResourceMonitor)
RayStopper)ThreadedRayInstaller)AutoscalerMetricsReporter)ResourceDemandScheduler)get_cluster_resource_state)AutoscalingStatec                       e Zd Z	 	 ddedededee         dee         ddfdZ	d	e
defd
Zdededed	e
fdZdee         fdZdS )
AutoscalerNsession_nameconfig_reader
gcs_clientevent_loggermetrics_reporterreturnc                    || _         |                                }t                              d|                                            || _        d| _        d| _        t                      | _	        t                      | _
        || _        || _        |                     ||           d| _        |                     ||| j        | j                   t!          | j                  | _        dS )a>  
        Args:
            session_name: The current Ray session name.
            config_reader: The config reader.
            gcs_client: The GCS client.
            event_logger: The event logger for emitting cluster events.
            metrics_reporter: The metrics reporter for emitting cluster metrics.
        zUsing Autoscaling Config: 
N)r!   configcloud_providerr#   )_config_readerget_cached_autoscaling_configloggerinfodump_gcs_client_cloud_instance_provider_instance_managerr   _ray_stop_errors_queue_ray_install_errors_queue_event_logger_metrics_reporter_init_cloud_instance_provider_cloud_resource_monitor_init_instance_managerr   
_scheduler)selfr!   r"   r#   r$   r%   r(   s          p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/v2/autoscaler.py__init__zAutoscaler.__init__3   s    " ,<<>>B6;;==BBCCC%(,%!%&+gg#).&)!1**6=AAA'+$##%8'	 	$ 	
 	
 	
 2$2DEE    r(   c                    |                                 }|d         dk    rA|                                |d<   t          |                    d          |          | _        dS |j        t          j        k    r&| j        j	        |d<   t          |          | _        dS t          ||                    d                    }t          ||          | _        dS )	z
        Initialize the cloud provider, and its dependencies (the v1 node provider)

        Args:
            config: The autoscaling config.
            config_reader: The config reader.

        typekuberayhead_node_typecluster_namegcs_address)provider_config)v1_providerr"   N)get_provider_configget_head_node_typer
   
get_configr0   providerr   	READ_ONLYr/   addressr   r   r   )r:   r(   r"   rD   node_provider_v1s        r;   r6   z(Autoscaler._init_cloud_instance_provider[   s     !44666"i//060I0I0K0KO,-,;!!.11- -D))) _ 222-1-=-EOM*,< /- - -D)))  2!!.11   
 -@,+- - -D)))r=   r)   c           
         t          |t                                }g }|                    t          |                     |                    t	          || j                             |                                st          |t                    rt          d| j
        j        z             j        }|
J d            |                    t          ||t          |j        |          | j        |                                pd	                     t%                      | _        |                    | j                   t)          ||
          | _        dS )zH
        Initialize the instance manager, and its dependencies.
        )
cluster_idstorage)r)   )r#   error_queuez//NzInvalid GCS address format)rI   r(   2   )head_node_ipinstance_storageray_installerrP   max_concurrent_installs)rS   "instance_status_update_subscribers)r   r   appendr   r   r2   disable_node_updaters
isinstancer   r   r/   rK   hostnamer   r   rE   r3   get_max_num_worker_nodesr   r7   r   r1   )r:   r!   r)   r#   r(   rS   subscribersrR   s           r;   r8   z!Autoscaler._init_instance_manager}   s    +##%%
 
 
 8:/~NNNOOO*$:UVVV	
 	
 	
 ++-- 	*/3
 3
 	 $D4+;+C$CDDML++-I+++$!-%5".!/!;%# # # !% > -3,K,K,M,M,SQS    " (<'='=$47888!0-/:"
 "
 "
r=   c                 @   	 g }| j                                         sE|                    | j                                                    | j                                         Eg }| j                                        sE|                    | j                                                   | j                                        Et          | j                  }| j                                         | j        	                                }t          j        | j        | j        | j        | j        || j                                        | j                                        |||| j                  S # t&          $ r%}t(                              |           Y d}~dS d}~ww xY w)a  Update the autoscaling state of the cluster by reconciling the current
        state of the cluster resources, the cloud providers as well as instance
        update subscribers with the desired state.

        Returns:
            AutoscalingState: The new autoscaling state of the cluster or None if
            the state is not updated.

        Raises:
            None: No exception.
        )instance_manager	schedulerr)   cloud_resource_monitorray_cluster_resource_statenon_terminated_cloud_instancescloud_provider_errorsray_install_errorsray_stop_errorsautoscaling_configr%   N)r2   emptyrW   getr3   r   r/   r*   !refresh_cached_autoscaling_configr+   r   	reconciler1   r9   r0   r7   get_non_terminatedpoll_errorsr5   	Exceptionr,   	exception)r:   re   rd   ra   rf   es         r;   update_autoscaling_statez#Autoscaler.update_autoscaling_state   s   !	 O17799 J&&t'B'F'F'H'HIII 17799 J "$4::<< P"))$*H*L*L*N*NOOO 4::<< P *DDDT)U)U& AACCC!%!4!R!R!T!T'!%!7/#<'+'C+E1DDFF&*&C&O&O&Q&Q#5 /#5!%!7     	 	 	Q44444	s   E+E. .
F8FF)NN)__name__
__module____qualname__strr   r   r   r	   r   r<   r   r6   r   r8   r   rp    r=   r;   r    r    2   s	        9=@D&F &F&F %&F 	&F
 45&F ##<=&F 
&F &F &F &FP ' 8E       D0
0
 /0
 	0

 "0
 0
 0
 0
d0	"	#0 0 0 0 0 0r=   r    )8loggingqueuer   typingr   r   urllib.parser   ray._rayletr   !ray.autoscaler._private.providersr   ray.autoscaler.v2.event_loggerr	   Iray.autoscaler.v2.instance_manager.cloud_providers.kuberay.cloud_providerr
   Kray.autoscaler.v2.instance_manager.cloud_providers.read_only.cloud_providerr   )ray.autoscaler.v2.instance_manager.configr   r   r   3ray.autoscaler.v2.instance_manager.instance_managerr   r   3ray.autoscaler.v2.instance_manager.instance_storager   0ray.autoscaler.v2.instance_manager.node_providerr   r   0ray.autoscaler.v2.instance_manager.ray_installerr   -ray.autoscaler.v2.instance_manager.reconcilerr   *ray.autoscaler.v2.instance_manager.storager   Eray.autoscaler.v2.instance_manager.subscribers.cloud_instance_updaterr   Eray.autoscaler.v2.instance_manager.subscribers.cloud_resource_monitorr   :ray.autoscaler.v2.instance_manager.subscribers.ray_stopperr   Eray.autoscaler.v2.instance_manager.subscribers.threaded_ray_installerr   "ray.autoscaler.v2.metrics_reporterr   ray.autoscaler.v2.schedulerr   ray.autoscaler.v2.sdkr   !ray.core.generated.autoscaler_pb2r   	getLoggerrq   r,   r    ru   r=   r;   <module>r      s          ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! @ @ @ @ @ @ @ @ @ @ @ @                   
        P O O O O O        J I I I I I D D D D D D F F F F F F           R Q Q Q Q Q      I H H H H H ? ? ? ? ? ? < < < < < < > > > > > >		8	$	$m m m m m m m m m mr=   