Coverage Report

Created: 2025-05-21 00:46

/root/doris/cloud/src/common/configbase.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <cstdint>
21
#include <functional>
22
#include <map>
23
#include <mutex>
24
#include <shared_mutex>
25
#include <string>
26
#include <utility>
27
#include <vector>
28
29
namespace doris::cloud::config {
30
31
class Register {
32
public:
33
    struct Field {
34
        const char* type = nullptr;
35
        const char* name = nullptr;
36
        void* storage = nullptr;
37
        const char* defval = nullptr;
38
        bool valmutable = false;
39
        Field(const char* ftype, const char* fname, void* fstorage, const char* fdefval,
40
              bool fvalmutable)
41
                : type(ftype),
42
                  name(fname),
43
                  storage(fstorage),
44
                  defval(fdefval),
45
2.89k
                  valmutable(fvalmutable) {}
46
    };
47
48
public:
49
    static std::map<std::string, Field>* _s_field_map;
50
51
public:
52
    Register(const char* ftype, const char* fname, void* fstorage, const char* fdefval,
53
2.89k
             bool fvalmutable) {
54
2.89k
        if (_s_field_map == nullptr) {
55
23
            _s_field_map = new std::map<std::string, Field>();
56
23
        }
57
2.89k
        Field field(ftype, fname, fstorage, fdefval, fvalmutable);
58
2.89k
        _s_field_map->insert(std::make_pair(std::string(fname), field));
59
2.89k
    }
60
};
61
62
// RegisterConfValidator class is used to store validator function of registered config fields in
63
// Register::_s_field_map.
64
// If any validator return false when BE bootstart, the bootstart will be terminated.
65
// If validator return false when use http API to update some config, the config will not
66
// be modified and the API will return failure.
67
class RegisterConfValidator {
68
public:
69
    // Validator for each config name.
70
    static std::map<std::string, std::function<bool()>>* _s_field_validator;
71
72
public:
73
115
    RegisterConfValidator(const char* fname, const std::function<bool()>& validator) {
74
115
        if (_s_field_validator == nullptr) {
75
23
            _s_field_validator = new std::map<std::string, std::function<bool()>>();
76
23
        }
77
        // register validator to _s_field_validator
78
115
        _s_field_validator->insert(std::make_pair(std::string(fname), validator));
79
115
    }
80
};
81
82
#define DEFINE_FIELD(FIELD_TYPE, FIELD_NAME, FIELD_DEFAULT, VALMUTABLE)                      \
83
    FIELD_TYPE FIELD_NAME;                                                                   \
84
    static Register reg_##FIELD_NAME(#FIELD_TYPE, #FIELD_NAME, &(FIELD_NAME), FIELD_DEFAULT, \
85
                                     VALMUTABLE);
86
87
#define DECLARE_FIELD(FIELD_TYPE, FIELD_NAME) extern FIELD_TYPE FIELD_NAME;
88
89
#define DEFINE_VALIDATOR(FIELD_NAME, VALIDATOR)              \
90
102
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config3$_8clEl
Line
Count
Source
90
17
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config3$_9clEl
Line
Count
Source
90
17
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_10clEl
Line
Count
Source
90
17
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_11clEl
Line
Count
Source
90
17
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_12clERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
90
34
    static auto validator_##FIELD_NAME = VALIDATOR;          \
91
    static RegisterConfValidator reg_validator_##FIELD_NAME( \
92
85
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_0clEv
Line
Count
Source
92
17
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_1clEv
Line
Count
Source
92
17
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_2clEv
Line
Count
Source
92
17
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_3clEv
Line
Count
Source
92
17
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_4clEv
Line
Count
Source
92
17
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
93
94
#define DECLARE_VALIDATOR(FIELD_NAME) ;
95
96
#ifdef __IN_CONFIGBASE_CPP__
97
#define CONF_Bool(name, defaultstr) DEFINE_FIELD(bool, name, defaultstr, false)
98
#define CONF_Int16(name, defaultstr) DEFINE_FIELD(int16_t, name, defaultstr, false)
99
#define CONF_Int32(name, defaultstr) DEFINE_FIELD(int32_t, name, defaultstr, false)
100
#define CONF_Int64(name, defaultstr) DEFINE_FIELD(int64_t, name, defaultstr, false)
101
#define CONF_Double(name, defaultstr) DEFINE_FIELD(double, name, defaultstr, false)
102
#define CONF_String(name, defaultstr) DEFINE_FIELD(std::string, name, defaultstr, false)
103
#define CONF_Bools(name, defaultstr) DEFINE_FIELD(std::vector<bool>, name, defaultstr, false)
104
#define CONF_Int16s(name, defaultstr) DEFINE_FIELD(std::vector<int16_t>, name, defaultstr, false)
105
#define CONF_Int32s(name, defaultstr) DEFINE_FIELD(std::vector<int32_t>, name, defaultstr, false)
106
#define CONF_Int64s(name, defaultstr) DEFINE_FIELD(std::vector<int64_t>, name, defaultstr, false)
107
#define CONF_Doubles(name, defaultstr) DEFINE_FIELD(std::vector<double>, name, defaultstr, false)
108
#define CONF_Strings(name, defaultstr) \
109
    DEFINE_FIELD(std::vector<std::string>, name, defaultstr, false)
110
#define CONF_mBool(name, defaultstr) DEFINE_FIELD(bool, name, defaultstr, true)
111
#define CONF_mInt16(name, defaultstr) DEFINE_FIELD(int16_t, name, defaultstr, true)
112
#define CONF_mInt32(name, defaultstr) DEFINE_FIELD(int32_t, name, defaultstr, true)
113
#define CONF_mInt64(name, defaultstr) DEFINE_FIELD(int64_t, name, defaultstr, true)
114
#define CONF_mDouble(name, defaultstr) DEFINE_FIELD(double, name, defaultstr, true)
115
#define CONF_mString(name, defaultstr) DEFINE_FIELD(std::string, name, defaultstr, true)
116
#define CONF_Validator(name, validator) DEFINE_VALIDATOR(name, validator)
117
118
#else
119
#define CONF_Bool(name, defaultstr) DECLARE_FIELD(bool, name)
120
#define CONF_Int16(name, defaultstr) DECLARE_FIELD(int16_t, name)
121
#define CONF_Int32(name, defaultstr) DECLARE_FIELD(int32_t, name)
122
#define CONF_Int64(name, defaultstr) DECLARE_FIELD(int64_t, name)
123
#define CONF_Double(name, defaultstr) DECLARE_FIELD(double, name)
124
#define CONF_String(name, defaultstr) DECLARE_FIELD(std::string, name)
125
#define CONF_Bools(name, defaultstr) DECLARE_FIELD(std::vector<bool>, name)
126
#define CONF_Int16s(name, defaultstr) DECLARE_FIELD(std::vector<int16_t>, name)
127
#define CONF_Int32s(name, defaultstr) DECLARE_FIELD(std::vector<int32_t>, name)
128
#define CONF_Int64s(name, defaultstr) DECLARE_FIELD(std::vector<int64_t>, name)
129
#define CONF_Doubles(name, defaultstr) DECLARE_FIELD(std::vector<double>, name)
130
#define CONF_Strings(name, defaultstr) DECLARE_FIELD(std::vector<std::string>, name)
131
#define CONF_mBool(name, defaultstr) DECLARE_FIELD(bool, name)
132
#define CONF_mInt16(name, defaultstr) DECLARE_FIELD(int16_t, name)
133
#define CONF_mInt32(name, defaultstr) DECLARE_FIELD(int32_t, name)
134
#define CONF_mInt64(name, defaultstr) DECLARE_FIELD(int64_t, name)
135
#define CONF_mDouble(name, defaultstr) DECLARE_FIELD(double, name)
136
#define CONF_mString(name, defaultstr) DECLARE_FIELD(std::string, name)
137
#define CONF_Validator(name, validator) DECLARE_VALIDATOR(name)
138
#endif
139
140
// configuration properties load from config file.
141
class Properties {
142
public:
143
    // load conf from file, if must_exist is true and file does not exist, return false
144
    bool load(const char* conf_file, bool must_exist = true);
145
146
    // Find the config value by key from `file_conf_map`.
147
    // If found, set `retval` to the config value,
148
    // or set `retval` to `defstr`
149
    // if retval is not set(in case defstr is nullptr), set is_retval_set to false
150
    template <typename T>
151
    bool get_or_default(const char* key, const char* defstr, T& retval, bool* is_retval_set) const;
152
153
    void set(const std::string& key, const std::string& val);
154
155
    void set_force(const std::string& key, const std::string& val);
156
157
    // Dump props to conf file if conffile exists, will append to it
158
    // else will dump to a new conffile.
159
    //
160
    // This Function will generate a tmp file for modification and rename it
161
    // to subtitute the original one if modication success.
162
    bool dump(const std::string& conffile);
163
164
private:
165
    std::map<std::string, std::string> file_conf_map;
166
};
167
168
// full configurations.
169
extern std::map<std::string, std::string>* full_conf_map;
170
171
// Init the config from `conf_file`.
172
// If fill_conf_map is true, the updated config will also update the `full_conf_map`.
173
// If must_exist is true and `conf_file` does not exist, this function will return false.
174
// If set_to_default is true, the config value will be set to default value if not found in `conf_file`.
175
// Return true if load
176
bool init(const char* conf_file, bool fill_conf_map = false, bool must_exist = true,
177
          bool set_to_default = true);
178
179
std::pair<bool, std::string> set_config(std::unordered_map<std::string, std::string> field_map,
180
                                        bool need_persist, const std::string& custom_conf_path);
181
182
std::shared_mutex* get_mutable_string_config_lock();
183
184
} // namespace doris::cloud::config