o
    `۷iE                     @   s  d dl Z d dlZd dl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	  m
Z
 d dlm  mZ d dlm  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m Z m!Z!m"Z"m#Z# e$e%Z&G d	d
 d
Z'e%dkre j(ddZ)e)j*dde+dd e)j*dde+dd e)j*dde+dd e)j*dde+dd e)j*dde,dd e)j*dde,dd e)j*dde,dd e)j*dde+ddd  e)j*d!d"e,e
j-d#d  e)j*d$de+dd%d  e)j*d&d"d'd( e
j.e
j/e
j0d) e)j*d*d"e+e
j1e
j2d  e)j*d+d"e+ej3d,4ej3d  e)j*d-de,d.d e)j*d/de,d0d e)j*d1de+dd2d  e)j*d3de+dd4d  e)j*d5de+dd6d  e)j*d7d8d9d: e)j*d;d8d<d: e)j*d=d8d>d: e)j*d?d"e+dd@d  e)j*dAd"e+dBdCd  e)j*dDd"e+dBdEd  e)5 Z6zwej7dFkre6j8nd Z9ej7dFkre6j:ndGZ;ee6j<e6j=e6j>e6j?e9e;dHZ&e@e6jAe6jBe9e; e ZCe'e6jDe6jEe6jFe6jGe6jHe6jIe6jJe6j>e6jKe6jLe6jMe6jNe6jOe6jPe6jQe6jRdIZSejTUe
jV dJdK ZWej7dFkreCXejYeW eCZeS[  W dS  e\y   e&]dL e^dG Y dS w dS )M    N)build_addressis_localhost)get_or_create_event_loop)logging_utils)create_check_raylet_task)AGENT_GRPC_MAX_MESSAGE_LENGTH)setup_component_logger) DASHBOARD_AGENT_LISTEN_PORT_NAMEMETRICS_AGENT_PORT_NAMEMETRICS_EXPORT_PORT_NAME	GcsClientpersist_portc                   @   sx   e Zd Zdddejddfdedededededed	ed
efddZdd Zdd Z	e
dd ZdefddZdd ZdS )DashboardAgentNFdisable_metrics_collectionis_headobject_store_nameraylet_namelog_dirtemp_dirsession_dirsession_namec                C   s   |dusJ || _ || _|dusJ || _|| _|| _|| _|| _|| _|| _|| _	|| _
|	| _|| _|| _tjd | _|
| _|| _d| _d| _t| j| jd| _|| _| js]|   dS t| j| jtd t| j| jtd dS )z%Initialize the DashboardAgent object.NRAY_NODE_ID)address
cluster_id)ipminimalgcs_addresscluster_id_hexr   r   r   	grpc_portmetrics_export_portnode_manager_portevents_export_addrlisten_portr   r   osenvironnode_idmetrics_collection_disabledr   serverhttp_serverr   
gcs_clientr   _init_non_minimalr   r
   r   )selfnode_ip_addressr   r   r   r   r    r!   r"   r#   r   r   r   r   r   r   r   r    r.   I/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/dashboard/agent.py__init__   sP   zDashboardAgent.__init__c                 C   sL  ddl m} ddlm} ddlm} ddlm} ddlm	} t
jjdkrNt
jjdkrNdd l}|  |jd	td
 |  W d    n1 sHw   Y  n|  g }| r]||  |j|ddtfdtffd| _|| jt| j| j| _t| js|| jd| j  t| j| jt| j tdt| j| j || j| j | _!d S )Nr   )aio)is_token_auth_enabled)$AsyncAuthenticationServerInterceptor)add_port_to_grpc_server)HttpServerAgent   
   ignore)category)zgrpc.so_reuseportr   zgrpc.max_send_message_lengthzgrpc.max_receive_message_length)interceptorsoptionsz
127.0.0.1:z Dashboard agent grpc address: %s)"grpcr1   0ray._private.authentication.authentication_utilsr2   Bray._private.authentication.grpc_authentication_server_interceptorr3   ray._private.tls_utilsr4   ray.dashboard.http_server_agentr5   sysversion_infomajorminorwarningscatch_warningssimplefilterDeprecationWarninginit_grpc_aioappendr(   r   r   r   r   r   r   r   r&   r
   loggerinfor#   r)   )r,   aiogrpcr2   r3   r4   r5   rE   r:   r.   r.   r/   r+   l   sV   



z DashboardAgent._init_non_minimalc                 C   sR   g }t t j}|D ]}tdt jj| || }|| q
tdt| |S )zLoad dashboard agent modules.zLoading %s: %szLoaded %d modules.)dashboard_utilsget_all_modulesDashboardAgentModulerK   rL   __name__rJ   len)r,   modulesagent_cls_listclscr.   r.   r/   _load_modules   s   
zDashboardAgent._load_modulesc                 C   s   | j sJ d| j jS )Nz=Accessing unsupported API (HttpServerAgent) in a minimal ray.)r)   http_sessionr,   r.   r.   r/   rX      s
   zDashboardAgent.http_sessionreturnc                 C   s   | j S N)r&   rY   r.   r.   r/   get_node_id   s   zDashboardAgent.get_node_idc              
      s   j r j  I d H    }d} jrCz j|I d H   jj _W n tyB } ztd| d d}W Y d }~nd }~ww t	 j
 jt jrQ|rQ jnd |r js[dn jj} j sddn j} jjtj  j  t j||g dtjd} jjtj  j  t j||g dtjd}t||I d H   fdd|D }tjd	vrʇ fd
d}	t j j|	t}
| |
  j r|  j !  n
dd }| |  tj| I d H   jr j" I d H  d S d S )NTz,Failed to start HTTP server with exception: zB. The agent will stay alive but the HTTP service will be disabled.Fr   )	namespacec                    s   g | ]}|  jqS r.   )runr(   ).0mrY   r.   r/   
<listcomp>  s    z&DashboardAgent.run.<locals>.<listcomp>)win32cygwinc                    s$   t d j d j d|   d S )NzTerminated Raylet: ip=z
, node_id=z. )rK   rL   r   r&   )msgrY   r.   r/   callback  s   z$DashboardAgent.run.<locals>.callbackc                      s   	 t dI d H  q)NTi  )asynciosleepr.   r.   r.   r/   wait_forever  s   z(DashboardAgent.run.<locals>.wait_forever)#r(   startrW   r)   	http_portr#   	ExceptionrK   	exceptionr   r   r&   r	   r   r*   async_internal_kv_putdashboard_consts#DASHBOARD_AGENT_ADDR_NODE_ID_PREFIXencodejsondumpsr   ray_constantsKV_NAMESPACE_DASHBOARDDASHBOARD_AGENT_ADDR_IP_PREFIXrf   gatherrA   platformr   r   looprJ   wait_for_terminationcleanup)r,   rS   launch_http_servererj   r   put_by_node_id	put_by_iptasksre   check_parent_taskrh   r.   rY   r/   r^      sj   


zDashboardAgent.run)rQ   
__module____qualname__rs   #DEFAULT_DASHBOARD_AGENT_LISTEN_PORTboolstrr0   r+   rW   propertyrX   r\   r^   r.   r.   r.   r/   r      s<    
MF
r   __main__zDashboard agent.)descriptionz	--node-idTzthe unique ID of this node.)requiredtypehelpz--node-ip-addresszthe IP address of this node.z--gcs-addresszThe address (ip:port) of GCS.z--cluster-id-hexzThe cluster id in hex.z--metrics-export-portz.The port to expose metrics through Prometheus.z--grpc-portz9The port on which the dashboard agent will receive GRPCs.z--node-manager-portz-The port to use for starting the node managerz--object-store-namez#The socket name of the plasma store)r   r   defaultr   z--listen-portFz!Port for HTTP server to listen onz--raylet-namez%The socket path of the raylet processz--logging-levelc                 C   s   t |  S r[   )logginggetLevelNameupper)sr.   r.   r/   <lambda>b  s    r   )r   r   r   choicesr   z--logging-formatz--logging-filenamezJSpecify the name of log file, log to stdout if set empty, default is "{}".z--logging-rotate-bytesz,Specify the max bytes for rotating log file.z--logging-rotate-backup-countz-Specify the backup count of rotated log file.z	--log-dirz"Specify the path of log directory.z
--temp-dirz?Specify the path of the temporary directory use by Ray process.z--session-dirz!Specify the path of this session.z	--minimal
store_truezMinimal agent only contains a subset of features that don't require additional dependencies installed when ray is installed by `pip install 'ray[default]'`.)actionr   z--disable-metrics-collectionzCIf this arg is set, metrics report won't be enabled from the agent.z--headz#Whether this node is the head node.z--session-namezThe current Ray session name.z--stdout-filepath z,The filepath to dump dashboard agent stdout.z--stderr-filepathz,The filepath to dump dashboard agent stderr.rb      )logging_levellogging_formatr   filename	max_bytesbackup_count)r   r   r   r    r!   r#   r   r   r   r   r   c                   C   s   t d ttj d S )Nz#Exiting with SIGTERM immediately...)rK   warningr$   _exitsignalSIGTERMr.   r.   r.   r/   sigterm_handler  s   
r   z6Agent is working abnormally. It will exit immediately.)_argparserf   rq   r   r$   r   rA   rayray._private.ray_constants_privaters   ray.dashboard.consts	dashboardconstsrn   ray.dashboard.utilsutilsrN   ray._common.network_utilsr   r   ray._common.utilsr   ray._privater   ray._private.process_watcherr   r   ray._private.ray_loggingr   ray._rayletr	   r
   r   r   r   	getLoggerrQ   rK   r   ArgumentParserparseradd_argumentr   intr   LOGGER_LEVELLOGGER_LEVEL_CHOICESLOGGER_LEVEL_HELPLOGGER_FORMATLOGGER_FORMAT_HELPDASHBOARD_AGENT_LOG_FILENAMEformat
parse_argsargsrw   logging_rotate_byteslogging_rotation_byteslogging_rotate_backup_countlogging_rotation_backup_countr   r   r   logging_filename redirect_stdout_stderr_if_neededstdout_filepathstderr_filepathrx   r-   r   r   r   r   r   r   r    r!   r#   r   r   r   headr   agent_rayletsetproctitle"AGENT_PROCESS_TYPE_DASHBOARD_AGENTr   add_signal_handlerr   run_until_completer^   rk   rl   exitr.   r.   r.   r/   <module>   s   
  

	
	
 