use rustfs_config::observability::{
DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM, DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM_GZIP,
DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM_ZSTD, DEFAULT_OBS_LOG_GZIP_COMPRESSION_ALL_EXTENSION,
DEFAULT_OBS_LOG_GZIP_COMPRESSION_EXTENSION, DEFAULT_OBS_LOG_MATCH_MODE, DEFAULT_OBS_LOG_MATCH_MODE_PREFIX,
DEFAULT_OBS_LOG_ZSTD_COMPRESSION_ALL_EXTENSION, DEFAULT_OBS_LOG_ZSTD_COMPRESSION_EXTENSION,
};
use std::fmt;
use std::path::PathBuf;
use std::time::SystemTime;
use tracing::warn;
const LOG_COMPONENT_OBS: &str = "obs";
const LOG_SUBSYSTEM_LOG_CLEANER: &str = "log_cleaner";
const EVENT_LOG_CLEANER_STATE: &str = "log_cleaner_state";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileMatchMode {
Prefix,
Suffix,
}
impl FileMatchMode {
pub fn as_str(&self) -> &'static str {
match self {
FileMatchMode::Prefix => DEFAULT_OBS_LOG_MATCH_MODE_PREFIX,
FileMatchMode::Suffix => DEFAULT_OBS_LOG_MATCH_MODE,
}
}
pub fn from_config_str(value: &str) -> Self {
let trimmed = value.trim();
if trimmed.eq_ignore_ascii_case(DEFAULT_OBS_LOG_MATCH_MODE_PREFIX) {
Self::Prefix
} else if trimmed.eq_ignore_ascii_case(DEFAULT_OBS_LOG_MATCH_MODE) {
Self::Suffix
} else {
if !trimmed.is_empty() {
warn!(
event = EVENT_LOG_CLEANER_STATE,
component = LOG_COMPONENT_OBS,
subsystem = LOG_SUBSYSTEM_LOG_CLEANER,
result = "unknown_match_mode",
value = %value,
fallback = DEFAULT_OBS_LOG_MATCH_MODE,
"log cleaner state changed"
);
}
Self::Suffix
}
}
}
impl fmt::Display for FileMatchMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompressionAlgorithm {
Gzip,
Zstd,
}
impl CompressionAlgorithm {
fn parse_normalized(value: &str) -> Option<Self> {
if value == DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM_GZIP || value == DEFAULT_OBS_LOG_GZIP_COMPRESSION_EXTENSION {
Some(Self::Gzip)
} else if value == DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM_ZSTD || value == DEFAULT_OBS_LOG_ZSTD_COMPRESSION_EXTENSION {
Some(Self::Zstd)
} else {
None
}
}
pub fn from_config_str(value: &str) -> Self {
let normalized = value.trim().to_ascii_lowercase();
match Self::parse_normalized(&normalized) {
Some(algorithm) => algorithm,
None => {
let fallback = Self::default();
if !normalized.is_empty() {
warn!(
event = EVENT_LOG_CLEANER_STATE,
component = LOG_COMPONENT_OBS,
subsystem = LOG_SUBSYSTEM_LOG_CLEANER,
result = "unknown_compression_algorithm",
value = %value,
fallback = %fallback,
"log cleaner state changed"
);
}
fallback
}
}
}
pub fn extension(self) -> &'static str {
match self {
Self::Gzip => DEFAULT_OBS_LOG_GZIP_COMPRESSION_ALL_EXTENSION.trim_start_matches('.'),
Self::Zstd => DEFAULT_OBS_LOG_ZSTD_COMPRESSION_ALL_EXTENSION.trim_start_matches('.'),
}
}
pub fn compressed_suffixes() -> [&'static str; 2] {
[
DEFAULT_OBS_LOG_GZIP_COMPRESSION_ALL_EXTENSION,
DEFAULT_OBS_LOG_ZSTD_COMPRESSION_ALL_EXTENSION,
]
}
pub fn as_str(self) -> &'static str {
match self {
Self::Gzip => DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM_GZIP,
Self::Zstd => DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM_ZSTD,
}
}
}
impl std::str::FromStr for CompressionAlgorithm {
type Err = &'static str;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let normalized = value.trim().to_ascii_lowercase();
Self::parse_normalized(&normalized).ok_or("invalid compression algorithm")
}
}
impl Default for CompressionAlgorithm {
fn default() -> Self {
Self::from_config_str(DEFAULT_OBS_LOG_COMPRESSION_ALGORITHM)
}
}
impl fmt::Display for CompressionAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
pub fn default_parallel_workers() -> usize {
num_cpus::get().clamp(1, 8)
}
#[derive(Debug, Clone)]
pub(super) struct FileInfo {
pub path: PathBuf,
pub size: u64,
pub projected_freed_bytes: u64,
pub modified: SystemTime,
}
#[cfg(test)]
mod tests {
use super::CompressionAlgorithm;
#[test]
fn compression_algorithm_accepts_full_names_and_aliases() {
assert_eq!(CompressionAlgorithm::from_config_str("gzip"), CompressionAlgorithm::Gzip);
assert_eq!(CompressionAlgorithm::from_config_str("GZ"), CompressionAlgorithm::Gzip);
assert_eq!(CompressionAlgorithm::from_config_str("zstd"), CompressionAlgorithm::Zstd);
assert_eq!(CompressionAlgorithm::from_config_str(" zst "), CompressionAlgorithm::Zstd);
}
#[test]
fn compression_algorithm_defaults_or_errors_for_invalid_values() {
assert_eq!(CompressionAlgorithm::from_config_str("brotli"), CompressionAlgorithm::default());
assert!("brotli".parse::<CompressionAlgorithm>().is_err());
}
}