Skip to content

Commit

Permalink
DRY close transaction metric
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Oct 23, 2023
1 parent bddfd05 commit 89040ca
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
6 changes: 3 additions & 3 deletions crates/storage/db/src/implementation/mdbx/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ impl<'tx, K: TransactionKind, T: Table> Cursor<'tx, K, T> {
self
}

fn execute_with_operation_metric(
fn execute_with_operation_metric<T>(
&mut self,
operation: Operation,
f: impl FnOnce(&mut Self) -> Result<(), DatabaseError>,
) -> Result<(), DatabaseError> {
f: impl FnOnce(&mut Self) -> T,
) -> T {
let start = Instant::now();
let result = f(self);

Expand Down
50 changes: 27 additions & 23 deletions crates/storage/db/src/implementation/mdbx/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,30 @@ impl<'env, K: TransactionKind, E: EnvironmentKind> Tx<'env, K, E> {
))
}

fn execute_with_close_transaction_metric<T>(
mut self,
outcome: TransactionOutcome,
f: impl FnOnce(Self) -> T,
) -> T {
let start = Instant::now();
let (txn_id, metrics_tx) =
self.metrics_tx.take().map(|metrics_tx| (self.id(), metrics_tx)).unzip();
let result = f(self);
if let (Some(txn_id), Some(metrics_tx)) = (txn_id, metrics_tx) {
let _ = metrics_tx.send(MetricEvent::CloseTransaction {
txn_id,
outcome,
commit_duration: start.elapsed(),
});
}
result
}

fn execute_with_operation_metric<T>(
&self,
operation: Operation,
f: impl FnOnce(&Transaction<'_, K, E>) -> Result<T, DatabaseError>,
) -> Result<T, DatabaseError> {
f: impl FnOnce(&Transaction<'_, K, E>) -> T,
) -> T {
let start = Instant::now();
let result = f(&self.inner);
if let Some(metrics_tx) = &self.metrics_tx {
Expand Down Expand Up @@ -111,30 +130,15 @@ impl<K: TransactionKind, E: EnvironmentKind> DbTx for Tx<'_, K, E> {
}

fn commit(mut self) -> Result<bool, DatabaseError> {
let start = Instant::now();
let metrics = self.metrics_tx.take().map(|metrics_tx| (self.id(), metrics_tx));
let result = self.inner.commit().map_err(|e| DatabaseError::Commit(e.into()));
if let Some((txn_id, metrics_tx)) = metrics {
let _ = metrics_tx.send(MetricEvent::CloseTransaction {
txn_id,
outcome: TransactionOutcome::Commit,
commit_duration: start.elapsed(),
});
}
result
self.execute_with_close_transaction_metric(TransactionOutcome::Commit, |this| {
this.inner.commit().map_err(|e| DatabaseError::Commit(e.into()))
})
}

fn drop(mut self) {
let start = Instant::now();
let metrics = self.metrics_tx.take().map(|metrics_tx| (self.id(), metrics_tx));
drop(self.inner);
if let Some((txn_id, metrics_tx)) = metrics {
let _ = metrics_tx.send(MetricEvent::CloseTransaction {
txn_id,
outcome: TransactionOutcome::Abort,
commit_duration: start.elapsed(),
});
}
self.execute_with_close_transaction_metric(TransactionOutcome::Abort, |this| {
drop(this.inner)
})
}

// Iterate over read only values in database.
Expand Down

0 comments on commit 89040ca

Please sign in to comment.