forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[runtime env] plugin refactor [7/n]: support runtime env in C++ API (r…
…ay-project#27010) Signed-off-by: 久龙 <[email protected]>
- Loading branch information
1 parent
5d6bc53
commit 06b0e71
Showing
22 changed files
with
419 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
cc_library( | ||
name = "nlohmann_json", | ||
hdrs = glob([ | ||
"include/**/*.hpp", | ||
"single_include/**/*.hpp", | ||
]), | ||
includes = ["include"], | ||
includes = ["single_include"], | ||
visibility = ["//visibility:public"], | ||
alwayslink = 1, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2022 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 <ray/api/ray_exception.h> | ||
|
||
#include <string> | ||
|
||
#include "nlohmann/json.hpp" | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace ray { | ||
|
||
/// This class provides interfaces of setting runtime environments for job/actor/task. | ||
class RuntimeEnv { | ||
public: | ||
/// Set a runtime env field by name and Object. | ||
/// \param[in] name The runtime env plugin name. | ||
/// \param[in] value An object with primitive data type or jsonable type of | ||
/// nlohmann/json. | ||
template <typename T> | ||
void Set(const std::string &name, const T &value); | ||
|
||
/// Get the object of a runtime env field. | ||
/// \param[in] name The runtime env plugin name. | ||
template <typename T> | ||
T Get(const std::string &name) const; | ||
|
||
/// Set a runtime env field by name and json string. | ||
/// \param[in] name The runtime env plugin name. | ||
/// \param[in] json_str A json string represents the runtime env field. | ||
void SetJsonStr(const std::string &name, const std::string &json_str); | ||
|
||
/// Get the json string of a runtime env field. | ||
/// \param[in] name The runtime env plugin name. | ||
std::string GetJsonStr(const std::string &name) const; | ||
|
||
/// Whether a field is contained. | ||
/// \param[in] name The runtime env plugin name. | ||
bool Contains(const std::string &name) const; | ||
|
||
/// Remove a field by name. | ||
/// \param[in] name The runtime env plugin name. | ||
/// \return true if remove an existing field, otherwise false. | ||
bool Remove(const std::string &name); | ||
|
||
/// Whether the runtime env is empty. | ||
bool Empty() const; | ||
|
||
/// Serialize the runtime env to string. | ||
std::string Serialize() const; | ||
|
||
/// Serialize the runtime env to RuntimeEnvInfo. | ||
std::string SerializeToRuntimeEnvInfo() const; | ||
|
||
/// Deserialize the runtime env from string. | ||
/// \return The deserialized RuntimeEnv instance. | ||
static RuntimeEnv Deserialize(const std::string &serialized_runtime_env); | ||
|
||
private: | ||
json fields_; | ||
}; | ||
|
||
// --------- inline implementation ------------ | ||
|
||
template <typename T> | ||
inline void RuntimeEnv::Set(const std::string &name, const T &value) { | ||
try { | ||
json value_j = value; | ||
fields_[name] = value_j; | ||
} catch (std::exception &e) { | ||
throw ray::internal::RayRuntimeEnvException("Failed to set the field " + name + ": " + | ||
e.what()); | ||
} | ||
} | ||
|
||
template <typename T> | ||
inline T RuntimeEnv::Get(const std::string &name) const { | ||
if (!Contains(name)) { | ||
throw ray::internal::RayRuntimeEnvException("The field " + name + " not found."); | ||
} | ||
try { | ||
return fields_[name].get<T>(); | ||
} catch (std::exception &e) { | ||
throw ray::internal::RayRuntimeEnvException("Failed to get the field " + name + ": " + | ||
e.what()); | ||
} | ||
} | ||
|
||
} // namespace ray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2022 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. | ||
|
||
#include <google/protobuf/util/json_util.h> | ||
#include <ray/api/runtime_env.h> | ||
#include <ray/util/logging.h> | ||
|
||
#include "src/ray/protobuf/runtime_env_common.pb.h" | ||
|
||
namespace ray { | ||
|
||
void RuntimeEnv::SetJsonStr(const std::string &name, const std::string &json_str) { | ||
try { | ||
json value_j = json::parse(json_str); | ||
fields_[name] = value_j; | ||
} catch (std::exception &e) { | ||
throw ray::internal::RayRuntimeEnvException("Failed to set the field " + name + | ||
" by json string: " + e.what()); | ||
} | ||
} | ||
|
||
std::string RuntimeEnv::GetJsonStr(const std::string &name) const { | ||
if (!Contains(name)) { | ||
throw ray::internal::RayRuntimeEnvException("The field " + name + " not found."); | ||
} | ||
auto j = fields_[name].get<json>(); | ||
return j.dump(); | ||
} | ||
|
||
bool RuntimeEnv::Contains(const std::string &name) const { | ||
return fields_.contains(name); | ||
} | ||
|
||
bool RuntimeEnv::Remove(const std::string &name) { | ||
if (Contains(name)) { | ||
fields_.erase(name); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool RuntimeEnv::Empty() const { return fields_.empty(); } | ||
|
||
std::string RuntimeEnv::Serialize() const { return fields_.dump(); } | ||
|
||
std::string RuntimeEnv::SerializeToRuntimeEnvInfo() const { | ||
rpc::RuntimeEnvInfo runtime_env_info; | ||
runtime_env_info.set_serialized_runtime_env(Serialize()); | ||
std::string serialized_runtime_env_info; | ||
RAY_CHECK(google::protobuf::util::MessageToJsonString(runtime_env_info, | ||
&serialized_runtime_env_info) | ||
.ok()); | ||
return serialized_runtime_env_info; | ||
} | ||
|
||
RuntimeEnv RuntimeEnv::Deserialize(const std::string &serialized_runtime_env) { | ||
RuntimeEnv runtime_env; | ||
try { | ||
runtime_env.fields_ = json::parse(serialized_runtime_env); | ||
} catch (std::exception &e) { | ||
throw ray::internal::RayRuntimeEnvException("Failed to deserialize runtime env " + | ||
serialized_runtime_env + ": " + e.what()); | ||
} | ||
return runtime_env; | ||
} | ||
|
||
} // namespace ray |
Oops, something went wrong.