-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathlib.rs
More file actions
279 lines (249 loc) · 10.1 KB
/
lib.rs
File metadata and controls
279 lines (249 loc) · 10.1 KB
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
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.
mod future;
mod metrics;
mod slab;
mod tls;
use std::time::Instant;
use kvproto::kvrpcpb as pb;
pub use self::{
future::{FutureTrack, track},
slab::{GLOBAL_TRACKERS, INVALID_TRACKER_TOKEN, TrackerToken, TrackerTokenArray},
tls::*,
};
#[derive(Debug)]
pub struct Tracker {
pub req_info: RequestInfo,
pub metrics: RequestMetrics,
}
impl Tracker {
pub fn new(req_info: RequestInfo) -> Self {
Self {
req_info,
metrics: Default::default(),
}
}
/// This function is used to merge the time detail of the request into the
/// `TimeDetailV2` of the request.
// Note: This function uses merge because the
// [`tikv::coprocessor::tracker::Tracker`] sets the `TimeDetailV2`, and we
// don't want to overwrite the its result.
pub fn merge_time_detail(&self, detail_v2: &mut pb::TimeDetailV2) {
detail_v2.set_kv_grpc_process_time_ns(
detail_v2.kv_grpc_process_time_ns + self.metrics.grpc_process_nanos,
);
detail_v2.set_process_wall_time_ns(
detail_v2.process_wall_time_ns + self.metrics.future_process_nanos,
);
detail_v2.set_process_suspend_wall_time_ns(
detail_v2.process_suspend_wall_time_ns + self.metrics.future_suspend_nanos,
);
}
pub fn write_scan_detail(&self, detail_v2: &mut pb::ScanDetailV2) {
detail_v2.set_rocksdb_block_read_byte(self.metrics.block_read_byte);
detail_v2.set_rocksdb_block_read_count(self.metrics.block_read_count);
detail_v2.set_rocksdb_block_read_nanos(self.metrics.block_read_nanos);
detail_v2.set_rocksdb_block_cache_hit_count(self.metrics.block_cache_hit_count);
detail_v2.set_rocksdb_key_skipped_count(self.metrics.internal_key_skipped_count);
detail_v2.set_rocksdb_delete_skipped_count(self.metrics.deleted_key_skipped_count);
detail_v2.set_get_snapshot_nanos(self.metrics.get_snapshot_nanos);
detail_v2.set_read_index_propose_wait_nanos(self.metrics.read_index_propose_wait_nanos);
detail_v2.set_read_index_confirm_wait_nanos(self.metrics.read_index_confirm_wait_nanos);
detail_v2.set_read_pool_schedule_wait_nanos(self.metrics.read_pool_schedule_wait_nanos);
// NOTE: These stats are mainly for write operations. Read operations populate
// MVCC scan stats by `Statistics::write_scan_detail` before calling this
// function. So only fill them when unset to avoid clobbering existing values.
if detail_v2.get_total_versions() == 0 {
detail_v2.set_total_versions(self.metrics.mvcc_total_versions);
}
if detail_v2.get_processed_versions() == 0 {
detail_v2.set_processed_versions(self.metrics.mvcc_processed_versions);
}
if detail_v2.get_processed_versions_size() == 0 {
detail_v2.set_processed_versions_size(self.metrics.mvcc_processed_versions_size);
}
}
pub fn write_write_detail(&self, detail: &mut pb::WriteDetail) {
detail.set_latch_wait_nanos(self.metrics.latch_wait_nanos);
detail.set_process_nanos(self.metrics.scheduler_process_nanos);
detail.set_throttle_nanos(self.metrics.scheduler_throttle_nanos);
detail.set_pessimistic_lock_wait_nanos(self.metrics.pessimistic_lock_wait_nanos);
detail.set_store_batch_wait_nanos(self.metrics.wf_batch_wait_nanos);
detail.set_propose_send_wait_nanos(
self.metrics
.wf_send_proposal_nanos
.saturating_sub(self.metrics.wf_send_to_queue_nanos),
);
detail.set_persist_log_nanos(
self.metrics.wf_persist_log_nanos - self.metrics.wf_send_to_queue_nanos,
);
detail.set_raft_db_write_leader_wait_nanos(
self.metrics.store_mutex_lock_nanos + self.metrics.store_thread_wait_nanos,
);
detail.set_raft_db_sync_log_nanos(self.metrics.store_write_wal_nanos);
detail.set_raft_db_write_memtable_nanos(self.metrics.store_write_memtable_nanos);
// It's an approximation considering generating proposal is fast CPU operation.
// And note that the time before flushing the raft message to the RPC channel is
// also counted in this value (to be improved in the future).
detail.set_commit_log_nanos(
self.metrics.wf_commit_log_nanos - self.metrics.wf_batch_wait_nanos,
);
detail.set_apply_batch_wait_nanos(self.metrics.apply_wait_nanos);
// When async_prewrite_apply is set, the `apply_time_nanos` could be less than
// apply_wait_nanos.
if self.metrics.apply_time_nanos > self.metrics.apply_wait_nanos {
detail
.set_apply_log_nanos(self.metrics.apply_time_nanos - self.metrics.apply_wait_nanos);
}
detail.set_apply_mutex_lock_nanos(self.metrics.apply_mutex_lock_nanos);
detail.set_apply_write_leader_wait_nanos(self.metrics.apply_thread_wait_nanos);
detail.set_apply_write_wal_nanos(self.metrics.apply_write_wal_nanos);
detail.set_apply_write_memtable_nanos(self.metrics.apply_write_memtable_nanos);
}
}
#[derive(Debug, Clone)]
pub struct RequestInfo {
pub region_id: u64,
pub start_ts: u64,
pub task_id: u64,
pub resource_group_tag: Vec<u8>,
pub begin: Instant,
// Information recorded after the task is scheduled.
pub request_type: RequestType,
pub cid: u64,
pub is_external_req: bool,
}
impl RequestInfo {
pub fn new(ctx: &pb::Context, request_type: RequestType, start_ts: u64) -> RequestInfo {
RequestInfo {
region_id: ctx.get_region_id(),
start_ts,
task_id: ctx.get_task_id(),
resource_group_tag: ctx.get_resource_group_tag().to_vec(),
begin: Instant::now(),
request_type,
cid: 0,
is_external_req: ctx.get_request_source().contains("external"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum RequestType {
#[default]
Unknown,
KvGet,
KvBatchGet,
KvBatchGetCommand,
KvScan,
KvScanLock,
KvPrewrite,
KvCommit,
KvPessimisticLock,
KvCheckTxnStatus,
KvCheckSecondaryLocks,
KvCleanup,
KvResolveLock,
KvTxnHeartBeat,
KvRollback,
KvPessimisticRollback,
KvFlashbackToVersion,
CoprocessorDag,
CoprocessorAnalyze,
CoprocessorChecksum,
KvFlush,
KvBufferBatchGet,
}
#[derive(Debug, Default, Clone)]
pub struct RequestMetrics {
pub grpc_process_nanos: u64,
pub get_snapshot_nanos: u64,
pub read_index_propose_wait_nanos: u64,
pub read_index_confirm_wait_nanos: u64,
pub read_pool_schedule_wait_nanos: u64,
pub local_read: bool,
pub block_cache_hit_count: u64,
pub block_read_count: u64,
pub block_read_byte: u64,
pub block_read_nanos: u64,
pub internal_key_skipped_count: u64,
pub deleted_key_skipped_count: u64,
pub pessimistic_lock_wait_nanos: u64,
pub latch_wait_nanos: u64,
pub scheduler_process_nanos: u64,
pub scheduler_throttle_nanos: u64,
// MVCC scan stats for txn commands, to be written into `ScanDetailV2`.
pub mvcc_total_versions: u64,
pub mvcc_processed_versions: u64,
pub mvcc_processed_versions_size: u64,
pub future_process_nanos: u64,
pub future_suspend_nanos: u64,
// temp instant used in raftstore metrics, first be the instant when creating the write
// callback, then reset when it is ready to apply
pub write_instant: Option<Instant>,
pub wf_batch_wait_nanos: u64,
pub wf_send_to_queue_nanos: u64,
pub wf_send_proposal_nanos: u64,
pub wf_persist_log_nanos: u64,
pub wf_before_write_nanos: u64,
pub wf_write_end_nanos: u64,
pub wf_kvdb_end_nanos: u64,
pub wf_commit_log_nanos: u64,
pub commit_not_persisted: bool,
pub store_mutex_lock_nanos: u64, // should be 0 if using raft-engine
pub store_thread_wait_nanos: u64,
pub store_write_wal_nanos: u64,
pub store_write_memtable_nanos: u64,
pub store_time_nanos: u64,
pub apply_wait_nanos: u64,
pub apply_time_nanos: u64,
pub apply_mutex_lock_nanos: u64,
pub apply_thread_wait_nanos: u64,
pub apply_write_wal_nanos: u64,
pub apply_write_memtable_nanos: u64,
// recorded outside the read_pool thread, accessed inside the read_pool thread for topsql usage
pub grpc_req_size: u64,
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use super::*;
fn new_req_info() -> RequestInfo {
RequestInfo {
region_id: 0,
start_ts: 0,
task_id: 0,
resource_group_tag: vec![],
begin: Instant::now(),
request_type: RequestType::default(),
cid: 0,
is_external_req: false,
}
}
#[test]
fn test_write_scan_detail_sets_mvcc_scan_stats_when_unset_and_is_idempotent() {
let mut tracker = Tracker::new(new_req_info());
tracker.metrics.mvcc_processed_versions = 3;
tracker.metrics.mvcc_total_versions = 5;
tracker.metrics.mvcc_processed_versions_size = 7;
let mut detail_v2 = pb::ScanDetailV2::default();
tracker.write_scan_detail(&mut detail_v2);
tracker.write_scan_detail(&mut detail_v2);
assert_eq!(detail_v2.get_processed_versions(), 3);
assert_eq!(detail_v2.get_total_versions(), 5);
assert_eq!(detail_v2.get_processed_versions_size(), 7);
}
#[test]
fn test_write_scan_detail_does_not_override_existing_mvcc_scan_stats() {
let mut tracker = Tracker::new(new_req_info());
tracker.metrics.mvcc_processed_versions = 3;
tracker.metrics.mvcc_total_versions = 5;
tracker.metrics.mvcc_processed_versions_size = 7;
let mut detail_v2 = pb::ScanDetailV2::default();
detail_v2.set_processed_versions(11);
detail_v2.set_total_versions(13);
detail_v2.set_processed_versions_size(17);
tracker.write_scan_detail(&mut detail_v2);
assert_eq!(detail_v2.get_processed_versions(), 11);
assert_eq!(detail_v2.get_total_versions(), 13);
assert_eq!(detail_v2.get_processed_versions_size(), 17);
}
}