o
    ci^'                     @   sL  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 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&	dDde'de'dee fddZ(dd Z)dd Z*dede	d efd!d"Z+dEde'defd#d$Z,edd%G d&d' d'Z-ej.d(dd)Z/d*d+ Z0	,	,	,	,	,dFd-e1d.e1d/e1d0e1d1e1f
d2d3Z2ee3Z4e
e1e
e1ej5f f e$d4< d5e1de
e1ej5f fd6d7Z6d5e1d8e1fd9d:Z7d5e1d8e1fd;d<Z8ee3Z9e
e1e
e1ef f e$d=< d>d? Z:d@dA Z;dBdC Z<dS )Gz
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Optional)RayActorError)ServeControllerClient)	ReplicaID)DeploymentConfig)SERVE_CONTROLLER_NAMESERVE_LOGGER_NAMESERVE_NAMESPACE)ReplicaResult)RayServeException)RayServegRPCContext)DeveloperAPIReplicaContext_INTERNAL_REPLICA_CONTEXT_global_clientc                   @   s`   e Zd ZU dZ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   aR  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.
    
replica_idservable_object_deployment_configreturnc                 C   
   | j jjS N)r   deployment_idapp_nameself r   E/home/ubuntu/.local/lib/python3.10/site-packages/ray/serve/context.pyr   2      
zReplicaContext.app_namec                 C   r   r   )r   r   namer   r   r   r    
deployment6   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
   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get_controllercheck_aliveremoter   loggerinfo_set_global_client_connect)r.   r/   r   r   r    _get_global_client?   s   

r9   c                 C   s   | a d S r   )r   )clientr   r   r    r7   a   r&   r7   c                   C   s   t S r   )r   r   r   r   r    _get_internal_replica_contextf   s   r;   r   r   r   c                 C   s   t | ||dad S )Nr   r   r   )r   r   r<   r   r   r    _set_internal_replica_contextj   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)r0   _privateworkerglobal_worker_filter_logs_by_jobis_initializedinitr   	get_actorr   
ValueErrorr   r   r7   )r/   
controllerr:   r   r   r    r8   x   s"   r8   )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)   rK   r-   r+   rL   rM   r   rN   rO   r   r   rP   boolrQ   r   r   r   r    rI      s   
 rI   z'Serve internal request context variable)defaultc                   C   s    t  du rt t  t  S )zWGet the current request context.

    Returns:
        The current request context
    N)_serve_request_contextr1   setrI   r   r   r   r    _get_serve_request_context   s   rV   rJ   rK   rL   rM   r   rN   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.)rK   rL   rM   r   rN   N)	rV   rT   rU   rI   rK   rL   rM   r   rN   )rK   rL   rM   r   rN   current_request_contextr   r   r    _set_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_requests  r_   rl   c                 C   ra   r   rk   )rZ   r`   replica_resultr   r   r    _add_in_flight_request!  s   rn   c                 C   rd   re   )rj   rf   rg   r   r   r    _remove_in_flight_request%  ri   ro   )FT)T)rJ   rJ   rJ   rJ   rJ   )=r*   asynciocontextvarsloggingcollectionsr   dataclassesr   typingr   r   r   r0   ray.exceptionsr   ray.serve._private.clientr   ray.serve._private.commonr	   ray.serve._private.configr
   ray.serve._private.constantsr   r   r   !ray.serve._private.replica_resultr   ray.serve.exceptionsr   ray.serve.grpc_utilr   ray.util.annotationsr   	getLoggerr5   r   r+   r   r   rR   r9   r7   r;   r=   r8   rI   
ContextVarrT   rV   r-   rX   dictrY   Taskr^   rc   rh   rj   rl   rn   ro   r   r   r   r    <module>   s    

"
6
"# 