Skip to content

Commit 8a3c9a2

Browse files
author
tongjian
committed
address comment
Signed-off-by: tongjian <[email protected]>
1 parent 291d193 commit 8a3c9a2

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

grpc-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ fn build_grpc(cc: &mut cc::Build, library: &str) {
300300

301301
fn figure_systemd_path(build_dir: &str) {
302302
let path = format!("{build_dir}/CMakeCache.txt");
303-
let f = BufReader::new(std::fs::File::open(&path).unwrap());
303+
let f = BufReader::new(std::fs::File::open(path).unwrap());
304304
let mut libdir: Option<String> = None;
305305
let mut libname: Option<String> = None;
306306
for l in f.lines() {

grpc-sys/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ mod bindings {
1010
mod grpc_wrap;
1111

1212
pub use bindings::*;
13+
#[allow(unused_imports)]
1314
pub use grpc_wrap::*;

src/env.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
#[cfg(feature = "prometheus")]
4+
use prometheus::{local::LocalHistogram, IntCounter};
35
use std::sync::atomic::{AtomicUsize, Ordering};
46
use std::sync::mpsc;
57
use std::sync::Arc;
@@ -16,39 +18,44 @@ use {
1618
GRPC_TASK_WAIT_DURATION,
1719
},
1820
crate::task::resolve,
19-
prometheus::{
20-
core::{AtomicU64, Counter},
21-
Histogram,
22-
},
2321
std::time::Instant,
2422
};
2523

24+
#[allow(dead_code)]
25+
const METRICS_FLUSH_INTERVAL: u64 = 10_000; // 10s
26+
2627
#[cfg(feature = "prometheus")]
2728
pub struct GRPCRunner {
28-
cq_next_duration_his: Histogram,
29-
execute_duration_his: Histogram,
30-
wait_duration_his: Histogram,
31-
event_counter: [Counter<AtomicU64>; 6],
29+
cq_next_duration_his: LocalHistogram,
30+
execute_duration_his: LocalHistogram,
31+
wait_duration_his: LocalHistogram,
32+
event_counter: [IntCounter; 6],
33+
last_flush_time: Instant,
3234
}
3335

3436
#[cfg(feature = "prometheus")]
3537
impl GRPCRunner {
3638
pub fn new(name: &String) -> GRPCRunner {
37-
let cq_next_duration_his = GRPC_POOL_CQ_NEXT_DURATION.with_label_values(&[name]);
38-
let execute_duration_his = GRPC_POOL_EXECUTE_DURATION.with_label_values(&[name]);
39-
let wait_duration_his = GRPC_TASK_WAIT_DURATION.with_label_values(&[name]);
39+
let cq_next_duration_his = GRPC_POOL_CQ_NEXT_DURATION
40+
.with_label_values(&[name])
41+
.local();
42+
let execute_duration_his = GRPC_POOL_EXECUTE_DURATION
43+
.with_label_values(&[name])
44+
.local();
45+
let wait_duration_his = GRPC_TASK_WAIT_DURATION.with_label_values(&[name]).local();
4046
let event_counter = ["batch", "request", "unary", "abort", "action", "spawn"]
4147
.map(|event| GRPC_POOL_EVENT_COUNT_VEC.with_label_values(&[name, event]));
4248
GRPCRunner {
4349
cq_next_duration_his,
4450
execute_duration_his,
4551
wait_duration_his,
4652
event_counter,
53+
last_flush_time: Instant::now(),
4754
}
4855
}
4956

5057
// event loop
51-
pub fn run(&self, tx: mpsc::Sender<CompletionQueue>) {
58+
pub fn run(&mut self, tx: mpsc::Sender<CompletionQueue>) {
5259
let cq = Arc::new(CompletionQueueHandle::new());
5360
let worker_info = Arc::new(WorkQueue::new());
5461
let cq = CompletionQueue::new(cq, worker_info);
@@ -73,7 +80,21 @@ impl GRPCRunner {
7380
}
7481
self.execute_duration_his
7582
.observe(now.elapsed().as_secs_f64());
83+
self.maybe_flush();
84+
}
85+
}
86+
87+
fn maybe_flush(&mut self) {
88+
let now = Instant::now();
89+
if now.saturating_duration_since(self.last_flush_time)
90+
< std::time::Duration::from_millis(METRICS_FLUSH_INTERVAL)
91+
{
92+
return;
7693
}
94+
self.last_flush_time = now;
95+
self.cq_next_duration_his.flush();
96+
self.execute_duration_his.flush();
97+
self.wait_duration_his.flush();
7798
}
7899

79100
fn resolve(&self, tag: Box<CallTag>, cq: &CompletionQueue, success: bool) {
@@ -193,7 +214,7 @@ impl EnvBuilder {
193214
.as_ref()
194215
.map_or(format!("grpc-pool-{i}"), |prefix| format!("{prefix}-{i}"));
195216
#[cfg(feature = "prometheus")]
196-
let runner = GRPCRunner::new(&name);
217+
let mut runner = GRPCRunner::new(&name);
197218
builder = builder.name(name);
198219
let after_start = self.after_start.clone();
199220
let before_stop = self.before_stop.clone();

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod env;
4040
mod error;
4141
mod log_util;
4242
mod metadata;
43+
#[cfg(feature = "prometheus")]
4344
mod metrics;
4445
mod quota;
4546
mod security;

src/metrics.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Copyright 2023 TiKV Project Authors. Licensed under Apache-2.0.
22

33
//! Metrics of the grpc pool.
4-
54
use lazy_static::lazy_static;
65
use prometheus::*;
7-
86
lazy_static! {
97
/// Grpc wait duration of one task.
108
pub static ref GRPC_TASK_WAIT_DURATION: HistogramVec = register_histogram_vec!(
@@ -29,7 +27,7 @@ lazy_static! {
2927
"grpc_pool_execute_duration",
3028
"Bucketed histogram of grpc pool execute duration for every time",
3129
&["name"],
32-
exponential_buckets(1e-7, 2.0, 20).unwrap() // 100ns ~ 100ms
30+
exponential_buckets(1e-7, 2.0, 30).unwrap() // 100ns ~ 100s
3331
)
3432
.unwrap();
3533

0 commit comments

Comments
 (0)