-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: add a LRU-based cache module for preheat jobs #945
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: southwest <[email protected]>
…tion Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
…with added annotations. Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
…he' into feature/storage-cache
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #945 +/- ##
==========================================
+ Coverage 32.66% 34.40% +1.73%
==========================================
Files 60 61 +1
Lines 9792 10067 +275
==========================================
+ Hits 3199 3464 +265
- Misses 6593 6603 +10
|
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest <[email protected]>
Signed-off-by: southwest miao <[email protected]>
Signed-off-by: southwest miao <[email protected]>
#[derive(Clone)] | ||
pub struct Cache { | ||
/// pieces stores the pieces with their piece id and content. | ||
pieces: Arc<Mutex<LruCache<String, bytes::Bytes>>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use RWMutex
.
} | ||
|
||
#[tokio::test] | ||
async fn test_cache_initialization() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more cases.
/// Cache implements the cache for storing piece content by LRU algorithm. | ||
impl Cache { | ||
/// new creates a new cache with the specified capacity. | ||
pub fn new(config: Arc<Config>) -> Result<Self> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub fn new(capacity: usize) -> Result<Self> {
@@ -405,7 +414,7 @@ impl Storage { | |||
piece_id: &str, | |||
task_id: &str, | |||
range: Option<Range>, | |||
) -> Result<impl AsyncRead> { | |||
) -> Result<Pin<Box<dyn AsyncRead + Send>>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return Result<impl AsyncRead>
.
@@ -415,6 +424,24 @@ impl Storage { | |||
// Get the piece metadata and return the content of the piece. | |||
match self.metadata.get_piece(piece_id) { | |||
Ok(Some(piece)) => { | |||
// Try to upload piece content form cache. | |||
if !self.cache.is_empty().await && self.cache.contains_piece(piece_id).await { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.cache.contains_piece(piece_id).await
@@ -520,6 +522,14 @@ impl Piece { | |||
}; | |||
})?; | |||
|
|||
// Load piece content to cache from parent | |||
if load_to_cache { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to download_piece_from_parent_finished
and download_piece_from_source_finished
.
if load_to_cache { | ||
// Load piece content to cache from source. | ||
self.storage | ||
.load_piece_to_cache(piece_id, &mut reader, length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to download_piece_from_parent_finished
and download_piece_from_source_finished
.
// If need_piece_content is true, read the piece content from the local. | ||
if need_piece_content { | ||
// If need_piece_content or load_to_cache is true, read the piece content from the local. | ||
if need_piece_content || load_to_cache { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove.
Description
This PR introduces a Cache module designed specifically for preheating jobs, providing an LRU-based mechanism for storing and managing piece content. The purpose of the cache is to enable other peers within the cluster to access user-space maintained cached pieces when downloading the same tasks.
Dependency Updates:
dragonfly-api
from2.1.3
to2.1.4
in Cargo.toml.Refactoring and Method Changes:
Refactored the
upload_piece
method indragonfly-client-storage/src/lib.rs
. This change aims to support uploading piece content from cache when hitting cache.Changed the return type from
Result<impl AsyncRead>
toResult<Pin<Box<dyn AsyncRead + Send>>>
because reading cache or disk will return different types implementingAsyncRead
.Refactored the
download_from_local_into_async_read
andupload_from_local_into_async_read
method indragonfly-client/src/resource/piece.rs
.Modified their return types, because they both call
upload_piece
and return its result.Refactored the
download_from_source
anddownload_from_parent
method indragonfly-client/src/resource/piece.rs
. Add a new parameterload_to_cache
on both. This change is aim to support loading piece content to cache when downloading piece content from parent peer or source.Refactored the
download_partial_from_local
indragonfly-client/src/resource/persistent_cache_task.rs
. Add a new parameterload_to_cache
. Added the code for loading content into cache.Add a new method
upload_piece_from_cache
indragonfly-client-storage/src/lib.rs
. This method is used by the storage to provide reading cache.Add a new method
load_piece_to_cache
indragonfly-client-storage/src/lib.rs
. This method is used by the storage to provide writing cache.Add a new parameter
load_to_cache
in some methods.Metadata and Struct Changes:
cache
inStorage
indragonfly-client-storage/src/lib.rs
.Module Changes:
Cache
with unit tests indragonfly-client-storage/src/cache.rs
.Related Issue
dragonflyoss/dragonfly#3742
Motivation and Context
Screenshots (if appropriate)
Types of changes
Checklist