Skip to content

Commit

Permalink
cache configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kobayurii committed Jan 6, 2025
1 parent 0d13df1 commit de297bc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions configuration/src/configs/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ impl CommonGeneralRpcServerConfig {
}

pub fn default_contract_code_cache_size() -> f64 {
0.25
2.0
}

pub fn default_block_cache_size() -> f64 {
0.125
3.0
}

pub fn default_shadow_data_consistency_rate() -> f64 {
Expand Down
10 changes: 6 additions & 4 deletions configuration/src/default_env_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ server_port = "${SERVER_PORT}"
max_gas_burnt = "${MAX_GAS_BURNT}"
## Contract code cache in gigabytes
## By default we use 0.25 gigabyte (256MB or 268_435_456 bytes)
## By default we use 2.0 gigabytes
## We divide the cache size 1/4 for contract code and 3/4 for compiled contract code
## Because the compiled contract code is bigger in 3 times than the contract code from the database
contract_code_cache_size = "${CONTRACT_CODE_CACHE_SIZE}"
## Block cache size in gigabytes
## By default we use 0.125 gigabyte (128MB or 134_217_728 bytes)
## One cache_block size is ≈ 96 bytes
## In 128MB we can put 1_398_101 cache_blocks
## By default we use 3 gigabytes
## We devide the cache size 1/3 for block cache and 2/3 for chunks cache
## Because the chunks for block is bigger in 2 times than the block
block_cache_size = "${BLOCK_CACHE_SIZE}"
## How many requests we should check for data consistency
Expand Down
45 changes: 24 additions & 21 deletions rpc-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ pub struct ServerContext {
pub genesis_info: GenesisInfo,
/// Near rpc client
pub near_rpc_client: crate::utils::JsonRpcClient,
/// Blocks cache
/// Blocks cache. Store block_view by block_height
pub blocks_cache:
std::sync::Arc<crate::cache::RwLockLruMemoryCache<u64, near_primitives::views::BlockView>>,
/// Chunks cache
/// Chunks cache. Store vector of block chunks by block_height
pub chunks_cache:
std::sync::Arc<crate::cache::RwLockLruMemoryCache<u64, crate::modules::blocks::ChunksInfo>>,
/// Final block info include final_block_cache and current_validators_info
Expand Down Expand Up @@ -86,30 +86,41 @@ pub struct ServerContext {

impl ServerContext {
pub async fn init(rpc_server_config: configuration::RpcServerConfig) -> anyhow::Result<Self> {
// let contract_code_cache_size_in_bytes =
// crate::utils::gigabytes_to_bytes(rpc_server_config.general.contract_code_cache_size)
// .await;
let total_contract_code_cache_size_in_bytes =
crate::utils::gigabytes_to_bytes(rpc_server_config.general.contract_code_cache_size)
.await;

// For contract code cache we use 0.5GB. make it configurable. temporary hardcoded
let contract_code_cache_size_in_bytes = crate::utils::gigabytes_to_bytes(0.5).await;
// For compiled contract code cache we use 3/4 of total_contract_code_cache_size_in_bytes
// because the compiled contract code is bigger in 3 times than the contract code from the database
let compiled_contract_code_cache_size_in_bytes =
total_contract_code_cache_size_in_bytes / 4;
let compiled_contract_code_cache = std::sync::Arc::new(CompiledCodeCache::new(
compiled_contract_code_cache_size_in_bytes,
));

// For contract code cache we use 1/4 of total_contract_code_cache_size_in_bytes
let contract_code_cache_size_in_bytes =
total_contract_code_cache_size_in_bytes - compiled_contract_code_cache_size_in_bytes;
let contract_code_cache = std::sync::Arc::new(crate::cache::RwLockLruMemoryCache::new(
contract_code_cache_size_in_bytes,
));

// let block_cache_size_in_bytes =
// crate::utils::gigabytes_to_bytes(rpc_server_config.general.block_cache_size).await;
let total_block_cache_size_in_bytes =
crate::utils::gigabytes_to_bytes(rpc_server_config.general.block_cache_size).await;

// For chunk block we use 1GB. make it configurable. temporary hardcoded
let block_cache_size_in_bytes = crate::utils::gigabytes_to_bytes(1.0).await;
// For block cache we use 1/3 of total_block_cache_size_in_bytes
let block_cache_size_in_bytes = total_block_cache_size_in_bytes / 3;
let blocks_cache = std::sync::Arc::new(crate::cache::RwLockLruMemoryCache::new(
block_cache_size_in_bytes,
));

// For chunk cache we use 2GB. make it configurable. temporary hardcoded
let chunk_cache_size_in_bytes = crate::utils::gigabytes_to_bytes(2.0).await;
// For chunk cache we use 2/3 of total_block_cache_size_in_bytes
// because the chunks for block is bigger in 2 times than the block
let chunk_cache_size_in_bytes = total_block_cache_size_in_bytes - block_cache_size_in_bytes;
let chunks_cache = std::sync::Arc::new(crate::cache::RwLockLruMemoryCache::new(
chunk_cache_size_in_bytes,
));

let near_rpc_client = crate::utils::JsonRpcClient::new(
rpc_server_config.general.near_rpc_url.clone(),
rpc_server_config.general.near_archival_rpc_url.clone(),
Expand Down Expand Up @@ -165,14 +176,6 @@ impl ServerContext {
)
.await?;

// For compiled contract code cache we use 1.5GB. make it configurable. temporary hardcoded
let compiled_contract_code_cache_size_in_bytes =
crate::utils::gigabytes_to_bytes(1.5).await;

let compiled_contract_code_cache = std::sync::Arc::new(CompiledCodeCache::new(
compiled_contract_code_cache_size_in_bytes,
));

crate::metrics::CARGO_PKG_VERSION
.with_label_values(&[NEARD_VERSION])
.inc();
Expand Down

0 comments on commit de297bc

Please sign in to comment.