o
    bi3=                     @   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 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 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	j*d e!j"d(de#ej+d),ej+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"d0de#dd1d e!j"d2de#dd3d e!j"d4d5d6d7 e!j"d8d5d9d7 e!j"d:de#dd;d e!j"d<de#d=d>d e!j"d?de#d=d@d e!- Z.znej/dAkr^e.j0nd Z1ej/dAkrie.j2ndBZ3ee.j4e.j5e.j6e.j7e1e3dCZe8e.j9e.j:e1e3 e Z;ee.j<e.j=e.j>e.j?e.j@e.jAe.jBe.j6e.jCe.jDe.jEe.jFe.jGe.jHe.jIdDZJdEdF ZKej/dAkre;LejMeK e;NeJO  W dS  ePy   eQdG eRdB Y dS w dS )H    N)get_or_create_event_loop)logging_utils)create_check_raylet_task)AGENT_GRPC_MAX_MESSAGE_LENGTH)setup_component_logger)	GcsClientc                   @   sp   e Zd Z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
                C   s   |dusJ || _ || _|dusJ || _|| _|| _|| _|| _|| _|| _|| _	|| _
|
| _|| _tjd | _|	| _|| _d| _d| _t| j| jd| _| jsW|   dS dS )z%Initialize the DashboardAgent object.NRAY_NODE_ID)address
cluster_id)ipminimalgcs_addresscluster_id_hexr   r   r   dashboard_agent_portmetrics_export_portnode_manager_portlisten_portr
   r   osenvironnode_idmetrics_collection_disabledr   serverhttp_serverr   
gcs_client_init_non_minimal)selfnode_ip_addressr   r   r   r   r   r   r   r	   r
   r   r   r   r   r    r%   G/home/ubuntu/.local/lib/python3.10/site-packages/ray/dashboard/agent.py__init__   s6   zDashboardAgent.__init__c                 C   s&  ddl m} ddlm} ddlm} tjjdkrBtjj	dkrBdd l
}|  |jdtd |  W d    n1 s<w   Y  n|  |jd	d
tfdtffd| _| jdkr[dnd}z|| j| d| j | _W n ty   td d | _d | _Y n	w td|| j || j| j| _d S )Nr   )aio)add_port_to_grpc_server)HttpServerAgent   
   ignore)category)zgrpc.so_reuseportr   zgrpc.max_send_message_lengthzgrpc.max_receive_message_length)optionsz	127.0.0.1z0.0.0.0:zVFailed to add port to grpc server. Agent will stay alive but disable the grpc service.z#Dashboard agent grpc address: %s:%s)grpcr(   ray._private.tls_utilsr)   ray.dashboard.http_server_agentr*   sysversion_infomajorminorwarningscatch_warningssimplefilterDeprecationWarninginit_grpc_aior   r   r   r   	grpc_port	Exceptionlogger	exceptioninfor   r    )r#   aiogrpcr)   r*   r8   grpc_ipr%   r%   r&   r"   O   sF   



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DashboardAgentModuler?   rA   __name__append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&   rO      s
   zDashboardAgent.http_sessionreturnc                 C   s   | j S N)r   rP   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%   )runr   ).0mrP   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. )r?   rA   r   r   )msgrP   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   startrN   r    r>   r?   r@   	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_PREFIXr^   gatherr4   platformr   r   looprH   wait_for_terminationcleanup)r#   rJ   launch_http_servererb   r=   put_by_node_id	put_by_iptasksr]   check_parent_taskr`   r%   rP   r&   rV      s\   


zDashboardAgent.run)rG   
__module____qualname__ri   #DEFAULT_DASHBOARD_AGENT_LISTEN_PORTboolstrr'   r"   rN   propertyrO   rS   rV   r%   r%   r%   r&   r      s4    

89
r   __main__zDashboard agent.)descriptionz--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--dashboard-agent-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 rR   )logginggetLevelNameupper)sr%   r%   r&   <lambda>'  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 session name (cluster id) of this cluster.z--stdout-filepath z,The filepath to dump dashboard agent stdout.z--stderr-filepathz,The filepath to dump dashboard agent stderr.rZ      )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...)r?   warningr   _exitsignalSIGTERMr%   r%   r%   r&   sigterm_handler  s   
r   z6Agent is working abnormally. It will exit immediately.)Sargparser^   rg   r   r   r   r4   ray._private.ray_constants_privateri   ray.dashboard.consts	dashboardconstsrd   ray.dashboard.utilsutilsrD   ray._common.utilsr   ray._privater   ray._private.process_watcherr   r   ray._private.ray_loggingr   ray._rayletr   	getLoggerrG   r?   r   ArgumentParserparseradd_argumentr{   intry   LOGGER_LEVELLOGGER_LEVEL_CHOICESLOGGER_LEVEL_HELPLOGGER_FORMATLOGGER_FORMAT_HELPDASHBOARD_AGENT_LOG_FILENAMEformat
parse_argsargsrm   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_filepathrn   r$   r   r   r   r   r   r   r   r   r   r
   r   r	   r   agentr   add_signal_handlerr   run_until_completerV   r>   r@   exitr%   r%   r%   r&   <module>   s   
 
W
	
	
 