New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: report read statistics to pd #2307
Merged
Merged
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
2768dc7
*: add statstics
nolouch da8839f
*: report to pd
nolouch 31f5e79
*: for test
nolouch 052cecc
remove unused
nolouch 4c72243
--wip-- [skip ci]
nolouch 58a4ad0
Merge branch 'master' into shuning/readbytes
nolouch 355b487
decoupling coprocessor report
nolouch b638558
address comment
nolouch e855e83
report store flow stat
nolouch b453c03
fix count read bytes
nolouch d61aa55
merge master
nolouch 3c74357
address comment
nolouch 72bad3d
address comment
nolouch b3922ba
Merge branch 'master' into shuning/readbytes
nolouch 1106006
address comment
nolouch 568c5bf
address comment
nolouch beb0ff0
fix
nolouch 76ce157
Merge branch 'master' into shuning/readbytes
nolouch 39403f2
Merge branch 'master' into shuning/readbytes
nolouch 0e37084
address comment
nolouch f591844
--wip-- [skip ci]
nolouch 515184a
Merge branch 'master' into shuning/readbytes
nolouch 83bcdb4
Merge branch 'shuning/readbytes' of https://github.com/pingcap/tikv i…
nolouch 560c430
Merge branch 'master' into shuning/readbytes
nolouch 426934d
Merge branch 'shuning/readbytes' of https://github.com/pingcap/tikv i…
nolouch 9cad433
fix conflicts
nolouch File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,9 +27,9 @@ use kvproto::kvrpcpb::{CommandPri, IsolationLevel}; | |
use util::time::{duration_to_sec, Instant}; | ||
use util::worker::{BatchRunnable, Scheduler}; | ||
use util::collections::HashMap; | ||
use util::threadpool::{Context, ThreadPool, ThreadPoolBuilder}; | ||
use util::threadpool::{Context, ContextFactory, ThreadPool, ThreadPoolBuilder}; | ||
use server::{Config, OnResponse}; | ||
use storage::{self, engine, Engine, Snapshot, Statistics, StatisticsSummary}; | ||
use storage::{self, engine, Engine, FlowStatistics, Snapshot, Statistics, StatisticsSummary}; | ||
use storage::engine::Error as EngineError; | ||
|
||
use super::codec::mysql; | ||
|
@@ -59,24 +59,50 @@ const OUTDATED_ERROR_MSG: &'static str = "request outdated."; | |
|
||
const ENDPOINT_IS_BUSY: &'static str = "endpoint is busy"; | ||
|
||
pub struct Host { | ||
pub struct Host<R: CopSender + 'static> { | ||
engine: Box<Engine>, | ||
sched: Scheduler<Task>, | ||
reqs: HashMap<u64, Vec<RequestTask>>, | ||
last_req_id: u64, | ||
pool: ThreadPool<CopContext>, | ||
low_priority_pool: ThreadPool<CopContext>, | ||
high_priority_pool: ThreadPool<CopContext>, | ||
pool: ThreadPool<CopContext<R>>, | ||
low_priority_pool: ThreadPool<CopContext<R>>, | ||
high_priority_pool: ThreadPool<CopContext<R>>, | ||
max_running_task_count: usize, | ||
} | ||
|
||
pub type CopRequestStatistics = HashMap<u64, FlowStatistics>; | ||
|
||
pub trait CopSender: Send + Clone { | ||
fn send(&self, CopRequestStatistics) -> Result<()>; | ||
} | ||
|
||
struct CopContextFactory<R: CopSender + 'static> { | ||
sender: R, | ||
} | ||
|
||
impl<R> ContextFactory<CopContext<R>> for CopContextFactory<R> | ||
where | ||
R: CopSender + 'static, | ||
{ | ||
fn create(&self) -> CopContext<R> { | ||
CopContext { | ||
sender: self.sender.clone(), | ||
select_stats: Default::default(), | ||
index_stats: Default::default(), | ||
request_stats: HashMap::default(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct CopContext { | ||
struct CopContext<R: CopSender + 'static> { | ||
select_stats: StatisticsSummary, | ||
index_stats: StatisticsSummary, | ||
request_stats: CopRequestStatistics, | ||
sender: R, | ||
} | ||
|
||
impl CopContext { | ||
impl<R: CopSender + 'static> CopContext<R> { | ||
fn add_statistics(&mut self, type_str: &str, stats: &Statistics) { | ||
self.get_statistics(type_str).add_statistics(stats); | ||
} | ||
|
@@ -91,9 +117,16 @@ impl CopContext { | |
} | ||
} | ||
} | ||
|
||
fn add_statistics_by_request(&mut self, id: u64, stats: &Statistics) { | ||
let empty_stat = FlowStatistics::default(); | ||
let flow_stats = self.request_stats.entry(id).or_insert(empty_stat); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/empty_stat/FlowStatistics::default() |
||
flow_stats.add(&stats.write.flow_stats); | ||
flow_stats.add(&stats.data.flow_stats); | ||
} | ||
} | ||
|
||
impl Context for CopContext { | ||
impl<R: CopSender + 'static> Context for CopContext<R> { | ||
fn on_tick(&mut self) { | ||
for type_str in &[STR_REQ_TYPE_SELECT, STR_REQ_TYPE_INDEX] { | ||
let this_statistics = self.get_statistics(type_str); | ||
|
@@ -110,26 +143,37 @@ impl Context for CopContext { | |
} | ||
*this_statistics = Default::default(); | ||
} | ||
if !self.request_stats.is_empty() { | ||
if let Err(e) = self.sender.send(self.request_stats.clone()) { | ||
error!("send coprocessor statistics: {:?}", e); | ||
}; | ||
self.request_stats = HashMap::default(); | ||
} | ||
|
||
} | ||
} | ||
|
||
impl Host { | ||
pub fn new(engine: Box<Engine>, scheduler: Scheduler<Task>, cfg: &Config) -> Host { | ||
impl<R: CopSender + 'static> Host<R> { | ||
pub fn new(engine: Box<Engine>, scheduler: Scheduler<Task>, cfg: &Config, r: R) -> Host<R> { | ||
Host { | ||
engine: engine, | ||
sched: scheduler, | ||
reqs: HashMap::default(), | ||
last_req_id: 0, | ||
max_running_task_count: cfg.end_point_max_tasks, | ||
pool: ThreadPoolBuilder::with_default_factory(thd_name!("endpoint-normal-pool")) | ||
.thread_count(cfg.end_point_concurrency) | ||
pool: ThreadPoolBuilder::new( | ||
thd_name!("endpoint-normal-pool"), | ||
CopContextFactory { sender: r.clone() }, | ||
).thread_count(cfg.end_point_concurrency) | ||
.build(), | ||
low_priority_pool: ThreadPoolBuilder::with_default_factory( | ||
low_priority_pool: ThreadPoolBuilder::new( | ||
thd_name!("endpoint-low-pool"), | ||
CopContextFactory { sender: r.clone() }, | ||
).thread_count(cfg.end_point_concurrency) | ||
.build(), | ||
high_priority_pool: ThreadPoolBuilder::with_default_factory( | ||
high_priority_pool: ThreadPoolBuilder::new( | ||
thd_name!("endpoint-high-pool"), | ||
CopContextFactory { sender: r.clone() }, | ||
).thread_count(cfg.end_point_concurrency) | ||
.build(), | ||
} | ||
|
@@ -170,9 +214,11 @@ impl Host { | |
CommandPri::High => &mut self.high_priority_pool, | ||
CommandPri::Normal => &mut self.pool, | ||
}; | ||
pool.execute(move |ctx: &mut CopContext| { | ||
pool.execute(move |ctx: &mut CopContext<R>| { | ||
let region_id = req.req.get_context().get_region_id(); | ||
let stats = end_point.handle_request(req); | ||
ctx.add_statistics(type_str, &stats); | ||
ctx.add_statistics_by_request(region_id, &stats); | ||
COPR_PENDING_REQS | ||
.with_label_values(&[type_str, pri_str]) | ||
.dec(); | ||
|
@@ -367,7 +413,7 @@ impl Display for RequestTask { | |
} | ||
} | ||
|
||
impl BatchRunnable<Task> for Host { | ||
impl<R: CopSender + 'static> BatchRunnable<Task> for Host<R> { | ||
// TODO: limit pending reqs | ||
#[allow(for_kv_map)] | ||
fn run_batch(&mut self, tasks: &mut Vec<Task>) { | ||
|
@@ -655,17 +701,29 @@ pub fn get_req_pri_str(pri: CommandPri) -> &'static str { | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use storage::engine::{self, TEMP_DIR}; | ||
use std::sync::*; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use kvproto::coprocessor::Request; | ||
|
||
use storage::engine::{self, TEMP_DIR}; | ||
use util::worker::Worker; | ||
use util::time::Instant; | ||
|
||
use super::*; | ||
#[derive(Clone)] | ||
struct MockCopSender {} | ||
impl MockCopSender { | ||
fn new() -> MockCopSender { | ||
MockCopSender {} | ||
} | ||
} | ||
impl CopSender for MockCopSender { | ||
fn send(&self, _stats: CopRequestStatistics) -> Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_get_reg_scan_tag() { | ||
|
@@ -686,7 +744,7 @@ mod tests { | |
let engine = engine::new_local_engine(TEMP_DIR, &[]).unwrap(); | ||
let mut cfg = Config::default(); | ||
cfg.end_point_concurrency = 1; | ||
let end_point = Host::new(engine, worker.scheduler(), &cfg); | ||
let end_point = Host::new(engine, worker.scheduler(), &cfg, MockCopSender::new()); | ||
worker.start_batch(end_point, 30).unwrap(); | ||
let (tx, rx) = mpsc::channel(); | ||
let mut task = RequestTask::new(Request::new(), box move |msg| { tx.send(msg).unwrap(); }); | ||
|
@@ -703,7 +761,7 @@ mod tests { | |
let engine = engine::new_local_engine(TEMP_DIR, &[]).unwrap(); | ||
let mut cfg = Config::default(); | ||
cfg.end_point_concurrency = 1; | ||
let mut end_point = Host::new(engine, worker.scheduler(), &cfg); | ||
let mut end_point = Host::new(engine, worker.scheduler(), &cfg, MockCopSender::new()); | ||
end_point.max_running_task_count = 3; | ||
worker.start_batch(end_point, 30).unwrap(); | ||
let (tx, rx) = mpsc::channel(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
id == region_id ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/add_statistics_by_request/add_statistics_by_region