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

rpc v2: backpressure chainHead_v1_storage #5741

Merged
merged 37 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7f32fdf
rpc v2: rely backpressure `Storage::query_iter`
niklasad1 Sep 9, 2024
c7d9a50
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head_storage.rs
niklasad1 Sep 17, 2024
540daa6
Update substrate/client/rpc-spec-v2/src/chain_head/chain_head_storage.rs
niklasad1 Sep 17, 2024
423bc30
Update substrate/client/rpc-spec-v2/src/chain_head/tests.rs
niklasad1 Sep 17, 2024
ef62794
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 17, 2024
7351206
cleanup
niklasad1 Sep 18, 2024
b963c8b
revert archive static limits
niklasad1 Sep 18, 2024
1cb17f3
cargo fmt
niklasad1 Sep 18, 2024
0c94a6b
add trait bound RawIter: Send
niklasad1 Sep 19, 2024
1612813
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 19, 2024
583ea58
cargo fmt
niklasad1 Sep 19, 2024
486b246
remove unused import
niklasad1 Sep 19, 2024
67a37ce
Update substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
niklasad1 Sep 19, 2024
05386e8
Update substrate/client/rpc-spec-v2/src/chain_head/subscription/inner.rs
niklasad1 Sep 19, 2024
dc17cc4
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 24, 2024
ab88c05
address grumbles
niklasad1 Sep 24, 2024
accb056
add prdoc
niklasad1 Sep 24, 2024
7a7a75e
Update prdoc/pr_5741.prdoc
niklasad1 Sep 24, 2024
6fa42e4
prdoc fixes
niklasad1 Sep 24, 2024
08699f6
".git/.scripts/commands/fmt/fmt.sh"
Sep 24, 2024
9766eb3
bump tokio for permit api
niklasad1 Sep 25, 2024
af28077
simplify the code
niklasad1 Sep 26, 2024
8ccd391
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 26, 2024
737e858
fix tests
niklasad1 Sep 27, 2024
797f1da
cargo fmt
niklasad1 Sep 27, 2024
2ceadfc
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 27, 2024
381fcc0
".git/.scripts/commands/fmt/fmt.sh"
Sep 27, 2024
6263c83
Revert "".git/.scripts/commands/fmt/fmt.sh""
niklasad1 Sep 27, 2024
097a50e
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Sep 27, 2024
34393db
Update substrate/client/rpc-spec-v2/Cargo.toml
niklasad1 Sep 27, 2024
25051cd
Update prdoc/pr_5741.prdoc
niklasad1 Oct 1, 2024
4a8c5c3
Merge branch 'master' into na-fix-rpc-storage-iter
niklasad1 Oct 1, 2024
9152f9e
remove needless trait bounds
niklasad1 Oct 2, 2024
ec25f00
Merge remote-tracking branch 'origin/master' into na-fix-rpc-storage-…
niklasad1 Oct 2, 2024
bb251b0
remove more needless trait bounds
niklasad1 Oct 2, 2024
49e013c
remove more needless trait bounds again
niklasad1 Oct 2, 2024
4a70b0e
Merge branch 'master' into na-fix-rpc-storage-iter
niklasad1 Oct 2, 2024
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
2 changes: 1 addition & 1 deletion substrate/client/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ where
};

let rpc_middleware =
RpcServiceBuilder::new().option_layer(middleware_layer.clone());
RpcServiceBuilder::new().rpc_logger(1024).option_layer(middleware_layer.clone());
let mut svc = service_builder
.set_rpc_middleware(rpc_middleware)
.build(methods, stop_handle);
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/rpc-spec-v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ sp-rpc = { workspace = true, default-features = true }
sp-blockchain = { workspace = true, default-features = true }
sp-version = { workspace = true, default-features = true }
sc-client-api = { workspace = true, default-features = true }
sc-utils = { workspace = true, default-features = true }
sc-rpc = { workspace = true, default-features = true }
codec = { workspace = true, default-features = true }
thiserror = { workspace = true }
Expand Down Expand Up @@ -59,3 +58,4 @@ sc-service = { features = ["test-helpers"], workspace = true, default-features =
assert_matches = { workspace = true }
pretty_assertions = { workspace = true }
sc-transaction-pool = { workspace = true, default-features = true }
sc-utils = { workspace = true, default-features = true }
56 changes: 7 additions & 49 deletions substrate/client/rpc-spec-v2/src/archive/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use sc_client_api::{
Backend, BlockBackend, BlockchainEvents, CallExecutor, ChildInfo, ExecutorProvider, StorageKey,
StorageProvider,
};
use sc_rpc::SubscriptionTaskExecutor;
use sp_api::{CallApiAt, CallContext};
use sp_blockchain::{
Backend as BlockChainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata,
Expand All @@ -43,36 +44,6 @@ use std::{collections::HashSet, marker::PhantomData, sync::Arc};

use super::archive_storage::ArchiveStorage;

/// The configuration of [`Archive`].
pub struct ArchiveConfig {
/// The maximum number of items the `archive_storage` can return for a descendant query before
/// pagination is required.
pub max_descendant_responses: usize,
/// The maximum number of queried items allowed for the `archive_storage` at a time.
pub max_queried_items: usize,
}

/// The maximum number of items the `archive_storage` can return for a descendant query before
/// pagination is required.
///
/// Note: this is identical to the `chainHead` value.
const MAX_DESCENDANT_RESPONSES: usize = 5;

/// The maximum number of queried items allowed for the `archive_storage` at a time.
///
/// Note: A queried item can also be a descendant query which can return up to
/// `MAX_DESCENDANT_RESPONSES`.
const MAX_QUERIED_ITEMS: usize = 8;

impl Default for ArchiveConfig {
fn default() -> Self {
Self {
max_descendant_responses: MAX_DESCENDANT_RESPONSES,
max_queried_items: MAX_QUERIED_ITEMS,
}
}
}

/// An API for archive RPC calls.
pub struct Archive<BE: Backend<Block>, Block: BlockT, Client> {
/// Substrate client.
Expand All @@ -81,13 +52,10 @@ pub struct Archive<BE: Backend<Block>, Block: BlockT, Client> {
backend: Arc<BE>,
/// The hexadecimal encoded hash of the genesis block.
genesis_hash: String,
/// The maximum number of items the `archive_storage` can return for a descendant query before
/// pagination is required.
storage_max_descendant_responses: usize,
/// The maximum number of queried items allowed for the `archive_storage` at a time.
storage_max_queried_items: usize,
/// Phantom member to pin the block type.
_phantom: PhantomData<Block>,
/// Subscription task executor.
executor: SubscriptionTaskExecutor,
}

impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
Expand All @@ -96,17 +64,10 @@ impl<BE: Backend<Block>, Block: BlockT, Client> Archive<BE, Block, Client> {
client: Arc<Client>,
backend: Arc<BE>,
genesis_hash: GenesisHash,
config: ArchiveConfig,
executor: SubscriptionTaskExecutor,
) -> Self {
let genesis_hash = hex_string(&genesis_hash.as_ref());
Self {
client,
backend,
genesis_hash,
storage_max_descendant_responses: config.max_descendant_responses,
storage_max_queried_items: config.max_queried_items,
_phantom: PhantomData,
}
Self { client, backend, genesis_hash, _phantom: PhantomData, executor }
}
}

Expand Down Expand Up @@ -270,11 +231,8 @@ where
.transpose()?
.map(ChildInfo::new_default_from_vec);

let storage_client = ArchiveStorage::new(
self.client.clone(),
self.storage_max_descendant_responses,
self.storage_max_queried_items,
);
let storage_client = ArchiveStorage::new(self.client.clone(), self.executor.clone());

Ok(storage_client.handle_query(hash, items, child_trie))
}
}
80 changes: 32 additions & 48 deletions substrate/client/rpc-spec-v2/src/archive/archive_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use std::sync::Arc;

use sc_client_api::{Backend, ChildInfo, StorageKey, StorageProvider};
use sc_rpc::SubscriptionTaskExecutor;
use sp_runtime::traits::Block as BlockT;

use crate::common::{
Expand All @@ -32,44 +33,31 @@ use crate::common::{
pub struct ArchiveStorage<Client, Block, BE> {
/// Storage client.
client: Storage<Client, Block, BE>,
/// The maximum number of responses the API can return for a descendant query at a time.
storage_max_descendant_responses: usize,
/// The maximum number of queried items allowed for the `archive_storage` at a time.
storage_max_queried_items: usize,
}

impl<Client, Block, BE> ArchiveStorage<Client, Block, BE> {
/// Constructs a new [`ArchiveStorage`].
pub fn new(
client: Arc<Client>,
storage_max_descendant_responses: usize,
storage_max_queried_items: usize,
) -> Self {
Self {
client: Storage::new(client),
storage_max_descendant_responses,
storage_max_queried_items,
}
pub fn new(client: Arc<Client>, executor: SubscriptionTaskExecutor) -> Self {
Self { client: Storage::new(client, executor) }
}
}

impl<Client, Block, BE> ArchiveStorage<Client, Block, BE>
where
Block: BlockT + 'static,
BE: Backend<Block> + 'static,
Client: StorageProvider<Block, BE> + 'static,
Block: BlockT + Send + 'static,
BE: Backend<Block> + Send + 'static,
Client: StorageProvider<Block, BE> + Send + Sync + 'static,
{
/// Generate the response of the `archive_storage` method.
pub fn handle_query(
&self,
hash: Block::Hash,
mut items: Vec<PaginatedStorageQuery<StorageKey>>,
items: Vec<PaginatedStorageQuery<StorageKey>>,
child_key: Option<ChildInfo>,
) -> ArchiveStorageResult {
let discarded_items = items.len().saturating_sub(self.storage_max_queried_items);
items.truncate(self.storage_max_queried_items);

let mut storage_results = Vec::with_capacity(items.len());
let mut query_iter = Vec::new();

for item in items {
match item.query_type {
StorageQueryType::Value => {
Expand All @@ -92,38 +80,34 @@ where
Err(error) => return ArchiveStorageResult::err(error),
},
StorageQueryType::DescendantsValues => {
match self.client.query_iter_pagination(
QueryIter {
query_key: item.key,
ty: IterQueryType::Value,
pagination_start_key: item.pagination_start_key,
},
hash,
child_key.as_ref(),
self.storage_max_descendant_responses,
) {
Ok((results, _)) => storage_results.extend(results),
Err(error) => return ArchiveStorageResult::err(error),
}
query_iter.push(QueryIter {
query_key: item.key,
ty: IterQueryType::Value,
pagination_start_key: item.pagination_start_key,
});
},
StorageQueryType::DescendantsHashes => {
match self.client.query_iter_pagination(
QueryIter {
query_key: item.key,
ty: IterQueryType::Hash,
pagination_start_key: item.pagination_start_key,
},
hash,
child_key.as_ref(),
self.storage_max_descendant_responses,
) {
Ok((results, _)) => storage_results.extend(results),
Err(error) => return ArchiveStorageResult::err(error),
}
query_iter.push(QueryIter {
query_key: item.key,
ty: IterQueryType::Hash,
pagination_start_key: item.pagination_start_key,
});
},
};
}

ArchiveStorageResult::ok(storage_results, discarded_items)
if !query_iter.is_empty() {
let mut rx = self.client.query_iter_pagination(query_iter, hash, child_key);

while let Some(val) = rx.blocking_recv() {
match val {
Ok(Some(value)) => storage_results.push(value),
Ok(None) => continue,
Err(error) => return ArchiveStorageResult::err(error),
}
}
}

ArchiveStorageResult::ok(storage_results, 0)
}
}
2 changes: 1 addition & 1 deletion substrate/client/rpc-spec-v2/src/archive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ pub mod archive;
pub mod error;

pub use api::ArchiveApiServer;
pub use archive::{Archive, ArchiveConfig};
pub use archive::Archive;
Loading
Loading