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

appender: refactor limit-by-size logic to remove the log index tracking #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adrianbenavides
Copy link

@adrianbenavides adrianbenavides commented Jul 9, 2024

Refactor the limit-by-size logic as described in this comment.

This function summarizes the new logic

Additionally, I've tried to remove unnecessary code to focus the PR on the new filter logic and make the reviewer's job easier.

tracing-appender/src/rolling.rs Outdated Show resolved Hide resolved
tracing-appender/src/rolling.rs Outdated Show resolved Hide resolved
@adrianbenavides adrianbenavides force-pushed the adrian/appender/limit-by-size branch 3 times, most recently from b8aa293 to 7a3c14a Compare July 10, 2024 09:21
@adrianbenavides
Copy link
Author

@CBenoit I pushed some changes adding your proposals 👍

Copy link
Owner

@CBenoit CBenoit left a comment

Choose a reason for hiding this comment

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

Thank you! I left a few last comments, I think it’s good to be merged otherwise 💯

tracing-appender/src/rolling.rs Outdated Show resolved Hide resolved
tracing-appender/src/rolling.rs Outdated Show resolved Hide resolved
tracing-appender/src/rolling.rs Outdated Show resolved Hide resolved
@adrianbenavides adrianbenavides force-pushed the adrian/appender/limit-by-size branch 2 times, most recently from 60cda8e to 7856ebc Compare July 16, 2024 06:57
@adrianbenavides
Copy link
Author

Ready! Thanks a lot @CBenoit for the review 🙏

Copy link
Owner

@CBenoit CBenoit left a comment

Choose a reason for hiding this comment

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

Thank you too!

I’m sorry for the back and forth, upon reviewing again, I noticed a few other places I want to comment on. The diff wasn’t super obvious at first.

Comment on lines 230 to 244
let should_rollover = |now: OffsetDateTime| -> bool {
if let Some(current_time) = self.state.should_rollover_by_date(now) {
let did_cas = self.state.advance_date(now, current_time);
debug_assert!(did_cas, "if we have &mut access to the appender, no other thread can have advanced the timestamp...");
return true;
}
if self.state.should_rollover_by_size() {
self.state.advance_same_date_filename_index();
return true;
}
false
};

let now = self.now();
let should_rollover = should_rollover(now);
Copy link
Owner

@CBenoit CBenoit Jul 16, 2024

Choose a reason for hiding this comment

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

style: It looks like we don’t need to use a closure at this place. This is making the code less readable in my opinion. We also don’t actually need to short-circuit the code using return because if-blocks are also expressions:

Suggested change
let should_rollover = |now: OffsetDateTime| -> bool {
if let Some(current_time) = self.state.should_rollover_by_date(now) {
let did_cas = self.state.advance_date(now, current_time);
debug_assert!(did_cas, "if we have &mut access to the appender, no other thread can have advanced the timestamp...");
return true;
}
if self.state.should_rollover_by_size() {
self.state.advance_same_date_filename_index();
return true;
}
false
};
let now = self.now();
let should_rollover = should_rollover(now);
let now = self.now();
let should_rollover = if let Some(current_time) = self.state.should_rollover_by_date(now) {
let did_cas = self.state.advance_date(now, current_time);
debug_assert!(did_cas, "if we have &mut access to the appender, no other thread can have advanced the timestamp...");
true
} else if self.state.should_rollover_by_size() {
self.state.advance_same_date_filename_index();
true
} else {
false
};

Copy link
Author

Choose a reason for hiding this comment

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

True, this is a leftover from a previous version where I needed the writer inside the should_rollover logic, and I added the closure to handle the writer lock.

Comment on lines 267 to 281
let should_rollover = |now: OffsetDateTime| -> bool {
if let Some(current_time) = self.state.should_rollover_by_date(now) {
// Did we get the right to lock the file? If not, another thread
// did it and we can just make a writer.
return self.state.advance_date(now, current_time);
}
if self.state.should_rollover_by_size() {
self.state.advance_same_date_filename_index();
return true;
}
false
};

let now = self.now();
if should_rollover(now) {
Copy link
Owner

@CBenoit CBenoit Jul 16, 2024

Choose a reason for hiding this comment

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

style: Similar remark here

Comment on lines 410 to 394
/// This will result in a log file located at `/some/path/rolling.log.yyyy-MM-dd`.
/// This will result in a log file located at `/some/path/rolling.log.yyyy-MM-dd-HH`.
pub fn daily(
Copy link
Owner

@CBenoit CBenoit Jul 16, 2024

Choose a reason for hiding this comment

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

issue: Wait, I don’t think daily is supposed to produce files with hours in the name… Did we accidentally changed the behavior?

Copy link
Author

Choose a reason for hiding this comment

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

My bad, not sure why I added that change there.

In any case, I've just double-checked and it all works as expected, nothing changed in this regard.

Comment on lines 532 to 481
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
#[derive(Clone, Eq, PartialEq, Debug)]
Copy link
Owner

@CBenoit CBenoit Jul 16, 2024

Choose a reason for hiding this comment

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

nitpick: Not a big deal, but this kind of enum should implement Copy

Comment on lines 684 to 685
let created = metadata.created().ok()?;
Some((entry, created))
Copy link
Owner

@CBenoit CBenoit Jul 16, 2024

Choose a reason for hiding this comment

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

issue: It’s better to use the date parsed from the filename, because the created date in the metadata of the file could be different and cause strange behaviors. From instance, when copying files around for, e.g.: backups. Some backup tools may preserve the created date, but definitely not all tools are doing so. We would start deleting old files in a pseudo-random order (the order in which they were transferred and re-created). My suggestion is to keep parsing the filename for the date instead, and sort accordingly. This is the least surprising behavior I think.

Copy link
Author

Choose a reason for hiding this comment

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

Got it, now I see why you made that change in this function. I'll add your code back.

Copy link
Author

Choose a reason for hiding this comment

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

Done, I think I got it right. In the current implementation we shouldn't need the metadata.created() for anything right? Or would you use it as a fallback in case we can't parse the filename as a date? I don't think this case will ever happen, as we use the same date format that was used to create the log file in the first place.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, at least my code was completely ignoring files not following the format, considering them as not managed by the logger. I think that’s the appropriate behavior.

@CBenoit
Copy link
Owner

CBenoit commented Jul 17, 2024

Thank you! Since there are many changes, I’ll try this patch locally before merging, but things are looking good🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants