Skip to content

Commit

Permalink
[Serve] Separate internal API and Public API (ray-project#26804)
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan138 <[email protected]>
  • Loading branch information
sihanwang41 authored and Rohan138 committed Jul 28, 2022
1 parent 7c05145 commit ba3aa20
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 38 deletions.
Empty file.
59 changes: 59 additions & 0 deletions python/ray/serve/_private/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from ray.serve.context import get_global_client
from ray.serve.deployment import Deployment
from typing import Dict


def get_deployment(name: str):
"""Dynamically fetch a handle to a Deployment object.
Args:
name(str): name of the deployment. This must have already been
deployed.
Returns:
Deployment
"""
try:
(
deployment_info,
route_prefix,
) = get_global_client().get_deployment_info(name)
except KeyError:
raise KeyError(
f"Deployment {name} was not found. Did you call Deployment.deploy()?"
)
return Deployment(
deployment_info.replica_config.deployment_def,
name,
deployment_info.deployment_config,
version=deployment_info.version,
init_args=deployment_info.replica_config.init_args,
init_kwargs=deployment_info.replica_config.init_kwargs,
route_prefix=route_prefix,
ray_actor_options=deployment_info.replica_config.ray_actor_options,
_internal=True,
)


def list_deployments() -> Dict[str, Deployment]:
"""Returns a dictionary of all active deployments.
Dictionary maps deployment name to Deployment objects.
"""
infos = get_global_client().list_deployments()

deployments = {}
for name, (deployment_info, route_prefix) in infos.items():
deployments[name] = Deployment(
deployment_info.replica_config.deployment_def,
name,
deployment_info.deployment_config,
version=deployment_info.version,
init_args=deployment_info.replica_config.init_args,
init_kwargs=deployment_info.replica_config.init_kwargs,
route_prefix=route_prefix,
ray_actor_options=deployment_info.replica_config.ray_actor_options,
_internal=True,
)

return deployments
42 changes: 5 additions & 37 deletions python/ray/serve/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
install_serve_encoders_to_fastapi,
)

from ray.serve._private import api as _private_api

logger = logging.getLogger(__file__)


Expand Down Expand Up @@ -483,26 +485,7 @@ def get_deployment(name: str) -> Deployment:
Returns:
Deployment
"""
try:
(
deployment_info,
route_prefix,
) = get_global_client().get_deployment_info(name)
except KeyError:
raise KeyError(
f"Deployment {name} was not found. Did you call Deployment.deploy()?"
)
return Deployment(
deployment_info.replica_config.deployment_def,
name,
deployment_info.deployment_config,
version=deployment_info.version,
init_args=deployment_info.replica_config.init_args,
init_kwargs=deployment_info.replica_config.init_kwargs,
route_prefix=route_prefix,
ray_actor_options=deployment_info.replica_config.ray_actor_options,
_internal=True,
)
return _private_api.get_deployment(name)


@deprecated(instructions="Please see https://docs.ray.io/en/latest/serve/index.html")
Expand All @@ -512,23 +495,8 @@ def list_deployments() -> Dict[str, Deployment]:
Dictionary maps deployment name to Deployment objects.
"""
infos = get_global_client().list_deployments()

deployments = {}
for name, (deployment_info, route_prefix) in infos.items():
deployments[name] = Deployment(
deployment_info.replica_config.deployment_def,
name,
deployment_info.deployment_config,
version=deployment_info.version,
init_args=deployment_info.replica_config.init_args,
init_kwargs=deployment_info.replica_config.init_kwargs,
route_prefix=route_prefix,
ray_actor_options=deployment_info.replica_config.ray_actor_options,
_internal=True,
)

return deployments
return _private_api.list_deployments()


@PublicAPI(stability="alpha")
Expand Down Expand Up @@ -611,7 +579,7 @@ def run(
)

if ingress is not None:
return ingress.get_handle()
return ingress._get_handle()


def build(target: Union[ClassNode, FunctionNode]) -> Application:
Expand Down
36 changes: 36 additions & 0 deletions python/ray/serve/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ def bind(self, *args, **kwargs) -> Union[ClassNode, FunctionNode]:
def deploy(self, *init_args, _blocking=True, **init_kwargs):
"""Deploy or update this deployment.
Args:
init_args: args to pass to the class __init__
method. Not valid if this deployment wraps a function.
init_kwargs: kwargs to pass to the class __init__
method. Not valid if this deployment wraps a function.
"""
self._deploy(*init_args, _blocking=_blocking, **init_kwargs)

# TODO(Sihan) Promote the _deploy to deploy after we fully deprecate the API
def _deploy(self, *init_args, _blocking=True, **init_kwargs):
"""Deploy or update this deployment.
Args:
init_args: args to pass to the class __init__
method. Not valid if this deployment wraps a function.
Expand Down Expand Up @@ -238,6 +250,12 @@ def deploy(self, *init_args, _blocking=True, **init_kwargs):
def delete(self):
"""Delete this deployment."""

return self._delete()

# TODO(Sihan) Promote the _delete to delete after we fully deprecate the API
def _delete(self):
"""Delete this deployment."""

return get_global_client().delete_deployments([self._name])

@deprecated(
Expand All @@ -259,6 +277,24 @@ def get_handle(
ServeHandle
"""

return self._get_handle(sync)

# TODO(Sihan) Promote the _get_handle to get_handle after we fully deprecate the API
def _get_handle(
self, sync: Optional[bool] = True
) -> Union[RayServeHandle, RayServeSyncHandle]:
"""Get a ServeHandle to this deployment to invoke it from Python.
Args:
sync: If true, then Serve will return a ServeHandle that
works everywhere. Otherwise, Serve will return an
asyncio-optimized ServeHandle that's only usable in an asyncio
loop.
Returns:
ServeHandle
"""

return get_global_client().get_handle(self._name, missing_ok=True, sync=sync)

@PublicAPI
Expand Down
4 changes: 3 additions & 1 deletion python/ray/serve/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ def options(self, *, method_name: str):

def remote(self, *args, **kwargs):
if not self.handle:
handle = serve.get_deployment(self.deployment_name).get_handle()
handle = serve._private.api.get_deployment(
self.deployment_name
)._get_handle()
self.handle = handle.options(method_name=self.handle_options.method_name)
# TODO (jiaodong): Polish async handles later for serve pipeline
return self.handle.remote(*args, **kwargs)
Expand Down

0 comments on commit ba3aa20

Please sign in to comment.