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

fix(pkg/stanza/input/file/reader): skip building fingerprint in case of configuration change #10485

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
### 🧰 Bug fixes 🧰

- `kubletetstatsreceiver`: Bring back `k8s.container.name` attribute (#10848)
- `pkg/stanza`: Skip building fingerprint in case of configuration change (#10485)

## v0.53.0

Expand Down
86 changes: 86 additions & 0 deletions pkg/stanza/operator/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,92 @@ func TestFingerprintGrowsAndStops(t *testing.T) {
}
}

// This is same test like TestFingerprintGrowsAndStops, but with additional check for fingerprint size check
// Test that a fingerprint:
// - Starts empty
// - Updates as a file is read
// - Stops updating when the max fingerprint size is reached
// - Stops exactly at max fingerprint size, regardless of content
// - Do not change size after fingerprint configuration change
func TestFingerprintChangeSize(t *testing.T) {
t.Parallel()

// Use a number with many factors.
// Sometimes fingerprint length will align with
// the end of a line, sometimes not. Test both.
maxFP := 360

// Use prime numbers to ensure variation in
// whether or not they are factors of maxFP
lineLens := []int{3, 5, 7, 11, 13, 17, 19, 23, 27}

for _, lineLen := range lineLens {
t.Run(fmt.Sprintf("%d", lineLen), func(t *testing.T) {
t.Parallel()
operator, _, tempDir := newTestFileOperator(t, func(cfg *Config) {
cfg.FingerprintSize = helper.ByteSize(maxFP)
}, nil)
defer func() {
require.NoError(t, operator.Stop())
}()

temp := openTemp(t, tempDir)
tempCopy := openFile(t, temp.Name())
fp, err := operator.NewFingerprint(temp)
require.NoError(t, err)
require.Equal(t, []byte(""), fp.FirstBytes)

splitter, err := operator.getMultiline()
require.NoError(t, err)

reader, err := operator.NewReader(temp.Name(), tempCopy, fp, splitter)
require.NoError(t, err)
defer reader.Close()

// keep track of what has been written to the file
fileContent := []byte{}

// keep track of expected fingerprint size
expectedFP := 0

// Write lines until file is much larger than the length of the fingerprint
for len(fileContent) < 2*maxFP {
expectedFP += lineLen
if expectedFP > maxFP {
expectedFP = maxFP
}

line := stringWithLength(lineLen-1) + "\n"
fileContent = append(fileContent, []byte(line)...)

writeString(t, temp, line)
reader.ReadToEnd(context.Background())
require.Equal(t, fileContent[:expectedFP], reader.Fingerprint.FirstBytes)
}

// Test fingerprint change
// Change fingerprint and try to read file again
// We do not expect fingerprint change
// We test both increasing and decreasing fingerprint size
reader.fileInput.fingerprintSize = maxFP * (lineLen / 3)
line := stringWithLength(lineLen-1) + "\n"
fileContent = append(fileContent, []byte(line)...)

writeString(t, temp, line)
reader.ReadToEnd(context.Background())
require.Equal(t, fileContent[:expectedFP], reader.Fingerprint.FirstBytes)

reader.fileInput.fingerprintSize = maxFP / 2
line = stringWithLength(lineLen-1) + "\n"
fileContent = append(fileContent, []byte(line)...)

writeString(t, temp, line)
reader.ReadToEnd(context.Background())
require.Equal(t, fileContent[:expectedFP], reader.Fingerprint.FirstBytes)
})
}
}

func TestEncodings(t *testing.T) {
t.Parallel()
cases := []struct {
Expand Down
10 changes: 9 additions & 1 deletion pkg/stanza/operator/input/file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,19 @@ func getScannerError(scanner *PositionalScanner) error {

// Read from the file and update the fingerprint if necessary
func (r *Reader) Read(dst []byte) (int, error) {
if len(r.Fingerprint.FirstBytes) == r.fileInput.fingerprintSize {
// Skip if fingerprint is already built
// or if fingerprint is behind Offset
if len(r.Fingerprint.FirstBytes) == r.fileInput.fingerprintSize || int(r.Offset) > len(r.Fingerprint.FirstBytes) {
return r.file.Read(dst)
}
n, err := r.file.Read(dst)
appendCount := min0(n, r.fileInput.fingerprintSize-int(r.Offset))
// return for n == 0 or r.Offset >= r.fileInput.fingerprintSize
if appendCount == 0 {
return n, err
}

// for appendCount==0, the following code would add `0` to fingerprint
r.Fingerprint.FirstBytes = append(r.Fingerprint.FirstBytes[:r.Offset], dst[:appendCount]...)
return n, err
}
Expand Down