Skip to content

Commit 137397b

Browse files
fix: Reset max queue length and reducer wait time metrics
Max value metrics are currently reset to zero after each collection period. However this won't be reflected in the metrics store immediately. Once the database records new values for said metrics, only then will the reset be reflected in the metrics store. This subtlety is imperceptible for metrics that are frequently updated. But if a metric is not frequently updated, or for periods of reduced load in general, the metrics store may present stale max value metrics. This anomaly was thought to have been fixed by #709. However it only fixed it for DB_METRICS. This patch removes the anomaly for WORKER_METRICS as well.
1 parent 79b2d04 commit 137397b

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

crates/core/src/host/scheduler.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use std::collections::hash_map::DefaultHasher;
2-
use std::hash::Hasher;
31
use std::path::Path;
42

53
use futures::StreamExt;
64
use rustc_hash::FxHashMap;
75
use sled::transaction::{ConflictableTransactionError::Abort as TxAbort, TransactionError};
6+
use spacetimedb_lib::bsatn;
87
use spacetimedb_lib::bsatn::ser::BsatnError;
9-
use spacetimedb_lib::{bsatn, Address};
108
use tokio::sync::mpsc;
119
use tokio_util::time::delay_queue::Expired;
1210
use tokio_util::time::{delay_queue, DelayQueue};
@@ -273,18 +271,11 @@ impl SchedulerActor {
273271
};
274272
let scheduled: ScheduledReducer = bsatn::from_slice(&scheduled).unwrap();
275273

276-
fn hash(a: &Address, b: &str) -> u64 {
277-
use std::hash::Hash;
278-
let mut hasher = DefaultHasher::new();
279-
a.hash(&mut hasher);
280-
b.hash(&mut hasher);
281-
hasher.finish()
282-
}
283-
284274
let db = module_host.info().address;
275+
let reducer = scheduled.reducer.clone();
285276
let mut guard = MAX_REDUCER_DELAY.lock().unwrap();
286277
let max_reducer_delay = *guard
287-
.entry(hash(&db, &scheduled.reducer))
278+
.entry((db, reducer))
288279
.and_modify(|max| {
289280
if delay > *max {
290281
*max = delay;

crates/core/src/worker_metrics/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,17 @@ metrics_group!(
8484
}
8585
);
8686

87+
type ReducerLabel = (Address, String);
88+
8789
pub static MAX_QUEUE_LEN: Lazy<Mutex<HashMap<Address, i64>>> = Lazy::new(|| Mutex::new(HashMap::new()));
88-
pub static MAX_REDUCER_DELAY: Lazy<Mutex<HashMap<u64, f64>>> = Lazy::new(|| Mutex::new(HashMap::new()));
90+
pub static MAX_REDUCER_DELAY: Lazy<Mutex<HashMap<ReducerLabel, f64>>> = Lazy::new(|| Mutex::new(HashMap::new()));
8991
pub static WORKER_METRICS: Lazy<WorkerMetrics> = Lazy::new(WorkerMetrics::new);
9092

9193
pub fn reset_counters() {
94+
// Reset max queue length
95+
WORKER_METRICS.instance_queue_length_max.0.reset();
9296
MAX_QUEUE_LEN.lock().unwrap().clear();
97+
// Reset max reducer wait time
98+
WORKER_METRICS.scheduled_reducer_delay_sec_max.0.reset();
9399
MAX_REDUCER_DELAY.lock().unwrap().clear();
94100
}

0 commit comments

Comments
 (0)