o
    ci                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZ d dlmZmZ d dlmZmZ d dlmZ eeZe G dd dZed	d
eG dd dZeddG dd dZG dd dZdS )    N)	dataclassfield)AnyCallableDictListOptionalSet)	ReplicaIDRequestMetadata)&RAY_SERVE_QUEUE_LENGTH_CACHE_TIMEOUT_SSERVE_LOGGER_NAME)	PublicAPIc                   @   sZ   e Zd ZU dZe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d< dS )	RequestRoutingContextNmultiplexed_start_matching_timeFtried_fewest_multiplexed_modelstried_first_multiplexed_modelstried_same_nodetried_same_azshould_backoff)__name__
__module____qualname__r   r   float__annotations__r   boolr   r   r   r    r   r   \/home/ubuntu/.local/lib/python3.10/site-packages/ray/serve/_private/request_router/common.pyr      s   
 r   alpha)	stabilityc                   @   s   e Zd ZU dZee ed< 	 eeef ed< 	 eed< 	 e	e
j
dZeed< 	 e	dd dZejed	< 	 e	edZeed
< 	 dd ZdS )PendingRequestz1A request that is pending execution by a replica.argskwargsmetadata)default_factory
created_atc                   C   s   t  S N)asyncioFuturer   r   r   r   <lambda>,   s    zPendingRequest.<lambda>futurerouting_contextc                 C   s   t  | _dS )zFReset the `asyncio.Future`, must be called if this request is re-used.N)r'   r(   r*   )selfr   r   r   reset_future4   s   zPendingRequest.reset_futureN)r   r   r   __doc__r   r   r   r   r   r   timer%   r   r*   r'   r(   r   r+   r-   r   r   r   r   r       s"   
 r    T)frozenc                   @   s   e Zd ZU eed< eed< dS )ReplicaQueueLengthCacheEntry	queue_len	timestampN)r   r   r   intr   r   r   r   r   r   r1   9   s   
 r1   c                   @   s   e Zd Zedddedeeg ef  fddZdede	fd	d
Z
dedee fddZdedefddZdefddZdee fddZdS )ReplicaQueueLengthCacheN)staleness_timeout_sget_curr_time_sr6   r7   c                C   s*   i | _ || _|d ur|| _d S tj| _d S r&   )_cache_staleness_timeout_sr/   _get_curr_time_s)r,   r6   r7   r   r   r   __init__@   s   
z ReplicaQueueLengthCache.__init__timestamp_sreturnc                 C   s   |   | | jkS r&   )r:   r9   )r,   r<   r   r   r   _is_timed_outL      z%ReplicaQueueLengthCache._is_timed_out
replica_idc                 C   s*   | j |}|du s| |jrdS |jS )zGet the queue length for a replica.

        Returns `None` if the replica ID is not present or the entry is timed out.
        N)r8   getr>   r3   r2   )r,   r@   entryr   r   r   rA   O   s   zReplicaQueueLengthCache.getr2   c                 C   s   t ||  | j|< dS )z2Set (or update) the queue length for a replica ID.N)r1   r:   r8   )r,   r@   r2   r   r   r   updateZ   s   zReplicaQueueLengthCache.updatec                 C   s   | j |d  d S r&   )r8   pop)r,   r@   r   r   r   invalidate_key`   r?   z&ReplicaQueueLengthCache.invalidate_keyactive_replica_idsc                C   s,   t | j D ]}||vr| j| qdS )zCRemoves entries for all replica IDs not in the provided active set.N)listr8   keysrD   )r,   rF   r@   r   r   r   remove_inactive_replicasc   s
   z0ReplicaQueueLengthCache.remove_inactive_replicas)r   r   r   r   r   r   r   r;   r4   r   r>   r
   rA   rC   rE   r	   rI   r   r   r   r   r5   ?   s    
r5   )r'   loggingr/   dataclassesr   r   typingr   r   r   r   r   r	   ray.serve._private.commonr
   r   ray.serve._private.constantsr   r   ray.util.annotationsr   	getLoggerloggerr   r    r1   r5   r   r   r   r   <module>   s"     
	