This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathstorage_monitor.rs
More file actions
153 lines (136 loc) · 4.51 KB
/
storage_monitor.rs
File metadata and controls
153 lines (136 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// This file is part of Substrate.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::{config::Configuration, error::Error};
use futures::StreamExt;
use nix::{errno::Errno, sys::statvfs::statvfs};
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver};
use std::{
path::{Path, PathBuf},
time::{Duration, Instant},
};
const LOG_TARGET: &str = "storage-monitor";
const THROTTLE_PERIOD: std::time::Duration = Duration::from_secs(2);
pub struct StorageMonitorService {
/// watched path
path: PathBuf,
/// notify's events receiver
stream: TracingUnboundedReceiver<Result<Event, notify::Error>>,
/// number of bytes that shall be free and available on the filesystem for watched path
threshold: u64,
/// timestamp of the most recent check
recent_check: Instant,
/// keeps the ref for file system watcher
_watcher: RecommendedWatcher,
}
impl StorageMonitorService {
/// creates new StorageMonitorService for given client config
pub fn new_for_config(config: &Configuration) -> Result<Option<Self>, Error> {
Ok(match (config.available_storage_threshold, config.database.path()) {
(0, _) => {
log::info!(
target: LOG_TARGET,
"StorageMonitorService: threshold 0 given, storage monitoring disabled",
);
None
},
(_, None) => {
log::warn!(
target: LOG_TARGET,
"StorageMonitorService: no database path to observe",
);
None
},
(threshold, Some(path)) => {
let (sink, stream) = tracing_unbounded("mpsc_storage_monitor", 1024);
let mut watcher = RecommendedWatcher::new(
move |res| {
sink.unbounded_send(res).unwrap();
},
Config::default(),
)
.map_err(|e| Error::Other(format!("Could not create fs watcher {e}")))?;
watcher
.watch(path.as_ref(), RecursiveMode::Recursive)
.map_err(|e| Error::Other(format!("Could not start fs watcher {e}")))?;
log::debug!(
target: LOG_TARGET,
"Initializing StorageMonitorService for db path: {:?}",
path,
);
Self::check_free_space(&path, threshold)?;
Some(StorageMonitorService {
path: path.to_path_buf(),
stream,
threshold,
recent_check: Instant::now(),
_watcher: watcher,
})
},
})
}
/// main monitoring loop, intended to be spawn as essential task
pub async fn run(mut self) {
while let Some(watch_event) = self.stream.next().await {
match watch_event {
Ok(_) =>
if self.recent_check.elapsed() >= THROTTLE_PERIOD {
self.recent_check = Instant::now();
if Self::check_free_space(&self.path, self.threshold).is_err() {
break
}
},
Err(e) => {
log::error!(target: LOG_TARGET, "watch error: {:?}", e);
},
}
}
}
// returns free space in MB
fn free_space(path: &Path) -> Result<u64, Errno> {
let fs_stats = statvfs(path);
fs_stats.map(|stats| stats.blocks_available() * stats.block_size() / 1_000_000)
}
// Checks if the amount of free space for given `path` is below given `threshold`.
// If it dropped below, error is returned.
// System errors are silently ignored.
fn check_free_space(path: &Path, threshold: u64) -> Result<(), Error> {
match StorageMonitorService::free_space(path.as_ref()) {
Ok(available_space) => {
log::trace!(
target: LOG_TARGET,
"free:{:?} , threshold:{:?}",
available_space,
threshold,
);
if available_space < threshold {
let msg = format!(
"Available space {:?}MB for path {:?} dropped below threshold:{:?}MB , terminating...",
available_space,
path,
threshold,
);
log::error!(target: LOG_TARGET, "{}", msg);
Err(Error::Other(msg))
} else {
Ok(())
}
},
Err(e) => {
log::warn!(target: LOG_TARGET, "could not read available space: {:?}", e);
Ok(())
},
}
}
}