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
// This file is part of Substrate.

// Copyright (C) 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 clap::Args;
use sc_client_db::DatabaseSource;
use sp_core::traits::SpawnEssentialNamed;
use std::{
	io,
	path::{Path, PathBuf},
	time::Duration,
};

const LOG_TARGET: &str = "storage-monitor";

/// Result type used in this crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Error type used in this crate.
#[derive(Debug, thiserror::Error)]
pub enum Error {
	#[error("IO Error")]
	IOError(#[from] io::Error),
	#[error("Out of storage space: available {0}MiB, required {1}MiB")]
	StorageOutOfSpace(u64, u64),
}

/// Parameters used to create the storage monitor.
#[derive(Default, Debug, Clone, Args)]
pub struct StorageMonitorParams {
	/// Required available space on database storage. If available space for DB storage drops below
	/// the given threshold, node will be gracefully terminated. If `0` is given monitoring will be
	/// disabled.
	#[arg(long = "db-storage-threshold", value_name = "MiB", default_value_t = 1024)]
	pub threshold: u64,

	/// How often available space is polled.
	#[arg(long = "db-storage-polling-period", value_name = "SECONDS", default_value_t = 5, value_parser = clap::value_parser!(u32).range(1..))]
	pub polling_period: u32,
}

/// Storage monitor service: checks the available space for the filesystem for given path.
pub struct StorageMonitorService {
	/// watched path
	path: PathBuf,
	/// number of megabytes that shall be free on the filesystem for watched path
	threshold: u64,
	/// storage space polling period
	polling_period: Duration,
}

impl StorageMonitorService {
	/// Creates new StorageMonitorService for given client config
	pub fn try_spawn(
		parameters: StorageMonitorParams,
		database: DatabaseSource,
		spawner: &impl SpawnEssentialNamed,
	) -> Result<()> {
		Ok(match (parameters.threshold, database.path()) {
			(0, _) => {
				log::info!(
					target: LOG_TARGET,
					"StorageMonitorService: threshold `0` given, storage monitoring disabled",
				);
			},
			(_, None) => {
				log::warn!(
					target: LOG_TARGET,
					"StorageMonitorService: no database path to observe",
				);
			},
			(threshold, Some(path)) => {
				log::debug!(
					target: LOG_TARGET,
					"Initializing StorageMonitorService for db path: {path:?}",
				);

				Self::check_free_space(&path, threshold)?;

				let storage_monitor_service = StorageMonitorService {
					path: path.to_path_buf(),
					threshold,
					polling_period: Duration::from_secs(parameters.polling_period.into()),
				};

				spawner.spawn_essential(
					"storage-monitor",
					None,
					Box::pin(storage_monitor_service.run()),
				);
			},
		})
	}

	/// Main monitoring loop, intended to be spawned as essential task. Quits if free space drop
	/// below threshold.
	async fn run(self) {
		loop {
			tokio::time::sleep(self.polling_period).await;
			if Self::check_free_space(&self.path, self.threshold).is_err() {
				break
			};
		}
	}

	/// Returns free space in MiB, or error if statvfs failed.
	fn free_space(path: &Path) -> Result<u64> {
		Ok(fs4::available_space(path).map(|s| s / 1024 / 1024)?)
	}

	/// Checks if the amount of free space for given `path` is above given `threshold` in MiB.
	/// If it dropped below, error is returned.
	/// System errors are silently ignored.
	fn check_free_space(path: &Path, threshold: u64) -> Result<()> {
		match StorageMonitorService::free_space(path) {
			Ok(available_space) => {
				log::trace!(
					target: LOG_TARGET,
					"free: {available_space} , threshold: {threshold}.",
				);

				if available_space < threshold {
					log::error!(target: LOG_TARGET, "Available space {available_space}MiB for path `{}` dropped below threshold: {threshold}MiB , terminating...", path.display());
					Err(Error::StorageOutOfSpace(available_space, threshold))
				} else {
					Ok(())
				}
			},
			Err(e) => {
				log::error!(target: LOG_TARGET, "Could not read available space: {e:?}.");
				Err(e)
			},
		}
	}
}