spetz commented on code in PR #2452:
URL: https://github.com/apache/iggy/pull/2452#discussion_r2617445951
##########
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() {
+ Ok(metadata) => metadata,
+ Err(e) => {
+ tracing::warn!("Failed to get metadata for {:?}: {}",
entry.path(), e);
+ continue;
+ }
+ };
+
+ if !metadata.is_file() {
+ continue;
+ }
+
+ let modified = match metadata.modified() {
+ Ok(modified) => modified,
+ Err(e) => {
+ tracing::warn!(
+ "Failed to get modification time for {:?}: {}",
+ entry.path(),
+ e
+ );
+ continue;
+ }
+ };
+
+ let elapsed = match modified.duration_since(UNIX_EPOCH) {
+ Ok(elapsed) => elapsed,
+ Err(e) => {
+ tracing::warn!(
+ "Failed to calculate elapsed time for {:?}: {}",
+ entry.path(),
+ e
+ );
+ continue;
+ }
+ };
+
+ let file_size = metadata.len();
+ file_entries.push((entry, modified, elapsed, file_size));
+ }
+
+ tracing::debug!(
+ "Processed {} log files from directory: {:?}",
+ file_entries.len(),
+ logs_path
+ );
+
+ let mut removed_files_count = 0;
+
+ if !retention.is_zero() {
+ let cutoff = match SystemTime::now().duration_since(UNIX_EPOCH) {
+ Ok(now) => now - retention,
+ Err(e) => {
+ tracing::warn!("Failed to get current time: {}", e);
+ return;
+ }
+ };
+
+ for (entry, _, elapsed, _) in &file_entries {
+ if *elapsed < cutoff {
+ if let Err(e) = fs::remove_file(entry.path()) {
+ tracing::warn!("Failed to remove old log file {:?}:
{}", entry.path(), e);
+ } else {
+ tracing::debug!("Removed old log file: {:?}",
entry.path());
+ removed_files_count += 1;
+ }
+ }
+ }
+ }
+
+ if max_size_bytes > 0 {
Review Comment:
To avoid the nested logic, this condition could be swapped with `if
max_size_bytes == 0` + quick return from the function.
--
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]