Skip to content

Commit

Permalink
feat(storage): split cells table into data and refs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Nov 25, 2024
1 parent ea9a6a3 commit 407f949
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 381 deletions.
4 changes: 2 additions & 2 deletions storage/src/db/kv_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use weedb::{
Caches, MigrationError, Semver, Tables, VersionProvider, WeeDb, WeeDbBuilder, WeeDbRaw,
};

pub mod refcount;
pub mod tables;

pub trait WeeDbExt<T: Tables>: Sized {
Expand Down Expand Up @@ -60,7 +59,8 @@ weedb::tables! {
pub package_entries: tables::PackageEntries,
pub block_data_entries: tables::BlockDataEntries,
pub shard_states: tables::ShardStates,
pub cells: tables::Cells,
pub cell_data: tables::CellData,
pub cell_refs: tables::CellRefs,
pub temp_cells: tables::TempCells,
pub block_connections: tables::BlockConnections,
pub shards_internal_messages: tables::ShardsInternalMessages,
Expand Down
87 changes: 0 additions & 87 deletions storage/src/db/kv_db/refcount.rs

This file was deleted.

45 changes: 36 additions & 9 deletions storage/src/db/kv_db/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use weedb::rocksdb::{
};
use weedb::{rocksdb, Caches, ColumnFamily, ColumnFamilyOptions};

use super::refcount;

// took from
// https://github.com/tikv/tikv/blob/d60c7fb6f3657dc5f3c83b0e3fc6ac75636e1a48/src/config/mod.rs#L170
// todo: need to benchmark and update if it's not optimal
Expand Down Expand Up @@ -198,21 +196,50 @@ impl ColumnFamilyOptions<Caches> for ShardStates {
}
}

/// Stores cells data
/// Stores cell data
/// - Key: `[u8; 32]` (cell repr hash)
/// - Value: `StorageCell`
pub struct Cells;
pub struct CellData;

impl ColumnFamily for Cells {
const NAME: &'static str = "cells";
impl ColumnFamily for CellData {
const NAME: &'static str = "cell_data";
}

impl ColumnFamilyOptions<Caches> for Cells {
impl ColumnFamilyOptions<Caches> for CellData {
fn options(opts: &mut Options, caches: &mut Caches) {
opts.set_level_compaction_dynamic_level_bytes(true);

opts.set_merge_operator_associative("cell_merge", refcount::merge_operator);
opts.set_compaction_filter("cell_compaction", refcount::compaction_filter);
optimize_for_level_compaction(opts, ByteSize::gib(1u64));

let mut block_factory = BlockBasedOptions::default();
block_factory.set_block_cache(&caches.block_cache);
block_factory.set_data_block_index_type(DataBlockIndexType::BinaryAndHash);
block_factory.set_whole_key_filtering(true);
block_factory.set_checksum_type(rocksdb::ChecksumType::NoChecksum);

block_factory.set_bloom_filter(10.0, false);
block_factory.set_block_size(16 * 1024);
block_factory.set_format_version(5);

opts.set_block_based_table_factory(&block_factory);
opts.set_optimize_filters_for_hits(true);
// option is set for cf
opts.set_compression_type(DBCompressionType::Lz4);
}
}

/// Stores cell refs
/// - Key: `[u8; 32]` (cell repr hash)
/// - Value: u64 (le)
pub struct CellRefs;

impl ColumnFamily for CellRefs {
const NAME: &'static str = "cell_refs";
}

impl ColumnFamilyOptions<Caches> for CellRefs {
fn options(opts: &mut rocksdb::Options, caches: &mut Caches) {
opts.set_level_compaction_dynamic_level_bytes(true);

optimize_for_level_compaction(opts, ByteSize::gib(1u64));

Expand Down
2 changes: 2 additions & 0 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ impl StorageBuilder {
temp_file_storage.clone(),
self.config.cells_cache_size.as_u64(),
)?;
shard_state_storage.preload_cell_refs().await?;

let persistent_state_storage = PersistentStateStorage::new(
base_db.clone(),
&file_db,
Expand Down
19 changes: 5 additions & 14 deletions storage/src/store/persistent_state/shard_state/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ impl<'a> ShardStateWriter<'a> {
let mut file = self.states_dir.unnamed_file().open()?;

let raw = self.db.rocksdb().as_ref();
let read_options = self.db.cells.read_config();
let cf = self.db.cells.cf();
let read_options = self.db.cell_data.read_config();
let cf = self.db.cell_data.cf();

let mut references_buffer = SmallVec::<[[u8; 32]; 4]>::with_capacity(4);

Expand Down Expand Up @@ -256,18 +256,9 @@ impl<'a> ShardStateWriter<'a> {
.get_pinned_cf_opt(&cf, hash, read_options)?
.ok_or(CellWriterError::CellNotFound)?;

let value = match crate::refcount::strip_refcount(value.as_ref()) {
Some(bytes) => bytes,
None => {
return Err(CellWriterError::CellNotFound.into());
}
};
if value.is_empty() {
return Err(CellWriterError::InvalidCell.into());
}

let (descriptor, data) = deserialize_cell(value, &mut references_buffer)
.ok_or(CellWriterError::InvalidCell)?;
let (descriptor, data) =
deserialize_cell(value.as_ref(), &mut references_buffer)
.ok_or(CellWriterError::InvalidCell)?;

let mut reference_indices = SmallVec::with_capacity(references_buffer.len());

Expand Down
Loading

0 comments on commit 407f949

Please # to comment.