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

#189 Zero byte read from NFS - Fix method file_read in watched_file.rb #190

Closed
wants to merge 1 commit into from

Conversation

sv-gh
Copy link

@sv-gh sv-gh commented May 11, 2018

This change has no any impact on currently working code, ment to improve reliability of logs reading in case when size/content update in file write implementation is not atomic in underlying FS software (as in NFSv3). Please approve/accept this fix for the nearest release.
This issue is blocking for our project and logstash use in jeopardy.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…, method file_read
@karmi
Copy link

karmi commented May 11, 2018

Hi @sv-gh, we have found your signature in our records, but it seems like you have signed with a different e-mail than the one used in yout Git commit. Can you please add both of these e-mails into your Github profile (they can be hidden), so we can match your e-mails to your Github profile?

@sv-gh sv-gh changed the title #189 Zero byte read from NFS - Fix in watched_file.rb, method file_read #189 Zero byte read from NFS - Fix method file_read in watched_file.rb May 11, 2018
@yaauie yaauie self-assigned this Jun 27, 2018
Copy link
Contributor

@yaauie yaauie left a comment

Choose a reason for hiding this comment

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

@sv-gh while this approach may work for your specific setup, it's not an approach that I believe scales to the wide user-base of this plugin. I appreciate the work you put in, and will work to get an acceptable solution put together shortly.


The primary reason is that the presence of a null-byte isn't necessarily a reliable indicator that the sysread has returned bytes beyond the end of the file:

  • a null byte is valid in gzipped log files, which means that when reading gzipped logs in this implementation we will frequently need to fall through all attempts, reading, sleeping, and un-seeking repeatedly before finally emitting each chunk.
  • When reading beyond the end of a file using sysread, the behaviour is undefined; on your system it may be observed to emit one or more null bytes, but this is not specified nor guaranteed.

In order to reliably ensure that we don't read beyond the end of our source files, each time we sysread we will need to first stat the file to determine how much is available.


There is currently work in-flight to improve support for file rotation, which touches this and many other methods, and a follow-up is already planned by @guyboertje to address the root cause of #189 to ensure that we don't sysread beyond the end of a file.

I'll work with him to ensure that the right solution gets implemented in the coming weeks.

set_accessed_at
buf = @file.sysread(amount)
# return if no zero byte inside
return buf unless zc = buf.index("\0")
Copy link
Contributor

Choose a reason for hiding this comment

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

The zc variable is bound, but unused.

Additionally, for readability we prefer that variables are descriptive and unabiguous, and while I can tell what buf is, variables like cc and dt would be better off with more descriptive names like attempts_remaining and backoff_delay.

# return if no zero byte inside
return buf unless zc = buf.index("\0")
# update amount to read
amount = buf.bytesize
Copy link
Contributor

Choose a reason for hiding this comment

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

amount here has multiple meanings (amount requested vs amount retrieved), and the mutation of the input parameter combined with the retry pattern means that on each subsequent attempt we will read potentially fewer bytes than initially requested. This is rather opaque and can present surprises later on that would be hard to debug, so we try to avoid overloading variables like this.

@sv-gh
Copy link
Author

sv-gh commented Jun 27, 2018 via email

@sv-gh sv-gh closed this Jul 10, 2018
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.

None yet

3 participants