Skip to content

Round initialTime in RollingFileManager #3872

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

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

kelunik
Copy link

@kelunik kelunik commented Aug 6, 2025

Caching of filesystem timestamps (at least in ext4 on Linux) results in non-accurate creation timestamps.

Thus, let's round it to the second.

Rounding is also applied for the lastModified time, but I'm not sure whether it's needed there.

Fixes #3068.

✅ Required checks

🧪 Tests (select one)

  • I have added or updated tests to cover my changes.
  • No additional tests are needed for this change.

📝 Changelog (select one)

  • I added a changelog entry in src/changelog/.2.x.x. (See Changelog Entry File Guide).
  • This is a trivial change and does not require a changelog entry.

@vy
Copy link
Member

vy commented Aug 9, 2025

While @ppkarwasz is the main figure stewarding #3068 and this one, I'd appreciate one or more new tests and a changelog entry.

@kelunik
Copy link
Author

kelunik commented Aug 9, 2025

@vy If you have an idea on how to test this properly, I'm happy to write a test.

Copy link

github-actions bot commented Aug 9, 2025

Job Requested goals Build Tool Version Build Outcome Build Scan®
build-macos-latest clean install 3.9.8 Build Scan PUBLISHED
build-ubuntu-latest clean install 3.9.8 Build Scan PUBLISHED
build-windows-latest clean install 3.9.8 Build Scan PUBLISHED
Generated by gradle/develocity-actions

@vy
Copy link
Member

vy commented Aug 9, 2025

If you have an idea on how to test this properly, I'm happy to write a test.

@kelunik, create a package-private initialFileTime(File,FileTime) that initialFileTime(File) delegates to, and write a unit test against initialFileTime(File,FileTime) verifying that the sub-second precision FileTime arguments are rounded as expected?

@kelunik
Copy link
Author

kelunik commented Aug 12, 2025

@vy I've added a test now in a similar way like you suggested. It tests the rounding now, but doesn't test the fact that initial time actually rounds. I've also modified the test in the second commit to actually call initialFileTime, which I had to also make package private.

Caching of filesystem timestamps (at least in ext4 on Linux) results in non-accurate creation timestamp.

Thus, let's round it to the second.

Rounding is also applied for the lastModified time, but I'm not sure whether it's needed there.

Fixes apache#3068.
@ppkarwasz
Copy link
Contributor

@kelunik,

Rounding the creation date from the filesystem might address the immediate issue, but I was wondering if it might be more robust to initialize the policy using the file’s last modification date instead:

  • If the rotation interval remains unchanged, the last modification time should still fall within the same rotation period as the creation timestamp, only further away from the interval boundaries. That means the small uncertainties caused by filesystem timestamp caching shouldn’t matter.
  • The JDK already falls back to the last modification date when it can’t determine the creation date, so this approach is both well-tested and consistent with existing behavior.

Would you see any downsides to this alternative?

@kelunik
Copy link
Author

kelunik commented Aug 18, 2025

Would you see any downsides to this alternative?

Yes, see #3068 (comment)

but I was wondering if it might be more robust to initialize the policy using the file’s last modification date instead

Additionally, relying on the creation date isn't a known issue, so no action needed here IMO.

@kelunik
Copy link
Author

kelunik commented Aug 18, 2025

@ppkarwasz Seems like the same caching also applies to the modification time, as I suspected, see https://stackoverflow.com/q/14392975/2373138.

@ppkarwasz
Copy link
Contributor

Would you see any downsides to this alternative?

Yes, see #3068 (comment)

To counteract this problem the RollingFileAppender could change programmatically the last modification date of the old log file.

My point here is that no Java API allows us to change the creation date, but with the last modification date we have full control.

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

Successfully merging this pull request may close these issues.

RollingFileAppender rotates files incorrectly on ext4 due to filesystem timestamp caching
3 participants