@@ -666,7 +666,9 @@ impl MutTxDatastore for Locking {
666666/// Various measurements, needed for metrics, of the work performed by a transaction.
667667#[ must_use = "TxMetrics should be reported" ]
668668pub struct TxMetrics {
669- table_stats : HashMap < TableId , TableStats > ,
669+ /// The transaction metrics for a particular table.
670+ /// The value `None` for a [`TableId`] means that it was deleted.
671+ table_stats : HashMap < TableId , Option < TableStats > > ,
670672 workload : WorkloadType ,
671673 database_identity : Identity ,
672674 elapsed_time : Duration ,
@@ -715,16 +717,12 @@ impl TxMetrics {
715717 let mut table_stats =
716718 <HashMap < _ , _ , _ > as HashCollectionExt >:: with_capacity ( tx_data. num_tables_affected ( ) ) ;
717719 for ( table_id, _) in tx_data. table_ids_and_names ( ) {
718- committed_state. get_table ( table_id) . and_then ( |table| {
719- table_stats. insert (
720- table_id,
721- TableStats {
722- row_count : table. row_count ,
723- bytes_occupied_overestimate : table. bytes_occupied_overestimate ( ) ,
724- num_indices : table. num_indices ( ) ,
725- } ,
726- )
720+ let stats = committed_state. get_table ( table_id) . map ( |table| TableStats {
721+ row_count : table. row_count ,
722+ bytes_occupied_overestimate : table. bytes_occupied_overestimate ( ) ,
723+ num_indices : table. num_indices ( ) ,
727724 } ) ;
725+ table_stats. insert ( table_id, stats) ;
728726 }
729727 table_stats
730728 } )
@@ -774,63 +772,106 @@ impl TxMetrics {
774772
775773 get_exec_counter ( self . workload ) . record ( & self . exec_metrics ) ;
776774
775+ // TODO(centril): simplify this by exposing `tx_data.for_table(table_id)`.
777776 if let Some ( tx_data) = tx_data {
778777 // Update table rows and table size gauges,
779778 // and sets them to zero if no table is present.
780779 for ( table_id, table_name) in tx_data. table_ids_and_names ( ) {
781- let stats = self . table_stats . get ( & table_id) . unwrap ( ) ;
782-
783- DB_METRICS
784- . rdb_num_table_rows
785- . with_label_values ( db, & table_id. 0 , table_name)
786- . set ( stats. row_count as i64 ) ;
787- DB_METRICS
788- . rdb_table_size
789- . with_label_values ( db, & table_id. 0 , table_name)
790- . set ( stats. bytes_occupied_overestimate as i64 ) ;
780+ if let Some ( stats) = self . table_stats . get ( & table_id) . unwrap ( ) {
781+ DB_METRICS
782+ . rdb_num_table_rows
783+ . with_label_values ( db, & table_id. 0 , table_name)
784+ . set ( stats. row_count as i64 ) ;
785+ DB_METRICS
786+ . rdb_table_size
787+ . with_label_values ( db, & table_id. 0 , table_name)
788+ . set ( stats. bytes_occupied_overestimate as i64 ) ;
789+ } else {
790+ // Table was dropped, remove the metrics.
791+ let _ = DB_METRICS
792+ . rdb_num_table_rows
793+ . remove_label_values ( db, & table_id. 0 , table_name) ;
794+ let _ = DB_METRICS
795+ . rdb_table_size
796+ . remove_label_values ( db, & table_id. 0 , table_name) ;
797+ }
791798 }
792799
793800 // Record inserts.
794801 for ( table_id, table_name, inserts) in tx_data. inserts_with_table_name ( ) {
795- let stats = self . table_stats . get ( table_id) . unwrap ( ) ;
796- let num_inserts = inserts. len ( ) as u64 ;
797- let num_indices = stats. num_indices as u64 ;
798-
799- // Increment rows inserted counter.
800- DB_METRICS
801- . rdb_num_rows_inserted
802- . with_label_values ( workload, db, reducer, & table_id. 0 , table_name)
803- . inc_by ( num_inserts) ;
804-
805- // We don't have sparse indexes, so we can just multiply by the number of indexes.
806- if stats. num_indices > 0 {
807- // Increment index rows inserted counter
802+ if let Some ( stats) = self . table_stats . get ( table_id) . unwrap ( ) {
803+ let num_inserts = inserts. len ( ) as u64 ;
804+ let num_indices = stats. num_indices as u64 ;
805+
806+ // Increment rows inserted counter.
808807 DB_METRICS
809- . rdb_num_index_entries_inserted
808+ . rdb_num_rows_inserted
810809 . with_label_values ( workload, db, reducer, & table_id. 0 , table_name)
811- . inc_by ( num_inserts * num_indices) ;
810+ . inc_by ( num_inserts) ;
811+
812+ // We don't have sparse indexes, so we can just multiply by the number of indexes.
813+ if stats. num_indices > 0 {
814+ // Increment index rows inserted counter
815+ DB_METRICS
816+ . rdb_num_index_entries_inserted
817+ . with_label_values ( workload, db, reducer, & table_id. 0 , table_name)
818+ . inc_by ( num_inserts * num_indices) ;
819+ }
820+ } else {
821+ // Table was dropped, remove the metrics.
822+ let _ = DB_METRICS . rdb_num_rows_inserted . remove_label_values (
823+ workload,
824+ db,
825+ reducer,
826+ & table_id. 0 ,
827+ table_name,
828+ ) ;
829+ let _ = DB_METRICS . rdb_num_index_entries_inserted . remove_label_values (
830+ workload,
831+ db,
832+ reducer,
833+ & table_id. 0 ,
834+ table_name,
835+ ) ;
812836 }
813837 }
814838
815839 // Record deletes.
816840 for ( table_id, table_name, deletes) in tx_data. deletes_with_table_name ( ) {
817- let stats = self . table_stats . get ( table_id) . unwrap ( ) ;
818- let num_deletes = deletes. len ( ) as u64 ;
819- let num_indices = stats. num_indices as u64 ;
820-
821- // Increment rows deleted counter.
822- DB_METRICS
823- . rdb_num_rows_deleted
824- . with_label_values ( workload, db, reducer, & table_id. 0 , table_name)
825- . inc_by ( num_deletes) ;
826-
827- // We don't have sparse indexes, so we can just multiply by the number of indexes.
828- if num_indices > 0 {
829- // Increment index rows deleted counter.
841+ if let Some ( stats) = self . table_stats . get ( table_id) . unwrap ( ) {
842+ let num_deletes = deletes. len ( ) as u64 ;
843+ let num_indices = stats. num_indices as u64 ;
844+
845+ // Increment rows deleted counter.
830846 DB_METRICS
831- . rdb_num_index_entries_deleted
847+ . rdb_num_rows_deleted
832848 . with_label_values ( workload, db, reducer, & table_id. 0 , table_name)
833- . inc_by ( num_deletes * num_indices) ;
849+ . inc_by ( num_deletes) ;
850+
851+ // We don't have sparse indexes, so we can just multiply by the number of indexes.
852+ if num_indices > 0 {
853+ // Increment index rows deleted counter.
854+ DB_METRICS
855+ . rdb_num_index_entries_deleted
856+ . with_label_values ( workload, db, reducer, & table_id. 0 , table_name)
857+ . inc_by ( num_deletes * num_indices) ;
858+ }
859+ } else {
860+ // Table was dropped, remove the metrics.
861+ let _ = DB_METRICS . rdb_num_rows_deleted . remove_label_values (
862+ workload,
863+ db,
864+ reducer,
865+ & table_id. 0 ,
866+ table_name,
867+ ) ;
868+ let _ = DB_METRICS . rdb_num_index_entries_deleted . remove_label_values (
869+ workload,
870+ db,
871+ reducer,
872+ & table_id. 0 ,
873+ table_name,
874+ ) ;
834875 }
835876 }
836877 }
0 commit comments