Skip to content

Commit 2c61272

Browse files
joshua-spacetimekulakowski
authored andcommitted
perf(505): Track the number of rows in each table (#525)
Closes #505.
1 parent 04349c1 commit 2c61272

File tree

4 files changed

+69
-18
lines changed

4 files changed

+69
-18
lines changed

crates/core/src/db/commit_log.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,33 +111,32 @@ impl CommitLog {
111111
let mut unwritten_commit = self.unwritten_commit.lock().unwrap();
112112
let mut writes = Vec::with_capacity(tx_data.records.len());
113113

114-
let rows_inserted = &DB_METRICS.rdb_num_rows_inserted;
115-
let rows_deleted = &DB_METRICS.rdb_num_rows_deleted;
114+
let txn_type = &ctx.txn_type();
115+
let db = &ctx.database();
116+
let reducer = &ctx.reducer_name().unwrap_or_default();
116117

117118
for record in &tx_data.records {
118119
let table_id: u32 = record.table_id.into();
119120

120121
let operation = match record.op {
121122
TxOp::Insert(_) => {
122123
// Increment rows inserted metric
123-
let metric = rows_inserted.with_label_values(
124-
&ctx.txn_type(),
125-
&ctx.database(),
126-
ctx.reducer_name().unwrap_or_default(),
127-
&table_id,
128-
);
129-
metric.inc();
124+
DB_METRICS
125+
.rdb_num_rows_inserted
126+
.with_label_values(txn_type, db, reducer, &table_id)
127+
.inc();
128+
// Increment table rows gauge
129+
DB_METRICS.rdb_num_table_rows.with_label_values(db, &table_id).inc();
130130
Operation::Insert
131131
}
132132
TxOp::Delete => {
133133
// Increment rows deleted metric
134-
let metric = rows_deleted.with_label_values(
135-
&ctx.txn_type(),
136-
&ctx.database(),
137-
ctx.reducer_name().unwrap_or_default(),
138-
&table_id,
139-
);
140-
metric.inc();
134+
DB_METRICS
135+
.rdb_num_rows_deleted
136+
.with_label_values(txn_type, db, reducer, &table_id)
137+
.inc();
138+
// Decrement table rows gauge
139+
DB_METRICS.rdb_num_table_rows.with_label_values(db, &table_id).dec();
141140
Operation::Delete
142141
}
143142
};

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ impl Inner {
364364
}
365365

366366
fn bootstrap_system_table(&mut self, schema: TableSchema) -> Result<(), DBError> {
367+
// Reset the row count metric for this system table
368+
DB_METRICS
369+
.rdb_num_table_rows
370+
.with_label_values(&self.database_address, &schema.table_id.into())
371+
.set(0);
372+
367373
let table_id = schema.table_id;
368374

369375
// Insert the table row into st_tables, creating st_tables if it's missing
@@ -380,6 +386,12 @@ impl Inner {
380386
let data_key = row.to_data_key();
381387
st_tables.rows.insert(RowId(data_key), row);
382388

389+
// Increment row count for st_tables
390+
DB_METRICS
391+
.rdb_num_table_rows
392+
.with_label_values(&self.database_address, &ST_TABLES_ID.into())
393+
.inc();
394+
383395
// Insert the columns into st_columns
384396
let first_col_id = schema.columns.first().unwrap().col_id;
385397
for (i, col) in schema.columns.into_iter().enumerate() {
@@ -399,6 +411,11 @@ impl Inner {
399411
self.committed_state
400412
.get_or_create_table(ST_COLUMNS_ID, &ST_COLUMNS_ROW_TYPE, &st_columns_schema());
401413
st_columns.rows.insert(RowId(data_key), row);
414+
// Increment row count for st_columns
415+
DB_METRICS
416+
.rdb_num_table_rows
417+
.with_label_values(&self.database_address, &ST_COLUMNS_ID.into())
418+
.inc();
402419
}
403420

404421
// If any columns are auto incrementing, we need to create a sequence
@@ -435,6 +452,11 @@ impl Inner {
435452
let row = ProductValue::from(row);
436453
let data_key = row.to_data_key();
437454
st_sequences.rows.insert(RowId(data_key), row);
455+
// Increment row count for st_sequences
456+
DB_METRICS
457+
.rdb_num_table_rows
458+
.with_label_values(&self.database_address, &ST_SEQUENCES_ID.into())
459+
.inc();
438460
}
439461
}
440462

@@ -463,6 +485,12 @@ impl Inner {
463485
let data_key = row.to_data_key();
464486
st_constraints.rows.insert(RowId(data_key), row);
465487

488+
// Increment row count for st_constraints
489+
DB_METRICS
490+
.rdb_num_table_rows
491+
.with_label_values(&self.database_address, &ST_CONSTRAINTS_ID.into())
492+
.inc();
493+
466494
//Check if add an index:
467495
match constraint.kind {
468496
x if x.is_unique() => IndexSchema {
@@ -498,6 +526,12 @@ impl Inner {
498526
let row = ProductValue::from(row);
499527
let data_key = row.to_data_key();
500528
st_indexes.rows.insert(RowId(data_key), row);
529+
530+
// Increment row count for st_indexes
531+
DB_METRICS
532+
.rdb_num_table_rows
533+
.with_label_values(&self.database_address, &ST_INDEXES_ID.into())
534+
.inc();
501535
}
502536

503537
Ok(())
@@ -1640,6 +1674,10 @@ impl Locking {
16401674
match write.operation {
16411675
Operation::Delete => {
16421676
Self::table_rows(&mut inner, table_id, schema, row_type).remove(&RowId(write.data_key));
1677+
DB_METRICS
1678+
.rdb_num_table_rows
1679+
.with_label_values(&inner.database_address, &table_id.into())
1680+
.dec();
16431681
}
16441682
Operation::Insert => {
16451683
let product_value = match write.data_key {
@@ -1657,6 +1695,10 @@ impl Locking {
16571695
};
16581696
Self::table_rows(&mut inner, table_id, schema, row_type)
16591697
.insert(RowId(write.data_key), product_value);
1698+
DB_METRICS
1699+
.rdb_num_table_rows
1700+
.with_label_values(&inner.database_address, &table_id.into())
1701+
.inc();
16601702
}
16611703
}
16621704
}

crates/core/src/db/db_metrics/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{execution_context::TransactionType, host::AbiCall, util::typed_prometheus::metrics_group};
22
use once_cell::sync::Lazy;
3-
use prometheus::{Histogram, HistogramVec, IntCounterVec};
3+
use prometheus::{Histogram, HistogramVec, IntCounterVec, IntGaugeVec};
44
use spacetimedb_lib::Address;
55

66
metrics_group!(
@@ -56,6 +56,11 @@ metrics_group!(
5656
#[labels(db: Address, reducer: str)]
5757
pub scheduled_reducer_delay_sec: HistogramVec,
5858

59+
#[name = spacetime_num_table_rows]
60+
#[help = "The number of rows in a table"]
61+
#[labels(db: Address, table_id: u32)]
62+
pub rdb_num_table_rows: IntGaugeVec,
63+
5964
#[name = spacetime_num_rows_inserted_cumulative]
6065
#[help = "The cumulative number of rows inserted into a table"]
6166
#[labels(txn_type: TransactionType, db: Address, reducer: str, table_id: u32)]

crates/core/src/db/relational_db.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,12 @@ impl RelationalDB {
395395
.rdb_drop_table_time
396396
.with_label_values(&table_id.0)
397397
.start_timer();
398-
self.inner.drop_table_mut_tx(tx, table_id)
398+
self.inner.drop_table_mut_tx(tx, table_id).map(|_| {
399+
DB_METRICS
400+
.rdb_num_table_rows
401+
.with_label_values(&self.address, &table_id.into())
402+
.set(0)
403+
})
399404
}
400405

401406
/// Rename a table.

0 commit comments

Comments
 (0)