|
18 | 18 | #include <algorithm>
|
19 | 19 | #include <cerrno>
|
20 | 20 | #include <cstring>
|
| 21 | +#include <filesystem> |
21 | 22 | #include <fstream>
|
22 | 23 | #include <iostream>
|
23 | 24 | #include <list>
|
24 | 25 | #include <map>
|
| 26 | +#include <mutex> |
25 | 27 | #include <sstream>
|
26 | 28 |
|
27 | 29 | #define __IN_CONFIGBASE_CPP__
|
|
30 | 32 |
|
31 | 33 | namespace doris::cloud::config {
|
32 | 34 |
|
| 35 | +std::string_view g_conf_path {}; |
| 36 | + |
33 | 37 | std::map<std::string, Register::Field>* Register::_s_field_map = nullptr;
|
34 | 38 | std::map<std::string, std::function<bool()>>* RegisterConfValidator::_s_field_validator = nullptr;
|
35 | 39 | std::map<std::string, std::string>* full_conf_map = nullptr;
|
| 40 | +std::mutex mutable_string_config_lock; |
| 41 | +std::mutex conf_persist_lock; |
36 | 42 |
|
37 | 43 | // trim string
|
38 | 44 | std::string& trim(std::string& s) {
|
@@ -224,6 +230,38 @@ bool Properties::load(const char* conf_file, bool must_exist) {
|
224 | 230 | return true;
|
225 | 231 | }
|
226 | 232 |
|
| 233 | +// dump props to conf file |
| 234 | +bool Properties::dump(const std::string& conffile) { |
| 235 | + std::string conffile_tmp = conffile + ".tmp"; |
| 236 | + |
| 237 | + if (std::filesystem::exists(conffile)) { |
| 238 | + // copy for modify |
| 239 | + std::ifstream in(conffile, std::ios::binary); |
| 240 | + std::ofstream out(conffile_tmp, std::ios::binary); |
| 241 | + out << in.rdbuf(); |
| 242 | + in.close(); |
| 243 | + out.close(); |
| 244 | + } |
| 245 | + |
| 246 | + std::ofstream file_writer; |
| 247 | + |
| 248 | + file_writer.open(conffile_tmp, std::ios::out | std::ios::app); |
| 249 | + |
| 250 | + file_writer << std::endl; |
| 251 | + |
| 252 | + for (auto const& iter : file_conf_map) { |
| 253 | + file_writer << iter.first << " = " << iter.second << std::endl; |
| 254 | + } |
| 255 | + |
| 256 | + file_writer.close(); |
| 257 | + if (!file_writer.good()) { |
| 258 | + return false; |
| 259 | + } |
| 260 | + |
| 261 | + std::filesystem::rename(conffile_tmp, conffile); |
| 262 | + return std::filesystem::exists(conffile); |
| 263 | +} |
| 264 | + |
227 | 265 | template <typename T>
|
228 | 266 | bool Properties::get_or_default(const char* key, const char* defstr, T& retval,
|
229 | 267 | bool* is_retval_set) const {
|
@@ -297,6 +335,37 @@ std::ostream& operator<<(std::ostream& out, const std::vector<T>& v) {
|
297 | 335 | continue; \
|
298 | 336 | }
|
299 | 337 |
|
| 338 | +#define UPDATE_FIELD(FIELD, VALUE, TYPE, PERSIST) \ |
| 339 | + if (strcmp((FIELD).type, #TYPE) == 0) { \ |
| 340 | + TYPE new_value; \ |
| 341 | + if (!convert((VALUE), new_value)) { \ |
| 342 | + std::cerr << "convert " << VALUE << "as" << #TYPE << "failed"; \ |
| 343 | + return false; \ |
| 344 | + } \ |
| 345 | + TYPE& ref_conf_value = *reinterpret_cast<TYPE*>((FIELD).storage); \ |
| 346 | + TYPE old_value = ref_conf_value; \ |
| 347 | + if (RegisterConfValidator::_s_field_validator != nullptr) { \ |
| 348 | + auto validator = RegisterConfValidator::_s_field_validator->find((FIELD).name); \ |
| 349 | + if (validator != RegisterConfValidator::_s_field_validator->end() && \ |
| 350 | + !(validator->second)()) { \ |
| 351 | + ref_conf_value = old_value; \ |
| 352 | + std::cerr << "validate " << (FIELD).name << "=" << new_value << " failed" \ |
| 353 | + << std::endl; \ |
| 354 | + return false; \ |
| 355 | + } \ |
| 356 | + } \ |
| 357 | + ref_conf_value = new_value; \ |
| 358 | + if (full_conf_map != nullptr) { \ |
| 359 | + std::ostringstream oss; \ |
| 360 | + oss << new_value; \ |
| 361 | + (*full_conf_map)[(FIELD).name] = oss.str(); \ |
| 362 | + } \ |
| 363 | + if (PERSIST) { \ |
| 364 | + props.set_force(std::string((FIELD).name), VALUE); \ |
| 365 | + } \ |
| 366 | + return true; \ |
| 367 | + } |
| 368 | + |
300 | 369 | // init conf fields
|
301 | 370 | bool init(const char* conf_file, bool fill_conf_map, bool must_exist, bool set_to_default) {
|
302 | 371 | Properties props;
|
@@ -328,4 +397,41 @@ bool init(const char* conf_file, bool fill_conf_map, bool must_exist, bool set_t
|
328 | 397 | return true;
|
329 | 398 | }
|
330 | 399 |
|
| 400 | +bool do_set_config(const Register::Field& feild, const std::string& value, bool need_persist, |
| 401 | + Properties& props) { |
| 402 | + UPDATE_FIELD(feild, value, bool, need_persist); |
| 403 | + UPDATE_FIELD(feild, value, int16_t, need_persist); |
| 404 | + UPDATE_FIELD(feild, value, int32_t, need_persist); |
| 405 | + UPDATE_FIELD(feild, value, int64_t, need_persist); |
| 406 | + UPDATE_FIELD(feild, value, double, need_persist); |
| 407 | + { |
| 408 | + // add lock to ensure thread safe |
| 409 | + std::lock_guard<std::mutex> lock(mutable_string_config_lock); |
| 410 | + UPDATE_FIELD(feild, value, std::string, need_persist); |
| 411 | + } |
| 412 | + return false; |
| 413 | +} |
| 414 | + |
| 415 | +bool set_config(const std::string& field, const std::string& value, bool need_persist) { |
| 416 | + auto it = Register::_s_field_map->find(field); |
| 417 | + if (it == Register::_s_field_map->end()) { |
| 418 | + return false; |
| 419 | + } |
| 420 | + if (!it->second.valmutable) { |
| 421 | + return false; |
| 422 | + } |
| 423 | + Properties props; |
| 424 | + if (!do_set_config(it->second, value, need_persist, props)) { |
| 425 | + std::cerr << "not supported to modify: " << field << "=" << value << std::endl; |
| 426 | + return false; |
| 427 | + } |
| 428 | + |
| 429 | + if (need_persist) { |
| 430 | + // lock to make sure only one thread can modify the be_custom.conf |
| 431 | + std::lock_guard<std::mutex> l(conf_persist_lock); |
| 432 | + return props.dump(config::g_conf_path.data()); |
| 433 | + } |
| 434 | + return true; |
| 435 | +} |
| 436 | + |
331 | 437 | } // namespace doris::cloud::config
|
0 commit comments