-
Notifications
You must be signed in to change notification settings - Fork 3k
[fix] [pkg/stanza/fileconsumer] do not update fingerprint size when less data has been read #22937
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
[fix] [pkg/stanza/fileconsumer] do not update fingerprint size when less data has been read #22937
Conversation
9d6578e
to
d56a30d
Compare
I'm checking failures of unit tests. |
@kkujawa-sumo, thanks for reporting this and opening the PR. I believe this may be related to some observations made by @maokitty in #19767, #19428, and #20851. Unfortunately, we were not able to resolve the issues yet. |
310f5e6
to
e8513a5
Compare
e8513a5
to
6e9dff0
Compare
It looks like unit tests failed for commit because of additional operation in @djaglowski I propose another approach to fix problem that I found (@22936) increasing scanner buffer to fingerprint size to enable reading the whole fingerprint in single iteration, please see this. What do you think about this approach? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still not clear to me why this scenario fails. Can you describe the failure mechanism in more detail?
func (b *readerBuilder) withBufferSize() *readerBuilder { | ||
if b.readerConfig.fingerprintSize > defaultBufSize { | ||
b.bufferSize = b.readerConfig.fingerprintSize | ||
} else { | ||
b.bufferSize = defaultBufSize | ||
} | ||
return b | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason not to always set the buffer size like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now default buffer size is set to 16 kb, default fingerprint size is set 1kb in filelogreceiver
so the difference is significant, lower buffer for reading could impact speed of reading from the log file (more read operations with smaller buffer), higher fingerprint size could cause higher memory consumption - more data kept in memory to store fingerprint size.
Maybe consequences of setting always b.bufferSize = b.readerConfig.fingerprintSize
will not be visible but the issue which I observed exists only for cases when fingerprint size is higher than the buffer.
@@ -32,6 +32,7 @@ type Reader struct { | |||
processFunc EmitFunc | |||
|
|||
Fingerprint *Fingerprint | |||
bufferSize int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be on readerConfig
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll change this
6e9dff0
to
da83b13
Compare
…t size to enable reading the whole fingerprint in single iteration Signed-off-by: Katarzyna Kujawa <kkujawa@sumologic.com>
da83b13
to
2689991
Compare
@djaglowski more details This will describe what happens in Read function now when generated_log.txt is read in initialization fingerprint is read,
Checking what happens Scan() function from
and I checked how os.File.Read() reads data from file using this simple code:
for the file:
the output is
and same situation is in the code of fileconsumer, os.File.Read() reads only unread data and data which left after splitting are kept in the buffer |
@kkujawa-sumo, thanks for detailing what happens in the scenario. I am still having difficultly understanding the root cause of this issue, but this stands out to me as a problem:
Specifically, this seems to indicate that the offset has been incorrectly updated. If this is indeed the case, then I think the proposed change may only avoid the issue, rather than solve it. |
I looked into this some more and believe the error lies in the // Read from the file and update the fingerprint if necessary
func (r *Reader) Read(dst []byte) (n int, err error) {
n, err = r.file.Read(dst)
// CASE 0: Steady state
// Just return what was read.
if len(r.Fingerprint.FirstBytes) == r.fingerprintSize {
return
}
// CASE 1: Fingerprint building
// The file is small or being read for the first time.
// It is the only condition in which we should update the fingerprint.
// Handled before edge cases in order to avoid unnecessary work.
if len(r.Fingerprint.FirstBytes) < r.fingerprintSize && len(r.Fingerprint.FirstBytes) == int(r.Offset) {
appendCount := r.fingerprintSize - len(r.Fingerprint.FirstBytes)
if appendCount > n {
appendCount = n
}
r.Fingerprint.FirstBytes = append(r.Fingerprint.FirstBytes, dst[:appendCount]...)
return
}
// The remaining cases can happen when the following has occured:
// 1. The component was started with a storage extension and saved a (partial or full) fingerprint for the file.
// 2. The component was restarted with the same storage extension and a different setting for 'fingerprint_size'.
// CASE 2: Oversized fingerprint
// The component was restarted with a decreased 'fingerprint_size'. Just keep reading.
if len(r.Fingerprint.FirstBytes) > r.fingerprintSize {
return
}
// CASE 3: Undersized fingerprint
// At this point we know that the fingerprint is undersized (didn't match CASE 0 or CASE 2).
// However, we also know that the fingerprint and offset are not aligned (didn't match second condition of CASE 1).
// Therefore, we cannot update the fingerprint because we are not reading from the correct staring position.
return
} If you agree it works, I'll continue on this tomorrow by writing a set of tests to validate each case. |
Offset is set to the end of the line but amount of bytes read from the file is larger ( offset != read bytes), some data are in the buffer and they waits for next iteration of reading. last characters read in first iteration of reading
offset is set after
I think that offset is correctly set - at the end of fully processed line and the root cause is that the offset was treated as the indicator of read bytes form the file what is not true because some bytes are temporary kept in the buffer (when the reading finishes reading in the middle of the line). |
@djaglowski I'm ok with fixing the problem in I also wrote test with line longer than default buffer and larger fingerprintsize - it passes: func TestReadingWithLargeFingerPrintSizeAndLineLargerThanScannerBuf(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
lineLength := defaultBufSize + 1024
fingerPrintSize := defaultBufSize + 2*1024
// Generate log lines
body := "0abcdefghijklmnopqrstuvwxyz"
firstline := ""
for len(firstline) < lineLength {
firstline += body
}
firstline = fmt.Sprintf("%s\n", firstline[:lineLength-1])
require.Equal(t, lineLength, len(firstline))
// Create a new file
temp := openTemp(t, tempDir)
writeString(t, temp, firstline)
// Create reader
emitChan := make(chan *emitParams, 1000)
splitterConfig := helper.NewSplitterConfig()
f := &readerFactory{
SugaredLogger: testutil.Logger(t),
readerConfig: &readerConfig{
fingerprintSize: fingerPrintSize,
maxLogSize: defaultMaxLogSize,
emit: testEmitFunc(emitChan),
},
fromBeginning: true,
splitterFactory: newMultilineSplitterFactory(splitterConfig),
encodingConfig: splitterConfig.EncodingConfig,
}
r, err := f.newReaderBuilder().withFile(temp).withBufferSize().build()
require.NoError(t, err)
initialFingerPrintSize := len(r.Fingerprint.FirstBytes)
initialFingerPrint := string(r.Fingerprint.FirstBytes)
r.ReadToEnd(context.Background())
require.Equal(t, initialFingerPrintSize, len(r.Fingerprint.FirstBytes))
require.Equal(t, initialFingerPrint, string(r.Fingerprint.FirstBytes))
require.Equal(t, []byte(firstline[:lineLength-1]), readToken(t, emitChan))
} |
Thanks for confirming @kkujawa-sumo. I've opened #23183. Perhaps if it's merged you'll rebase to add your test? |
@djaglowski Thanks for help with this issue ❤️ |
Depends on #31298 Fixes #22936 This PR changes the way readers update their fingerprints. Currently, when `reader.ReadToEnd` is called, it creates a scanner and passes itself (the reader) in as the `io.Reader` so that a custom implementation of `Read` will be used by the scanner. Each time the scanner calls `Read`, we try to perform appropriate reasoning about whether the data we've read should be appended to the fingerprint. The problem is that the correct positioning of the bytes buffer is in some rare cases different than the file's "offset", as we track it. See example [here](#22937 (comment)). There appear to be two ways to solve this. A "simple" solution is to independently determine the file handle's current offset with a clever use of `Seek`, ([suggested here](https://stackoverflow.com/a/10901436/3511338). Although this does appear to work, it leaves open the possibility that the fingerprint is corrupted because _if the file was truncated_, we may be updating the fingerprint with incorrect information. The other solution, proposed in this PR, simply has the custom `Read` function set a flag to indicate that the fingerprint _should_ be updated. Then, just before returning from `ReadToEnd`, we create an entirely new fingerprint. This has the advantage of not having to manage any kind of append operations, but also allows the the opportunity to independently check that the fingerprint has not been altered by truncation. Benchmarks appear to show all three solutions are close in performance.
Depends on open-telemetry#31298 Fixes open-telemetry#22936 This PR changes the way readers update their fingerprints. Currently, when `reader.ReadToEnd` is called, it creates a scanner and passes itself (the reader) in as the `io.Reader` so that a custom implementation of `Read` will be used by the scanner. Each time the scanner calls `Read`, we try to perform appropriate reasoning about whether the data we've read should be appended to the fingerprint. The problem is that the correct positioning of the bytes buffer is in some rare cases different than the file's "offset", as we track it. See example [here](open-telemetry#22937 (comment)). There appear to be two ways to solve this. A "simple" solution is to independently determine the file handle's current offset with a clever use of `Seek`, ([suggested here](https://stackoverflow.com/a/10901436/3511338). Although this does appear to work, it leaves open the possibility that the fingerprint is corrupted because _if the file was truncated_, we may be updating the fingerprint with incorrect information. The other solution, proposed in this PR, simply has the custom `Read` function set a flag to indicate that the fingerprint _should_ be updated. Then, just before returning from `ReadToEnd`, we create an entirely new fingerprint. This has the advantage of not having to manage any kind of append operations, but also allows the the opportunity to independently check that the fingerprint has not been altered by truncation. Benchmarks appear to show all three solutions are close in performance.
Depends on open-telemetry#31298 Fixes open-telemetry#22936 This PR changes the way readers update their fingerprints. Currently, when `reader.ReadToEnd` is called, it creates a scanner and passes itself (the reader) in as the `io.Reader` so that a custom implementation of `Read` will be used by the scanner. Each time the scanner calls `Read`, we try to perform appropriate reasoning about whether the data we've read should be appended to the fingerprint. The problem is that the correct positioning of the bytes buffer is in some rare cases different than the file's "offset", as we track it. See example [here](open-telemetry#22937 (comment)). There appear to be two ways to solve this. A "simple" solution is to independently determine the file handle's current offset with a clever use of `Seek`, ([suggested here](https://stackoverflow.com/a/10901436/3511338). Although this does appear to work, it leaves open the possibility that the fingerprint is corrupted because _if the file was truncated_, we may be updating the fingerprint with incorrect information. The other solution, proposed in this PR, simply has the custom `Read` function set a flag to indicate that the fingerprint _should_ be updated. Then, just before returning from `ReadToEnd`, we create an entirely new fingerprint. This has the advantage of not having to manage any kind of append operations, but also allows the the opportunity to independently check that the fingerprint has not been altered by truncation. Benchmarks appear to show all three solutions are close in performance.
Description:
Do not update fingerprint bytes when less data has been read than it was read during initialisation of fingerprint bytes
Link to tracking Issue:
#22936
Testing:
Manual tests - done
Unit test - in progress
Documentation: