Skip to content

Commit

Permalink
Change LogFile.restoreState() so it does not return negative offset. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-mateen authored Dec 1, 2022
1 parent ce2454b commit 1b0f75d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
8 changes: 5 additions & 3 deletions plugins/inputs/logfile/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (t *LogFile) Stop() {
close(t.done)
}

//Try to find if there is any new file needs to be added for monitoring.
// Try to find if there is any new file needs to be added for monitoring.
func (t *LogFile) FindLogSrc() []logs.LogSrc {
if !t.started {
return nil
Expand Down Expand Up @@ -313,7 +313,7 @@ func (t *LogFile) getTargetFiles(fileconfig *FileConfig) ([]string, error) {
return targetFileList, nil
}

//The plugin will look at the state folder, and restore the offset of the file seeked if such state exists.
// The plugin will look at the state folder, and restore the offset of the file seeked if such state exists.
func (t *LogFile) restoreState(filename string) (int64, error) {
filePath := t.getStateFilePath(filename)

Expand All @@ -334,8 +334,10 @@ func (t *LogFile) restoreState(filename string) (int64, error) {
return 0, err
}

if offset < 0 {
return 0, fmt.Errorf("negative state file offset, %v, %v", filePath, offset)
}
t.Log.Infof("Reading from offset %v in %s", offset, filename)

return offset, nil
}

Expand Down
19 changes: 16 additions & 3 deletions plugins/inputs/logfile/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,20 @@ func TestRestoreState(t *testing.T) {
tt.Log = TestLogger{t}
tt.FileStateFolder = tmpfolder
roffset, err := tt.restoreState(logFilePath)
require.NoError(t, err)
assert.Equal(t, offset, roffset, fmt.Sprintf("The actual offset is %d, different from the expected offset %d.", roffset, offset))

// Test negative offset.
offset = int64(-8675)
err = ioutil.WriteFile(
tmpfolder+string(filepath.Separator)+logFileStateFileName,
[]byte(strconv.FormatInt(offset, 10)+"\n"+logFilePath),
os.ModePerm)
require.NoError(t, err)
roffset, err = tt.restoreState(logFilePath)
require.Error(t, err)
assert.Equal(t, int64(0), roffset, fmt.Sprintf("The actual offset is %d, different from the expected offset %d.", roffset, offset))

tt.Stop()
}

Expand Down Expand Up @@ -311,7 +324,7 @@ func TestLogsMultilineEvent(t *testing.T) {
tt.Stop()
}

//When file is removed, the related tail routing should exit
// When file is removed, the related tail routing should exit
func TestLogsFileRemove(t *testing.T) {
multilineWaitPeriod = 10 * time.Millisecond
logEntryString := "anything"
Expand Down Expand Up @@ -429,9 +442,9 @@ func createWriteRead(t *testing.T, prefix string, logFile *LogFile, done chan bo
}
t.Log("Verify every line written to the temp file is received.")
for i := 0; i < numLines; i++ {
logEvent := <- evts
logEvent := <-evts
require.Equal(t, msg, logEvent.Message())
if i != numLines / 2 {
if i != numLines/2 {
continue
}
// Halfway through start another goroutine to create another temp file.
Expand Down

0 comments on commit 1b0f75d

Please sign in to comment.