Skip to content
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
14 changes: 11 additions & 3 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,15 @@ those.
- `GRAPH_STORE_ACCOUNT_LIKE_SCAN_INTERVAL_HOURS`: If set, enables an experimental job that
periodically scans for entity tables that may benefit from an [account-like optimization](https://thegraph.com/docs/en/indexing/tooling/graph-node/#account-like-optimisation) and marks them with an
account-like flag. The value is the interval in hours at which the job
should run. The job reads data from the `info.table_stats` materialized view, which refreshes every six hours. Expects an integer value, e.g., 24. Requires also setting
should run. The job reads data from the `info.table_stats` materialized view, which refreshes every six hours.
Expects an integer value, e.g., 24. Requires also setting
`GRAPH_STORE_ACCOUNT_LIKE_MIN_VERSIONS_COUNT` and `GRAPH_STORE_ACCOUNT_LIKE_MAX_UNIQUE_RATIO`.
- `GRAPH_STORE_ACCOUNT_LIKE_MIN_VERSIONS_COUNT`: Sets the minimum total number of versions a table must have to be considered for account-like flagging. Expects a positive integer value. No default value.
- `GRAPH_STORE_ACCOUNT_LIKE_MAX_UNIQUE_RATIO`: Sets the maximum unique entities to version ratio (e.g., 0.01 ≈ 1:100 entity-to-version ratio).
- `GRAPH_STORE_ACCOUNT_LIKE_MIN_VERSIONS_COUNT`: Sets the minimum total number of versions a table must have
to be considered for account-like flagging. Expects a positive integer value. No default value.
- `GRAPH_STORE_ACCOUNT_LIKE_MAX_UNIQUE_RATIO`: Sets the maximum unique entities to version ratio
(e.g., 0.01 ≈ 1:100 entity-to-version ratio).
- `GRAPH_STORE_DISABLE_CALL_CACHE`: Disables storing or reading `eth_call` results from the store call cache.
This option may be useful for indexers who are running their own RPC nodes.
Disabling the store call cache may significantly impact performance; the actual impact depends on
the average execution time of an `eth_call` compared to the cost of a database lookup for a cached result.
(default: false)
20 changes: 20 additions & 0 deletions graph/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ lazy_static! {
lazy_static! {
pub static ref TEST_WITH_NO_REORG: Mutex<bool> = Mutex::new(false);
pub static ref TEST_SQL_QUERIES_ENABLED: Mutex<bool> = Mutex::new(false);
pub static ref TEST_STORE_CALL_CACHE_DISABLED: Mutex<bool> = Mutex::new(false);
}

/// Panics if:
Expand Down Expand Up @@ -444,6 +445,25 @@ impl EnvVars {
let mut lock = TEST_SQL_QUERIES_ENABLED.lock().unwrap();
*lock = enable;
}

#[cfg(debug_assertions)]
pub fn store_call_cache_disabled(&self) -> bool {
if *TEST_STORE_CALL_CACHE_DISABLED.lock().unwrap() {
true
} else {
self.store.disable_call_cache
}
}

#[cfg(not(debug_assertions))]
pub fn store_call_cache_disabled(&self) -> bool {
self.store.disable_call_cache
}

#[cfg(debug_assertions)]
pub fn set_store_call_cache_disabled_for_tests(&self, value: bool) {
*TEST_STORE_CALL_CACHE_DISABLED.lock().unwrap() = value;
}
}

impl Default for EnvVars {
Expand Down
7 changes: 6 additions & 1 deletion graph/src/env/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ pub struct EnvVarsStore {
/// The number of rows to fetch from the foreign data wrapper in one go,
/// this will be set as the option 'fetch_size' on all foreign servers
pub fdw_fetch_size: usize,

/// Experimental feature to automatically set the account-like flag on eligible tables
/// Set by the environment variable `GRAPH_STORE_ACCOUNT_LIKE_SCAN_INTERVAL_HOURS`
/// If not set, the job is disabled.
Expand All @@ -161,6 +160,9 @@ pub struct EnvVarsStore {
/// Set by the environment variable `GRAPH_STORE_ACCOUNT_LIKE_MAX_UNIQUE_RATIO`
/// Defines the maximum share of unique entities (e.g. 0.01 for a 1:100 entity-to-version ratio).
pub account_like_max_unique_ratio: Option<f64>,
/// 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,
}

// This does not print any values avoid accidentally leaking any sensitive env vars
Expand Down Expand Up @@ -221,6 +223,7 @@ impl TryFrom<InnerStore> for EnvVarsStore {
account_like_scan_interval_hours: x.account_like_scan_interval_hours,
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,
};
if let Some(timeout) = vars.batch_timeout {
if timeout < 2 * vars.batch_target_duration {
Expand Down Expand Up @@ -326,6 +329,8 @@ pub struct InnerStore {
account_like_min_versions_count: Option<u64>,
#[envconfig(from = "GRAPH_STORE_ACCOUNT_LIKE_MAX_UNIQUE_RATIO")]
account_like_max_unique_ratio: Option<ZeroToOneF64>,
#[envconfig(from = "GRAPH_STORE_DISABLE_CALL_CACHE", default = "false")]
disable_call_cache: bool,
}

#[derive(Clone, Copy, Debug)]
Expand Down
8 changes: 8 additions & 0 deletions store/postgres/src/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3081,6 +3081,10 @@ impl EthereumCallCache for ChainStore {
req: &call::Request,
block: BlockPtr,
) -> Result<Option<call::Response>, Error> {
if ENV_VARS.store_call_cache_disabled() {
return Ok(None);
}

let id = contract_call_id(req, &block);
let conn = &mut self.pool.get_permitted().await?;
let return_value = conn
Expand Down Expand Up @@ -3171,6 +3175,10 @@ impl EthereumCallCache for ChainStore {
block: BlockPtr,
return_value: call::Retval,
) -> Result<(), Error> {
if ENV_VARS.store_call_cache_disabled() {
return Ok(());
}

let return_value = match return_value {
call::Retval::Value(return_value) if !return_value.is_empty() => return_value,
_ => {
Expand Down
35 changes: 35 additions & 0 deletions store/test-store/tests/postgres/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,41 @@ fn eth_call_cache() {
})
}

#[test]
fn test_disable_call_cache() {
let chain = vec![&*GENESIS_BLOCK, &*BLOCK_ONE, &*BLOCK_TWO];

run_test_async(chain, |store, _, _| async move {
ENV_VARS.set_store_call_cache_disabled_for_tests(true);

let logger = LOGGER.cheap_clone();
let address = H160([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]);
let call: [u8; 6] = [1, 2, 3, 4, 5, 6];
let return_value: [u8; 3] = [7, 8, 9];

let call = call::Request::new(address, call.to_vec(), 0);

store
.cheap_clone()
.set_call(
&logger,
call.cheap_clone(),
BLOCK_ONE.block_ptr(),
call::Retval::Value(Bytes::from(return_value)),
)
.await
.unwrap();

let ret = store
.cheap_clone()
.get_call(&call, BLOCK_ONE.block_ptr())
.await
.unwrap();

assert!(ret.is_none());
});
}

#[test]
/// Tests mainly query correctness. Requires data in order not to hit early returns when no stale contracts are found.
fn test_clear_stale_call_cache() {
Expand Down