Skip to content

Commit

Permalink
track other caches hit/miss
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Nov 18, 2024
1 parent 5f642bb commit 06dcb0d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use hyper::service::{make_service_fn, service_fn};
use hyper::Server;
use lazy_static::lazy_static;
use network_parse::NetworkParse;
use prometheus::{register_counter_vec, register_histogram_vec, CounterVec, HistogramVec};
use prometheus::{
register_counter_vec, register_histogram_vec, register_int_counter_vec, CounterVec,
HistogramVec, IntCounterVec,
};
use serde::Deserialize;
use std::collections::HashMap;
use std::convert::Infallible;
Expand Down Expand Up @@ -304,4 +307,17 @@ lazy_static! {
&["method", "content"]
)
.unwrap();
static ref CACHE_COUNTER: IntCounterVec = register_int_counter_vec!(
"height_time_cache_counter",
"Hit/Miss of the height/time cache",
&["name", "event"]
)
.unwrap();
}

pub(crate) fn cache_counter(cache_name: &str, hit_miss: bool) {
let hit_miss = if hit_miss { "hit" } else { "miss" };
crate::CACHE_COUNTER
.with_label_values(&[cache_name, hit_miss])
.inc();
}
18 changes: 12 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use lru::LruCache;
use prometheus::Registry;
use tokio::sync::{Mutex, MutexGuard};

use crate::cache_counter;
use crate::rpc::block::SerBlock;
use crate::{
error::Error,
Expand Down Expand Up @@ -68,6 +69,7 @@ pub struct SharedState {
// if the mempool has 100k with an average of 1.5 inputs, we have 150k*(36+36) = 10MB
pub mempool_spending: Mutex<FxHashMap<OutPoint, SpendPoint>>,

/// A note on known transactions
pub known_txs: HashMap<Txid, String>,
}

Expand Down Expand Up @@ -160,12 +162,13 @@ impl SharedState {

pub async fn height_time(&self, block_hash: BlockHash) -> Result<HeightTime, Error> {
let mut hash_to_timestamp = self.hash_to_height_time.lock().await;
if let Some(height_time) = hash_to_timestamp.get(&block_hash) {
log::trace!("timestamp hit");
let timestamp = hash_to_timestamp.get(&block_hash);

cache_counter("height-time", timestamp.is_some());

if let Some(height_time) = timestamp {
Ok(*height_time)
} else {
log::debug!("timestamp miss");
// let _ = self.rpc_calls.fetch_add(1, Ordering::Relaxed);
let header = rpc::headers::call_one(block_hash).await?;
hash_to_timestamp.insert(block_hash, header.height_time);
drop(hash_to_timestamp);
Expand Down Expand Up @@ -203,12 +206,15 @@ impl SharedState {
let txs = self.txs.lock().await;
if !needs_block_hash {
if let Some(tx) = txs.get(&txid) {
log::trace!("tx hit");
return Ok((SerTx(tx.to_vec()), None));
}
} else {
let mut tx_in_block = self.tx_in_block.lock().await;
match (txs.get(&txid), tx_in_block.get(&txid)) {

let block_hash = tx_in_block.get(&txid);
cache_counter("txid-block_hash", block_hash.is_some());

match (txs.get(&txid), block_hash) {
(Some(tx), Some(block_hash)) => {
// add prometheus measure tx="hit/miss" block_hash="hit/miss"
log::trace!("tx hit");
Expand Down

0 comments on commit 06dcb0d

Please sign in to comment.