Skip to main content

rustfs_notify/
runtime_view.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{Event, NotificationTargetMetricSnapshot, notifier::SharedNotifyTargetList};
16use rustfs_targets::{ReplayWorkerManager, RuntimeTargetHealthSnapshot, SharedTarget, arn::TargetID};
17use std::sync::Arc;
18use tokio::sync::RwLock;
19
20#[derive(Clone)]
21pub struct NotifyRuntimeView {
22    target_list: SharedNotifyTargetList,
23    stream_cancellers: Arc<RwLock<ReplayWorkerManager>>,
24}
25
26impl NotifyRuntimeView {
27    pub fn new(target_list: SharedNotifyTargetList, stream_cancellers: Arc<RwLock<ReplayWorkerManager>>) -> Self {
28        Self {
29            target_list,
30            stream_cancellers,
31        }
32    }
33
34    pub async fn get_active_targets(&self) -> Vec<TargetID> {
35        self.target_list.read().await.keys()
36    }
37
38    pub fn get_all_targets(&self) -> SharedNotifyTargetList {
39        self.target_list.clone()
40    }
41
42    pub async fn get_target_values(&self) -> Vec<SharedTarget<Event>> {
43        self.target_list.read().await.values()
44    }
45
46    pub async fn snapshot_target_metrics(&self) -> Vec<NotificationTargetMetricSnapshot> {
47        self.target_list
48            .read()
49            .await
50            .runtime_snapshots()
51            .into_iter()
52            .map(|snapshot| NotificationTargetMetricSnapshot {
53                failed_messages: snapshot.failed_messages,
54                failed_store_length: snapshot.failed_store_length,
55                queue_length: snapshot.queue_length,
56                target_id: snapshot.target_id,
57                target_type: snapshot.target_type,
58                total_messages: snapshot.total_messages,
59            })
60            .collect()
61    }
62
63    pub async fn snapshot_target_health(&self) -> Vec<RuntimeTargetHealthSnapshot> {
64        let targets = self.target_list.read().await.values();
65        rustfs_targets::health_snapshots_for_targets(targets).await
66    }
67
68    pub async fn runtime_status_snapshot(&self) -> rustfs_targets::RuntimeStatusSnapshot {
69        let replay_workers = self.stream_cancellers.read().await;
70        let target_list = self.target_list.read().await;
71        target_list.runtime_status_snapshot(&replay_workers)
72    }
73}
74
75#[cfg(test)]
76mod tests {
77    use super::NotifyRuntimeView;
78    use crate::{Event, notifier::TargetList};
79    use rustfs_targets::arn::TargetID;
80    use rustfs_targets::target::TargetDeliverySnapshot;
81    use rustfs_targets::testkit::MockTarget;
82    use rustfs_targets::{ReplayWorkerManager, Target};
83    use std::sync::Arc;
84    use tokio::sync::{Notify, RwLock};
85
86    #[tokio::test]
87    async fn runtime_view_reports_empty_runtime_queries() {
88        let runtime_view = NotifyRuntimeView::new(
89            Arc::new(RwLock::new(TargetList::new())),
90            Arc::new(RwLock::new(ReplayWorkerManager::new())),
91        );
92
93        assert!(runtime_view.get_active_targets().await.is_empty());
94        assert!(runtime_view.get_target_values().await.is_empty());
95        assert!(runtime_view.get_all_targets().read().await.is_empty());
96    }
97
98    #[tokio::test]
99    async fn runtime_view_reports_empty_runtime_snapshots() {
100        let runtime_view = NotifyRuntimeView::new(
101            Arc::new(RwLock::new(TargetList::new())),
102            Arc::new(RwLock::new(ReplayWorkerManager::new())),
103        );
104
105        assert!(runtime_view.snapshot_target_metrics().await.is_empty());
106        assert!(runtime_view.snapshot_target_health().await.is_empty());
107
108        let status = runtime_view.runtime_status_snapshot().await;
109        assert_eq!(status.target_count, 0);
110        assert_eq!(status.replay_worker_count, 0);
111    }
112
113    #[tokio::test]
114    async fn runtime_view_reports_non_empty_runtime_queries_and_snapshots() {
115        let target_list = Arc::new(RwLock::new(TargetList::new()));
116        let replay_workers = Arc::new(RwLock::new(ReplayWorkerManager::new()));
117
118        let online = Arc::new(MockTarget::new("primary", "webhook").with_delivery_snapshot(TargetDeliverySnapshot {
119            failed_messages: 1,
120            failed_store_length: 7,
121            queue_length: 0,
122            total_messages: 3,
123        }));
124
125        let disabled = Arc::new(
126            MockTarget::new("backup", "mqtt")
127                .disabled()
128                .with_active(false)
129                .with_delivery_snapshot(TargetDeliverySnapshot {
130                    total_messages: 2,
131                    ..TargetDeliverySnapshot::default()
132                }),
133        );
134
135        {
136            let mut targets = target_list.write().await;
137            targets.add(online.clone() as Arc<dyn Target<Event> + Send + Sync>).unwrap();
138            targets.add(disabled.clone() as Arc<dyn Target<Event> + Send + Sync>).unwrap();
139        }
140
141        let runtime_view = NotifyRuntimeView::new(target_list.clone(), replay_workers.clone());
142
143        let mut active_targets = runtime_view.get_active_targets().await;
144        active_targets.sort();
145        assert_eq!(
146            active_targets,
147            vec![
148                TargetID::new("backup".to_string(), "mqtt".to_string()),
149                TargetID::new("primary".to_string(), "webhook".to_string())
150            ]
151        );
152
153        let target_values = runtime_view.get_target_values().await;
154        assert_eq!(target_values.len(), 2);
155        assert_eq!(runtime_view.get_all_targets().read().await.len(), 2);
156
157        let metric_snapshots = runtime_view.snapshot_target_metrics().await;
158        assert_eq!(metric_snapshots.len(), 2);
159        assert_eq!(metric_snapshots[0].target_id, "backup:mqtt");
160        assert_eq!(metric_snapshots[0].failed_messages, 0);
161        assert_eq!(metric_snapshots[0].failed_store_length, 0);
162        assert_eq!(metric_snapshots[0].total_messages, 2);
163        assert_eq!(metric_snapshots[1].target_id, "primary:webhook");
164        assert_eq!(metric_snapshots[1].failed_messages, 1);
165        assert_eq!(metric_snapshots[1].failed_store_length, 7);
166        assert_eq!(metric_snapshots[1].total_messages, 3);
167
168        let health_snapshots = runtime_view.snapshot_target_health().await;
169        assert_eq!(health_snapshots.len(), 2);
170        assert_eq!(health_snapshots[0].target_id, "backup:mqtt");
171        assert!(!health_snapshots[0].enabled);
172        assert_eq!(health_snapshots[0].state, rustfs_targets::RuntimeTargetHealthState::Disabled);
173        assert_eq!(health_snapshots[1].target_id, "primary:webhook");
174        assert!(health_snapshots[1].enabled);
175        assert_eq!(health_snapshots[1].state, rustfs_targets::RuntimeTargetHealthState::Online);
176
177        let status = runtime_view.runtime_status_snapshot().await;
178        assert_eq!(status.target_count, 2);
179        assert_eq!(status.replay_worker_count, 0);
180    }
181
182    #[tokio::test]
183    async fn health_probe_does_not_hold_the_target_list_read_lock() {
184        let target_list = Arc::new(RwLock::new(TargetList::new()));
185        let release = Arc::new(Notify::new());
186        let target = MockTarget::new("blocked", "webhook").with_health_gate(release.clone());
187        let started = target.health_started();
188        target_list
189            .write()
190            .await
191            .add(Arc::new(target) as Arc<dyn Target<Event> + Send + Sync>)
192            .expect("test target should be added");
193        let runtime_view = NotifyRuntimeView::new(target_list.clone(), Arc::new(RwLock::new(ReplayWorkerManager::new())));
194
195        let snapshot_task = tokio::spawn(async move { runtime_view.snapshot_target_health().await });
196        started.notified().await;
197
198        let write_guard = tokio::time::timeout(std::time::Duration::from_secs(1), target_list.write())
199            .await
200            .expect("network health probe must not retain the target-list read lock");
201        drop(write_guard);
202        release.notify_one();
203
204        assert_eq!(snapshot_task.await.expect("snapshot task should finish").len(), 1);
205    }
206}