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

[6.2] Filebeat: Fix leak in log harvester (#6797) #7132

Merged
merged 1 commit into from
May 24, 2018
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
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ https://github.com/elastic/beats/compare/v6.2.4...6.2[Check the HEAD diff]

*Filebeat*

- Fix memory leak in log prospector when files cannot be read. {issue}6797[6797]

*Heartbeat*
- Fix race due to updates of shared a map, that was not supposed to be shared between multiple go-routines. {issue}6616[6616]

Expand Down
36 changes: 21 additions & 15 deletions filebeat/prospector/log/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ var (
ErrClosed = errors.New("reader closed")
)

// OutletFactory provides an outlet for the harvester
type OutletFactory func() channel.Outleter

// Harvester contains all harvester related data
type Harvester struct {
id uuid.UUID
Expand All @@ -74,8 +77,8 @@ type Harvester struct {
encoding encoding.Encoding

// event/state publishing
forwarder *harvester.Forwarder
publishState func(*util.Data) bool
outletFactory OutletFactory
publishState func(*util.Data) bool

onTerminate func()
}
Expand All @@ -86,17 +89,18 @@ func NewHarvester(
state file.State,
states *file.States,
publishState func(*util.Data) bool,
outlet channel.Outleter,
outletFactory OutletFactory,
) (*Harvester, error) {

h := &Harvester{
config: defaultConfig,
state: state,
states: states,
publishState: publishState,
done: make(chan struct{}),
stopWg: &sync.WaitGroup{},
id: uuid.NewV4(),
config: defaultConfig,
state: state,
states: states,
publishState: publishState,
done: make(chan struct{}),
stopWg: &sync.WaitGroup{},
id: uuid.NewV4(),
outletFactory: outletFactory,
}

if err := config.Unpack(&h.config); err != nil {
Expand All @@ -115,8 +119,6 @@ func NewHarvester(
}

// Add outlet signal so harvester can also stop itself
outlet = channel.CloseOnSignal(outlet, h.done)
h.forwarder = harvester.NewForwarder(outlet)
return h, nil
}

Expand Down Expand Up @@ -163,6 +165,10 @@ func (h *Harvester) Run() error {
if h.onTerminate != nil {
defer h.onTerminate()
}

outlet := channel.CloseOnSignal(h.outletFactory(), h.done)
forwarder := harvester.NewForwarder(outlet)

// This is to make sure a harvester is not started anymore if stop was already
// called before the harvester was started. The waitgroup is not incremented afterwards
// as otherwise it could happened that between checking for the close channel and incrementing
Expand Down Expand Up @@ -302,7 +308,7 @@ func (h *Harvester) Run() error {

// Always send event to update state, also if lines was skipped
// Stop harvester in case of an error
if !h.sendEvent(data) {
if !h.sendEvent(data, forwarder) {
return nil
}

Expand All @@ -326,12 +332,12 @@ func (h *Harvester) Stop() {

// sendEvent sends event to the spooler channel
// Return false if event was not sent
func (h *Harvester) sendEvent(data *util.Data) bool {
func (h *Harvester) sendEvent(data *util.Data, forwarder *harvester.Forwarder) bool {
if h.source.HasState() {
h.states.Update(data.GetState())
}

err := h.forwarder.Send(data)
err := forwarder.Send(data)
return err == nil
}

Expand Down
15 changes: 13 additions & 2 deletions filebeat/prospector/log/prospector.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,18 +572,29 @@ func (p *Prospector) isCleanInactive(state file.State) bool {
return false
}

// subOutletWrap returns a factory method that will wrap the passed outlet
// in a SubOutlet and memoize the result so the wrapping is done only once.
func subOutletWrap(outlet channel.Outleter) func() channel.Outleter {
var subOutlet channel.Outleter
return func() channel.Outleter {
if subOutlet == nil {
subOutlet = channel.SubOutlet(outlet)
}
return subOutlet
}
}

// createHarvester creates a new harvester instance from the given state
func (p *Prospector) createHarvester(state file.State, onTerminate func()) (*Harvester, error) {
// Each wraps the outlet, for closing the outlet individually
outlet := channel.SubOutlet(p.outlet)
h, err := NewHarvester(
p.cfg,
state,
p.states,
func(d *util.Data) bool {
return p.stateOutlet.OnEvent(d)
},
outlet,
subOutletWrap(p.outlet),
)
if err == nil {
h.onTerminate = onTerminate
Expand Down
4 changes: 3 additions & 1 deletion filebeat/prospector/stdin/prospector.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (p *Prospector) createHarvester(state file.State) (*log.Harvester, error) {
h, err := log.NewHarvester(
p.cfg,
state, nil, nil,
p.outlet,
func() channel.Outleter {
return p.outlet
},
)

return h, err
Expand Down