From a02c9f9a9a1d93f891bd8e131a4268aad0ee70a2 Mon Sep 17 00:00:00 2001 From: Lev Neiman Date: Fri, 13 Nov 2026 07:06:09 +0000 Subject: [PATCH] fix(rust): exclude observer work from local lookup duration Capture the local lookup duration before invoking request observers. Add a deterministic regression for both the initial miss and a later hit. --- rust/src/execution.rs | 3 +- rust/tests/metrics_exporters.rs | 61 ++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/rust/src/execution.rs b/rust/src/execution.rs index fec0c8f9..9786a0e1 100644 --- a/rust/src/execution.rs +++ b/rust/src/execution.rs @@ -590,12 +590,13 @@ impl Execution { }); } Ok(found) => { + let seconds = self.seconds_since(start); self.emit(Event::Request { labels: self.labels(Layer::Local), }); self.emit(Event::Get { labels: self.labels(Layer::Local), - seconds: self.seconds_since(start), + seconds, }); let reason = match found { Some(value) if value.as_ref().type_id() == self.op.metadata.value_type => { diff --git a/rust/tests/metrics_exporters.rs b/rust/tests/metrics_exporters.rs index 3149ad76..ad387a6c 100644 --- a/rust/tests/metrics_exporters.rs +++ b/rust/tests/metrics_exporters.rs @@ -764,7 +764,7 @@ mod prometheus_exporter { mod isolation { use super::*; - use dialcache::testing::{TestExecutor, WALL_EPOCH_MS}; + use dialcache::testing::{TestExecutor, VirtualClock, WALL_EPOCH_MS}; use dialcache::{DialCache, KeySpec, Policy}; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -782,6 +782,65 @@ mod isolation { } } + struct AdvancingObserver { + clock: Arc, + durations: Arc>>, + } + + impl Observer for AdvancingObserver { + fn observe(&self, event: &Event) { + match event { + Event::Request { labels } if labels.layer == Layer::Local => { + self.clock.shift(250, false); + } + Event::Get { labels, seconds } if labels.layer == Layer::Local => { + self.durations.lock().push(*seconds); + } + _ => {} + } + } + } + + #[test] + fn local_get_duration_excludes_request_observer_time() { + let mut exec = TestExecutor::new(WALL_EPOCH_MS); + let durations = Arc::new(Mutex::new(Vec::new())); + let cache = DialCache::builder() + .clock_arc(exec.clock.clone()) + .runtime_arc(exec.runtime.clone()) + .observer(AdvancingObserver { + clock: exec.clock.clone(), + durations: durations.clone(), + }) + .build() + .expect("configuration"); + let source_calls = Arc::new(AtomicUsize::new(0)); + let counting = source_calls.clone(); + let lookup = cache + .use_case::("item", "lookup") + .policy(Policy::default().local_ttl_sec(60)) + .key(|id: &i64| KeySpec::new(id.to_string())) + .source(move |_scope, id: i64| { + counting.fetch_add(1, Ordering::SeqCst); + async move { Ok(id * 10) } + }) + .register() + .expect("use case"); + + for _ in 0..2 { + let cache = cache.clone(); + let lookup = lookup.clone(); + let value = exec.block_on(async move { + cache + .enable(|scope| async move { lookup.get(&scope, 7).await }) + .await + }); + assert_eq!(*value.expect("lookup result"), 70); + } + assert_eq!(source_calls.load(Ordering::SeqCst), 1); + assert_eq!(*durations.lock(), [0.0, 0.0]); + } + /// A panicking observer never changes the result of a cached call. #[test] fn panicking_observer_does_not_change_cached_results() {