Skip to content

Commit 7623404

Browse files
perf(472): Add metric for index seeks and keys scanned (#501)
Closes #472.
1 parent d1cbef2 commit 7623404

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,27 @@ impl Iterator for BTreeIndexIter<'_> {
6262
/// [BTreeIndex]
6363
pub struct BTreeIndexRangeIter<'a> {
6464
range_iter: btree_set::Range<'a, IndexKey>,
65+
num_keys_scanned: u64,
6566
}
6667

6768
impl<'a> Iterator for BTreeIndexRangeIter<'a> {
6869
type Item = &'a RowId;
6970

7071
#[tracing::instrument(skip_all)]
7172
fn next(&mut self) -> Option<Self::Item> {
72-
self.range_iter.next().map(|key| &key.row_id)
73+
if let Some(key) = self.range_iter.next() {
74+
self.num_keys_scanned += 1;
75+
Some(&key.row_id)
76+
} else {
77+
None
78+
}
79+
}
80+
}
81+
82+
impl BTreeIndexRangeIter<'_> {
83+
/// Returns the current number of keys the iterator has scanned.
84+
pub fn keys_scanned(&self) -> u64 {
85+
self.num_keys_scanned
7386
}
7487
}
7588

@@ -161,6 +174,7 @@ impl BTreeIndex {
161174
let end = map(range.end_bound(), DataKey::max_datakey());
162175
BTreeIndexRangeIter {
163176
range_iter: self.idx.range((start, end)),
177+
num_keys_scanned: 0,
164178
}
165179
}
166180

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

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,11 +1495,13 @@ impl Inner {
14951495
// The current transaction has modified this table, and the table is indexed.
14961496
let tx_state = self.tx_state.as_ref().unwrap();
14971497
Ok(IterByColRange::Index(IndexSeekIterInner {
1498+
ctx,
14981499
table_id: *table_id,
14991500
tx_state,
15001501
inserted_rows,
15011502
committed_rows: self.committed_state.index_seek(table_id, &cols, &range),
15021503
committed_state: &self.committed_state,
1504+
num_committed_rows_fetched: 0,
15031505
}))
15041506
} else {
15051507
// Either the current transaction has not modified this table, or the table is not
@@ -1513,10 +1515,12 @@ impl Inner {
15131515
scan_iter: self.iter(ctx, table_id)?,
15141516
})),
15151517
Some(tx_state) => Ok(IterByColRange::CommittedIndex(CommittedIndexIter {
1518+
ctx,
15161519
table_id: *table_id,
15171520
tx_state,
15181521
committed_state: &self.committed_state,
15191522
committed_rows,
1523+
num_committed_rows_fetched: 0,
15201524
})),
15211525
},
15221526
None => Ok(IterByColRange::Scan(ScanIterByColRange {
@@ -1689,7 +1693,7 @@ pub struct Iter<'a> {
16891693
table_id: TableId,
16901694
inner: &'a Inner,
16911695
stage: ScanStage<'a>,
1692-
committed_rows_fetched: u64,
1696+
num_committed_rows_fetched: u64,
16931697
}
16941698

16951699
impl Drop for Iter<'_> {
@@ -1702,7 +1706,7 @@ impl Drop for Iter<'_> {
17021706
self.ctx.reducer_name().unwrap_or_default(),
17031707
&self.table_id.into(),
17041708
)
1705-
.inc_by(self.committed_rows_fetched);
1709+
.inc_by(self.num_committed_rows_fetched);
17061710
}
17071711
}
17081712

@@ -1713,7 +1717,7 @@ impl<'a> Iter<'a> {
17131717
table_id,
17141718
inner,
17151719
stage: ScanStage::Start,
1716-
committed_rows_fetched: 0,
1720+
num_committed_rows_fetched: 0,
17171721
}
17181722
}
17191723
}
@@ -1771,7 +1775,7 @@ impl<'a> Iterator for Iter<'a> {
17711775
let _span = tracing::debug_span!("ScanStage::Committed").entered();
17721776
for (row_id, row) in iter {
17731777
// Increment metric for number of committed rows scanned.
1774-
self.committed_rows_fetched += 1;
1778+
self.num_committed_rows_fetched += 1;
17751779
// Check the committed row's state in the current tx.
17761780
match self.inner.tx_state.as_ref().map(|tx_state| tx_state.get_row_op(&table_id, row_id)) {
17771781
Some(RowState::Committed(_)) => unreachable!("a row cannot be committed in a tx state"),
@@ -1802,11 +1806,50 @@ impl<'a> Iterator for Iter<'a> {
18021806
}
18031807

18041808
pub struct IndexSeekIterInner<'a> {
1809+
ctx: &'a ExecutionContext<'a>,
18051810
table_id: TableId,
18061811
tx_state: &'a TxState,
18071812
committed_state: &'a CommittedState,
18081813
inserted_rows: BTreeIndexRangeIter<'a>,
18091814
committed_rows: Option<BTreeIndexRangeIter<'a>>,
1815+
num_committed_rows_fetched: u64,
1816+
}
1817+
1818+
impl Drop for IndexSeekIterInner<'_> {
1819+
fn drop(&mut self) {
1820+
// Increment number of index seeks
1821+
DB_METRICS
1822+
.rdb_num_index_seeks
1823+
.with_label_values(
1824+
&self.ctx.txn_type(),
1825+
&self.ctx.database(),
1826+
self.ctx.reducer_name().unwrap_or_default(),
1827+
&self.table_id.0,
1828+
)
1829+
.inc();
1830+
1831+
// Increment number of index keys scanned
1832+
DB_METRICS
1833+
.rdb_num_keys_scanned
1834+
.with_label_values(
1835+
&self.ctx.txn_type(),
1836+
&self.ctx.database(),
1837+
self.ctx.reducer_name().unwrap_or_default(),
1838+
&self.table_id.0,
1839+
)
1840+
.inc_by(self.committed_rows.as_ref().map_or(0, |iter| iter.keys_scanned()));
1841+
1842+
// Increment number of rows fetched
1843+
DB_METRICS
1844+
.rdb_num_rows_fetched
1845+
.with_label_values(
1846+
&self.ctx.txn_type(),
1847+
&self.ctx.database(),
1848+
self.ctx.reducer_name().unwrap_or_default(),
1849+
&self.table_id.0,
1850+
)
1851+
.inc_by(self.num_committed_rows_fetched);
1852+
}
18101853
}
18111854

18121855
impl<'a> Iterator for IndexSeekIterInner<'a> {
@@ -1830,6 +1873,7 @@ impl<'a> Iterator for IndexSeekIterInner<'a> {
18301873
.map_or(false, |table| table.contains(row_id))
18311874
})
18321875
}) {
1876+
self.num_committed_rows_fetched += 1;
18331877
return Some(get_committed_row(self.committed_state, &self.table_id, row_id));
18341878
}
18351879

@@ -1838,10 +1882,48 @@ impl<'a> Iterator for IndexSeekIterInner<'a> {
18381882
}
18391883

18401884
pub struct CommittedIndexIter<'a> {
1885+
ctx: &'a ExecutionContext<'a>,
18411886
table_id: TableId,
18421887
tx_state: &'a TxState,
18431888
committed_state: &'a CommittedState,
18441889
committed_rows: BTreeIndexRangeIter<'a>,
1890+
num_committed_rows_fetched: u64,
1891+
}
1892+
1893+
impl Drop for CommittedIndexIter<'_> {
1894+
fn drop(&mut self) {
1895+
DB_METRICS
1896+
.rdb_num_index_seeks
1897+
.with_label_values(
1898+
&self.ctx.txn_type(),
1899+
&self.ctx.database(),
1900+
self.ctx.reducer_name().unwrap_or_default(),
1901+
&self.table_id.0,
1902+
)
1903+
.inc();
1904+
1905+
// Increment number of index keys scanned
1906+
DB_METRICS
1907+
.rdb_num_keys_scanned
1908+
.with_label_values(
1909+
&self.ctx.txn_type(),
1910+
&self.ctx.database(),
1911+
self.ctx.reducer_name().unwrap_or_default(),
1912+
&self.table_id.0,
1913+
)
1914+
.inc_by(self.committed_rows.keys_scanned());
1915+
1916+
// Increment number of rows fetched
1917+
DB_METRICS
1918+
.rdb_num_rows_fetched
1919+
.with_label_values(
1920+
&self.ctx.txn_type(),
1921+
&self.ctx.database(),
1922+
self.ctx.reducer_name().unwrap_or_default(),
1923+
&self.table_id.0,
1924+
)
1925+
.inc_by(self.num_committed_rows_fetched);
1926+
}
18451927
}
18461928

18471929
impl<'a> Iterator for CommittedIndexIter<'a> {
@@ -1856,6 +1938,7 @@ impl<'a> Iterator for CommittedIndexIter<'a> {
18561938
.get(&self.table_id)
18571939
.map_or(false, |table| table.contains(row_id))
18581940
}) {
1941+
self.num_committed_rows_fetched += 1;
18591942
return Some(get_committed_row(self.committed_state, &self.table_id, row_id));
18601943
}
18611944

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ metrics_group!(
7171
#[labels(txn_type: TransactionType, db: Address, reducer: str, table_id: u32)]
7272
pub rdb_num_rows_fetched: IntCounterVec,
7373

74+
#[name = spacetime_num_index_keys_scanned_cumulative]
75+
#[help = "The cumulative number of keys scanned from an index"]
76+
#[labels(txn_type: TransactionType, db: Address, reducer: str, table_id: u32)]
77+
pub rdb_num_keys_scanned: IntCounterVec,
78+
79+
#[name = spacetime_num_index_seeks_cumulative]
80+
#[help = "The cumulative number of index seeks"]
81+
#[labels(txn_type: TransactionType, db: Address, reducer: str, table_id: u32)]
82+
pub rdb_num_index_seeks: IntCounterVec,
83+
7484
#[name = spacetime_num_txns_committed_cumulative]
7585
#[help = "The cumulative number of committed transactions"]
7686
#[labels(txn_type: TransactionType, db: Address, reducer: str)]

0 commit comments

Comments
 (0)