Skip to content

Commit f45feb5

Browse files
graph, store: Add option to disable store call cache
Signed-off-by: Maksim Dimitrov <[email protected]>
1 parent 82b60ac commit f45feb5

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

docs/environment-variables.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,7 @@ those.
287287
- `GRAPH_ENABLE_SQL_QUERIES`: Enable the experimental [SQL query
288288
interface](implementation/sql-interface.md).
289289
(default: false)
290+
- `GRAPH_STORE_DISABLE_CALL_CACHE`: Disables the store call cache entirely. Graph node will skip writing and reading from the
291+
call cache. The buffered block call cache will still be enabled. This option may be useful
292+
for indexers who are running their own RPC nodes. Disabling the store call cache may have
293+
significant performance impact. (default: false)

graph/src/env/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ lazy_static! {
2525
lazy_static! {
2626
pub static ref TEST_WITH_NO_REORG: Mutex<bool> = Mutex::new(false);
2727
pub static ref TEST_SQL_QUERIES_ENABLED: Mutex<bool> = Mutex::new(false);
28+
pub static ref TEST_STORE_CALL_CACHE_DISABLED: Mutex<bool> = Mutex::new(false);
2829
}
2930

3031
/// Panics if:
@@ -441,6 +442,25 @@ impl EnvVars {
441442
let mut lock = TEST_SQL_QUERIES_ENABLED.lock().unwrap();
442443
*lock = enable;
443444
}
445+
446+
#[cfg(debug_assertions)]
447+
pub fn store_call_cache_disabled(&self) -> bool {
448+
if *TEST_STORE_CALL_CACHE_DISABLED.lock().unwrap() {
449+
true
450+
} else {
451+
self.store.disable_call_cache
452+
}
453+
}
454+
455+
#[cfg(not(debug_assertions))]
456+
pub fn store_call_cache_disabled(&self) -> bool {
457+
self.store.disable_call_cache
458+
}
459+
460+
#[cfg(debug_assertions)]
461+
pub fn set_store_call_cache_disabled_for_tests(&self, value: bool) {
462+
*TEST_STORE_CALL_CACHE_DISABLED.lock().unwrap() = value;
463+
}
444464
}
445465

446466
impl Default for EnvVars {

graph/src/env/store.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ pub struct EnvVarsStore {
149149
/// The number of rows to fetch from the foreign data wrapper in one go,
150150
/// this will be set as the option 'fetch_size' on all foreign servers
151151
pub fdw_fetch_size: usize,
152+
/// Disables the store call cache entirely. Graph node will skip writing and reading from the
153+
/// call cache. The buffered block call cache will still be enabled.
154+
/// Set by `GRAPH_STORE_DISABLE_CALL_CACHE`. Defaults to false.
155+
pub disable_call_cache: bool,
152156
}
153157

154158
// This does not print any values avoid accidentally leaking any sensitive env vars
@@ -206,6 +210,7 @@ impl TryFrom<InnerStore> for EnvVarsStore {
206210
disable_block_cache_for_lookup: x.disable_block_cache_for_lookup,
207211
insert_extra_cols: x.insert_extra_cols,
208212
fdw_fetch_size: x.fdw_fetch_size,
213+
disable_call_cache: x.disable_call_cache,
209214
};
210215
if let Some(timeout) = vars.batch_timeout {
211216
if timeout < 2 * vars.batch_target_duration {
@@ -295,6 +300,8 @@ pub struct InnerStore {
295300
insert_extra_cols: usize,
296301
#[envconfig(from = "GRAPH_STORE_FDW_FETCH_SIZE", default = "1000")]
297302
fdw_fetch_size: usize,
303+
#[envconfig(from = "GRAPH_STORE_DISABLE_CALL_CACHE", default = "false")]
304+
disable_call_cache: bool,
298305
}
299306

300307
#[derive(Clone, Copy, Debug)]

store/postgres/src/chain_store.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3044,6 +3044,10 @@ impl EthereumCallCache for ChainStore {
30443044
req: &call::Request,
30453045
block: BlockPtr,
30463046
) -> Result<Option<call::Response>, Error> {
3047+
if ENV_VARS.store_call_cache_disabled() {
3048+
return Ok(None);
3049+
}
3050+
30473051
let id = contract_call_id(req, &block);
30483052
let conn = &mut self.pool.get().await?;
30493053
let return_value = conn
@@ -3134,6 +3138,10 @@ impl EthereumCallCache for ChainStore {
31343138
block: BlockPtr,
31353139
return_value: call::Retval,
31363140
) -> Result<(), Error> {
3141+
if ENV_VARS.store_call_cache_disabled() {
3142+
return Ok(());
3143+
}
3144+
31373145
let return_value = match return_value {
31383146
call::Retval::Value(return_value) if !return_value.is_empty() => return_value,
31393147
_ => {

store/test-store/tests/postgres/chain_head.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,36 @@ fn eth_call_cache() {
529529
})
530530
}
531531

532+
#[test]
533+
fn test_disable_call_cache() {
534+
let chain = vec![&*GENESIS_BLOCK, &*BLOCK_ONE, &*BLOCK_TWO];
535+
536+
run_test(chain, |store, _| {
537+
ENV_VARS.set_store_call_cache_disabled_for_tests(true);
538+
539+
let logger = LOGGER.cheap_clone();
540+
let address = H160([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]);
541+
let call: [u8; 6] = [1, 2, 3, 4, 5, 6];
542+
let return_value: [u8; 3] = [7, 8, 9];
543+
544+
let call = call::Request::new(address, call.to_vec(), 0);
545+
546+
store
547+
.set_call(
548+
&logger,
549+
call.cheap_clone(),
550+
BLOCK_ONE.block_ptr(),
551+
call::Retval::Value(Bytes::from(return_value)),
552+
)
553+
.unwrap();
554+
555+
let ret = store.get_call(&call, BLOCK_ONE.block_ptr()).unwrap();
556+
557+
assert!(ret.is_none());
558+
Ok(())
559+
});
560+
}
561+
532562
#[test]
533563
/// Tests mainly query correctness. Requires data in order not to hit early returns when no stale contracts are found.
534564
fn test_clear_stale_call_cache() {

0 commit comments

Comments
 (0)