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

Add Executor Service To Delete Logs When log_daily Is Enabled #1754

Merged
merged 30 commits into from
Mar 1, 2024

Conversation

deleonenriqueta
Copy link
Contributor

@deleonenriqueta deleonenriqueta commented Feb 22, 2024

Fixes #1688

deleonenriqueta and others added 25 commits February 1, 2024 16:41
@codecov-commenter
Copy link

codecov-commenter commented Feb 22, 2024

Codecov Report

Attention: Patch coverage is 91.48936% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 70.85%. Comparing base (d49c2ca) to head (cdec94a).
Report is 13 commits behind head on main.

Files Patch % Lines
...wrelic/agent/logging/ClearExpiredLogsRunnable.java 87.09% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1754      +/-   ##
============================================
+ Coverage     70.81%   70.85%   +0.04%     
+ Complexity     9954     9946       -8     
============================================
  Files           827      826       -1     
  Lines         39917    39868      -49     
  Branches       6059     6040      -19     
============================================
- Hits          28267    28250      -17     
+ Misses         8924     8891      -33     
- Partials       2726     2727       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


// Define the pattern for matching log file names
pattern = Pattern.compile(fileNamePrefix.replace(".", "\\.")
+ "\\.(\\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))(\\.\\d)?$");
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a constant. It would make the code easier to read, and the name of the constant will document what this part of the regex is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created constant DATE_REGEX as a class field


@Override
public void run() {
Thread.currentThread().setName("New Relic Expiring Log File Cleanup");
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be done in the executor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to the executor 👍


String filePattern = fileName + ".%d{yyyy-MM-dd}";
if (logLimitBytes > 0) {
// If we might roll within a day, use a number ordering suffix
filePattern = fileName + ".%d{yyyy-MM-dd}.%i";
}

Path directory = new File(this.path).toPath();
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this will generate non daemon threads. Which will prevent the application from shutting down.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

used a ThreadFactory to explicitly configure the generated threads used for the executorService as daemon threads

public void tearDown() throws IOException {
if (tempDir != null && Files.exists(tempDir)) {
Files.walk(tempDir)
.sorted((a, b) -> -a.compareTo(b))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do they need to be sorted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only test that requires files to be sorted prior to deletion is testDirectoryWithMixedFiles. This test contains a subdirectory with files so the sorting ensures that all files are deleted before subdirectories are deleted. Without sorting, this test throws a DirectoryNotEmptyException when attempting to delete the parent directory and the test fails.


@Test
public void testRunnableAgainstEmptyLogDirectory() throws IOException {
tempDir = Files.createTempDirectory("emptyLogDir");
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need to create another temp directory? The code in @before just created one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed 👍

date = calendar.getTime();
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Good testing.

@deleonenriqueta deleonenriqueta merged commit 97330dc into main Mar 1, 2024
103 checks passed
@deleonenriqueta deleonenriqueta deleted the log-daily branch May 24, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

When log_daily == true, the log_file_count config value is not respected if greater than 0
4 participants