
    &`i+              	          d Z ddlZddlZddlZddlZddlZddlmZ ddlZddl	m
c m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 dd	lmZ dd
l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(m)Z)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z1 ddl2m3Z3 	 ddl4Z4n# e5$ r dZ4Y nw xY w ej6        e7          Z8 G d d          Z9deddfdZ:e7dk    r ej;        d          Z<e<=                    dde>d            e<=                    d!de>d"            e<=                    d#de>ej?        ej@        ejA        $           e<=                    d%de>ejB        ejC        &           e<=                    d'de>ejD        d(ejD         d)&           e<=                    d*d+e>d,            e<=                    d-deEed.e d/&           e<=                    d0deEed1e d2&           e<=                    d3de>dd4&           e<=                    d5de>d6d7&           e<=                    d8de>d6d9&           e<F                                ZGejH        d:k    reGjI        ndZJejH        d:k    reGjK        nd;ZL eeGjM        eGjN        eGjO        eGjP        eJeL<            ejQ        eGjR        eGjS        eJeL           e8T                    d=ejU                    e8T                    d>ejV                    e8T                    d?ejW                    e8T                    d@ejX                    eGjY        ZYeY eZdA          eGj[        s!e8T                    dB            e*eY          Z\n,ej]        ^                    eGj[                  Z[ e(e[d+C          Z\ e9eYe\eGjO        eGj_        D          Z`e`a                                 dS dS )EzAutoscaler monitoring loop daemon.

See autoscaler._private/monitor.py for the legacy implementation. All the legacy flags
are supported here, but the new implementation uses the new autoscaler v2.
    N)Optional)build_addressparse_address)LOGGING_ROTATE_BACKUP_COUNTLOGGING_ROTATE_BYTES)record_extra_usage_tag)logging_utils)get_event_logger)setup_component_logger)SCRIPT_MODE)	GcsClient)AUTOSCALER_METRIC_PORTAUTOSCALER_UPDATE_INTERVAL_S)AutoscalerPrometheusMetrics)
Autoscaler)AutoscalerEventLogger)FileConfigReaderIConfigReaderReadOnlyProviderConfigReader)AutoscalerMetricsReporter)AutoscalingState)Event)TagKeyc            
           e Zd ZdZ	 	 ddededee         dee         fdZede	d	ee         fd
            Z
ede	defd            Zd Zd ZdS )AutoscalerMonitora  Autoscaling monitor.

    This process periodically collects stats from the GCS and triggers
    autoscaler updates.

    TODO:
    We should also handle autoscaler failures properly in the future.
    Right now, we don't restart autoscaler if it fails (internal reconciliation
    however, should not fail the autoscaler process).
    With the Reconciler able to handle extra cloud instances, we could in fact
    recover the autoscaler process from reconciliation.
    Naddressconfig_readerlog_dir
monitor_ipc                    t          t          |                     || _        t          j        j        j        }t          | j                  | _        |rDt          |t                    }| j        
                    d|                                dd            |                     | j                  | _        t                              d| j                    |                    t"                     t%          | j                  d         }d | _        |rL	 t)          t*          j        j        |          }t1          |          | _        n# t4          $ r
 d | _        Y nw xY wd | _        t7          | j                  }	t9          |	          | _        |rt<          r	 t                              d                    t                               |dk    rd	dini }
t=          j         dt          |	j!        d
|
 nK# t4          $ r t          "                    d           Y n%w xY wt<          st          #                    d           tI          | j        || j        | j        | j                  | _        d S )N)r   s   AutoscalerMetricsAddressTzsession_name: r   )session_namez-Starting autoscaler metrics server on port {}z	127.0.0.1addr)portregistryz8An exception occurred while starting the metrics server.z?`prometheus_client` not found, so metrics will not be exported.)r!   r   
gcs_clientevent_loggermetrics_reporter )%record_autoscaler_v2_usager   gcs_addressray_privateworkerglobal_workerr%   r   r   internal_kv_putencode_get_session_name_session_nameloggerinfoset_moder   r   
autoscalerr
   RayEvent
SourceType
AUTOSCALERr   r&   	Exceptionr   r   metric_reporterprometheus_clientformatstart_http_serverr$   	exceptionwarningr   )selfr   r   r   r   r-   monitor_addrhead_node_ipray_event_loggerprom_metricskwargss              m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/autoscaler/v2/monitor.py__init__zAutoscalerMonitor.__init__C   s    	#9W#5#5666"$2#D,<=== 	(5KLLLO+++\-@-@-B-BD$   "33DODD9T%799:::$$$$T%566q9 		%)#3'2G$ $  %::J$K$K!! ) ) )$(!!!) !%D2@RSSS8FF 	+ 	CJJ.   
 3?+2M2M&+..SU!3 /)2     
      N     # 	NNQ   %+'*!1
 
 
s%   3E E! E!AG; ;$H"!H"r%   returnc                 v    |                      dt          j        d          }|r|                                }|S )zObtain the session name from the GCS.

        If the GCS doesn't respond, session name is considered None.
        In this case, the metrics reported from the monitor won't have
        the correct session name.
        s   session_name
   )timeout)internal_kv_getray_constantsKV_NAMESPACE_SESSIONdecode)r%   r!   s     rG   r1   z#AutoscalerMonitor._get_session_name   sK     "11. 2 
 
  	1'..00L    autoscaling_statec                     	 |                      |                                           dS # t          $ r t                              d           Y dS w xY w)z(Report the autoscaling state to the GCS.z)Error reporting autoscaling state to GCS.N)report_autoscaling_stateSerializeToStringr:   r3   r?   )r%   rR   s     rG   _report_autoscaling_statez+AutoscalerMonitor._report_autoscaling_state   sl    
	J//0A0S0S0U0UVVVVV 	J 	J 	JHIIIIII	Js   '+ $AAc                     	 | j                                         }|r|                     | j        |           nt                              d           t          j        t                     k)zRun the monitor loop.TzNo autoscaling state to report.)	r6   update_autoscaling_staterV   r%   r3   r@   timesleepr   )rA   rR   s     rG   _runzAutoscalerMonitor._run   sk    
	5 $ H H J J  B..t@QRRRR@AAA J3444
	5rQ   c                     	 |                                   d S # t          $ r t                              d            w xY w)NzError in monitor loop)r[   r:   r3   r?   )rA   s    rG   runzAutoscalerMonitor.run   sK    	IIKKKKK 	 	 	4555	s    &>)NN)__name__
__module____qualname____doc__strr   r   rH   staticmethodr   r1   r   rV   r[   r]   r(   rQ   rG   r   r   5   s         " "&$(G
 G
G
 %G
 #	G

 SMG
 G
 G
 G
R i HSM    \$ JJ2BJ J J \J5 5 5    rQ   r   r%   rI   c                     	 t          t          j        d|            dS # t          $ r t                              d           Y dS w xY w)z)
    Record usage for autoscaler v2.
    v2z(Error recording usage for autoscaler v2.N)r   r   AUTOSCALER_VERSIONr:   r3   r?   )r%   s    rG   r)   r)      s`    Ev8$
KKKKK E E ECDDDDDDEs    $AA__main__z/Parse GCS server for the monitor to connect to.)descriptionz--gcs-addressFzThe address (ip:port) of GCS.)requiredtypehelpz--autoscaling-configz'the path to the autoscaling config filez--logging-level)ri   rj   defaultchoicesrk   z--logging-format)ri   rj   rl   rk   z--logging-filenamezFSpecify the name of log file, log to stdout if set empty, default is ""z
--logs-dirTzBSpecify the path of the temporary directory used by Ray processes.z--logging-rotate-bytesz8Specify the max bytes for rotating log file, default is z bytes.z--logging-rotate-backup-countz9Specify the backup count of rotated log file, default is .z--monitor-ipz:The IP address of the machine hosting the monitor process.z--stdout-filepath z$The filepath to dump monitor stdout.z--stderr-filepathz$The filepath to dump monitor stderr.win32   )logging_levellogging_formatr   filename	max_bytesbackup_countz7Starting autoscaler v2 monitor using ray installation: zRay version: zRay commit: z(AutoscalerMonitor started with command: z--gcs-address must be set!z<No autoscaling config provided: use read only node provider.)config_fileskip_content_hash)r   r   )bra   argparseloggingossysrY   typingr   r+   ray._private.ray_constantsr,   rN   ray._common.network_utilsr   r   ray._common.ray_constantsr   r   ray._common.usage.usage_libr   ray._privater	   ray._private.event.event_loggerr
   ray._private.ray_loggingr   ray._private.workerr   ray._rayletr   !ray.autoscaler._private.constantsr   r   $ray.autoscaler._private.prom_metricsr   ray.autoscaler.v2.autoscalerr   ray.autoscaler.v2.event_loggerr   )ray.autoscaler.v2.instance_manager.configr   r   r   "ray.autoscaler.v2.metrics_reporterr   !ray.core.generated.autoscaler_pb2r   ray.core.generated.event_pb2r   r7   ray.core.generated.usage_pb2r   r<   ImportError	getLoggerr^   r3   r   r)   ArgumentParserparseradd_argumentrb   LOGGER_LEVELLOGGER_LEVEL_CHOICESLOGGER_LEVEL_HELPLOGGER_FORMATLOGGER_FORMAT_HELPMONITOR_LOG_FILE_NAMEint
parse_argsargsplatformlogging_rotate_byteslogging_rotation_byteslogging_rotate_backup_countlogging_rotation_backup_countrs   rt   logs_dirlogging_filename redirect_stdout_stderr_if_neededstdout_filepathstderr_filepathr4   __file____version__
__commit__argvr*   
ValueErrorautoscaling_configr   path
expanduserr   monitorr]   r(   rQ   rG   <module>r      s      				 



        



 2 2 2 2 2 2 2 2 2 B B B B B B B B        ? > > > > > & & & & & & < < < < < < ; ; ; ; ; ; + + + + + + ! ! ! ! ! !        M L L L L L 3 3 3 3 3 3 @ @ @ @ @ @         
 I H H H H H > > > > > > : : : : : : / / / / / /    
	8	$	$H H H H H H H HVE9 E E E E E z$X$F  F %c8W     6	     *2,     +-     33/3 3 3     Q	      $)) ) )     '+*&* * *     I     3     3     D ;>,':Q:QT66WX,/LG,C,C(( " (*&(2    3M2%	   KKP#,PP   KK111222
KK/s~//000
KKE38EEFFF"Kj5666" 
RSSS44[AAW//0GHH((*d
 
 
  ?	  G KKMMMMMM s   B   B*)B*