Skip to content

Commit a16bbf8

Browse files
fix: Record durations for aborted and read transactions (#850)
1 parent 29f3c89 commit a16bbf8

File tree

2 files changed

+48
-87
lines changed

2 files changed

+48
-87
lines changed

crates/core/src/db/datastore/locking_tx_datastore/datastore.rs

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ use spacetimedb_primitives::{ColList, ConstraintId, IndexId, SequenceId, TableId
2626
use spacetimedb_sats::db::def::{IndexDef, SequenceDef, TableDef, TableSchema};
2727
use spacetimedb_sats::{hash::Hash, AlgebraicValue, DataKey, ProductType, ProductValue};
2828
use spacetimedb_table::{indexes::RowPointer, table::RowRef};
29-
use std::borrow::Cow;
3029
use std::ops::RangeBounds;
3130
use std::sync::Arc;
3231
use std::time::Instant;
32+
use std::{borrow::Cow, time::Duration};
3333

3434
pub type Result<T> = std::result::Result<T, DBError>;
3535

@@ -474,6 +474,48 @@ impl MutTxDatastore for Locking {
474474
}
475475
}
476476

477+
#[cfg(feature = "metrics")]
478+
pub(crate) fn record_metrics(ctx: &ExecutionContext, tx_timer: Instant, lock_wait_time: Duration, committed: bool) {
479+
let workload = &ctx.workload();
480+
let db = &ctx.database();
481+
let reducer = ctx.reducer_name();
482+
let elapsed_time = tx_timer.elapsed();
483+
let cpu_time = elapsed_time - lock_wait_time;
484+
485+
let elapsed_time = elapsed_time.as_secs_f64();
486+
let cpu_time = cpu_time.as_secs_f64();
487+
// Note, we record empty transactions in our metrics.
488+
// That is, transactions that don't write any rows to the commit log.
489+
DB_METRICS
490+
.rdb_num_txns
491+
.with_label_values(workload, db, reducer, &committed)
492+
.inc();
493+
DB_METRICS
494+
.rdb_txn_cpu_time_sec
495+
.with_label_values(workload, db, reducer)
496+
.observe(cpu_time);
497+
DB_METRICS
498+
.rdb_txn_elapsed_time_sec
499+
.with_label_values(workload, db, reducer)
500+
.observe(elapsed_time);
501+
502+
let mut guard = MAX_TX_CPU_TIME.lock().unwrap();
503+
let max_cpu_time = *guard
504+
.entry((*db, *workload, reducer.to_owned()))
505+
.and_modify(|max| {
506+
if cpu_time > *max {
507+
*max = cpu_time;
508+
}
509+
})
510+
.or_insert_with(|| cpu_time);
511+
512+
drop(guard);
513+
DB_METRICS
514+
.rdb_txn_cpu_time_sec_max
515+
.with_label_values(workload, db, reducer)
516+
.set(max_cpu_time);
517+
}
518+
477519
impl MutTx for Locking {
478520
type MutTx = MutTxId;
479521

@@ -493,74 +535,12 @@ impl MutTx for Locking {
493535
}
494536

495537
fn rollback_mut_tx(&self, ctx: &ExecutionContext, tx: Self::MutTx) {
496-
let workload = &ctx.workload();
497-
let db = &ctx.database();
498-
let reducer = ctx.reducer_name();
499-
let elapsed_time = tx.timer.elapsed();
500-
let cpu_time = elapsed_time - tx.lock_wait_time;
501-
#[cfg(feature = "metrics")]
502-
DB_METRICS
503-
.rdb_num_txns
504-
.with_label_values(workload, db, reducer, &false)
505-
.inc();
506-
507-
#[cfg(feature = "metrics")]
508-
DB_METRICS
509-
.rdb_txn_cpu_time_sec
510-
.with_label_values(workload, db, reducer)
511-
.observe(cpu_time.as_secs_f64());
512-
513-
#[cfg(feature = "metrics")]
514-
DB_METRICS
515-
.rdb_txn_elapsed_time_sec
516-
.with_label_values(workload, db, reducer)
517-
.observe(elapsed_time.as_secs_f64());
538+
record_metrics(ctx, tx.timer, tx.lock_wait_time, false);
518539
tx.rollback();
519540
}
520541

521542
fn commit_mut_tx(&self, ctx: &ExecutionContext, tx: Self::MutTx) -> Result<Option<TxData>> {
522-
#[cfg(feature = "metrics")]
523-
{
524-
let workload = &ctx.workload();
525-
let db = &ctx.database();
526-
let reducer = ctx.reducer_name();
527-
let elapsed_time = tx.timer.elapsed();
528-
let cpu_time = elapsed_time - tx.lock_wait_time;
529-
530-
let elapsed_time = elapsed_time.as_secs_f64();
531-
let cpu_time = cpu_time.as_secs_f64();
532-
// Note, we record empty transactions in our metrics.
533-
// That is, transactions that don't write any rows to the commit log.
534-
DB_METRICS
535-
.rdb_num_txns
536-
.with_label_values(workload, db, reducer, &true)
537-
.inc();
538-
DB_METRICS
539-
.rdb_txn_cpu_time_sec
540-
.with_label_values(workload, db, reducer)
541-
.observe(cpu_time);
542-
DB_METRICS
543-
.rdb_txn_elapsed_time_sec
544-
.with_label_values(workload, db, reducer)
545-
.observe(elapsed_time);
546-
547-
let mut guard = MAX_TX_CPU_TIME.lock().unwrap();
548-
let max_cpu_time = *guard
549-
.entry((*db, *workload, reducer.to_owned()))
550-
.and_modify(|max| {
551-
if cpu_time > *max {
552-
*max = cpu_time;
553-
}
554-
})
555-
.or_insert_with(|| cpu_time);
556-
557-
drop(guard);
558-
DB_METRICS
559-
.rdb_txn_cpu_time_sec_max
560-
.with_label_values(workload, db, reducer)
561-
.set(max_cpu_time);
562-
}
563-
543+
record_metrics(ctx, tx.timer, tx.lock_wait_time, true);
564544
Ok(Some(tx.commit()))
565545
}
566546

crates/core/src/db/datastore/locking_tx_datastore/tx.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::{
22
committed_state::{CommittedIndexIter, CommittedState},
3-
datastore::Result,
3+
datastore::{record_metrics, Result},
44
state_view::{Iter, IterByColRange, StateView},
55
SharedReadGuard,
66
};
7-
use crate::{db::db_metrics::DB_METRICS, execution_context::ExecutionContext};
7+
use crate::execution_context::ExecutionContext;
88
use spacetimedb_primitives::{ColList, TableId};
99
use spacetimedb_sats::{db::def::TableSchema, AlgebraicValue};
1010
use std::{
@@ -58,25 +58,6 @@ impl StateView for TxId {
5858

5959
impl TxId {
6060
pub(crate) fn release(self, ctx: &ExecutionContext) {
61-
#[cfg(feature = "metrics")]
62-
{
63-
let workload = &ctx.workload();
64-
let db = &ctx.database();
65-
let reducer = ctx.reducer_name();
66-
let elapsed_time = self.timer.elapsed();
67-
let cpu_time = elapsed_time - self.lock_wait_time;
68-
DB_METRICS
69-
.rdb_num_txns
70-
.with_label_values(workload, db, reducer, &false)
71-
.inc();
72-
DB_METRICS
73-
.rdb_txn_cpu_time_sec
74-
.with_label_values(workload, db, reducer)
75-
.observe(cpu_time.as_secs_f64());
76-
DB_METRICS
77-
.rdb_txn_elapsed_time_sec
78-
.with_label_values(workload, db, reducer)
79-
.observe(elapsed_time.as_secs_f64());
80-
}
61+
record_metrics(ctx, self.timer, self.lock_wait_time, true);
8162
}
8263
}

0 commit comments

Comments
 (0)