Skip to content

Commit fda115c

Browse files
committed
hmon: approach to monitors rework
- Remove unnecessary abstraction from monitors. - Improve docs. - Move `FFIBorrowed` and `FFIHandle` to main `ffi` module. - Small esthetics fixes.
1 parent c74e3d7 commit fda115c

10 files changed

Lines changed: 430 additions & 489 deletions

File tree

src/health_monitoring_lib/rust/common.rs

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
// *******************************************************************************
1313

14-
use crate::tag::MonitorTag;
1514
use core::hash::Hash;
1615
use core::time::Duration;
17-
use std::sync::Arc;
1816
use std::time::Instant;
1917

2018
/// Range of accepted time.
@@ -44,88 +42,6 @@ pub(crate) fn duration_to_u32(duration: Duration) -> u32 {
4442
u32::try_from(millis).expect("Monitor running for too long")
4543
}
4644

47-
/// Heartbeat monitor error subgroup.
48-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, crate::log::ScoreDebug)]
49-
pub(crate) enum HeartbeatMonitorEvaluationError {
50-
/// Multiple heartbeats were observed in the same epoch.
51-
MultipleHeartbeats,
52-
}
53-
54-
/// Errors that can occur during monitor evaluation.
55-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, crate::log::ScoreDebug)]
56-
pub(crate) enum MonitorEvaluationError {
57-
TooEarly,
58-
TooLate,
59-
HeartbeatSpecific(HeartbeatMonitorEvaluationError),
60-
}
61-
62-
/// Trait for evaluating monitors and reporting errors to be used by HealthMonitor.
63-
pub(crate) trait MonitorEvaluator {
64-
/// Run monitor evaluation.
65-
///
66-
/// - `hmon_starting_point` - starting point of all monitors.
67-
/// - `on_error` - error handling, containing tag of a failing monitor and error code.
68-
fn evaluate(&self, hmon_starting_point: Instant, on_error: &mut dyn FnMut(&MonitorTag, MonitorEvaluationError));
69-
}
70-
71-
/// Handle to a monitor evaluator, allowing for dynamic dispatch.
72-
pub(crate) struct MonitorEvalHandle {
73-
inner: Arc<dyn MonitorEvaluator + Send + Sync>,
74-
}
75-
76-
impl MonitorEvalHandle {
77-
pub(crate) fn new<T: MonitorEvaluator + Send + Sync + 'static>(inner: Arc<T>) -> Self {
78-
Self { inner }
79-
}
80-
}
81-
82-
impl MonitorEvaluator for MonitorEvalHandle {
83-
fn evaluate(&self, hmon_starting_point: Instant, on_error: &mut dyn FnMut(&MonitorTag, MonitorEvaluationError)) {
84-
self.inner.evaluate(hmon_starting_point, on_error)
85-
}
86-
}
87-
88-
pub(crate) mod ffi {
89-
use core::mem::ManuallyDrop;
90-
use core::ops::{Deref, DerefMut};
91-
92-
pub(crate) type FFIHandle = *mut core::ffi::c_void;
93-
94-
pub(crate) const HM_OK: i32 = 0;
95-
pub(crate) const HM_NOT_FOUND: i32 = HM_OK + 1;
96-
pub(crate) const HM_ALREADY_EXISTS: i32 = HM_OK + 2;
97-
pub(crate) const _HM_INVALID_ARGS: i32 = HM_OK + 3;
98-
pub(crate) const _HM_WRONG_STATE: i32 = HM_OK + 4;
99-
pub(crate) const HM_FAILED: i32 = HM_OK + 5;
100-
101-
/// A wrapper to represent borrowed data over FFI boundary without taking ownership.
102-
pub(crate) struct FFIBorrowed<T> {
103-
data: ManuallyDrop<T>,
104-
}
105-
106-
impl<T> FFIBorrowed<T> {
107-
pub(crate) fn new(data: T) -> Self {
108-
Self {
109-
data: ManuallyDrop::new(data),
110-
}
111-
}
112-
}
113-
114-
impl<T: Deref> Deref for FFIBorrowed<T> {
115-
type Target = T;
116-
117-
fn deref(&self) -> &Self::Target {
118-
&self.data
119-
}
120-
}
121-
122-
impl<T: DerefMut> DerefMut for FFIBorrowed<T> {
123-
fn deref_mut(&mut self) -> &mut Self::Target {
124-
&mut self.data
125-
}
126-
}
127-
}
128-
12945
#[cfg(test)]
13046
mod tests {
13147
use crate::common::{duration_to_u32, hmon_time_offset};

0 commit comments

Comments
 (0)