-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Serve] Refactor ReplicaQueueLengthAutoscalingPolicy
into AutoscalingPolicyManager
and policy function
#42242
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import logging | ||
from typing import List, Optional | ||
|
||
from ray.serve._private.common import TargetCapacityDirection | ||
from ray.serve._private.constants import SERVE_LOGGER_NAME | ||
from ray.serve._private.utils import get_capacity_adjusted_num_replicas | ||
from ray.serve.config import AutoscalingConfig | ||
|
||
logger = logging.getLogger(SERVE_LOGGER_NAME) | ||
|
||
|
||
class AutoscalingPolicyManager: | ||
"""Managing autoscaling policies and the lifecycle of the scaling function calls.""" | ||
|
||
def __init__(self, config: Optional[AutoscalingConfig]): | ||
self.config = config | ||
self.policy = None | ||
self.policy_state = {} | ||
self._create_policy() | ||
|
||
def _create_policy(self): | ||
"""Creates an autoscaling policy based on the given config.""" | ||
if self.config: | ||
self.policy = self.config.get_policy() | ||
|
||
def should_autoscale(self) -> bool: | ||
"""Returns whether autoscaling should be performed.""" | ||
return self.policy is not None | ||
|
||
def get_decision_num_replicas( | ||
self, | ||
curr_target_num_replicas: int, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Let's be consistent w/ naming -- either prefix all params w/ "curr" or "current" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 will rename in the upcoming PR |
||
current_num_ongoing_requests: List[float], | ||
current_handle_queued_queries: float, | ||
target_capacity: Optional[float] = None, | ||
target_capacity_direction: Optional[TargetCapacityDirection] = None, | ||
_skip_bound_check: bool = False, | ||
) -> Optional[int]: | ||
"""Interface with the autoscaling policy to get a decision to scale replicas. | ||
|
||
After the decision number of replicas is returned by the policy, it is then | ||
bounded by the bounds min and max adjusted by the target capacity and returned. | ||
If `_skip_bound_check` is True, then the bounds are not applied. | ||
""" | ||
capacity_adjusted_min_replicas = self.get_current_lower_bound( | ||
target_capacity, | ||
target_capacity_direction, | ||
) | ||
capacity_adjusted_max_replicas = get_capacity_adjusted_num_replicas( | ||
self.config.max_replicas, | ||
target_capacity, | ||
) | ||
decision_num_replicas = self.policy( | ||
curr_target_num_replicas=curr_target_num_replicas, | ||
current_num_ongoing_requests=current_num_ongoing_requests, | ||
current_handle_queued_queries=current_handle_queued_queries, | ||
config=self.config, | ||
capacity_adjusted_min_replicas=capacity_adjusted_min_replicas, | ||
capacity_adjusted_max_replicas=capacity_adjusted_max_replicas, | ||
policy_state=self.policy_state, | ||
) | ||
|
||
if _skip_bound_check: | ||
return decision_num_replicas | ||
|
||
return self.apply_bounds( | ||
curr_target_num_replicas=decision_num_replicas, | ||
target_capacity=target_capacity, | ||
target_capacity_direction=target_capacity_direction, | ||
) | ||
|
||
def get_current_lower_bound( | ||
self, | ||
target_capacity: Optional[float] = None, | ||
target_capacity_direction: Optional[TargetCapacityDirection] = None, | ||
) -> int: | ||
"""Get the autoscaling lower bound, including target_capacity changes. | ||
|
||
The autoscaler uses initial_replicas scaled by target_capacity only | ||
if the target capacity direction is UP. | ||
""" | ||
|
||
if self.config.initial_replicas is not None and ( | ||
target_capacity_direction == TargetCapacityDirection.UP | ||
): | ||
return get_capacity_adjusted_num_replicas( | ||
self.config.initial_replicas, | ||
target_capacity, | ||
) | ||
else: | ||
return get_capacity_adjusted_num_replicas( | ||
self.config.min_replicas, | ||
target_capacity, | ||
) | ||
|
||
def apply_bounds( | ||
self, | ||
curr_target_num_replicas: int, | ||
target_capacity: Optional[float] = None, | ||
target_capacity_direction: Optional[TargetCapacityDirection] = None, | ||
) -> int: | ||
"""Clips curr_target_num_replicas using the current bounds.""" | ||
|
||
upper_bound = get_capacity_adjusted_num_replicas( | ||
self.config.max_replicas, | ||
target_capacity, | ||
) | ||
lower_bound = self.get_current_lower_bound( | ||
target_capacity, | ||
target_capacity_direction, | ||
) | ||
return max(lower_bound, min(upper_bound, curr_target_num_replicas)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd rather call this
is_enabled
, since we're checking whether APM is working rather whether we want to autoscale itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is refactored from https://github.com/ray-project/ray/pull/42242/files#diff-c4c2583a4c2f3a3c87ada6faebd0b2dfef404e165df133a11195be5d65fcb387L1267 and keeping the naming. I feel we can change both places to
is_autoscaling_policy_enabled
to be more descriptive. What do you think?