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
use std::sync::Mutex;
use std::time::{Duration, Instant};
use crate::server::Error;
use futures::compat::Future01CompatExt;
use futures::future::{FutureExt, TryFutureExt};
use futures::sink::SinkExt;
use futures::stream::StreamExt;
use grpcio::{
Result as GrpcResult, RpcContext, RpcStatus, RpcStatusCode, ServerStreamingSink, UnarySink,
WriteFlags,
};
#[cfg(feature = "prost-codec")]
use kvproto::diagnosticspb::search_log_request::Target as SearchLogRequestTarget;
#[cfg(not(feature = "prost-codec"))]
use kvproto::diagnosticspb::SearchLogRequestTarget;
use kvproto::diagnosticspb::{
Diagnostics, SearchLogRequest, SearchLogResponse, ServerInfoRequest, ServerInfoResponse,
ServerInfoType,
};
use tikv_util::{sys::SystemExt, timer::GLOBAL_TIMER_HANDLE};
use tokio::runtime::Handle;
mod ioload;
mod log;
mod sys;
lazy_static! {
pub static ref SYS_INFO: Mutex<sysinfo::System> = Mutex::new(sysinfo::System::new());
}
#[derive(Clone)]
pub struct Service {
pool: Handle,
log_file: String,
slow_log_file: String,
}
impl Service {
pub fn new(pool: Handle, log_file: String, slow_log_file: String) -> Self {
Service {
pool,
log_file,
slow_log_file,
}
}
}
impl Diagnostics for Service {
fn search_log(
&mut self,
ctx: RpcContext<'_>,
req: SearchLogRequest,
mut sink: ServerStreamingSink<SearchLogResponse>,
) {
let log_file = if req.get_target() == SearchLogRequestTarget::Normal {
self.log_file.to_owned()
} else {
self.slow_log_file.to_owned()
};
let stream = self.pool.spawn(async move {
log::search(log_file, req)
.map(|stream| stream.map(|resp| (resp, WriteFlags::default().buffer_hint(true))))
.map_err(|e| {
grpcio::Error::RpcFailure(RpcStatus::new(
RpcStatusCode::UNKNOWN,
Some(format!("{:?}", e)),
))
})
});
let f = self
.pool
.spawn(async move {
match stream.await.unwrap() {
Ok(s) => {
let res = async move {
sink.send_all(&mut s.map(Ok)).await?;
sink.close().await?;
GrpcResult::Ok(())
}
.await;
if let Err(e) = res {
error!("search log RPC error"; "error" => ?e);
}
}
Err(e) => {
error!("search log RPC error"; "error" => ?e);
}
}
})
.map(|res| res.unwrap());
ctx.spawn(f);
}
fn server_info(
&mut self,
ctx: RpcContext<'_>,
req: ServerInfoRequest,
sink: UnarySink<ServerInfoResponse>,
) {
let tp = req.get_tp();
let collect = async move {
let (load, when) = match tp {
ServerInfoType::LoadInfo | ServerInfoType::All => {
let mut system = SYS_INFO.lock().unwrap();
system.refresh_networks_list();
system.refresh_all();
let load = (
sys::cpu_time_snapshot(),
system
.get_networks()
.into_iter()
.map(|(n, d)| (n.to_owned(), sys::NicSnapshot::from_network_data(d)))
.collect(),
ioload::IoLoad::snapshot(),
);
let when = Instant::now() + Duration::from_millis(1000);
(Some(load), when)
}
_ => (None, Instant::now()),
};
let timer = GLOBAL_TIMER_HANDLE.clone();
let _ = timer.delay(when).compat().await;
let mut server_infos = Vec::new();
match req.get_tp() {
ServerInfoType::HardwareInfo => sys::hardware_info(&mut server_infos),
ServerInfoType::LoadInfo => sys::load_info(load.unwrap(), &mut server_infos),
ServerInfoType::SystemInfo => sys::system_info(&mut server_infos),
ServerInfoType::All => {
sys::hardware_info(&mut server_infos);
sys::load_info(load.unwrap(), &mut server_infos);
sys::system_info(&mut server_infos);
}
};
server_infos
.sort_by(|a, b| (a.get_tp(), a.get_name()).cmp(&(b.get_tp(), b.get_name())));
let mut resp = ServerInfoResponse::default();
resp.set_items(server_infos.into());
resp
};
let f = self.pool.spawn(collect).then(|res| async move {
let res = sink.success(res.unwrap()).map_err(Error::from).await;
if let Err(e) = res {
debug!("Diagnostics rpc failed"; "err" => ?e);
}
});
ctx.spawn(f);
}
}