Skip to content

Commit bf8f076

Browse files
authored
feat(storage): Splitting table change log from HummockVersion on CN side (#20050)
1 parent 323d8a2 commit bf8f076

File tree

8 files changed

+242
-57
lines changed

8 files changed

+242
-57
lines changed

src/storage/hummock_sdk/src/change_log.rs

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ impl<T> TableChangeLogCommon<T> {
5858
.flat_map(|epoch_change_log| epoch_change_log.epochs.iter())
5959
.cloned()
6060
}
61+
62+
pub(crate) fn change_log_into_iter(self) -> impl Iterator<Item = EpochNewChangeLogCommon<T>> {
63+
self.0.into_iter()
64+
}
65+
66+
pub(crate) fn change_log_iter_mut(
67+
&mut self,
68+
) -> impl Iterator<Item = &mut EpochNewChangeLogCommon<T>> {
69+
self.0.iter_mut()
70+
}
6171
}
6272

6373
pub type TableChangeLog = TableChangeLogCommon<SstableInfo>;

src/storage/hummock_sdk/src/compact_task.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ impl CompactTask {
115115
}
116116

117117
impl From<PbCompactTask> for CompactTask {
118-
#[expect(deprecated)]
119118
fn from(pb_compact_task: PbCompactTask) -> Self {
119+
#[expect(deprecated)]
120120
Self {
121121
input_ssts: pb_compact_task
122122
.input_ssts
@@ -168,8 +168,8 @@ impl From<PbCompactTask> for CompactTask {
168168
}
169169

170170
impl From<&PbCompactTask> for CompactTask {
171-
#[expect(deprecated)]
172171
fn from(pb_compact_task: &PbCompactTask) -> Self {
172+
#[expect(deprecated)]
173173
Self {
174174
input_ssts: pb_compact_task
175175
.input_ssts
@@ -221,8 +221,8 @@ impl From<&PbCompactTask> for CompactTask {
221221
}
222222

223223
impl From<CompactTask> for PbCompactTask {
224-
#[expect(deprecated)]
225224
fn from(compact_task: CompactTask) -> Self {
225+
#[expect(deprecated)]
226226
Self {
227227
input_ssts: compact_task
228228
.input_ssts
@@ -272,8 +272,8 @@ impl From<CompactTask> for PbCompactTask {
272272
}
273273

274274
impl From<&CompactTask> for PbCompactTask {
275-
#[expect(deprecated)]
276275
fn from(compact_task: &CompactTask) -> Self {
276+
#[expect(deprecated)]
277277
Self {
278278
input_ssts: compact_task
279279
.input_ssts

src/storage/hummock_sdk/src/compaction_group/hummock_version_ext.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::level::{Level, LevelCommon, Levels, OverlappingLevel};
3636
use crate::sstable_info::SstableInfo;
3737
use crate::table_watermark::{ReadTableWatermark, TableWatermarks};
3838
use crate::version::{
39-
GroupDelta, GroupDeltaCommon, HummockVersion, HummockVersionCommon, HummockVersionDelta,
39+
GroupDelta, GroupDeltaCommon, HummockVersion, HummockVersionCommon, HummockVersionDeltaCommon,
4040
HummockVersionStateTableInfo, IntraLevelDelta, IntraLevelDeltaCommon, ObjectIdReader,
4141
SstableIdReader,
4242
};
@@ -50,7 +50,7 @@ pub struct SstDeltaInfo {
5050

5151
pub type BranchedSstInfo = HashMap<CompactionGroupId, Vec<HummockSstableId>>;
5252

53-
impl HummockVersion {
53+
impl<L> HummockVersionCommon<SstableInfo, L> {
5454
pub fn get_compaction_group_levels(&self, compaction_group_id: CompactionGroupId) -> &Levels {
5555
self.levels
5656
.get(&compaction_group_id)
@@ -187,7 +187,7 @@ pub fn safe_epoch_read_table_watermarks_impl(
187187
.collect()
188188
}
189189

190-
impl HummockVersion {
190+
impl<L: Clone> HummockVersionCommon<SstableInfo, L> {
191191
pub fn count_new_ssts_in_group_split(
192192
&self,
193193
parent_group_id: CompactionGroupId,
@@ -356,7 +356,10 @@ impl HummockVersion {
356356
.all(|level| !level.table_infos.is_empty()));
357357
}
358358

359-
pub fn build_sst_delta_infos(&self, version_delta: &HummockVersionDelta) -> Vec<SstDeltaInfo> {
359+
pub fn build_sst_delta_infos(
360+
&self,
361+
version_delta: &HummockVersionDeltaCommon<SstableInfo, L>,
362+
) -> Vec<SstDeltaInfo> {
360363
let mut infos = vec![];
361364

362365
// Skip trivial move delta for refiller
@@ -459,7 +462,10 @@ impl HummockVersion {
459462
infos
460463
}
461464

462-
pub fn apply_version_delta(&mut self, version_delta: &HummockVersionDelta) {
465+
pub fn apply_version_delta(
466+
&mut self,
467+
version_delta: &HummockVersionDeltaCommon<SstableInfo, L>,
468+
) {
463469
assert_eq!(self.id, version_delta.prev_id);
464470

465471
let (changed_table_info, mut is_commit_epoch) = self.state_table_info.apply_delta(
@@ -934,12 +940,6 @@ impl<T> HummockVersionCommon<T>
934940
where
935941
T: SstableIdReader + ObjectIdReader,
936942
{
937-
pub fn get_combined_levels(&self) -> impl Iterator<Item = &'_ LevelCommon<T>> + '_ {
938-
self.levels
939-
.values()
940-
.flat_map(|level| level.l0.sub_levels.iter().rev().chain(level.levels.iter()))
941-
}
942-
943943
pub fn get_object_ids(&self) -> HashSet<HummockSstableObjectId> {
944944
self.get_sst_infos().map(|s| s.object_id()).collect()
945945
}
@@ -1094,6 +1094,14 @@ impl Levels {
10941094
}
10951095
}
10961096

1097+
impl<T, L> HummockVersionCommon<T, L> {
1098+
pub fn get_combined_levels(&self) -> impl Iterator<Item = &'_ LevelCommon<T>> + '_ {
1099+
self.levels
1100+
.values()
1101+
.flat_map(|level| level.l0.sub_levels.iter().rev().chain(level.levels.iter()))
1102+
}
1103+
}
1104+
10971105
pub fn build_initial_compaction_group_levels(
10981106
group_id: CompactionGroupId,
10991107
compaction_config: &CompactionConfig,

src/storage/hummock_sdk/src/version.rs

+103-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ use risingwave_pb::hummock::{
3030
};
3131
use tracing::warn;
3232

33-
use crate::change_log::{ChangeLogDeltaCommon, TableChangeLogCommon};
33+
use crate::change_log::{
34+
ChangeLogDeltaCommon, EpochNewChangeLogCommon, TableChangeLog, TableChangeLogCommon,
35+
};
3436
use crate::compaction_group::hummock_version_ext::build_initial_compaction_group_levels;
3537
use crate::compaction_group::StaticCompactionGroupId;
3638
use crate::level::LevelsCommon;
@@ -217,18 +219,20 @@ impl HummockVersionStateTableInfo {
217219
}
218220

219221
#[derive(Debug, Clone, PartialEq)]
220-
pub struct HummockVersionCommon<T> {
222+
pub struct HummockVersionCommon<T, L = T> {
221223
pub id: HummockVersionId,
222224
pub levels: HashMap<CompactionGroupId, LevelsCommon<T>>,
223225
#[deprecated]
224226
pub(crate) max_committed_epoch: u64,
225227
pub table_watermarks: HashMap<TableId, Arc<TableWatermarks>>,
226-
pub table_change_log: HashMap<TableId, TableChangeLogCommon<T>>,
228+
pub table_change_log: HashMap<TableId, TableChangeLogCommon<L>>,
227229
pub state_table_info: HummockVersionStateTableInfo,
228230
}
229231

230232
pub type HummockVersion = HummockVersionCommon<SstableInfo>;
231233

234+
pub type LocalHummockVersion = HummockVersionCommon<SstableInfo, ()>;
235+
232236
impl Default for HummockVersion {
233237
fn default() -> Self {
234238
HummockVersion::from(&PbHummockVersion::default())
@@ -433,13 +437,6 @@ impl HummockVersion {
433437
}
434438
}
435439

436-
pub fn table_committed_epoch(&self, table_id: TableId) -> Option<u64> {
437-
self.state_table_info
438-
.info()
439-
.get(&table_id)
440-
.map(|info| info.committed_epoch)
441-
}
442-
443440
pub fn create_init_version(default_compaction_config: Arc<CompactionConfig>) -> HummockVersion {
444441
#[expect(deprecated)]
445442
let mut init_version = HummockVersion {
@@ -476,10 +473,41 @@ impl HummockVersion {
476473
state_table_info_delta: Default::default(),
477474
}
478475
}
476+
477+
pub fn split_change_log(mut self) -> (LocalHummockVersion, HashMap<TableId, TableChangeLog>) {
478+
let table_change_log = {
479+
let mut table_change_log = HashMap::new();
480+
for (table_id, log) in &mut self.table_change_log {
481+
let change_log_iter =
482+
log.change_log_iter_mut()
483+
.map(|item| EpochNewChangeLogCommon {
484+
new_value: std::mem::take(&mut item.new_value),
485+
old_value: std::mem::take(&mut item.old_value),
486+
epochs: item.epochs.clone(),
487+
});
488+
table_change_log.insert(*table_id, TableChangeLogCommon::new(change_log_iter));
489+
}
490+
491+
table_change_log
492+
};
493+
494+
let local_version = LocalHummockVersion::from(self);
495+
496+
(local_version, table_change_log)
497+
}
498+
}
499+
500+
impl<T, L> HummockVersionCommon<T, L> {
501+
pub fn table_committed_epoch(&self, table_id: TableId) -> Option<u64> {
502+
self.state_table_info
503+
.info()
504+
.get(&table_id)
505+
.map(|info| info.committed_epoch)
506+
}
479507
}
480508

481509
#[derive(Debug, PartialEq, Clone)]
482-
pub struct HummockVersionDeltaCommon<T> {
510+
pub struct HummockVersionDeltaCommon<T, L = T> {
483511
pub id: HummockVersionId,
484512
pub prev_id: HummockVersionId,
485513
pub group_deltas: HashMap<CompactionGroupId, GroupDeltasCommon<T>>,
@@ -488,12 +516,14 @@ pub struct HummockVersionDeltaCommon<T> {
488516
pub trivial_move: bool,
489517
pub new_table_watermarks: HashMap<TableId, TableWatermarks>,
490518
pub removed_table_ids: HashSet<TableId>,
491-
pub change_log_delta: HashMap<TableId, ChangeLogDeltaCommon<T>>,
519+
pub change_log_delta: HashMap<TableId, ChangeLogDeltaCommon<L>>,
492520
pub state_table_info_delta: HashMap<TableId, StateTableInfoDelta>,
493521
}
494522

495523
pub type HummockVersionDelta = HummockVersionDeltaCommon<SstableInfo>;
496524

525+
pub type LocalHummockVersionDelta = HummockVersionDeltaCommon<SstableInfo, ()>;
526+
497527
impl Default for HummockVersionDelta {
498528
fn default() -> Self {
499529
HummockVersionDelta::from(&PbHummockVersionDelta::default())
@@ -1095,3 +1125,64 @@ where
10951125
self.into()
10961126
}
10971127
}
1128+
1129+
impl From<HummockVersionDelta> for LocalHummockVersionDelta {
1130+
#[expect(deprecated)]
1131+
fn from(delta: HummockVersionDelta) -> Self {
1132+
Self {
1133+
id: delta.id,
1134+
prev_id: delta.prev_id,
1135+
group_deltas: delta.group_deltas,
1136+
max_committed_epoch: delta.max_committed_epoch,
1137+
trivial_move: delta.trivial_move,
1138+
new_table_watermarks: delta.new_table_watermarks,
1139+
removed_table_ids: delta.removed_table_ids,
1140+
change_log_delta: delta
1141+
.change_log_delta
1142+
.into_iter()
1143+
.map(|(k, v)| {
1144+
(
1145+
k,
1146+
ChangeLogDeltaCommon {
1147+
truncate_epoch: v.truncate_epoch,
1148+
new_log: EpochNewChangeLogCommon {
1149+
epochs: v.new_log.epochs,
1150+
new_value: Vec::new(),
1151+
old_value: Vec::new(),
1152+
},
1153+
},
1154+
)
1155+
})
1156+
.collect(),
1157+
state_table_info_delta: delta.state_table_info_delta,
1158+
}
1159+
}
1160+
}
1161+
1162+
impl From<HummockVersion> for LocalHummockVersion {
1163+
#[expect(deprecated)]
1164+
fn from(version: HummockVersion) -> Self {
1165+
Self {
1166+
id: version.id,
1167+
levels: version.levels,
1168+
max_committed_epoch: version.max_committed_epoch,
1169+
table_watermarks: version.table_watermarks,
1170+
table_change_log: version
1171+
.table_change_log
1172+
.into_iter()
1173+
.map(|(k, v)| {
1174+
let epoch_new_change_logs: Vec<EpochNewChangeLogCommon<()>> = v
1175+
.change_log_into_iter()
1176+
.map(|epoch_new_change_log| EpochNewChangeLogCommon {
1177+
epochs: epoch_new_change_log.epochs,
1178+
new_value: Vec::new(),
1179+
old_value: Vec::new(),
1180+
})
1181+
.collect();
1182+
(k, TableChangeLogCommon::new(epoch_new_change_logs))
1183+
})
1184+
.collect(),
1185+
state_table_info: version.state_table_info,
1186+
}
1187+
}
1188+
}

0 commit comments

Comments
 (0)