From 8cfdae1a163da4f10ba2df339fb6aaa7cb337e36 Mon Sep 17 00:00:00 2001 From: Antoni Spaanderman <56turtle56@gmail.com> Date: Sat, 13 Jul 2024 22:55:21 +0200 Subject: [PATCH] add missing flush and update_metrics implementations --- src/db/lmdb.rs | 37 +++++++++++++++++++++++++++++++-- src/db/redb.rs | 56 ++++++++++++++++++++++++++++++++++++++------------ src/db/sled.rs | 33 ++++++++++++++++------------- 3 files changed, 97 insertions(+), 29 deletions(-) diff --git a/src/db/lmdb.rs b/src/db/lmdb.rs index c7a782e3e..8b8f023ab 100644 --- a/src/db/lmdb.rs +++ b/src/db/lmdb.rs @@ -164,8 +164,41 @@ impl Database for DBStore { self.env.force_sync().unwrap(); } - fn update_metrics(&self, _gauge: &crate::metrics::Gauge) { - // TODO + fn update_metrics(&self, gauge: &crate::metrics::Gauge) { + fn update_table_metrics( + gauge: &crate::metrics::Gauge, + rtxn: &RoTxn, + db: &heed::Database, + db_name: &str, + ) { + let stats = db.stat(rtxn).unwrap(); + + for (name, value) in [ + ("page_size", stats.page_size as f64), + ("depth", stats.depth as f64), + ("branch_pages", stats.branch_pages as f64), + ("leaf_pages", stats.leaf_pages as f64), + ("overflow_pages", stats.overflow_pages as f64), + ("entries", stats.entries as f64), + ] { + gauge.set(&format!("lmdb.{}:{}", name, db_name), value); + } + } + + gauge.set( + "lmdb.size_on_disk", + self.env + .real_disk_size() + .expect("unable to get database size") as f64, + ); + + let rtxn = self.env.read_txn().unwrap(); + + update_table_metrics(gauge, &rtxn, &self.funding, "funding"); + update_table_metrics(gauge, &rtxn, &self.spending, "spending"); + update_table_metrics(gauge, &rtxn, &self.txid, "txid"); + update_table_metrics(gauge, &rtxn, &self.headers, "headers"); + update_table_metrics(gauge, &rtxn, &self.config, "config"); } } diff --git a/src/db/redb.rs b/src/db/redb.rs index 08896985d..717f6e4fe 100644 --- a/src/db/redb.rs +++ b/src/db/redb.rs @@ -3,7 +3,7 @@ use crate::types::{ HashPrefix, SerializedHashPrefixRow, SerializedHeaderRow, HASH_PREFIX_ROW_SIZE, HEADER_ROW_SIZE, }; use anyhow::{Context, Result}; -use redb::TableDefinition; +use redb::{ReadableTableMetadata, TableDefinition, TableHandle}; use std::fs::{create_dir_all, remove_file}; use std::path::Path; @@ -176,20 +176,50 @@ impl Database for DBStore { } fn flush(&self) { - // TODO? + let mut write_txn = self + .db + .begin_write() + .expect("failed to begin write transaction"); + write_txn.set_durability(redb::Durability::Immediate); + write_txn.commit().expect("failed to commit transaction"); } - fn update_metrics(&self, _gauge: &crate::metrics::Gauge) { - // TODO do something with table stats - - // let read_txn = self - // .db - // .begin_read() - // .expect("unable to create read transaction"); - // let table = read_txn - // .open_table(FUNDING_TABLE) - // .expect("unable to open table"); - // let stats = table.stats().expect("unable to get table stats"); + fn update_metrics(&self, gauge: &crate::metrics::Gauge) { + fn update_table_metrics( + gauge: &crate::metrics::Gauge, + read_txn: &redb::ReadTransaction, + table_definition: redb::TableDefinition, + ) { + let table = read_txn + .open_table(table_definition) + .expect("unable to open table"); + + let table_name = table_definition.name(); + + let stats = table.stats().expect("unable to get table stats"); + + for (name, value) in [ + ("tree_height", stats.tree_height() as f64), + ("leaf_pages", stats.leaf_pages() as f64), + ("branch_pages", stats.branch_pages() as f64), + ("stored_leaf_bytes", stats.stored_bytes() as f64), + ("metadata_bytes", stats.metadata_bytes() as f64), + ("fragmented_bytes", stats.fragmented_bytes() as f64), + ] { + gauge.set(&format!("redb.{}:{}", name, table_name), value); + } + } + + let read_txn = self + .db + .begin_read() + .expect("unable to create read transaction"); + + update_table_metrics(gauge, &read_txn, FUNDING_TABLE); + update_table_metrics(gauge, &read_txn, SPENDING_TABLE); + update_table_metrics(gauge, &read_txn, TXID_TABLE); + update_table_metrics(gauge, &read_txn, HEADERS_TABLE); + update_table_metrics(gauge, &read_txn, CONFIG_TABLE); } } diff --git a/src/db/sled.rs b/src/db/sled.rs index 4f22c361c..1c03ab951 100644 --- a/src/db/sled.rs +++ b/src/db/sled.rs @@ -24,6 +24,7 @@ impl Iterator for RowKeyIter { } pub struct DBStore { + db: sled::Db, config: sled::Tree, headers: sled::Tree, funding: sled::Tree, @@ -156,17 +157,14 @@ impl Database for DBStore { } fn flush(&self) { - // TODO - - self.config.flush().expect("config flush failed"); - self.headers.flush().expect("headers flush failed"); - self.funding.flush().expect("funding flush failed"); - self.spending.flush().expect("spending flush failed"); - self.txid.flush().expect("txid flush failed"); + self.db.flush().expect("flush failed"); } - fn update_metrics(&self, _gauge: &crate::metrics::Gauge) { - // TODO + fn update_metrics(&self, gauge: &crate::metrics::Gauge) { + gauge.set( + "sled.size_on_disk", + self.db.size_on_disk().expect("unable to get database size") as f64, + ); } } @@ -175,12 +173,19 @@ impl DBStore { // TODO config let db = sled::Config::new().path(path).open()?; + let config = db.open_tree("config")?; + let headers = db.open_tree("headers")?; + let funding = db.open_tree("funding")?; + let spending = db.open_tree("spending")?; + let txid = db.open_tree("txid")?; + Ok(Self { - config: db.open_tree("config")?, - headers: db.open_tree("headers")?, - funding: db.open_tree("funding")?, - spending: db.open_tree("spending")?, - txid: db.open_tree("txid")?, + db, + config, + headers, + funding, + spending, + txid, }) }