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

Bugfix for log lines without or delayed newlines results in a file truncated detection and reading the file again #164

Closed
wants to merge 3 commits into from
Closed
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
49 changes: 34 additions & 15 deletions harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ func (h *Harvester) Harvest(output chan *FileEvent) {

// TODO(sissel): Make the buffer size tunable at start-time
reader := bufio.NewReaderSize(h.file, 16<<10) // 16kb buffer by default
buffer := new(bytes.Buffer)

var read_timeout = 10 * time.Second
last_read_time := time.Now()
for {
text, err := h.readline(reader, read_timeout)
text, bytesread, err := h.readline(reader, buffer, read_timeout)

if err != nil {
if err == io.EOF {
Expand Down Expand Up @@ -78,7 +79,7 @@ func (h *Harvester) Harvest(output chan *FileEvent) {
Fields: &h.Fields,
fileinfo: &info,
}
offset += int64(len(*event.Text)) + 1 // +1 because of the line terminator
offset += int64(bytesread)

output <- event // ship the new event downstream
} /* forever */
Expand Down Expand Up @@ -116,38 +117,56 @@ func (h *Harvester) open() *os.File {
return h.file
}

func (h *Harvester) readline(reader *bufio.Reader, eof_timeout time.Duration) (*string, error) {
var buffer bytes.Buffer
func (h *Harvester) readline(reader *bufio.Reader, buffer *bytes.Buffer, eof_timeout time.Duration) (*string, int, error) {
var is_partial bool = true
var newline_length int = 1
start_time := time.Now()

for {
segment, is_partial, err := reader.ReadLine()
segment, err := reader.ReadBytes('\n')
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't fully read this patch, but it looks like this reimplements go's ReadLine() method with respect to partial detection. Is there a bug we should file upstream to the Go developers? Put differently, what caused this to need reimplementing of is_partial?

Copy link
Contributor

Choose a reason for hiding this comment

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

As described in #149 we discovered ReadLine() actually treats EOF as an end-of-line delimiter (I checked the pkg source) - which is the root cause of the issue.
So if you have data at the end of the log, with no new line, ReadLine() returns normally with is_partial set to false and you can never know if there was a new line or not - and LSF assumes there was (the +1 we have for offset). So we end up offset too far ahead of the Size() and detect truncation, wrongly.

It's also worth noting http://golang.org/pkg/bufio/#Reader.ReadLine

ReadLine is a low-level line-reading primitive. Most callers should use ReadBytes('\n') or ReadString('\n') instead or use a Scanner.

Using ReadBytes('\n') we can safely ensure that we don't treat EOF as a line delimiter and properly count the \r\n bytes at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also think Go have documented the behaviour at EOF. Note the second sentence.

The text returned from ReadLine does not include the line end ("\r\n" or "\n"). No indication or error is given if the input ends without a final line end.

So an upstream patch is probably not viable as it's the defined behaviour.


if segment != nil && len(segment) > 0 {
if segment[len(segment)-1] == '\n' {
// Found a complete line
is_partial = false

// Check if also a CR present
if len(segment) > 1 && segment[len(segment)-2] == '\r' {
newline_length++
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Line 145. Just needs blank line removing here and at end of block - forget what I said about moved - got it now, needed bufferSize earlier ^^ Coolios

// TODO(sissel): if buffer exceeds a certain length, maybe report an error condition? chop it?
buffer.Write(segment)
}

if err != nil {
if err == io.EOF {
if err == io.EOF && is_partial {
time.Sleep(1 * time.Second) // TODO(sissel): Implement backoff

// Give up waiting for data after a certain amount of time.
// If we time out, return the error (eof)
if time.Since(start_time) > eof_timeout {
return nil, err
return nil, 0, err
}
continue
} else {
log.Println(err)
return nil, err // TODO(sissel): don't do this?
return nil, 0, err // TODO(sissel): don't do this?
}
}

// TODO(sissel): if buffer exceeds a certain length, maybe report an error condition? chop it?
buffer.Write(segment)

// If we got a full line, return the whole line without the EOL chars (CRLF or LF)
if !is_partial {
// If we got a full line, return the whole line.
// Get the str length with the EOL chars (LF or CRLF)
bufferSize := buffer.Len()
str := new(string)
*str = buffer.String()
return str, nil
*str = buffer.String()[:bufferSize - newline_length]
// Reset the buffer for the next line
buffer.Reset()
return str, bufferSize, nil
}
} /* forever read chunks */

return nil, nil
return nil, 0, nil
}
1 change: 1 addition & 0 deletions registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Registrar(input chan []*FileEvent) {
Source: event.Source,
// take the offset + length of the line + newline char and
// save it as the new starting offset.
// This issues a problem, if the EOL is a CRLF! Then on start it read the LF again and generates a event with an empty line
Copy link
Contributor

Choose a reason for hiding this comment

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

This is easy fix. In Harvester just needs offset updating before passing the event down. Then we don't need to do this calculation here at all and can just use event.Offset. Might be easier to do after merge.

Choose a reason for hiding this comment

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

Hi Driskell,
I am too facing this duplicate events pushed.
I tried your https://github.com/driskell/logstash-forwarder.
With the following config

"files": [
{
"paths": [
"/path/to/log1.log"
],
"fields": { "type": "log1" }
},
{
"paths": [
"/path/to/log2.log"
],
"fields": { "type": "log2" }
}
]

I see the messages with the type flag getting misplaced. For instance log1.log gets the log2 type.

Also, is this patch is committed in to the main git repo?
https://github.com/elasticsearch/logstash-forwarder?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi,

This isn't merged yet. I believe @alphazero-es was testing a merge if this with one of my PR but not sure where he got.

I'm not working on my fork of the logstash-forwarder repo anymore. I moved everything to http://github.com/driskell/log-courier and started maintaining it as my own shipper. It has it's own protocol and logstash plugins and there's docs on how to install them to logstash.

Your specific issue was reported at driskell/log-courier#9 and it's now fixed in the develop branch. Hoping to merge to stable at the weekend.

Jason

Offset: event.Offset + int64(len(*event.Text)) + 1,
Inode: ino,
Device: dev,
Expand Down