o
    ci                     @   s  d dl 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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 e eZed
ZedZG dd dee	eef ZeddG dd dZdede defddZ!ddddddede dee  deeeef  deee ef  deeee gef  defddZ"dd dede d!eee f d"eeef deee ef  deee gef de
e fd#d$Z#d%edeee ef  defd&d'Z$ded!eee f de fd(d)Z%dS )*    N)deepcopy)	dataclass)AnyCallableDictGenericListOptionalTypeVarUnion)_PyObjScanner)SERVE_LOGGER_NAME)Application
Deployment)DeploymentHandle)LoggingConfigKVc                       sd   e Zd ZdZdedef fddZdedef fddZdef fd	d
Zde	f fddZ
  ZS )IDDictzDictionary that uses id() for keys instead of hash().

    This is necessary because Application objects aren't hashable and we want each
    instance to map to a unique key.
    keyreturnc                       t |ts	t|}t |S N)
isinstanceintidsuper__getitem__selfr   	__class__ P/home/ubuntu/.local/lib/python3.10/site-packages/ray/serve/_private/build_app.pyr         
zIDDict.__getitem__valuec                    s    t |ts	t|}t ||S r   )r   r   r   r   __setitem__)r   r   r%   r    r"   r#   r&      s   
zIDDict.__setitem__c                    r   r   )r   r   r   r   __delitem__r   r    r"   r#   r'   #   r$   zIDDict.__delitem__c                    r   r   )r   r   r   r   __contains__r   r    r"   r#   r(   (   r$   zIDDict.__contains__)__name__
__module____qualname____doc__r   r   r   r&   r'   objectr(   __classcell__r"   r"   r    r#   r      s    r   T)frozenc                   @   sR   e Zd ZU eed< ee ed< ee ed< eed< ee ed< e	ee
f ed< dS )BuiltApplicationnameroute_prefixlogging_configingress_deployment_namedeploymentsdeployment_handlesN)r)   r*   r+   str__annotations__r	   r   r   r   r   r   r"   r"   r"   r#   r0   .   s   
 r0   
deploymentapp_namer   c                 C   s   t | j|dS )N)r:   )r   r1   )r9   r:   r"   r"   r#   _make_deployment_handle_default>   s   r;   )r2   r3   default_runtime_envmake_deployment_handleappr1   r2   r3   r<   r=   c             	      sV   |du rt }t }t  t| || ||d}t|||| jj| fdd| D dS )a  Builds the application into a list of finalized deployments.

    The following transformations are made:
        - Application objects in constructor args/kwargs are converted to
          DeploymentHandles for injection at runtime.
        - Name conflicts from deployments that use the same class are handled
          by appending a monotonically increasing suffix (e.g., SomeClass_1).

    Returns: BuiltApplication
    N)r:   handlesdeployment_namesr<   r=   c                    s   i | ]	\}} | |qS r"   r"   ).0r>   handler@   r"   r#   
<dictcomp>o   s    zbuild_app.<locals>.<dictcomp>)r1   r2   r3   r4   r5   r6   )r;   r   _build_app_recursiver0   _bound_deploymentr1   items)r>   r1   r2   r3   r<   r=   r?   r5   r"   rC   r#   	build_appG   s,   
rH   )r<   r@   r?   c                C   s   | |v rg S g }t td}zF|| jj| jjf}|D ]}	|t|	|||||d q||\}
}| jj	t
| ||
|d}t||}||||| < ||g W |  S |  w )aN  Recursively traverses the graph of Application objects.

    Each Application will have an associated DeploymentHandle created that will replace
    it in any occurrences in other Applications' args or kwargs.

    Also collects a list of the unique Applications encountered and returns them as
    deployable Deployment objects.
    )source_type)r:   r?   r@   r=   r<   )r1   
_init_args_init_kwargs)r   r   
find_nodesrF   	init_argsinit_kwargsextendrE   replace_nodesoptions$_get_unique_deployment_name_memoized_set_default_runtime_envclear)r>   r:   r@   r?   r<   r=   r5   scanner
child_apps	child_appnew_init_argsnew_init_kwargsfinal_deploymentr"   r"   r#   rE   u   sD   

rE   dc                 C   sX   |s| S t | jp	i }|dd}d|vr||d< n|dur&|d d| | j|dS )ah  Configures the deployment with the provided default runtime_env.

    If the deployment does not have a runtime_env configured, the default will be set.

    If it does have a runtime_env configured but that runtime_env does not have a
    working_dir, only the working_dir field will be set.

    Else the deployment's runtime_env will be left untouched.
    working_dirNruntime_env)ray_actor_options)r   r^   get
setdefaultrQ   )r[   r<   r^   default_working_dirr"   r"   r#   rS      s   
rS   c                 C   s~   | |v r||  S d}| j j}|| v r'| j j d| }|d7 }|| v s|dkr9td| j j d| d ||| < |S )a  Generates a name for the deployment.

    This is used to handle collisions when the user does not specify a name
    explicitly, so typically we'd use the class name as the default.

    In that case, we append a monotonically increasing suffix to the name, e.g.,
    Deployment, then Deployment_1, then Deployment_2, ...

    Names are memoized in the `deployment_names` dict, which should be passed to
    subsequent calls to this function.
       _z3There are multiple deployments with the same name 'z'. Renaming one to 'z'.)rF   r1   valuesloggerwarning)r>   r@   idxr1   r"   r"   r#   rR      s$   rR   )&loggingcopyr   dataclassesr   typingr   r   r   r   r   r	   r
   r   ray.dag.py_obj_scannerr   ray.serve._private.constantsr   ray.serve.deploymentr   r   ray.serve.handler   ray.serve.schemar   	getLoggerre   r   r   dictr   r0   r7   r;   rH   rE   rS   rR   r"   r"   r"   r#   <module>   s    (



4


A

