Skip to content

Commit 27766ce

Browse files
committed
hmon: rework heartbeat monitor state
Rework heartbeat monitor state into two atomics.
1 parent 176ff06 commit 27766ce

4 files changed

Lines changed: 184 additions & 198 deletions

File tree

src/health_monitoring_lib/rust/common.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,38 @@ impl MonitorEvaluator for MonitorEvalHandle {
9292
}
9393
}
9494

95-
/// Get offset between HMON and monitor starting time points as [`u32`].
96-
pub(crate) fn hmon_time_offset(hmon_starting_point: Instant, monitor_starting_point: Instant) -> u32 {
95+
/// Get offset between HMON and monitor starting time points as integers.
96+
pub(crate) fn hmon_time_offset<T>(hmon_starting_point: Instant, monitor_starting_point: Instant) -> T
97+
where
98+
T: TryFrom<u128>,
99+
<T as TryFrom<u128>>::Error: core::fmt::Debug,
100+
{
97101
let result = hmon_starting_point.checked_duration_since(monitor_starting_point);
98102
let duration_since = result.expect("HMON starting point is earlier than monitor starting point");
99-
duration_to_u32(duration_since)
103+
duration_to_int(duration_since)
100104
}
101105

102-
/// Get duration as [`u32`].
103-
pub(crate) fn duration_to_u32(duration: Duration) -> u32 {
106+
/// Get duration as an integer.
107+
pub(crate) fn duration_to_int<T>(duration: Duration) -> T
108+
where
109+
T: TryFrom<u128>,
110+
<T as TryFrom<u128>>::Error: core::fmt::Debug,
111+
{
104112
let millis = duration.as_millis();
105-
u32::try_from(millis).expect("Monitor running for too long")
113+
T::try_from(millis).expect("Monitor running for too long")
106114
}
107115

108116
#[cfg(all(test, not(loom)))]
109117
mod tests {
110-
use crate::common::{duration_to_u32, hmon_time_offset};
118+
use crate::common::{duration_to_int, hmon_time_offset};
111119
use core::time::Duration;
112120
use std::time::Instant;
113121

114122
#[test]
115123
fn hmon_time_offset_valid() {
116124
let monitor_starting_point = Instant::now();
117125
let hmon_starting_point = Instant::now();
118-
let offset = hmon_time_offset(hmon_starting_point, monitor_starting_point);
126+
let offset: u32 = hmon_time_offset(hmon_starting_point, monitor_starting_point);
119127
// Allow small offset.
120128
assert!(offset < 10);
121129
}
@@ -125,7 +133,7 @@ mod tests {
125133
fn hmon_time_offset_wrong_order() {
126134
let hmon_starting_point = Instant::now();
127135
let monitor_starting_point = Instant::now();
128-
let _offset = hmon_time_offset(hmon_starting_point, monitor_starting_point);
136+
let _offset: u32 = hmon_time_offset(hmon_starting_point, monitor_starting_point);
129137
}
130138

131139
#[test]
@@ -136,19 +144,19 @@ mod tests {
136144
let hmon_starting_point = Instant::now()
137145
.checked_add(Duration::from_secs(HUNDRED_DAYS_AS_SECS))
138146
.unwrap();
139-
let _offset = hmon_time_offset(hmon_starting_point, monitor_starting_point);
147+
let _offset: u32 = hmon_time_offset(hmon_starting_point, monitor_starting_point);
140148
}
141149

142150
#[test]
143-
fn duration_to_u32_valid() {
144-
let result = duration_to_u32(Duration::from_millis(1234));
151+
fn duration_to_int_valid() {
152+
let result: u32 = duration_to_int(Duration::from_millis(1234));
145153
assert_eq!(result, 1234);
146154
}
147155

148156
#[test]
149157
#[should_panic(expected = "Monitor running for too long")]
150-
fn duration_to_u32_too_large() {
158+
fn duration_to_int_too_large() {
151159
const HUNDRED_DAYS_AS_SECS: u64 = 100 * 24 * 60 * 60;
152-
let _result = duration_to_u32(Duration::from_secs(HUNDRED_DAYS_AS_SECS));
160+
let _result: u32 = duration_to_int(Duration::from_secs(HUNDRED_DAYS_AS_SECS));
153161
}
154162
}

src/health_monitoring_lib/rust/deadline/deadline_monitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
// SPDX-License-Identifier: Apache-2.0
1212
// *******************************************************************************
13-
use crate::common::{duration_to_u32, Monitor, MonitorEvalHandle, MonitorEvaluationError, MonitorEvaluator, TimeRange};
13+
use crate::common::{duration_to_int, Monitor, MonitorEvalHandle, MonitorEvaluationError, MonitorEvaluator, TimeRange};
1414
use crate::deadline::common::{DeadlineTemplate, StateIndex};
1515
use crate::deadline::deadline_state::{DeadlineState, DeadlineStateSnapshot};
1616
use crate::log::{error, warn, ScoreDebug};
@@ -152,7 +152,7 @@ impl Deadline {
152152
/// Caller must ensure that deadline is not used until it's stopped.
153153
/// After this call You shall assure there's only a single owner of the `Deadline` instance and it does not call start before stopping.
154154
pub(super) unsafe fn start_internal(&mut self) -> Result<(), DeadlineError> {
155-
let now = duration_to_u32(self.monitor.monitor_starting_point.elapsed());
155+
let now = duration_to_int::<u32>(self.monitor.monitor_starting_point.elapsed());
156156
let max_time = now + self.range.max.as_millis() as u32;
157157

158158
let mut is_broken = false;
@@ -177,7 +177,7 @@ impl Deadline {
177177
}
178178

179179
pub(super) fn stop_internal(&mut self) {
180-
let now = duration_to_u32(self.monitor.monitor_starting_point.elapsed());
180+
let now = duration_to_int::<u32>(self.monitor.monitor_starting_point.elapsed());
181181
let max = self.range.max.as_millis() as u32;
182182
let min = self.range.min.as_millis() as u32;
183183

@@ -274,7 +274,7 @@ impl MonitorEvaluator for DeadlineMonitorInner {
274274
"Deadline snapshot cannot be both running and stopped"
275275
);
276276

277-
let now = duration_to_u32(self.monitor_starting_point.elapsed());
277+
let now = duration_to_int::<u32>(self.monitor_starting_point.elapsed());
278278
let expected = snapshot.timestamp_ms();
279279
if now > expected {
280280
// Deadline missed, report

0 commit comments

Comments
 (0)