hubcio commented on code in PR #2452:
URL: https://github.com/apache/iggy/pull/2452#discussion_r2618540335
##########
core/server/src/log/logger.rs:
##########
@@ -253,8 +255,48 @@ impl Logging {
let base_directory = PathBuf::from(base_directory);
let logs_subdirectory = PathBuf::from(config.path.clone());
let logs_path = base_directory.join(logs_subdirectory.clone());
- let file_appender =
- tracing_appender::rolling::hourly(logs_path.clone(),
IGGY_LOG_FILE_PREFIX);
+
+ if let Err(e) = std::fs::create_dir_all(&logs_path) {
+ tracing::warn!("Failed to create logs directory {:?}: {}",
logs_path, e);
+ return Err(LogError::FileReloadFailure);
+ }
+
+ // Check available disk space (at least 10MB)
+ let min_disk_space: u64 = 10 * 1024 * 1024; // 10MB
+ if let Ok(available_space) = fs2::available_space(&logs_path) {
+ if available_space < min_disk_space {
+ tracing::warn!(
+ "Low disk space for logs. Available: {} bytes,
Recommended: {} bytes",
+ available_space,
+ min_disk_space
+ );
+ }
+ } else {
+ tracing::warn!(
+ "Failed to check available disk space for logs directory:
{:?}",
+ logs_path
+ );
+ }
+
+ let max_files = Self::calculate_max_files(
+ config.max_size.as_bytes_u64(),
+ config.max_size.as_bytes_u64(),
Review Comment:
you pass same value twice to this function, should it be max total size?
##########
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;
+ }
Review Comment:
either filter file_entries after retention cleanup or use retain(),
otherwise you use same iterator that has stale entries
##########
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(()));
Review Comment:
i don't understand what's the point of rotation mutex, only one thread is
accessing `cleanup_log_files`
##########
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 || {
Review Comment:
this thread never terminates, please implement graceful shutdown
--
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]