o
    ciK                     @   s  d dl Z d dlZd dl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mZmZmZ d dl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mZ d d
lm Z  e!eZ"e ddG dd dZ#e ddG dd dZ$de$defddZ%dede$fddZ&dS )    N)deepcopy)AnyCallableDictListOptionalTupleUnion)DeploymentConfigReplicaConfigRequestRouterConfighandle_num_replicas_auto)SERVE_LOGGER_NAME)ServeUsageTag)DEFAULTDefault)AutoscalingConfig)DeploymentSchemaLoggingConfigRayActorOptionsSchema)	PublicAPIstable)	stabilityc                   @   s   e Zd ZdZdddZdS )Applicationa  One or more deployments bound with arguments that can be deployed together.

    Can be passed into another `Deployment.bind()` to compose multiple deployments in a
    single application, passed to `serve.run`, or deployed via a Serve config file.

    For example, to define an Application and run it in Python:

        .. code-block:: python

            from ray import serve
            from ray.serve import Application

            @serve.deployment
            class MyDeployment:
                pass

            app: Application = MyDeployment.bind(OtherDeployment.bind())
            serve.run(app)

    To run the same app using the command line interface (CLI):

        .. code-block:: bash

            serve run python_file:app

    To deploy the same app via a config file:

        .. code-block:: yaml

            applications:
                my_app:
                    import_path: python_file:app

    bound_deployment
Deploymentc                 C   s
   || _ d S N)_bound_deployment)selfr    r   H/home/ubuntu/.local/lib/python3.10/site-packages/ray/serve/deployment.py__init__<   s   
zApplication.__init__N)r   r   )__name__
__module____qualname____doc__r!   r   r   r   r    r      s    #r   c                /   @   s  e Zd ZdZ		dIdedededee ddf
d	d
ZdefddZ	e
defddZe
dee fddZe
deeef fddZe
defddZe
defddZe
defddZe
defddZe
dd Ze
dee fddZe
dee fdd Ze
dee fd!d"Ze
dee fd#d$Ze
defd%d&Zd'efd(d)Zd*d+ Zde fd,d-Z!de"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#e"j#dfd.ee de$e de$e d/e$eeeef   d0e$eedf  d1e$ee  d2e$e%eee&f   d3e$e d4e$e d5e$ee  d6e$e d7e$e d8e$eee'df  d9e$e& d:e$e& d;e$e& d<e$e& d'e$eee(df  d=e$eee)df  d>e$ee  d?e$eeef  d@e*dd f.dAdBZ+dCdD Z,dEdF Z-dGdH Z.dS )Jr   a  Class (or function) decorated with the `@serve.deployment` decorator.

    This is run on a number of replica actors. Requests to those replicas call
    this class.

    One or more deployments can be composed together into an `Application` which is
    then run via `serve.run` or a config file.

    Example:

    .. code-block:: python

        @serve.deployment
        class MyDeployment:
            def __init__(self, name: str):
                self._name = name

            def __call__(self, request):
                return "Hello world!"

        app = MyDeployment.bind()
        # Run via `serve.run` or the `serve run` CLI command.
        serve.run(app)

    NFnamedeployment_configreplica_configversionreturnc                 C   s   |st d| | |d u st|tstdd }t|jr6t|jdr6|jj	dkr6t|jdr6|jj
}|| _|| _|| _|| _|| _d S )NzZThe Deployment constructor should not be called directly. Use `@serve.deployment` instead.zversion must be a string.r#   zray.serve.api__fastapi_docs_path__)RuntimeError_validate_name
isinstancestr	TypeErrorinspectisclassdeployment_defhasattrr#   r+   _name_version_deployment_config_replica_config
_docs_path)r   r&   r'   r(   r)   	_internal	docs_pathr   r   r    r!   ]   s*   




zDeployment.__init__c                 C   s4   t |ts	tdd|v rtd| d d S d S )Nzname must be a string.#zvDeployment names should not contain the '#' character, this will raise an error starting in Ray 2.46.0. Current name: .)r.   r/   r0   warningswarn)r   r&   r   r   r    r-   |   s   
zDeployment._validate_namec                 C      | j S )zUnique name of this deployment.r5   r   r   r   r    r&      s   zDeployment.namec                 C   r@   r   )r6   rB   r   r   r    r)      s   zDeployment.versionc                 C      | j jS )z8Underlying class or function that this deployment wraps.)r8   r3   rB   r   r   r    func_or_class      zDeployment.func_or_classc                 C   rC   )zTarget number of replicas.)r7   num_replicasrB   r   r   r    rF      rE   zDeployment.num_replicasc                 C   rC   )z%Dynamic user-provided config options.)r7   user_configrB   r   r   r    rG      rE   zDeployment.user_configc                 C   rC   )z4Max number of requests a replica can handle at once.)r7   max_ongoing_requestsrB   r   r   r    rH      rE   zDeployment.max_ongoing_requestsc                 C   rC   )zDMax number of requests that can be queued in each deployment handle.)r7   max_queued_requestsrB   r   r   r    rI      rE   zDeployment.max_queued_requestsc                 C      t d)N`route_prefix` can no longer be specified at the deployment level. Pass it to `serve.run` or in the application config instead.)
ValueErrorrB   r   r   r    route_prefix   s   zDeployment.route_prefixc                 C   rC   )z:Actor options such as resources required for each replica.)r8   ray_actor_optionsrB   r   r   r    rN      rE   zDeployment.ray_actor_optionsc                 C   rC   r   )r8   	init_argsrB   r   r   r    rO         zDeployment.init_argsc                 C   rC   r   )r8   init_kwargsrB   r   r   r    rQ      rP   zDeployment.init_kwargsc                 C   s   t d d S )NzUDeprecationWarning: `Deployment.url` is deprecated and will be removed in the future.)loggerwarningrB   r   r   r    url   s   zDeployment.urlc                 C   rC   r   r7   logging_configrB   r   r   r    rV      rP   zDeployment.logging_configrV   c                 C   s   || j _d S r   rU   )r   rV   r   r   r    set_logging_config   s   zDeployment.set_logging_configc                 C   rJ   )NzNDeployments cannot be constructed directly. Use `deployment.deploy() instead.`)r,   rB   r   r   r    __call__   s   zDeployment.__call__c                 O   s   t | j||dS )zBind the arguments to the deployment and return an Application.

        The returned Application can be deployed using `serve.run` (or via
        config file) or bound to another deployment for composition.
        )
_init_args_init_kwargs)r   options)r   argskwargsr   r   r    bind   s   zDeployment.bindrD   rF   rM   rN   placement_group_bundlesplacement_group_strategymax_replicas_per_noderG   rH   rI   autoscaling_configgraceful_shutdown_wait_loop_sgraceful_shutdown_timeout_shealth_check_period_shealth_check_timeout_srequest_router_configrY   rZ   r:   c              	   C   sX  |t jur	td|du rtd|dkr$d}t||\}}tjd dd t  D }t	| j
}|s;|j| |t jddfvrN|t jdfvrNtd|d	krVtd
|sc|t jurctd n
|t jdfvrm||_|
t juru|
|_|t jur}||_|t jur||_|du r| jj}|t ju r| j}|t ju r| j}|t ju r| jj}|t ju r| jj}|t ju r| jj}|t ju r| jj}|t ju r| jj}|	t ju r| jj}	|t jur||_|t jur||_|t jur||_|t jur||_ |t jur||_!|t jur||_"|t jurt#|t$r|% }||_&t'j(|||||||	d}t)||||ddS )a  Return a copy of this deployment with updated options.

        Only those options passed in will be updated, all others will remain
        unchanged from the existing deployment.

        Refer to the `@serve.deployment` decorator docs for available arguments.
        rK   Nz2`max_ongoing_requests` must be non-null, got None.auto1c                 S   s&   g | ]\}}|d vr|t jur|qS )>   r   r:   rD   )r   VALUE).0optionvaluer   r   r    
<listcomp>  s    
z&Deployment.options.<locals>.<listcomp>zQManually setting num_replicas is not allowed when autoscaling_config is provided.r   z)num_replicas is expected to larger than 0zDeprecationWarning: `version` in `Deployment.options()` has been deprecated. Explicitly specifying version will raise an error in the future!)rO   rQ   rN   r_   r`   ra   T)r)   r:   )*r   rj   rL   r   r   AUTO_NUM_REPLICAS_USEDrecordlocalsitemsr   r7   user_configured_option_namesupdaterR   rS   rF   rG   rH   rI   r8   r3   r5   r6   rO   rQ   rN   r_   r`   ra   rb   rg   rc   rd   re   rf   r.   r   dictrV   r   creater   )r   rD   r&   r)   rF   rM   rN   r_   r`   ra   rG   rH   rI   rb   rc   rd   re   rf   rV   rg   rY   rZ   r:   rs   new_deployment_confignew_replica_configr   r   r    r[      s   
$

















zDeployment.optionsc                 C   sP   t | j|jk| j|jk| j|jk| jj|jjk| jj|jjk| jj|jjkgS r   )allr5   r6   r7   r8   rO   rQ   rN   )r   otherr   r   r    __eq__  s   


zDeployment.__eq__c                 C   s   d| j  dS )NzDeployment(name=)rA   rB   r   r   r    __str__  s   zDeployment.__str__c                 C   s   t | S r   )r/   rB   r   r   r    __repr__  s   zDeployment.__repr__)NF)/r"   r#   r$   r%   r/   r
   r   r   r!   r-   propertyr&   r)   r	   r   rD   intrF   r   rG   rH   rI   rM   r   rN   r   rO   rQ   rT   rV   rW   rX   r   r^   r   rj   r   r   floatr   r   r   boolr[   r{   r}   r~   r   r   r   r    r   A   s    



	



 3r   dr*   c                 C   s   | j durt| j }nd}i d| jd| jjrdn| jd| jd| jd| j	d| jjd| jj
d	| jjd
| jjd| jjd|d| jjd| jjd| jjd| jjd| jj}t| D ]}|dkrt|| jjvrt||= qetdi |S )zConverts a live deployment object to a corresponding structured schema.

    Args:
        d: Deployment object to convert
    Nr&   rF   rH   rI   rG   rb   rc   rd   re   rf   rN   r`   r_   ra   rV   rg   r   )rN   r   	parse_objr&   r7   rb   rF   rH   rI   rG   rc   rd   re   rf   r8   r`   r_   ra   rV   rg   listkeysrs   r   )r   ray_actor_options_schemadeployment_optionsrl   r   r   r    deployment_to_schema  sZ   
	
r   sc                 C   s   | j tju r	d}n| j jdd}| jtju rd}n| j}| jtju r%d}n| j}| jtju r1d}n| j}tj| j	| j
| j| j| j| j| j| j| j| j| jd}|  |_tjddi ||||d}t| j||ddS )	aM  Creates a deployment with parameters specified in schema.

    The returned deployment CANNOT be deployed immediately. It's func_or_class
    value is an empty string (""), which is not a valid import path. The
    func_or_class value must be overwritten with a valid function or class
    before the deployment can be deployed.
    NT)exclude_unset)rF   rG   rH   rI   rb   rc   rd   re   rf   rV   rg    r   )r3   rO   rQ   rN   r_   r`   ra   )r&   r'   r(   r:   )rN   r   rj   ru   r_   r`   ra   r
   from_defaultrF   rG   rH   rI   rb   rc   rd   re   rf   rV   rg   !_get_user_configured_option_namesrs   r   rv   r   r&   )r   rN   r_   r`   ra   r'   r(   r   r   r    schema_to_deployment  sT   	
r   )'r1   loggingr>   copyr   typingr   r   r   r   r   r   r	   ray.serve._private.configr
   r   r   r   ray.serve._private.constantsr   ray.serve._private.usager   ray.serve._private.utilsr   r   ray.serve.configr   ray.serve.schemar   r   r   ray.util.annotationsr   	getLoggerrR   r   r   r   r   r   r   r   r    <module>   s*    $
)  `3