Skip to content
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

Ignore not found error when rotating log file #4971

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions components/tikv_util/src/logger/file_log.rs
Expand Up @@ -2,7 +2,7 @@

use chrono::{DateTime, Duration, Utc};
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::io::{self, ErrorKind, Write};
use std::path::{Path, PathBuf};

/// Adds `Duration` to the initial date and time.
Expand Down Expand Up @@ -67,7 +67,11 @@ impl RotatingFileLogger {

// Note: renaming files while they're open only works on Linux and macOS.
let new_path = rotation_file_path_with_timestamp(&self.file_path, &Utc::now());
fs::rename(&self.file_path, new_path)?;
match fs::rename(&self.file_path, new_path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is better to use a failpoint to add a test here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea. Let me put one here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddontang failpoint 'file_log_rename' added.

Ok(_) => Ok(()),
Err(ref e) if e.kind() == ErrorKind::NotFound => Ok(()),
Err(e) => Err(e),
}?;
let new_file = open_log_file(&self.file_path)?;
self.update_rotation_time();
self.file = new_file;
Expand Down Expand Up @@ -162,7 +166,7 @@ mod tests {
let mut logger = RotatingFileLogger::new(&log_file, Duration::days(1)).unwrap();
// delete the log_file so rotation fails.
std::fs::remove_file(&log_file).unwrap();
logger.rotate().unwrap_err();
logger.rotate().unwrap();
// dropping the logger still should not panic.
drop(logger);
}
Expand Down