o
    1 icS                     @   s~  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 d dl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 d d	lmZmZmZ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/ d dl0m1Z1m2Z2mZ3mZ4 d dl5m6Z6 d dl7m8Z8m9Z9m:Z:m;Z; e<e!Z=de
de
fddZ>G dd dZ?dS )    N)Sequencewraps)CallableDictListOptionalTupleUnion)ActorHandle)StatusOverview)BuiltApplication)DeploymentIDDeploymentStatusDeploymentStatusInfoRequestRoutingInfo)(CLIENT_CHECK_CREATION_POLLING_INTERVAL_SCLIENT_POLLING_INTERVAL_SMAX_CACHED_HANDLESSERVE_DEFAULT_APP_NAMESERVE_LOGGER_NAME)ServeController)get_deploy_args)DeploymentInfo)ASGIAppReplicaWrapper)get_random_string)HTTPOptionsRayServeException)DeploymentArgsDeploymentRouter   r   )DeploymentHandle)ApplicationStatusLoggingConfigServeApplicationSchemaServeDeploySchemafreturnc                    s   t   fdd}|S )Nc                    s$   | j rtd | g|R i |S )Nz"Client has already been shut down.)	_shutdownr   )selfargskwargsr&    e/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/serve/_private/client.pycheck4   s   z _ensure_connected.<locals>.checkr   )r&   r/   r-   r,   r.   _ensure_connected3   s   r0   c                   @   s  e Zd ZdefddZedd Zedd Zdd	 Zd
d Z	dd Z
dHdeddfddZdHdeddfddZdIdedefddZ	dJdededefddZ	dIdededefdd ZdIdedefd!d"Zed#d#d$d%ee d&ed'edee fd(d)Ze	*dKd+eeef d,eddfd-d.Zd%ee ddfd/d0ZedLd1ee d2efd3d4Z edLd2efd5d6Z!ededede"e#ef fd7d8Z$ee%fdede&fd9d:Z'edee( fd;d<Z)ede*fd=d>Z+ee%d#fdede,e d?edefd@dAZ-edBe.fdCdDZ/edEe0fdFdGZ1dS )MServeControllerClient
controllerc                 C   sD   || _ d| _t|j | _t|j | _t	 | _
t | _d S )NF)_controllerr(   raygetZget_http_configremote_http_configZget_root_url	_root_urldicthandle_cacheset_evicted_handle_keys)r)   r2   r-   r-   r.   __init__>   s   zServeControllerClient.__init__c                 C      | j S N)r8   r)   r-   r-   r.   root_urlK      zServeControllerClient.root_urlc                 C   r>   r?   )r7   r@   r-   r-   r.   http_configO   rB   z!ServeControllerClient.http_configc                 C   s   t d)Nz&Ray Serve client cannot be serialized.r   r@   r-   r-   r.   
__reduce__S   s   z ServeControllerClient.__reduce__c                 C   s*   t | jD ]}| j|   | j|= qdS )zShuts down all cached handles.

        Remove the reference to the cached handles so that they can be
        garbage collected.
        N)listr:   shutdown)r)   	cache_keyr-   r-   r.   shutdown_cached_handlesV   s   
z-ServeControllerClient.shutdown_cached_handlesc                    s6    fddt jfddt jD  I dH  dS )zShuts down all cached handles asynchronously.

        Remove the reference to the cached handles so that they can be
        garbage collected.
        c                    s"    j |   I d H   j | = d S r?   )r:   shutdown_async)rG   r@   r-   r.   shutdown_taskg   s   zJServeControllerClient.shutdown_cached_handles_async.<locals>.shutdown_taskc                    s   g | ]} |qS r-   r-   ).0rG   )rJ   r-   r.   
<listcomp>l   s    zGServeControllerClient.shutdown_cached_handles_async.<locals>.<listcomp>N)asynciogatherrE   r:   r@   r-   )r)   rJ   r.   shutdown_cached_handles_async`   s
   z3ServeControllerClient.shutdown_cached_handles_async      >@	timeout_sr'   Nc                 C   s|   |    t r:| js<ztj| jj |d W n tjj	y#   Y n t
y4   td| d Y nw d| _dS dS dS )Completely shut down the connected Serve instance.

        Shuts down all processes and deletes all state associated with the
        instance.
        timeout&Controller failed to shut down within *s. Check controller logs for more details.TN)rH   r4   is_initializedr(   r5   r3   graceful_shutdownr6   
exceptionsRayActorErrorTimeoutErrorloggerwarningr)   rQ   r-   r-   r.   rF   o   s   

zServeControllerClient.shutdownc                    s   |   I dH  t rA| jsCztj| jj |dI dH  W n tj	j
y*   Y n ty;   td| d Y nw d| _dS dS dS )rR   NrS   rU   rV   T)rO   r4   rW   r(   rM   wait_forr3   rX   r6   rY   rZ   r[   r\   r]   r^   r-   r-   r.   rI      s    

z$ServeControllerClient.shutdown_asyncnamec                 C   s   t   }t   | |k s|dk rmt| jj|}|du r&td| dtt	
|}|jtjkr6dS |jtjkrGtd| d|j |jtjksOJ td| d|j d	 t t t   | |k s|dk std| d
| d)zWaits for the named deployment to enter "HEALTHY" status.

        Raises RuntimeError if the deployment enters the "UNHEALTHY" status
        instead.

        Raises TimeoutError if this doesn't happen before timeout_s.
        r   NzWaiting for deployment z- to be HEALTHY, but deployment doesn't exist.Deployment z is UNHEALTHY: Waiting for z  to be healthy, current status: .z did not become HEALTHY after s.)timer4   r5   r3   get_deployment_statusr6   RuntimeErrorr   
from_protoDeploymentStatusInfoProto
FromStringstatusr   ZHEALTHYZ	UNHEALTHYmessageZUPDATINGr\   debugsleepr   r[   r)   ra   rQ   startstatus_bytesrl   r-   r-   r.   _wait_for_deployment_healthy   s6   

z2ServeControllerClient._wait_for_deployment_healthy<   app_namec                 C   s   t   }t   | |k r=t| jj|}|du rdS tt	|}t
d| d| d t t t   | |k std| d| d)zWaits for the named deployment to be shut down and deleted.

        Raises TimeoutError if this doesn't happen before timeout_s.
        Nrc   z  to be deleted, current status: rd   rb   z wasn't deleted after re   )rf   r4   r5   r3   rg   r6   r   ri   rj   rk   r\   rn   ro   r   r[   )r)   ra   ru   rQ   rq   Zcurr_status_bytescurr_statusr-   r-   r.   _wait_for_deployment_deleted   s    
z2ServeControllerClient._wait_for_deployment_deleteddeployment_namec                 C   s   t   }t   | |k s|dk r>t| jj||}|dur!dS td| d| d t t	 t   | |k s|dk st
d| d| d| d)	ag  Waits for the named deployment to be created.

        A deployment being created simply means that its been registered
        with the deployment state manager. The deployment state manager
        will then continue to reconcile the deployment towards its
        target state.

        Raises TimeoutError if this doesn't happen before timeout_s.
        r   NzWaiting for deployment 'z' in application 'z' to be created.zDeployment 'z' did not become HEALTHY after re   )rf   r4   r5   r3   rg   r6   r\   rn   ro   r   r[   )r)   rx   ru   rQ   rq   rr   r-   r-   r.   _wait_for_deployment_created   s&   
z2ServeControllerClient._wait_for_deployment_createdc                 C   s   t   }t   | |k s|dk rit| jj|}|du r&td| dtt	
|}|jjtjkr7dS |jjtjkrJtd| d|jj td| d|jj d	 t t t   | |k s|dk std
| d| d)zWaits for the named application to enter "RUNNING" status.

        Raises:
            RuntimeError: if the application enters the "DEPLOY_FAILED" status instead.
            TimeoutError: if this doesn't happen before timeout_s.
        r   NzWaiting for application z. to be RUNNING, but application doesn't exist.zDeploying application z	 failed: rc   z  to be RUNNING, current status: rd   zApplication z did not become RUNNING after re   )rf   r4   r5   r3   get_serve_statusr6   rh   r   ri   StatusOverviewProtork   
app_statusrl   r"   RUNNINGZDEPLOY_FAILEDrm   r\   rn   ro   r   r[   rp   r-   r-   r.   _wait_for_application_running   s4   

z3ServeControllerClient._wait_for_application_runningT)$wait_for_ingress_deployment_creationwait_for_applications_running
built_appsr   r   c             
   C   s~  i }|D ]f}g }|j D ]Y}|jd u r|jr|j|jd}|j|jk}t|j||j|j|jp/t	 |r4|j
nd d}	t }
|	d |
_|	d |
_|	d |
_|	d |
_|	d rX|	d |
_
|	d |
_||
  q|||j< q| | t| jj| g }|D ]>}|r| |j|j |r| |j |j
d urd	| j |j
 }nd
}td|j d| d || j|j|jdd q~|S )N)logging_config)ingressreplica_configdeployment_configversionroute_prefixrx   Zdeployment_config_proto_bytesZreplica_config_proto_bytesdeployer_job_idr   r   z at  zApplication 'z
' is readyrd   F)check_exists)deploymentsr   optionsra   Zingress_deployment_namer   Z_replica_configZ_deployment_config_versionr   r   r   rx   r   r   r   r   appendZSerializeToString_check_ingress_deploymentsr4   r5   r3   deploy_applicationsr6   ry   r~   r8   r\   info
get_handle)r)   r   r   r   Zname_to_deployment_args_listappZdeployment_args_list
deploymentZ
is_ingressZdeployment_argsZdeployment_args_protoZhandlesZurl_partr-   r-   r.   r   #  sj   

	




z)ServeControllerClient.deploy_applicationsFconfig	_blockingc                 C   s|   t | jj| |r<d}t }t | |k r4|  }|jjt	j
kr'dS tt t | |k std| ddS )a  Starts a task on the controller that deploys application(s) from a config.

        Args:
            config: A single-application config (ServeApplicationSchema) or a
                multi-application config (ServeDeploySchema)
            _blocking: Whether to block until the application is running.

        Raises:
            RayTaskError: If the deploy task on the controller fails. This can be
                because a single-app config was deployed after deploying a multi-app
                config, or vice versa.
        rt   z&Serve application isn't running after re   N)r4   r5   r3   Zapply_configr6   rf   rz   r|   rl   r"   r}   ro   r   r[   )r)   r   r   rQ   rq   rv   r-   r-   r.   deploy_appsp  s   

z!ServeControllerClient.deploy_appsc                 C   sX   |D ]'}d}|j D ]}t|jrt|jtr|d7 }q	|dkr)td|j dqdS )zCheck @serve.ingress of deployments across applications.

        Raises: RayServeException if more than one @serve.ingress
            is found among deployments in any single application.
        r      z3Found multiple FastAPI deployments in application "za".Please only include one deployment with @serve.ingress in your application to avoid this issue.N)r   inspectisclassZfunc_or_class
issubclassr   r   ra   )r)   r   r   Znum_ingress_deploymentsr   r-   r-   r.   r     s   
z0ServeControllerClient._check_ingress_deploymentsnamesblockingc                 C   s   |sd S t d|  | jj| |r[t }t | dk rTt| jj|}d}|D ]}t	
t|}|jjtjkrBd}q/|rGd S tt t | dk s!td| d S )NzDeleting app rt   TFz6Some of these applications weren't deleted after 60s: )r\   r   r3   delete_appsr6   rf   r4   r5   Zget_serve_statusesr   ri   r{   rk   r|   rl   r"   ZNOT_STARTEDro   r   r[   )r)   r   r   rq   Zcurr_statuses_bytesZall_deletedZcur_status_bytesZ
cur_statusr-   r-   r.   r     s4   
z!ServeControllerClient.delete_appsc                 C   sL   g }t | jj D ]}t|}t|}|	|j
 q| || dS )zDelete all applicationsN)r4   r5   r3   Zlist_serve_statusesr6   r{   rk   r   ri   r   ra   r   )r)   r   Zall_appsrr   protorl   r-   r-   r.   delete_all_apps  s   

z%ServeControllerClient.delete_all_appsc                 C   s>   t t| jj||}t|j	|j
dkr|j
fS d fS )Nr   )r    rk   r4   r5   r3   get_deployment_infor6   r   ri   Zdeployment_infoZroute)r)   ra   ru   Zdeployment_router-   r-   r.   r     s   
z)ServeControllerClient.get_deployment_infoc                 C   s$   t t| jj|}t|S r?   )	r{   rk   r4   r5   r3   rz   r6   r   ri   )r)   ra   r   r-   r-   r.   rz     s   
z&ServeControllerClient.get_serve_statusc                 C   s    t | jj }dd |D S )Nc                 S   s   g | ]
}t t|qS r-   )r   ri   rj   rk   )rK   rr   r-   r-   r.   rL     s    zEServeControllerClient.get_all_deployment_statuses.<locals>.<listcomp>)r4   r5   r3   get_all_deployment_statusesr6   )r)   Zstatuses_bytesr-   r-   r.   r     s   z1ServeControllerClient.get_all_deployment_statusesc                 C   s   t | jj S r?   )r4   r5   r3   Zget_serve_instance_detailsr6   r@   r-   r-   r.   get_serve_details  s   z'ServeControllerClient.get_serve_detailsr   c           	      C   s   t ||d}|||f}|| jv r| j| S |r+t| jj }||vr+t| dt||}|| j|< || j	v r?t
d t| jtkr\tt| j }| j	| | j| |S )aG  Construct a handle for the specified deployment.

        Args:
            deployment_name: Deployment name.
            app_name: Application name.
            check_exists: If False, then Serve won't check the deployment
                is registered. True by default.

        Returns:
            DeploymentHandle
        )ra   ru   z does not exist.a4  You just got a ServeHandle that was evicted from internal cache. This means you are getting too many ServeHandles in the same process, this will bring down Serve's performance. Please post a github issue at https://github.com/ray-project/ray/issues to let the Serve team to find workaround for your use case.)r   r:   r4   r5   r3   Zlist_deployment_idsr6   KeyErrorr!   r<   r\   r]   lenr   randomchoicerE   keysaddpop)	r)   rx   ru   r   Zdeployment_idrG   Zall_deploymentshandleZ	evict_keyr-   r-   r.   r     s&   





	z ServeControllerClient.get_handler   c                 C      | j j| dS )zRecord replica routing information for a replica.

        Args:
            info: RequestRoutingInfo including deployment name, replica tag,
                multiplex model ids, and routing stats.
        N)r3   record_request_routing_infor6   )r)   r   r-   r-   r.   r   "  s   z1ServeControllerClient.record_request_routing_infor   c                 C   r   )z<Reconfigure the logging config for the controller & proxies.N)r3   Z!reconfigure_global_logging_configr6   )r)   r   r-   r-   r.   update_global_logging_config,  s   z2ServeControllerClient.update_global_logging_config)rP   )r`   )rt   )F)T)2__name__
__module____qualname__r   r=   propertyrA   rC   rD   rH   rO   floatrF   rI   strintrs   rw   ry   r~   r0   r   r   boolr   r!   r   r
   r$   r%   r   r   r   r   r	   r   r   r   r   rz   r   r   r   r   r   r   r   r   r#   r   r-   r-   r-   r.   r1   =   s    



+

!%L
!
	
	1	r1   )@rM   r   loggingr   rf   collections.abcr   	functoolsr   typingr   r   r   r   r	   r
   r4   Z	ray.actorr   Z$ray.serve._private.application_stater   Zray.serve._private.build_appr   Zray.serve._private.commonr   r   r   r   Zray.serve._private.constantsr   r   r   r   r   Zray.serve._private.controllerr   Zray.serve._private.deploy_utilsr   Z"ray.serve._private.deployment_infor   Zray.serve._private.http_utilr   Zray.serve._private.utilsr   Zray.serve.configr   Zray.serve.exceptionsr   Zray.serve.generated.serve_pb2r   r    rj   r{   Zray.serve.handler!   Zray.serve.schemar"   r#   r$   r%   	getLoggerr\   r0   r1   r-   r-   r-   r.   <module>   s6     

