/src/brpc/src/bvar/mvariable.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | // Date: 2021/11/17 14:37:53 |
19 | | |
20 | | #include <gflags/gflags.h> |
21 | | #include <gflags/gflags_declare.h> |
22 | | #include "butil/logging.h" // LOG |
23 | | #include "butil/errno.h" // berror |
24 | | #include "butil/containers/flat_map.h" // butil::FlatMap |
25 | | #include "butil/scoped_lock.h" // BAIDU_SCOPE_LOCK |
26 | | #include "butil/file_util.h" // butil::FilePath |
27 | | #include "butil/reloadable_flags.h" |
28 | | #include "bvar/variable.h" |
29 | | #include "bvar/mvariable.h" |
30 | | |
31 | | namespace bvar { |
32 | | |
33 | | constexpr uint64_t MAX_LABELS_COUNT = 10; |
34 | | |
35 | | DECLARE_bool(bvar_abort_on_same_name); |
36 | | |
37 | | extern bool s_bvar_may_abort; |
38 | | |
39 | 0 | static bool validator_bvar_max_multi_dimension_metric_number(const char*, int32_t v) { |
40 | 0 | if (v < 1) { |
41 | 0 | LOG(ERROR) << "Invalid bvar_max_multi_dimension_metric_number=" << v; |
42 | 0 | return false; |
43 | 0 | } |
44 | 0 | return true; |
45 | 0 | } |
46 | | |
47 | | DEFINE_int32(bvar_max_multi_dimension_metric_number, 1024, "Max number of multi dimension"); |
48 | | BUTIL_VALIDATE_GFLAG(bvar_max_multi_dimension_metric_number, |
49 | | validator_bvar_max_multi_dimension_metric_number); |
50 | | |
51 | 0 | static bool validator_bvar_max_dump_multi_dimension_metric_number(const char*, int32_t v) { |
52 | 0 | if (v < 0) { |
53 | 0 | LOG(ERROR) << "Invalid bvar_max_dump_multi_dimension_metric_number=" << v; |
54 | 0 | return false; |
55 | 0 | } |
56 | 0 | return true; |
57 | 0 | } |
58 | | DEFINE_int32(bvar_max_dump_multi_dimension_metric_number, 1024, |
59 | | "Max number of multi dimension metric number to dump by prometheus rpc service"); |
60 | | BUTIL_VALIDATE_GFLAG(bvar_max_dump_multi_dimension_metric_number, |
61 | | validator_bvar_max_dump_multi_dimension_metric_number); |
62 | | |
63 | 0 | static bool validator_max_multi_dimension_stats_count(const char*, uint32_t v) { |
64 | 0 | if (v < 1) { |
65 | 0 | LOG(ERROR) << "Invalid max_multi_dimension_stats_count=" << v; |
66 | 0 | return false; |
67 | 0 | } |
68 | 0 | return true; |
69 | 0 | } |
70 | | DEFINE_uint32(max_multi_dimension_stats_count, 20000, "Max stats count of a multi dimension metric."); |
71 | | BUTIL_VALIDATE_GFLAG(max_multi_dimension_stats_count, |
72 | | validator_max_multi_dimension_stats_count); |
73 | | |
74 | | class MVarEntry { |
75 | | public: |
76 | 0 | MVarEntry() : var(NULL) {} |
77 | | |
78 | | MVariable* var; |
79 | | }; |
80 | | |
81 | | typedef butil::FlatMap<std::string, MVarEntry> MVarMap; |
82 | | |
83 | | struct MVarMapWithLock : public MVarMap { |
84 | | pthread_mutex_t mutex; |
85 | | |
86 | 0 | MVarMapWithLock() { |
87 | 0 | if (init(256) != 0) { |
88 | 0 | LOG(WARNING) << "Fail to init"; |
89 | 0 | } |
90 | 0 | pthread_mutex_init(&mutex, NULL); |
91 | 0 | } |
92 | | }; |
93 | | |
94 | | // We have to initialize global map on need because bvar is possibly used |
95 | | // before main(). |
96 | | static pthread_once_t s_mvar_map_once = PTHREAD_ONCE_INIT; |
97 | | static MVarMapWithLock* s_mvar_map = NULL; |
98 | | |
99 | 0 | static void init_mvar_map() { |
100 | | // It's probably slow to initialize all sub maps, but rpc often expose |
101 | | // variables before user. So this should not be an issue to users. |
102 | 0 | s_mvar_map = new MVarMapWithLock(); |
103 | 0 | } |
104 | | |
105 | 0 | inline MVarMapWithLock& get_mvar_map() { |
106 | 0 | pthread_once(&s_mvar_map_once, init_mvar_map); |
107 | 0 | return *s_mvar_map; |
108 | 0 | } |
109 | | |
110 | 0 | MVariable::MVariable(const std::list<std::string>& labels) { |
111 | 0 | _labels.assign(labels.begin(), labels.end()); |
112 | 0 | size_t n = labels.size(); |
113 | 0 | if (n > MAX_LABELS_COUNT) { |
114 | 0 | LOG(ERROR) << "Too many labels: " << n << " seen, overflow detected, max labels count: " << MAX_LABELS_COUNT; |
115 | 0 | _labels.resize(MAX_LABELS_COUNT); |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | 0 | MVariable::~MVariable() { |
120 | 0 | CHECK(!hide()) << "Subclass of MVariable MUST call hide() manually in their" |
121 | 0 | " dtors to avoid displaying a variable that is just destructing"; |
122 | 0 | } |
123 | | |
124 | 0 | std::string MVariable::get_description() { |
125 | 0 | std::ostringstream os; |
126 | 0 | describe(os); |
127 | 0 | return os.str(); |
128 | 0 | } |
129 | | |
130 | | int MVariable::describe_exposed(const std::string& name, |
131 | 0 | std::ostream& os) { |
132 | 0 | MVarMapWithLock& m = get_mvar_map(); |
133 | 0 | BAIDU_SCOPED_LOCK(m.mutex); |
134 | 0 | MVarEntry* entry = m.seek(name); |
135 | 0 | if (entry == NULL) { |
136 | 0 | return -1; |
137 | 0 | } |
138 | 0 | entry->var->describe(os); |
139 | 0 | return 0; |
140 | 0 | } |
141 | | |
142 | 0 | std::string MVariable::describe_exposed(const std::string& name) { |
143 | 0 | std::ostringstream oss; |
144 | 0 | if (describe_exposed(name, oss) == 0) { |
145 | 0 | return oss.str(); |
146 | 0 | } |
147 | 0 | return std::string(); |
148 | 0 | } |
149 | | |
150 | | int MVariable::expose_impl(const butil::StringPiece& prefix, |
151 | 0 | const butil::StringPiece& name) { |
152 | 0 | if (name.empty()) { |
153 | 0 | LOG(ERROR) << "Parameter[name] is empty"; |
154 | 0 | return -1; |
155 | 0 | } |
156 | | // NOTE: It's impossible to atomically erase from a submap and insert into |
157 | | // another submap without a global lock. When the to-be-exposed name |
158 | | // already exists, there's a chance that we can't insert back previous |
159 | | // name. But it should be fine generally because users are unlikely to |
160 | | // expose a variable more than once and calls to expose() are unlikely |
161 | | // to contend heavily. |
162 | | |
163 | | // remove previous pointer from the map if needed. |
164 | 0 | hide(); |
165 | | |
166 | | // Build the name. |
167 | 0 | _name.clear(); |
168 | 0 | _name.reserve((prefix.size() + name.size()) * 5 / 4); |
169 | 0 | if (!prefix.empty()) { |
170 | 0 | to_underscored_name(&_name, prefix); |
171 | 0 | if (!_name.empty() && butil::back_char(_name) != '_') { |
172 | 0 | _name.push_back('_'); |
173 | 0 | } |
174 | 0 | } |
175 | 0 | to_underscored_name(&_name, name); |
176 | | |
177 | 0 | if (count_exposed() > (size_t)FLAGS_bvar_max_multi_dimension_metric_number) { |
178 | 0 | LOG(ERROR) << "Too many metric seen, overflow detected, max metric count:" << FLAGS_bvar_max_multi_dimension_metric_number; |
179 | 0 | return -1; |
180 | 0 | } |
181 | | |
182 | 0 | MVarMapWithLock& m = get_mvar_map(); |
183 | 0 | { |
184 | 0 | BAIDU_SCOPED_LOCK(m.mutex); |
185 | 0 | MVarEntry* entry = m.seek(_name); |
186 | 0 | if (entry == NULL) { |
187 | 0 | entry = &m[_name]; |
188 | 0 | entry->var = this; |
189 | 0 | return 0; |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | 0 | RELEASE_ASSERT_VERBOSE(!FLAGS_bvar_abort_on_same_name, |
194 | 0 | "Abort due to name conflict"); |
195 | 0 | if (!s_bvar_may_abort) { |
196 | | // Mark name conflict occurs, If this conflict happens before |
197 | | // initialization of bvar_abort_on_same_name, the validator will |
198 | | // abort the program if needed. |
199 | 0 | s_bvar_may_abort = true; |
200 | 0 | } |
201 | |
|
202 | 0 | LOG(WARNING) << "Already exposed `" << _name << "' whose describe is`" |
203 | 0 | << get_description() << "'"; |
204 | 0 | _name.clear(); |
205 | 0 | return 0; |
206 | 0 | } |
207 | | |
208 | 0 | bool MVariable::hide() { |
209 | 0 | if (_name.empty()) { |
210 | 0 | return false; |
211 | 0 | } |
212 | | |
213 | 0 | MVarMapWithLock& m = get_mvar_map(); |
214 | 0 | BAIDU_SCOPED_LOCK(m.mutex); |
215 | 0 | MVarEntry* entry = m.seek(_name); |
216 | 0 | if (entry) { |
217 | 0 | CHECK_EQ(1UL, m.erase(_name)); |
218 | 0 | } else { |
219 | 0 | CHECK(false) << "`" << _name << "' must exist"; |
220 | 0 | } |
221 | 0 | _name.clear(); |
222 | 0 | return true; |
223 | 0 | } |
224 | | |
225 | | #ifdef UNIT_TEST |
226 | 0 | void MVariable::hide_all() { |
227 | 0 | MVarMapWithLock& m = get_mvar_map(); |
228 | 0 | BAIDU_SCOPED_LOCK(m.mutex); |
229 | 0 | m.clear(); |
230 | 0 | } |
231 | | #endif // end UNIT_TEST |
232 | | |
233 | 0 | size_t MVariable::count_exposed() { |
234 | 0 | MVarMapWithLock& m = get_mvar_map(); |
235 | 0 | BAIDU_SCOPED_LOCK(m.mutex); |
236 | 0 | return m.size(); |
237 | 0 | } |
238 | | |
239 | 0 | void MVariable::list_exposed(std::vector<std::string>* names) { |
240 | 0 | if (names == NULL) { |
241 | 0 | return; |
242 | 0 | } |
243 | | |
244 | 0 | names->clear(); |
245 | |
|
246 | 0 | MVarMapWithLock& mvar_map = get_mvar_map(); |
247 | 0 | BAIDU_SCOPED_LOCK(mvar_map.mutex); |
248 | 0 | names->reserve(mvar_map.size()); |
249 | 0 | for (MVarMap::const_iterator it = mvar_map.begin(); it != mvar_map.end(); ++it) { |
250 | 0 | names->push_back(it->first); |
251 | 0 | } |
252 | 0 | } |
253 | | |
254 | 0 | size_t MVariable::dump_exposed(Dumper* dumper, const DumpOptions* options) { |
255 | 0 | if (NULL == dumper) { |
256 | 0 | LOG(ERROR) << "Parameter[dumper] is NULL"; |
257 | 0 | return -1; |
258 | 0 | } |
259 | 0 | DumpOptions opt; |
260 | 0 | if (options) { |
261 | 0 | opt = *options; |
262 | 0 | } |
263 | 0 | std::vector<std::string> mvars; |
264 | 0 | list_exposed(&mvars); |
265 | 0 | size_t n = 0; |
266 | 0 | for (auto& mvar : mvars) { |
267 | 0 | MVarMapWithLock& m = get_mvar_map(); |
268 | 0 | BAIDU_SCOPED_LOCK(m.mutex); |
269 | 0 | MVarEntry* entry = m.seek(mvar); |
270 | 0 | if (entry) { |
271 | 0 | n += entry->var->dump(dumper, &opt); |
272 | 0 | } |
273 | 0 | if (n > static_cast<size_t>(FLAGS_bvar_max_dump_multi_dimension_metric_number)) { |
274 | 0 | LOG(WARNING) << "truncated because of \ |
275 | 0 | exceed max dump multi dimension label number[" |
276 | 0 | << FLAGS_bvar_max_dump_multi_dimension_metric_number |
277 | 0 | << "]"; |
278 | 0 | break; |
279 | 0 | } |
280 | 0 | } |
281 | 0 | return n; |
282 | 0 | } |
283 | | |
284 | | } // namespace bvar |