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

fix: account for cells tree size in block cache weigher #373

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 24 additions & 4 deletions block-util/src/block/block_stuff.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::{Arc, OnceLock};
use std::time::Duration;

use anyhow::Result;
use anyhow::{Context, Result};
use bytes::Bytes;
use everscale_types::boc::de::BocHeader;
use everscale_types::models::*;
use everscale_types::prelude::*;
use tycho_util::FastHashMap;
Expand Down Expand Up @@ -70,10 +71,16 @@ impl BlockStuff {
file_hash,
};

Self::from_block_and_root(&block_id, block, root, DATA_SIZE)
Self::from_block_and_root(&block_id, block, root, DATA_SIZE, 0)
}

pub fn from_block_and_root(id: &BlockId, block: Block, root: Cell, data_size: usize) -> Self {
pub fn from_block_and_root(
id: &BlockId,
block: Block,
root: Cell,
data_size: usize,
uniq_cells: usize,
) -> Self {
debug_assert_eq!(&id.root_hash, root.repr_hash());

Self {
Expand All @@ -85,6 +92,7 @@ impl BlockStuff {
block_extra: Default::default(),
block_mc_extra: Default::default(),
data_size,
uniq_cells,
}),
}
}
Expand All @@ -100,7 +108,13 @@ impl BlockStuff {
}

pub fn deserialize(id: &BlockId, data: &[u8]) -> Result<Self> {
let root = Boc::decode(data)?;
let root = BocHeader::decode(data, &Default::default())?;
let uniq_cells = root.cells().len();
let root = root.finalize(&mut Cell::empty_context())?;
let root = root
.get(0)
.context("Invalid blocK: failed to get root cell")?;

anyhow::ensure!(
&id.root_hash == root.repr_hash(),
"root_hash mismatch for {id}"
Expand All @@ -116,6 +130,7 @@ impl BlockStuff {
block_extra: Default::default(),
block_mc_extra: Default::default(),
data_size: data.len(),
uniq_cells,
}),
})
}
Expand All @@ -128,6 +143,10 @@ impl BlockStuff {
self.inner.data_size
}

pub fn num_uniq_cells(&self) -> usize {
self.inner.uniq_cells
}

pub fn with_archive_data<A>(self, data: A) -> WithArchiveData<Self>
where
Bytes: From<A>,
Expand Down Expand Up @@ -286,4 +305,5 @@ pub struct Inner {
block_extra: OnceLock<Result<BlockExtra, everscale_types::error::Error>>,
block_mc_extra: OnceLock<Result<McBlockExtra, everscale_types::error::Error>>,
data_size: usize,
uniq_cells: usize,
}
16 changes: 14 additions & 2 deletions collator/src/collator/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use std::time::Duration;

use anyhow::{anyhow, bail, Result};
use everscale_types::boc::ser::BocHeader;
use everscale_types::merkle::*;
use everscale_types::models::*;
use everscale_types::prelude::*;
Expand Down Expand Up @@ -327,7 +328,12 @@ impl CollatorStdImpl {
// TODO: Check (assert) whether the serialized block contains usage cells
let root = CellBuilder::build_from(&block)?;

let data = everscale_types::boc::Boc::encode_rayon(&root);
let mut data = Vec::with_capacity(50_000_000); // todo: config based on some metrics
let header = BocHeader::<ahash::RandomState>::with_root(root.as_ref());
header.encode_rayon(&mut data);

let num_uniq_cells = header.compute_stats().total_cells_size; // todo: expose root.len() in everscale_types

let block_id = BlockId {
shard: collation_data.block_id_short.shard,
seqno: collation_data.block_id_short.seqno,
Expand All @@ -337,7 +343,13 @@ impl CollatorStdImpl {

build_block_elapsed = histogram.finish();

let block = BlockStuff::from_block_and_root(&block_id, block, root, data.len());
let block = BlockStuff::from_block_and_root(
&block_id,
block,
root,
data.len(),
num_uniq_cells as usize,
);

(
WithArchiveData::new(block, data),
Expand Down
16 changes: 13 additions & 3 deletions storage/src/store/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ impl BlockStorage {
fn weigher(_key: &BlockId, value: &BlockStuff) -> u32 {
const BLOCK_STUFF_OVERHEAD: u32 = 1024; // 1 KB

std::mem::size_of::<BlockId>() as u32
+ BLOCK_STUFF_OVERHEAD
+ value.data_size().try_into().unwrap_or(u32::MAX)
let mut weight = size_of::<BlockId>() as u32;

weight = weight.saturating_add(BLOCK_STUFF_OVERHEAD);
weight = weight.saturating_add(value.data_size().try_into().unwrap_or(u32::MAX));

// Calculate cells size
let cells_size = value
.num_uniq_cells()
.try_into()
.unwrap_or(u32::MAX)
.saturating_mul(size_of::<everscale_types::cell::Cell>() as u32); // todo: ask @Rexagon about real size

weight.saturating_add(cells_size)
}

let blocks_cache = moka::sync::Cache::builder()
Expand Down
Loading