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

Filebeat: Fix leak in log harvester (#6797) #6829

Merged
merged 1 commit into from
Apr 13, 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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
*Filebeat*

- Fix panic when log prospector configuration fails to load. {issue}6800[6800]
- Fix memory leak in log prospector when files cannot be read. {issue}6797[6797]

*Heartbeat*

Expand Down
36 changes: 21 additions & 15 deletions filebeat/input/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 @@ -75,8 +78,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 @@ -87,17 +90,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 @@ -116,8 +120,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 @@ -164,6 +166,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 @@ -308,7 +314,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 @@ -335,12 +341,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/input/log/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,18 +572,29 @@ func (p *Input) 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 *Input) 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/input/stdin/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ func (p *Input) 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