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: clean up import sst file only if flushed epoch is stale. #15064

Merged
merged 17 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
72 changes: 61 additions & 11 deletions components/engine_traits/src/flush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::{
},
};

use kvproto::import_sstpb::SstMeta;
use slog_global::info;
use tikv_util::set_panic_mark;

Expand Down Expand Up @@ -61,7 +62,8 @@ struct FlushProgress {
/// if the flushed index greater than it .
#[derive(Debug, Clone)]
pub struct SstApplyState {
sst_map: Arc<RwLock<HashMap<Vec<u8>, u64>>>,
// Map from cf to SstApplyEntry.
sst_map: Arc<RwLock<HashMap<usize, Vec<SstApplyEntry>>>>,
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for SstApplyState {
Expand All @@ -72,25 +74,51 @@ impl Default for SstApplyState {
}
}

#[derive(Debug)]
pub struct SstApplyEntry {
pub applied_index: u64,
pub sst: SstMeta,
}

impl SstApplyEntry {
pub fn new(applied_index: u64, sst: SstMeta) -> Self {
Self { applied_index, sst }
}
}

impl SstApplyState {
#[inline]
pub fn registe_ssts(&self, uuids: Vec<Vec<u8>>, sst_applied_index: u64) {
pub fn register_ssts(&self, applied_index: u64, ssts: Vec<SstMeta>) {
let mut map = self.sst_map.write().unwrap();
for uuid in uuids {
map.insert(uuid, sst_applied_index);
for sst in ssts {
let cf_index = data_cf_offset(sst.get_cf_name());
let entry = SstApplyEntry::new(applied_index, sst);
map.entry(cf_index).or_default().push(entry);
}
}

/// Query the sst applied index.
#[inline]
pub fn sst_applied_index(&self, uuid: &Vec<u8>) -> Option<u64> {
self.sst_map.read().unwrap().get(uuid).copied()
pub fn stale_ssts(&self, cf: &str, flushed_index: u64) -> Vec<SstMeta> {
let map = self.sst_map.read().unwrap();
let cf_index = data_cf_offset(cf);
if let Some(ssts) = map.get(&cf_index) {
return ssts
.iter()
.filter(|entry| entry.applied_index <= flushed_index)
.map(|entry| entry.sst.clone())
.collect();
}
vec![]
}

pub fn delete_ssts(&self, uuids: Vec<Vec<u8>>) {
let mut map = self.sst_map.write().unwrap();
for uuid in uuids {
map.remove(&uuid);
pub fn delete_ssts(&self, ssts: &Vec<SstMeta>) {
let mut map: std::sync::RwLockWriteGuard<'_, HashMap<usize, Vec<SstApplyEntry>>> =
self.sst_map.write().unwrap();
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
for sst in ssts {
let cf_index = data_cf_offset(sst.get_cf_name());
if let Some(metas) = map.get_mut(&cf_index) {
metas.drain_filter(|entry| entry.sst.get_uuid() == sst.get_uuid());
}
}
}
}
Expand Down Expand Up @@ -270,3 +298,25 @@ impl<R: RaftEngine> StateStorage for R {
self.consume(&mut batch, true).unwrap();
}
}

#[cfg(test)]
mod test {
use std::vec;

use kvproto::import_sstpb::SstMeta;

use super::SstApplyState;

#[test]
pub fn test_sst_apply_state() {
let stat = SstApplyState::default();
let mut sst = SstMeta::default();
sst.set_cf_name("write".to_owned());
sst.set_uuid(vec![1, 2, 3, 4]);
stat.register_ssts(10, vec![sst.clone()]);
assert!(stat.stale_ssts("default", 10).is_empty());
let sst = stat.stale_ssts("write", 10);
assert_eq!(sst[0].get_uuid(), vec![1, 2, 3, 4]);
stat.delete_ssts(&sst);
}
}
1 change: 1 addition & 0 deletions components/engine_traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@
#![feature(linked_list_cursors)]
#![feature(let_chains)]
#![feature(str_split_as_str)]
#![feature(drain_filter)]

#[macro_use(fail_point)]
extern crate fail;
Expand Down
9 changes: 6 additions & 3 deletions components/raftstore-v2/src/fsm/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,12 @@ impl<'a, EK: KvEngine, ER: RaftEngine, T: Transport> PeerFsmDelegate<'a, EK, ER,
tablet_index,
flushed_index,
} => {
self.fsm
.peer_mut()
.on_data_flushed(cf, tablet_index, flushed_index);
self.fsm.peer_mut().on_data_flushed(
self.store_ctx,
cf,
tablet_index,
flushed_index,
);
}
PeerMsg::PeerUnreachable { to_peer_id } => {
self.fsm.peer_mut().on_peer_unreachable(to_peer_id)
Expand Down
46 changes: 16 additions & 30 deletions components/raftstore-v2/src/operation/command/write/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use raftstore::{
store::{check_sst_for_ingestion, metrics::PEER_WRITE_CMD_COUNTER, util},
Result,
};
use slog::error;
use slog::{error, info};
use tikv_util::{box_try, slog_panic};

use crate::{
Expand Down Expand Up @@ -67,38 +67,27 @@ impl<EK: KvEngine, ER: RaftEngine> Peer<EK, ER> {
ctx: &mut StoreContext<EK, ER, T>,
ssts: Box<[SstMeta]>,
) {
let mut stale_ssts = Vec::from(ssts);
let epoch = self.region().get_region_epoch();
stale_ssts.retain(|sst| {
fail::fail_point!("on_cleanup_import_sst", |_| true);
util::is_epoch_stale(sst.get_region_epoch(), epoch)
});

// some sst needs to be kept if the log didn't flush the disk.
let flushed_indexes = self.storage().apply_trace().flushed_indexes();
stale_ssts.retain(|sst| {
let off = data_cf_offset(sst.get_cf_name());
let uuid = sst.get_uuid().to_vec();
let sst_index = self.sst_apply_state().sst_applied_index(&uuid);
if let Some(index) = sst_index {
return flushed_indexes.as_ref()[off] >= index;
}
true
});
let mut stale_ssts: Vec<SstMeta> = Vec::from(ssts);
let flushed_epoch = self.storage().flushed_epoch();
stale_ssts.retain(|sst| util::is_epoch_stale(sst.get_region_epoch(), flushed_epoch));

fail::fail_point!("on_cleanup_import_sst_schedule");
if stale_ssts.is_empty() {
return;
}
let uuids = stale_ssts
.iter()
.map(|sst| sst.get_uuid().to_vec())
.collect();
self.sst_apply_state().delete_ssts(uuids);
info!(
self.logger,
"clean up import sst file";
"flushed_epoch" => ?flushed_epoch,
"stale_ssts" => ?stale_ssts);

self.sst_apply_state().delete_ssts(&stale_ssts);
let _ = ctx
.schedulers
.tablet
.schedule(tablet::Task::CleanupImportSst(stale_ssts.into()));
.schedule(tablet::Task::CleanupImportSst(
stale_ssts.into_boxed_slice(),
));
}
}

Expand Down Expand Up @@ -143,12 +132,9 @@ impl<EK: KvEngine, R: ApplyResReporter> Apply<EK, R> {
if let Err(e) = self.sst_importer().ingest(&infos, self.tablet()) {
slog_panic!(self.logger, "ingest fail"; "ssts" => ?ssts, "error" => ?e);
}
let metas: Vec<SstMeta> = infos.iter().map(|info| info.meta.clone()).collect();
self.sst_apply_state().register_ssts(index, metas);
}
let uuids = infos
.iter()
.map(|info| info.meta.get_uuid().to_vec())
.collect::<Vec<_>>();
self.set_sst_applied_index(uuids, index);

self.metrics.size_diff_hint += size;
self.metrics.written_bytes += size as u64;
Expand Down
36 changes: 35 additions & 1 deletion components/raftstore-v2/src/operation/ready/apply_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use crate::{
},
raft::{Peer, Storage},
router::PeerMsg,
worker::tablet,
Result, StoreRouter,
};

Expand Down Expand Up @@ -519,6 +520,13 @@ impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
}
let region_id = self.region().get_id();
let raft_engine = self.entry_storage().raft_engine();
let epoch = raft_engine
.get_region_state(region_id, trace.admin.flushed)
.unwrap()
.unwrap()
.get_region()
.get_region_epoch()
.clone();
let tablet_index = self.tablet_index();
let lb = write_task
.extra_write
Expand All @@ -529,11 +537,19 @@ impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
.unwrap();
trace.try_persist = false;
trace.persisted_applied = trace.admin.flushed;

self.set_flushed_epoch(epoch);
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<EK: KvEngine, ER: RaftEngine> Peer<EK, ER> {
pub fn on_data_flushed(&mut self, cf: &str, tablet_index: u64, index: u64) {
pub fn on_data_flushed<T>(
&mut self,
ctx: &mut StoreContext<EK, ER, T>,
cf: &str,
tablet_index: u64,
index: u64,
) {
trace!(self.logger, "data flushed"; "cf" => cf, "tablet_index" => tablet_index, "index" => index, "trace" => ?self.storage().apply_trace());
if tablet_index < self.storage().tablet_index() {
// Stale tablet.
Expand All @@ -543,6 +559,24 @@ impl<EK: KvEngine, ER: RaftEngine> Peer<EK, ER> {
let apply_trace = self.storage_mut().apply_trace_mut();
apply_trace.on_flush(cf, index);
apply_trace.maybe_advance_admin_flushed(apply_index);
let stale_ssts = self.sst_apply_state().stale_ssts(cf, index);
if stale_ssts.is_empty() {
return;
}
info!(
self.logger,
"delete stale ssts after flush";
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
"stale_ssts" => ?stale_ssts,
"apply_index" => apply_index,
"cf" => cf,
"flushed_index" => index,
);
let _ = ctx
.schedulers
.tablet
.schedule(tablet::Task::CleanupImportSst(
stale_ssts.into_boxed_slice(),
));
}

pub fn on_data_modified(&mut self, modification: DataTrace) {
Expand Down
1 change: 1 addition & 0 deletions components/raftstore-v2/src/operation/ready/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
slog_panic!(self.logger(), "failed to clean up region"; "error" => ?e);
});
self.init_apply_trace(write_task);
self.set_flushed_epoch(self.region_state().get_region().get_region_epoch().clone());
self.set_ever_persisted();
}
if self.apply_trace().should_persist() {
Expand Down
1 change: 1 addition & 0 deletions components/raftstore-v2/src/operation/ready/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
for cf in ALL_CFS {
lb.put_flushed_index(region_id, cf, last_index, last_index)
.unwrap();
self.set_flushed_epoch(self.region_state().get_region().get_region_epoch().clone());
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
}

let (path, clean_split) = match self.split_init_mut() {
Expand Down
4 changes: 2 additions & 2 deletions components/raftstore-v2/src/raft/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ impl<EK: KvEngine, R> Apply<EK, R> {
}

#[inline]
pub fn set_sst_applied_index(&mut self, uuid: Vec<Vec<u8>>, apply_index: u64) {
self.sst_apply_state.registe_ssts(uuid, apply_index);
pub fn sst_apply_state(&self) -> &SstApplyState {
&self.sst_apply_state
}

#[inline]
Expand Down
17 changes: 17 additions & 0 deletions components/raftstore-v2/src/raft/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use collections::HashMap;
use engine_traits::{KvEngine, RaftEngine};
use kvproto::{
metapb,
metapb::RegionEpoch,
raft_serverpb::{RaftApplyState, RaftLocalState, RegionLocalState},
};
use raft::{
Expand Down Expand Up @@ -46,6 +47,8 @@ pub struct Storage<EK: KvEngine, ER> {
split_init: Option<Box<SplitInit>>,
/// The flushed index of all CFs.
apply_trace: ApplyTrace,
// The flushed epoch means that the epoch has persisted into the raft engine.
flushed_epoch: RegionEpoch,
}

impl<EK: KvEngine, ER> Debug for Storage<EK, ER> {
Expand Down Expand Up @@ -129,6 +132,18 @@ impl<EK: KvEngine, ER> Storage<EK, ER> {
pub fn has_dirty_data(&self) -> bool {
self.has_dirty_data
}

#[inline]
pub fn set_flushed_epoch(&mut self, epoch: RegionEpoch) {
if util::is_epoch_stale(&self.flushed_epoch, &epoch) {
self.flushed_epoch = epoch;
}
bufferflies marked this conversation as resolved.
Show resolved Hide resolved
}

#[inline]
pub fn flushed_epoch(&self) -> &RegionEpoch {
&self.flushed_epoch
}
}

impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
Expand All @@ -151,6 +166,7 @@ impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
}
};
let region = region_state.get_region();
let epoch = region.get_region_epoch().clone();
let logger = logger.new(o!("region_id" => region.id, "peer_id" => peer.get_id()));
let has_dirty_data =
match engine.get_dirty_mark(region.get_id(), region_state.get_tablet_index()) {
Expand Down Expand Up @@ -183,6 +199,7 @@ impl<EK: KvEngine, ER: RaftEngine> Storage<EK, ER> {
gen_snap_task: RefCell::new(Box::new(None)),
split_init: None,
apply_trace,
flushed_epoch: epoch,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this epoch is the newly epoch and irrelevant with flush.
Will it delete the file before ingest replayed after restart?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q1: epoch refresh only if trace.admin.index has writed in the disk.
Q2: sst will be delete if the sst's epoch is stale than the flush epoch

})
}

Expand Down