-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.rs
456 lines (397 loc) · 15.7 KB
/
mod.rs
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
446
447
448
449
450
451
452
453
454
455
456
use crate::{
configs::node::{Node as NodeConfig, LOCAL_ADDRESS, METRICS_NAME, NODE_NAME},
metrics::collector::establish_global_collector,
};
use metrics::{register_counter, Recorder};
use metrics_exporter_prometheus::PrometheusRecorder;
use pearl::init_pearl;
use std::{
net::SocketAddr,
sync::Arc,
time::{Duration, Instant},
};
use tokio::{pin, select};
pub mod collector;
mod exporters;
pub mod pearl;
use exporters::global_exporter::GlobalRecorder;
pub use self::collector::SharedMetricsSnapshot;
/// Counts number of PUT requests, processed by Grinder
pub const GRINDER_PUT_COUNTER: &str = "cluster_grinder.put_count";
/// Counts number of PUT requests return error, processed by Grinder
pub const GRINDER_PUT_ERROR_COUNT_COUNTER: &str = "cluster_grinder.put_error_count";
/// Measures processing time of the PUT request
pub const GRINDER_PUT_TIMER: &str = "cluster_grinder.put_timer";
/// Counts number of GET requests, processed by Grinder
pub const GRINDER_GET_COUNTER: &str = "cluster_grinder.get_count";
/// Counts number of GET requests return error, processed by Grinder
pub const GRINDER_GET_ERROR_COUNT_COUNTER: &str = "cluster_grinder.get_error_count";
/// Measures processing time of the GET request
pub const GRINDER_GET_TIMER: &str = "cluster_grinder.get_timer";
/// Counts number of EXIST requests, processed by Grinder
pub const GRINDER_EXIST_COUNTER: &str = "cluster_grinder.exist_count";
/// Counts number of EXIST requests return error, processed by Grinder
pub const GRINDER_EXIST_ERROR_COUNT_COUNTER: &str = "cluster_grinder.exist_error_count";
/// Counts number of keys in error EXIST requests, processed by Grinder
pub const GRINDER_EXIST_ERROR_KEYS_COUNT_COUNTER: &str = "cluster_grinder.exist_error_keys_count";
/// Counts number of keys in EXIST requests, processed by Grinder
pub const GRINDER_EXIST_KEYS_COUNT_COUNTER: &str = "cluster_grinder.exist_keys_count";
/// Measures processing time of the EXIST request
pub const GRINDER_EXIST_TIMER: &str = "cluster_grinder.exist_timer";
/// Counts number of DELETE requests, processed by Grinder
pub const GRINDER_DELETE_COUNTER: &str = "cluster_grinder.delete_count";
/// Counts number of DELETE requests return error, processed by Grinder
pub const GRINDER_DELETE_ERROR_COUNT_COUNTER: &str = "cluster_grinder.delete_error_count";
/// Measures processing time of the DELETE request
pub const GRINDER_DELETE_TIMER: &str = "cluster_grinder.delete_timer";
/// Counts number of PUT requests, processed by Client
pub const CLIENT_PUT_COUNTER: &str = "client.put_count";
/// Counts number of PUT requests return error, processed by Client
pub const CLIENT_PUT_ERROR_COUNT_COUNTER: &str = "client.put_error_count";
/// Measures processing time of the PUT request
pub const CLIENT_PUT_TIMER: &str = "client.put_timer";
/// Counts number of GET requests, processed by Client
pub const CLIENT_GET_COUNTER: &str = "client.get_count";
/// Counts number of GET requests return error, processed by Client
pub const CLIENT_GET_ERROR_COUNT_COUNTER: &str = "client.get_error_count";
/// Measures processing time of the GET request
pub const CLIENT_GET_TIMER: &str = "client.get_timer";
/// Counts number of EXIST requests, processed by Client
pub const CLIENT_EXIST_COUNTER: &str = "client.exist_count";
/// Counts number of EXIST requests return error, processed by Client
pub const CLIENT_EXIST_ERROR_COUNT_COUNTER: &str = "client.exist_error_count";
/// Counts number of keys in error EXIST requests, processed by Client
pub const CLIENT_EXIST_ERROR_KEYS_COUNT_COUNTER: &str = "client.exist_error_keys_count";
/// Counts number of keys in EXIST requests, processed by Client
pub const CLIENT_EXIST_KEYS_COUNT_COUNTER: &str = "client.exist_keys_count";
/// Measures processing time of the EXIST request
pub const CLIENT_EXIST_TIMER: &str = "client.exist_timer";
/// Counts number of DELETE requests, processed by Client
pub const CLIENT_DELETE_COUNTER: &str = "client.delete_count";
/// Counts number of DELETE requests return error, processed by Client
pub const CLIENT_DELETE_ERROR_COUNT_COUNTER: &str = "client.delete_error_count";
/// Measures processing time of the DELETE request
pub const CLIENT_DELETE_TIMER: &str = "client.delete_timer";
/// Observes number of connected nodes
pub const AVAILABLE_NODES_COUNT: &str = "link_manager.nodes_number";
/// Observes if bob has started already
pub const BACKEND_STATE: &str = "backend.backend_state";
/// Count blobs (without aliens)
pub const BLOBS_COUNT: &str = "backend.blob_count";
/// Count corrupted blobs
pub const CORRUPTED_BLOBS_COUNT: &str = "backend.corrupted_blob_count";
/// Memory occupied bybloom filters
pub const BLOOM_FILTERS_RAM: &str = "backend.bloom_filters_ram";
/// Count alien blobs
pub const ALIEN_BLOBS_COUNT: &str = "backend.alien_count";
/// Count memory occupied by indices
pub const INDEX_MEMORY: &str = "backend.index_memory";
/// Count disks, that are available
pub const ACTIVE_DISKS_COUNT: &str = "backend.active_disks";
/// Directory, which contains each disks state
pub const DISKS_FOLDER: &str = "backend.disks";
/// Directory, which contains each disks hardware metrics
pub const HW_DISKS_FOLDER: &str = "hardware.disks";
/// Occupied disk space by disks
pub const DISK_USED: &str = "backend.used_disk";
pub const DESCRIPTORS_AMOUNT: &str = "hardware.descr_amount";
pub const BOB_CPU_LOAD: &str = "hardware.bob_cpu_load";
pub const CPU_IOWAIT: &str = "hardware.cpu_iowait";
pub const AVAILABLE_RAM: &str = "hardware.available_ram";
pub const USED_RAM: &str = "hardware.used_ram";
pub const TOTAL_RAM: &str = "hardware.total_ram";
pub const USED_SWAP: &str = "hardware.used_swap";
pub const BOB_RAM: &str = "hardware.bob_ram";
pub const BOB_VIRTUAL_RAM: &str = "hardware.bob_virtual_ram";
pub const FREE_SPACE: &str = "hardware.free_space";
pub const USED_SPACE: &str = "hardware.used_space";
pub const TOTAL_SPACE: &str = "hardware.total_space";
const CLIENTS_METRICS_DIR: &str = "clients";
const PROMETHEUS_RENDER_INTERVAL_SEC: u64 = 3600;
/// Type to measure time of requests processing
pub type Timer = Instant;
/// Structure contains put/get metrics for `BobClient`
#[derive(Debug, Clone)]
pub struct BobClient {
prefixed_names: Arc<PrefixedNames>,
}
#[derive(Debug, Clone)]
struct PrefixedNames {
put_count: String,
put_timer: String,
put_error_count: String,
get_count: String,
get_timer: String,
get_error_count: String,
exist_count: String,
exist_timer: String,
exist_error_count: String,
delete_count: String,
delete_timer: String,
delete_error_count: String,
}
impl PrefixedNames {
fn new(prefix: &str) -> Self {
Self {
put_count: format!("{}.put_count", prefix),
put_timer: format!("{}.put_timer", prefix),
put_error_count: format!("{}.put_error_count", prefix),
get_count: format!("{}.get_count", prefix),
get_timer: format!("{}.get_timer", prefix),
get_error_count: format!("{}.get_error_count", prefix),
exist_count: format!("{}.exist_count", prefix),
exist_timer: format!("{}.exist_timer", prefix),
exist_error_count: format!("{}.exist_error_count", prefix),
delete_count: format!("{}.delete_count", prefix),
delete_timer: format!("{}.delete_timer", prefix),
delete_error_count: format!("{}.delete_error_count", prefix),
}
}
}
impl BobClient {
fn new(prefixed_names: Arc<PrefixedNames>) -> Self {
BobClient { prefixed_names }
}
pub(crate) fn put_count(&self) {
counter!(self.prefixed_names.put_count.clone(), 1);
}
pub(crate) fn start_timer() -> Timer {
Instant::now()
}
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn put_timer_stop(&self, timer: Timer) {
histogram!(
self.prefixed_names.put_timer.clone(),
timer.elapsed().as_nanos() as f64
);
}
pub(crate) fn put_error_count(&self) {
counter!(self.prefixed_names.put_error_count.clone(), 1);
}
pub(crate) fn get_count(&self) {
counter!(self.prefixed_names.get_count.clone(), 1);
}
pub(crate) fn get_timer_stop(&self, timer: Timer) {
histogram!(
self.prefixed_names.get_timer.clone(),
timer.elapsed().as_nanos() as f64
);
}
pub(crate) fn get_error_count(&self) {
counter!(self.prefixed_names.get_error_count.clone(), 1);
}
pub(crate) fn exist_count(&self) {
counter!(self.prefixed_names.exist_count.clone(), 1);
}
pub(crate) fn exist_timer_stop(&self, timer: Timer) {
histogram!(
self.prefixed_names.exist_timer.clone(),
timer.elapsed().as_nanos() as f64
);
}
pub(crate) fn exist_error_count(&self) {
counter!(self.prefixed_names.exist_error_count.clone(), 1);
}
pub(crate) fn delete_count(&self) {
counter!(self.prefixed_names.delete_count.clone(), 1);
}
pub(crate) fn delete_timer_stop(&self, timer: Timer) {
histogram!(
self.prefixed_names.delete_timer.clone(),
timer.elapsed().as_nanos() as f64
);
}
pub(crate) fn delete_error_count(&self) {
counter!(self.prefixed_names.delete_error_count.clone(), 1);
}
}
#[derive(Debug, Clone)]
struct MetricsContainer {
duration: Duration,
prefixed_names: Arc<PrefixedNames>,
}
impl MetricsContainer {
pub(crate) fn new(duration: Duration, prefix: String) -> Self {
let prefixed_names = Arc::new(PrefixedNames::new(&prefix));
MetricsContainer {
duration,
prefixed_names,
}
}
}
/// A trait for generic metrics builders
pub trait ContainerBuilder {
/// Initializes `BobClient` container with given name
fn get_metrics(&self) -> BobClient;
}
impl ContainerBuilder for MetricsContainer {
fn get_metrics(&self) -> BobClient {
BobClient::new(self.prefixed_names.clone())
}
}
/// initializes bob counters with given config and address of the local node
#[allow(unused_variables)]
pub async fn init_counters(
node_config: &NodeConfig,
local_address: &str,
) -> (
Arc<dyn ContainerBuilder + Send + Sync>,
SharedMetricsSnapshot,
) {
//install_prometheus();
//install_graphite(node_config, local_address);
let shared = install_global(node_config, local_address).await;
let container = MetricsContainer::new(
Duration::from_secs(1),
format!(
"{}.{}",
CLIENTS_METRICS_DIR,
local_address.replace(".", "_")
),
);
info!(
"metrics container initialized with update interval: {}ms",
container.duration.as_millis()
);
let metrics = Arc::new(container);
init_grinder();
init_client();
init_backend();
init_link_manager();
init_pearl();
(metrics, shared)
}
fn init_grinder() {
register_counter!(GRINDER_GET_COUNTER);
register_counter!(GRINDER_PUT_COUNTER);
register_counter!(GRINDER_EXIST_COUNTER);
register_counter!(GRINDER_DELETE_COUNTER);
register_counter!(GRINDER_EXIST_KEYS_COUNT_COUNTER);
register_counter!(GRINDER_GET_ERROR_COUNT_COUNTER);
register_counter!(GRINDER_PUT_ERROR_COUNT_COUNTER);
register_counter!(GRINDER_EXIST_ERROR_COUNT_COUNTER);
register_counter!(GRINDER_DELETE_ERROR_COUNT_COUNTER);
register_counter!(GRINDER_EXIST_ERROR_KEYS_COUNT_COUNTER);
}
fn init_client() {
register_counter!(CLIENT_EXIST_COUNTER);
register_counter!(CLIENT_EXIST_ERROR_COUNT_COUNTER);
register_counter!(CLIENT_EXIST_ERROR_KEYS_COUNT_COUNTER);
register_counter!(CLIENT_EXIST_KEYS_COUNT_COUNTER);
register_counter!(CLIENT_DELETE_COUNTER);
register_counter!(CLIENT_DELETE_ERROR_COUNT_COUNTER);
register_counter!(CLIENT_GET_COUNTER);
register_counter!(CLIENT_GET_ERROR_COUNT_COUNTER);
register_counter!(CLIENT_PUT_COUNTER);
register_counter!(CLIENT_PUT_ERROR_COUNT_COUNTER);
}
fn init_backend() {
register_gauge!(BACKEND_STATE);
register_gauge!(BLOBS_COUNT);
register_gauge!(ALIEN_BLOBS_COUNT);
}
fn init_link_manager() {
register_gauge!(AVAILABLE_NODES_COUNT);
}
async fn install_global(node_config: &NodeConfig, local_address: &str) -> SharedMetricsSnapshot {
let (recorder, metrics) = establish_global_collector(Duration::from_secs(1));
let mut recorders: Vec<Box<dyn Recorder>> = vec![Box::new(recorder)];
if node_config.metrics().graphite_enabled() {
build_graphite(node_config, local_address, metrics.clone());
}
if node_config.metrics().prometheus_enabled() {
let prometheus_rec = build_prometheus(node_config);
recorders.push(Box::new(prometheus_rec));
info!("prometheus exporter enabled");
} else {
info!("prometheus exporter disabled");
}
if !recorders.is_empty() {
install_global_recorder(recorders);
}
metrics
}
fn install_global_recorder(recorders: Vec<Box<dyn Recorder>>) {
let global_rec = GlobalRecorder::new(recorders);
metrics::set_boxed_recorder(Box::new(global_rec)).expect("Can't set global recorder");
}
#[allow(unused)]
fn install_prometheus(node_config: &NodeConfig) {
let recorder = build_prometheus(node_config);
metrics::set_boxed_recorder(Box::new(recorder)).expect("Can't set Prometheus recorder");
}
fn build_prometheus(node_config: &NodeConfig) -> PrometheusRecorder {
let addr = node_config
.metrics()
.prometheus_addr()
.parse::<SocketAddr>()
.expect("Bad prometheus address");
let (recorder, exporter) = metrics_exporter_prometheus::PrometheusBuilder::new()
.listen_address(addr)
.build_with_exporter()
.expect("Failed to set Prometheus exporter");
debug!("prometheus built");
let future = async move {
pin!(exporter);
loop {
select! {
_ = &mut exporter => {}
}
}
};
tokio::spawn(future);
// Fix for memory leak in prometheus crate, https://github.com/qoollo/bob/issues/788
let handle = recorder.handle();
let future = async move {
let mut timer = tokio::time::interval(Duration::from_secs(PROMETHEUS_RENDER_INTERVAL_SEC));
loop {
let _ = timer.tick().await;
let _ = handle.render();
}
};
tokio::spawn(future);
recorder
}
#[allow(unused)]
fn resolve_prefix_pattern(
mut pattern: String,
node_config: &NodeConfig,
local_address: &str,
) -> String {
let mut pats_subs = vec![
(LOCAL_ADDRESS, local_address),
(NODE_NAME, node_config.name()),
];
if let Some(name) = node_config.metrics().name() {
pats_subs.push((METRICS_NAME, name));
}
for (pat, sub) in pats_subs {
let sub = sub.to_owned().replace(|c| c == ' ' || c == '.', "_");
pattern = pattern.replace(pat, &sub);
}
pattern
}
#[allow(unused)]
fn install_graphite(node_config: &NodeConfig, local_address: &str) {
let (recorder, metrics) = establish_global_collector(Duration::from_secs(1));
build_graphite(node_config, local_address, metrics);
metrics::set_boxed_recorder(Box::new(recorder)).expect("Can't set graphite recorder");
}
fn build_graphite(node_config: &NodeConfig, local_address: &str, metrics: SharedMetricsSnapshot) {
let prefix_pattern = node_config
.metrics()
.prefix()
.map_or(format!("{}.{}", NODE_NAME, LOCAL_ADDRESS), str::to_owned);
let prefix = resolve_prefix_pattern(prefix_pattern, node_config, local_address);
exporters::graphite_exporter::GraphiteBuilder::new()
.set_address(
node_config
.metrics()
.graphite()
.expect("graphite is enabled but address is not set")
.to_string(),
)
.set_interval(Duration::from_secs(1))
.set_prefix(prefix)
.build(metrics);
}