• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PS_CORE_FILE_CONFIGURATION_H_
18 #define MINDSPORE_CCSRC_PS_CORE_FILE_CONFIGURATION_H_
19 
20 #include <atomic>
21 #include <cstdlib>
22 #include <iostream>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 #include <thread>
27 #include <mutex>
28 #include <unordered_map>
29 
30 #include "utils/hash_map.h"
31 #include "include/common/utils/json_operation_utils.h"
32 #include "include/backend/distributed/ps/constants.h"
33 #include "utils/log_adapter.h"
34 #include "ps/core/comm_util.h"
35 #include "ps/core/configuration.h"
36 
37 namespace mindspore {
38 namespace ps {
39 namespace core {
40 // File storage persistent information.
41 // for example
42 //{
43 //   "scheduler_ip": "127.0.0.1",
44 //   "scheduler_port": 1,
45 //   "worker_num": 8,
46 //   "server_num": 16,
47 //   "total_node_num": 16
48 //}
49 class FileConfiguration : public Configuration {
50  public:
FileConfiguration(const std::string & path)51   explicit FileConfiguration(const std::string &path) : file_path_(path), is_initialized_(false) {}
52   ~FileConfiguration() = default;
53 
54   bool Initialize() override;
55 
56   bool IsInitialized() const override;
57 
58   std::string Get(const std::string &key, const std::string &defaultvalue) const override;
59 
60   std::string GetString(const std::string &key, const std::string &defaultvalue) const override;
61 
62   std::vector<nlohmann::json> GetVector(const std::string &key) const override;
63 
64   int64_t GetInt(const std::string &key, int64_t default_value) const override;
65 
66   template <typename T>
GetValue(const std::string & key)67   T GetValue(const std::string &key) const {
68     if (!js.contains(key)) {
69       MS_LOG(EXCEPTION) << "The key:" << key << " is not exist.";
70     }
71 
72     return GetJsonValue<T>(js, key);
73   }
74 
75   void Put(const std::string &key, const std::string &value) override;
76 
77   template <typename T>
PutValue(const std::string & key,const T & value)78   void PutValue(const std::string &key, const T &value) {
79     std::ofstream output_file(file_path_);
80     js[key] = value;
81     output_file << js.dump();
82     output_file.close();
83   }
84 
85   bool Exists(const std::string &key) const override;
86 
87   void PersistFile(const core::ClusterConfig &clusterConfig) const override;
88 
89   void PersistNodes(const core::ClusterConfig &clusterConfig) const override;
90 
91   std::string file_path() const override;
92 
93  private:
94   // The path of the configuration file.
95   std::string file_path_;
96 
97   nlohmann::json js;
98 
99   std::atomic<bool> is_initialized_;
100 };
101 }  // namespace core
102 }  // namespace ps
103 }  // namespace mindspore
104 #endif  // MINDSPORE_CCSRC_PS_CORE_FILE_CONFIGURATION_H_
105