Skip to content

Commit

Permalink
introduce specific databasemetrics value type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Dec 14, 2023
1 parent dfbd866 commit 755612d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
8 changes: 4 additions & 4 deletions bin/reth/src/node/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ impl<DB> NodeState<DB> {
}
}

impl<DB: DatabaseMetadata<Metadata = usize>> NodeState<DB> {
impl<DB: DatabaseMetadata> NodeState<DB> {
fn freelist(&self) -> Option<usize> {
self.db.metadata().get("freelist").copied()
self.db.metadata().freelist_size()
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ pub async fn handle_events<E, DB>(
db: DB,
) where
E: Stream<Item = NodeEvent> + Unpin,
DB: DatabaseMetadata<Metadata = usize> + Database + 'static,
DB: DatabaseMetadata + Database + 'static,
{
let state = NodeState::new(db, network, latest_block_number);

Expand All @@ -303,7 +303,7 @@ struct EventHandler<E, DB> {
impl<E, DB> Future for EventHandler<E, DB>
where
E: Stream<Item = NodeEvent> + Unpin,
DB: DatabaseMetadata<Metadata = usize> + Database + 'static,
DB: DatabaseMetadata + Database + 'static,
{
type Output = ();

Expand Down
32 changes: 23 additions & 9 deletions crates/storage/db/src/abstraction/database_metrics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use metrics::{counter, gauge, histogram, Label};
use std::{collections::HashMap, sync::Arc};
use std::sync::Arc;

/// Represents a type that can report metrics, used mainly with the database. The `report_metrics`
/// method can be used as a prometheus hook.
Expand Down Expand Up @@ -41,20 +41,34 @@ impl<DB: DatabaseMetrics> DatabaseMetrics for Arc<DB> {
}
}

/// The type used to store metadata about the database.
#[derive(Debug)]
pub struct DatabaseMetadataValue {
/// The freelist size
freelist_size: Option<usize>,
}

impl DatabaseMetadataValue {
/// Creates a new [DatabaseMetadataValue] with the given freelist size.
pub fn new(freelist_size: Option<usize>) -> Self {
Self { freelist_size }
}

/// Returns the freelist size, if available.
pub fn freelist_size(&self) -> Option<usize> {
self.freelist_size
}
}

/// Extends [Database] to include a [Metadata] type, which can be used by methods which need to
/// dynamically retrieve information about the database.
pub trait DatabaseMetadata {
/// The type used to store metadata about the database.
type Metadata;

/// Returns a [HashMap] of [&'static str]-addressable metadata about the database.
fn metadata(&self) -> HashMap<&'static str, Self::Metadata>;
/// TODO
fn metadata(&self) -> DatabaseMetadataValue;
}

impl<DB: DatabaseMetadata> DatabaseMetadata for Arc<DB> {
type Metadata = DB::Metadata;

fn metadata(&self) -> HashMap<&'static str, Self::Metadata> {
fn metadata(&self) -> DatabaseMetadataValue {
<DB as DatabaseMetadata>::metadata(self)
}
}
17 changes: 5 additions & 12 deletions crates/storage/db/src/implementation/mdbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
database::Database,
database_metrics::{DatabaseMetadata, DatabaseMetrics},
database_metrics::{DatabaseMetadata, DatabaseMetadataValue, DatabaseMetrics},
tables::{TableType, Tables},
utils::default_page_size,
DatabaseError,
Expand All @@ -14,7 +14,7 @@ use reth_libmdbx::{
DatabaseFlags, Environment, EnvironmentFlags, Geometry, Mode, PageSize, SyncMode, RO, RW,
};
use reth_tracing::tracing::error;
use std::{collections::HashMap, ops::Deref, path::Path};
use std::{ops::Deref, path::Path};
use tx::Tx;

pub mod cursor;
Expand Down Expand Up @@ -133,15 +133,8 @@ impl DatabaseMetrics for DatabaseEnv {
}

impl DatabaseMetadata for DatabaseEnv {
type Metadata = usize;

fn metadata(&self) -> HashMap<&'static str, Self::Metadata> {
let mut metadata = HashMap::new();

if let Ok(freelist) = self.freelist() {
metadata.insert("freelist", freelist);
}
metadata
fn metadata(&self) -> DatabaseMetadataValue {
DatabaseMetadataValue::new(self.freelist().ok())
}
}

Expand Down Expand Up @@ -234,7 +227,7 @@ impl DatabaseEnv {
LogLevel::Extra => 7,
});
} else {
return Err(DatabaseError::LogLevelUnavailable(log_level))
return Err(DatabaseError::LogLevelUnavailable(log_level));
}
}

Expand Down
8 changes: 3 additions & 5 deletions crates/storage/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ pub mod test_utils {
use super::*;
use crate::{
database::Database,
database_metrics::{DatabaseMetadata, DatabaseMetrics},
database_metrics::{DatabaseMetadata, DatabaseMetadataValue, DatabaseMetrics},
};
use std::{collections::HashMap, path::PathBuf, sync::Arc};
use std::{path::PathBuf, sync::Arc};

/// Error during database open
pub const ERROR_DB_OPEN: &str = "Not able to open the database file.";
Expand Down Expand Up @@ -220,9 +220,7 @@ pub mod test_utils {
}

impl<DB: DatabaseMetadata> DatabaseMetadata for TempDatabase<DB> {
type Metadata = <DB as DatabaseMetadata>::Metadata;

fn metadata(&self) -> HashMap<&'static str, Self::Metadata> {
fn metadata(&self) -> DatabaseMetadataValue {
self.db().metadata()
}
}
Expand Down

0 comments on commit 755612d

Please sign in to comment.