o
    1 iD                     @   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Zd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZmZmZmZmZmZ d dlZd dlZd dlZd dlm  m  mZ d dlm  m  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l0m1Z1 d dl2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZAmBZB d dlCmDZD eEeFZGe)ddZHdd ZIG dd dejJZKdd ZLdeMfddZNd ejOdeeeMef  fd!d"ZPd4d#eMd$eQfd%d&ZRd#eMd$eQfd'd(ZSd5d*d+ZTd4d,d-ZUd.eeM dee1 fd/d0ZVd1d2 ZWeFd3krDeW  dS dS )6    N)defaultdict)futures)AnyCallableDictListOptionalSetUnion)cloudpickle)build_addressis_localhost)ray_constants)disable_client_hook)env_integer)setup_logger)%canonicalize_bootstrap_address_or_die)add_port_to_grpc_server)	GcsClient	JobConfig)CLIENT_SERVER_MAX_THREADSGRPC_OPTIONSOBJECT_TRANSFER_CHUNK_SIZEClientServerHandleResponseCache)DataServicer)LogstreamServicer)serve_proxier)dumps_from_serverloads_from_client)current_serverTIMEOUT_FOR_SPECIFIC_SERVER_S   c                    s   t   fdd}|S )z
    Decorator for gRPC stubs. Before calling the real stubs, checks if there's
    an existing entry in the caches. If there is, then return the cached
    entry. Otherwise, call the real function and use the real cache
    c              
      s  t |  d}t fdd|D r| ||S  d } d }t d }| j| }|||}|d urMt|trK|t	j
j |t| ||S z| ||}	W n" tyw }
 z||||
 |t	j
j |t|
  d }
~
ww ||||	 |	S )N)	client_id	thread_idreq_idc                 3   s    | ]}| vV  qd S N ).0imetadatar(   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/util/client/server/server.py	<genexpr>=   s    z7_use_response_cache.<locals>.wrapper.<locals>.<genexpr>r$   r%   r&   )dictinvocation_metadataanyintresponse_cachescheck_cache
isinstance	Exceptionset_codegrpc
StatusCodeZFAILED_PRECONDITIONset_detailsstrZupdate_cache)selfrequestcontextZexpected_idsr$   r%   r&   Zresponse_cacheZcached_entryrespefuncr+   r-   wrapper9   s4   

z$_use_response_cache.<locals>.wrapper)	functoolswraps)rB   rC   r(   rA   r-   _use_response_cache2   s   $rF   c                   @   s  e Zd ZdefddZ	dLdejdejfddZe	dLdej
fd	d
ZdLdejfddZe	dLdejfddZdLdejfddZdLdejfddZ	dLdejfddZdLdejfddZdLdefddZdededefddZdd Zdd  Zd!d" Zd#d$ Z e	dLd%d&Z!	dLdej"ded'e#d(e$j%de&ej' f
d)d*Z(dej"fd+d,Z)dej"defd-d.Z*	dLdej+dej,fd/d0Z-	dLd1e.ee/f d2eded3efd4d5Z0dLdej1fd6d7Z2	dLd8ej3d9e4e5 d:e6ee5f dej7fd;d<Z8	dLd8ej3d9e4e5 d:e6ee5f dej7fd=d>Z9	dLd8ej3d9e4e5 d:e6ee5f dej7fd?d@Z:	dLd8ej3d9e4e5 d:e6ee5f dej7fdAdBZ;	dLd8ej3dej7fdCdDZ<dededEe&e6 de=j>j?fdFdGZ@dededEe&e6 fdHdIZAdJdK ZBdS )MRayletServicerray_connect_handlerc                 C   sV   t t| _t t| _i | _i | _t t| _i | _t | _	t
 | _|| _t t| _dS )zuConstruct a raylet service

        Args:
           ray_connect_handler: Function to connect to ray cluster
        N)r   r/   object_refsclient_side_ref_mapfunction_refs
actor_refssetactor_ownersregistered_actor_classesnamed_actors	threadingLock
state_lockrH   r   r3   )r<   rH   r(   r(   r-   __init__b   s   



zRayletServicer.__init__Nr=   returnc           
      C   s|  |j rt|j }d|_nd }d }t S t r$tjjj	}|j
 }n<t|jp*d}z| j|fi | W n( ty_ } ztd tjdd| dW  Y d }~W  d    S d }~ww W d    n1 sjw   Y  |d u rytjddS | }|rt|jjj}||jjj t|jjj}	|	|jjj ||	krt|dkrtjdd	|jj d
|jj dS tjddS )NTz{}zRunning Ray Init failed:Fz0Call to `ray.init()` on the server failed with: )okmsgrV   r   z.Runtime environment doesn't match request one z current one )
job_configpickleloadsZ_client_jobr   rayis_initialized_privateworkerZglobal_workerZcore_workerZget_job_configjsonray_init_kwargsrH   r6   logger	exceptionray_client_pb2InitResponseZ_get_proto_job_configrM   Zruntime_env_infourisZworking_dir_uriupdateZpy_modules_urislen)
r<   r=   r>   rY   Zcurrent_job_configr_   extra_kwargsr@   Zjob_urisZcurrent_job_urisr(   r(   r-   Initu   sZ   

zRayletServicer.Initc              
   C   s   z$t   tjjj|j|j|j|jd}W d    n1 sw   Y  W n t	y= } zt
|| d}W Y d }~nd }~ww tj|dS )N)	overwrite	namespaceF)already_exists)r   r\   experimentalinternal_kv_internal_kv_putkeyvaluerk   rl   r6   return_exception_in_contextrd   KVPutResponse)r<   r=   r>   rm   r@   r(   r(   r-   KVPut   s    
zRayletServicer.KVPutc              
   C      z t   tjjj|j|jd}W d    n1 sw   Y  W n ty9 } zt|| d}W Y d }~nd }~ww t	j
|dS )Nrl       )rr   )r   r\   rn   ro   Z_internal_kv_getrq   rl   r6   rs   rd   KVGetResponse)r<   r=   r>   rr   r@   r(   r(   r-   KVGet      
zRayletServicer.KVGetc              
   C   s   z"t   tjjj|j|j|jd}W d    n1 sw   Y  W n ty; } zt	|| d}W Y d }~nd }~ww t
j|dS )N)del_by_prefixrl   r   )deleted_num)r   r\   rn   ro   Z_internal_kv_delrq   r|   rl   r6   rs   rd   KVDelResponse)r<   r=   r>   r}   r@   r(   r(   r-   KVDel   s   
zRayletServicer.KVDelc              
   C   s   z t   tjjj|j|jd}W d    n1 sw   Y  W n ty9 } zt|| g }W Y d }~nd }~ww t	j
|dS )Nrw   )keys)r   r\   rn   ro   Z_internal_kv_listprefixrl   r6   rs   rd   KVListResponse)r<   r=   r>   r   r@   r(   r(   r-   KVList   r{   zRayletServicer.KVListc              
   C   rv   )Nrw   F)exists)r   r\   rn   ro   Z_internal_kv_existsrq   rl   r6   rs   rd   KVExistsResponse)r<   r=   r>   r   r@   r(   r(   r-   KVExists   r{   zRayletServicer.KVExistsc                 C   sH   t   tjj|jd}W d    n1 sw   Y  tjt|dS )N)all_namespaces)Zactors_json)	r   r\   utilZlist_named_actorsr   rd   ClientListNamedActorsResponser`   dumps)r<   r=   r>   Zactorsr(   r(   r-   ListNamedActors   s   zRayletServicer.ListNamedActorsc                 C   s  t  }|j|_|jt jjkr<t  t }W d    n1 s!w   Y  dd | D }|j	
t jj|d |S |jt jjkrpt  t }W d    n1 sUw   Y  dd | D }|j	
t jj|d |S |jt jjkrt j }t 1 t }tjj| |_tjj| |_|j|_|j|_|j|_| |_W d    n1 sw   Y  |j
| |S t  | |||_ W d    |S 1 sw   Y  |S )Nc                 S      i | ]	\}}|t |qS r(   floatr)   kvr(   r(   r-   
<dictcomp>       z.RayletServicer.ClusterInfo.<locals>.<dictcomp>)tablec                 S   r   r(   r   r   r(   r(   r-   r     r   )!rd   ClusterInfoResponsetypeClusterInfoTypeZCLUSTER_RESOURCESr   r\   Zcluster_resourcesitemsZresource_tableZCopyFromZResourceTableZAVAILABLE_RESOURCESZavailable_resourcesZRUNTIME_CONTEXTZRuntimeContextZget_runtime_context_commonutilsZhex_to_binaryZ
get_job_idZjob_idZget_node_idZnode_idrl   Z-should_capture_child_tasks_in_placement_groupZcapture_client_tasksZgcs_addressZget_runtime_env_stringZruntime_envZruntime_context_return_debug_cluster_infor`   )r<   r=   r>   r?   	resourcesZfloat_resourcesctxZrtcr(   r(   r-   ClusterInfo   sP   




zRayletServicer.ClusterInfoc                 C   s   d}|j tjjkrt }n6|j tjjkrt }n*|j tjjkr&t	 }n|j tjj
kr0i }n|j tjjkr@dtjj i}ntdt|S )z9Handle ClusterInfo requests that only return a json blob.NZdashboard_urlzUnsupported cluster info type)r   rd   r   ZNODESr\   nodesZIS_INITIALIZEDr]   ZTIMELINEZtimelineZPINGZDASHBOARD_URLr^   r_   Zget_dashboard_url	TypeErrorr`   r   )r<   r=   r>   datar(   r(   r-   r     s   



z)RayletServicer._return_debug_cluster_infor$   idc                 C   s   | j o || jv r,|| j| v r,td|  d|  | j| |= 	 W d    dS || jv ri|| j| v ritd|  d|  | j| | | |r`td|   | j|= 	 W d    dS 	 W d    dS 1 suw   Y  d S )NzReleasing object z for TzReleasing actor Deleting reference to actor F)	rS   rI   rb   debughexrN   remove_can_remove_actor_refrL   )r<   r$   r   r(   r(   r-   release)  s$   


$zRayletServicer.releasec                 C   sF   | j  | | | | W d    n1 sw   Y  t  d S r'   )rS   _release_objects_release_actorsgcZcollect)r<   r$   r(   r(   r-   release_all<  s
   
zRayletServicer.release_allc                    s,   t  fdd| j D  }|o | jvS )Nc                 3   s    | ]} |v V  qd S r'   r(   )r)   Z
actor_listactor_id_bytesr(   r-   r.   G  s    
z7RayletServicer._can_remove_actor_ref.<locals>.<genexpr>)r1   rN   valuesrP   )r<   r   Zno_ownerr(   r   r-   r   F  s   
z$RayletServicer._can_remove_actor_refc                 C   sr   || j vrtd|  d S t| j | }| j |= || jv r#| j|= || jv r,| j|= td| d|  d S )Nz%Releasing client with no references: Released all z objects for client )rI   rb   r   rh   rJ   r3   )r<   r$   countr(   r(   r-   r   L  s   


zRayletServicer._release_objectsc                 C   s   || j vrtd|  d S d}| j |}|D ]}|d7 }| |r2td|   | j|= qtd| d|  d S )Nz!Releasing client with no actors: r      r   r   z actors for client: )rN   rb   r   popr   r   rL   )r<   r$   r   Zactors_to_removeZid_bytesr(   r(   r-   r   X  s   

zRayletServicer._release_actorsc              
   C   s0  | ddkrJz+| j|j |jj }t  tj||jj|jj	d W d    n1 s,w   Y  W n_ t
yI } zt|| W Y d }~nMd }~ww | ddkrz%| j|jj }t  tj||jjd W d    n1 spw   Y  W n t
y } zt|| W Y d }~n	d }~ww tdtjddS )	NZterminate_typetask_object)force	recursiveactor)
no_restartzEClient requested termination without providing a valid terminate_typeTrX   )Z
WhichOneofrI   r$   r   r   r   r\   cancelr   r   r6   rs   rL   r   killr   RuntimeErrorrd   ZTerminateResponse)r<   reqr>   Z
object_refr@   Z	actor_refr(   r(   r-   	Terminateg  s<   zRayletServicer.Terminater&   result_queuec           
   
      s  t jdkrtd jd }j  |d}|s0tjdttd| d  dS z4t	
d	|  t  d
tddf fdd}|| 	 W d   W dS 1 s]w   Y  W dS  ty }	 ztjdt|	dW  Y d}	~	S d}	~	ww )zAttempts to schedule a callback to push the GetResponse to the
        main loop when the desired object is ready. If there is some failure
        in scheduling, a GetResponse will be immediately returned.
        r   z3Async get() must have exactly 1 Object ID. Actual: r   NFzClientObjectRef with id z not found for client validerrorzasync get: %sresultrU   c              
      s   zLt |  }t|}|dksJ dt|t }tj|D ])}|t }t||d t }tj	d||| |||d}tj
|d}| q W d	S  tyu }	 ztj	dt|	d}tj
|d}
|
 W Y d	}	~	d	S d	}	~	ww )
zPushes GetResponses to the main DataPath loop to send
                    to the client. This is called when the object is ready
                    on the server side.r   &Serialized object cannot be zero bytesr   Tr   r   chunk_idtotal_chunks
total_size)getr&   Fr   N)r   rh   mathceilr   rangestart_chunk_idminrd   GetResponseZDataResponseputr6   r   r   )r   
serializedr   r   r   startendZget_respZ
chunk_respexcr?   r$   r&   r=   r   r<   r(   r-   send_get_response  s@   

z;RayletServicer._async_get_object.<locals>.send_get_response)rh   ids
ValueErrorrI   r   rd   r   r   r   rb   r   r   r   Z_on_completedr6   )
r<   r=   r$   r&   r   r>   ridrefr   r@   r(   r   r-   _async_get_object  s8   
	
"(' z RayletServicer._async_get_objectc                 c   sT    t | }|d}|d u rtjdttddV  d S | ||E d H  d S )Nr$   Fz.client_id is not specified in request metadatar   )	r/   r0   r   rd   r   r   r   r   _get_object)r<   r=   r>   r,   r$   r(   r(   r-   	GetObject  s   
zRayletServicer.GetObjectc              
   c   sd   g }|j D ](}| j| |d }|r|| qtjdttd| d| dV   d S z$t	
d|  t  tj||jd}W d    n1 sMw   Y  W n tyq } ztjdt|dV  W Y d }~d S d }~ww t||| }t|}	|	dksJ dt|	t }
t|j|
D ]}|t }t|	|d	 t }tjd
||| ||
|	dV  qd S )NFzClientObjectRef z is not found for client r   zget: %s)timeoutr   r   r   Tr   )r   rI   r   appendrd   r   r   r   r   rb   r   r   r\   r   r6   r   rh   r   r   r   r   r   r   )r<   r=   r$   Z
objectrefsr   r   r   r@   r   r   r   r   r   r   r(   r(   r-   r     sV   
	

zRayletServicer._get_objectc                 C   s   |  |j|jd|j|S )z#gRPC entrypoint for unary PutObject )_put_objectr   client_ref_idowner_id)r<   r=   r>   r(   r(   r-   	PutObject  s   zRayletServicer.PutObjectr   r   r   c           
   
   C   s   z+t || }|r| j| }nd}t  tj||d}W d   n1 s%w   Y  W n" tyM }	 ztd tj	ddt
|	dW  Y d}	~	S d}	~	ww || j| | < t|dkrf| | j| |< td|  tj	| d	d
S )a  Put an object in the cluster with ray.put() via gRPC.

        Args:
            data: Pickled data. Can either be bytearray if this is called
              from the dataservicer, or bytes if called from PutObject.
            client_ref_id: The id associated with this object on the client.
            client_id: The client who owns this data, for tracking when to
              delete this reference.
            owner_id: The owner id of the object.
            context: gRPC context.
        N)_ownerzPut failed:rx   F)r   r   r   r   zput: %sT)r   r   )r    rL   r   r\   r   r6   rb   rc   rd   PutResponser   r   rI   binaryrh   rJ   r   )
r<   r   r   r$   r   r>   objownerZ	objectrefr@   r(   r(   r-   r     s*   

zRayletServicer._put_objectc              
   C   s$  g }|j D ]}|| j|j vrtdt| || j|j |  q|j}|j}z%t  t	j
|||dkr8|nd d\}}W d    n1 sHw   Y  W n  tyn }	 ztd|	  tjddW  Y d }	~	S d }	~	ww tdt|t|f  dd	 |D }
d
d	 |D }tjd|
|dS )Nz4Asking for a ref not associated with this client: %s)num_returnsr   z
Exception F)r   zwait: %s %sc                 S      g | ]}|  qS r(   r   )r)   Zready_object_refr(   r(   r-   
<listcomp>F  s    z-RayletServicer.WaitObject.<locals>.<listcomp>c                 S   r   r(   r   )r)   Zremaining_object_refr(   r(   r-   r   I  s    T)r   ready_object_idsremaining_object_ids)Z
object_idsrI   r$   r6   r;   r   r   r   r   r\   waitrb   r   rd   WaitResponser   )r<   r=   r>   rI   r   r   r   Zready_object_refsZremaining_object_refsr@   r   r   r(   r(   r-   
WaitObject.  sJ   

zRayletServicer.WaitObjecttaskarglistkwargsc              
   C   s4  t d|jtjj|jf  zft X |jtjj	kr%| 
||||}n:|jtjjkr5| ||||}n*|jtjjkrE| ||||}n|jtjjkrS| ||}ntdtjj|j d|_|W  d    W S 1 sow   Y  W d S  ty } zt jddd tjdt|dW  Y d }~S d }~ww )Nzschedule: %s %sz$Unimplemented Schedule task type: %sTzCaught schedule exception)exc_infoFr   )rb   r   namerd   
ClientTaskZRemoteExecTypeNamer   r   ZFUNCTION_schedule_functionZACTOR_schedule_actorZMETHOD_schedule_methodZNAMED_ACTOR_schedule_named_actorNotImplementedErrorr   r6   ClientTaskTicketr   r   )r<   r   r   r   r>   r   r@   r(   r(   r-   ScheduleS  s<   (
zRayletServicer.Schedulec           
      C   sv   | j |j}|d u rtdt||j}t|j}|d ur&|jdi |}|j|i |}| 	||j
}	tj|	dS )Nz7Can't run an actor the server doesn't have a handle forZ
return_idsr(   )rL   r   
payload_idr6   getattrr   decode_optionsoptionsremoteunify_and_track_outputsr$   rd   r   )
r<   r   r   r   r>   Zactor_handlemethodoptsoutputr   r(   r(   r-   r   u  s   
zRayletServicer._schedule_methodc                 C   s   |  |j|jt|j}t|j}|d ur|jdi |}t|  |j|i |}W d    n1 s4w   Y  || j|j	
 < | j|j |j	
  tj|j	
 gdS Nr  r(   )lookup_or_register_actorr  r$   r  baseline_optionsr  r!   r  rL   	_actor_idr   rN   addrd   r   )r<   r   r   r   r>   Zremote_classr	  r   r(   r(   r-   r     s   

zRayletServicer._schedule_actorc           	      C   s   |  |j|jt|j}t|j}|d ur|jdi |}t|  |j|i |}W d    n1 s4w   Y  | ||j}t	j
|dS r  )lookup_or_register_funcr  r$   r  r  r  r!   r  r  rd   r   )	r<   r   r   r   r>   Zremote_funcr	  r
  r   r(   r(   r-   r     s   

z!RayletServicer._schedule_functionc                 C   sv   t |jdks	J t|j|jpd }|j }|| jvr"|| j|< | j	|j
 | | j| tj|j gdS )Nr   r  )rh   r  r\   Z	get_actorr   rl   r  r   rL   rN   r$   r  rP   rd   r   )r<   r   r>   r   Zbin_actor_idr(   r(   r-   r     s   


z$RayletServicer._schedule_named_actorr  c                 C   s   t  B || jvr>| j| | }t|}t|std|d u s(t|dkr1t	|| j|< ntj	di ||| j|< W d    n1 sHw   Y  | j| S )Nz6Attempting to register function that isn't a function.r   r(   )
r   rK   rI   r\   r   inspect
isfunctionr6   rh   r  )r<   r   r$   r  ZfuncrefrB   r(   r(   r-   r    s   



z&RayletServicer.lookup_or_register_funcc                 C   s   t  A || jvr=| j| | }t|}t|std|d u s(t|dkr.t	|}n
tj	di ||}|| j|< W d    n1 sGw   Y  | j| S )Nz0Attempting to schedule actor that isn't a class.r   r(   )
r   rO   rI   r\   r   r  isclassr6   rh   r  )r<   r   r$   r  Zactor_class_refZactor_classZ	reg_classr(   r(   r-   r    s   




z'RayletServicer.lookup_or_register_actorc                 C   sp   |d u rg }nt |tr|}n|g}|D ]}| | j| v r'td|  || j| | < qdd |D S )NzAlready saw object_ref c                 S   r   r(   r   )r)   outr(   r(   r-   r     s    z:RayletServicer.unify_and_track_outputs.<locals>.<listcomp>)r5   listr   rI   rb   warning)r<   r
  r$   Zoutputsr  r(   r(   r-   r    s   
z&RayletServicer.unify_and_track_outputsr'   )C__name__
__module____qualname__r   rT   rd   ZInitRequestre   rj   rF   rt   ru   ry   rz   r~   r   r   r   r   r   r   r   r   r   r;   r   bytesboolr   r   r   r   r   r   Z
GetRequestr2   queueQueuer   r   r   r   r   Z
PutRequestr   r   r
   	bytearrayr   r   r   r   r   r   r   r   r   r   r   r   r   r\   Zremote_functionZRemoteFunctionr  r  r  r(   r(   r(   r-   rG   a   s    
4

'
 
H(


(*

'








rG   c                 C   s,   |d ur| t|  |tjj d S d S r'   )r:   encode_exceptionr7   r8   r9   ZABORTED)errr>   r(   r(   r-   rs     s   rs   rU   c                 C   s   t | }t| S r'   )r   r   base64standard_b64encodedecode)rc   r   r(   r(   r-   r    s   
r  r  c                 C   s(   | j sd S t| j }t|tsJ |S r'   )Zpickled_optionsrZ   r[   r5   r/   )r  r	  r(   r(   r-   r    s
   r  hostportc           	      C   s   	 ddt dtttf fdd}|p|}tjtjtddt	d}t
|}t|}t }t|| t|| t|| t| sHt|d|  t||  d	|  t||||d
}|  |S )NrY   ra   c                 [   sV   t   t stjdd| i|W  d    S W d    d S 1 s$w   Y  d S )NrY   r(   )r   r\   r]   initrY   ra   r(   r(   r-   default_connect_handler  s   "z&serve.<locals>.default_connect_handlerray_client_server)max_workersthread_name_prefix)r  z
127.0.0.1::)task_servicerdata_servicerlogs_servicerZgrpc_serverr'   )r   r   r;   r   r8   serverr   ThreadPoolExecutorr   r   rG   r   r   ray_client_pb2_grpcZ"add_RayletDriverServicer_to_serverZ(add_RayletDataStreamerServicer_to_serverZ'add_RayletLogStreamerServicer_to_serverr   r   r   r   )	r$  r%  rH   r(  r0  r-  r.  r/  Zcurrent_handler(   r(   r-   serve   s>   

r3  c                    s^   t   tj i W d    n1 sw   Y  d fdd	}t| ||d}|fS )Nc                    s    t  rS t j d| iS )NrY   )r\   r]   r&  r'  argsinfor   r(   r-   rH   (  s   z+init_and_serve.<locals>.ray_connect_handler)rH   r'   )r   r\   r&  r3  )r$  r%  r5  r   rH   Zserver_handler(   r4  r-   init_and_serve#  s   r7  Fc                 C   s@   |  d t  t| W d    d S 1 sw   Y  d S )Nr   )stopr   r\   shutdown)r0  Z_exiting_interpreterr(   r(   r-   shutdown_with_server4  s   
"r:  c                    s   ddt f fdd}|S )NrY   c                    sX    r rt jd | d| d S t jd | d| d S t jdd| i| d S )N)addressZ_redis_usernameZ_redis_passwordrY   )r;  rY   rY   r(   )r\   r&  r'  r;  redis_passwordredis_usernamer(   r-   rH   ;  s   
z/create_ray_handler.<locals>.ray_connect_handlerr'   r   )r;  r=  r>  rH   r(   r<  r-   create_ray_handler:  s   r?  r;  c                 C   s   t | } t| dS )zr
    Try to create a gcs client based on the command line args or by
    autodetecting a running Ray cluster.
    r;  )r   r   r@  r(   r(   r-   try_create_gcs_clientM  s   
rA  c               
   C   sl  dd l } |  }|jdtddd |jddtdd	d |jd
tg ddd |jddtdd |jddtdd |jddtdd |jddtd dd | \}}ttjtj	 t
|j|j|j}t|j|j}t|}|jrv||jd}td| d|  |jdkrt|j|j|j|j|j|jd}nt|j|j|}zt}	 dt i}	z"tjj st|j}
tjj|
 tjjj dt!"|	tj#d  W n$ t$y } zt%d!|j d"|j  t&| W Y d }~nd }~ww t'd# |jd$kr%|j(j)dkrt}n|d#8 }|dkrt* |d% dkr%|tkr%t| d& q t*y5   |+d Y d S w )'Nr   z--hostz0.0.0.0zHost IP to bind to)r   defaulthelpz-pz--porti'  zPort to bind toz--mode)proxylegacyspecific-serverrD  )r   choicesrB  z	--addressFz Address to use to connect to Ray)requiredr   rC  z--redis-usernamez username for connecting to Redisz--redis-passwordz Password for connecting to Redisz--runtime-env-agent-addressz8The port to use for connecting to the runtime_env_agent.)rH  r   rB  rC  z****zStarting Ray Client server on z, args )r>  r=  runtime_env_agent_addressTtimer)  rw   [z ] Failed to put health check on r   rF     z idle checks before shutdown.),argparseArgumentParseradd_argumentr;   r2   parse_known_argsr   r   ZLOGGER_LEVELZLOGGER_FORMATr?  r;  r=  r>  r   r$  r%  replacerb   r6  moder   rI  r3  r"   rJ  r\   rn   ro   Z_internal_kv_initializedrA  Z_initialize_internal_kvrp   r`   r   ZKV_NAMESPACE_HEALTHCHECKr6   r   rc   sleepr.  Znum_clientsKeyboardInterruptr8  )rM  parserr5  _rH   hostportargs_strr0  Zidle_checks_remainingZhealth_reportZ
gcs_clientr@   r(   r(   r-   mainV  s   
	




"rY  __main__r'   )F)Xr!  rD   r   r  r`   loggingr   rZ   r  rQ   rJ  collectionsr   
concurrentr   typingr   r   r   r   r   r	   r
   r8   r\   Zray._private.stateZ!ray.core.generated.ray_client_pb2core	generatedrd   Z&ray.core.generated.ray_client_pb2_grpcr2  r   Zray._common.network_utilsr   r   Zray._privater   Zray._private.client_mode_hookr   Zray._private.ray_constantsr   Zray._private.ray_loggingr   Zray._private.servicesr   Zray._private.tls_utilsr   Zray._rayletr   Zray.job_configr   Zray.util.client.commonr   r   r   r   r   Z#ray.util.client.server.dataservicerr   Z"ray.util.client.server.logservicerr   Zray.util.client.server.proxierr   Z%ray.util.client.server.server_picklerr   r    Z#ray.util.client.server.server_stubsr!   	getLoggerr  rb   r"   rF   ZRayletDriverServicerrG   rs   r;   r  ZTaskOptionsr  r2   r3  r7  r:  r?  rA  rY  r(   r(   r(   r-   <module>   sp    $

/     	 	#

	
d
