o
    $i>A                     @   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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 d d	lm Z m!Z! e"e#Z$G d
d dZ%e#dkre 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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	j.d( e'j(d)d!e)e	j/e	j0d e'j(d*d!e)ej1d+2ej1d e'j(d,de*d-d e'j(d.de*d/d e'j(d0de)dd1d e'j(d2de)dd3d e'j(d4de)dd5d e'j(d6d7d8d9 e'j(d:d7d;d9 e'j(d<d!e)dd=d e'j(d>d!e)d?d@d e'j(dAd!e)d?dBd e'3 Z4znej5dCkrne4j6nd Z7ej5dCkrye4j8ndDZ9ee4j:e4j;e4j<e4j=e7e9dEZ$e>e4j?e4j@e7e9 e ZAe%e4jBe4jCe4jDe4jEe4jFe4jGe4jHe4j<e4jIe4jJe4jKe4jLe4jMe4jNe4jOdFZPdGdH ZQej5dCkreARejSeQ eATePU  W dS  eVy   e$WdI eXdD Y dS w dS )J    N)build_addressis_localhost)call_with_retry)get_or_create_event_loop)logging_utils)create_check_raylet_task)AGENT_GRPC_MAX_MESSAGE_LENGTH)setup_component_logger)	GcsClientNodeIDc                   @   sr   e Zd Zdddejdf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object_store_nameraylet_namelog_dirtemp_dirsession_dirsession_namec                   s   |dusJ | _ | _|dusJ | _| _| _| _| _| _| _| _	| _
|	 _| _| _tjd  _|
 _| _d _d _t j jd _t fdddddd	}|j _ jsj   dS dS )
z%Initialize the DashboardAgent object.NRAY_NODE_ID)address
cluster_idc                      s    j  t j S N)
gcs_clientget_all_node_infor   from_hexnode_id selfr   P/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/dashboard/agent.py<lambda>R   s    z)DashboardAgent.__init__.<locals>.<lambda>zget self node info      )descriptionmax_attemptsmax_backoff_s)ipminimalgcs_addresscluster_id_hexr   r   r   	grpc_portmetrics_export_portnode_manager_portevents_export_addrlisten_portr   r   osenvironr   metrics_collection_disabledr   serverhttp_serverr
   r   r   is_head_nodeis_head_init_non_minimal)r   node_ip_addressr*   r(   r)   r'   r+   r,   r-   r.   r   r   r   r   r   r   r   	node_infor   r   r   __init__   sF   
zDashboardAgent.__init__c                 C   sd  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| _z|| jt| j| j t| js|| jd| j  W n ty   td d | _d | _Y nw 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:zVFailed to add port to grpc server. Agent will stay alive but disable the grpc service.z Dashboard agent grpc address: %s) grpcr:   0ray._private.authentication.authentication_utilsr;   Bray._private.authentication.grpc_authentication_server_interceptorr<   ray._private.tls_utilsr=   ray.dashboard.http_server_agentr>   sysversion_infomajorminorwarningscatch_warningssimplefilterDeprecationWarninginit_grpc_aioappendr2   r   r   r&   r*   r   	Exceptionlogger	exceptioninfor.   r3   )r   aiogrpcr;   r<   r=   r>   rN   rC   r   r   r   r6   \   sX   




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DashboardAgentModulerU   rW   __name__rS   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.)r3   http_sessionr   r   r   r   rc      s
   zDashboardAgent.http_sessionreturnc                 C   s   | j S r   )r   r   r   r   r   get_node_id   s   zDashboardAgent.get_node_idc              
      s   j r j  I d H    }d} jr>z j|I d H  W n ty= } ztd| d d}W Y d }~nd }~ww |r jsEdn jj} j sNd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.F)	namespacec                    s   g | ]}|  jqS r   )runr2   ).0mr   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. )rU   rW   r&   r   )msgr   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)r2   startrb   r3   rT   rU   rV   	http_portr*   r   async_internal_kv_putdashboard_consts#DASHBOARD_AGENT_ADDR_NODE_ID_PREFIXr   encodejsondumpsr&   ray_constantsKV_NAMESPACE_DASHBOARDDASHBOARD_AGENT_ADDR_IP_PREFIXrp   gatherrJ   platformr   r   looprS   wait_for_terminationcleanup)r   r^   launch_http_serverert   r*   put_by_node_id	put_by_iptasksro   check_parent_taskrr   r   r   r   rh      s\   


zDashboardAgent.run)r\   
__module____qualname__r{   #DEFAULT_DASHBOARD_AGENT_LISTEN_PORTboolstrr9   r6   rb   propertyrc   re   rh   r   r   r   r   r      s6    
CG
r   __main__zDashboard agent.)r#   z--node-ip-addressTzthe IP address of this node.)requiredtypehelpz--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   r    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--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.rl   r"   )logging_levellogging_formatr   filename	max_bytesbackup_count)
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...)rU   warningr/   _exitsignalSIGTERMr   r   r   r   sigterm_handler  s   
r   z6Agent is working abnormally. It will exit immediately.)Yargparserp   ry   r   r/   r   rJ   ray._private.ray_constants_privater{   ray.dashboard.consts	dashboardconstsrv   ray.dashboard.utilsutilsrY   ray._common.network_utilsr   r   ray._common.retryr   ray._common.utilsr   ray._privater   ray._private.process_watcherr   r   ray._private.ray_loggingr	   ray._rayletr
   r   	getLoggerr\   rU   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argsr   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_filepathr   r7   r*   r(   r)   r'   r   r   r+   r,   r.   r   r   r   r   agentr   add_signal_handlerr   run_until_completerh   rT   rV   exitr   r   r   r   <module>   s   
 
p
	
	
 