Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
more fine grained runtime api caching + a fix for av-store (#3457)
Browse files Browse the repository at this point in the history
* finer grained runtime-api caching

* fix av-store

* simplify a request

* remove unused imports

* fix tests
  • Loading branch information
ordian authored Jul 12, 2021
1 parent 1d47832 commit ac38d38
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
32 changes: 13 additions & 19 deletions node/core/av-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use polkadot_node_subsystem_util::{
metrics::{self, prometheus},
};
use polkadot_subsystem::messages::{
AvailabilityStoreMessage, ChainApiMessage, RuntimeApiMessage, RuntimeApiRequest,
AvailabilityStoreMessage, ChainApiMessage,
};
use bitvec::{vec::BitVec, order::Lsb0 as BitOrderLsb0};

Expand Down Expand Up @@ -655,9 +655,11 @@ where
subsystem.finalized_number.unwrap_or(block_number.saturating_sub(1)),
).await?;

let mut tx = DBTransaction::new();
// determine_new_blocks is descending in block height
for (hash, header) in new_blocks.into_iter().rev() {
// it's important to commit the db transactions for a head before the next one is processed
// alternatively, we could utilize the OverlayBackend from approval-voting
let mut tx = DBTransaction::new();
process_new_head(
ctx,
&subsystem.db,
Expand All @@ -669,8 +671,8 @@ where
header,
).await?;
subsystem.known_blocks.insert(hash, block_number);
subsystem.db.write(tx)?;
}
subsystem.db.write(tx)?;

Ok(())
}
Expand All @@ -690,25 +692,17 @@ where
Context: overseer::SubsystemContext<Message = AvailabilityStoreMessage>,
{

let candidate_events = {
let (tx, rx) = oneshot::channel();
ctx.send_message(
RuntimeApiMessage::Request(hash, RuntimeApiRequest::CandidateEvents(tx))
).await;

rx.await??
};
let candidate_events = util::request_candidate_events(
hash,
ctx.sender(),
).await.await??;

// We need to request the number of validators based on the parent state,
// as that is the number of validators used to create this block.
let n_validators = {
let (tx, rx) = oneshot::channel();
ctx.send_message(
RuntimeApiMessage::Request(header.parent_hash, RuntimeApiRequest::Validators(tx))
).await;

rx.await??.len()
};
let n_validators = util::request_validators(
header.parent_hash,
ctx.sender(),
).await.await??.len();

for event in candidate_events {
match event {
Expand Down
4 changes: 2 additions & 2 deletions node/core/av-store/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use polkadot_primitives::v1::{
use polkadot_node_primitives::{AvailableData, BlockData, PoV};
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_subsystem::{
ActiveLeavesUpdate, errors::RuntimeApiError, jaeger, messages::AllMessages, ActivatedLeaf,
LeafStatus,
ActiveLeavesUpdate, errors::RuntimeApiError, jaeger, ActivatedLeaf,
LeafStatus, messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest},
};
use polkadot_node_subsystem_test_helpers as test_helpers;
use sp_keyring::Sr25519Keyring;
Expand Down
14 changes: 8 additions & 6 deletions node/core/runtime-api/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ pub(crate) struct RequestResultCache {
check_validation_outputs: MemoryLruCache<(Hash, ParaId, CandidateCommitments), ResidentSizeOf<bool>>,
session_index_for_child: MemoryLruCache<Hash, ResidentSizeOf<SessionIndex>>,
validation_code: MemoryLruCache<(Hash, ParaId, OccupiedCoreAssumption), ResidentSizeOf<Option<ValidationCode>>>,
validation_code_by_hash: MemoryLruCache<(Hash, ValidationCodeHash), ResidentSizeOf<Option<ValidationCode>>>,
validation_code_by_hash: MemoryLruCache<ValidationCodeHash, ResidentSizeOf<Option<ValidationCode>>>,
candidate_pending_availability: MemoryLruCache<(Hash, ParaId), ResidentSizeOf<Option<CommittedCandidateReceipt>>>,
candidate_events: MemoryLruCache<Hash, ResidentSizeOf<Vec<CandidateEvent>>>,
session_info: MemoryLruCache<(Hash, SessionIndex), ResidentSizeOf<Option<SessionInfo>>>,
session_info: MemoryLruCache<SessionIndex, ResidentSizeOf<Option<SessionInfo>>>,
dmq_contents: MemoryLruCache<(Hash, ParaId), ResidentSizeOf<Vec<InboundDownwardMessage<BlockNumber>>>>,
inbound_hrmp_channels_contents: MemoryLruCache<(Hash, ParaId), ResidentSizeOf<BTreeMap<ParaId, Vec<InboundHrmpMessage<BlockNumber>>>>>,
current_babe_epoch: MemoryLruCache<Hash, DoesNotAllocate<Epoch>>,
Expand Down Expand Up @@ -173,11 +173,13 @@ impl RequestResultCache {
self.validation_code.insert(key, ResidentSizeOf(value));
}

// the actual key is `ValidationCodeHash` (`Hash` is ignored),
// but we keep the interface that way to keep the macro simple
pub(crate) fn validation_code_by_hash(&mut self, key: (Hash, ValidationCodeHash)) -> Option<&Option<ValidationCode>> {
self.validation_code_by_hash.get(&key).map(|v| &v.0)
self.validation_code_by_hash.get(&key.1).map(|v| &v.0)
}

pub(crate) fn cache_validation_code_by_hash(&mut self, key: (Hash, ValidationCodeHash), value: Option<ValidationCode>) {
pub(crate) fn cache_validation_code_by_hash(&mut self, key: ValidationCodeHash, value: Option<ValidationCode>) {
self.validation_code_by_hash.insert(key, ResidentSizeOf(value));
}

Expand All @@ -198,10 +200,10 @@ impl RequestResultCache {
}

pub(crate) fn session_info(&mut self, key: (Hash, SessionIndex)) -> Option<&Option<SessionInfo>> {
self.session_info.get(&key).map(|v| &v.0)
self.session_info.get(&key.1).map(|v| &v.0)
}

pub(crate) fn cache_session_info(&mut self, key: (Hash, SessionIndex), value: Option<SessionInfo>) {
pub(crate) fn cache_session_info(&mut self, key: SessionIndex, value: Option<SessionInfo>) {
self.session_info.insert(key, ResidentSizeOf(value));
}

Expand Down
8 changes: 4 additions & 4 deletions node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ impl<Client> RuntimeApiSubsystem<Client> where
self.requests_cache.cache_session_index_for_child(relay_parent, session_index),
ValidationCode(relay_parent, para_id, assumption, code) =>
self.requests_cache.cache_validation_code((relay_parent, para_id, assumption), code),
ValidationCodeByHash(relay_parent, validation_code_hash, code) =>
self.requests_cache.cache_validation_code_by_hash((relay_parent, validation_code_hash), code),
ValidationCodeByHash(_relay_parent, validation_code_hash, code) =>
self.requests_cache.cache_validation_code_by_hash(validation_code_hash, code),
CandidatePendingAvailability(relay_parent, para_id, candidate) =>
self.requests_cache.cache_candidate_pending_availability((relay_parent, para_id), candidate),
CandidateEvents(relay_parent, events) =>
self.requests_cache.cache_candidate_events(relay_parent, events),
SessionInfo(relay_parent, session_index, info) =>
self.requests_cache.cache_session_info((relay_parent, session_index), info),
SessionInfo(_relay_parent, session_index, info) =>
self.requests_cache.cache_session_info(session_index, info),
DmqContents(relay_parent, para_id, messages) =>
self.requests_cache.cache_dmq_contents((relay_parent, para_id), messages),
InboundHrmpChannelsContents(relay_parent, para_id, contents) =>
Expand Down

0 comments on commit ac38d38

Please sign in to comment.