-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Chi Z <iskyzh@gmail.com>
- Loading branch information
Showing
11 changed files
with
216 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,46 @@ | ||
# Snack Time: Compaction Filter | ||
# Snack Time: Compaction Filters | ||
|
||
Congratulations! You made it there! In the previous chapter, you made your LSM engine multi-version capable, and the users can use transaction APIs to interact with your storage engine. At the end of this week, we will implement some easy but important features of the storage engine. Welcome to Mini-LSM's week 3 snack time! | ||
|
||
In this chapter, we will generalize our compaction garbage collection logic to become compaction filters. | ||
|
||
For now, our compaction will simply retain the keys above the watermark and the latest version of the keys below the watermark. We can add some magic to the compaction process to help the user collect some unused data automatically as a background job. | ||
|
||
Consider a case that the user uses Mini-LSM to store database tables. Each row in the table are prefixed with the table name. For example, | ||
|
||
``` | ||
table1_key1 -> row | ||
table1_key2 -> row | ||
table1_key3 -> row | ||
table2_key1 -> row | ||
table2_key2 -> row | ||
``` | ||
|
||
Now the user executes `DROP TABLE table1`. The engine will need to clean up all the data beginning with `table1`. | ||
|
||
There are a lot of ways to achieve the goal. The user of Mini-LSM can scan all the keys beginning with `table1` and requests the engine to delete it. However, scanning a very large database might be slow, and it will generate the same number of delete tombstones as the existing keys. Therefore, scan-and-delete will not free up the space occupied by the dropped table -- instead, it will add more data to the engine and the space can only be reclaimed when the tombstones reach the bottom level of the engine. | ||
|
||
Or, they can create column families (we will talk about this in *rest of your life* chapter). They store each table in a column family, which is a standalone LSM state, and directly remove the SST files corresponding to the column family when the user drop the table. | ||
|
||
In this tutorial, we will implement the third approach: compaction filters. Compaction filters can be dynamically added to the engine at runtime. During the compaction, if a key matching the compaction filter is found, we can silently remove it in the background. Therefore, the user can attach a compaction filter of `prefix=table1` to the engine, and all these keys will be removed during compaction. | ||
|
||
## Task 1: Compaction Filter | ||
|
||
In this task, you will need to modify: | ||
|
||
``` | ||
src/compact.rs | ||
``` | ||
|
||
You can iterate all compaction filters in `LsmStorageInner::compaction_filters`. If the first version of the key below watermark matches the compaction filter, simply remove it instead of keeping it in the SST file. | ||
|
||
To run test cases, | ||
|
||
``` | ||
cargo x copy-test --week 3 --day 7 | ||
cargo x scheck | ||
``` | ||
|
||
You can assume that the user will not get the keys within the prefix filter range. And, they will not scan the keys in the prefix range. Therefore, it is okay to return a wrong value when a user requests the keys in the prefix filter range (i.e., undefined behavior). | ||
|
||
{{#include copyright.md}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ mod week3_day3; | |
mod week3_day4; | ||
mod week3_day5; | ||
mod week3_day6; | ||
mod week3_day7; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use bytes::Bytes; | ||
use tempfile::tempdir; | ||
|
||
use crate::{ | ||
compact::CompactionOptions, | ||
lsm_storage::{CompactionFilter, LsmStorageOptions, MiniLsm, WriteBatchRecord}, | ||
}; | ||
|
||
use super::harness::{check_iter_result_by_key, construct_merge_iterator_over_storage}; | ||
|
||
#[test] | ||
fn test_task3_mvcc_compaction() { | ||
let dir = tempdir().unwrap(); | ||
let options = LsmStorageOptions::default_for_week2_test(CompactionOptions::NoCompaction); | ||
let storage = MiniLsm::open(&dir, options.clone()).unwrap(); | ||
storage | ||
.write_batch(&[ | ||
WriteBatchRecord::Put("table1_a", "1"), | ||
WriteBatchRecord::Put("table1_b", "1"), | ||
WriteBatchRecord::Put("table1_c", "1"), | ||
WriteBatchRecord::Put("table2_a", "1"), | ||
WriteBatchRecord::Put("table2_b", "1"), | ||
WriteBatchRecord::Put("table2_c", "1"), | ||
]) | ||
.unwrap(); | ||
storage.force_flush().unwrap(); | ||
let snapshot0 = storage.new_txn().unwrap(); | ||
storage | ||
.write_batch(&[ | ||
WriteBatchRecord::Put("table1_a", "2"), | ||
WriteBatchRecord::Del("table1_b"), | ||
WriteBatchRecord::Put("table1_c", "2"), | ||
WriteBatchRecord::Put("table2_a", "2"), | ||
WriteBatchRecord::Del("table2_b"), | ||
WriteBatchRecord::Put("table2_c", "2"), | ||
]) | ||
.unwrap(); | ||
storage.force_flush().unwrap(); | ||
storage.add_compaction_filter(CompactionFilter::Prefix(Bytes::from("table2_"))); | ||
storage.force_full_compaction().unwrap(); | ||
|
||
let mut iter = construct_merge_iterator_over_storage(&storage.inner.state.read()); | ||
check_iter_result_by_key( | ||
&mut iter, | ||
vec![ | ||
(Bytes::from("table1_a"), Bytes::from("2")), | ||
(Bytes::from("table1_a"), Bytes::from("1")), | ||
(Bytes::from("table1_b"), Bytes::new()), | ||
(Bytes::from("table1_b"), Bytes::from("1")), | ||
(Bytes::from("table1_c"), Bytes::from("2")), | ||
(Bytes::from("table1_c"), Bytes::from("1")), | ||
(Bytes::from("table2_a"), Bytes::from("2")), | ||
(Bytes::from("table2_b"), Bytes::new()), | ||
(Bytes::from("table2_c"), Bytes::from("2")), | ||
], | ||
); | ||
|
||
drop(snapshot0); | ||
|
||
storage.force_full_compaction().unwrap(); | ||
|
||
let mut iter = construct_merge_iterator_over_storage(&storage.inner.state.read()); | ||
check_iter_result_by_key( | ||
&mut iter, | ||
vec![ | ||
(Bytes::from("table1_a"), Bytes::from("2")), | ||
(Bytes::from("table1_c"), Bytes::from("2")), | ||
], | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.