From 76acf12d8e0078b45d687c4ad6c8cdc8f048e4b6 Mon Sep 17 00:00:00 2001 From: Kould <2435992353@qq.com> Date: Mon, 8 Jan 2024 21:02:45 +0800 Subject: [PATCH] fix: `Compaction` cannot be performed when writing continuous transactions --- Cargo.toml | 2 +- src/kernel/lsm/compactor.rs | 4 +--- src/kernel/lsm/mem_table.rs | 18 +++++++----------- src/kernel/lsm/mod.rs | 2 +- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index efa4412..1cf6895 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kip_db" -version = "0.1.2-alpha.20" +version = "0.1.2-alpha.21" edition = "2021" authors = ["Kould "] description = "轻量级、异步 基于LSM Leveled Compaction K-V数据库" diff --git a/src/kernel/lsm/compactor.rs b/src/kernel/lsm/compactor.rs index de2f980..da0e5f4 100644 --- a/src/kernel/lsm/compactor.rs +++ b/src/kernel/lsm/compactor.rs @@ -57,9 +57,7 @@ impl Compactor { &mut self, option_tx: Option>, ) -> KernelResult<()> { - let is_force = option_tx.is_some(); - - if let Some((gen, values)) = self.mem_table().try_swap(is_force)? { + if let Some((gen, values)) = self.mem_table().swap()? { if !values.is_empty() { let start = Instant::now(); // 目前minor触发major时是同步进行的,所以此处对live_tag是在此方法体保持存活 diff --git a/src/kernel/lsm/mem_table.rs b/src/kernel/lsm/mem_table.rs index 1ef591e..5aec558 100644 --- a/src/kernel/lsm/mem_table.rs +++ b/src/kernel/lsm/mem_table.rs @@ -160,14 +160,10 @@ pub(crate) struct TableInner { } macro_rules! check_count { - ($count:ident, $is_force:ident) => { + ($count:ident) => { if 0 != $count.load(Acquire) { - if $is_force { - std::hint::spin_loop(); - continue; - } else { - return Ok(None); - } + std::hint::spin_loop(); + continue; } }; } @@ -270,11 +266,11 @@ impl MemTable { } /// MemTable将数据弹出并转移到immut table中 (弹出数据为转移至immut table中数据的迭代器) - pub(crate) fn try_swap(&self, is_force: bool) -> KernelResult)>> { + pub(crate) fn swap(&self) -> KernelResult)>> { let count = &self.tx_count; loop { - check_count!(count, is_force); + check_count!(count); let mut inner = self.inner.lock(); // 二重检测防止lock时(前)突然出现事务 @@ -282,7 +278,7 @@ impl MemTable { // 因此不会对读写进行干扰 // 并且事务即使在lock后出现,所持有的seq为该压缩之前, // 也不会丢失该seq的_mem,因为转移到了_immut,可以从_immut得到对应seq的数据 - check_count!(count, is_force); + check_count!(count); return if !inner._mem.is_empty() { inner.trigger.reset(); @@ -510,7 +506,7 @@ mod tests { let _ = mem_table .insert_data((Bytes::from(vec![b'k', b'2']), Some(Bytes::from(vec![b'2']))))?; - let (_, mut vec) = mem_table.try_swap(false)?.unwrap(); + let (_, mut vec) = mem_table.swap()?.unwrap(); assert_eq!( vec.pop(), diff --git a/src/kernel/lsm/mod.rs b/src/kernel/lsm/mod.rs index ba8d5fd..fcd121b 100644 --- a/src/kernel/lsm/mod.rs +++ b/src/kernel/lsm/mod.rs @@ -14,7 +14,7 @@ mod mem_table; pub mod mvcc; pub mod storage; mod table; -mod trigger; +pub mod trigger; pub mod version; const MAX_LEVEL: usize = 4;