Skip to content

Commit 35c7c0d

Browse files
committed
hmon: Cargo fix post-review fixes
- Less restrictive feature flags. - Additional small code changes.
1 parent 3cb438a commit 35c7c0d

8 files changed

Lines changed: 41 additions & 44 deletions

File tree

.github/workflows/lint_clippy.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,8 @@ jobs:
4848
override: true
4949
components: clippy
5050

51-
- name: check clippy errors (with "--features stub_supervisor_api_client")
51+
- name: check clippy errors
5252
uses: actions-rs/cargo@v1
5353
with:
5454
command: clippy
55-
args: --features stub_supervisor_api_client --all-targets -- -D warnings
56-
57-
- name: check clippy errors (with "--features score_supervisor_api_client")
58-
uses: actions-rs/cargo@v1
59-
with:
60-
command: clippy
61-
args: --features score_supervisor_api_client --no-default-features --all-targets -- -D warnings
55+
args: --all-features --all-targets --workspace -- -D warnings

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
"rust-analyzer.cargo.cfgs": [
1010
"!miri"
1111
],
12-
"rust-analyzer.cargo.features": [
13-
"stub_supervisor_api_client"
14-
],
1512
"rust-analyzer.check.command": "clippy",
1613
"rust-analyzer.rustfmt.overrideCommand": [
1714
"${workspaceFolder}/.vscode/rustfmt.sh"

src/health_monitoring_lib/rust/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub(crate) enum MonitorEvaluationError {
3939

4040
/// Trait for evaluating monitors and reporting errors to be used by HealthMonitor.
4141
pub(crate) trait MonitorEvaluator {
42+
/// Run monitor evaluation.
43+
///
44+
/// - `on_error` - error handling, containing tag of failing object and error code.
4245
fn evaluate(&self, on_error: &mut dyn FnMut(&MonitorTag, MonitorEvaluationError));
4346
}
4447

src/health_monitoring_lib/rust/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ mod worker;
2222
pub mod deadline;
2323

2424
use crate::common::MonitorEvalHandle;
25-
use crate::supervisor_api_client::SupervisorAPIClientImpl;
2625
pub use common::TimeRange;
2726
use containers::fixed_capacity::FixedCapacityVec;
2827
use core::time::Duration;
@@ -216,8 +215,17 @@ impl HealthMonitor {
216215
}
217216

218217
pub(crate) fn start_internal(&mut self, monitors: FixedCapacityVec<MonitorEvalHandle>) {
219-
let monitoring_logic =
220-
worker::MonitoringLogic::new(monitors, self.supervisor_api_cycle, SupervisorAPIClientImpl::new());
218+
let monitoring_logic = worker::MonitoringLogic::new(
219+
monitors,
220+
self.supervisor_api_cycle,
221+
#[cfg(all(not(test), feature = "score_supervisor_api_client"))]
222+
supervisor_api_client::score_supervisor_api_client::ScoreSupervisorAPIClient::new(),
223+
#[cfg(any(
224+
test,
225+
all(feature = "stub_supervisor_api_client", not(feature = "score_supervisor_api_client"))
226+
))]
227+
supervisor_api_client::stub_supervisor_api_client::StubSupervisorAPIClient::new(),
228+
);
221229

222230
self.worker.start(monitoring_logic)
223231
}

src/health_monitoring_lib/rust/supervisor_api_client/mod.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
// *******************************************************************************
1313

14-
//! Module for selecting [`SupervisorAPIClient`] implementation.
14+
//! Module providing [`SupervisorAPIClient`] implementations.
1515
//! Currently `ScoreSupervisorAPIClient` and `StubSupervisorAPIClient` are supported.
1616
//! The latter is meant for testing purposes.
1717
@@ -20,19 +20,9 @@ pub trait SupervisorAPIClient {
2020
fn notify_alive(&self);
2121
}
2222

23-
// Disallow both and none features.
24-
#[cfg(any(
25-
all(feature = "score_supervisor_api_client", feature = "stub_supervisor_api_client"),
26-
not(any(feature = "score_supervisor_api_client", feature = "stub_supervisor_api_client"))
27-
))]
28-
compile_error!("Either 'score_supervisor_api_client' or 'stub_supervisor_api_client' must be enabled!");
23+
// NOTE: various implementations are not mutually exclusive.
2924

3025
#[cfg(feature = "score_supervisor_api_client")]
31-
mod score_supervisor_api_client;
26+
pub mod score_supervisor_api_client;
3227
#[cfg(feature = "stub_supervisor_api_client")]
33-
mod stub_supervisor_api_client;
34-
35-
#[cfg(feature = "score_supervisor_api_client")]
36-
pub use score_supervisor_api_client::ScoreSupervisorAPIClient as SupervisorAPIClientImpl;
37-
#[cfg(feature = "stub_supervisor_api_client")]
38-
pub use stub_supervisor_api_client::StubSupervisorAPIClient as SupervisorAPIClientImpl;
28+
pub mod stub_supervisor_api_client;

src/health_monitoring_lib/rust/supervisor_api_client/score_supervisor_api_client.rs

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

14+
#![allow(dead_code)]
15+
1416
use crate::log::debug;
1517
use crate::supervisor_api_client::SupervisorAPIClient;
1618
use crate::worker::Checks;
1719

18-
#[allow(dead_code)]
1920
pub struct ScoreSupervisorAPIClient {
2021
supervisor_link: monitor_rs::Monitor<Checks>,
2122
}
2223

2324
unsafe impl Send for ScoreSupervisorAPIClient {} // Just assuming it's safe to send across threads, this is a temporary solution
2425

25-
#[allow(dead_code)]
2626
impl ScoreSupervisorAPIClient {
2727
pub fn new() -> Self {
2828
let value = std::env::var("IDENTIFIER").expect("IDENTIFIER env not set");

src/health_monitoring_lib/rust/supervisor_api_client/stub_supervisor_api_client.rs

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

14+
#![allow(dead_code)]
15+
1416
use crate::log::warn;
1517
use crate::supervisor_api_client::SupervisorAPIClient;
1618

1719
/// A stub implementation of the SupervisorAPIClient that logs alive notifications.
18-
#[allow(dead_code)]
1920
pub struct StubSupervisorAPIClient;
2021

2122
impl StubSupervisorAPIClient {
@@ -24,7 +25,6 @@ impl StubSupervisorAPIClient {
2425
}
2526
}
2627

27-
#[allow(dead_code)]
2828
impl SupervisorAPIClient for StubSupervisorAPIClient {
2929
fn notify_alive(&self) {
3030
warn!("StubSupervisorAPIClient: notify_alive called");

src/health_monitoring_lib/rust/worker.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ use crate::common::{MonitorEvalHandle, MonitorEvaluator};
1414
use crate::log::{info, warn};
1515
use crate::supervisor_api_client::SupervisorAPIClient;
1616
use containers::fixed_capacity::FixedCapacityVec;
17+
use core::sync::atomic::{AtomicBool, Ordering};
1718
use core::time::Duration;
19+
use std::sync::Arc;
20+
use std::time::Instant;
1821

1922
pub(super) struct MonitoringLogic<T: SupervisorAPIClient> {
2023
monitors: FixedCapacityVec<MonitorEvalHandle>,
2124
client: T,
22-
last_notification: std::time::Instant,
25+
last_notification: Instant,
2326
supervisor_api_cycle: Duration,
2427
}
2528

@@ -38,7 +41,7 @@ impl<T: SupervisorAPIClient> MonitoringLogic<T> {
3841
monitors,
3942
client,
4043
supervisor_api_cycle,
41-
last_notification: std::time::Instant::now(),
44+
last_notification: Instant::now(),
4245
}
4346
}
4447

@@ -55,7 +58,7 @@ impl<T: SupervisorAPIClient> MonitoringLogic<T> {
5558

5659
if !has_any_error {
5760
if self.last_notification.elapsed() > self.supervisor_api_cycle {
58-
self.last_notification = std::time::Instant::now();
61+
self.last_notification = Instant::now();
5962
self.client.notify_alive();
6063
}
6164
} else {
@@ -70,15 +73,15 @@ impl<T: SupervisorAPIClient> MonitoringLogic<T> {
7073
/// A struct that manages a unique thread for running monitoring logic periodically.
7174
pub struct UniqueThreadRunner {
7275
handle: Option<std::thread::JoinHandle<()>>,
73-
should_stop: std::sync::Arc<core::sync::atomic::AtomicBool>,
76+
should_stop: Arc<AtomicBool>,
7477
internal_duration_cycle: Duration,
7578
}
7679

7780
impl UniqueThreadRunner {
7881
pub(super) fn new(internal_duration_cycle: Duration) -> Self {
7982
Self {
8083
handle: None,
81-
should_stop: std::sync::Arc::new(core::sync::atomic::AtomicBool::new(false)),
84+
should_stop: Arc::new(AtomicBool::new(false)),
8285
internal_duration_cycle,
8386
}
8487
}
@@ -96,10 +99,10 @@ impl UniqueThreadRunner {
9699
let mut next_sleep_time = interval;
97100

98101
// TODO Add some checks and log if cyclicly here is not met.
99-
while !should_stop.load(core::sync::atomic::Ordering::Relaxed) {
102+
while !should_stop.load(Ordering::Relaxed) {
100103
std::thread::sleep(next_sleep_time);
101104

102-
let now = std::time::Instant::now();
105+
let now = Instant::now();
103106

104107
if !monitoring_logic.run() {
105108
info!("Monitoring logic failed, stopping thread.");
@@ -115,7 +118,7 @@ impl UniqueThreadRunner {
115118
}
116119

117120
pub fn join(&mut self) {
118-
self.should_stop.store(true, core::sync::atomic::Ordering::Relaxed);
121+
self.should_stop.store(true, Ordering::Relaxed);
119122
if let Some(handle) = self.handle.take() {
120123
let _ = handle.join();
121124
}
@@ -152,28 +155,30 @@ mod tests {
152155
use crate::worker::{MonitoringLogic, UniqueThreadRunner};
153156
use crate::TimeRange;
154157
use containers::fixed_capacity::FixedCapacityVec;
158+
use core::sync::atomic::{AtomicUsize, Ordering};
155159
use core::time::Duration;
160+
use std::sync::Arc;
156161

157162
#[derive(Clone)]
158163
struct MockSupervisorAPIClient {
159-
pub notify_called: std::sync::Arc<core::sync::atomic::AtomicUsize>,
164+
pub notify_called: Arc<AtomicUsize>,
160165
}
161166

162167
impl MockSupervisorAPIClient {
163168
pub fn new() -> Self {
164169
Self {
165-
notify_called: std::sync::Arc::new(core::sync::atomic::AtomicUsize::new(0)),
170+
notify_called: Arc::new(AtomicUsize::new(0)),
166171
}
167172
}
168173

169174
fn get_notify_count(&self) -> usize {
170-
self.notify_called.load(core::sync::atomic::Ordering::Acquire)
175+
self.notify_called.load(Ordering::Acquire)
171176
}
172177
}
173178

174179
impl SupervisorAPIClient for MockSupervisorAPIClient {
175180
fn notify_alive(&self) {
176-
self.notify_called.fetch_add(1, core::sync::atomic::Ordering::AcqRel);
181+
self.notify_called.fetch_add(1, Ordering::AcqRel);
177182
}
178183
}
179184

0 commit comments

Comments
 (0)