o
    ciy                     @   s  d dl Z d dlZd dlmZmZmZmZmZ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mZmZmZ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 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-m.Z/m0Z0m1Z2m3Z4m5Z6m+Z7 d dl8m9Z9 de0de:fddZ;dedefddZ<G dd deZ.de
e=e'f dee
ee*e'f  fddZ>G dd dZ5dS )    N)AnyCallableDictListOptionalSetTupleUnion)FieldDescriptor)Message)cloudpickle)	BaseModelFieldNonNegativeFloatNonNegativeIntPositiveFloatPositiveInt	validator)resources_from_ray_options)ray_option_utils)pickle_dumps)#DEFAULT_GRACEFUL_SHUTDOWN_TIMEOUT_S%DEFAULT_GRACEFUL_SHUTDOWN_WAIT_LOOP_SDEFAULT_HEALTH_CHECK_PERIOD_SDEFAULT_HEALTH_CHECK_TIMEOUT_SDEFAULT_MAX_ONGOING_REQUESTSMAX_REPLICAS_PER_NODE_MAX_VALUE)DEFAULTDeploymentOptionUpdateType)AutoscalingConfigRequestRouterConfig)r   DeploymentConfigDeploymentLanguageEncodingTypeLoggingConfigReplicaConfigr    )validate_placement_groupdeployment_languageis_cross_languagec                 C   s(   | t jkr	|s	dS | t jkr|rdS dS )zGFrom Serve client API's perspective, decide whether pickling is needed.TF)r"   PYTHONJAVA)r'   r(    r+   M/home/ubuntu/.local/lib/python3.10/site-packages/ray/serve/_private/config.py_needs_pickle+   s
   r-   protoreturnc                 C   s   i }|   D ]6\}}|jtjkr)|jtjkr!dd |D ||j< qt|||j< q|jtjkr7t|||j< q|||j< q| j	j
D ]}|j|vrW|jtjkrW|jsW|j||j< qA|S )zRecursively convert a protobuf into a Python dictionary.

    This is an alternative to protobuf's `MessageToDict`. Unlike
    `MessageToDict`, this function doesn't add an extra base64
    encoding to bytes when constructing a json response.
    c                 S   s   g | ]}t |qS r+   )_proto_to_dict).0vr+   r+   r,   
<listcomp>H   s    z"_proto_to_dict.<locals>.<listcomp>)
ListFieldslabelr
   LABEL_REPEATEDtypeTYPE_MESSAGEnamelistr0   
DESCRIPTORfieldscontaining_oneofdefault_value)r.   datafieldvaluer+   r+   r,   r0   8   s&   
r0   c                   @   s  e Zd ZU dZedejdZee	 e
d< eeejdZee
d< edejdZee
d< edejdZee
d	< eeejdZee
d
< eeejdZee
d< eeejdZee
d< eeejdZee
d< edejdZee e
d< ee  ejdZ!e e
d< dZ"e#e
d< e$j%Z&ee
d< edej'dZ(ee) e
d< edejdZ*ee+ e
d< e, Z-e.e) e
d< G dd dZ/e0d	dddd Z1e0ddddd Z2e0ddddd Z3d d! Z4d"d# Z5d$d% Z6e7d&e8fd'd(Z9e7d)e:fd*d+Z;e7d,d- Z<dS ).r!   a  Internal datastructure wrapping config options for a deployment.

    Args:
        num_replicas: The number of processes to start up that
            handles requests to this deployment. Defaults to 1.
        max_ongoing_requests: The maximum number of queries
            that is sent to a replica of this deployment without receiving
            a response. Defaults to 5.
        max_queued_requests: Maximum number of requests to this deployment that will be
            queued at each *caller* (proxy or DeploymentHandle). Once this limit is
            reached, subsequent requests will raise a BackPressureError (for handles) or
            return an HTTP 503 status code (for HTTP requests). Defaults to -1 (no
            limit).
        user_config: Arguments to pass to the reconfigure
            method of the deployment. The reconfigure method is called if
            user_config is not None. Must be JSON-serializable.
        graceful_shutdown_wait_loop_s: Duration
            that deployment replicas wait until there is no more work to
            be done before shutting down.
        graceful_shutdown_timeout_s: Controller waits for this duration
            to forcefully kill the replica for shutdown.
        health_check_period_s: Frequency at which the controller health
            checks replicas.
        health_check_timeout_s: Timeout that the controller waits for a
            response from the replica's health check before marking it
            unhealthy.
        autoscaling_config: Autoscaling configuration.
        logging_config: Configuration for deployment logs.
        user_configured_option_names: The names of options manually
            configured by the user.
        request_router_config: Configuration for deployment request router.
       )defaultupdate_typenum_replicasmax_ongoing_requestsmax_queued_requestsNuser_configgraceful_shutdown_timeout_sgraceful_shutdown_wait_loop_shealth_check_period_shealth_check_timeout_sautoscaling_configrequest_router_configFr(   r'   versionlogging_configuser_configured_option_namesc                   @   s   e Zd ZdZdZdS )zDeploymentConfig.ConfigTN)__name__
__module____qualname__validate_assignmentarbitrary_types_allowedr+   r+   r+   r,   Config   s    rX   T)alwaysc              
   C   sX   t |tr|S |d ur*zt| W |S  ty) } z
tdt| dd }~ww |S )Nz&user_config is not JSON-serializable: .)
isinstancebytesjsondumps	TypeError
ValueErrorstr)clsr2   er+   r+   r,   user_config_json_serializable   s   
z.DeploymentConfig.user_config_json_serializablec                 C   sL   |d u r|S t |tstdt| dddlm} |di | }|S )NzGot invalid type 'z,' for logging_config. Expected a dictionary.r   )r$   r+   )r[   dictr_   r7   ray.serve.schemar$   )rb   r2   r$   r+   r+   r,   logging_config_valid   s   
z%DeploymentConfig.logging_config_validc                 C   s.   t |ts	td|dk r|dkrtd|S )Nz'max_queued_requests must be an integer.rB   rG   z@max_queued_requests must be -1 (no limit) or a positive integer.)r[   intr_   r`   )rb   r2   r+   r+   r,   validate_max_queued_requests   s   
z-DeploymentConfig.validate_max_queued_requestsc                 C   s   t | j| jS N)r-   r'   r(   selfr+   r+   r,   needs_pickle   s   zDeploymentConfig.needs_picklec                 C   s&  |   }|dd ur|  rt|d |d< |dr(td
i |d |d< |dra|d d}|d urV|sAd|d d< n|  rOt||d d< ntd| td
i |d |d< |drd|d v ryt	|d d |d d< t
d
i |d |d< t|d	 |d	< td
i |S )NrI   rN   rO   request_router_kwargs    zRNon-empty request_router_kwargs not supportedfor cross-language deployments. Got: rQ   encodingrR   r+   )re   getrm   r   r^   AutoscalingConfigProtor`   RequestRouterConfigProtoEncodingTypeProtoValueLoggingConfigProtor:   DeploymentConfigProto)rl   r?   router_kwargsr+   r+   r,   to_proto   sH   





zDeploymentConfig.to_protoc                 C      |    S rj   ry   SerializeToStringrk   r+   r+   r,   to_proto_bytes     zDeploymentConfig.to_proto_bytesr.   c                 C   s  t |}d|v r|d ntj}d|v r|d nd}t||}d|v r=|d dkr9|r3t|j|d< n
|j|d< nd |d< d|v ryd|d v rn|d d }|dkrh|r_t|jj|d d< n|jj|d d< ni |d d< t	di |d |d< d|v r|d 
d	sd |d d	< |d 
d
sd |d d
< |d 
dsd |d d< |d 
dsd |d d< |d 
dsd |d d< tdi |d |d< d|v r|d dkrd |d< d|v rt|d |d< d|v rd|d v rt|d d |d d< | di |S )Nr'   r(   FrI   ro   rO   rn   rN   upscale_smoothing_factordownscale_smoothing_factorupscaling_factordownscaling_factortarget_ongoing_requestsrP    rR   rQ   rp   r+   )r0   r"   r)   r-   r   loadsrI   rO   rn   r    rq   r   setrt   Name)rb   r.   r?   r'   r(   rm   rn   r+   r+   r,   
from_proto  sx   


zDeploymentConfig.from_protoproto_bytesc                 C   s   t |}| |S rj   )rw   
FromStringr   )rb   r   r.   r+   r+   r,   from_proto_bytes[  s   

z!DeploymentConfig.from_proto_bytesc              	   K   s   |  }t |  }| D ]\}}||vr'td| d| dt| dqdd | D }| D ]
\}}||| q5|S )zCreates a default DeploymentConfig and overrides it with kwargs.

        Ignores any kwargs set to DEFAULT.VALUE.

        Raises:
            TypeError: when a keyword that's not an argument to the class is
                passed in.
        z&Got invalid Deployment config option "z" (with value zO) as keyword argument. All Deployment config options must come from this list: rZ   c                 S   s    i | ]\}}|t jkr||qS r+   )r   VALUE)r1   keyvalr+   r+   r,   
<dictcomp>x  s     z1DeploymentConfig.from_default.<locals>.<dictcomp>)r   re   keysitemsr_   r:   __setattr__)rb   kwargsconfigvalid_config_optionsr   r   r+   r+   r,   from_default`  s    zDeploymentConfig.from_default)=rS   rT   rU   __doc__r   r   LightWeightrE   r   r   __annotations__r   NeedsActorReconfigurerF   r   rH   rh   rI   r   r   NeedsReconfigurerJ   r   r   rK   r   rL   r   r   rM   rN   r   r    rO   r(   boolr"   r)   r'   HeavyWeightrP   ra   rQ   re   r   rR   r   rX   r   rd   rg   ri   rm   ry   r}   classmethodrw   r   r\   r   r   r+   r+   r+   r,   r!   ^   s   
 !





&Dr!   rF   rN   c                 C   sh   |t jdfv rt }| |fS t jdd}t|tr|n|jdd}|| tdi |}| |fS )aF  Return modified `max_ongoing_requests` and `autoscaling_config`
    for when num_replicas="auto".

    If `autoscaling_config` is unspecified, returns the modified value
    AutoscalingConfig.default().
    If it is specified, the specified fields in `autoscaling_config`
    override that of AutoscalingConfig.default().
    NT)exclude_unsetr+   )r   r   r   rC   re   r[   update)rF   rN   default_configr+   r+   r,   handle_num_replicas_auto  s   

r   c                   @   s  e Zd ZdZ				d/dededededed	eeeee	f   d
ee dee
 defddZdd Z			d0ded	eeeee	f   d
ee dee
 fddZe							d1deeef deee  deeeef  dee d	eeeee	f   d
ee dee
 dee fddZdd Zd2ddZd2ddZedeeef fdd Zedeeee ef  fd!d"Zedeee  fd#d$Zed3d%edefd&d'Zed3d(edefd)d*Zd+d, Z d-d. Z!dS )4r%   a>  Internal datastructure wrapping config options for a deployment's replicas.

    Provides five main properties (see property docstrings for more info):
        deployment_def: the code, or a reference to the code, that this
            replica should run.
        init_args: the deployment_def's init_args.
        init_kwargs: the deployment_def's init_kwargs.
        ray_actor_options: the Ray actor options to pass into the replica's
            actor.
        resource_dict: contains info on this replica's actor's resource needs.

    Offers a serialized equivalent (e.g. serialized_deployment_def) for
    deployment_def, init_args, and init_kwargs. Deserializes these properties
    when they're first accessed, if they were not passed in directly through
    create().

    Use the classmethod create() to make a ReplicaConfig with the deserialized
    properties.

    Note: overwriting or setting any property after the ReplicaConfig has been
    constructed is currently undefined behavior. The config's fields should not
    be modified externally after it is created.
    NTdeployment_def_nameserialized_deployment_defserialized_init_argsserialized_init_kwargsray_actor_optionsplacement_group_bundlesplacement_group_strategymax_replicas_per_noderm   c
           
      C   s`   || _ || _|| _|| _d| _d| _d| _|| _|| _|| _	|| _
|   t| j| _|	| _dS )zConstruct a ReplicaConfig with serialized properties.

        All parameters are required. See classmethod create() for defaults.
        N)r   r   r   r   _deployment_def
_init_args_init_kwargsr   r   r   r   	_validater   resource_dictrm   )
rl   r   r   r   r   r   r   r   r   rm   r+   r+   r,   __init__  s   
zReplicaConfig.__init__c                 C   s<   |    |   |   | jd ur| jd urtdd S d S )NzVSetting max_replicas_per_node is not allowed when placement_group_bundles is provided.)_validate_ray_actor_options!_validate_placement_group_options_validate_max_replicas_per_noder   r   r`   rk   r+   r+   r,   r     s   

zReplicaConfig._validatec                 C   s0   || _ || _|| _|| _|   t| j | _d S rj   )r   r   r   r   r   r   r   )rl   r   r   r   r   r+   r+   r,   r     s   zReplicaConfig.updatedeployment_def	init_argsinit_kwargsc	           
   	   C   s   t |st|tstd|du st|ttfstd|du s)t|ts)tdt|r:|r4t	d|r:t	dt|t
tfsKtdt| d|du rQd	}|du rWi }|du r]i }|du rlt|tri|}n|j}| |t|d
t| t|dt|d||||}	||	_||	_||	_|	S )z4Create a ReplicaConfig from deserialized parameters.z8@serve.deployment must be called on a class or function.Nzinit_args must be a tuple.zinit_kwargs must be a dict.z1init_args not supported for function deployments.z3init_kwargs not supported for function deployments.Got invalid type "zQ" for deployment_def. Expected deployment_def to be a class, function, or string.r+   z#Could not serialize the deployment z,Could not serialize the deployment init argsz.Could not serialize the deployment init kwargs)callabler[   ra   r_   tupler:   re   inspect
isfunctionr`   r   r7   rS   r   reprr   r   r   )
rb   r   r   r   r   r   r   r   r   r   r+   r+   r,   create  sT   

zReplicaConfig.createc                 C   s   t | jtstdt| j dh d}| jD ]}||vr(td| d| qtj| jdd | jdd u r@d	| jd< d S d S )
Nr   z/" for ray_actor_options. Expected a dictionary.>   memorynum_cpusnum_gpus	resourcesruntime_envaccelerator_typezSpecifying 'z8' in ray_actor_options is not allowed. Allowed options: T)
in_optionsr   rB   )	r[   r   re   r_   r7   r`   r   validate_actor_optionsrq   )rl   allowed_ray_actor_optionsoptionr+   r+   r,   r   J  s"   
z)ReplicaConfig._validate_ray_actor_optionsr/   c                 C   sf   | j d u rd S t| j tstdt| j  dt d| j dk s%| j tkr1td| j  dt dd S )NzGet invalid type 'zM' for max_replicas_per_node. Expected None or an integer in the range of [1, z].rB   zInvalid max_replicas_per_node z:. Valid values are None or an integer in the range of [1, )r   r[   rh   r_   r7   r   r`   rk   r+   r+   r,   r   i  s    



z-ReplicaConfig._validate_max_replicas_per_nodec                 C   s$  | j d ur| jd u rtd| jd urt| j| j pddd d}| jd }|dd}| jdd}||k rCt| d	| d
| d|dd}| jdd}||k rat| d| d
| d| jdi }| D ]#\}}	||d}
|
|	k rt| d| d|	 d
|
 d| d
qld S d S )Nz[If `placement_group_strategy` is provided, `placement_group_bundles` must also be provided.PACKdetached)bundlesstrategylifetimezWhen using `placement_group_bundles`, the replica actor will be placed in the first bundle, so the resource requirements for the actor must be a subset of the first bundle.r   CPUr   z `num_cpus` for the actor is z, but the bundle only has z `CPU` specified.GPUr   z `num_gpus` for the actor is z `GPU` specified.r   z `z` requirement for the actor is z` specified.)r   r   r`   r&   rq   r   r   )rl   resource_error_prefixfirst_bundle
bundle_cpureplica_actor_num_cpus
bundle_gpureplica_actor_num_gpusreplica_actor_resourcesactor_resourceactor_valuebundle_valuer+   r+   r,   r   |  sd   



$z/ReplicaConfig._validate_placement_group_optionsc                 C   s:   | j du r| jrt| j| _ | j S | jjdd| _ | j S )aD  The code, or a reference to the code, that this replica runs.

        For Python replicas, this can be one of the following:
            - Function (Callable)
            - Class (Callable)
            - Import path (str)

        For Java replicas, this can be one of the following:
            - Class path (str)
        Nzutf-8)rp   )r   rm   r   r   r   decoderk   r+   r+   r,   r     s   
zReplicaConfig.deployment_defc                 C   s2   | j du r| jrt| j| _ | j S | j| _ | j S )zThe init_args for a Python class.

        This property is only meaningful if deployment_def is a Python class.
        Otherwise, it is None.
        N)r   rm   r   r   r   rk   r+   r+   r,   r     s   
zReplicaConfig.init_argsc                 C   s   | j du rt| j| _ | j S )zThe init_kwargs for a Python class.

        This property is only meaningful if deployment_def is a Python class.
        Otherwise, it is None.
        N)r   r   r   r   rk   r+   r+   r,   r     s   
zReplicaConfig.init_kwargsr.   c              
   C   sv   t |j|j|jdkr|jnd |jdkr|jnd t|j|jr%t|jnd |j	dkr.|j	nd |j
r7|j
|	S d |	S )Nro   r   )r%   r   r   r   r   r]   r   r   r   r   r   )rb   r.   rm   r+   r+   r,   r     s$   


zReplicaConfig.from_protor   c                 C   s   t |}| ||S rj   )ReplicaConfigProtor   r   )rb   r   rm   r.   r+   r+   r,   r     s   
zReplicaConfig.from_proto_bytesc              
   C   sT   t | j| j| j| jt| j| jd urt| jnd| j	| j
d ur&| j
dS ddS )Nr   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   r]   r^   r   r   r   r   rk   r+   r+   r,   ry     s   


zReplicaConfig.to_protoc                 C   rz   rj   r{   rk   r+   r+   r,   r}   
  r~   zReplicaConfig.to_proto_bytes)NNNT)NNN)NNNNNNN)r/   N)T)"rS   rT   rU   r   ra   r\   r   r   r   floatrh   r   r   r   re   r   r   r	   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   ry   r}   r+   r+   r+   r,   r%     s    	

-


	C

5 r%   )?r   r]   typingr   r   r   r   r   r   r   r	   google.protobuf.descriptorr
   google.protobuf.messager   rayr   ray._common.pydantic_compatr   r   r   r   r   r   r   ray._common.utilsr   ray._privater   ray._private.serializationr   ray.serve._private.constantsr   r   r   r   r   r   ray.serve._private.utilsr   r   ray.serve.configr   r    ray.serve.generated.serve_pb2rr   r!   rw   r"   r#   rt   r$   rv   r%   r   rs   ray.util.placement_groupr&   r   r-   r0   rh   r   r+   r+   r+   r,   <module>   s4    ($	 $	&  $

 