-
Notifications
You must be signed in to change notification settings - Fork 635
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/introduce cache related metrics for prometheus #7439
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ use near_primitives::hash::CryptoHash; | |||||||||
|
||||||||||
use crate::db::refcount::decode_value_with_rc; | ||||||||||
use crate::trie::POISONED_LOCK_ERR; | ||||||||||
use crate::{DBCol, StorageError, Store}; | ||||||||||
use crate::{metrics, DBCol, StorageError, Store}; | ||||||||||
use lru::LruCache; | ||||||||||
use near_primitives::shard_layout::ShardUId; | ||||||||||
use near_primitives::types::{TrieCacheMode, TrieNodesCount}; | ||||||||||
|
@@ -34,19 +34,34 @@ impl TrieCache { | |||||||||
self.0.lock().expect(POISONED_LOCK_ERR).clear() | ||||||||||
} | ||||||||||
|
||||||||||
pub fn update_cache(&self, ops: Vec<(CryptoHash, Option<&Vec<u8>>)>) { | ||||||||||
pub fn update_cache(&self, ops: Vec<(CryptoHash, Option<&Vec<u8>>)>, shard_uid: ShardUId) { | ||||||||||
let shard_id_str = format!("{}", shard_uid.shard_id); | ||||||||||
let labels: [&str; 1] = [&shard_id_str]; | ||||||||||
|
||||||||||
let mut guard = self.0.lock().expect(POISONED_LOCK_ERR); | ||||||||||
for (hash, opt_value_rc) in ops { | ||||||||||
if let Some(value_rc) = opt_value_rc { | ||||||||||
if let (Some(value), _rc) = decode_value_with_rc(&value_rc) { | ||||||||||
if value.len() < TRIE_LIMIT_CACHED_VALUE_SIZE { | ||||||||||
guard.put(hash, value.into()); | ||||||||||
} else { | ||||||||||
metrics::SHARD_CACHE_TOO_LARGE.with_label_values(&labels).inc(); | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We need to inc this metric here as well - this is my bug from the first impl |
||||||||||
} else { | ||||||||||
guard.pop(&hash); | ||||||||||
match guard.pop(&hash) { | ||||||||||
Some(_) => { | ||||||||||
metrics::SHARD_CACHE_POPS.with_label_values(&labels).inc(); | ||||||||||
} | ||||||||||
_ => {} | ||||||||||
}; | ||||||||||
} | ||||||||||
} else { | ||||||||||
guard.pop(&hash); | ||||||||||
match guard.pop(&hash) { | ||||||||||
Some(_) => { | ||||||||||
metrics::SHARD_CACHE_POPS.with_label_values(&labels).inc(); | ||||||||||
} | ||||||||||
_ => {} | ||||||||||
}; | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
@@ -179,10 +194,17 @@ pub struct TrieCachingStorage { | |||||||||
pub(crate) db_read_nodes: Cell<u64>, | ||||||||||
/// Counts trie nodes retrieved from the chunk cache. | ||||||||||
pub(crate) mem_read_nodes: Cell<u64>, | ||||||||||
/// Boolean for determining if the cache is a view cache or not | ||||||||||
is_view: bool, | ||||||||||
} | ||||||||||
|
||||||||||
impl TrieCachingStorage { | ||||||||||
pub fn new(store: Store, shard_cache: TrieCache, shard_uid: ShardUId) -> TrieCachingStorage { | ||||||||||
pub fn new( | ||||||||||
store: Store, | ||||||||||
shard_cache: TrieCache, | ||||||||||
shard_uid: ShardUId, | ||||||||||
is_view: bool, | ||||||||||
) -> TrieCachingStorage { | ||||||||||
TrieCachingStorage { | ||||||||||
store, | ||||||||||
shard_uid, | ||||||||||
|
@@ -191,6 +213,7 @@ impl TrieCachingStorage { | |||||||||
chunk_cache: RefCell::new(Default::default()), | ||||||||||
db_read_nodes: Cell::new(0), | ||||||||||
mem_read_nodes: Cell::new(0), | ||||||||||
is_view, | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -227,25 +250,45 @@ impl TrieCachingStorage { | |||||||||
pub fn set_mode(&self, state: TrieCacheMode) { | ||||||||||
self.cache_mode.set(state); | ||||||||||
} | ||||||||||
|
||||||||||
fn update_cache_size_metrics(&self, labels: [&str; 2]) { | ||||||||||
{ | ||||||||||
metrics::CHUNK_CACHE_SIZE | ||||||||||
.with_label_values(&labels) | ||||||||||
.set(self.chunk_cache.borrow().len() as i64); | ||||||||||
metrics::SHARD_CACHE_SIZE | ||||||||||
.with_label_values(&labels) | ||||||||||
.set(self.shard_cache.0.lock().expect(POISONED_LOCK_ERR).len() as i64); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl TrieStorage for TrieCachingStorage { | ||||||||||
fn retrieve_raw_bytes(&self, hash: &CryptoHash) -> Result<Arc<[u8]>, StorageError> { | ||||||||||
let shard_id_str = format!("{}", self.shard_uid.shard_id); | ||||||||||
let is_view_str = format!("{}", self.is_view as u8); | ||||||||||
let labels: [&str; 2] = [&shard_id_str, &is_view_str]; | ||||||||||
|
||||||||||
self.update_cache_size_metrics(labels); | ||||||||||
// Try to get value from chunk cache containing nodes with cheaper access. We can do it for any `TrieCacheMode`, | ||||||||||
// because we charge for reading nodes only when `CachingChunk` mode is enabled anyway. | ||||||||||
if let Some(val) = self.chunk_cache.borrow_mut().get(hash) { | ||||||||||
metrics::CHUNK_CACHE_HITS.with_label_values(&labels).inc(); | ||||||||||
self.inc_mem_read_nodes(); | ||||||||||
return Ok(val.clone()); | ||||||||||
} | ||||||||||
metrics::CHUNK_CACHE_MISSES.with_label_values(&labels).inc(); | ||||||||||
|
||||||||||
// Try to get value from shard cache containing most recently touched nodes. | ||||||||||
let mut guard = self.shard_cache.0.lock().expect(POISONED_LOCK_ERR); | ||||||||||
let val = match guard.get(hash) { | ||||||||||
Some(val) => { | ||||||||||
metrics::SHARD_CACHE_HITS.with_label_values(&labels).inc(); | ||||||||||
near_o11y::io_trace!(count: "shard_cache_hit"); | ||||||||||
val.clone() | ||||||||||
} | ||||||||||
None => { | ||||||||||
metrics::SHARD_CACHE_MISSES.with_label_values(&labels).inc(); | ||||||||||
near_o11y::io_trace!(count: "shard_cache_miss"); | ||||||||||
// If value is not present in cache, get it from the storage. | ||||||||||
let key = Self::get_key_from_shard_uid_and_hash(self.shard_uid, hash); | ||||||||||
|
@@ -265,6 +308,7 @@ impl TrieStorage for TrieCachingStorage { | |||||||||
if val.len() < TRIE_LIMIT_CACHED_VALUE_SIZE { | ||||||||||
guard.put(*hash, val.clone()); | ||||||||||
} else { | ||||||||||
metrics::SHARD_CACHE_TOO_LARGE.with_label_values(&labels).inc(); | ||||||||||
near_o11y::io_trace!(count: "shard_cache_too_large"); | ||||||||||
} | ||||||||||
|
||||||||||
|
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
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.
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.
Now I think that
TrieCache
should holdshard_uid
, so we could avoid passing it here. Let's fix it in a separate PR