o
    1 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	m
Z
mZmZmZ d dlmZ d dlZd dlmZ d dlmZmZ d dlmZmZmZ d d	lmZ d d
lm Z  dZ!eddG dd dZ"e ddeddG dd dZ#e$Z%e&Z'e$Z(eG dd dZ)G dd de$eZ*G dd de$eZG dd de$eZG dd de$eZ+ej,fd ej-fdej.fdej/ej0fdej1ej0fdej/ej2fd ej1ej2fd ej3fd iZ4edd!G d"d# d#ZeddG d$d% d%Z5eddG d&d' d'Z6G d(d) d)e$eZ7G d*d+ d+e$eZ8eG d,d- d-Z9eG d.d/ d/Z:G d0d1 d1e$eZ;G d2d3 d3e$eZ<eG d4d5 d5Z=G d6d7 d7Z>G d8d9 d9e$eZ?eddG d:d; d;Z@eddG d<d= d=ZAeBd>ZCd?ZDd@ZEdAZFeddBG dCdD dDZGeeG ZHeG dEdF dFZIeG dGdH dHZJdS )I    N)asdict	dataclassfield)Enum)Any	AwaitableCallableDictListOptional)Scope)ActorHandle)SERVE_DEFAULT_APP_NAMESERVE_NAMESPACE)DeploymentStatusDeploymentStatusInfoDeploymentStatusTrigger)RayServegRPCContext)	PublicAPIzSERVE_REPLICA::T)frozenc                   @   s:   e Zd ZU eed< eZeed< dd Zdd Zdd Z	d	S )
DeploymentIDnameapp_namec                 C   s   d| j  d| j S )NzServeReplica::)r   r   self r   e/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/serve/_private/common.pyto_replica_actor_class_name   s   z(DeploymentID.to_replica_actor_class_namec                 C   s   d| j  d| j dS )NzDeployment(name='', app='')r   r   r   r   r   r   __str__   s   zDeploymentID.__str__c                 C      t | S Nstrr   r   r   r   __repr__!      zDeploymentID.__repr__N)
__name__
__module____qualname__r&   __annotations__r   r   r   r"   r'   r   r   r   r   r      s   
 r   alpha)Z	stabilityc                   @   sx   e Zd ZU dZeed< 	 eed< 	 defddZedede	fdd	Z
edefd
dZdefddZdefddZdS )	ReplicaIDz"A unique identifier for a replica.	unique_iddeployment_idreturnc                 C   s:   | j j d| j }| j jr| j j d| }t | S )N#)r0   r   r/   r   REPLICA_ID_FULL_ID_STR_PREFIX)r   sr   r   r   to_full_id_str0   s   zReplicaID.to_full_id_strr4   c                 C   s
   |  tS r$   )
startswithr3   )r4   r   r   r   is_full_id_str7   s   
zReplicaID.is_full_id_strc                 C   sz   |  |sJ |ttd  d}t|dkr|\}}}nt|dkr+d}|\}}ntd| d| |t||ddS )	Nr2          zGiven replica ID string z\ didn't match expected pattern, ensure it has either two or three fields with delimiter '#'.r!   )r0   )r7   lenr3   split
ValueErrorr   )clsr4   parsedr   Zdeployment_namer/   r   r   r   from_full_id_str;   s   


zReplicaID.from_full_id_strc                 C   r#   r$   r%   r   r   r   r   r'   P   r(   zReplicaID.__repr__c                 C   s"   d| j  d| jj d| jj dS )z{Returns a human-readable string.

        This is used in user-facing log messages, so take care when updating it.
        zReplica(id='z', deployment='r   r    )r/   r0   r   r   r   r   r   r   r"   S   s   zReplicaID.__str__N)r)   r*   r+   __doc__r&   r,   r   r5   staticmethodboolr7   classmethodr@   r'   r"   r   r   r   r   r.   %   s   
 r.   c                   @   s"   e Zd ZU eed< dZeed< dS )EndpointInforouteFapp_is_cross_languageN)r)   r*   r+   r&   r,   rG   rC   r   r   r   r   rE   f      
 rE   c                   @   $   e Zd ZdZdZdZdZdZdZdS )ReplicaStateSTARTINGUPDATING
RECOVERINGRUNNINGSTOPPINGPENDING_MIGRATIONN)	r)   r*   r+   rK   rL   rM   rN   rO   rP   r   r   r   r   rJ   m       rJ   c                   @   rI   )r   rL   HEALTHY	UNHEALTHYDEPLOY_FAILED	UPSCALINGDOWNSCALINGN)	r)   r*   r+   rL   rR   rS   rT   rU   rV   r   r   r   r   r   v   rQ   r   c                   @   s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdS )r   z?Explains how a deployment reached its current DeploymentStatus.UNSPECIFIEDCONFIG_UPDATE_STARTEDCONFIG_UPDATE_COMPLETEDUPSCALE_COMPLETEDDOWNSCALE_COMPLETEDAUTOSCALINGREPLICA_STARTUP_FAILEDHEALTH_CHECK_FAILEDINTERNAL_ERRORDELETINGN)r)   r*   r+   rA   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   r   r   r   r   r      s    r   c                   @   s4   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )DeploymentStatusInternalTriggerrR   CONFIG_UPDATEAUTOSCALE_UPAUTOSCALE_DOWNMANUALLY_INCREASE_NUM_REPLICASMANUALLY_DECREASE_NUM_REPLICASr]   r^   r_   DELETEN)r)   r*   r+   rR   rb   rc   rd   re   rf   r]   r^   r_   rg   r   r   r   r   ra      s    ra      r9   r8   )eqc                   @   s   e Zd ZU eed< eed< eed< dZeed< ede	fddZ
d	d
 Z			ddededefddZdefddZ	ddededd fddZdd ZedefddZdS )r   r   statusstatus_triggerr:   messager1   c                 C   s<   | j ftv rt| j f S | j | jftv rt| j | jf S dS )zGet priority of state based on ranking_order().

        The ranked order indicates what the status should be of a
        hierarchically "higher" resource when derived from a group of
        `DeploymentStatusInfo` sub-resources.
        N)rj   DEPLOYMENT_STATUS_RANKING_ORDERrk   r   r   r   r   rank   s
   	zDeploymentStatusInfo.rankc                 C   s   t jt| ddS )N   )indent)jsondumpsr   r   r   r   r   debug_string   s   z!DeploymentStatusInfo.debug_stringNc                 C   s,   t | j|r|n| j|r||dS | j|dS )zGReturns a copy of the current object with the passed in kwargs updated.r   rj   rk   rl   )r   r   rj   rk   )r   rj   rk   rl   r   r   r   _updated_copy   s   z"DeploymentStatusInfo._updated_copyc                 C   s   | j |dS )N)rl   )ru   )r   rl   r   r   r   update_message   s   z#DeploymentStatusInfo.update_messagetriggerc                 C   s  |t jkr| jtjtj|dS |t jkr| jtjtj|dS | j	tjkr|t j
kr3| jtj
tj|dS |t jkrB| jtjtj|dS |t jkrQ| jtjtj|dS |t jkr`| jtjtj|dS |t jt jhv rj| S |t jkry| jtjtj|dS |t jkr| jtjtj|dS | S | j	tjtjhv rD|t jkr| jtjtj|dS |t jkr| jtjtj|dS |t j
kr| jtj
| j	tjkrtj|dS tj|dS |t jkr| jtjtj|dS | jtjkr|t jkr| jtj|dS |t jkr| jtj|dS |t jkr| jtjtj|dS |t jkr| jtjtj|dS | S | jtjkrB|t jkr4| jtj|dS |t jkrB| jtj|dS | S | j	tj
kr|t j
krS| S |t jkrc| jtjtj|dS |t jkrs| jtjtj|dS |t jkr| jtjtj|dS |t jkr| jtjtj|dS |t jkr| jtjtj|dS |t jkr| jtjtj|dS | S | j	tjkr|t j
kr| jtj
tj|dS |t jkr| jtjtj|dS |t jkr| jtjtj|dS |t jkr| jtjtj|dS | S | j	tjkrE|t j
kr| jtj
tj|dS |t jkr%| jtjtj|dS |t jkr5| jtjtj|dS |t jkrE| jtjtj|dS | S )a  Handles a transition from the current state to the next state.

        Args:
            trigger: An internal trigger that determines the state
                transition. This is the new incoming trigger causing the
                transition.
            message: The message to set in status info.

        Returns:
            New instance of DeploymentStatusInfo representing the
            next state to transition to.
        )rj   rk   rl   )rj   rl   )ra   r_   ru   r   rS   r   rg   rL   r`   rj   rR   rY   rb   rX   rc   rU   r\   rd   rV   re   rf   r^   rT   r]   rZ   r[   rk   rW   )r   rw   rl   r   r   r   handle_transition   s  







 P 



	

  xE&z&DeploymentStatusInfo.handle_transitionc                 C   s(   t | jd| jj d| jj | jdS NZDEPLOYMENT_STATUS_ZDEPLOYMENT_STATUS_TRIGGER_rt   )DeploymentStatusInfoProtor   rj   rk   rl   r   r   r   r   to_proto	  s   zDeploymentStatusInfo.to_protoprotoc                 C   sL   t |jtdd  }t|jtdd  }| |jt|t||j	dS ry   )
DeploymentStatusProtoNamerj   r;   DeploymentStatusTriggerProtork   r   r   r   rl   )r>   r|   rj   rk   r   r   r   
from_proto  s   

zDeploymentStatusInfo.from_proto)NNr:   )r:   )r)   r*   r+   r&   r,   r   r   rl   propertyintrn   rs   ru   rv   ra   rx   r{   rD   rz   r   r   r   r   r   r      s@   
 

  'r   c                   @   s   e Zd ZU eed< ee ed< ee ed< ee ed< eed< eed< dZ	e
ed< eed	Zee ed
< eed	Zeeef ed< dZee ed< dd Zdd Zdd ZdS )RunningReplicaInfo
replica_idnode_idZnode_ipZavailability_zoneactor_handlemax_ongoing_requestsFis_cross_language)default_factorymultiplexed_model_idsrouting_statsNportc                 C   s`   t d| j | jr| jndt| jjt| jt| j	t| j
t| jg}t| d| d S )N r:   _hash)hashjoinr   r5   r   r&   r   Z	_actor_idr   r   r   r   object__setattr__)r   Zhash_valr   r   r   __post_init__,  s   
z RunningReplicaInfo.__post_init__c                 C      | j S r$   )r   r   r   r   r   __hash__E  s   zRunningReplicaInfo.__hash__c                 C   s   t t|t| j|jkgS r$   )all
isinstancer   r   )r   otherr   r   r   __eq__H  s
   
zRunningReplicaInfo.__eq__)r)   r*   r+   r.   r,   r   r&   r   r   r   rC   r   listr   r
   dictr   r	   r   r   r   r   r   r   r   r   r   r     s   
 r   c                   @   s"   e Zd ZU eed< ee ed< dS )DeploymentTargetInfoZis_availableZrunning_replicasN)r)   r*   r+   rC   r,   r
   r   r   r   r   r   r   Q  rH   r   c                   @      e Zd ZdZdS )ServeDeployMode	MULTI_APPN)r)   r*   r+   r   r   r   r   r   r   W      r   c                   @   r   )ServeComponentTypeZreplicaN)r)   r*   r+   REPLICAr   r   r   r   r   [  r   r   c                   @   sF   e Zd ZU dZeed< dZeee	  ed< dZ
eee	ef  ed< dS )RequestRoutingInfozInformation about the request routing.

    It includes deployment name (from ReplicaID), replica tag (from ReplicaID),
    multiplex model ids, and routing stats.
    r   Nr   r   )r)   r*   r+   rA   r.   r,   r   r   r
   r&   r   r	   r   r   r   r   r   r   _  s
   
 r   c                   @   s   e Zd ZU dZeed< dS )gRPCRequestzKSent from the GRPC proxy to replicas on both unary and streaming codepaths.Zuser_request_protoN)r)   r*   r+   rA   r   r,   r   r   r   r   r   l  s   
 r   c                   @      e Zd ZdZdZdZdS )RequestProtocol	UNDEFINEDHTTPZgRPCN)r)   r*   r+   r   r   GRPCr   r   r   r   r   s      r   c                   @   r   )DeploymentHandleSourceUNKNOWNPROXYr   N)r)   r*   r+   r   r   r   r   r   r   r   r   y  r   r   c                   @   s   e Zd ZU eed< 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Zeed< ejZeed< dZee ed< dZe
ed< ede
fddZede
fddZdS )RequestMetadataZ
request_idZinternal_request_id__call__call_methodr:   rF   r   multiplexed_model_idFis_streaming_http_method_request_protocolNgrpc_contextT_by_referencer1   c                 C      | j tjkS r$   )r   r   r   r   r   r   r   is_http_request     zRequestMetadata.is_http_requestc                 C   r   r$   )r   r   r   r   r   r   r   is_grpc_request  r   zRequestMetadata.is_grpc_request)r)   r*   r+   r&   r,   r   rF   r   r   r   rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    
 r   c                   @   sv   e Zd ZdZddddedee deeege	e
 f  fddZed	efd
dZed	eege	e
 f fddZdS )StreamingHTTPRequestz?Sent from the HTTP proxy to replicas on the streaming codepath.N)proxy_actor_namereceive_asgi_messages
asgi_scoper   r   c                C   s4   || _ |d u r|d u rtd|| _d | _|| _d S )NzBEither proxy_actor_name or receive_asgi_messages must be provided.)_asgi_scoper=   _proxy_actor_name_cached_proxy_actor_receive_asgi_messages)r   r   r   r   r   r   r   __init__  s   	zStreamingHTTPRequest.__init__r1   c                 C   r   r$   )r   r   r   r   r   r     s   zStreamingHTTPRequest.asgi_scopec                 C   s.   | j d u rtj| jtd| _| jjj| _ | j S )N)	namespace)r   rayZ	get_actorr   r   r   r   remoter   r   r   r   r     s   
z*StreamingHTTPRequest.receive_asgi_messages)r)   r*   r+   rA   r   r   r&   r   r   r   bytesr   r   r   r   r   r   r   r   r     s"    
"r   c                   @   r   )TargetCapacityDirectionz9Determines what direction the target capacity is scaling.UPDOWNN)r)   r*   r+   rA   r   r   r   r   r   r   r     s    r   c                   @   s   e Zd ZU eed< eed< dS )ReplicaQueueLengthInfoacceptedZnum_ongoing_requestsN)r)   r*   r+   rC   r,   r   r   r   r   r   r     s   
 r   c                   @   sJ   e Zd ZU eeeef  ed< eed< eed< eed< dZe	e ed< dS )CreatePlacementGroupRequestZbundlesZstrategyZtarget_node_idr   Nruntime_env)
r)   r*   r+   r
   r	   r&   floatr,   r   r   r   r   r   r   r     s   
 r   z|Converting by-value DeploymentResponses to ObjectRefs is not supported. Use handle.options(_by_reference=True) to enable it.Zrunning_requestsZongoing_requestsqueued_requests)orderc                   @   s(   e Zd ZU eed< eddZeed< dS )TimeStampedValue	timestampF)comparevalueN)r)   r*   r+   r   r,   r   r   r   r   r   r   r     s   
 r   c                   @   s   e Zd ZU dZeed< eed< eed< eed< eed< e	ed< e
ee
eef f ed< e
ee
ee	f f ed	< eed
< edefddZedefddZdS )HandleMetricReporta  Report from a deployment handle on queued and ongoing requests.

    Args:
        deployment_id: The deployment ID of the deployment handle.
        handle_id: The handle ID of the deployment handle.
        actor_id: If the deployment handle (from which this metric was
            sent) lives on an actor, the ID of that actor.
        handle_source: Describes what kind of entity holds this
            deployment handle: a Serve proxy, a Serve replica, or
            unknown.
        aggregated_queued_requests: average number of queued requests at the
            handle over the past look_back_period_s seconds.
        queued_requests: list of values of queued requests at the
            handle over the past look_back_period_s seconds. This is a list because
            we take multiple measurements over time.
        aggregated_metrics: A map of metric name to the aggregated value over the past
            look_back_period_s seconds at the handle for each replica.
        metrics: A map of metric name to the list of values running at that handle for each replica
            over the past look_back_period_s seconds. This is a list because
            we take multiple measurements over time.
        timestamp: The time at which this report was created.
    r0   Z	handle_idZactor_idhandle_sourceaggregated_queued_requestsr   aggregated_metricsmetricsr   r1   c                 C   s   | j t| jti   S )z,Total number of queued and running requests.)r   sumr   getRUNNING_REQUESTS_KEYvaluesr   r   r   r   total_requests(  s   z!HandleMetricReport.total_requestsc                 C   s   | j tjtjfv S )aQ  Whether the handle source is a Serve actor.

        More specifically, this returns whether a Serve actor tracked
        by the controller holds the deployment handle that sent this
        report. If the deployment handle lives on a driver, a Ray task,
        or an actor that's not a Serve replica, then this returns False.
        )r   r   r   r   r   r   r   r   is_serve_component_source/  s   	z,HandleMetricReport.is_serve_component_sourceN)r)   r*   r+   rA   r   r,   r&   r   r   
TimeSeriesr	   r.   r   r   rC   r   r   r   r   r   r     s   
 r   c                   @   sB   e Zd ZU dZeed< eeef ed< eee	f ed< eed< dS )ReplicaMetricReporta  Report from a replica on ongoing requests.

    Args:
        replica_id: The replica ID of the replica.
        aggregated_metrics: A map of metric name to the aggregated value over the past
            look_back_period_s seconds at the replica.
        metrics: A map of metric name to the list of values running at that replica
            over the past look_back_period_s seconds. This is a list because
            we take multiple measurements over time.
        timestamp: The time at which this report was created.
    r   r   r   r   N)
r)   r*   r+   rA   r.   r,   r	   r&   r   r   r   r   r   r   r   >  s   
 r   )Krq   dataclassesr   r   r   enumr   typingr   r   r   r	   r
   r   Zstarlette.typesr   r   Z	ray.actorr   Zray.serve._private.constantsr   r   Zray.serve.generated.serve_pb2r   r}   r   rz   r   r   Zray.serve.grpc_utilr   Zray.util.annotationsr   r3   r   r.   r&   ZNodeIdr   ZDurationZApplicationNamerE   rJ   ra   rT   rS   rL   rU   rX   rV   r\   rR   rm   r   r   r   r   r   r   r   r   r   r   r   r   r   RuntimeErrorZOBJ_REF_NOT_SUPPORTED_ERRORr   ZONGOING_REQUESTS_KEYZQUEUED_REQUESTS_KEYr   r   r   r   r   r   r   r   <module>   s     :		  f1+.
8