From c57a06ad4ae9e953496730507e02a613f69bddac Mon Sep 17 00:00:00 2001 From: Dan Jaglowski Date: Thu, 5 Oct 2023 15:16:55 -0600 Subject: [PATCH] [chore][pkg/stanza] Clarify and fix truncated file func --- .../fileconsumer/internal/reader/reader.go | 22 ++++++++++++------- pkg/stanza/fileconsumer/roller_other.go | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/stanza/fileconsumer/internal/reader/reader.go b/pkg/stanza/fileconsumer/internal/reader/reader.go index 6d4194d0361f..c2115f1fb9ed 100644 --- a/pkg/stanza/fileconsumer/internal/reader/reader.go +++ b/pkg/stanza/fileconsumer/internal/reader/reader.go @@ -12,6 +12,7 @@ import ( "go.uber.org/zap" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/attrs" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decode" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/emit" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint" @@ -187,24 +188,29 @@ func min0(a, b int) int { return b } -// validateFingerprint checks whether or not the reader still has a valid file handle. +// ValidateOrClose returns true if the reader still has a valid file handle, false otherwise. +// If false is returned, the file handle should be considered closed. // -// It creates a new fingerprint from the old file handle and compares it to the +// It may create a new fingerprint from the old file handle and compare it to the // previously known fingerprint. If there has been a change to the fingerprint -// (other than appended data), the file is considered invalid. Consequently, the +// (other than appended data), the file is considered truncated. Consequently, the // reader will automatically close the file and drop the handle. -// -// The function returns true if the file handle is still valid, false otherwise. -func (r *Reader) ValidateFingerprint() bool { +func (r *Reader) ValidateOrClose() bool { if r.file == nil { return false } refreshedFingerprint, err := fingerprint.New(r.file, r.FingerprintSize) if err != nil { - r.logger.Debugw("Failed to create fingerprint", zap.Error(err)) + r.logger.Debugw("Closing unreadable file", zap.Error(err), zap.String(attrs.LogFileName, r.FileName)) + r.Close() return false } - return refreshedFingerprint.StartsWith(r.Fingerprint) + if refreshedFingerprint.StartsWith(r.Fingerprint) { + return true + } + r.logger.Debugw("Closing truncated file", zap.String(attrs.LogFileName, r.FileName)) + r.Close() + return false } func (r *Reader) AtEOF() bool { diff --git a/pkg/stanza/fileconsumer/roller_other.go b/pkg/stanza/fileconsumer/roller_other.go index efda6b3a2731..0d2910530f63 100644 --- a/pkg/stanza/fileconsumer/roller_other.go +++ b/pkg/stanza/fileconsumer/roller_other.go @@ -34,8 +34,8 @@ OUTER: // At this point, we know that the file has been rotated. However, we do not know // if it was moved or truncated. If truncated, then both handles point to the same // file, in which case we should only read from it using the new reader. We can use - // the validateFingerprint method to establish that the file has not been truncated. - if !oldReader.ValidateFingerprint() { + // the ValidateOrClose method to establish that the file has not been truncated. + if !oldReader.ValidateOrClose() { continue OUTER } }