o
    $i                     @   s   d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ dZe eZG d	d
 d
ZdS )    N)AnyDictOptional)cloudpickle)DeploymentIDEndpointInfo)SERVE_LOGGER_NAME)LongPollHostLongPollNamespace)KVStoreBasezserve-endpoint-state-checkpointc                   @   s   e Zd ZdZdedefddZdd Zdefd	d
Z	dd Z
dd Zdedee fddZdededdfddZdedee fddZdeeeeef f fddZdeddfddZdS )EndpointStatezManages all state for endpoints in the system.

    This class is *not* thread safe, so any state-modifying methods should be
    called with a lock held.
    kv_storelong_poll_hostc                 C   s@   || _ || _t | _| j t}|d urt|| _|   d S N)		_kv_store_long_poll_hostdict
_endpointsgetCHECKPOINT_KEYr   loads_notify_route_table_changed)selfr   r   
checkpoint r   ^/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/serve/_private/endpoint_state.py__init__   s   zEndpointState.__init__c                 C   s   | j t d S r   )r   deleter   r   r   r   r   shutdown!   s   zEndpointState.shutdownreturnc                 C   s   | j tdu S )zReturns whether the endpoint checkpoint has been deleted.

        Get the endpoint checkpoint from the kv store. If it is None, then it has been
        deleted.
        N)r   r   r   r   r   r   r   is_ready_for_shutdown$   s   z#EndpointState.is_ready_for_shutdownc                 C   s   | j tt| j d S r   )r   putr   r   dumpsr   r   r   r   r   _checkpoint,   s   zEndpointState._checkpointc                 C   s   | j tj| ji d S r   )r   notify_changedr
   ROUTE_TABLEr   r   r   r   r   r   /   s   
z)EndpointState._notify_route_table_changedroutec                 C   s*   | j  D ]\}}|j|kr|  S qd S r   r   itemsr'   )r   r'   endpointinfor   r   r   _get_endpoint_for_route4   s
   
z%EndpointState._get_endpoint_for_router*   endpoint_infoNc                 C   s   | j ||kr
dS | |j}|dur2||kr2td|j d|j d|j d|j d	 | j |= || j |< |   |   dS )zCreate or update the given endpoint.

        This method is idempotent - if the endpoint already exists it will be
        updated to match the given parameters. Calling this twice with the same
        arguments is a no-op.
        Nzroute_prefix "z)" is currently registered to deployment "z ". Re-registering route_prefix "z" to deployment "z".)	r   r   r,   r'   loggerdebugnamer$   r   )r   r*   r-   existing_route_endpointr   r   r   update_endpoint;   s"   


zEndpointState.update_endpointc                 C   s   || j v r| j | jS d S r   )r   r'   r   r*   r   r   r   get_endpoint_routeW   s   
z EndpointState.get_endpoint_routec                 C   s*   i }| j  D ]\}}d|ji||< q|S )Nr'   r(   )r   	endpointsr*   r+   r   r   r   get_endpoints\   s
   
zEndpointState.get_endpointsc                 C   s*   || j vrd S | j |= |   |   d S r   )r   r$   r   r3   r   r   r   delete_endpointd   s
   
zEndpointState.delete_endpoint)__name__
__module____qualname____doc__r   r	   r   r   boolr!   r$   r   strr   r   r,   r   r2   r4   r   r   r6   r7   r   r   r   r   r      s$    
r   )loggingtypingr   r   r   rayr   ray.serve._private.commonr   r   ray.serve._private.constantsr   ray.serve._private.long_pollr	   r
   #ray.serve._private.storage.kv_storer   r   	getLoggerr.   r   r   r   r   r   <module>   s    
