Skip to content
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

raftstore-v2: store heartbeat supports write keys and bytes. #14271

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion components/raftstore-v2/src/batch/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use raft::{StateRole, INVALID_ID};
use raftstore::{
coprocessor::{CoprocessorHost, RegionChangeEvent},
store::{
fsm::store::{PeerTickBatch, ENTRY_CACHE_EVICT_TICK_DURATION},
fsm::{
store::{PeerTickBatch, ENTRY_CACHE_EVICT_TICK_DURATION},
GlobalStoreStat, LocalStoreStat,
},
local_metrics::RaftMetrics,
AutoSplitController, Config, ReadRunner, ReadTask, SplitCheckRunner, SplitCheckTask,
StoreWriters, TabletSnapManager, Transport, WriteSenders,
Expand Down Expand Up @@ -84,6 +87,9 @@ pub struct StoreContext<EK: KvEngine, ER: RaftEngine, T> {
pub self_disk_usage: DiskUsage,

pub snap_mgr: TabletSnapManager,

pub global_stat: GlobalStoreStat,
pub store_stat: LocalStoreStat,
}

impl<EK: KvEngine, ER: RaftEngine, T> StoreContext<EK, ER, T> {
Expand Down Expand Up @@ -160,6 +166,7 @@ impl<EK: KvEngine, ER: RaftEngine, T> StorePoller<EK, ER, T> {
fn flush_events(&mut self) {
self.schedule_ticks();
self.poll_ctx.raft_metrics.maybe_flush();
self.poll_ctx.store_stat.flush();
}

fn schedule_ticks(&mut self) {
Expand Down Expand Up @@ -277,6 +284,7 @@ struct StorePollerBuilder<EK: KvEngine, ER: RaftEngine, T> {
store_meta: Arc<Mutex<StoreMeta<EK>>>,
shutdown: Arc<AtomicBool>,
snap_mgr: TabletSnapManager,
global_stat: GlobalStoreStat,
}

impl<EK: KvEngine, ER: RaftEngine, T> StorePollerBuilder<EK, ER, T> {
Expand Down Expand Up @@ -304,6 +312,7 @@ impl<EK: KvEngine, ER: RaftEngine, T> StorePollerBuilder<EK, ER, T> {
.after_start(move || set_io_type(IoType::ForegroundWrite))
.name_prefix("apply")
.build_future_pool();
let global_stat = GlobalStoreStat::default();
StorePollerBuilder {
cfg,
store_id,
Expand All @@ -318,6 +327,7 @@ impl<EK: KvEngine, ER: RaftEngine, T> StorePollerBuilder<EK, ER, T> {
snap_mgr,
shutdown,
coprocessor_host,
global_stat,
}
}

Expand Down Expand Up @@ -435,6 +445,8 @@ where
self_disk_usage: DiskUsage::Normal,
snap_mgr: self.snap_mgr.clone(),
coprocessor_host: self.coprocessor_host.clone(),
global_stat: self.global_stat.clone(),
store_stat: self.global_stat.local(),
};
poll_ctx.update_ticks_timeout();
let cfg_tracker = self.cfg.clone().tracker("raftstore".to_string());
Expand Down
2 changes: 2 additions & 0 deletions components/raftstore-v2/src/operation/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ impl<EK: KvEngine, ER: RaftEngine> Peer<EK, ER> {

self.update_split_flow_control(&apply_res.metrics);
self.update_stat(&apply_res.metrics);
ctx.store_stat.engine_total_bytes_written += apply_res.metrics.written_bytes;
ctx.store_stat.engine_total_keys_written += apply_res.metrics.written_keys;

self.raft_group_mut()
.advance_apply_to(apply_res.applied_index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pub struct SimpleWriteBinary {
buf: Box<[u8]>,
}

impl SimpleWriteBinary {
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
pub fn data_size(&self) -> usize {
self.buf.len()
}
}

/// We usually use `RaftCmdRequest` for read write request. But the codec is
/// not efficient enough for simple request. `SimpleWrite` is introduce to make
/// codec alloc less and fast.
Expand Down
16 changes: 14 additions & 2 deletions components/raftstore-v2/src/operation/pd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

//! This module implements the interactions with pd.

use std::sync::atomic::Ordering;

use engine_traits::{KvEngine, RaftEngine};
use fail::fail_point;
use kvproto::{metapb, pdpb};
Expand Down Expand Up @@ -47,8 +49,18 @@ impl Store {

stats.set_start_time(self.start_time().unwrap() as u32);

stats.set_bytes_written(0);
stats.set_keys_written(0);
stats.set_bytes_written(
ctx.global_stat
.stat
.engine_total_bytes_written
.swap(0, Ordering::SeqCst),
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
);
stats.set_keys_written(
ctx.global_stat
.stat
.engine_total_keys_written
.swap(0, Ordering::SeqCst),
);
stats.set_is_busy(false);
// TODO: add query stats
let task = pd::Task::StoreHeartbeat { stats };
Expand Down
37 changes: 27 additions & 10 deletions components/raftstore-v2/tests/integrations/test_pd_heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use kvproto::raft_cmdpb::{RaftCmdRequest, StatusCmdType};
use pd_client::PdClient;
use raftstore::coprocessor::Config as CopConfig;
use raftstore_v2::{
router::{PeerMsg, PeerTick},
router::{PeerMsg, PeerTick, StoreMsg, StoreTick},
SimpleWriteEncoder,
};
use tikv_util::{config::ReadableSize, store::new_peer};
Expand Down Expand Up @@ -54,18 +54,35 @@ fn test_region_heartbeat() {

#[test]
fn test_store_heartbeat() {
let region_id = 2;
let cluster = Cluster::with_node_count(1, None);
let store_id = cluster.node(0).id();
for _ in 0..5 {
let stats = block_on(cluster.node(0).pd_client().get_store_stats_async(store_id)).unwrap();
if stats.get_start_time() > 0 {
assert_ne!(stats.get_capacity(), 0);
assert_ne!(stats.get_used_size(), 0);
return;
}
std::thread::sleep(std::time::Duration::from_millis(50));
let router = &cluster.routers[0];
// load data to split bucket.
let header = Box::new(router.new_request_for(region_id).take_header());
let mut put = SimpleWriteEncoder::with_capacity(64);
put.put(CF_DEFAULT, b"key", b"value");
let data = put.encode();
let write_bytes = data.data_size();
let (msg, sub) = PeerMsg::simple_write(header, data);
router.send(region_id, msg).unwrap();
let _resp = block_on(sub.result()).unwrap();

// report store heartbeat to pd.
std::thread::sleep(std::time::Duration::from_millis(50));
router
.store_router()
.send_control(StoreMsg::Tick(StoreTick::PdStoreHeartbeat))
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(50));

let stats = block_on(cluster.node(0).pd_client().get_store_stats_async(store_id)).unwrap();
if stats.get_start_time() > 0 {
assert_ne!(stats.get_capacity(), 0);
assert_ne!(stats.get_used_size(), 0);
assert_eq!(stats.get_keys_written(), 1);
assert!(stats.get_bytes_written() > write_bytes.try_into().unwrap());
}
panic!("failed to get store stats");
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions components/raftstore/src/store/fsm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use self::{
ChangePeer, ExecResult, GenSnapTask, Msg as ApplyTask, Notifier as ApplyNotifier, Proposal,
Registration, SwitchWitness, TaskRes as ApplyTaskRes,
},
metrics::{GlobalStoreStat, LocalStoreStat},
peer::{new_admin_request, DestroyPeerJob, PeerFsm, MAX_PROPOSAL_SIZE_RATIO},
store::{
create_raft_batch_system, RaftBatchSystem, RaftPollerBuilder, RaftRouter, StoreInfo,
Expand Down