o
    1 i*                     @   s  U d 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 ddl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 ddlmZ ddlmZ ddlmZ ddl m!Z! e"eZ#da$de%d< da&ee%d< e!eG dd dZ'	dNde(de(dee fddZ)dd Z*dd Z+dede	d ed!e,d"e,f
d#d$Z-dOde(defd%d&Z.edd'G d(d) d)Z/ej0d*dd+Z1ej0d,dd+Z2d-d. Z3d/d0 Z4	1	1	1	1	1dPd2e5d3e5d4e5d5e5d6e5f
d7d8Z6d9d: Z7d;ee/ fd<d=Z8ee9Z:e
e5e
e5ej;f f e%d>< d?e5de
e5ej;f fd@dAZ<d?e5dBe5fdCdDZ=d?e5dBe5fdEdFZ>ee9Z?e
e5e
e5ef f e%dG< dHdI Z@dJdK ZAdLdM ZBdS )Qz
This file stores global state for a Serve application. Deployment replicas
can use this state to access metadata or the Serve controller.
    N)defaultdict)	dataclass)CallableDictListOptional)RayActorError)ServeControllerClient)	ReplicaID)DeploymentConfig)SERVE_CONTROLLER_NAMESERVE_LOGGER_NAMESERVE_NAMESPACE)ReplicaResult)RayServeException)RayServegRPCContext)DeveloperAPIReplicaContext_INTERNAL_REPLICA_CONTEXT_global_clientc                   @   sp   e Zd ZU dZeed< eed< eed< eed< eed< e	de
fdd	Ze	de
fd
dZe	de
fddZdS )r   a  Stores runtime context info for replicas.

    Fields:
        - app_name: name of the application the replica is a part of.
        - deployment: name of the deployment the replica is a part of.
        - replica_tag: unique ID for the replica.
        - servable_object: instance of the user class/function this replica is running.
        - rank: the rank of the replica.
        - world_size: the number of replicas in the deployment.
    
replica_idservable_object_deployment_configrank
world_sizereturnc                 C   
   | j jjS N)r   deployment_idapp_nameself r"   ]/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/serve/context.pyr   6      
zReplicaContext.app_namec                 C   r   r   )r   r   namer    r"   r"   r#   
deployment:   r$   zReplicaContext.deploymentc                 C   s   | j jS r   )r   	unique_idr    r"   r"   r#   replica_tag>      zReplicaContext.replica_tagN)__name__
__module____qualname____doc__r
   __annotations__r   r   intpropertystrr   r&   r(   r"   r"   r"   r#   r   "   s   
 FT_health_check_controllerraise_if_no_controller_runningr   c                 C   sZ   zt dur| rtt jj  t W S W t
|S  ty,   td t	d Y t
|S w )a  Gets the global client, which stores the controller's handle.

    Args:
        _health_check_controller: If True, run a health check on the
            cached controller if it exists. If the check fails, try reconnecting
            to the controller.
        raise_if_no_controller_running: Whether to raise an exception if
            there is no currently running Serve controller.

    Returns:
        ServeControllerClient to the running Serve controller. If there
        is no running controller and raise_if_no_controller_running is
        set to False, returns None.

    Raises:
        RayServeException: If there is no running Serve controller actor
            and raise_if_no_controller_running is set to True.
    Nz-The cached controller has died. Reconnecting.)r   raygetZ_controllerZcheck_aliveremoter   loggerinfo_set_global_client_connect)r2   r3   r"   r"   r#   _get_global_clientC   s   

r;   c                 C   s   | a d S r   )r   )clientr"   r"   r#   r9   e   r)   r9   c                   C   s   t S r   )r   r"   r"   r"   r#   _get_internal_replica_contextj   s   r=   r   r   r   r   r   c                 C   s   t | ||||dad S )Nr   r   r   r   r   )r   r   r>   r"   r"   r#   _set_internal_replica_contextn   s   	
r?   c                 C   sh   dt jjj_t  st jtd z	t jt	td}W n t
y)   | r&tdY dS w t|}t| |S )aO  Connect to an existing Serve application on this Ray cluster.

    If called from within a replica, this will connect to the same Serve
    app that the replica is running in.

    Returns:
        ServeControllerClient that encapsulates a Ray actor handle to the
        existing Serve application's Serve Controller. None if there is
        no running Serve controller actor and raise_if_no_controller_running
        is set to False.

    Raises:
        RayServeException: If there is no running Serve controller actor
            and raise_if_no_controller_running is set to True.
    F)	namespacez7There is no Serve instance running on this Ray cluster.N)r4   Z_privateZworkerZglobal_workerZ_filter_logs_by_jobZis_initializedinitr   Z	get_actorr   
ValueErrorr   r	   r9   )r3   
controllerr<   r"   r"   r#   r:      s"   r:   )frozenc                   @   sr   e Zd ZU dZeed< dZeed< dZeed< dZeed< dZ	eed< dZ
ee ed< d	Zeed
< d	Zeed< dS )_RequestContext route
request_id_internal_request_idr   multiplexed_model_idNgrpc_contextFis_http_requestcancel_on_parent_request_cancel)r*   r+   r,   rG   r1   r.   rH   rI   r   rJ   rK   r   r   rL   boolrM   r"   r"   r"   r#   rE      s   
 rE   z'Serve internal request context variable)defaultz0Serve internal batching request context variablec                   C   s    t  du rt t  t  S )zWGet the current request context.

    Returns:
        The current request context
    N)_serve_request_contextr5   setrE   r"   r"   r"   r#   _get_serve_request_context   s   rR   c                   C   s   t  du rt g  t  S )z7Get the list of request contexts for the current batch.N)_serve_batch_request_contextr5   rQ   r"   r"   r"   r#    _get_serve_batch_request_context   s   
rT   rF   rG   rH   rI   r   rJ   c              	   C   s@   t  }tt| p
|j|p|j|p|j|p|j|p|jd dS )z]Set the request context. If the value is not set,
    the current context value will be used.)rG   rH   rI   r   rJ   N)	rR   rP   rQ   rE   rG   rH   rI   r   rJ   )rG   rH   rI   r   rJ   Zcurrent_request_contextr"   r"   r#   _set_request_context   s   
rU   c                   C   s   t t  dS )zUnset the request context.N)rP   rQ   rE   r"   r"   r"   r#   _unset_request_context   s   rV   request_contextsc                 C   s   t |  dS )z5Add the request context to the batch request context.N)rS   rQ   )rW   r"   r"   r#   _set_batch_request_context   s   rX   _requests_pending_assignmentparent_request_idc                 C      | t v rt |  S i S r   rY   rZ   r"   r"   r#    _get_requests_pending_assignment     r^   response_idc                 C      |t |  |< d S r   r\   )rZ   r`   taskr"   r"   r#   _add_request_pending_assignment  s   rc   c                 C   4   |t |  v rt |  |= tt |  dkrt | = d S d S Nr   )rY   lenrZ   r`   r"   r"   r#   "_remove_request_pending_assignment   
   

rh   _in_flight_requestsc                 C   r[   r   rj   r]   r"   r"   r#   _get_in_flight_requests7  r_   rl   c                 C   ra   r   rk   )rZ   r`   Zreplica_resultr"   r"   r#   _add_in_flight_request>  s   rm   c                 C   rd   re   )rj   rf   rg   r"   r"   r#   _remove_in_flight_requestB  ri   rn   )FT)T)rF   rF   rF   rF   rF   )Cr-   asynciocontextvarsloggingcollectionsr   dataclassesr   typingr   r   r   r   r4   Zray.exceptionsr   Zray.serve._private.clientr	   Zray.serve._private.commonr
   Zray.serve._private.configr   Zray.serve._private.constantsr   r   r   Z!ray.serve._private.replica_resultr   Zray.serve.exceptionsr   Zray.serve.grpc_utilr   Zray.util.annotationsr   	getLoggerr7   r   r.   r   r   rN   r;   r9   r=   r/   r?   r:   rE   
ContextVarrP   rS   rR   rT   r1   rU   rV   rX   dictrY   Taskr^   rc   rh   rj   rl   rm   rn   r"   r"   r"   r#   <module>   s    
 
"
6
" 