-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compactor): implement compactor task
Refs: #188 Signed-off-by: Phoeniix Zhao <[email protected]>
- Loading branch information
1 parent
8aa1f3b
commit 6ae4a29
Showing
6 changed files
with
102 additions
and
31 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::sync::Arc; | ||
|
||
use event_listener::Event; | ||
use tokio::{sync::mpsc::UnboundedReceiver, time::sleep}; | ||
|
||
use super::{ | ||
index::{Index, IndexOperate}, | ||
storage_api::StorageApi, | ||
KvStore, | ||
}; | ||
|
||
/// background compact executor | ||
pub(crate) async fn compactor<DB>( | ||
kv_store: Arc<KvStore<DB>>, | ||
index: Arc<Index>, | ||
mut compact_task_rx: UnboundedReceiver<(i64, Option<Arc<Event>>)>, | ||
) where | ||
DB: StorageApi, | ||
{ | ||
// TODO: make compact_interval and compact_batch_limit configurable | ||
let compact_interval = std::time::Duration::from_millis(10); | ||
let compact_batch_limit = 1000; | ||
while let Some((revision, listener)) = compact_task_rx.recv().await { | ||
let target_revisions = index | ||
.compact(revision) | ||
.into_iter() | ||
.map(|key_rev| key_rev.as_revision().encode_to_vec()) | ||
.collect::<Vec<Vec<_>>>(); | ||
for revision_chunk in target_revisions.chunks(compact_batch_limit) { | ||
if let Err(e) = kv_store.compact(revision_chunk) { | ||
panic!("failed to compact revision chunk {revision_chunk:?} due to {e}"); | ||
} | ||
sleep(compact_interval).await; | ||
} | ||
if let Some(notifier) = listener { | ||
notifier.notify(usize::MAX); | ||
} | ||
} | ||
} |
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