spetz commented on code in PR #2452:
URL: https://github.com/apache/iggy/pull/2452#discussion_r2617444004
##########
core/server/src/log/logger.rs:
##########
@@ -417,6 +457,160 @@ impl Logging {
)
}
}
+
+ fn calculate_max_files(max_total_size_bytes: u64, max_file_size_bytes:
u64) -> usize {
+ if max_file_size_bytes == 0 {
+ return 10;
+ }
+
+ let max_files = max_total_size_bytes / max_file_size_bytes;
+ max_files.clamp(1, 1000) as usize
+ }
+
+ // Use a mutex lock to ensure log rotation operations do not produce race
conditions.
+ fn _install_log_rotation_handler(&self, config: &LoggingConfig, logs_path:
Option<&PathBuf>) {
+ if let Some(logs_path) = logs_path {
+ let path = logs_path.to_path_buf();
+ let max_size = config.max_size.as_bytes_u64();
+ let retention = config.retention.get_duration();
+ let rotation_mutex = Arc::new(Mutex::new(()));
+ let rotation_mutex_clone = Arc::clone(&rotation_mutex);
+ std::thread::spawn(move || {
+ loop {
+ std::thread::sleep(Duration::from_secs(3600));
+ match rotation_mutex_clone.lock() {
+ Ok(_guard) => {
+ Self::cleanup_log_files(&path, retention,
max_size);
+ }
+ Err(e) => {
+ tracing::warn!("Failed to acquire log rotation
lock: {:?}", e);
+ }
+ }
+ }
+ });
+ }
+ }
+
+ fn cleanup_log_files(logs_path: &PathBuf, retention: Duration,
max_size_bytes: u64) {
+ use std::fs;
+ use std::time::{SystemTime, UNIX_EPOCH};
+
+ tracing::debug!(
+ "Starting log cleanup for directory: {:?}, retention: {:?},
max_size: {} bytes",
+ logs_path,
+ retention,
+ max_size_bytes
+ );
+
+ let entries = match fs::read_dir(logs_path) {
+ Ok(entries) => entries,
+ Err(e) => {
+ tracing::warn!("Failed to read log directory {:?}: {}",
logs_path, e);
+ return;
+ }
+ };
+
+ let mut file_entries = Vec::new();
+
+ for entry in entries.flatten() {
+ let metadata = match entry.metadata() {
Review Comment:
Here and the few lines below, also the `let Ok(...) = {}` construct could be
used, but w/e seems more readable :)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]