Move stats out of common crate

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron 2020-10-29 13:49:04 +13:00
parent 797257aff4
commit 20384872a8
11 changed files with 31 additions and 40 deletions

1
Cargo.lock generated
View File

@ -1888,7 +1888,6 @@ dependencies = [
"kvproto", "kvproto",
"lazy_static", "lazy_static",
"log", "log",
"prometheus",
"proptest", "proptest",
"proptest-derive", "proptest-derive",
"regex", "regex",

View File

@ -82,6 +82,7 @@ mod config;
mod pd; mod pd;
pub mod raw; pub mod raw;
mod request; mod request;
mod stats;
#[cfg(test)] #[cfg(test)]
mod mock; mod mock;

View File

@ -2,7 +2,7 @@
//! A utility module for managing and retrying PD requests. //! A utility module for managing and retrying PD requests.
use crate::{Error, Region, RegionId, Result, SecurityManager, StoreId}; use crate::{stats::pd_stats, Error, Region, RegionId, Result, SecurityManager, StoreId};
use async_trait::async_trait; use async_trait::async_trait;
use futures_timer::Delay; use futures_timer::Delay;
use grpcio::Environment; use grpcio::Environment;
@ -15,7 +15,6 @@ use std::{
sync::Arc, sync::Arc,
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use tikv_client_common::stats::pd_stats;
use tikv_client_pd::{Cluster, Connection}; use tikv_client_pd::{Cluster, Connection};
use tokio::sync::RwLock; use tokio::sync::RwLock;

View File

@ -3,6 +3,7 @@
use crate::{ use crate::{
backoff::{Backoff, NoBackoff, NoJitterBackoff}, backoff::{Backoff, NoBackoff, NoJitterBackoff},
pd::PdClient, pd::PdClient,
stats::tikv_stats,
transaction::{resolve_locks, HasLocks}, transaction::{resolve_locks, HasLocks},
BoundRange, Error, ErrorKind, Key, Result, BoundRange, Error, ErrorKind, Key, Result,
}; };
@ -62,7 +63,8 @@ pub trait KvRequest: Request + Clone + Sync + Send + 'static + Sized {
let request = self.make_rpc_request(key_data, &store); let request = self.make_rpc_request(key_data, &store);
async move { async move {
let request = request?; let request = request?;
let response = store.dispatch::<_, Self::RpcResponse>(&request).await?; let response = store.dispatch::<_, Self::RpcResponse>(&request).await;
let response = tikv_stats(request.label()).done(response)?;
Ok((request, *response)) Ok((request, *response))
} }
}) })
@ -263,7 +265,6 @@ mod test {
use grpcio::CallOption; use grpcio::CallOption;
use kvproto::{kvrpcpb, tikvpb::TikvClient}; use kvproto::{kvrpcpb, tikvpb::TikvClient};
use std::{any::Any, sync::Mutex}; use std::{any::Any, sync::Mutex};
use tikv_client_common::stats::{tikv_stats, RequestStats};
#[test] #[test]
fn test_region_retry() { fn test_region_retry() {
@ -295,8 +296,8 @@ mod test {
Ok(Box::new(MockRpcResponse {})) Ok(Box::new(MockRpcResponse {}))
} }
fn stats(&self) -> RequestStats { fn label(&self) -> &'static str {
tikv_stats("mock") "mock"
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {

View File

@ -1,7 +1,10 @@
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0. // Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
use crate::Result; use crate::Result;
use prometheus::{Histogram, HistogramVec, IntCounterVec}; use prometheus::{
register_histogram, register_histogram_vec, register_int_counter_vec, Histogram, HistogramVec,
IntCounterVec,
};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
pub struct RequestStats { pub struct RequestStats {
@ -70,7 +73,7 @@ pub fn observe_tso_batch(batch_size: usize) {
PD_TSO_BATCH_SIZE_HISTOGRAM.observe(batch_size as f64); PD_TSO_BATCH_SIZE_HISTOGRAM.observe(batch_size as f64);
} }
lazy_static! { lazy_static::lazy_static! {
static ref TIKV_REQUEST_DURATION_HISTOGRAM_VEC: HistogramVec = register_histogram_vec!( static ref TIKV_REQUEST_DURATION_HISTOGRAM_VEC: HistogramVec = register_histogram_vec!(
"tikv_request_duration_seconds", "tikv_request_duration_seconds",
"Bucketed histogram of TiKV requests duration", "Bucketed histogram of TiKV requests duration",

View File

@ -5,27 +5,21 @@ edition = "2018"
[dependencies] [dependencies]
derive-new = "0.5"
failure = "0.1" failure = "0.1"
grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false }
futures = { version = "0.3.5", features = ["compat", "async-await", "thread-pool"] } futures = { version = "0.3.5", features = ["compat", "async-await", "thread-pool"] }
grpcio = { version = "0.6", features = [ "secure", "prost-codec" ], default-features = false }
kvproto = { git = "https://github.com/pingcap/kvproto.git", features = [ "prost-codec" ], default-features = false }
lazy_static = "1" lazy_static = "1"
log = "0.4"
regex = "1" regex = "1"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
log = "0.4"
derive-new = "0.5"
kvproto = { git = "https://github.com/pingcap/kvproto.git", features = [ "prost-codec" ], default-features = false }
[dependencies.prometheus]
version = "0.8"
default-features = false
features = ["push", "process"]
[dev-dependencies] [dev-dependencies]
clap = "2.32" clap = "2.32"
tempdir = "0.3" fail = { version = "0.3", features = [ "failpoints" ] }
tokio = { version = "0.2", features = ["rt-threaded", "macros"] }
proptest = "0.9" proptest = "0.9"
proptest-derive = "0.1.0" proptest-derive = "0.1.0"
fail = { version = "0.3", features = [ "failpoints" ] } tempdir = "0.3"
tokio = { version = "0.2", features = ["rt-threaded", "macros"] }

View File

@ -2,15 +2,10 @@
mod errors; mod errors;
mod kv; mod kv;
pub mod security; pub mod security;
pub mod stats;
mod timestamp; mod timestamp;
#[macro_use]
extern crate lazy_static;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use]
extern crate prometheus;
#[doc(inline)] #[doc(inline)]
pub use crate::errors::{Error, ErrorKind, Result}; pub use crate::errors::{Error, ErrorKind, Result};

View File

@ -11,7 +11,7 @@ use std::{
time::Duration, time::Duration,
}; };
lazy_static! { lazy_static::lazy_static! {
static ref SCHEME_REG: Regex = Regex::new(r"^\s*(https?://)").unwrap(); static ref SCHEME_REG: Regex = Regex::new(r"^\s*(https?://)").unwrap();
} }

View File

@ -70,9 +70,11 @@ pub struct Store {
impl Store { impl Store {
pub async fn dispatch<Req: Request, Resp: Any>(&self, request: &Req) -> Result<Box<Resp>> { pub async fn dispatch<Req: Request, Resp: Any>(&self, request: &Req) -> Result<Box<Resp>> {
let result = self.client.dispatch(request).await; Ok(self
let result = result.map(|r| r.downcast().expect("Downcast failed")); .client
.dispatch(request)
request.stats().done(result) .await?
.downcast()
.expect("Downcast failed"))
} }
} }

View File

@ -16,6 +16,4 @@ pub use crate::{
region::{Region, RegionId, RegionVerId, StoreId}, region::{Region, RegionId, RegionVerId, StoreId},
request::Request, request::Request,
}; };
pub use tikv_client_common::{ pub use tikv_client_common::{security::SecurityManager, Error, ErrorKind, Key, Result};
security::SecurityManager, stats::RequestStats, Error, ErrorKind, Key, Result,
};

View File

@ -1,16 +1,15 @@
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0. // Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
use crate::{ErrorKind, RequestStats, Result}; use crate::{ErrorKind, Result};
use async_trait::async_trait; use async_trait::async_trait;
use grpcio::CallOption; use grpcio::CallOption;
use kvproto::{kvrpcpb, tikvpb::TikvClient}; use kvproto::{kvrpcpb, tikvpb::TikvClient};
use std::any::Any; use std::any::Any;
use tikv_client_common::stats::tikv_stats;
#[async_trait] #[async_trait]
pub trait Request: Any + Sync + Send + 'static { pub trait Request: Any + Sync + Send + 'static {
async fn dispatch(&self, client: &TikvClient, options: CallOption) -> Result<Box<dyn Any>>; async fn dispatch(&self, client: &TikvClient, options: CallOption) -> Result<Box<dyn Any>>;
fn stats(&self) -> RequestStats; fn label(&self) -> &'static str;
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
fn set_context(&mut self, context: kvrpcpb::Context); fn set_context(&mut self, context: kvrpcpb::Context);
} }
@ -31,8 +30,8 @@ macro_rules! impl_request {
.map_err(|e| ErrorKind::Grpc(e).into()) .map_err(|e| ErrorKind::Grpc(e).into())
} }
fn stats(&self) -> RequestStats { fn label(&self) -> &'static str {
tikv_stats($label) $label
} }
fn as_any(&self) -> &dyn Any { fn as_any(&self) -> &dyn Any {