-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add size-based operational log rotation #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rapids-bot
merged 4 commits into
NVIDIA:main
from
ericevans-nv:feat/operational-log-rotation
Jul 25, 2026
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ebd5ac
feat: add size-based operational log rotation
ericevans-nv 47c0739
fix(logging): preserve backups during rotation
ericevans-nv c5d19bb
fix(logging): cap retained backup count
ericevans-nv ebcc9e2
fix(logging): use dotted rotation suffixes
ericevans-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Size-based file rotation for operational log sinks. | ||
|
|
||
| use std::fs::{self, File, OpenOptions}; | ||
| use std::io::{self, BufWriter, Write}; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| pub(crate) struct SizeRotatingFileWriter { | ||
| base_path: PathBuf, | ||
| file: Option<BufWriter<File>>, | ||
| current_size: u64, | ||
| max_file_size_bytes: u64, | ||
| retained_files: usize, | ||
| } | ||
|
|
||
| impl SizeRotatingFileWriter { | ||
| pub(crate) fn new( | ||
| base_path: PathBuf, | ||
| max_file_size_bytes: u64, | ||
| retained_files: usize, | ||
| ) -> io::Result<Self> { | ||
| create_parent_directory(&base_path)?; | ||
| let file = open_active_file(&base_path, false)?; | ||
| let current_size = file.get_ref().metadata()?.len(); | ||
|
|
||
| Ok(Self { | ||
| base_path, | ||
| file: Some(file), | ||
| current_size, | ||
| max_file_size_bytes, | ||
| retained_files, | ||
| }) | ||
| } | ||
|
|
||
| fn rotate_if_needed(&mut self, incoming_bytes: usize) -> io::Result<()> { | ||
| if self.current_size == 0 | ||
| || self.current_size.saturating_add(incoming_bytes as u64) <= self.max_file_size_bytes | ||
| { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| self.rotate() | ||
| } | ||
|
|
||
| fn rotate(&mut self) -> io::Result<()> { | ||
| let mut file = self | ||
| .file | ||
| .take() | ||
| .ok_or_else(|| io::Error::other("rotating log file is not open"))?; | ||
| if let Err(error) = file.flush() { | ||
| self.file = Some(file); | ||
| return Err(error); | ||
| } | ||
| drop(file); | ||
|
|
||
| if let Err(error) = rotate_files(&self.base_path, self.retained_files) { | ||
| return match self.reopen_after_failed_rotation() { | ||
| Ok(()) => Err(error), | ||
| Err(reopen_error) => Err(io::Error::new( | ||
| reopen_error.kind(), | ||
| format!( | ||
| "log rotation failed: {error}; failed to reopen active log file: \ | ||
| {reopen_error}" | ||
| ), | ||
| )), | ||
| }; | ||
| } | ||
|
|
||
| self.file = Some(open_active_file(&self.base_path, true)?); | ||
| self.current_size = 0; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn reopen_after_failed_rotation(&mut self) -> io::Result<()> { | ||
| let file = open_active_file(&self.base_path, false)?; | ||
| self.current_size = file.get_ref().metadata()?.len(); | ||
| self.file = Some(file); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| impl Write for SizeRotatingFileWriter { | ||
| fn write(&mut self, buffer: &[u8]) -> io::Result<usize> { | ||
| self.rotate_if_needed(buffer.len())?; | ||
| self.file | ||
| .as_mut() | ||
| .ok_or_else(|| io::Error::other("rotating log file is not open"))? | ||
| .write_all(buffer)?; | ||
| self.current_size = self.current_size.saturating_add(buffer.len() as u64); | ||
| Ok(buffer.len()) | ||
| } | ||
|
|
||
| fn flush(&mut self) -> io::Result<()> { | ||
| self.file | ||
| .as_mut() | ||
| .ok_or_else(|| io::Error::other("rotating log file is not open"))? | ||
| .flush() | ||
| } | ||
| } | ||
|
|
||
| fn create_parent_directory(path: &Path) -> io::Result<()> { | ||
| if let Some(parent) = path.parent() | ||
| && !parent.as_os_str().is_empty() | ||
| { | ||
| fs::create_dir_all(parent)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn open_active_file(path: &Path, truncate: bool) -> io::Result<BufWriter<File>> { | ||
| let file = OpenOptions::new() | ||
| .create(true) | ||
| .write(true) | ||
| .append(!truncate) | ||
| .truncate(truncate) | ||
| .open(path)?; | ||
| Ok(BufWriter::new(file)) | ||
| } | ||
|
|
||
| fn rotate_files(base_path: &Path, retained_files: usize) -> io::Result<()> { | ||
| for index in (1..=retained_files).rev() { | ||
| let source = if index == 1 { | ||
| base_path.to_path_buf() | ||
| } else { | ||
| rotated_log_path(base_path, index - 1) | ||
| }; | ||
| if !source.exists() { | ||
| continue; | ||
| } | ||
|
|
||
| let destination = rotated_log_path(base_path, index); | ||
| fs::rename(source, destination)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn rotated_log_path(base_path: &Path, index: usize) -> PathBuf { | ||
| let stem = base_path.file_stem().unwrap_or(base_path.as_os_str()); | ||
| let mut file_name = stem.to_os_string(); | ||
| file_name.push(format!("_{index}")); | ||
|
ericevans-nv marked this conversation as resolved.
Outdated
|
||
| if let Some(extension) = base_path.extension() { | ||
| file_name.push("."); | ||
| file_name.push(extension); | ||
| } | ||
| base_path.with_file_name(file_name) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.