2024-01-16 16:30:01 +08:00
|
|
|
use std::collections::HashMap;
|
2022-12-24 10:11:06 -05:00
|
|
|
use std::ops::Bound;
|
2022-12-24 18:07:18 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-01-10 14:25:23 +08:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
2022-12-24 10:11:06 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use bytes::Bytes;
|
2022-12-24 17:13:52 -05:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2022-12-24 10:11:06 -05:00
|
|
|
|
2022-12-24 18:07:18 -05:00
|
|
|
use crate::block::Block;
|
2024-01-18 17:51:24 +08:00
|
|
|
use crate::compact::{
|
|
|
|
|
CompactionController, CompactionOptions, LeveledCompactionController, LeveledCompactionOptions,
|
|
|
|
|
SimpleLeveledCompactionController, SimpleLeveledCompactionOptions, TieredCompactionController,
|
|
|
|
|
};
|
2022-12-24 14:48:57 -05:00
|
|
|
use crate::iterators::merge_iterator::MergeIterator;
|
|
|
|
|
use crate::iterators::two_merge_iterator::TwoMergeIterator;
|
2022-12-24 18:25:41 -05:00
|
|
|
use crate::iterators::StorageIterator;
|
2022-12-24 14:48:57 -05:00
|
|
|
use crate::lsm_iterator::{FusedIterator, LsmIterator};
|
|
|
|
|
use crate::mem_table::{map_bound, MemTable};
|
|
|
|
|
use crate::table::{SsTable, SsTableBuilder, SsTableIterator};
|
2022-12-24 10:11:06 -05:00
|
|
|
|
2022-12-24 18:07:18 -05:00
|
|
|
pub type BlockCache = moka::sync::Cache<(usize, usize), Arc<Block>>;
|
|
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
#[derive(Clone)]
|
2024-01-18 17:51:24 +08:00
|
|
|
pub struct LsmStorageState {
|
2022-12-24 16:27:42 -05:00
|
|
|
/// The current memtable.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub memtable: Arc<MemTable>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// Immutable memtables, from earliest to latest.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub imm_memtables: Vec<Arc<MemTable>>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// L0 SSTs, from earliest to latest.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub l0_sstables: Vec<usize>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// SsTables sorted by key range; L1 - L_max for leveled compaction, or tiers for tiered
|
2024-01-16 16:30:01 +08:00
|
|
|
/// compaction.
|
|
|
|
|
pub levels: Vec<(usize, Vec<usize>)>,
|
2024-01-18 17:51:24 +08:00
|
|
|
/// SST objects.
|
2024-01-16 16:30:01 +08:00
|
|
|
pub sstables: HashMap<usize, Arc<SsTable>>,
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
impl LsmStorageState {
|
|
|
|
|
fn create(options: &LsmStorageOptions) -> Self {
|
|
|
|
|
match &options.compaction_options {
|
|
|
|
|
CompactionOptions::Leveled(LeveledCompactionOptions { max_levels, .. })
|
|
|
|
|
| CompactionOptions::Simple(SimpleLeveledCompactionOptions { max_levels, .. }) => {
|
|
|
|
|
Self {
|
|
|
|
|
memtable: Arc::new(MemTable::create()),
|
|
|
|
|
imm_memtables: Vec::new(),
|
|
|
|
|
l0_sstables: Vec::new(),
|
|
|
|
|
levels: (1..=*max_levels)
|
|
|
|
|
.map(|level| (level, Vec::new()))
|
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
|
sstables: Default::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CompactionOptions::Tiered(_) | CompactionOptions::NoCompaction => Self {
|
|
|
|
|
memtable: Arc::new(MemTable::create()),
|
|
|
|
|
imm_memtables: Vec::new(),
|
|
|
|
|
l0_sstables: Vec::new(),
|
|
|
|
|
levels: Vec::new(),
|
|
|
|
|
sstables: Default::default(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct LsmStorageOptions {
|
|
|
|
|
block_size: usize,
|
|
|
|
|
target_sst_size: usize,
|
|
|
|
|
compaction_options: CompactionOptions,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl LsmStorageOptions {
|
|
|
|
|
pub fn default_for_week1_test() -> Self {
|
2022-12-24 10:11:06 -05:00
|
|
|
Self {
|
2024-01-18 17:51:24 +08:00
|
|
|
block_size: 4096,
|
|
|
|
|
target_sst_size: 2 << 20,
|
|
|
|
|
compaction_options: CompactionOptions::NoCompaction,
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The storage interface of the LSM tree.
|
2024-01-18 17:51:24 +08:00
|
|
|
pub(crate) struct LsmStorageInner {
|
|
|
|
|
pub(crate) state: Arc<RwLock<Arc<LsmStorageState>>>,
|
|
|
|
|
state_lock: Mutex<()>,
|
2022-12-24 18:07:18 -05:00
|
|
|
path: PathBuf,
|
2024-01-10 14:25:23 +08:00
|
|
|
pub(crate) block_cache: Arc<BlockCache>,
|
|
|
|
|
next_sst_id: AtomicUsize,
|
2024-01-18 17:51:24 +08:00
|
|
|
options: Arc<LsmStorageOptions>,
|
|
|
|
|
compaction_controller: CompactionController,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct MiniLsm {
|
|
|
|
|
inner: Arc<LsmStorageInner>,
|
|
|
|
|
compaction_notifier: std::sync::mpsc::Sender<()>,
|
|
|
|
|
compaction_thread: Mutex<Option<std::thread::JoinHandle<()>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Drop for MiniLsm {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
self.compaction_notifier.send(()).ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MiniLsm {
|
|
|
|
|
pub fn close(&self) -> Result<()> {
|
|
|
|
|
self.compaction_notifier.send(()).ok();
|
|
|
|
|
let mut compaction_thread = self.compaction_thread.lock();
|
|
|
|
|
if let Some(mut compaction_thread) = compaction_thread.take() {
|
|
|
|
|
compaction_thread
|
|
|
|
|
.join()
|
|
|
|
|
.map_err(|e| anyhow::anyhow!("{:?}", e))?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn open(path: impl AsRef<Path>, options: LsmStorageOptions) -> Result<Arc<Self>> {
|
|
|
|
|
let inner = Arc::new(LsmStorageInner::open(path, options)?);
|
|
|
|
|
let (tx, rx) = std::sync::mpsc::channel();
|
|
|
|
|
let compaction_thread = inner.spawn_compaction_thread(rx)?;
|
|
|
|
|
Ok(Arc::new(Self {
|
|
|
|
|
inner,
|
|
|
|
|
compaction_notifier: tx,
|
|
|
|
|
compaction_thread: Mutex::new(compaction_thread),
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
|
|
|
|
|
self.inner.get(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
|
|
|
|
self.inner.put(key, value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn delete(&self, key: &[u8]) -> Result<()> {
|
|
|
|
|
self.inner.delete(key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn scan(
|
|
|
|
|
&self,
|
|
|
|
|
lower: Bound<&[u8]>,
|
|
|
|
|
upper: Bound<&[u8]>,
|
|
|
|
|
) -> Result<FusedIterator<LsmIterator>> {
|
|
|
|
|
self.inner.scan(lower, upper)
|
|
|
|
|
}
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
impl LsmStorageInner {
|
2024-01-10 14:25:23 +08:00
|
|
|
pub(crate) fn next_sst_id(&self) -> usize {
|
|
|
|
|
self.next_sst_id
|
|
|
|
|
.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
pub(crate) fn open(path: impl AsRef<Path>, options: LsmStorageOptions) -> Result<Self> {
|
2022-12-24 10:11:06 -05:00
|
|
|
Ok(Self {
|
2024-01-18 17:51:24 +08:00
|
|
|
state: Arc::new(RwLock::new(Arc::new(LsmStorageState::create(&options)))),
|
|
|
|
|
state_lock: Mutex::new(()),
|
2022-12-24 18:07:18 -05:00
|
|
|
path: path.as_ref().to_path_buf(),
|
2024-01-10 14:25:23 +08:00
|
|
|
block_cache: Arc::new(BlockCache::new(1 << 20)), // 4GB block cache,
|
|
|
|
|
next_sst_id: AtomicUsize::new(1),
|
2024-01-18 17:51:24 +08:00
|
|
|
compaction_controller: match &options.compaction_options {
|
|
|
|
|
CompactionOptions::Leveled(options) => {
|
|
|
|
|
CompactionController::Leveled(LeveledCompactionController::new(options.clone()))
|
|
|
|
|
}
|
|
|
|
|
CompactionOptions::Tiered(options) => {
|
|
|
|
|
CompactionController::Tiered(TieredCompactionController::new(options.clone()))
|
|
|
|
|
}
|
|
|
|
|
CompactionOptions::Simple(options) => CompactionController::Simple(
|
|
|
|
|
SimpleLeveledCompactionController::new(options.clone()),
|
|
|
|
|
),
|
|
|
|
|
CompactionOptions::NoCompaction => CompactionController::NoCompaction,
|
|
|
|
|
},
|
|
|
|
|
options: options.into(),
|
2022-12-24 10:11:06 -05:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Get a key from the storage. In day 7, this can be further optimized by using a bloom filter.
|
2022-12-24 10:11:06 -05:00
|
|
|
pub fn get(&self, key: &[u8]) -> Result<Option<Bytes>> {
|
2022-12-24 17:13:52 -05:00
|
|
|
let snapshot = {
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2022-12-24 17:13:52 -05:00
|
|
|
Arc::clone(&guard)
|
|
|
|
|
}; // drop global lock here
|
|
|
|
|
|
2022-12-24 16:27:42 -05:00
|
|
|
// Search on the current memtable.
|
|
|
|
|
if let Some(value) = snapshot.memtable.get(key) {
|
|
|
|
|
if value.is_empty() {
|
|
|
|
|
// found tomestone, return key not exists
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
return Ok(Some(value));
|
|
|
|
|
}
|
|
|
|
|
// Search on immutable memtables.
|
|
|
|
|
for memtable in snapshot.imm_memtables.iter().rev() {
|
|
|
|
|
if let Some(value) = memtable.get(key) {
|
2022-12-24 10:11:06 -05:00
|
|
|
if value.is_empty() {
|
|
|
|
|
// found tomestone, return key not exists
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
return Ok(Some(value));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-10 14:12:34 +08:00
|
|
|
let mut iters = Vec::with_capacity(snapshot.l0_sstables.len());
|
2022-12-24 14:48:57 -05:00
|
|
|
for table in snapshot.l0_sstables.iter().rev() {
|
|
|
|
|
iters.push(Box::new(SsTableIterator::create_and_seek_to_key(
|
2024-01-16 16:30:01 +08:00
|
|
|
snapshot.sstables[table].clone(),
|
2022-12-24 14:48:57 -05:00
|
|
|
key,
|
|
|
|
|
)?));
|
|
|
|
|
}
|
|
|
|
|
let iter = MergeIterator::create(iters);
|
|
|
|
|
if iter.is_valid() {
|
|
|
|
|
return Ok(Some(Bytes::copy_from_slice(iter.value())));
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Put a key-value pair into the storage by writing into the current memtable.
|
2022-12-24 14:48:57 -05:00
|
|
|
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
|
2022-12-24 10:11:06 -05:00
|
|
|
assert!(!value.is_empty(), "value cannot be empty");
|
|
|
|
|
assert!(!key.is_empty(), "key cannot be empty");
|
2022-12-24 17:13:52 -05:00
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2022-12-24 17:13:52 -05:00
|
|
|
guard.memtable.put(key, value);
|
|
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
Ok(())
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Remove a key from the storage by writing an empty value.
|
2022-12-24 14:48:57 -05:00
|
|
|
pub fn delete(&self, key: &[u8]) -> Result<()> {
|
2022-12-24 16:27:42 -05:00
|
|
|
assert!(!key.is_empty(), "key cannot be empty");
|
2022-12-24 17:13:52 -05:00
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2022-12-24 17:13:52 -05:00
|
|
|
guard.memtable.put(key, b"");
|
|
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
Ok(())
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-01-10 14:25:23 +08:00
|
|
|
pub(crate) fn path_of_sst(&self, id: usize) -> PathBuf {
|
2022-12-24 18:07:18 -05:00
|
|
|
self.path.join(format!("{:05}.sst", id))
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-18 17:51:24 +08:00
|
|
|
/// Force freeze the current memetable to an immutable memtable
|
|
|
|
|
pub fn force_freeze_memtable(&self) -> Result<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Force flush the all immutable memtables to disk
|
|
|
|
|
pub fn force_flush_imm_memtables(&self) -> Result<()> {
|
|
|
|
|
let _flush_lock = self.state_lock.lock();
|
2022-12-24 16:27:42 -05:00
|
|
|
|
|
|
|
|
let flush_memtable;
|
2022-12-24 18:07:18 -05:00
|
|
|
let sst_id;
|
2022-12-24 16:27:42 -05:00
|
|
|
|
|
|
|
|
// Move mutable memtable to immutable memtables.
|
|
|
|
|
{
|
2024-01-18 17:51:24 +08:00
|
|
|
let mut guard = self.state.write();
|
2022-12-24 16:27:42 -05:00
|
|
|
// Swap the current memtable with a new one.
|
|
|
|
|
let mut snapshot = guard.as_ref().clone();
|
|
|
|
|
let memtable = std::mem::replace(&mut snapshot.memtable, Arc::new(MemTable::create()));
|
|
|
|
|
flush_memtable = memtable.clone();
|
2024-01-10 14:25:23 +08:00
|
|
|
sst_id = self.next_sst_id();
|
2022-12-24 16:27:42 -05:00
|
|
|
// Add the memtable to the immutable memtables.
|
2022-12-24 17:13:52 -05:00
|
|
|
snapshot.imm_memtables.push(memtable);
|
2022-12-24 16:27:42 -05:00
|
|
|
// Update the snapshot.
|
2022-12-24 17:13:52 -05:00
|
|
|
*guard = Arc::new(snapshot);
|
2022-12-24 16:27:42 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 18:07:18 -05:00
|
|
|
// At this point, the old memtable should be disabled for write, and all write threads
|
|
|
|
|
// should be operating on the new memtable. We can safely flush the old memtable to
|
|
|
|
|
// disk.
|
2022-12-24 14:48:57 -05:00
|
|
|
|
|
|
|
|
let mut builder = SsTableBuilder::new(4096);
|
2022-12-24 16:27:42 -05:00
|
|
|
flush_memtable.flush(&mut builder)?;
|
2022-12-24 18:07:18 -05:00
|
|
|
let sst = Arc::new(builder.build(
|
|
|
|
|
sst_id,
|
|
|
|
|
Some(self.block_cache.clone()),
|
|
|
|
|
self.path_of_sst(sst_id),
|
|
|
|
|
)?);
|
2022-12-24 16:27:42 -05:00
|
|
|
|
|
|
|
|
// Add the flushed L0 table to the list.
|
|
|
|
|
{
|
2024-01-18 17:51:24 +08:00
|
|
|
let mut guard = self.state.write();
|
2022-12-24 16:27:42 -05:00
|
|
|
let mut snapshot = guard.as_ref().clone();
|
|
|
|
|
// Remove the memtable from the immutable memtables.
|
|
|
|
|
snapshot.imm_memtables.pop();
|
|
|
|
|
// Add L0 table
|
2024-01-18 17:51:24 +08:00
|
|
|
if self.compaction_controller.flush_to_l0() {
|
|
|
|
|
// In leveled compaction or no compaction, simply flush to L0
|
|
|
|
|
snapshot.l0_sstables.push(sst_id);
|
|
|
|
|
} else {
|
|
|
|
|
// In tiered compaction, create a new tier
|
|
|
|
|
snapshot.levels.insert(0, (sst_id, vec![sst_id]));
|
|
|
|
|
}
|
2024-01-16 16:30:01 +08:00
|
|
|
snapshot.sstables.insert(sst_id, sst);
|
2022-12-24 16:27:42 -05:00
|
|
|
// Update the snapshot.
|
2022-12-24 17:13:52 -05:00
|
|
|
*guard = Arc::new(snapshot);
|
2022-12-24 16:27:42 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 14:48:57 -05:00
|
|
|
Ok(())
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-24 17:13:52 -05:00
|
|
|
/// Create an iterator over a range of keys.
|
2022-12-24 14:48:57 -05:00
|
|
|
pub fn scan(
|
|
|
|
|
&self,
|
|
|
|
|
lower: Bound<&[u8]>,
|
|
|
|
|
upper: Bound<&[u8]>,
|
|
|
|
|
) -> Result<FusedIterator<LsmIterator>> {
|
2022-12-24 17:13:52 -05:00
|
|
|
let snapshot = {
|
2024-01-18 17:51:24 +08:00
|
|
|
let guard = self.state.read();
|
2022-12-24 17:13:52 -05:00
|
|
|
Arc::clone(&guard)
|
|
|
|
|
}; // drop global lock here
|
2022-12-24 14:48:57 -05:00
|
|
|
|
2024-01-10 14:12:34 +08:00
|
|
|
let mut memtable_iters = Vec::with_capacity(snapshot.imm_memtables.len() + 1);
|
2022-12-24 17:13:52 -05:00
|
|
|
memtable_iters.push(Box::new(snapshot.memtable.scan(lower, upper)));
|
2022-12-24 16:27:42 -05:00
|
|
|
for memtable in snapshot.imm_memtables.iter().rev() {
|
2022-12-24 17:13:52 -05:00
|
|
|
memtable_iters.push(Box::new(memtable.scan(lower, upper)));
|
2022-12-24 14:48:57 -05:00
|
|
|
}
|
|
|
|
|
let memtable_iter = MergeIterator::create(memtable_iters);
|
|
|
|
|
|
2024-01-10 14:12:34 +08:00
|
|
|
let mut table_iters = Vec::with_capacity(snapshot.l0_sstables.len());
|
2024-01-16 16:30:01 +08:00
|
|
|
for table_id in snapshot.l0_sstables.iter().rev() {
|
|
|
|
|
let table = snapshot.sstables[table_id].clone();
|
2022-12-24 14:48:57 -05:00
|
|
|
let iter = match lower {
|
2024-01-16 16:30:01 +08:00
|
|
|
Bound::Included(key) => SsTableIterator::create_and_seek_to_key(table, key)?,
|
2022-12-24 14:48:57 -05:00
|
|
|
Bound::Excluded(key) => {
|
2024-01-16 16:30:01 +08:00
|
|
|
let mut iter = SsTableIterator::create_and_seek_to_key(table, key)?;
|
2022-12-24 14:48:57 -05:00
|
|
|
if iter.is_valid() && iter.key() == key {
|
|
|
|
|
iter.next()?;
|
|
|
|
|
}
|
|
|
|
|
iter
|
|
|
|
|
}
|
2024-01-16 16:30:01 +08:00
|
|
|
Bound::Unbounded => SsTableIterator::create_and_seek_to_first(table)?,
|
2022-12-24 14:48:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
table_iters.push(Box::new(iter));
|
|
|
|
|
}
|
|
|
|
|
let table_iter = MergeIterator::create(table_iters);
|
|
|
|
|
|
|
|
|
|
let iter = TwoMergeIterator::create(memtable_iter, table_iter)?;
|
|
|
|
|
|
2024-01-18 15:15:51 +08:00
|
|
|
Ok(FusedIterator::new(LsmIterator::new(
|
|
|
|
|
iter,
|
|
|
|
|
map_bound(upper),
|
|
|
|
|
)?))
|
2022-12-24 10:11:06 -05:00
|
|
|
}
|
|
|
|
|
}
|