1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_FL_SERVER_LOCAL_META_STORE_H_ 18 #define MINDSPORE_CCSRC_FL_SERVER_LOCAL_META_STORE_H_ 19 20 #include <any> 21 #include <mutex> 22 #include <string> 23 #include <unordered_map> 24 #include "fl/server/common.h" 25 26 namespace mindspore { 27 namespace fl { 28 namespace server { 29 // LocalMetaStore class is used for metadata storage of this server process. 30 // For example, the current iteration number, time windows for round kernels, etc. 31 // LocalMetaStore is threadsafe. 32 class LocalMetaStore { 33 public: GetInstance()34 static LocalMetaStore &GetInstance() { 35 static LocalMetaStore instance; 36 return instance; 37 } 38 39 template <typename T> put_value(const std::string & name,const T & value)40 void put_value(const std::string &name, const T &value) { 41 std::unique_lock<std::mutex> lock(mtx_); 42 key_to_meta_[name] = value; 43 } 44 45 template <typename T> value(const std::string & name)46 T value(const std::string &name) { 47 std::unique_lock<std::mutex> lock(mtx_); 48 try { 49 T value = std::any_cast<T>(key_to_meta_[name]); 50 return value; 51 } catch (const std::exception &e) { 52 MS_LOG(EXCEPTION) << "Value of " << name << " is not set."; 53 } 54 } 55 56 // This method returns a reference so that user can change this value without calling put_value. 57 template <typename T> mutable_value(const std::string & name)58 T &mutable_value(const std::string &name) { 59 std::unique_lock<std::mutex> lock(mtx_); 60 try { 61 return std::any_cast<T &>(key_to_meta_[name]); 62 } catch (const std::exception &e) { 63 MS_LOG(EXCEPTION) << "Value of " << name << " is not set."; 64 } 65 } 66 67 void remove_value(const std::string &name); 68 bool has_value(const std::string &name); 69 70 void set_curr_iter_num(size_t num); 71 const size_t curr_iter_num(); 72 73 private: LocalMetaStore()74 LocalMetaStore() : key_to_meta_({}), curr_iter_num_(0) {} 75 ~LocalMetaStore() = default; 76 LocalMetaStore(const LocalMetaStore &) = delete; 77 LocalMetaStore &operator=(const LocalMetaStore &) = delete; 78 79 // key_to_meta_ stores metadata with key-value format. 80 std::unordered_map<std::string, std::any> key_to_meta_; 81 // This mutex makes sure that the operations on key_to_meta_ is threadsafe. 82 std::mutex mtx_; 83 size_t curr_iter_num_; 84 }; 85 } // namespace server 86 } // namespace fl 87 } // namespace mindspore 88 #endif // MINDSPORE_CCSRC_FL_SERVER_LOCAL_META_STORE_H_ 89