Skip to content

Commit ae420bf

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

File tree

4 files changed

+102
-21
lines changed

4 files changed

+102
-21
lines changed

crates/core/src/db/commit_log.rs

Lines changed: 30 additions & 19 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,19 @@ 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 mut num_inserts = HashMap::new();
115+
let mut num_deletes = HashMap::new();
116116

117117
for record in &tx_data.records {
118118
let table_id: u32 = record.table_id.into();
119119

120120
let operation = match record.op {
121121
TxOp::Insert(_) => {
122-
// 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();
122+
num_inserts.entry(table_id).and_modify(|n| *n += 1).or_insert(1);
130123
Operation::Insert
131124
}
132125
TxOp::Delete => {
133-
// 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();
126+
num_deletes.entry(table_id).and_modify(|n| *n += 1).or_insert(1);
141127
Operation::Delete
142128
}
143129
};
@@ -149,6 +135,31 @@ impl CommitLog {
149135
})
150136
}
151137

138+
let txn_type = &ctx.txn_type();
139+
let db = &ctx.database();
140+
let reducer = &ctx.reducer_name().unwrap_or_default();
141+
142+
for (ref table_id, n) in num_inserts {
143+
DB_METRICS
144+
.rdb_num_rows_inserted
145+
.with_label_values(txn_type, db, reducer, table_id)
146+
.inc_by(n);
147+
DB_METRICS
148+
.rdb_num_table_rows
149+
.with_label_values(db, table_id)
150+
.add(n as i64);
151+
}
152+
for (ref table_id, n) in num_deletes {
153+
DB_METRICS
154+
.rdb_num_rows_deleted
155+
.with_label_values(txn_type, db, reducer, table_id)
156+
.inc_by(n);
157+
DB_METRICS
158+
.rdb_num_table_rows
159+
.with_label_values(db, table_id)
160+
.sub(n as i64);
161+
}
162+
152163
let transaction = Transaction { writes };
153164
unwritten_commit.transactions.push(Arc::new(transaction));
154165

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+
.add(1);
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+
.add(1);
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+
.add(1);
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+
.add(1);
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+
.add(1);
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+
.sub(1);
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+
.add(1);
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)