o
    1 iJ                     @   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	 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_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)

ValueErrorZget_target_ongoing_requestsget_upscaling_factorZget_downscaling_factormathceilmin_replicasmax_replicasmaxmin)r   r   r   r   r   Ztarget_num_requestsZerror_ratioZscaling_factorZsmoothed_error_ratiodesired_num_replicasr   r    r   h/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/serve/autoscaling_policy.py_calculate_desired_num_replicas   s*   

r   alpha)Z	stabilityctxc                 C   s:  | j }| j}| j}| j}| j}| j}| j}|d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}||d< |	|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.
    decision_counterr   r   )r   r   r   N)Ztarget_num_replicasr   Zcurrent_num_replicasconfigcapacity_adjusted_min_replicascapacity_adjusted_max_replicaspolicy_stategetr   r   r   r   r   intZupscale_delay_sr   Zdownscale_to_zero_delay_sZdownscale_delay_s)r   Zcurr_target_num_replicasr   r   r!   r"   r#   r$   r    Zdecision_num_replicasr   Zis_scaling_to_zeroZdelay_sr   r   r   'replica_queue_length_autoscaling_policyT   sf   

r'   )NN)loggingr   typingr   r   r   r   Zray.serve._private.constantsr   r   Zray.serve.configr   r	   Zray.util.annotationsr
   	getLoggerloggerr&   floatr   strr'   Zdefault_autoscaling_policyr   r   r   r   <module>   s:    

H[