Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backing: improve session buffering for runtime information #6284

Merged
merged 10 commits into from
Nov 13, 2024

Conversation

sw10pa
Copy link
Member

@sw10pa sw10pa commented Oct 30, 2024

Issue

[#3421] backing: improve session buffering for runtime information

Description

In the current implementation of the backing module, certain pieces of information, which remain unchanged throughout a session, are fetched multiple times via runtime API calls. The goal of this task was to introduce a local cache to store such session-stable information and perform the runtime API call only once per session.

This PR implements caching specifically for the validators list, node features, executor parameters, minimum backing votes threshold, and validator-to-group mapping, which were previously fetched from the runtime or computed each time PerRelayParentState was built. Now, this information is cached and reused within the session.

TODO

  • Create a separate struct for per-session caches;
  • Cache validators list;
  • Cache node features;
  • Cache executor parameters;
  • Cache minimum backing votes threshold;
  • Cache validator-to-group mapping;
  • Update tests to reflect these changes;
  • Add prdoc.

For the next PR

Cache validator groups and any other session-stable data (if present).

@sw10pa sw10pa added I9-optimisation An enhancement to provide better overall performance in terms of time-to-completion for a task. T4-runtime_API This PR/Issue is related to runtime APIs. C2-good-first-issue A task for a first time contributor to become familiar with the Polkadot-SDK. labels Oct 30, 2024
@sw10pa sw10pa self-assigned this Oct 30, 2024
@sw10pa sw10pa linked an issue Oct 30, 2024 that may be closed by this pull request
@sw10pa sw10pa added T0-node This PR/Issue is related to the topic “node”. T8-polkadot This PR/Issue is related to/affects the Polkadot network. labels Oct 30, 2024
Copy link
Contributor

@alindima alindima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looking good so far! Let's see what else we can cache 😄

polkadot/node/core/backing/src/lib.rs Outdated Show resolved Hide resolved
…d caching for node_features, minimum_backing_votes and validator_to_group mapping
@sw10pa sw10pa requested a review from alindima November 4, 2024 23:24
polkadot/node/core/backing/src/lib.rs Outdated Show resolved Hide resolved
polkadot/node/core/backing/src/lib.rs Outdated Show resolved Hide resolved
polkadot/node/core/backing/src/lib.rs Outdated Show resolved Hide resolved
polkadot/node/core/backing/src/lib.rs Outdated Show resolved Hide resolved
@sw10pa sw10pa requested a review from alindima November 6, 2024 19:17
@sw10pa sw10pa marked this pull request as ready for review November 6, 2024 19:17
@sw10pa sw10pa changed the title [WIP] backing: improve session buffering for runtime information backing: improve session buffering for runtime information Nov 7, 2024
Copy link
Contributor

@alindima alindima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small suggestions left. Let's also add a prdoc. Thanks!

polkadot/node/core/backing/src/tests/mod.rs Show resolved Hide resolved
polkadot/node/subsystem-util/src/lib.rs Outdated Show resolved Hide resolved
polkadot/node/core/backing/src/tests/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@eskimor eskimor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good, but I would suggest to move the implementation to the general purpose one we already have.

}
}

impl PerSessionCache {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we already have such a cache:

I would suggest to adjust the existing implementation to cater to the needs here. For example, usage of multiple LRUs makes sense, so subsystems don't pay for what they don't use.

@alindima
Copy link
Contributor

code looks good, but I would suggest to move the implementation to the general purpose one we already have.

@eskimor, I would suggest merging this PR first as it is, since there are already multiple subsystems doing their own caching and it was also a learning experience for @sw10pa as his first PR. The RuntimeInfo needs a redesign in order for it to be reusable by more subsystems. We can open an issue for it and be worked at a later time.

@@ -105,6 +105,9 @@ pub enum Error {

#[error("Availability store error")]
StoreAvailableData(#[source] StoreAvailableDataError),

#[error("Data is not available")]
DataNotAvailable,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't say anything

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a temporary solution, I have changed the name and description of the error but, as discussed separately, will try to replace RuntimeApiErrors with Subsystem errors in a separate PR.

Copy link
Contributor

@sandreim sandreim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @sw10pa ! I agree with @eskimor about adapting an existing solution and it should be fine to do that as part of larger refactor and diff PR as @alindima suggests

}

/// Gets validators from the cache or fetches them from the runtime if not present.
async fn get_or_fetch_validators(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is a cache struct, the function names are overly verbose. I'd suggest just validators(), node_fetures(), executor_params.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed.

@alindima alindima added this pull request to the merge queue Nov 13, 2024
Merged via the queue into master with commit e617d1d Nov 13, 2024
195 of 199 checks passed
@alindima alindima deleted the sw10pa/backing-session-buffering branch November 13, 2024 14:37
tdimitrov added a commit that referenced this pull request Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C2-good-first-issue A task for a first time contributor to become familiar with the Polkadot-SDK. I9-optimisation An enhancement to provide better overall performance in terms of time-to-completion for a task. T0-node This PR/Issue is related to the topic “node”. T4-runtime_API This PR/Issue is related to runtime APIs. T8-polkadot This PR/Issue is related to/affects the Polkadot network.
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

backing: improve session buffering for runtime information
4 participants