Loading...
Searching...
No Matches
resdb_config_utils.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <fcntl.h>
23#include <glog/logging.h>
24#include <google/protobuf/util/json_util.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28
29#include <fstream>
30
31namespace resdb {
32
33using ::google::protobuf::util::JsonParseOptions;
34
35namespace {
36
37KeyInfo ReadKey(const std::string& file_name) {
38 int fd = open(file_name.c_str(), O_RDONLY, 0666);
39 if (fd < 0) {
40 LOG(ERROR) << "open file:" << file_name << " fail:" << strerror(errno);
41 }
42 assert(fd >= 0);
43
44 std::string res;
45 int read_len = 0;
46 char tmp[1024];
47 while (true) {
48 read_len = read(fd, tmp, sizeof(tmp));
49 if (read_len <= 0) {
50 break;
51 }
52 res.append(tmp, read_len);
53 }
54 close(fd);
55 KeyInfo key;
56 assert(key.ParseFromString(res));
57 return key;
58}
59
60CertificateInfo ReadCert(const std::string& file_name) {
61 int fd = open(file_name.c_str(), O_RDONLY, 0666);
62 if (fd < 0) {
63 LOG(ERROR) << "open file:" << file_name << " fail" << strerror(errno);
64 }
65 assert(fd >= 0);
66
67 std::string res;
68 int read_len = 0;
69 char tmp[1024];
70 while (true) {
71 read_len = read(fd, tmp, sizeof(tmp));
72 if (read_len <= 0) {
73 break;
74 }
75 res.append(tmp, read_len);
76 }
77 close(fd);
78 CertificateInfo info;
79 assert(info.ParseFromString(res));
80 return info;
81}
82
83} // namespace
84
85ReplicaInfo GenerateReplicaInfo(int id, const std::string& ip, int port) {
86 ReplicaInfo info;
87 info.set_id(id);
88 info.set_ip(ip);
89 info.set_port(port);
90 return info;
91}
92
93ResConfigData ReadConfigFromFile(const std::string& file_name) {
94 std::stringstream json_data;
95 std::ifstream infile(file_name.c_str());
96 json_data << infile.rdbuf();
97
98 ResConfigData config_data;
99 JsonParseOptions options;
100 auto status = JsonStringToMessage(json_data.str(), &config_data, options);
101 if (!status.ok()) {
102 LOG(ERROR) << "parse json :" << file_name << " fail:" << status.message();
103 }
104 assert(status.ok());
105 return config_data;
106}
107
108std::vector<ReplicaInfo> ReadConfig(const std::string& file_name) {
109 std::vector<ReplicaInfo> replicas;
110 std::string line;
111 std::ifstream infile(file_name.c_str());
112 int id;
113 std::string ip;
114 int port;
115 while (infile >> id >> ip >> port) {
116 replicas.push_back(GenerateReplicaInfo(id, ip, port));
117 }
118 if (replicas.size() == 0) {
119 LOG(ERROR) << "read config:" << file_name << " fail.";
120 assert(replicas.size() > 0);
121 }
122 return replicas;
123}
124
125std::unique_ptr<ResDBConfig> GenerateResDBConfig(
126 const std::string& config_file, const std::string& private_key_file,
127 const std::string& cert_file, std::optional<ReplicaInfo> self_info,
128 std::optional<ConfigGenFunc> gen_func) {
129 ResConfigData config_data = ReadConfigFromFile(config_file);
130 KeyInfo private_key = ReadKey(private_key_file);
131 CertificateInfo cert_info = ReadCert(cert_file);
132
133 LOG(ERROR) << "private key:" << private_key.DebugString()
134 << " cert:" << cert_info.DebugString();
135 if (!self_info.has_value()) {
136 self_info = ReplicaInfo();
137 }
138
139 (*self_info).set_id(cert_info.public_key().public_key_info().node_id());
140 (*self_info).set_ip(cert_info.public_key().public_key_info().ip());
141 (*self_info).set_port(cert_info.public_key().public_key_info().port());
142
143 *(*self_info).mutable_certificate_info() = cert_info;
144
145 if (gen_func.has_value()) {
146 return (*gen_func)(config_data, self_info.value(), private_key, cert_info);
147 }
148 return std::make_unique<ResDBConfig>(config_data, self_info.value(),
149 private_key, cert_info);
150}
151
152ResDBConfig GenerateResDBConfig(const std::string& config_file) {
153 std::vector<ReplicaInfo> replicas = ReadConfig(config_file);
154 return ResDBConfig(replicas, ReplicaInfo());
155}
156
157} // namespace resdb
KeyInfo ReadKey(const std::string &file_name)
res
Definition addr.py:25
std::vector< ReplicaInfo > ReadConfig(const std::string &file_name)
ReplicaInfo GenerateReplicaInfo(int id, const std::string &ip, int port)
ResConfigData ReadConfigFromFile(const std::string &file_name)
std::unique_ptr< ResDBConfig > GenerateResDBConfig(const std::string &config_file, const std::string &private_key_file, const std::string &cert_file, std::optional< ReplicaInfo > self_info, std::optional< ConfigGenFunc > gen_func)