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

Fix race condition for harvester Start / Stop in registry #4314

Merged
merged 1 commit into from
May 15, 2017
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 @@ -31,6 +31,7 @@ https://github.com/elastic/beats/compare/v6.0.0-alpha1...master[Check the HEAD d
- Fix importing the dashboards when the limit for max open files is too low. {issue}4244[4244]

*Filebeat*
- Fix race condition on harvester stopping with reloading enabled. {issue}3779[3779]

*Heartbeat*

Expand Down
69 changes: 41 additions & 28 deletions filebeat/harvester/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,78 @@ type Registry struct {
sync.RWMutex
harvesters map[uuid.UUID]Harvester
wg sync.WaitGroup
done chan struct{}
}

// NewRegistry creates a new registry object
func NewRegistry() *Registry {
return &Registry{
harvesters: map[uuid.UUID]Harvester{},
done: make(chan struct{}),
}
}

func (hr *Registry) add(h Harvester) {
hr.Lock()
defer hr.Unlock()
hr.harvesters[h.ID()] = h
}

func (hr *Registry) remove(h Harvester) {
hr.Lock()
defer hr.Unlock()
delete(hr.harvesters, h.ID())
func (r *Registry) remove(h Harvester) {
r.Lock()
defer r.Unlock()
delete(r.harvesters, h.ID())
}

// Stop stops all harvesters in the registry
func (hr *Registry) Stop() {
hr.Lock()
for _, hv := range hr.harvesters {
hr.wg.Add(1)
func (r *Registry) Stop() {
r.Lock()
defer func() {
r.Unlock()
r.WaitForCompletion()
}()
// Makes sure no new harvesters are added during stopping
close(r.done)

for _, hv := range r.harvesters {
r.wg.Add(1)
go func(h Harvester) {
hr.wg.Done()
r.wg.Done()
h.Stop()
}(hv)
}
hr.Unlock()
hr.WaitForCompletion()

}

// WaitForCompletion can be used to wait until all harvesters are stopped
func (hr *Registry) WaitForCompletion() {
hr.wg.Wait()
func (r *Registry) WaitForCompletion() {
r.wg.Wait()
}

// Start starts the given harvester and add its to the registry
func (hr *Registry) Start(h Harvester) {
func (r *Registry) Start(h Harvester) {

// Make sure stop is not called during starting a harvester
r.Lock()
defer r.Unlock()

// Make sure no new harvesters are started after stop was called
select {
case <-r.done:
return
default:
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Having that you already have a lock in place done could be just a boolean isn't it? not against this approach though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, it could be also a bool. I would like to keep it that way as it has become quite a common pattern across our code base.


hr.wg.Add(1)
hr.add(h)
r.wg.Add(1)
r.harvesters[h.ID()] = h
Copy link
Contributor

Choose a reason for hiding this comment

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

You are no longer using add function, I would either use it or remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the method.


go func() {
defer func() {
hr.remove(h)
hr.wg.Done()
r.remove(h)
r.wg.Done()
}()
// Starts harvester and picks the right type. In case type is not set, set it to default (log)
h.Start()
}()
}

// Len returns the current number of harvesters in the registry
func (hr *Registry) Len() uint64 {
hr.RLock()
defer hr.RUnlock()
return uint64(len(hr.harvesters))
func (r *Registry) Len() uint64 {
r.RLock()
defer r.RUnlock()
return uint64(len(r.harvesters))
}