예제 #1
0
def connect(conn_str: str,
            secure: bool = False,
            metadata: List[Tuple[str, str]] = None,
            connection_retries: int = 3,
            job_config: JobConfig = None,
            namespace: str = None,
            *,
            ignore_version: bool = False) -> Dict[str, Any]:
    if ray.is_connected():
        raise RuntimeError("Ray Client is already connected. "
                           "Maybe you called ray.util.connect twice by "
                           "accident?")
    # Enable the same hooks that RAY_CLIENT_MODE does, as
    # calling ray.util.connect() is specifically for using client mode.
    _set_client_hook_status(True)
    _explicitly_enable_client_mode()

    # TODO(barakmich): https://github.com/ray-project/ray/issues/13274
    # for supporting things like cert_path, ca_path, etc and creating
    # the correct metadata
    return ray.connect(conn_str,
                       job_config=job_config,
                       secure=secure,
                       metadata=metadata,
                       connection_retries=connection_retries,
                       namespace=namespace,
                       ignore_version=ignore_version)
예제 #2
0
def connect(
        conn_str: str,
        secure: bool = False,
        metadata: List[Tuple[str, str]] = None,
        connection_retries: int = 3,
        job_config: JobConfig = None,
        namespace: str = None,
        *,
        ignore_version: bool = False,
        _credentials: Optional[grpc.ChannelCredentials] = None,
        ray_init_kwargs: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    if ray.is_connected():
        raise RuntimeError("Ray Client is already connected. Maybe you called "
                           'ray.init("ray://<address>") twice by accident?')

    # Enable the same hooks that RAY_CLIENT_MODE does, as calling
    # ray.init("ray://<address>") is specifically for using client mode.
    _set_client_hook_status(True)
    _explicitly_enable_client_mode()

    # TODO(barakmich): https://github.com/ray-project/ray/issues/13274
    # for supporting things like cert_path, ca_path, etc and creating
    # the correct metadata
    conn = ray.connect(
        conn_str,
        job_config=job_config,
        secure=secure,
        metadata=metadata,
        connection_retries=connection_retries,
        namespace=namespace,
        ignore_version=ignore_version,
        _credentials=_credentials,
        ray_init_kwargs=ray_init_kwargs,
    )
    return conn
예제 #3
0
def test_pretask_posttask_shared_state_multi_client(ray_start_regular_shared):
    """
    Repeat the last test with Ray client.
    """
    class PretaskPosttaskCallback(RayDaskCallback):
        def __init__(self, suffix):
            self.suffix = suffix

        def _ray_pretask(self, key, object_refs):
            return key + self.suffix

        def _ray_posttask(self, key, result, pre_state):
            assert pre_state == key + self.suffix

    class PretaskOnlyCallback(RayDaskCallback):
        def _ray_pretask(self, key, object_refs):
            return "baz"

    class PosttaskOnlyCallback(RayDaskCallback):
        def _ray_posttask(self, key, result, pre_state):
            assert pre_state is None

    cb1 = PretaskPosttaskCallback("foo")
    cb2 = PretaskOnlyCallback()
    cb3 = PosttaskOnlyCallback()
    cb4 = PretaskPosttaskCallback("bar")
    with ray_start_client_server():
        _explicitly_enable_client_mode()
        with cb1, cb2, cb3, cb4:
            z = add(2, 3)
            result = z.compute(scheduler=ray_dask_get)

    assert result == 5
예제 #4
0
def test_rllib_integration(ray_start_regular_shared):
    with ray_start_client_server():
        # Confirming the behavior of this context manager.
        # (Client mode hook not yet enabled.)
        assert not client_mode_should_convert()
        # Need to enable this for client APIs to be used.
        _explicitly_enable_client_mode()
        # Confirming mode hook is enabled.
        assert client_mode_should_convert()

        rock_paper_scissors_multiagent.main()
예제 #5
0
def test_client_gpu_ids(call_ray_stop_only):
    import ray
    ray.init(num_cpus=2)

    _explicitly_enable_client_mode()
    # No client connection.
    with pytest.raises(Exception) as e:
        ray.get_gpu_ids()
    assert str(e.value) == "Ray Client is not connected."\
        " Please connect by calling `ray.connect`."

    with ray_start_client_server():
        # Now have a client connection.
        assert ray.get_gpu_ids() == []
예제 #6
0
async def test_serve_handle(ray_start_regular_shared):
    with ray_start_client_server() as ray:
        from ray import serve
        _explicitly_enable_client_mode()
        serve.start()

        @serve.deployment
        def hello():
            return "hello"

        hello.deploy()
        handle = hello.get_handle()
        assert ray.get(handle.remote()) == "hello"
        assert await handle.remote() == "hello"
예제 #7
0
def connect(conn_str: str,
            secure: bool = False,
            metadata: List[Tuple[str, str]] = None) -> None:
    if ray.is_connected():
        raise RuntimeError("Ray Client is already connected. "
                           "Maybe you called ray.util.connect twice by "
                           "accident?")
    # Enable the same hooks that RAY_CLIENT_MODE does, as
    # calling ray.util.connect() is specifically for using client mode.
    _enable_client_hook(True)
    _explicitly_enable_client_mode()

    # TODO(barakmich): https://github.com/ray-project/ray/issues/13274
    # for supporting things like cert_path, ca_path, etc and creating
    # the correct metadata
    return ray.connect(conn_str, secure=secure, metadata=metadata)
async def test_serve_handle(ray_start_regular_shared):
    with ray_start_client_server() as ray:
        from ray import serve
        _explicitly_enable_client_mode()
        serve.start(detached=True)

        def hello(request):
            return "hello"

        serve.create_backend("my_backend", hello, config={"num_replicas": 1})
        serve.create_endpoint("my_endpoint",
                              backend="my_backend",
                              route="/hello")
        handle = serve.get_handle("my_endpoint")
        assert ray.get(handle.remote()) == "hello"
        assert await handle.remote() == "hello"
예제 #9
0
    def connect(
        self,
        conn_str: str,
        job_config: JobConfig = None,
        secure: bool = False,
        metadata: List[Tuple[str, str]] = None,
        connection_retries: int = 3,
        namespace: str = None,
        *,
        ignore_version: bool = False,
        _credentials: Optional[grpc.ChannelCredentials] = None,
        ray_init_kwargs: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """Connect the Ray Client to a server.

        Args:
            conn_str: Connection string, in the form "[host]:port"
            job_config: The job config of the server.
            secure: Whether to use a TLS secured gRPC channel
            metadata: gRPC metadata to send on connect
            connection_retries: number of connection attempts to make
            ignore_version: whether to ignore Python or Ray version mismatches.
                This should only be used for debugging purposes.

        Returns:
            Dictionary of connection info, e.g., {"num_clients": 1}.
        """
        # Delay imports until connect to avoid circular imports.
        from ray.util.client.worker import Worker

        if self.client_worker is not None:
            if self._connected_with_init:
                return
            raise Exception("ray.init() called, but ray client is already connected")
        if not self._inside_client_test:
            # If we're calling a client connect specifically and we're not
            # currently in client mode, ensure we are.
            _explicitly_enable_client_mode()
        if namespace is not None:
            job_config = job_config or JobConfig()
            job_config.set_ray_namespace(namespace)

        logging_level = ray_constants.LOGGER_LEVEL
        logging_format = ray_constants.LOGGER_FORMAT

        if ray_init_kwargs is not None:
            if ray_init_kwargs.get("logging_level") is not None:
                logging_level = ray_init_kwargs["logging_level"]
            if ray_init_kwargs.get("logging_format") is not None:
                logging_format = ray_init_kwargs["logging_format"]

        setup_logger(logging_level, logging_format)

        try:
            self.client_worker = Worker(
                conn_str,
                secure=secure,
                _credentials=_credentials,
                metadata=metadata,
                connection_retries=connection_retries,
            )
            self.api.worker = self.client_worker
            self.client_worker._server_init(job_config, ray_init_kwargs)
            conn_info = self.client_worker.connection_info()
            self._check_versions(conn_info, ignore_version)
            self._register_serializers()
            return conn_info
        except Exception:
            self.disconnect()
            raise
예제 #10
0
    def connect(self,
                conn_str: str,
                job_config: JobConfig = None,
                secure: bool = False,
                metadata: List[Tuple[str, str]] = None,
                connection_retries: int = 3,
                namespace: str = None,
                *,
                ignore_version: bool = False,
                _credentials: Optional[grpc.ChannelCredentials] = None,
                ray_init_kwargs: Optional[Dict[str, Any]] = None
                ) -> Dict[str, Any]:
        """Connect the Ray Client to a server.

        Args:
            conn_str: Connection string, in the form "[host]:port"
            job_config: The job config of the server.
            secure: Whether to use a TLS secured gRPC channel
            metadata: gRPC metadata to send on connect
            connection_retries: number of connection attempts to make
            ignore_version: whether to ignore Python or Ray version mismatches.
                This should only be used for debugging purposes.

        Returns:
            Dictionary of connection info, e.g., {"num_clients": 1}.
        """
        # Delay imports until connect to avoid circular imports.
        from ray.util.client.worker import Worker
        if self.client_worker is not None:
            if self._connected_with_init:
                return
            raise Exception(
                "ray.init() called, but ray client is already connected")
        if not self._inside_client_test:
            # If we're calling a client connect specifically and we're not
            # currently in client mode, ensure we are.
            _explicitly_enable_client_mode()
        if namespace is not None:
            job_config = job_config or JobConfig()
            job_config.set_ray_namespace(namespace)
        if job_config is not None:
            runtime_env = json.loads(job_config.get_serialized_runtime_env())
            if runtime_env.get("pip") or runtime_env.get("conda"):
                logger.warning("The 'pip' or 'conda' field was specified in "
                               "the runtime env, so it may take some time to "
                               "install the environment before Ray connects.")
        try:
            self.client_worker = Worker(
                conn_str,
                secure=secure,
                _credentials=_credentials,
                metadata=metadata,
                connection_retries=connection_retries)
            self.api.worker = self.client_worker
            self.client_worker._server_init(job_config, ray_init_kwargs)
            conn_info = self.client_worker.connection_info()
            self._check_versions(conn_info, ignore_version)
            self._register_serializers()
            return conn_info
        except Exception:
            self.disconnect()
            raise