controller_avatar.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import ray
  2. from ray.serve._private.constants import SERVE_CONTROLLER_NAME, SERVE_NAMESPACE
  3. from ray.serve._private.default_impl import get_controller_impl
  4. from ray.serve.config import HTTPOptions
  5. from ray.serve.schema import LoggingConfig
  6. @ray.remote(num_cpus=0)
  7. class ServeControllerAvatar:
  8. """A hack that proxy the creation of async actors from Java.
  9. To be removed after https://github.com/ray-project/ray/pull/26037
  10. Java api can not support python async actor. If we use java api create
  11. python async actor. The async init method won't be executed. The async
  12. method will fail with pickle error. And the run_control_loop of controller
  13. actor can't be executed too. We use this proxy actor create python async
  14. actor to avoid the above problem.
  15. """
  16. def __init__(
  17. self,
  18. http_proxy_port: int = 8000,
  19. ):
  20. try:
  21. self._controller = ray.get_actor(
  22. SERVE_CONTROLLER_NAME, namespace=SERVE_NAMESPACE
  23. )
  24. except ValueError:
  25. self._controller = None
  26. if self._controller is None:
  27. controller_impl = get_controller_impl()
  28. self._controller = controller_impl.remote(
  29. http_options=HTTPOptions(port=http_proxy_port),
  30. global_logging_config=LoggingConfig(),
  31. )
  32. def check_alive(self) -> None:
  33. """No-op to check if this actor is alive."""
  34. return