o
    $i                     @   s   d dl Z d dlZd dlmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZmZ d dlmZ e e
Z		ddededed	ee d
ee defddZedddedeeeeef f fddZeZdS )    N)AnyDictOptionalTuple)CONTROL_LOOP_INTERVAL_S'SERVE_AUTOSCALING_DECISION_COUNTERS_KEYSERVE_LOGGER_NAME)AutoscalingConfigAutoscalingContext)	PublicAPIautoscaling_configtotal_num_requestsnum_running_replicasoverride_min_replicasoverride_max_replicasreturnc                 C   s   |dkrt d|  | }|| }|dkr|  }n|  }d|d |  }t|| }	t|| |k r?|	|kr?|	d8 }	| j}
| j}|durK|}
|durQ|}t|
t	||	}	|	S )a8  Returns the number of replicas to scale to based on the given metrics.

    Args:
        autoscaling_config: The autoscaling parameters to use for this
            calculation.
        current_num_ongoing_requests (List[float]): A list of the number of
            ongoing requests for each replica.  Assumes each entry has already
            been time-averaged over the desired lookback window.
        override_min_replicas: Overrides min_replicas from the config
            when calculating the final number of replicas.
        override_max_replicas: Overrides max_replicas from the config
            when calculating the final number of replicas.

    Returns:
        desired_num_replicas: The desired number of replicas to scale to, based
            on the input metrics and the current number of replicas.

    r   z!Number of replicas cannot be zero   N)

ValueErrorget_target_ongoing_requestsget_upscaling_factorget_downscaling_factormathceilmin_replicasmax_replicasmaxmin)r   r   r   r   r   target_num_requestserror_ratioscaling_factorsmoothed_error_ratiodesired_num_replicasr   r    r"   Y/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/serve/autoscaling_policy.py_calculate_desired_num_replicas   s*   

r$   alpha)	stabilityctxc                 C   s:  | j }| j}| j}| j}| j}| j}| j}|td}|dkr5|dkr1t	t
d|  ||fS ||fS |}	t|||||d}
|
|kr\|dk rJd}|d7 }|t|jt kr[d}|
}	n9|
|k r|dkrfd}|d8 }|dk}|r}|jdury|j}n|j}n|j}t	d|
}
|t|t  k rd}|
}	nd}||t< |	|fS )a  The default autoscaling policy based on basic thresholds for scaling.
    There is a minimum threshold for the average queue length in the cluster
    to scale up and a maximum threshold to scale down. Each period, a 'scale
    up' or 'scale down' decision is made. This decision must be made for a
    specified number of periods in a row before the number of replicas is
    actually scaled. See config options for more details.  Assumes
    `get_decision_num_replicas` is called once every CONTROL_LOOP_PERIOD_S
    seconds.
    r   r   )r   r   r   N)target_num_replicasr   current_num_replicasconfigcapacity_adjusted_min_replicascapacity_adjusted_max_replicaspolicy_stategetr   r   r   r   r   r$   intupscale_delay_sr   downscale_to_zero_delay_sdownscale_delay_s)r'   curr_target_num_replicasr   r   r*   r+   r,   r-   decision_counterdecision_num_replicasr!   is_scaling_to_zerodelay_sr"   r"   r#   'replica_queue_length_autoscaling_policyX   sf   

r8   )NN)loggingr   typingr   r   r   r   ray.serve._private.constantsr   r   r   ray.serve.configr	   r
   ray.util.annotationsr   	getLoggerloggerr/   floatr$   strr8   default_autoscaling_policyr"   r"   r"   r#   <module>   s:    

H[