-
Notifications
You must be signed in to change notification settings - Fork 3
Make LogicalMeterActor resilient to wall-clock time jumps #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
973bbd8
Add NTP-jump-resilient wall clock timer
shsms 58bf296
Use WallClockTimer in LogicalMeterActor for time-jump resilience
shsms f5321d9
Add LogicalMeterActor tests for resampling and NTP time jump recovery
shsms 8cdd357
Remove ChronoError variant from ErrorKind
shsms 3861b28
Refactor stream polling into a function
shsms ffd2df1
Extract resampler-config helper from startup and rebuild paths
shsms a4d8878
Log Lagged broadcast errors at warn level
shsms 5e47325
Reject non-positive resampling_interval in LogicalMeterActor::try_new
shsms ebbd469
Reject max_age_in_intervals values exceeding i32::MAX
shsms 47f7253
Update release notes
shsms d6d8ec6
Drop real-time sleep from MockMicrogridApiClient::new()
shsms 1cb49de
Clean up clippy warnings across the crate
shsms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // License: MIT | ||
| // Copyright © 2026 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| //! Test clock for `#[tokio::test(start_paused = true)]` tests. | ||
| //! | ||
| //! Pins wall-clock time to tokio's (possibly paused) monotonic clock, so | ||
| //! advancing tokio time advances wall time too. Clones share state, so a | ||
| //! test can hand copies to the actor and a mock telemetry source and jump | ||
| //! both by calling | ||
| //! [`inject_wall_jump`][TokioSyncedClock::inject_wall_jump] once. | ||
| //! | ||
| //! Lives under `test_utils` so the `Arc`/`RwLock`/injection machinery | ||
| //! isn't compiled into production builds. | ||
|
|
||
| use chrono::{DateTime, TimeDelta, Utc}; | ||
| use tokio::time::Instant; | ||
|
|
||
| use crate::wall_clock_timer::Clock; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct TokioSyncedClock { | ||
| inner: std::sync::Arc<std::sync::RwLock<TokioSyncedClockInner>>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct TokioSyncedClockInner { | ||
| wall_anchor: DateTime<Utc>, | ||
| mono_anchor: Instant, | ||
| } | ||
|
|
||
| impl Default for TokioSyncedClock { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl TokioSyncedClock { | ||
| /// Creates a clock anchored to the current `Utc::now()` (and the current | ||
| /// tokio monotonic instant). Suitable when the caller doesn't care about | ||
| /// a specific starting wall-clock value. | ||
| pub fn new() -> Self { | ||
| Self::with_wall_anchor(Utc::now()) | ||
| } | ||
|
|
||
| /// Creates a clock whose wall-clock time at the current tokio instant | ||
| /// is exactly `wall_anchor`. Useful when the caller needs the anchor | ||
| /// aligned to a specific boundary (e.g. a whole-second tick). | ||
| pub fn with_wall_anchor(wall_anchor: DateTime<Utc>) -> Self { | ||
| Self { | ||
| inner: std::sync::Arc::new(std::sync::RwLock::new(TokioSyncedClockInner { | ||
| wall_anchor, | ||
| mono_anchor: Instant::now(), | ||
| })), | ||
| } | ||
| } | ||
|
|
||
| /// Shifts wall-clock time by `offset` relative to the monotonic clock, | ||
| /// simulating an NTP jump. Visible to every clone. | ||
| pub fn inject_wall_jump(&self, offset: TimeDelta) { | ||
| let mut inner = self.inner.write().expect("clock poisoned"); | ||
| // Only `wall_anchor` moves — `mono_anchor` is intentionally left | ||
| // untouched so wall and monotonic diverge, which is what makes | ||
| // this simulate an NTP jump rather than a re-anchor of both | ||
| // clocks together. | ||
| inner.wall_anchor += offset; | ||
|
llucax marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| impl Clock for TokioSyncedClock { | ||
| fn wall_now(&self) -> DateTime<Utc> { | ||
| let inner = self.inner.read().expect("clock poisoned"); | ||
| let elapsed = Instant::now().duration_since(inner.mono_anchor); | ||
| inner.wall_anchor | ||
| + TimeDelta::from_std(elapsed).expect("tokio elapsed fits in TimeDelta (~292 years)") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.