-
Notifications
You must be signed in to change notification settings - Fork 477
feat: compaction scheduler and rate limiter #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
v0y4g3r
merged 9 commits into
GreptimeTeam:develop
from
v0y4g3r:feat/compaction-scheduler
Feb 9, 2023
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
47bb357
wip: compaction schdduler
v0y4g3r e31ed97
feat: imple simple compaction scheduler
v0y4g3r e931bc2
fix: typo
v0y4g3r 16a4db4
feat: add generic parameter to make scheduler friendly to tests
v0y4g3r c64dc9a
chore: add more tests
v0y4g3r 3beb071
fix: CR comments
v0y4g3r 4652724
fix: CR comments
v0y4g3r 2b2b893
fix: ensure idempotency for rate limit token
v0y4g3r a4fb5ab
fix: Cr ct omments
v0y4g3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,19 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| mod dedup_deque; | ||
| mod picker; | ||
| mod rate_limit; | ||
| mod scheduler; | ||
| mod task; |
This file contains hidden or 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,101 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::collections::{HashMap, VecDeque}; | ||
| use std::fmt::{Debug, Formatter}; | ||
| use std::hash::Hash; | ||
|
|
||
| /// Deque with key deduplication. | ||
| #[derive(Default)] | ||
| pub struct DedupDeque<K, V> { | ||
| deque: VecDeque<K>, | ||
| existing: HashMap<K, V>, | ||
| } | ||
|
|
||
| impl<K: Eq + Hash + Clone, V> DedupDeque<K, V> { | ||
| /// Pushes a key value to the back of deque. | ||
| /// Returns true if the deque does not already contain value with the same key, otherwise | ||
| /// returns false. | ||
| pub fn push_back(&mut self, key: K, value: V) -> bool { | ||
| debug_assert_eq!(self.deque.len(), self.existing.len()); | ||
| if !self.existing.contains_key(&key) { | ||
| self.existing.insert(key.clone(), value); | ||
| self.deque.push_back(key); | ||
| return true; | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /// Pushes a key value to the front of deque. | ||
| /// Returns true if the deque does not already contain value with the same key, otherwise | ||
| /// returns false. | ||
| pub fn push_front(&mut self, key: K, value: V) -> bool { | ||
| debug_assert_eq!(self.deque.len(), self.existing.len()); | ||
| if !self.existing.contains_key(&key) { | ||
| self.existing.insert(key.clone(), value); | ||
| self.deque.push_front(key); | ||
| return true; | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /// Pops a pair from the back of deque. Returns [None] if the deque is empty. | ||
| pub fn pop_front(&mut self) -> Option<(K, V)> { | ||
| debug_assert_eq!(self.deque.len(), self.existing.len()); | ||
| let key = self.deque.pop_front()?; | ||
| let value = self.existing.remove(&key)?; | ||
| Some((key, value)) | ||
| } | ||
|
|
||
| pub fn len(&self) -> usize { | ||
| debug_assert_eq!(self.deque.len(), self.existing.len()); | ||
| self.deque.len() | ||
| } | ||
| } | ||
|
|
||
| impl<K, V> Debug for DedupDeque<K, V> | ||
| where | ||
| K: Debug, | ||
| V: Debug, | ||
| { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| f.debug_struct("DedupDeque") | ||
| .field("deque", &self.deque) | ||
| .field("existing", &self.existing) | ||
| .finish() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_dedup_deque() { | ||
| let mut deque = DedupDeque::default(); | ||
| assert!(deque.push_back(1, "hello".to_string())); | ||
| assert_eq!(1, deque.len()); | ||
| assert!(deque.push_back(2, "world".to_string())); | ||
| assert_eq!(2, deque.len()); | ||
| assert_eq!((1, "hello".to_string()), deque.pop_front().unwrap()); | ||
| assert_eq!(1, deque.len()); | ||
| assert_eq!((2, "world".to_string()), deque.pop_front().unwrap()); | ||
| assert_eq!(0, deque.len()); | ||
|
|
||
| // insert duplicated item | ||
| assert!(deque.push_back(1, "hello".to_string())); | ||
| assert!(!deque.push_back(1, "world".to_string())); | ||
| assert_eq!((1, "hello".to_string()), deque.pop_front().unwrap()); | ||
| } | ||
| } | ||
This file contains hidden or 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,67 @@ | ||
| // Copyright 2023 Greptime Team | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use crate::compaction::scheduler::CompactionRequestImpl; | ||
| use crate::compaction::task::{CompactionTask, CompactionTaskImpl}; | ||
|
|
||
| /// Picker picks input SST files and build the compaction task. | ||
| /// Different compaction strategy may implement different pickers. | ||
| pub trait Picker<R, T: CompactionTask>: Send + 'static { | ||
|
v0y4g3r marked this conversation as resolved.
|
||
| fn pick(&self, req: &R) -> crate::error::Result<T>; | ||
| } | ||
|
|
||
| /// L0 -> L1 all-to-all compaction based on time windows. | ||
| pub(crate) struct SimplePicker {} | ||
|
|
||
| #[allow(unused)] | ||
| impl SimplePicker { | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| impl Picker<CompactionRequestImpl, CompactionTaskImpl> for SimplePicker { | ||
| fn pick(&self, _req: &CompactionRequestImpl) -> crate::error::Result<CompactionTaskImpl> { | ||
| todo!() | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub mod tests { | ||
| use std::marker::PhantomData; | ||
|
|
||
| use super::*; | ||
| use crate::compaction::scheduler::CompactionRequest; | ||
| use crate::compaction::task::tests::{CallbackRef, NoopCompactionTask}; | ||
|
|
||
| pub(crate) struct MockPicker<R: CompactionRequest> { | ||
| pub cbs: Vec<CallbackRef>, | ||
| _phantom_data: PhantomData<R>, | ||
| } | ||
|
|
||
| impl<R: CompactionRequest> MockPicker<R> { | ||
| pub fn new(cbs: Vec<CallbackRef>) -> Self { | ||
| Self { | ||
| cbs, | ||
| _phantom_data: Default::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<R: CompactionRequest> Picker<R, NoopCompactionTask> for MockPicker<R> { | ||
| fn pick(&self, _req: &R) -> crate::error::Result<NoopCompactionTask> { | ||
| Ok(NoopCompactionTask::new(self.cbs.clone())) | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.