Skip to content

Commit c10ca31

Browse files
jugalshah291Jugal Shah
andauthored
Optimize get_live_deployments (#54454)
## Why are these changes needed? The [_get_live_deployments](https://github.com/ray-project/ray/blob/master/python/ray/serve/_private/application_state.py#L476) function internally calls [get_deployments_in_application](https://github.com/ray-project/ray/blob/master/python/ray/serve/_private/deployment_state.py#L2603) that iterates over self._deployment_state ( that stores all the live deployments) and filters out deployment for a specific app. This is inefficient especially when you have a large of serve application running. (For every serve application we are iterating over all the deployments) ## Related issue number Closes #45793 ## Checks - [x] I've signed off every commit(by using the -s flag, i.e., `git commit -s`) in this PR. - [x] I've run `scripts/format.sh` to lint the changes in this PR. - [ ] I've included any doc changes needed for https://docs.ray.io/en/master/. - [ ] I've added any new APIs to the API Reference. For example, if I added a method in Tune, I've added it in `doc/source/tune/api/` under the corresponding `.rst` file. - [x] I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/ - Testing Strategy - [ ] Unit tests - [ ] Release tests - [x] This PR is not tested :( --------- Signed-off-by: Jugal Shah <[email protected]> Co-authored-by: Jugal Shah <[email protected]>
1 parent 16d280e commit c10ca31

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

python/ray/serve/_private/deployment_state.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,7 @@ def __init__(
25202520
self._shutting_down = False
25212521

25222522
self._deployment_states: Dict[DeploymentID, DeploymentState] = {}
2523+
self._app_deployment_mapping: Dict[str, Set[str]] = defaultdict(set)
25232524

25242525
self._recover_from_checkpoint(
25252526
all_current_actor_names, all_current_placement_group_names
@@ -2644,6 +2645,9 @@ def _recover_from_checkpoint(
26442645
deployment_to_current_replicas[deployment_id]
26452646
)
26462647
self._deployment_states[deployment_id] = deployment_state
2648+
self._app_deployment_mapping[deployment_id.app_name].add(
2649+
deployment_id.name
2650+
)
26472651

26482652
def shutdown(self):
26492653
"""
@@ -2793,19 +2797,14 @@ def deploy(
27932797
self._deployment_states[deployment_id] = self._create_deployment_state(
27942798
deployment_id
27952799
)
2800+
self._app_deployment_mapping[deployment_id.app_name].add(deployment_id.name)
27962801
self._record_deployment_usage()
27972802

27982803
return self._deployment_states[deployment_id].deploy(deployment_info)
27992804

28002805
def get_deployments_in_application(self, app_name: str) -> List[str]:
28012806
"""Return list of deployment names in application."""
2802-
2803-
deployments = []
2804-
for deployment_id in self._deployment_states:
2805-
if deployment_id.app_name == app_name:
2806-
deployments.append(deployment_id.name)
2807-
2808-
return deployments
2807+
return list(self._app_deployment_mapping[app_name])
28092808

28102809
def delete_deployment(self, id: DeploymentID):
28112810
# This method must be idempotent. We should validate that the
@@ -2907,6 +2906,17 @@ def update(self) -> bool:
29072906
self._deployment_scheduler.on_deployment_deleted(deployment_id)
29082907
self._autoscaling_state_manager.deregister_deployment(deployment_id)
29092908
del self._deployment_states[deployment_id]
2909+
if (
2910+
deployment_id.app_name in self._app_deployment_mapping
2911+
and deployment_id.name
2912+
in self._app_deployment_mapping[deployment_id.app_name]
2913+
):
2914+
self._app_deployment_mapping[deployment_id.app_name].remove(
2915+
deployment_id.name
2916+
)
2917+
# Clean up the app_name entry if no deployments are left
2918+
if not self._app_deployment_mapping[deployment_id.app_name]:
2919+
del self._app_deployment_mapping[deployment_id.app_name]
29102920

29112921
if len(deleted_ids):
29122922
self._record_deployment_usage()

0 commit comments

Comments
 (0)