Skip to content

Commit 69878ff

Browse files
committed
store: Thread cache_size to ChainStore
Pass the per-chain cache_size configuration from TOML config through StoreBuilder and BlockStore to ChainStore, where it will be used to determine which blocks should be treated as uncached.
1 parent 06d592d commit 69878ff

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

node/src/store_builder.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::iter::FromIterator;
22
use std::{collections::HashMap, sync::Arc};
33

4+
use graph::components::store::BLOCK_CACHE_SIZE;
45
use graph::prelude::{o, MetricsRegistry, NodeId};
56
use graph::slog::warn;
67
use graph::url::Url;
@@ -24,7 +25,8 @@ pub struct StoreBuilder {
2425
subscription_manager: Arc<SubscriptionManager>,
2526
chain_head_update_listener: Arc<PostgresChainHeadUpdateListener>,
2627
/// Map network names to the shards where they are/should be stored
27-
chains: HashMap<String, ShardName>,
28+
/// and their cache_size setting
29+
chains: HashMap<String, (ShardName, i32)>,
2830
pub coord: Arc<PoolCoordinator>,
2931
registry: Arc<MetricsRegistry>,
3032
}
@@ -65,7 +67,7 @@ impl StoreBuilder {
6567
let chains = HashMap::from_iter(config.chains.chains.iter().map(|(name, chain)| {
6668
let shard = ShardName::new(chain.shard.to_string())
6769
.expect("config validation catches invalid names");
68-
(name.to_string(), shard)
70+
(name.to_string(), (shard, chain.cache_size))
6971
}));
7072

7173
let chain_head_update_listener = Arc::new(PostgresChainHeadUpdateListener::new(
@@ -177,15 +179,18 @@ impl StoreBuilder {
177179
logger: &Logger,
178180
pools: HashMap<ShardName, ConnectionPool>,
179181
subgraph_store: Arc<SubgraphStore>,
180-
chains: HashMap<String, ShardName>,
182+
chains: HashMap<String, (ShardName, i32)>,
181183
networks: Vec<String>,
182184
registry: Arc<MetricsRegistry>,
183185
) -> Arc<DieselStore> {
184186
let networks = networks
185187
.into_iter()
186188
.map(|name| {
187-
let shard = chains.get(&name).unwrap_or(&*PRIMARY_SHARD).clone();
188-
(name, shard)
189+
let (shard, cache_size) = chains
190+
.get(&name)
191+
.cloned()
192+
.unwrap_or_else(|| (PRIMARY_SHARD.clone(), BLOCK_CACHE_SIZE));
193+
(name, shard, cache_size)
189194
})
190195
.collect();
191196

store/postgres/src/block_store.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{collections::HashMap, sync::Arc, time::Duration};
22

3-
use graph::parking_lot::RwLock;
3+
use graph::{components::store::BLOCK_CACHE_SIZE, parking_lot::RwLock};
44

55
use anyhow::anyhow;
66
use async_trait::async_trait;
@@ -217,8 +217,9 @@ pub struct Inner {
217217
/// known to the system at startup, either from configuration or from
218218
/// previous state in the database.
219219
stores: RwLock<HashMap<String, Arc<ChainStore>>>,
220-
// We keep this information so we can create chain stores during startup
221-
shards: Vec<(String, Shard)>,
220+
/// We keep this information so we can create chain stores during
221+
/// startup. The triple is (network, shard, cache_size)
222+
shards: Vec<(String, Shard, BlockNumber)>,
222223
pools: HashMap<Shard, ConnectionPool>,
223224
sender: Arc<NotificationSender>,
224225
mirror: PrimaryMirror,
@@ -240,8 +241,8 @@ impl BlockStore {
240241
/// a chain uses the pool from `pools` for the given shard.
241242
pub async fn new(
242243
logger: Logger,
243-
// (network, shard)
244-
shards: Vec<(String, Shard)>,
244+
// (network, shard, cache_size)
245+
shards: Vec<(String, Shard, BlockNumber)>,
245246
// shard -> pool
246247
pools: HashMap<Shard, ConnectionPool>,
247248
sender: Arc<NotificationSender>,
@@ -271,7 +272,7 @@ impl BlockStore {
271272
let block_store = Self { inner };
272273

273274
// For each configured chain, add a chain store
274-
for (chain_name, shard) in chains {
275+
for (chain_name, shard, _cache_size) in chains {
275276
if let Some(chain) = existing_chains
276277
.iter()
277278
.find(|chain| chain.name == chain_name)
@@ -363,6 +364,17 @@ impl BlockStore {
363364
);
364365
let ident = chain.network_identifier()?;
365366
let logger = self.logger.new(o!("network" => chain.name.clone()));
367+
let cache_size = self
368+
.shards
369+
.iter()
370+
.find_map(|(network, _, chain_size)| {
371+
if network == &chain.name {
372+
Some(*chain_size)
373+
} else {
374+
None
375+
}
376+
})
377+
.unwrap_or(BLOCK_CACHE_SIZE);
366378
let store = ChainStore::new(
367379
logger,
368380
chain.name.clone(),
@@ -371,6 +383,7 @@ impl BlockStore {
371383
pool,
372384
ENV_VARS.store.recent_blocks_cache_capacity,
373385
self.chain_store_metrics.clone(),
386+
cache_size,
374387
);
375388
if create {
376389
store.create(&ident).await?;
@@ -565,7 +578,7 @@ impl BlockStore {
565578
let shard = self
566579
.shards
567580
.iter()
568-
.find_map(|(chain_id, shard)| {
581+
.find_map(|(chain_id, shard, _cache_size)| {
569582
if chain_id.as_str().eq(network) {
570583
Some(shard)
571584
} else {

store/postgres/src/chain_store.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,9 @@ pub struct ChainStore {
23472347
chain_head_ptr_cache: ChainHeadPtrCache,
23482348
/// Herd cache to prevent thundering herd on chain_head_ptr() lookups
23492349
chain_head_ptr_herd: HerdCache<Arc<Result<Option<BlockPtr>, StoreError>>>,
2350+
/// Number of blocks from chain head for which to keep block data cached.
2351+
/// Used with `GRAPH_STORE_IGNORE_BLOCK_CACHE` to simulate block data eviction.
2352+
cache_size: BlockNumber,
23502353
}
23512354

23522355
impl ChainStore {
@@ -2358,6 +2361,7 @@ impl ChainStore {
23582361
pool: ConnectionPool,
23592362
recent_blocks_cache_capacity: usize,
23602363
metrics: Arc<ChainStoreMetrics>,
2364+
cache_size: BlockNumber,
23612365
) -> Self {
23622366
let recent_blocks_cache =
23632367
RecentBlocksCache::new(recent_blocks_cache_capacity, chain.clone(), metrics.clone());
@@ -2378,6 +2382,7 @@ impl ChainStore {
23782382
ancestor_cache,
23792383
chain_head_ptr_cache,
23802384
chain_head_ptr_herd,
2385+
cache_size,
23812386
}
23822387
}
23832388

0 commit comments

Comments
 (0)