@@ -9,7 +9,7 @@ use core::mem;
99use parking_lot:: { Mutex , MutexGuard } ;
1010use smallvec:: SmallVec ;
1111use spacetimedb_datastore:: locking_tx_datastore:: state_view:: StateView ;
12- use spacetimedb_datastore:: locking_tx_datastore:: { MutTxId , UniqueView } ;
12+ use spacetimedb_datastore:: locking_tx_datastore:: { FuncCallType , MutTxId } ;
1313use spacetimedb_lib:: { ConnectionId , Identity , Timestamp } ;
1414use spacetimedb_primitives:: { ColId , ColList , IndexId , TableId } ;
1515use spacetimedb_sats:: {
@@ -22,16 +22,22 @@ use spacetimedb_table::table::RowRef;
2222use std:: fmt:: Display ;
2323use std:: ops:: DerefMut ;
2424use std:: sync:: Arc ;
25+ use std:: time:: Instant ;
2526use std:: vec:: IntoIter ;
2627
2728#[ derive( Clone ) ]
2829pub struct InstanceEnv {
2930 pub replica_ctx : Arc < ReplicaContext > ,
3031 pub scheduler : Scheduler ,
3132 pub tx : TxSlot ,
32- /// The timestamp the current reducer began running.
33+ /// The timestamp the current function began running.
3334 pub start_time : Timestamp ,
34- pub view : Option < UniqueView > ,
35+ /// The instant the current function began running.
36+ pub start_instant : Instant ,
37+ /// The type of the last, including current, function to be executed by this environment.
38+ pub func_type : FuncCallType ,
39+ /// The name of the last, including current, function to be executed by this environment.
40+ pub func_name : String ,
3541}
3642
3743#[ derive( Clone , Default ) ]
@@ -173,7 +179,11 @@ impl InstanceEnv {
173179 scheduler,
174180 tx : TxSlot :: default ( ) ,
175181 start_time : Timestamp :: now ( ) ,
176- view : None ,
182+ start_instant : Instant :: now ( ) ,
183+ // arbitrary - change if we need to recognize that an `InstanceEnv` has never
184+ // run a function
185+ func_type : FuncCallType :: Reducer ,
186+ func_name : String :: from ( "<initializing>" ) ,
177187 }
178188 }
179189
@@ -182,16 +192,12 @@ impl InstanceEnv {
182192 & self . replica_ctx . database . database_identity
183193 }
184194
185- /// Signal to this `InstanceEnv` that a reducer, procedure call is beginning.
186- pub fn start_funcall ( & mut self , ts : Timestamp ) {
195+ /// Signal to this `InstanceEnv` that a function call is beginning.
196+ pub fn start_funcall ( & mut self , name : & str , ts : Timestamp , func_type : FuncCallType ) {
187197 self . start_time = ts;
188- self . view = None ;
189- }
190-
191- /// Signal to this `InstanceEnv` that a view is starting.
192- pub fn start_view ( & mut self , ts : Timestamp , view : UniqueView ) {
193- self . start_time = ts;
194- self . view = Some ( view) ;
198+ self . start_instant = Instant :: now ( ) ;
199+ self . func_type = func_type;
200+ name. clone_into ( & mut self . func_name ) ;
195201 }
196202
197203 fn get_tx ( & self ) -> Result < impl DerefMut < Target = MutTxId > + ' _ , GetTxError > {
@@ -472,7 +478,7 @@ impl InstanceEnv {
472478 stdb. table_row_count_mut ( tx, table_id)
473479 . ok_or ( NodesError :: TableNotFound )
474480 . inspect ( |_| {
475- tx. record_table_scan ( self . view . clone ( ) , table_id) ;
481+ tx. record_table_scan ( & self . func_type , table_id) ;
476482 } )
477483 }
478484
@@ -497,7 +503,7 @@ impl InstanceEnv {
497503 & mut bytes_scanned,
498504 ) ;
499505
500- tx. record_table_scan ( self . view . clone ( ) , table_id) ;
506+ tx. record_table_scan ( & self . func_type , table_id) ;
501507
502508 tx. metrics . rows_scanned += rows_scanned;
503509 tx. metrics . bytes_scanned += bytes_scanned;
@@ -528,7 +534,7 @@ impl InstanceEnv {
528534 // Scan the index and serialize rows to bsatn
529535 let chunks = ChunkedWriter :: collect_iter ( pool, iter, & mut rows_scanned, & mut bytes_scanned) ;
530536
531- tx. record_index_scan ( self . view . clone ( ) , table_id, index_id, lower, upper) ;
537+ tx. record_index_scan ( & self . func_type , table_id, index_id, lower, upper) ;
532538
533539 tx. metrics . index_seeks += 1 ;
534540 tx. metrics . rows_scanned += rows_scanned;
@@ -659,16 +665,7 @@ mod test {
659665 fn instance_env ( db : Arc < RelationalDB > ) -> Result < ( InstanceEnv , tokio:: runtime:: Runtime ) > {
660666 let ( scheduler, _) = Scheduler :: open ( db. clone ( ) ) ;
661667 let ( replica_context, runtime) = replica_ctx ( db) ?;
662- Ok ( (
663- InstanceEnv {
664- replica_ctx : Arc :: new ( replica_context) ,
665- scheduler,
666- tx : TxSlot :: default ( ) ,
667- start_time : Timestamp :: now ( ) ,
668- view : None ,
669- } ,
670- runtime,
671- ) )
668+ Ok ( ( InstanceEnv :: new ( Arc :: new ( replica_context) , scheduler) , runtime) )
672669 }
673670
674671 /// An in-memory `RelationalDB` for testing.
0 commit comments