Skip to content

Commit d249d9c

Browse files
perf(505): Track the number of rows in each table
Closes #505.
1 parent 0a4c2f1 commit d249d9c

File tree

4 files changed

+94
-19
lines changed

4 files changed

+94
-19
lines changed

crates/core/src/db/commit_log.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use spacetimedb_lib::{
2323
DataKey,
2424
};
2525

26-
use std::io;
2726
use std::sync::Arc;
2827
use std::sync::{Mutex, MutexGuard};
28+
use std::{collections::HashMap, io};
2929

3030
#[derive(Clone)]
3131
pub struct CommitLog {
@@ -111,33 +111,38 @@ 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.into())
127+
.inc();
128+
// Increment table rows gauge
129+
DB_METRICS
130+
.rdb_num_table_rows
131+
.with_label_values(db, &table_id.into())
132+
.inc();
130133
Operation::Insert
131134
}
132135
TxOp::Delete => {
133136
// 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();
137+
DB_METRICS
138+
.rdb_num_rows_deleted
139+
.with_label_values(txn_type, db, reducer, &table_id.into())
140+
.inc();
141+
// Decrement table rows gauge
142+
DB_METRICS
143+
.rdb_num_table_rows
144+
.with_label_values(db, &table_id.into())
145+
.dec();
141146
Operation::Delete
142147
}
143148
};

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ impl Inner {
380380
let data_key = row.to_data_key();
381381
st_tables.rows.insert(RowId(data_key), row);
382382

383+
// Increment row count for st_tables
384+
DB_METRICS
385+
.rdb_num_table_rows
386+
.with_label_values(&self.database_address, &ST_TABLES_ID.into())
387+
.inc();
388+
383389
// Insert the columns into st_columns
384390
let first_col_id = schema.columns.first().unwrap().col_id;
385391
for (i, col) in schema.columns.into_iter().enumerate() {
@@ -399,6 +405,11 @@ impl Inner {
399405
self.committed_state
400406
.get_or_create_table(ST_COLUMNS_ID, &ST_COLUMNS_ROW_TYPE, &st_columns_schema());
401407
st_columns.rows.insert(RowId(data_key), row);
408+
// Increment row count for st_columns
409+
DB_METRICS
410+
.rdb_num_table_rows
411+
.with_label_values(&self.database_address, &ST_COLUMNS_ID.into())
412+
.inc();
402413
}
403414

404415
// If any columns are auto incrementing, we need to create a sequence
@@ -435,6 +446,11 @@ impl Inner {
435446
let row = ProductValue::from(row);
436447
let data_key = row.to_data_key();
437448
st_sequences.rows.insert(RowId(data_key), row);
449+
// Increment row count for st_sequences
450+
DB_METRICS
451+
.rdb_num_table_rows
452+
.with_label_values(&self.database_address, &ST_SEQUENCES_ID.into())
453+
.inc();
438454
}
439455
}
440456

@@ -463,6 +479,12 @@ impl Inner {
463479
let data_key = row.to_data_key();
464480
st_constraints.rows.insert(RowId(data_key), row);
465481

482+
// Increment row count for st_constraints
483+
DB_METRICS
484+
.rdb_num_table_rows
485+
.with_label_values(&self.database_address, &ST_CONSTRAINTS_ID.into())
486+
.inc();
487+
466488
//Check if add an index:
467489
match constraint.kind {
468490
x if x.is_unique() => IndexSchema {
@@ -498,6 +520,12 @@ impl Inner {
498520
let row = ProductValue::from(row);
499521
let data_key = row.to_data_key();
500522
st_indexes.rows.insert(RowId(data_key), row);
523+
524+
// Increment row count for st_indexes
525+
DB_METRICS
526+
.rdb_num_table_rows
527+
.with_label_values(&self.database_address, &ST_INDEXES_ID.into())
528+
.inc();
501529
}
502530

503531
Ok(())
@@ -1569,10 +1597,34 @@ impl Locking {
15691597

15701598
// Create the system tables and insert information about themselves into
15711599
// st_table, st_columns, st_indexes, and st_sequences.
1600+
DB_METRICS
1601+
.rdb_num_table_rows
1602+
.with_label_values(&database_address, &st_table_schema().table_id.into())
1603+
.set(0);
15721604
datastore.bootstrap_system_table(st_table_schema())?;
1605+
1606+
DB_METRICS
1607+
.rdb_num_table_rows
1608+
.with_label_values(&database_address, &st_columns_schema().table_id.into())
1609+
.set(0);
15731610
datastore.bootstrap_system_table(st_columns_schema())?;
1611+
1612+
DB_METRICS
1613+
.rdb_num_table_rows
1614+
.with_label_values(&database_address, &st_constraints_schema().table_id.into())
1615+
.set(0);
15741616
datastore.bootstrap_system_table(st_constraints_schema())?;
1617+
1618+
DB_METRICS
1619+
.rdb_num_table_rows
1620+
.with_label_values(&database_address, &st_indexes_schema().table_id.into())
1621+
.set(0);
15751622
datastore.bootstrap_system_table(st_indexes_schema())?;
1623+
1624+
DB_METRICS
1625+
.rdb_num_table_rows
1626+
.with_label_values(&database_address, &st_sequences_schema().table_id.into())
1627+
.set(0);
15761628
datastore.bootstrap_system_table(st_sequences_schema())?;
15771629
// TODO(kim): We need to make sure to have ST_MODULE in the committed
15781630
// state. `bootstrap_system_table` initializes the others lazily, but
@@ -1640,6 +1692,10 @@ impl Locking {
16401692
match write.operation {
16411693
Operation::Delete => {
16421694
Self::table_rows(&mut inner, table_id, schema, row_type).remove(&RowId(write.data_key));
1695+
DB_METRICS
1696+
.rdb_num_table_rows
1697+
.with_label_values(&inner.database_address, &table_id.into())
1698+
.dec();
16431699
}
16441700
Operation::Insert => {
16451701
let product_value = match write.data_key {
@@ -1657,6 +1713,10 @@ impl Locking {
16571713
};
16581714
Self::table_rows(&mut inner, table_id, schema, row_type)
16591715
.insert(RowId(write.data_key), product_value);
1716+
DB_METRICS
1717+
.rdb_num_table_rows
1718+
.with_label_values(&inner.database_address, &table_id.into())
1719+
.inc();
16601720
}
16611721
}
16621722
}

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)