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

Remove empty temp dirs at plugin startup. #39

Merged

Conversation

mashhurs
Copy link
Contributor

@mashhurs mashhurs commented Aug 31, 2023

Release notes

[rn:skip]

What does this PR do?

For some environments, especially Windows OS, file scanning (such as antivirus enabled) tools hold temporarily created dirs' IO and a plugin cannot remove the temporarily dir after successfully uploading file to S3. Over time, there will be many empty temp dirs left under the temporary_directory.
This PR introduces a logic which, at the Logstash start up, plugin cleans up temporary dirs.

Why is it important/What is the impact to the user?

The users who are facing empty temp dirs left issues get benefit from this automation.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files (and/or docker env variables)
  • I have added tests that prove my fix is effective or that my feature works

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

Use cases

Screenshots

Logs

…es due to scaning tools such as antiviruses.
@mashhurs mashhurs added the bug Something isn't working label Aug 31, 2023
@mashhurs mashhurs self-assigned this Aug 31, 2023
@mashhurs mashhurs marked this pull request as ready for review September 1, 2023 23:03
@mashhurs
Copy link
Contributor Author

mashhurs commented Sep 1, 2023

🔴 CI is not relative and even re-run doesn't kick new run.

Analyzing the error a bit more provides a clear picture that it is due to docker 8.10 snapshot which uses JRuby 9.3.4.0 and has Concurrent::TimerTask regression.
The error coming through stale sweeper initialization: @stale_sweeper = Concurrent::TimerTask.new(:execution_interval => @sweeper_interval) do.

spec/outputs/s3_spec.rb Show resolved Hide resolved
spec/outputs/s3_spec.rb Show resolved Hide resolved
spec/outputs/s3_spec.rb Show resolved Hide resolved
.select { |path| ::File.directory?(path) }
.select { |path| (Dir.entries(path) - %w[ . .. ]).empty? } # current and parent dirs escape
.each do |path|
FileUtils.rm_rf(path, :secure => true)
Copy link
Contributor

Choose a reason for hiding this comment

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

If the directory is empty, do we need to use rm_rf here, or can we use rm_f?

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 intention was here to avoid handling StandardError cases, such as IOError.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess my question was more around the need to use the recursive (and destructive) form of rm rather than the force functionality. Would you expect that the recursive form is appropriate, and worth the risk of data loss in the event of a coding error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah got it. Yup, we don't need recursive operation here. We are starting to remove the leaf of the tree. Our operation itself is recursive, will change it to FileUtils.rm_f.

lib/logstash/outputs/s3.rb Show resolved Hide resolved
lib/logstash/outputs/s3.rb Outdated Show resolved Hide resolved
Leave debug logs while removing an empty dir. Apply unit test suggestions.

Co-authored-by: Rob Bavey <rob.bavey@elastic.co>
lib/logstash/outputs/s3.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@robbavey robbavey left a comment

Choose a reason for hiding this comment

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

LGTM

@mashhurs mashhurs merged commit a5dfa0c into logstash-plugins:main Sep 12, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

S3 Output should clean up empty temp folders
2 participants