forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_file.cc
445 lines (390 loc) · 11.9 KB
/
config_file.cc
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright (C) 2017-present ScyllaDB
*
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#include <unordered_map>
#include <yaml-cpp/yaml.h>
#include <boost/program_options.hpp>
#include <boost/any.hpp>
#include <seastar/core/file.hh>
#include <seastar/core/seastar.hh>
#include <seastar/core/smp.hh>
#include <seastar/core/shared_ptr.hh>
#include <seastar/core/fstream.hh>
#include <seastar/core/do_with.hh>
#include <seastar/core/format.hh>
#include <seastar/core/thread.hh>
#include <seastar/util/defer.hh>
#include <seastar/json/json_elements.hh>
#include <ranges>
#include "config_file.hh"
#include "config_file_impl.hh"
namespace bpo = boost::program_options;
template<>
std::istream& std::operator>>(std::istream& is, std::unordered_map<seastar::sstring, seastar::sstring>& map) {
std::istreambuf_iterator<char> i(is), e;
int level = 0;
bool sq = false, dq = false, qq = false;
sstring key, val;
sstring* ps = &key;
auto add = [&] {
if (!key.empty()) {
map[key] = std::move(val);
}
key = {};
val = {};
ps = &key;
};
while (i != e && level >= 0) {
auto c = *i++;
switch (c) {
case '\\':
qq = !qq;
if (qq) {
continue;
}
break;
case '\'':
if (!qq) {
sq = !sq;
}
break;
case '"':
if (!qq) {
dq = !dq;
}
break;
case '=':
if (level <= 1 && !sq && !dq && !qq) {
ps = &val;
continue;
}
break;
case '{': case '[':
if (!sq && !dq && !qq) {
++level;
continue;
}
break;
case ']': case '}':
if (!sq && !dq && !qq && level > 0) {
--level;
continue;
}
break;
case ',':
if (level == 1 && !sq && !dq && !qq) {
add();
continue;
}
break;
case ' ': case '\t': case '\n':
if (!sq && !dq && !qq) {
continue;
}
break;
default:
break;
}
if (level == 0) {
++level;
}
qq = false;
ps->append(&c, 1);
}
add();
return is;
}
template<>
std::istream& std::operator>>(std::istream& is, std::vector<seastar::sstring>& res) {
std::istreambuf_iterator<char> i(is), e;
int level = 0;
bool sq = false, dq = false, qq = false;
sstring val;
auto add = [&] {
if (!val.empty()) {
res.emplace_back(std::exchange(val, {}));
}
val = {};
};
while (i != e && level >= 0) {
auto c = *i++;
switch (c) {
case '\\':
qq = !qq;
if (qq) {
continue;
}
break;
case '\'':
if (!qq) {
sq = !sq;
}
break;
case '"':
if (!qq) {
dq = !dq;
}
break;
case '{': case '[':
if (!sq && !dq && !qq) {
++level;
continue;
}
break;
case '}': case ']':
if (!sq && !dq && !qq && level > 0) {
--level;
continue;
}
break;
case ',':
if (level == 1 && !sq && !dq && !qq) {
add();
continue;
}
break;
case ' ': case '\t': case '\n':
if (!sq && !dq && !qq) {
continue;
}
break;
default:
break;
}
if (level == 0) {
++level;
}
qq = false;
val.append(&c, 1);
}
add();
return is;
}
thread_local unsigned utils::config_file::s_shard_id = 0;
json::json_return_type
utils::config_type::to_json(const void* value) const {
return _to_json(value);
}
bool
utils::config_file::config_src::matches(std::string_view name) const {
// The below line provides support for option names in the "long_name,short_name" format,
// such as "workdir,W". We only want the long name ("workdir") to be used in the YAML.
// But since at some point (due to a bug) the YAML config parser expected the silly
// double form ("workdir,W") instead, we support both for backward compatibility.
std::string_view long_name = _name.substr(0, _name.find_first_of(','));
if (_name == name || long_name == name) {
return true;
}
if (!_alias.empty() && _alias == name) {
return true;
}
return false;
}
json::json_return_type
utils::config_file::config_src::value_as_json() const {
return _type->to_json(current_value());
}
sstring utils::hyphenate(const std::string_view& v) {
sstring result(v.begin(), v.end());
std::replace(result.begin(), result.end(), '_', '-');
return result;
}
utils::config_file::config_file(std::initializer_list<cfg_ref> cfgs)
: _cfgs(cfgs)
{}
void utils::config_file::add(cfg_ref cfg, std::unique_ptr<any_value> value) {
if (_per_shard_values.size() != 1) {
throw std::runtime_error("Can only add config_src to config_file during initialization");
}
_cfgs.emplace_back(cfg);
auto undo = defer([&] { _cfgs.pop_back(); });
cfg.get()._per_shard_values_offset = _per_shard_values[0].size();
_per_shard_values[0].emplace_back(std::move(value));
undo.cancel();
}
void utils::config_file::add(std::initializer_list<cfg_ref> cfgs) {
_cfgs.insert(_cfgs.end(), cfgs.begin(), cfgs.end());
}
void utils::config_file::add(const std::vector<cfg_ref> & cfgs) {
_cfgs.insert(_cfgs.end(), cfgs.begin(), cfgs.end());
}
bpo::options_description utils::config_file::get_options_description() {
bpo::options_description opts("");
return get_options_description(opts);
}
bpo::options_description utils::config_file::get_options_description(boost::program_options::options_description opts) {
auto init = opts.add_options();
add_options(init);
return opts;
}
bpo::options_description_easy_init&
utils::config_file::add_options(bpo::options_description_easy_init& init) {
for (config_src& src : _cfgs) {
if (src.status() == value_status::Used) {
src.add_command_line_option(init);
}
}
return init;
}
bpo::options_description_easy_init&
utils::config_file::add_deprecated_options(bpo::options_description_easy_init& init) {
for (config_src& src : _cfgs) {
if (src.status() == value_status::Deprecated) {
src.add_command_line_option(init);
}
}
return init;
}
void utils::config_file::read_from_yaml(const sstring& yaml, error_handler h) {
read_from_yaml(yaml.c_str(), std::move(h));
}
void utils::config_file::read_from_yaml(const char* yaml, error_handler h) {
std::unordered_map<sstring, cfg_ref> values;
if (!h) {
h = [](auto & opt, auto & msg, auto) {
throw std::invalid_argument(msg + " : " + opt);
};
}
/*
* Note: this is not very "half-fault" tolerant. I.e. there could be
* yaml syntax errors that origin handles and still sets the options
* where as we don't...
* There are no exhaustive attempts at converting, we rely on syntax of
* file mapping to the data type...
*/
auto doc = YAML::Load(yaml);
for (auto node : doc) {
auto label = node.first.as<sstring>();
auto i = std::find_if(_cfgs.begin(), _cfgs.end(), [&label](const config_src& cfg) { return cfg.matches(label); });
if (i == _cfgs.end()) {
h(label, "Unknown option", std::nullopt);
continue;
}
config_src& cfg = *i;
if (cfg.source() > config_source::SettingsFile) {
// already set
continue;
}
switch (cfg.status()) {
case value_status::Deprecated:
h(label, "Option is deprecated", cfg.status());
continue;
case value_status::Invalid:
h(label, "Option is not applicable", cfg.status());
continue;
case value_status::Unused:
default:
break;
}
if (node.second.IsNull()) {
continue;
}
// Still, a syntax error is an error warning, not a fail
try {
cfg.set_value(node.second);
} catch (std::exception& e) {
h(label, e.what(), cfg.status());
} catch (...) {
h(label, "Could not convert value", cfg.status());
}
}
}
utils::config_file::configs utils::config_file::set_values() const {
return _cfgs | std::views::filter([] (const config_src& cfg) {
return cfg.status() > value_status::Used || cfg.source() > config_source::None;
}) | std::ranges::to<configs>();
}
utils::config_file::configs utils::config_file::unset_values() const {
configs res;
for (config_src& cfg : _cfgs) {
if (cfg.status() > value_status::Used) {
continue;
}
if (cfg.source() > config_source::None) {
continue;
}
res.emplace_back(cfg);
}
return res;
}
future<> utils::config_file::read_from_file(file f, error_handler h) {
return f.size().then([this, f, h](size_t s) {
return do_with(make_file_input_stream(f), [this, s, h](input_stream<char>& in) {
return in.read_exactly(s).then([this, h](temporary_buffer<char> buf) {
read_from_yaml(sstring(buf.begin(), buf.end()), h);
});
});
});
}
future<> utils::config_file::read_from_file(const sstring& filename, error_handler h) {
return open_file_dma(filename, open_flags::ro).then([this, h](file f) {
return read_from_file(std::move(f), h);
});
}
future<> utils::config_file::broadcast_to_all_shards() {
return async([this] {
if (_per_shard_values.size() != smp::count) {
_per_shard_values.resize(smp::count);
smp::invoke_on_all([this] {
auto cpu = this_shard_id();
if (cpu != 0) {
s_shard_id = cpu;
auto& shard_0_values = _per_shard_values[0];
auto nr_values = shard_0_values.size();
auto& this_shard_values = _per_shard_values[cpu];
this_shard_values.resize(nr_values);
for (size_t i = 0; i != nr_values; ++i) {
this_shard_values[i] = shard_0_values[i]->clone();
}
}
}).get();
} else {
smp::invoke_on_all([this] {
if (s_shard_id != 0) {
auto& shard_0_values = _per_shard_values[0];
auto nr_values = shard_0_values.size();
auto& this_shard_values = _per_shard_values[s_shard_id];
for (size_t i = 0; i != nr_values; ++i) {
this_shard_values[i]->update_from(shard_0_values[i].get());
}
}
}).get();
}
// #4713
// We can have values retained that are not pointing to
// our storage (extension config). Need to broadcast
// these configs as well.
std::set<config_file *> files;
for (config_src& v : _cfgs) {
auto f = v.get_config_file();
if (f != this) {
files.insert(f);
}
}
for (auto* f : files) {
f->broadcast_to_all_shards().get();
}
});
}
sstring utils::config_file::config_src::source_name() const noexcept {
auto src = source();
switch (src) {
case utils::config_file::config_source::None:
return "default";
case utils::config_file::config_source::SettingsFile:
return "config";
case utils::config_file::config_source::CommandLine:
return "cli";
case utils::config_file::config_source::Internal:
return "internal";
case utils::config_file::config_source::CQL:
return "cql";
case utils::config_file::config_source::API:
return "api";
}
__builtin_unreachable();
}