Coverage Report

Created: 2025-07-26 16:05

/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 <string>
25
#include <utility>
26
#include <vector>
27
28
namespace doris::cloud::config {
29
30
class Register {
31
public:
32
    struct Field {
33
        const char* type = nullptr;
34
        const char* name = nullptr;
35
        void* storage = nullptr;
36
        const char* defval = nullptr;
37
        bool valmutable = false;
38
        Field(const char* ftype, const char* fname, void* fstorage, const char* fdefval,
39
              bool fvalmutable)
40
                : type(ftype),
41
                  name(fname),
42
                  storage(fstorage),
43
                  defval(fdefval),
44
3.19k
                  valmutable(fvalmutable) {}
45
    };
46
47
public:
48
    static std::map<std::string, Field>* _s_field_map;
49
50
public:
51
    Register(const char* ftype, const char* fname, void* fstorage, const char* fdefval,
52
3.19k
             bool fvalmutable) {
53
3.19k
        if (_s_field_map == nullptr) {
54
24
            _s_field_map = new std::map<std::string, Field>();
55
24
        }
56
3.19k
        Field field(ftype, fname, fstorage, fdefval, fvalmutable);
57
3.19k
        _s_field_map->insert(std::make_pair(std::string(fname), field));
58
3.19k
    }
59
};
60
61
// RegisterConfValidator class is used to store validator function of registered config fields in
62
// Register::_s_field_map.
63
// If any validator return false when BE bootstart, the bootstart will be terminated.
64
// If validator return false when use http API to update some config, the config will not
65
// be modified and the API will return failure.
66
class RegisterConfValidator {
67
public:
68
    // Validator for each config name.
69
    static std::map<std::string, std::function<bool()>>* _s_field_validator;
70
71
public:
72
168
    RegisterConfValidator(const char* fname, const std::function<bool()>& validator) {
73
168
        if (_s_field_validator == nullptr) {
74
24
            _s_field_validator = new std::map<std::string, std::function<bool()>>();
75
24
        }
76
        // register validator to _s_field_validator
77
168
        _s_field_validator->insert(std::make_pair(std::string(fname), validator));
78
168
    }
79
};
80
81
#define DEFINE_FIELD(FIELD_TYPE, FIELD_NAME, FIELD_DEFAULT, VALMUTABLE)                      \
82
    FIELD_TYPE FIELD_NAME;                                                                   \
83
    static Register reg_##FIELD_NAME(#FIELD_TYPE, #FIELD_NAME, &(FIELD_NAME), FIELD_DEFAULT, \
84
                                     VALMUTABLE);
85
86
#define DECLARE_FIELD(FIELD_TYPE, FIELD_NAME) extern FIELD_TYPE FIELD_NAME;
87
88
#define DEFINE_VALIDATOR(FIELD_NAME, VALIDATOR)              \
89
216
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_10clEl
Line
Count
Source
89
18
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_11clEl
Line
Count
Source
89
18
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_12clEl
Line
Count
Source
89
18
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_13clEl
Line
Count
Source
89
18
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_14clERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
89
36
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_15clEi
Line
Count
Source
89
54
    static auto validator_##FIELD_NAME = VALIDATOR;          \
configbase.cpp:_ZNK5doris5cloud6config4$_16clEi
Line
Count
Source
89
54
    static auto validator_##FIELD_NAME = VALIDATOR;          \
90
    static RegisterConfValidator reg_validator_##FIELD_NAME( \
91
126
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_0clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_1clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_2clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_3clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_4clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_5clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
configbase.cpp:_ZNK5doris5cloud6config3$_6clEv
Line
Count
Source
91
18
            #FIELD_NAME, []() -> bool { return validator_##FIELD_NAME(FIELD_NAME); });
92
93
#define DECLARE_VALIDATOR(FIELD_NAME) ;
94
95
#ifdef __IN_CONFIGBASE_CPP__
96
#define CONF_Bool(name, defaultstr) DEFINE_FIELD(bool, name, defaultstr, false)
97
#define CONF_Int16(name, defaultstr) DEFINE_FIELD(int16_t, name, defaultstr, false)
98
#define CONF_Int32(name, defaultstr) DEFINE_FIELD(int32_t, name, defaultstr, false)
99
#define CONF_Int64(name, defaultstr) DEFINE_FIELD(int64_t, name, defaultstr, false)
100
#define CONF_Double(name, defaultstr) DEFINE_FIELD(double, name, defaultstr, false)
101
#define CONF_String(name, defaultstr) DEFINE_FIELD(std::string, name, defaultstr, false)
102
#define CONF_Bools(name, defaultstr) DEFINE_FIELD(std::vector<bool>, name, defaultstr, false)
103
#define CONF_Int16s(name, defaultstr) DEFINE_FIELD(std::vector<int16_t>, name, defaultstr, false)
104
#define CONF_Int32s(name, defaultstr) DEFINE_FIELD(std::vector<int32_t>, name, defaultstr, false)
105
#define CONF_Int64s(name, defaultstr) DEFINE_FIELD(std::vector<int64_t>, name, defaultstr, false)
106
#define CONF_Doubles(name, defaultstr) DEFINE_FIELD(std::vector<double>, name, defaultstr, false)
107
#define CONF_Strings(name, defaultstr) \
108
    DEFINE_FIELD(std::vector<std::string>, name, defaultstr, false)
109
#define CONF_mBool(name, defaultstr) DEFINE_FIELD(bool, name, defaultstr, true)
110
#define CONF_mInt16(name, defaultstr) DEFINE_FIELD(int16_t, name, defaultstr, true)
111
#define CONF_mInt32(name, defaultstr) DEFINE_FIELD(int32_t, name, defaultstr, true)
112
#define CONF_mInt64(name, defaultstr) DEFINE_FIELD(int64_t, name, defaultstr, true)
113
#define CONF_mDouble(name, defaultstr) DEFINE_FIELD(double, name, defaultstr, true)
114
#define CONF_mString(name, defaultstr) DEFINE_FIELD(std::string, name, defaultstr, true)
115
#define CONF_Validator(name, validator) DEFINE_VALIDATOR(name, validator)
116
117
#else
118
#define CONF_Bool(name, defaultstr) DECLARE_FIELD(bool, name)
119
#define CONF_Int16(name, defaultstr) DECLARE_FIELD(int16_t, name)
120
#define CONF_Int32(name, defaultstr) DECLARE_FIELD(int32_t, name)
121
#define CONF_Int64(name, defaultstr) DECLARE_FIELD(int64_t, name)
122
#define CONF_Double(name, defaultstr) DECLARE_FIELD(double, name)
123
#define CONF_String(name, defaultstr) DECLARE_FIELD(std::string, name)
124
#define CONF_Bools(name, defaultstr) DECLARE_FIELD(std::vector<bool>, name)
125
#define CONF_Int16s(name, defaultstr) DECLARE_FIELD(std::vector<int16_t>, name)
126
#define CONF_Int32s(name, defaultstr) DECLARE_FIELD(std::vector<int32_t>, name)
127
#define CONF_Int64s(name, defaultstr) DECLARE_FIELD(std::vector<int64_t>, name)
128
#define CONF_Doubles(name, defaultstr) DECLARE_FIELD(std::vector<double>, name)
129
#define CONF_Strings(name, defaultstr) DECLARE_FIELD(std::vector<std::string>, name)
130
#define CONF_mBool(name, defaultstr) DECLARE_FIELD(bool, name)
131
#define CONF_mInt16(name, defaultstr) DECLARE_FIELD(int16_t, name)
132
#define CONF_mInt32(name, defaultstr) DECLARE_FIELD(int32_t, name)
133
#define CONF_mInt64(name, defaultstr) DECLARE_FIELD(int64_t, name)
134
#define CONF_mDouble(name, defaultstr) DECLARE_FIELD(double, name)
135
#define CONF_mString(name, defaultstr) DECLARE_FIELD(std::string, name)
136
#define CONF_Validator(name, validator) DECLARE_VALIDATOR(name)
137
#endif
138
139
// configuration properties load from config file.
140
class Properties {
141
public:
142
    // load conf from file, if must_exist is true and file does not exist, return false
143
    bool load(const char* conf_file, bool must_exist = true);
144
145
    // Find the config value by key from `file_conf_map`.
146
    // If found, set `retval` to the config value,
147
    // or set `retval` to `defstr`
148
    // if retval is not set(in case defstr is nullptr), set is_retval_set to false
149
    template <typename T>
150
    bool get_or_default(const char* key, const char* defstr, T& retval, bool* is_retval_set) const;
151
152
    void set(const std::string& key, const std::string& val);
153
154
    void set_force(const std::string& key, const std::string& val);
155
156
    // Dump props to conf file if conffile exists, will append to it
157
    // else will dump to a new conffile.
158
    //
159
    // This Function will generate a tmp file for modification and rename it
160
    // to subtitute the original one if modication success.
161
    bool dump(const std::string& conffile);
162
163
private:
164
    std::map<std::string, std::string> file_conf_map;
165
};
166
167
// full configurations.
168
extern std::map<std::string, std::string>* full_conf_map;
169
170
// Init the config from `conf_file`.
171
// If fill_conf_map is true, the updated config will also update the `full_conf_map`.
172
// If must_exist is true and `conf_file` does not exist, this function will return false.
173
// If set_to_default is true, the config value will be set to default value if not found in `conf_file`.
174
// Return true if load
175
bool init(const char* conf_file, bool fill_conf_map = false, bool must_exist = true,
176
          bool set_to_default = true);
177
178
std::pair<bool, std::string> set_config(std::unordered_map<std::string, std::string> field_map,
179
                                        bool need_persist, const std::string& custom_conf_path);
180
181
} // namespace doris::cloud::config