|
- /**
- * Copyright 2021 Huawei Technologies Co., Ltd
- *
- * 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 "ps/server/consistent_hash_ring.h"
-
- namespace mindspore {
- namespace ps {
- namespace server {
- bool ConsistentHashRing::Insert(uint32_t rank) {
- std::string physical_node_hash_key = std::to_string(rank);
- for (uint32_t i = 0; i < virtual_node_num_; i++) {
- physical_node_hash_key += "#" + std::to_string(i);
- MS_LOG(DEBUG) << "Insert virtual node " << physical_node_hash_key << " for node " << rank;
-
- size_t hash_value = std::hash<std::string>()(physical_node_hash_key);
- if (ring_.count(hash_value) != 0) {
- MS_LOG(WARNING) << "Virtual node " << physical_node_hash_key << " is already mapped to the ring.";
- continue;
- }
- ring_[hash_value] = rank;
- }
- return true;
- }
-
- bool ConsistentHashRing::Erase(uint32_t rank) {
- for (auto iterator = ring_.begin(); iterator != ring_.end();) {
- if (iterator->second == rank) {
- ring_.erase(iterator++);
- }
- }
- return true;
- }
-
- uint32_t ConsistentHashRing::Find(const std::string &key) {
- size_t hash_value = std::hash<std::string>()(key);
- auto iterator = ring_.lower_bound(hash_value);
- if (iterator == ring_.end()) {
- // If the virtual node is not found clockwise, the key will be mapped to the first virtual node on the ring.
- iterator = ring_.begin();
- }
- return iterator->second;
- }
- } // namespace server
- } // namespace ps
- } // namespace mindspore
|