Skip to content

Commit

Permalink
add missing flush and update_metrics implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Sep 11, 2024
1 parent 7933f0c commit 8cfdae1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 29 deletions.
37 changes: 35 additions & 2 deletions src/db/lmdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KC, DC>(
gauge: &crate::metrics::Gauge,
rtxn: &RoTxn,
db: &heed::Database<KC, DC>,
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");
}
}

Expand Down
56 changes: 43 additions & 13 deletions src/db/redb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<K: redb::Key, V: redb::Value>(
gauge: &crate::metrics::Gauge,
read_txn: &redb::ReadTransaction,
table_definition: redb::TableDefinition<K, V>,
) {
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);
}
}

Expand Down
33 changes: 19 additions & 14 deletions src/db/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl<const N: usize> Iterator for RowKeyIter<N> {
}

pub struct DBStore {
db: sled::Db,
config: sled::Tree,
headers: sled::Tree,
funding: sled::Tree,
Expand Down Expand Up @@ -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,
);
}
}

Expand All @@ -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,
})
}

Expand Down

0 comments on commit 8cfdae1

Please sign in to comment.