o
    civ                     @   s   d dl Z d dlZd dlmZmZmZ d dlmZmZ d dl	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dedee
 dededeeef defddZeZdS )    N)AnyDictOptional)CONTROL_LOOP_INTERVAL_SSERVE_LOGGER_NAME)AutoscalingConfig)	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   P/home/ubuntu/.local/lib/python3.10/site-packages/ray/serve/autoscaling_policy.py_calculate_desired_num_replicas   s*   

r!   alpha)	stabilitycurr_target_num_replicasconfigcapacity_adjusted_min_replicascapacity_adjusted_max_replicaspolicy_statec           
      C   s   | dd}|dkr|dkrttd|  | S | S | }t|||||d}	|	| krC|dk r1d}|d7 }|t|jt krBd}|	}n|	| k r`|dkrMd}|d8 }|t|j	t  k r_d}|	}nd}||d< |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.
    decision_counterr   r   )r   r   r   )
getr   r   r   r   r!   intupscale_delay_sr   downscale_delay_s)
r$   r
   r   r%   r&   r'   r(   r)   decision_num_replicasr   r   r   r    'replica_queue_length_autoscaling_policyT   sF   r/   )NN)loggingr   typingr   r   r   ray.serve._private.constantsr   r   ray.serve.configr   ray.util.annotationsr   	getLoggerloggerr+   floatr!   strr/   default_autoscaling_policyr   r   r   r    <module>   sR    

H
J