// Copyright 2020-2021 The Ray Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include namespace ray { /// A handle to an actor which can be used to invoke a remote actor method, with the /// `Call` method. /// \param ActorType The type of the concrete actor class. /// Note, the `Call` method is defined in actor_call.generated.h. template class ActorHandle { public: ActorHandle() = default; ActorHandle(const std::string &id) { id_ = id; } // Used to identify its type. static bool IsActorHandle() { return true; } /// Get a untyped ID of the actor const std::string &ID() const { return id_; } /// Include the `Call` methods for calling remote functions. template ray::internal::ActorTaskCaller Task(F actor_func) { static_assert(!IsXlang && !ray::internal::is_python_v, "Actor method is not a member function of actor class."); static_assert(std::is_member_function_pointer_v, "Actor method is not a member function of actor class."); using Self = boost::callable_traits::class_of_t; static_assert( std::is_same::value || std::is_base_of::value, "Class types must be same."); auto func_name = internal::FunctionManager::Instance().GetFunctionName(actor_func); ray::internal::RemoteFunctionHolder remote_func_holder(func_name); return ray::internal::ActorTaskCaller( internal::GetRayRuntime().get(), id_, std::move(remote_func_holder)); } template ray::internal::ActorTaskCaller> Task(PyActorMethod func) { static_assert(IsXlang, "Actor function type does not match actor class"); ray::internal::RemoteFunctionHolder remote_func_holder( "", func.function_name, "", ray::internal::LangType::PYTHON); return {ray::internal::GetRayRuntime().get(), id_, std::move(remote_func_holder)}; } template ray::internal::ActorTaskCaller> Task(JavaActorMethod func) { static_assert(IsXlang, "Actor function type does not match actor class"); ray::internal::RemoteFunctionHolder remote_func_holder( "", func.function_name, "", ray::internal::LangType::JAVA); return {ray::internal::GetRayRuntime().get(), id_, std::move(remote_func_holder)}; } void Kill() { Kill(true); } void Kill(bool no_restart) { ray::internal::GetRayRuntime()->KillActor(id_, no_restart); } static ActorHandle FromBytes(const std::string &serialized_actor_handle) { std::string id = ray::internal::GetRayRuntime()->DeserializeAndRegisterActorHandle( serialized_actor_handle); return ActorHandle(id); } /// Make ActorHandle serializable MSGPACK_DEFINE(id_); private: std::string id_; }; typedef ActorHandle ActorHandleXlang; } // namespace ray