diff --git a/src/ray/common/BUILD b/src/ray/common/BUILD index bc614883e57f9..6c359c7ae1589 100644 --- a/src/ray/common/BUILD +++ b/src/ray/common/BUILD @@ -127,6 +127,8 @@ ray_cc_library( "common_protocol.h", "id.h", "id_def.h", + "simple_id.h", + "virtual_cluster_id.h", ], deps = [ ":constants", diff --git a/src/ray/common/simple_id.h b/src/ray/common/simple_id.h new file mode 100644 index 0000000000000..50451cc35f4e7 --- /dev/null +++ b/src/ray/common/simple_id.h @@ -0,0 +1,48 @@ +// Copyright 2017 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/common/id.h" + +namespace ray { + +template +struct SimpleID { + static T FromBinary(const std::string &binary) { + T id; + id.id_ = binary; + return id; + } + + size_t Hash() const { + // Note(ashione): hash code lazy calculation(it's invoked every time if hash code is + // default value 0) + if (!hash_) { + hash_ = MurmurHash64A(id_.data(), id_.size(), 0); + } + return hash_; + } + + const std::string &Binary() const { return id_; } + + bool operator==(const T &rhs) const { return id_ == rhs.id_; } + bool operator!=(const T &rhs) const { return !(*this == rhs); } + + private: + std::string id_; + mutable size_t hash_ = 0; +}; + +} // namespace ray diff --git a/src/ray/common/virtual_cluster_id.h b/src/ray/common/virtual_cluster_id.h new file mode 100644 index 0000000000000..9464bc9920fcb --- /dev/null +++ b/src/ray/common/virtual_cluster_id.h @@ -0,0 +1,43 @@ +// Copyright 2017 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/common/simple_id.h" + +namespace ray { + +class VirtualClusterID : public SimpleID { + using SimpleID::SimpleID; +}; + +inline std::ostream &operator<<(std::ostream &os, const ray::VirtualClusterID &id) { + os << id.Binary(); + return os; +} + +} // namespace ray + +namespace std { + +template <> +struct hash { + size_t operator()(const ray::VirtualClusterID &id) const { return id.Hash(); } +}; +template <> +struct hash { + size_t operator()(const ray::VirtualClusterID &id) const { return id.Hash(); } +}; + +} // namespace std diff --git a/src/ray/gcs/gcs_server/gcs_table_storage.cc b/src/ray/gcs/gcs_server/gcs_table_storage.cc index 3b30f2cb7fb41..4250d76d5c902 100644 --- a/src/ray/gcs/gcs_server/gcs_table_storage.cc +++ b/src/ray/gcs/gcs_server/gcs_table_storage.cc @@ -213,6 +213,7 @@ template class GcsTable; template class GcsTableWithJobId; template class GcsTableWithJobId; template class GcsTable; +template class GcsTable; } // namespace gcs } // namespace ray diff --git a/src/ray/gcs/gcs_server/gcs_table_storage.h b/src/ray/gcs/gcs_server/gcs_table_storage.h index f6d89c9cc4378..c233ea2dfb774 100644 --- a/src/ray/gcs/gcs_server/gcs_table_storage.h +++ b/src/ray/gcs/gcs_server/gcs_table_storage.h @@ -18,6 +18,7 @@ #include #include "ray/common/asio/instrumented_io_context.h" +#include "ray/common/virtual_cluster_id.h" #include "ray/gcs/store_client/in_memory_store_client.h" #include "ray/gcs/store_client/observable_store_client.h" #include "ray/gcs/store_client/redis_store_client.h" @@ -33,6 +34,7 @@ using rpc::JobTableData; using rpc::PlacementGroupTableData; using rpc::ResourceUsageBatchData; using rpc::TaskSpec; +using rpc::VirtualClusterTableData; using rpc::WorkerTableData; /// \class GcsTable @@ -208,6 +210,15 @@ class GcsWorkerTable : public GcsTable { } }; +class GcsVirtualClusterTable + : public GcsTable { + public: + explicit GcsVirtualClusterTable(std::shared_ptr store_client) + : GcsTable(std::move(store_client)) { + table_name_ = TablePrefix_Name(TablePrefix::VIRTUAL_CLUSTER); + } +}; + /// \class GcsTableStorage /// /// This class is not meant to be used directly. All gcs table storage classes should @@ -222,6 +233,7 @@ class GcsTableStorage { placement_group_table_ = std::make_unique(store_client_); node_table_ = std::make_unique(store_client_); worker_table_ = std::make_unique(store_client_); + virtual_cluster_table_ = std::make_unique(store_client_); } virtual ~GcsTableStorage() = default; @@ -256,6 +268,11 @@ class GcsTableStorage { return *worker_table_; } + GcsVirtualClusterTable &VirtualClusterTable() { + RAY_CHECK(virtual_cluster_table_ != nullptr); + return *virtual_cluster_table_; + } + int GetNextJobID() { RAY_CHECK(store_client_); return store_client_->GetNextJobID(); @@ -269,6 +286,7 @@ class GcsTableStorage { std::unique_ptr placement_group_table_; std::unique_ptr node_table_; std::unique_ptr worker_table_; + std::unique_ptr virtual_cluster_table_; }; /// \class RedisGcsTableStorage diff --git a/src/ray/protobuf/gcs.proto b/src/ray/protobuf/gcs.proto index 0dc8b87251739..4a35e5a1476bf 100644 --- a/src/ray/protobuf/gcs.proto +++ b/src/ray/protobuf/gcs.proto @@ -43,6 +43,7 @@ enum TablePrefix { PLACEMENT_GROUP = 17; KV = 18; ACTOR_TASK_SPEC = 19; + VIRTUAL_CLUSTER = 20; } // The channel that Add operations to the Table should be published on, if any.