Skip to content
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

Journal fixed lru cache #498

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

najeal
Copy link
Contributor

@najeal najeal commented Feb 17, 2025

Closes #452

This makes changes to use LRU Cache with fixed length item journal.

Copy link

codecov bot commented Feb 18, 2025

Codecov Report

Attention: Patch coverage is 96.15385% with 6 lines in your changes missing coverage. Please review.

Project coverage is 94.37%. Comparing base (0d7677e) to head (ed141e0).

Files with missing lines Patch % Lines
storage/src/journal/fixed.rs 96.10% 6 Missing ⚠️
@@            Coverage Diff             @@
##             main     #498      +/-   ##
==========================================
+ Coverage   94.34%   94.37%   +0.02%     
==========================================
  Files         109      109              
  Lines       30217    30282      +65     
==========================================
+ Hits        28509    28579      +70     
+ Misses       1708     1703       -5     
Files with missing lines Coverage Δ
storage/src/journal/mod.rs 100.00% <ø> (ø)
storage/src/mmr/journaled.rs 99.06% <100.00%> (+<0.01%) ⬆️
storage/src/journal/fixed.rs 98.07% <96.10%> (-0.13%) ⬇️

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0d7677e...ed141e0. Read the comment docs.

//blobs: BTreeMap<u64, B>,
min_blob_index: u64,
newest_index_blob: (u64, B),
lru_cache: Arc<tokio::sync::Mutex<LruCache<u64, B>>>,
Copy link
Collaborator

@roberto-bayardo roberto-bayardo Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need the Arc<> ? (& Mutex < > ?)

@@ -84,7 +91,10 @@ pub struct Journal<B: Blob, E: Storage<B>, A: Array> {

// Blobs are stored in a BTreeMap to ensure they are always iterated in order of their indices.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comment

@@ -24,6 +24,8 @@ futures-util = { workspace = true }
tracing = { workspace = true }
zstd = { workspace = true }
rangemap = "1.5.1"
lru = "0.13.0"
tokio = { workspace = true}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember team said we should not import tokio. This is currently used to open the discussion.

When removing Arc<Mutex<>> implies to change the read(&self) trait method signature to accept read(&mut self), this creates chained changes and not sure this is expected.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, this introduces an internal mutability concern. I think we can just change the API to read(&mut self) for now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. We shouldn't be importing tokio here.

We have a few options:

  1. make read(&mut self) (gross)
  2. use a cache that doesn't require a mutable reference (either external crate or write our own)
  3. refactor code to avoid holding cache references over await

If we aren't thoughtful with this cache integration, we could accidentally prohibit concurrent reads (even if we keep read(&self) and use a cache with interior mutability, it may lock on all requests to update the cache state).

A concurrent_lru could look something like this (we shouldn't use this crate but to give you some idea of a more compatible interface with our goals): https://docs.rs/concurrent_lru/latest/concurrent_lru/.

}
let items_per_blob = self.cfg.items_per_blob;
let blob = self.get_blob(blob_index).await?;
let item_index = item_position % items_per_blob;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let item_index = item_position % items_per_blob;
let item_index = item_position % self.cfg.items_per_blob;

then drop L309

let offset = item_index * Self::CHUNK_SIZE as u64;
let mut buf = vec![0u8; Self::CHUNK_SIZE];
blob.read_at(&mut buf, offset).await?;

// Verify integrity
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you're here can you delete this comment which is simply stating the obvious?

@@ -37,4 +37,6 @@ pub enum Error {
InvalidItem(u64),
#[error("invalid rewind: {0}")]
InvalidRewind(u64),
#[error("invalid index: {0}")]
InvalidIndex(u64),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't expose the fact that we index blobs using integers to the external API like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[commonware-storage/journal] fixed-length item journal should use LRU cache for open blobs
3 participants