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

Adds logging of column family size and database size on startup and s… #8336

Merged
merged 16 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 87796acaee1f1592fdf835469006899f9dd41c3db5ba448e6ddba4b51f699ffc # shrinks to total_number_of_peers = 2
14 changes: 14 additions & 0 deletions zebra-state/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ impl Drop for StateService {
"dropping the state: dropped unused non-finalized state queue block",
);

// Log database metrics before shutting down
info!("dropping the state: logging database metrics");
self.log_db_metrics();

// Then drop self.read_service, which checks the block write task for panics,
// and tries to shut down the database.
}
Expand Down Expand Up @@ -451,6 +455,11 @@ impl StateService {
(state, read_service, latest_chain_tip, chain_tip_change)
}

/// Call read only state service to log rocksdb database metrics.
pub fn log_db_metrics(&self) {
self.read_service.db.print_db_metrics();
}

/// Queue a checkpoint verified block for verification and storage in the finalized state.
///
/// Returns a channel receiver that provides the result of the block commit.
Expand Down Expand Up @@ -853,6 +862,11 @@ impl ReadStateService {
pub fn db(&self) -> &ZebraDb {
&self.db
}

/// Logs rocksdb metrics using the read only state service.
pub fn log_db_metrics(&self) {
self.db.print_db_metrics();
}
}

impl Service<Request> for StateService {
Expand Down
80 changes: 79 additions & 1 deletion zebra-state/src/service/finalized_state/disk_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use std::{
collections::{BTreeMap, HashMap},
fmt::Debug,
fmt::Write,
ops::RangeBounds,
path::Path,
sync::Arc,
Expand All @@ -21,7 +22,7 @@ use std::{
use itertools::Itertools;
use rlimit::increase_nofile_limit;

use rocksdb::ReadOptions;
use rocksdb::{ColumnFamilyDescriptor, Options, ReadOptions};
use semver::Version;
use zebra_chain::{parameters::Network, primitives::byte_array::increment_big_endian};

Expand Down Expand Up @@ -512,6 +513,57 @@ impl DiskWriteBatch {
}

impl DiskDb {
/// Prints rocksdb metrics for each column family along with total database disk size, live data disk size and database memory size.
pub fn print_db_metrics(&self) {
let mut total_size_on_disk = 0;
let mut total_live_size_on_disk = 0;
let mut total_size_in_mem = 0;
let db: &Arc<DB> = &self.db;
let db_options = DiskDb::options();
let column_families = DiskDb::construct_column_families(&db_options, db.path(), &[]);
let mut column_families_log_string = String::from("");
write!(column_families_log_string, "Column families and sizes: ").unwrap();
for cf_descriptor in column_families.iter() {
let cf_name = &cf_descriptor.name();
let cf_handle = db
.cf_handle(cf_name)
.expect("Column family handle must exist");
let live_data_size = db
.property_int_value_cf(cf_handle, "rocksdb.estimate-live-data-size")
.unwrap_or(Some(0));
let total_sst_files_size = db
.property_int_value_cf(cf_handle, "rocksdb.total-sst-files-size")
.unwrap_or(Some(0));
let cf_disk_size = live_data_size.unwrap_or(0) + total_sst_files_size.unwrap_or(0);
total_size_on_disk += cf_disk_size;
total_live_size_on_disk += live_data_size.unwrap_or(0);
let mem_table_size = db
.property_int_value_cf(cf_handle, "rocksdb.size-all-mem-tables")
.unwrap_or(Some(0));
total_size_in_mem += mem_table_size.unwrap_or(0);

// TODO: Consider displaying the disk and memory sizes in a human-readable format (e.g., MiB, GiB)
// This might require adding a dependency like the `human_bytes` crate (https://crates.io/crates/human_bytes)
// See https://github.com/ZcashFoundation/zebra/pull/8336#discussion_r1520535787
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
write!(
column_families_log_string,
"{} (Disk: {} bytes, Memory: {} bytes)",
cf_name,
cf_disk_size,
mem_table_size.unwrap_or(0)
)
.unwrap();
}

debug!("{}", column_families_log_string);
info!("Total Database Disk Size: {} bytes", total_size_on_disk);
info!(
"Total Live Data Disk Size: {} bytes",
total_live_size_on_disk
);
info!("Total Database Memory Size: {} bytes", total_size_in_mem);
}

/// Returns a forward iterator over the items in `cf` in `range`.
///
/// Holding this iterator open might delay block commit transactions.
Expand Down Expand Up @@ -720,6 +772,32 @@ impl DiskDb {
/// <https://github.com/facebook/rocksdb/wiki/RocksDB-FAQ#configuration-and-tuning>
const MEMTABLE_RAM_CACHE_MEGABYTES: usize = 128;

/// Build a vector of current column families on the disk and optionally any new column families.
/// Returns an iterable collection of all column families.
fn construct_column_families(
db_options: &Options,
path: &Path,
column_families_in_code: &[String],
) -> Vec<ColumnFamilyDescriptor> {
// When opening the database in read/write mode, all column families must be opened.
//
// To make Zebra forward-compatible with databases updated by later versions,
// we read any existing column families off the disk, then add any new column families
// from the current implementation.
//
// <https://github.com/facebook/rocksdb/wiki/Column-Families#reference
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
let column_families_on_disk = DB::list_cf(db_options, path).unwrap_or_default();
let column_families = column_families_on_disk
.into_iter()
.chain(column_families_in_code.iter().cloned())
.unique()
.collect::<Vec<_>>();
column_families
.into_iter()
.map(|cf_name| ColumnFamilyDescriptor::new(cf_name, db_options.clone()))
.collect()
}

/// Opens or creates the database at a path based on the kind, major version and network,
/// with the supplied column families, preserving any existing column families,
/// and returns a shared low-level database wrapper.
Expand Down
8 changes: 8 additions & 0 deletions zebra-state/src/service/finalized_state/zebra_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ impl ZebraDb {

Ok(())
}

/// Logs metrics related to the underlying RocksDB instance.
///
/// This function prints various metrics and statistics about the RocksDB database,
/// such as disk usage, memory usage, and other performance-related metrics.
pub fn print_db_metrics(&self) {
self.db.print_db_metrics();
}
}

impl Drop for ZebraDb {
Expand Down
3 changes: 3 additions & 0 deletions zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ impl StartCmd {
)
.await?;

info!("logging database metrics on startup");
read_only_state_service.log_db_metrics();

let state = ServiceBuilder::new()
.buffer(Self::state_buffer_bound())
.service(state_service);
Expand Down
Loading