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

Include a mode for cache metrics. #796

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions storage/src/linear/filebacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ impl ReadableStorage for FileBacked {
Ok(self.fd.metadata()?.len())
}

fn read_cached_node(&self, addr: LinearAddress) -> Option<SharedNode> {
fn read_cached_node(&self, addr: LinearAddress, mode: &'static str) -> Option<SharedNode> {
let mut guard = self.cache.lock().expect("poisoned lock");
let cached = guard.get(&addr).cloned();
counter!("firewood.cache.node", "type" => if cached.is_some() { "hit" } else { "miss" })
counter!("firewood.cache.node", "mode" => mode, "type" => if cached.is_some() { "hit" } else { "miss" })
.increment(1);
cached
}
Expand Down
2 changes: 1 addition & 1 deletion storage/src/linear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait ReadableStorage: Debug + Sync + Send {
fn size(&self) -> Result<u64, Error>;

/// Read a node from the cache (if any)
fn read_cached_node(&self, _addr: LinearAddress) -> Option<SharedNode> {
fn read_cached_node(&self, _addr: LinearAddress, _mode: &'static str) -> Option<SharedNode> {
None
}

Expand Down
29 changes: 19 additions & 10 deletions storage/src/nodestore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,12 @@ impl<T: ReadInMemoryNode, S: ReadableStorage> NodeStore<T, S> {

/// Read a [Node] from the provided [LinearAddress].
/// `addr` is the address of a StoredArea in the ReadableStorage.
pub fn read_node_from_disk(&self, addr: LinearAddress) -> Result<SharedNode, Error> {
if let Some(node) = self.storage.read_cached_node(addr) {
pub fn read_node_from_disk(
&self,
addr: LinearAddress,
mode: &'static str,
) -> Result<SharedNode, Error> {
if let Some(node) = self.storage.read_cached_node(addr, mode) {
return Ok(node);
}

Expand Down Expand Up @@ -272,7 +276,7 @@ impl<S: ReadableStorage> NodeStore<Committed, S> {
};

if let Some(root_address) = nodestore.header.root_address {
let node = nodestore.read_node_from_disk(root_address);
let node = nodestore.read_node_from_disk(root_address, "open");
let root_hash = node.map(|n| hash_node(&n, &Path(Default::default())))?;
nodestore.kind.root_hash = Some(root_hash);
}
Expand Down Expand Up @@ -1103,13 +1107,22 @@ impl<S: ReadableStorage> From<NodeStore<MutableProposal, S>>
}
}

impl<T: ReadInMemoryNode, S: ReadableStorage> NodeReader for NodeStore<T, S> {
impl<S: ReadableStorage> NodeReader for NodeStore<MutableProposal, S> {
fn read_node(&self, addr: LinearAddress) -> Result<SharedNode, Error> {
if let Some(node) = self.kind.read_in_memory_node(addr) {
return Ok(node);
}

self.read_node_from_disk(addr)
self.read_node_from_disk(addr, "write")
}
}

impl<T: Parentable + ReadInMemoryNode, S: ReadableStorage> NodeReader for NodeStore<T, S> {
fn read_node(&self, addr: LinearAddress) -> Result<SharedNode, Error> {
if let Some(node) = self.kind.read_in_memory_node(addr) {
return Ok(node);
}
self.read_node_from_disk(addr, "read")
}
}

Expand All @@ -1119,11 +1132,7 @@ impl<S: ReadableStorage> RootReader for NodeStore<MutableProposal, S> {
}
}

trait Hashed {}
impl Hashed for Committed {}
impl Hashed for Arc<ImmutableProposal> {}

impl<T: ReadInMemoryNode + Hashed, S: ReadableStorage> RootReader for NodeStore<T, S> {
impl<T: ReadInMemoryNode + Parentable, S: ReadableStorage> RootReader for NodeStore<T, S> {
fn root_node(&self) -> Option<SharedNode> {
// TODO: If the read_node fails, we just say there is no root; this is incorrect
self.header
Expand Down