Skip to content
Open
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
6 changes: 3 additions & 3 deletions graph/src/blockchain/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
network_provider::ChainName,
store::{
BlockNumber, ChainHeadStore, ChainIdStore, DeploymentCursorTracker, DeploymentLocator,
SourceableStore,
SourceableStore, StaleCallCacheResult,
},
subgraph::InstanceDSTemplateInfo,
},
Expand Down Expand Up @@ -595,8 +595,8 @@ impl ChainStore for MockChainStore {
async fn clear_stale_call_cache(
&self,
_ttl_days: usize,
_ttl_max_contracts: Option<usize>,
) -> Result<(), Error> {
_max_contracts: Option<usize>,
) -> Result<StaleCallCacheResult, Error> {
unimplemented!()
}
async fn chain_identifier(&self) -> Result<ChainIdentifier, Error> {
Expand Down
20 changes: 17 additions & 3 deletions graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,18 @@ pub trait ChainIdStore: Send + Sync + 'static {
/// configuration can change this for individual chains
pub const BLOCK_CACHE_SIZE: BlockNumber = i32::MAX;

/// Result of clearing stale call cache entries.
pub struct StaleCallCacheResult {
/// The effective TTL in days that was actually used for deletion.
/// This may be larger than the requested TTL if `max_contracts`
/// was set and caused the cutoff to be adjusted.
pub effective_ttl_days: usize,
/// Number of cache entries deleted from the call cache.
pub cache_entries_deleted: usize,
/// Number of contract entries deleted from call meta.
pub contracts_deleted: usize,
}

/// Common trait for blockchain store implementations.
#[async_trait]
pub trait ChainStore: ChainHeadStore {
Expand Down Expand Up @@ -657,12 +669,14 @@ pub trait ChainStore: ChainHeadStore {
/// Clears call cache of the chain for the given `from` and `to` block number.
async fn clear_call_cache(&self, from: BlockNumber, to: BlockNumber) -> Result<(), Error>;

/// Clears stale call cache entries for the given TTL in days.
/// Clears stale call cache entries for the given TTL in days. If
/// `max_contracts` is set, increase the effective TTL so that at
/// most `max_contracts` contracts are evicted.
async fn clear_stale_call_cache(
&self,
ttl_days: usize,
ttl_max_contracts: Option<usize>,
) -> Result<(), Error>;
max_contracts: Option<usize>,
) -> Result<StaleCallCacheResult, Error>;

/// Return the chain identifier for this store.
async fn chain_identifier(&self) -> Result<ChainIdentifier, Error>;
Expand Down
11 changes: 0 additions & 11 deletions graph/src/env/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ pub struct EnvVarsStore {
/// Disables storing or reading `eth_call` results from the store call cache.
/// Set by `GRAPH_STORE_DISABLE_CALL_CACHE`. Defaults to false.
pub disable_call_cache: bool,
/// The number of contracts to delete from the call cache in one batch
/// when clearing stale entries, set by
/// `GRAPH_STORE_STALE_CALL_CACHE_CONTRACTS_BATCH_SIZE`. The default
/// value is 100 contracts.
pub stale_call_cache_contracts_batch_size: usize,
/// Set by `GRAPH_STORE_DISABLE_CHAIN_HEAD_PTR_CACHE`. Default is false.
/// Set to true to disable chain_head_ptr caching (safety escape hatch).
pub disable_chain_head_ptr_cache: bool,
Expand Down Expand Up @@ -253,7 +248,6 @@ impl TryFrom<InnerStore> for EnvVarsStore {
account_like_min_versions_count: x.account_like_min_versions_count,
account_like_max_unique_ratio: x.account_like_max_unique_ratio.map(|r| r.0),
disable_call_cache: x.disable_call_cache,
stale_call_cache_contracts_batch_size: x.stale_call_cache_contracts_batch_size,
disable_chain_head_ptr_cache: x.disable_chain_head_ptr_cache,
connection_validation_idle_secs: Duration::from_secs(x.connection_validation_idle_secs),
connection_unavailable_retry: Duration::from_secs(
Expand Down Expand Up @@ -370,11 +364,6 @@ pub struct InnerStore {
account_like_max_unique_ratio: Option<ZeroToOneF64>,
#[envconfig(from = "GRAPH_STORE_DISABLE_CALL_CACHE", default = "false")]
disable_call_cache: bool,
#[envconfig(
from = "GRAPH_STORE_STALE_CALL_CACHE_CONTRACTS_BATCH_SIZE",
default = "100"
)]
stale_call_cache_contracts_batch_size: usize,
#[envconfig(from = "GRAPH_STORE_DISABLE_CHAIN_HEAD_PTR_CACHE", default = "false")]
disable_chain_head_ptr_cache: bool,
#[envconfig(from = "GRAPH_STORE_CONNECTION_VALIDATION_IDLE_SECS", default = "30")]
Expand Down
4 changes: 2 additions & 2 deletions graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ pub mod prelude {
EntityCollection, EntityFilter, EntityLink, EntityOperation, EntityOrder,
EntityOrderByChild, EntityOrderByChildInfo, EntityQuery, EntityRange, EntityWindow,
EthereumCallCache, ParentLink, PartialBlockPtr, PoolWaitStats, QueryStore,
QueryStoreManager, SeqGenerator, StoreError, StoreEvent, StoreEventStreamBox,
SubgraphStore, UnfailOutcome, WindowAttribute, BLOCK_NUMBER_MAX,
QueryStoreManager, SeqGenerator, StaleCallCacheResult, StoreError, StoreEvent,
StoreEventStreamBox, SubgraphStore, UnfailOutcome, WindowAttribute, BLOCK_NUMBER_MAX,
};
pub use crate::components::subgraph::{
BlockState, BlockStateCheckpoint, HostMetrics, InstanceDSTemplateInfo, RuntimeHost,
Expand Down
15 changes: 8 additions & 7 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,11 @@ pub enum CallCacheCommand {
remove_entire_cache: bool,
/// Remove the cache for contracts that have not been accessed in the last <TTL_DAYS> days
#[clap(long, conflicts_with_all = &["from", "to", "remove-entire-cache"], value_parser = clap::value_parser!(u32).range(1..))]
ttl_days: Option<usize>,
/// Limits the number of contracts to consider for cache removal when using --ttl_days
#[clap(long, conflicts_with_all = &["remove-entire-cache", "to", "from"], requires = "ttl_days", value_parser = clap::value_parser!(u64).range(1..))]
ttl_max_contracts: Option<usize>,
ttl_days: Option<u32>,
/// Maximum number of contracts to evict. When set, the effective TTL
/// is increased so that at most this many contracts are deleted.
#[clap(long, requires = "ttl_days")]
max_contracts: Option<usize>,
/// Starting block number
#[clap(long, short, conflicts_with = "remove-entire-cache", requires = "to")]
from: Option<i32>,
Expand Down Expand Up @@ -1549,14 +1550,14 @@ async fn main() -> anyhow::Result<()> {
to,
remove_entire_cache,
ttl_days,
ttl_max_contracts,
max_contracts,
} => {
let chain_store = ctx.chain_store(&chain_name).await?;
if let Some(ttl_days) = ttl_days {
return commands::chain::clear_stale_call_cache(
chain_store,
ttl_days,
ttl_max_contracts,
ttl_days as usize,
max_contracts,
)
.await;
}
Expand Down
18 changes: 15 additions & 3 deletions node/src/manager/commands/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,27 @@ pub async fn clear_call_cache(
pub async fn clear_stale_call_cache(
chain_store: Arc<ChainStore>,
ttl_days: usize,
ttl_max_contracts: Option<usize>,
max_contracts: Option<usize>,
) -> Result<(), Error> {
println!(
"Removing stale entries from the call cache for `{}`",
chain_store.chain
);
chain_store
.clear_stale_call_cache(ttl_days, ttl_max_contracts)
let result = chain_store
.clear_stale_call_cache(ttl_days, max_contracts)
.await?;
if result.effective_ttl_days != ttl_days {
println!(
"Effective TTL: {} days (adjusted from {} to stay within {} contracts)",
result.effective_ttl_days,
ttl_days,
max_contracts.unwrap()
);
}
println!(
"Deleted {} cache entries for {} contracts",
result.cache_entries_deleted, result.contracts_deleted
);
Ok(())
}

Expand Down
Loading
Loading