-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[docker plugin] Refactor pipeline reader for data safety #14375
Merged
fearful-symmetry
merged 8 commits into
elastic:feature/dockerbeat
from
fearful-symmetry:refactor-protobuf-reader
Nov 13, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e78b96f
refactor pipeline reader for data safety
fearful-symmetry c5417f9
clean up commented out code
fearful-symmetry 6e44187
first round of clean up
fearful-symmetry 9cbf000
add constructor for ReadCloser
fearful-symmetry f3f3287
remove extra dep, use containerd/fifo
fearful-symmetry 01bd5b7
remove extra dep
fearful-symmetry 9110fc7
update notice
fearful-symmetry 19b9d81
refactor buffer sizing for better memory management
fearful-symmetry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package pipereader | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"io" | ||
"syscall" | ||
|
||
"github.com/containerd/fifo" | ||
"github.com/docker/engine/api/types/plugins/logdriver" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// PipeReader reads from the FIFO pipe we get from the docker container | ||
type PipeReader struct { | ||
fifoPipe io.ReadCloser | ||
byteOrder binary.ByteOrder | ||
lenFrameBuf []byte | ||
bodyBuf []byte | ||
maxSize int | ||
} | ||
|
||
// NewReaderFromPath creates a new FIFO pipe reader from a docker log pipe location | ||
func NewReaderFromPath(file string) (*PipeReader, error) { | ||
inputFile, err := fifo.OpenFifo(context.Background(), file, syscall.O_RDONLY, 0700) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "error opening logger fifo: %q", file) | ||
} | ||
|
||
return &PipeReader{fifoPipe: inputFile, byteOrder: binary.BigEndian, lenFrameBuf: make([]byte, 4), bodyBuf: nil, maxSize: 2e6}, nil | ||
} | ||
fearful-symmetry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// NewReaderFromReadCloser creates a new FIFO pipe reader from an existing ReadCloser | ||
func NewReaderFromReadCloser(pipe io.ReadCloser) (*PipeReader, error) { | ||
return &PipeReader{fifoPipe: pipe, byteOrder: binary.BigEndian, lenFrameBuf: make([]byte, 4), bodyBuf: nil, maxSize: 2e6}, nil | ||
} | ||
|
||
// ReadMessage reads a log message from the pipe | ||
// The message stream consists of a 4-byte length frame and a message body | ||
// There's three logical paths for this code to take: | ||
// 1) If length <0, we have bad data, and we cycle through the frames until we get a valid length. | ||
// 2) If length is valid but larger than the max buffer size, we disregard length bytes and continue | ||
// 3) If length is valid and we can consume everything into the buffer, continue. | ||
func (reader *PipeReader) ReadMessage(log *logdriver.LogEntry) error { | ||
// loop until we're at a valid state and ready to read a message body | ||
var lenFrame int | ||
var err error | ||
for { | ||
lenFrame, err = reader.getValidLengthFrame() | ||
if err != nil { | ||
return errors.Wrap(err, "error getting length frame") | ||
} | ||
if lenFrame <= reader.maxSize { | ||
break | ||
} | ||
|
||
// 2) we have a too-large message. Disregard length bytes | ||
_, err = io.CopyBuffer(nil, io.LimitReader(reader.fifoPipe, int64(lenFrame)), reader.bodyBuf) | ||
if err != nil { | ||
return errors.Wrap(err, "error emptying buffer") | ||
} | ||
} | ||
|
||
//proceed with 3) | ||
readBuf := reader.setBuffer(lenFrame) | ||
_, err = io.ReadFull(reader.fifoPipe, readBuf[:lenFrame]) | ||
if err != nil { | ||
return errors.Wrap(err, "error reading buffer") | ||
} | ||
return proto.Unmarshal(readBuf[:lenFrame], log) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: given |
||
|
||
} | ||
|
||
// setBuffer checks the needed size, and returns a buffer, allocating a new buffer if needed | ||
func (reader *PipeReader) setBuffer(sz int) []byte { | ||
const minSz = 1024 | ||
const maxSz = minSz * 32 | ||
|
||
// return only the portion of the buffer we need | ||
if len(reader.bodyBuf) >= sz { | ||
return reader.bodyBuf[:sz] | ||
} | ||
|
||
// if we have an abnormally large buffer, don't set it to bodyBuf so GC can clean it up | ||
if sz > maxSz { | ||
return make([]byte, sz) | ||
} | ||
|
||
reader.bodyBuf = make([]byte, sz) | ||
return reader.bodyBuf | ||
} | ||
fearful-symmetry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// getValidLengthFrame scrolls through the buffer until we get a valid length | ||
func (reader *PipeReader) getValidLengthFrame() (int, error) { | ||
for { | ||
if _, err := io.ReadFull(reader.fifoPipe, reader.lenFrameBuf); err != nil { | ||
return 0, err | ||
} | ||
bodyLen := int(reader.byteOrder.Uint32(reader.lenFrameBuf)) | ||
if bodyLen > 0 { | ||
return bodyLen, nil | ||
} | ||
} | ||
} | ||
|
||
// Close closes the reader and underlying pipe | ||
func (reader *PipeReader) Close() error { | ||
return reader.fifoPipe.Close() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
consider calling
NewReaderFromReadCloser