From 20384872a8baed739726ccd18b59ef34f1914de6 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 29 Oct 2020 13:49:04 +1300 Subject: [PATCH] Move stats out of common crate Signed-off-by: Nick Cameron --- Cargo.lock | 1 - src/lib.rs | 1 + src/pd/retry.rs | 3 +-- src/request.rs | 9 +++++---- {tikv-client-common/src => src}/stats.rs | 7 +++++-- tikv-client-common/Cargo.toml | 20 +++++++------------- tikv-client-common/src/lib.rs | 5 ----- tikv-client-common/src/security.rs | 2 +- tikv-client-store/src/client.rs | 10 ++++++---- tikv-client-store/src/lib.rs | 4 +--- tikv-client-store/src/request.rs | 9 ++++----- 11 files changed, 31 insertions(+), 40 deletions(-) rename {tikv-client-common/src => src}/stats.rs (96%) diff --git a/Cargo.lock b/Cargo.lock index f351ade..ffed3cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1888,7 +1888,6 @@ dependencies = [ "kvproto", "lazy_static", "log", - "prometheus", "proptest", "proptest-derive", "regex", diff --git a/src/lib.rs b/src/lib.rs index 1094ae0..88d2f44 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,6 +82,7 @@ mod config; mod pd; pub mod raw; mod request; +mod stats; #[cfg(test)] mod mock; diff --git a/src/pd/retry.rs b/src/pd/retry.rs index 161f6d5..40bcf42 100644 --- a/src/pd/retry.rs +++ b/src/pd/retry.rs @@ -2,7 +2,7 @@ //! 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 futures_timer::Delay; use grpcio::Environment; @@ -15,7 +15,6 @@ use std::{ sync::Arc, time::{Duration, Instant}, }; -use tikv_client_common::stats::pd_stats; use tikv_client_pd::{Cluster, Connection}; use tokio::sync::RwLock; diff --git a/src/request.rs b/src/request.rs index ecd621c..9c6b0f6 100644 --- a/src/request.rs +++ b/src/request.rs @@ -3,6 +3,7 @@ use crate::{ backoff::{Backoff, NoBackoff, NoJitterBackoff}, pd::PdClient, + stats::tikv_stats, transaction::{resolve_locks, HasLocks}, 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); async move { 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)) } }) @@ -263,7 +265,6 @@ mod test { use grpcio::CallOption; use kvproto::{kvrpcpb, tikvpb::TikvClient}; use std::{any::Any, sync::Mutex}; - use tikv_client_common::stats::{tikv_stats, RequestStats}; #[test] fn test_region_retry() { @@ -295,8 +296,8 @@ mod test { Ok(Box::new(MockRpcResponse {})) } - fn stats(&self) -> RequestStats { - tikv_stats("mock") + fn label(&self) -> &'static str { + "mock" } fn as_any(&self) -> &dyn Any { diff --git a/tikv-client-common/src/stats.rs b/src/stats.rs similarity index 96% rename from tikv-client-common/src/stats.rs rename to src/stats.rs index 2bd647a..016e1a5 100644 --- a/tikv-client-common/src/stats.rs +++ b/src/stats.rs @@ -1,7 +1,10 @@ // Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0. 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}; 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); } -lazy_static! { +lazy_static::lazy_static! { static ref TIKV_REQUEST_DURATION_HISTOGRAM_VEC: HistogramVec = register_histogram_vec!( "tikv_request_duration_seconds", "Bucketed histogram of TiKV requests duration", diff --git a/tikv-client-common/Cargo.toml b/tikv-client-common/Cargo.toml index 93c2eb1..2f3f54c 100644 --- a/tikv-client-common/Cargo.toml +++ b/tikv-client-common/Cargo.toml @@ -5,27 +5,21 @@ edition = "2018" [dependencies] +derive-new = "0.5" 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"] } +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" +log = "0.4" regex = "1" serde = "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] clap = "2.32" -tempdir = "0.3" -tokio = { version = "0.2", features = ["rt-threaded", "macros"] } +fail = { version = "0.3", features = [ "failpoints" ] } proptest = "0.9" proptest-derive = "0.1.0" -fail = { version = "0.3", features = [ "failpoints" ] } +tempdir = "0.3" +tokio = { version = "0.2", features = ["rt-threaded", "macros"] } diff --git a/tikv-client-common/src/lib.rs b/tikv-client-common/src/lib.rs index 152084b..066b523 100644 --- a/tikv-client-common/src/lib.rs +++ b/tikv-client-common/src/lib.rs @@ -2,15 +2,10 @@ mod errors; mod kv; pub mod security; -pub mod stats; mod timestamp; -#[macro_use] -extern crate lazy_static; #[macro_use] extern crate log; -#[macro_use] -extern crate prometheus; #[doc(inline)] pub use crate::errors::{Error, ErrorKind, Result}; diff --git a/tikv-client-common/src/security.rs b/tikv-client-common/src/security.rs index 526aa91..1bc1a53 100644 --- a/tikv-client-common/src/security.rs +++ b/tikv-client-common/src/security.rs @@ -11,7 +11,7 @@ use std::{ time::Duration, }; -lazy_static! { +lazy_static::lazy_static! { static ref SCHEME_REG: Regex = Regex::new(r"^\s*(https?://)").unwrap(); } diff --git a/tikv-client-store/src/client.rs b/tikv-client-store/src/client.rs index e14fdc0..709886e 100644 --- a/tikv-client-store/src/client.rs +++ b/tikv-client-store/src/client.rs @@ -70,9 +70,11 @@ pub struct Store { impl Store { pub async fn dispatch(&self, request: &Req) -> Result> { - let result = self.client.dispatch(request).await; - let result = result.map(|r| r.downcast().expect("Downcast failed")); - - request.stats().done(result) + Ok(self + .client + .dispatch(request) + .await? + .downcast() + .expect("Downcast failed")) } } diff --git a/tikv-client-store/src/lib.rs b/tikv-client-store/src/lib.rs index 14d9d1d..d00ab9e 100644 --- a/tikv-client-store/src/lib.rs +++ b/tikv-client-store/src/lib.rs @@ -16,6 +16,4 @@ pub use crate::{ region::{Region, RegionId, RegionVerId, StoreId}, request::Request, }; -pub use tikv_client_common::{ - security::SecurityManager, stats::RequestStats, Error, ErrorKind, Key, Result, -}; +pub use tikv_client_common::{security::SecurityManager, Error, ErrorKind, Key, Result}; diff --git a/tikv-client-store/src/request.rs b/tikv-client-store/src/request.rs index d51d01f..53c66e5 100644 --- a/tikv-client-store/src/request.rs +++ b/tikv-client-store/src/request.rs @@ -1,16 +1,15 @@ // 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 grpcio::CallOption; use kvproto::{kvrpcpb, tikvpb::TikvClient}; use std::any::Any; -use tikv_client_common::stats::tikv_stats; #[async_trait] pub trait Request: Any + Sync + Send + 'static { async fn dispatch(&self, client: &TikvClient, options: CallOption) -> Result>; - fn stats(&self) -> RequestStats; + fn label(&self) -> &'static str; fn as_any(&self) -> &dyn Any; fn set_context(&mut self, context: kvrpcpb::Context); } @@ -31,8 +30,8 @@ macro_rules! impl_request { .map_err(|e| ErrorKind::Grpc(e).into()) } - fn stats(&self) -> RequestStats { - tikv_stats($label) + fn label(&self) -> &'static str { + $label } fn as_any(&self) -> &dyn Any {