11use super :: datastore:: locking_tx_datastore:: committed_state:: CommittedState ;
2- use super :: datastore:: locking_tx_datastore:: datastore:: { report_tx_metricses , TxMetrics } ;
2+ use super :: datastore:: locking_tx_datastore:: datastore:: TxMetrics ;
33use super :: datastore:: locking_tx_datastore:: state_view:: {
44 IterByColEqMutTx , IterByColRangeMutTx , IterMutTx , IterTx , StateView ,
55} ;
@@ -23,6 +23,7 @@ use crate::messages::control_db::HostType;
2323use crate :: subscription:: ExecutionCounters ;
2424use crate :: util:: { asyncify, spawn_rayon} ;
2525use anyhow:: { anyhow, Context } ;
26+ use enum_map:: EnumMap ;
2627use fs2:: FileExt ;
2728use futures:: channel:: mpsc;
2829use futures:: StreamExt ;
@@ -107,6 +108,9 @@ pub struct RelationalDB {
107108 // We want to release the file lock last.
108109 // TODO(noa): is this lockfile still necessary now that we have data-dir?
109110 _lock : LockFile ,
111+
112+ /// A map from workload types to their cached prometheus counters.
113+ workload_type_to_exec_counters : Arc < EnumMap < WorkloadType , ExecutionCounters > > ,
110114}
111115
112116#[ derive( Clone ) ]
@@ -224,6 +228,8 @@ impl RelationalDB {
224228 let ( durability, disk_size_fn) = durability. unzip ( ) ;
225229 let snapshot_worker =
226230 snapshot_repo. map ( |repo| SnapshotWorker :: new ( inner. committed_state . clone ( ) , repo. clone ( ) ) ) ;
231+ let workload_type_to_exec_counters =
232+ Arc :: new ( EnumMap :: from_fn ( |ty| ExecutionCounters :: new ( & ty, & database_identity) ) ) ;
227233
228234 Self {
229235 inner,
@@ -237,6 +243,7 @@ impl RelationalDB {
237243 disk_size_fn,
238244
239245 _lock : lock,
246+ workload_type_to_exec_counters,
240247 }
241248 }
242249
@@ -735,7 +742,7 @@ impl RelationalDB {
735742
736743 /// Returns the execution counters for `workload_type` for this database.
737744 pub fn exec_counters_for ( & self , workload_type : WorkloadType ) -> & ExecutionCounters {
738- self . inner . exec_counters_for ( workload_type)
745+ & self . workload_type_to_exec_counters [ workload_type]
739746 }
740747
741748 /// Begin a transaction.
@@ -972,7 +979,7 @@ impl RelationalDB {
972979 let mut tx = self . begin_tx ( workload) ;
973980 let res = f ( & mut tx) ;
974981 let ( tx_metics, reducer) = self . release_tx ( tx) ;
975- report_tx_metricses ( & reducer, self , None , None , & tx_metics) ;
982+ self . report_tx_metricses ( & reducer, None , None , & tx_metics) ;
976983 res
977984 }
978985
@@ -983,11 +990,11 @@ impl RelationalDB {
983990 {
984991 if res. is_err ( ) {
985992 let ( tx_metrics, reducer) = self . rollback_mut_tx ( tx) ;
986- tx_metrics . report_with_db ( & reducer, self , None ) ;
993+ self . report ( & reducer, & tx_metrics , None ) ;
987994 } else {
988995 match self . commit_tx ( tx) . map_err ( E :: from) ? {
989996 Some ( ( tx_data, tx_metrics, reducer) ) => {
990- tx_metrics . report_with_db ( & reducer, self , Some ( & tx_data) ) ;
997+ self . report ( & reducer, & tx_metrics , Some ( & tx_data) ) ;
991998 }
992999 None => panic ! ( "TODO: retry?" ) ,
9931000 }
@@ -1002,7 +1009,7 @@ impl RelationalDB {
10021009 match res {
10031010 Err ( e) => {
10041011 let ( tx_metrics, reducer) = self . rollback_mut_tx ( tx) ;
1005- tx_metrics . report_with_db ( & reducer, self , None ) ;
1012+ self . report ( & reducer, & tx_metrics , None ) ;
10061013
10071014 Err ( e)
10081015 }
@@ -1013,6 +1020,22 @@ impl RelationalDB {
10131020 pub ( crate ) fn alter_table_access ( & self , tx : & mut MutTx , name : Box < str > , access : StAccess ) -> Result < ( ) , DBError > {
10141021 self . inner . alter_table_access_mut_tx ( tx, name, access)
10151022 }
1023+
1024+ /// Reports the `TxMetrics`s passed.
1025+ ///
1026+ /// Should only be called after the tx lock has been fully released.
1027+ pub ( crate ) fn report_tx_metricses (
1028+ & self ,
1029+ reducer : & str ,
1030+ tx_data : Option < & TxData > ,
1031+ metrics_mut : Option < & TxMetrics > ,
1032+ metrics_read : & TxMetrics ,
1033+ ) {
1034+ if let Some ( metrics_mut) = metrics_mut {
1035+ self . report ( reducer, metrics_mut, tx_data) ;
1036+ }
1037+ self . report ( reducer, metrics_read, None ) ;
1038+ }
10161039}
10171040
10181041impl RelationalDB {
@@ -1360,6 +1383,11 @@ impl RelationalDB {
13601383 pub fn drop_constraint ( & self , tx : & mut MutTx , constraint_id : ConstraintId ) -> Result < ( ) , DBError > {
13611384 self . inner . drop_constraint_mut_tx ( tx, constraint_id)
13621385 }
1386+
1387+ /// Reports the metrics for `reducer`, using counters provided by `db`.
1388+ pub fn report ( & self , reducer : & str , metrics : & TxMetrics , tx_data : Option < & TxData > ) {
1389+ metrics. report ( tx_data, reducer, |wl : WorkloadType | self . exec_counters_for ( wl) ) ;
1390+ }
13631391}
13641392
13651393#[ allow( unused) ]
0 commit comments