forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_file_impl.hh
252 lines (202 loc) · 7.1 KB
/
config_file_impl.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright (C) 2015-present ScyllaDB
*
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <boost/any.hpp>
#include <boost/regex.hpp>
#include <boost/program_options/errors.hpp>
#include <boost/program_options/value_semantic.hpp>
#include <boost/lexical_cast.hpp>
#include <yaml-cpp/node/convert.h>
#include <seastar/core/smp.hh>
#include "config_file.hh"
namespace utils {
template <typename T>
T config_from_string(std::string_view string_representation) {
return boost::lexical_cast<T>(string_representation);
}
template <>
bool config_from_string(std::string_view string_representation);
template <>
sstring config_from_string(std::string_view string_representation);
}
namespace YAML {
/*
* Add converters as needed here...
*
* TODO: Maybe we should just define all node conversions as "lexical_cast".
* However, vanilla yamp-cpp does some special treatment of scalar types,
* mainly inf handling etc. Hm.
*/
template<>
struct convert<seastar::sstring> {
static bool decode(const Node& node, sstring& rhs) {
std::string tmp;
if (!convert<std::string>::decode(node, tmp)) {
return false;
}
rhs = tmp;
return true;
}
};
template<typename K, typename V, typename... Rest>
struct convert<std::unordered_map<K, V, Rest...>> {
using map_type = std::unordered_map<K, V, Rest...>;
static bool decode(const Node& node, map_type& rhs) {
if (!node.IsMap()) {
return false;
}
rhs.clear();
for (auto& n : node) {
rhs[n.first.as<K>()] = n.second.as<V>();
}
return true;
}
};
}
namespace std {
template<typename K, typename V, typename... Args>
std::istream& operator>>(std::istream&, std::unordered_map<K, V, Args...>&);
template<>
std::istream& operator>>(std::istream&, std::unordered_map<seastar::sstring, seastar::sstring>&);
template<typename V, typename... Args>
std::istream& operator>>(std::istream&, std::vector<V, Args...>&);
template<>
std::istream& operator>>(std::istream&, std::vector<seastar::sstring>&);
extern template
std::istream& operator>>(std::istream&, std::vector<seastar::sstring>&);
template<typename K, typename V, typename... Args>
std::istream& operator>>(std::istream& is, std::unordered_map<K, V, Args...>& map) {
std::unordered_map<sstring, sstring> tmp;
is >> tmp;
for (auto& p : tmp) {
map[utils::config_from_string<K>(p.first)] = utils::config_from_string<V>(p.second);
}
return is;
}
template<typename V, typename... Args>
std::istream& operator>>(std::istream& is, std::vector<V, Args...>& dst) {
std::vector<seastar::sstring> tmp;
is >> tmp;
for (auto& v : tmp) {
dst.emplace_back(utils::config_from_string<V>(v));
}
return is;
}
template<typename K, typename V, typename... Args>
void validate(boost::any& out, const std::vector<std::string>& in, std::unordered_map<K, V, Args...>*, int utf8) {
using map_type = std::unordered_map<K, V, Args...>;
if (out.empty()) {
out = boost::any(map_type());
}
static const boost::regex key(R"foo((?:^|\:)([^=:]+)=)foo");
auto* p = boost::any_cast<map_type>(&out);
for (const auto& s : in) {
boost::sregex_iterator i(s.begin(), s.end(), key), e;
if (i == e) {
throw boost::program_options::invalid_option_value(s);
}
while (i != e) {
auto k = (*i)[1].str();
auto vs = s.begin() + i->position() + i->length();
auto ve = s.end();
if (++i != e) {
ve = s.begin() + i->position();
}
(*p)[utils::config_from_string<K>(k)] = utils::config_from_string<V>(sstring(vs, ve));
}
}
}
}
namespace utils {
/*
* Our own bpo::typed_valye.
* Only difference is that we _don't_ apply defaults (they are already applied)
* Needed to make aliases work properly.
*/
template<class T, class charT = char>
class typed_value_ex : public bpo::typed_value<T, charT> {
public:
typedef bpo::typed_value<T, charT> _Super;
typed_value_ex()
: _Super(nullptr)
{}
bool apply_default(boost::any& value_store) const override {
return false;
}
};
template <typename T>
void maybe_multitoken(typed_value_ex<T>* r) {
}
template <typename T>
void maybe_multitoken(std::vector<typed_value_ex<T>>* r) {
r->multitoken();
}
template<class T>
inline typed_value_ex<T>* value_ex() {
typed_value_ex<T>* r = new typed_value_ex<T>();
maybe_multitoken(r);
return r;
}
sstring hyphenate(const std::string_view&);
}
template<typename T>
void utils::config_file::named_value<T>::add_command_line_option(boost::program_options::options_description_easy_init& init) {
const auto hyphenated_name = hyphenate(name());
// NOTE. We are not adding default values. We could, but must in that case manually (in some way) generate the textual
// version, since the available ostream operators for things like pairs and collections don't match what we can deal with parser-wise.
// See removed ostream operators above.
init(hyphenated_name.data(), value_ex<T>()->notifier([this](T new_val) {
try {
set(std::move(new_val), config_source::CommandLine);
} catch (const std::invalid_argument& e) {
throw bpo::invalid_option_value(e.what());
}
}), desc().data());
if (!alias().empty()) {
const auto alias_desc = fmt::format("Alias for {}", hyphenated_name);
init(hyphenate(alias()).data(), value_ex<T>()->notifier([this](T new_val) { set(std::move(new_val), config_source::CommandLine); }), alias_desc.data());
}
}
template<typename T>
void utils::config_file::named_value<T>::set_value(const YAML::Node& node) {
if (_source == config_source::SettingsFile && _liveness != liveness::LiveUpdate) {
// FIXME: warn if different?
return;
}
(*this)(node.as<T>());
_source = config_source::SettingsFile;
}
template<typename T>
bool utils::config_file::named_value<T>::set_value(sstring value, config_source src) {
if ((_liveness != liveness::LiveUpdate) || (src == config_source::CQL && !_cf->are_live_updatable_config_params_changeable_via_cql())) {
return false;
}
(*this)(config_from_string<T>(value), src);
return true;
}
template<typename T>
future<> utils::config_file::named_value<T>::set_value_on_all_shards(const YAML::Node& node) {
if (_source == config_source::SettingsFile && _liveness != liveness::LiveUpdate) {
// FIXME: warn if different?
co_return;
}
co_await smp::invoke_on_all([this, value = node.as<T>()] () {
(*this)(value);
});
_source = config_source::SettingsFile;
}
template<typename T>
future<bool> utils::config_file::named_value<T>::set_value_on_all_shards(sstring value, config_source src) {
if ((_liveness != liveness::LiveUpdate) || (src == config_source::CQL && !_cf->are_live_updatable_config_params_changeable_via_cql())) {
co_return false;
}
co_await smp::invoke_on_all([this, value = config_from_string<T>(value), src] () {
(*this)(value, src);
});
co_return true;
}