Skip to content

Commit

Permalink
optimise number of connections to database
Browse files Browse the repository at this point in the history
  • Loading branch information
kobayurii committed Jul 24, 2024
1 parent 9e665e3 commit ebd76f3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 45 deletions.
2 changes: 2 additions & 0 deletions database/src/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl PostgresDBManager {
read_only: bool,
) -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(128) // Start with 64 connections based on 4x the number of CPU cores.
.connect(database_url)
.await?;
if !read_only {
Expand All @@ -60,6 +61,7 @@ impl PostgresDBManager {
read_only: bool,
) -> anyhow::Result<sqlx::Pool<sqlx::Postgres>> {
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(128) // Start with 64 connections based on 4x the number of CPU cores.
.connect(database_url)
.await?;
if !read_only {
Expand Down
4 changes: 2 additions & 2 deletions rpc-server/src/modules/queries/contract_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ pub async fn run_contract(
};

// TODO: Temporary solution just for testing
let prefetch_state = if contract.data.storage_usage() < 1000000 {
let prefetch_state = if contract.data.storage_usage() < prefetch_state_size_limit {
true
} else if contract.data.storage_usage() < prefetch_state_size_limit {
} else if contract.data.storage_usage() < 5000000 {
if account_id.to_string().ends_with("poolv1near") && method_name == "get_accounts" {
true
} else {
Expand Down
38 changes: 16 additions & 22 deletions rpc-server/src/modules/queries/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use jsonrpc_v2::{Data, Params};
use near_jsonrpc::RpcRequest;

use super::contract_runner;
use super::utils::get_state_from_db_with_timeout;
use super::utils::get_state_from_db;

/// `query` rpc method implementation
/// calls proxy_rpc_call to get `query` from near-rpc if request parameters not supported by read-rpc
Expand Down Expand Up @@ -402,7 +402,8 @@ async fn view_state(
is_optimistic,
);

data.db_manager
let account = data
.db_manager
.get_account(account_id, block.block_height, "query_view_state")
.await
.map_err(
Expand All @@ -412,6 +413,15 @@ async fn view_state(
block_hash: block.block_hash,
},
)?;
if account.data.storage_usage() > data.prefetch_state_size_limit {
return Err(
near_jsonrpc::primitives::types::query::RpcQueryError::TooLargeContractState {
contract_account_id: account_id.clone(),
block_height: block.block_height,
block_hash: block.block_hash,
},
);
}

let state_item = if is_optimistic {
optimistic_view_state(data, block, account_id, prefix).await?
Expand Down Expand Up @@ -445,22 +455,14 @@ async fn optimistic_view_state(
.blocks_info_by_finality
.optimistic_state_changes_in_block(account_id, prefix)
.await;
let state_from_db = get_state_from_db_with_timeout(
let state_from_db = get_state_from_db(
&data.db_manager,
account_id,
block.block_height,
prefix,
"query_view_state",
tokio::time::Duration::from_secs(3),
)
.await
.map_err(|_| {
near_jsonrpc::primitives::types::query::RpcQueryError::TooLargeContractState {
contract_account_id: account_id.clone(),
block_height: block.block_height,
block_hash: block.block_hash,
}
})?;
.await;

let mut values: Vec<near_primitives::views::StateItem> = state_from_db
.into_iter()
Expand Down Expand Up @@ -501,22 +503,14 @@ async fn database_view_state(
Vec<near_primitives::views::StateItem>,
near_jsonrpc::primitives::types::query::RpcQueryError,
> {
let state_from_db = get_state_from_db_with_timeout(
let state_from_db = get_state_from_db(
&data.db_manager,
account_id,
block.block_height,
prefix,
"query_view_state",
tokio::time::Duration::from_secs(3),
)
.await
.map_err(|_| {
near_jsonrpc::primitives::types::query::RpcQueryError::TooLargeContractState {
contract_account_id: account_id.clone(),
block_height: block.block_height,
block_hash: block.block_hash,
}
})?;
.await;

let values: Vec<near_primitives::views::StateItem> = state_from_db
.into_iter()
Expand Down
21 changes: 0 additions & 21 deletions rpc-server/src/modules/queries/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,3 @@ pub async fn get_state_from_db(
.await
.unwrap_or_default()
}

pub async fn get_state_from_db_with_timeout(
db_manager: &std::sync::Arc<Box<dyn database::ReaderDbManager + Sync + Send + 'static>>,
account_id: &near_primitives::types::AccountId,
block_height: near_primitives::types::BlockHeight,
prefix: &[u8],
method_name: &str,
timeout: tokio::time::Duration,
) -> anyhow::Result<HashMap<readnode_primitives::StateKey, readnode_primitives::StateValue>> {
tracing::debug!(
"`get_state_from_db_with_timeout` call. AccountId {}, block {}, prefix {:?}",
account_id,
block_height,
prefix,
);
Ok(tokio::time::timeout(
timeout,
get_state_from_db(db_manager, account_id, block_height, prefix, method_name),
)
.await?)
}

0 comments on commit ebd76f3

Please sign in to comment.