Skip to content

Commit 2b4c99e

Browse files
committed
A bit of refactoring
1 parent f109e8b commit 2b4c99e

File tree

11 files changed

+269
-301
lines changed

11 files changed

+269
-301
lines changed

crates/core/src/host/instance_env.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::mem;
99
use parking_lot::{Mutex, MutexGuard};
1010
use smallvec::SmallVec;
1111
use 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};
1313
use spacetimedb_lib::{ConnectionId, Identity, Timestamp};
1414
use spacetimedb_primitives::{ColId, ColList, IndexId, TableId};
1515
use spacetimedb_sats::{
@@ -22,16 +22,22 @@ use spacetimedb_table::table::RowRef;
2222
use std::fmt::Display;
2323
use std::ops::DerefMut;
2424
use std::sync::Arc;
25+
use std::time::Instant;
2526
use std::vec::IntoIter;
2627

2728
#[derive(Clone)]
2829
pub 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.

crates/core/src/host/v8/budget.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(super) extern "C" fn cb_log_long_running(isolate: &mut Isolate, _: *mut c_vo
5555
return;
5656
};
5757
let database = env.instance_env.replica_ctx.database_identity;
58-
let reducer = env.reducer_name();
58+
let reducer = env.funcall_name();
5959
let dur = env.reducer_start().elapsed();
6060
tracing::warn!(reducer, ?database, "JavaScript has been running for {dur:?}");
6161
}

0 commit comments

Comments
 (0)